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