blob: 16d742a767ec83967c8d9376d4f6a53e340c8d06 [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 */
14
Michal Vasko52927e22020-03-16 17:26:14 +010015#include "tests/config.h"
16
Michal Vaskocde73ac2019-11-14 16:10:27 +010017#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "../../src/context.h"
26#include "../../src/tree_data_internal.h"
Michal Vaskof03ed032020-03-04 13:31:44 +010027#include "../../src/printer_data.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31int store = -1; /* negative for infinite logging, positive for limited logging */
32
33struct ly_ctx *ctx; /* context for tests */
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38#if ENABLE_LOGGER_CHECKING
39static void
40logger(LY_LOG_LEVEL level, const char *msg, const char *path)
41{
42 (void) level; /* unused */
43 if (store) {
44 if (path && path[0]) {
45 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
46 } else {
47 strncpy(logbuf, msg, BUFSIZE - 1);
48 }
49 if (store > 0) {
50 --store;
51 }
52 }
53}
54#endif
55
56static int
57setup(void **state)
58{
59 (void) state; /* unused */
60
61 const char *schema_a =
62 "module a {"
63 "namespace urn:tests:a;"
64 "prefix a;"
65 "yang-version 1.1;"
66
67 "container cont {"
68 "leaf a {"
69 "when \"../../c = 'val_c'\";"
70 "type string;"
71 "}"
72 "leaf b {"
73 "type string;"
74 "}"
75 "}"
76 "leaf c {"
77 "when \"/cont/b = 'val_b'\";"
78 "type string;"
79 "}"
80 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010081 const char *schema_b =
82 "module b {"
83 "namespace urn:tests:b;"
84 "prefix b;"
85 "yang-version 1.1;"
86
87 "choice choic {"
88 "mandatory true;"
89 "leaf a {"
90 "type string;"
91 "}"
92 "case b {"
93 "leaf l {"
94 "type string;"
95 "}"
96 "}"
97 "}"
98 "leaf c {"
99 "mandatory true;"
100 "type string;"
101 "}"
102 "leaf d {"
103 "type empty;"
104 "}"
105 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100106 const char *schema_c =
107 "module c {"
108 "namespace urn:tests:c;"
109 "prefix c;"
110 "yang-version 1.1;"
111
112 "choice choic {"
113 "leaf a {"
114 "type string;"
115 "}"
116 "case b {"
117 "leaf-list l {"
118 "min-elements 3;"
119 "type string;"
120 "}"
121 "}"
122 "}"
123 "list lt {"
124 "max-elements 4;"
125 "key \"k\";"
126 "leaf k {"
127 "type string;"
128 "}"
129 "}"
130 "leaf d {"
131 "type empty;"
132 "}"
133 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100134 const char *schema_d =
135 "module d {"
136 "namespace urn:tests:d;"
137 "prefix d;"
138 "yang-version 1.1;"
139
140 "list lt {"
141 "key \"k\";"
142 "unique \"l1\";"
143 "leaf k {"
144 "type string;"
145 "}"
146 "leaf l1 {"
147 "type string;"
148 "}"
149 "}"
150 "list lt2 {"
151 "key \"k\";"
152 "unique \"cont/l2 l4\";"
153 "unique \"l5 l6\";"
154 "leaf k {"
155 "type string;"
156 "}"
157 "container cont {"
158 "leaf l2 {"
159 "type string;"
160 "}"
161 "}"
162 "leaf l4 {"
163 "type string;"
164 "}"
165 "leaf l5 {"
166 "type string;"
167 "}"
168 "leaf l6 {"
169 "type string;"
170 "}"
171 "list lt3 {"
172 "key \"kk\";"
173 "unique \"l3\";"
174 "leaf kk {"
175 "type string;"
176 "}"
177 "leaf l3 {"
178 "type string;"
179 "}"
180 "}"
181 "}"
182 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100183 const char *schema_e =
184 "module e {"
185 "namespace urn:tests:e;"
186 "prefix e;"
187 "yang-version 1.1;"
188
189 "choice choic {"
190 "leaf a {"
191 "type string;"
192 "}"
193 "case b {"
194 "leaf-list l {"
195 "type string;"
196 "}"
197 "}"
198 "}"
199 "list lt {"
200 "key \"k\";"
201 "leaf k {"
202 "type string;"
203 "}"
204 "}"
205 "leaf d {"
206 "type uint32;"
207 "}"
208 "leaf-list ll {"
209 "type string;"
210 "}"
211 "container cont {"
212 "list lt {"
213 "key \"k\";"
214 "leaf k {"
215 "type string;"
216 "}"
217 "}"
218 "leaf d {"
219 "type uint32;"
220 "}"
221 "leaf-list ll {"
222 "type string;"
223 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 "leaf-list ll2 {"
225 "type enumeration {"
226 "enum one;"
227 "enum two;"
228 "}"
229 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100230 "}"
231 "}";
232 const char *schema_f =
233 "module f {"
234 "namespace urn:tests:f;"
235 "prefix f;"
236 "yang-version 1.1;"
237
238 "choice choic {"
239 "default \"c\";"
240 "leaf a {"
241 "type string;"
242 "}"
243 "case b {"
244 "leaf l {"
245 "type string;"
246 "}"
247 "}"
248 "case c {"
249 "leaf-list ll1 {"
250 "type string;"
251 "default \"def1\";"
252 "default \"def2\";"
253 "default \"def3\";"
254 "}"
255 "}"
256 "}"
257 "leaf d {"
258 "type uint32;"
259 "default 15;"
260 "}"
261 "leaf-list ll2 {"
262 "type string;"
263 "default \"dflt1\";"
264 "default \"dflt2\";"
265 "}"
266 "container cont {"
267 "choice choic {"
268 "default \"c\";"
269 "leaf a {"
270 "type string;"
271 "}"
272 "case b {"
273 "leaf l {"
274 "type string;"
275 "}"
276 "}"
277 "case c {"
278 "leaf-list ll1 {"
279 "type string;"
280 "default \"def1\";"
281 "default \"def2\";"
282 "default \"def3\";"
283 "}"
284 "}"
285 "}"
286 "leaf d {"
287 "type uint32;"
288 "default 15;"
289 "}"
290 "leaf-list ll2 {"
291 "type string;"
292 "default \"dflt1\";"
293 "default \"dflt2\";"
294 "}"
295 "}"
296 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100297 const char *schema_g =
298 "module g {"
299 "namespace urn:tests:g;"
300 "prefix g;"
301 "yang-version 1.1;"
302
303 "feature f1;"
304 "feature f2;"
305 "feature f3;"
306
307 "container cont {"
308 "if-feature \"f1\";"
309 "choice choic {"
310 "if-feature \"f2 or f3\";"
311 "leaf a {"
312 "type string;"
313 "}"
314 "case b {"
315 "if-feature \"f2 and f1\";"
316 "leaf l {"
317 "type string;"
318 "}"
319 "}"
320 "}"
321 "leaf d {"
322 "type uint32;"
323 "}"
324 "container cont2 {"
325 "if-feature \"f2\";"
326 "leaf e {"
327 "type string;"
328 "}"
329 "}"
330 "}"
331 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100332 const char *schema_h =
333 "module h {"
334 "namespace urn:tests:h;"
335 "prefix h;"
336 "yang-version 1.1;"
337
338 "container cont {"
339 "container cont2 {"
340 "config false;"
341 "leaf l {"
342 "type string;"
343 "}"
344 "}"
345 "}"
346 "}";
Michal Vaskocc048b22020-03-27 15:52:38 +0100347 const char *schema_i =
348 "module i {"
349 "namespace urn:tests:i;"
350 "prefix i;"
351 "yang-version 1.1;"
352
353 "container cont {"
354 "leaf l {"
355 "type string;"
356 "}"
357 "leaf l2 {"
358 "must \"../l = 'right'\";"
359 "type string;"
360 "}"
361 "}"
362 "}";
Michal Vaskofea12c62020-03-30 11:00:15 +0200363 const char *schema_j =
364 "module j {"
365 "namespace urn:tests:j;"
366 "prefix j;"
367 "yang-version 1.1;"
368
369 "feature feat1;"
370
371 "container cont {"
372 "must \"false()\";"
373 "list l1 {"
374 "key \"k\";"
375 "leaf k {"
376 "type string;"
377 "}"
378 "action act {"
379 "if-feature feat1;"
380 "input {"
381 "must \"../../lf1 = 'true'\";"
382 "leaf lf2 {"
383 "type leafref {"
384 "path /lf3;"
385 "}"
386 "}"
387 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200388 "output {"
389 "must \"../../lf1 = 'true2'\";"
390 "leaf lf2 {"
391 "type leafref {"
392 "path /lf4;"
393 "}"
394 "}"
395 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200396 "}"
397 "}"
398
399 "leaf lf1 {"
400 "type string;"
401 "}"
402 "}"
403
404 "leaf lf3 {"
405 "type string;"
406 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200407
408 "leaf lf4 {"
409 "type string;"
410 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200411 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100412
413#if ENABLE_LOGGER_CHECKING
414 ly_set_log_clb(logger, 1);
415#endif
416
Michal Vaskof03ed032020-03-04 13:31:44 +0100417 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
418 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100419 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100420 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100421 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100422 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100423 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
424 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100425 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100426 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocc048b22020-03-27 15:52:38 +0100427 assert_non_null(lys_parse_mem(ctx, schema_i, LYS_IN_YANG));
Michal Vaskofea12c62020-03-30 11:00:15 +0200428 assert_non_null(lys_parse_mem(ctx, schema_j, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100429
430 return 0;
431}
432
433static int
434teardown(void **state)
435{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100436 (void)state;
437 ly_ctx_destroy(ctx, NULL);
438 ctx = NULL;
439
440 return 0;
441}
442
443static int
444teardown_s(void **state)
445{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100446#if ENABLE_LOGGER_CHECKING
447 if (*state) {
448 fprintf(stderr, "%s\n", logbuf);
449 }
450#else
451 (void) state; /* unused */
452#endif
453
Michal Vaskocde73ac2019-11-14 16:10:27 +0100454 return 0;
455}
456
457void
458logbuf_clean(void)
459{
460 logbuf[0] = '\0';
461}
462
463#if ENABLE_LOGGER_CHECKING
464# define logbuf_assert(str) assert_string_equal(logbuf, str)
465#else
466# define logbuf_assert(str)
467#endif
468
469static void
470test_when(void **state)
471{
472 *state = test_when;
473
474 const char *data;
475 struct lyd_node *tree;
476
477 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100478 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100479 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100480 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100481
482 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100483 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100484 assert_non_null(tree);
485 assert_string_equal("c", tree->next->schema->name);
486 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
487 lyd_free_all(tree);
488
489 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100490 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100491 assert_non_null(tree);
492 assert_string_equal("a", lyd_node_children(tree)->schema->name);
493 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
494 assert_string_equal("c", tree->next->schema->name);
495 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
496 lyd_free_all(tree);
497
498 *state = NULL;
499}
500
Michal Vaskoa3881362020-01-21 15:57:35 +0100501static void
502test_mandatory(void **state)
503{
504 *state = test_mandatory;
505
506 const char *data;
507 struct lyd_node *tree;
508
509 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100510 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100511 assert_null(tree);
512 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
513
514 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100515 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100516 assert_null(tree);
517 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
518
519 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100520 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100521 assert_null(tree);
522 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
523
524 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100525 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100526 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100527 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100528
529 *state = NULL;
530}
531
Michal Vaskoacd83e72020-02-04 14:12:01 +0100532static void
533test_minmax(void **state)
534{
535 *state = test_minmax;
536
537 const char *data;
538 struct lyd_node *tree;
539
540 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100541 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100542 assert_null(tree);
543 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
544
545 data =
546 "<l xmlns=\"urn:tests:c\">val1</l>"
547 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100548 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100549 assert_null(tree);
550 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
551
552 data =
553 "<l xmlns=\"urn:tests:c\">val1</l>"
554 "<l xmlns=\"urn:tests:c\">val2</l>"
555 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100556 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100557 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100558 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100559
560 data =
561 "<l xmlns=\"urn:tests:c\">val1</l>"
562 "<l xmlns=\"urn:tests:c\">val2</l>"
563 "<l xmlns=\"urn:tests:c\">val3</l>"
564 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
565 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
566 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
567 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
568 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100569 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100570 assert_null(tree);
571 logbuf_assert("Too many \"lt\" instances. /c:lt");
572
573 *state = NULL;
574}
575
Michal Vasko14654712020-02-06 08:35:21 +0100576static void
577test_unique(void **state)
578{
579 *state = test_unique;
580
581 const char *data;
582 struct lyd_node *tree;
583
584 data =
585 "<lt xmlns=\"urn:tests:d\">"
586 "<k>val1</k>"
587 "<l1>same</l1>"
588 "</lt>"
589 "<lt xmlns=\"urn:tests:d\">"
590 "<k>val2</k>"
591 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100592 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100593 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100594 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100595
596 data =
597 "<lt xmlns=\"urn:tests:d\">"
598 "<k>val1</k>"
599 "<l1>same</l1>"
600 "</lt>"
601 "<lt xmlns=\"urn:tests:d\">"
602 "<k>val2</k>"
603 "<l1>not-same</l1>"
604 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100605 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100606 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100607 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100608
609 data =
610 "<lt xmlns=\"urn:tests:d\">"
611 "<k>val1</k>"
612 "<l1>same</l1>"
613 "</lt>"
614 "<lt xmlns=\"urn:tests:d\">"
615 "<k>val2</k>"
616 "<l1>same</l1>"
617 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100618 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100619 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100620 logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val1']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100621
622 /* now try with more instances */
623 data =
624 "<lt xmlns=\"urn:tests:d\">"
625 "<k>val1</k>"
626 "<l1>1</l1>"
627 "</lt>"
628 "<lt xmlns=\"urn:tests:d\">"
629 "<k>val2</k>"
630 "<l1>2</l1>"
631 "</lt>"
632 "<lt xmlns=\"urn:tests:d\">"
633 "<k>val3</k>"
634 "<l1>3</l1>"
635 "</lt>"
636 "<lt xmlns=\"urn:tests:d\">"
637 "<k>val4</k>"
638 "<l1>4</l1>"
639 "</lt>"
640 "<lt xmlns=\"urn:tests:d\">"
641 "<k>val5</k>"
642 "<l1>5</l1>"
643 "</lt>"
644 "<lt xmlns=\"urn:tests:d\">"
645 "<k>val6</k>"
646 "<l1>6</l1>"
647 "</lt>"
648 "<lt xmlns=\"urn:tests:d\">"
649 "<k>val7</k>"
650 "<l1>7</l1>"
651 "</lt>"
652 "<lt xmlns=\"urn:tests:d\">"
653 "<k>val8</k>"
654 "<l1>8</l1>"
655 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100656 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100657 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100658 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100659
660 data =
661 "<lt xmlns=\"urn:tests:d\">"
662 "<k>val1</k>"
663 "<l1>1</l1>"
664 "</lt>"
665 "<lt xmlns=\"urn:tests:d\">"
666 "<k>val2</k>"
667 "<l1>2</l1>"
668 "</lt>"
669 "<lt xmlns=\"urn:tests:d\">"
670 "<k>val3</k>"
671 "<l1>3</l1>"
672 "</lt>"
673 "<lt xmlns=\"urn:tests:d\">"
674 "<k>val4</k>"
675 "</lt>"
676 "<lt xmlns=\"urn:tests:d\">"
677 "<k>val5</k>"
678 "<l1>5</l1>"
679 "</lt>"
680 "<lt xmlns=\"urn:tests:d\">"
681 "<k>val6</k>"
682 "<l1>6</l1>"
683 "</lt>"
684 "<lt xmlns=\"urn:tests:d\">"
685 "<k>val7</k>"
686 "</lt>"
687 "<lt xmlns=\"urn:tests:d\">"
688 "<k>val8</k>"
689 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100690 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100691 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100692 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100693
694 data =
695 "<lt xmlns=\"urn:tests:d\">"
696 "<k>val1</k>"
697 "<l1>1</l1>"
698 "</lt>"
699 "<lt xmlns=\"urn:tests:d\">"
700 "<k>val2</k>"
701 "<l1>2</l1>"
702 "</lt>"
703 "<lt xmlns=\"urn:tests:d\">"
704 "<k>val3</k>"
705 "</lt>"
706 "<lt xmlns=\"urn:tests:d\">"
707 "<k>val4</k>"
708 "<l1>4</l1>"
709 "</lt>"
710 "<lt xmlns=\"urn:tests:d\">"
711 "<k>val5</k>"
712 "</lt>"
713 "<lt xmlns=\"urn:tests:d\">"
714 "<k>val6</k>"
715 "</lt>"
716 "<lt xmlns=\"urn:tests:d\">"
717 "<k>val7</k>"
718 "<l1>2</l1>"
719 "</lt>"
720 "<lt xmlns=\"urn:tests:d\">"
721 "<k>val8</k>"
722 "<l1>8</l1>"
723 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100724 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100725 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100726 logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val7']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100727
728 *state = NULL;
729}
730
731static void
732test_unique_nested(void **state)
733{
734 *state = test_unique_nested;
735
736 const char *data;
737 struct lyd_node *tree;
738
739 /* nested list uniquest are compared only with instances in the same parent list instance */
740 data =
741 "<lt2 xmlns=\"urn:tests:d\">"
742 "<k>val1</k>"
743 "<cont>"
744 "<l2>1</l2>"
745 "</cont>"
746 "<l4>1</l4>"
747 "</lt2>"
748 "<lt2 xmlns=\"urn:tests:d\">"
749 "<k>val2</k>"
750 "<cont>"
751 "<l2>2</l2>"
752 "</cont>"
753 "<l4>2</l4>"
754 "<lt3>"
755 "<kk>val1</kk>"
756 "<l3>1</l3>"
757 "</lt3>"
758 "<lt3>"
759 "<kk>val2</kk>"
760 "<l3>2</l3>"
761 "</lt3>"
762 "</lt2>"
763 "<lt2 xmlns=\"urn:tests:d\">"
764 "<k>val3</k>"
765 "<cont>"
766 "<l2>3</l2>"
767 "</cont>"
768 "<l4>3</l4>"
769 "<lt3>"
770 "<kk>val1</kk>"
771 "<l3>2</l3>"
772 "</lt3>"
773 "</lt2>"
774 "<lt2 xmlns=\"urn:tests:d\">"
775 "<k>val4</k>"
776 "<cont>"
777 "<l2>4</l2>"
778 "</cont>"
779 "<l4>4</l4>"
780 "<lt3>"
781 "<kk>val1</kk>"
782 "<l3>3</l3>"
783 "</lt3>"
784 "</lt2>"
785 "<lt2 xmlns=\"urn:tests:d\">"
786 "<k>val5</k>"
787 "<cont>"
788 "<l2>5</l2>"
789 "</cont>"
790 "<l4>5</l4>"
791 "<lt3>"
792 "<kk>val1</kk>"
793 "<l3>3</l3>"
794 "</lt3>"
795 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100796 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_OPT_STRICT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100797 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100798 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100799
800 data =
801 "<lt2 xmlns=\"urn:tests:d\">"
802 "<k>val1</k>"
803 "<cont>"
804 "<l2>1</l2>"
805 "</cont>"
806 "<l4>1</l4>"
807 "</lt2>"
808 "<lt2 xmlns=\"urn:tests:d\">"
809 "<k>val2</k>"
810 "<cont>"
811 "<l2>2</l2>"
812 "</cont>"
813 "<lt3>"
814 "<kk>val1</kk>"
815 "<l3>1</l3>"
816 "</lt3>"
817 "<lt3>"
818 "<kk>val2</kk>"
819 "<l3>2</l3>"
820 "</lt3>"
821 "<lt3>"
822 "<kk>val3</kk>"
823 "<l3>1</l3>"
824 "</lt3>"
825 "</lt2>"
826 "<lt2 xmlns=\"urn:tests:d\">"
827 "<k>val3</k>"
828 "<cont>"
829 "<l2>3</l2>"
830 "</cont>"
831 "<l4>1</l4>"
832 "<lt3>"
833 "<kk>val1</kk>"
834 "<l3>2</l3>"
835 "</lt3>"
836 "</lt2>"
837 "<lt2 xmlns=\"urn:tests:d\">"
838 "<k>val4</k>"
839 "<cont>"
840 "<l2>4</l2>"
841 "</cont>"
842 "<lt3>"
843 "<kk>val1</kk>"
844 "<l3>3</l3>"
845 "</lt3>"
846 "</lt2>"
847 "<lt2 xmlns=\"urn:tests:d\">"
848 "<k>val5</k>"
849 "<cont>"
850 "<l2>5</l2>"
851 "</cont>"
852 "<lt3>"
853 "<kk>val1</kk>"
854 "<l3>3</l3>"
855 "</lt3>"
856 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100857 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100858 assert_null(tree);
859 logbuf_assert("Unique data leaf(s) \"l3\" not satisfied in \"/d:lt2[k='val2']/lt3[kk='val3']\" and"
Michal Vasko9b368d32020-02-14 13:53:31 +0100860 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100861
862 data =
863 "<lt2 xmlns=\"urn:tests:d\">"
864 "<k>val1</k>"
865 "<cont>"
866 "<l2>1</l2>"
867 "</cont>"
868 "<l4>1</l4>"
869 "</lt2>"
870 "<lt2 xmlns=\"urn:tests:d\">"
871 "<k>val2</k>"
872 "<cont>"
873 "<l2>2</l2>"
874 "</cont>"
875 "<l4>2</l4>"
876 "</lt2>"
877 "<lt2 xmlns=\"urn:tests:d\">"
878 "<k>val3</k>"
879 "<cont>"
880 "<l2>3</l2>"
881 "</cont>"
882 "<l4>3</l4>"
883 "</lt2>"
884 "<lt2 xmlns=\"urn:tests:d\">"
885 "<k>val4</k>"
886 "<cont>"
887 "<l2>2</l2>"
888 "</cont>"
889 "<l4>2</l4>"
890 "</lt2>"
891 "<lt2 xmlns=\"urn:tests:d\">"
892 "<k>val5</k>"
893 "<cont>"
894 "<l2>5</l2>"
895 "</cont>"
896 "<l4>5</l4>"
897 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100898 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100899 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100900 logbuf_assert("Unique data leaf(s) \"cont/l2 l4\" not satisfied in \"/d:lt2[k='val4']\" and \"/d:lt2[k='val2']\". /d:lt2[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100901
902 data =
903 "<lt2 xmlns=\"urn:tests:d\">"
904 "<k>val1</k>"
905 "<cont>"
906 "<l2>1</l2>"
907 "</cont>"
908 "<l4>1</l4>"
909 "<l5>1</l5>"
910 "<l6>1</l6>"
911 "</lt2>"
912 "<lt2 xmlns=\"urn:tests:d\">"
913 "<k>val2</k>"
914 "<cont>"
915 "<l2>2</l2>"
916 "</cont>"
917 "<l4>1</l4>"
918 "<l5>1</l5>"
919 "</lt2>"
920 "<lt2 xmlns=\"urn:tests:d\">"
921 "<k>val3</k>"
922 "<cont>"
923 "<l2>3</l2>"
924 "</cont>"
925 "<l4>1</l4>"
926 "<l5>3</l5>"
927 "<l6>3</l6>"
928 "</lt2>"
929 "<lt2 xmlns=\"urn:tests:d\">"
930 "<k>val4</k>"
931 "<cont>"
932 "<l2>4</l2>"
933 "</cont>"
934 "<l4>1</l4>"
935 "<l6>1</l6>"
936 "</lt2>"
937 "<lt2 xmlns=\"urn:tests:d\">"
938 "<k>val5</k>"
939 "<cont>"
940 "<l2>5</l2>"
941 "</cont>"
942 "<l4>1</l4>"
943 "<l5>3</l5>"
944 "<l6>3</l6>"
945 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100946 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100947 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100948 logbuf_assert("Unique data leaf(s) \"l5 l6\" not satisfied in \"/d:lt2[k='val5']\" and \"/d:lt2[k='val3']\". /d:lt2[k='val3']");
Michal Vasko14654712020-02-06 08:35:21 +0100949
950 *state = NULL;
951}
952
Michal Vaskof03ed032020-03-04 13:31:44 +0100953static void
954test_dup(void **state)
955{
956 *state = test_dup;
957
958 const char *data;
959 struct lyd_node *tree;
960
961 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100962 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100963 assert_null(tree);
964 logbuf_assert("Duplicate instance of \"d\". /e:d");
965
966 data = "<lt xmlns=\"urn:tests:e\"><k>A</k></lt><lt xmlns=\"urn:tests:e\"><k>B</k></lt><lt xmlns=\"urn:tests:e\"><k>A</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100967 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100968 assert_null(tree);
969 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
970
971 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100972 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100973 assert_null(tree);
974 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
975
976 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100977 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100978 assert_null(tree);
979 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
980
981 /* same tests again but using hashes */
982 data = "<cont xmlns=\"urn:tests:e\"><d>25</d><d>50</d><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100983 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100984 assert_null(tree);
985 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
986
987 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
988 "<lt><k>a</k></lt><lt><k>b</k></lt><lt><k>c</k></lt><lt><k>d</k></lt><lt><k>c</k></lt></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100989 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100990 assert_null(tree);
991 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
992
993 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
994 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100995 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100996 assert_null(tree);
997 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
998
999 /* cases */
1000 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><l xmlns=\"urn:tests:e\">b</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001001 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001002 assert_null(tree);
1003 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1004
1005 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><a xmlns=\"urn:tests:e\">aa</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001006 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001007 assert_null(tree);
1008 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1009
1010 *state = NULL;
1011}
1012
1013static void
1014test_defaults(void **state)
1015{
1016 *state = test_defaults;
1017
Michal Vaskof03ed032020-03-04 13:31:44 +01001018 char *str;
1019 struct lyd_node *tree, *node;
1020 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1021
Radek Krejcia5bba312020-01-09 15:41:20 +01001022 struct lyp_out *out;
1023 assert_non_null(out = lyp_new_memory(&str, 0));
1024
Michal Vaskob1b5c262020-03-05 14:29:47 +01001025 /* get defaults */
1026 tree = NULL;
1027 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001028 assert_non_null(tree);
1029
1030 /* check all defaults exist */
Radek Krejcia5bba312020-01-09 15:41:20 +01001031 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001032 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001033 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1034 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1035 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1036 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1037 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001038 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001039 "<cont xmlns=\"urn:tests:f\">"
1040 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1041 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1042 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1043 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1044 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1045 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001046 "</cont>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001047 lyp_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001048
1049 /* create another explicit case and validate */
1050 node = lyd_new_term(NULL, mod, "l", "value");
1051 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001052 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1053 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001054
1055 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001056 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001057 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +01001058 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1059 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1060 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001061 "<cont xmlns=\"urn:tests:f\">"
1062 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1063 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1064 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1065 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1066 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1067 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001068 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001069 "<l xmlns=\"urn:tests:f\">value</l>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001070 lyp_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001071
1072 /* create explicit leaf-list and leaf and validate */
1073 node = lyd_new_term(NULL, mod, "d", "15");
1074 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001075 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001076 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1077 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001078 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1079 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001080
1081 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001082 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001083 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001084 "<cont xmlns=\"urn:tests:f\">"
1085 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1086 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1087 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1088 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1089 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1090 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001091 "</cont>"
1092 "<l xmlns=\"urn:tests:f\">value</l>"
1093 "<d xmlns=\"urn:tests:f\">15</d>"
1094 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001095 lyp_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001096
Michal Vaskob1b5c262020-03-05 14:29:47 +01001097 /* create first explicit container, which should become implicit */
1098 node = lyd_new_inner(NULL, mod, "cont");
1099 assert_non_null(node);
1100 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1101 tree = tree->prev;
1102 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1103
1104 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001105 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001106 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001107 "<cont xmlns=\"urn:tests:f\">"
1108 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1109 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1110 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1111 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1112 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1113 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001114 "</cont>"
1115 "<l xmlns=\"urn:tests:f\">value</l>"
1116 "<d xmlns=\"urn:tests:f\">15</d>"
1117 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001118 lyp_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001119
1120 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1121 node = lyd_new_inner(NULL, mod, "cont");
1122 assert_non_null(node);
1123 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1124 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1125
1126 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001127 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001128 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001129 "<cont xmlns=\"urn:tests:f\">"
1130 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1131 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1132 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1133 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1134 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1135 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001136 "</cont>"
1137 "<l xmlns=\"urn:tests:f\">value</l>"
1138 "<d xmlns=\"urn:tests:f\">15</d>"
1139 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001140 lyp_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001141
1142 /* similar changes for nested defaults */
1143 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1144 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1145 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001146 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001147
1148 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001149 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001150 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001151 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001152 "<ll1>def3</ll1>"
1153 "<d>5</d>"
1154 "<ll2>non-dflt</ll2>"
1155 "</cont>"
1156 "<l xmlns=\"urn:tests:f\">value</l>"
1157 "<d xmlns=\"urn:tests:f\">15</d>"
1158 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejcia5bba312020-01-09 15:41:20 +01001159 lyp_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001160
1161 lyd_free_siblings(tree);
Radek Krejcia5bba312020-01-09 15:41:20 +01001162 lyp_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001163
1164 *state = NULL;
1165}
1166
Michal Vaskoc193ce92020-03-06 11:04:48 +01001167static void
1168test_iffeature(void **state)
1169{
1170 *state = test_iffeature;
1171
1172 const char *data;
1173 struct lyd_node *tree;
1174 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1175
1176 /* get empty data */
1177 tree = NULL;
1178 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1179 assert_null(tree);
1180
1181 /* disabled by f1 */
1182 data =
1183 "<cont xmlns=\"urn:tests:g\">"
1184 "<d>51</d>"
1185 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001186 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001187 assert_null(tree);
1188 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1189
1190 /* enable f1 */
1191 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1192
1193 /* get data with default container */
1194 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1195 assert_non_null(tree);
1196 lyd_free_siblings(tree);
1197
1198 /* disabled by f2 */
1199 data =
1200 "<cont xmlns=\"urn:tests:g\">"
1201 "<cont2>"
1202 "<e>val</e>"
1203 "</cont2>"
1204 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001205 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001206 assert_null(tree);
1207 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1208
1209 data =
1210 "<cont xmlns=\"urn:tests:g\">"
1211 "<a>val</a>"
1212 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001213 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001214 assert_null(tree);
1215 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1216
1217 /* enable f3 */
1218 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1219
Michal Vasko9f96a052020-03-10 09:41:45 +01001220 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001221 assert_non_null(tree);
1222 lyd_free_siblings(tree);
1223
1224 /* disabled by f2 */
1225 data =
1226 "<cont xmlns=\"urn:tests:g\">"
1227 "<l>val</l>"
1228 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001229 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001230 assert_null(tree);
1231 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1232
1233 /* enable f2 */
1234 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1235
Michal Vasko9f96a052020-03-10 09:41:45 +01001236 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001237 assert_non_null(tree);
1238 lyd_free_siblings(tree);
1239
1240 /* try separate validation */
1241 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1242 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1243 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1244
1245 data =
1246 "<cont xmlns=\"urn:tests:g\">"
1247 "<l>val</l>"
1248 "<d>51</d>"
1249 "<cont2>"
1250 "<e>val</e>"
1251 "</cont2>"
1252 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001253 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001254 assert_non_null(tree);
1255
1256 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1257 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1258
1259 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1260
1261 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1262 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1263
1264 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1265
1266 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1267
1268 lyd_free_siblings(tree);
1269
1270 *state = NULL;
1271}
1272
Michal Vasko5b37a352020-03-06 13:38:33 +01001273static void
1274test_state(void **state)
1275{
Michal Vaskocc048b22020-03-27 15:52:38 +01001276 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001277
1278 const char *data;
1279 struct lyd_node *tree;
1280
1281 data =
1282 "<cont xmlns=\"urn:tests:h\">"
1283 "<cont2>"
1284 "<l>val</l>"
1285 "</cont2>"
1286 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001287 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001288 assert_null(tree);
1289 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1290
Michal Vasko9f96a052020-03-10 09:41:45 +01001291 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001292 assert_null(tree);
1293 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1294
Michal Vasko9f96a052020-03-10 09:41:45 +01001295 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001296 assert_non_null(tree);
1297
1298 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE));
1299 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1300
1301 lyd_free_siblings(tree);
1302
1303 *state = NULL;
1304}
1305
Michal Vaskocc048b22020-03-27 15:52:38 +01001306static void
1307test_must(void **state)
1308{
1309 *state = test_must;
1310
1311 const char *data;
1312 struct lyd_node *tree;
1313
1314 data =
1315 "<cont xmlns=\"urn:tests:i\">"
1316 "<l>wrong</l>"
1317 "<l2>val</l2>"
1318 "</cont>";
1319 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1320 assert_null(tree);
1321 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1322
1323 data =
1324 "<cont xmlns=\"urn:tests:i\">"
1325 "<l>right</l>"
1326 "<l2>val</l2>"
1327 "</cont>";
1328 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1329 assert_non_null(tree);
1330 lyd_free_tree(tree);
1331
1332 *state = NULL;
1333}
1334
Michal Vaskofea12c62020-03-30 11:00:15 +02001335static void
1336test_action(void **state)
1337{
1338 *state = test_action;
1339
1340 const char *data;
1341 struct lyd_node *tree, *op_tree;
1342 const struct lys_module *mod;
1343
1344 data =
1345 "<cont xmlns=\"urn:tests:j\">"
1346 "<l1>"
1347 "<k>val1</k>"
1348 "<act>"
1349 "<lf2>target</lf2>"
1350 "</act>"
1351 "</l1>"
1352 "</cont>";
1353 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &op_tree, NULL));
1354 assert_non_null(op_tree);
1355
1356 /* missing leafref */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001357 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001358 logbuf_assert("Invalid leafref - required instance \"/lf3\" does not exists in the data tree(s). /j:cont/l1[k='val1']/act/lf2");
1359
1360 data =
1361 "<cont xmlns=\"urn:tests:j\">"
1362 "<lf1>not true</lf1>"
1363 "</cont>"
1364 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1365 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1366 assert_non_null(tree);
1367
1368 /* disabled if-feature */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001369 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001370 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1371
1372 mod = ly_ctx_get_module_latest(ctx, "j");
1373 assert_non_null(mod);
1374 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1375
1376 /* input must false */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001377 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001378 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1379
1380 lyd_free_siblings(tree);
1381 data =
1382 "<cont xmlns=\"urn:tests:j\">"
1383 "<lf1>true</lf1>"
1384 "</cont>"
1385 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1386 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1387 assert_non_null(tree);
1388
1389 /* success */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001390 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001391
Michal Vaskocb7526d2020-03-30 15:08:26 +02001392 lys_feature_disable(mod, "feat1");
1393 lyd_free_tree(op_tree);
1394 lyd_free_siblings(tree);
1395
1396 *state = NULL;
1397}
1398
1399static void
1400test_reply(void **state)
1401{
1402 *state = test_reply;
1403
1404 const char *data;
1405 struct lyd_node *tree, *op_tree, *request;
1406 const struct lys_module *mod;
1407
1408 data =
1409 "<cont xmlns=\"urn:tests:j\">"
1410 "<l1>"
1411 "<k>val1</k>"
1412 "<act>"
1413 "<lf2>target</lf2>"
1414 "</act>"
1415 "</l1>"
1416 "</cont>";
1417 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &request, NULL));
1418 assert_non_null(request);
1419 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
1420 assert_int_equal(LY_SUCCESS, lyd_parse_xml_reply(request, data, &op_tree, NULL));
1421 lyd_free_all(request);
1422 assert_non_null(op_tree);
1423
1424 /* missing leafref */
1425 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_OUTPUT));
1426 logbuf_assert("Invalid leafref - required instance \"/lf4\" does not exists in the data tree(s). /j:cont/l1[k='val1']/act/lf2");
1427
1428 data =
1429 "<cont xmlns=\"urn:tests:j\">"
1430 "<lf1>not true</lf1>"
1431 "</cont>"
1432 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1433 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1434 assert_non_null(tree);
1435
1436 /* disabled if-feature */
1437 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1438 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1439
1440 mod = ly_ctx_get_module_latest(ctx, "j");
1441 assert_non_null(mod);
1442 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1443
1444 /* input must false */
1445 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1446 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1447
1448 lyd_free_siblings(tree);
1449 data =
1450 "<cont xmlns=\"urn:tests:j\">"
1451 "<lf1>true2</lf1>"
1452 "</cont>"
1453 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1454 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1455 assert_non_null(tree);
1456
1457 /* success */
1458 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1459
1460 lys_feature_disable(mod, "feat1");
Michal Vaskofea12c62020-03-30 11:00:15 +02001461 lyd_free_tree(op_tree);
1462 lyd_free_siblings(tree);
1463
1464 *state = NULL;
1465}
1466
Michal Vaskocde73ac2019-11-14 16:10:27 +01001467int main(void)
1468{
1469 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001470 cmocka_unit_test_teardown(test_when, teardown_s),
1471 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1472 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001473 cmocka_unit_test_teardown(test_unique, teardown_s),
1474 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001475 cmocka_unit_test_teardown(test_dup, teardown_s),
1476 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001477 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001478 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001479 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001480 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001481 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001482 };
1483
Michal Vaskoacd83e72020-02-04 14:12:01 +01001484 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001485}