Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_inout.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for input and output handlers functions |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 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 | */ |
| 14 | |
| 15 | #include "tests/config.h" |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "../../src/common.h" |
| 29 | #include "../../src/log.h" |
| 30 | #include "../../src/printer.h" |
| 31 | #include "../../src/parser.h" |
| 32 | |
| 33 | |
| 34 | #define BUFSIZE 1024 |
| 35 | char logbuf[BUFSIZE] = {0}; |
| 36 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 37 | |
| 38 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 39 | #define ENABLE_LOGGER_CHECKING 1 |
| 40 | |
| 41 | #if ENABLE_LOGGER_CHECKING |
| 42 | static void |
| 43 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 44 | { |
| 45 | (void) level; /* unused */ |
| 46 | if (store) { |
| 47 | if (path && path[0]) { |
| 48 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 49 | } else { |
| 50 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 51 | } |
| 52 | if (store > 0) { |
| 53 | --store; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | static int |
| 60 | logger_setup(void **state) |
| 61 | { |
| 62 | (void) state; /* unused */ |
| 63 | |
| 64 | ly_set_log_clb(logger, 0); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int |
| 70 | logger_teardown(void **state) |
| 71 | { |
| 72 | (void) state; /* unused */ |
| 73 | #if ENABLE_LOGGER_CHECKING |
| 74 | if (*state) { |
| 75 | fprintf(stderr, "%s\n", logbuf); |
| 76 | } |
| 77 | #endif |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | logbuf_clean(void) |
| 83 | { |
| 84 | logbuf[0] = '\0'; |
| 85 | } |
| 86 | |
| 87 | #if ENABLE_LOGGER_CHECKING |
| 88 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 89 | #else |
| 90 | # define logbuf_assert(str) |
| 91 | #endif |
| 92 | |
| 93 | static void |
| 94 | test_input_mem(void **state) |
| 95 | { |
| 96 | struct ly_in *in = NULL; |
| 97 | char *str1 = "a", *str2 = "b"; |
| 98 | |
| 99 | *state = test_input_mem; |
| 100 | |
| 101 | assert_int_equal(LY_EINVAL, ly_in_new_memory(NULL, NULL)); |
| 102 | assert_int_equal(LY_EINVAL, ly_in_new_memory(str1, NULL)); |
| 103 | assert_null(ly_in_memory(NULL, NULL)); |
| 104 | |
| 105 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in)); |
| 106 | assert_int_equal(LY_IN_MEMORY, ly_in_type(in)); |
| 107 | assert_ptr_equal(str1, ly_in_memory(in, str2)); |
| 108 | assert_ptr_equal(str2, ly_in_memory(in, NULL)); |
| 109 | assert_ptr_equal(str2, ly_in_memory(in, NULL)); |
| 110 | ly_in_free(in, 0); |
| 111 | |
| 112 | /* cleanup */ |
| 113 | *state = NULL; |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | test_input_fd(void **state) |
| 118 | { |
| 119 | struct ly_in *in = NULL; |
| 120 | int fd1, fd2; |
| 121 | struct stat statbuf; |
| 122 | |
| 123 | *state = test_input_fd; |
| 124 | |
| 125 | assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL)); |
| 126 | assert_int_equal(-1, ly_in_fd(NULL, -1)); |
| 127 | |
| 128 | assert_int_not_equal(-1, fd1 = open(__FILE__, O_RDONLY)); |
| 129 | assert_int_not_equal(-1, fd2 = open(__FILE__, O_RDONLY)); |
| 130 | |
| 131 | assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL)); |
| 132 | |
| 133 | assert_int_equal(LY_SUCCESS, ly_in_new_fd(fd1, &in)); |
| 134 | assert_int_equal(LY_IN_FD, ly_in_type(in)); |
| 135 | assert_ptr_equal(fd1, ly_in_fd(in, fd2)); |
| 136 | assert_ptr_equal(fd2, ly_in_fd(in, -1)); |
| 137 | assert_ptr_equal(fd2, ly_in_fd(in, -1)); |
| 138 | ly_in_free(in, 1); |
| 139 | /* fd1 is still open */ |
| 140 | assert_int_equal(0, fstat(fd1, &statbuf)); |
| 141 | close(fd1); |
| 142 | /* but fd2 was closed by ly_in_free() */ |
| 143 | errno = 0; |
| 144 | assert_int_equal(-1, fstat(fd2, &statbuf)); |
| 145 | assert_int_equal(errno, EBADF); |
| 146 | |
| 147 | /* cleanup */ |
| 148 | *state = NULL; |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | test_input_file(void **state) |
| 153 | { |
| 154 | struct ly_in *in = NULL; |
| 155 | FILE *f1 = NULL, *f2 = NULL; |
| 156 | |
| 157 | *state = test_input_file; |
| 158 | |
| 159 | assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL)); |
| 160 | assert_null(ly_in_file(NULL, NULL)); |
| 161 | |
| 162 | assert_int_not_equal(-1, f1 = fopen(__FILE__, "r")); |
| 163 | assert_int_not_equal(-1, f2 = fopen(__FILE__, "r")); |
| 164 | |
| 165 | assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL)); |
| 166 | |
| 167 | assert_int_equal(LY_SUCCESS, ly_in_new_file(f1, &in)); |
| 168 | assert_int_equal(LY_IN_FILE, ly_in_type(in)); |
| 169 | assert_ptr_equal(f1, ly_in_file(in, f2)); |
| 170 | assert_ptr_equal(f2, ly_in_file(in, NULL)); |
| 171 | assert_ptr_equal(f2, ly_in_file(in, NULL)); |
| 172 | ly_in_free(in, 1); |
| 173 | /* f1 is still open */ |
| 174 | assert_int_not_equal(-1, fileno(f1)); |
| 175 | fclose(f1); |
| 176 | /* but f2 was closed by ly_in_free() */ |
| 177 | |
| 178 | /* cleanup */ |
| 179 | *state = NULL; |
| 180 | } |
| 181 | |
| 182 | static void |
| 183 | test_input_filepath(void **state) |
| 184 | { |
| 185 | struct ly_in *in = NULL; |
| 186 | const char *path1 = __FILE__, *path2 = __FILE__; |
| 187 | |
| 188 | *state = test_input_filepath; |
| 189 | |
| 190 | assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL)); |
| 191 | assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL)); |
| 192 | assert_ptr_equal(((void *)-1), ly_in_filepath(NULL, NULL, 0)); |
| 193 | |
| 194 | assert_int_equal(LY_SUCCESS, ly_in_new_filepath(path1, 0, &in)); |
| 195 | assert_int_equal(LY_IN_FILEPATH, ly_in_type(in)); |
| 196 | assert_ptr_equal(NULL, ly_in_filepath(in, path2, 0)); |
| 197 | assert_string_equal(path2, ly_in_filepath(in, NULL, 0)); |
| 198 | ly_in_free(in, 0); |
| 199 | |
| 200 | /* cleanup */ |
| 201 | *state = NULL; |
| 202 | } |
| 203 | |
| 204 | static void |
| 205 | test_output_mem(void **state) |
| 206 | { |
| 207 | struct ly_out *out = NULL; |
| 208 | char *buf1 = NULL, *buf2 = NULL; |
| 209 | |
| 210 | *state = test_output_mem; |
| 211 | |
| 212 | /* manipulate with the handler */ |
| 213 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out)); |
| 214 | assert_int_equal(LY_OUT_MEMORY, ly_out_type(out)); |
| 215 | ly_write(out, "test", 4); |
| 216 | assert_ptr_equal(buf1, ly_out_memory(out, &buf2, 0)); |
| 217 | assert_ptr_equal(buf2, ly_out_memory(out, NULL, 0)); |
| 218 | assert_ptr_equal(buf2, ly_out_memory(out, &buf1, strlen(buf1))); |
| 219 | ly_out_free(out, NULL, 0); |
| 220 | |
| 221 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, strlen(buf1), &out)); |
| 222 | ly_out_free(out, NULL, 1); |
| 223 | |
| 224 | /* writing data */ |
| 225 | |
| 226 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&buf1, 0, &out)); |
| 227 | assert_int_equal(10, ly_print(out, "test %s", "print")); |
| 228 | assert_string_equal("test print", buf1); |
| 229 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 230 | assert_int_equal(8, ly_write(out, "rewrite", 8)); |
| 231 | assert_string_equal("rewrite", buf1); |
| 232 | ly_out_free(out, NULL, 1); |
| 233 | |
| 234 | /* cleanup */ |
| 235 | *state = NULL; |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | test_output_fd(void **state) |
| 240 | { |
| 241 | struct ly_out *out = NULL; |
| 242 | int fd1, fd2; |
| 243 | char buf[31] = {0}; |
| 244 | const char *filepath = "/tmp/libyang_test_output"; |
| 245 | |
| 246 | *state = test_output_fd; |
| 247 | |
| 248 | assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 249 | assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 250 | |
| 251 | /* manipulate with the handler */ |
| 252 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
| 253 | assert_int_equal(LY_OUT_FD, ly_out_type(out)); |
| 254 | assert_ptr_equal(fd1, ly_out_fd(out, fd2)); |
| 255 | assert_ptr_equal(fd2, ly_out_fd(out, -1)); |
| 256 | assert_ptr_equal(fd2, ly_out_fd(out, fd1)); |
| 257 | ly_out_free(out, NULL, 0); |
| 258 | assert_int_equal(0, close(fd2)); |
| 259 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
| 260 | ly_out_free(out, NULL, 1); |
| 261 | |
| 262 | /* writing data */ |
| 263 | assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 264 | assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 265 | /* truncate file to start with no data */ |
| 266 | assert_int_equal(0, ftruncate(fd1, 0)); |
| 267 | |
| 268 | assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out)); |
| 269 | assert_int_equal(10, ly_print(out, "test %s", "print")); |
| 270 | ly_print_flush(out); |
| 271 | assert_int_equal(10, read(fd2, buf, 30)); |
| 272 | assert_string_equal("test print", buf); |
| 273 | assert_int_equal(0, lseek(fd2, 0, SEEK_SET)); |
| 274 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 275 | |
| 276 | assert_int_equal(8, ly_write(out, "rewrite", 8)); |
| 277 | ly_print_flush(out); |
| 278 | assert_int_equal(8, read(fd2, buf, 30)); |
| 279 | assert_string_equal("rewrite", buf); |
| 280 | |
| 281 | close(fd2); |
| 282 | ly_out_free(out, NULL, 1); |
| 283 | |
| 284 | /* cleanup */ |
| 285 | *state = NULL; |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | test_output_file(void **state) |
| 290 | { |
| 291 | struct ly_out *out = NULL; |
| 292 | FILE *f1, *f2; |
| 293 | char buf[31] = {0}; |
| 294 | const char *filepath = "/tmp/libyang_test_output"; |
| 295 | |
| 296 | *state = test_output_file; |
| 297 | |
| 298 | assert_int_not_equal(-1, f1 = fopen(filepath, "w")); |
| 299 | assert_int_not_equal(-1, f2 = fopen(filepath, "w")); |
| 300 | |
| 301 | /* manipulate with the handler */ |
| 302 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
| 303 | assert_int_equal(LY_OUT_FILE, ly_out_type(out)); |
| 304 | assert_ptr_equal(f1, ly_out_file(out, f2)); |
| 305 | assert_ptr_equal(f2, ly_out_file(out, NULL)); |
| 306 | assert_ptr_equal(f2, ly_out_file(out, f1)); |
| 307 | ly_out_free(out, NULL, 0); |
| 308 | assert_int_equal(0, fclose(f2)); |
| 309 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
| 310 | ly_out_free(out, NULL, 1); |
| 311 | |
| 312 | /* writing data */ |
| 313 | assert_int_not_equal(-1, f1 = fopen(filepath, "w")); |
| 314 | assert_int_not_equal(-1, f2 = fopen(filepath, "r")); |
| 315 | |
| 316 | assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out)); |
| 317 | assert_int_equal(10, ly_print(out, "test %s", "print")); |
| 318 | ly_print_flush(out); |
| 319 | assert_non_null(fgets(buf, 31, f2)); |
| 320 | assert_string_equal("test print", buf); |
| 321 | assert_int_equal(0, fseek(f2, 0, SEEK_SET)); |
| 322 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 323 | |
| 324 | assert_int_equal(8, ly_write(out, "rewrite", 8)); |
| 325 | ly_print_flush(out); |
| 326 | assert_non_null(fgets(buf, 31, f2)); |
| 327 | assert_string_equal("rewrite", buf); |
| 328 | |
| 329 | fclose(f2); |
| 330 | ly_out_free(out, NULL, 1); |
| 331 | |
| 332 | /* cleanup */ |
| 333 | *state = NULL; |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | test_output_filepath(void **state) |
| 338 | { |
| 339 | struct ly_out *out = NULL; |
| 340 | FILE *f1; |
| 341 | char buf[31] = {0}; |
| 342 | const char *fp1 = "/tmp/libyang_test_output"; |
| 343 | const char *fp2 = "/tmp/libyang_test_output2"; |
| 344 | |
| 345 | *state = test_output_filepath; |
| 346 | |
| 347 | /* manipulate with the handler */ |
| 348 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
| 349 | assert_int_equal(LY_OUT_FILEPATH, ly_out_type(out)); |
| 350 | assert_ptr_equal(NULL, ly_out_filepath(out, fp2)); |
| 351 | assert_string_equal(fp2, ly_out_filepath(out, NULL)); |
| 352 | assert_ptr_equal(NULL, ly_out_filepath(out, fp1)); |
| 353 | ly_out_free(out, NULL, 0); |
| 354 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
| 355 | ly_out_free(out, NULL, 1); |
| 356 | |
| 357 | /* writing data */ |
| 358 | assert_int_not_equal(-1, f1 = fopen(fp1, "r")); |
| 359 | |
| 360 | assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out)); |
| 361 | assert_int_equal(10, ly_print(out, "test %s", "print")); |
| 362 | ly_print_flush(out); |
| 363 | assert_non_null(fgets(buf, 31, f1)); |
| 364 | assert_string_equal("test print", buf); |
| 365 | assert_int_equal(0, fseek(f1, 0, SEEK_SET)); |
| 366 | assert_int_equal(LY_SUCCESS, ly_out_reset(out)); |
| 367 | |
| 368 | assert_int_equal(8, ly_write(out, "rewrite", 8)); |
| 369 | ly_print_flush(out); |
| 370 | assert_non_null(fgets(buf, 31, f1)); |
| 371 | assert_string_equal("rewrite", buf); |
| 372 | |
| 373 | fclose(f1); |
| 374 | ly_out_free(out, NULL, 1); |
| 375 | |
| 376 | /* cleanup */ |
| 377 | *state = NULL; |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | test_output_clb(void **state) |
| 382 | { |
| 383 | struct ly_out *out = NULL; |
| 384 | int fd1, fd2; |
| 385 | char buf[31] = {0}; |
| 386 | const char *filepath = "/tmp/libyang_test_output"; |
| 387 | |
| 388 | *state = test_output_clb; |
| 389 | |
| 390 | assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 391 | assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 392 | |
| 393 | /* manipulate with the handler */ |
| 394 | assert_int_equal(LY_SUCCESS, ly_out_new_clb((ssize_t (*)(void *, const void *, size_t))write, (void*)(intptr_t)fd1, &out)); |
| 395 | assert_int_equal(LY_OUT_CALLBACK, ly_out_type(out)); |
| 396 | assert_ptr_equal(fd1, ly_out_clb_arg(out, (void*)(intptr_t)fd2)); |
| 397 | assert_ptr_equal(fd2, ly_out_clb_arg(out, NULL)); |
| 398 | assert_ptr_equal(fd2, ly_out_clb_arg(out, (void*)(intptr_t)fd1)); |
| 399 | assert_ptr_equal(write, ly_out_clb(out, (ssize_t (*)(void *, const void *, size_t))write)); |
| 400 | ly_out_free(out, NULL, 0); |
| 401 | assert_int_equal(0, close(fd2)); |
| 402 | assert_int_equal(LY_SUCCESS, ly_out_new_clb((ssize_t (*)(void *, const void *, size_t))write, (void*)(intptr_t)fd1, &out)); |
| 403 | ly_out_free(out, (void (*)(void *))close, 0); |
| 404 | |
| 405 | /* writing data */ |
| 406 | assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 407 | assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)); |
| 408 | /* truncate file to start with no data */ |
| 409 | assert_int_equal(0, ftruncate(fd1, 0)); |
| 410 | |
| 411 | assert_int_equal(LY_SUCCESS, ly_out_new_clb((ssize_t (*)(void *, const void *, size_t))write, (void*)(intptr_t)fd1, &out)); |
| 412 | assert_int_equal(10, ly_print(out, "test %s", "print")); |
| 413 | assert_int_equal(10, read(fd2, buf, 30)); |
| 414 | assert_string_equal("test print", buf); |
| 415 | |
| 416 | close(fd2); |
| 417 | ly_out_free(out, (void (*)(void *))close, 0); |
| 418 | |
| 419 | /* cleanup */ |
| 420 | *state = NULL; |
| 421 | } |
| 422 | |
| 423 | int main(void) |
| 424 | { |
| 425 | const struct CMUnitTest tests[] = { |
| 426 | cmocka_unit_test_setup_teardown(test_input_mem, logger_setup, logger_teardown), |
| 427 | cmocka_unit_test_setup_teardown(test_input_fd, logger_setup, logger_teardown), |
| 428 | cmocka_unit_test_setup_teardown(test_input_file, logger_setup, logger_teardown), |
| 429 | cmocka_unit_test_setup_teardown(test_input_filepath, logger_setup, logger_teardown), |
| 430 | cmocka_unit_test_setup_teardown(test_output_mem, logger_setup, logger_teardown), |
| 431 | cmocka_unit_test_setup_teardown(test_output_fd, logger_setup, logger_teardown), |
| 432 | cmocka_unit_test_setup_teardown(test_output_file, logger_setup, logger_teardown), |
| 433 | cmocka_unit_test_setup_teardown(test_output_filepath, logger_setup, logger_teardown), |
| 434 | cmocka_unit_test_setup_teardown(test_output_clb, logger_setup, logger_teardown), |
| 435 | }; |
| 436 | |
| 437 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 438 | } |