blob: b9419ae0e2dea08ebf72093658f760f9b7bd982b [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
Radek Iša56ca9e42020-09-08 18:42:00 +020044 UTEST_SETUP;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020045
aPiecek24252202021-03-30 12:23:32 +020046 UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
47 UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
Radek Krejci1f05b6a2019-07-18 16:15:06 +020048
49 return 0;
50}
51
Radek Iša56ca9e42020-09-08 18:42:00 +020052#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
53 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1f05b6a2019-07-18 16:15:06 +020054
aPiecek24252202021-03-30 12:23:32 +020055#define CHECK_PARSE_LYD_PARAM_CTX(CTX, INPUT, PARSE_OPTION, OUT_NODE) \
56 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(CTX, INPUT, LYD_XML, PARSE_OPTION, LYD_VALIDATE_PRESENT, &OUT_NODE)); \
57 assert_non_null(OUT_NODE);
58
59#define RECREATE_CTX_WITH_MODULE(CTX, MODULE) \
Radek Krejci90ed21e2021-04-12 14:47:46 +020060 ly_ctx_destroy(CTX); \
aPiecek24252202021-03-30 12:23:32 +020061 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &CTX)); \
62 assert_int_equal(LY_SUCCESS, ly_in_new_memory(MODULE, &_UC->in)); \
63 assert_int_equal(LY_SUCCESS, lys_parse(CTX, _UC->in, LYS_IN_YANG, NULL, NULL)); \
64 ly_in_free(_UC->in, 0);
65
Radek Krejci1f05b6a2019-07-18 16:15:06 +020066static void
67test_compare(void **state)
68{
Radek Krejci1f05b6a2019-07-18 16:15:06 +020069 struct lyd_node *tree1, *tree2;
Radek Iša56ca9e42020-09-08 18:42:00 +020070 const char *data1;
71 const char *data2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020072
Michal Vasko8f359bf2020-07-28 10:41:15 +020073 assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020074
Radek Iša56ca9e42020-09-08 18:42:00 +020075 data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
76 data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
77 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
78 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020079 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
80 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010081 assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner *)tree1)->child, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020082 lyd_free_all(tree1);
83 lyd_free_all(tree2);
84
85 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
86 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020087 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
88 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +010089 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
90 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020091 lyd_free_all(tree1);
92 lyd_free_all(tree2);
93
94 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
95 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +020096 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
97 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020098 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
99 assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0));
100 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0));
101 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200102 lyd_free_all(tree1);
103 lyd_free_all(tree2);
104
105 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
106 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
108 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200109 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
110 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200111 lyd_free_all(tree1);
112 lyd_free_all(tree2);
113
114 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
115 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
117 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200118 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
119 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200120 lyd_free_all(tree1);
121 lyd_free_all(tree2);
122
123 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
124 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200125 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
126 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100127 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200128 lyd_free_all(tree1);
129 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200130 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200131 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200132 lyd_free_all(tree1);
133 lyd_free_all(tree2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200134}
135
Radek Krejci22ebdba2019-07-25 13:59:43 +0200136static void
aPiecek24252202021-03-30 12:23:32 +0200137test_compare_diff_ctx(void **state)
138{
139 struct lyd_node *tree1, *tree2;
140 const char *data1, *data2;
141 struct ly_ctx *ctx2 = NULL;
142 const char *module;
143
144 /* create second context with the same schema */
145 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
146 "revision 2014-05-08;"
147 "list l2 {config false;"
148 " container c{leaf x {type string;}}"
149 "}}";
150 RECREATE_CTX_WITH_MODULE(ctx2, module);
151 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
152 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
153 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
154 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
155 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
156 lyd_free_all(tree1);
157 lyd_free_all(tree2);
158
159 /* recreate second context with schema that has a different name */
160 module = "module c {namespace urn:tests:c;prefix c;yang-version 1.1;"
161 "revision 2014-05-08;"
162 "list l2 {config false;"
163 " container c{leaf x {type string;}}"
164 "}}";
165 RECREATE_CTX_WITH_MODULE(ctx2, module);
166 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
167 data2 = "<l2 xmlns=\"urn:tests:c\"><c><x>b</x></c></l2>";
168 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
169 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
170 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
171 lyd_free_all(tree1);
172 lyd_free_all(tree2);
173
174 /* recreate second context with schema that has a different revision */
175 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
176 "revision 2015-05-08;"
177 "list l2 {config false;"
178 " container c{leaf x {type string;}}"
179 "}}";
180 RECREATE_CTX_WITH_MODULE(ctx2, module);
181 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
182 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
183 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
184 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
185 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
186 lyd_free_all(tree1);
187 lyd_free_all(tree2);
188
189 /* recreate second context with schema that has no revision */
190 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
191 "list l2 {config false;"
192 " container c{leaf x {type string;}}"
193 "}}";
194 RECREATE_CTX_WITH_MODULE(ctx2, module);
195 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
196 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
197 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
198 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
199 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
200 lyd_free_all(tree1);
201 lyd_free_all(tree2);
202
203 /* recreate second context with schema that has a different parent nodetype */
204 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
205 "revision 2014-05-08;"
206 "container l2 {config false;"
207 " container c{leaf x {type string;}}"
208 "}}";
209 RECREATE_CTX_WITH_MODULE(ctx2, module);
210 data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
211 data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>";
212 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1);
213 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2);
214 assert_int_equal(LY_ENOT, lyd_compare_single(lyd_child(lyd_child(tree1)), lyd_child(lyd_child(tree2)), 0));
215 lyd_free_all(tree1);
216 lyd_free_all(tree2);
217
218 /* recreate second context with the same opaq data nodes */
219 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
220 "revision 2014-05-08;"
221 "anydata any {config false;}"
222 "}";
223 RECREATE_CTX_WITH_MODULE(ctx2, module);
224 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
225 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:x</x></any>";
226 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
227 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
228 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
229 lyd_free_all(tree1);
230 lyd_free_all(tree2);
231
232 /* recreate second context with the different opaq data node value */
233 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
234 "revision 2014-05-08;"
235 "anydata any {config false;}"
236 "}";
237 RECREATE_CTX_WITH_MODULE(ctx2, module);
238 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
239 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:y</x></any>";
240 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
241 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
242 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
243 lyd_free_all(tree1);
244 lyd_free_all(tree2);
245
246 /* recreate second context with the wrong prefix in opaq data node value */
247 module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;"
248 "revision 2014-05-08;"
249 "anydata any {config false;}"
250 "}";
251 RECREATE_CTX_WITH_MODULE(ctx2, module);
252 data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>";
253 data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>cc:x</x></any>";
254 CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1);
255 CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2);
256 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
257 lyd_free_all(tree1);
258 lyd_free_all(tree2);
259
260 /* clean up */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200261 ly_ctx_destroy(ctx2);
aPiecek24252202021-03-30 12:23:32 +0200262 _UC->in = NULL;
263}
264
265static void
Radek Krejci22ebdba2019-07-25 13:59:43 +0200266test_dup(void **state)
267{
Radek Krejci22ebdba2019-07-25 13:59:43 +0200268 struct lyd_node *tree1, *tree2;
269 const char *result;
Radek Iša56ca9e42020-09-08 18:42:00 +0200270 const char *data;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200271
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
273 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200274 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200275 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200276 lyd_free_all(tree1);
277 lyd_free_all(tree2);
278
279 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
280 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200282 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200283 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200284 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200285 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200286 lyd_free_all(tree1);
287 lyd_free_all(tree2);
288
289 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
290 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200291 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200292 assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko26123192020-11-09 21:02:34 +0100293 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200294 lyd_free_all(tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100295 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &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 Vasko26123192020-11-09 21:02:34 +0100298 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200299 lyd_free_all(tree2);
300
Michal Vasko26123192020-11-09 21:02:34 +0100301 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200302 lyd_free_all(tree1);
303 result = "<l2 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200304 CHECK_PARSE_LYD_PARAM(result, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200305 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200306 lyd_free_all(tree1);
307 lyd_free_all(tree2);
308
309 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200310 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200311 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200312 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200313 lyd_free_all(tree1);
314 lyd_free_all(tree2);
315
316 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200317 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100318 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL,
319 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM;
321
322 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100323 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node *)tree2->parent->parent,
Michal Vasko26123192020-11-09 21:02:34 +0100324 LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200325 lyd_free_all(tree1);
326 lyd_free_all(tree2);
327
328 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200329 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100330 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL,
331 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200332 flag = LYS_CONFIG_W | LYS_SET_ENUM;
333 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "c", 0, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100334 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 +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);
Michal Vasko26123192020-11-09 21:02:34 +0100340 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100341 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child,
342 (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko26123192020-11-09 21:02:34 +0100343 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200344 lyd_free_all(tree1);
345 lyd_free_all(tree2);
346
347 /* invalid */
348 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 +0200349 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100350 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev,
351 (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200352 lyd_free_all(tree1);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200353}
354
Michal Vasko38c2ba72020-07-27 14:46:59 +0200355static void
356test_target(void **state)
357{
Michal Vasko38c2ba72020-07-27 14:46:59 +0200358 const struct lyd_node_term *term;
359 struct lyd_node *tree;
360 struct lyxp_expr *exp;
361 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200362 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200363 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100364 "<l2 xmlns=\"urn:tests:a\"><c>"
365 " <d>a</d>"
366 " </c></l2>"
367 "<l2 xmlns=\"urn:tests:a\"><c>"
368 " <d>a</d>"
369 " <d>b</d>"
370 " <d>b</d>"
371 " <d>c</d>"
372 "</c></l2>"
373 "<l2 xmlns=\"urn:tests:a\"><c>"
374 "</c></l2>";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200375
Radek Iša56ca9e42020-09-08 18:42:00 +0200376 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
377 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 +0100378 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
Radek Krejcid5d37432021-03-12 13:46:40 +0100379 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 +0200380 LY_PATH_TARGET_SINGLE, LY_VALUE_JSON, NULL, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200381 term = lyd_target(path, tree);
382
Michal Vaskoe78faec2021-04-08 17:24:43 +0200383 const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_USER;
Radek Iša56ca9e42020-09-08 18:42:00 +0200384
385 CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200386 assert_string_equal(lyd_get_value(&term->node), "b");
387 assert_string_equal(lyd_get_value(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200388
389 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 ly_path_free(UTEST_LYCTX, path);
391 lyxp_expr_free(UTEST_LYCTX, exp);
Michal Vasko38c2ba72020-07-27 14:46:59 +0200392}
393
Radek Krejcica989142020-11-05 11:32:22 +0100394static void
395test_list_pos(void **state)
396{
Radek Krejcica989142020-11-05 11:32:22 +0100397 const char *data;
398 struct lyd_node *tree;
399
400 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100401 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
402 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
403 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200404 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 +0100405 assert_int_equal(0, lyd_list_pos(tree));
406 assert_int_equal(1, lyd_list_pos(tree->next));
407 assert_int_equal(2, lyd_list_pos(tree->next->next));
408 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
409 lyd_free_all(tree);
410
411 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100412 "<ll xmlns=\"urn:tests:a\">two</ll>"
413 "<ll xmlns=\"urn:tests:a\">three</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200414 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 +0100415 assert_int_equal(1, lyd_list_pos(tree));
416 assert_int_equal(2, lyd_list_pos(tree->next));
417 assert_int_equal(3, lyd_list_pos(tree->next->next));
418 lyd_free_all(tree);
419
420 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100421 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
422 "<ll xmlns=\"urn:tests:a\">two</ll>"
423 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
424 "<ll xmlns=\"urn:tests:a\">three</ll>"
425 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200426 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 +0100427 assert_string_equal("l1", tree->schema->name);
428 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 assert_string_equal("ll", tree->next->next->next->schema->name);
432 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
433 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
434 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
435 lyd_free_all(tree);
Radek Krejcica989142020-11-05 11:32:22 +0100436}
437
Radek Krejci4233f9b2020-11-05 12:38:35 +0100438static void
439test_first_sibling(void **state)
440{
Radek Krejci4233f9b2020-11-05 12:38:35 +0100441 const char *data;
442 struct lyd_node *tree;
443 struct lyd_node_inner *parent;
444
445 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100446 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
447 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 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 +0100449 assert_ptr_equal(tree, lyd_first_sibling(tree->next));
450 assert_ptr_equal(tree, lyd_first_sibling(tree));
451 assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100452 parent = (struct lyd_node_inner *)tree->next;
Radek Krejci4233f9b2020-11-05 12:38:35 +0100453 assert_int_equal(LYS_LIST, parent->schema->nodetype);
454 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
455 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
456 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
457 lyd_free_all(tree);
Radek Krejci4233f9b2020-11-05 12:38:35 +0100458}
459
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100460int
461main(void)
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200462{
463 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 UTEST(test_compare, setup),
aPiecek24252202021-03-30 12:23:32 +0200465 UTEST(test_compare_diff_ctx, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 UTEST(test_dup, setup),
467 UTEST(test_target, setup),
468 UTEST(test_list_pos, setup),
469 UTEST(test_first_sibling, setup),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200470 };
471
472 return cmocka_run_group_tests(tests, NULL, NULL);
473}