Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 2 | * @file test_inout.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for input and output handlers functions |
| 5 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 7 | * |
| 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 16 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 20 | #include <sys/stat.h> |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 21 | #include <sys/types.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 24 | #include "common.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 25 | #include "in.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 26 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 27 | #include "out.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 28 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 29 | #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 | |
| 33 | static int |
| 34 | setup_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 | |
| 69 | static int |
| 70 | teardown_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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 80 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 81 | test_input_mem(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 82 | { |
| 83 | struct ly_in *in = NULL; |
| 84 | char *str1 = "a", *str2 = "b"; |
| 85 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 86 | assert_int_equal(LY_EINVAL, ly_in_new_memory(NULL, NULL)); |
| 87 | assert_int_equal(LY_EINVAL, ly_in_new_memory(str1, NULL)); |
| 88 | assert_null(ly_in_memory(NULL, NULL)); |
| 89 | |
| 90 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in)); |
| 91 | assert_int_equal(LY_IN_MEMORY, ly_in_type(in)); |
| 92 | assert_ptr_equal(str1, ly_in_memory(in, str2)); |
| 93 | assert_ptr_equal(str2, ly_in_memory(in, NULL)); |
| 94 | assert_ptr_equal(str2, ly_in_memory(in, NULL)); |
| 95 | ly_in_free(in, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 99 | test_input_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 100 | { |
| 101 | struct ly_in *in = NULL; |
| 102 | int fd1, fd2; |
| 103 | struct stat statbuf; |
| 104 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 105 | assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL)); |
| 106 | assert_int_equal(-1, ly_in_fd(NULL, -1)); |
| 107 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 108 | assert_int_not_equal(-1, fd1 = open(TEST_INPUT_FILE, O_RDONLY)); |
| 109 | assert_int_not_equal(-1, fd2 = open(TEST_INPUT_FILE, O_RDONLY)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 110 | |
| 111 | assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL)); |
| 112 | |
| 113 | assert_int_equal(LY_SUCCESS, ly_in_new_fd(fd1, &in)); |
| 114 | assert_int_equal(LY_IN_FD, ly_in_type(in)); |
| 115 | assert_ptr_equal(fd1, ly_in_fd(in, fd2)); |
| 116 | assert_ptr_equal(fd2, ly_in_fd(in, -1)); |
| 117 | assert_ptr_equal(fd2, ly_in_fd(in, -1)); |
| 118 | ly_in_free(in, 1); |
| 119 | /* fd1 is still open */ |
| 120 | assert_int_equal(0, fstat(fd1, &statbuf)); |
| 121 | close(fd1); |
Jan Kundrát | b156803 | 2021-12-13 20:06:17 +0100 | [diff] [blame] | 122 | #ifndef _WIN32 |
| 123 | /* But fd2 was closed by ly_in_free(). This results in an "invalid handler" on Windows. */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 124 | errno = 0; |
| 125 | assert_int_equal(-1, fstat(fd2, &statbuf)); |
| 126 | assert_int_equal(errno, EBADF); |
Jan Kundrát | b156803 | 2021-12-13 20:06:17 +0100 | [diff] [blame] | 127 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 131 | test_input_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 132 | { |
| 133 | struct ly_in *in = NULL; |
| 134 | FILE *f1 = NULL, *f2 = NULL; |
| 135 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 136 | assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL)); |
| 137 | assert_null(ly_in_file(NULL, NULL)); |
| 138 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 139 | assert_non_null(f1 = fopen(TEST_INPUT_FILE, "rb")); |
| 140 | assert_non_null(f2 = fopen(TEST_INPUT_FILE, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 141 | |
| 142 | assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL)); |
| 143 | |
| 144 | assert_int_equal(LY_SUCCESS, ly_in_new_file(f1, &in)); |
| 145 | assert_int_equal(LY_IN_FILE, ly_in_type(in)); |
| 146 | assert_ptr_equal(f1, ly_in_file(in, f2)); |
| 147 | assert_ptr_equal(f2, ly_in_file(in, NULL)); |
| 148 | assert_ptr_equal(f2, ly_in_file(in, NULL)); |
| 149 | ly_in_free(in, 1); |
| 150 | /* f1 is still open */ |
| 151 | assert_int_not_equal(-1, fileno(f1)); |
| 152 | fclose(f1); |
| 153 | /* but f2 was closed by ly_in_free() */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 157 | test_input_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 158 | { |
| 159 | struct ly_in *in = NULL; |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 160 | const char *path1 = TEST_INPUT_FILE, *path2 = TEST_INPUT_FILE; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 161 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 162 | assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL)); |
| 163 | assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL)); |
| 164 | assert_ptr_equal(((void *)-1), ly_in_filepath(NULL, NULL, 0)); |
| 165 | |
| 166 | assert_int_equal(LY_SUCCESS, ly_in_new_filepath(path1, 0, &in)); |
| 167 | assert_int_equal(LY_IN_FILEPATH, ly_in_type(in)); |
| 168 | assert_ptr_equal(NULL, ly_in_filepath(in, path2, 0)); |
| 169 | assert_string_equal(path2, ly_in_filepath(in, NULL, 0)); |
| 170 | ly_in_free(in, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 174 | test_output_mem(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 175 | { |
| 176 | struct ly_out *out = NULL; |
| 177 | char *buf1 = NULL, *buf2 = NULL; |
| 178 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 179 | /* manipulate with the handler */ |
| 180 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out)); |
| 181 | assert_int_equal(LY_OUT_MEMORY, ly_out_type(out)); |
| 182 | ly_write(out, "test", 4); |
| 183 | assert_ptr_equal(buf1, ly_out_memory(out, &buf2, 0)); |
| 184 | assert_ptr_equal(buf2, ly_out_memory(out, NULL, 0)); |
| 185 | assert_ptr_equal(buf2, ly_out_memory(out, &buf1, strlen(buf1))); |
| 186 | ly_out_free(out, NULL, 0); |
| 187 | |
| 188 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, strlen(buf1), &out)); |
| 189 | ly_out_free(out, NULL, 1); |
| 190 | |
| 191 | /* writing data */ |
| 192 | |
| 193 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 194 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 195 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 196 | assert_string_equal("test print", buf1); |
| 197 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 198 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 199 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 200 | assert_string_equal("rewrite", buf1); |
| 201 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 205 | test_output_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 206 | { |
| 207 | struct ly_out *out = NULL; |
| 208 | int fd1, fd2; |
| 209 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 210 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 211 | assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 212 | assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 213 | |
| 214 | /* manipulate with the handler */ |
| 215 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
| 216 | assert_int_equal(LY_OUT_FD, ly_out_type(out)); |
| 217 | assert_ptr_equal(fd1, ly_out_fd(out, fd2)); |
| 218 | assert_ptr_equal(fd2, ly_out_fd(out, -1)); |
| 219 | assert_ptr_equal(fd2, ly_out_fd(out, fd1)); |
| 220 | ly_out_free(out, NULL, 0); |
| 221 | assert_int_equal(0, close(fd2)); |
| 222 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
| 223 | ly_out_free(out, NULL, 1); |
| 224 | |
| 225 | /* writing data */ |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 226 | assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 227 | assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 228 | /* truncate file to start with no data */ |
| 229 | assert_int_equal(0, ftruncate(fd1, 0)); |
| 230 | |
| 231 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 232 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 233 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 234 | ly_print_flush(out); |
| 235 | assert_int_equal(10, read(fd2, buf, 30)); |
| 236 | assert_string_equal("test print", buf); |
| 237 | assert_int_equal(0, lseek(fd2, 0, SEEK_SET)); |
| 238 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 239 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 240 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 241 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 242 | ly_print_flush(out); |
| 243 | assert_int_equal(8, read(fd2, buf, 30)); |
| 244 | assert_string_equal("rewrite", buf); |
| 245 | |
| 246 | close(fd2); |
| 247 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 251 | test_output_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 252 | { |
| 253 | struct ly_out *out = NULL; |
| 254 | FILE *f1, *f2; |
| 255 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 256 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 257 | assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb")); |
| 258 | assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "wb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 259 | |
| 260 | /* manipulate with the handler */ |
| 261 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
| 262 | assert_int_equal(LY_OUT_FILE, ly_out_type(out)); |
| 263 | assert_ptr_equal(f1, ly_out_file(out, f2)); |
| 264 | assert_ptr_equal(f2, ly_out_file(out, NULL)); |
| 265 | assert_ptr_equal(f2, ly_out_file(out, f1)); |
| 266 | ly_out_free(out, NULL, 0); |
| 267 | assert_int_equal(0, fclose(f2)); |
| 268 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
| 269 | ly_out_free(out, NULL, 1); |
| 270 | |
| 271 | /* writing data */ |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 272 | assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb")); |
| 273 | assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 274 | |
| 275 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 276 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 277 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 278 | ly_print_flush(out); |
| 279 | assert_non_null(fgets(buf, 31, f2)); |
| 280 | assert_string_equal("test print", buf); |
| 281 | assert_int_equal(0, fseek(f2, 0, SEEK_SET)); |
| 282 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 283 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 284 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 285 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 286 | ly_print_flush(out); |
| 287 | assert_non_null(fgets(buf, 31, f2)); |
| 288 | assert_string_equal("rewrite", buf); |
| 289 | |
| 290 | fclose(f2); |
| 291 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 295 | test_output_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 296 | { |
| 297 | struct ly_out *out = NULL; |
| 298 | FILE *f1; |
| 299 | char buf[31] = {0}; |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 300 | const char *fp1 = TEST_OUTPUT_FILE; |
| 301 | const char *fp2 = TEST_OUTPUT_FILE2; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 302 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 303 | /* manipulate with the handler */ |
| 304 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
| 305 | assert_int_equal(LY_OUT_FILEPATH, ly_out_type(out)); |
| 306 | assert_ptr_equal(NULL, ly_out_filepath(out, fp2)); |
| 307 | assert_string_equal(fp2, ly_out_filepath(out, NULL)); |
| 308 | assert_ptr_equal(NULL, ly_out_filepath(out, fp1)); |
| 309 | ly_out_free(out, NULL, 0); |
| 310 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
| 311 | ly_out_free(out, NULL, 1); |
| 312 | |
| 313 | /* writing data */ |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 314 | assert_non_null(f1 = fopen(fp1, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 315 | |
| 316 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 317 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 318 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 319 | ly_print_flush(out); |
| 320 | assert_non_null(fgets(buf, 31, f1)); |
| 321 | assert_string_equal("test print", buf); |
| 322 | assert_int_equal(0, fseek(f1, 0, SEEK_SET)); |
| 323 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 324 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 325 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 326 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 327 | ly_print_flush(out); |
| 328 | assert_non_null(fgets(buf, 31, f1)); |
| 329 | assert_string_equal("rewrite", buf); |
| 330 | |
| 331 | fclose(f1); |
| 332 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 335 | static ssize_t |
| 336 | write_clb(void *user_data, const void *buf, size_t count) |
| 337 | { |
| 338 | return write((uintptr_t)user_data, buf, count); |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | close_clb(void *arg) |
| 343 | { |
| 344 | close((uintptr_t)arg); |
| 345 | } |
| 346 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 347 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 348 | test_output_clb(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 349 | { |
| 350 | struct ly_out *out = NULL; |
| 351 | int fd1, fd2; |
| 352 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 353 | |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 354 | assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR)); |
| 355 | assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 356 | |
| 357 | /* manipulate with the handler */ |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 358 | assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 359 | assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 360 | assert_ptr_equal(fd1, ly_out_clb_arg(out, (void *)(intptr_t)fd2)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 361 | assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 362 | assert_ptr_equal(fd2, ly_out_clb_arg(out, (void *)(intptr_t)fd1)); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 363 | assert_ptr_equal(write_clb, ly_out_clb(out, write_clb)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 364 | ly_out_free(out, NULL, 0); |
| 365 | assert_int_equal(0, close(fd2)); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 366 | assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out)); |
| 367 | ly_out_free(out, close_clb, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 368 | |
| 369 | /* writing data */ |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 370 | assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR)); |
| 371 | assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 372 | /* truncate file to start with no data */ |
| 373 | assert_int_equal(0, ftruncate(fd1, 0)); |
| 374 | |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 375 | assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 376 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 377 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 378 | assert_int_equal(10, read(fd2, buf, 30)); |
| 379 | assert_string_equal("test print", buf); |
| 380 | |
| 381 | close(fd2); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 382 | ly_out_free(out, close_clb, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 385 | int |
| 386 | main(void) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 387 | { |
| 388 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 389 | UTEST(test_input_mem), |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 390 | UTEST(test_input_fd, setup_files, teardown_files), |
| 391 | UTEST(test_input_file, setup_files, teardown_files), |
| 392 | UTEST(test_input_filepath, setup_files, teardown_files), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 393 | UTEST(test_output_mem), |
Michal Vasko | 4266d2b | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 394 | UTEST(test_output_fd, setup_files, teardown_files), |
| 395 | UTEST(test_output_file, setup_files, teardown_files), |
| 396 | UTEST(test_output_filepath, setup_files, teardown_files), |
| 397 | UTEST(test_output_clb, setup_files, teardown_files), |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 401 | } |