blob: e0c4de88669e0f22ce3efa4b9ffadd7dce99f61d [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"
Michal Vasko63f3d842020-07-08 10:10:14 +020024#include "parser.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "printer.h"
27#include "printer_data.h"
28#include "tests/config.h"
29#include "tree_schema.h"
30#include "tree_data_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34int store = -1; /* negative for infinite logging, positive for limited logging */
35
36struct ly_ctx *ctx; /* context for tests */
37
38/* set to 0 to printing error messages to stderr instead of checking them in code */
39#define ENABLE_LOGGER_CHECKING 1
40
41#if ENABLE_LOGGER_CHECKING
42static void
43logger(LY_LOG_LEVEL level, const char *msg, const char *path)
44{
45 (void) level; /* unused */
46 if (store) {
47 if (path && path[0]) {
48 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
49 } else {
50 strncpy(logbuf, msg, BUFSIZE - 1);
51 }
52 if (store > 0) {
53 --store;
54 }
55 }
56}
57#endif
58
59static int
60setup(void **state)
61{
62 (void) state; /* unused */
63
64 const char *schema_a =
65 "module a {"
66 "namespace urn:tests:a;"
67 "prefix a;"
68 "yang-version 1.1;"
69
70 "container cont {"
71 "leaf a {"
72 "when \"../../c = 'val_c'\";"
73 "type string;"
74 "}"
75 "leaf b {"
76 "type string;"
77 "}"
78 "}"
79 "leaf c {"
80 "when \"/cont/b = 'val_b'\";"
81 "type string;"
82 "}"
83 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010084 const char *schema_b =
85 "module b {"
86 "namespace urn:tests:b;"
87 "prefix b;"
88 "yang-version 1.1;"
89
90 "choice choic {"
91 "mandatory true;"
92 "leaf a {"
93 "type string;"
94 "}"
95 "case b {"
96 "leaf l {"
97 "type string;"
98 "}"
99 "}"
100 "}"
101 "leaf c {"
102 "mandatory true;"
103 "type string;"
104 "}"
105 "leaf d {"
106 "type empty;"
107 "}"
108 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100109 const char *schema_c =
110 "module c {"
111 "namespace urn:tests:c;"
112 "prefix c;"
113 "yang-version 1.1;"
114
115 "choice choic {"
116 "leaf a {"
117 "type string;"
118 "}"
119 "case b {"
120 "leaf-list l {"
121 "min-elements 3;"
122 "type string;"
123 "}"
124 "}"
125 "}"
126 "list lt {"
127 "max-elements 4;"
128 "key \"k\";"
129 "leaf k {"
130 "type string;"
131 "}"
132 "}"
133 "leaf d {"
134 "type empty;"
135 "}"
136 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100137 const char *schema_d =
138 "module d {"
139 "namespace urn:tests:d;"
140 "prefix d;"
141 "yang-version 1.1;"
142
143 "list lt {"
144 "key \"k\";"
145 "unique \"l1\";"
146 "leaf k {"
147 "type string;"
148 "}"
149 "leaf l1 {"
150 "type string;"
151 "}"
152 "}"
153 "list lt2 {"
154 "key \"k\";"
155 "unique \"cont/l2 l4\";"
156 "unique \"l5 l6\";"
157 "leaf k {"
158 "type string;"
159 "}"
160 "container cont {"
161 "leaf l2 {"
162 "type string;"
163 "}"
164 "}"
165 "leaf l4 {"
166 "type string;"
167 "}"
168 "leaf l5 {"
169 "type string;"
170 "}"
171 "leaf l6 {"
172 "type string;"
173 "}"
174 "list lt3 {"
175 "key \"kk\";"
176 "unique \"l3\";"
177 "leaf kk {"
178 "type string;"
179 "}"
180 "leaf l3 {"
181 "type string;"
182 "}"
183 "}"
184 "}"
185 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100186 const char *schema_e =
187 "module e {"
188 "namespace urn:tests:e;"
189 "prefix e;"
190 "yang-version 1.1;"
191
192 "choice choic {"
193 "leaf a {"
194 "type string;"
195 "}"
196 "case b {"
197 "leaf-list l {"
198 "type string;"
199 "}"
200 "}"
201 "}"
202 "list lt {"
203 "key \"k\";"
204 "leaf k {"
205 "type string;"
206 "}"
207 "}"
208 "leaf d {"
209 "type uint32;"
210 "}"
211 "leaf-list ll {"
212 "type string;"
213 "}"
214 "container cont {"
215 "list lt {"
216 "key \"k\";"
217 "leaf k {"
218 "type string;"
219 "}"
220 "}"
221 "leaf d {"
222 "type uint32;"
223 "}"
224 "leaf-list ll {"
225 "type string;"
226 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100227 "leaf-list ll2 {"
228 "type enumeration {"
229 "enum one;"
230 "enum two;"
231 "}"
232 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100233 "}"
234 "}";
235 const char *schema_f =
236 "module f {"
237 "namespace urn:tests:f;"
238 "prefix f;"
239 "yang-version 1.1;"
240
241 "choice choic {"
242 "default \"c\";"
243 "leaf a {"
244 "type string;"
245 "}"
246 "case b {"
247 "leaf l {"
248 "type string;"
249 "}"
250 "}"
251 "case c {"
252 "leaf-list ll1 {"
253 "type string;"
254 "default \"def1\";"
255 "default \"def2\";"
256 "default \"def3\";"
257 "}"
258 "}"
259 "}"
260 "leaf d {"
261 "type uint32;"
262 "default 15;"
263 "}"
264 "leaf-list ll2 {"
265 "type string;"
266 "default \"dflt1\";"
267 "default \"dflt2\";"
268 "}"
269 "container cont {"
270 "choice choic {"
271 "default \"c\";"
272 "leaf a {"
273 "type string;"
274 "}"
275 "case b {"
276 "leaf l {"
277 "type string;"
278 "}"
279 "}"
280 "case c {"
281 "leaf-list ll1 {"
282 "type string;"
283 "default \"def1\";"
284 "default \"def2\";"
285 "default \"def3\";"
286 "}"
287 "}"
288 "}"
289 "leaf d {"
290 "type uint32;"
291 "default 15;"
292 "}"
293 "leaf-list ll2 {"
294 "type string;"
295 "default \"dflt1\";"
296 "default \"dflt2\";"
297 "}"
298 "}"
299 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100300 const char *schema_g =
301 "module g {"
302 "namespace urn:tests:g;"
303 "prefix g;"
304 "yang-version 1.1;"
305
306 "feature f1;"
307 "feature f2;"
308 "feature f3;"
309
310 "container cont {"
311 "if-feature \"f1\";"
312 "choice choic {"
313 "if-feature \"f2 or f3\";"
314 "leaf a {"
315 "type string;"
316 "}"
317 "case b {"
318 "if-feature \"f2 and f1\";"
319 "leaf l {"
320 "type string;"
321 "}"
322 "}"
323 "}"
324 "leaf d {"
325 "type uint32;"
326 "}"
327 "container cont2 {"
328 "if-feature \"f2\";"
329 "leaf e {"
330 "type string;"
331 "}"
332 "}"
333 "}"
334 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100335 const char *schema_h =
336 "module h {"
337 "namespace urn:tests:h;"
338 "prefix h;"
339 "yang-version 1.1;"
340
341 "container cont {"
342 "container cont2 {"
343 "config false;"
344 "leaf l {"
345 "type string;"
346 "}"
347 "}"
348 "}"
349 "}";
Michal Vaskocc048b22020-03-27 15:52:38 +0100350 const char *schema_i =
351 "module i {"
352 "namespace urn:tests:i;"
353 "prefix i;"
354 "yang-version 1.1;"
355
356 "container cont {"
357 "leaf l {"
358 "type string;"
359 "}"
360 "leaf l2 {"
361 "must \"../l = 'right'\";"
362 "type string;"
363 "}"
364 "}"
365 "}";
Michal Vaskofea12c62020-03-30 11:00:15 +0200366 const char *schema_j =
367 "module j {"
368 "namespace urn:tests:j;"
369 "prefix j;"
370 "yang-version 1.1;"
371
372 "feature feat1;"
373
374 "container cont {"
375 "must \"false()\";"
376 "list l1 {"
377 "key \"k\";"
378 "leaf k {"
379 "type string;"
380 "}"
381 "action act {"
382 "if-feature feat1;"
383 "input {"
384 "must \"../../lf1 = 'true'\";"
385 "leaf lf2 {"
386 "type leafref {"
387 "path /lf3;"
388 "}"
389 "}"
390 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200391 "output {"
392 "must \"../../lf1 = 'true2'\";"
393 "leaf lf2 {"
394 "type leafref {"
395 "path /lf4;"
396 "}"
397 "}"
398 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200399 "}"
400 "}"
401
402 "leaf lf1 {"
403 "type string;"
404 "}"
405 "}"
406
407 "leaf lf3 {"
408 "type string;"
409 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200410
411 "leaf lf4 {"
412 "type string;"
413 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200414 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100415
416#if ENABLE_LOGGER_CHECKING
417 ly_set_log_clb(logger, 1);
418#endif
419
Michal Vaskof03ed032020-03-04 13:31:44 +0100420 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
421 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200422 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL));
423 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_b, LYS_IN_YANG, NULL));
424 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_c, LYS_IN_YANG, NULL));
425 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_d, LYS_IN_YANG, NULL));
426 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_e, LYS_IN_YANG, NULL));
427 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_f, LYS_IN_YANG, NULL));
428 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_g, LYS_IN_YANG, NULL));
429 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_h, LYS_IN_YANG, NULL));
430 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_i, LYS_IN_YANG, NULL));
431 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_j, LYS_IN_YANG, NULL));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100432
433 return 0;
434}
435
436static int
437teardown(void **state)
438{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100439 (void)state;
440 ly_ctx_destroy(ctx, NULL);
441 ctx = NULL;
442
443 return 0;
444}
445
446static int
447teardown_s(void **state)
448{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100449#if ENABLE_LOGGER_CHECKING
450 if (*state) {
451 fprintf(stderr, "%s\n", logbuf);
452 }
453#else
454 (void) state; /* unused */
455#endif
456
Michal Vaskocde73ac2019-11-14 16:10:27 +0100457 return 0;
458}
459
460void
461logbuf_clean(void)
462{
463 logbuf[0] = '\0';
464}
465
466#if ENABLE_LOGGER_CHECKING
467# define logbuf_assert(str) assert_string_equal(logbuf, str)
468#else
469# define logbuf_assert(str)
470#endif
471
472static void
473test_when(void **state)
474{
475 *state = test_when;
476
477 const char *data;
478 struct lyd_node *tree;
479
480 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100482 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100483 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100484
485 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100487 assert_non_null(tree);
488 assert_string_equal("c", tree->next->schema->name);
489 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
490 lyd_free_all(tree);
491
492 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100494 assert_non_null(tree);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200495 assert_string_equal("a", lyd_node_children(tree, 0)->schema->name);
496 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree, 0)->flags);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100497 assert_string_equal("c", tree->next->schema->name);
498 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
499 lyd_free_all(tree);
500
501 *state = NULL;
502}
503
Michal Vaskoa3881362020-01-21 15:57:35 +0100504static void
505test_mandatory(void **state)
506{
507 *state = test_mandatory;
508
509 const char *data;
510 struct lyd_node *tree;
511
512 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100514 assert_null(tree);
515 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
516
517 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100519 assert_null(tree);
520 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
521
522 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100524 assert_null(tree);
525 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
526
527 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100529 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100530 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100531
532 *state = NULL;
533}
534
Michal Vaskoacd83e72020-02-04 14:12:01 +0100535static void
536test_minmax(void **state)
537{
538 *state = test_minmax;
539
540 const char *data;
541 struct lyd_node *tree;
542
543 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100545 assert_null(tree);
546 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
547
548 data =
549 "<l xmlns=\"urn:tests:c\">val1</l>"
550 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100552 assert_null(tree);
553 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
554
555 data =
556 "<l xmlns=\"urn:tests:c\">val1</l>"
557 "<l xmlns=\"urn:tests:c\">val2</l>"
558 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100560 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100561 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100562
563 data =
564 "<l xmlns=\"urn:tests:c\">val1</l>"
565 "<l xmlns=\"urn:tests:c\">val2</l>"
566 "<l xmlns=\"urn:tests:c\">val3</l>"
567 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
568 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
569 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
570 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
571 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200572 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100573 assert_null(tree);
574 logbuf_assert("Too many \"lt\" instances. /c:lt");
575
576 *state = NULL;
577}
578
Michal Vasko14654712020-02-06 08:35:21 +0100579static void
580test_unique(void **state)
581{
582 *state = test_unique;
583
584 const char *data;
585 struct lyd_node *tree;
586
587 data =
588 "<lt xmlns=\"urn:tests:d\">"
589 "<k>val1</k>"
590 "<l1>same</l1>"
591 "</lt>"
592 "<lt xmlns=\"urn:tests:d\">"
593 "<k>val2</k>"
594 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200595 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100596 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100597 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100598
599 data =
600 "<lt xmlns=\"urn:tests:d\">"
601 "<k>val1</k>"
602 "<l1>same</l1>"
603 "</lt>"
604 "<lt xmlns=\"urn:tests:d\">"
605 "<k>val2</k>"
606 "<l1>not-same</l1>"
607 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200608 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100609 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100610 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100611
612 data =
613 "<lt xmlns=\"urn:tests:d\">"
614 "<k>val1</k>"
615 "<l1>same</l1>"
616 "</lt>"
617 "<lt xmlns=\"urn:tests:d\">"
618 "<k>val2</k>"
619 "<l1>same</l1>"
620 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200621 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100622 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100623 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 +0100624
625 /* now try with more instances */
626 data =
627 "<lt xmlns=\"urn:tests:d\">"
628 "<k>val1</k>"
629 "<l1>1</l1>"
630 "</lt>"
631 "<lt xmlns=\"urn:tests:d\">"
632 "<k>val2</k>"
633 "<l1>2</l1>"
634 "</lt>"
635 "<lt xmlns=\"urn:tests:d\">"
636 "<k>val3</k>"
637 "<l1>3</l1>"
638 "</lt>"
639 "<lt xmlns=\"urn:tests:d\">"
640 "<k>val4</k>"
641 "<l1>4</l1>"
642 "</lt>"
643 "<lt xmlns=\"urn:tests:d\">"
644 "<k>val5</k>"
645 "<l1>5</l1>"
646 "</lt>"
647 "<lt xmlns=\"urn:tests:d\">"
648 "<k>val6</k>"
649 "<l1>6</l1>"
650 "</lt>"
651 "<lt xmlns=\"urn:tests:d\">"
652 "<k>val7</k>"
653 "<l1>7</l1>"
654 "</lt>"
655 "<lt xmlns=\"urn:tests:d\">"
656 "<k>val8</k>"
657 "<l1>8</l1>"
658 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200659 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100660 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100661 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100662
663 data =
664 "<lt xmlns=\"urn:tests:d\">"
665 "<k>val1</k>"
666 "<l1>1</l1>"
667 "</lt>"
668 "<lt xmlns=\"urn:tests:d\">"
669 "<k>val2</k>"
670 "<l1>2</l1>"
671 "</lt>"
672 "<lt xmlns=\"urn:tests:d\">"
673 "<k>val3</k>"
674 "<l1>3</l1>"
675 "</lt>"
676 "<lt xmlns=\"urn:tests:d\">"
677 "<k>val4</k>"
678 "</lt>"
679 "<lt xmlns=\"urn:tests:d\">"
680 "<k>val5</k>"
681 "<l1>5</l1>"
682 "</lt>"
683 "<lt xmlns=\"urn:tests:d\">"
684 "<k>val6</k>"
685 "<l1>6</l1>"
686 "</lt>"
687 "<lt xmlns=\"urn:tests:d\">"
688 "<k>val7</k>"
689 "</lt>"
690 "<lt xmlns=\"urn:tests:d\">"
691 "<k>val8</k>"
692 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200693 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100694 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100695 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100696
697 data =
698 "<lt xmlns=\"urn:tests:d\">"
699 "<k>val1</k>"
700 "<l1>1</l1>"
701 "</lt>"
702 "<lt xmlns=\"urn:tests:d\">"
703 "<k>val2</k>"
704 "<l1>2</l1>"
705 "</lt>"
706 "<lt xmlns=\"urn:tests:d\">"
707 "<k>val3</k>"
708 "</lt>"
709 "<lt xmlns=\"urn:tests:d\">"
710 "<k>val4</k>"
711 "<l1>4</l1>"
712 "</lt>"
713 "<lt xmlns=\"urn:tests:d\">"
714 "<k>val5</k>"
715 "</lt>"
716 "<lt xmlns=\"urn:tests:d\">"
717 "<k>val6</k>"
718 "</lt>"
719 "<lt xmlns=\"urn:tests:d\">"
720 "<k>val7</k>"
721 "<l1>2</l1>"
722 "</lt>"
723 "<lt xmlns=\"urn:tests:d\">"
724 "<k>val8</k>"
725 "<l1>8</l1>"
726 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200727 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100728 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100729 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 +0100730
731 *state = NULL;
732}
733
734static void
735test_unique_nested(void **state)
736{
737 *state = test_unique_nested;
738
739 const char *data;
740 struct lyd_node *tree;
741
742 /* nested list uniquest are compared only with instances in the same parent list instance */
743 data =
744 "<lt2 xmlns=\"urn:tests:d\">"
745 "<k>val1</k>"
746 "<cont>"
747 "<l2>1</l2>"
748 "</cont>"
749 "<l4>1</l4>"
750 "</lt2>"
751 "<lt2 xmlns=\"urn:tests:d\">"
752 "<k>val2</k>"
753 "<cont>"
754 "<l2>2</l2>"
755 "</cont>"
756 "<l4>2</l4>"
757 "<lt3>"
758 "<kk>val1</kk>"
759 "<l3>1</l3>"
760 "</lt3>"
761 "<lt3>"
762 "<kk>val2</kk>"
763 "<l3>2</l3>"
764 "</lt3>"
765 "</lt2>"
766 "<lt2 xmlns=\"urn:tests:d\">"
767 "<k>val3</k>"
768 "<cont>"
769 "<l2>3</l2>"
770 "</cont>"
771 "<l4>3</l4>"
772 "<lt3>"
773 "<kk>val1</kk>"
774 "<l3>2</l3>"
775 "</lt3>"
776 "</lt2>"
777 "<lt2 xmlns=\"urn:tests:d\">"
778 "<k>val4</k>"
779 "<cont>"
780 "<l2>4</l2>"
781 "</cont>"
782 "<l4>4</l4>"
783 "<lt3>"
784 "<kk>val1</kk>"
785 "<l3>3</l3>"
786 "</lt3>"
787 "</lt2>"
788 "<lt2 xmlns=\"urn:tests:d\">"
789 "<k>val5</k>"
790 "<cont>"
791 "<l2>5</l2>"
792 "</cont>"
793 "<l4>5</l4>"
794 "<lt3>"
795 "<kk>val1</kk>"
796 "<l3>3</l3>"
797 "</lt3>"
798 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200799 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100800 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100801 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100802
803 data =
804 "<lt2 xmlns=\"urn:tests:d\">"
805 "<k>val1</k>"
806 "<cont>"
807 "<l2>1</l2>"
808 "</cont>"
809 "<l4>1</l4>"
810 "</lt2>"
811 "<lt2 xmlns=\"urn:tests:d\">"
812 "<k>val2</k>"
813 "<cont>"
814 "<l2>2</l2>"
815 "</cont>"
816 "<lt3>"
817 "<kk>val1</kk>"
818 "<l3>1</l3>"
819 "</lt3>"
820 "<lt3>"
821 "<kk>val2</kk>"
822 "<l3>2</l3>"
823 "</lt3>"
824 "<lt3>"
825 "<kk>val3</kk>"
826 "<l3>1</l3>"
827 "</lt3>"
828 "</lt2>"
829 "<lt2 xmlns=\"urn:tests:d\">"
830 "<k>val3</k>"
831 "<cont>"
832 "<l2>3</l2>"
833 "</cont>"
834 "<l4>1</l4>"
835 "<lt3>"
836 "<kk>val1</kk>"
837 "<l3>2</l3>"
838 "</lt3>"
839 "</lt2>"
840 "<lt2 xmlns=\"urn:tests:d\">"
841 "<k>val4</k>"
842 "<cont>"
843 "<l2>4</l2>"
844 "</cont>"
845 "<lt3>"
846 "<kk>val1</kk>"
847 "<l3>3</l3>"
848 "</lt3>"
849 "</lt2>"
850 "<lt2 xmlns=\"urn:tests:d\">"
851 "<k>val5</k>"
852 "<cont>"
853 "<l2>5</l2>"
854 "</cont>"
855 "<lt3>"
856 "<kk>val1</kk>"
857 "<l3>3</l3>"
858 "</lt3>"
859 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200860 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100861 assert_null(tree);
862 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 +0100863 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100864
865 data =
866 "<lt2 xmlns=\"urn:tests:d\">"
867 "<k>val1</k>"
868 "<cont>"
869 "<l2>1</l2>"
870 "</cont>"
871 "<l4>1</l4>"
872 "</lt2>"
873 "<lt2 xmlns=\"urn:tests:d\">"
874 "<k>val2</k>"
875 "<cont>"
876 "<l2>2</l2>"
877 "</cont>"
878 "<l4>2</l4>"
879 "</lt2>"
880 "<lt2 xmlns=\"urn:tests:d\">"
881 "<k>val3</k>"
882 "<cont>"
883 "<l2>3</l2>"
884 "</cont>"
885 "<l4>3</l4>"
886 "</lt2>"
887 "<lt2 xmlns=\"urn:tests:d\">"
888 "<k>val4</k>"
889 "<cont>"
890 "<l2>2</l2>"
891 "</cont>"
892 "<l4>2</l4>"
893 "</lt2>"
894 "<lt2 xmlns=\"urn:tests:d\">"
895 "<k>val5</k>"
896 "<cont>"
897 "<l2>5</l2>"
898 "</cont>"
899 "<l4>5</l4>"
900 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200901 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100902 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100903 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 +0100904
905 data =
906 "<lt2 xmlns=\"urn:tests:d\">"
907 "<k>val1</k>"
908 "<cont>"
909 "<l2>1</l2>"
910 "</cont>"
911 "<l4>1</l4>"
912 "<l5>1</l5>"
913 "<l6>1</l6>"
914 "</lt2>"
915 "<lt2 xmlns=\"urn:tests:d\">"
916 "<k>val2</k>"
917 "<cont>"
918 "<l2>2</l2>"
919 "</cont>"
920 "<l4>1</l4>"
921 "<l5>1</l5>"
922 "</lt2>"
923 "<lt2 xmlns=\"urn:tests:d\">"
924 "<k>val3</k>"
925 "<cont>"
926 "<l2>3</l2>"
927 "</cont>"
928 "<l4>1</l4>"
929 "<l5>3</l5>"
930 "<l6>3</l6>"
931 "</lt2>"
932 "<lt2 xmlns=\"urn:tests:d\">"
933 "<k>val4</k>"
934 "<cont>"
935 "<l2>4</l2>"
936 "</cont>"
937 "<l4>1</l4>"
938 "<l6>1</l6>"
939 "</lt2>"
940 "<lt2 xmlns=\"urn:tests:d\">"
941 "<k>val5</k>"
942 "<cont>"
943 "<l2>5</l2>"
944 "</cont>"
945 "<l4>1</l4>"
946 "<l5>3</l5>"
947 "<l6>3</l6>"
948 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200949 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100950 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100951 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 +0100952
953 *state = NULL;
954}
955
Michal Vaskof03ed032020-03-04 13:31:44 +0100956static void
957test_dup(void **state)
958{
959 *state = test_dup;
960
961 const char *data;
962 struct lyd_node *tree;
963
964 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200965 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100966 assert_null(tree);
967 logbuf_assert("Duplicate instance of \"d\". /e:d");
968
969 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 Vasko63f3d842020-07-08 10:10:14 +0200970 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100971 assert_null(tree);
972 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
973
974 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200975 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100976 assert_null(tree);
977 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
978
979 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100981 assert_null(tree);
982 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
983
984 /* same tests again but using hashes */
985 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 Vasko63f3d842020-07-08 10:10:14 +0200986 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100987 assert_null(tree);
988 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
989
990 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
991 "<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 Vasko63f3d842020-07-08 10:10:14 +0200992 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100993 assert_null(tree);
994 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
995
996 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
997 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100999 assert_null(tree);
1000 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
1001
1002 /* cases */
1003 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 Vasko63f3d842020-07-08 10:10:14 +02001004 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001005 assert_null(tree);
1006 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1007
1008 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 Vasko63f3d842020-07-08 10:10:14 +02001009 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001010 assert_null(tree);
1011 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1012
1013 *state = NULL;
1014}
1015
1016static void
1017test_defaults(void **state)
1018{
1019 *state = test_defaults;
1020
Michal Vaskof03ed032020-03-04 13:31:44 +01001021 char *str;
Michal Vasko8104fd42020-07-13 11:09:51 +02001022 struct lyd_node *tree, *node, *diff;
Michal Vaskof03ed032020-03-04 13:31:44 +01001023 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1024
Radek Krejci241f6b52020-05-21 18:13:49 +02001025 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +02001026 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +01001027
Michal Vaskob1b5c262020-03-05 14:29:47 +01001028 /* get defaults */
1029 tree = NULL;
Michal Vasko8104fd42020-07-13 11:09:51 +02001030 assert_int_equal(lyd_validate_module(&tree, mod, 0, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001031 assert_non_null(tree);
Michal Vasko8104fd42020-07-13 11:09:51 +02001032 assert_non_null(diff);
Michal Vaskof03ed032020-03-04 13:31:44 +01001033
1034 /* check all defaults exist */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001035 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001036 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001037 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1038 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1039 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1040 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1041 "<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 +01001042 "<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 +01001043 "<cont xmlns=\"urn:tests:f\">"
1044 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1045 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1046 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1047 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1048 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1049 "<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 +01001050 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001051 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001052
Michal Vasko8104fd42020-07-13 11:09:51 +02001053 /* check diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001054 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001055 assert_string_equal(str,
1056 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def1</ll1>"
1057 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def2</ll1>"
1058 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def3</ll1>"
1059 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">15</d>"
1060 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt1</ll2>"
1061 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt2</ll2>"
1062 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
1063 "<ll1 yang:operation=\"create\">def1</ll1>"
1064 "<ll1 yang:operation=\"create\">def2</ll1>"
1065 "<ll1 yang:operation=\"create\">def3</ll1>"
1066 "<d yang:operation=\"create\">15</d>"
1067 "<ll2 yang:operation=\"create\">dflt1</ll2>"
1068 "<ll2 yang:operation=\"create\">dflt2</ll2>"
1069 "</cont>"
1070 );
1071 ly_out_reset(out);
1072 lyd_free_siblings(diff);
1073
Michal Vaskof03ed032020-03-04 13:31:44 +01001074 /* create another explicit case and validate */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001075 assert_int_equal(lyd_new_term(NULL, mod, "l", "value", &node), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001076 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001077 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001078
1079 /* check data tree */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001080 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001081 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +01001082 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1083 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1084 "<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 +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>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001093 "<l xmlns=\"urn:tests:f\">value</l>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001094 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001095
Michal Vasko8104fd42020-07-13 11:09:51 +02001096 /* check diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001097 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001098 assert_string_equal(str,
1099 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def1</ll1>"
1100 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def2</ll1>"
1101 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def3</ll1>"
1102 );
1103 ly_out_reset(out);
1104 lyd_free_siblings(diff);
1105
Michal Vaskof03ed032020-03-04 13:31:44 +01001106 /* create explicit leaf-list and leaf and validate */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001107 assert_int_equal(lyd_new_term(NULL, mod, "d", "15", &node), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001108 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001109 assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", &node), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001110 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001111 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001112
1113 /* check data tree */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001114 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001115 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001116 "<cont xmlns=\"urn:tests:f\">"
1117 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1118 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1119 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1120 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1121 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1122 "<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 +01001123 "</cont>"
1124 "<l xmlns=\"urn:tests:f\">value</l>"
1125 "<d xmlns=\"urn:tests:f\">15</d>"
1126 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001127 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001128
Michal Vasko8104fd42020-07-13 11:09:51 +02001129 /* check diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001130 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001131 assert_string_equal(str,
1132 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">15</d>"
1133 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt1</ll2>"
1134 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt2</ll2>"
1135 );
1136 ly_out_reset(out);
1137 lyd_free_siblings(diff);
1138
Michal Vaskob1b5c262020-03-05 14:29:47 +01001139 /* create first explicit container, which should become implicit */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001140 assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001141 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1142 tree = tree->prev;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001143 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001144
1145 /* check data tree */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001146 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001147 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001148 "<cont xmlns=\"urn:tests:f\">"
1149 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1150 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1151 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1152 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1153 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1154 "<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 +01001155 "</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 Krejci241f6b52020-05-21 18:13:49 +02001159 ly_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001160
Michal Vasko8104fd42020-07-13 11:09:51 +02001161 /* check diff */
1162 assert_null(diff);
1163
Michal Vaskob1b5c262020-03-05 14:29:47 +01001164 /* create second explicit container, which should become implicit, so the first tree node should be removed */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001165 assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001166 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001167 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001168
1169 /* check data tree */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001170 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001171 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001172 "<cont xmlns=\"urn:tests:f\">"
1173 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1174 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1175 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1176 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1177 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1178 "<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 +01001179 "</cont>"
1180 "<l xmlns=\"urn:tests:f\">value</l>"
1181 "<d xmlns=\"urn:tests:f\">15</d>"
1182 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001183 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001184
Michal Vasko8104fd42020-07-13 11:09:51 +02001185 /* check diff */
1186 assert_null(diff);
1187
Michal Vaskof03ed032020-03-04 13:31:44 +01001188 /* similar changes for nested defaults */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001189 assert_int_equal(lyd_new_term(tree, NULL, "ll1", "def3", NULL), LY_SUCCESS);
1190 assert_int_equal(lyd_new_term(tree, NULL, "d", "5", NULL), LY_SUCCESS);
1191 assert_int_equal(lyd_new_term(tree, NULL, "ll2", "non-dflt", NULL), LY_SUCCESS);
1192 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001193
1194 /* check data tree */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001195 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001196 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001197 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001198 "<ll1>def3</ll1>"
1199 "<d>5</d>"
1200 "<ll2>non-dflt</ll2>"
1201 "</cont>"
1202 "<l xmlns=\"urn:tests:f\">value</l>"
1203 "<d xmlns=\"urn:tests:f\">15</d>"
1204 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001205 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001206
Michal Vasko8104fd42020-07-13 11:09:51 +02001207 /* check diff */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001208 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL);
Michal Vasko8104fd42020-07-13 11:09:51 +02001209 assert_string_equal(str,
1210 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
1211 "<ll1 yang:operation=\"delete\">def1</ll1>"
1212 "<ll1 yang:operation=\"delete\">def2</ll1>"
1213 "<ll1 yang:operation=\"delete\">def3</ll1>"
1214 "<d yang:operation=\"delete\">15</d>"
1215 "<ll2 yang:operation=\"delete\">dflt1</ll2>"
1216 "<ll2 yang:operation=\"delete\">dflt2</ll2>"
1217 "</cont>"
1218 );
1219 ly_out_reset(out);
1220 lyd_free_siblings(diff);
1221
Michal Vaskof03ed032020-03-04 13:31:44 +01001222 lyd_free_siblings(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +02001223 ly_out_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001224
1225 *state = NULL;
1226}
1227
Michal Vaskoc193ce92020-03-06 11:04:48 +01001228static void
1229test_iffeature(void **state)
1230{
1231 *state = test_iffeature;
1232
1233 const char *data;
1234 struct lyd_node *tree;
1235 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1236
1237 /* get empty data */
1238 tree = NULL;
Michal Vasko8104fd42020-07-13 11:09:51 +02001239 assert_int_equal(lyd_validate_module(&tree, mod, 0, NULL), LY_SUCCESS);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001240 assert_null(tree);
1241
1242 /* disabled by f1 */
1243 data =
1244 "<cont xmlns=\"urn:tests:g\">"
1245 "<d>51</d>"
1246 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001247 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001248 assert_null(tree);
1249 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1250
1251 /* enable f1 */
1252 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1253
1254 /* get data with default container */
Michal Vasko8104fd42020-07-13 11:09:51 +02001255 assert_int_equal(lyd_validate_module(&tree, mod, 0, NULL), LY_SUCCESS);
Michal Vaskoc193ce92020-03-06 11:04:48 +01001256 assert_non_null(tree);
1257 lyd_free_siblings(tree);
1258
1259 /* disabled by f2 */
1260 data =
1261 "<cont xmlns=\"urn:tests:g\">"
1262 "<cont2>"
1263 "<e>val</e>"
1264 "</cont2>"
1265 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001266 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001267 assert_null(tree);
1268 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1269
1270 data =
1271 "<cont xmlns=\"urn:tests:g\">"
1272 "<a>val</a>"
1273 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001274 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001275 assert_null(tree);
1276 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1277
1278 /* enable f3 */
1279 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1280
Michal Vasko63f3d842020-07-08 10:10:14 +02001281 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001282 assert_non_null(tree);
1283 lyd_free_siblings(tree);
1284
1285 /* disabled by f2 */
1286 data =
1287 "<cont xmlns=\"urn:tests:g\">"
1288 "<l>val</l>"
1289 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001290 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001291 assert_null(tree);
1292 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1293
1294 /* enable f2 */
1295 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1296
Michal Vasko63f3d842020-07-08 10:10:14 +02001297 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001298 assert_non_null(tree);
1299 lyd_free_siblings(tree);
1300
1301 /* try separate validation */
1302 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1303 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1304 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1305
1306 data =
1307 "<cont xmlns=\"urn:tests:g\">"
1308 "<l>val</l>"
1309 "<d>51</d>"
1310 "<cont2>"
1311 "<e>val</e>"
1312 "</cont2>"
1313 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001314 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001315 assert_non_null(tree);
1316
Michal Vasko3a41dff2020-07-15 14:30:28 +02001317 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001318 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1319
1320 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1321
Michal Vasko3a41dff2020-07-15 14:30:28 +02001322 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001323 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1324
1325 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1326
Michal Vasko3a41dff2020-07-15 14:30:28 +02001327 assert_int_equal(LY_SUCCESS, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001328
1329 lyd_free_siblings(tree);
1330
1331 *state = NULL;
1332}
1333
Michal Vasko5b37a352020-03-06 13:38:33 +01001334static void
1335test_state(void **state)
1336{
Michal Vaskocc048b22020-03-27 15:52:38 +01001337 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001338
1339 const char *data;
1340 struct lyd_node *tree;
1341
1342 data =
1343 "<cont xmlns=\"urn:tests:h\">"
1344 "<cont2>"
1345 "<l>val</l>"
1346 "</cont2>"
1347 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001348 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001349 assert_null(tree);
1350 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1351
Michal Vasko63f3d842020-07-08 10:10:14 +02001352 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001353 assert_null(tree);
1354 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1355
Michal Vasko63f3d842020-07-08 10:10:14 +02001356 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001357 assert_non_null(tree);
1358
Michal Vasko3a41dff2020-07-15 14:30:28 +02001359 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, NULL));
Michal Vasko5b37a352020-03-06 13:38:33 +01001360 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1361
1362 lyd_free_siblings(tree);
1363
1364 *state = NULL;
1365}
1366
Michal Vaskocc048b22020-03-27 15:52:38 +01001367static void
1368test_must(void **state)
1369{
1370 *state = test_must;
1371
1372 const char *data;
1373 struct lyd_node *tree;
1374
1375 data =
1376 "<cont xmlns=\"urn:tests:i\">"
1377 "<l>wrong</l>"
1378 "<l2>val</l2>"
1379 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001380 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocc048b22020-03-27 15:52:38 +01001381 assert_null(tree);
1382 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1383
1384 data =
1385 "<cont xmlns=\"urn:tests:i\">"
1386 "<l>right</l>"
1387 "<l2>val</l2>"
1388 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001389 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocc048b22020-03-27 15:52:38 +01001390 assert_non_null(tree);
1391 lyd_free_tree(tree);
1392
1393 *state = NULL;
1394}
1395
Michal Vaskofea12c62020-03-30 11:00:15 +02001396static void
1397test_action(void **state)
1398{
1399 *state = test_action;
1400
1401 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001402 struct ly_in *in;
Michal Vaskofea12c62020-03-30 11:00:15 +02001403 struct lyd_node *tree, *op_tree;
1404 const struct lys_module *mod;
1405
1406 data =
1407 "<cont xmlns=\"urn:tests:j\">"
1408 "<l1>"
1409 "<k>val1</k>"
1410 "<act>"
1411 "<lf2>target</lf2>"
1412 "</act>"
1413 "</l1>"
1414 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001415 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1416 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &op_tree, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001417 assert_non_null(op_tree);
1418
1419 /* missing leafref */
Michal Vasko8104fd42020-07-13 11:09:51 +02001420 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_RPC, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +02001421 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value."
1422 " /j:cont/l1[k='val1']/act/lf2");
Michal Vasko63f3d842020-07-08 10:10:14 +02001423 ly_in_free(in, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001424
1425 data =
1426 "<cont xmlns=\"urn:tests:j\">"
1427 "<lf1>not true</lf1>"
1428 "</cont>"
1429 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001430 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001431 assert_non_null(tree);
1432
1433 /* disabled if-feature */
Michal Vasko8104fd42020-07-13 11:09:51 +02001434 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001435 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1436
1437 mod = ly_ctx_get_module_latest(ctx, "j");
1438 assert_non_null(mod);
1439 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1440
1441 /* input must false */
Michal Vasko8104fd42020-07-13 11:09:51 +02001442 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001443 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1444
1445 lyd_free_siblings(tree);
1446 data =
1447 "<cont xmlns=\"urn:tests:j\">"
1448 "<lf1>true</lf1>"
1449 "</cont>"
1450 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001451 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001452 assert_non_null(tree);
1453
1454 /* success */
Michal Vasko8104fd42020-07-13 11:09:51 +02001455 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001456
Michal Vaskocb7526d2020-03-30 15:08:26 +02001457 lys_feature_disable(mod, "feat1");
1458 lyd_free_tree(op_tree);
1459 lyd_free_siblings(tree);
1460
1461 *state = NULL;
1462}
1463
1464static void
1465test_reply(void **state)
1466{
1467 *state = test_reply;
1468
1469 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001470 struct ly_in *in;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001471 struct lyd_node *tree, *op_tree, *request;
1472 const struct lys_module *mod;
1473
1474 data =
1475 "<cont xmlns=\"urn:tests:j\">"
1476 "<l1>"
1477 "<k>val1</k>"
1478 "<act>"
1479 "<lf2>target</lf2>"
1480 "</act>"
1481 "</l1>"
1482 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001483 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1484 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &request, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001485 assert_non_null(request);
Michal Vasko63f3d842020-07-08 10:10:14 +02001486 ly_in_free(in, 0);
1487
Michal Vaskocb7526d2020-03-30 15:08:26 +02001488 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001489 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1490 assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_XML, &op_tree, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001491 lyd_free_all(request);
1492 assert_non_null(op_tree);
Michal Vasko63f3d842020-07-08 10:10:14 +02001493 ly_in_free(in, 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001494
1495 /* missing leafref */
Michal Vasko8104fd42020-07-13 11:09:51 +02001496 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +02001497 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value."
1498 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001499
1500 data =
1501 "<cont xmlns=\"urn:tests:j\">"
1502 "<lf1>not true</lf1>"
1503 "</cont>"
1504 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001505 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001506 assert_non_null(tree);
1507
1508 /* disabled if-feature */
Michal Vasko8104fd42020-07-13 11:09:51 +02001509 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001510 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1511
1512 mod = ly_ctx_get_module_latest(ctx, "j");
1513 assert_non_null(mod);
1514 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1515
1516 /* input must false */
Michal Vasko8104fd42020-07-13 11:09:51 +02001517 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001518 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1519
1520 lyd_free_siblings(tree);
1521 data =
1522 "<cont xmlns=\"urn:tests:j\">"
1523 "<lf1>true2</lf1>"
1524 "</cont>"
1525 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001526 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001527 assert_non_null(tree);
1528
1529 /* success */
Michal Vasko8104fd42020-07-13 11:09:51 +02001530 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001531
1532 lys_feature_disable(mod, "feat1");
Michal Vaskofea12c62020-03-30 11:00:15 +02001533 lyd_free_tree(op_tree);
1534 lyd_free_siblings(tree);
1535
1536 *state = NULL;
1537}
1538
Michal Vaskocde73ac2019-11-14 16:10:27 +01001539int main(void)
1540{
1541 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001542 cmocka_unit_test_teardown(test_when, teardown_s),
1543 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1544 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001545 cmocka_unit_test_teardown(test_unique, teardown_s),
1546 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001547 cmocka_unit_test_teardown(test_dup, teardown_s),
1548 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001549 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001550 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001551 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001552 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001553 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001554 };
1555
Michal Vaskoacd83e72020-02-04 14:12:01 +01001556 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001557}