blob: cd051919fffaaf44034921fef4b1b5c0f7fb91b6 [file] [log] [blame]
Michal Vaskob1b5c262020-03-05 14:29:47 +01001/**
2 * @file test_validation.c
Michal Vaskocde73ac2019-11-14 16:10:27 +01003 * @author: Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob1b5c262020-03-05 14:29:47 +01004 * @brief unit tests for functions from validation.c
Michal Vaskocde73ac2019-11-14 16:10:27 +01005 *
Michal Vaskob1b5c262020-03-05 14:29:47 +01006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vaskocde73ac2019-11-14 16:10:27 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "context.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020024#include "parser.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "printer.h"
27#include "printer_data.h"
28#include "tests/config.h"
29#include "tree_schema.h"
30#include "tree_data_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34int store = -1; /* negative for infinite logging, positive for limited logging */
35
36struct ly_ctx *ctx; /* context for tests */
37
38/* set to 0 to printing error messages to stderr instead of checking them in code */
39#define ENABLE_LOGGER_CHECKING 1
40
41#if ENABLE_LOGGER_CHECKING
42static void
43logger(LY_LOG_LEVEL level, const char *msg, const char *path)
44{
45 (void) level; /* unused */
46 if (store) {
47 if (path && path[0]) {
48 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
49 } else {
50 strncpy(logbuf, msg, BUFSIZE - 1);
51 }
52 if (store > 0) {
53 --store;
54 }
55 }
56}
57#endif
58
59static int
60setup(void **state)
61{
62 (void) state; /* unused */
63
64 const char *schema_a =
65 "module a {"
66 "namespace urn:tests:a;"
67 "prefix a;"
68 "yang-version 1.1;"
69
70 "container cont {"
71 "leaf a {"
72 "when \"../../c = 'val_c'\";"
73 "type string;"
74 "}"
75 "leaf b {"
76 "type string;"
77 "}"
78 "}"
79 "leaf c {"
80 "when \"/cont/b = 'val_b'\";"
81 "type string;"
82 "}"
83 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010084 const char *schema_b =
85 "module b {"
86 "namespace urn:tests:b;"
87 "prefix b;"
88 "yang-version 1.1;"
89
90 "choice choic {"
91 "mandatory true;"
92 "leaf a {"
93 "type string;"
94 "}"
95 "case b {"
96 "leaf l {"
97 "type string;"
98 "}"
99 "}"
100 "}"
101 "leaf c {"
102 "mandatory true;"
103 "type string;"
104 "}"
105 "leaf d {"
106 "type empty;"
107 "}"
108 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100109 const char *schema_c =
110 "module c {"
111 "namespace urn:tests:c;"
112 "prefix c;"
113 "yang-version 1.1;"
114
115 "choice choic {"
116 "leaf a {"
117 "type string;"
118 "}"
119 "case b {"
120 "leaf-list l {"
121 "min-elements 3;"
122 "type string;"
123 "}"
124 "}"
125 "}"
126 "list lt {"
127 "max-elements 4;"
128 "key \"k\";"
129 "leaf k {"
130 "type string;"
131 "}"
132 "}"
133 "leaf d {"
134 "type empty;"
135 "}"
136 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100137 const char *schema_d =
138 "module d {"
139 "namespace urn:tests:d;"
140 "prefix d;"
141 "yang-version 1.1;"
142
143 "list lt {"
144 "key \"k\";"
145 "unique \"l1\";"
146 "leaf k {"
147 "type string;"
148 "}"
149 "leaf l1 {"
150 "type string;"
151 "}"
152 "}"
153 "list lt2 {"
154 "key \"k\";"
155 "unique \"cont/l2 l4\";"
156 "unique \"l5 l6\";"
157 "leaf k {"
158 "type string;"
159 "}"
160 "container cont {"
161 "leaf l2 {"
162 "type string;"
163 "}"
164 "}"
165 "leaf l4 {"
166 "type string;"
167 "}"
168 "leaf l5 {"
169 "type string;"
170 "}"
171 "leaf l6 {"
172 "type string;"
173 "}"
174 "list lt3 {"
175 "key \"kk\";"
176 "unique \"l3\";"
177 "leaf kk {"
178 "type string;"
179 "}"
180 "leaf l3 {"
181 "type string;"
182 "}"
183 "}"
184 "}"
185 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100186 const char *schema_e =
187 "module e {"
188 "namespace urn:tests:e;"
189 "prefix e;"
190 "yang-version 1.1;"
191
192 "choice choic {"
193 "leaf a {"
194 "type string;"
195 "}"
196 "case b {"
197 "leaf-list l {"
198 "type string;"
199 "}"
200 "}"
201 "}"
202 "list lt {"
203 "key \"k\";"
204 "leaf k {"
205 "type string;"
206 "}"
207 "}"
208 "leaf d {"
209 "type uint32;"
210 "}"
211 "leaf-list ll {"
212 "type string;"
213 "}"
214 "container cont {"
215 "list lt {"
216 "key \"k\";"
217 "leaf k {"
218 "type string;"
219 "}"
220 "}"
221 "leaf d {"
222 "type uint32;"
223 "}"
224 "leaf-list ll {"
225 "type string;"
226 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100227 "leaf-list ll2 {"
228 "type enumeration {"
229 "enum one;"
230 "enum two;"
231 "}"
232 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100233 "}"
234 "}";
235 const char *schema_f =
236 "module f {"
237 "namespace urn:tests:f;"
238 "prefix f;"
239 "yang-version 1.1;"
240
241 "choice choic {"
242 "default \"c\";"
243 "leaf a {"
244 "type string;"
245 "}"
246 "case b {"
247 "leaf l {"
248 "type string;"
249 "}"
250 "}"
251 "case c {"
252 "leaf-list ll1 {"
253 "type string;"
254 "default \"def1\";"
255 "default \"def2\";"
256 "default \"def3\";"
257 "}"
258 "}"
259 "}"
260 "leaf d {"
261 "type uint32;"
262 "default 15;"
263 "}"
264 "leaf-list ll2 {"
265 "type string;"
266 "default \"dflt1\";"
267 "default \"dflt2\";"
268 "}"
269 "container cont {"
270 "choice choic {"
271 "default \"c\";"
272 "leaf a {"
273 "type string;"
274 "}"
275 "case b {"
276 "leaf l {"
277 "type string;"
278 "}"
279 "}"
280 "case c {"
281 "leaf-list ll1 {"
282 "type string;"
283 "default \"def1\";"
284 "default \"def2\";"
285 "default \"def3\";"
286 "}"
287 "}"
288 "}"
289 "leaf d {"
290 "type uint32;"
291 "default 15;"
292 "}"
293 "leaf-list ll2 {"
294 "type string;"
295 "default \"dflt1\";"
296 "default \"dflt2\";"
297 "}"
298 "}"
299 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100300 const char *schema_g =
301 "module g {"
302 "namespace urn:tests:g;"
303 "prefix g;"
304 "yang-version 1.1;"
305
306 "feature f1;"
307 "feature f2;"
308 "feature f3;"
309
310 "container cont {"
311 "if-feature \"f1\";"
312 "choice choic {"
313 "if-feature \"f2 or f3\";"
314 "leaf a {"
315 "type string;"
316 "}"
317 "case b {"
318 "if-feature \"f2 and f1\";"
319 "leaf l {"
320 "type string;"
321 "}"
322 "}"
323 "}"
324 "leaf d {"
325 "type uint32;"
326 "}"
327 "container cont2 {"
328 "if-feature \"f2\";"
329 "leaf e {"
330 "type string;"
331 "}"
332 "}"
333 "}"
334 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100335 const char *schema_h =
336 "module h {"
337 "namespace urn:tests:h;"
338 "prefix h;"
339 "yang-version 1.1;"
340
341 "container cont {"
342 "container cont2 {"
343 "config false;"
344 "leaf l {"
345 "type string;"
346 "}"
347 "}"
348 "}"
349 "}";
Michal Vaskocc048b22020-03-27 15:52:38 +0100350 const char *schema_i =
351 "module i {"
352 "namespace urn:tests:i;"
353 "prefix i;"
354 "yang-version 1.1;"
355
356 "container cont {"
357 "leaf l {"
358 "type string;"
359 "}"
360 "leaf l2 {"
361 "must \"../l = 'right'\";"
362 "type string;"
363 "}"
364 "}"
365 "}";
Michal Vaskofea12c62020-03-30 11:00:15 +0200366 const char *schema_j =
367 "module j {"
368 "namespace urn:tests:j;"
369 "prefix j;"
370 "yang-version 1.1;"
371
372 "feature feat1;"
373
374 "container cont {"
375 "must \"false()\";"
376 "list l1 {"
377 "key \"k\";"
378 "leaf k {"
379 "type string;"
380 "}"
381 "action act {"
382 "if-feature feat1;"
383 "input {"
384 "must \"../../lf1 = 'true'\";"
385 "leaf lf2 {"
386 "type leafref {"
387 "path /lf3;"
388 "}"
389 "}"
390 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200391 "output {"
392 "must \"../../lf1 = 'true2'\";"
393 "leaf lf2 {"
394 "type leafref {"
395 "path /lf4;"
396 "}"
397 "}"
398 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200399 "}"
400 "}"
401
402 "leaf lf1 {"
403 "type string;"
404 "}"
405 "}"
406
407 "leaf lf3 {"
408 "type string;"
409 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200410
411 "leaf lf4 {"
412 "type string;"
413 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200414 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100415
416#if ENABLE_LOGGER_CHECKING
417 ly_set_log_clb(logger, 1);
418#endif
419
Michal Vaskof03ed032020-03-04 13:31:44 +0100420 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
421 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100422 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100423 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100424 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100425 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100426 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
427 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100428 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100429 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocc048b22020-03-27 15:52:38 +0100430 assert_non_null(lys_parse_mem(ctx, schema_i, LYS_IN_YANG));
Michal Vaskofea12c62020-03-30 11:00:15 +0200431 assert_non_null(lys_parse_mem(ctx, schema_j, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100432
433 return 0;
434}
435
436static int
437teardown(void **state)
438{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100439 (void)state;
440 ly_ctx_destroy(ctx, NULL);
441 ctx = NULL;
442
443 return 0;
444}
445
446static int
447teardown_s(void **state)
448{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100449#if ENABLE_LOGGER_CHECKING
450 if (*state) {
451 fprintf(stderr, "%s\n", logbuf);
452 }
453#else
454 (void) state; /* unused */
455#endif
456
Michal Vaskocde73ac2019-11-14 16:10:27 +0100457 return 0;
458}
459
460void
461logbuf_clean(void)
462{
463 logbuf[0] = '\0';
464}
465
466#if ENABLE_LOGGER_CHECKING
467# define logbuf_assert(str) assert_string_equal(logbuf, str)
468#else
469# define logbuf_assert(str)
470#endif
471
472static void
473test_when(void **state)
474{
475 *state = test_when;
476
477 const char *data;
478 struct lyd_node *tree;
479
480 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100482 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100483 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100484
485 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100487 assert_non_null(tree);
488 assert_string_equal("c", tree->next->schema->name);
489 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
490 lyd_free_all(tree);
491
492 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100494 assert_non_null(tree);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200495 assert_string_equal("a", lyd_node_children(tree, 0)->schema->name);
496 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree, 0)->flags);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100497 assert_string_equal("c", tree->next->schema->name);
498 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
499 lyd_free_all(tree);
500
501 *state = NULL;
502}
503
Michal Vaskoa3881362020-01-21 15:57:35 +0100504static void
505test_mandatory(void **state)
506{
507 *state = test_mandatory;
508
509 const char *data;
510 struct lyd_node *tree;
511
512 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100514 assert_null(tree);
515 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
516
517 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100519 assert_null(tree);
520 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
521
522 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100524 assert_null(tree);
525 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
526
527 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100529 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100530 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100531
532 *state = NULL;
533}
534
Michal Vaskoacd83e72020-02-04 14:12:01 +0100535static void
536test_minmax(void **state)
537{
538 *state = test_minmax;
539
540 const char *data;
541 struct lyd_node *tree;
542
543 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100545 assert_null(tree);
546 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
547
548 data =
549 "<l xmlns=\"urn:tests:c\">val1</l>"
550 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200551 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100552 assert_null(tree);
553 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
554
555 data =
556 "<l xmlns=\"urn:tests:c\">val1</l>"
557 "<l xmlns=\"urn:tests:c\">val2</l>"
558 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200559 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100560 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100561 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100562
563 data =
564 "<l xmlns=\"urn:tests:c\">val1</l>"
565 "<l xmlns=\"urn:tests:c\">val2</l>"
566 "<l xmlns=\"urn:tests:c\">val3</l>"
567 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
568 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
569 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
570 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
571 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200572 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100573 assert_null(tree);
574 logbuf_assert("Too many \"lt\" instances. /c:lt");
575
576 *state = NULL;
577}
578
Michal Vasko14654712020-02-06 08:35:21 +0100579static void
580test_unique(void **state)
581{
582 *state = test_unique;
583
584 const char *data;
585 struct lyd_node *tree;
586
587 data =
588 "<lt xmlns=\"urn:tests:d\">"
589 "<k>val1</k>"
590 "<l1>same</l1>"
591 "</lt>"
592 "<lt xmlns=\"urn:tests:d\">"
593 "<k>val2</k>"
594 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200595 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100596 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100597 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100598
599 data =
600 "<lt xmlns=\"urn:tests:d\">"
601 "<k>val1</k>"
602 "<l1>same</l1>"
603 "</lt>"
604 "<lt xmlns=\"urn:tests:d\">"
605 "<k>val2</k>"
606 "<l1>not-same</l1>"
607 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200608 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100609 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100610 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100611
612 data =
613 "<lt xmlns=\"urn:tests:d\">"
614 "<k>val1</k>"
615 "<l1>same</l1>"
616 "</lt>"
617 "<lt xmlns=\"urn:tests:d\">"
618 "<k>val2</k>"
619 "<l1>same</l1>"
620 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200621 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100622 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100623 logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val1']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100624
625 /* now try with more instances */
626 data =
627 "<lt xmlns=\"urn:tests:d\">"
628 "<k>val1</k>"
629 "<l1>1</l1>"
630 "</lt>"
631 "<lt xmlns=\"urn:tests:d\">"
632 "<k>val2</k>"
633 "<l1>2</l1>"
634 "</lt>"
635 "<lt xmlns=\"urn:tests:d\">"
636 "<k>val3</k>"
637 "<l1>3</l1>"
638 "</lt>"
639 "<lt xmlns=\"urn:tests:d\">"
640 "<k>val4</k>"
641 "<l1>4</l1>"
642 "</lt>"
643 "<lt xmlns=\"urn:tests:d\">"
644 "<k>val5</k>"
645 "<l1>5</l1>"
646 "</lt>"
647 "<lt xmlns=\"urn:tests:d\">"
648 "<k>val6</k>"
649 "<l1>6</l1>"
650 "</lt>"
651 "<lt xmlns=\"urn:tests:d\">"
652 "<k>val7</k>"
653 "<l1>7</l1>"
654 "</lt>"
655 "<lt xmlns=\"urn:tests:d\">"
656 "<k>val8</k>"
657 "<l1>8</l1>"
658 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200659 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100660 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100661 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100662
663 data =
664 "<lt xmlns=\"urn:tests:d\">"
665 "<k>val1</k>"
666 "<l1>1</l1>"
667 "</lt>"
668 "<lt xmlns=\"urn:tests:d\">"
669 "<k>val2</k>"
670 "<l1>2</l1>"
671 "</lt>"
672 "<lt xmlns=\"urn:tests:d\">"
673 "<k>val3</k>"
674 "<l1>3</l1>"
675 "</lt>"
676 "<lt xmlns=\"urn:tests:d\">"
677 "<k>val4</k>"
678 "</lt>"
679 "<lt xmlns=\"urn:tests:d\">"
680 "<k>val5</k>"
681 "<l1>5</l1>"
682 "</lt>"
683 "<lt xmlns=\"urn:tests:d\">"
684 "<k>val6</k>"
685 "<l1>6</l1>"
686 "</lt>"
687 "<lt xmlns=\"urn:tests:d\">"
688 "<k>val7</k>"
689 "</lt>"
690 "<lt xmlns=\"urn:tests:d\">"
691 "<k>val8</k>"
692 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200693 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100694 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100695 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100696
697 data =
698 "<lt xmlns=\"urn:tests:d\">"
699 "<k>val1</k>"
700 "<l1>1</l1>"
701 "</lt>"
702 "<lt xmlns=\"urn:tests:d\">"
703 "<k>val2</k>"
704 "<l1>2</l1>"
705 "</lt>"
706 "<lt xmlns=\"urn:tests:d\">"
707 "<k>val3</k>"
708 "</lt>"
709 "<lt xmlns=\"urn:tests:d\">"
710 "<k>val4</k>"
711 "<l1>4</l1>"
712 "</lt>"
713 "<lt xmlns=\"urn:tests:d\">"
714 "<k>val5</k>"
715 "</lt>"
716 "<lt xmlns=\"urn:tests:d\">"
717 "<k>val6</k>"
718 "</lt>"
719 "<lt xmlns=\"urn:tests:d\">"
720 "<k>val7</k>"
721 "<l1>2</l1>"
722 "</lt>"
723 "<lt xmlns=\"urn:tests:d\">"
724 "<k>val8</k>"
725 "<l1>8</l1>"
726 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200727 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100728 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100729 logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val7']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100730
731 *state = NULL;
732}
733
734static void
735test_unique_nested(void **state)
736{
737 *state = test_unique_nested;
738
739 const char *data;
740 struct lyd_node *tree;
741
742 /* nested list uniquest are compared only with instances in the same parent list instance */
743 data =
744 "<lt2 xmlns=\"urn:tests:d\">"
745 "<k>val1</k>"
746 "<cont>"
747 "<l2>1</l2>"
748 "</cont>"
749 "<l4>1</l4>"
750 "</lt2>"
751 "<lt2 xmlns=\"urn:tests:d\">"
752 "<k>val2</k>"
753 "<cont>"
754 "<l2>2</l2>"
755 "</cont>"
756 "<l4>2</l4>"
757 "<lt3>"
758 "<kk>val1</kk>"
759 "<l3>1</l3>"
760 "</lt3>"
761 "<lt3>"
762 "<kk>val2</kk>"
763 "<l3>2</l3>"
764 "</lt3>"
765 "</lt2>"
766 "<lt2 xmlns=\"urn:tests:d\">"
767 "<k>val3</k>"
768 "<cont>"
769 "<l2>3</l2>"
770 "</cont>"
771 "<l4>3</l4>"
772 "<lt3>"
773 "<kk>val1</kk>"
774 "<l3>2</l3>"
775 "</lt3>"
776 "</lt2>"
777 "<lt2 xmlns=\"urn:tests:d\">"
778 "<k>val4</k>"
779 "<cont>"
780 "<l2>4</l2>"
781 "</cont>"
782 "<l4>4</l4>"
783 "<lt3>"
784 "<kk>val1</kk>"
785 "<l3>3</l3>"
786 "</lt3>"
787 "</lt2>"
788 "<lt2 xmlns=\"urn:tests:d\">"
789 "<k>val5</k>"
790 "<cont>"
791 "<l2>5</l2>"
792 "</cont>"
793 "<l4>5</l4>"
794 "<lt3>"
795 "<kk>val1</kk>"
796 "<l3>3</l3>"
797 "</lt3>"
798 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200799 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100800 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100801 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100802
803 data =
804 "<lt2 xmlns=\"urn:tests:d\">"
805 "<k>val1</k>"
806 "<cont>"
807 "<l2>1</l2>"
808 "</cont>"
809 "<l4>1</l4>"
810 "</lt2>"
811 "<lt2 xmlns=\"urn:tests:d\">"
812 "<k>val2</k>"
813 "<cont>"
814 "<l2>2</l2>"
815 "</cont>"
816 "<lt3>"
817 "<kk>val1</kk>"
818 "<l3>1</l3>"
819 "</lt3>"
820 "<lt3>"
821 "<kk>val2</kk>"
822 "<l3>2</l3>"
823 "</lt3>"
824 "<lt3>"
825 "<kk>val3</kk>"
826 "<l3>1</l3>"
827 "</lt3>"
828 "</lt2>"
829 "<lt2 xmlns=\"urn:tests:d\">"
830 "<k>val3</k>"
831 "<cont>"
832 "<l2>3</l2>"
833 "</cont>"
834 "<l4>1</l4>"
835 "<lt3>"
836 "<kk>val1</kk>"
837 "<l3>2</l3>"
838 "</lt3>"
839 "</lt2>"
840 "<lt2 xmlns=\"urn:tests:d\">"
841 "<k>val4</k>"
842 "<cont>"
843 "<l2>4</l2>"
844 "</cont>"
845 "<lt3>"
846 "<kk>val1</kk>"
847 "<l3>3</l3>"
848 "</lt3>"
849 "</lt2>"
850 "<lt2 xmlns=\"urn:tests:d\">"
851 "<k>val5</k>"
852 "<cont>"
853 "<l2>5</l2>"
854 "</cont>"
855 "<lt3>"
856 "<kk>val1</kk>"
857 "<l3>3</l3>"
858 "</lt3>"
859 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200860 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100861 assert_null(tree);
862 logbuf_assert("Unique data leaf(s) \"l3\" not satisfied in \"/d:lt2[k='val2']/lt3[kk='val3']\" and"
Michal Vasko9b368d32020-02-14 13:53:31 +0100863 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100864
865 data =
866 "<lt2 xmlns=\"urn:tests:d\">"
867 "<k>val1</k>"
868 "<cont>"
869 "<l2>1</l2>"
870 "</cont>"
871 "<l4>1</l4>"
872 "</lt2>"
873 "<lt2 xmlns=\"urn:tests:d\">"
874 "<k>val2</k>"
875 "<cont>"
876 "<l2>2</l2>"
877 "</cont>"
878 "<l4>2</l4>"
879 "</lt2>"
880 "<lt2 xmlns=\"urn:tests:d\">"
881 "<k>val3</k>"
882 "<cont>"
883 "<l2>3</l2>"
884 "</cont>"
885 "<l4>3</l4>"
886 "</lt2>"
887 "<lt2 xmlns=\"urn:tests:d\">"
888 "<k>val4</k>"
889 "<cont>"
890 "<l2>2</l2>"
891 "</cont>"
892 "<l4>2</l4>"
893 "</lt2>"
894 "<lt2 xmlns=\"urn:tests:d\">"
895 "<k>val5</k>"
896 "<cont>"
897 "<l2>5</l2>"
898 "</cont>"
899 "<l4>5</l4>"
900 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200901 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100902 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100903 logbuf_assert("Unique data leaf(s) \"cont/l2 l4\" not satisfied in \"/d:lt2[k='val4']\" and \"/d:lt2[k='val2']\". /d:lt2[k='val2']");
Michal Vasko14654712020-02-06 08:35:21 +0100904
905 data =
906 "<lt2 xmlns=\"urn:tests:d\">"
907 "<k>val1</k>"
908 "<cont>"
909 "<l2>1</l2>"
910 "</cont>"
911 "<l4>1</l4>"
912 "<l5>1</l5>"
913 "<l6>1</l6>"
914 "</lt2>"
915 "<lt2 xmlns=\"urn:tests:d\">"
916 "<k>val2</k>"
917 "<cont>"
918 "<l2>2</l2>"
919 "</cont>"
920 "<l4>1</l4>"
921 "<l5>1</l5>"
922 "</lt2>"
923 "<lt2 xmlns=\"urn:tests:d\">"
924 "<k>val3</k>"
925 "<cont>"
926 "<l2>3</l2>"
927 "</cont>"
928 "<l4>1</l4>"
929 "<l5>3</l5>"
930 "<l6>3</l6>"
931 "</lt2>"
932 "<lt2 xmlns=\"urn:tests:d\">"
933 "<k>val4</k>"
934 "<cont>"
935 "<l2>4</l2>"
936 "</cont>"
937 "<l4>1</l4>"
938 "<l6>1</l6>"
939 "</lt2>"
940 "<lt2 xmlns=\"urn:tests:d\">"
941 "<k>val5</k>"
942 "<cont>"
943 "<l2>5</l2>"
944 "</cont>"
945 "<l4>1</l4>"
946 "<l5>3</l5>"
947 "<l6>3</l6>"
948 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200949 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100950 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100951 logbuf_assert("Unique data leaf(s) \"l5 l6\" not satisfied in \"/d:lt2[k='val5']\" and \"/d:lt2[k='val3']\". /d:lt2[k='val3']");
Michal Vasko14654712020-02-06 08:35:21 +0100952
953 *state = NULL;
954}
955
Michal Vaskof03ed032020-03-04 13:31:44 +0100956static void
957test_dup(void **state)
958{
959 *state = test_dup;
960
961 const char *data;
962 struct lyd_node *tree;
963
964 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200965 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100966 assert_null(tree);
967 logbuf_assert("Duplicate instance of \"d\". /e:d");
968
969 data = "<lt xmlns=\"urn:tests:e\"><k>A</k></lt><lt xmlns=\"urn:tests:e\"><k>B</k></lt><lt xmlns=\"urn:tests:e\"><k>A</k></lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200970 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100971 assert_null(tree);
972 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
973
974 data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200975 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100976 assert_null(tree);
977 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
978
979 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100981 assert_null(tree);
982 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
983
984 /* same tests again but using hashes */
985 data = "<cont xmlns=\"urn:tests:e\"><d>25</d><d>50</d><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll></cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200986 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100987 assert_null(tree);
988 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
989
990 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
991 "<lt><k>a</k></lt><lt><k>b</k></lt><lt><k>c</k></lt><lt><k>d</k></lt><lt><k>c</k></lt></cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100993 assert_null(tree);
994 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
995
996 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
997 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100999 assert_null(tree);
1000 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
1001
1002 /* cases */
1003 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><l xmlns=\"urn:tests:e\">b</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001004 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001005 assert_null(tree);
1006 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1007
1008 data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><a xmlns=\"urn:tests:e\">aa</a>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001010 assert_null(tree);
1011 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1012
1013 *state = NULL;
1014}
1015
1016static void
1017test_defaults(void **state)
1018{
1019 *state = test_defaults;
1020
Michal Vaskof03ed032020-03-04 13:31:44 +01001021 char *str;
1022 struct lyd_node *tree, *node;
1023 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1024
Radek Krejci241f6b52020-05-21 18:13:49 +02001025 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +02001026 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +01001027
Michal Vaskob1b5c262020-03-05 14:29:47 +01001028 /* get defaults */
1029 tree = NULL;
1030 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001031 assert_non_null(tree);
1032
1033 /* check all defaults exist */
Radek Krejci7931b192020-06-25 17:05:03 +02001034 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001035 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001036 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1037 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1038 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1039 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1040 "<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 +01001041 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001042 "<cont xmlns=\"urn:tests:f\">"
1043 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1044 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1045 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1046 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1047 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1048 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001049 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001050 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001051
1052 /* create another explicit case and validate */
1053 node = lyd_new_term(NULL, mod, "l", "value");
1054 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001055 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +02001056 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALIDATE_PRESENT), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001057
1058 /* check data tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001059 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001060 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +01001061 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1062 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1063 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001064 "<cont xmlns=\"urn:tests:f\">"
1065 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1066 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1067 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1068 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1069 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1070 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001071 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001072 "<l xmlns=\"urn:tests:f\">value</l>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001073 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001074
1075 /* create explicit leaf-list and leaf and validate */
1076 node = lyd_new_term(NULL, mod, "d", "15");
1077 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001078 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001079 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1080 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001081 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +02001082 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALIDATE_PRESENT), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001083
1084 /* check data tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001085 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001086 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001087 "<cont xmlns=\"urn:tests:f\">"
1088 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1089 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1090 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1091 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1092 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1093 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001094 "</cont>"
1095 "<l xmlns=\"urn:tests:f\">value</l>"
1096 "<d xmlns=\"urn:tests:f\">15</d>"
1097 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001098 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001099
Michal Vaskob1b5c262020-03-05 14:29:47 +01001100 /* create first explicit container, which should become implicit */
1101 node = lyd_new_inner(NULL, mod, "cont");
1102 assert_non_null(node);
1103 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1104 tree = tree->prev;
Radek Krejci7931b192020-06-25 17:05:03 +02001105 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALIDATE_PRESENT), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001106
1107 /* check data tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001108 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001109 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001110 "<cont xmlns=\"urn:tests:f\">"
1111 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1112 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1113 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1114 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1115 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1116 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001117 "</cont>"
1118 "<l xmlns=\"urn:tests:f\">value</l>"
1119 "<d xmlns=\"urn:tests:f\">15</d>"
1120 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001121 ly_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001122
1123 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1124 node = lyd_new_inner(NULL, mod, "cont");
1125 assert_non_null(node);
1126 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +02001127 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALIDATE_PRESENT), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001128
1129 /* check data tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001130 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001131 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001132 "<cont xmlns=\"urn:tests:f\">"
1133 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1134 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1135 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1136 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1137 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1138 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001139 "</cont>"
1140 "<l xmlns=\"urn:tests:f\">value</l>"
1141 "<d xmlns=\"urn:tests:f\">15</d>"
1142 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001143 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001144
1145 /* similar changes for nested defaults */
1146 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1147 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1148 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Radek Krejci7931b192020-06-25 17:05:03 +02001149 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALIDATE_PRESENT), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001150
1151 /* check data tree */
Radek Krejci7931b192020-06-25 17:05:03 +02001152 lyd_print(out, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001153 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001154 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001155 "<ll1>def3</ll1>"
1156 "<d>5</d>"
1157 "<ll2>non-dflt</ll2>"
1158 "</cont>"
1159 "<l xmlns=\"urn:tests:f\">value</l>"
1160 "<d xmlns=\"urn:tests:f\">15</d>"
1161 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001162 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001163
1164 lyd_free_siblings(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +02001165 ly_out_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001166
1167 *state = NULL;
1168}
1169
Michal Vaskoc193ce92020-03-06 11:04:48 +01001170static void
1171test_iffeature(void **state)
1172{
1173 *state = test_iffeature;
1174
1175 const char *data;
1176 struct lyd_node *tree;
1177 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1178
1179 /* get empty data */
1180 tree = NULL;
1181 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1182 assert_null(tree);
1183
1184 /* disabled by f1 */
1185 data =
1186 "<cont xmlns=\"urn:tests:g\">"
1187 "<d>51</d>"
1188 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001189 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001190 assert_null(tree);
1191 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1192
1193 /* enable f1 */
1194 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1195
1196 /* get data with default container */
1197 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1198 assert_non_null(tree);
1199 lyd_free_siblings(tree);
1200
1201 /* disabled by f2 */
1202 data =
1203 "<cont xmlns=\"urn:tests:g\">"
1204 "<cont2>"
1205 "<e>val</e>"
1206 "</cont2>"
1207 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001208 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001209 assert_null(tree);
1210 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1211
1212 data =
1213 "<cont xmlns=\"urn:tests:g\">"
1214 "<a>val</a>"
1215 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001216 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001217 assert_null(tree);
1218 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1219
1220 /* enable f3 */
1221 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1222
Michal Vasko63f3d842020-07-08 10:10:14 +02001223 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001224 assert_non_null(tree);
1225 lyd_free_siblings(tree);
1226
1227 /* disabled by f2 */
1228 data =
1229 "<cont xmlns=\"urn:tests:g\">"
1230 "<l>val</l>"
1231 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001232 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001233 assert_null(tree);
1234 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1235
1236 /* enable f2 */
1237 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1238
Michal Vasko63f3d842020-07-08 10:10:14 +02001239 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001240 assert_non_null(tree);
1241 lyd_free_siblings(tree);
1242
1243 /* try separate validation */
1244 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1245 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1246 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1247
1248 data =
1249 "<cont xmlns=\"urn:tests:g\">"
1250 "<l>val</l>"
1251 "<d>51</d>"
1252 "<cont2>"
1253 "<e>val</e>"
1254 "</cont2>"
1255 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001256 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001257 assert_non_null(tree);
1258
Radek Krejci7931b192020-06-25 17:05:03 +02001259 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALIDATE_PRESENT));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001260 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1261
1262 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1263
Radek Krejci7931b192020-06-25 17:05:03 +02001264 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALIDATE_PRESENT));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001265 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1266
1267 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1268
Radek Krejci7931b192020-06-25 17:05:03 +02001269 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALIDATE_PRESENT));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001270
1271 lyd_free_siblings(tree);
1272
1273 *state = NULL;
1274}
1275
Michal Vasko5b37a352020-03-06 13:38:33 +01001276static void
1277test_state(void **state)
1278{
Michal Vaskocc048b22020-03-27 15:52:38 +01001279 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001280
1281 const char *data;
1282 struct lyd_node *tree;
1283
1284 data =
1285 "<cont xmlns=\"urn:tests:h\">"
1286 "<cont2>"
1287 "<l>val</l>"
1288 "</cont2>"
1289 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001290 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001291 assert_null(tree);
1292 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1293
Michal Vasko63f3d842020-07-08 10:10:14 +02001294 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001295 assert_null(tree);
1296 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1297
Michal Vasko63f3d842020-07-08 10:10:14 +02001298 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001299 assert_non_null(tree);
1300
Radek Krejci7931b192020-06-25 17:05:03 +02001301 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE));
Michal Vasko5b37a352020-03-06 13:38:33 +01001302 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1303
1304 lyd_free_siblings(tree);
1305
1306 *state = NULL;
1307}
1308
Michal Vaskocc048b22020-03-27 15:52:38 +01001309static void
1310test_must(void **state)
1311{
1312 *state = test_must;
1313
1314 const char *data;
1315 struct lyd_node *tree;
1316
1317 data =
1318 "<cont xmlns=\"urn:tests:i\">"
1319 "<l>wrong</l>"
1320 "<l2>val</l2>"
1321 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001322 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocc048b22020-03-27 15:52:38 +01001323 assert_null(tree);
1324 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1325
1326 data =
1327 "<cont xmlns=\"urn:tests:i\">"
1328 "<l>right</l>"
1329 "<l2>val</l2>"
1330 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001331 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskocc048b22020-03-27 15:52:38 +01001332 assert_non_null(tree);
1333 lyd_free_tree(tree);
1334
1335 *state = NULL;
1336}
1337
Michal Vaskofea12c62020-03-30 11:00:15 +02001338static void
1339test_action(void **state)
1340{
1341 *state = test_action;
1342
1343 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001344 struct ly_in *in;
Michal Vaskofea12c62020-03-30 11:00:15 +02001345 struct lyd_node *tree, *op_tree;
1346 const struct lys_module *mod;
1347
1348 data =
1349 "<cont xmlns=\"urn:tests:j\">"
1350 "<l1>"
1351 "<k>val1</k>"
1352 "<act>"
1353 "<lf2>target</lf2>"
1354 "</act>"
1355 "</l1>"
1356 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001357 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1358 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &op_tree, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001359 assert_non_null(op_tree);
1360
1361 /* missing leafref */
Radek Krejci7931b192020-06-25 17:05:03 +02001362 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_RPC));
Michal Vasko004d3152020-06-11 19:59:22 +02001363 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value."
1364 " /j:cont/l1[k='val1']/act/lf2");
Michal Vasko63f3d842020-07-08 10:10:14 +02001365 ly_in_free(in, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001366
1367 data =
1368 "<cont xmlns=\"urn:tests:j\">"
1369 "<lf1>not true</lf1>"
1370 "</cont>"
1371 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001372 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001373 assert_non_null(tree);
1374
1375 /* disabled if-feature */
Radek Krejci7931b192020-06-25 17:05:03 +02001376 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC));
Michal Vaskofea12c62020-03-30 11:00:15 +02001377 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1378
1379 mod = ly_ctx_get_module_latest(ctx, "j");
1380 assert_non_null(mod);
1381 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1382
1383 /* input must false */
Radek Krejci7931b192020-06-25 17:05:03 +02001384 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC));
Michal Vaskofea12c62020-03-30 11:00:15 +02001385 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1386
1387 lyd_free_siblings(tree);
1388 data =
1389 "<cont xmlns=\"urn:tests:j\">"
1390 "<lf1>true</lf1>"
1391 "</cont>"
1392 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001393 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001394 assert_non_null(tree);
1395
1396 /* success */
Radek Krejci7931b192020-06-25 17:05:03 +02001397 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC));
Michal Vaskofea12c62020-03-30 11:00:15 +02001398
Michal Vaskocb7526d2020-03-30 15:08:26 +02001399 lys_feature_disable(mod, "feat1");
1400 lyd_free_tree(op_tree);
1401 lyd_free_siblings(tree);
1402
1403 *state = NULL;
1404}
1405
1406static void
1407test_reply(void **state)
1408{
1409 *state = test_reply;
1410
1411 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001412 struct ly_in *in;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001413 struct lyd_node *tree, *op_tree, *request;
1414 const struct lys_module *mod;
1415
1416 data =
1417 "<cont xmlns=\"urn:tests:j\">"
1418 "<l1>"
1419 "<k>val1</k>"
1420 "<act>"
1421 "<lf2>target</lf2>"
1422 "</act>"
1423 "</l1>"
1424 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001425 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1426 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &request, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001427 assert_non_null(request);
Michal Vasko63f3d842020-07-08 10:10:14 +02001428 ly_in_free(in, 0);
1429
Michal Vaskocb7526d2020-03-30 15:08:26 +02001430 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001431 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1432 assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_XML, &op_tree, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001433 lyd_free_all(request);
1434 assert_non_null(op_tree);
Michal Vasko63f3d842020-07-08 10:10:14 +02001435 ly_in_free(in, 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001436
1437 /* missing leafref */
Radek Krejci7931b192020-06-25 17:05:03 +02001438 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_REPLY));
Michal Vasko004d3152020-06-11 19:59:22 +02001439 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value."
1440 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001441
1442 data =
1443 "<cont xmlns=\"urn:tests:j\">"
1444 "<lf1>not true</lf1>"
1445 "</cont>"
1446 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001447 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001448 assert_non_null(tree);
1449
1450 /* disabled if-feature */
Radek Krejci7931b192020-06-25 17:05:03 +02001451 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001452 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1453
1454 mod = ly_ctx_get_module_latest(ctx, "j");
1455 assert_non_null(mod);
1456 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1457
1458 /* input must false */
Radek Krejci7931b192020-06-25 17:05:03 +02001459 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001460 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1461
1462 lyd_free_siblings(tree);
1463 data =
1464 "<cont xmlns=\"urn:tests:j\">"
1465 "<lf1>true2</lf1>"
1466 "</cont>"
1467 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001468 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001469 assert_non_null(tree);
1470
1471 /* success */
Radek Krejci7931b192020-06-25 17:05:03 +02001472 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001473
1474 lys_feature_disable(mod, "feat1");
Michal Vaskofea12c62020-03-30 11:00:15 +02001475 lyd_free_tree(op_tree);
1476 lyd_free_siblings(tree);
1477
1478 *state = NULL;
1479}
1480
Michal Vaskocde73ac2019-11-14 16:10:27 +01001481int main(void)
1482{
1483 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001484 cmocka_unit_test_teardown(test_when, teardown_s),
1485 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1486 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001487 cmocka_unit_test_teardown(test_unique, teardown_s),
1488 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001489 cmocka_unit_test_teardown(test_dup, teardown_s),
1490 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001491 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001492 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001493 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001494 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001495 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001496 };
1497
Michal Vaskoacd83e72020-02-04 14:12:01 +01001498 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001499}