blob: f183eca22677750b1ed7537db45bf40def29e551 [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
Michal Vasko52927e22020-03-16 17:26:14 +010015#include "tests/config.h"
16
Michal Vaskocde73ac2019-11-14 16:10:27 +010017#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "../../src/context.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020026#include "../../src/tree_schema.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010027#include "../../src/tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "../../src/printer.h"
Michal Vaskof03ed032020-03-04 13:31:44 +010029#include "../../src/printer_data.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010030
31#define BUFSIZE 1024
32char logbuf[BUFSIZE] = {0};
33int store = -1; /* negative for infinite logging, positive for limited logging */
34
35struct ly_ctx *ctx; /* context for tests */
36
37/* set to 0 to printing error messages to stderr instead of checking them in code */
38#define ENABLE_LOGGER_CHECKING 1
39
40#if ENABLE_LOGGER_CHECKING
41static void
42logger(LY_LOG_LEVEL level, const char *msg, const char *path)
43{
44 (void) level; /* unused */
45 if (store) {
46 if (path && path[0]) {
47 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
48 } else {
49 strncpy(logbuf, msg, BUFSIZE - 1);
50 }
51 if (store > 0) {
52 --store;
53 }
54 }
55}
56#endif
57
58static int
59setup(void **state)
60{
61 (void) state; /* unused */
62
63 const char *schema_a =
64 "module a {"
65 "namespace urn:tests:a;"
66 "prefix a;"
67 "yang-version 1.1;"
68
69 "container cont {"
70 "leaf a {"
71 "when \"../../c = 'val_c'\";"
72 "type string;"
73 "}"
74 "leaf b {"
75 "type string;"
76 "}"
77 "}"
78 "leaf c {"
79 "when \"/cont/b = 'val_b'\";"
80 "type string;"
81 "}"
82 "}";
Michal Vaskoa3881362020-01-21 15:57:35 +010083 const char *schema_b =
84 "module b {"
85 "namespace urn:tests:b;"
86 "prefix b;"
87 "yang-version 1.1;"
88
89 "choice choic {"
90 "mandatory true;"
91 "leaf a {"
92 "type string;"
93 "}"
94 "case b {"
95 "leaf l {"
96 "type string;"
97 "}"
98 "}"
99 "}"
100 "leaf c {"
101 "mandatory true;"
102 "type string;"
103 "}"
104 "leaf d {"
105 "type empty;"
106 "}"
107 "}";
Michal Vaskoacd83e72020-02-04 14:12:01 +0100108 const char *schema_c =
109 "module c {"
110 "namespace urn:tests:c;"
111 "prefix c;"
112 "yang-version 1.1;"
113
114 "choice choic {"
115 "leaf a {"
116 "type string;"
117 "}"
118 "case b {"
119 "leaf-list l {"
120 "min-elements 3;"
121 "type string;"
122 "}"
123 "}"
124 "}"
125 "list lt {"
126 "max-elements 4;"
127 "key \"k\";"
128 "leaf k {"
129 "type string;"
130 "}"
131 "}"
132 "leaf d {"
133 "type empty;"
134 "}"
135 "}";
Michal Vasko14654712020-02-06 08:35:21 +0100136 const char *schema_d =
137 "module d {"
138 "namespace urn:tests:d;"
139 "prefix d;"
140 "yang-version 1.1;"
141
142 "list lt {"
143 "key \"k\";"
144 "unique \"l1\";"
145 "leaf k {"
146 "type string;"
147 "}"
148 "leaf l1 {"
149 "type string;"
150 "}"
151 "}"
152 "list lt2 {"
153 "key \"k\";"
154 "unique \"cont/l2 l4\";"
155 "unique \"l5 l6\";"
156 "leaf k {"
157 "type string;"
158 "}"
159 "container cont {"
160 "leaf l2 {"
161 "type string;"
162 "}"
163 "}"
164 "leaf l4 {"
165 "type string;"
166 "}"
167 "leaf l5 {"
168 "type string;"
169 "}"
170 "leaf l6 {"
171 "type string;"
172 "}"
173 "list lt3 {"
174 "key \"kk\";"
175 "unique \"l3\";"
176 "leaf kk {"
177 "type string;"
178 "}"
179 "leaf l3 {"
180 "type string;"
181 "}"
182 "}"
183 "}"
184 "}";
Michal Vaskof03ed032020-03-04 13:31:44 +0100185 const char *schema_e =
186 "module e {"
187 "namespace urn:tests:e;"
188 "prefix e;"
189 "yang-version 1.1;"
190
191 "choice choic {"
192 "leaf a {"
193 "type string;"
194 "}"
195 "case b {"
196 "leaf-list l {"
197 "type string;"
198 "}"
199 "}"
200 "}"
201 "list lt {"
202 "key \"k\";"
203 "leaf k {"
204 "type string;"
205 "}"
206 "}"
207 "leaf d {"
208 "type uint32;"
209 "}"
210 "leaf-list ll {"
211 "type string;"
212 "}"
213 "container cont {"
214 "list lt {"
215 "key \"k\";"
216 "leaf k {"
217 "type string;"
218 "}"
219 "}"
220 "leaf d {"
221 "type uint32;"
222 "}"
223 "leaf-list ll {"
224 "type string;"
225 "}"
Michal Vasko9f96a052020-03-10 09:41:45 +0100226 "leaf-list ll2 {"
227 "type enumeration {"
228 "enum one;"
229 "enum two;"
230 "}"
231 "}"
Michal Vaskof03ed032020-03-04 13:31:44 +0100232 "}"
233 "}";
234 const char *schema_f =
235 "module f {"
236 "namespace urn:tests:f;"
237 "prefix f;"
238 "yang-version 1.1;"
239
240 "choice choic {"
241 "default \"c\";"
242 "leaf a {"
243 "type string;"
244 "}"
245 "case b {"
246 "leaf l {"
247 "type string;"
248 "}"
249 "}"
250 "case c {"
251 "leaf-list ll1 {"
252 "type string;"
253 "default \"def1\";"
254 "default \"def2\";"
255 "default \"def3\";"
256 "}"
257 "}"
258 "}"
259 "leaf d {"
260 "type uint32;"
261 "default 15;"
262 "}"
263 "leaf-list ll2 {"
264 "type string;"
265 "default \"dflt1\";"
266 "default \"dflt2\";"
267 "}"
268 "container cont {"
269 "choice choic {"
270 "default \"c\";"
271 "leaf a {"
272 "type string;"
273 "}"
274 "case b {"
275 "leaf l {"
276 "type string;"
277 "}"
278 "}"
279 "case c {"
280 "leaf-list ll1 {"
281 "type string;"
282 "default \"def1\";"
283 "default \"def2\";"
284 "default \"def3\";"
285 "}"
286 "}"
287 "}"
288 "leaf d {"
289 "type uint32;"
290 "default 15;"
291 "}"
292 "leaf-list ll2 {"
293 "type string;"
294 "default \"dflt1\";"
295 "default \"dflt2\";"
296 "}"
297 "}"
298 "}";
Michal Vaskoc193ce92020-03-06 11:04:48 +0100299 const char *schema_g =
300 "module g {"
301 "namespace urn:tests:g;"
302 "prefix g;"
303 "yang-version 1.1;"
304
305 "feature f1;"
306 "feature f2;"
307 "feature f3;"
308
309 "container cont {"
310 "if-feature \"f1\";"
311 "choice choic {"
312 "if-feature \"f2 or f3\";"
313 "leaf a {"
314 "type string;"
315 "}"
316 "case b {"
317 "if-feature \"f2 and f1\";"
318 "leaf l {"
319 "type string;"
320 "}"
321 "}"
322 "}"
323 "leaf d {"
324 "type uint32;"
325 "}"
326 "container cont2 {"
327 "if-feature \"f2\";"
328 "leaf e {"
329 "type string;"
330 "}"
331 "}"
332 "}"
333 "}";
Michal Vasko5b37a352020-03-06 13:38:33 +0100334 const char *schema_h =
335 "module h {"
336 "namespace urn:tests:h;"
337 "prefix h;"
338 "yang-version 1.1;"
339
340 "container cont {"
341 "container cont2 {"
342 "config false;"
343 "leaf l {"
344 "type string;"
345 "}"
346 "}"
347 "}"
348 "}";
Michal Vaskocc048b22020-03-27 15:52:38 +0100349 const char *schema_i =
350 "module i {"
351 "namespace urn:tests:i;"
352 "prefix i;"
353 "yang-version 1.1;"
354
355 "container cont {"
356 "leaf l {"
357 "type string;"
358 "}"
359 "leaf l2 {"
360 "must \"../l = 'right'\";"
361 "type string;"
362 "}"
363 "}"
364 "}";
Michal Vaskofea12c62020-03-30 11:00:15 +0200365 const char *schema_j =
366 "module j {"
367 "namespace urn:tests:j;"
368 "prefix j;"
369 "yang-version 1.1;"
370
371 "feature feat1;"
372
373 "container cont {"
374 "must \"false()\";"
375 "list l1 {"
376 "key \"k\";"
377 "leaf k {"
378 "type string;"
379 "}"
380 "action act {"
381 "if-feature feat1;"
382 "input {"
383 "must \"../../lf1 = 'true'\";"
384 "leaf lf2 {"
385 "type leafref {"
386 "path /lf3;"
387 "}"
388 "}"
389 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200390 "output {"
391 "must \"../../lf1 = 'true2'\";"
392 "leaf lf2 {"
393 "type leafref {"
394 "path /lf4;"
395 "}"
396 "}"
397 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200398 "}"
399 "}"
400
401 "leaf lf1 {"
402 "type string;"
403 "}"
404 "}"
405
406 "leaf lf3 {"
407 "type string;"
408 "}"
Michal Vaskocb7526d2020-03-30 15:08:26 +0200409
410 "leaf lf4 {"
411 "type string;"
412 "}"
Michal Vaskofea12c62020-03-30 11:00:15 +0200413 "}";
Michal Vaskocde73ac2019-11-14 16:10:27 +0100414
415#if ENABLE_LOGGER_CHECKING
416 ly_set_log_clb(logger, 1);
417#endif
418
Michal Vaskof03ed032020-03-04 13:31:44 +0100419 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
420 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100421 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
Michal Vaskoa3881362020-01-21 15:57:35 +0100422 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100423 assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG));
Michal Vasko14654712020-02-06 08:35:21 +0100424 assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG));
Michal Vaskof03ed032020-03-04 13:31:44 +0100425 assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG));
426 assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG));
Michal Vaskoc193ce92020-03-06 11:04:48 +0100427 assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG));
Michal Vasko5b37a352020-03-06 13:38:33 +0100428 assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG));
Michal Vaskocc048b22020-03-27 15:52:38 +0100429 assert_non_null(lys_parse_mem(ctx, schema_i, LYS_IN_YANG));
Michal Vaskofea12c62020-03-30 11:00:15 +0200430 assert_non_null(lys_parse_mem(ctx, schema_j, LYS_IN_YANG));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100431
432 return 0;
433}
434
435static int
436teardown(void **state)
437{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100438 (void)state;
439 ly_ctx_destroy(ctx, NULL);
440 ctx = NULL;
441
442 return 0;
443}
444
445static int
446teardown_s(void **state)
447{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100448#if ENABLE_LOGGER_CHECKING
449 if (*state) {
450 fprintf(stderr, "%s\n", logbuf);
451 }
452#else
453 (void) state; /* unused */
454#endif
455
Michal Vaskocde73ac2019-11-14 16:10:27 +0100456 return 0;
457}
458
459void
460logbuf_clean(void)
461{
462 logbuf[0] = '\0';
463}
464
465#if ENABLE_LOGGER_CHECKING
466# define logbuf_assert(str) assert_string_equal(logbuf, str)
467#else
468# define logbuf_assert(str)
469#endif
470
471static void
472test_when(void **state)
473{
474 *state = test_when;
475
476 const char *data;
477 struct lyd_node *tree;
478
479 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100480 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100481 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100482 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100483
484 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 +0100485 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100486 assert_non_null(tree);
487 assert_string_equal("c", tree->next->schema->name);
488 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
489 lyd_free_all(tree);
490
491 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 +0100492 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskocde73ac2019-11-14 16:10:27 +0100493 assert_non_null(tree);
494 assert_string_equal("a", lyd_node_children(tree)->schema->name);
495 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
496 assert_string_equal("c", tree->next->schema->name);
497 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
498 lyd_free_all(tree);
499
500 *state = NULL;
501}
502
Michal Vaskoa3881362020-01-21 15:57:35 +0100503static void
504test_mandatory(void **state)
505{
506 *state = test_mandatory;
507
508 const char *data;
509 struct lyd_node *tree;
510
511 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100513 assert_null(tree);
514 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
515
516 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100517 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100518 assert_null(tree);
519 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
520
521 data = "<a xmlns=\"urn:tests:b\">string</a>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100522 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100523 assert_null(tree);
524 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
525
526 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100527 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoa3881362020-01-21 15:57:35 +0100528 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100529 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100530
531 *state = NULL;
532}
533
Michal Vaskoacd83e72020-02-04 14:12:01 +0100534static void
535test_minmax(void **state)
536{
537 *state = test_minmax;
538
539 const char *data;
540 struct lyd_node *tree;
541
542 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100543 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100544 assert_null(tree);
545 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
546
547 data =
548 "<l xmlns=\"urn:tests:c\">val1</l>"
549 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100550 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100551 assert_null(tree);
552 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
553
554 data =
555 "<l xmlns=\"urn:tests:c\">val1</l>"
556 "<l xmlns=\"urn:tests:c\">val2</l>"
557 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100558 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100559 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100560 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100561
562 data =
563 "<l xmlns=\"urn:tests:c\">val1</l>"
564 "<l xmlns=\"urn:tests:c\">val2</l>"
565 "<l xmlns=\"urn:tests:c\">val3</l>"
566 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
567 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
568 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
569 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
570 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100571 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoacd83e72020-02-04 14:12:01 +0100572 assert_null(tree);
573 logbuf_assert("Too many \"lt\" instances. /c:lt");
574
575 *state = NULL;
576}
577
Michal Vasko14654712020-02-06 08:35:21 +0100578static void
579test_unique(void **state)
580{
581 *state = test_unique;
582
583 const char *data;
584 struct lyd_node *tree;
585
586 data =
587 "<lt xmlns=\"urn:tests:d\">"
588 "<k>val1</k>"
589 "<l1>same</l1>"
590 "</lt>"
591 "<lt xmlns=\"urn:tests:d\">"
592 "<k>val2</k>"
593 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100594 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100595 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100596 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100597
598 data =
599 "<lt xmlns=\"urn:tests:d\">"
600 "<k>val1</k>"
601 "<l1>same</l1>"
602 "</lt>"
603 "<lt xmlns=\"urn:tests:d\">"
604 "<k>val2</k>"
605 "<l1>not-same</l1>"
606 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100607 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100608 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100609 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100610
611 data =
612 "<lt xmlns=\"urn:tests:d\">"
613 "<k>val1</k>"
614 "<l1>same</l1>"
615 "</lt>"
616 "<lt xmlns=\"urn:tests:d\">"
617 "<k>val2</k>"
618 "<l1>same</l1>"
619 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100620 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100621 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100622 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 +0100623
624 /* now try with more instances */
625 data =
626 "<lt xmlns=\"urn:tests:d\">"
627 "<k>val1</k>"
628 "<l1>1</l1>"
629 "</lt>"
630 "<lt xmlns=\"urn:tests:d\">"
631 "<k>val2</k>"
632 "<l1>2</l1>"
633 "</lt>"
634 "<lt xmlns=\"urn:tests:d\">"
635 "<k>val3</k>"
636 "<l1>3</l1>"
637 "</lt>"
638 "<lt xmlns=\"urn:tests:d\">"
639 "<k>val4</k>"
640 "<l1>4</l1>"
641 "</lt>"
642 "<lt xmlns=\"urn:tests:d\">"
643 "<k>val5</k>"
644 "<l1>5</l1>"
645 "</lt>"
646 "<lt xmlns=\"urn:tests:d\">"
647 "<k>val6</k>"
648 "<l1>6</l1>"
649 "</lt>"
650 "<lt xmlns=\"urn:tests:d\">"
651 "<k>val7</k>"
652 "<l1>7</l1>"
653 "</lt>"
654 "<lt xmlns=\"urn:tests:d\">"
655 "<k>val8</k>"
656 "<l1>8</l1>"
657 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100658 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100659 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100660 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100661
662 data =
663 "<lt xmlns=\"urn:tests:d\">"
664 "<k>val1</k>"
665 "<l1>1</l1>"
666 "</lt>"
667 "<lt xmlns=\"urn:tests:d\">"
668 "<k>val2</k>"
669 "<l1>2</l1>"
670 "</lt>"
671 "<lt xmlns=\"urn:tests:d\">"
672 "<k>val3</k>"
673 "<l1>3</l1>"
674 "</lt>"
675 "<lt xmlns=\"urn:tests:d\">"
676 "<k>val4</k>"
677 "</lt>"
678 "<lt xmlns=\"urn:tests:d\">"
679 "<k>val5</k>"
680 "<l1>5</l1>"
681 "</lt>"
682 "<lt xmlns=\"urn:tests:d\">"
683 "<k>val6</k>"
684 "<l1>6</l1>"
685 "</lt>"
686 "<lt xmlns=\"urn:tests:d\">"
687 "<k>val7</k>"
688 "</lt>"
689 "<lt xmlns=\"urn:tests:d\">"
690 "<k>val8</k>"
691 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100692 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100693 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100694 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100695
696 data =
697 "<lt xmlns=\"urn:tests:d\">"
698 "<k>val1</k>"
699 "<l1>1</l1>"
700 "</lt>"
701 "<lt xmlns=\"urn:tests:d\">"
702 "<k>val2</k>"
703 "<l1>2</l1>"
704 "</lt>"
705 "<lt xmlns=\"urn:tests:d\">"
706 "<k>val3</k>"
707 "</lt>"
708 "<lt xmlns=\"urn:tests:d\">"
709 "<k>val4</k>"
710 "<l1>4</l1>"
711 "</lt>"
712 "<lt xmlns=\"urn:tests:d\">"
713 "<k>val5</k>"
714 "</lt>"
715 "<lt xmlns=\"urn:tests:d\">"
716 "<k>val6</k>"
717 "</lt>"
718 "<lt xmlns=\"urn:tests:d\">"
719 "<k>val7</k>"
720 "<l1>2</l1>"
721 "</lt>"
722 "<lt xmlns=\"urn:tests:d\">"
723 "<k>val8</k>"
724 "<l1>8</l1>"
725 "</lt>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100726 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100727 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100728 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 +0100729
730 *state = NULL;
731}
732
733static void
734test_unique_nested(void **state)
735{
736 *state = test_unique_nested;
737
738 const char *data;
739 struct lyd_node *tree;
740
741 /* nested list uniquest are compared only with instances in the same parent list instance */
742 data =
743 "<lt2 xmlns=\"urn:tests:d\">"
744 "<k>val1</k>"
745 "<cont>"
746 "<l2>1</l2>"
747 "</cont>"
748 "<l4>1</l4>"
749 "</lt2>"
750 "<lt2 xmlns=\"urn:tests:d\">"
751 "<k>val2</k>"
752 "<cont>"
753 "<l2>2</l2>"
754 "</cont>"
755 "<l4>2</l4>"
756 "<lt3>"
757 "<kk>val1</kk>"
758 "<l3>1</l3>"
759 "</lt3>"
760 "<lt3>"
761 "<kk>val2</kk>"
762 "<l3>2</l3>"
763 "</lt3>"
764 "</lt2>"
765 "<lt2 xmlns=\"urn:tests:d\">"
766 "<k>val3</k>"
767 "<cont>"
768 "<l2>3</l2>"
769 "</cont>"
770 "<l4>3</l4>"
771 "<lt3>"
772 "<kk>val1</kk>"
773 "<l3>2</l3>"
774 "</lt3>"
775 "</lt2>"
776 "<lt2 xmlns=\"urn:tests:d\">"
777 "<k>val4</k>"
778 "<cont>"
779 "<l2>4</l2>"
780 "</cont>"
781 "<l4>4</l4>"
782 "<lt3>"
783 "<kk>val1</kk>"
784 "<l3>3</l3>"
785 "</lt3>"
786 "</lt2>"
787 "<lt2 xmlns=\"urn:tests:d\">"
788 "<k>val5</k>"
789 "<cont>"
790 "<l2>5</l2>"
791 "</cont>"
792 "<l4>5</l4>"
793 "<lt3>"
794 "<kk>val1</kk>"
795 "<l3>3</l3>"
796 "</lt3>"
797 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100798 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 +0100799 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100800 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100801
802 data =
803 "<lt2 xmlns=\"urn:tests:d\">"
804 "<k>val1</k>"
805 "<cont>"
806 "<l2>1</l2>"
807 "</cont>"
808 "<l4>1</l4>"
809 "</lt2>"
810 "<lt2 xmlns=\"urn:tests:d\">"
811 "<k>val2</k>"
812 "<cont>"
813 "<l2>2</l2>"
814 "</cont>"
815 "<lt3>"
816 "<kk>val1</kk>"
817 "<l3>1</l3>"
818 "</lt3>"
819 "<lt3>"
820 "<kk>val2</kk>"
821 "<l3>2</l3>"
822 "</lt3>"
823 "<lt3>"
824 "<kk>val3</kk>"
825 "<l3>1</l3>"
826 "</lt3>"
827 "</lt2>"
828 "<lt2 xmlns=\"urn:tests:d\">"
829 "<k>val3</k>"
830 "<cont>"
831 "<l2>3</l2>"
832 "</cont>"
833 "<l4>1</l4>"
834 "<lt3>"
835 "<kk>val1</kk>"
836 "<l3>2</l3>"
837 "</lt3>"
838 "</lt2>"
839 "<lt2 xmlns=\"urn:tests:d\">"
840 "<k>val4</k>"
841 "<cont>"
842 "<l2>4</l2>"
843 "</cont>"
844 "<lt3>"
845 "<kk>val1</kk>"
846 "<l3>3</l3>"
847 "</lt3>"
848 "</lt2>"
849 "<lt2 xmlns=\"urn:tests:d\">"
850 "<k>val5</k>"
851 "<cont>"
852 "<l2>5</l2>"
853 "</cont>"
854 "<lt3>"
855 "<kk>val1</kk>"
856 "<l3>3</l3>"
857 "</lt3>"
858 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100859 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100860 assert_null(tree);
861 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 +0100862 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100863
864 data =
865 "<lt2 xmlns=\"urn:tests:d\">"
866 "<k>val1</k>"
867 "<cont>"
868 "<l2>1</l2>"
869 "</cont>"
870 "<l4>1</l4>"
871 "</lt2>"
872 "<lt2 xmlns=\"urn:tests:d\">"
873 "<k>val2</k>"
874 "<cont>"
875 "<l2>2</l2>"
876 "</cont>"
877 "<l4>2</l4>"
878 "</lt2>"
879 "<lt2 xmlns=\"urn:tests:d\">"
880 "<k>val3</k>"
881 "<cont>"
882 "<l2>3</l2>"
883 "</cont>"
884 "<l4>3</l4>"
885 "</lt2>"
886 "<lt2 xmlns=\"urn:tests:d\">"
887 "<k>val4</k>"
888 "<cont>"
889 "<l2>2</l2>"
890 "</cont>"
891 "<l4>2</l4>"
892 "</lt2>"
893 "<lt2 xmlns=\"urn:tests:d\">"
894 "<k>val5</k>"
895 "<cont>"
896 "<l2>5</l2>"
897 "</cont>"
898 "<l4>5</l4>"
899 "</lt2>";
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 Vasko14654712020-02-06 08:35:21 +0100901 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100902 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 +0100903
904 data =
905 "<lt2 xmlns=\"urn:tests:d\">"
906 "<k>val1</k>"
907 "<cont>"
908 "<l2>1</l2>"
909 "</cont>"
910 "<l4>1</l4>"
911 "<l5>1</l5>"
912 "<l6>1</l6>"
913 "</lt2>"
914 "<lt2 xmlns=\"urn:tests:d\">"
915 "<k>val2</k>"
916 "<cont>"
917 "<l2>2</l2>"
918 "</cont>"
919 "<l4>1</l4>"
920 "<l5>1</l5>"
921 "</lt2>"
922 "<lt2 xmlns=\"urn:tests:d\">"
923 "<k>val3</k>"
924 "<cont>"
925 "<l2>3</l2>"
926 "</cont>"
927 "<l4>1</l4>"
928 "<l5>3</l5>"
929 "<l6>3</l6>"
930 "</lt2>"
931 "<lt2 xmlns=\"urn:tests:d\">"
932 "<k>val4</k>"
933 "<cont>"
934 "<l2>4</l2>"
935 "</cont>"
936 "<l4>1</l4>"
937 "<l6>1</l6>"
938 "</lt2>"
939 "<lt2 xmlns=\"urn:tests:d\">"
940 "<k>val5</k>"
941 "<cont>"
942 "<l2>5</l2>"
943 "</cont>"
944 "<l4>1</l4>"
945 "<l5>3</l5>"
946 "<l6>3</l6>"
947 "</lt2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100948 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko14654712020-02-06 08:35:21 +0100949 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100950 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 +0100951
952 *state = NULL;
953}
954
Michal Vaskof03ed032020-03-04 13:31:44 +0100955static void
956test_dup(void **state)
957{
958 *state = test_dup;
959
960 const char *data;
961 struct lyd_node *tree;
962
963 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100964 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100965 assert_null(tree);
966 logbuf_assert("Duplicate instance of \"d\". /e:d");
967
968 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 +0100969 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100970 assert_null(tree);
971 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
972
973 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 +0100974 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100975 assert_null(tree);
976 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
977
978 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100979 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100980 assert_null(tree);
981 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
982
983 /* same tests again but using hashes */
984 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 +0100985 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100986 assert_null(tree);
987 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
988
989 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
990 "<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 +0100991 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100992 assert_null(tree);
993 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
994
995 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
996 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100997 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +0100998 assert_null(tree);
999 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
1000
1001 /* cases */
1002 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 +01001003 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001004 assert_null(tree);
1005 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1006
1007 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 +01001008 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskof03ed032020-03-04 13:31:44 +01001009 assert_null(tree);
1010 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1011
1012 *state = NULL;
1013}
1014
1015static void
1016test_defaults(void **state)
1017{
1018 *state = test_defaults;
1019
Michal Vaskof03ed032020-03-04 13:31:44 +01001020 char *str;
1021 struct lyd_node *tree, *node;
1022 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1023
Radek Krejci241f6b52020-05-21 18:13:49 +02001024 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +02001025 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +01001026
Michal Vaskob1b5c262020-03-05 14:29:47 +01001027 /* get defaults */
1028 tree = NULL;
1029 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001030 assert_non_null(tree);
1031
1032 /* check all defaults exist */
Radek Krejcia5bba312020-01-09 15:41:20 +01001033 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001034 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001035 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1036 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1037 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1038 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1039 "<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 +01001040 "<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 +01001041 "<cont xmlns=\"urn:tests:f\">"
1042 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1043 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1044 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1045 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1046 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1047 "<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 +01001048 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001049 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001050
1051 /* create another explicit case and validate */
1052 node = lyd_new_term(NULL, mod, "l", "value");
1053 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001054 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1055 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001056
1057 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001058 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001059 assert_string_equal(str,
Michal Vaskob1b5c262020-03-05 14:29:47 +01001060 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1061 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1062 "<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 +01001063 "<cont xmlns=\"urn:tests:f\">"
1064 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1065 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1066 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1067 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1068 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1069 "<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 +01001070 "</cont>"
Michal Vaskof03ed032020-03-04 13:31:44 +01001071 "<l xmlns=\"urn:tests:f\">value</l>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001072 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001073
1074 /* create explicit leaf-list and leaf and validate */
1075 node = lyd_new_term(NULL, mod, "d", "15");
1076 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001077 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001078 node = lyd_new_term(NULL, mod, "ll2", "dflt2");
1079 assert_non_null(node);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001080 assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
1081 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001082
1083 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001084 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001085 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001086 "<cont xmlns=\"urn:tests:f\">"
1087 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1088 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1089 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1090 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1091 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1092 "<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 +01001093 "</cont>"
1094 "<l xmlns=\"urn:tests:f\">value</l>"
1095 "<d xmlns=\"urn:tests:f\">15</d>"
1096 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001097 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001098
Michal Vaskob1b5c262020-03-05 14:29:47 +01001099 /* create first explicit container, which should become implicit */
1100 node = lyd_new_inner(NULL, mod, "cont");
1101 assert_non_null(node);
1102 assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
1103 tree = tree->prev;
1104 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1105
1106 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001107 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001108 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001109 "<cont xmlns=\"urn:tests:f\">"
1110 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1111 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1112 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1113 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1114 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1115 "<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 +01001116 "</cont>"
1117 "<l xmlns=\"urn:tests:f\">value</l>"
1118 "<d xmlns=\"urn:tests:f\">15</d>"
1119 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001120 ly_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001121
1122 /* create second explicit container, which should become implicit, so the first tree node should be removed */
1123 node = lyd_new_inner(NULL, mod, "cont");
1124 assert_non_null(node);
1125 assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
1126 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
1127
1128 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001129 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001130 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001131 "<cont xmlns=\"urn:tests:f\">"
1132 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1133 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1134 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1135 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1136 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1137 "<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 +01001138 "</cont>"
1139 "<l xmlns=\"urn:tests:f\">value</l>"
1140 "<d xmlns=\"urn:tests:f\">15</d>"
1141 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001142 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001143
1144 /* similar changes for nested defaults */
1145 assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3"));
1146 assert_non_null(lyd_new_term(tree, NULL, "d", "5"));
1147 assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt"));
Michal Vaskob1b5c262020-03-05 14:29:47 +01001148 assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001149
1150 /* check data tree */
Radek Krejcia5bba312020-01-09 15:41:20 +01001151 lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG);
Michal Vaskof03ed032020-03-04 13:31:44 +01001152 assert_string_equal(str,
Michal Vasko52927e22020-03-16 17:26:14 +01001153 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001154 "<ll1>def3</ll1>"
1155 "<d>5</d>"
1156 "<ll2>non-dflt</ll2>"
1157 "</cont>"
1158 "<l xmlns=\"urn:tests:f\">value</l>"
1159 "<d xmlns=\"urn:tests:f\">15</d>"
1160 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001161 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001162
1163 lyd_free_siblings(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +02001164 ly_out_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001165
1166 *state = NULL;
1167}
1168
Michal Vaskoc193ce92020-03-06 11:04:48 +01001169static void
1170test_iffeature(void **state)
1171{
1172 *state = test_iffeature;
1173
1174 const char *data;
1175 struct lyd_node *tree;
1176 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g");
1177
1178 /* get empty data */
1179 tree = NULL;
1180 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1181 assert_null(tree);
1182
1183 /* disabled by f1 */
1184 data =
1185 "<cont xmlns=\"urn:tests:g\">"
1186 "<d>51</d>"
1187 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001188 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001189 assert_null(tree);
1190 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1191
1192 /* enable f1 */
1193 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1194
1195 /* get data with default container */
1196 assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS);
1197 assert_non_null(tree);
1198 lyd_free_siblings(tree);
1199
1200 /* disabled by f2 */
1201 data =
1202 "<cont xmlns=\"urn:tests:g\">"
1203 "<cont2>"
1204 "<e>val</e>"
1205 "</cont2>"
1206 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001207 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001208 assert_null(tree);
1209 logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2");
1210
1211 data =
1212 "<cont xmlns=\"urn:tests:g\">"
1213 "<a>val</a>"
1214 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001215 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001216 assert_null(tree);
1217 logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a");
1218
1219 /* enable f3 */
1220 assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS);
1221
Michal Vasko9f96a052020-03-10 09:41:45 +01001222 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001223 assert_non_null(tree);
1224 lyd_free_siblings(tree);
1225
1226 /* disabled by f2 */
1227 data =
1228 "<cont xmlns=\"urn:tests:g\">"
1229 "<l>val</l>"
1230 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001231 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001232 assert_null(tree);
1233 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1234
1235 /* enable f2 */
1236 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1237
Michal Vasko9f96a052020-03-10 09:41:45 +01001238 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001239 assert_non_null(tree);
1240 lyd_free_siblings(tree);
1241
1242 /* try separate validation */
1243 assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS);
1244 assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS);
1245 assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS);
1246
1247 data =
1248 "<cont xmlns=\"urn:tests:g\">"
1249 "<l>val</l>"
1250 "<d>51</d>"
1251 "<cont2>"
1252 "<e>val</e>"
1253 "</cont2>"
1254 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001255 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vaskoc193ce92020-03-06 11:04:48 +01001256 assert_non_null(tree);
1257
1258 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1259 logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont");
1260
1261 assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS);
1262
1263 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1264 logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l");
1265
1266 assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS);
1267
1268 assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY));
1269
1270 lyd_free_siblings(tree);
1271
1272 *state = NULL;
1273}
1274
Michal Vasko5b37a352020-03-06 13:38:33 +01001275static void
1276test_state(void **state)
1277{
Michal Vaskocc048b22020-03-27 15:52:38 +01001278 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001279
1280 const char *data;
1281 struct lyd_node *tree;
1282
1283 data =
1284 "<cont xmlns=\"urn:tests:h\">"
1285 "<cont2>"
1286 "<l>val</l>"
1287 "</cont2>"
1288 "</cont>";
Michal Vasko9f96a052020-03-10 09:41:45 +01001289 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 +01001290 assert_null(tree);
1291 logbuf_assert("Invalid state data node \"cont2\" found. Line number 1.");
1292
Michal Vasko9f96a052020-03-10 09:41:45 +01001293 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 +01001294 assert_null(tree);
1295 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1296
Michal Vasko9f96a052020-03-10 09:41:45 +01001297 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree));
Michal Vasko5b37a352020-03-06 13:38:33 +01001298 assert_non_null(tree);
1299
1300 assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE));
1301 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1302
1303 lyd_free_siblings(tree);
1304
1305 *state = NULL;
1306}
1307
Michal Vaskocc048b22020-03-27 15:52:38 +01001308static void
1309test_must(void **state)
1310{
1311 *state = test_must;
1312
1313 const char *data;
1314 struct lyd_node *tree;
1315
1316 data =
1317 "<cont xmlns=\"urn:tests:i\">"
1318 "<l>wrong</l>"
1319 "<l2>val</l2>"
1320 "</cont>";
1321 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1322 assert_null(tree);
1323 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1324
1325 data =
1326 "<cont xmlns=\"urn:tests:i\">"
1327 "<l>right</l>"
1328 "<l2>val</l2>"
1329 "</cont>";
1330 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
1331 assert_non_null(tree);
1332 lyd_free_tree(tree);
1333
1334 *state = NULL;
1335}
1336
Michal Vaskofea12c62020-03-30 11:00:15 +02001337static void
1338test_action(void **state)
1339{
1340 *state = test_action;
1341
1342 const char *data;
1343 struct lyd_node *tree, *op_tree;
1344 const struct lys_module *mod;
1345
1346 data =
1347 "<cont xmlns=\"urn:tests:j\">"
1348 "<l1>"
1349 "<k>val1</k>"
1350 "<act>"
1351 "<lf2>target</lf2>"
1352 "</act>"
1353 "</l1>"
1354 "</cont>";
1355 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &op_tree, NULL));
1356 assert_non_null(op_tree);
1357
1358 /* missing leafref */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001359 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_INPUT));
Michal Vasko004d3152020-06-11 19:59:22 +02001360 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value."
1361 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskofea12c62020-03-30 11:00:15 +02001362
1363 data =
1364 "<cont xmlns=\"urn:tests:j\">"
1365 "<lf1>not true</lf1>"
1366 "</cont>"
1367 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1368 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1369 assert_non_null(tree);
1370
1371 /* disabled if-feature */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001372 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001373 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1374
1375 mod = ly_ctx_get_module_latest(ctx, "j");
1376 assert_non_null(mod);
1377 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1378
1379 /* input must false */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001380 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001381 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1382
1383 lyd_free_siblings(tree);
1384 data =
1385 "<cont xmlns=\"urn:tests:j\">"
1386 "<lf1>true</lf1>"
1387 "</cont>"
1388 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
1389 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1390 assert_non_null(tree);
1391
1392 /* success */
Michal Vaskocb7526d2020-03-30 15:08:26 +02001393 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_INPUT));
Michal Vaskofea12c62020-03-30 11:00:15 +02001394
Michal Vaskocb7526d2020-03-30 15:08:26 +02001395 lys_feature_disable(mod, "feat1");
1396 lyd_free_tree(op_tree);
1397 lyd_free_siblings(tree);
1398
1399 *state = NULL;
1400}
1401
1402static void
1403test_reply(void **state)
1404{
1405 *state = test_reply;
1406
1407 const char *data;
1408 struct lyd_node *tree, *op_tree, *request;
1409 const struct lys_module *mod;
1410
1411 data =
1412 "<cont xmlns=\"urn:tests:j\">"
1413 "<l1>"
1414 "<k>val1</k>"
1415 "<act>"
1416 "<lf2>target</lf2>"
1417 "</act>"
1418 "</l1>"
1419 "</cont>";
1420 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &request, NULL));
1421 assert_non_null(request);
1422 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
1423 assert_int_equal(LY_SUCCESS, lyd_parse_xml_reply(request, data, &op_tree, NULL));
1424 lyd_free_all(request);
1425 assert_non_null(op_tree);
1426
1427 /* missing leafref */
1428 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALOPT_OUTPUT));
Michal Vasko004d3152020-06-11 19:59:22 +02001429 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value."
1430 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001431
1432 data =
1433 "<cont xmlns=\"urn:tests:j\">"
1434 "<lf1>not true</lf1>"
1435 "</cont>"
1436 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1437 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1438 assert_non_null(tree);
1439
1440 /* disabled if-feature */
1441 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1442 logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act");
1443
1444 mod = ly_ctx_get_module_latest(ctx, "j");
1445 assert_non_null(mod);
1446 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
1447
1448 /* input must false */
1449 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1450 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1451
1452 lyd_free_siblings(tree);
1453 data =
1454 "<cont xmlns=\"urn:tests:j\">"
1455 "<lf1>true2</lf1>"
1456 "</cont>"
1457 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
1458 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_TRUSTED, &tree));
1459 assert_non_null(tree);
1460
1461 /* success */
1462 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALOPT_OUTPUT));
1463
1464 lys_feature_disable(mod, "feat1");
Michal Vaskofea12c62020-03-30 11:00:15 +02001465 lyd_free_tree(op_tree);
1466 lyd_free_siblings(tree);
1467
1468 *state = NULL;
1469}
1470
Michal Vaskocde73ac2019-11-14 16:10:27 +01001471int main(void)
1472{
1473 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001474 cmocka_unit_test_teardown(test_when, teardown_s),
1475 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1476 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001477 cmocka_unit_test_teardown(test_unique, teardown_s),
1478 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001479 cmocka_unit_test_teardown(test_dup, teardown_s),
1480 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vaskoc193ce92020-03-06 11:04:48 +01001481 cmocka_unit_test_teardown(test_iffeature, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001482 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001483 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001484 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001485 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001486 };
1487
Michal Vaskoacd83e72020-02-04 14:12:01 +01001488 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001489}