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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 15 | #include "utests.h" |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 16 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 17 | #include "common.h" |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 19 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 20 | test_utf8(void **UNUSED(state)) |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 21 | { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 22 | char buf[5] = {0}; |
| 23 | const char *str = buf; |
| 24 | unsigned int c; |
| 25 | size_t len; |
| 26 | |
| 27 | /* test invalid UTF-8 characters in lyxml_getutf8 |
| 28 | * - https://en.wikipedia.org/wiki/UTF-8 */ |
| 29 | buf[0] = 0x04; |
| 30 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 31 | buf[0] = 0x80; |
| 32 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 33 | |
| 34 | buf[0] = 0xc0; |
| 35 | buf[1] = 0x00; |
| 36 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 37 | buf[1] = 0x80; |
| 38 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 39 | |
| 40 | buf[0] = 0xe0; |
| 41 | buf[1] = 0x00; |
| 42 | buf[2] = 0x80; |
| 43 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 44 | buf[1] = 0x80; |
| 45 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 46 | |
| 47 | buf[0] = 0xf0; |
| 48 | buf[1] = 0x00; |
| 49 | buf[2] = 0x80; |
| 50 | buf[3] = 0x80; |
| 51 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 52 | buf[1] = 0x80; |
| 53 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 54 | } |
| 55 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 56 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 57 | test_parse_int(void **UNUSED(state)) |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 58 | { |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 59 | const char *str; |
| 60 | int64_t i = 500; |
| 61 | |
| 62 | str = "10"; |
| 63 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 64 | assert_int_equal(i, 10); |
| 65 | |
| 66 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 67 | str = "000\n\t "; |
| 68 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 69 | assert_int_equal(i, 0); |
| 70 | |
| 71 | /* negative value */ |
| 72 | str = "-10"; |
| 73 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 74 | assert_int_equal(i, -10); |
| 75 | |
| 76 | /* non-NULL terminated string */ |
| 77 | str = "+5sometext"; |
| 78 | assert_int_equal(LY_SUCCESS, ly_parse_int(str, 2, -10, 10, 10, &i)); |
| 79 | assert_int_equal(i, 5); |
| 80 | |
| 81 | /* out of bounds value */ |
| 82 | str = "11"; |
| 83 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 84 | str = "-11"; |
| 85 | assert_int_equal(LY_EDENIED, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 86 | |
| 87 | /* NaN */ |
| 88 | str = "zero"; |
| 89 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 90 | |
| 91 | /* mixing number with text */ |
| 92 | str = "10zero"; |
| 93 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
| 94 | |
| 95 | str = "10 zero"; |
| 96 | assert_int_equal(LY_EVALID, ly_parse_int(str, strlen(str), -10, 10, 10, &i)); |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 100 | test_parse_uint(void **UNUSED(state)) |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 101 | { |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 102 | const char *str; |
| 103 | uint64_t u = 500; |
| 104 | |
| 105 | str = "10"; |
| 106 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 107 | assert_int_equal(u, 10); |
| 108 | |
| 109 | /* leading zeros are allowed, trailing whitespaces are allowed */ |
| 110 | str = "000\n\t "; |
| 111 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 112 | assert_int_equal(u, 0); |
| 113 | /* non-NULL terminated string */ |
| 114 | str = "+5sometext"; |
| 115 | assert_int_equal(LY_SUCCESS, ly_parse_uint(str, 2, 10, 10, &u)); |
| 116 | assert_int_equal(u, 5); |
| 117 | |
| 118 | /* out of bounds value */ |
| 119 | str = "11"; |
| 120 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 121 | str = "-1"; |
| 122 | assert_int_equal(LY_EDENIED, ly_parse_uint(str, strlen(str), (uint64_t)-1, 10, &u)); |
| 123 | |
| 124 | /* NaN */ |
| 125 | str = "zero"; |
| 126 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 127 | |
| 128 | /* mixing number with text */ |
| 129 | str = "10zero"; |
| 130 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
| 131 | |
| 132 | str = "10 zero"; |
| 133 | assert_int_equal(LY_EVALID, ly_parse_uint(str, strlen(str), 10, 10, &u)); |
Radek Krejci | 9ea8ca1 | 2019-06-10 13:11:55 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 137 | test_parse_nodeid(void **UNUSED(state)) |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 138 | { |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 139 | const char *str; |
| 140 | const char *prefix, *name; |
| 141 | size_t prefix_len, name_len; |
| 142 | |
| 143 | str = "123"; |
| 144 | assert_int_equal(LY_EINVAL, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 145 | |
| 146 | str = "a12_-.!"; |
| 147 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 148 | assert_null(prefix); |
| 149 | assert_int_equal(0, prefix_len); |
| 150 | assert_non_null(name); |
| 151 | assert_int_equal(6, name_len); |
| 152 | assert_int_equal(0, strncmp("a12_-.", name, name_len)); |
| 153 | assert_string_equal("!", str); |
| 154 | |
| 155 | str = "a12_-.:_b2 xxx"; |
| 156 | assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len)); |
| 157 | assert_non_null(prefix); |
| 158 | assert_int_equal(6, prefix_len); |
| 159 | assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len)); |
| 160 | assert_non_null(name); |
| 161 | assert_int_equal(3, name_len); |
| 162 | assert_int_equal(0, strncmp("_b2", name, name_len)); |
| 163 | assert_string_equal(" xxx", str); |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 167 | test_parse_instance_predicate(void **UNUSED(state)) |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 168 | { |
Radek Krejci | 10bfdf8 | 2019-06-10 14:08:13 +0200 | [diff] [blame] | 169 | const char *str, *errmsg; |
| 170 | const char *prefix, *id, *value; |
| 171 | size_t prefix_len, id_len, value_len; |
| 172 | |
| 173 | str = "[ex:name='fred']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 174 | 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] | 175 | assert_string_equal(str, ""); |
| 176 | assert_string_equal(prefix, "ex:name='fred']"); |
| 177 | assert_int_equal(prefix_len, 2); |
| 178 | assert_string_equal(id, "name='fred']"); |
| 179 | assert_int_equal(id_len, 4); |
| 180 | assert_string_equal(value, "fred']"); |
| 181 | assert_int_equal(value_len, 4); |
| 182 | |
| 183 | str = "[ex:ip = \"[192.0.2.1]\"][ex:port='80']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 184 | 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] | 185 | assert_string_equal(str, "[ex:port='80']"); |
| 186 | assert_string_equal(prefix, "ex:ip = \"[192.0.2.1]\"][ex:port='80']"); |
| 187 | assert_int_equal(prefix_len, 2); |
| 188 | assert_string_equal(id, "ip = \"[192.0.2.1]\"][ex:port='80']"); |
| 189 | assert_int_equal(id_len, 2); |
| 190 | assert_string_equal(value, "[192.0.2.1]\"][ex:port='80']"); |
| 191 | assert_int_equal(value_len, 11); |
| 192 | |
| 193 | str = "[. = 'blowfish-cbc']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 194 | 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] | 195 | assert_string_equal(str, ""); |
| 196 | assert_null(prefix); |
| 197 | assert_int_equal(prefix_len, 0); |
| 198 | assert_string_equal(id, ". = 'blowfish-cbc']"); |
| 199 | assert_int_equal(id_len, 1); |
| 200 | assert_string_equal(value, "blowfish-cbc']"); |
| 201 | assert_int_equal(value_len, 12); |
| 202 | |
| 203 | str = "[ 3 ]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 204 | 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] | 205 | assert_string_equal(str, ""); |
| 206 | assert_null(prefix); |
| 207 | assert_int_equal(prefix_len, 0); |
| 208 | assert_null(id); |
| 209 | assert_int_equal(id_len, 0); |
| 210 | assert_string_equal(value, "3 ]"); |
| 211 | assert_int_equal(value_len, 1); |
| 212 | |
| 213 | /* invalid predicates */ |
| 214 | /* position must be positive integer */ |
| 215 | str = "[0]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 216 | 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] | 217 | assert_string_equal(errmsg, "The position predicate cannot be zero."); |
| 218 | str = "[-1]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 219 | 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] | 220 | assert_string_equal(errmsg, "Invalid instance predicate format (negative position or invalid node-identifier)."); |
| 221 | |
| 222 | /* invalid node-identifier */ |
| 223 | str = "[$node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 224 | 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] | 225 | assert_string_equal(errmsg, "Invalid node-identifier."); |
| 226 | str = "[.node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 227 | 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] | 228 | assert_string_equal(errmsg, "Unexpected character instead of '=' in leaf-list-predicate."); |
| 229 | str = "[13node='value']"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 230 | 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] | 231 | assert_string_equal(errmsg, "Predicate (pos) is not terminated by \']\' character."); |
| 232 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 233 | str = "[ex:node]"; |
| 234 | 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] | 235 | assert_string_equal(errmsg, "Unexpected character instead of '=' in key-predicate."); |
| 236 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 237 | str = "[ex:node= value]"; |
| 238 | 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] | 239 | assert_string_equal(errmsg, "String value is not quoted."); |
| 240 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 241 | str = "[ex:node='value\"]"; |
| 242 | 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] | 243 | assert_string_equal(errmsg, "Value is not terminated quoted-string."); |
| 244 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 245 | str = "[ex:node='value ]"; |
| 246 | 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] | 247 | assert_string_equal(errmsg, "Value is not terminated quoted-string."); |
| 248 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 249 | str = "[ex:node=\"value\"[3]"; |
| 250 | 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] | 251 | assert_string_equal(errmsg, "Predicate (key-predicate) is not terminated by \']\' character."); |
| 252 | str = "[.=\"value\"[3]"; |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 253 | 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] | 254 | assert_string_equal(errmsg, "Predicate (leaf-list-predicate) is not terminated by \']\' character."); |
| 255 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 256 | /* the limit of the string is too short, it ends one character earlier */ |
| 257 | str = "[ex:node='value']"; |
| 258 | 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] | 259 | assert_string_equal(errmsg, "Predicate is incomplete."); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 262 | int |
| 263 | main(void) |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 264 | { |
| 265 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 266 | UTEST(test_utf8), |
| 267 | UTEST(test_parse_int), |
| 268 | UTEST(test_parse_uint), |
| 269 | UTEST(test_parse_nodeid), |
| 270 | UTEST(test_parse_instance_predicate), |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 274 | } |