blob: d2455fd3185ee55cd2326d7425cfc6905c5bd1e7 [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
Michal Vasko8759f3c2021-09-22 12:19:43 +0200284static ssize_t
285write_clb(void *user_data, const void *buf, size_t count)
286{
287 return write((uintptr_t)user_data, buf, count);
288}
289
290void
291close_clb(void *arg)
292{
293 close((uintptr_t)arg);
294}
295
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200296static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200297test_output_clb(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200298{
299 struct ly_out *out = NULL;
300 int fd1, fd2;
301 char buf[31] = {0};
302 const char *filepath = "/tmp/libyang_test_output";
303
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200304 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
305 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
306
307 /* manipulate with the handler */
Michal Vasko8759f3c2021-09-22 12:19:43 +0200308 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200309 assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100310 assert_ptr_equal(fd1, ly_out_clb_arg(out, (void *)(intptr_t)fd2));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200311 assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100312 assert_ptr_equal(fd2, ly_out_clb_arg(out, (void *)(intptr_t)fd1));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200313 assert_ptr_equal(write_clb, ly_out_clb(out, write_clb));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200314 ly_out_free(out, NULL, 0);
315 assert_int_equal(0, close(fd2));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200316 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
317 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200318
319 /* writing data */
320 assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
321 assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
322 /* truncate file to start with no data */
323 assert_int_equal(0, ftruncate(fd1, 0));
324
Michal Vasko8759f3c2021-09-22 12:19:43 +0200325 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200326 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
327 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200328 assert_int_equal(10, read(fd2, buf, 30));
329 assert_string_equal("test print", buf);
330
331 close(fd2);
Michal Vasko8759f3c2021-09-22 12:19:43 +0200332 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200333}
334
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100335int
336main(void)
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200337{
338 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 UTEST(test_input_mem),
340 UTEST(test_input_fd),
341 UTEST(test_input_file),
342 UTEST(test_input_filepath),
343 UTEST(test_output_mem),
344 UTEST(test_output_fd),
345 UTEST(test_output_file),
346 UTEST(test_output_filepath),
347 UTEST(test_output_clb),
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200348 };
349
350 return cmocka_run_group_tests(tests, NULL, NULL);
351}