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