Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 1 | /* |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_common.c |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 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 */ |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 29 | buf[0] = (char)0x04; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 30 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 31 | buf[0] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 32 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 33 | |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 34 | buf[0] = (char)0xc0; |
| 35 | buf[1] = (char)0x00; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 36 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 37 | buf[1] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 38 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 39 | |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 40 | buf[0] = (char)0xe0; |
| 41 | buf[1] = (char)0x00; |
| 42 | buf[2] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 43 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 44 | buf[1] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 45 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
| 46 | |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 47 | buf[0] = (char)0xf0; |
| 48 | buf[1] = (char)0x00; |
| 49 | buf[2] = (char)0x80; |
| 50 | buf[3] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 51 | assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len)); |
Michal Vasko | 7980d9c | 2021-09-22 12:18:46 +0200 | [diff] [blame] | 52 | buf[1] = (char)0x80; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 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 | |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 262 | static void |
| 263 | test_value_prefix_next(void **UNUSED(state)) |
| 264 | { |
| 265 | const char *next; |
| 266 | ly_bool is_prefix; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 267 | uint32_t bytes; |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 268 | |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 269 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(NULL, NULL, &bytes, &is_prefix, &next)); |
| 270 | assert_int_equal(0, bytes); |
| 271 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next("", NULL, &bytes, &is_prefix, &next)); |
| 272 | assert_int_equal(0, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 273 | |
| 274 | /* prefix */ |
| 275 | next = "pref:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 276 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 277 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 278 | assert_null(next); |
| 279 | assert_int_equal(1, is_prefix); |
| 280 | |
| 281 | /* no-prefix */ |
| 282 | next = "node"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 283 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 284 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 285 | assert_null(next); |
| 286 | assert_int_equal(0, is_prefix); |
| 287 | |
| 288 | /* no-prefix */ |
| 289 | next = "::::"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 290 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 291 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 292 | assert_null(next); |
| 293 | assert_int_equal(0, is_prefix); |
| 294 | |
| 295 | /* no-prefix */ |
| 296 | next = "//a/:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 297 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 298 | assert_int_equal(5, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 299 | assert_null(next); |
| 300 | assert_int_equal(0, is_prefix); |
| 301 | |
| 302 | /* no-prefix */ |
| 303 | next = "//a//"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 304 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 305 | assert_int_equal(5, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 306 | assert_null(next); |
| 307 | assert_int_equal(0, is_prefix); |
| 308 | |
| 309 | /* prefix, prefix */ |
| 310 | next = "pref1:pref2:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 311 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 312 | assert_int_equal(5, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 313 | assert_string_equal(next, "pref2:"); |
| 314 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 315 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 316 | assert_int_equal(5, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 317 | assert_null(next); |
| 318 | assert_int_equal(1, is_prefix); |
| 319 | |
| 320 | /* prefix, no-prefix */ |
| 321 | next = "pref:node"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 322 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 323 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 324 | assert_string_equal(next, "node"); |
| 325 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 326 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 327 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 328 | assert_null(next); |
| 329 | assert_int_equal(0, is_prefix); |
| 330 | |
| 331 | /* no-prefix, prefix */ |
| 332 | next = "/pref:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 333 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 334 | assert_int_equal(1, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 335 | assert_string_equal(next, "pref:"); |
| 336 | assert_int_equal(0, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 337 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 338 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 339 | assert_null(next); |
| 340 | assert_int_equal(1, is_prefix); |
| 341 | |
| 342 | /* no-prefix, prefix */ |
| 343 | next = "//pref:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 344 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 345 | assert_int_equal(2, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 346 | assert_string_equal(next, "pref:"); |
| 347 | assert_int_equal(0, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 348 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 349 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 350 | assert_null(next); |
| 351 | assert_int_equal(1, is_prefix); |
| 352 | |
| 353 | /* no-prefix, prefix, no-prefix */ |
Radek Krejci | 12a28c7 | 2021-04-06 17:23:37 +0200 | [diff] [blame] | 354 | next = "/pref:node"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 355 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 356 | assert_int_equal(1, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 357 | assert_string_equal(next, "pref:node"); |
| 358 | assert_int_equal(0, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 359 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 360 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 361 | assert_string_equal(next, "node"); |
| 362 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 363 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 364 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 365 | assert_null(next); |
| 366 | assert_int_equal(0, is_prefix); |
| 367 | |
| 368 | /* prefix, no-prefix, prefix */ |
Radek Krejci | 12a28c7 | 2021-04-06 17:23:37 +0200 | [diff] [blame] | 369 | next = "pref:node pref:"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 370 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 371 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 372 | assert_string_equal(next, "node pref:"); |
| 373 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 374 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 375 | assert_int_equal(5, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 376 | assert_string_equal(next, "pref:"); |
| 377 | assert_int_equal(0, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 378 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 379 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 380 | assert_null(next); |
| 381 | assert_int_equal(1, is_prefix); |
| 382 | |
| 383 | /* prefix, no-prefix, prefix, no-prefix */ |
| 384 | next = "pref:node /pref:node"; |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 385 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 386 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 387 | assert_string_equal(next, "node /pref:node"); |
| 388 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 389 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 390 | assert_int_equal(6, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 391 | assert_string_equal(next, "pref:node"); |
| 392 | assert_int_equal(0, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 393 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 394 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 395 | assert_string_equal(next, "node"); |
| 396 | assert_int_equal(1, is_prefix); |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 397 | assert_int_equal(LY_SUCCESS, ly_value_prefix_next(next, NULL, &bytes, &is_prefix, &next)); |
| 398 | assert_int_equal(4, bytes); |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 399 | assert_null(next); |
| 400 | assert_int_equal(0, is_prefix); |
| 401 | } |
| 402 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 403 | int |
| 404 | main(void) |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 405 | { |
| 406 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 407 | UTEST(test_utf8), |
| 408 | UTEST(test_parse_int), |
| 409 | UTEST(test_parse_uint), |
| 410 | UTEST(test_parse_nodeid), |
| 411 | UTEST(test_parse_instance_predicate), |
aPiecek | f0d8123 | 2021-03-30 12:19:59 +0200 | [diff] [blame] | 412 | UTEST(test_value_prefix_next), |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 416 | } |