Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from common.c |
| 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 "../../src/common.c" |
| 16 | |
| 17 | #define _BSD_SOURCE |
| 18 | #define _DEFAULT_SOURCE |
| 19 | #include <stdarg.h> |
| 20 | #include <stddef.h> |
| 21 | #include <setjmp.h> |
| 22 | #include <cmocka.h> |
| 23 | |
| 24 | #include "libyang.h" |
| 25 | |
| 26 | #define BUFSIZE 1024 |
| 27 | char logbuf[BUFSIZE] = {0}; |
| 28 | |
| 29 | static void |
| 30 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 31 | { |
| 32 | (void) level; /* unused */ |
| 33 | (void) path; /* unused */ |
| 34 | |
| 35 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 36 | } |
| 37 | |
| 38 | static int |
| 39 | logger_setup(void **state) |
| 40 | { |
| 41 | (void) state; /* unused */ |
| 42 | |
| 43 | ly_set_log_clb(logger, 0); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | test_date(void **state) |
| 50 | { |
| 51 | (void) state; /* unused */ |
| 52 | |
| 53 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
| 54 | assert_string_equal(logbuf, "Invalid argument date (lysp_check_date())."); |
| 55 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
| 56 | assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date())."); |
| 57 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
| 58 | assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\"."); |
| 59 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
| 60 | assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\"."); |
| 61 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
| 62 | assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\"."); |
| 63 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
| 64 | assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\"."); |
| 65 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
| 66 | assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\"."); |
| 67 | |
| 68 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 69 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 70 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 71 | } |
| 72 | |
| 73 | void *__real_realloc(void *ptr, size_t size); |
| 74 | void *__wrap_realloc(void *ptr, size_t size) |
| 75 | { |
| 76 | int wrap = mock_type(int); |
| 77 | |
| 78 | if (wrap) { |
| 79 | /* memory allocation failed */ |
| 80 | return NULL; |
| 81 | } else { |
| 82 | return __real_realloc(ptr, size); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | test_lyrealloc(void **state) |
| 88 | { |
| 89 | (void) state; /* unused */ |
| 90 | |
| 91 | char *ptr; |
| 92 | |
| 93 | ptr = malloc(1); |
| 94 | assert_non_null(ptr); |
| 95 | |
| 96 | /* realloc */ |
| 97 | will_return(__wrap_realloc, 0); |
| 98 | ptr = ly_realloc(ptr, 2048); |
| 99 | assert_non_null(ptr); |
| 100 | ptr[2047] = 0; /* test write */ |
| 101 | |
| 102 | /* realloc fails */ |
| 103 | will_return(__wrap_realloc, 1); |
| 104 | ptr = ly_realloc(ptr, 2048); |
| 105 | assert_null(ptr); |
| 106 | |
| 107 | /* ptr should be freed by ly_realloc() */ |
| 108 | } |
| 109 | |
| 110 | int main(void) |
| 111 | { |
| 112 | const struct CMUnitTest tests[] = { |
| 113 | cmocka_unit_test_setup(test_date, logger_setup), |
| 114 | cmocka_unit_test(test_lyrealloc), |
| 115 | }; |
| 116 | |
| 117 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 118 | } |