blob: f0ba38878935e327cab5e014396cf6eba841df7c [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002 * @file test_tree_schema.c
3 * @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;"
48 " type string {pattern \"\";}"
49 "}}"
50 "container cont {"
51 " list nexthop {min-elements 1; key \"gateway\";"
52 " leaf gateway {type optional-ip-address;}"
53 " }"
54 "}}";
55
Radek Iša56ca9e42020-09-08 18:42:00 +020056 UTEST_SETUP;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020057
aPiecek24252202021-03-30 12:23:32 +020058 UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
59 UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
Michal Vasko6b669ff2021-04-30 16:25:36 +020060 UTEST_ADD_MODULE(schema3, LYS_IN_YANG, NULL, NULL);
Radek Krejci1f05b6a2019-07-18 16:15:06 +020061
62 return 0;
63}
64
Radek Iša56ca9e42020-09-08 18:42:00 +020065#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
66 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1f05b6a2019-07-18 16:15:06 +020067
aPiecek24252202021-03-30 12:23:32 +020068#define CHECK_PARSE_LYD_PARAM_CTX(CTX, INPUT, PARSE_OPTION, OUT_NODE) \
69 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(CTX, INPUT, LYD_XML, PARSE_OPTION, LYD_VALIDATE_PRESENT, &OUT_NODE)); \
70 assert_non_null(OUT_NODE);
71
72#define RECREATE_CTX_WITH_MODULE(CTX, MODULE) \
Radek Krejci90ed21e2021-04-12 14:47:46 +020073 ly_ctx_destroy(CTX); \
aPiecek24252202021-03-30 12:23:32 +020074 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &CTX)); \
75 assert_int_equal(LY_SUCCESS, ly_in_new_memory(MODULE, &_UC->in)); \
76 assert_int_equal(LY_SUCCESS, lys_parse(CTX, _UC->in, LYS_IN_YANG, NULL, NULL)); \
77 ly_in_free(_UC->in, 0);
78
Radek Krejci1f05b6a2019-07-18 16:15:06 +020079static void
80test_compare(void **state)
81{
Radek Krejci1f05b6a2019-07-18 16:15:06 +020082 struct lyd_node *tree1, *tree2;
Radek Iša56ca9e42020-09-08 18:42:00 +020083 const char *data1;
84 const char *data2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020085
Michal Vasko8f359bf2020-07-28 10:41:15 +020086 assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020087
Radek Iša56ca9e42020-09-08 18:42:00 +020088 data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
89 data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
90 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
91 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020092 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
93 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010094 assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner *)tree1)->child, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020095 lyd_free_all(tree1);
96 lyd_free_all(tree2);
97
98 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
99 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200100 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
101 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100102 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
103 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200104 lyd_free_all(tree1);
105 lyd_free_all(tree2);
106
107 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
108 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
110 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200111 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
112 assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0));
113 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0));
114 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200115 lyd_free_all(tree1);
116 lyd_free_all(tree2);
117
118 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
119 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200120 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
121 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200122 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
123 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200124 lyd_free_all(tree1);
125 lyd_free_all(tree2);
126
127 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
128 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200129 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
130 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200131 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
132 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200133 lyd_free_all(tree1);
134 lyd_free_all(tree2);
135
136 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
137 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
139 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100140 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200141 lyd_free_all(tree1);
142 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200144 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200145 lyd_free_all(tree1);
146 lyd_free_all(tree2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200147}
148
Radek Krejci22ebdba2019-07-25 13:59:43 +0200149static void
aPiecek24252202021-03-30 12:23:32 +0200150test_compare_diff_ctx(void **state)
151{
152 struct lyd_node *tree1, *tree2;
153 const char *data1, *data2;
154 struct ly_ctx *ctx2 = NULL;
155 const char *module;
156
157 /* create second context with the same schema */
158 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
159 "revision 2014-05-08;"
160 "list l2 {config false;"
161 " container c{leaf x {type string;}}"
162 "}}";
163 RECREATE_CTX_WITH_MODULE(ctx2, module);
164 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
165 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
166 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
167 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
168 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
169 lyd_free_all(tree1);
170 lyd_free_all(tree2);
171
172 /* recreate second context with schema that has a different name */
173 module = "module c {namespace urn:tests:c;prefix c;yang-version 1.1;"
174 "revision 2014-05-08;"
175 "list l2 {config false;"
176 " container c{leaf x {type string;}}"
177 "}}";
178 RECREATE_CTX_WITH_MODULE(ctx2, module);
179 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
180 data2 = "<l2 xmlns=\"urn:tests:c\"><c><x>b</x></c></l2>";
181 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
182 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
183 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
184 lyd_free_all(tree1);
185 lyd_free_all(tree2);
186
187 /* recreate second context with schema that has a different revision */
188 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
189 "revision 2015-05-08;"
190 "list l2 {config false;"
191 " container c{leaf x {type string;}}"
192 "}}";
193 RECREATE_CTX_WITH_MODULE(ctx2, module);
194 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
195 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
196 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
197 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
198 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
199 lyd_free_all(tree1);
200 lyd_free_all(tree2);
201
202 /* recreate second context with schema that has no revision */
203 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
204 "list l2 {config false;"
205 " container c{leaf x {type string;}}"
206 "}}";
207 RECREATE_CTX_WITH_MODULE(ctx2, module);
208 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
209 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
210 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
211 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
212 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
213 lyd_free_all(tree1);
214 lyd_free_all(tree2);
215
216 /* recreate second context with schema that has a different parent nodetype */
217 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
218 "revision 2014-05-08;"
219 "container l2 {config false;"
220 " container c{leaf x {type string;}}"
221 "}}";
222 RECREATE_CTX_WITH_MODULE(ctx2, module);
223 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
224 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
225 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
226 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
227 assert_int_equal(LY_ENOT, lyd_compare_single(lyd_child(lyd_child(tree1)), lyd_child(lyd_child(tree2)), 0));
228 lyd_free_all(tree1);
229 lyd_free_all(tree2);
230
231 /* recreate second context with the same opaq data nodes */
232 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
233 "revision 2014-05-08;"
234 "anydata any {config false;}"
235 "}";
236 RECREATE_CTX_WITH_MODULE(ctx2, module);
237 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
238 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:x</x></any>";
239 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
240 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
241 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
242 lyd_free_all(tree1);
243 lyd_free_all(tree2);
244
245 /* recreate second context with the different opaq data node value */
246 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
247 "revision 2014-05-08;"
248 "anydata any {config false;}"
249 "}";
250 RECREATE_CTX_WITH_MODULE(ctx2, module);
251 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
252 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:y</x></any>";
253 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
254 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
255 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
256 lyd_free_all(tree1);
257 lyd_free_all(tree2);
258
259 /* recreate second context with the wrong prefix in opaq data node value */
260 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
261 "revision 2014-05-08;"
262 "anydata any {config false;}"
263 "}";
264 RECREATE_CTX_WITH_MODULE(ctx2, module);
265 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
266 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>cc:x</x></any>";
267 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
268 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
269 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
270 lyd_free_all(tree1);
271 lyd_free_all(tree2);
272
273 /* clean up */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200274 ly_ctx_destroy(ctx2);
aPiecek24252202021-03-30 12:23:32 +0200275 _UC->in = NULL;
276}
277
278static void
Radek Krejci22ebdba2019-07-25 13:59:43 +0200279test_dup(void **state)
280{
Radek Krejci22ebdba2019-07-25 13:59:43 +0200281 struct lyd_node *tree1, *tree2;
282 const char *result;
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 const char *data;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200284
Radek Iša56ca9e42020-09-08 18:42:00 +0200285 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
286 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200287 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200288 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200289 lyd_free_all(tree1);
290 lyd_free_all(tree2);
291
292 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
293 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200294 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200295 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200296 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200297 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200298 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200299 lyd_free_all(tree1);
300 lyd_free_all(tree2);
301
302 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
303 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200304 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200305 assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko26123192020-11-09 21:02:34 +0100306 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200307 lyd_free_all(tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100308 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200309 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200310 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100311 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200312 lyd_free_all(tree2);
313
Michal Vasko26123192020-11-09 21:02:34 +0100314 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200315 lyd_free_all(tree1);
316 result = "<l2 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200317 CHECK_PARSE_LYD_PARAM(result, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200318 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200319 lyd_free_all(tree1);
320 lyd_free_all(tree2);
321
322 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200323 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200324 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200325 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200326 lyd_free_all(tree1);
327 lyd_free_all(tree2);
328
329 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100331 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL,
332 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200333 int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM;
334
335 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100336 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node *)tree2->parent->parent,
Michal Vasko26123192020-11-09 21:02:34 +0100337 LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200338 lyd_free_all(tree1);
339 lyd_free_all(tree2);
340
341 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100343 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL,
344 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200345 flag = LYS_CONFIG_W | LYS_SET_ENUM;
346 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "c", 0, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100347 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 +0200348 lyd_free_all(tree1);
349 lyd_free_all(tree2);
350
351 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200352 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100353 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100354 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child,
355 (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko26123192020-11-09 21:02:34 +0100356 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200357 lyd_free_all(tree1);
358 lyd_free_all(tree2);
359
360 /* invalid */
361 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 +0200362 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100363 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev,
364 (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200365 lyd_free_all(tree1);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200366}
367
Michal Vasko38c2ba72020-07-27 14:46:59 +0200368static void
369test_target(void **state)
370{
Michal Vasko38c2ba72020-07-27 14:46:59 +0200371 const struct lyd_node_term *term;
372 struct lyd_node *tree;
373 struct lyxp_expr *exp;
374 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200375 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200376 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100377 "<l2 xmlns=\"urn:tests:a\"><c>"
378 " <d>a</d>"
379 " </c></l2>"
380 "<l2 xmlns=\"urn:tests:a\"><c>"
381 " <d>a</d>"
382 " <d>b</d>"
383 " <d>b</d>"
384 " <d>c</d>"
385 "</c></l2>"
386 "<l2 xmlns=\"urn:tests:a\"><c>"
387 "</c></l2>";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200388
Radek Iša56ca9e42020-09-08 18:42:00 +0200389 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
390 assert_int_equal(LY_SUCCESS, ly_path_parse(UTEST_LYCTX, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100391 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
Radek Krejcid5d37432021-03-12 13:46:40 +0100392 assert_int_equal(LY_SUCCESS, ly_path_compile(UTEST_LYCTX, NULL, NULL, NULL, exp, LY_PATH_LREF_FALSE, LY_PATH_OPER_INPUT,
Radek Krejci8df109d2021-04-23 12:19:08 +0200393 LY_PATH_TARGET_SINGLE, LY_VALUE_JSON, NULL, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200394 term = lyd_target(path, tree);
395
Michal Vaskoe78faec2021-04-08 17:24:43 +0200396 const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_USER;
Radek Iša56ca9e42020-09-08 18:42:00 +0200397
398 CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200399 assert_string_equal(lyd_get_value(&term->node), "b");
400 assert_string_equal(lyd_get_value(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200401
402 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 ly_path_free(UTEST_LYCTX, path);
404 lyxp_expr_free(UTEST_LYCTX, exp);
Michal Vasko38c2ba72020-07-27 14:46:59 +0200405}
406
Radek Krejcica989142020-11-05 11:32:22 +0100407static void
408test_list_pos(void **state)
409{
Radek Krejcica989142020-11-05 11:32:22 +0100410 const char *data;
411 struct lyd_node *tree;
412
413 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100414 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
415 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
416 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200417 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 +0100418 assert_int_equal(0, lyd_list_pos(tree));
419 assert_int_equal(1, lyd_list_pos(tree->next));
420 assert_int_equal(2, lyd_list_pos(tree->next->next));
421 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
422 lyd_free_all(tree);
423
424 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100425 "<ll xmlns=\"urn:tests:a\">two</ll>"
426 "<ll xmlns=\"urn:tests:a\">three</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 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 +0100428 assert_int_equal(1, lyd_list_pos(tree));
429 assert_int_equal(2, lyd_list_pos(tree->next));
430 assert_int_equal(3, lyd_list_pos(tree->next->next));
431 lyd_free_all(tree);
432
433 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100434 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
435 "<ll xmlns=\"urn:tests:a\">two</ll>"
436 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
437 "<ll xmlns=\"urn:tests:a\">three</ll>"
438 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 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 +0100440 assert_string_equal("l1", tree->schema->name);
441 assert_int_equal(1, lyd_list_pos(tree));
442 assert_int_equal(2, lyd_list_pos(tree->next));
443 assert_int_equal(3, lyd_list_pos(tree->next->next));
444 assert_string_equal("ll", tree->next->next->next->schema->name);
445 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
446 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
447 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
448 lyd_free_all(tree);
Radek Krejcica989142020-11-05 11:32:22 +0100449}
450
Radek Krejci4233f9b2020-11-05 12:38:35 +0100451static void
452test_first_sibling(void **state)
453{
Radek Krejci4233f9b2020-11-05 12:38:35 +0100454 const char *data;
455 struct lyd_node *tree;
456 struct lyd_node_inner *parent;
457
458 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100459 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
460 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200461 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 +0100462 assert_ptr_equal(tree, lyd_first_sibling(tree->next));
463 assert_ptr_equal(tree, lyd_first_sibling(tree));
464 assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100465 parent = (struct lyd_node_inner *)tree->next;
Radek Krejci4233f9b2020-11-05 12:38:35 +0100466 assert_int_equal(LYS_LIST, parent->schema->nodetype);
467 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
468 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
469 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
470 lyd_free_all(tree);
Radek Krejci4233f9b2020-11-05 12:38:35 +0100471}
472
Michal Vasko6b669ff2021-04-30 16:25:36 +0200473static void
474test_find_path(void **state)
475{
476 struct lyd_node *root;
477 const struct lys_module *mod;
478
479 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
480 assert_non_null(mod);
481
482 assert_int_equal(LY_SUCCESS, lyd_new_inner(NULL, mod, "cont", 0, &root));
483 assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='10.0.0.1']", NULL, LYD_NEW_PATH_UPDATE, NULL));
484 assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='2100::1']", NULL, LYD_NEW_PATH_UPDATE, NULL));
485
486 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='10.0.0.1']", 0, NULL));
487 assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='2100::1']", 0, NULL));
488 lyd_free_all(root);
489}
490
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100491int
492main(void)
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200493{
494 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200495 UTEST(test_compare, setup),
aPiecek24252202021-03-30 12:23:32 +0200496 UTEST(test_compare_diff_ctx, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 UTEST(test_dup, setup),
498 UTEST(test_target, setup),
499 UTEST(test_list_pos, setup),
500 UTEST(test_first_sibling, setup),
Michal Vasko6b669ff2021-04-30 16:25:36 +0200501 UTEST(test_find_path, setup),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200502 };
503
504 return cmocka_run_group_tests(tests, NULL, NULL);
505}