blob: ce0a9a4d962a348a25c646a52b5c3b566ca34262 [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
15#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "context.h"
24#include "printer.h"
25#include "printer_data.h"
26#include "tests/config.h"
27#include "tree_schema.h"
28#include "tree_data_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010029
30#define BUFSIZE 1024
31char logbuf[BUFSIZE] = {0};
32int store = -1; /* negative for infinite logging, positive for limited logging */
33
34struct ly_ctx *ctx; /* context for tests */
35
36/* set to 0 to printing error messages to stderr instead of checking them in code */
37#define ENABLE_LOGGER_CHECKING 1
38
39#if ENABLE_LOGGER_CHECKING
40static void
41logger(LY_LOG_LEVEL level, const char *msg, const char *path)
42{
43 (void) level; /* unused */
44 if (store) {
45 if (path && path[0]) {
46 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
47 } else {
48 strncpy(logbuf, msg, BUFSIZE - 1);
49 }
50 if (store > 0) {
51 --store;
52 }
53 }
54}
55#endif
56
57static int
58setup(void **state)
59{
60 (void) state; /* unused */
61
62 const char *schema_a =
63 "module a {"
64 "namespace urn:tests:a;"
65 "prefix a;"
66 "yang-version 1.1;"
67
68 "container cont {"
69 "leaf a {"
70 "when \"../../c = 'val_c'\";"
71 "type string;"
72 "}"
73 "leaf b {"
74 "type string;"
75 "}"
76 "}"
77 "leaf c {"
78 "when \"/cont/b = 'val_b'\";"
79 "type string;"
80 "}"
81 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010082 const char *schema_b =
83 "module b {"
84 "namespace urn:tests:b;"
85 "prefix b;"
86 "yang-version 1.1;"
87
88 "choice choic {"
89 "mandatory true;"
90 "leaf a {"
91 "type string;"
92 "}"
93 "case b {"
94 "leaf l {"
95 "type string;"
96 "}"
97 "}"
98 "}"
99 "leaf c {"
100 "mandatory true;"
101 "type string;"
102 "}"
103 "leaf d {"
104 "type empty;"
105 "}"
106 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100107 const char *schema_c =
108 "module c {"
109 "namespace urn:tests:c;"
110 "prefix c;"
111 "yang-version 1.1;"
112
113 "choice choic {"
114 "leaf a {"
115 "type string;"
116 "}"
117 "case b {"
118 "leaf-list l {"
119 "min-elements 3;"
120 "type string;"
121 "}"
122 "}"
123 "}"
124 "list lt {"
125 "max-elements 4;"
126 "key \"k\";"
127 "leaf k {"
128 "type string;"
129 "}"
130 "}"
131 "leaf d {"
132 "type empty;"
133 "}"
134 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100135 const char *schema_d =
136 "module d {"
137 "namespace urn:tests:d;"
138 "prefix d;"
139 "yang-version 1.1;"
140
141 "list lt {"
142 "key \"k\";"
143 "unique \"l1\";"
144 "leaf k {"
145 "type string;"
146 "}"
147 "leaf l1 {"
148 "type string;"
149 "}"
150 "}"
151 "list lt2 {"
152 "key \"k\";"
153 "unique \"cont/l2 l4\";"
154 "unique \"l5 l6\";"
155 "leaf k {"
156 "type string;"
157 "}"
158 "container cont {"
159 "leaf l2 {"
160 "type string;"
161 "}"
162 "}"
163 "leaf l4 {"
164 "type string;"
165 "}"
166 "leaf l5 {"
167 "type string;"
168 "}"
169 "leaf l6 {"
170 "type string;"
171 "}"
172 "list lt3 {"
173 "key \"kk\";"
174 "unique \"l3\";"
175 "leaf kk {"
176 "type string;"
177 "}"
178 "leaf l3 {"
179 "type string;"
180 "}"
181 "}"
182 "}"
183 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100184 const char *schema_e =
185 "module e {"
186 "namespace urn:tests:e;"
187 "prefix e;"
188 "yang-version 1.1;"
189
190 "choice choic {"
191 "leaf a {"
192 "type string;"
193 "}"
194 "case b {"
195 "leaf-list l {"
196 "type string;"
197 "}"
198 "}"
199 "}"
200 "list lt {"
201 "key \"k\";"
202 "leaf k {"
203 "type string;"
204 "}"
205 "}"
206 "leaf d {"
207 "type uint32;"
208 "}"
209 "leaf-list ll {"
210 "type string;"
211 "}"
212 "container cont {"
213 "list lt {"
214 "key \"k\";"
215 "leaf k {"
216 "type string;"
217 "}"
218 "}"
219 "leaf d {"
220 "type uint32;"
221 "}"
222 "leaf-list ll {"
223 "type string;"
224 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100225 "leaf-list ll2 {"
226 "type enumeration {"
227 "enum one;"
228 "enum two;"
229 "}"
230 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100231 "}"
232 "}";
233 const char *schema_f =
234 "module f {"
235 "namespace urn:tests:f;"
236 "prefix f;"
237 "yang-version 1.1;"
238
239 "choice choic {"
240 "default \"c\";"
241 "leaf a {"
242 "type string;"
243 "}"
244 "case b {"
245 "leaf l {"
246 "type string;"
247 "}"
248 "}"
249 "case c {"
250 "leaf-list ll1 {"
251 "type string;"
252 "default \"def1\";"
253 "default \"def2\";"
254 "default \"def3\";"
255 "}"
256 "}"
257 "}"
258 "leaf d {"
259 "type uint32;"
260 "default 15;"
261 "}"
262 "leaf-list ll2 {"
263 "type string;"
264 "default \"dflt1\";"
265 "default \"dflt2\";"
266 "}"
267 "container cont {"
268 "choice choic {"
269 "default \"c\";"
270 "leaf a {"
271 "type string;"
272 "}"
273 "case b {"
274 "leaf l {"
275 "type string;"
276 "}"
277 "}"
278 "case c {"
279 "leaf-list ll1 {"
280 "type string;"
281 "default \"def1\";"
282 "default \"def2\";"
283 "default \"def3\";"
284 "}"
285 "}"
286 "}"
287 "leaf d {"
288 "type uint32;"
289 "default 15;"
290 "}"
291 "leaf-list ll2 {"
292 "type string;"
293 "default \"dflt1\";"
294 "default \"dflt2\";"
295 "}"
296 "}"
297 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100298 const char *schema_g =
299 "module g {"
300 "namespace urn:tests:g;"
301 "prefix g;"
302 "yang-version 1.1;"
303
304 "feature f1;"
305 "feature f2;"
306 "feature f3;"
307
308 "container cont {"
309 "if-feature \"f1\";"
310 "choice choic {"
311 "if-feature \"f2 or f3\";"
312 "leaf a {"
313 "type string;"
314 "}"
315 "case b {"
316 "if-feature \"f2 and f1\";"
317 "leaf l {"
318 "type string;"
319 "}"
320 "}"
321 "}"
322 "leaf d {"
323 "type uint32;"
324 "}"
325 "container cont2 {"
326 "if-feature \"f2\";"
327 "leaf e {"
328 "type string;"
329 "}"
330 "}"
331 "}"
332 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100333 const char *schema_h =
334 "module h {"
335 "namespace urn:tests:h;"
336 "prefix h;"
337 "yang-version 1.1;"
338
339 "container cont {"
340 "container cont2 {"
341 "config false;"
342 "leaf l {"
343 "type string;"
344 "}"
345 "}"
346 "}"
347 "}";
Michal Vaskocc048b22020-03-27 15:52:38 +0100348 const char *schema_i =
349 "module i {"
350 "namespace urn:tests:i;"
351 "prefix i;"
352 "yang-version 1.1;"
353
354 "container cont {"
355 "leaf l {"
356 "type string;"
357 "}"
358 "leaf l2 {"
359 "must \"../l = 'right'\";"
360 "type string;"
361 "}"
362 "}"
363 "}";
Michal Vaskofea12c62020-03-30 11:00:15 +0200364 const char *schema_j =
365 "module j {"
366 "namespace urn:tests:j;"
367 "prefix j;"
368 "yang-version 1.1;"
369
370 "feature feat1;"
371
372 "container cont {"
373 "must \"false()\";"
374 "list l1 {"
375 "key \"k\";"
376 "leaf k {"
377 "type string;"
378 "}"
379 "action act {"
380 "if-feature feat1;"
381 "input {"
382 "must \"../../lf1 = 'true'\";"
383 "leaf lf2 {"
384 "type leafref {"
385 "path /lf3;"
386 "}"
387 "}"
388 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200389 "output {"
390 "must \"../../lf1 = 'true2'\";"
391 "leaf lf2 {"
392 "type leafref {"
393 "path /lf4;"
394 "}"
395 "}"
396 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200397 "}"
398 "}"
399
400 "leaf lf1 {"
401 "type string;"
402 "}"
403 "}"
404
405 "leaf lf3 {"
406 "type string;"
407 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200408
409 "leaf lf4 {"
410 "type string;"
411 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200412 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100413
414#if ENABLE_LOGGER_CHECKING
415 ly_set_log_clb(logger, 1);
416#endif
417
Michal Vaskof03ed032020-03-04 13:31:44 +0100418 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
419 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100420 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100421 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100422 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100423 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100424 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
425 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100426 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100427 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocc048b22020-03-27 15:52:38 +0100428 assert_non_null(lys_parse_mem(ctx, schema_i, LYS_IN_YANG));
Michal Vaskofea12c62020-03-30 11:00:15 +0200429 assert_non_null(lys_parse_mem(ctx, schema_j, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100430
431 return 0;
432}
433
434static int
435teardown(void **state)
436{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100437 (void)state;
438 ly_ctx_destroy(ctx, NULL);
439 ctx = NULL;
440
441 return 0;
442}
443
444static int
445teardown_s(void **state)
446{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100447#if ENABLE_LOGGER_CHECKING
448 if (*state) {
449 fprintf(stderr, "%s\n", logbuf);
450 }
451#else
452 (void) state; /* unused */
453#endif
454
Michal Vaskocde73ac2019-11-14 16:10:27 +0100455 return 0;
456}
457
458void
459logbuf_clean(void)
460{
461 logbuf[0] = '\0';
462}
463
464#if ENABLE_LOGGER_CHECKING
465# define logbuf_assert(str) assert_string_equal(logbuf, str)
466#else
467# define logbuf_assert(str)
468#endif
469
470static void
471test_when(void **state)
472{
473 *state = test_when;
474
475 const char *data;
476 struct lyd_node *tree;
477
478 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100479 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100480 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100481 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100482
483 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 +0100484 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100485 assert_non_null(tree);
486 assert_string_equal("c", tree->next->schema->name);
487 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
488 lyd_free_all(tree);
489
490 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 +0100491 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100492 assert_non_null(tree);
493 assert_string_equal("a", lyd_node_children(tree)->schema->name);
494 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
495 assert_string_equal("c", tree->next->schema->name);
496 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
497 lyd_free_all(tree);
498
499 *state = NULL;
500}
501
Michal Vaskoa3881362020-01-21 15:57:35 +0100502static void
503test_mandatory(void **state)
504{
505 *state = test_mandatory;
506
507 const char *data;
508 struct lyd_node *tree;
509
510 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100511 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100512 assert_null(tree);
513 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
514
515 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100516 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100517 assert_null(tree);
518 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
519
520 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100521 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100522 assert_null(tree);
523 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
524
525 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100526 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100527 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100528 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100529
530 *state = NULL;
531}
532
Michal Vaskoacd83e72020-02-04 14:12:01 +0100533static void
534test_minmax(void **state)
535{
536 *state = test_minmax;
537
538 const char *data;
539 struct lyd_node *tree;
540
541 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100542 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100543 assert_null(tree);
544 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
545
546 data =
547 "<l xmlns=\"urn:tests:c\">val1</l>"
548 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100549 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100550 assert_null(tree);
551 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
552
553 data =
554 "<l xmlns=\"urn:tests:c\">val1</l>"
555 "<l xmlns=\"urn:tests:c\">val2</l>"
556 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100557 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100558 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100559 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100560
561 data =
562 "<l xmlns=\"urn:tests:c\">val1</l>"
563 "<l xmlns=\"urn:tests:c\">val2</l>"
564 "<l xmlns=\"urn:tests:c\">val3</l>"
565 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
566 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
567 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
568 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
569 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100570 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100571 assert_null(tree);
572 logbuf_assert("Too many \"lt\" instances. /c:lt");
573
574 *state = NULL;
575}
576
Michal Vasko14654712020-02-06 08:35:21 +0100577static void
578test_unique(void **state)
579{
580 *state = test_unique;
581
582 const char *data;
583 struct lyd_node *tree;
584
585 data =
586 "<lt xmlns=\"urn:tests:d\">"
587 "<k>val1</k>"
588 "<l1>same</l1>"
589 "</lt>"
590 "<lt xmlns=\"urn:tests:d\">"
591 "<k>val2</k>"
592 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100593 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100594 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100595 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100596
597 data =
598 "<lt xmlns=\"urn:tests:d\">"
599 "<k>val1</k>"
600 "<l1>same</l1>"
601 "</lt>"
602 "<lt xmlns=\"urn:tests:d\">"
603 "<k>val2</k>"
604 "<l1>not-same</l1>"
605 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100606 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100607 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100608 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100609
610 data =
611 "<lt xmlns=\"urn:tests:d\">"
612 "<k>val1</k>"
613 "<l1>same</l1>"
614 "</lt>"
615 "<lt xmlns=\"urn:tests:d\">"
616 "<k>val2</k>"
617 "<l1>same</l1>"
618 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100619 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100620 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100621 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 +0100622
623 /* now try with more instances */
624 data =
625 "<lt xmlns=\"urn:tests:d\">"
626 "<k>val1</k>"
627 "<l1>1</l1>"
628 "</lt>"
629 "<lt xmlns=\"urn:tests:d\">"
630 "<k>val2</k>"
631 "<l1>2</l1>"
632 "</lt>"
633 "<lt xmlns=\"urn:tests:d\">"
634 "<k>val3</k>"
635 "<l1>3</l1>"
636 "</lt>"
637 "<lt xmlns=\"urn:tests:d\">"
638 "<k>val4</k>"
639 "<l1>4</l1>"
640 "</lt>"
641 "<lt xmlns=\"urn:tests:d\">"
642 "<k>val5</k>"
643 "<l1>5</l1>"
644 "</lt>"
645 "<lt xmlns=\"urn:tests:d\">"
646 "<k>val6</k>"
647 "<l1>6</l1>"
648 "</lt>"
649 "<lt xmlns=\"urn:tests:d\">"
650 "<k>val7</k>"
651 "<l1>7</l1>"
652 "</lt>"
653 "<lt xmlns=\"urn:tests:d\">"
654 "<k>val8</k>"
655 "<l1>8</l1>"
656 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100657 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100658 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100659 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100660
661 data =
662 "<lt xmlns=\"urn:tests:d\">"
663 "<k>val1</k>"
664 "<l1>1</l1>"
665 "</lt>"
666 "<lt xmlns=\"urn:tests:d\">"
667 "<k>val2</k>"
668 "<l1>2</l1>"
669 "</lt>"
670 "<lt xmlns=\"urn:tests:d\">"
671 "<k>val3</k>"
672 "<l1>3</l1>"
673 "</lt>"
674 "<lt xmlns=\"urn:tests:d\">"
675 "<k>val4</k>"
676 "</lt>"
677 "<lt xmlns=\"urn:tests:d\">"
678 "<k>val5</k>"
679 "<l1>5</l1>"
680 "</lt>"
681 "<lt xmlns=\"urn:tests:d\">"
682 "<k>val6</k>"
683 "<l1>6</l1>"
684 "</lt>"
685 "<lt xmlns=\"urn:tests:d\">"
686 "<k>val7</k>"
687 "</lt>"
688 "<lt xmlns=\"urn:tests:d\">"
689 "<k>val8</k>"
690 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100691 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100692 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100693 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100694
695 data =
696 "<lt xmlns=\"urn:tests:d\">"
697 "<k>val1</k>"
698 "<l1>1</l1>"
699 "</lt>"
700 "<lt xmlns=\"urn:tests:d\">"
701 "<k>val2</k>"
702 "<l1>2</l1>"
703 "</lt>"
704 "<lt xmlns=\"urn:tests:d\">"
705 "<k>val3</k>"
706 "</lt>"
707 "<lt xmlns=\"urn:tests:d\">"
708 "<k>val4</k>"
709 "<l1>4</l1>"
710 "</lt>"
711 "<lt xmlns=\"urn:tests:d\">"
712 "<k>val5</k>"
713 "</lt>"
714 "<lt xmlns=\"urn:tests:d\">"
715 "<k>val6</k>"
716 "</lt>"
717 "<lt xmlns=\"urn:tests:d\">"
718 "<k>val7</k>"
719 "<l1>2</l1>"
720 "</lt>"
721 "<lt xmlns=\"urn:tests:d\">"
722 "<k>val8</k>"
723 "<l1>8</l1>"
724 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100725 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100726 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100727 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 +0100728
729 *state = NULL;
730}
731
732static void
733test_unique_nested(void **state)
734{
735 *state = test_unique_nested;
736
737 const char *data;
738 struct lyd_node *tree;
739
740 /* nested list uniquest are compared only with instances in the same parent list instance */
741 data =
742 "<lt2 xmlns=\"urn:tests:d\">"
743 "<k>val1</k>"
744 "<cont>"
745 "<l2>1</l2>"
746 "</cont>"
747 "<l4>1</l4>"
748 "</lt2>"
749 "<lt2 xmlns=\"urn:tests:d\">"
750 "<k>val2</k>"
751 "<cont>"
752 "<l2>2</l2>"
753 "</cont>"
754 "<l4>2</l4>"
755 "<lt3>"
756 "<kk>val1</kk>"
757 "<l3>1</l3>"
758 "</lt3>"
759 "<lt3>"
760 "<kk>val2</kk>"
761 "<l3>2</l3>"
762 "</lt3>"
763 "</lt2>"
764 "<lt2 xmlns=\"urn:tests:d\">"
765 "<k>val3</k>"
766 "<cont>"
767 "<l2>3</l2>"
768 "</cont>"
769 "<l4>3</l4>"
770 "<lt3>"
771 "<kk>val1</kk>"
772 "<l3>2</l3>"
773 "</lt3>"
774 "</lt2>"
775 "<lt2 xmlns=\"urn:tests:d\">"
776 "<k>val4</k>"
777 "<cont>"
778 "<l2>4</l2>"
779 "</cont>"
780 "<l4>4</l4>"
781 "<lt3>"
782 "<kk>val1</kk>"
783 "<l3>3</l3>"
784 "</lt3>"
785 "</lt2>"
786 "<lt2 xmlns=\"urn:tests:d\">"
787 "<k>val5</k>"
788 "<cont>"
789 "<l2>5</l2>"
790 "</cont>"
791 "<l4>5</l4>"
792 "<lt3>"
793 "<kk>val1</kk>"
794 "<l3>3</l3>"
795 "</lt3>"
796 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100797 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 +0100798 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100799 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100800
801 data =
802 "<lt2 xmlns=\"urn:tests:d\">"
803 "<k>val1</k>"
804 "<cont>"
805 "<l2>1</l2>"
806 "</cont>"
807 "<l4>1</l4>"
808 "</lt2>"
809 "<lt2 xmlns=\"urn:tests:d\">"
810 "<k>val2</k>"
811 "<cont>"
812 "<l2>2</l2>"
813 "</cont>"
814 "<lt3>"
815 "<kk>val1</kk>"
816 "<l3>1</l3>"
817 "</lt3>"
818 "<lt3>"
819 "<kk>val2</kk>"
820 "<l3>2</l3>"
821 "</lt3>"
822 "<lt3>"
823 "<kk>val3</kk>"
824 "<l3>1</l3>"
825 "</lt3>"
826 "</lt2>"
827 "<lt2 xmlns=\"urn:tests:d\">"
828 "<k>val3</k>"
829 "<cont>"
830 "<l2>3</l2>"
831 "</cont>"
832 "<l4>1</l4>"
833 "<lt3>"
834 "<kk>val1</kk>"
835 "<l3>2</l3>"
836 "</lt3>"
837 "</lt2>"
838 "<lt2 xmlns=\"urn:tests:d\">"
839 "<k>val4</k>"
840 "<cont>"
841 "<l2>4</l2>"
842 "</cont>"
843 "<lt3>"
844 "<kk>val1</kk>"
845 "<l3>3</l3>"
846 "</lt3>"
847 "</lt2>"
848 "<lt2 xmlns=\"urn:tests:d\">"
849 "<k>val5</k>"
850 "<cont>"
851 "<l2>5</l2>"
852 "</cont>"
853 "<lt3>"
854 "<kk>val1</kk>"
855 "<l3>3</l3>"
856 "</lt3>"
857 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100858 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100859 assert_null(tree);
860 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 +0100861 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100862
863 data =
864 "<lt2 xmlns=\"urn:tests:d\">"
865 "<k>val1</k>"
866 "<cont>"
867 "<l2>1</l2>"
868 "</cont>"
869 "<l4>1</l4>"
870 "</lt2>"
871 "<lt2 xmlns=\"urn:tests:d\">"
872 "<k>val2</k>"
873 "<cont>"
874 "<l2>2</l2>"
875 "</cont>"
876 "<l4>2</l4>"
877 "</lt2>"
878 "<lt2 xmlns=\"urn:tests:d\">"
879 "<k>val3</k>"
880 "<cont>"
881 "<l2>3</l2>"
882 "</cont>"
883 "<l4>3</l4>"
884 "</lt2>"
885 "<lt2 xmlns=\"urn:tests:d\">"
886 "<k>val4</k>"
887 "<cont>"
888 "<l2>2</l2>"
889 "</cont>"
890 "<l4>2</l4>"
891 "</lt2>"
892 "<lt2 xmlns=\"urn:tests:d\">"
893 "<k>val5</k>"
894 "<cont>"
895 "<l2>5</l2>"
896 "</cont>"
897 "<l4>5</l4>"
898 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100899 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100900 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100901 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 +0100902
903 data =
904 "<lt2 xmlns=\"urn:tests:d\">"
905 "<k>val1</k>"
906 "<cont>"
907 "<l2>1</l2>"
908 "</cont>"
909 "<l4>1</l4>"
910 "<l5>1</l5>"
911 "<l6>1</l6>"
912 "</lt2>"
913 "<lt2 xmlns=\"urn:tests:d\">"
914 "<k>val2</k>"
915 "<cont>"
916 "<l2>2</l2>"
917 "</cont>"
918 "<l4>1</l4>"
919 "<l5>1</l5>"
920 "</lt2>"
921 "<lt2 xmlns=\"urn:tests:d\">"
922 "<k>val3</k>"
923 "<cont>"
924 "<l2>3</l2>"
925 "</cont>"
926 "<l4>1</l4>"
927 "<l5>3</l5>"
928 "<l6>3</l6>"
929 "</lt2>"
930 "<lt2 xmlns=\"urn:tests:d\">"
931 "<k>val4</k>"
932 "<cont>"
933 "<l2>4</l2>"
934 "</cont>"
935 "<l4>1</l4>"
936 "<l6>1</l6>"
937 "</lt2>"
938 "<lt2 xmlns=\"urn:tests:d\">"
939 "<k>val5</k>"
940 "<cont>"
941 "<l2>5</l2>"
942 "</cont>"
943 "<l4>1</l4>"
944 "<l5>3</l5>"
945 "<l6>3</l6>"
946 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100947 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100948 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100949 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 +0100950
951 *state = NULL;
952}
953
Michal Vaskof03ed032020-03-04 13:31:44 +0100954static void
955test_dup(void **state)
956{
957 *state = test_dup;
958
959 const char *data;
960 struct lyd_node *tree;
961
962 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100963 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100964 assert_null(tree);
965 logbuf_assert("Duplicate instance of \"d\". /e:d");
966
967 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 +0100968 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100969 assert_null(tree);
970 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
971
972 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 +0100973 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100974 assert_null(tree);
975 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
976
977 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100978 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100979 assert_null(tree);
980 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
981
982 /* same tests again but using hashes */
983 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 +0100984 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100985 assert_null(tree);
986 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
987
988 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
989 "<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 +0100990 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100991 assert_null(tree);
992 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
993
994 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
995 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100996 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100997 assert_null(tree);
998 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
999
1000 /* cases */
1001 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 +01001002 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001003 assert_null(tree);
1004 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1005
1006 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 +01001007 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001008 assert_null(tree);
1009 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1010
1011 *state = NULL;
1012}
1013
1014static void
1015test_defaults(void **state)
1016{
1017 *state = test_defaults;
1018
Michal Vaskof03ed032020-03-04 13:31:44 +01001019 char *str;
1020 struct lyd_node *tree, *node;
1021 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1022
Radek Krejci241f6b52020-05-21 18:13:49 +02001023 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +02001024 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +01001025
Michal Vaskob1b5c262020-03-05 14:29:47 +01001026 /* get defaults */
1027 tree = NULL;
1028 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001029 assert_non_null(tree);
1030
1031 /* check all defaults exist */
Radek Krejcia5bba312020-01-09 15:41:20 +01001032 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001033 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001034 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1035 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1036 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1037 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1038 "<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 +01001039 "<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 +01001040 "<cont xmlns=\"urn:tests:f\">"
1041 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1042 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1043 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1044 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1045 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1046 "<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 +01001047 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001048 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001049
1050 /* create another explicit case and validate */
1051 node = lyd_new_term(NULL, mod, "l", "value");
1052 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001053 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1054 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001055
1056 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001057 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001058 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +01001059 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1060 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1061 "<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 +01001062 "<cont xmlns=\"urn:tests:f\">"
1063 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1064 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1065 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1066 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1067 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1068 "<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 +01001069 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001070 "<l xmlns=\"urn:tests:f\">value</l>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001071 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001072
1073 /* create explicit leaf-list and leaf and validate */
1074 node = lyd_new_term(NULL, mod, "d", "15");
1075 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001076 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001077 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1078 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001079 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1080 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001081
1082 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001083 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001084 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001085 "<cont xmlns=\"urn:tests:f\">"
1086 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1087 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1088 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1089 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1090 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1091 "<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 +01001092 "</cont>"
1093 "<l xmlns=\"urn:tests:f\">value</l>"
1094 "<d xmlns=\"urn:tests:f\">15</d>"
1095 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001096 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001097
Michal Vaskob1b5c262020-03-05 14:29:47 +01001098 /* create first explicit container, which should become implicit */
1099 node = lyd_new_inner(NULL, mod, "cont");
1100 assert_non_null(node);
1101 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1102 tree = tree->prev;
1103 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1104
1105 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001106 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001107 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001108 "<cont xmlns=\"urn:tests:f\">"
1109 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1110 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1111 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1112 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1113 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1114 "<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 +01001115 "</cont>"
1116 "<l xmlns=\"urn:tests:f\">value</l>"
1117 "<d xmlns=\"urn:tests:f\">15</d>"
1118 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001119 ly_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001120
1121 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1122 node = lyd_new_inner(NULL, mod, "cont");
1123 assert_non_null(node);
1124 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1125 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1126
1127 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001128 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001129 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001130 "<cont xmlns=\"urn:tests:f\">"
1131 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1132 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1133 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1134 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1135 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1136 "<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 +01001137 "</cont>"
1138 "<l xmlns=\"urn:tests:f\">value</l>"
1139 "<d xmlns=\"urn:tests:f\">15</d>"
1140 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001141 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001142
1143 /* similar changes for nested defaults */
1144 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1145 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1146 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001147 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001148
1149 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001150 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001151 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001152 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001153 "<ll1>def3</ll1>"
1154 "<d>5</d>"
1155 "<ll2>non-dflt</ll2>"
1156 "</cont>"
1157 "<l xmlns=\"urn:tests:f\">value</l>"
1158 "<d xmlns=\"urn:tests:f\">15</d>"
1159 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001160 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001161
1162 lyd_free_siblings(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +02001163 ly_out_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001164
1165 *state = NULL;
1166}
1167
Michal Vaskoc193ce92020-03-06 11:04:48 +01001168static void
1169test_iffeature(void **state)
1170{
1171 *state = test_iffeature;
1172
1173 const char *data;
1174 struct lyd_node *tree;
1175 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1176
1177 /* get empty data */
1178 tree = NULL;
1179 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1180 assert_null(tree);
1181
1182 /* disabled by f1 */
1183 data =
1184 "<cont xmlns=\"urn:tests:g\">"
1185 "<d>51</d>"
1186 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001187 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001188 assert_null(tree);
1189 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1190
1191 /* enable f1 */
1192 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1193
1194 /* get data with default container */
1195 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1196 assert_non_null(tree);
1197 lyd_free_siblings(tree);
1198
1199 /* disabled by f2 */
1200 data =
1201 "<cont xmlns=\"urn:tests:g\">"
1202 "<cont2>"
1203 "<e>val</e>"
1204 "</cont2>"
1205 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001206 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001207 assert_null(tree);
1208 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1209
1210 data =
1211 "<cont xmlns=\"urn:tests:g\">"
1212 "<a>val</a>"
1213 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001214 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001215 assert_null(tree);
1216 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1217
1218 /* enable f3 */
1219 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1220
Michal Vasko9f96a052020-03-10 09:41:45 +01001221 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001222 assert_non_null(tree);
1223 lyd_free_siblings(tree);
1224
1225 /* disabled by f2 */
1226 data =
1227 "<cont xmlns=\"urn:tests:g\">"
1228 "<l>val</l>"
1229 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001230 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001231 assert_null(tree);
1232 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1233
1234 /* enable f2 */
1235 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1236
Michal Vasko9f96a052020-03-10 09:41:45 +01001237 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001238 assert_non_null(tree);
1239 lyd_free_siblings(tree);
1240
1241 /* try separate validation */
1242 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1243 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1244 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1245
1246 data =
1247 "<cont xmlns=\"urn:tests:g\">"
1248 "<l>val</l>"
1249 "<d>51</d>"
1250 "<cont2>"
1251 "<e>val</e>"
1252 "</cont2>"
1253 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001254 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001255 assert_non_null(tree);
1256
1257 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1258 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1259
1260 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1261
1262 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1263 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1264
1265 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1266
1267 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1268
1269 lyd_free_siblings(tree);
1270
1271 *state = NULL;
1272}
1273
Michal Vasko5b37a352020-03-06 13:38:33 +01001274static void
1275test_state(void **state)
1276{
Michal Vaskocc048b22020-03-27 15:52:38 +01001277 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001278
1279 const char *data;
1280 struct lyd_node *tree;
1281
1282 data =
1283 "<cont xmlns=\"urn:tests:h\">"
1284 "<cont2>"
1285 "<l>val</l>"
1286 "</cont2>"
1287 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001288 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 +01001289 assert_null(tree);
1290 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1291
Michal Vasko9f96a052020-03-10 09:41:45 +01001292 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 +01001293 assert_null(tree);
1294 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1295
Michal Vasko9f96a052020-03-10 09:41:45 +01001296 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001297 assert_non_null(tree);
1298
1299 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE));
1300 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1301
1302 lyd_free_siblings(tree);
1303
1304 *state = NULL;
1305}
1306
Michal Vaskocc048b22020-03-27 15:52:38 +01001307static void
1308test_must(void **state)
1309{
1310 *state = test_must;
1311
1312 const char *data;
1313 struct lyd_node *tree;
1314
1315 data =
1316 "<cont xmlns=\"urn:tests:i\">"
1317 "<l>wrong</l>"
1318 "<l2>val</l2>"
1319 "</cont>";
1320 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1321 assert_null(tree);
1322 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1323
1324 data =
1325 "<cont xmlns=\"urn:tests:i\">"
1326 "<l>right</l>"
1327 "<l2>val</l2>"
1328 "</cont>";
1329 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1330 assert_non_null(tree);
1331 lyd_free_tree(tree);
1332
1333 *state = NULL;
1334}
1335
Michal Vaskofea12c62020-03-30 11:00:15 +02001336static void
1337test_action(void **state)
1338{
1339 *state = test_action;
1340
1341 const char *data;
1342 struct lyd_node *tree, *op_tree;
1343 const struct lys_module *mod;
1344
1345 data =
1346 "<cont xmlns=\"urn:tests:j\">"
1347 "<l1>"
1348 "<k>val1</k>"
1349 "<act>"
1350 "<lf2>target</lf2>"
1351 "</act>"
1352 "</l1>"
1353 "</cont>";
1354 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &op_tree, NULL));
1355 assert_non_null(op_tree);
1356
1357 /* missing leafref */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001358 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_INPUT));
Michal Vasko004d3152020-06-11 19:59:22 +02001359 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value."
1360 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskofea12c62020-03-30 11:00:15 +02001361
1362 data =
1363 "<cont xmlns=\"urn:tests:j\">"
1364 "<lf1>not true</lf1>"
1365 "</cont>"
1366 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1367 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1368 assert_non_null(tree);
1369
1370 /* disabled if-feature */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001371 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001372 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1373
1374 mod = ly_ctx_get_module_latest(ctx, "j");
1375 assert_non_null(mod);
1376 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1377
1378 /* input must false */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001379 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001380 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1381
1382 lyd_free_siblings(tree);
1383 data =
1384 "<cont xmlns=\"urn:tests:j\">"
1385 "<lf1>true</lf1>"
1386 "</cont>"
1387 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1388 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1389 assert_non_null(tree);
1390
1391 /* success */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001392 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001393
Michal Vaskocb7526d2020-03-30 15:08:26 +02001394 lys_feature_disable(mod, "feat1");
1395 lyd_free_tree(op_tree);
1396 lyd_free_siblings(tree);
1397
1398 *state = NULL;
1399}
1400
1401static void
1402test_reply(void **state)
1403{
1404 *state = test_reply;
1405
1406 const char *data;
1407 struct lyd_node *tree, *op_tree, *request;
1408 const struct lys_module *mod;
1409
1410 data =
1411 "<cont xmlns=\"urn:tests:j\">"
1412 "<l1>"
1413 "<k>val1</k>"
1414 "<act>"
1415 "<lf2>target</lf2>"
1416 "</act>"
1417 "</l1>"
1418 "</cont>";
1419 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &request, NULL));
1420 assert_non_null(request);
1421 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
1422 assert_int_equal(LY_SUCCESS, lyd_parse_xml_reply(request, data, &op_tree, NULL));
1423 lyd_free_all(request);
1424 assert_non_null(op_tree);
1425
1426 /* missing leafref */
1427 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_OUTPUT));
Michal Vasko004d3152020-06-11 19:59:22 +02001428 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value."
1429 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001430
1431 data =
1432 "<cont xmlns=\"urn:tests:j\">"
1433 "<lf1>not true</lf1>"
1434 "</cont>"
1435 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1436 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1437 assert_non_null(tree);
1438
1439 /* disabled if-feature */
1440 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1441 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1442
1443 mod = ly_ctx_get_module_latest(ctx, "j");
1444 assert_non_null(mod);
1445 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1446
1447 /* input must false */
1448 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1449 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1450
1451 lyd_free_siblings(tree);
1452 data =
1453 "<cont xmlns=\"urn:tests:j\">"
1454 "<lf1>true2</lf1>"
1455 "</cont>"
1456 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1457 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1458 assert_non_null(tree);
1459
1460 /* success */
1461 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1462
1463 lys_feature_disable(mod, "feat1");
Michal Vaskofea12c62020-03-30 11:00:15 +02001464 lyd_free_tree(op_tree);
1465 lyd_free_siblings(tree);
1466
1467 *state = NULL;
1468}
1469
Michal Vaskocde73ac2019-11-14 16:10:27 +01001470int main(void)
1471{
1472 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001473 cmocka_unit_test_teardown(test_when, teardown_s),
1474 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1475 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001476 cmocka_unit_test_teardown(test_unique, teardown_s),
1477 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001478 cmocka_unit_test_teardown(test_dup, teardown_s),
1479 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001480 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001481 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001482 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001483 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001484 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001485 };
1486
Michal Vaskoacd83e72020-02-04 14:12:01 +01001487 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001488}