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 | |
| 29 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 30 | test_input_mem(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 31 | { |
| 32 | struct ly_in *in = NULL; |
| 33 | char *str1 = "a", *str2 = "b"; |
| 34 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 35 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 48 | test_input_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 49 | { |
| 50 | struct ly_in *in = NULL; |
| 51 | int fd1, fd2; |
| 52 | struct stat statbuf; |
| 53 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 54 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 78 | test_input_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 79 | { |
| 80 | struct ly_in *in = NULL; |
| 81 | FILE *f1 = NULL, *f2 = NULL; |
| 82 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 83 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 104 | test_input_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 105 | { |
| 106 | struct ly_in *in = NULL; |
| 107 | const char *path1 = __FILE__, *path2 = __FILE__; |
| 108 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 109 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 121 | test_output_mem(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 122 | { |
| 123 | struct ly_out *out = NULL; |
| 124 | char *buf1 = NULL, *buf2 = NULL; |
| 125 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 126 | /* 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 141 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 142 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 143 | assert_string_equal("test print", buf1); |
| 144 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 145 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 146 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 147 | assert_string_equal("rewrite", buf1); |
| 148 | ly_out_free(out, NULL, 1); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 152 | test_output_fd(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 153 | { |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 159 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 180 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 181 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 182 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 188 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 189 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 190 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 199 | test_output_file(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 200 | { |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 206 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 225 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 226 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 227 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 233 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 234 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 235 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 244 | test_output_filepath(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 245 | { |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 252 | /* 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 266 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 267 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 268 | 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 Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 274 | assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); |
| 275 | assert_int_equal(8, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 276 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 284 | static ssize_t |
| 285 | write_clb(void *user_data, const void *buf, size_t count) |
| 286 | { |
| 287 | return write((uintptr_t)user_data, buf, count); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | close_clb(void *arg) |
| 292 | { |
| 293 | close((uintptr_t)arg); |
| 294 | } |
| 295 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 296 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 297 | test_output_clb(void **UNUSED(state)) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 298 | { |
| 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 304 | 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 Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 308 | 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] | 309 | assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 310 | 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] | 311 | assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 312 | 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] | 313 | assert_ptr_equal(write_clb, ly_out_clb(out, write_clb)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 314 | ly_out_free(out, NULL, 0); |
| 315 | assert_int_equal(0, close(fd2)); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 316 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 318 | |
| 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 Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 325 | 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] | 326 | assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print")); |
| 327 | assert_int_equal(10, ly_out_printed(out)); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 328 | assert_int_equal(10, read(fd2, buf, 30)); |
| 329 | assert_string_equal("test print", buf); |
| 330 | |
| 331 | close(fd2); |
Michal Vasko | 8759f3c | 2021-09-22 12:19:43 +0200 | [diff] [blame] | 332 | ly_out_free(out, close_clb, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 335 | int |
| 336 | main(void) |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 337 | { |
| 338 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 339 | 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 Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 351 | } |