blob: 4d4c614a14ee1fd83144c8679292a034c7fa5c93 [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
Michal Vaskof03ed032020-03-04 13:31:44 +010023#include "tests/config.h"
24
Michal Vaskocde73ac2019-11-14 16:10:27 +010025#include "../../src/context.h"
26#include "../../src/tree_data_internal.h"
Michal Vaskof03ed032020-03-04 13:31:44 +010027#include "../../src/printer_data.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31int store = -1; /* negative for infinite logging, positive for limited logging */
32
33struct ly_ctx *ctx; /* context for tests */
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38#if ENABLE_LOGGER_CHECKING
39static void
40logger(LY_LOG_LEVEL level, const char *msg, const char *path)
41{
42 (void) level; /* unused */
43 if (store) {
44 if (path && path[0]) {
45 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
46 } else {
47 strncpy(logbuf, msg, BUFSIZE - 1);
48 }
49 if (store > 0) {
50 --store;
51 }
52 }
53}
54#endif
55
56static int
57setup(void **state)
58{
59 (void) state; /* unused */
60
61 const char *schema_a =
62 "module a {"
63 "namespace urn:tests:a;"
64 "prefix a;"
65 "yang-version 1.1;"
66
67 "container cont {"
68 "leaf a {"
69 "when \"../../c = 'val_c'\";"
70 "type string;"
71 "}"
72 "leaf b {"
73 "type string;"
74 "}"
75 "}"
76 "leaf c {"
77 "when \"/cont/b = 'val_b'\";"
78 "type string;"
79 "}"
80 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010081 const char *schema_b =
82 "module b {"
83 "namespace urn:tests:b;"
84 "prefix b;"
85 "yang-version 1.1;"
86
87 "choice choic {"
88 "mandatory true;"
89 "leaf a {"
90 "type string;"
91 "}"
92 "case b {"
93 "leaf l {"
94 "type string;"
95 "}"
96 "}"
97 "}"
98 "leaf c {"
99 "mandatory true;"
100 "type string;"
101 "}"
102 "leaf d {"
103 "type empty;"
104 "}"
105 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100106 const char *schema_c =
107 "module c {"
108 "namespace urn:tests:c;"
109 "prefix c;"
110 "yang-version 1.1;"
111
112 "choice choic {"
113 "leaf a {"
114 "type string;"
115 "}"
116 "case b {"
117 "leaf-list l {"
118 "min-elements 3;"
119 "type string;"
120 "}"
121 "}"
122 "}"
123 "list lt {"
124 "max-elements 4;"
125 "key \"k\";"
126 "leaf k {"
127 "type string;"
128 "}"
129 "}"
130 "leaf d {"
131 "type empty;"
132 "}"
133 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100134 const char *schema_d =
135 "module d {"
136 "namespace urn:tests:d;"
137 "prefix d;"
138 "yang-version 1.1;"
139
140 "list lt {"
141 "key \"k\";"
142 "unique \"l1\";"
143 "leaf k {"
144 "type string;"
145 "}"
146 "leaf l1 {"
147 "type string;"
148 "}"
149 "}"
150 "list lt2 {"
151 "key \"k\";"
152 "unique \"cont/l2 l4\";"
153 "unique \"l5 l6\";"
154 "leaf k {"
155 "type string;"
156 "}"
157 "container cont {"
158 "leaf l2 {"
159 "type string;"
160 "}"
161 "}"
162 "leaf l4 {"
163 "type string;"
164 "}"
165 "leaf l5 {"
166 "type string;"
167 "}"
168 "leaf l6 {"
169 "type string;"
170 "}"
171 "list lt3 {"
172 "key \"kk\";"
173 "unique \"l3\";"
174 "leaf kk {"
175 "type string;"
176 "}"
177 "leaf l3 {"
178 "type string;"
179 "}"
180 "}"
181 "}"
182 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100183 const char *schema_e =
184 "module e {"
185 "namespace urn:tests:e;"
186 "prefix e;"
187 "yang-version 1.1;"
188
189 "choice choic {"
190 "leaf a {"
191 "type string;"
192 "}"
193 "case b {"
194 "leaf-list l {"
195 "type string;"
196 "}"
197 "}"
198 "}"
199 "list lt {"
200 "key \"k\";"
201 "leaf k {"
202 "type string;"
203 "}"
204 "}"
205 "leaf d {"
206 "type uint32;"
207 "}"
208 "leaf-list ll {"
209 "type string;"
210 "}"
211 "container cont {"
212 "list lt {"
213 "key \"k\";"
214 "leaf k {"
215 "type string;"
216 "}"
217 "}"
218 "leaf d {"
219 "type uint32;"
220 "}"
221 "leaf-list ll {"
222 "type string;"
223 "}"
224 "}"
225 "}";
226 const char *schema_f =
227 "module f {"
228 "namespace urn:tests:f;"
229 "prefix f;"
230 "yang-version 1.1;"
231
232 "choice choic {"
233 "default \"c\";"
234 "leaf a {"
235 "type string;"
236 "}"
237 "case b {"
238 "leaf l {"
239 "type string;"
240 "}"
241 "}"
242 "case c {"
243 "leaf-list ll1 {"
244 "type string;"
245 "default \"def1\";"
246 "default \"def2\";"
247 "default \"def3\";"
248 "}"
249 "}"
250 "}"
251 "leaf d {"
252 "type uint32;"
253 "default 15;"
254 "}"
255 "leaf-list ll2 {"
256 "type string;"
257 "default \"dflt1\";"
258 "default \"dflt2\";"
259 "}"
260 "container cont {"
261 "choice choic {"
262 "default \"c\";"
263 "leaf a {"
264 "type string;"
265 "}"
266 "case b {"
267 "leaf l {"
268 "type string;"
269 "}"
270 "}"
271 "case c {"
272 "leaf-list ll1 {"
273 "type string;"
274 "default \"def1\";"
275 "default \"def2\";"
276 "default \"def3\";"
277 "}"
278 "}"
279 "}"
280 "leaf d {"
281 "type uint32;"
282 "default 15;"
283 "}"
284 "leaf-list ll2 {"
285 "type string;"
286 "default \"dflt1\";"
287 "default \"dflt2\";"
288 "}"
289 "}"
290 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100291 const char *schema_g =
292 "module g {"
293 "namespace urn:tests:g;"
294 "prefix g;"
295 "yang-version 1.1;"
296
297 "feature f1;"
298 "feature f2;"
299 "feature f3;"
300
301 "container cont {"
302 "if-feature \"f1\";"
303 "choice choic {"
304 "if-feature \"f2 or f3\";"
305 "leaf a {"
306 "type string;"
307 "}"
308 "case b {"
309 "if-feature \"f2 and f1\";"
310 "leaf l {"
311 "type string;"
312 "}"
313 "}"
314 "}"
315 "leaf d {"
316 "type uint32;"
317 "}"
318 "container cont2 {"
319 "if-feature \"f2\";"
320 "leaf e {"
321 "type string;"
322 "}"
323 "}"
324 "}"
325 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100326 const char *schema_h =
327 "module h {"
328 "namespace urn:tests:h;"
329 "prefix h;"
330 "yang-version 1.1;"
331
332 "container cont {"
333 "container cont2 {"
334 "config false;"
335 "leaf l {"
336 "type string;"
337 "}"
338 "}"
339 "}"
340 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100341
342#if ENABLE_LOGGER_CHECKING
343 ly_set_log_clb(logger, 1);
344#endif
345
Michal Vaskof03ed032020-03-04 13:31:44 +0100346 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
347 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100348 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100349 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100350 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100351 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100352 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
353 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100354 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100355 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100356
357 return 0;
358}
359
360static int
361teardown(void **state)
362{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100363 (void)state;
364 ly_ctx_destroy(ctx, NULL);
365 ctx = NULL;
366
367 return 0;
368}
369
370static int
371teardown_s(void **state)
372{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100373#if ENABLE_LOGGER_CHECKING
374 if (*state) {
375 fprintf(stderr, "%s\n", logbuf);
376 }
377#else
378 (void) state; /* unused */
379#endif
380
Michal Vaskocde73ac2019-11-14 16:10:27 +0100381 return 0;
382}
383
384void
385logbuf_clean(void)
386{
387 logbuf[0] = '\0';
388}
389
390#if ENABLE_LOGGER_CHECKING
391# define logbuf_assert(str) assert_string_equal(logbuf, str)
392#else
393# define logbuf_assert(str)
394#endif
395
396static void
397test_when(void **state)
398{
399 *state = test_when;
400
401 const char *data;
402 struct lyd_node *tree;
403
404 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100405 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100406 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100407 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100408
409 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100410 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100411 assert_non_null(tree);
412 assert_string_equal("c", tree->next->schema->name);
413 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
414 lyd_free_all(tree);
415
416 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100417 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100418 assert_non_null(tree);
419 assert_string_equal("a", lyd_node_children(tree)->schema->name);
420 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
421 assert_string_equal("c", tree->next->schema->name);
422 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
423 lyd_free_all(tree);
424
425 *state = NULL;
426}
427
Michal Vaskoa3881362020-01-21 15:57:35 +0100428static void
429test_mandatory(void **state)
430{
431 *state = test_mandatory;
432
433 const char *data;
434 struct lyd_node *tree;
435
436 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100437 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100438 assert_null(tree);
439 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
440
441 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100442 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100443 assert_null(tree);
444 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
445
446 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100447 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100448 assert_null(tree);
449 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
450
451 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100452 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100453 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100454 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100455
456 *state = NULL;
457}
458
Michal Vaskoacd83e72020-02-04 14:12:01 +0100459static void
460test_minmax(void **state)
461{
462 *state = test_minmax;
463
464 const char *data;
465 struct lyd_node *tree;
466
467 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100468 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100469 assert_null(tree);
470 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
471
472 data =
473 "<l xmlns=\"urn:tests:c\">val1</l>"
474 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100475 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100476 assert_null(tree);
477 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
478
479 data =
480 "<l xmlns=\"urn:tests:c\">val1</l>"
481 "<l xmlns=\"urn:tests:c\">val2</l>"
482 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100483 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100484 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100485 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100486
487 data =
488 "<l xmlns=\"urn:tests:c\">val1</l>"
489 "<l xmlns=\"urn:tests:c\">val2</l>"
490 "<l xmlns=\"urn:tests:c\">val3</l>"
491 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
492 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
493 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
494 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
495 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100496 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100497 assert_null(tree);
498 logbuf_assert("Too many \"lt\" instances. /c:lt");
499
500 *state = NULL;
501}
502
Michal Vasko14654712020-02-06 08:35:21 +0100503static void
504test_unique(void **state)
505{
506 *state = test_unique;
507
508 const char *data;
509 struct lyd_node *tree;
510
511 data =
512 "<lt xmlns=\"urn:tests:d\">"
513 "<k>val1</k>"
514 "<l1>same</l1>"
515 "</lt>"
516 "<lt xmlns=\"urn:tests:d\">"
517 "<k>val2</k>"
518 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100519 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100520 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100521 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100522
523 data =
524 "<lt xmlns=\"urn:tests:d\">"
525 "<k>val1</k>"
526 "<l1>same</l1>"
527 "</lt>"
528 "<lt xmlns=\"urn:tests:d\">"
529 "<k>val2</k>"
530 "<l1>not-same</l1>"
531 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100532 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100533 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100534 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100535
536 data =
537 "<lt xmlns=\"urn:tests:d\">"
538 "<k>val1</k>"
539 "<l1>same</l1>"
540 "</lt>"
541 "<lt xmlns=\"urn:tests:d\">"
542 "<k>val2</k>"
543 "<l1>same</l1>"
544 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100545 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100546 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100547 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 +0100548
549 /* now try with more instances */
550 data =
551 "<lt xmlns=\"urn:tests:d\">"
552 "<k>val1</k>"
553 "<l1>1</l1>"
554 "</lt>"
555 "<lt xmlns=\"urn:tests:d\">"
556 "<k>val2</k>"
557 "<l1>2</l1>"
558 "</lt>"
559 "<lt xmlns=\"urn:tests:d\">"
560 "<k>val3</k>"
561 "<l1>3</l1>"
562 "</lt>"
563 "<lt xmlns=\"urn:tests:d\">"
564 "<k>val4</k>"
565 "<l1>4</l1>"
566 "</lt>"
567 "<lt xmlns=\"urn:tests:d\">"
568 "<k>val5</k>"
569 "<l1>5</l1>"
570 "</lt>"
571 "<lt xmlns=\"urn:tests:d\">"
572 "<k>val6</k>"
573 "<l1>6</l1>"
574 "</lt>"
575 "<lt xmlns=\"urn:tests:d\">"
576 "<k>val7</k>"
577 "<l1>7</l1>"
578 "</lt>"
579 "<lt xmlns=\"urn:tests:d\">"
580 "<k>val8</k>"
581 "<l1>8</l1>"
582 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100583 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100584 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100585 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100586
587 data =
588 "<lt xmlns=\"urn:tests:d\">"
589 "<k>val1</k>"
590 "<l1>1</l1>"
591 "</lt>"
592 "<lt xmlns=\"urn:tests:d\">"
593 "<k>val2</k>"
594 "<l1>2</l1>"
595 "</lt>"
596 "<lt xmlns=\"urn:tests:d\">"
597 "<k>val3</k>"
598 "<l1>3</l1>"
599 "</lt>"
600 "<lt xmlns=\"urn:tests:d\">"
601 "<k>val4</k>"
602 "</lt>"
603 "<lt xmlns=\"urn:tests:d\">"
604 "<k>val5</k>"
605 "<l1>5</l1>"
606 "</lt>"
607 "<lt xmlns=\"urn:tests:d\">"
608 "<k>val6</k>"
609 "<l1>6</l1>"
610 "</lt>"
611 "<lt xmlns=\"urn:tests:d\">"
612 "<k>val7</k>"
613 "</lt>"
614 "<lt xmlns=\"urn:tests:d\">"
615 "<k>val8</k>"
616 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100617 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100618 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100619 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100620
621 data =
622 "<lt xmlns=\"urn:tests:d\">"
623 "<k>val1</k>"
624 "<l1>1</l1>"
625 "</lt>"
626 "<lt xmlns=\"urn:tests:d\">"
627 "<k>val2</k>"
628 "<l1>2</l1>"
629 "</lt>"
630 "<lt xmlns=\"urn:tests:d\">"
631 "<k>val3</k>"
632 "</lt>"
633 "<lt xmlns=\"urn:tests:d\">"
634 "<k>val4</k>"
635 "<l1>4</l1>"
636 "</lt>"
637 "<lt xmlns=\"urn:tests:d\">"
638 "<k>val5</k>"
639 "</lt>"
640 "<lt xmlns=\"urn:tests:d\">"
641 "<k>val6</k>"
642 "</lt>"
643 "<lt xmlns=\"urn:tests:d\">"
644 "<k>val7</k>"
645 "<l1>2</l1>"
646 "</lt>"
647 "<lt xmlns=\"urn:tests:d\">"
648 "<k>val8</k>"
649 "<l1>8</l1>"
650 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100651 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100652 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100653 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 +0100654
655 *state = NULL;
656}
657
658static void
659test_unique_nested(void **state)
660{
661 *state = test_unique_nested;
662
663 const char *data;
664 struct lyd_node *tree;
665
666 /* nested list uniquest are compared only with instances in the same parent list instance */
667 data =
668 "<lt2 xmlns=\"urn:tests:d\">"
669 "<k>val1</k>"
670 "<cont>"
671 "<l2>1</l2>"
672 "</cont>"
673 "<l4>1</l4>"
674 "</lt2>"
675 "<lt2 xmlns=\"urn:tests:d\">"
676 "<k>val2</k>"
677 "<cont>"
678 "<l2>2</l2>"
679 "</cont>"
680 "<l4>2</l4>"
681 "<lt3>"
682 "<kk>val1</kk>"
683 "<l3>1</l3>"
684 "</lt3>"
685 "<lt3>"
686 "<kk>val2</kk>"
687 "<l3>2</l3>"
688 "</lt3>"
689 "</lt2>"
690 "<lt2 xmlns=\"urn:tests:d\">"
691 "<k>val3</k>"
692 "<cont>"
693 "<l2>3</l2>"
694 "</cont>"
695 "<l4>3</l4>"
696 "<lt3>"
697 "<kk>val1</kk>"
698 "<l3>2</l3>"
699 "</lt3>"
700 "</lt2>"
701 "<lt2 xmlns=\"urn:tests:d\">"
702 "<k>val4</k>"
703 "<cont>"
704 "<l2>4</l2>"
705 "</cont>"
706 "<l4>4</l4>"
707 "<lt3>"
708 "<kk>val1</kk>"
709 "<l3>3</l3>"
710 "</lt3>"
711 "</lt2>"
712 "<lt2 xmlns=\"urn:tests:d\">"
713 "<k>val5</k>"
714 "<cont>"
715 "<l2>5</l2>"
716 "</cont>"
717 "<l4>5</l4>"
718 "<lt3>"
719 "<kk>val1</kk>"
720 "<l3>3</l3>"
721 "</lt3>"
722 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100723 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_OPT_STRICT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100724 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100725 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100726
727 data =
728 "<lt2 xmlns=\"urn:tests:d\">"
729 "<k>val1</k>"
730 "<cont>"
731 "<l2>1</l2>"
732 "</cont>"
733 "<l4>1</l4>"
734 "</lt2>"
735 "<lt2 xmlns=\"urn:tests:d\">"
736 "<k>val2</k>"
737 "<cont>"
738 "<l2>2</l2>"
739 "</cont>"
740 "<lt3>"
741 "<kk>val1</kk>"
742 "<l3>1</l3>"
743 "</lt3>"
744 "<lt3>"
745 "<kk>val2</kk>"
746 "<l3>2</l3>"
747 "</lt3>"
748 "<lt3>"
749 "<kk>val3</kk>"
750 "<l3>1</l3>"
751 "</lt3>"
752 "</lt2>"
753 "<lt2 xmlns=\"urn:tests:d\">"
754 "<k>val3</k>"
755 "<cont>"
756 "<l2>3</l2>"
757 "</cont>"
758 "<l4>1</l4>"
759 "<lt3>"
760 "<kk>val1</kk>"
761 "<l3>2</l3>"
762 "</lt3>"
763 "</lt2>"
764 "<lt2 xmlns=\"urn:tests:d\">"
765 "<k>val4</k>"
766 "<cont>"
767 "<l2>4</l2>"
768 "</cont>"
769 "<lt3>"
770 "<kk>val1</kk>"
771 "<l3>3</l3>"
772 "</lt3>"
773 "</lt2>"
774 "<lt2 xmlns=\"urn:tests:d\">"
775 "<k>val5</k>"
776 "<cont>"
777 "<l2>5</l2>"
778 "</cont>"
779 "<lt3>"
780 "<kk>val1</kk>"
781 "<l3>3</l3>"
782 "</lt3>"
783 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100784 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100785 assert_null(tree);
786 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 +0100787 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100788
789 data =
790 "<lt2 xmlns=\"urn:tests:d\">"
791 "<k>val1</k>"
792 "<cont>"
793 "<l2>1</l2>"
794 "</cont>"
795 "<l4>1</l4>"
796 "</lt2>"
797 "<lt2 xmlns=\"urn:tests:d\">"
798 "<k>val2</k>"
799 "<cont>"
800 "<l2>2</l2>"
801 "</cont>"
802 "<l4>2</l4>"
803 "</lt2>"
804 "<lt2 xmlns=\"urn:tests:d\">"
805 "<k>val3</k>"
806 "<cont>"
807 "<l2>3</l2>"
808 "</cont>"
809 "<l4>3</l4>"
810 "</lt2>"
811 "<lt2 xmlns=\"urn:tests:d\">"
812 "<k>val4</k>"
813 "<cont>"
814 "<l2>2</l2>"
815 "</cont>"
816 "<l4>2</l4>"
817 "</lt2>"
818 "<lt2 xmlns=\"urn:tests:d\">"
819 "<k>val5</k>"
820 "<cont>"
821 "<l2>5</l2>"
822 "</cont>"
823 "<l4>5</l4>"
824 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100825 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100826 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100827 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 +0100828
829 data =
830 "<lt2 xmlns=\"urn:tests:d\">"
831 "<k>val1</k>"
832 "<cont>"
833 "<l2>1</l2>"
834 "</cont>"
835 "<l4>1</l4>"
836 "<l5>1</l5>"
837 "<l6>1</l6>"
838 "</lt2>"
839 "<lt2 xmlns=\"urn:tests:d\">"
840 "<k>val2</k>"
841 "<cont>"
842 "<l2>2</l2>"
843 "</cont>"
844 "<l4>1</l4>"
845 "<l5>1</l5>"
846 "</lt2>"
847 "<lt2 xmlns=\"urn:tests:d\">"
848 "<k>val3</k>"
849 "<cont>"
850 "<l2>3</l2>"
851 "</cont>"
852 "<l4>1</l4>"
853 "<l5>3</l5>"
854 "<l6>3</l6>"
855 "</lt2>"
856 "<lt2 xmlns=\"urn:tests:d\">"
857 "<k>val4</k>"
858 "<cont>"
859 "<l2>4</l2>"
860 "</cont>"
861 "<l4>1</l4>"
862 "<l6>1</l6>"
863 "</lt2>"
864 "<lt2 xmlns=\"urn:tests:d\">"
865 "<k>val5</k>"
866 "<cont>"
867 "<l2>5</l2>"
868 "</cont>"
869 "<l4>1</l4>"
870 "<l5>3</l5>"
871 "<l6>3</l6>"
872 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100873 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100874 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100875 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 +0100876
877 *state = NULL;
878}
879
Michal Vaskof03ed032020-03-04 13:31:44 +0100880static void
881test_dup(void **state)
882{
883 *state = test_dup;
884
885 const char *data;
886 struct lyd_node *tree;
887
888 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
889 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
890 assert_null(tree);
891 logbuf_assert("Duplicate instance of \"d\". /e:d");
892
893 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>";
894 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
895 assert_null(tree);
896 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
897
898 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
899 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
900 assert_null(tree);
901 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
902
903 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
904 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
905 assert_null(tree);
906 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
907
908 /* same tests again but using hashes */
909 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>";
910 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
911 assert_null(tree);
912 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
913
914 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
915 "<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>";
916 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
917 assert_null(tree);
918 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
919
920 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
921 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
922 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
923 assert_null(tree);
924 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
925
926 /* cases */
927 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>";
928 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
929 assert_null(tree);
930 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
931
932 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>";
933 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
934 assert_null(tree);
935 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
936
937 *state = NULL;
938}
939
940static void
941test_defaults(void **state)
942{
943 *state = test_defaults;
944
Michal Vaskof03ed032020-03-04 13:31:44 +0100945 char *str;
946 struct lyd_node *tree, *node;
947 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
948
Michal Vaskob1b5c262020-03-05 14:29:47 +0100949 /* get defaults */
950 tree = NULL;
951 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100952 assert_non_null(tree);
953
954 /* check all defaults exist */
955 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
956 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +0100957 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
958 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
959 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
960 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
961 "<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 +0100962 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
963 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
964 "<ll1 ncwd:default=\"true\">def1</ll1>"
965 "<ll1 ncwd:default=\"true\">def2</ll1>"
966 "<ll1 ncwd:default=\"true\">def3</ll1>"
967 "<d ncwd:default=\"true\">15</d>"
968 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
969 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
970 "</cont>");
Michal Vaskof03ed032020-03-04 13:31:44 +0100971 free(str);
972
973 /* create another explicit case and validate */
974 node = lyd_new_term(NULL, mod, "l", "value");
975 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100976 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
977 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100978
979 /* check data tree */
980 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
981 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100982 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
983 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
984 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskof03ed032020-03-04 13:31:44 +0100985 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
986 "<ll1 ncwd:default=\"true\">def1</ll1>"
987 "<ll1 ncwd:default=\"true\">def2</ll1>"
988 "<ll1 ncwd:default=\"true\">def3</ll1>"
989 "<d ncwd:default=\"true\">15</d>"
990 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
991 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
992 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +0100993 "<l xmlns=\"urn:tests:f\">value</l>");
994 free(str);
995
996 /* create explicit leaf-list and leaf and validate */
997 node = lyd_new_term(NULL, mod, "d", "15");
998 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100999 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001000 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1001 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001002 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1003 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001004
1005 /* check data tree */
1006 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1007 assert_string_equal(str,
1008 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1009 "<ll1 ncwd:default=\"true\">def1</ll1>"
1010 "<ll1 ncwd:default=\"true\">def2</ll1>"
1011 "<ll1 ncwd:default=\"true\">def3</ll1>"
1012 "<d ncwd:default=\"true\">15</d>"
1013 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1014 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1015 "</cont>"
1016 "<l xmlns=\"urn:tests:f\">value</l>"
1017 "<d xmlns=\"urn:tests:f\">15</d>"
1018 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1019 free(str);
1020
Michal Vaskob1b5c262020-03-05 14:29:47 +01001021 /* create first explicit container, which should become implicit */
1022 node = lyd_new_inner(NULL, mod, "cont");
1023 assert_non_null(node);
1024 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1025 tree = tree->prev;
1026 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1027
1028 /* check data tree */
1029 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1030 assert_string_equal(str,
1031 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1032 "<ll1 ncwd:default=\"true\">def1</ll1>"
1033 "<ll1 ncwd:default=\"true\">def2</ll1>"
1034 "<ll1 ncwd:default=\"true\">def3</ll1>"
1035 "<d ncwd:default=\"true\">15</d>"
1036 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1037 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1038 "</cont>"
1039 "<l xmlns=\"urn:tests:f\">value</l>"
1040 "<d xmlns=\"urn:tests:f\">15</d>"
1041 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1042 free(str);
1043
1044 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1045 node = lyd_new_inner(NULL, mod, "cont");
1046 assert_non_null(node);
1047 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1048 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1049
1050 /* check data tree */
1051 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1052 assert_string_equal(str,
1053 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1054 "<ll1 ncwd:default=\"true\">def1</ll1>"
1055 "<ll1 ncwd:default=\"true\">def2</ll1>"
1056 "<ll1 ncwd:default=\"true\">def3</ll1>"
1057 "<d ncwd:default=\"true\">15</d>"
1058 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1059 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1060 "</cont>"
1061 "<l xmlns=\"urn:tests:f\">value</l>"
1062 "<d xmlns=\"urn:tests:f\">15</d>"
1063 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1064 free(str);
Michal Vaskof03ed032020-03-04 13:31:44 +01001065
1066 /* similar changes for nested defaults */
1067 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1068 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1069 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001070 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001071
1072 /* check data tree */
1073 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1074 assert_string_equal(str,
1075 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1076 "<ll1>def3</ll1>"
1077 "<d>5</d>"
1078 "<ll2>non-dflt</ll2>"
1079 "</cont>"
1080 "<l xmlns=\"urn:tests:f\">value</l>"
1081 "<d xmlns=\"urn:tests:f\">15</d>"
1082 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1083 free(str);
1084
1085 lyd_free_siblings(tree);
1086
1087 *state = NULL;
1088}
1089
Michal Vaskoc193ce92020-03-06 11:04:48 +01001090static void
1091test_iffeature(void **state)
1092{
1093 *state = test_iffeature;
1094
1095 const char *data;
1096 struct lyd_node *tree;
1097 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1098
1099 /* get empty data */
1100 tree = NULL;
1101 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1102 assert_null(tree);
1103
1104 /* disabled by f1 */
1105 data =
1106 "<cont xmlns=\"urn:tests:g\">"
1107 "<d>51</d>"
1108 "</cont>";
1109 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1110 assert_null(tree);
1111 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1112
1113 /* enable f1 */
1114 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1115
1116 /* get data with default container */
1117 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1118 assert_non_null(tree);
1119 lyd_free_siblings(tree);
1120
1121 /* disabled by f2 */
1122 data =
1123 "<cont xmlns=\"urn:tests:g\">"
1124 "<cont2>"
1125 "<e>val</e>"
1126 "</cont2>"
1127 "</cont>";
1128 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1129 assert_null(tree);
1130 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1131
1132 data =
1133 "<cont xmlns=\"urn:tests:g\">"
1134 "<a>val</a>"
1135 "</cont>";
1136 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1137 assert_null(tree);
1138 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1139
1140 /* enable f3 */
1141 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1142
1143 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1144 assert_non_null(tree);
1145 lyd_free_siblings(tree);
1146
1147 /* disabled by f2 */
1148 data =
1149 "<cont xmlns=\"urn:tests:g\">"
1150 "<l>val</l>"
1151 "</cont>";
1152 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1153 assert_null(tree);
1154 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1155
1156 /* enable f2 */
1157 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1158
1159 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1160 assert_non_null(tree);
1161 lyd_free_siblings(tree);
1162
1163 /* try separate validation */
1164 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1165 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1166 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1167
1168 data =
1169 "<cont xmlns=\"urn:tests:g\">"
1170 "<l>val</l>"
1171 "<d>51</d>"
1172 "<cont2>"
1173 "<e>val</e>"
1174 "</cont2>"
1175 "</cont>";
1176 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
1177 assert_non_null(tree);
1178
1179 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1180 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1181
1182 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1183
1184 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1185 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1186
1187 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1188
1189 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1190
1191 lyd_free_siblings(tree);
1192
1193 *state = NULL;
1194}
1195
Michal Vasko5b37a352020-03-06 13:38:33 +01001196static void
1197test_state(void **state)
1198{
1199 *state = test_iffeature;
1200
1201 const char *data;
1202 struct lyd_node *tree;
1203
1204 data =
1205 "<cont xmlns=\"urn:tests:h\">"
1206 "<cont2>"
1207 "<l>val</l>"
1208 "</cont2>"
1209 "</cont>";
1210 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE, &tree));
1211 assert_null(tree);
1212 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1213
1214 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE, &tree));
1215 assert_null(tree);
1216 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1217
1218 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
1219 assert_non_null(tree);
1220
1221 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE));
1222 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1223
1224 lyd_free_siblings(tree);
1225
1226 *state = NULL;
1227}
1228
Michal Vaskocde73ac2019-11-14 16:10:27 +01001229int main(void)
1230{
1231 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001232 cmocka_unit_test_teardown(test_when, teardown_s),
1233 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1234 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001235 cmocka_unit_test_teardown(test_unique, teardown_s),
1236 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001237 cmocka_unit_test_teardown(test_dup, teardown_s),
1238 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001239 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001240 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001241 };
1242
Michal Vaskoacd83e72020-02-04 14:12:01 +01001243 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001244}