blob: da10568b3f394361486b534017053695cb25b851 [file] [log] [blame]
Michal Vasko00cbf532020-06-15 13:58:47 +02001/**
Radek Krejcif0e1ba52020-05-22 15:14:35 +02002 * @file test_inout.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for input and output handlers functions
5 *
Michal Vasko00cbf532020-06-15 13:58:47 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Radek Krejcif0e1ba52020-05-22 15:14:35 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020016
Radek Krejcif0e1ba52020-05-22 15:14:35 +020017#include <errno.h>
18#include <fcntl.h>
Radek Krejci70593c12020-06-13 20:48:09 +020019#include <stdio.h>
Radek Krejcif0e1ba52020-05-22 15:14:35 +020020#include <sys/stat.h>
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include <sys/types.h>
Radek Krejcif0e1ba52020-05-22 15:14:35 +020022#include <unistd.h>
23
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "common.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010025#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "out.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020028
29static void
Radek Iša56ca9e42020-09-08 18:42:00 +020030test_input_mem(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +020031{
32 struct ly_in *in = NULL;
33 char *str1 = "a", *str2 = "b";
34
Radek Krejcif0e1ba52020-05-22 15:14:35 +020035 assert_int_equal(LY_EINVAL, ly_in_new_memory(NULL, NULL));
36 assert_int_equal(LY_EINVAL, ly_in_new_memory(str1, NULL));
37 assert_null(ly_in_memory(NULL, NULL));
38
39 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in));
40 assert_int_equal(LY_IN_MEMORY, ly_in_type(in));
41 assert_ptr_equal(str1, ly_in_memory(in, str2));
42 assert_ptr_equal(str2, ly_in_memory(in, NULL));
43 assert_ptr_equal(str2, ly_in_memory(in, NULL));
44 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020045}
46
47static void
Radek Iša56ca9e42020-09-08 18:42:00 +020048test_input_fd(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +020049{
50 struct ly_in *in = NULL;
51 int fd1, fd2;
52 struct stat statbuf;
53
Radek Krejcif0e1ba52020-05-22 15:14:35 +020054 assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL));
55 assert_int_equal(-1, ly_in_fd(NULL, -1));
56
57 assert_int_not_equal(-1, fd1 = open(__FILE__, O_RDONLY));
58 assert_int_not_equal(-1, fd2 = open(__FILE__, O_RDONLY));
59
60 assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL));
61
62 assert_int_equal(LY_SUCCESS, ly_in_new_fd(fd1, &in));
63 assert_int_equal(LY_IN_FD, ly_in_type(in));
64 assert_ptr_equal(fd1, ly_in_fd(in, fd2));
65 assert_ptr_equal(fd2, ly_in_fd(in, -1));
66 assert_ptr_equal(fd2, ly_in_fd(in, -1));
67 ly_in_free(in, 1);
68 /* fd1 is still open */
69 assert_int_equal(0, fstat(fd1, &statbuf));
70 close(fd1);
Jan Kundrátb1568032021-12-13 20:06:17 +010071#ifndef _WIN32
72 /* But fd2 was closed by ly_in_free(). This results in an "invalid handler" on Windows. */
Radek Krejcif0e1ba52020-05-22 15:14:35 +020073 errno = 0;
74 assert_int_equal(-1, fstat(fd2, &statbuf));
75 assert_int_equal(errno, EBADF);
Jan Kundrátb1568032021-12-13 20:06:17 +010076#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +020077}
78
79static void
Radek Iša56ca9e42020-09-08 18:42:00 +020080test_input_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +020081{
82 struct ly_in *in = NULL;
83 FILE *f1 = NULL, *f2 = NULL;
84
Radek Krejcif0e1ba52020-05-22 15:14:35 +020085 assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL));
86 assert_null(ly_in_file(NULL, NULL));
87
Jan Kundrát79ad0142021-12-13 15:16:47 +010088 assert_int_not_equal(-1, f1 = fopen(__FILE__, "rb"));
89 assert_int_not_equal(-1, f2 = fopen(__FILE__, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +020090
91 assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL));
92
93 assert_int_equal(LY_SUCCESS, ly_in_new_file(f1, &in));
94 assert_int_equal(LY_IN_FILE, ly_in_type(in));
95 assert_ptr_equal(f1, ly_in_file(in, f2));
96 assert_ptr_equal(f2, ly_in_file(in, NULL));
97 assert_ptr_equal(f2, ly_in_file(in, NULL));
98 ly_in_free(in, 1);
99 /* f1 is still open */
100 assert_int_not_equal(-1, fileno(f1));
101 fclose(f1);
102 /* but f2 was closed by ly_in_free() */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200103}
104
105static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200106test_input_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200107{
108 struct ly_in *in = NULL;
109 const char *path1 = __FILE__, *path2 = __FILE__;
110
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200111 assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL));
112 assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL));
113 assert_ptr_equal(((void *)-1), ly_in_filepath(NULL, NULL, 0));
114
115 assert_int_equal(LY_SUCCESS, ly_in_new_filepath(path1, 0, &in));
116 assert_int_equal(LY_IN_FILEPATH, ly_in_type(in));
117 assert_ptr_equal(NULL, ly_in_filepath(in, path2, 0));
118 assert_string_equal(path2, ly_in_filepath(in, NULL, 0));
119 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200120}
121
122static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200123test_output_mem(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200124{
125 struct ly_out *out = NULL;
126 char *buf1 = NULL, *buf2 = NULL;
127
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200128 /* manipulate with the handler */
129 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
130 assert_int_equal(LY_OUT_MEMORY, ly_out_type(out));
131 ly_write(out, "test", 4);
132 assert_ptr_equal(buf1, ly_out_memory(out, &buf2, 0));
133 assert_ptr_equal(buf2, ly_out_memory(out, NULL, 0));
134 assert_ptr_equal(buf2, ly_out_memory(out, &buf1, strlen(buf1)));
135 ly_out_free(out, NULL, 0);
136
137 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, strlen(buf1), &out));
138 ly_out_free(out, NULL, 1);
139
140 /* writing data */
141
142 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200143 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
144 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200145 assert_string_equal("test print", buf1);
146 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
Michal Vasko5233e962020-08-14 14:26:20 +0200147 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
148 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200149 assert_string_equal("rewrite", buf1);
150 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200151}
152
153static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200154test_output_fd(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200155{
156 struct ly_out *out = NULL;
157 int fd1, fd2;
158 char buf[31] = {0};
Jan Kundrát9b7ca332021-12-13 20:51:44 +0100159 const char *filepath = TESTS_BIN "/libyang_test_output";
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200160
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200161 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
162 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
163
164 /* manipulate with the handler */
165 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
166 assert_int_equal(LY_OUT_FD, ly_out_type(out));
167 assert_ptr_equal(fd1, ly_out_fd(out, fd2));
168 assert_ptr_equal(fd2, ly_out_fd(out, -1));
169 assert_ptr_equal(fd2, ly_out_fd(out, fd1));
170 ly_out_free(out, NULL, 0);
171 assert_int_equal(0, close(fd2));
172 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
173 ly_out_free(out, NULL, 1);
174
175 /* writing data */
176 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
177 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
178 /* truncate file to start with no data */
179 assert_int_equal(0, ftruncate(fd1, 0));
180
181 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200182 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
183 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200184 ly_print_flush(out);
185 assert_int_equal(10, read(fd2, buf, 30));
186 assert_string_equal("test print", buf);
187 assert_int_equal(0, lseek(fd2, 0, SEEK_SET));
188 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
189
Michal Vasko5233e962020-08-14 14:26:20 +0200190 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
191 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200192 ly_print_flush(out);
193 assert_int_equal(8, read(fd2, buf, 30));
194 assert_string_equal("rewrite", buf);
195
196 close(fd2);
197 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200198}
199
200static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200201test_output_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200202{
203 struct ly_out *out = NULL;
204 FILE *f1, *f2;
205 char buf[31] = {0};
Jan Kundrát9b7ca332021-12-13 20:51:44 +0100206 const char *filepath = TESTS_BIN "/libyang_test_output";
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200207
Jan Kundrát79ad0142021-12-13 15:16:47 +0100208 assert_int_not_equal(-1, f1 = fopen(filepath, "wb"));
209 assert_int_not_equal(-1, f2 = fopen(filepath, "wb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200210
211 /* manipulate with the handler */
212 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
213 assert_int_equal(LY_OUT_FILE, ly_out_type(out));
214 assert_ptr_equal(f1, ly_out_file(out, f2));
215 assert_ptr_equal(f2, ly_out_file(out, NULL));
216 assert_ptr_equal(f2, ly_out_file(out, f1));
217 ly_out_free(out, NULL, 0);
218 assert_int_equal(0, fclose(f2));
219 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
220 ly_out_free(out, NULL, 1);
221
222 /* writing data */
Jan Kundrát79ad0142021-12-13 15:16:47 +0100223 assert_int_not_equal(-1, f1 = fopen(filepath, "wb"));
224 assert_int_not_equal(-1, f2 = fopen(filepath, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200225
226 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200227 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
228 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200229 ly_print_flush(out);
230 assert_non_null(fgets(buf, 31, f2));
231 assert_string_equal("test print", buf);
232 assert_int_equal(0, fseek(f2, 0, SEEK_SET));
233 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
234
Michal Vasko5233e962020-08-14 14:26:20 +0200235 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
236 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200237 ly_print_flush(out);
238 assert_non_null(fgets(buf, 31, f2));
239 assert_string_equal("rewrite", buf);
240
241 fclose(f2);
242 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200243}
244
245static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200246test_output_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200247{
248 struct ly_out *out = NULL;
249 FILE *f1;
250 char buf[31] = {0};
Jan Kundrát9b7ca332021-12-13 20:51:44 +0100251 const char *fp1 = TESTS_BIN "/libyang_test_output";
252 const char *fp2 = TESTS_BIN "/libyang_test_output2";
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200253
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200254 /* manipulate with the handler */
255 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
256 assert_int_equal(LY_OUT_FILEPATH, ly_out_type(out));
257 assert_ptr_equal(NULL, ly_out_filepath(out, fp2));
258 assert_string_equal(fp2, ly_out_filepath(out, NULL));
259 assert_ptr_equal(NULL, ly_out_filepath(out, fp1));
260 ly_out_free(out, NULL, 0);
261 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
262 ly_out_free(out, NULL, 1);
263
264 /* writing data */
Jan Kundrát79ad0142021-12-13 15:16:47 +0100265 assert_int_not_equal(-1, f1 = fopen(fp1, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200266
267 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200268 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
269 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200270 ly_print_flush(out);
271 assert_non_null(fgets(buf, 31, f1));
272 assert_string_equal("test print", buf);
273 assert_int_equal(0, fseek(f1, 0, SEEK_SET));
274 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
275
Michal Vasko5233e962020-08-14 14:26:20 +0200276 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
277 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200278 ly_print_flush(out);
279 assert_non_null(fgets(buf, 31, f1));
280 assert_string_equal("rewrite", buf);
281
282 fclose(f1);
283 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200284}
285
Michal Vasko8759f3c2021-09-22 12:19:43 +0200286static ssize_t
287write_clb(void *user_data, const void *buf, size_t count)
288{
289 return write((uintptr_t)user_data, buf, count);
290}
291
292void
293close_clb(void *arg)
294{
295 close((uintptr_t)arg);
296}
297
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200298static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200299test_output_clb(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200300{
301 struct ly_out *out = NULL;
302 int fd1, fd2;
303 char buf[31] = {0};
Jan Kundrát9b7ca332021-12-13 20:51:44 +0100304 const char *filepath = TESTS_BIN "/libyang_test_output";
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200305
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200306 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
307 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
308
309 /* manipulate with the handler */
Michal Vasko8759f3c2021-09-22 12:19:43 +0200310 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200311 assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100312 assert_ptr_equal(fd1, ly_out_clb_arg(out, (void *)(intptr_t)fd2));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200313 assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100314 assert_ptr_equal(fd2, ly_out_clb_arg(out, (void *)(intptr_t)fd1));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200315 assert_ptr_equal(write_clb, ly_out_clb(out, write_clb));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200316 ly_out_free(out, NULL, 0);
317 assert_int_equal(0, close(fd2));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200318 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
319 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200320
321 /* writing data */
322 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
323 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
324 /* truncate file to start with no data */
325 assert_int_equal(0, ftruncate(fd1, 0));
326
Michal Vasko8759f3c2021-09-22 12:19:43 +0200327 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200328 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
329 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200330 assert_int_equal(10, read(fd2, buf, 30));
331 assert_string_equal("test print", buf);
332
333 close(fd2);
Michal Vasko8759f3c2021-09-22 12:19:43 +0200334 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200335}
336
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100337int
338main(void)
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200339{
340 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200341 UTEST(test_input_mem),
342 UTEST(test_input_fd),
343 UTEST(test_input_file),
344 UTEST(test_input_filepath),
345 UTEST(test_output_mem),
346 UTEST(test_output_fd),
347 UTEST(test_output_file),
348 UTEST(test_output_filepath),
349 UTEST(test_output_clb),
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200350 };
351
352 return cmocka_run_group_tests(tests, NULL, NULL);
353}