blob: 71252b9e0350a06c452f8e9bc086c08fffea1a0f [file] [log] [blame]
Michal Vasko93923692021-05-07 15:28:02 +02001/**
Michal Vasko14795a42020-05-22 16:44:44 +02002 * @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ša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Michal Vasko14795a42020-05-22 16:44:44 +020016
Michal Vasko14795a42020-05-22 16:44:44 +020017#include <string.h>
18
Radek Krejci70593c12020-06-13 20:48:09 +020019#include "context.h"
Radek Krejci7931b192020-06-25 17:05:03 +020020#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "set.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020022#include "tests_config.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "tree_data.h"
24#include "tree_schema.h"
Michal Vasko14795a42020-05-22 16:44:44 +020025
Radek Iša56ca9e42020-09-08 18:42:00 +020026const 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 Vasko93923692021-05-07 15:28:02 +020032 " 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ša56ca9e42020-09-08 18:42:00 +020040 " 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 Vasko93923692021-05-07 15:28:02 +020058 " leaf foo3 {\n"
59 " type identityref {\n"
60 " base id_a;\n"
61 " }\n"
62 " }\n"
Michal Vasko00099082022-07-28 10:07:41 +020063 " leaf foo4 {\n"
64 " type decimal64 {\n"
65 " fraction-digits 5;\n"
66 " }\n"
67 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020068 " 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 Vasko14795a42020-05-22 16:44:44 +020092
93static int
94setup(void **state)
95{
Radek Iša56ca9e42020-09-08 18:42:00 +020096 UTEST_SETUP;
Michal Vasko14795a42020-05-22 16:44:44 +020097
Radek Iša56ca9e42020-09-08 18:42:00 +020098 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
Michal Vasko14795a42020-05-22 16:44:44 +020099
100 return 0;
101}
102
Michal Vasko14795a42020-05-22 16:44:44 +0200103static void
aPiecekfff4dca2021-10-07 10:59:53 +0200104test_predicate(void **state)
105{
106 const char *data;
107 struct lyd_node *tree;
108 struct ly_set *set;
109
110 data =
Michal Vasko49fec8e2022-05-24 10:28:33 +0200111 "<foo2 xmlns=\"urn:tests:a\">50</foo2>"
Michal Vasko3d969ff2022-07-29 15:02:08 +0200112 "<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 Vasko49fec8e2022-05-24 10:28:33 +0200135 "<c xmlns=\"urn:tests:a\">"
Michal Vasko3d969ff2022-07-29 15:02:08 +0200136 " <x>key2</x>"
Michal Vasko49fec8e2022-05-24 10:28:33 +0200137 " <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 Vasko3d969ff2022-07-29 15:02:08 +0200163 " <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 Vasko49fec8e2022-05-24 10:28:33 +0200174 "</c>";
aPiecekfff4dca2021-10-07 10:59:53 +0200175 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 Vasko49fec8e2022-05-24 10:28:33 +0200178 /* predicate after number */
aPiecekfff4dca2021-10-07 10:59:53 +0200179 assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/foo2[4[3 = 3]]", &set));
Michal Vasko49fec8e2022-05-24 10:28:33 +0200180 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])));
aPiecekfff4dca2021-10-07 10:59:53 +0200187 ly_set_free(set, NULL);
188
Michal Vasko3d969ff2022-07-29 15:02:08 +0200189 /* 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
aPiecekfff4dca2021-10-07 10:59:53 +0200203 lyd_free_all(tree);
204}
205
206static void
aPiecekadc1e4f2021-10-07 11:15:12 +0200207test_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
238static void
Michal Vaskoe2be5462021-08-04 10:49:42 +0200239test_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
258static void
Michal Vasko14795a42020-05-22 16:44:44 +0200259test_hash(void **state)
260{
Michal Vasko14795a42020-05-22 16:44:44 +0200261 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100262 "<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 Vasko14795a42020-05-22 16:44:44 +0200313 struct lyd_node *tree, *node;
314 struct ly_set *set;
Michal Vasko14795a42020-05-22 16:44:44 +0200315
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14795a42020-05-22 16:44:44 +0200317 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 Krejcia1c1e542020-09-29 16:06:52 +0200325 node = lyd_child(node);
Michal Vasko14795a42020-05-22 16:44:44 +0200326 assert_string_equal(node->schema->name, "a");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200327 assert_string_equal(lyd_get_value(node), "a3");
Michal Vasko14795a42020-05-22 16:44:44 +0200328
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 Krejcia1c1e542020-09-29 16:06:52 +0200337 node = lyd_child(node);
Michal Vasko14795a42020-05-22 16:44:44 +0200338 assert_string_equal(node->schema->name, "a");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200339 assert_string_equal(lyd_get_value(node), "val_b");
Michal Vasko14795a42020-05-22 16:44:44 +0200340 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 Vasko660cc8f2020-05-25 10:33:19 +0200352 /* 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 Krejci6d5ba0c2021-04-26 07:49:59 +0200358 assert_string_equal(lyd_get_value(node), "three");
Michal Vasko660cc8f2020-05-25 10:33:19 +0200359
360 ly_set_free(set, NULL);
361
Michal Vasko14795a42020-05-22 16:44:44 +0200362 /* 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 Vasko14795a42020-05-22 16:44:44 +0200375}
376
Michal Vasko61ac2f62020-05-25 12:39:51 +0200377static void
378test_toplevel(void **state)
379{
Radek Iša56ca9e42020-09-08 18:42:00 +0200380 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 Vasko61ac2f62020-05-25 12:39:51 +0200396 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100397 "<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 Vaskof60e7102020-05-26 10:48:59 +0200424 struct lyd_node *tree;
Michal Vasko61ac2f62020-05-25 12:39:51 +0200425 struct ly_set *set;
Michal Vasko61ac2f62020-05-25 12:39:51 +0200426
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 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 Vasko61ac2f62020-05-25 12:39:51 +0200430 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 Vasko61ac2f62020-05-25 12:39:51 +0200463}
464
Michal Vasko519fd602020-05-26 12:17:39 +0200465static void
466test_atomize(void **state)
467{
Michal Vasko519fd602020-05-26 12:17:39 +0200468 struct ly_set *set;
469 const struct lys_module *mod;
470
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
472 assert_non_null(mod);
Michal Vasko519fd602020-05-26 12:17:39 +0200473
474 /* some random paths just making sure the API function works */
Michal Vasko400e9672021-01-11 13:39:17 +0100475 assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "/a:*", 0, &set));
Michal Vasko00099082022-07-28 10:07:41 +0200476 assert_int_equal(6, set->count);
Michal Vasko519fd602020-05-26 12:17:39 +0200477 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 Vasko400e9672021-01-11 13:39:17 +0100480 assert_int_equal(LY_SUCCESS, lys_find_xpath_atoms(UTEST_LYCTX, NULL, "//.", 0, &set));
Michal Vasko93923692021-05-07 15:28:02 +0200481 assert_in_range(set->count, 17, UINT32_MAX);
Michal Vasko519fd602020-05-26 12:17:39 +0200482 ly_set_free(set, NULL);
483
Michal Vasko400e9672021-01-11 13:39:17 +0100484 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 Vasko49fec8e2022-05-24 10:28:33 +0200486 ly_set_free(set, NULL);
Michal Vasko519fd602020-05-26 12:17:39 +0200487
Michal Vasko49fec8e2022-05-24 10:28:33 +0200488 /*
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 Vasko00099082022-07-28 10:07:41 +0200519 assert_int_equal(7, set->count);
Michal Vasko49fec8e2022-05-24 10:28:33 +0200520 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 Vasko00099082022-07-28 10:07:41 +0200534 assert_int_equal(7, set->count);
Michal Vasko49fec8e2022-05-24 10:28:33 +0200535 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 Vasko519fd602020-05-26 12:17:39 +0200554 ly_set_free(set, NULL);
555}
556
Michal Vasko4c7763f2020-07-27 17:40:37 +0200557static void
558test_canonize(void **state)
559{
Michal Vasko4c7763f2020-07-27 17:40:37 +0200560 const char *data =
Michal Vasko00099082022-07-28 10:07:41 +0200561 "<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 Vasko4c7763f2020-07-27 17:40:37 +0200564 struct lyd_node *tree;
565 struct ly_set *set;
566
Radek Iša56ca9e42020-09-08 18:42:00 +0200567 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko4c7763f2020-07-27 17:40:37 +0200568 assert_non_null(tree);
569
Michal Vasko00099082022-07-28 10:07:41 +0200570 /* integer */
Michal Vasko4c7763f2020-07-27 17:40:37 +0200571 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 Vasko00099082022-07-28 10:07:41 +0200575 /* 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 Vasko4c7763f2020-07-27 17:40:37 +0200584
585 lyd_free_all(tree);
Michal Vasko4c7763f2020-07-27 17:40:37 +0200586}
587
Michal Vasko93923692021-05-07 15:28:02 +0200588static void
589test_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 Vaskodb08ce52021-10-06 08:57:49 +0200610static void
611test_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
aPiecekfba75362021-10-07 12:39:48 +0200655static void
656test_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 Vasko49fec8e2022-05-24 10:28:33 +0200910static void
911test_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 Vasko79a7a872022-06-17 09:00:48 +0200928 " <a yang:operation=\"none\" yang:key=\"[no-key='no-value']\">a3</a>\n"
Michal Vasko49fec8e2022-05-24 10:28:33 +0200929 " <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 Krejcib4ac5a92020-11-23 17:54:33 +01001039int
1040main(void)
Michal Vasko14795a42020-05-22 16:44:44 +02001041{
1042 const struct CMUnitTest tests[] = {
aPiecekfff4dca2021-10-07 10:59:53 +02001043 UTEST(test_predicate, setup),
aPiecekadc1e4f2021-10-07 11:15:12 +02001044 UTEST(test_union, setup),
Michal Vaskoe2be5462021-08-04 10:49:42 +02001045 UTEST(test_invalid, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +02001046 UTEST(test_hash, setup),
1047 UTEST(test_toplevel, setup),
1048 UTEST(test_atomize, setup),
1049 UTEST(test_canonize, setup),
Michal Vasko93923692021-05-07 15:28:02 +02001050 UTEST(test_derived_from, setup),
Michal Vaskodb08ce52021-10-06 08:57:49 +02001051 UTEST(test_augment, setup),
aPiecekfba75362021-10-07 12:39:48 +02001052 UTEST(test_variables, setup),
Michal Vasko49fec8e2022-05-24 10:28:33 +02001053 UTEST(test_axes, setup),
Michal Vasko14795a42020-05-22 16:44:44 +02001054 };
1055
1056 return cmocka_run_group_tests(tests, NULL, NULL);
1057}