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 | de2e8b2 | 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)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 87 | CHECK_LOG_LASTMSG("Invalid argument str (ly_in_new_memory())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 88 | assert_int_equal(LY_EINVAL, ly_in_new_memory(str1, NULL)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 89 | CHECK_LOG_LASTMSG("Invalid argument in (ly_in_new_memory())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 90 | assert_null(ly_in_memory(NULL, NULL)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 91 | CHECK_LOG_LASTMSG("Invalid argument in (ly_in_memory())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 92 | |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 102 | test_input_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 103 | { |
| 104 | struct ly_in *in = NULL; |
| 105 | int fd1, fd2; |
| 106 | struct stat statbuf; |
| 107 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 108 | assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 109 | CHECK_LOG_LASTMSG("Invalid argument fd >= 0 (ly_in_new_fd())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 110 | assert_int_equal(-1, ly_in_fd(NULL, -1)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 111 | CHECK_LOG_LASTMSG("Invalid argument in (ly_in_fd())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 112 | |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 113 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 115 | |
| 116 | assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL)); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 117 | CHECK_LOG_LASTMSG("Invalid argument in (ly_in_new_fd())."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 118 | |
| 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át | d104767 | 2021-12-13 20:06:17 +0100 | [diff] [blame] | 128 | #ifndef _WIN32 |
| 129 | /* 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] | 130 | errno = 0; |
| 131 | assert_int_equal(-1, fstat(fd2, &statbuf)); |
| 132 | assert_int_equal(errno, EBADF); |
Jan Kundrát | d104767 | 2021-12-13 20:06:17 +0100 | [diff] [blame] | 133 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 137 | test_input_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 138 | { |
| 139 | struct ly_in *in = NULL; |
| 140 | FILE *f1 = NULL, *f2 = NULL; |
| 141 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 142 | assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL)); |
| 143 | assert_null(ly_in_file(NULL, NULL)); |
| 144 | |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 145 | assert_non_null(f1 = fopen(TEST_INPUT_FILE, "rb")); |
| 146 | assert_non_null(f2 = fopen(TEST_INPUT_FILE, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 147 | |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 163 | test_input_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 164 | { |
| 165 | struct ly_in *in = NULL; |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 166 | const char *path1 = TEST_INPUT_FILE, *path2 = TEST_INPUT_FILE; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 167 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 168 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 180 | test_output_mem(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 181 | { |
| 182 | struct ly_out *out = NULL; |
| 183 | char *buf1 = NULL, *buf2 = NULL; |
| 184 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 185 | /* 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 200 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 201 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 202 | assert_string_equal("test print", buf1); |
| 203 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 204 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 205 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 206 | assert_string_equal("rewrite", buf1); |
| 207 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 211 | test_output_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 212 | { |
| 213 | struct ly_out *out = NULL; |
| 214 | int fd1, fd2; |
| 215 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 216 | |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 217 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 219 | |
| 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 Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 232 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 234 | /* 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 238 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 239 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 240 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 246 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 247 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 248 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 257 | test_output_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 258 | { |
| 259 | struct ly_out *out = NULL; |
| 260 | FILE *f1, *f2; |
| 261 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 262 | |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 263 | assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb")); |
| 264 | assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "wb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 265 | |
| 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 Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 278 | assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "wb")); |
| 279 | assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 280 | |
| 281 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 282 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 283 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 284 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 290 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 291 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 292 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 301 | test_output_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 302 | { |
| 303 | struct ly_out *out = NULL; |
| 304 | FILE *f1; |
| 305 | char buf[31] = {0}; |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 306 | const char *fp1 = TEST_OUTPUT_FILE; |
| 307 | const char *fp2 = TEST_OUTPUT_FILE2; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 308 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 309 | /* 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 Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 320 | assert_non_null(f1 = fopen(fp1, "rb")); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 321 | |
| 322 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 323 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 324 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 325 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 331 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 332 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 333 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 341 | static ssize_t |
| 342 | write_clb(void *user_data, const void *buf, size_t count) |
| 343 | { |
| 344 | return write((uintptr_t)user_data, buf, count); |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | close_clb(void *arg) |
| 349 | { |
| 350 | close((uintptr_t)arg); |
| 351 | } |
| 352 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 353 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 354 | test_output_clb(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 355 | { |
| 356 | struct ly_out *out = NULL; |
| 357 | int fd1, fd2; |
| 358 | char buf[31] = {0}; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 359 | |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 360 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 362 | |
| 363 | /* manipulate with the handler */ |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 364 | 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] | 365 | assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 366 | 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] | 367 | assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 368 | 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] | 369 | assert_ptr_equal(write_clb, ly_out_clb(out, write_clb)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 370 | ly_out_free(out, NULL, 0); |
| 371 | assert_int_equal(0, close(fd2)); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 372 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 374 | |
| 375 | /* writing data */ |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 376 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 378 | /* truncate file to start with no data */ |
| 379 | assert_int_equal(0, ftruncate(fd1, 0)); |
| 380 | |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 381 | 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] | 382 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 383 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 384 | assert_int_equal(10, read(fd2, buf, 30)); |
| 385 | assert_string_equal("test print", buf); |
| 386 | |
| 387 | close(fd2); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 388 | ly_out_free(out, close_clb, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 391 | int |
| 392 | main(void) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 393 | { |
| 394 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 395 | UTEST(test_input_mem), |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 396 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 399 | UTEST(test_output_mem), |
Michal Vasko | de2e8b2 | 2022-01-19 09:13:38 +0100 | [diff] [blame] | 400 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 407 | } |