blob: 9a33ad276d31a28673dd7c5b7e5e1e4217b47e29 [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 Vaskoafac7822020-10-20 14:22:26 +020024#include "in.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "out.h"
Radek Krejci70593c12020-06-13 20:48:09 +020027#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 Vasko7b1ad1a2020-11-02 15:41:27 +0100415 struct ly_in *in;
416 const char *feats[] = {"feat1", NULL};
Michal Vaskocde73ac2019-11-14 16:10:27 +0100417
418#if ENABLE_LOGGER_CHECKING
419 ly_set_log_clb(logger, 1);
420#endif
421
Michal Vaskof03ed032020-03-04 13:31:44 +0100422 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100423 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200424 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL));
425 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_b, LYS_IN_YANG, NULL));
426 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_c, LYS_IN_YANG, NULL));
427 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_d, LYS_IN_YANG, NULL));
428 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_e, LYS_IN_YANG, NULL));
429 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_f, LYS_IN_YANG, NULL));
430 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_g, LYS_IN_YANG, NULL));
431 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_h, LYS_IN_YANG, NULL));
432 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_i, LYS_IN_YANG, NULL));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100433 assert_int_equal(LY_SUCCESS, ly_in_new_memory(schema_j, &in));
434 assert_int_equal(LY_SUCCESS, lys_parse(ctx, in, LYS_IN_YANG, feats, NULL));
435 ly_in_free(in, 0);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100436
437 return 0;
438}
439
440static int
441teardown(void **state)
442{
Michal Vaskoacd83e72020-02-04 14:12:01 +0100443 (void)state;
444 ly_ctx_destroy(ctx, NULL);
445 ctx = NULL;
446
447 return 0;
448}
449
450static int
451teardown_s(void **state)
452{
Michal Vaskocde73ac2019-11-14 16:10:27 +0100453#if ENABLE_LOGGER_CHECKING
454 if (*state) {
455 fprintf(stderr, "%s\n", logbuf);
456 }
457#else
458 (void) state; /* unused */
459#endif
460
Michal Vaskocde73ac2019-11-14 16:10:27 +0100461 return 0;
462}
463
464void
465logbuf_clean(void)
466{
467 logbuf[0] = '\0';
468}
469
470#if ENABLE_LOGGER_CHECKING
471# define logbuf_assert(str) assert_string_equal(logbuf, str)
472#else
473# define logbuf_assert(str)
474#endif
475
476static void
477test_when(void **state)
478{
479 *state = test_when;
480
481 const char *data;
482 struct lyd_node *tree;
483
484 data = "<c xmlns=\"urn:tests:a\">hey</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 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 +0100486 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100487 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c");
Michal Vaskocde73ac2019-11-14 16:10:27 +0100488
489 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 +0200490 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 +0100491 assert_non_null(tree);
492 assert_string_equal("c", tree->next->schema->name);
493 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
494 lyd_free_all(tree);
495
496 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 +0200497 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 +0100498 assert_non_null(tree);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200499 assert_string_equal("a", lyd_child(tree)->schema->name);
500 assert_int_equal(LYD_WHEN_TRUE, lyd_child(tree)->flags);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100501 assert_string_equal("c", tree->next->schema->name);
502 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
503 lyd_free_all(tree);
504
505 *state = NULL;
506}
507
Michal Vaskoa3881362020-01-21 15:57:35 +0100508static void
509test_mandatory(void **state)
510{
511 *state = test_mandatory;
512
513 const char *data;
514 struct lyd_node *tree;
515
516 data = "<d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200517 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 +0100518 assert_null(tree);
519 logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic");
520
521 data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200522 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 +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>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 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 +0100528 assert_null(tree);
529 logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c");
530
531 data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200532 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 +0100533 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100534 lyd_free_siblings(tree);
Michal Vaskoa3881362020-01-21 15:57:35 +0100535
536 *state = NULL;
537}
538
Michal Vaskoacd83e72020-02-04 14:12:01 +0100539static void
540test_minmax(void **state)
541{
542 *state = test_minmax;
543
544 const char *data;
545 struct lyd_node *tree;
546
547 data = "<d xmlns=\"urn:tests:c\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200548 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 +0100549 assert_null(tree);
550 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
551
552 data =
553 "<l xmlns=\"urn:tests:c\">val1</l>"
554 "<l xmlns=\"urn:tests:c\">val2</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200555 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 +0100556 assert_null(tree);
557 logbuf_assert("Too few \"l\" instances. /c:choic/b/l");
558
559 data =
560 "<l xmlns=\"urn:tests:c\">val1</l>"
561 "<l xmlns=\"urn:tests:c\">val2</l>"
562 "<l xmlns=\"urn:tests:c\">val3</l>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 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 +0100564 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100565 lyd_free_siblings(tree);
Michal Vaskoacd83e72020-02-04 14:12:01 +0100566
567 data =
568 "<l xmlns=\"urn:tests:c\">val1</l>"
569 "<l xmlns=\"urn:tests:c\">val2</l>"
570 "<l xmlns=\"urn:tests:c\">val3</l>"
571 "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>"
572 "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>"
573 "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>"
574 "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>"
575 "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200576 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 +0100577 assert_null(tree);
578 logbuf_assert("Too many \"lt\" instances. /c:lt");
579
580 *state = NULL;
581}
582
Michal Vasko14654712020-02-06 08:35:21 +0100583static void
584test_unique(void **state)
585{
586 *state = test_unique;
587
588 const char *data;
589 struct lyd_node *tree;
590
591 data =
592 "<lt xmlns=\"urn:tests:d\">"
593 "<k>val1</k>"
594 "<l1>same</l1>"
595 "</lt>"
596 "<lt xmlns=\"urn:tests:d\">"
597 "<k>val2</k>"
598 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200599 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 +0100600 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100601 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100602
603 data =
604 "<lt xmlns=\"urn:tests:d\">"
605 "<k>val1</k>"
606 "<l1>same</l1>"
607 "</lt>"
608 "<lt xmlns=\"urn:tests:d\">"
609 "<k>val2</k>"
610 "<l1>not-same</l1>"
611 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200612 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 +0100613 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100614 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100615
616 data =
617 "<lt xmlns=\"urn:tests:d\">"
618 "<k>val1</k>"
619 "<l1>same</l1>"
620 "</lt>"
621 "<lt xmlns=\"urn:tests:d\">"
622 "<k>val2</k>"
623 "<l1>same</l1>"
624 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200625 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 +0100626 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100627 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 +0100628
629 /* now try with more instances */
630 data =
631 "<lt xmlns=\"urn:tests:d\">"
632 "<k>val1</k>"
633 "<l1>1</l1>"
634 "</lt>"
635 "<lt xmlns=\"urn:tests:d\">"
636 "<k>val2</k>"
637 "<l1>2</l1>"
638 "</lt>"
639 "<lt xmlns=\"urn:tests:d\">"
640 "<k>val3</k>"
641 "<l1>3</l1>"
642 "</lt>"
643 "<lt xmlns=\"urn:tests:d\">"
644 "<k>val4</k>"
645 "<l1>4</l1>"
646 "</lt>"
647 "<lt xmlns=\"urn:tests:d\">"
648 "<k>val5</k>"
649 "<l1>5</l1>"
650 "</lt>"
651 "<lt xmlns=\"urn:tests:d\">"
652 "<k>val6</k>"
653 "<l1>6</l1>"
654 "</lt>"
655 "<lt xmlns=\"urn:tests:d\">"
656 "<k>val7</k>"
657 "<l1>7</l1>"
658 "</lt>"
659 "<lt xmlns=\"urn:tests:d\">"
660 "<k>val8</k>"
661 "<l1>8</l1>"
662 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200663 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 +0100664 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100665 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100666
667 data =
668 "<lt xmlns=\"urn:tests:d\">"
669 "<k>val1</k>"
670 "<l1>1</l1>"
671 "</lt>"
672 "<lt xmlns=\"urn:tests:d\">"
673 "<k>val2</k>"
674 "<l1>2</l1>"
675 "</lt>"
676 "<lt xmlns=\"urn:tests:d\">"
677 "<k>val3</k>"
678 "<l1>3</l1>"
679 "</lt>"
680 "<lt xmlns=\"urn:tests:d\">"
681 "<k>val4</k>"
682 "</lt>"
683 "<lt xmlns=\"urn:tests:d\">"
684 "<k>val5</k>"
685 "<l1>5</l1>"
686 "</lt>"
687 "<lt xmlns=\"urn:tests:d\">"
688 "<k>val6</k>"
689 "<l1>6</l1>"
690 "</lt>"
691 "<lt xmlns=\"urn:tests:d\">"
692 "<k>val7</k>"
693 "</lt>"
694 "<lt xmlns=\"urn:tests:d\">"
695 "<k>val8</k>"
696 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200697 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 +0100698 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100699 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100700
701 data =
702 "<lt xmlns=\"urn:tests:d\">"
703 "<k>val1</k>"
704 "<l1>1</l1>"
705 "</lt>"
706 "<lt xmlns=\"urn:tests:d\">"
707 "<k>val2</k>"
708 "<l1>2</l1>"
709 "</lt>"
710 "<lt xmlns=\"urn:tests:d\">"
711 "<k>val3</k>"
712 "</lt>"
713 "<lt xmlns=\"urn:tests:d\">"
714 "<k>val4</k>"
715 "<l1>4</l1>"
716 "</lt>"
717 "<lt xmlns=\"urn:tests:d\">"
718 "<k>val5</k>"
719 "</lt>"
720 "<lt xmlns=\"urn:tests:d\">"
721 "<k>val6</k>"
722 "</lt>"
723 "<lt xmlns=\"urn:tests:d\">"
724 "<k>val7</k>"
725 "<l1>2</l1>"
726 "</lt>"
727 "<lt xmlns=\"urn:tests:d\">"
728 "<k>val8</k>"
729 "<l1>8</l1>"
730 "</lt>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 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 +0100732 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100733 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 +0100734
735 *state = NULL;
736}
737
738static void
739test_unique_nested(void **state)
740{
741 *state = test_unique_nested;
742
743 const char *data;
744 struct lyd_node *tree;
745
746 /* nested list uniquest are compared only with instances in the same parent list instance */
747 data =
748 "<lt2 xmlns=\"urn:tests:d\">"
749 "<k>val1</k>"
750 "<cont>"
751 "<l2>1</l2>"
752 "</cont>"
753 "<l4>1</l4>"
754 "</lt2>"
755 "<lt2 xmlns=\"urn:tests:d\">"
756 "<k>val2</k>"
757 "<cont>"
758 "<l2>2</l2>"
759 "</cont>"
760 "<l4>2</l4>"
761 "<lt3>"
762 "<kk>val1</kk>"
763 "<l3>1</l3>"
764 "</lt3>"
765 "<lt3>"
766 "<kk>val2</kk>"
767 "<l3>2</l3>"
768 "</lt3>"
769 "</lt2>"
770 "<lt2 xmlns=\"urn:tests:d\">"
771 "<k>val3</k>"
772 "<cont>"
773 "<l2>3</l2>"
774 "</cont>"
775 "<l4>3</l4>"
776 "<lt3>"
777 "<kk>val1</kk>"
778 "<l3>2</l3>"
779 "</lt3>"
780 "</lt2>"
781 "<lt2 xmlns=\"urn:tests:d\">"
782 "<k>val4</k>"
783 "<cont>"
784 "<l2>4</l2>"
785 "</cont>"
786 "<l4>4</l4>"
787 "<lt3>"
788 "<kk>val1</kk>"
789 "<l3>3</l3>"
790 "</lt3>"
791 "</lt2>"
792 "<lt2 xmlns=\"urn:tests:d\">"
793 "<k>val5</k>"
794 "<cont>"
795 "<l2>5</l2>"
796 "</cont>"
797 "<l4>5</l4>"
798 "<lt3>"
799 "<kk>val1</kk>"
800 "<l3>3</l3>"
801 "</lt3>"
802 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 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 +0100804 assert_non_null(tree);
Michal Vaskof03ed032020-03-04 13:31:44 +0100805 lyd_free_siblings(tree);
Michal Vasko14654712020-02-06 08:35:21 +0100806
807 data =
808 "<lt2 xmlns=\"urn:tests:d\">"
809 "<k>val1</k>"
810 "<cont>"
811 "<l2>1</l2>"
812 "</cont>"
813 "<l4>1</l4>"
814 "</lt2>"
815 "<lt2 xmlns=\"urn:tests:d\">"
816 "<k>val2</k>"
817 "<cont>"
818 "<l2>2</l2>"
819 "</cont>"
820 "<lt3>"
821 "<kk>val1</kk>"
822 "<l3>1</l3>"
823 "</lt3>"
824 "<lt3>"
825 "<kk>val2</kk>"
826 "<l3>2</l3>"
827 "</lt3>"
828 "<lt3>"
829 "<kk>val3</kk>"
830 "<l3>1</l3>"
831 "</lt3>"
832 "</lt2>"
833 "<lt2 xmlns=\"urn:tests:d\">"
834 "<k>val3</k>"
835 "<cont>"
836 "<l2>3</l2>"
837 "</cont>"
838 "<l4>1</l4>"
839 "<lt3>"
840 "<kk>val1</kk>"
841 "<l3>2</l3>"
842 "</lt3>"
843 "</lt2>"
844 "<lt2 xmlns=\"urn:tests:d\">"
845 "<k>val4</k>"
846 "<cont>"
847 "<l2>4</l2>"
848 "</cont>"
849 "<lt3>"
850 "<kk>val1</kk>"
851 "<l3>3</l3>"
852 "</lt3>"
853 "</lt2>"
854 "<lt2 xmlns=\"urn:tests:d\">"
855 "<k>val5</k>"
856 "<cont>"
857 "<l2>5</l2>"
858 "</cont>"
859 "<lt3>"
860 "<kk>val1</kk>"
861 "<l3>3</l3>"
862 "</lt3>"
863 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200864 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 +0100865 assert_null(tree);
866 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 +0100867 " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']");
Michal Vasko14654712020-02-06 08:35:21 +0100868
869 data =
870 "<lt2 xmlns=\"urn:tests:d\">"
871 "<k>val1</k>"
872 "<cont>"
873 "<l2>1</l2>"
874 "</cont>"
875 "<l4>1</l4>"
876 "</lt2>"
877 "<lt2 xmlns=\"urn:tests:d\">"
878 "<k>val2</k>"
879 "<cont>"
880 "<l2>2</l2>"
881 "</cont>"
882 "<l4>2</l4>"
883 "</lt2>"
884 "<lt2 xmlns=\"urn:tests:d\">"
885 "<k>val3</k>"
886 "<cont>"
887 "<l2>3</l2>"
888 "</cont>"
889 "<l4>3</l4>"
890 "</lt2>"
891 "<lt2 xmlns=\"urn:tests:d\">"
892 "<k>val4</k>"
893 "<cont>"
894 "<l2>2</l2>"
895 "</cont>"
896 "<l4>2</l4>"
897 "</lt2>"
898 "<lt2 xmlns=\"urn:tests:d\">"
899 "<k>val5</k>"
900 "<cont>"
901 "<l2>5</l2>"
902 "</cont>"
903 "<l4>5</l4>"
904 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 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 +0100906 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100907 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 +0100908
909 data =
910 "<lt2 xmlns=\"urn:tests:d\">"
911 "<k>val1</k>"
912 "<cont>"
913 "<l2>1</l2>"
914 "</cont>"
915 "<l4>1</l4>"
916 "<l5>1</l5>"
917 "<l6>1</l6>"
918 "</lt2>"
919 "<lt2 xmlns=\"urn:tests:d\">"
920 "<k>val2</k>"
921 "<cont>"
922 "<l2>2</l2>"
923 "</cont>"
924 "<l4>1</l4>"
925 "<l5>1</l5>"
926 "</lt2>"
927 "<lt2 xmlns=\"urn:tests:d\">"
928 "<k>val3</k>"
929 "<cont>"
930 "<l2>3</l2>"
931 "</cont>"
932 "<l4>1</l4>"
933 "<l5>3</l5>"
934 "<l6>3</l6>"
935 "</lt2>"
936 "<lt2 xmlns=\"urn:tests:d\">"
937 "<k>val4</k>"
938 "<cont>"
939 "<l2>4</l2>"
940 "</cont>"
941 "<l4>1</l4>"
942 "<l6>1</l6>"
943 "</lt2>"
944 "<lt2 xmlns=\"urn:tests:d\">"
945 "<k>val5</k>"
946 "<cont>"
947 "<l2>5</l2>"
948 "</cont>"
949 "<l4>1</l4>"
950 "<l5>3</l5>"
951 "<l6>3</l6>"
952 "</lt2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200953 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 +0100954 assert_null(tree);
Michal Vasko9b368d32020-02-14 13:53:31 +0100955 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 +0100956
957 *state = NULL;
958}
959
Michal Vaskof03ed032020-03-04 13:31:44 +0100960static void
961test_dup(void **state)
962{
963 *state = test_dup;
964
965 const char *data;
966 struct lyd_node *tree;
967
968 data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200969 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 +0100970 assert_null(tree);
971 logbuf_assert("Duplicate instance of \"d\". /e:d");
972
973 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 +0200974 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 +0100975 assert_null(tree);
976 logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']");
977
978 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 +0200979 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 +0100980 assert_null(tree);
981 logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']");
982
983 data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 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 +0100985 assert_null(tree);
986 logbuf_assert("Duplicate instance of \"cont\". /e:cont");
987
988 /* same tests again but using hashes */
989 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 +0200990 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 +0100991 assert_null(tree);
992 logbuf_assert("Duplicate instance of \"d\". /e:cont/d");
993
994 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
995 "<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 +0200996 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 +0100997 assert_null(tree);
998 logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']");
999
1000 data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>"
1001 "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001002 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 +01001003 assert_null(tree);
1004 logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']");
1005
1006 /* cases */
1007 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 +02001008 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 +01001009 assert_null(tree);
1010 logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']");
1011
1012 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 +02001013 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 +01001014 assert_null(tree);
1015 logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic");
1016
1017 *state = NULL;
1018}
1019
1020static void
1021test_defaults(void **state)
1022{
1023 *state = test_defaults;
1024
Michal Vaskof03ed032020-03-04 13:31:44 +01001025 char *str;
Michal Vasko8104fd42020-07-13 11:09:51 +02001026 struct lyd_node *tree, *node, *diff;
Michal Vaskof03ed032020-03-04 13:31:44 +01001027 const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f");
1028
Radek Krejci241f6b52020-05-21 18:13:49 +02001029 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +02001030 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +01001031
Michal Vaskob1b5c262020-03-05 14:29:47 +01001032 /* get defaults */
1033 tree = NULL;
Michal Vasko8104fd42020-07-13 11:09:51 +02001034 assert_int_equal(lyd_validate_module(&tree, mod, 0, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001035 assert_non_null(tree);
Michal Vasko8104fd42020-07-13 11:09:51 +02001036 assert_non_null(diff);
Michal Vaskof03ed032020-03-04 13:31:44 +01001037
1038 /* check all defaults exist */
Radek Krejci52f65552020-09-01 17:03:35 +02001039 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskof03ed032020-03-04 13:31:44 +01001040 assert_string_equal(str,
Michal Vaskof03ed032020-03-04 13:31:44 +01001041 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1042 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1043 "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1044 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1045 "<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 +01001046 "<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 +01001047 "<cont xmlns=\"urn:tests:f\">"
1048 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1049 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1050 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1051 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1052 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1053 "<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 +01001054 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001055 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001056
Michal Vasko8104fd42020-07-13 11:09:51 +02001057 /* check diff */
Radek Krejci52f65552020-09-01 17:03:35 +02001058 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK);
Michal Vasko8104fd42020-07-13 11:09:51 +02001059 assert_string_equal(str,
1060 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def1</ll1>"
1061 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def2</ll1>"
1062 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def3</ll1>"
1063 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">15</d>"
1064 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt1</ll2>"
1065 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt2</ll2>"
Michal Vaskoe49b6322020-11-05 17:38:36 +01001066 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">"
Michal Vasko8104fd42020-07-13 11:09:51 +02001067 "<ll1 yang:operation=\"create\">def1</ll1>"
1068 "<ll1 yang:operation=\"create\">def2</ll1>"
1069 "<ll1 yang:operation=\"create\">def3</ll1>"
1070 "<d yang:operation=\"create\">15</d>"
1071 "<ll2 yang:operation=\"create\">dflt1</ll2>"
1072 "<ll2 yang:operation=\"create\">dflt2</ll2>"
1073 "</cont>"
1074 );
1075 ly_out_reset(out);
1076 lyd_free_siblings(diff);
1077
Michal Vaskof03ed032020-03-04 13:31:44 +01001078 /* create another explicit case and validate */
Radek Krejci41ac9942020-11-02 14:47:56 +01001079 assert_int_equal(lyd_new_term(NULL, mod, "l", "value", 0, &node), LY_SUCCESS);
Michal Vaskob104f112020-07-17 09:54:54 +02001080 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001081 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001082
1083 /* check data tree */
Radek Krejci52f65552020-09-01 17:03:35 +02001084 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskof03ed032020-03-04 13:31:44 +01001085 assert_string_equal(str,
Michal Vaskob104f112020-07-17 09:54:54 +02001086 "<l xmlns=\"urn:tests:f\">value</l>"
Michal Vaskob1b5c262020-03-05 14:29:47 +01001087 "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1088 "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1089 "<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 +01001090 "<cont xmlns=\"urn:tests:f\">"
1091 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1092 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1093 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1094 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1095 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1096 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob104f112020-07-17 09:54:54 +02001097 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001098 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001099
Michal Vasko8104fd42020-07-13 11:09:51 +02001100 /* check diff */
Radek Krejci52f65552020-09-01 17:03:35 +02001101 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK);
Michal Vasko8104fd42020-07-13 11:09:51 +02001102 assert_string_equal(str,
1103 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def1</ll1>"
1104 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def2</ll1>"
1105 "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def3</ll1>"
1106 );
1107 ly_out_reset(out);
1108 lyd_free_siblings(diff);
1109
Michal Vaskof03ed032020-03-04 13:31:44 +01001110 /* create explicit leaf-list and leaf and validate */
Radek Krejci41ac9942020-11-02 14:47:56 +01001111 assert_int_equal(lyd_new_term(NULL, mod, "d", "15", 0, &node), LY_SUCCESS);
Michal Vaskob104f112020-07-17 09:54:54 +02001112 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
Radek Krejci41ac9942020-11-02 14:47:56 +01001113 assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", 0, &node), LY_SUCCESS);
Michal Vaskob104f112020-07-17 09:54:54 +02001114 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001115 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001116
1117 /* check data tree */
Radek Krejci52f65552020-09-01 17:03:35 +02001118 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskof03ed032020-03-04 13:31:44 +01001119 assert_string_equal(str,
Michal Vaskob104f112020-07-17 09:54:54 +02001120 "<l xmlns=\"urn:tests:f\">value</l>"
1121 "<d xmlns=\"urn:tests:f\">15</d>"
1122 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001123 "<cont xmlns=\"urn:tests:f\">"
1124 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1125 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1126 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1127 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1128 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1129 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob104f112020-07-17 09:54:54 +02001130 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001131 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001132
Michal Vasko8104fd42020-07-13 11:09:51 +02001133 /* check diff */
Radek Krejci52f65552020-09-01 17:03:35 +02001134 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK);
Michal Vasko8104fd42020-07-13 11:09:51 +02001135 assert_string_equal(str,
1136 "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">15</d>"
1137 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt1</ll2>"
1138 "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt2</ll2>"
1139 );
1140 ly_out_reset(out);
1141 lyd_free_siblings(diff);
1142
Michal Vaskob1b5c262020-03-05 14:29:47 +01001143 /* create first explicit container, which should become implicit */
Radek Krejci41ac9942020-11-02 14:47:56 +01001144 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
Michal Vaskob104f112020-07-17 09:54:54 +02001145 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001146 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001147
1148 /* check data tree */
Radek Krejci52f65552020-09-01 17:03:35 +02001149 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001150 assert_string_equal(str,
Michal Vaskob104f112020-07-17 09:54:54 +02001151 "<l xmlns=\"urn:tests:f\">value</l>"
1152 "<d xmlns=\"urn:tests:f\">15</d>"
1153 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001154 "<cont xmlns=\"urn:tests:f\">"
1155 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1156 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1157 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1158 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1159 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1160 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob104f112020-07-17 09:54:54 +02001161 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001162 ly_out_reset(out);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001163
Michal Vasko8104fd42020-07-13 11:09:51 +02001164 /* check diff */
1165 assert_null(diff);
1166
Michal Vaskob1b5c262020-03-05 14:29:47 +01001167 /* create second explicit container, which should become implicit, so the first tree node should be removed */
Radek Krejci41ac9942020-11-02 14:47:56 +01001168 assert_int_equal(lyd_new_inner(NULL, mod, "cont", 0, &node), LY_SUCCESS);
Michal Vaskob104f112020-07-17 09:54:54 +02001169 assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001170 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001171
1172 /* check data tree */
Radek Krejci52f65552020-09-01 17:03:35 +02001173 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001174 assert_string_equal(str,
Michal Vaskob104f112020-07-17 09:54:54 +02001175 "<l xmlns=\"urn:tests:f\">value</l>"
1176 "<d xmlns=\"urn:tests:f\">15</d>"
1177 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001178 "<cont xmlns=\"urn:tests:f\">"
1179 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
1180 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
1181 "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>"
1182 "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
1183 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
1184 "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
Michal Vaskob104f112020-07-17 09:54:54 +02001185 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001186 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001187
Michal Vasko8104fd42020-07-13 11:09:51 +02001188 /* check diff */
1189 assert_null(diff);
1190
Michal Vaskof03ed032020-03-04 13:31:44 +01001191 /* similar changes for nested defaults */
Radek Krejci41ac9942020-11-02 14:47:56 +01001192 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll1", "def3", 0, NULL), LY_SUCCESS);
1193 assert_int_equal(lyd_new_term(tree->prev, NULL, "d", "5", 0, NULL), LY_SUCCESS);
1194 assert_int_equal(lyd_new_term(tree->prev, NULL, "ll2", "non-dflt", 0, NULL), LY_SUCCESS);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001195 assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
Michal Vaskof03ed032020-03-04 13:31:44 +01001196
1197 /* check data tree */
Radek Krejci52f65552020-09-01 17:03:35 +02001198 lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK);
Michal Vaskof03ed032020-03-04 13:31:44 +01001199 assert_string_equal(str,
Michal Vaskob104f112020-07-17 09:54:54 +02001200 "<l xmlns=\"urn:tests:f\">value</l>"
1201 "<d xmlns=\"urn:tests:f\">15</d>"
1202 "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
Michal Vasko52927e22020-03-16 17:26:14 +01001203 "<cont xmlns=\"urn:tests:f\">"
Michal Vaskof03ed032020-03-04 13:31:44 +01001204 "<ll1>def3</ll1>"
1205 "<d>5</d>"
1206 "<ll2>non-dflt</ll2>"
Michal Vaskob104f112020-07-17 09:54:54 +02001207 "</cont>");
Radek Krejci241f6b52020-05-21 18:13:49 +02001208 ly_out_reset(out);
Michal Vaskof03ed032020-03-04 13:31:44 +01001209
Michal Vasko8104fd42020-07-13 11:09:51 +02001210 /* check diff */
Radek Krejci52f65552020-09-01 17:03:35 +02001211 lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK);
Michal Vasko8104fd42020-07-13 11:09:51 +02001212 assert_string_equal(str,
1213 "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
1214 "<ll1 yang:operation=\"delete\">def1</ll1>"
1215 "<ll1 yang:operation=\"delete\">def2</ll1>"
1216 "<ll1 yang:operation=\"delete\">def3</ll1>"
1217 "<d yang:operation=\"delete\">15</d>"
1218 "<ll2 yang:operation=\"delete\">dflt1</ll2>"
1219 "<ll2 yang:operation=\"delete\">dflt2</ll2>"
1220 "</cont>"
1221 );
1222 ly_out_reset(out);
1223 lyd_free_siblings(diff);
1224
Michal Vaskof03ed032020-03-04 13:31:44 +01001225 lyd_free_siblings(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +02001226 ly_out_free(out, NULL, 1);
Michal Vaskof03ed032020-03-04 13:31:44 +01001227
1228 *state = NULL;
1229}
1230
Michal Vaskoc193ce92020-03-06 11:04:48 +01001231static void
Michal Vasko5b37a352020-03-06 13:38:33 +01001232test_state(void **state)
1233{
Michal Vaskocc048b22020-03-27 15:52:38 +01001234 *state = test_state;
Michal Vasko5b37a352020-03-06 13:38:33 +01001235
1236 const char *data;
1237 struct lyd_node *tree;
1238
1239 data =
1240 "<cont xmlns=\"urn:tests:h\">"
1241 "<cont2>"
1242 "<l>val</l>"
1243 "</cont2>"
1244 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001245 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 +01001246 assert_null(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +02001247 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
Michal Vasko5b37a352020-03-06 13:38:33 +01001248
Michal Vasko63f3d842020-07-08 10:10:14 +02001249 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 +01001250 assert_null(tree);
1251 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1252
Michal Vasko63f3d842020-07-08 10:10:14 +02001253 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 +01001254 assert_non_null(tree);
1255
Michal Vasko3a41dff2020-07-15 14:30:28 +02001256 assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, NULL));
Michal Vasko5b37a352020-03-06 13:38:33 +01001257 logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2");
1258
1259 lyd_free_siblings(tree);
1260
1261 *state = NULL;
1262}
1263
Michal Vaskocc048b22020-03-27 15:52:38 +01001264static void
1265test_must(void **state)
1266{
1267 *state = test_must;
1268
1269 const char *data;
1270 struct lyd_node *tree;
1271
1272 data =
1273 "<cont xmlns=\"urn:tests:i\">"
1274 "<l>wrong</l>"
1275 "<l2>val</l2>"
1276 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001277 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 +01001278 assert_null(tree);
1279 logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2");
1280
1281 data =
1282 "<cont xmlns=\"urn:tests:i\">"
1283 "<l>right</l>"
1284 "<l2>val</l2>"
1285 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001286 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 +01001287 assert_non_null(tree);
1288 lyd_free_tree(tree);
1289
1290 *state = NULL;
1291}
1292
Michal Vaskofea12c62020-03-30 11:00:15 +02001293static void
1294test_action(void **state)
1295{
1296 *state = test_action;
1297
1298 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001299 struct ly_in *in;
Michal Vaskofea12c62020-03-30 11:00:15 +02001300 struct lyd_node *tree, *op_tree;
Michal Vaskofea12c62020-03-30 11:00:15 +02001301
1302 data =
1303 "<cont xmlns=\"urn:tests:j\">"
1304 "<l1>"
1305 "<k>val1</k>"
1306 "<act>"
1307 "<lf2>target</lf2>"
1308 "</act>"
1309 "</l1>"
1310 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001311 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1312 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &op_tree, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001313 assert_non_null(op_tree);
1314
1315 /* missing leafref */
Michal Vasko8104fd42020-07-13 11:09:51 +02001316 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_RPC, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +02001317 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value."
1318 " /j:cont/l1[k='val1']/act/lf2");
Michal Vasko63f3d842020-07-08 10:10:14 +02001319 ly_in_free(in, 0);
Michal Vaskofea12c62020-03-30 11:00:15 +02001320
1321 data =
1322 "<cont xmlns=\"urn:tests:j\">"
1323 "<lf1>not true</lf1>"
1324 "</cont>"
1325 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko0f3377d2020-11-09 20:56:11 +01001326 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001327 assert_non_null(tree);
1328
Michal Vaskofea12c62020-03-30 11:00:15 +02001329 /* input must false */
Michal Vasko8104fd42020-07-13 11:09:51 +02001330 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001331 logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act");
1332
1333 lyd_free_siblings(tree);
1334 data =
1335 "<cont xmlns=\"urn:tests:j\">"
1336 "<lf1>true</lf1>"
1337 "</cont>"
1338 "<lf3 xmlns=\"urn:tests:j\">target</lf3>";
Michal Vasko0f3377d2020-11-09 20:56:11 +01001339 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskofea12c62020-03-30 11:00:15 +02001340 assert_non_null(tree);
1341
1342 /* success */
Michal Vasko8104fd42020-07-13 11:09:51 +02001343 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL));
Michal Vaskofea12c62020-03-30 11:00:15 +02001344
Michal Vaskocb7526d2020-03-30 15:08:26 +02001345 lyd_free_tree(op_tree);
1346 lyd_free_siblings(tree);
1347
1348 *state = NULL;
1349}
1350
1351static void
1352test_reply(void **state)
1353{
1354 *state = test_reply;
1355
1356 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +02001357 struct ly_in *in;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001358 struct lyd_node *tree, *op_tree, *request;
Michal Vaskocb7526d2020-03-30 15:08:26 +02001359
1360 data =
1361 "<cont xmlns=\"urn:tests:j\">"
1362 "<l1>"
1363 "<k>val1</k>"
1364 "<act>"
1365 "<lf2>target</lf2>"
1366 "</act>"
1367 "</l1>"
1368 "</cont>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001369 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1370 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &request, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001371 assert_non_null(request);
Michal Vasko63f3d842020-07-08 10:10:14 +02001372 ly_in_free(in, 0);
1373
Michal Vaskocb7526d2020-03-30 15:08:26 +02001374 data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>";
Michal Vasko63f3d842020-07-08 10:10:14 +02001375 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1376 assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_XML, &op_tree, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001377 lyd_free_all(request);
1378 assert_non_null(op_tree);
Michal Vasko63f3d842020-07-08 10:10:14 +02001379 ly_in_free(in, 0);
Michal Vaskocb7526d2020-03-30 15:08:26 +02001380
1381 /* missing leafref */
Michal Vasko8104fd42020-07-13 11:09:51 +02001382 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +02001383 logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value."
1384 " /j:cont/l1[k='val1']/act/lf2");
Michal Vaskocb7526d2020-03-30 15:08:26 +02001385
1386 data =
1387 "<cont xmlns=\"urn:tests:j\">"
1388 "<lf1>not true</lf1>"
1389 "</cont>"
1390 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko0f3377d2020-11-09 20:56:11 +01001391 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001392 assert_non_null(tree);
1393
Michal Vaskocb7526d2020-03-30 15:08:26 +02001394 /* input must false */
Michal Vasko8104fd42020-07-13 11:09:51 +02001395 assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001396 logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act");
1397
1398 lyd_free_siblings(tree);
1399 data =
1400 "<cont xmlns=\"urn:tests:j\">"
1401 "<lf1>true2</lf1>"
1402 "</cont>"
1403 "<lf4 xmlns=\"urn:tests:j\">target</lf4>";
Michal Vasko0f3377d2020-11-09 20:56:11 +01001404 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001405 assert_non_null(tree);
1406
1407 /* success */
Michal Vasko8104fd42020-07-13 11:09:51 +02001408 assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL));
Michal Vaskocb7526d2020-03-30 15:08:26 +02001409
Michal Vaskofea12c62020-03-30 11:00:15 +02001410 lyd_free_tree(op_tree);
1411 lyd_free_siblings(tree);
1412
1413 *state = NULL;
1414}
1415
Michal Vaskocde73ac2019-11-14 16:10:27 +01001416int main(void)
1417{
1418 const struct CMUnitTest tests[] = {
Michal Vaskoacd83e72020-02-04 14:12:01 +01001419 cmocka_unit_test_teardown(test_when, teardown_s),
1420 cmocka_unit_test_teardown(test_mandatory, teardown_s),
1421 cmocka_unit_test_teardown(test_minmax, teardown_s),
Michal Vasko14654712020-02-06 08:35:21 +01001422 cmocka_unit_test_teardown(test_unique, teardown_s),
1423 cmocka_unit_test_teardown(test_unique_nested, teardown_s),
Michal Vaskof03ed032020-03-04 13:31:44 +01001424 cmocka_unit_test_teardown(test_dup, teardown_s),
1425 cmocka_unit_test_teardown(test_defaults, teardown_s),
Michal Vasko5b37a352020-03-06 13:38:33 +01001426 cmocka_unit_test_teardown(test_state, teardown_s),
Michal Vaskocc048b22020-03-27 15:52:38 +01001427 cmocka_unit_test_teardown(test_must, teardown_s),
Michal Vaskofea12c62020-03-30 11:00:15 +02001428 cmocka_unit_test_teardown(test_action, teardown_s),
Michal Vaskocb7526d2020-03-30 15:08:26 +02001429 cmocka_unit_test_teardown(test_reply, teardown_s),
Michal Vaskocde73ac2019-11-14 16:10:27 +01001430 };
1431
Michal Vaskoacd83e72020-02-04 14:12:01 +01001432 return cmocka_run_group_tests(tests, setup, teardown);
Michal Vaskocde73ac2019-11-14 16:10:27 +01001433}