blob: c6cf3c943b199cc216063aa878ab3cfce0180f61 [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 Vaskocde73ac2019-11-14 16:10:27 +0100326
327#if ENABLE_LOGGER_CHECKING
328 ly_set_log_clb(logger, 1);
329#endif
330
Michal Vaskof03ed032020-03-04 13:31:44 +0100331 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
332 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100333 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100334 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100335 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100336 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100337 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
338 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100339 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100340
341 return 0;
342}
343
344static int
345teardown(void **state)
346{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100347 (void)state;
348 ly_ctx_destroy(ctx, NULL);
349 ctx = NULL;
350
351 return 0;
352}
353
354static int
355teardown_s(void **state)
356{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100357#if ENABLE_LOGGER_CHECKING
358 if (*state) {
359 fprintf(stderr, "%s\n", logbuf);
360 }
361#else
362 (void) state; /* unused */
363#endif
364
Michal Vaskocde73ac2019-11-14 16:10:27 +0100365 return 0;
366}
367
368void
369logbuf_clean(void)
370{
371 logbuf[0] = '\0';
372}
373
374#if ENABLE_LOGGER_CHECKING
375# define logbuf_assert(str) assert_string_equal(logbuf, str)
376#else
377# define logbuf_assert(str)
378#endif
379
380static void
381test_when(void **state)
382{
383 *state = test_when;
384
385 const char *data;
386 struct lyd_node *tree;
387
388 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100389 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100390 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100391 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100392
393 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 +0100394 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100395 assert_non_null(tree);
396 assert_string_equal("c", tree->next->schema->name);
397 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
398 lyd_free_all(tree);
399
400 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 +0100401 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100402 assert_non_null(tree);
403 assert_string_equal("a", lyd_node_children(tree)->schema->name);
404 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
405 assert_string_equal("c", tree->next->schema->name);
406 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
407 lyd_free_all(tree);
408
409 *state = NULL;
410}
411
Michal Vaskoa3881362020-01-21 15:57:35 +0100412static void
413test_mandatory(void **state)
414{
415 *state = test_mandatory;
416
417 const char *data;
418 struct lyd_node *tree;
419
420 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100421 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100422 assert_null(tree);
423 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
424
425 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100426 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100427 assert_null(tree);
428 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
429
430 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100431 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100432 assert_null(tree);
433 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
434
435 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100436 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100437 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100438 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100439
440 *state = NULL;
441}
442
Michal Vaskoacd83e72020-02-04 14:12:01 +0100443static void
444test_minmax(void **state)
445{
446 *state = test_minmax;
447
448 const char *data;
449 struct lyd_node *tree;
450
451 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100452 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100453 assert_null(tree);
454 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
455
456 data =
457 "<l xmlns=\"urn:tests:c\">val1</l>"
458 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100459 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100460 assert_null(tree);
461 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
462
463 data =
464 "<l xmlns=\"urn:tests:c\">val1</l>"
465 "<l xmlns=\"urn:tests:c\">val2</l>"
466 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100467 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100468 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100469 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100470
471 data =
472 "<l xmlns=\"urn:tests:c\">val1</l>"
473 "<l xmlns=\"urn:tests:c\">val2</l>"
474 "<l xmlns=\"urn:tests:c\">val3</l>"
475 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
476 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
477 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
478 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
479 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100480 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100481 assert_null(tree);
482 logbuf_assert("Too many \"lt\" instances. /c:lt");
483
484 *state = NULL;
485}
486
Michal Vasko14654712020-02-06 08:35:21 +0100487static void
488test_unique(void **state)
489{
490 *state = test_unique;
491
492 const char *data;
493 struct lyd_node *tree;
494
495 data =
496 "<lt xmlns=\"urn:tests:d\">"
497 "<k>val1</k>"
498 "<l1>same</l1>"
499 "</lt>"
500 "<lt xmlns=\"urn:tests:d\">"
501 "<k>val2</k>"
502 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100503 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100504 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100505 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100506
507 data =
508 "<lt xmlns=\"urn:tests:d\">"
509 "<k>val1</k>"
510 "<l1>same</l1>"
511 "</lt>"
512 "<lt xmlns=\"urn:tests:d\">"
513 "<k>val2</k>"
514 "<l1>not-same</l1>"
515 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100516 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100517 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100518 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100519
520 data =
521 "<lt xmlns=\"urn:tests:d\">"
522 "<k>val1</k>"
523 "<l1>same</l1>"
524 "</lt>"
525 "<lt xmlns=\"urn:tests:d\">"
526 "<k>val2</k>"
527 "<l1>same</l1>"
528 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100529 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100530 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100531 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 +0100532
533 /* now try with more instances */
534 data =
535 "<lt xmlns=\"urn:tests:d\">"
536 "<k>val1</k>"
537 "<l1>1</l1>"
538 "</lt>"
539 "<lt xmlns=\"urn:tests:d\">"
540 "<k>val2</k>"
541 "<l1>2</l1>"
542 "</lt>"
543 "<lt xmlns=\"urn:tests:d\">"
544 "<k>val3</k>"
545 "<l1>3</l1>"
546 "</lt>"
547 "<lt xmlns=\"urn:tests:d\">"
548 "<k>val4</k>"
549 "<l1>4</l1>"
550 "</lt>"
551 "<lt xmlns=\"urn:tests:d\">"
552 "<k>val5</k>"
553 "<l1>5</l1>"
554 "</lt>"
555 "<lt xmlns=\"urn:tests:d\">"
556 "<k>val6</k>"
557 "<l1>6</l1>"
558 "</lt>"
559 "<lt xmlns=\"urn:tests:d\">"
560 "<k>val7</k>"
561 "<l1>7</l1>"
562 "</lt>"
563 "<lt xmlns=\"urn:tests:d\">"
564 "<k>val8</k>"
565 "<l1>8</l1>"
566 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100567 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100568 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100569 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100570
571 data =
572 "<lt xmlns=\"urn:tests:d\">"
573 "<k>val1</k>"
574 "<l1>1</l1>"
575 "</lt>"
576 "<lt xmlns=\"urn:tests:d\">"
577 "<k>val2</k>"
578 "<l1>2</l1>"
579 "</lt>"
580 "<lt xmlns=\"urn:tests:d\">"
581 "<k>val3</k>"
582 "<l1>3</l1>"
583 "</lt>"
584 "<lt xmlns=\"urn:tests:d\">"
585 "<k>val4</k>"
586 "</lt>"
587 "<lt xmlns=\"urn:tests:d\">"
588 "<k>val5</k>"
589 "<l1>5</l1>"
590 "</lt>"
591 "<lt xmlns=\"urn:tests:d\">"
592 "<k>val6</k>"
593 "<l1>6</l1>"
594 "</lt>"
595 "<lt xmlns=\"urn:tests:d\">"
596 "<k>val7</k>"
597 "</lt>"
598 "<lt xmlns=\"urn:tests:d\">"
599 "<k>val8</k>"
600 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100601 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100602 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100603 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100604
605 data =
606 "<lt xmlns=\"urn:tests:d\">"
607 "<k>val1</k>"
608 "<l1>1</l1>"
609 "</lt>"
610 "<lt xmlns=\"urn:tests:d\">"
611 "<k>val2</k>"
612 "<l1>2</l1>"
613 "</lt>"
614 "<lt xmlns=\"urn:tests:d\">"
615 "<k>val3</k>"
616 "</lt>"
617 "<lt xmlns=\"urn:tests:d\">"
618 "<k>val4</k>"
619 "<l1>4</l1>"
620 "</lt>"
621 "<lt xmlns=\"urn:tests:d\">"
622 "<k>val5</k>"
623 "</lt>"
624 "<lt xmlns=\"urn:tests:d\">"
625 "<k>val6</k>"
626 "</lt>"
627 "<lt xmlns=\"urn:tests:d\">"
628 "<k>val7</k>"
629 "<l1>2</l1>"
630 "</lt>"
631 "<lt xmlns=\"urn:tests:d\">"
632 "<k>val8</k>"
633 "<l1>8</l1>"
634 "</lt>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100635 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100636 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100637 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 +0100638
639 *state = NULL;
640}
641
642static void
643test_unique_nested(void **state)
644{
645 *state = test_unique_nested;
646
647 const char *data;
648 struct lyd_node *tree;
649
650 /* nested list uniquest are compared only with instances in the same parent list instance */
651 data =
652 "<lt2 xmlns=\"urn:tests:d\">"
653 "<k>val1</k>"
654 "<cont>"
655 "<l2>1</l2>"
656 "</cont>"
657 "<l4>1</l4>"
658 "</lt2>"
659 "<lt2 xmlns=\"urn:tests:d\">"
660 "<k>val2</k>"
661 "<cont>"
662 "<l2>2</l2>"
663 "</cont>"
664 "<l4>2</l4>"
665 "<lt3>"
666 "<kk>val1</kk>"
667 "<l3>1</l3>"
668 "</lt3>"
669 "<lt3>"
670 "<kk>val2</kk>"
671 "<l3>2</l3>"
672 "</lt3>"
673 "</lt2>"
674 "<lt2 xmlns=\"urn:tests:d\">"
675 "<k>val3</k>"
676 "<cont>"
677 "<l2>3</l2>"
678 "</cont>"
679 "<l4>3</l4>"
680 "<lt3>"
681 "<kk>val1</kk>"
682 "<l3>2</l3>"
683 "</lt3>"
684 "</lt2>"
685 "<lt2 xmlns=\"urn:tests:d\">"
686 "<k>val4</k>"
687 "<cont>"
688 "<l2>4</l2>"
689 "</cont>"
690 "<l4>4</l4>"
691 "<lt3>"
692 "<kk>val1</kk>"
693 "<l3>3</l3>"
694 "</lt3>"
695 "</lt2>"
696 "<lt2 xmlns=\"urn:tests:d\">"
697 "<k>val5</k>"
698 "<cont>"
699 "<l2>5</l2>"
700 "</cont>"
701 "<l4>5</l4>"
702 "<lt3>"
703 "<kk>val1</kk>"
704 "<l3>3</l3>"
705 "</lt3>"
706 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100707 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 +0100708 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100709 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100710
711 data =
712 "<lt2 xmlns=\"urn:tests:d\">"
713 "<k>val1</k>"
714 "<cont>"
715 "<l2>1</l2>"
716 "</cont>"
717 "<l4>1</l4>"
718 "</lt2>"
719 "<lt2 xmlns=\"urn:tests:d\">"
720 "<k>val2</k>"
721 "<cont>"
722 "<l2>2</l2>"
723 "</cont>"
724 "<lt3>"
725 "<kk>val1</kk>"
726 "<l3>1</l3>"
727 "</lt3>"
728 "<lt3>"
729 "<kk>val2</kk>"
730 "<l3>2</l3>"
731 "</lt3>"
732 "<lt3>"
733 "<kk>val3</kk>"
734 "<l3>1</l3>"
735 "</lt3>"
736 "</lt2>"
737 "<lt2 xmlns=\"urn:tests:d\">"
738 "<k>val3</k>"
739 "<cont>"
740 "<l2>3</l2>"
741 "</cont>"
742 "<l4>1</l4>"
743 "<lt3>"
744 "<kk>val1</kk>"
745 "<l3>2</l3>"
746 "</lt3>"
747 "</lt2>"
748 "<lt2 xmlns=\"urn:tests:d\">"
749 "<k>val4</k>"
750 "<cont>"
751 "<l2>4</l2>"
752 "</cont>"
753 "<lt3>"
754 "<kk>val1</kk>"
755 "<l3>3</l3>"
756 "</lt3>"
757 "</lt2>"
758 "<lt2 xmlns=\"urn:tests:d\">"
759 "<k>val5</k>"
760 "<cont>"
761 "<l2>5</l2>"
762 "</cont>"
763 "<lt3>"
764 "<kk>val1</kk>"
765 "<l3>3</l3>"
766 "</lt3>"
767 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100768 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100769 assert_null(tree);
770 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 +0100771 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100772
773 data =
774 "<lt2 xmlns=\"urn:tests:d\">"
775 "<k>val1</k>"
776 "<cont>"
777 "<l2>1</l2>"
778 "</cont>"
779 "<l4>1</l4>"
780 "</lt2>"
781 "<lt2 xmlns=\"urn:tests:d\">"
782 "<k>val2</k>"
783 "<cont>"
784 "<l2>2</l2>"
785 "</cont>"
786 "<l4>2</l4>"
787 "</lt2>"
788 "<lt2 xmlns=\"urn:tests:d\">"
789 "<k>val3</k>"
790 "<cont>"
791 "<l2>3</l2>"
792 "</cont>"
793 "<l4>3</l4>"
794 "</lt2>"
795 "<lt2 xmlns=\"urn:tests:d\">"
796 "<k>val4</k>"
797 "<cont>"
798 "<l2>2</l2>"
799 "</cont>"
800 "<l4>2</l4>"
801 "</lt2>"
802 "<lt2 xmlns=\"urn:tests:d\">"
803 "<k>val5</k>"
804 "<cont>"
805 "<l2>5</l2>"
806 "</cont>"
807 "<l4>5</l4>"
808 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100809 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100810 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100811 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 +0100812
813 data =
814 "<lt2 xmlns=\"urn:tests:d\">"
815 "<k>val1</k>"
816 "<cont>"
817 "<l2>1</l2>"
818 "</cont>"
819 "<l4>1</l4>"
820 "<l5>1</l5>"
821 "<l6>1</l6>"
822 "</lt2>"
823 "<lt2 xmlns=\"urn:tests:d\">"
824 "<k>val2</k>"
825 "<cont>"
826 "<l2>2</l2>"
827 "</cont>"
828 "<l4>1</l4>"
829 "<l5>1</l5>"
830 "</lt2>"
831 "<lt2 xmlns=\"urn:tests:d\">"
832 "<k>val3</k>"
833 "<cont>"
834 "<l2>3</l2>"
835 "</cont>"
836 "<l4>1</l4>"
837 "<l5>3</l5>"
838 "<l6>3</l6>"
839 "</lt2>"
840 "<lt2 xmlns=\"urn:tests:d\">"
841 "<k>val4</k>"
842 "<cont>"
843 "<l2>4</l2>"
844 "</cont>"
845 "<l4>1</l4>"
846 "<l6>1</l6>"
847 "</lt2>"
848 "<lt2 xmlns=\"urn:tests:d\">"
849 "<k>val5</k>"
850 "<cont>"
851 "<l2>5</l2>"
852 "</cont>"
853 "<l4>1</l4>"
854 "<l5>3</l5>"
855 "<l6>3</l6>"
856 "</lt2>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100857 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100858 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100859 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 +0100860
861 *state = NULL;
862}
863
Michal Vaskof03ed032020-03-04 13:31:44 +0100864static void
865test_dup(void **state)
866{
867 *state = test_dup;
868
869 const char *data;
870 struct lyd_node *tree;
871
872 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
873 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
874 assert_null(tree);
875 logbuf_assert("Duplicate instance of \"d\". /e:d");
876
877 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>";
878 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
879 assert_null(tree);
880 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
881
882 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
883 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
884 assert_null(tree);
885 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
886
887 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
888 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
889 assert_null(tree);
890 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
891
892 /* same tests again but using hashes */
893 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>";
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 \"d\". /e:cont/d");
897
898 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
899 "<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>";
900 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
901 assert_null(tree);
902 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
903
904 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
905 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
906 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
907 assert_null(tree);
908 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
909
910 /* cases */
911 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>";
912 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
913 assert_null(tree);
914 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
915
916 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>";
917 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
918 assert_null(tree);
919 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
920
921 *state = NULL;
922}
923
924static void
925test_defaults(void **state)
926{
927 *state = test_defaults;
928
Michal Vaskof03ed032020-03-04 13:31:44 +0100929 char *str;
930 struct lyd_node *tree, *node;
931 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
932
Michal Vaskob1b5c262020-03-05 14:29:47 +0100933 /* get defaults */
934 tree = NULL;
935 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100936 assert_non_null(tree);
937
938 /* check all defaults exist */
939 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
940 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +0100941 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
942 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
943 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
944 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
945 "<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 +0100946 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
947 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
948 "<ll1 ncwd:default=\"true\">def1</ll1>"
949 "<ll1 ncwd:default=\"true\">def2</ll1>"
950 "<ll1 ncwd:default=\"true\">def3</ll1>"
951 "<d ncwd:default=\"true\">15</d>"
952 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
953 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
954 "</cont>");
Michal Vaskof03ed032020-03-04 13:31:44 +0100955 free(str);
956
957 /* create another explicit case and validate */
958 node = lyd_new_term(NULL, mod, "l", "value");
959 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100960 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
961 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100962
963 /* check data tree */
964 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
965 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100966 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
967 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
968 "<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 +0100969 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
970 "<ll1 ncwd:default=\"true\">def1</ll1>"
971 "<ll1 ncwd:default=\"true\">def2</ll1>"
972 "<ll1 ncwd:default=\"true\">def3</ll1>"
973 "<d ncwd:default=\"true\">15</d>"
974 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
975 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
976 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +0100977 "<l xmlns=\"urn:tests:f\">value</l>");
978 free(str);
979
980 /* create explicit leaf-list and leaf and validate */
981 node = lyd_new_term(NULL, mod, "d", "15");
982 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100983 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100984 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
985 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100986 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
987 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100988
989 /* check data tree */
990 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
991 assert_string_equal(str,
992 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
993 "<ll1 ncwd:default=\"true\">def1</ll1>"
994 "<ll1 ncwd:default=\"true\">def2</ll1>"
995 "<ll1 ncwd:default=\"true\">def3</ll1>"
996 "<d ncwd:default=\"true\">15</d>"
997 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
998 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
999 "</cont>"
1000 "<l xmlns=\"urn:tests:f\">value</l>"
1001 "<d xmlns=\"urn:tests:f\">15</d>"
1002 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1003 free(str);
1004
Michal Vaskob1b5c262020-03-05 14:29:47 +01001005 /* create first explicit container, which should become implicit */
1006 node = lyd_new_inner(NULL, mod, "cont");
1007 assert_non_null(node);
1008 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1009 tree = tree->prev;
1010 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1011
1012 /* check data tree */
1013 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1014 assert_string_equal(str,
1015 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1016 "<ll1 ncwd:default=\"true\">def1</ll1>"
1017 "<ll1 ncwd:default=\"true\">def2</ll1>"
1018 "<ll1 ncwd:default=\"true\">def3</ll1>"
1019 "<d ncwd:default=\"true\">15</d>"
1020 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1021 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1022 "</cont>"
1023 "<l xmlns=\"urn:tests:f\">value</l>"
1024 "<d xmlns=\"urn:tests:f\">15</d>"
1025 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1026 free(str);
1027
1028 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1029 node = lyd_new_inner(NULL, mod, "cont");
1030 assert_non_null(node);
1031 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1032 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1033
1034 /* check data tree */
1035 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1036 assert_string_equal(str,
1037 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1038 "<ll1 ncwd:default=\"true\">def1</ll1>"
1039 "<ll1 ncwd:default=\"true\">def2</ll1>"
1040 "<ll1 ncwd:default=\"true\">def3</ll1>"
1041 "<d ncwd:default=\"true\">15</d>"
1042 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1043 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1044 "</cont>"
1045 "<l xmlns=\"urn:tests:f\">value</l>"
1046 "<d xmlns=\"urn:tests:f\">15</d>"
1047 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1048 free(str);
Michal Vaskof03ed032020-03-04 13:31:44 +01001049
1050 /* similar changes for nested defaults */
1051 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1052 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1053 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001054 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001055
1056 /* check data tree */
1057 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1058 assert_string_equal(str,
1059 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1060 "<ll1>def3</ll1>"
1061 "<d>5</d>"
1062 "<ll2>non-dflt</ll2>"
1063 "</cont>"
1064 "<l xmlns=\"urn:tests:f\">value</l>"
1065 "<d xmlns=\"urn:tests:f\">15</d>"
1066 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1067 free(str);
1068
1069 lyd_free_siblings(tree);
1070
1071 *state = NULL;
1072}
1073
Michal Vaskoc193ce92020-03-06 11:04:48 +01001074static void
1075test_iffeature(void **state)
1076{
1077 *state = test_iffeature;
1078
1079 const char *data;
1080 struct lyd_node *tree;
1081 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1082
1083 /* get empty data */
1084 tree = NULL;
1085 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1086 assert_null(tree);
1087
1088 /* disabled by f1 */
1089 data =
1090 "<cont xmlns=\"urn:tests:g\">"
1091 "<d>51</d>"
1092 "</cont>";
1093 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1094 assert_null(tree);
1095 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1096
1097 /* enable f1 */
1098 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1099
1100 /* get data with default container */
1101 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1102 assert_non_null(tree);
1103 lyd_free_siblings(tree);
1104
1105 /* disabled by f2 */
1106 data =
1107 "<cont xmlns=\"urn:tests:g\">"
1108 "<cont2>"
1109 "<e>val</e>"
1110 "</cont2>"
1111 "</cont>";
1112 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1113 assert_null(tree);
1114 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1115
1116 data =
1117 "<cont xmlns=\"urn:tests:g\">"
1118 "<a>val</a>"
1119 "</cont>";
1120 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1121 assert_null(tree);
1122 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1123
1124 /* enable f3 */
1125 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1126
1127 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1128 assert_non_null(tree);
1129 lyd_free_siblings(tree);
1130
1131 /* disabled by f2 */
1132 data =
1133 "<cont xmlns=\"urn:tests:g\">"
1134 "<l>val</l>"
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 \"b\" schema node if-feature. /g:cont/l");
1139
1140 /* enable f2 */
1141 assert_int_equal(lys_feature_enable(mod, "f2"), 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 /* try separate validation */
1148 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1149 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1150 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1151
1152 data =
1153 "<cont xmlns=\"urn:tests:g\">"
1154 "<l>val</l>"
1155 "<d>51</d>"
1156 "<cont2>"
1157 "<e>val</e>"
1158 "</cont2>"
1159 "</cont>";
1160 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
1161 assert_non_null(tree);
1162
1163 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1164 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1165
1166 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1167
1168 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1169 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1170
1171 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1172
1173 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1174
1175 lyd_free_siblings(tree);
1176
1177 *state = NULL;
1178}
1179
Michal Vaskocde73ac2019-11-14 16:10:27 +01001180int main(void)
1181{
1182 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001183 cmocka_unit_test_teardown(test_when, teardown_s),
1184 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1185 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001186 cmocka_unit_test_teardown(test_unique, teardown_s),
1187 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001188 cmocka_unit_test_teardown(test_dup, teardown_s),
1189 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001190 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001191 };
1192
Michal Vaskoacd83e72020-02-04 14:12:01 +01001193 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001194}