Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 2 | * @file test_xpath.c |
| 3 | * @author: Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief unit tests for XPath evaluation |
| 5 | * |
| 6 | * Copyright (c) 2020 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_ |
| 15 | #include "utests.h" |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 17 | #include <string.h> |
| 18 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include "context.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 20 | #include "parser_data.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 21 | #include "set.h" |
Radek Krejci | ef5f767 | 2021-04-01 17:04:12 +0200 | [diff] [blame] | 22 | #include "tests_config.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
| 24 | #include "tree_schema.h" |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 25 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 26 | const char *schema_a = |
| 27 | "module a {\n" |
| 28 | " namespace urn:tests:a;\n" |
| 29 | " prefix a;\n" |
| 30 | " yang-version 1.1;\n" |
| 31 | "\n" |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 32 | " identity id_a;\n" |
| 33 | " identity id_b {\n" |
| 34 | " base id_a;\n" |
| 35 | " }\n" |
| 36 | " identity id_c {\n" |
| 37 | " base id_b;\n" |
| 38 | " }\n" |
| 39 | "\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 40 | " list l1 {\n" |
| 41 | " key \"a b\";\n" |
| 42 | " leaf a {\n" |
| 43 | " type string;\n" |
| 44 | " }\n" |
| 45 | " leaf b {\n" |
| 46 | " type string;\n" |
| 47 | " }\n" |
| 48 | " leaf c {\n" |
| 49 | " type string;\n" |
| 50 | " }\n" |
| 51 | " }\n" |
| 52 | " leaf foo {\n" |
| 53 | " type string;\n" |
| 54 | " }\n" |
| 55 | " leaf foo2 {\n" |
| 56 | " type uint8;\n" |
| 57 | " }\n" |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 58 | " leaf foo3 {\n" |
| 59 | " type identityref {\n" |
| 60 | " base id_a;\n" |
| 61 | " }\n" |
| 62 | " }\n" |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 63 | " leaf foo4 {\n" |
| 64 | " type decimal64 {\n" |
| 65 | " fraction-digits 5;\n" |
| 66 | " }\n" |
| 67 | " }\n" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 68 | " container c {\n" |
| 69 | " leaf x {\n" |
| 70 | " type string;\n" |
| 71 | " }\n" |
| 72 | " list ll {\n" |
| 73 | " key \"a\";\n" |
| 74 | " leaf a {\n" |
| 75 | " type string;\n" |
| 76 | " }\n" |
| 77 | " list ll {\n" |
| 78 | " key \"a\";\n" |
| 79 | " leaf a {\n" |
| 80 | " type string;\n" |
| 81 | " }\n" |
| 82 | " leaf b {\n" |
| 83 | " type string;\n" |
| 84 | " }\n" |
| 85 | " }\n" |
| 86 | " }\n" |
| 87 | " leaf-list ll2 {\n" |
| 88 | " type string;\n" |
| 89 | " }\n" |
| 90 | " }\n" |
| 91 | "}"; |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 92 | |
| 93 | static int |
| 94 | setup(void **state) |
| 95 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 96 | UTEST_SETUP; |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 97 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 98 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 99 | lys_parse_path(UTEST_LYCTX, TESTS_DIR_MODULES_YANG "/ietf-interfaces@2014-05-08.yang", LYS_IN_YANG, NULL); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 104 | static void |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 105 | test_predicate(void **state) |
| 106 | { |
| 107 | const char *data; |
| 108 | struct lyd_node *tree; |
| 109 | struct ly_set *set; |
| 110 | |
| 111 | data = |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 112 | "<foo2 xmlns=\"urn:tests:a\">50</foo2>" |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 113 | "<l1 xmlns=\"urn:tests:a\">" |
| 114 | " <a>a1</a>" |
| 115 | " <b>b1</b>" |
| 116 | " <c>c1</c>" |
| 117 | "</l1>" |
| 118 | "<l1 xmlns=\"urn:tests:a\">" |
| 119 | " <a>a2</a>" |
| 120 | " <b>b2</b>" |
| 121 | "</l1>" |
| 122 | "<l1 xmlns=\"urn:tests:a\">" |
| 123 | " <a>a3</a>" |
| 124 | " <b>b3</b>" |
| 125 | "</l1>" |
| 126 | "<l1 xmlns=\"urn:tests:a\">" |
| 127 | " <a>a4</a>" |
| 128 | " <b>b4</b>" |
| 129 | " <c>c4</c>" |
| 130 | "</l1>" |
| 131 | "<l1 xmlns=\"urn:tests:a\">" |
| 132 | " <a>a5</a>" |
| 133 | " <b>b5</b>" |
| 134 | " <c>c5</c>" |
| 135 | "</l1>" |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 136 | "<c xmlns=\"urn:tests:a\">" |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 137 | " <x>key2</x>" |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 138 | " <ll>" |
| 139 | " <a>key1</a>" |
| 140 | " <ll>" |
| 141 | " <a>key11</a>" |
| 142 | " <b>val11</b>" |
| 143 | " </ll>" |
| 144 | " <ll>" |
| 145 | " <a>key12</a>" |
| 146 | " <b>val12</b>" |
| 147 | " </ll>" |
| 148 | " <ll>" |
| 149 | " <a>key13</a>" |
| 150 | " <b>val13</b>" |
| 151 | " </ll>" |
| 152 | " </ll>" |
| 153 | " <ll>" |
| 154 | " <a>key2</a>" |
| 155 | " <ll>" |
| 156 | " <a>key21</a>" |
| 157 | " <b>val21</b>" |
| 158 | " </ll>" |
| 159 | " <ll>" |
| 160 | " <a>key22</a>" |
| 161 | " <b>val22</b>" |
| 162 | " </ll>" |
| 163 | " </ll>" |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 164 | " <ll>" |
| 165 | " <a>key3</a>" |
| 166 | " <ll>" |
| 167 | " <a>key31</a>" |
| 168 | " <b>val31</b>" |
| 169 | " </ll>" |
| 170 | " <ll>" |
| 171 | " <a>key32</a>" |
| 172 | " <b>val32</b>" |
| 173 | " </ll>" |
| 174 | " </ll>" |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 175 | "</c>"; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 176 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 177 | assert_non_null(tree); |
| 178 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 179 | /* predicate after number */ |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 180 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/foo2[4[3 = 3]]", &set)); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 181 | assert_int_equal(0, set->count); |
| 182 | ly_set_free(set, NULL); |
| 183 | |
| 184 | /* reverse axis */ |
| 185 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/child::ll[2]/preceding::ll[3]", &set)); |
| 186 | assert_int_equal(1, set->count); |
| 187 | assert_string_equal("key11", lyd_get_value(lyd_child(set->dnodes[0]))); |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 188 | ly_set_free(set, NULL); |
| 189 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 190 | /* special predicate evaluated using hashes */ |
| 191 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:l1[a=concat('a', '1')][b=substring('ab1',2)]", &set)); |
| 192 | assert_int_equal(1, set->count); |
| 193 | ly_set_free(set, NULL); |
| 194 | |
| 195 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll[a=../x]", &set)); |
| 196 | assert_int_equal(1, set->count); |
| 197 | ly_set_free(set, NULL); |
| 198 | |
| 199 | /* cannot use hashes */ |
| 200 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll[a=substring(ll/a,1,4)]", &set)); |
| 201 | assert_int_equal(3, set->count); |
| 202 | ly_set_free(set, NULL); |
| 203 | |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 204 | /* nested predicate */ |
| 205 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/a:ll[a:a=string(/a:l1[a:a='foo']/a:a)]/a:a", &set)); |
| 206 | assert_int_equal(0, set->count); |
| 207 | ly_set_free(set, NULL); |
| 208 | |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 209 | lyd_free_all(tree); |
| 210 | } |
| 211 | |
| 212 | static void |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 213 | test_union(void **state) |
| 214 | { |
| 215 | const char *data; |
| 216 | struct lyd_node *tree; |
| 217 | struct ly_set *set; |
| 218 | |
| 219 | data = |
| 220 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 221 | " <a>a1</a>\n" |
| 222 | " <b>b1</b>\n" |
| 223 | " <c>c1</c>\n" |
| 224 | "</l1>\n" |
| 225 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 226 | " <a>a2</a>\n" |
| 227 | " <b>b2</b>\n" |
| 228 | "</l1>" |
| 229 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 230 | " <a>a3</a>\n" |
| 231 | " <b>b3</b>\n" |
| 232 | " <c>c3</c>\n" |
| 233 | "</l1>"; |
| 234 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 235 | assert_non_null(tree); |
| 236 | |
| 237 | /* Predicate for operand. */ |
| 238 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/l1[c[../a = 'a1'] | c]/a", &set)); |
| 239 | ly_set_free(set, NULL); |
| 240 | |
| 241 | lyd_free_all(tree); |
| 242 | } |
| 243 | |
| 244 | static void |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 245 | test_invalid(void **state) |
| 246 | { |
| 247 | const char *data = |
| 248 | "<foo2 xmlns=\"urn:tests:a\">50</foo2>"; |
| 249 | struct lyd_node *tree; |
| 250 | struct ly_set *set; |
| 251 | |
| 252 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 253 | assert_non_null(tree); |
| 254 | |
| 255 | assert_int_equal(LY_EVALID, lyd_find_xpath(tree, "/a:foo2[.=]", &set)); |
| 256 | assert_null(set); |
| 257 | |
| 258 | assert_int_equal(LY_EVALID, lyd_find_xpath(tree, "/a:", &set)); |
| 259 | assert_null(set); |
| 260 | |
| 261 | lyd_free_all(tree); |
| 262 | } |
| 263 | |
| 264 | static void |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 265 | test_hash(void **state) |
| 266 | { |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 267 | const char *data = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 268 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 269 | " <a>a1</a>\n" |
| 270 | " <b>b1</b>\n" |
| 271 | " <c>c1</c>\n" |
| 272 | "</l1>\n" |
| 273 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 274 | " <a>a2</a>\n" |
| 275 | " <b>b2</b>\n" |
| 276 | "</l1>\n" |
| 277 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 278 | " <a>a3</a>\n" |
| 279 | " <b>b3</b>\n" |
| 280 | " <c>c3</c>\n" |
| 281 | "</l1>\n" |
| 282 | "<foo xmlns=\"urn:tests:a\">foo value</foo>\n" |
| 283 | "<c xmlns=\"urn:tests:a\">\n" |
| 284 | " <x>val</x>\n" |
| 285 | " <ll>\n" |
| 286 | " <a>val_a</a>\n" |
| 287 | " <ll>\n" |
| 288 | " <a>val_a</a>\n" |
| 289 | " <b>val</b>\n" |
| 290 | " </ll>\n" |
| 291 | " <ll>\n" |
| 292 | " <a>val_b</a>\n" |
| 293 | " </ll>\n" |
| 294 | " </ll>\n" |
| 295 | " <ll>\n" |
| 296 | " <a>val_b</a>\n" |
| 297 | " <ll>\n" |
| 298 | " <a>val_a</a>\n" |
| 299 | " </ll>\n" |
| 300 | " <ll>\n" |
| 301 | " <a>val_b</a>\n" |
| 302 | " <b>val</b>\n" |
| 303 | " </ll>\n" |
| 304 | " </ll>\n" |
| 305 | " <ll>\n" |
| 306 | " <a>val_c</a>\n" |
| 307 | " <ll>\n" |
| 308 | " <a>val_a</a>\n" |
| 309 | " </ll>\n" |
| 310 | " <ll>\n" |
| 311 | " <a>val_b</a>\n" |
| 312 | " </ll>\n" |
| 313 | " </ll>\n" |
| 314 | " <ll2>one</ll2>\n" |
| 315 | " <ll2>two</ll2>\n" |
| 316 | " <ll2>three</ll2>\n" |
| 317 | " <ll2>four</ll2>\n" |
| 318 | "</c>"; |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 319 | struct lyd_node *tree, *node; |
| 320 | struct ly_set *set; |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 321 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 322 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 323 | assert_non_null(tree); |
| 324 | |
| 325 | /* top-level, so hash table is not ultimately used but instances can be compared based on hashes */ |
| 326 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:l1[a='a3'][b='b3']", &set)); |
| 327 | assert_int_equal(1, set->count); |
| 328 | |
| 329 | node = set->objs[0]; |
| 330 | assert_string_equal(node->schema->name, "l1"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 331 | node = lyd_child(node); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 332 | assert_string_equal(node->schema->name, "a"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 333 | assert_string_equal(lyd_get_value(node), "a3"); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 334 | |
| 335 | ly_set_free(set, NULL); |
| 336 | |
| 337 | /* hashes should be used for both searches (well, there are not enough nested ll instances, so technically not true) */ |
| 338 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll[a='val_b']/ll[a='val_b']", &set)); |
| 339 | assert_int_equal(1, set->count); |
| 340 | |
| 341 | node = set->objs[0]; |
| 342 | assert_string_equal(node->schema->name, "ll"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 343 | node = lyd_child(node); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 344 | assert_string_equal(node->schema->name, "a"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 345 | assert_string_equal(lyd_get_value(node), "val_b"); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 346 | node = node->next; |
| 347 | assert_string_equal(node->schema->name, "b"); |
| 348 | assert_null(node->next); |
| 349 | |
| 350 | ly_set_free(set, NULL); |
| 351 | |
| 352 | /* hashes are not used */ |
| 353 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c//ll[a='val_b']", &set)); |
| 354 | assert_int_equal(4, set->count); |
| 355 | |
| 356 | ly_set_free(set, NULL); |
| 357 | |
Michal Vasko | 660cc8f | 2020-05-25 10:33:19 +0200 | [diff] [blame] | 358 | /* hashes used even for leaf-lists */ |
| 359 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll2[. = 'three']", &set)); |
| 360 | assert_int_equal(1, set->count); |
| 361 | |
| 362 | node = set->objs[0]; |
| 363 | assert_string_equal(node->schema->name, "ll2"); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 364 | assert_string_equal(lyd_get_value(node), "three"); |
Michal Vasko | 660cc8f | 2020-05-25 10:33:19 +0200 | [diff] [blame] | 365 | |
| 366 | ly_set_free(set, NULL); |
| 367 | |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 368 | /* not found using hashes */ |
| 369 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll[a='val_d']", &set)); |
| 370 | assert_int_equal(0, set->count); |
| 371 | |
| 372 | ly_set_free(set, NULL); |
| 373 | |
| 374 | /* white-spaces are also ok */ |
| 375 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/ll[ \na = 'val_c' ]", &set)); |
| 376 | assert_int_equal(1, set->count); |
| 377 | |
| 378 | ly_set_free(set, NULL); |
| 379 | |
| 380 | lyd_free_all(tree); |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 383 | static void |
| 384 | test_toplevel(void **state) |
| 385 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 386 | const char *schema_b = |
| 387 | "module b {\n" |
| 388 | " namespace urn:tests:b;\n" |
| 389 | " prefix b;\n" |
| 390 | " yang-version 1.1;\n" |
| 391 | "\n" |
| 392 | " list l2 {\n" |
| 393 | " key \"a\";\n" |
| 394 | " leaf a {\n" |
| 395 | " type uint16;\n" |
| 396 | " }\n" |
| 397 | " leaf b {\n" |
| 398 | " type uint16;\n" |
| 399 | " }\n" |
| 400 | " }\n" |
| 401 | "}"; |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 402 | const char *data = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 403 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 404 | " <a>a1</a>\n" |
| 405 | " <b>b1</b>\n" |
| 406 | " <c>c1</c>\n" |
| 407 | "</l1>\n" |
| 408 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 409 | " <a>a2</a>\n" |
| 410 | " <b>b2</b>\n" |
| 411 | "</l1>\n" |
| 412 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 413 | " <a>a3</a>\n" |
| 414 | " <b>b3</b>\n" |
| 415 | " <c>c3</c>\n" |
| 416 | "</l1>\n" |
| 417 | "<foo xmlns=\"urn:tests:a\">foo value</foo>\n" |
| 418 | "<l2 xmlns=\"urn:tests:b\">\n" |
| 419 | " <a>1</a>\n" |
| 420 | " <b>1</b>\n" |
| 421 | "</l2>\n" |
| 422 | "<l2 xmlns=\"urn:tests:b\">\n" |
| 423 | " <a>2</a>\n" |
| 424 | " <b>1</b>\n" |
| 425 | "</l2>\n" |
| 426 | "<l2 xmlns=\"urn:tests:b\">\n" |
| 427 | " <a>3</a>\n" |
| 428 | " <b>1</b>\n" |
| 429 | "</l2>"; |
Michal Vasko | f60e710 | 2020-05-26 10:48:59 +0200 | [diff] [blame] | 430 | struct lyd_node *tree; |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 431 | struct ly_set *set; |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 432 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 433 | UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL); |
| 434 | |
| 435 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 436 | assert_non_null(tree); |
| 437 | |
| 438 | /* all top-level nodes from one module (default container as well) */ |
| 439 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:*", &set)); |
| 440 | assert_int_equal(5, set->count); |
| 441 | |
| 442 | ly_set_free(set, NULL); |
| 443 | |
| 444 | /* all top-level nodes from all modules */ |
| 445 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/*", &set)); |
| 446 | assert_int_equal(8, set->count); |
| 447 | |
| 448 | ly_set_free(set, NULL); |
| 449 | |
| 450 | /* all nodes from one module */ |
| 451 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//a:*", &set)); |
| 452 | assert_int_equal(13, set->count); |
| 453 | |
| 454 | ly_set_free(set, NULL); |
| 455 | |
| 456 | /* all nodes from all modules */ |
| 457 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//*", &set)); |
| 458 | assert_int_equal(22, set->count); |
| 459 | |
| 460 | ly_set_free(set, NULL); |
| 461 | |
| 462 | /* all nodes from all modules #2 */ |
| 463 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//.", &set)); |
| 464 | assert_int_equal(22, set->count); |
| 465 | |
| 466 | ly_set_free(set, NULL); |
| 467 | |
| 468 | lyd_free_all(tree); |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 471 | static void |
| 472 | test_atomize(void **state) |
| 473 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 474 | struct ly_set *set; |
| 475 | const struct lys_module *mod; |
| 476 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 477 | mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a"); |
| 478 | assert_non_null(mod); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 479 | |
| 480 | /* some random paths just making sure the API function works */ |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 481 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:*", 0, &set)); |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 482 | assert_int_equal(6, set->count); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 483 | ly_set_free(set, NULL); |
| 484 | |
| 485 | /* all nodes from all modules (including internal, which can change easily, so check just the test modules) */ |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 486 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "//.", 0, &set)); |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 487 | assert_in_range(set->count, 17, UINT32_MAX); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 488 | ly_set_free(set, NULL); |
| 489 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 490 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:c/ll[a='val1']/ll[a='val2']/b", 0, &set)); |
| 491 | assert_int_equal(6, set->count); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 492 | ly_set_free(set, NULL); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 493 | |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 494 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/ietf-interfaces:interfaces/*", 0, &set)); |
| 495 | assert_int_equal(2, set->count); |
| 496 | ly_set_free(set, NULL); |
| 497 | |
| 498 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/*", 0, &set)); |
| 499 | assert_int_equal(13, set->count); |
| 500 | ly_set_free(set, NULL); |
| 501 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 502 | /* |
| 503 | * axes |
| 504 | */ |
| 505 | |
| 506 | /* ancestor */ |
| 507 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "//ll[a and b]/a/ancestor::node()", 0, &set)); |
| 508 | assert_int_equal(6, set->count); |
| 509 | ly_set_free(set, NULL); |
| 510 | |
| 511 | /* ancestor-or-self */ |
| 512 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "//ll[a and b]/ancestor-or-self::ll", 0, &set)); |
| 513 | assert_int_equal(5, set->count); |
| 514 | ly_set_free(set, NULL); |
| 515 | |
| 516 | /* attribute */ |
| 517 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/l1/attribute::key", 0, &set)); |
| 518 | assert_int_equal(1, set->count); |
| 519 | ly_set_free(set, NULL); |
| 520 | |
| 521 | /* child */ |
| 522 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/child::l1/child::a", 0, &set)); |
| 523 | assert_int_equal(2, set->count); |
| 524 | ly_set_free(set, NULL); |
| 525 | |
| 526 | /* descendant */ |
| 527 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/descendant::c/descendant::b", 0, &set)); |
| 528 | assert_int_equal(3, set->count); |
| 529 | ly_set_free(set, NULL); |
| 530 | |
| 531 | /* descendant-or-self */ |
| 532 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:*/descendant-or-self::c", 0, &set)); |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 533 | assert_int_equal(7, set->count); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 534 | ly_set_free(set, NULL); |
| 535 | |
| 536 | /* following */ |
| 537 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/c/x/following::a", 0, &set)); |
| 538 | assert_int_equal(4, set->count); |
| 539 | ly_set_free(set, NULL); |
| 540 | |
| 541 | /* following-sibling */ |
| 542 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/c/x/following-sibling::ll", 0, &set)); |
| 543 | assert_int_equal(3, set->count); |
| 544 | ly_set_free(set, NULL); |
| 545 | |
| 546 | /* parent */ |
| 547 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/child::a:*/c/parent::l1", 0, &set)); |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 548 | assert_int_equal(7, set->count); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 549 | ly_set_free(set, NULL); |
| 550 | |
| 551 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/child::a:c//..", 0, &set)); |
| 552 | assert_int_equal(8, set->count); |
| 553 | ly_set_free(set, NULL); |
| 554 | |
| 555 | /* preceding */ |
| 556 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/c/preceding::a", 0, &set)); |
| 557 | assert_int_equal(2, set->count); |
| 558 | ly_set_free(set, NULL); |
| 559 | |
| 560 | /* preceding-sibling */ |
| 561 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/c/ll/preceding-sibling::node()", 0, &set)); |
| 562 | assert_int_equal(3, set->count); |
| 563 | ly_set_free(set, NULL); |
| 564 | |
| 565 | /* self */ |
| 566 | assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/c/self::c/ll/ll/b/self::b", 0, &set)); |
| 567 | assert_int_equal(4, set->count); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 568 | ly_set_free(set, NULL); |
| 569 | } |
| 570 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 571 | static void |
| 572 | test_canonize(void **state) |
| 573 | { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 574 | const char *data = |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 575 | "<foo2 xmlns=\"urn:tests:a\">50</foo2>" |
| 576 | "<foo3 xmlns=\"urn:tests:a\" xmlns:a=\"urn:tests:a\">a:id_b</foo3>" |
| 577 | "<foo4 xmlns=\"urn:tests:a\">250.5</foo4>"; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 578 | struct lyd_node *tree; |
| 579 | struct ly_set *set; |
| 580 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 581 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 582 | assert_non_null(tree); |
| 583 | |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 584 | /* integer */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 585 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:foo2[.='050']", &set)); |
| 586 | assert_int_equal(1, set->count); |
| 587 | ly_set_free(set, NULL); |
| 588 | |
Michal Vasko | 0009908 | 2022-07-28 10:07:41 +0200 | [diff] [blame] | 589 | /* identityref */ |
| 590 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:foo3[.='id_b']", &set)); |
| 591 | assert_int_equal(1, set->count); |
| 592 | ly_set_free(set, NULL); |
| 593 | |
| 594 | /* decimal64 */ |
| 595 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:foo4[.='0250.500']", &set)); |
| 596 | assert_int_equal(1, set->count); |
| 597 | ly_set_free(set, NULL); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 598 | |
| 599 | lyd_free_all(tree); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 602 | static void |
| 603 | test_derived_from(void **state) |
| 604 | { |
| 605 | const char *data = |
| 606 | "<foo3 xmlns=\"urn:tests:a\">id_c</foo3>"; |
| 607 | struct lyd_node *tree; |
| 608 | struct ly_set *set; |
| 609 | |
| 610 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 611 | assert_non_null(tree); |
| 612 | |
| 613 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:foo3[derived-from(., 'a:id_b')]", &set)); |
| 614 | assert_int_equal(1, set->count); |
| 615 | ly_set_free(set, NULL); |
| 616 | |
| 617 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:foo3[derived-from(., 'a:id_a')]", &set)); |
| 618 | assert_int_equal(1, set->count); |
| 619 | ly_set_free(set, NULL); |
| 620 | |
| 621 | lyd_free_all(tree); |
| 622 | } |
| 623 | |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 624 | static void |
| 625 | test_augment(void **state) |
| 626 | { |
| 627 | const char *schema_b = |
| 628 | "module b {\n" |
| 629 | " namespace urn:tests:b;\n" |
| 630 | " prefix b;\n" |
| 631 | " yang-version 1.1;\n" |
| 632 | "\n" |
| 633 | " import a {\n" |
| 634 | " prefix a;\n" |
| 635 | " }\n" |
| 636 | "\n" |
| 637 | " augment /a:c {\n" |
| 638 | " leaf a {\n" |
| 639 | " type uint16;\n" |
| 640 | " }\n" |
| 641 | " }\n" |
| 642 | "}"; |
| 643 | const char *data = |
| 644 | "<c xmlns=\"urn:tests:a\">\n" |
| 645 | " <x>value</x>\n" |
| 646 | " <ll>\n" |
| 647 | " <a>key</a>\n" |
| 648 | " </ll>\n" |
| 649 | " <a xmlns=\"urn:tests:b\">25</a>\n" |
| 650 | " <ll2>c1</ll2>\n" |
| 651 | "</c>"; |
| 652 | struct lyd_node *tree; |
| 653 | struct ly_set *set; |
| 654 | |
| 655 | UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL); |
| 656 | |
| 657 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 658 | assert_non_null(tree); |
| 659 | |
| 660 | /* get all children ignoring their module */ |
| 661 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/*", &set)); |
| 662 | assert_int_equal(4, set->count); |
| 663 | |
| 664 | ly_set_free(set, NULL); |
| 665 | |
| 666 | lyd_free_all(tree); |
| 667 | } |
| 668 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 669 | static void |
| 670 | test_variables(void **state) |
| 671 | { |
| 672 | struct lyd_node *tree, *node; |
| 673 | struct ly_set *set; |
| 674 | const char *data; |
| 675 | struct lyxp_var *vars = NULL; |
| 676 | |
| 677 | #define LOCAL_SETUP(DATA, TREE) \ |
| 678 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, DATA, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &TREE)); \ |
| 679 | assert_non_null(TREE); |
| 680 | |
| 681 | #define SET_NODE(NODE, SET, INDEX) \ |
| 682 | assert_non_null(SET); \ |
| 683 | assert_true(INDEX < SET->count); \ |
| 684 | NODE = SET->objs[INDEX]; |
| 685 | |
| 686 | #define LOCAL_TEARDOWN(SET, TREE, VARS) \ |
| 687 | ly_set_free(SET, NULL); \ |
| 688 | lyd_free_all(TREE); \ |
| 689 | lyxp_vars_free(VARS); \ |
| 690 | vars = NULL; |
| 691 | |
| 692 | /* Eval variable to number. */ |
| 693 | data = |
| 694 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 695 | " <a>a1</a>\n" |
| 696 | " <b>b1</b>\n" |
| 697 | " <c>c1</c>\n" |
| 698 | "</l1>" |
| 699 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 700 | " <a>a2</a>\n" |
| 701 | " <b>b2</b>\n" |
| 702 | " <c>c2</c>\n" |
| 703 | "</l1>"; |
| 704 | LOCAL_SETUP(data, tree); |
| 705 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "2")); |
| 706 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var]/a", vars, &set)); |
| 707 | SET_NODE(node, set, 0); |
| 708 | assert_string_equal(lyd_get_value(node), "a2"); |
| 709 | LOCAL_TEARDOWN(set, tree, vars); |
| 710 | |
| 711 | /* Eval variable to string. */ |
| 712 | data = |
| 713 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 714 | LOCAL_SETUP(data, tree); |
| 715 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "\"mstr\"")); |
| 716 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/foo[text() = $var]", vars, &set)); |
| 717 | SET_NODE(node, set, 0); |
| 718 | assert_string_equal(lyd_get_value(node), "mstr"); |
| 719 | LOCAL_TEARDOWN(set, tree, vars); |
| 720 | |
| 721 | /* Eval variable to set of nodes. */ |
| 722 | data = |
| 723 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 724 | " <a>a1</a>\n" |
| 725 | " <b>b1</b>\n" |
| 726 | "</l1>" |
| 727 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 728 | " <a>a2</a>\n" |
| 729 | " <b>b2</b>\n" |
| 730 | " <c>c2</c>\n" |
| 731 | "</l1>"; |
| 732 | LOCAL_SETUP(data, tree); |
| 733 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "c")); |
| 734 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var]/a", vars, &set)); |
| 735 | SET_NODE(node, set, 0); |
| 736 | assert_string_equal(lyd_get_value(node), "a2"); |
| 737 | LOCAL_TEARDOWN(set, tree, vars); |
| 738 | |
| 739 | /* Variable in union expr. */ |
| 740 | data = |
| 741 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 742 | " <a>a1</a>\n" |
| 743 | " <b>b1</b>\n" |
| 744 | " <c>c1</c>\n" |
| 745 | "</l1>" |
| 746 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 747 | " <a>a2</a>\n" |
| 748 | " <b>b2</b>\n" |
| 749 | " <c>c2</c>\n" |
| 750 | "</l1>" |
| 751 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 752 | " <a>a3</a>\n" |
| 753 | " <b>b3</b>\n" |
| 754 | " <c>c3</c>\n" |
| 755 | "</l1>"; |
| 756 | LOCAL_SETUP(data, tree); |
| 757 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "c[../a = 'a3']")); |
| 758 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[c[../a = 'a1'] | $var]/a", vars, &set)); |
| 759 | SET_NODE(node, set, 0); |
| 760 | assert_string_equal(lyd_get_value(node), "a1"); |
| 761 | SET_NODE(node, set, 1); |
| 762 | assert_string_equal(lyd_get_value(node), "a3"); |
| 763 | assert_int_equal(set->count, 2); |
| 764 | LOCAL_TEARDOWN(set, tree, vars); |
| 765 | |
| 766 | /* Predicate after variable. */ |
| 767 | data = |
| 768 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 769 | " <a>a1</a>\n" |
| 770 | " <b>b1</b>\n" |
| 771 | " <c>c1</c>\n" |
| 772 | "</l1>" |
| 773 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 774 | " <a>a2</a>\n" |
| 775 | " <b>b2</b>\n" |
| 776 | " <c>c2</c>\n" |
| 777 | "</l1>"; |
| 778 | LOCAL_SETUP(data, tree); |
| 779 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "c")); |
| 780 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var[../a = 'a1']]/a", vars, &set)); |
| 781 | SET_NODE(node, set, 0); |
| 782 | assert_string_equal(lyd_get_value(node), "a1"); |
| 783 | LOCAL_TEARDOWN(set, tree, vars); |
| 784 | |
| 785 | /* Variable in variable. */ |
| 786 | data = |
| 787 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 788 | LOCAL_SETUP(data, tree); |
| 789 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "$var2")); |
| 790 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "\"mstr\"")); |
| 791 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/foo[text() = $var]", vars, &set)); |
| 792 | SET_NODE(node, set, 0); |
| 793 | assert_string_equal(lyd_get_value(node), "mstr"); |
| 794 | LOCAL_TEARDOWN(set, tree, vars); |
| 795 | |
| 796 | /* Compare two variables. */ |
| 797 | data = |
| 798 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 799 | LOCAL_SETUP(data, tree); |
| 800 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "\"str\"")); |
| 801 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "\"str\"")); |
| 802 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/foo[$var1 = $var2]", vars, &set)); |
| 803 | SET_NODE(node, set, 0); |
| 804 | assert_string_equal(lyd_get_value(node), "mstr"); |
| 805 | LOCAL_TEARDOWN(set, tree, vars); |
| 806 | |
| 807 | /* Arithmetic operation with variable. */ |
| 808 | data = |
| 809 | "<foo2 xmlns=\"urn:tests:a\">4</foo2>"; |
| 810 | LOCAL_SETUP(data, tree); |
| 811 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "2")); |
| 812 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/foo2[.= ($var1 * 2)]", vars, &set)); |
| 813 | SET_NODE(node, set, 0); |
| 814 | assert_string_equal(lyd_get_value(node), "4"); |
| 815 | LOCAL_TEARDOWN(set, tree, vars); |
| 816 | |
| 817 | /* Variable as function parameter. */ |
| 818 | data = |
| 819 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 820 | " <a>a1</a>\n" |
| 821 | " <b>b1</b>\n" |
| 822 | " <c>c1</c>\n" |
| 823 | "</l1>" |
| 824 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 825 | " <a>a2</a>\n" |
| 826 | " <b>b2</b>\n" |
| 827 | "</l1>"; |
| 828 | LOCAL_SETUP(data, tree); |
| 829 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "./c")); |
| 830 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[count($var) = 1]/a", vars, &set)); |
| 831 | SET_NODE(node, set, 0); |
| 832 | assert_string_equal(lyd_get_value(node), "a1"); |
| 833 | LOCAL_TEARDOWN(set, tree, vars); |
| 834 | |
| 835 | /* Variable in path expr. */ |
| 836 | /* NOTE: The variable can only be at the beginning of the expression path. */ |
| 837 | data = |
| 838 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 839 | " <a>a1</a>\n" |
| 840 | " <b>b1</b>\n" |
| 841 | " <c>c1</c>\n" |
| 842 | "</l1>" |
| 843 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 844 | " <a>a2</a>\n" |
| 845 | " <b>b2</b>\n" |
| 846 | "</l1>"; |
| 847 | LOCAL_SETUP(data, tree); |
| 848 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "/l1")); |
| 849 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var/a]", vars, &set)); |
| 850 | assert_int_equal(set->count, 2); |
| 851 | LOCAL_TEARDOWN(set, tree, vars); |
| 852 | |
| 853 | /* Variable as function. */ |
| 854 | data = |
| 855 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 856 | " <a>a1</a>\n" |
| 857 | " <b>b1</b>\n" |
| 858 | " <c>c1</c>\n" |
| 859 | "</l1>" |
| 860 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 861 | " <a>a2</a>\n" |
| 862 | " <b>b2</b>\n" |
| 863 | "</l1>"; |
| 864 | LOCAL_SETUP(data, tree); |
| 865 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "position()")); |
| 866 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var = 2]/a", vars, &set)); |
| 867 | SET_NODE(node, set, 0); |
| 868 | assert_string_equal(lyd_get_value(node), "a2"); |
| 869 | LOCAL_TEARDOWN(set, tree, vars); |
| 870 | |
| 871 | /* Dynamic change of value. */ |
| 872 | data = |
| 873 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 874 | " <a>a1</a>\n" |
| 875 | " <b>b1</b>\n" |
| 876 | " <c>c1</c>\n" |
| 877 | "</l1>" |
| 878 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 879 | " <a>a2</a>\n" |
| 880 | " <b>b2</b>\n" |
| 881 | " <c>c2</c>\n" |
| 882 | "</l1>"; |
| 883 | LOCAL_SETUP(data, tree); |
| 884 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "1")); |
| 885 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var]/a", vars, &set)); |
| 886 | SET_NODE(node, set, 0); |
| 887 | assert_string_equal(lyd_get_value(node), "a1"); |
| 888 | ly_set_free(set, NULL); |
| 889 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "2")); |
| 890 | assert_int_equal(LY_SUCCESS, lyd_find_xpath2(tree, "/l1[$var]/a", vars, &set)); |
| 891 | SET_NODE(node, set, 0); |
| 892 | assert_string_equal(lyd_get_value(node), "a2"); |
| 893 | LOCAL_TEARDOWN(set, tree, vars); |
| 894 | |
| 895 | /* Variable not defined. */ |
| 896 | data = |
| 897 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 898 | LOCAL_SETUP(data, tree); |
| 899 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "\"mstr\"")); |
| 900 | assert_int_equal(LY_ENOTFOUND, lyd_find_xpath2(tree, "/foo[text() = $var55]", vars, &set)); |
| 901 | LOCAL_TEARDOWN(set, tree, vars); |
| 902 | |
| 903 | /* Syntax error in value. */ |
| 904 | data = |
| 905 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 906 | LOCAL_SETUP(data, tree); |
| 907 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "\"")); |
| 908 | assert_int_equal(LY_EVALID, lyd_find_xpath2(tree, "/foo[$var]", vars, &set)); |
| 909 | LOCAL_TEARDOWN(set, tree, vars); |
| 910 | |
| 911 | /* Prefix is not supported. */ |
| 912 | data = |
| 913 | "<foo xmlns=\"urn:tests:a\">mstr</foo>"; |
| 914 | LOCAL_SETUP(data, tree); |
| 915 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var", "\"")); |
| 916 | assert_int_equal(LY_EVALID, lyd_find_xpath2(tree, "/foo[$pref:var]", vars, &set)); |
| 917 | assert_string_equal("Variable with prefix is not supported.", _UC->err_msg); |
| 918 | LOCAL_TEARDOWN(set, tree, vars); |
| 919 | |
| 920 | #undef LOCAL_SETUP |
| 921 | #undef LOCAL_TEARDOWN |
| 922 | } |
| 923 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 924 | static void |
| 925 | test_axes(void **state) |
| 926 | { |
| 927 | const char *data; |
| 928 | struct lyd_node *tree; |
| 929 | struct ly_set *set; |
| 930 | |
| 931 | data = |
| 932 | "<l1 xmlns=\"urn:tests:a\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\">\n" |
| 933 | " <a>a1</a>\n" |
| 934 | " <b yang:operation=\"replace\">b1</b>\n" |
| 935 | " <c yang:operation=\"none\">c1</c>\n" |
| 936 | "</l1>\n" |
| 937 | "<l1 xmlns=\"urn:tests:a\">\n" |
| 938 | " <a>a2</a>\n" |
| 939 | " <b>b2</b>\n" |
| 940 | "</l1>" |
| 941 | "<l1 xmlns=\"urn:tests:a\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\">\n" |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 942 | " <a yang:operation=\"none\" yang:key=\"[no-key='no-value']\">a3</a>\n" |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 943 | " <b>b3</b>\n" |
| 944 | " <c yang:value=\"no-val\">c3</c>\n" |
| 945 | "</l1>" |
| 946 | "<c xmlns=\"urn:tests:a\">" |
| 947 | " <x>val</x>" |
| 948 | " <ll>" |
| 949 | " <a>key1</a>" |
| 950 | " <ll>" |
| 951 | " <a>key11</a>" |
| 952 | " <b>val11</b>" |
| 953 | " </ll>" |
| 954 | " <ll>" |
| 955 | " <a>key12</a>" |
| 956 | " <b>val12</b>" |
| 957 | " </ll>" |
| 958 | " <ll>" |
| 959 | " <a>key13</a>" |
| 960 | " <b>val13</b>" |
| 961 | " </ll>" |
| 962 | " </ll>" |
| 963 | " <ll>" |
| 964 | " <a>key2</a>" |
| 965 | " <ll>" |
| 966 | " <a>key21</a>" |
| 967 | " <b>val21</b>" |
| 968 | " </ll>" |
| 969 | " <ll>" |
| 970 | " <a>key22</a>" |
| 971 | " <b>val22</b>" |
| 972 | " </ll>" |
| 973 | " </ll>" |
| 974 | "</c>"; |
| 975 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 976 | assert_non_null(tree); |
| 977 | |
| 978 | /* ancestor */ |
| 979 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//ll[a and b]/a/ancestor::node()", &set)); |
| 980 | assert_int_equal(8, set->count); |
| 981 | ly_set_free(set, NULL); |
| 982 | |
| 983 | /* ancestor-or-self */ |
| 984 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//ll[a and b]/ancestor-or-self::ll", &set)); |
| 985 | assert_int_equal(7, set->count); |
| 986 | ly_set_free(set, NULL); |
| 987 | |
| 988 | /* attribute */ |
| 989 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/l1/@operation", &set)); |
| 990 | assert_int_equal(0, set->count); |
| 991 | ly_set_free(set, NULL); |
| 992 | |
| 993 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/l1/attribute::key", &set)); |
| 994 | assert_int_equal(0, set->count); |
| 995 | ly_set_free(set, NULL); |
| 996 | |
| 997 | /* child */ |
| 998 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/child::l1/child::a", &set)); |
| 999 | assert_int_equal(3, set->count); |
| 1000 | ly_set_free(set, NULL); |
| 1001 | |
| 1002 | /* descendant */ |
| 1003 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/descendant::c/descendant::b", &set)); |
| 1004 | assert_int_equal(5, set->count); |
| 1005 | ly_set_free(set, NULL); |
| 1006 | |
| 1007 | /* descendant-or-self */ |
| 1008 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "//c", &set)); |
| 1009 | assert_int_equal(3, set->count); |
| 1010 | ly_set_free(set, NULL); |
| 1011 | |
| 1012 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/descendant-or-self::node()/c", &set)); |
| 1013 | assert_int_equal(3, set->count); |
| 1014 | ly_set_free(set, NULL); |
| 1015 | |
| 1016 | /* following */ |
| 1017 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/x/following::a", &set)); |
| 1018 | assert_int_equal(7, set->count); |
| 1019 | ly_set_free(set, NULL); |
| 1020 | |
| 1021 | /* following-sibling */ |
| 1022 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/x/following-sibling::ll", &set)); |
| 1023 | assert_int_equal(2, set->count); |
| 1024 | ly_set_free(set, NULL); |
| 1025 | |
| 1026 | /* parent */ |
| 1027 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/child::*/c/parent::l1", &set)); |
| 1028 | assert_int_equal(2, set->count); |
| 1029 | ly_set_free(set, NULL); |
| 1030 | |
| 1031 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/child::c//..", &set)); |
| 1032 | assert_int_equal(8, set->count); |
| 1033 | ly_set_free(set, NULL); |
| 1034 | |
| 1035 | /* preceding */ |
| 1036 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/preceding::a", &set)); |
| 1037 | assert_int_equal(3, set->count); |
| 1038 | ly_set_free(set, NULL); |
| 1039 | |
| 1040 | /* preceding-sibling */ |
| 1041 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/ll/preceding-sibling::node()", &set)); |
| 1042 | assert_int_equal(2, set->count); |
| 1043 | ly_set_free(set, NULL); |
| 1044 | |
| 1045 | /* self */ |
| 1046 | assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/c/self::c/ll/ll/b/self::b", &set)); |
| 1047 | assert_int_equal(5, set->count); |
| 1048 | ly_set_free(set, NULL); |
| 1049 | |
| 1050 | lyd_free_all(tree); |
| 1051 | } |
| 1052 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1053 | int |
| 1054 | main(void) |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 1055 | { |
| 1056 | const struct CMUnitTest tests[] = { |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 1057 | UTEST(test_predicate, setup), |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1058 | UTEST(test_union, setup), |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 1059 | UTEST(test_invalid, setup), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1060 | UTEST(test_hash, setup), |
| 1061 | UTEST(test_toplevel, setup), |
| 1062 | UTEST(test_atomize, setup), |
| 1063 | UTEST(test_canonize, setup), |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 1064 | UTEST(test_derived_from, setup), |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 1065 | UTEST(test_augment, setup), |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 1066 | UTEST(test_variables, setup), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1067 | UTEST(test_axes, setup), |
Michal Vasko | 14795a4 | 2020-05-22 16:44:44 +0200 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1071 | } |