blob: 7b17e504ddf0e074ce5058d5a1bf3b5c2a5dc0eb [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_tree_data.c
Radek Krejci1f05b6a2019-07-18 16:15:06 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from tress_data.c
5 *
6 * Copyright (c) 2018-2019 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"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020016
Michal Vasko38c2ba72020-07-27 14:46:59 +020017#include "common.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010018#include "libyang.h"
Michal Vasko38c2ba72020-07-27 14:46:59 +020019#include "path.h"
20#include "xpath.h"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020021
Radek Krejci1f05b6a2019-07-18 16:15:06 +020022static int
23setup(void **state)
24{
aPiecek24252202021-03-30 12:23:32 +020025 const char *schema1 = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
26 "revision 2014-05-08;"
Radek Krejcica989142020-11-05 11:32:22 +010027 "leaf bar {type string;}"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020028 "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
29 "leaf foo { type string;}"
30 "leaf-list ll { type string;}"
31 "container c {leaf-list x {type string;}}"
32 "anydata any {config false;}"
Michal Vasko38c2ba72020-07-27 14:46:59 +020033 "list l2 {config false;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010034 " container c{leaf x {type string;} leaf-list d {type string;}}"
Michal Vasko38c2ba72020-07-27 14:46:59 +020035 "}}";
Radek Krejci1f05b6a2019-07-18 16:15:06 +020036
aPiecek24252202021-03-30 12:23:32 +020037 const char *schema2 = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
38 "revision 2014-05-08;"
39 "list l2 {config false;"
40 " container c{leaf x {type string;}}}"
41 "anydata any {config false;}"
42 "}";
43
Michal Vasko6b669ff2021-04-30 16:25:36 +020044 const char *schema3 = "module c {yang-version 1.1; namespace \"http://example.com/main\";prefix m;"
45 "import \"ietf-inet-types\" {prefix inet;}"
46 "typedef optional-ip-address {type union {"
47 " type inet:ip-address;"
Michal Vasko4fd91922021-09-15 08:51:06 +020048 " type string;"
Michal Vasko6b669ff2021-04-30 16:25:36 +020049 "}}"
50 "container cont {"
51 " list nexthop {min-elements 1; key \"gateway\";"
52 " leaf gateway {type optional-ip-address;}"
53 " }"
Michal Vasko15dc9fa2021-05-03 14:33:05 +020054 " leaf-list pref {type inet:ipv6-prefix;}"
Michal Vasko6b669ff2021-04-30 16:25:36 +020055 "}}";
56
Radek Iša56ca9e42020-09-08 18:42:00 +020057 UTEST_SETUP;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020058
aPiecek24252202021-03-30 12:23:32 +020059 UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
60 UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
Michal Vasko6b669ff2021-04-30 16:25:36 +020061 UTEST_ADD_MODULE(schema3, LYS_IN_YANG, NULL, NULL);
Radek Krejci1f05b6a2019-07-18 16:15:06 +020062
63 return 0;
64}
65
Radek Iša56ca9e42020-09-08 18:42:00 +020066#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
67 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1f05b6a2019-07-18 16:15:06 +020068
aPiecek24252202021-03-30 12:23:32 +020069#define CHECK_PARSE_LYD_PARAM_CTX(CTX, INPUT, PARSE_OPTION, OUT_NODE) \
70 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(CTX, INPUT, LYD_XML, PARSE_OPTION, LYD_VALIDATE_PRESENT, &OUT_NODE)); \
71 assert_non_null(OUT_NODE);
72
73#define RECREATE_CTX_WITH_MODULE(CTX, MODULE) \
Radek Krejci90ed21e2021-04-12 14:47:46 +020074 ly_ctx_destroy(CTX); \
aPiecek24252202021-03-30 12:23:32 +020075 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &CTX)); \
76 assert_int_equal(LY_SUCCESS, ly_in_new_memory(MODULE, &_UC->in)); \
77 assert_int_equal(LY_SUCCESS, lys_parse(CTX, _UC->in, LYS_IN_YANG, NULL, NULL)); \
78 ly_in_free(_UC->in, 0);
79
Radek Krejci1f05b6a2019-07-18 16:15:06 +020080static void
81test_compare(void **state)
82{
Radek Krejci1f05b6a2019-07-18 16:15:06 +020083 struct lyd_node *tree1, *tree2;
Radek Iša56ca9e42020-09-08 18:42:00 +020084 const char *data1;
85 const char *data2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020086
Michal Vasko8f359bf2020-07-28 10:41:15 +020087 assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020088
Radek Iša56ca9e42020-09-08 18:42:00 +020089 data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
90 data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
91 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
92 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020093 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
94 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010095 assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner *)tree1)->child, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020096 lyd_free_all(tree1);
97 lyd_free_all(tree2);
98
99 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
100 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
102 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100103 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
104 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200105 lyd_free_all(tree1);
106 lyd_free_all(tree2);
107
108 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
109 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
111 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200112 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
113 assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0));
114 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0));
115 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200116 lyd_free_all(tree1);
117 lyd_free_all(tree2);
118
119 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
120 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200121 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
122 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200123 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
124 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200125 lyd_free_all(tree1);
126 lyd_free_all(tree2);
127
128 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
129 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200130 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
131 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200132 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
133 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200134 lyd_free_all(tree1);
135 lyd_free_all(tree2);
136
137 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
138 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
140 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100141 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200142 lyd_free_all(tree1);
143 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200144 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200145 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200146 lyd_free_all(tree1);
147 lyd_free_all(tree2);
Michal Vaskof277d362023-04-24 09:08:31 +0200148
149 data1 = "<c xmlns=\"urn:tests:a\"><x>c</x><x>a</x><x>b</x></c>";
150 data2 = "<c xmlns=\"urn:tests:a\"><x>a</x><x>b</x><x>c</x></c>";
151 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
152 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
153 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
154 lyd_free_all(tree1);
155 lyd_free_all(tree2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200156}
157
Radek Krejci22ebdba2019-07-25 13:59:43 +0200158static void
aPiecek24252202021-03-30 12:23:32 +0200159test_compare_diff_ctx(void **state)
160{
161 struct lyd_node *tree1, *tree2;
162 const char *data1, *data2;
163 struct ly_ctx *ctx2 = NULL;
164 const char *module;
165
166 /* create second context with the same schema */
167 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
168 "revision 2014-05-08;"
169 "list l2 {config false;"
170 " container c{leaf x {type string;}}"
171 "}}";
172 RECREATE_CTX_WITH_MODULE(ctx2, module);
173 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
174 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
175 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
176 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
177 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
178 lyd_free_all(tree1);
179 lyd_free_all(tree2);
180
181 /* recreate second context with schema that has a different name */
182 module = "module c {namespace urn:tests:c;prefix c;yang-version 1.1;"
183 "revision 2014-05-08;"
184 "list l2 {config false;"
185 " container c{leaf x {type string;}}"
186 "}}";
187 RECREATE_CTX_WITH_MODULE(ctx2, module);
188 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
189 data2 = "<l2 xmlns=\"urn:tests:c\"><c><x>b</x></c></l2>";
190 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
191 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
192 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
193 lyd_free_all(tree1);
194 lyd_free_all(tree2);
195
196 /* recreate second context with schema that has a different revision */
197 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
198 "revision 2015-05-08;"
199 "list l2 {config false;"
200 " container c{leaf x {type string;}}"
201 "}}";
202 RECREATE_CTX_WITH_MODULE(ctx2, module);
203 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
204 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
205 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
206 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
Michal Vasko5d49f942023-02-14 10:02:36 +0100207 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
aPiecek24252202021-03-30 12:23:32 +0200208 lyd_free_all(tree1);
209 lyd_free_all(tree2);
210
211 /* recreate second context with schema that has no revision */
212 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
213 "list l2 {config false;"
214 " container c{leaf x {type string;}}"
215 "}}";
216 RECREATE_CTX_WITH_MODULE(ctx2, module);
217 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
218 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
219 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
220 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
Michal Vasko5d49f942023-02-14 10:02:36 +0100221 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
aPiecek24252202021-03-30 12:23:32 +0200222 lyd_free_all(tree1);
223 lyd_free_all(tree2);
224
225 /* recreate second context with schema that has a different parent nodetype */
226 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
227 "revision 2014-05-08;"
228 "container l2 {config false;"
229 " container c{leaf x {type string;}}"
230 "}}";
231 RECREATE_CTX_WITH_MODULE(ctx2, module);
232 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
233 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
234 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
235 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
236 assert_int_equal(LY_ENOT, lyd_compare_single(lyd_child(lyd_child(tree1)), lyd_child(lyd_child(tree2)), 0));
237 lyd_free_all(tree1);
238 lyd_free_all(tree2);
239
240 /* recreate second context with the same opaq data nodes */
241 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
242 "revision 2014-05-08;"
243 "anydata any {config false;}"
244 "}";
245 RECREATE_CTX_WITH_MODULE(ctx2, module);
246 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
247 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:x</x></any>";
248 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
249 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
250 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
251 lyd_free_all(tree1);
252 lyd_free_all(tree2);
253
254 /* recreate second context with the different opaq data node value */
255 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
256 "revision 2014-05-08;"
257 "anydata any {config false;}"
258 "}";
259 RECREATE_CTX_WITH_MODULE(ctx2, module);
260 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
261 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:y</x></any>";
262 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
263 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
264 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
265 lyd_free_all(tree1);
266 lyd_free_all(tree2);
267
268 /* recreate second context with the wrong prefix in opaq data node value */
269 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
270 "revision 2014-05-08;"
271 "anydata any {config false;}"
272 "}";
273 RECREATE_CTX_WITH_MODULE(ctx2, module);
274 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
275 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>cc:x</x></any>";
276 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
277 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
278 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
279 lyd_free_all(tree1);
280 lyd_free_all(tree2);
281
282 /* clean up */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200283 ly_ctx_destroy(ctx2);
aPiecek24252202021-03-30 12:23:32 +0200284 _UC->in = NULL;
285}
286
287static void
Radek Krejci22ebdba2019-07-25 13:59:43 +0200288test_dup(void **state)
289{
Radek Krejci22ebdba2019-07-25 13:59:43 +0200290 struct lyd_node *tree1, *tree2;
291 const char *result;
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 const char *data;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200293
Radek Iša56ca9e42020-09-08 18:42:00 +0200294 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
295 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200296 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200297 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200298 lyd_free_all(tree1);
299 lyd_free_all(tree2);
300
301 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
302 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200304 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200305 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200306 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200307 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200308 lyd_free_all(tree1);
309 lyd_free_all(tree2);
310
311 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
312 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200313 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200314 assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko26123192020-11-09 21:02:34 +0100315 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200316 lyd_free_all(tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100317 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200318 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200319 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100320 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200321 lyd_free_all(tree2);
322
Michal Vasko26123192020-11-09 21:02:34 +0100323 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200324 lyd_free_all(tree1);
325 result = "<l2 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200326 CHECK_PARSE_LYD_PARAM(result, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200327 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200328 lyd_free_all(tree1);
329 lyd_free_all(tree2);
330
331 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200332 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200333 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200334 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200335 lyd_free_all(tree1);
336 lyd_free_all(tree2);
337
338 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100340 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL,
341 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM;
343
344 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100345 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node *)tree2->parent->parent,
Michal Vasko26123192020-11-09 21:02:34 +0100346 LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200347 lyd_free_all(tree1);
348 lyd_free_all(tree2);
349
350 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200351 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100352 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL,
353 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200354 flag = LYS_CONFIG_W | LYS_SET_ENUM;
355 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "c", 0, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100356 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node *)tree2->parent, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200357 lyd_free_all(tree1);
358 lyd_free_all(tree2);
359
360 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200361 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100362 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100363 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child,
364 (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko26123192020-11-09 21:02:34 +0100365 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200366 lyd_free_all(tree1);
367 lyd_free_all(tree2);
368
369 /* invalid */
370 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200371 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100372 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev,
373 (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100374 CHECK_LOG_CTX("Invalid argument parent (lyd_dup_get_local_parent()) - does not interconnect with the created node's parents chain.",
375 NULL);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200376 lyd_free_all(tree1);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200377}
378
Michal Vasko38c2ba72020-07-27 14:46:59 +0200379static void
380test_target(void **state)
381{
Michal Vasko38c2ba72020-07-27 14:46:59 +0200382 const struct lyd_node_term *term;
383 struct lyd_node *tree;
384 struct lyxp_expr *exp;
385 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200386 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200387 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100388 "<l2 xmlns=\"urn:tests:a\"><c>"
389 " <d>a</d>"
390 " </c></l2>"
391 "<l2 xmlns=\"urn:tests:a\"><c>"
392 " <d>a</d>"
393 " <d>b</d>"
394 " <d>b</d>"
395 " <d>c</d>"
396 "</c></l2>"
397 "<l2 xmlns=\"urn:tests:a\"><c>"
398 "</c></l2>";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200399
Radek Iša56ca9e42020-09-08 18:42:00 +0200400 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vaskoed725d72021-06-23 12:03:45 +0200401 assert_int_equal(LY_SUCCESS, ly_path_parse(UTEST_LYCTX, NULL, path_str, strlen(path_str), 0, LY_PATH_BEGIN_EITHER,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100402 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
Michal Vaskoed725d72021-06-23 12:03:45 +0200403 assert_int_equal(LY_SUCCESS, ly_path_compile(UTEST_LYCTX, NULL, NULL, NULL, exp, LY_PATH_OPER_INPUT,
Michal Vasko0884d212021-10-14 09:21:46 +0200404 LY_PATH_TARGET_SINGLE, 1, LY_VALUE_JSON, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200405 term = lyd_target(path, tree);
406
Michal Vaskoe78faec2021-04-08 17:24:43 +0200407 const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_USER;
Radek Iša56ca9e42020-09-08 18:42:00 +0200408
409 CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200410 assert_string_equal(lyd_get_value(&term->node), "b");
411 assert_string_equal(lyd_get_value(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200412
413 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200414 ly_path_free(UTEST_LYCTX, path);
415 lyxp_expr_free(UTEST_LYCTX, exp);
Michal Vasko38c2ba72020-07-27 14:46:59 +0200416}
417
Radek Krejcica989142020-11-05 11:32:22 +0100418static void
419test_list_pos(void **state)
420{
Radek Krejcica989142020-11-05 11:32:22 +0100421 const char *data;
422 struct lyd_node *tree;
423
424 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100425 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
426 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
427 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200428 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100429 assert_int_equal(0, lyd_list_pos(tree));
430 assert_int_equal(1, lyd_list_pos(tree->next));
431 assert_int_equal(2, lyd_list_pos(tree->next->next));
432 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
433 lyd_free_all(tree);
434
435 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100436 "<ll xmlns=\"urn:tests:a\">two</ll>"
437 "<ll xmlns=\"urn:tests:a\">three</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200438 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100439 assert_int_equal(1, lyd_list_pos(tree));
440 assert_int_equal(2, lyd_list_pos(tree->next));
441 assert_int_equal(3, lyd_list_pos(tree->next->next));
442 lyd_free_all(tree);
443
444 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100445 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
446 "<ll xmlns=\"urn:tests:a\">two</ll>"
447 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
448 "<ll xmlns=\"urn:tests:a\">three</ll>"
449 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200450 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100451 assert_string_equal("l1", tree->schema->name);
452 assert_int_equal(1, lyd_list_pos(tree));
453 assert_int_equal(2, lyd_list_pos(tree->next));
454 assert_int_equal(3, lyd_list_pos(tree->next->next));
455 assert_string_equal("ll", tree->next->next->next->schema->name);
456 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
457 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
458 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
459 lyd_free_all(tree);
Radek Krejcica989142020-11-05 11:32:22 +0100460}
461
Radek Krejci4233f9b2020-11-05 12:38:35 +0100462static void
463test_first_sibling(void **state)
464{
Radek Krejci4233f9b2020-11-05 12:38:35 +0100465 const char *data;
466 struct lyd_node *tree;
467 struct lyd_node_inner *parent;
468
469 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100470 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
471 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200472 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci4233f9b2020-11-05 12:38:35 +0100473 assert_ptr_equal(tree, lyd_first_sibling(tree->next));
474 assert_ptr_equal(tree, lyd_first_sibling(tree));
475 assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100476 parent = (struct lyd_node_inner *)tree->next;
Radek Krejci4233f9b2020-11-05 12:38:35 +0100477 assert_int_equal(LYS_LIST, parent->schema->nodetype);
478 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
479 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
480 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
481 lyd_free_all(tree);
Radek Krejci4233f9b2020-11-05 12:38:35 +0100482}
483
Michal Vasko6b669ff2021-04-30 16:25:36 +0200484static void
485test_find_path(void **state)
486{
487 struct lyd_node *root;
488 const struct lys_module *mod;
489
490 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
491 assert_non_null(mod);
492
493 assert_int_equal(LY_SUCCESS, lyd_new_inner(NULL, mod, "cont", 0, &root));
494 assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='10.0.0.1']", NULL, LYD_NEW_PATH_UPDATE, NULL));
495 assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='2100::1']", NULL, LYD_NEW_PATH_UPDATE, NULL));
Michal Vasko15dc9fa2021-05-03 14:33:05 +0200496 assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/pref[.='fc00::/64']", NULL, 0, NULL));
Michal Vasko6b669ff2021-04-30 16:25:36 +0200497
498 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='10.0.0.1']", 0, NULL));
499 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='2100::1']", 0, NULL));
Michal Vasko15dc9fa2021-05-03 14:33:05 +0200500 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/pref[.='fc00::/64']", 0, NULL));
Michal Vasko32ca49b2023-02-17 15:11:35 +0100501
502 assert_int_equal(LY_EVALID, lyd_find_path(root, "/cont", 0, NULL));
503 CHECK_LOG_CTX("Prefix missing for \"cont\" in path.", "Schema location \"/c:cont\".");
504 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "nexthop[gateway='2100::1']", 0, NULL));
505
Michal Vasko6b669ff2021-04-30 16:25:36 +0200506 lyd_free_all(root);
507}
508
aPiecek4f07c3e2021-06-11 10:53:07 +0200509static void
510test_data_hash(void **state)
511{
512 struct lyd_node *tree;
513 const char *schema, *data;
514
515 schema =
516 "module test-data-hash {"
517 " yang-version 1.1;"
518 " namespace \"urn:tests:tdh\";"
519 " prefix t;"
520 " container c {"
521 " leaf-list ll {"
522 " type string;"
523 " }"
524 " }"
525 "}";
526
527 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
528
529 /* The number of <ll/> must be greater or equal to LYD_HT_MIN_ITEMS
530 * for the correct test run. It should guarantee the creation of a hash table.
531 */
532 assert_true(LYD_HT_MIN_ITEMS <= 4);
533 data =
534 "<c xmlns='urn:tests:tdh'>"
535 " <ll/>"
536 " <ll/>"
537 " <ll/>"
538 " <ll/>"
539 "</c>";
540
541 /* The run must not crash due to the assert that checks the hash. */
542 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko62af3692023-02-09 14:00:09 +0100543 CHECK_LOG_CTX("Duplicate instance of \"ll\".", "Data location \"/test-data-hash:c/ll[.='']\", line number 1.");
aPiecek4f07c3e2021-06-11 10:53:07 +0200544 lyd_free_all(tree);
545}
546
aPiecekdf23eee2021-10-07 12:21:50 +0200547static void
548test_lyxp_vars(void **UNUSED(state))
549{
550 struct lyxp_var *vars;
551
552 /* Test free. */
553 vars = NULL;
554 lyxp_vars_free(vars);
555
556 /* Bad arguments for lyxp_vars_add(). */
557 assert_int_equal(LY_EINVAL, lyxp_vars_set(NULL, "var1", "val1"));
558 assert_int_equal(LY_EINVAL, lyxp_vars_set(&vars, NULL, "val1"));
559 assert_int_equal(LY_EINVAL, lyxp_vars_set(&vars, "var1", NULL));
560 lyxp_vars_free(vars);
561 vars = NULL;
562
563 /* Add one item. */
564 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1"));
565 assert_int_equal(LY_ARRAY_COUNT(vars), 1);
566 assert_string_equal(vars[0].name, "var1");
567 assert_string_equal(vars[0].value, "val1");
568 lyxp_vars_free(vars);
569 vars = NULL;
570
571 /* Add three items. */
572 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1"));
573 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "val2"));
574 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var3", "val3"));
575 assert_int_equal(LY_ARRAY_COUNT(vars), 3);
576 assert_string_equal(vars[0].name, "var1");
577 assert_string_equal(vars[0].value, "val1");
578 assert_string_equal(vars[1].name, "var2");
579 assert_string_equal(vars[1].value, "val2");
580 assert_string_equal(vars[2].name, "var3");
581 assert_string_equal(vars[2].value, "val3");
582 lyxp_vars_free(vars);
583 vars = NULL;
584
585 /* Change value of a variable. */
586 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1"));
587 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "val2"));
588 assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "new_value"));
589 assert_string_equal(vars[0].name, "var1");
590 assert_string_equal(vars[0].value, "new_value");
591 assert_string_equal(vars[1].name, "var2");
592 assert_string_equal(vars[1].value, "val2");
593 lyxp_vars_free(vars);
594 vars = NULL;
595}
596
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100597int
598main(void)
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200599{
600 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 UTEST(test_compare, setup),
aPiecek24252202021-03-30 12:23:32 +0200602 UTEST(test_compare_diff_ctx, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200603 UTEST(test_dup, setup),
604 UTEST(test_target, setup),
605 UTEST(test_list_pos, setup),
606 UTEST(test_first_sibling, setup),
Michal Vasko6b669ff2021-04-30 16:25:36 +0200607 UTEST(test_find_path, setup),
aPiecek4f07c3e2021-06-11 10:53:07 +0200608 UTEST(test_data_hash, setup),
aPiecekdf23eee2021-10-07 12:21:50 +0200609 UTEST(test_lyxp_vars),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200610 };
611
612 return cmocka_run_group_tests(tests, NULL, NULL);
613}