blob: 3f83568dbf5897fb42b265bb644b2f7ed6b251e9 [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
Michal Vaskode2e8b22022-01-19 09:13:38 +010029#define TEST_INPUT_FILE TESTS_BIN "/libyang_test_input"
30#define TEST_OUTPUT_FILE TESTS_BIN "/libyang_test_output"
31#define TEST_OUTPUT_FILE2 TESTS_BIN "/libyang_test_output2"
32
33static int
34setup_files(void **state)
35{
36 int fd;
37
38 UTEST_SETUP;
39
40 /* create input */
41 fd = open(TEST_INPUT_FILE, O_CREAT | O_WRONLY, 00600);
42 if (fd == -1) {
43 return 1;
44 }
45
46 /* write something */
47 if (write(fd, "data", 4) != 4) {
48 return 1;
49 }
50 close(fd);
51
52 /* create output */
53 fd = open(TEST_OUTPUT_FILE, O_CREAT | O_RDONLY, 00600);
54 if (fd == -1) {
55 return 1;
56 }
57 close(fd);
58
59 /* create output2 */
60 fd = open(TEST_OUTPUT_FILE2, O_CREAT | O_RDONLY, 00600);
61 if (fd == -1) {
62 return 1;
63 }
64 close(fd);
65
66 return 0;
67}
68
69static int
70teardown_files(void **state)
71{
72 unlink(TEST_INPUT_FILE);
73 unlink(TEST_OUTPUT_FILE);
74 unlink(TEST_OUTPUT_FILE2);
75
76 UTEST_TEARDOWN;
77 return 0;
78}
79
Radek Krejcif0e1ba52020-05-22 15:14:35 +020080static void
Radek Iša56ca9e42020-09-08 18:42:00 +020081test_input_mem(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +020082{
83 struct ly_in *in = NULL;
84 char *str1 = "a", *str2 = "b";
85
Radek Krejcif0e1ba52020-05-22 15:14:35 +020086 assert_int_equal(LY_EINVAL, ly_in_new_memory(NULL, NULL));
Michal Vasko236cbac2023-02-10 15:45:37 +010087 CHECK_LOG_LASTMSG("Invalid argument str (ly_in_new_memory()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +020088 assert_int_equal(LY_EINVAL, ly_in_new_memory(str1, NULL));
Michal Vasko236cbac2023-02-10 15:45:37 +010089 CHECK_LOG_LASTMSG("Invalid argument in (ly_in_new_memory()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +020090 assert_null(ly_in_memory(NULL, NULL));
Michal Vasko236cbac2023-02-10 15:45:37 +010091 CHECK_LOG_LASTMSG("Invalid argument in (ly_in_memory()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +020092
93 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in));
94 assert_int_equal(LY_IN_MEMORY, ly_in_type(in));
95 assert_ptr_equal(str1, ly_in_memory(in, str2));
96 assert_ptr_equal(str2, ly_in_memory(in, NULL));
97 assert_ptr_equal(str2, ly_in_memory(in, NULL));
98 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +020099}
100
101static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200102test_input_fd(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200103{
104 struct ly_in *in = NULL;
105 int fd1, fd2;
106 struct stat statbuf;
107
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200108 assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL));
Michal Vasko236cbac2023-02-10 15:45:37 +0100109 CHECK_LOG_LASTMSG("Invalid argument fd >= 0 (ly_in_new_fd()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200110 assert_int_equal(-1, ly_in_fd(NULL, -1));
Michal Vasko236cbac2023-02-10 15:45:37 +0100111 CHECK_LOG_LASTMSG("Invalid argument in (ly_in_fd()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200112
Michal Vaskode2e8b22022-01-19 09:13:38 +0100113 assert_int_not_equal(-1, fd1 = open(TEST_INPUT_FILE, O_RDONLY));
114 assert_int_not_equal(-1, fd2 = open(TEST_INPUT_FILE, O_RDONLY));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200115
116 assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL));
Michal Vasko236cbac2023-02-10 15:45:37 +0100117 CHECK_LOG_LASTMSG("Invalid argument in (ly_in_new_fd()).");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200118
119 assert_int_equal(LY_SUCCESS, ly_in_new_fd(fd1, &in));
120 assert_int_equal(LY_IN_FD, ly_in_type(in));
121 assert_ptr_equal(fd1, ly_in_fd(in, fd2));
122 assert_ptr_equal(fd2, ly_in_fd(in, -1));
123 assert_ptr_equal(fd2, ly_in_fd(in, -1));
124 ly_in_free(in, 1);
125 /* fd1 is still open */
126 assert_int_equal(0, fstat(fd1, &statbuf));
127 close(fd1);
Jan Kundrátd1047672021-12-13 20:06:17 +0100128#ifndef _WIN32
129 /* But fd2 was closed by ly_in_free(). This results in an "invalid handler" on Windows. */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200130 errno = 0;
131 assert_int_equal(-1, fstat(fd2, &statbuf));
132 assert_int_equal(errno, EBADF);
Jan Kundrátd1047672021-12-13 20:06:17 +0100133#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200134}
135
136static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200137test_input_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200138{
139 struct ly_in *in = NULL;
140 FILE *f1 = NULL, *f2 = NULL;
141
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200142 assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL));
143 assert_null(ly_in_file(NULL, NULL));
144
Michal Vaskode2e8b22022-01-19 09:13:38 +0100145 assert_non_null(f1 = fopen(TEST_INPUT_FILE, "rb"));
146 assert_non_null(f2 = fopen(TEST_INPUT_FILE, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200147
148 assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL));
149
150 assert_int_equal(LY_SUCCESS, ly_in_new_file(f1, &in));
151 assert_int_equal(LY_IN_FILE, ly_in_type(in));
152 assert_ptr_equal(f1, ly_in_file(in, f2));
153 assert_ptr_equal(f2, ly_in_file(in, NULL));
154 assert_ptr_equal(f2, ly_in_file(in, NULL));
155 ly_in_free(in, 1);
156 /* f1 is still open */
157 assert_int_not_equal(-1, fileno(f1));
158 fclose(f1);
159 /* but f2 was closed by ly_in_free() */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200160}
161
162static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200163test_input_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200164{
165 struct ly_in *in = NULL;
Michal Vaskode2e8b22022-01-19 09:13:38 +0100166 const char *path1 = TEST_INPUT_FILE, *path2 = TEST_INPUT_FILE;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200167
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200168 assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL));
169 assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL));
170 assert_ptr_equal(((void *)-1), ly_in_filepath(NULL, NULL, 0));
171
172 assert_int_equal(LY_SUCCESS, ly_in_new_filepath(path1, 0, &in));
173 assert_int_equal(LY_IN_FILEPATH, ly_in_type(in));
174 assert_ptr_equal(NULL, ly_in_filepath(in, path2, 0));
175 assert_string_equal(path2, ly_in_filepath(in, NULL, 0));
176 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200177}
178
179static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200180test_output_mem(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200181{
182 struct ly_out *out = NULL;
183 char *buf1 = NULL, *buf2 = NULL;
184
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200185 /* manipulate with the handler */
186 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
187 assert_int_equal(LY_OUT_MEMORY, ly_out_type(out));
188 ly_write(out, "test", 4);
189 assert_ptr_equal(buf1, ly_out_memory(out, &buf2, 0));
190 assert_ptr_equal(buf2, ly_out_memory(out, NULL, 0));
191 assert_ptr_equal(buf2, ly_out_memory(out, &buf1, strlen(buf1)));
192 ly_out_free(out, NULL, 0);
193
194 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, strlen(buf1), &out));
195 ly_out_free(out, NULL, 1);
196
197 /* writing data */
198
199 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200200 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
201 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200202 assert_string_equal("test print", buf1);
203 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
Michal Vasko5233e962020-08-14 14:26:20 +0200204 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
205 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200206 assert_string_equal("rewrite", buf1);
207 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200208}
209
210static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200211test_output_fd(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200212{
213 struct ly_out *out = NULL;
214 int fd1, fd2;
215 char buf[31] = {0};
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200216
Michal Vaskode2e8b22022-01-19 09:13:38 +0100217 assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
218 assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200219
220 /* manipulate with the handler */
221 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
222 assert_int_equal(LY_OUT_FD, ly_out_type(out));
223 assert_ptr_equal(fd1, ly_out_fd(out, fd2));
224 assert_ptr_equal(fd2, ly_out_fd(out, -1));
225 assert_ptr_equal(fd2, ly_out_fd(out, fd1));
226 ly_out_free(out, NULL, 0);
227 assert_int_equal(0, close(fd2));
228 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
229 ly_out_free(out, NULL, 1);
230
231 /* writing data */
Michal Vaskode2e8b22022-01-19 09:13:38 +0100232 assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
233 assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200234 /* truncate file to start with no data */
235 assert_int_equal(0, ftruncate(fd1, 0));
236
237 assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200238 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
239 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200240 ly_print_flush(out);
241 assert_int_equal(10, read(fd2, buf, 30));
242 assert_string_equal("test print", buf);
243 assert_int_equal(0, lseek(fd2, 0, SEEK_SET));
244 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
245
Michal Vasko5233e962020-08-14 14:26:20 +0200246 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
247 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200248 ly_print_flush(out);
249 assert_int_equal(8, read(fd2, buf, 30));
250 assert_string_equal("rewrite", buf);
251
252 close(fd2);
253 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200254}
255
256static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200257test_output_file(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200258{
259 struct ly_out *out = NULL;
260 FILE *f1, *f2;
261 char buf[31] = {0};
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200262
Michal Vaskode2e8b22022-01-19 09:13:38 +0100263 assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb"));
264 assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "wb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200265
266 /* manipulate with the handler */
267 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
268 assert_int_equal(LY_OUT_FILE, ly_out_type(out));
269 assert_ptr_equal(f1, ly_out_file(out, f2));
270 assert_ptr_equal(f2, ly_out_file(out, NULL));
271 assert_ptr_equal(f2, ly_out_file(out, f1));
272 ly_out_free(out, NULL, 0);
273 assert_int_equal(0, fclose(f2));
274 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
275 ly_out_free(out, NULL, 1);
276
277 /* writing data */
Michal Vaskode2e8b22022-01-19 09:13:38 +0100278 assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb"));
279 assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200280
281 assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200282 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
283 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200284 ly_print_flush(out);
285 assert_non_null(fgets(buf, 31, f2));
286 assert_string_equal("test print", buf);
287 assert_int_equal(0, fseek(f2, 0, SEEK_SET));
288 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
289
Michal Vasko5233e962020-08-14 14:26:20 +0200290 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
291 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200292 ly_print_flush(out);
293 assert_non_null(fgets(buf, 31, f2));
294 assert_string_equal("rewrite", buf);
295
296 fclose(f2);
297 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200298}
299
300static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200301test_output_filepath(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200302{
303 struct ly_out *out = NULL;
304 FILE *f1;
305 char buf[31] = {0};
Michal Vaskode2e8b22022-01-19 09:13:38 +0100306 const char *fp1 = TEST_OUTPUT_FILE;
307 const char *fp2 = TEST_OUTPUT_FILE2;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200308
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200309 /* manipulate with the handler */
310 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
311 assert_int_equal(LY_OUT_FILEPATH, ly_out_type(out));
312 assert_ptr_equal(NULL, ly_out_filepath(out, fp2));
313 assert_string_equal(fp2, ly_out_filepath(out, NULL));
314 assert_ptr_equal(NULL, ly_out_filepath(out, fp1));
315 ly_out_free(out, NULL, 0);
316 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
317 ly_out_free(out, NULL, 1);
318
319 /* writing data */
Michal Vaskode2e8b22022-01-19 09:13:38 +0100320 assert_non_null(f1 = fopen(fp1, "rb"));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200321
322 assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200323 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
324 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200325 ly_print_flush(out);
326 assert_non_null(fgets(buf, 31, f1));
327 assert_string_equal("test print", buf);
328 assert_int_equal(0, fseek(f1, 0, SEEK_SET));
329 assert_int_equal(LY_SUCCESS, ly_out_reset(out));
330
Michal Vasko5233e962020-08-14 14:26:20 +0200331 assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8));
332 assert_int_equal(8, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200333 ly_print_flush(out);
334 assert_non_null(fgets(buf, 31, f1));
335 assert_string_equal("rewrite", buf);
336
337 fclose(f1);
338 ly_out_free(out, NULL, 1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200339}
340
Michal Vasko8759f3c2021-09-22 12:19:43 +0200341static ssize_t
342write_clb(void *user_data, const void *buf, size_t count)
343{
344 return write((uintptr_t)user_data, buf, count);
345}
346
347void
348close_clb(void *arg)
349{
350 close((uintptr_t)arg);
351}
352
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200353static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200354test_output_clb(void **UNUSED(state))
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200355{
356 struct ly_out *out = NULL;
357 int fd1, fd2;
358 char buf[31] = {0};
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200359
Michal Vaskode2e8b22022-01-19 09:13:38 +0100360 assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR));
361 assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200362
363 /* manipulate with the handler */
Michal Vasko8759f3c2021-09-22 12:19:43 +0200364 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200365 assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100366 assert_ptr_equal(fd1, ly_out_clb_arg(out, (void *)(intptr_t)fd2));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200367 assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100368 assert_ptr_equal(fd2, ly_out_clb_arg(out, (void *)(intptr_t)fd1));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200369 assert_ptr_equal(write_clb, ly_out_clb(out, write_clb));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200370 ly_out_free(out, NULL, 0);
371 assert_int_equal(0, close(fd2));
Michal Vasko8759f3c2021-09-22 12:19:43 +0200372 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
373 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200374
375 /* writing data */
Michal Vaskode2e8b22022-01-19 09:13:38 +0100376 assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR));
377 assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200378 /* truncate file to start with no data */
379 assert_int_equal(0, ftruncate(fd1, 0));
380
Michal Vasko8759f3c2021-09-22 12:19:43 +0200381 assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
Michal Vasko5233e962020-08-14 14:26:20 +0200382 assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
383 assert_int_equal(10, ly_out_printed(out));
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200384 assert_int_equal(10, read(fd2, buf, 30));
385 assert_string_equal("test print", buf);
386
387 close(fd2);
Michal Vasko8759f3c2021-09-22 12:19:43 +0200388 ly_out_free(out, close_clb, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200389}
390
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100391int
392main(void)
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200393{
394 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 UTEST(test_input_mem),
Michal Vaskode2e8b22022-01-19 09:13:38 +0100396 UTEST(test_input_fd, setup_files, teardown_files),
397 UTEST(test_input_file, setup_files, teardown_files),
398 UTEST(test_input_filepath, setup_files, teardown_files),
Radek Iša56ca9e42020-09-08 18:42:00 +0200399 UTEST(test_output_mem),
Michal Vaskode2e8b22022-01-19 09:13:38 +0100400 UTEST(test_output_fd, setup_files, teardown_files),
401 UTEST(test_output_file, setup_files, teardown_files),
402 UTEST(test_output_filepath, setup_files, teardown_files),
403 UTEST(test_output_clb, setup_files, teardown_files),
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200404 };
405
406 return cmocka_run_group_tests(tests, NULL, NULL);
407}