blob: f0f796897c8b760d50f932307f40cd34c01221f4 [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);
71 /* but fd2 was closed by ly_in_free() */
72 errno = 0;
73 assert_int_equal(-1, fstat(fd2, &statbuf));
74 assert_int_equal(errno, EBADF);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020075}
76
77static void
Radek Iša56ca9e42020-09-08 18:42:00 +020078test_input_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +020079{
80 struct ly_in *in = NULL;
81 FILE *f1 = NULL, *f2 = NULL;
82
Radek Krejcif0e1ba52020-05-22 15:14:35 +020083 assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL));
84 assert_null(ly_in_file(NULL, NULL));
85
86 assert_int_not_equal(-1, f1 = fopen(__FILE__, "r"));
87 assert_int_not_equal(-1, f2 = fopen(__FILE__, "r"));
88
89 assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL));
90
91 assert_int_equal(LY_SUCCESS, ly_in_new_file(f1, &in));
92 assert_int_equal(LY_IN_FILE, ly_in_type(in));
93 assert_ptr_equal(f1, ly_in_file(in, f2));
94 assert_ptr_equal(f2, ly_in_file(in, NULL));
95 assert_ptr_equal(f2, ly_in_file(in, NULL));
96 ly_in_free(in, 1);
97 /* f1 is still open */
98 assert_int_not_equal(-1, fileno(f1));
99 fclose(f1);
100 /* but f2 was closed by ly_in_free() */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200101}
102
103static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200104test_input_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200105{
106 struct ly_in *in = NULL;
107 const char *path1 = __FILE__, *path2 = __FILE__;
108
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200109 assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL));
110 assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL));
111 assert_ptr_equal(((void *)-1), ly_in_filepath(NULL, NULL, 0));
112
113 assert_int_equal(LY_SUCCESS, ly_in_new_filepath(path1, 0, &in));
114 assert_int_equal(LY_IN_FILEPATH, ly_in_type(in));
115 assert_ptr_equal(NULL, ly_in_filepath(in, path2, 0));
116 assert_string_equal(path2, ly_in_filepath(in, NULL, 0));
117 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200118}
119
120static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200121test_output_mem(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200122{
123 struct ly_out *out = NULL;
124 char *buf1 = NULL, *buf2 = NULL;
125
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200126 /* manipulate with the handler */
127 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
128 assert_int_equal(LY_OUT_MEMORY, ly_out_type(out));
129 ly_write(out, "test", 4);
130 assert_ptr_equal(buf1, ly_out_memory(out, &buf2, 0));
131 assert_ptr_equal(buf2, ly_out_memory(out, NULL, 0));
132 assert_ptr_equal(buf2, ly_out_memory(out, &buf1, strlen(buf1)));
133 ly_out_free(out, NULL, 0);
134
135 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, strlen(buf1), &out));
136 ly_out_free(out, NULL, 1);
137
138 /* writing data */
139
140 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200141 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
142 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200143 assert_string_equal("test print", buf1);
144 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
Michal Vasko5233e962020-08-14 14:26:20 +0200145 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
146 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200147 assert_string_equal("rewrite", buf1);
148 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200149}
150
151static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200152test_output_fd(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200153{
154 struct ly_out *out = NULL;
155 int fd1, fd2;
156 char buf[31] = {0};
157 const char *filepath = "/tmp/libyang_test_output";
158
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200159 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
160 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
161
162 /* manipulate with the handler */
163 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
164 assert_int_equal(LY_OUT_FD, ly_out_type(out));
165 assert_ptr_equal(fd1, ly_out_fd(out, fd2));
166 assert_ptr_equal(fd2, ly_out_fd(out, -1));
167 assert_ptr_equal(fd2, ly_out_fd(out, fd1));
168 ly_out_free(out, NULL, 0);
169 assert_int_equal(0, close(fd2));
170 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
171 ly_out_free(out, NULL, 1);
172
173 /* writing data */
174 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
175 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
176 /* truncate file to start with no data */
177 assert_int_equal(0, ftruncate(fd1, 0));
178
179 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200180 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
181 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200182 ly_print_flush(out);
183 assert_int_equal(10, read(fd2, buf, 30));
184 assert_string_equal("test print", buf);
185 assert_int_equal(0, lseek(fd2, 0, SEEK_SET));
186 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
187
Michal Vasko5233e962020-08-14 14:26:20 +0200188 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
189 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200190 ly_print_flush(out);
191 assert_int_equal(8, read(fd2, buf, 30));
192 assert_string_equal("rewrite", buf);
193
194 close(fd2);
195 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200196}
197
198static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200199test_output_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200200{
201 struct ly_out *out = NULL;
202 FILE *f1, *f2;
203 char buf[31] = {0};
204 const char *filepath = "/tmp/libyang_test_output";
205
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200206 assert_int_not_equal(-1, f1 = fopen(filepath, "w"));
207 assert_int_not_equal(-1, f2 = fopen(filepath, "w"));
208
209 /* manipulate with the handler */
210 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
211 assert_int_equal(LY_OUT_FILE, ly_out_type(out));
212 assert_ptr_equal(f1, ly_out_file(out, f2));
213 assert_ptr_equal(f2, ly_out_file(out, NULL));
214 assert_ptr_equal(f2, ly_out_file(out, f1));
215 ly_out_free(out, NULL, 0);
216 assert_int_equal(0, fclose(f2));
217 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
218 ly_out_free(out, NULL, 1);
219
220 /* writing data */
221 assert_int_not_equal(-1, f1 = fopen(filepath, "w"));
222 assert_int_not_equal(-1, f2 = fopen(filepath, "r"));
223
224 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200225 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
226 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200227 ly_print_flush(out);
228 assert_non_null(fgets(buf, 31, f2));
229 assert_string_equal("test print", buf);
230 assert_int_equal(0, fseek(f2, 0, SEEK_SET));
231 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
232
Michal Vasko5233e962020-08-14 14:26:20 +0200233 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
234 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200235 ly_print_flush(out);
236 assert_non_null(fgets(buf, 31, f2));
237 assert_string_equal("rewrite", buf);
238
239 fclose(f2);
240 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200241}
242
243static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200244test_output_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200245{
246 struct ly_out *out = NULL;
247 FILE *f1;
248 char buf[31] = {0};
249 const char *fp1 = "/tmp/libyang_test_output";
250 const char *fp2 = "/tmp/libyang_test_output2";
251
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200252 /* manipulate with the handler */
253 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
254 assert_int_equal(LY_OUT_FILEPATH, ly_out_type(out));
255 assert_ptr_equal(NULL, ly_out_filepath(out, fp2));
256 assert_string_equal(fp2, ly_out_filepath(out, NULL));
257 assert_ptr_equal(NULL, ly_out_filepath(out, fp1));
258 ly_out_free(out, NULL, 0);
259 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
260 ly_out_free(out, NULL, 1);
261
262 /* writing data */
263 assert_int_not_equal(-1, f1 = fopen(fp1, "r"));
264
265 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200266 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
267 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200268 ly_print_flush(out);
269 assert_non_null(fgets(buf, 31, f1));
270 assert_string_equal("test print", buf);
271 assert_int_equal(0, fseek(f1, 0, SEEK_SET));
272 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
273
Michal Vasko5233e962020-08-14 14:26:20 +0200274 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
275 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200276 ly_print_flush(out);
277 assert_non_null(fgets(buf, 31, f1));
278 assert_string_equal("rewrite", buf);
279
280 fclose(f1);
281 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200282}
283
284static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200285test_output_clb(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200286{
287 struct ly_out *out = NULL;
288 int fd1, fd2;
289 char buf[31] = {0};
290 const char *filepath = "/tmp/libyang_test_output";
291
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200292 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
293 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
294
295 /* manipulate with the handler */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100296 assert_int_equal(LY_SUCCESS, ly_out_new_clb((void *)write, (void *)(intptr_t)fd1, &out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200297 assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100298 assert_ptr_equal(fd1, ly_out_clb_arg(out, (void *)(intptr_t)fd2));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200299 assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100300 assert_ptr_equal(fd2, ly_out_clb_arg(out, (void *)(intptr_t)fd1));
Michal Vaskoed213e12020-10-15 11:12:35 +0200301 assert_ptr_equal(write, ly_out_clb(out, (void *)write));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200302 ly_out_free(out, NULL, 0);
303 assert_int_equal(0, close(fd2));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100304 assert_int_equal(LY_SUCCESS, ly_out_new_clb((void *)write, (void *)(intptr_t)fd1, &out));
Michal Vaskoed213e12020-10-15 11:12:35 +0200305 ly_out_free(out, (void *)close, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200306
307 /* writing data */
308 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
309 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
310 /* truncate file to start with no data */
311 assert_int_equal(0, ftruncate(fd1, 0));
312
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100313 assert_int_equal(LY_SUCCESS, ly_out_new_clb((void *)write, (void *)(intptr_t)fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200314 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
315 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200316 assert_int_equal(10, read(fd2, buf, 30));
317 assert_string_equal("test print", buf);
318
319 close(fd2);
Michal Vaskoed213e12020-10-15 11:12:35 +0200320 ly_out_free(out, (void *)close, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200321}
322
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100323int
324main(void)
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200325{
326 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200327 UTEST(test_input_mem),
328 UTEST(test_input_fd),
329 UTEST(test_input_file),
330 UTEST(test_input_filepath),
331 UTEST(test_output_mem),
332 UTEST(test_output_fd),
333 UTEST(test_output_file),
334 UTEST(test_output_filepath),
335 UTEST(test_output_clb),
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200336 };
337
338 return cmocka_run_group_tests(tests, NULL, NULL);
339}