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 | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 24 | #include "common.h" |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 25 | |
| 26 | #define BUFSIZE 1024 |
| 27 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 28 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 29 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 30 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 31 | #define ENABLE_LOGGER_CHECKING 1 |
| 32 | |
| 33 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 34 | static void |
| 35 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 36 | { |
| 37 | (void) level; /* unused */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 38 | if (store) { |
| 39 | if (path && path[0]) { |
| 40 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 41 | } else { |
| 42 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 43 | } |
| 44 | if (store > 0) { |
| 45 | --store; |
| 46 | } |
| 47 | } |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 48 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 49 | #endif |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 50 | |
| 51 | static int |
| 52 | logger_setup(void **state) |
| 53 | { |
| 54 | (void) state; /* unused */ |
| 55 | |
| 56 | ly_set_log_clb(logger, 0); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 61 | static int |
| 62 | logger_teardown(void **state) |
| 63 | { |
| 64 | (void) state; /* unused */ |
| 65 | #if ENABLE_LOGGER_CHECKING |
| 66 | if (*state) { |
| 67 | fprintf(stderr, "%s\n", logbuf); |
| 68 | } |
| 69 | #endif |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | logbuf_clean(void) |
| 75 | { |
| 76 | logbuf[0] = '\0'; |
| 77 | } |
| 78 | |
| 79 | #if ENABLE_LOGGER_CHECKING |
| 80 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 81 | #else |
| 82 | # define logbuf_assert(str) |
| 83 | #endif |
| 84 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 85 | static void |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 86 | test_utf8(void **state) |
| 87 | { |
| 88 | (void) state; /* unused */ |
| 89 | |
| 90 | char buf[5] = {0}; |
| 91 | const char *str = buf; |
| 92 | unsigned int c; |
| 93 | size_t len; |
| 94 | |
| 95 | /* test invalid UTF-8 characters in lyxml_getutf8 |
| 96 | * - https://en.wikipedia.org/wiki/UTF-8 */ |
| 97 | buf[0] = 0x04; |
| 98 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 99 | buf[0] = 0x80; |
| 100 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 101 | |
| 102 | buf[0] = 0xc0; |
| 103 | buf[1] = 0x00; |
| 104 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 105 | buf[1] = 0x80; |
| 106 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 107 | |
| 108 | buf[0] = 0xe0; |
| 109 | buf[1] = 0x00; |
| 110 | buf[2] = 0x80; |
| 111 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 112 | buf[1] = 0x80; |
| 113 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 114 | |
| 115 | buf[0] = 0xf0; |
| 116 | buf[1] = 0x00; |
| 117 | buf[2] = 0x80; |
| 118 | buf[3] = 0x80; |
| 119 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 120 | buf[1] = 0x80; |
| 121 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 122 | } |
| 123 | |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 124 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 125 | void *__real_realloc(void *ptr, size_t size); |
| 126 | void *__wrap_realloc(void *ptr, size_t size) |
| 127 | { |
| 128 | int wrap = mock_type(int); |
| 129 | |
| 130 | if (wrap) { |
| 131 | /* memory allocation failed */ |
| 132 | return NULL; |
| 133 | } else { |
| 134 | return __real_realloc(ptr, size); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | test_lyrealloc(void **state) |
| 140 | { |
| 141 | (void) state; /* unused */ |
| 142 | |
| 143 | char *ptr; |
| 144 | |
| 145 | ptr = malloc(1); |
| 146 | assert_non_null(ptr); |
| 147 | |
| 148 | /* realloc */ |
| 149 | will_return(__wrap_realloc, 0); |
| 150 | ptr = ly_realloc(ptr, 2048); |
| 151 | assert_non_null(ptr); |
| 152 | ptr[2047] = 0; /* test write */ |
| 153 | |
| 154 | /* realloc fails */ |
| 155 | will_return(__wrap_realloc, 1); |
| 156 | ptr = ly_realloc(ptr, 2048); |
| 157 | assert_null(ptr); |
| 158 | |
| 159 | /* ptr should be freed by ly_realloc() */ |
| 160 | } |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 161 | #endif /* not APPLE */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 162 | |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 163 | static void |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 164 | test_parse_int(void **state) |
| 165 | { |
| 166 | *state = test_parse_int; |
| 167 | const char *str; |
| 168 | int64_t i = 500; |
| 169 | |
| 170 | str = "10"; |
| 171 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 172 | assert_int_equal(i, 10); |
| 173 | |
| 174 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 175 | str = "000\n\t "; |
| 176 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 177 | assert_int_equal(i, 0); |
| 178 | |
| 179 | /* negative value */ |
| 180 | str = "-10"; |
| 181 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 182 | assert_int_equal(i, -10); |
| 183 | |
| 184 | /* non-NULL terminated string */ |
| 185 | str = "+5sometext"; |
| 186 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, 2, -10, 10, 10, &i)); |
| 187 | assert_int_equal(i, 5); |
| 188 | |
| 189 | /* out of bounds value */ |
| 190 | str = "11"; |
| 191 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 192 | str = "-11"; |
| 193 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 194 | |
| 195 | /* NaN */ |
| 196 | str = "zero"; |
| 197 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 198 | |
| 199 | /* mixing number with text */ |
| 200 | str = "10zero"; |
| 201 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 202 | |
| 203 | str = "10 zero"; |
| 204 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 205 | |
| 206 | *state = NULL; |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | test_parse_uint(void **state) |
| 211 | { |
| 212 | *state = test_parse_int; |
| 213 | const char *str; |
| 214 | uint64_t u = 500; |
| 215 | |
| 216 | str = "10"; |
| 217 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 218 | assert_int_equal(u, 10); |
| 219 | |
| 220 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 221 | str = "000\n\t "; |
| 222 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 223 | assert_int_equal(u, 0); |
| 224 | /* non-NULL terminated string */ |
| 225 | str = "+5sometext"; |
| 226 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, 2, 10, 10, &u)); |
| 227 | assert_int_equal(u, 5); |
| 228 | |
| 229 | /* out of bounds value */ |
| 230 | str = "11"; |
| 231 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 232 | str = "-1"; |
| 233 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), (uint64_t)-1, 10, &u)); |
| 234 | |
| 235 | /* NaN */ |
| 236 | str = "zero"; |
| 237 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 238 | |
| 239 | /* mixing number with text */ |
| 240 | str = "10zero"; |
| 241 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 242 | |
| 243 | str = "10 zero"; |
| 244 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 245 | |
| 246 | *state = NULL; |
| 247 | } |
| 248 | |
| 249 | static void |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 250 | test_parse_nodeid(void **state) |
| 251 | { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 252 | *state = test_parse_nodeid; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 253 | const char *str; |
| 254 | const char *prefix, *name; |
| 255 | size_t prefix_len, name_len; |
| 256 | |
| 257 | str = "123"; |
| 258 | assert_int_equal(LY_EINVAL, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 259 | |
| 260 | str = "a12_-.!"; |
| 261 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 262 | assert_null(prefix); |
| 263 | assert_int_equal(0, prefix_len); |
| 264 | assert_non_null(name); |
| 265 | assert_int_equal(6, name_len); |
| 266 | assert_int_equal(0, strncmp("a12_-.", name, name_len)); |
| 267 | assert_string_equal("!", str); |
| 268 | |
| 269 | str = "a12_-.:_b2 xxx"; |
| 270 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 271 | assert_non_null(prefix); |
| 272 | assert_int_equal(6, prefix_len); |
| 273 | assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len)); |
| 274 | assert_non_null(name); |
| 275 | assert_int_equal(3, name_len); |
| 276 | assert_int_equal(0, strncmp("_b2", name, name_len)); |
| 277 | assert_string_equal(" xxx", str); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 278 | |
| 279 | *state = NULL; |
| 280 | } |
| 281 | |
| 282 | static void |
| 283 | test_parse_instance_predicate(void **state) |
| 284 | { |
| 285 | *state = test_parse_instance_predicate; |
| 286 | const char *str, *errmsg; |
| 287 | const char *prefix, *id, *value; |
| 288 | size_t prefix_len, id_len, value_len; |
| 289 | |
| 290 | str = "[ex:name='fred']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 291 | assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 292 | assert_string_equal(str, ""); |
| 293 | assert_string_equal(prefix, "ex:name='fred']"); |
| 294 | assert_int_equal(prefix_len, 2); |
| 295 | assert_string_equal(id, "name='fred']"); |
| 296 | assert_int_equal(id_len, 4); |
| 297 | assert_string_equal(value, "fred']"); |
| 298 | assert_int_equal(value_len, 4); |
| 299 | |
| 300 | str = "[ex:ip = \"[192.0.2.1]\"][ex:port='80']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 301 | assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 302 | assert_string_equal(str, "[ex:port='80']"); |
| 303 | assert_string_equal(prefix, "ex:ip = \"[192.0.2.1]\"][ex:port='80']"); |
| 304 | assert_int_equal(prefix_len, 2); |
| 305 | assert_string_equal(id, "ip = \"[192.0.2.1]\"][ex:port='80']"); |
| 306 | assert_int_equal(id_len, 2); |
| 307 | assert_string_equal(value, "[192.0.2.1]\"][ex:port='80']"); |
| 308 | assert_int_equal(value_len, 11); |
| 309 | |
| 310 | str = "[. = 'blowfish-cbc']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 311 | assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 312 | assert_string_equal(str, ""); |
| 313 | assert_null(prefix); |
| 314 | assert_int_equal(prefix_len, 0); |
| 315 | assert_string_equal(id, ". = 'blowfish-cbc']"); |
| 316 | assert_int_equal(id_len, 1); |
| 317 | assert_string_equal(value, "blowfish-cbc']"); |
| 318 | assert_int_equal(value_len, 12); |
| 319 | |
| 320 | str = "[ 3 ]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 321 | assert_int_equal(LY_SUCCESS, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 322 | assert_string_equal(str, ""); |
| 323 | assert_null(prefix); |
| 324 | assert_int_equal(prefix_len, 0); |
| 325 | assert_null(id); |
| 326 | assert_int_equal(id_len, 0); |
| 327 | assert_string_equal(value, "3 ]"); |
| 328 | assert_int_equal(value_len, 1); |
| 329 | |
| 330 | /* invalid predicates */ |
| 331 | /* position must be positive integer */ |
| 332 | str = "[0]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 333 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 334 | assert_string_equal(errmsg, "The position predicate cannot be zero."); |
| 335 | str = "[-1]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 336 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 337 | assert_string_equal(errmsg, "Invalid instance predicate format (negative position or invalid node-identifier)."); |
| 338 | |
| 339 | /* invalid node-identifier */ |
| 340 | str = "[$node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 341 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 342 | assert_string_equal(errmsg, "Invalid node-identifier."); |
| 343 | str = "[.node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 344 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 345 | assert_string_equal(errmsg, "Unexpected character instead of '=' in leaf-list-predicate."); |
| 346 | str = "[13node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 347 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 348 | assert_string_equal(errmsg, "Predicate (pos) is not terminated by \']\' character."); |
| 349 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 350 | str = "[ex:node]"; |
| 351 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 352 | assert_string_equal(errmsg, "Unexpected character instead of '=' in key-predicate."); |
| 353 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 354 | str = "[ex:node= value]"; |
| 355 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 356 | assert_string_equal(errmsg, "String value is not quoted."); |
| 357 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 358 | str = "[ex:node='value\"]"; |
| 359 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 360 | assert_string_equal(errmsg, "Value is not terminated quoted-string."); |
| 361 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 362 | str = "[ex:node='value ]"; |
| 363 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 364 | assert_string_equal(errmsg, "Value is not terminated quoted-string."); |
| 365 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 366 | str = "[ex:node=\"value\"[3]"; |
| 367 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 368 | assert_string_equal(errmsg, "Predicate (key-predicate) is not terminated by \']\' character."); |
| 369 | str = "[.=\"value\"[3]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 370 | assert_int_equal(LY_EVALID, ly_parse_instance_predicate(&str, strlen(str), LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 371 | assert_string_equal(errmsg, "Predicate (leaf-list-predicate) is not terminated by \']\' character."); |
| 372 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 373 | /* the limit of the string is too short, it ends one character earlier */ |
| 374 | str = "[ex:node='value']"; |
| 375 | assert_int_equal(LY_EINVAL, ly_parse_instance_predicate(&str, strlen(str) - 1, LYD_XML, &prefix, &prefix_len, &id, &id_len, &value, &value_len, &errmsg)); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 376 | assert_string_equal(errmsg, "Predicate is incomplete."); |
| 377 | |
| 378 | *state = NULL; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 381 | int main(void) |
| 382 | { |
| 383 | const struct CMUnitTest tests[] = { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 384 | cmocka_unit_test_setup_teardown(test_utf8, logger_setup, logger_teardown), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 385 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 386 | cmocka_unit_test(test_lyrealloc), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 387 | #endif |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 388 | cmocka_unit_test_setup_teardown(test_parse_int, logger_setup, logger_teardown), |
| 389 | cmocka_unit_test_setup_teardown(test_parse_uint, logger_setup, logger_teardown), |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 390 | cmocka_unit_test_setup_teardown(test_parse_nodeid, logger_setup, logger_teardown), |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 391 | cmocka_unit_test_setup_teardown(test_parse_instance_predicate, logger_setup, logger_teardown), |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 395 | } |