blob: d81ea74ff88a8672ee028940e10f5a8f24601e82 [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 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 "leaf-list ll2 {"
225 "type enumeration {"
226 "enum one;"
227 "enum two;"
228 "}"
229 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100230 "}"
231 "}";
232 const char *schema_f =
233 "module f {"
234 "namespace urn:tests:f;"
235 "prefix f;"
236 "yang-version 1.1;"
237
238 "choice choic {"
239 "default \"c\";"
240 "leaf a {"
241 "type string;"
242 "}"
243 "case b {"
244 "leaf l {"
245 "type string;"
246 "}"
247 "}"
248 "case c {"
249 "leaf-list ll1 {"
250 "type string;"
251 "default \"def1\";"
252 "default \"def2\";"
253 "default \"def3\";"
254 "}"
255 "}"
256 "}"
257 "leaf d {"
258 "type uint32;"
259 "default 15;"
260 "}"
261 "leaf-list ll2 {"
262 "type string;"
263 "default \"dflt1\";"
264 "default \"dflt2\";"
265 "}"
266 "container cont {"
267 "choice choic {"
268 "default \"c\";"
269 "leaf a {"
270 "type string;"
271 "}"
272 "case b {"
273 "leaf l {"
274 "type string;"
275 "}"
276 "}"
277 "case c {"
278 "leaf-list ll1 {"
279 "type string;"
280 "default \"def1\";"
281 "default \"def2\";"
282 "default \"def3\";"
283 "}"
284 "}"
285 "}"
286 "leaf d {"
287 "type uint32;"
288 "default 15;"
289 "}"
290 "leaf-list ll2 {"
291 "type string;"
292 "default \"dflt1\";"
293 "default \"dflt2\";"
294 "}"
295 "}"
296 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100297 const char *schema_g =
298 "module g {"
299 "namespace urn:tests:g;"
300 "prefix g;"
301 "yang-version 1.1;"
302
303 "feature f1;"
304 "feature f2;"
305 "feature f3;"
306
307 "container cont {"
308 "if-feature \"f1\";"
309 "choice choic {"
310 "if-feature \"f2 or f3\";"
311 "leaf a {"
312 "type string;"
313 "}"
314 "case b {"
315 "if-feature \"f2 and f1\";"
316 "leaf l {"
317 "type string;"
318 "}"
319 "}"
320 "}"
321 "leaf d {"
322 "type uint32;"
323 "}"
324 "container cont2 {"
325 "if-feature \"f2\";"
326 "leaf e {"
327 "type string;"
328 "}"
329 "}"
330 "}"
331 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100332 const char *schema_h =
333 "module h {"
334 "namespace urn:tests:h;"
335 "prefix h;"
336 "yang-version 1.1;"
337
338 "container cont {"
339 "container cont2 {"
340 "config false;"
341 "leaf l {"
342 "type string;"
343 "}"
344 "}"
345 "}"
346 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100347
348#if ENABLE_LOGGER_CHECKING
349 ly_set_log_clb(logger, 1);
350#endif
351
Michal Vaskof03ed032020-03-04 13:31:44 +0100352 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
353 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100354 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100355 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100356 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100357 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100358 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
359 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100360 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100361 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100362
363 return 0;
364}
365
366static int
367teardown(void **state)
368{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100369 (void)state;
370 ly_ctx_destroy(ctx, NULL);
371 ctx = NULL;
372
373 return 0;
374}
375
376static int
377teardown_s(void **state)
378{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100379#if ENABLE_LOGGER_CHECKING
380 if (*state) {
381 fprintf(stderr, "%s\n", logbuf);
382 }
383#else
384 (void) state; /* unused */
385#endif
386
Michal Vaskocde73ac2019-11-14 16:10:27 +0100387 return 0;
388}
389
390void
391logbuf_clean(void)
392{
393 logbuf[0] = '\0';
394}
395
396#if ENABLE_LOGGER_CHECKING
397# define logbuf_assert(str) assert_string_equal(logbuf, str)
398#else
399# define logbuf_assert(str)
400#endif
401
402static void
403test_when(void **state)
404{
405 *state = test_when;
406
407 const char *data;
408 struct lyd_node *tree;
409
410 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100411 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100412 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100413 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100414
415 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100416 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100417 assert_non_null(tree);
418 assert_string_equal("c", tree->next->schema->name);
419 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
420 lyd_free_all(tree);
421
422 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100423 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100424 assert_non_null(tree);
425 assert_string_equal("a", lyd_node_children(tree)->schema->name);
426 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
427 assert_string_equal("c", tree->next->schema->name);
428 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
429 lyd_free_all(tree);
430
431 *state = NULL;
432}
433
Michal Vaskoa3881362020-01-21 15:57:35 +0100434static void
435test_mandatory(void **state)
436{
437 *state = test_mandatory;
438
439 const char *data;
440 struct lyd_node *tree;
441
442 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100443 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100444 assert_null(tree);
445 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
446
447 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100448 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100449 assert_null(tree);
450 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
451
452 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100453 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100454 assert_null(tree);
455 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
456
457 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100458 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100459 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100460 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100461
462 *state = NULL;
463}
464
Michal Vaskoacd83e72020-02-04 14:12:01 +0100465static void
466test_minmax(void **state)
467{
468 *state = test_minmax;
469
470 const char *data;
471 struct lyd_node *tree;
472
473 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100474 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100475 assert_null(tree);
476 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
477
478 data =
479 "<l xmlns=\"urn:tests:c\">val1</l>"
480 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100481 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100482 assert_null(tree);
483 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
484
485 data =
486 "<l xmlns=\"urn:tests:c\">val1</l>"
487 "<l xmlns=\"urn:tests:c\">val2</l>"
488 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100489 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100490 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100491 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100492
493 data =
494 "<l xmlns=\"urn:tests:c\">val1</l>"
495 "<l xmlns=\"urn:tests:c\">val2</l>"
496 "<l xmlns=\"urn:tests:c\">val3</l>"
497 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
498 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
499 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
500 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
501 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100502 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100503 assert_null(tree);
504 logbuf_assert("Too many \"lt\" instances. /c:lt");
505
506 *state = NULL;
507}
508
Michal Vasko14654712020-02-06 08:35:21 +0100509static void
510test_unique(void **state)
511{
512 *state = test_unique;
513
514 const char *data;
515 struct lyd_node *tree;
516
517 data =
518 "<lt xmlns=\"urn:tests:d\">"
519 "<k>val1</k>"
520 "<l1>same</l1>"
521 "</lt>"
522 "<lt xmlns=\"urn:tests:d\">"
523 "<k>val2</k>"
524 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100525 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100526 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100527 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100528
529 data =
530 "<lt xmlns=\"urn:tests:d\">"
531 "<k>val1</k>"
532 "<l1>same</l1>"
533 "</lt>"
534 "<lt xmlns=\"urn:tests:d\">"
535 "<k>val2</k>"
536 "<l1>not-same</l1>"
537 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100538 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100539 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100540 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100541
542 data =
543 "<lt xmlns=\"urn:tests:d\">"
544 "<k>val1</k>"
545 "<l1>same</l1>"
546 "</lt>"
547 "<lt xmlns=\"urn:tests:d\">"
548 "<k>val2</k>"
549 "<l1>same</l1>"
550 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100551 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100552 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100553 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 +0100554
555 /* now try with more instances */
556 data =
557 "<lt xmlns=\"urn:tests:d\">"
558 "<k>val1</k>"
559 "<l1>1</l1>"
560 "</lt>"
561 "<lt xmlns=\"urn:tests:d\">"
562 "<k>val2</k>"
563 "<l1>2</l1>"
564 "</lt>"
565 "<lt xmlns=\"urn:tests:d\">"
566 "<k>val3</k>"
567 "<l1>3</l1>"
568 "</lt>"
569 "<lt xmlns=\"urn:tests:d\">"
570 "<k>val4</k>"
571 "<l1>4</l1>"
572 "</lt>"
573 "<lt xmlns=\"urn:tests:d\">"
574 "<k>val5</k>"
575 "<l1>5</l1>"
576 "</lt>"
577 "<lt xmlns=\"urn:tests:d\">"
578 "<k>val6</k>"
579 "<l1>6</l1>"
580 "</lt>"
581 "<lt xmlns=\"urn:tests:d\">"
582 "<k>val7</k>"
583 "<l1>7</l1>"
584 "</lt>"
585 "<lt xmlns=\"urn:tests:d\">"
586 "<k>val8</k>"
587 "<l1>8</l1>"
588 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100589 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100590 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100591 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100592
593 data =
594 "<lt xmlns=\"urn:tests:d\">"
595 "<k>val1</k>"
596 "<l1>1</l1>"
597 "</lt>"
598 "<lt xmlns=\"urn:tests:d\">"
599 "<k>val2</k>"
600 "<l1>2</l1>"
601 "</lt>"
602 "<lt xmlns=\"urn:tests:d\">"
603 "<k>val3</k>"
604 "<l1>3</l1>"
605 "</lt>"
606 "<lt xmlns=\"urn:tests:d\">"
607 "<k>val4</k>"
608 "</lt>"
609 "<lt xmlns=\"urn:tests:d\">"
610 "<k>val5</k>"
611 "<l1>5</l1>"
612 "</lt>"
613 "<lt xmlns=\"urn:tests:d\">"
614 "<k>val6</k>"
615 "<l1>6</l1>"
616 "</lt>"
617 "<lt xmlns=\"urn:tests:d\">"
618 "<k>val7</k>"
619 "</lt>"
620 "<lt xmlns=\"urn:tests:d\">"
621 "<k>val8</k>"
622 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100623 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100624 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100625 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100626
627 data =
628 "<lt xmlns=\"urn:tests:d\">"
629 "<k>val1</k>"
630 "<l1>1</l1>"
631 "</lt>"
632 "<lt xmlns=\"urn:tests:d\">"
633 "<k>val2</k>"
634 "<l1>2</l1>"
635 "</lt>"
636 "<lt xmlns=\"urn:tests:d\">"
637 "<k>val3</k>"
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 "</lt>"
646 "<lt xmlns=\"urn:tests:d\">"
647 "<k>val6</k>"
648 "</lt>"
649 "<lt xmlns=\"urn:tests:d\">"
650 "<k>val7</k>"
651 "<l1>2</l1>"
652 "</lt>"
653 "<lt xmlns=\"urn:tests:d\">"
654 "<k>val8</k>"
655 "<l1>8</l1>"
656 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100657 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100658 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100659 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 +0100660
661 *state = NULL;
662}
663
664static void
665test_unique_nested(void **state)
666{
667 *state = test_unique_nested;
668
669 const char *data;
670 struct lyd_node *tree;
671
672 /* nested list uniquest are compared only with instances in the same parent list instance */
673 data =
674 "<lt2 xmlns=\"urn:tests:d\">"
675 "<k>val1</k>"
676 "<cont>"
677 "<l2>1</l2>"
678 "</cont>"
679 "<l4>1</l4>"
680 "</lt2>"
681 "<lt2 xmlns=\"urn:tests:d\">"
682 "<k>val2</k>"
683 "<cont>"
684 "<l2>2</l2>"
685 "</cont>"
686 "<l4>2</l4>"
687 "<lt3>"
688 "<kk>val1</kk>"
689 "<l3>1</l3>"
690 "</lt3>"
691 "<lt3>"
692 "<kk>val2</kk>"
693 "<l3>2</l3>"
694 "</lt3>"
695 "</lt2>"
696 "<lt2 xmlns=\"urn:tests:d\">"
697 "<k>val3</k>"
698 "<cont>"
699 "<l2>3</l2>"
700 "</cont>"
701 "<l4>3</l4>"
702 "<lt3>"
703 "<kk>val1</kk>"
704 "<l3>2</l3>"
705 "</lt3>"
706 "</lt2>"
707 "<lt2 xmlns=\"urn:tests:d\">"
708 "<k>val4</k>"
709 "<cont>"
710 "<l2>4</l2>"
711 "</cont>"
712 "<l4>4</l4>"
713 "<lt3>"
714 "<kk>val1</kk>"
715 "<l3>3</l3>"
716 "</lt3>"
717 "</lt2>"
718 "<lt2 xmlns=\"urn:tests:d\">"
719 "<k>val5</k>"
720 "<cont>"
721 "<l2>5</l2>"
722 "</cont>"
723 "<l4>5</l4>"
724 "<lt3>"
725 "<kk>val1</kk>"
726 "<l3>3</l3>"
727 "</lt3>"
728 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100729 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_OPT_STRICT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100730 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100731 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100732
733 data =
734 "<lt2 xmlns=\"urn:tests:d\">"
735 "<k>val1</k>"
736 "<cont>"
737 "<l2>1</l2>"
738 "</cont>"
739 "<l4>1</l4>"
740 "</lt2>"
741 "<lt2 xmlns=\"urn:tests:d\">"
742 "<k>val2</k>"
743 "<cont>"
744 "<l2>2</l2>"
745 "</cont>"
746 "<lt3>"
747 "<kk>val1</kk>"
748 "<l3>1</l3>"
749 "</lt3>"
750 "<lt3>"
751 "<kk>val2</kk>"
752 "<l3>2</l3>"
753 "</lt3>"
754 "<lt3>"
755 "<kk>val3</kk>"
756 "<l3>1</l3>"
757 "</lt3>"
758 "</lt2>"
759 "<lt2 xmlns=\"urn:tests:d\">"
760 "<k>val3</k>"
761 "<cont>"
762 "<l2>3</l2>"
763 "</cont>"
764 "<l4>1</l4>"
765 "<lt3>"
766 "<kk>val1</kk>"
767 "<l3>2</l3>"
768 "</lt3>"
769 "</lt2>"
770 "<lt2 xmlns=\"urn:tests:d\">"
771 "<k>val4</k>"
772 "<cont>"
773 "<l2>4</l2>"
774 "</cont>"
775 "<lt3>"
776 "<kk>val1</kk>"
777 "<l3>3</l3>"
778 "</lt3>"
779 "</lt2>"
780 "<lt2 xmlns=\"urn:tests:d\">"
781 "<k>val5</k>"
782 "<cont>"
783 "<l2>5</l2>"
784 "</cont>"
785 "<lt3>"
786 "<kk>val1</kk>"
787 "<l3>3</l3>"
788 "</lt3>"
789 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100790 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100791 assert_null(tree);
792 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 +0100793 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100794
795 data =
796 "<lt2 xmlns=\"urn:tests:d\">"
797 "<k>val1</k>"
798 "<cont>"
799 "<l2>1</l2>"
800 "</cont>"
801 "<l4>1</l4>"
802 "</lt2>"
803 "<lt2 xmlns=\"urn:tests:d\">"
804 "<k>val2</k>"
805 "<cont>"
806 "<l2>2</l2>"
807 "</cont>"
808 "<l4>2</l4>"
809 "</lt2>"
810 "<lt2 xmlns=\"urn:tests:d\">"
811 "<k>val3</k>"
812 "<cont>"
813 "<l2>3</l2>"
814 "</cont>"
815 "<l4>3</l4>"
816 "</lt2>"
817 "<lt2 xmlns=\"urn:tests:d\">"
818 "<k>val4</k>"
819 "<cont>"
820 "<l2>2</l2>"
821 "</cont>"
822 "<l4>2</l4>"
823 "</lt2>"
824 "<lt2 xmlns=\"urn:tests:d\">"
825 "<k>val5</k>"
826 "<cont>"
827 "<l2>5</l2>"
828 "</cont>"
829 "<l4>5</l4>"
830 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100831 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100832 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100833 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 +0100834
835 data =
836 "<lt2 xmlns=\"urn:tests:d\">"
837 "<k>val1</k>"
838 "<cont>"
839 "<l2>1</l2>"
840 "</cont>"
841 "<l4>1</l4>"
842 "<l5>1</l5>"
843 "<l6>1</l6>"
844 "</lt2>"
845 "<lt2 xmlns=\"urn:tests:d\">"
846 "<k>val2</k>"
847 "<cont>"
848 "<l2>2</l2>"
849 "</cont>"
850 "<l4>1</l4>"
851 "<l5>1</l5>"
852 "</lt2>"
853 "<lt2 xmlns=\"urn:tests:d\">"
854 "<k>val3</k>"
855 "<cont>"
856 "<l2>3</l2>"
857 "</cont>"
858 "<l4>1</l4>"
859 "<l5>3</l5>"
860 "<l6>3</l6>"
861 "</lt2>"
862 "<lt2 xmlns=\"urn:tests:d\">"
863 "<k>val4</k>"
864 "<cont>"
865 "<l2>4</l2>"
866 "</cont>"
867 "<l4>1</l4>"
868 "<l6>1</l6>"
869 "</lt2>"
870 "<lt2 xmlns=\"urn:tests:d\">"
871 "<k>val5</k>"
872 "<cont>"
873 "<l2>5</l2>"
874 "</cont>"
875 "<l4>1</l4>"
876 "<l5>3</l5>"
877 "<l6>3</l6>"
878 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100879 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100880 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100881 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 +0100882
883 *state = NULL;
884}
885
Michal Vaskof03ed032020-03-04 13:31:44 +0100886static void
887test_dup(void **state)
888{
889 *state = test_dup;
890
891 const char *data;
892 struct lyd_node *tree;
893
894 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100895 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100896 assert_null(tree);
897 logbuf_assert("Duplicate instance of \"d\". /e:d");
898
899 data = "<lt xmlns=\"urn:tests:e\"><k>A</k></lt><lt xmlns=\"urn:tests:e\"><k>B</k></lt><lt xmlns=\"urn:tests:e\"><k>A</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100900 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100901 assert_null(tree);
902 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
903
904 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100905 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100906 assert_null(tree);
907 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
908
909 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100910 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100911 assert_null(tree);
912 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
913
914 /* same tests again but using hashes */
915 data = "<cont xmlns=\"urn:tests:e\"><d>25</d><d>50</d><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100916 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100917 assert_null(tree);
918 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
919
920 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
921 "<lt><k>a</k></lt><lt><k>b</k></lt><lt><k>c</k></lt><lt><k>d</k></lt><lt><k>c</k></lt></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100922 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100923 assert_null(tree);
924 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
925
926 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
927 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100928 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100929 assert_null(tree);
930 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
931
932 /* cases */
933 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><l xmlns=\"urn:tests:e\">b</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100934 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100935 assert_null(tree);
936 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
937
938 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><a xmlns=\"urn:tests:e\">aa</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100939 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100940 assert_null(tree);
941 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
942
943 *state = NULL;
944}
945
946static void
947test_defaults(void **state)
948{
949 *state = test_defaults;
950
Michal Vaskof03ed032020-03-04 13:31:44 +0100951 char *str;
952 struct lyd_node *tree, *node;
953 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
954
Michal Vaskob1b5c262020-03-05 14:29:47 +0100955 /* get defaults */
956 tree = NULL;
957 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100958 assert_non_null(tree);
959
960 /* check all defaults exist */
961 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
962 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +0100963 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
964 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
965 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
966 "<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>"
Michal Vaskob1b5c262020-03-05 14:29:47 +0100968 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
969 "<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 free(str);
978
979 /* create another explicit case and validate */
980 node = lyd_new_term(NULL, mod, "l", "value");
981 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100982 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
983 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +0100984
985 /* check data tree */
986 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
987 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100988 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
989 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
990 "<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 +0100991 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
992 "<ll1 ncwd:default=\"true\">def1</ll1>"
993 "<ll1 ncwd:default=\"true\">def2</ll1>"
994 "<ll1 ncwd:default=\"true\">def3</ll1>"
995 "<d ncwd:default=\"true\">15</d>"
996 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
997 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
998 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +0100999 "<l xmlns=\"urn:tests:f\">value</l>");
1000 free(str);
1001
1002 /* create explicit leaf-list and leaf and validate */
1003 node = lyd_new_term(NULL, mod, "d", "15");
1004 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001005 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001006 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1007 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001008 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1009 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001010
1011 /* check data tree */
1012 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1013 assert_string_equal(str,
1014 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1015 "<ll1 ncwd:default=\"true\">def1</ll1>"
1016 "<ll1 ncwd:default=\"true\">def2</ll1>"
1017 "<ll1 ncwd:default=\"true\">def3</ll1>"
1018 "<d ncwd:default=\"true\">15</d>"
1019 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1020 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1021 "</cont>"
1022 "<l xmlns=\"urn:tests:f\">value</l>"
1023 "<d xmlns=\"urn:tests:f\">15</d>"
1024 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1025 free(str);
1026
Michal Vaskob1b5c262020-03-05 14:29:47 +01001027 /* create first explicit container, which should become implicit */
1028 node = lyd_new_inner(NULL, mod, "cont");
1029 assert_non_null(node);
1030 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1031 tree = tree->prev;
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);
1049
1050 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1051 node = lyd_new_inner(NULL, mod, "cont");
1052 assert_non_null(node);
1053 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1054 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1055
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 ncwd:default=\"true\">def1</ll1>"
1061 "<ll1 ncwd:default=\"true\">def2</ll1>"
1062 "<ll1 ncwd:default=\"true\">def3</ll1>"
1063 "<d ncwd:default=\"true\">15</d>"
1064 "<ll2 ncwd:default=\"true\">dflt1</ll2>"
1065 "<ll2 ncwd:default=\"true\">dflt2</ll2>"
1066 "</cont>"
1067 "<l xmlns=\"urn:tests:f\">value</l>"
1068 "<d xmlns=\"urn:tests:f\">15</d>"
1069 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1070 free(str);
Michal Vaskof03ed032020-03-04 13:31:44 +01001071
1072 /* similar changes for nested defaults */
1073 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1074 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1075 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001076 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001077
1078 /* check data tree */
1079 lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
1080 assert_string_equal(str,
1081 "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">"
1082 "<ll1>def3</ll1>"
1083 "<d>5</d>"
1084 "<ll2>non-dflt</ll2>"
1085 "</cont>"
1086 "<l xmlns=\"urn:tests:f\">value</l>"
1087 "<d xmlns=\"urn:tests:f\">15</d>"
1088 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
1089 free(str);
1090
1091 lyd_free_siblings(tree);
1092
1093 *state = NULL;
1094}
1095
Michal Vaskoc193ce92020-03-06 11:04:48 +01001096static void
1097test_iffeature(void **state)
1098{
1099 *state = test_iffeature;
1100
1101 const char *data;
1102 struct lyd_node *tree;
1103 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1104
1105 /* get empty data */
1106 tree = NULL;
1107 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1108 assert_null(tree);
1109
1110 /* disabled by f1 */
1111 data =
1112 "<cont xmlns=\"urn:tests:g\">"
1113 "<d>51</d>"
1114 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001115 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001116 assert_null(tree);
1117 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1118
1119 /* enable f1 */
1120 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1121
1122 /* get data with default container */
1123 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1124 assert_non_null(tree);
1125 lyd_free_siblings(tree);
1126
1127 /* disabled by f2 */
1128 data =
1129 "<cont xmlns=\"urn:tests:g\">"
1130 "<cont2>"
1131 "<e>val</e>"
1132 "</cont2>"
1133 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001134 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001135 assert_null(tree);
1136 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1137
1138 data =
1139 "<cont xmlns=\"urn:tests:g\">"
1140 "<a>val</a>"
1141 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001142 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001143 assert_null(tree);
1144 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1145
1146 /* enable f3 */
1147 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1148
Michal Vasko9f96a052020-03-10 09:41:45 +01001149 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001150 assert_non_null(tree);
1151 lyd_free_siblings(tree);
1152
1153 /* disabled by f2 */
1154 data =
1155 "<cont xmlns=\"urn:tests:g\">"
1156 "<l>val</l>"
1157 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001158 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001159 assert_null(tree);
1160 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1161
1162 /* enable f2 */
1163 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1164
Michal Vasko9f96a052020-03-10 09:41:45 +01001165 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001166 assert_non_null(tree);
1167 lyd_free_siblings(tree);
1168
1169 /* try separate validation */
1170 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1171 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1172 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1173
1174 data =
1175 "<cont xmlns=\"urn:tests:g\">"
1176 "<l>val</l>"
1177 "<d>51</d>"
1178 "<cont2>"
1179 "<e>val</e>"
1180 "</cont2>"
1181 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001182 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001183 assert_non_null(tree);
1184
1185 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1186 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1187
1188 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1189
1190 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1191 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1192
1193 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1194
1195 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1196
1197 lyd_free_siblings(tree);
1198
1199 *state = NULL;
1200}
1201
Michal Vasko5b37a352020-03-06 13:38:33 +01001202static void
1203test_state(void **state)
1204{
1205 *state = test_iffeature;
1206
1207 const char *data;
1208 struct lyd_node *tree;
1209
1210 data =
1211 "<cont xmlns=\"urn:tests:h\">"
1212 "<cont2>"
1213 "<l>val</l>"
1214 "</cont2>"
1215 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001216 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001217 assert_null(tree);
1218 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1219
Michal Vasko9f96a052020-03-10 09:41:45 +01001220 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001221 assert_null(tree);
1222 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1223
Michal Vasko9f96a052020-03-10 09:41:45 +01001224 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001225 assert_non_null(tree);
1226
1227 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE));
1228 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1229
1230 lyd_free_siblings(tree);
1231
1232 *state = NULL;
1233}
1234
Michal Vaskocde73ac2019-11-14 16:10:27 +01001235int main(void)
1236{
1237 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001238 cmocka_unit_test_teardown(test_when, teardown_s),
1239 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1240 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001241 cmocka_unit_test_teardown(test_unique, teardown_s),
1242 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001243 cmocka_unit_test_teardown(test_dup, teardown_s),
1244 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001245 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001246 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vasko9f96a052020-03-10 09:41:45 +01001247 //cmocka_unit_test_teardown(test_edit, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001248 };
1249
Michal Vaskoacd83e72020-02-04 14:12:01 +01001250 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001251}