blob: 17fd07837ef0675f50b0369a91efcf3a91bf9a9d [file] [log] [blame]
Michal Vaskob1b5c262020-03-05 14:29:47 +01001/**
2 * @file test_validation.c
Michal Vaskocde73ac2019-11-14 16:10:27 +01003 * @author: Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob1b5c262020-03-05 14:29:47 +01004 * @brief unit tests for functions from validation.c
Michal Vaskocde73ac2019-11-14 16:10:27 +01005 *
Michal Vaskob1b5c262020-03-05 14:29:47 +01006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vaskocde73ac2019-11-14 16:10:27 +01007 *
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 Vaskocde73ac2019-11-14 16:10:27 +010016
Michal Vaskocde73ac2019-11-14 16:10:27 +010017#include <stdio.h>
18#include <string.h>
19
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010023#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020025#include "tests_config.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "tree_data_internal.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010027#include "tree_schema.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010028
Radek Iša56ca9e42020-09-08 18:42:00 +020029#define LYD_TREE_CREATE(INPUT, MODEL) \
30 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, MODEL)
Michal Vaskocde73ac2019-11-14 16:10:27 +010031
Michal Vaskocde73ac2019-11-14 16:10:27 +010032static void
Radek Iša56ca9e42020-09-08 18:42:00 +020033test_when(void **state)
Michal Vaskocde73ac2019-11-14 16:10:27 +010034{
Radek Iša56ca9e42020-09-08 18:42:00 +020035 struct lyd_node *tree;
36 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010037 "module a {\n"
38 " namespace urn:tests:a;\n"
39 " prefix a;\n"
40 " yang-version 1.1;\n"
41 "\n"
42 " container cont {\n"
43 " leaf a {\n"
44 " when \"../../c = 'val_c'\";\n"
45 " type string;\n"
46 " }\n"
47 " leaf b {\n"
48 " type string;\n"
49 " }\n"
50 " }\n"
51 " leaf c {\n"
52 " when \"/cont/b = 'val_b'\";\n"
53 " type string;\n"
54 " }\n"
55 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +020056
57 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
58
59 CHECK_PARSE_LYD_PARAM("<c xmlns=\"urn:tests:a\">hey</c>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +010060 CHECK_LOG_CTX("When condition \"/cont/b = 'val_b'\" not satisfied.", "/a:c", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +020061
62 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>", tree);
63 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 0, LYS_LEAF, 0, 0, NULL, 1);
64 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
65 lyd_free_all(tree);
66
67 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>", tree);
68 CHECK_LYSC_NODE(lyd_child(tree)->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 1);
69 assert_int_equal(LYD_WHEN_TRUE, lyd_child(tree)->flags);
70 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 0, LYS_LEAF, 0, 0, NULL, 1);
71 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
72 lyd_free_all(tree);
73}
74
75static void
Christian Hoppsb6ecaea2021-02-06 09:45:38 -050076test_mandatory_when(void **state)
77{
78 struct lyd_node *tree;
79 const char *schema =
80 "module a {\n"
81 " namespace urn:tests:a;\n"
82 " prefix a;\n"
83 " yang-version 1.1;\n"
84 "\n"
85 " container cont {\n"
86 " leaf a {\n"
87 " type string;\n"
88 " }\n"
89 " leaf b {\n"
90 " when \"../a = 'val_a'\";\n"
91 " mandatory true;\n"
92 " type string;\n"
93 " }\n"
94 " }\n"
95 " leaf c {\n"
96 " type string;\n"
97 " }\n"
98 " leaf d {\n"
99 " when \"../c = 'val_c'\";\n"
100 " mandatory true;\n"
101 " type string;\n"
102 " }\n"
103 "}";
104
105 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
106
107 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:a\">hey</d>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100108 CHECK_LOG_CTX("When condition \"../c = 'val_c'\" not satisfied.", "/a:d", 0);
Christian Hoppsb6ecaea2021-02-06 09:45:38 -0500109
110 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:a\"><b>hey</b></cont>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100111 CHECK_LOG_CTX("When condition \"../a = 'val_a'\" not satisfied.", "/a:cont/b", 0);
Christian Hoppsb6ecaea2021-02-06 09:45:38 -0500112
113 LYD_TREE_CREATE("<c xmlns=\"urn:tests:a\">val_c</c><d xmlns=\"urn:tests:a\">hey</d>", tree);
114 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "d", 0, LYS_LEAF, 0, 0, NULL, 1);
115 assert_int_equal(LYD_WHEN_TRUE, tree->next->next->flags);
116 lyd_free_all(tree);
117
118 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:a\"><a>val_a</a><b>hey</b></cont>", tree);
119 CHECK_LYSC_NODE(lyd_child(tree)->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "b", 0, LYS_LEAF, tree->schema, 0, NULL, 1);
120 assert_int_equal(LYD_WHEN_TRUE, lyd_child(tree)->next->flags);
121 lyd_free_all(tree);
122}
123
124static void
Michal Vasko850d8cb2022-08-05 12:51:00 +0200125test_type_incomplete_when(void **state)
126{
127 struct lys_module *mod;
128 struct lyd_node *tree;
129 const char *schema =
130 "module a {\n"
131 " namespace urn:tests:a;\n"
132 " prefix a;\n"
133 " yang-version 1.1;\n"
134 "\n"
135 " container cont {\n"
136 " when \"../c = 'val_c'\";\n"
137 " leaf a {\n"
138 " type leafref {\n"
139 " path \"/a:c\";\n"
140 " }\n"
141 " }\n"
142 " leaf b {\n"
143 " type string;\n"
144 " }\n"
145 " }\n"
146 " leaf c {\n"
147 " type string;\n"
148 " }\n"
149 "}";
150
151 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
152
153 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:a\"><a>val_c</a><b>val</b></cont><c xmlns=\"urn:tests:a\">val_c</c>", tree);
154
155 /* make the when false */
156 assert_int_equal(LY_SUCCESS, lyd_change_term(tree->next, "wrong-val"));
157
158 /* autodelete when with a leafref */
159 assert_int_equal(LY_SUCCESS, lyd_validate_module(&tree, mod, 0, NULL));
160 assert_string_equal(LYD_NAME(tree), "c");
161 assert_null(tree->next);
162
163 lyd_free_all(tree);
164}
165
166static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200167test_mandatory(void **state)
168{
169 struct lyd_node *tree;
170 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100171 "module b {\n"
172 " namespace urn:tests:b;\n"
173 " prefix b;\n"
174 " yang-version 1.1;\n"
175 "\n"
176 " choice choic {\n"
177 " mandatory true;\n"
178 " leaf a {\n"
179 " type string;\n"
180 " }\n"
181 " case b {\n"
182 " leaf l {\n"
183 " type string;\n"
184 " }\n"
185 " }\n"
186 " }\n"
187 " leaf c {\n"
188 " mandatory true;\n"
189 " type string;\n"
190 " }\n"
191 " leaf d {\n"
192 " type empty;\n"
193 " }\n"
194 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200195
196 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
197
198 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:b\"/>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100199 CHECK_LOG_CTX_APPTAG("Mandatory choice \"choic\" data do not exist.", "/b:choic", 0, "missing-choice");
Radek Iša56ca9e42020-09-08 18:42:00 +0200200
201 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100202 CHECK_LOG_CTX("Mandatory node \"c\" instance does not exist.", "/b:c", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200203
204 CHECK_PARSE_LYD_PARAM("<a xmlns=\"urn:tests:b\">string</a>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100205 CHECK_LOG_CTX("Mandatory node \"c\" instance does not exist.", "/b:c", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200206
207 LYD_TREE_CREATE("<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>", tree);
208 lyd_free_siblings(tree);
209}
210
211static void
212test_minmax(void **state)
213{
214 struct lyd_node *tree;
215 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100216 "module c {\n"
217 " namespace urn:tests:c;\n"
218 " prefix c;\n"
219 " yang-version 1.1;\n"
220 "\n"
221 " choice choic {\n"
222 " leaf a {\n"
223 " type string;\n"
224 " }\n"
225 " case b {\n"
226 " leaf-list l {\n"
227 " min-elements 3;\n"
228 " type string;\n"
229 " }\n"
230 " }\n"
231 " }\n"
232 " list lt {\n"
233 " max-elements 4;\n"
234 " key \"k\";\n"
235 " leaf k {\n"
236 " type string;\n"
237 " }\n"
238 " }\n"
239 " leaf d {\n"
240 " type empty;\n"
241 " }\n"
242 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200243
244 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
245
Michal Vasko6c16cda2021-02-04 11:05:52 +0100246 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">mate</l>"
247 "<d xmlns=\"urn:tests:c\"/>",
248 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100249 CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:choic/b/l", 0, "too-few-elements");
Radek Iša56ca9e42020-09-08 18:42:00 +0200250
251 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">val1</l>"
252 "<l xmlns=\"urn:tests:c\">val2</l>",
253 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100254 CHECK_LOG_CTX_APPTAG("Too few \"l\" instances.", "/c:choic/b/l", 0, "too-few-elements");
Radek Iša56ca9e42020-09-08 18:42:00 +0200255
256 LYD_TREE_CREATE("<l xmlns=\"urn:tests:c\">val1</l>"
257 "<l xmlns=\"urn:tests:c\">val2</l>"
258 "<l xmlns=\"urn:tests:c\">val3</l>", tree);
259 lyd_free_all(tree);
260
261 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">val1</l>"
262 "<l xmlns=\"urn:tests:c\">val2</l>"
263 "<l xmlns=\"urn:tests:c\">val3</l>"
264 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
265 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
266 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
267 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100268 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>"
269 "<lt xmlns=\"urn:tests:c\"><k>val6</k></lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100270 CHECK_LOG_CTX_APPTAG("Too many \"lt\" instances.", "/c:lt[k='val5']", 0, "too-many-elements");
Radek Iša56ca9e42020-09-08 18:42:00 +0200271}
272
273const char *schema_d =
274 "module d {\n"
275 " namespace urn:tests:d;\n"
276 " prefix d;\n"
277 " yang-version 1.1;\n"
278 "\n"
279 " list lt {\n"
280 " key \"k\";\n"
281 " unique \"l1\";\n"
282 " leaf k {\n"
283 " type string;\n"
284 " }\n"
285 " leaf l1 {\n"
286 " type string;\n"
287 " }\n"
288 " }\n"
289 " list lt2 {\n"
290 " key \"k\";\n"
291 " unique \"cont/l2 l4\";\n"
292 " unique \"l5 l6\";\n"
293 " leaf k {\n"
294 " type string;\n"
295 " }\n"
296 " container cont {\n"
297 " leaf l2 {\n"
298 " type string;\n"
299 " }\n"
300 " }\n"
301 " leaf l4 {\n"
302 " type string;\n"
303 " }\n"
304 " leaf l5 {\n"
305 " type string;\n"
306 " }\n"
307 " leaf l6 {\n"
308 " type string;\n"
309 " }\n"
310 " list lt3 {\n"
311 " key \"kk\";\n"
312 " unique \"l3\";\n"
313 " leaf kk {\n"
314 " type string;\n"
315 " }\n"
316 " leaf l3 {\n"
317 " type string;\n"
318 " }\n"
319 " }\n"
320 " }\n"
321 "}";
322
323static void
324test_unique(void **state)
325{
326 struct lyd_node *tree;
327
328 UTEST_ADD_MODULE(schema_d, LYS_IN_YANG, NULL, NULL);
329
330 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
331 " <k>val1</k>\n"
332 " <l1>same</l1>\n"
333 "</lt>\n"
334 "<lt xmlns=\"urn:tests:d\">\n"
335 " <k>val2</k>\n"
336 "</lt>", tree);
337 lyd_free_all(tree);
338
339 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
340 " <k>val1</k>\n"
341 " <l1>same</l1>\n"
342 "</lt>\n"
343 "<lt xmlns=\"urn:tests:d\">\n"
344 " <k>val2</k>\n"
345 " <l1>not-same</l1>\n"
346 "</lt>", tree);
347 lyd_free_all(tree);
348
349 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:d\">\n"
350 " <k>val1</k>\n"
351 " <l1>same</l1>\n"
352 "</lt>\n"
353 "<lt xmlns=\"urn:tests:d\">\n"
354 " <k>val2</k>\n"
355 " <l1>same</l1>\n"
356 "</lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200357 CHECK_LOG_CTX_APPTAG("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val1']\" and \"/d:lt[k='val2']\".",
Michal Vasko7a266772024-01-23 11:02:38 +0100358 "/d:lt[k='val2']", 0, "data-not-unique");
Radek Iša56ca9e42020-09-08 18:42:00 +0200359
360 /* now try with more instances */
361 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
362 " <k>val1</k>\n"
363 " <l1>1</l1>\n"
364 "</lt>\n"
365 "<lt xmlns=\"urn:tests:d\">\n"
366 " <k>val2</k>\n"
367 " <l1>2</l1>\n"
368 "</lt>\n"
369 "<lt xmlns=\"urn:tests:d\">\n"
370 " <k>val3</k>\n"
371 " <l1>3</l1>\n"
372 "</lt>\n"
373 "<lt xmlns=\"urn:tests:d\">\n"
374 " <k>val4</k>\n"
375 " <l1>4</l1>\n"
376 "</lt>\n"
377 "<lt xmlns=\"urn:tests:d\">\n"
378 " <k>val5</k>\n"
379 " <l1>5</l1>\n"
380 "</lt>\n"
381 "<lt xmlns=\"urn:tests:d\">\n"
382 " <k>val6</k>\n"
383 " <l1>6</l1>\n"
384 "</lt>\n"
385 "<lt xmlns=\"urn:tests:d\">\n"
386 " <k>val7</k>\n"
387 " <l1>7</l1>\n"
388 "</lt>\n"
389 "<lt xmlns=\"urn:tests:d\">\n"
390 " <k>val8</k>\n"
391 " <l1>8</l1>\n"
392 "</lt>", tree);
393 lyd_free_all(tree);
394
395 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
396 " <k>val1</k>\n"
397 " <l1>1</l1>\n"
398 "</lt>\n"
399 "<lt xmlns=\"urn:tests:d\">\n"
400 " <k>val2</k>\n"
401 " <l1>2</l1>\n"
402 "</lt>\n"
403 "<lt xmlns=\"urn:tests:d\">\n"
404 " <k>val3</k>\n"
405 " <l1>3</l1>\n"
406 "</lt>\n"
407 "<lt xmlns=\"urn:tests:d\">\n"
408 " <k>val4</k>\n"
409 "</lt>\n"
410 "<lt xmlns=\"urn:tests:d\">\n"
411 " <k>val5</k>\n"
412 " <l1>5</l1>\n"
413 "</lt>\n"
414 "<lt xmlns=\"urn:tests:d\">\n"
415 " <k>val6</k>\n"
416 " <l1>6</l1>\n"
417 "</lt>\n"
418 "<lt xmlns=\"urn:tests:d\">\n"
419 " <k>val7</k>\n"
420 "</lt>\n"
421 "<lt xmlns=\"urn:tests:d\">\n"
422 " <k>val8</k>\n"
423 "</lt>", tree);
424 lyd_free_all(tree);
425
426 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:d\">\n"
427 " <k>val1</k>\n"
428 " <l1>1</l1>\n"
429 "</lt>\n"
430 "<lt xmlns=\"urn:tests:d\">\n"
431 " <k>val2</k>\n"
432 " <l1>2</l1>\n"
433 "</lt>\n"
434 "<lt xmlns=\"urn:tests:d\">\n"
435 " <k>val3</k>\n"
436 "</lt>\n"
437 "<lt xmlns=\"urn:tests:d\">\n"
438 " <k>val4</k>\n"
439 " <l1>4</l1>\n"
440 "</lt>\n"
441 "<lt xmlns=\"urn:tests:d\">\n"
442 " <k>val5</k>\n"
443 "</lt>\n"
444 "<lt xmlns=\"urn:tests:d\">\n"
445 " <k>val6</k>\n"
446 "</lt>\n"
447 "<lt xmlns=\"urn:tests:d\">\n"
448 " <k>val7</k>\n"
449 " <l1>2</l1>\n"
450 "</lt>\n"
451 "<lt xmlns=\"urn:tests:d\">\n"
452 " <k>val8</k>\n"
453 " <l1>8</l1>\n"
454 "</lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200455 CHECK_LOG_CTX_APPTAG("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val7']\" and \"/d:lt[k='val2']\".",
Michal Vasko7a266772024-01-23 11:02:38 +0100456 "/d:lt[k='val2']", 0, "data-not-unique");
Radek Iša56ca9e42020-09-08 18:42:00 +0200457}
458
459static void
460test_unique_nested(void **state)
461{
462 struct lyd_node *tree;
463
464 UTEST_ADD_MODULE(schema_d, LYS_IN_YANG, NULL, NULL);
465
466 /* nested list uniquest are compared only with instances in the same parent list instance */
467 LYD_TREE_CREATE("<lt2 xmlns=\"urn:tests:d\">\n"
468 " <k>val1</k>\n"
469 " <cont>\n"
470 " <l2>1</l2>\n"
471 " </cont>\n"
472 " <l4>1</l4>\n"
473 "</lt2>\n"
474 "<lt2 xmlns=\"urn:tests:d\">\n"
475 " <k>val2</k>\n"
476 " <cont>\n"
477 " <l2>2</l2>\n"
478 " </cont>\n"
479 " <l4>2</l4>\n"
480 " <lt3>\n"
481 " <kk>val1</kk>\n"
482 " <l3>1</l3>\n"
483 " </lt3>\n"
484 " <lt3>\n"
485 " <kk>val2</kk>\n"
486 " <l3>2</l3>\n"
487 " </lt3>\n"
488 "</lt2>\n"
489 "<lt2 xmlns=\"urn:tests:d\">\n"
490 " <k>val3</k>\n"
491 " <cont>\n"
492 " <l2>3</l2>\n"
493 " </cont>\n"
494 " <l4>3</l4>\n"
495 " <lt3>\n"
496 " <kk>val1</kk>\n"
497 " <l3>2</l3>\n"
498 " </lt3>\n"
499 "</lt2>\n"
500 "<lt2 xmlns=\"urn:tests:d\">\n"
501 " <k>val4</k>\n"
502 " <cont>\n"
503 " <l2>4</l2>\n"
504 " </cont>\n"
505 " <l4>4</l4>\n"
506 " <lt3>\n"
507 " <kk>val1</kk>\n"
508 " <l3>3</l3>\n"
509 " </lt3>\n"
510 "</lt2>\n"
511 "<lt2 xmlns=\"urn:tests:d\">\n"
512 " <k>val5</k>\n"
513 " <cont>\n"
514 " <l2>5</l2>\n"
515 " </cont>\n"
516 " <l4>5</l4>\n"
517 " <lt3>\n"
518 " <kk>val1</kk>\n"
519 " <l3>3</l3>\n"
520 " </lt3>\n"
521 "</lt2>", tree);
522 lyd_free_all(tree);
523
524 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
525 " <k>val1</k>\n"
526 " <cont>\n"
527 " <l2>1</l2>\n"
528 " </cont>\n"
529 " <l4>1</l4>\n"
530 "</lt2>\n"
531 "<lt2 xmlns=\"urn:tests:d\">\n"
532 " <k>val2</k>\n"
533 " <cont>\n"
534 " <l2>2</l2>\n"
535 " </cont>\n"
536 " <lt3>\n"
537 " <kk>val1</kk>\n"
538 " <l3>1</l3>\n"
539 " </lt3>\n"
540 " <lt3>\n"
541 " <kk>val2</kk>\n"
542 " <l3>2</l3>\n"
543 " </lt3>\n"
544 " <lt3>\n"
545 " <kk>val3</kk>\n"
546 " <l3>1</l3>\n"
547 " </lt3>\n"
548 "</lt2>\n"
549 "<lt2 xmlns=\"urn:tests:d\">\n"
550 " <k>val3</k>\n"
551 " <cont>\n"
552 " <l2>3</l2>\n"
553 " </cont>\n"
554 " <l4>1</l4>\n"
555 " <lt3>\n"
556 " <kk>val1</kk>\n"
557 " <l3>2</l3>\n"
558 " </lt3>\n"
559 "</lt2>\n"
560 "<lt2 xmlns=\"urn:tests:d\">\n"
561 " <k>val4</k>\n"
562 " <cont>\n"
563 " <l2>4</l2>\n"
564 " </cont>\n"
565 " <lt3>\n"
566 " <kk>val1</kk>\n"
567 " <l3>3</l3>\n"
568 " </lt3>\n"
569 "</lt2>\n"
570 "<lt2 xmlns=\"urn:tests:d\">\n"
571 " <k>val5</k>\n"
572 " <cont>\n"
573 " <l2>5</l2>\n"
574 " </cont>\n"
575 " <lt3>\n"
576 " <kk>val1</kk>\n"
577 " <l3>3</l3>\n"
578 " </lt3>\n"
579 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200580 CHECK_LOG_CTX_APPTAG("Unique data leaf(s) \"l3\" not satisfied in \"/d:lt2[k='val2']/lt3[kk='val3']\" and "
Michal Vasko7a266772024-01-23 11:02:38 +0100581 "\"/d:lt2[k='val2']/lt3[kk='val1']\".", "/d:lt2[k='val2']/lt3[kk='val1']", 0, "data-not-unique");
Radek Iša56ca9e42020-09-08 18:42:00 +0200582
583 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
584 " <k>val1</k>\n"
585 " <cont>\n"
586 " <l2>1</l2>\n"
587 " </cont>\n"
588 " <l4>1</l4>\n"
589 "</lt2>\n"
590 "<lt2 xmlns=\"urn:tests:d\">\n"
591 " <k>val2</k>\n"
592 " <cont>\n"
593 " <l2>2</l2>\n"
594 " </cont>\n"
595 " <l4>2</l4>\n"
596 "</lt2>\n"
597 "<lt2 xmlns=\"urn:tests:d\">\n"
598 " <k>val3</k>\n"
599 " <cont>\n"
600 " <l2>3</l2>\n"
601 " </cont>\n"
602 " <l4>3</l4>\n"
603 "</lt2>\n"
604 "<lt2 xmlns=\"urn:tests:d\">\n"
605 " <k>val4</k>\n"
606 " <cont>\n"
607 " <l2>2</l2>\n"
608 " </cont>\n"
609 " <l4>2</l4>\n"
610 "</lt2>\n"
611 "<lt2 xmlns=\"urn:tests:d\">\n"
612 " <k>val5</k>\n"
613 " <cont>\n"
614 " <l2>5</l2>\n"
615 " </cont>\n"
616 " <l4>5</l4>\n"
617 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200618 CHECK_LOG_CTX_APPTAG("Unique data leaf(s) \"cont/l2 l4\" not satisfied in \"/d:lt2[k='val4']\" and \"/d:lt2[k='val2']\".",
Michal Vasko7a266772024-01-23 11:02:38 +0100619 "/d:lt2[k='val2']", 0, "data-not-unique");
Radek Iša56ca9e42020-09-08 18:42:00 +0200620
621 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
622 " <k>val1</k>\n"
623 " <cont>\n"
624 " <l2>1</l2>\n"
625 " </cont>\n"
626 " <l4>1</l4>\n"
627 " <l5>1</l5>\n"
628 " <l6>1</l6>\n"
629 "</lt2>\n"
630 "<lt2 xmlns=\"urn:tests:d\">\n"
631 " <k>val2</k>\n"
632 " <cont>\n"
633 " <l2>2</l2>\n"
634 " </cont>\n"
635 " <l4>1</l4>\n"
636 " <l5>1</l5>\n"
637 "</lt2>\n"
638 "<lt2 xmlns=\"urn:tests:d\">\n"
639 " <k>val3</k>\n"
640 " <cont>\n"
641 " <l2>3</l2>\n"
642 " </cont>\n"
643 " <l4>1</l4>\n"
644 " <l5>3</l5>\n"
645 " <l6>3</l6>\n"
646 "</lt2>\n"
647 "<lt2 xmlns=\"urn:tests:d\">\n"
648 " <k>val4</k>\n"
649 " <cont>\n"
650 " <l2>4</l2>\n"
651 " </cont>\n"
652 " <l4>1</l4>\n"
653 " <l6>1</l6>\n"
654 "</lt2>\n"
655 "<lt2 xmlns=\"urn:tests:d\">\n"
656 " <k>val5</k>\n"
657 " <cont>\n"
658 " <l2>5</l2>\n"
659 " </cont>\n"
660 " <l4>1</l4>\n"
661 " <l5>3</l5>\n"
662 " <l6>3</l6>\n"
663 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200664 CHECK_LOG_CTX_APPTAG("Unique data leaf(s) \"l5 l6\" not satisfied in \"/d:lt2[k='val5']\" and \"/d:lt2[k='val3']\".",
Michal Vasko7a266772024-01-23 11:02:38 +0100665 "/d:lt2[k='val3']", 0, "data-not-unique");
Radek Iša56ca9e42020-09-08 18:42:00 +0200666}
667
668static void
669test_dup(void **state)
670{
671 struct lyd_node *tree;
672 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100673 "module e {\n"
674 " namespace urn:tests:e;\n"
675 " prefix e;\n"
676 " yang-version 1.1;\n"
677 "\n"
678 " choice choic {\n"
679 " leaf a {\n"
680 " type string;\n"
681 " }\n"
682 " case b {\n"
683 " leaf-list l {\n"
684 " type string;\n"
685 " }\n"
686 " }\n"
687 " }\n"
688 " list lt {\n"
689 " key \"k\";\n"
690 " leaf k {\n"
691 " type string;\n"
692 " }\n"
693 " }\n"
694 " leaf d {\n"
695 " type uint32;\n"
696 " }\n"
697 " leaf-list ll {\n"
698 " type string;\n"
699 " }\n"
700 " container cont {\n"
701 " list lt {\n"
702 " key \"k\";\n"
703 " leaf k {\n"
704 " type string;\n"
705 " }\n"
706 " }\n"
707 " leaf d {\n"
708 " type uint32;\n"
709 " }\n"
710 " leaf-list ll {\n"
711 " type string;\n"
712 " }\n"
713 " leaf-list ll2 {\n"
714 " type enumeration {\n"
715 " enum one;\n"
716 " enum two;\n"
717 " }\n"
718 " }\n"
719 " }\n"
720 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200721
722 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
723
724 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>",
725 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100726 CHECK_LOG_CTX("Duplicate instance of \"d\".", "/e:d", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200727
728 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:e\"><k>A</k></lt>"
729 "<lt xmlns=\"urn:tests:e\"><k>B</k></lt>"
730 "<lt xmlns=\"urn:tests:e\"><k>A</k></lt>",
731 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100732 CHECK_LOG_CTX("Duplicate instance of \"lt\".", "/e:lt[k='A']", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200733
734 CHECK_PARSE_LYD_PARAM("<ll xmlns=\"urn:tests:e\">A</ll>"
735 "<ll xmlns=\"urn:tests:e\">B</ll>"
736 "<ll xmlns=\"urn:tests:e\">B</ll>",
737 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100738 CHECK_LOG_CTX("Duplicate instance of \"ll\".", "/e:ll[.='B']", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200739
740 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>",
741 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100742 CHECK_LOG_CTX("Duplicate instance of \"cont\".", "/e:cont", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200743
744 /* same tests again but using hashes */
745 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"><d>25</d><d>50</d><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll></cont>",
746 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100747 CHECK_LOG_CTX("Duplicate instance of \"d\".", "/e:cont/d", 1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200748
749 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
750 "<lt><k>a</k></lt>"
751 "<lt><k>b</k></lt>"
752 "<lt><k>c</k></lt>"
753 "<lt><k>d</k></lt>"
754 "<lt><k>c</k></lt></cont>",
755 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100756 CHECK_LOG_CTX("Duplicate instance of \"lt\".", "/e:cont/lt[k='c']", 1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200757
758 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
759 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>",
760 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100761 CHECK_LOG_CTX("Duplicate instance of \"ll\".", "/e:cont/ll[.='d']", 1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200762
763 /* cases */
764 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:e\">a</l>"
765 "<l xmlns=\"urn:tests:e\">b</l>"
766 "<l xmlns=\"urn:tests:e\">c</l>"
767 "<l xmlns=\"urn:tests:e\">b</l>",
768 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100769 CHECK_LOG_CTX("Duplicate instance of \"l\".", "/e:l[.='b']", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200770
771 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l>"
772 "<l xmlns=\"urn:tests:e\">c</l>"
773 "<a xmlns=\"urn:tests:e\">aa</a>",
774 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100775 CHECK_LOG_CTX("Data for both cases \"a\" and \"b\" exist.", "/e:choic", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200776}
777
778static void
779test_defaults(void **state)
780{
781 struct lyd_node *tree, *node, *diff;
Michal Vasko4de7d072021-07-09 09:13:18 +0200782 struct lys_module *mod;
Radek Iša56ca9e42020-09-08 18:42:00 +0200783 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100784 "module f {\n"
785 " namespace urn:tests:f;\n"
786 " prefix f;\n"
787 " yang-version 1.1;\n"
788 "\n"
789 " choice choic {\n"
790 " default \"c\";\n"
791 " leaf a {\n"
792 " type string;\n"
793 " }\n"
794 " case b {\n"
795 " leaf l {\n"
796 " type string;\n"
797 " }\n"
798 " }\n"
799 " case c {\n"
800 " leaf-list ll1 {\n"
801 " type string;\n"
802 " default \"def1\";\n"
803 " default \"def2\";\n"
804 " default \"def3\";\n"
805 " }\n"
806 " }\n"
807 " }\n"
808 " leaf d {\n"
809 " type uint32;\n"
810 " default 15;\n"
811 " }\n"
Radek Krejci7be7b9f2021-02-24 11:46:27 +0100812 " leaf dd {\n"
813 " type uint32;\n"
814 " when '../d = 666';\n"
815 " default 15;\n"
816 " }\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100817 " leaf-list ll2 {\n"
818 " type string;\n"
819 " default \"dflt1\";\n"
820 " default \"dflt2\";\n"
821 " }\n"
822 " container cont {\n"
823 " choice choic {\n"
824 " default \"c\";\n"
825 " leaf a {\n"
826 " type string;\n"
827 " }\n"
828 " case b {\n"
829 " leaf l {\n"
830 " type string;\n"
831 " }\n"
832 " }\n"
833 " case c {\n"
834 " leaf-list ll1 {\n"
835 " type string;\n"
836 " default \"def1\";\n"
837 " default \"def2\";\n"
838 " default \"def3\";\n"
839 " }\n"
840 " }\n"
841 " }\n"
842 " leaf d {\n"
843 " type uint32;\n"
844 " default 15;\n"
845 " }\n"
Radek Krejci7be7b9f2021-02-24 11:46:27 +0100846 " leaf dd {\n"
847 " type uint32;\n"
848 " when '../d = 666';\n"
849 " default 15;\n"
850 " }\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100851 " leaf-list ll2 {\n"
852 " type string;\n"
853 " default \"dflt1\";\n"
854 " default \"dflt2\";\n"
855 " }\n"
856 " }\n"
857 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200858
859 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
860
861 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
862 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));\
863
864 /* get defaults */
865 tree = NULL;
866 assert_int_equal(lyd_validate_module(&tree, mod, 0, &diff), LY_SUCCESS);
867 assert_non_null(tree);
868 assert_non_null(diff);
869
870 /* check all defaults exist */
871 CHECK_LYD_STRING_PARAM(tree,
872 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
873 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
874 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
875 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
876 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
877 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
878 "<cont xmlns=\"urn:tests:f\">\n"
879 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
880 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
881 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
882 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
883 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
884 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
885 "</cont>\n",
886 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
887
888 /* check diff */
889 CHECK_LYD_STRING_PARAM(diff,
890 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def1</ll1>\n"
891 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def2</ll1>\n"
892 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def3</ll1>\n"
893 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">15</d>\n"
894 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt1</ll2>\n"
895 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt2</ll2>\n"
896 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">\n"
897 " <ll1 yang:operation=\"create\">def1</ll1>\n"
898 " <ll1 yang:operation=\"create\">def2</ll1>\n"
899 " <ll1 yang:operation=\"create\">def3</ll1>\n"
900 " <d yang:operation=\"create\">15</d>\n"
901 " <ll2 yang:operation=\"create\">dflt1</ll2>\n"
902 " <ll2 yang:operation=\"create\">dflt2</ll2>\n"
903 "</cont>\n",
904 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
905 lyd_free_all(diff);
906
907 /* create another explicit case and validate */
908 assert_int_equal(lyd_new_term(NULL, mod, "l", "value", 0, &node), LY_SUCCESS);
909 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
910 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
911
912 /* check data tree */
913 CHECK_LYD_STRING_PARAM(tree,
914 "<l xmlns=\"urn:tests:f\">value</l>\n"
915 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
916 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
917 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
918 "<cont xmlns=\"urn:tests:f\">\n"
919 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
920 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
921 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
922 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
923 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
924 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
925 "</cont>\n",
926 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
927
928 /* check diff */
929 CHECK_LYD_STRING_PARAM(diff,
930 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def1</ll1>\n"
931 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def2</ll1>\n"
932 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def3</ll1>\n",
933 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
934 lyd_free_all(diff);
935
936 /* create explicit leaf-list and leaf and validate */
937 assert_int_equal(lyd_new_term(NULL, mod, "d", "15", 0, &node), LY_SUCCESS);
938 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
939 assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", 0, &node), LY_SUCCESS);
940 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
941 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
942
943 /* check data tree */
944 CHECK_LYD_STRING_PARAM(tree,
945 "<l xmlns=\"urn:tests:f\">value</l>\n"
946 "<d xmlns=\"urn:tests:f\">15</d>\n"
947 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
948 "<cont xmlns=\"urn:tests:f\">\n"
949 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
950 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
951 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
952 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
953 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
954 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
955 "</cont>\n",
956 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
957
958 /* check diff */
959 CHECK_LYD_STRING_PARAM(diff,
960 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">15</d>\n"
961 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt1</ll2>\n"
962 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt2</ll2>\n",
963 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
964 lyd_free_all(diff);
965
966 /* create first explicit container, which should become implicit */
967 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
968 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
969 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
970
971 /* check data tree */
972 CHECK_LYD_STRING_PARAM(tree,
973 "<l xmlns=\"urn:tests:f\">value</l>\n"
974 "<d xmlns=\"urn:tests:f\">15</d>\n"
975 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
976 "<cont xmlns=\"urn:tests:f\">\n"
977 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
978 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
979 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
980 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
981 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
982 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
983 "</cont>\n",
984 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
985 /* check diff */
986 assert_null(diff);
987
988 /* create second explicit container, which should become implicit, so the first tree node should be removed */
989 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
990 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
991 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
992
993 /* check data tree */
994 CHECK_LYD_STRING_PARAM(tree,
995 "<l xmlns=\"urn:tests:f\">value</l>\n"
996 "<d xmlns=\"urn:tests:f\">15</d>\n"
997 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
998 "<cont xmlns=\"urn:tests:f\">\n"
999 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
1000 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
1001 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
1002 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
1003 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
1004 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
1005 "</cont>\n",
1006 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
1007 /* check diff */
1008 assert_null(diff);
1009
1010 /* similar changes for nested defaults */
1011 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll1", "def3", 0, NULL), LY_SUCCESS);
1012 assert_int_equal(lyd_new_term(tree->prev, NULL, "d", "5", 0, NULL), LY_SUCCESS);
1013 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll2", "non-dflt", 0, NULL), LY_SUCCESS);
1014 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
1015
1016 /* check data tree */
1017 CHECK_LYD_STRING_PARAM(tree,
1018 "<l xmlns=\"urn:tests:f\">value</l>\n"
1019 "<d xmlns=\"urn:tests:f\">15</d>\n"
1020 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
1021 "<cont xmlns=\"urn:tests:f\">\n"
1022 " <ll1>def3</ll1>\n"
1023 " <d>5</d>\n"
1024 " <ll2>non-dflt</ll2>\n"
1025 "</cont>\n",
1026 LYD_XML, LYD_PRINT_WITHSIBLINGS);
1027
1028 /* check diff */
1029 CHECK_LYD_STRING_PARAM(diff,
1030 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">\n"
1031 " <ll1 yang:operation=\"delete\">def1</ll1>\n"
1032 " <ll1 yang:operation=\"delete\">def2</ll1>\n"
1033 " <ll1 yang:operation=\"delete\">def3</ll1>\n"
1034 " <d yang:operation=\"delete\">15</d>\n"
1035 " <ll2 yang:operation=\"delete\">dflt1</ll2>\n"
1036 " <ll2 yang:operation=\"delete\">dflt2</ll2>\n"
1037 "</cont>\n",
1038 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
1039 lyd_free_all(diff);
1040 lyd_free_all(tree);
Radek Krejci7be7b9f2021-02-24 11:46:27 +01001041
1042 /* check data tree - when enabled node */
1043 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:f\">666</d><cont xmlns=\"urn:tests:f\"><d>666</d></cont>",
1044 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1045 CHECK_LYD_STRING_PARAM(tree,
1046 "<ll1 xmlns=\"urn:tests:f\">def1</ll1>\n"
1047 "<ll1 xmlns=\"urn:tests:f\">def2</ll1>\n"
1048 "<ll1 xmlns=\"urn:tests:f\">def3</ll1>\n"
1049 "<d xmlns=\"urn:tests:f\">666</d>\n"
1050 "<dd xmlns=\"urn:tests:f\">15</dd>\n"
1051 "<ll2 xmlns=\"urn:tests:f\">dflt1</ll2>\n"
1052 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
1053 "<cont xmlns=\"urn:tests:f\">\n"
1054 " <ll1>def1</ll1>\n"
1055 " <ll1>def2</ll1>\n"
1056 " <ll1>def3</ll1>\n"
1057 " <d>666</d>\n"
1058 " <dd>15</dd>\n"
1059 " <ll2>dflt1</ll2>\n"
1060 " <ll2>dflt2</ll2>\n"
1061 "</cont>\n",
1062 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
1063 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +02001064}
1065
1066static void
1067test_state(void **state)
1068{
1069 const char *data;
1070 struct lyd_node *tree;
1071 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001072 "module h {\n"
1073 " namespace urn:tests:h;\n"
1074 " prefix h;\n"
1075 " yang-version 1.1;\n"
1076 "\n"
1077 " container cont {\n"
1078 " container cont2 {\n"
1079 " config false;\n"
1080 " leaf l {\n"
1081 " type string;\n"
1082 " }\n"
1083 " }\n"
1084 " }\n"
1085 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +02001086
1087 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1088
1089 data = "<cont xmlns=\"urn:tests:h\">\n"
1090 " <cont2>\n"
1091 " <l>val</l>\n"
1092 " </cont2>\n"
1093 "</cont>\n";
1094 CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001095 CHECK_LOG_CTX("Unexpected data state node \"cont2\" found.", "/h:cont/cont2", 3);
Radek Iša56ca9e42020-09-08 18:42:00 +02001096
1097 CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
1098 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +01001099 CHECK_LOG_CTX("Unexpected data state node \"cont2\" found.", "/h:cont/cont2", 0);
Radek Iša56ca9e42020-09-08 18:42:00 +02001100 lyd_free_all(tree);
1101}
1102
1103static void
1104test_must(void **state)
1105{
1106 struct lyd_node *tree;
1107 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001108 "module i {\n"
1109 " namespace urn:tests:i;\n"
1110 " prefix i;\n"
1111 " yang-version 1.1;\n"
1112 "\n"
1113 " container cont {\n"
1114 " leaf l {\n"
1115 " type string;\n"
1116 " }\n"
1117 " leaf l2 {\n"
1118 " must \"../l = 'right'\";\n"
1119 " type string;\n"
1120 " }\n"
Michal Vaskoe9391c72021-10-05 10:04:56 +02001121 " leaf l3 {\n"
1122 " must \"../l = 'left'\" {\n"
1123 " error-app-tag \"not-left\";\n"
1124 " error-message \"l leaf is not left\";\n"
1125 " }\n"
1126 " type string;\n"
1127 " }\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001128 " }\n"
1129 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +01001130
Radek Iša56ca9e42020-09-08 18:42:00 +02001131 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001132
Radek Iša56ca9e42020-09-08 18:42:00 +02001133 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:i\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001134 " <l>wrong</l>\n"
1135 " <l2>val</l2>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001136 "</cont>\n", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001137 CHECK_LOG_CTX_APPTAG("Must condition \"../l = 'right'\" not satisfied.", "/i:cont/l2", 0, "must-violation");
Michal Vaskocc048b22020-03-27 15:52:38 +01001138
Radek Iša56ca9e42020-09-08 18:42:00 +02001139 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:i\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001140 " <l>right</l>\n"
1141 " <l2>val</l2>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001142 "</cont>\n", tree);
1143 lyd_free_all(tree);
Michal Vaskoe9391c72021-10-05 10:04:56 +02001144
1145 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:i\">\n"
1146 " <l>wrong</l>\n"
1147 " <l3>val</l3>\n"
1148 "</cont>\n", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001149 CHECK_LOG_CTX_APPTAG("l leaf is not left", "/i:cont/l3", 0, "not-left");
Michal Vaskocc048b22020-03-27 15:52:38 +01001150}
1151
Michal Vaskod027f382023-02-10 09:13:25 +01001152static void
1153test_multi_error(void **state)
1154{
1155 struct lyd_node *tree;
1156 const char *schema =
1157 "module ii {\n"
1158 " namespace urn:tests:ii;\n"
1159 " prefix ii;\n"
1160 " yang-version 1.1;\n"
1161 "\n"
1162 " container cont {\n"
1163 " leaf l {\n"
1164 " type string;\n"
1165 " }\n"
1166 " leaf l2 {\n"
1167 " must \"../l = 'right'\";\n"
1168 " type string;\n"
1169 " }\n"
1170 " leaf l3 {\n"
1171 " must \"../l = 'left'\" {\n"
1172 " error-app-tag \"not-left\";\n"
1173 " error-message \"l leaf is not left\";\n"
1174 " }\n"
1175 " type string;\n"
1176 " }\n"
1177 " leaf-list ll {\n"
1178 " type uint32;\n"
1179 " min-elements 2;\n"
1180 " }\n"
1181 " }\n"
1182 "}";
1183 const char *data;
1184
1185 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1186
1187 /* xml */
1188 data =
1189 "<cont xmlns=\"urn:tests:ii\">\n"
1190 " <l>wrong</l>\n"
1191 " <l>wrong2</l>\n"
1192 " <l2>val</l2>\n"
1193 " <l3>val</l3>\n"
1194 " <ll>ahoy</ll>\n"
1195 "</cont>\n";
1196 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_MULTI_ERROR, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001197 CHECK_LOG_CTX_APPTAG("Too few \"ll\" instances.", "/ii:cont/ll", 0, "too-few-elements");
1198 CHECK_LOG_CTX_APPTAG("l leaf is not left", "/ii:cont/l3", 0, "not-left");
1199 CHECK_LOG_CTX_APPTAG("Must condition \"../l = 'right'\" not satisfied.", "/ii:cont/l2", 0, "must-violation");
1200 CHECK_LOG_CTX_APPTAG("Invalid type uint32 value \"ahoy\".", "/ii:cont/ll", 6, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001201
1202 /* json */
1203 data = "{\n"
1204 " \"ii:cont\": {\n"
1205 " \"l\": \"wrong\",\n"
1206 " \"l\": \"wrong2\",\n"
1207 " \"l2\": \"val\",\n"
1208 " \"l3\": \"val\",\n"
1209 " \"ll\": [\"ahoy\"]\n"
1210 " }\n"
1211 "}\n";
1212 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_MULTI_ERROR, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001213 CHECK_LOG_CTX_APPTAG("Too few \"ll\" instances.", "/ii:cont/ll", 0, "too-few-elements");
1214 CHECK_LOG_CTX_APPTAG("l leaf is not left", "/ii:cont/l3", 0, "not-left");
1215 CHECK_LOG_CTX_APPTAG("Must condition \"../l = 'right'\" not satisfied.", "/ii:cont/l2", 0, "must-violation");
1216 CHECK_LOG_CTX_APPTAG("Invalid non-number-encoded uint32 value \"ahoy\".", "/ii:cont/ll", 7, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001217
1218 /* validation */
1219 data = "{\n"
1220 " \"ii:cont\": {\n"
1221 " \"l\": \"wrong\",\n"
1222 " \"l\": \"wrong2\",\n"
1223 " \"l2\": \"val\",\n"
1224 " \"l3\": \"val\",\n"
1225 " \"ll\": [25]\n"
1226 " }\n"
1227 "}\n";
1228 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
1229 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_MULTI_ERROR, NULL));
1230 lyd_free_tree(tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001231 CHECK_LOG_CTX_APPTAG("Too few \"ll\" instances.", "/ii:cont/ll", 0, "too-few-elements");
1232 CHECK_LOG_CTX_APPTAG("l leaf is not left", "/ii:cont/l3", 0, "not-left");
1233 CHECK_LOG_CTX_APPTAG("Must condition \"../l = 'right'\" not satisfied.", "/ii:cont/l2", 0, "must-violation");
1234 CHECK_LOG_CTX_APPTAG("Duplicate instance of \"l\".", "/ii:cont/l", 0, NULL);
1235 CHECK_LOG_CTX_APPTAG("Duplicate instance of \"l\".", "/ii:cont/l", 0, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001236}
1237
Radek Iša56ca9e42020-09-08 18:42:00 +02001238const char *schema_j =
1239 "module j {\n"
1240 " namespace urn:tests:j;\n"
1241 " prefix j;\n"
1242 " yang-version 1.1;\n"
1243 "\n"
1244 " feature feat1;\n"
1245 "\n"
1246 " container cont {\n"
1247 " must \"false()\";\n"
1248 " list l1 {\n"
1249 " key \"k\";\n"
1250 " leaf k {\n"
1251 " type string;\n"
1252 " }\n"
1253 " action act {\n"
1254 " if-feature feat1;\n"
1255 " input {\n"
1256 " must \"../../lf1 = 'true'\";\n"
1257 " leaf lf2 {\n"
1258 " type leafref {\n"
1259 " path /lf3;\n"
1260 " }\n"
1261 " }\n"
1262 " }\n"
1263 " output {\n"
1264 " must \"../../lf1 = 'true2'\";\n"
1265 " leaf lf2 {\n"
1266 " type leafref {\n"
1267 " path /lf4;\n"
1268 " }\n"
1269 " }\n"
1270 " }\n"
1271 " }\n"
1272 " }\n"
1273 "\n"
1274 " leaf lf1 {\n"
1275 " type string;\n"
1276 " }\n"
1277 " }\n"
1278 "\n"
1279 " leaf lf3 {\n"
1280 " type string;\n"
1281 " }\n"
1282 "\n"
1283 " leaf lf4 {\n"
1284 " type string;\n"
1285 " }\n"
1286 "}";
1287const char *feats_j[] = {"feat1", NULL};
1288
Michal Vaskofea12c62020-03-30 11:00:15 +02001289static void
1290test_action(void **state)
1291{
Michal Vasko63f3d842020-07-08 10:10:14 +02001292 struct ly_in *in;
Michal Vaskofea12c62020-03-30 11:00:15 +02001293 struct lyd_node *tree, *op_tree;
Michal Vaskofea12c62020-03-30 11:00:15 +02001294
Radek Iša56ca9e42020-09-08 18:42:00 +02001295 UTEST_ADD_MODULE(schema_j, LYS_IN_YANG, feats_j, NULL);
Michal Vasko62af3692023-02-09 14:00:09 +01001296 UTEST_LOG_CTX_CLEAN;
Radek Iša56ca9e42020-09-08 18:42:00 +02001297
1298 assert_int_equal(LY_SUCCESS, ly_in_new_memory(
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001299 "<cont xmlns=\"urn:tests:j\">\n"
1300 " <l1>\n"
1301 " <k>val1</k>\n"
1302 " <act>\n"
1303 " <lf2>target</lf2>\n"
1304 " </act>\n"
1305 " </l1>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001306 "</cont>\n", &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001307 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &op_tree, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001308 assert_non_null(op_tree);
1309
1310 /* missing leafref */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001311 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_TYPE_RPC_YANG, NULL));
Michal Vasko5edfefd2022-08-05 10:07:11 +02001312 CHECK_LOG_CTX("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value.",
Michal Vasko7a266772024-01-23 11:02:38 +01001313 "/j:cont/l1[k='val1']/act/lf2", 0);
Michal Vasko63f3d842020-07-08 10:10:14 +02001314 ly_in_free(in, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001315
Radek Iša56ca9e42020-09-08 18:42:00 +02001316 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001317 " <lf1>not true</lf1>\n"
1318 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001319 "<lf3 xmlns=\"urn:tests:j\">target</lf3>\n",
1320 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001321
Michal Vaskofea12c62020-03-30 11:00:15 +02001322 /* input must false */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001323 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_TYPE_RPC_YANG, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +01001324 CHECK_LOG_CTX("Must condition \"../../lf1 = 'true'\" not satisfied.", "/j:cont/l1[k='val1']/act", 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001325
Radek Iša56ca9e42020-09-08 18:42:00 +02001326 lyd_free_all(tree);
1327 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001328 " <lf1>true</lf1>\n"
1329 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001330 "<lf3 xmlns=\"urn:tests:j\">target</lf3>\n",
1331 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001332
1333 /* success */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001334 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_TYPE_RPC_YANG, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001335
Michal Vaskocb7526d2020-03-30 15:08:26 +02001336 lyd_free_tree(op_tree);
1337 lyd_free_siblings(tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001338}
1339
1340static void
aPiecek18a844e2021-08-10 11:06:24 +02001341test_rpc(void **state)
1342{
1343 const char *schema, *data;
1344 struct ly_in *in;
1345 struct lyd_node *tree;
1346
1347 /* Testing constraint violation in RPC. */
1348 schema =
1349 "module val-str {\n"
1350 " namespace \"urn:vstr\";\n"
1351 " prefix v;\n"
1352 "\n"
1353 " rpc modify-user-password {\n"
1354 " input {\n"
1355 " leaf old-password {\n"
1356 " type string {\n"
1357 " length \"4..8\";\n"
1358 " }\n"
1359 " }\n"
1360 " leaf new-password {\n"
1361 " type string {\n"
1362 " length \"4..8\";\n"
1363 " }\n"
1364 " }\n"
1365 " }\n"
1366 " }\n"
1367 "}\n";
1368 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1369 data =
1370 "<modify-user-password xmlns=\"urn:vstr\">\n"
1371 " <old-password>12345</old-password>\n"
1372 " <new-password>123</new-password>\n"
1373 "</modify-user-password>";
1374 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko140ede92022-05-10 09:27:30 +02001375 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
Michal Vaskodbf3e652022-10-21 08:46:25 +02001376 CHECK_LOG_CTX("Unsatisfied length - string \"123\" length is not allowed.",
Michal Vasko7a266772024-01-23 11:02:38 +01001377 "/val-str:modify-user-password/new-password", 3);
aPiecek18a844e2021-08-10 11:06:24 +02001378 ly_in_free(in, 0);
aPiecek18a844e2021-08-10 11:06:24 +02001379}
1380
1381static void
Michal Vaskocb7526d2020-03-30 15:08:26 +02001382test_reply(void **state)
1383{
Michal Vasko63f3d842020-07-08 10:10:14 +02001384 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +01001385 struct lyd_node *tree, *op_tree;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001386
Radek Iša56ca9e42020-09-08 18:42:00 +02001387 UTEST_ADD_MODULE(schema_j, LYS_IN_YANG, feats_j, NULL);
Michal Vasko62af3692023-02-09 14:00:09 +01001388 UTEST_LOG_CTX_CLEAN;
Radek Iša56ca9e42020-09-08 18:42:00 +02001389
1390 assert_int_equal(LY_SUCCESS, ly_in_new_memory(
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001391 "<cont xmlns=\"urn:tests:j\">\n"
1392 " <l1>\n"
1393 " <k>val1</k>\n"
1394 " <act>\n"
1395 " <lf2>target</lf2>\n"
1396 " </act>\n"
1397 " </l1>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001398 "</cont>\n", &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001399 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_REPLY_YANG, &op_tree, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001400 assert_non_null(op_tree);
Michal Vasko63f3d842020-07-08 10:10:14 +02001401 ly_in_free(in, 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001402
1403 /* missing leafref */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001404 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_TYPE_REPLY_YANG, NULL));
Michal Vasko5edfefd2022-08-05 10:07:11 +02001405 CHECK_LOG_CTX("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value.",
Michal Vasko7a266772024-01-23 11:02:38 +01001406 "/j:cont/l1[k='val1']/act/lf2", 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001407
Radek Iša56ca9e42020-09-08 18:42:00 +02001408 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001409 " <lf1>not true</lf1>\n"
1410 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001411 "<lf4 xmlns=\"urn:tests:j\">target</lf4>\n",
1412 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001413
Michal Vaskocb7526d2020-03-30 15:08:26 +02001414 /* input must false */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001415 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_TYPE_REPLY_YANG, NULL));
Michal Vasko7a266772024-01-23 11:02:38 +01001416 CHECK_LOG_CTX("Must condition \"../../lf1 = 'true2'\" not satisfied.", "/j:cont/l1[k='val1']/act", 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001417
Radek Iša56ca9e42020-09-08 18:42:00 +02001418 lyd_free_all(tree);
1419 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001420 " <lf1>true2</lf1>\n"
1421 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001422 "<lf4 xmlns=\"urn:tests:j\">target</lf4>\n",
1423 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001424
1425 /* success */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001426 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_TYPE_REPLY_YANG, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001427
Michal Vaskofea12c62020-03-30 11:00:15 +02001428 lyd_free_tree(op_tree);
Radek Iša56ca9e42020-09-08 18:42:00 +02001429 lyd_free_all(tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001430}
1431
Michal Vasko8d151222021-07-22 12:12:41 +02001432static void
1433test_case(void **state)
1434{
1435 struct lyd_node *tree;
1436 const char *schema =
1437 "module k {\n"
1438 " namespace urn:tests:k;\n"
1439 " prefix k;\n"
1440 " yang-version 1.1;\n"
1441 "\n"
1442 " container ch {\n"
1443 " choice a0 {\n"
1444 " case v0 {\n"
1445 " leaf g0 {\n"
1446 " type string;\n"
1447 " }\n"
1448 " }\n"
1449 " case v1 {\n"
1450 " choice a1 {\n"
1451 " case r0 {\n"
1452 " leaf g1 {\n"
1453 " type string;\n"
1454 " }\n"
1455 " }\n"
1456 " case r1 {\n"
1457 " leaf g2 {\n"
1458 " type string;\n"
1459 " }\n"
1460 " leaf g3 {\n"
1461 " type string;\n"
1462 " }\n"
1463 " }\n"
1464 " case r2 {\n"
1465 " leaf g4 {\n"
1466 " type string;\n"
1467 " }\n"
1468 " }\n"
1469 " }\n"
1470 " }\n"
1471 " case v2 {\n"
1472 " choice a2 {\n"
1473 " case y0 {\n"
1474 " leaf g5 {\n"
1475 " type string;\n"
1476 " }\n"
1477 " }\n"
1478 " case y1 {\n"
1479 " leaf g6 {\n"
1480 " type string;\n"
1481 " }\n"
1482 " leaf g7 {\n"
1483 " type string;\n"
1484 " }\n"
1485 " }\n"
1486 " case y2 {\n"
1487 " leaf g8 {\n"
1488 " type string;\n"
1489 " }\n"
1490 " }\n"
1491 " }\n"
1492 " }\n"
1493 " }\n"
1494 " }\n"
1495 "}";
1496
1497 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1498
1499 CHECK_PARSE_LYD_PARAM(
1500 "{\n"
1501 " \"k:ch\": {\n"
1502 " \"g0\": \"value_g0\",\n"
1503 " \"g7\": \"value_g7\"\n"
1504 " }\n"
1505 "}\n", LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001506 CHECK_LOG_CTX("Data for both cases \"v0\" and \"v2\" exist.", "/k:ch", 6);
Michal Vasko8d151222021-07-22 12:12:41 +02001507
1508 CHECK_PARSE_LYD_PARAM(
1509 "{\n"
1510 " \"k:ch\": {\n"
1511 " \"g7\": \"value_g7\",\n"
1512 " \"g0\": \"value_g0\"\n"
1513 " }\n"
1514 "}\n", LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko7a266772024-01-23 11:02:38 +01001515 CHECK_LOG_CTX("Data for both cases \"v0\" and \"v2\" exist.", "/k:ch", 6);
Michal Vasko8d151222021-07-22 12:12:41 +02001516}
1517
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001518int
1519main(void)
Michal Vaskocde73ac2019-11-14 16:10:27 +01001520{
1521 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001522 UTEST(test_when),
1523 UTEST(test_mandatory),
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05001524 UTEST(test_mandatory_when),
Michal Vasko850d8cb2022-08-05 12:51:00 +02001525 UTEST(test_type_incomplete_when),
Radek Iša56ca9e42020-09-08 18:42:00 +02001526 UTEST(test_minmax),
1527 UTEST(test_unique),
1528 UTEST(test_unique_nested),
1529 UTEST(test_dup),
1530 UTEST(test_defaults),
1531 UTEST(test_state),
1532 UTEST(test_must),
Michal Vaskod027f382023-02-10 09:13:25 +01001533 UTEST(test_multi_error),
Radek Iša56ca9e42020-09-08 18:42:00 +02001534 UTEST(test_action),
aPiecek18a844e2021-08-10 11:06:24 +02001535 UTEST(test_rpc),
Radek Iša56ca9e42020-09-08 18:42:00 +02001536 UTEST(test_reply),
Michal Vasko8d151222021-07-22 12:12:41 +02001537 UTEST(test_case),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001538 };
1539
Radek Iša56ca9e42020-09-08 18:42:00 +02001540 return cmocka_run_group_tests(tests, NULL, NULL);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001541}