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 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 20 | #include "../../src/common.h" |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 21 | |
| 22 | #define BUFSIZE 1024 |
| 23 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 24 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 25 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 26 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 27 | #define ENABLE_LOGGER_CHECKING 1 |
| 28 | |
| 29 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 30 | static void |
| 31 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 32 | { |
| 33 | (void) level; /* unused */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 34 | if (store) { |
| 35 | if (path && path[0]) { |
| 36 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 37 | } else { |
| 38 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 39 | } |
| 40 | if (store > 0) { |
| 41 | --store; |
| 42 | } |
| 43 | } |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 44 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 45 | #endif |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 46 | |
| 47 | static int |
| 48 | logger_setup(void **state) |
| 49 | { |
| 50 | (void) state; /* unused */ |
| 51 | |
| 52 | ly_set_log_clb(logger, 0); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 57 | static int |
| 58 | logger_teardown(void **state) |
| 59 | { |
| 60 | (void) state; /* unused */ |
| 61 | #if ENABLE_LOGGER_CHECKING |
| 62 | if (*state) { |
| 63 | fprintf(stderr, "%s\n", logbuf); |
| 64 | } |
| 65 | #endif |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | logbuf_clean(void) |
| 71 | { |
| 72 | logbuf[0] = '\0'; |
| 73 | } |
| 74 | |
| 75 | #if ENABLE_LOGGER_CHECKING |
| 76 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 77 | #else |
| 78 | # define logbuf_assert(str) |
| 79 | #endif |
| 80 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 81 | static void |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 82 | test_utf8(void **state) |
| 83 | { |
| 84 | (void) state; /* unused */ |
| 85 | |
| 86 | char buf[5] = {0}; |
| 87 | const char *str = buf; |
| 88 | unsigned int c; |
| 89 | size_t len; |
| 90 | |
| 91 | /* test invalid UTF-8 characters in lyxml_getutf8 |
| 92 | * - https://en.wikipedia.org/wiki/UTF-8 */ |
| 93 | buf[0] = 0x04; |
| 94 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 95 | buf[0] = 0x80; |
| 96 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 97 | |
| 98 | buf[0] = 0xc0; |
| 99 | buf[1] = 0x00; |
| 100 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 101 | buf[1] = 0x80; |
| 102 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 103 | |
| 104 | buf[0] = 0xe0; |
| 105 | buf[1] = 0x00; |
| 106 | buf[2] = 0x80; |
| 107 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 108 | buf[1] = 0x80; |
| 109 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 110 | |
| 111 | buf[0] = 0xf0; |
| 112 | buf[1] = 0x00; |
| 113 | buf[2] = 0x80; |
| 114 | buf[3] = 0x80; |
| 115 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 116 | buf[1] = 0x80; |
| 117 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 118 | } |
| 119 | |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 120 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 121 | void *__real_realloc(void *ptr, size_t size); |
| 122 | void *__wrap_realloc(void *ptr, size_t size) |
| 123 | { |
| 124 | int wrap = mock_type(int); |
| 125 | |
| 126 | if (wrap) { |
| 127 | /* memory allocation failed */ |
| 128 | return NULL; |
| 129 | } else { |
| 130 | return __real_realloc(ptr, size); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | test_lyrealloc(void **state) |
| 136 | { |
| 137 | (void) state; /* unused */ |
| 138 | |
| 139 | char *ptr; |
| 140 | |
| 141 | ptr = malloc(1); |
| 142 | assert_non_null(ptr); |
| 143 | |
| 144 | /* realloc */ |
| 145 | will_return(__wrap_realloc, 0); |
| 146 | ptr = ly_realloc(ptr, 2048); |
| 147 | assert_non_null(ptr); |
| 148 | ptr[2047] = 0; /* test write */ |
| 149 | |
| 150 | /* realloc fails */ |
| 151 | will_return(__wrap_realloc, 1); |
| 152 | ptr = ly_realloc(ptr, 2048); |
| 153 | assert_null(ptr); |
| 154 | |
| 155 | /* ptr should be freed by ly_realloc() */ |
| 156 | } |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 157 | #endif /* not APPLE */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 158 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 159 | static void |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame^] | 160 | test_parse_int(void **state) |
| 161 | { |
| 162 | *state = test_parse_int; |
| 163 | const char *str; |
| 164 | int64_t i = 500; |
| 165 | |
| 166 | str = "10"; |
| 167 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 168 | assert_int_equal(i, 10); |
| 169 | |
| 170 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 171 | str = "000\n\t "; |
| 172 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 173 | assert_int_equal(i, 0); |
| 174 | |
| 175 | /* negative value */ |
| 176 | str = "-10"; |
| 177 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 178 | assert_int_equal(i, -10); |
| 179 | |
| 180 | /* non-NULL terminated string */ |
| 181 | str = "+5sometext"; |
| 182 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, 2, -10, 10, 10, &i)); |
| 183 | assert_int_equal(i, 5); |
| 184 | |
| 185 | /* out of bounds value */ |
| 186 | str = "11"; |
| 187 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 188 | str = "-11"; |
| 189 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 190 | |
| 191 | /* NaN */ |
| 192 | str = "zero"; |
| 193 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 194 | |
| 195 | /* mixing number with text */ |
| 196 | str = "10zero"; |
| 197 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 198 | |
| 199 | str = "10 zero"; |
| 200 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 201 | |
| 202 | *state = NULL; |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | test_parse_uint(void **state) |
| 207 | { |
| 208 | *state = test_parse_int; |
| 209 | const char *str; |
| 210 | uint64_t u = 500; |
| 211 | |
| 212 | str = "10"; |
| 213 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 214 | assert_int_equal(u, 10); |
| 215 | |
| 216 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 217 | str = "000\n\t "; |
| 218 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 219 | assert_int_equal(u, 0); |
| 220 | /* non-NULL terminated string */ |
| 221 | str = "+5sometext"; |
| 222 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, 2, 10, 10, &u)); |
| 223 | assert_int_equal(u, 5); |
| 224 | |
| 225 | /* out of bounds value */ |
| 226 | str = "11"; |
| 227 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 228 | str = "-1"; |
| 229 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), (uint64_t)-1, 10, &u)); |
| 230 | |
| 231 | /* NaN */ |
| 232 | str = "zero"; |
| 233 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 234 | |
| 235 | /* mixing number with text */ |
| 236 | str = "10zero"; |
| 237 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 238 | |
| 239 | str = "10 zero"; |
| 240 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 241 | |
| 242 | *state = NULL; |
| 243 | } |
| 244 | |
| 245 | static void |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 246 | test_parse_nodeid(void **state) |
| 247 | { |
| 248 | (void) state; /* unused */ |
| 249 | const char *str; |
| 250 | const char *prefix, *name; |
| 251 | size_t prefix_len, name_len; |
| 252 | |
| 253 | str = "123"; |
| 254 | assert_int_equal(LY_EINVAL, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 255 | |
| 256 | str = "a12_-.!"; |
| 257 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 258 | assert_null(prefix); |
| 259 | assert_int_equal(0, prefix_len); |
| 260 | assert_non_null(name); |
| 261 | assert_int_equal(6, name_len); |
| 262 | assert_int_equal(0, strncmp("a12_-.", name, name_len)); |
| 263 | assert_string_equal("!", str); |
| 264 | |
| 265 | str = "a12_-.:_b2 xxx"; |
| 266 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 267 | assert_non_null(prefix); |
| 268 | assert_int_equal(6, prefix_len); |
| 269 | assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len)); |
| 270 | assert_non_null(name); |
| 271 | assert_int_equal(3, name_len); |
| 272 | assert_int_equal(0, strncmp("_b2", name, name_len)); |
| 273 | assert_string_equal(" xxx", str); |
| 274 | } |
| 275 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 276 | int main(void) |
| 277 | { |
| 278 | const struct CMUnitTest tests[] = { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 279 | cmocka_unit_test_setup_teardown(test_utf8, logger_setup, logger_teardown), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 280 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 281 | cmocka_unit_test(test_lyrealloc), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 282 | #endif |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame^] | 283 | cmocka_unit_test_setup_teardown(test_parse_int, logger_setup, logger_teardown), |
| 284 | cmocka_unit_test_setup_teardown(test_parse_uint, logger_setup, logger_teardown), |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 285 | cmocka_unit_test_setup_teardown(test_parse_nodeid, logger_setup, logger_teardown), |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 289 | } |