blob: db5802247daeebacf2ffc6b076d49eae706ffc41 [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);
Radek Krejci2efc45b2020-12-22 16:25:44 +010060 CHECK_LOG_CTX("When condition \"/cont/b = 'val_b'\" not satisfied.", "Schema location /a:c, data location /a:c.");
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);
108 CHECK_LOG_CTX("When condition \"../c = 'val_c'\" not satisfied.", "Schema location /a:d, data location /a:d.");
109
110 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:a\"><b>hey</b></cont>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
111 CHECK_LOG_CTX("When condition \"../a = 'val_a'\" not satisfied.", "Schema location /a:cont/b, data location /a:cont/b.");
112
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
Radek Iša56ca9e42020-09-08 18:42:00 +0200125test_mandatory(void **state)
126{
127 struct lyd_node *tree;
128 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100129 "module b {\n"
130 " namespace urn:tests:b;\n"
131 " prefix b;\n"
132 " yang-version 1.1;\n"
133 "\n"
134 " choice choic {\n"
135 " mandatory true;\n"
136 " leaf a {\n"
137 " type string;\n"
138 " }\n"
139 " case b {\n"
140 " leaf l {\n"
141 " type string;\n"
142 " }\n"
143 " }\n"
144 " }\n"
145 " leaf c {\n"
146 " mandatory true;\n"
147 " type string;\n"
148 " }\n"
149 " leaf d {\n"
150 " type empty;\n"
151 " }\n"
152 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200153
154 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
155
156 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:b\"/>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Michal Vasko538b8952021-02-17 11:27:26 +0100157 CHECK_LOG_CTX("Mandatory choice \"choic\" data do not exist.", "Schema location /b:choic.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200158
159 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);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100160 CHECK_LOG_CTX("Mandatory node \"c\" instance does not exist.", "Schema location /b:c.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200161
162 CHECK_PARSE_LYD_PARAM("<a xmlns=\"urn:tests:b\">string</a>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100163 CHECK_LOG_CTX("Mandatory node \"c\" instance does not exist.", "Schema location /b:c.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200164
165 LYD_TREE_CREATE("<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>", tree);
166 lyd_free_siblings(tree);
167}
168
169static void
170test_minmax(void **state)
171{
172 struct lyd_node *tree;
173 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100174 "module c {\n"
175 " namespace urn:tests:c;\n"
176 " prefix c;\n"
177 " yang-version 1.1;\n"
178 "\n"
179 " choice choic {\n"
180 " leaf a {\n"
181 " type string;\n"
182 " }\n"
183 " case b {\n"
184 " leaf-list l {\n"
185 " min-elements 3;\n"
186 " type string;\n"
187 " }\n"
188 " }\n"
189 " }\n"
190 " list lt {\n"
191 " max-elements 4;\n"
192 " key \"k\";\n"
193 " leaf k {\n"
194 " type string;\n"
195 " }\n"
196 " }\n"
197 " leaf d {\n"
198 " type empty;\n"
199 " }\n"
200 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200201
202 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
203
Michal Vasko6c16cda2021-02-04 11:05:52 +0100204 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">mate</l>"
205 "<d xmlns=\"urn:tests:c\"/>",
206 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100207 CHECK_LOG_CTX("Too few \"l\" instances.", "Schema location /c:choic/b/l.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200208
209 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">val1</l>"
210 "<l xmlns=\"urn:tests:c\">val2</l>",
211 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100212 CHECK_LOG_CTX("Too few \"l\" instances.", "Schema location /c:choic/b/l.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200213
214 LYD_TREE_CREATE("<l xmlns=\"urn:tests:c\">val1</l>"
215 "<l xmlns=\"urn:tests:c\">val2</l>"
216 "<l xmlns=\"urn:tests:c\">val3</l>", tree);
217 lyd_free_all(tree);
218
219 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:c\">val1</l>"
220 "<l xmlns=\"urn:tests:c\">val2</l>"
221 "<l xmlns=\"urn:tests:c\">val3</l>"
222 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
223 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
224 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
225 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
Radek Krejci2efc45b2020-12-22 16:25:44 +0100226 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>"
227 "<lt xmlns=\"urn:tests:c\"><k>val6</k></lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
228 CHECK_LOG_CTX("Too many \"lt\" instances.", "Schema location /c:lt, data location /c:lt[k='val5'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200229}
230
231const char *schema_d =
232 "module d {\n"
233 " namespace urn:tests:d;\n"
234 " prefix d;\n"
235 " yang-version 1.1;\n"
236 "\n"
237 " list lt {\n"
238 " key \"k\";\n"
239 " unique \"l1\";\n"
240 " leaf k {\n"
241 " type string;\n"
242 " }\n"
243 " leaf l1 {\n"
244 " type string;\n"
245 " }\n"
246 " }\n"
247 " list lt2 {\n"
248 " key \"k\";\n"
249 " unique \"cont/l2 l4\";\n"
250 " unique \"l5 l6\";\n"
251 " leaf k {\n"
252 " type string;\n"
253 " }\n"
254 " container cont {\n"
255 " leaf l2 {\n"
256 " type string;\n"
257 " }\n"
258 " }\n"
259 " leaf l4 {\n"
260 " type string;\n"
261 " }\n"
262 " leaf l5 {\n"
263 " type string;\n"
264 " }\n"
265 " leaf l6 {\n"
266 " type string;\n"
267 " }\n"
268 " list lt3 {\n"
269 " key \"kk\";\n"
270 " unique \"l3\";\n"
271 " leaf kk {\n"
272 " type string;\n"
273 " }\n"
274 " leaf l3 {\n"
275 " type string;\n"
276 " }\n"
277 " }\n"
278 " }\n"
279 "}";
280
281static void
282test_unique(void **state)
283{
284 struct lyd_node *tree;
285
286 UTEST_ADD_MODULE(schema_d, LYS_IN_YANG, NULL, NULL);
287
288 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
289 " <k>val1</k>\n"
290 " <l1>same</l1>\n"
291 "</lt>\n"
292 "<lt xmlns=\"urn:tests:d\">\n"
293 " <k>val2</k>\n"
294 "</lt>", tree);
295 lyd_free_all(tree);
296
297 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
298 " <k>val1</k>\n"
299 " <l1>same</l1>\n"
300 "</lt>\n"
301 "<lt xmlns=\"urn:tests:d\">\n"
302 " <k>val2</k>\n"
303 " <l1>not-same</l1>\n"
304 "</lt>", tree);
305 lyd_free_all(tree);
306
307 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:d\">\n"
308 " <k>val1</k>\n"
309 " <l1>same</l1>\n"
310 "</lt>\n"
311 "<lt xmlns=\"urn:tests:d\">\n"
312 " <k>val2</k>\n"
313 " <l1>same</l1>\n"
314 "</lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100315 CHECK_LOG_CTX("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val1']\" and \"/d:lt[k='val2']\".",
316 "Schema location /d:lt, data location /d:lt[k='val2'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200317
318 /* now try with more instances */
319 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
320 " <k>val1</k>\n"
321 " <l1>1</l1>\n"
322 "</lt>\n"
323 "<lt xmlns=\"urn:tests:d\">\n"
324 " <k>val2</k>\n"
325 " <l1>2</l1>\n"
326 "</lt>\n"
327 "<lt xmlns=\"urn:tests:d\">\n"
328 " <k>val3</k>\n"
329 " <l1>3</l1>\n"
330 "</lt>\n"
331 "<lt xmlns=\"urn:tests:d\">\n"
332 " <k>val4</k>\n"
333 " <l1>4</l1>\n"
334 "</lt>\n"
335 "<lt xmlns=\"urn:tests:d\">\n"
336 " <k>val5</k>\n"
337 " <l1>5</l1>\n"
338 "</lt>\n"
339 "<lt xmlns=\"urn:tests:d\">\n"
340 " <k>val6</k>\n"
341 " <l1>6</l1>\n"
342 "</lt>\n"
343 "<lt xmlns=\"urn:tests:d\">\n"
344 " <k>val7</k>\n"
345 " <l1>7</l1>\n"
346 "</lt>\n"
347 "<lt xmlns=\"urn:tests:d\">\n"
348 " <k>val8</k>\n"
349 " <l1>8</l1>\n"
350 "</lt>", tree);
351 lyd_free_all(tree);
352
353 LYD_TREE_CREATE("<lt xmlns=\"urn:tests:d\">\n"
354 " <k>val1</k>\n"
355 " <l1>1</l1>\n"
356 "</lt>\n"
357 "<lt xmlns=\"urn:tests:d\">\n"
358 " <k>val2</k>\n"
359 " <l1>2</l1>\n"
360 "</lt>\n"
361 "<lt xmlns=\"urn:tests:d\">\n"
362 " <k>val3</k>\n"
363 " <l1>3</l1>\n"
364 "</lt>\n"
365 "<lt xmlns=\"urn:tests:d\">\n"
366 " <k>val4</k>\n"
367 "</lt>\n"
368 "<lt xmlns=\"urn:tests:d\">\n"
369 " <k>val5</k>\n"
370 " <l1>5</l1>\n"
371 "</lt>\n"
372 "<lt xmlns=\"urn:tests:d\">\n"
373 " <k>val6</k>\n"
374 " <l1>6</l1>\n"
375 "</lt>\n"
376 "<lt xmlns=\"urn:tests:d\">\n"
377 " <k>val7</k>\n"
378 "</lt>\n"
379 "<lt xmlns=\"urn:tests:d\">\n"
380 " <k>val8</k>\n"
381 "</lt>", tree);
382 lyd_free_all(tree);
383
384 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:d\">\n"
385 " <k>val1</k>\n"
386 " <l1>1</l1>\n"
387 "</lt>\n"
388 "<lt xmlns=\"urn:tests:d\">\n"
389 " <k>val2</k>\n"
390 " <l1>2</l1>\n"
391 "</lt>\n"
392 "<lt xmlns=\"urn:tests:d\">\n"
393 " <k>val3</k>\n"
394 "</lt>\n"
395 "<lt xmlns=\"urn:tests:d\">\n"
396 " <k>val4</k>\n"
397 " <l1>4</l1>\n"
398 "</lt>\n"
399 "<lt xmlns=\"urn:tests:d\">\n"
400 " <k>val5</k>\n"
401 "</lt>\n"
402 "<lt xmlns=\"urn:tests:d\">\n"
403 " <k>val6</k>\n"
404 "</lt>\n"
405 "<lt xmlns=\"urn:tests:d\">\n"
406 " <k>val7</k>\n"
407 " <l1>2</l1>\n"
408 "</lt>\n"
409 "<lt xmlns=\"urn:tests:d\">\n"
410 " <k>val8</k>\n"
411 " <l1>8</l1>\n"
412 "</lt>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100413 CHECK_LOG_CTX("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val7']\" and \"/d:lt[k='val2']\".",
414 "Schema location /d:lt, data location /d:lt[k='val2'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200415}
416
417static void
418test_unique_nested(void **state)
419{
420 struct lyd_node *tree;
421
422 UTEST_ADD_MODULE(schema_d, LYS_IN_YANG, NULL, NULL);
423
424 /* nested list uniquest are compared only with instances in the same parent list instance */
425 LYD_TREE_CREATE("<lt2 xmlns=\"urn:tests:d\">\n"
426 " <k>val1</k>\n"
427 " <cont>\n"
428 " <l2>1</l2>\n"
429 " </cont>\n"
430 " <l4>1</l4>\n"
431 "</lt2>\n"
432 "<lt2 xmlns=\"urn:tests:d\">\n"
433 " <k>val2</k>\n"
434 " <cont>\n"
435 " <l2>2</l2>\n"
436 " </cont>\n"
437 " <l4>2</l4>\n"
438 " <lt3>\n"
439 " <kk>val1</kk>\n"
440 " <l3>1</l3>\n"
441 " </lt3>\n"
442 " <lt3>\n"
443 " <kk>val2</kk>\n"
444 " <l3>2</l3>\n"
445 " </lt3>\n"
446 "</lt2>\n"
447 "<lt2 xmlns=\"urn:tests:d\">\n"
448 " <k>val3</k>\n"
449 " <cont>\n"
450 " <l2>3</l2>\n"
451 " </cont>\n"
452 " <l4>3</l4>\n"
453 " <lt3>\n"
454 " <kk>val1</kk>\n"
455 " <l3>2</l3>\n"
456 " </lt3>\n"
457 "</lt2>\n"
458 "<lt2 xmlns=\"urn:tests:d\">\n"
459 " <k>val4</k>\n"
460 " <cont>\n"
461 " <l2>4</l2>\n"
462 " </cont>\n"
463 " <l4>4</l4>\n"
464 " <lt3>\n"
465 " <kk>val1</kk>\n"
466 " <l3>3</l3>\n"
467 " </lt3>\n"
468 "</lt2>\n"
469 "<lt2 xmlns=\"urn:tests:d\">\n"
470 " <k>val5</k>\n"
471 " <cont>\n"
472 " <l2>5</l2>\n"
473 " </cont>\n"
474 " <l4>5</l4>\n"
475 " <lt3>\n"
476 " <kk>val1</kk>\n"
477 " <l3>3</l3>\n"
478 " </lt3>\n"
479 "</lt2>", tree);
480 lyd_free_all(tree);
481
482 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
483 " <k>val1</k>\n"
484 " <cont>\n"
485 " <l2>1</l2>\n"
486 " </cont>\n"
487 " <l4>1</l4>\n"
488 "</lt2>\n"
489 "<lt2 xmlns=\"urn:tests:d\">\n"
490 " <k>val2</k>\n"
491 " <cont>\n"
492 " <l2>2</l2>\n"
493 " </cont>\n"
494 " <lt3>\n"
495 " <kk>val1</kk>\n"
496 " <l3>1</l3>\n"
497 " </lt3>\n"
498 " <lt3>\n"
499 " <kk>val2</kk>\n"
500 " <l3>2</l3>\n"
501 " </lt3>\n"
502 " <lt3>\n"
503 " <kk>val3</kk>\n"
504 " <l3>1</l3>\n"
505 " </lt3>\n"
506 "</lt2>\n"
507 "<lt2 xmlns=\"urn:tests:d\">\n"
508 " <k>val3</k>\n"
509 " <cont>\n"
510 " <l2>3</l2>\n"
511 " </cont>\n"
512 " <l4>1</l4>\n"
513 " <lt3>\n"
514 " <kk>val1</kk>\n"
515 " <l3>2</l3>\n"
516 " </lt3>\n"
517 "</lt2>\n"
518 "<lt2 xmlns=\"urn:tests:d\">\n"
519 " <k>val4</k>\n"
520 " <cont>\n"
521 " <l2>4</l2>\n"
522 " </cont>\n"
523 " <lt3>\n"
524 " <kk>val1</kk>\n"
525 " <l3>3</l3>\n"
526 " </lt3>\n"
527 "</lt2>\n"
528 "<lt2 xmlns=\"urn:tests:d\">\n"
529 " <k>val5</k>\n"
530 " <cont>\n"
531 " <l2>5</l2>\n"
532 " </cont>\n"
533 " <lt3>\n"
534 " <kk>val1</kk>\n"
535 " <l3>3</l3>\n"
536 " </lt3>\n"
537 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100538 CHECK_LOG_CTX("Unique data leaf(s) \"l3\" not satisfied in \"/d:lt2[k='val2']/lt3[kk='val3']\" and \"/d:lt2[k='val2']/lt3[kk='val1']\".",
539 "Schema location /d:lt2/lt3, data location /d:lt2[k='val2']/lt3[kk='val1'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200540
541 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
542 " <k>val1</k>\n"
543 " <cont>\n"
544 " <l2>1</l2>\n"
545 " </cont>\n"
546 " <l4>1</l4>\n"
547 "</lt2>\n"
548 "<lt2 xmlns=\"urn:tests:d\">\n"
549 " <k>val2</k>\n"
550 " <cont>\n"
551 " <l2>2</l2>\n"
552 " </cont>\n"
553 " <l4>2</l4>\n"
554 "</lt2>\n"
555 "<lt2 xmlns=\"urn:tests:d\">\n"
556 " <k>val3</k>\n"
557 " <cont>\n"
558 " <l2>3</l2>\n"
559 " </cont>\n"
560 " <l4>3</l4>\n"
561 "</lt2>\n"
562 "<lt2 xmlns=\"urn:tests:d\">\n"
563 " <k>val4</k>\n"
564 " <cont>\n"
565 " <l2>2</l2>\n"
566 " </cont>\n"
567 " <l4>2</l4>\n"
568 "</lt2>\n"
569 "<lt2 xmlns=\"urn:tests:d\">\n"
570 " <k>val5</k>\n"
571 " <cont>\n"
572 " <l2>5</l2>\n"
573 " </cont>\n"
574 " <l4>5</l4>\n"
575 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100576 CHECK_LOG_CTX("Unique data leaf(s) \"cont/l2 l4\" not satisfied in \"/d:lt2[k='val4']\" and \"/d:lt2[k='val2']\".",
577 "Schema location /d:lt2, data location /d:lt2[k='val2'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200578
579 CHECK_PARSE_LYD_PARAM("<lt2 xmlns=\"urn:tests:d\">\n"
580 " <k>val1</k>\n"
581 " <cont>\n"
582 " <l2>1</l2>\n"
583 " </cont>\n"
584 " <l4>1</l4>\n"
585 " <l5>1</l5>\n"
586 " <l6>1</l6>\n"
587 "</lt2>\n"
588 "<lt2 xmlns=\"urn:tests:d\">\n"
589 " <k>val2</k>\n"
590 " <cont>\n"
591 " <l2>2</l2>\n"
592 " </cont>\n"
593 " <l4>1</l4>\n"
594 " <l5>1</l5>\n"
595 "</lt2>\n"
596 "<lt2 xmlns=\"urn:tests:d\">\n"
597 " <k>val3</k>\n"
598 " <cont>\n"
599 " <l2>3</l2>\n"
600 " </cont>\n"
601 " <l4>1</l4>\n"
602 " <l5>3</l5>\n"
603 " <l6>3</l6>\n"
604 "</lt2>\n"
605 "<lt2 xmlns=\"urn:tests:d\">\n"
606 " <k>val4</k>\n"
607 " <cont>\n"
608 " <l2>4</l2>\n"
609 " </cont>\n"
610 " <l4>1</l4>\n"
611 " <l6>1</l6>\n"
612 "</lt2>\n"
613 "<lt2 xmlns=\"urn:tests:d\">\n"
614 " <k>val5</k>\n"
615 " <cont>\n"
616 " <l2>5</l2>\n"
617 " </cont>\n"
618 " <l4>1</l4>\n"
619 " <l5>3</l5>\n"
620 " <l6>3</l6>\n"
621 "</lt2>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
622 CHECK_LOG_CTX("Unique data leaf(s) \"l5 l6\" not satisfied in \"/d:lt2[k='val5']\" and \"/d:lt2[k='val3']\".",
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 "Schema location /d:lt2, data location /d:lt2[k='val3'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200624}
625
626static void
627test_dup(void **state)
628{
629 struct lyd_node *tree;
630 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100631 "module e {\n"
632 " namespace urn:tests:e;\n"
633 " prefix e;\n"
634 " yang-version 1.1;\n"
635 "\n"
636 " choice choic {\n"
637 " leaf a {\n"
638 " type string;\n"
639 " }\n"
640 " case b {\n"
641 " leaf-list l {\n"
642 " type string;\n"
643 " }\n"
644 " }\n"
645 " }\n"
646 " list lt {\n"
647 " key \"k\";\n"
648 " leaf k {\n"
649 " type string;\n"
650 " }\n"
651 " }\n"
652 " leaf d {\n"
653 " type uint32;\n"
654 " }\n"
655 " leaf-list ll {\n"
656 " type string;\n"
657 " }\n"
658 " container cont {\n"
659 " list lt {\n"
660 " key \"k\";\n"
661 " leaf k {\n"
662 " type string;\n"
663 " }\n"
664 " }\n"
665 " leaf d {\n"
666 " type uint32;\n"
667 " }\n"
668 " leaf-list ll {\n"
669 " type string;\n"
670 " }\n"
671 " leaf-list ll2 {\n"
672 " type enumeration {\n"
673 " enum one;\n"
674 " enum two;\n"
675 " }\n"
676 " }\n"
677 " }\n"
678 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200679
680 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
681
682 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>",
683 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 CHECK_LOG_CTX("Duplicate instance of \"d\".", "Schema location /e:d, data location /e:d.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200685
686 CHECK_PARSE_LYD_PARAM("<lt xmlns=\"urn:tests:e\"><k>A</k></lt>"
687 "<lt xmlns=\"urn:tests:e\"><k>B</k></lt>"
688 "<lt xmlns=\"urn:tests:e\"><k>A</k></lt>",
689 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100690 CHECK_LOG_CTX("Duplicate instance of \"lt\".", "Schema location /e:lt, data location /e:lt[k='A'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200691
692 CHECK_PARSE_LYD_PARAM("<ll xmlns=\"urn:tests:e\">A</ll>"
693 "<ll xmlns=\"urn:tests:e\">B</ll>"
694 "<ll xmlns=\"urn:tests:e\">B</ll>",
695 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696 CHECK_LOG_CTX("Duplicate instance of \"ll\".", "Schema location /e:ll, data location /e:ll[.='B'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200697
698 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>",
699 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100700 CHECK_LOG_CTX("Duplicate instance of \"cont\".", "Schema location /e:cont, data location /e:cont.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200701
702 /* same tests again but using hashes */
703 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>",
704 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100705 CHECK_LOG_CTX("Duplicate instance of \"d\".", "Schema location /e:cont/d, data location /e:cont/d, line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200706
707 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
708 "<lt><k>a</k></lt>"
709 "<lt><k>b</k></lt>"
710 "<lt><k>c</k></lt>"
711 "<lt><k>d</k></lt>"
712 "<lt><k>c</k></lt></cont>",
713 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100714 CHECK_LOG_CTX("Duplicate instance of \"lt\".", "Schema location /e:cont/lt, data location /e:cont/lt[k='c'], line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200715
716 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
717 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>",
718 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100719 CHECK_LOG_CTX("Duplicate instance of \"ll\".", "Schema location /e:cont/ll, data location /e:cont/ll[.='d'], line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200720
721 /* cases */
722 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:e\">a</l>"
723 "<l xmlns=\"urn:tests:e\">b</l>"
724 "<l xmlns=\"urn:tests:e\">c</l>"
725 "<l xmlns=\"urn:tests:e\">b</l>",
726 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100727 CHECK_LOG_CTX("Duplicate instance of \"l\".", "Schema location /e:choic/b/l, data location /e:l[.='b'].");
Radek Iša56ca9e42020-09-08 18:42:00 +0200728
729 CHECK_PARSE_LYD_PARAM("<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l>"
730 "<l xmlns=\"urn:tests:e\">c</l>"
731 "<a xmlns=\"urn:tests:e\">aa</a>",
732 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100733 CHECK_LOG_CTX("Data for both cases \"a\" and \"b\" exist.", "Schema location /e:choic.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200734}
735
736static void
737test_defaults(void **state)
738{
739 struct lyd_node *tree, *node, *diff;
Michal Vasko4de7d072021-07-09 09:13:18 +0200740 struct lys_module *mod;
Radek Iša56ca9e42020-09-08 18:42:00 +0200741 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100742 "module f {\n"
743 " namespace urn:tests:f;\n"
744 " prefix f;\n"
745 " yang-version 1.1;\n"
746 "\n"
747 " choice choic {\n"
748 " default \"c\";\n"
749 " leaf a {\n"
750 " type string;\n"
751 " }\n"
752 " case b {\n"
753 " leaf l {\n"
754 " type string;\n"
755 " }\n"
756 " }\n"
757 " case c {\n"
758 " leaf-list ll1 {\n"
759 " type string;\n"
760 " default \"def1\";\n"
761 " default \"def2\";\n"
762 " default \"def3\";\n"
763 " }\n"
764 " }\n"
765 " }\n"
766 " leaf d {\n"
767 " type uint32;\n"
768 " default 15;\n"
769 " }\n"
Radek Krejci7be7b9f2021-02-24 11:46:27 +0100770 " leaf dd {\n"
771 " type uint32;\n"
772 " when '../d = 666';\n"
773 " default 15;\n"
774 " }\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100775 " leaf-list ll2 {\n"
776 " type string;\n"
777 " default \"dflt1\";\n"
778 " default \"dflt2\";\n"
779 " }\n"
780 " container cont {\n"
781 " choice choic {\n"
782 " default \"c\";\n"
783 " leaf a {\n"
784 " type string;\n"
785 " }\n"
786 " case b {\n"
787 " leaf l {\n"
788 " type string;\n"
789 " }\n"
790 " }\n"
791 " case c {\n"
792 " leaf-list ll1 {\n"
793 " type string;\n"
794 " default \"def1\";\n"
795 " default \"def2\";\n"
796 " default \"def3\";\n"
797 " }\n"
798 " }\n"
799 " }\n"
800 " leaf d {\n"
801 " type uint32;\n"
802 " default 15;\n"
803 " }\n"
Radek Krejci7be7b9f2021-02-24 11:46:27 +0100804 " leaf dd {\n"
805 " type uint32;\n"
806 " when '../d = 666';\n"
807 " default 15;\n"
808 " }\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100809 " leaf-list ll2 {\n"
810 " type string;\n"
811 " default \"dflt1\";\n"
812 " default \"dflt2\";\n"
813 " }\n"
814 " }\n"
815 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200816
817 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
818
819 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
820 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));\
821
822 /* get defaults */
823 tree = NULL;
824 assert_int_equal(lyd_validate_module(&tree, mod, 0, &diff), LY_SUCCESS);
825 assert_non_null(tree);
826 assert_non_null(diff);
827
828 /* check all defaults exist */
829 CHECK_LYD_STRING_PARAM(tree,
830 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
831 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
832 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
833 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
834 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
835 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
836 "<cont xmlns=\"urn:tests:f\">\n"
837 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
838 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
839 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
840 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
841 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
842 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
843 "</cont>\n",
844 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
845
846 /* check diff */
847 CHECK_LYD_STRING_PARAM(diff,
848 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def1</ll1>\n"
849 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def2</ll1>\n"
850 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def3</ll1>\n"
851 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">15</d>\n"
852 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt1</ll2>\n"
853 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt2</ll2>\n"
854 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">\n"
855 " <ll1 yang:operation=\"create\">def1</ll1>\n"
856 " <ll1 yang:operation=\"create\">def2</ll1>\n"
857 " <ll1 yang:operation=\"create\">def3</ll1>\n"
858 " <d yang:operation=\"create\">15</d>\n"
859 " <ll2 yang:operation=\"create\">dflt1</ll2>\n"
860 " <ll2 yang:operation=\"create\">dflt2</ll2>\n"
861 "</cont>\n",
862 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
863 lyd_free_all(diff);
864
865 /* create another explicit case and validate */
866 assert_int_equal(lyd_new_term(NULL, mod, "l", "value", 0, &node), LY_SUCCESS);
867 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
868 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
869
870 /* check data tree */
871 CHECK_LYD_STRING_PARAM(tree,
872 "<l xmlns=\"urn:tests:f\">value</l>\n"
873 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
874 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
875 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
876 "<cont xmlns=\"urn:tests:f\">\n"
877 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
878 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
879 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
880 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
881 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
882 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
883 "</cont>\n",
884 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
885
886 /* check diff */
887 CHECK_LYD_STRING_PARAM(diff,
888 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def1</ll1>\n"
889 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def2</ll1>\n"
890 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def3</ll1>\n",
891 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
892 lyd_free_all(diff);
893
894 /* create explicit leaf-list and leaf and validate */
895 assert_int_equal(lyd_new_term(NULL, mod, "d", "15", 0, &node), LY_SUCCESS);
896 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
897 assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", 0, &node), LY_SUCCESS);
898 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
899 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
900
901 /* check data tree */
902 CHECK_LYD_STRING_PARAM(tree,
903 "<l xmlns=\"urn:tests:f\">value</l>\n"
904 "<d xmlns=\"urn:tests:f\">15</d>\n"
905 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
906 "<cont xmlns=\"urn:tests:f\">\n"
907 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
908 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
909 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
910 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
911 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
912 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
913 "</cont>\n",
914 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
915
916 /* check diff */
917 CHECK_LYD_STRING_PARAM(diff,
918 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">15</d>\n"
919 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt1</ll2>\n"
920 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt2</ll2>\n",
921 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
922 lyd_free_all(diff);
923
924 /* create first explicit container, which should become implicit */
925 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
926 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
927 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
928
929 /* check data tree */
930 CHECK_LYD_STRING_PARAM(tree,
931 "<l xmlns=\"urn:tests:f\">value</l>\n"
932 "<d xmlns=\"urn:tests:f\">15</d>\n"
933 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
934 "<cont xmlns=\"urn:tests:f\">\n"
935 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
936 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
937 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
938 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
939 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
940 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
941 "</cont>\n",
942 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
943 /* check diff */
944 assert_null(diff);
945
946 /* create second explicit container, which should become implicit, so the first tree node should be removed */
947 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
948 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
949 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
950
951 /* check data tree */
952 CHECK_LYD_STRING_PARAM(tree,
953 "<l xmlns=\"urn:tests:f\">value</l>\n"
954 "<d xmlns=\"urn:tests:f\">15</d>\n"
955 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
956 "<cont xmlns=\"urn:tests:f\">\n"
957 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>\n"
958 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>\n"
959 " <ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>\n"
960 " <d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>\n"
961 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>\n"
962 " <ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>\n"
963 "</cont>\n",
964 LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_WITHSIBLINGS);
965 /* check diff */
966 assert_null(diff);
967
968 /* similar changes for nested defaults */
969 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll1", "def3", 0, NULL), LY_SUCCESS);
970 assert_int_equal(lyd_new_term(tree->prev, NULL, "d", "5", 0, NULL), LY_SUCCESS);
971 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll2", "non-dflt", 0, NULL), LY_SUCCESS);
972 assert_int_equal(lyd_validate_all(&tree, UTEST_LYCTX, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
973
974 /* check data tree */
975 CHECK_LYD_STRING_PARAM(tree,
976 "<l xmlns=\"urn:tests:f\">value</l>\n"
977 "<d xmlns=\"urn:tests:f\">15</d>\n"
978 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
979 "<cont xmlns=\"urn:tests:f\">\n"
980 " <ll1>def3</ll1>\n"
981 " <d>5</d>\n"
982 " <ll2>non-dflt</ll2>\n"
983 "</cont>\n",
984 LYD_XML, LYD_PRINT_WITHSIBLINGS);
985
986 /* check diff */
987 CHECK_LYD_STRING_PARAM(diff,
988 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">\n"
989 " <ll1 yang:operation=\"delete\">def1</ll1>\n"
990 " <ll1 yang:operation=\"delete\">def2</ll1>\n"
991 " <ll1 yang:operation=\"delete\">def3</ll1>\n"
992 " <d yang:operation=\"delete\">15</d>\n"
993 " <ll2 yang:operation=\"delete\">dflt1</ll2>\n"
994 " <ll2 yang:operation=\"delete\">dflt2</ll2>\n"
995 "</cont>\n",
996 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
997 lyd_free_all(diff);
998 lyd_free_all(tree);
Radek Krejci7be7b9f2021-02-24 11:46:27 +0100999
1000 /* check data tree - when enabled node */
1001 CHECK_PARSE_LYD_PARAM("<d xmlns=\"urn:tests:f\">666</d><cont xmlns=\"urn:tests:f\"><d>666</d></cont>",
1002 LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1003 CHECK_LYD_STRING_PARAM(tree,
1004 "<ll1 xmlns=\"urn:tests:f\">def1</ll1>\n"
1005 "<ll1 xmlns=\"urn:tests:f\">def2</ll1>\n"
1006 "<ll1 xmlns=\"urn:tests:f\">def3</ll1>\n"
1007 "<d xmlns=\"urn:tests:f\">666</d>\n"
1008 "<dd xmlns=\"urn:tests:f\">15</dd>\n"
1009 "<ll2 xmlns=\"urn:tests:f\">dflt1</ll2>\n"
1010 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>\n"
1011 "<cont xmlns=\"urn:tests:f\">\n"
1012 " <ll1>def1</ll1>\n"
1013 " <ll1>def2</ll1>\n"
1014 " <ll1>def3</ll1>\n"
1015 " <d>666</d>\n"
1016 " <dd>15</dd>\n"
1017 " <ll2>dflt1</ll2>\n"
1018 " <ll2>dflt2</ll2>\n"
1019 "</cont>\n",
1020 LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_WITHSIBLINGS);
1021 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +02001022}
1023
1024static void
1025test_state(void **state)
1026{
1027 const char *data;
1028 struct lyd_node *tree;
1029 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001030 "module h {\n"
1031 " namespace urn:tests:h;\n"
1032 " prefix h;\n"
1033 " yang-version 1.1;\n"
1034 "\n"
1035 " container cont {\n"
1036 " container cont2 {\n"
1037 " config false;\n"
1038 " leaf l {\n"
1039 " type string;\n"
1040 " }\n"
1041 " }\n"
1042 " }\n"
1043 "}";
Radek Iša56ca9e42020-09-08 18:42:00 +02001044
1045 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1046
1047 data = "<cont xmlns=\"urn:tests:h\">\n"
1048 " <cont2>\n"
1049 " <l>val</l>\n"
1050 " </cont2>\n"
1051 "</cont>\n";
1052 CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, LY_EVALID, tree);
Michal Vasko224e7772021-02-18 14:22:33 +01001053 CHECK_LOG_CTX("Unexpected data state node \"cont2\" found.",
Radek Krejci2efc45b2020-12-22 16:25:44 +01001054 "Schema location /h:cont/cont2, data location /h:cont, line number 3.");
Radek Iša56ca9e42020-09-08 18:42:00 +02001055
1056 CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
1057 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, NULL));
Michal Vasko224e7772021-02-18 14:22:33 +01001058 CHECK_LOG_CTX("Unexpected data state node \"cont2\" found.",
Radek Krejci2efc45b2020-12-22 16:25:44 +01001059 "Schema location /h:cont/cont2, data location /h:cont/cont2.");
Radek Iša56ca9e42020-09-08 18:42:00 +02001060 lyd_free_all(tree);
1061}
1062
1063static void
1064test_must(void **state)
1065{
1066 struct lyd_node *tree;
1067 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001068 "module i {\n"
1069 " namespace urn:tests:i;\n"
1070 " prefix i;\n"
1071 " yang-version 1.1;\n"
1072 "\n"
1073 " container cont {\n"
1074 " leaf l {\n"
1075 " type string;\n"
1076 " }\n"
1077 " leaf l2 {\n"
1078 " must \"../l = 'right'\";\n"
1079 " type string;\n"
1080 " }\n"
1081 " }\n"
1082 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +01001083
Radek Iša56ca9e42020-09-08 18:42:00 +02001084 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001085
Radek Iša56ca9e42020-09-08 18:42:00 +02001086 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:i\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001087 " <l>wrong</l>\n"
1088 " <l2>val</l2>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001089 "</cont>\n", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001090 CHECK_LOG_CTX("Must condition \"../l = 'right'\" not satisfied.",
1091 "Schema location /i:cont/l2, data location /i:cont/l2.");
Michal Vaskocc048b22020-03-27 15:52:38 +01001092
Radek Iša56ca9e42020-09-08 18:42:00 +02001093 LYD_TREE_CREATE("<cont xmlns=\"urn:tests:i\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001094 " <l>right</l>\n"
1095 " <l2>val</l2>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001096 "</cont>\n", tree);
1097 lyd_free_all(tree);
Michal Vaskocc048b22020-03-27 15:52:38 +01001098}
1099
Radek Iša56ca9e42020-09-08 18:42:00 +02001100const char *schema_j =
1101 "module j {\n"
1102 " namespace urn:tests:j;\n"
1103 " prefix j;\n"
1104 " yang-version 1.1;\n"
1105 "\n"
1106 " feature feat1;\n"
1107 "\n"
1108 " container cont {\n"
1109 " must \"false()\";\n"
1110 " list l1 {\n"
1111 " key \"k\";\n"
1112 " leaf k {\n"
1113 " type string;\n"
1114 " }\n"
1115 " action act {\n"
1116 " if-feature feat1;\n"
1117 " input {\n"
1118 " must \"../../lf1 = 'true'\";\n"
1119 " leaf lf2 {\n"
1120 " type leafref {\n"
1121 " path /lf3;\n"
1122 " }\n"
1123 " }\n"
1124 " }\n"
1125 " output {\n"
1126 " must \"../../lf1 = 'true2'\";\n"
1127 " leaf lf2 {\n"
1128 " type leafref {\n"
1129 " path /lf4;\n"
1130 " }\n"
1131 " }\n"
1132 " }\n"
1133 " }\n"
1134 " }\n"
1135 "\n"
1136 " leaf lf1 {\n"
1137 " type string;\n"
1138 " }\n"
1139 " }\n"
1140 "\n"
1141 " leaf lf3 {\n"
1142 " type string;\n"
1143 " }\n"
1144 "\n"
1145 " leaf lf4 {\n"
1146 " type string;\n"
1147 " }\n"
1148 "}";
1149const char *feats_j[] = {"feat1", NULL};
1150
Michal Vaskofea12c62020-03-30 11:00:15 +02001151static void
1152test_action(void **state)
1153{
Michal Vasko63f3d842020-07-08 10:10:14 +02001154 struct ly_in *in;
Michal Vaskofea12c62020-03-30 11:00:15 +02001155 struct lyd_node *tree, *op_tree;
Michal Vaskofea12c62020-03-30 11:00:15 +02001156
Radek Iša56ca9e42020-09-08 18:42:00 +02001157 UTEST_ADD_MODULE(schema_j, LYS_IN_YANG, feats_j, NULL);
1158
1159 assert_int_equal(LY_SUCCESS, ly_in_new_memory(
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001160 "<cont xmlns=\"urn:tests:j\">\n"
1161 " <l1>\n"
1162 " <k>val1</k>\n"
1163 " <act>\n"
1164 " <lf2>target</lf2>\n"
1165 " </act>\n"
1166 " </l1>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001167 "</cont>\n", &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001168 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 +02001169 assert_non_null(op_tree);
1170
1171 /* missing leafref */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001172 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_TYPE_RPC_YANG, NULL));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001173 CHECK_LOG_CTX("Invalid leafref value \"target\" - no existing target instance \"/lf3\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001174 "Schema location /j:cont/l1/act/input/lf2, data location /j:cont/l1[k='val1']/act/lf2.");
Michal Vasko63f3d842020-07-08 10:10:14 +02001175 ly_in_free(in, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001176
Radek Iša56ca9e42020-09-08 18:42:00 +02001177 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001178 " <lf1>not true</lf1>\n"
1179 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001180 "<lf3 xmlns=\"urn:tests:j\">target</lf3>\n",
1181 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001182
Michal Vaskofea12c62020-03-30 11:00:15 +02001183 /* input must false */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001184 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_TYPE_RPC_YANG, NULL));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001185 CHECK_LOG_CTX("Must condition \"../../lf1 = 'true'\" not satisfied.",
1186 "Data location /j:cont/l1[k='val1']/act.");
Michal Vaskofea12c62020-03-30 11:00:15 +02001187
Radek Iša56ca9e42020-09-08 18:42:00 +02001188 lyd_free_all(tree);
1189 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001190 " <lf1>true</lf1>\n"
1191 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001192 "<lf3 xmlns=\"urn:tests:j\">target</lf3>\n",
1193 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001194
1195 /* success */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001196 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_TYPE_RPC_YANG, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001197
Michal Vaskocb7526d2020-03-30 15:08:26 +02001198 lyd_free_tree(op_tree);
1199 lyd_free_siblings(tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001200}
1201
1202static void
aPiecek18a844e2021-08-10 11:06:24 +02001203test_rpc(void **state)
1204{
1205 const char *schema, *data;
1206 struct ly_in *in;
1207 struct lyd_node *tree;
1208
1209 /* Testing constraint violation in RPC. */
1210 schema =
1211 "module val-str {\n"
1212 " namespace \"urn:vstr\";\n"
1213 " prefix v;\n"
1214 "\n"
1215 " rpc modify-user-password {\n"
1216 " input {\n"
1217 " leaf old-password {\n"
1218 " type string {\n"
1219 " length \"4..8\";\n"
1220 " }\n"
1221 " }\n"
1222 " leaf new-password {\n"
1223 " type string {\n"
1224 " length \"4..8\";\n"
1225 " }\n"
1226 " }\n"
1227 " }\n"
1228 " }\n"
1229 "}\n";
1230 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1231 data =
1232 "<modify-user-password xmlns=\"urn:vstr\">\n"
1233 " <old-password>12345</old-password>\n"
1234 " <new-password>123</new-password>\n"
1235 "</modify-user-password>";
1236 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1237 /* Success, although the validation found a violation of
1238 * the restriction. An \"opaq\" node was created instead of
1239 * the \"new-password\" node from schema.
1240 */
1241 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
1242 assert_non_null(tree);
1243 /* Validate data as RPC request. */
1244 assert_int_equal(LY_EVALID, lyd_validate_op(tree, NULL, LYD_TYPE_RPC_YANG, NULL));
1245 CHECK_LOG_CTX("Invalid opaque node \"new-password\" found.",
1246 "Data location /val-str:modify-user-password/new-password.");
1247 ly_in_free(in, 0);
1248 lyd_free_all(tree);
1249}
1250
1251static void
Michal Vaskocb7526d2020-03-30 15:08:26 +02001252test_reply(void **state)
1253{
Michal Vasko63f3d842020-07-08 10:10:14 +02001254 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +01001255 struct lyd_node *tree, *op_tree;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001256
Radek Iša56ca9e42020-09-08 18:42:00 +02001257 UTEST_ADD_MODULE(schema_j, LYS_IN_YANG, feats_j, NULL);
1258
1259 assert_int_equal(LY_SUCCESS, ly_in_new_memory(
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001260 "<cont xmlns=\"urn:tests:j\">\n"
1261 " <l1>\n"
1262 " <k>val1</k>\n"
1263 " <act>\n"
1264 " <lf2>target</lf2>\n"
1265 " </act>\n"
1266 " </l1>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001267 "</cont>\n", &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001268 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 +02001269 assert_non_null(op_tree);
Michal Vasko63f3d842020-07-08 10:10:14 +02001270 ly_in_free(in, 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001271
1272 /* missing leafref */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001273 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_TYPE_REPLY_YANG, NULL));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001274 CHECK_LOG_CTX("Invalid leafref value \"target\" - no existing target instance \"/lf4\".",
Radek Krejci2a9fc652021-01-22 17:44:34 +01001275 "Schema location /j:cont/l1/act/output/lf2, data location /j:cont/l1[k='val1']/act/lf2.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001276
Radek Iša56ca9e42020-09-08 18:42:00 +02001277 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001278 " <lf1>not true</lf1>\n"
1279 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001280 "<lf4 xmlns=\"urn:tests:j\">target</lf4>\n",
1281 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001282
Michal Vaskocb7526d2020-03-30 15:08:26 +02001283 /* input must false */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001284 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_TYPE_REPLY_YANG, NULL));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001285 CHECK_LOG_CTX("Must condition \"../../lf1 = 'true2'\" not satisfied.", "Data location /j:cont/l1[k='val1']/act.");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001286
Radek Iša56ca9e42020-09-08 18:42:00 +02001287 lyd_free_all(tree);
1288 CHECK_PARSE_LYD_PARAM("<cont xmlns=\"urn:tests:j\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001289 " <lf1>true2</lf1>\n"
1290 "</cont>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001291 "<lf4 xmlns=\"urn:tests:j\">target</lf4>\n",
1292 LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001293
1294 /* success */
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001295 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_TYPE_REPLY_YANG, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001296
Michal Vaskofea12c62020-03-30 11:00:15 +02001297 lyd_free_tree(op_tree);
Radek Iša56ca9e42020-09-08 18:42:00 +02001298 lyd_free_all(tree);
Michal Vaskofea12c62020-03-30 11:00:15 +02001299}
1300
Michal Vasko8d151222021-07-22 12:12:41 +02001301static void
1302test_case(void **state)
1303{
1304 struct lyd_node *tree;
1305 const char *schema =
1306 "module k {\n"
1307 " namespace urn:tests:k;\n"
1308 " prefix k;\n"
1309 " yang-version 1.1;\n"
1310 "\n"
1311 " container ch {\n"
1312 " choice a0 {\n"
1313 " case v0 {\n"
1314 " leaf g0 {\n"
1315 " type string;\n"
1316 " }\n"
1317 " }\n"
1318 " case v1 {\n"
1319 " choice a1 {\n"
1320 " case r0 {\n"
1321 " leaf g1 {\n"
1322 " type string;\n"
1323 " }\n"
1324 " }\n"
1325 " case r1 {\n"
1326 " leaf g2 {\n"
1327 " type string;\n"
1328 " }\n"
1329 " leaf g3 {\n"
1330 " type string;\n"
1331 " }\n"
1332 " }\n"
1333 " case r2 {\n"
1334 " leaf g4 {\n"
1335 " type string;\n"
1336 " }\n"
1337 " }\n"
1338 " }\n"
1339 " }\n"
1340 " case v2 {\n"
1341 " choice a2 {\n"
1342 " case y0 {\n"
1343 " leaf g5 {\n"
1344 " type string;\n"
1345 " }\n"
1346 " }\n"
1347 " case y1 {\n"
1348 " leaf g6 {\n"
1349 " type string;\n"
1350 " }\n"
1351 " leaf g7 {\n"
1352 " type string;\n"
1353 " }\n"
1354 " }\n"
1355 " case y2 {\n"
1356 " leaf g8 {\n"
1357 " type string;\n"
1358 " }\n"
1359 " }\n"
1360 " }\n"
1361 " }\n"
1362 " }\n"
1363 " }\n"
1364 "}";
1365
1366 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1367
1368 CHECK_PARSE_LYD_PARAM(
1369 "{\n"
1370 " \"k:ch\": {\n"
1371 " \"g0\": \"value_g0\",\n"
1372 " \"g7\": \"value_g7\"\n"
1373 " }\n"
1374 "}\n", LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1375 CHECK_LOG_CTX("Data for both cases \"v0\" and \"v2\" exist.",
1376 "Schema location /k:ch/a0, data location /k:ch, line number 5.");
1377
1378 CHECK_PARSE_LYD_PARAM(
1379 "{\n"
1380 " \"k:ch\": {\n"
1381 " \"g7\": \"value_g7\",\n"
1382 " \"g0\": \"value_g0\"\n"
1383 " }\n"
1384 "}\n", LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1385 CHECK_LOG_CTX("Data for both cases \"v0\" and \"v2\" exist.",
1386 "Schema location /k:ch/a0, data location /k:ch, line number 5.");
1387}
1388
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001389int
1390main(void)
Michal Vaskocde73ac2019-11-14 16:10:27 +01001391{
1392 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001393 UTEST(test_when),
1394 UTEST(test_mandatory),
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05001395 UTEST(test_mandatory_when),
Radek Iša56ca9e42020-09-08 18:42:00 +02001396 UTEST(test_minmax),
1397 UTEST(test_unique),
1398 UTEST(test_unique_nested),
1399 UTEST(test_dup),
1400 UTEST(test_defaults),
1401 UTEST(test_state),
1402 UTEST(test_must),
1403 UTEST(test_action),
aPiecek18a844e2021-08-10 11:06:24 +02001404 UTEST(test_rpc),
Radek Iša56ca9e42020-09-08 18:42:00 +02001405 UTEST(test_reply),
Michal Vasko8d151222021-07-22 12:12:41 +02001406 UTEST(test_case),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001407 };
1408
Radek Iša56ca9e42020-09-08 18:42:00 +02001409 return cmocka_run_group_tests(tests, NULL, NULL);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001410}