blob: 69197331b0c9d2fe8dfe21b31e50d68bc4c2cc25 [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file test_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Cmocka tests for LYB binary data format.
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020016
Michal Vasko60ea6352020-06-29 13:39:39 +020017#include "hash_table.h"
18#include "libyang.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020019
aPiecekfbfcba42021-09-13 13:53:44 +020020#define CHECK_PARSE_LYD(INPUT, OUT_NODE) \
21 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, LY_SUCCESS, OUT_NODE)
Michal Vasko60ea6352020-06-29 13:39:39 +020022
Radek Iša56ca9e42020-09-08 18:42:00 +020023#define CHECK_LYD_STRING(MODEL, TEXT) \
24 CHECK_LYD_STRING_PARAM(MODEL, TEXT, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK)
Michal Vasko60ea6352020-06-29 13:39:39 +020025
aPiecekfbfcba42021-09-13 13:53:44 +020026#define CHECK_PRINT_THEN_PARSE(DATA_XML) \
27 { \
28 struct lyd_node *tree_1; \
29 struct lyd_node *tree_2; \
30 char *xml_out; \
31 CHECK_PARSE_LYD(DATA_XML, tree_1); \
32 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0); \
33 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2)); \
34 assert_non_null(tree_2); \
35 CHECK_LYD(tree_1, tree_2); \
36 free(xml_out); \
37 lyd_free_all(tree_1); \
38 lyd_free_all(tree_2); \
39 }
40
Michal Vasko60ea6352020-06-29 13:39:39 +020041static int
Radek Iša56ca9e42020-09-08 18:42:00 +020042setup(void **state)
Michal Vasko60ea6352020-06-29 13:39:39 +020043{
Radek Iša56ca9e42020-09-08 18:42:00 +020044 UTEST_SETUP;
45 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Michal Vasko60ea6352020-06-29 13:39:39 +020046
47 return 0;
48}
49
50static void
aPiecek3c6cf2f2021-09-21 14:15:50 +020051tests_leaflist(void **state)
52{
53 const char *mod;
54 const char *data_xml;
55
56 mod =
57 "module mod { namespace \"urn:test-leaflist\"; prefix m;"
58 " container cont {"
59 " presence \"\";"
60 " leaf-list ll {"
61 " type uint8;"
62 " }"
63 " }"
64 "}";
65 UTEST_ADD_MODULE(mod, LYS_IN_YANG, NULL, NULL);
66
67 data_xml =
68 "<cont xmlns=\"urn:test-leaflist\">\n"
69 "</cont>\n";
70 CHECK_PRINT_THEN_PARSE(data_xml);
71
72 data_xml =
73 "<cont xmlns=\"urn:test-leaflist\">\n"
74 " <ll>1</ll>\n"
75 "</cont>\n";
76 CHECK_PRINT_THEN_PARSE(data_xml);
77
78 data_xml =
79 "<cont xmlns=\"urn:test-leaflist\">\n"
80 " <ll>1</ll>\n"
81 " <ll>2</ll>\n"
82 "</cont>\n";
83 CHECK_PRINT_THEN_PARSE(data_xml);
84}
85
86static void
87tests_list(void **state)
88{
89 const char *mod;
90 const char *data_xml;
91
92 mod =
93 "module mod { namespace \"urn:test-list\"; prefix m;"
94 " container cont {"
95 " presence \"\";"
96 " list lst {"
97 " key \"lf\";"
98 " leaf lf {"
99 " type uint8;"
100 " }"
101 " }"
102 " }"
103 "}";
104 UTEST_ADD_MODULE(mod, LYS_IN_YANG, NULL, NULL);
105
106 data_xml =
107 "<cont xmlns=\"urn:test-list\">\n"
108 " <lst>"
109 " <lf>1</lf>"
110 " </lst>"
111 "</cont>\n";
112 CHECK_PRINT_THEN_PARSE(data_xml);
113
114 data_xml =
115 "<cont xmlns=\"urn:test-list\">\n"
116 " <lst>"
117 " <lf>1</lf>"
118 " <lf>2</lf>"
119 " </lst>"
120 "</cont>\n";
121 CHECK_PRINT_THEN_PARSE(data_xml);
122}
123
124static void
125tests_any(void **state)
126{
127 const char *mod;
128 const char *data_xml;
129
130 mod =
131 "module mod { namespace \"urn:test-any\"; prefix m;"
132 " container cont {"
133 " presence \"\";"
134 " anyxml anxml;\n"
135 " }"
136 "}";
137 UTEST_ADD_MODULE(mod, LYS_IN_YANG, NULL, NULL);
138
139 data_xml =
140 "<cont xmlns=\"urn:test-any\">\n"
141 "</cont>\n";
142 CHECK_PRINT_THEN_PARSE(data_xml);
143
144 data_xml =
145 "<cont xmlns=\"urn:test-any\">\n"
146 " <anxml><node>value</node></anxml>\n"
147 "</cont>\n";
148 CHECK_PRINT_THEN_PARSE(data_xml);
149
150 data_xml =
151 "<cont xmlns=\"urn:test-any\">\n"
152 " <anxml><node1>value</node1></anxml>\n"
153 " <anxml><node2>value</node2></anxml>\n"
154 "</cont>\n";
155 CHECK_PRINT_THEN_PARSE(data_xml);
156}
157
158static void
Michal Vasko60ea6352020-06-29 13:39:39 +0200159test_ietf_interfaces(void **state)
160{
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100162 "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
163 " <interface>\n"
164 " <name>eth0</name>\n"
165 " <description>Ethernet 0</description>\n"
166 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
167 " <enabled>true</enabled>\n"
168 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
169 " <enabled>true</enabled>\n"
170 " <mtu>1500</mtu>\n"
171 " <address>\n"
172 " <ip>192.168.2.100</ip>\n"
173 " <prefix-length>24</prefix-length>\n"
174 " </address>\n"
175 " </ipv4>\n"
176 " </interface>\n"
177 " <interface>\n"
178 " <name>eth1</name>\n"
179 " <description>Ethernet 1</description>\n"
180 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
181 " <enabled>true</enabled>\n"
182 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
183 " <enabled>true</enabled>\n"
184 " <mtu>1500</mtu>\n"
185 " <address>\n"
186 " <ip>10.10.1.5</ip>\n"
187 " <prefix-length>16</prefix-length>\n"
188 " </address>\n"
189 " </ipv4>\n"
190 " </interface>\n"
191 " <interface>\n"
192 " <name>gigaeth0</name>\n"
193 " <description>GigabitEthernet 0</description>\n"
194 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
195 " <enabled>false</enabled>\n"
196 " </interface>\n"
197 "</interfaces>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200198
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-ip", NULL, NULL));
200 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "iana-if-type", NULL, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200201
aPiecekfbfcba42021-09-13 13:53:44 +0200202 CHECK_PRINT_THEN_PARSE(data_xml);
Michal Vasko60ea6352020-06-29 13:39:39 +0200203}
204
205static void
206test_origin(void **state)
207{
Michal Vasko60ea6352020-06-29 13:39:39 +0200208 const char *origin_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100209 "module test-origin {"
210 " namespace \"urn:test-origin\";"
211 " prefix to;"
212 " import ietf-origin {"
213 " prefix or;"
214 " }"
215 ""
216 " container cont {"
217 " leaf leaf1 {"
218 " type string;"
219 " }"
220 " leaf leaf2 {"
221 " type string;"
222 " }"
223 " leaf leaf3 {"
224 " type uint8;"
225 " }"
226 " }"
227 "}";
Michal Vasko60ea6352020-06-29 13:39:39 +0200228 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100229 "<cont xmlns=\"urn:test-origin\">\n"
230 " <leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>\n"
231 " <leaf2>value2</leaf2>\n"
232 " <leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>\n"
233 "</cont>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200234
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 UTEST_ADD_MODULE(origin_yang, LYS_IN_YANG, NULL, NULL);
236 assert_int_equal(LY_SUCCESS, lys_set_implemented(ly_ctx_get_module_latest(UTEST_LYCTX, "ietf-origin"), NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200237
aPiecekfbfcba42021-09-13 13:53:44 +0200238 CHECK_PRINT_THEN_PARSE(data_xml);
Michal Vasko60ea6352020-06-29 13:39:39 +0200239}
240
241static void
242test_statements(void **state)
243{
Michal Vasko60ea6352020-06-29 13:39:39 +0200244 const char *links_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100245 "module links {\n"
246 " yang-version 1.1;\n"
247 " namespace \"urn:module2\";\n"
248 " prefix mod2;\n"
249 "\n"
250 " identity just-another-identity;\n"
251 "\n"
252 " leaf one-leaf {\n"
253 " type string;\n"
254 " }\n"
255 "\n"
256 " list list-for-augment {\n"
257 " key keyleaf;\n"
258 "\n"
259 " leaf keyleaf {\n"
260 " type string;\n"
261 " }\n"
262 "\n"
263 " leaf just-leaf {\n"
264 " type int32;\n"
265 " }\n"
266 " }\n"
267 "\n"
268 " leaf rleaf {\n"
269 " type string;\n"
270 " }\n"
271 "\n"
272 " leaf-list llist {\n"
273 " type string;\n"
274 " min-elements 0;\n"
275 " max-elements 100;\n"
276 " ordered-by user;\n"
277 " }\n"
278 "\n"
279 " grouping rgroup {\n"
280 " leaf rg1 {\n"
281 " type string;\n"
282 " }\n"
283 "\n"
284 " leaf rg2 {\n"
285 " type string;\n"
286 " }\n"
287 " }\n"
288 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200289
290 const char *statements_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100291 "module statements {\n"
292 " namespace \"urn:module\";\n"
293 " prefix mod;\n"
294 " yang-version 1.1;\n"
295 "\n"
296 " import links {\n"
297 " prefix mod2;\n"
298 " }\n"
299 "\n"
300 " identity random-identity {\n"
301 " base \"mod2:just-another-identity\";\n"
302 " base \"another-identity\";\n"
303 " }\n"
304 "\n"
305 " identity another-identity {\n"
306 " base \"mod2:just-another-identity\";\n"
307 " }\n"
308 "\n"
309 " typedef percent {\n"
310 " type uint8 {\n"
311 " range \"0 .. 100\";\n"
312 " }\n"
313 " units percent;\n"
314 " }\n"
315 "\n"
316 " container ice-cream-shop {\n"
317 " container employees {\n"
318 " list employee {\n"
319 " config true;\n"
320 " key id;\n"
321 " unique name;\n"
322 " min-elements 0;\n"
323 " max-elements 100;\n"
324 "\n"
325 " leaf id {\n"
326 " type uint64;\n"
327 " mandatory true;\n"
328 " }\n"
329 "\n"
330 " leaf name {\n"
331 " type string;\n"
332 " }\n"
333 "\n"
334 " leaf age {\n"
335 " type uint32;\n"
336 " }\n"
337 " }\n"
338 " }\n"
339 " }\n"
340 "\n"
341 " container random {\n"
342 " choice switch {\n"
343 " case a {\n"
344 " leaf aleaf {\n"
345 " type string;\n"
346 " default aaa;\n"
347 " }\n"
348 " }\n"
349 "\n"
350 " case c {\n"
351 " leaf cleaf {\n"
352 " type string;\n"
353 " }\n"
354 " }\n"
355 " }\n"
356 "\n"
357 " anyxml xml-data;\n"
358 " anydata any-data;\n"
359 " leaf-list leaflist {\n"
360 " type string;\n"
361 " min-elements 0;\n"
362 " max-elements 20;\n"
363 " ordered-by system;\n"
364 " }\n"
365 "\n"
366 " grouping group {\n"
367 " leaf g1 {\n"
368 " mandatory false;\n"
369 " type percent;\n"
370 " }\n"
371 "\n"
372 " leaf g2 {\n"
373 " type string;\n"
374 " }\n"
375 " }\n"
376 "\n"
377 " uses group;\n"
378 " uses mod2:rgroup;\n"
379 "\n"
380 " leaf lref {\n"
381 " type leafref {\n"
382 " path \"/mod2:one-leaf\";\n"
383 " }\n"
384 " }\n"
385 "\n"
386 " leaf iref {\n"
387 " type identityref {\n"
388 " base \"mod2:just-another-identity\";\n"
389 " }\n"
390 " }\n"
391 " }\n"
392 "\n"
Michal Vasko02ed9d82021-07-15 14:58:04 +0200393 " notification notif;\n"
394 "\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100395 " augment \"/random\" {\n"
396 " leaf aug-leaf {\n"
397 " type string;\n"
398 " }\n"
399 " }\n"
400 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200401
402 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100403 "<ice-cream-shop xmlns=\"urn:module\">\n"
404 " <employees>\n"
405 " <employee>\n"
406 " <id>0</id>\n"
407 " <name>John Doe</name>\n"
408 " <age>28</age>\n"
409 " </employee>\n"
410 " <employee>\n"
411 " <id>1</id>\n"
412 " <name>Dohn Joe</name>\n"
413 " <age>20</age>\n"
414 " </employee>\n"
415 " </employees>\n"
416 "</ice-cream-shop>\n"
417 "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>\n"
418 "<random xmlns=\"urn:module\">\n"
419 " <aleaf>string</aleaf>\n"
420 " <xml-data><anyxml>data</anyxml></xml-data>\n"
Michal Vasko02ed9d82021-07-15 14:58:04 +0200421 " <any-data><notif/></any-data>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100422 " <leaflist>l0</leaflist>\n"
423 " <leaflist>l1</leaflist>\n"
424 " <leaflist>l2</leaflist>\n"
425 " <g1>40</g1>\n"
426 " <g2>string</g2>\n"
427 " <aug-leaf>string</aug-leaf>\n"
428 " <rg1>string</rg1>\n"
429 " <rg2>string</rg2>\n"
430 " <lref>reference leaf</lref>\n"
431 " <iref>random-identity</iref>\n"
432 "</random>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200433
Radek Iša56ca9e42020-09-08 18:42:00 +0200434 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
435 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200436
aPiecekfbfcba42021-09-13 13:53:44 +0200437 CHECK_PRINT_THEN_PARSE(data_xml);
Radek Iša56ca9e42020-09-08 18:42:00 +0200438}
439
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200440static void
441test_opaq(void **state)
442{
443 const char *nc_feats[] = {"writable-running", NULL};
444 const char *data_xml =
445 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
446 " <target>\n"
447 " <running/>\n"
448 " </target>\n"
449 " <config>\n"
450 " <top xmlns=\"urn:ed\">\n"
451 " <first>TestFirst</first>\n"
452 " </top>\n"
453 " </config>\n"
454 "</edit-config>\n";
455 struct ly_in *in;
456 struct lyd_node *tree_1;
457 struct lyd_node *tree_2;
458 char *xml_out; /* tree_2 */
459 LY_ERR rc;
460
461 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", NULL, nc_feats));
462
463 ly_in_new_memory(data_xml, &in);
464 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree_1, NULL);
465 ly_in_free(in, 0);
466 assert_int_equal(rc, LY_SUCCESS);
467
468 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
469
470 ly_in_new_memory(xml_out, &in);
471 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_LYB, LYD_TYPE_RPC_YANG, &tree_2, NULL);
472 ly_in_free(in, 0);
473 assert_int_equal(rc, LY_SUCCESS);
474
475 /* compare models */
476 CHECK_LYD(tree_1, tree_2);
477
478 /* clean */
479 free(xml_out);
480 lyd_free_all(tree_1);
481 lyd_free_all(tree_2);
482}
483
Radek Iša56ca9e42020-09-08 18:42:00 +0200484#if 0
485
486static void
487test_types(void **state)
488{
489 struct state *st = (*state);
490 int ret;
491
492 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
493 assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL));
494
495 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/types.xml", LYD_XML, LYD_OPT_CONFIG);
Michal Vasko60ea6352020-06-29 13:39:39 +0200496 assert_ptr_not_equal(st->dt1, NULL);
497
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200499 assert_int_equal(ret, 0);
500
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200502 assert_ptr_not_equal(st->dt2, NULL);
503
504 check_data_tree(st->dt1, st->dt2);
505}
506
Radek Iša56ca9e42020-09-08 18:42:00 +0200507static void
508test_annotations(void **state)
509{
510 struct state *st = (*state);
511 int ret;
512
513 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
514 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
515
516 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG);
517 assert_ptr_not_equal(st->dt1, NULL);
518
519 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
520 assert_int_equal(ret, 0);
521
522 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
523 assert_ptr_not_equal(st->dt2, NULL);
524
525 check_data_tree(st->dt1, st->dt2);
526}
527
528static void
529test_similar_annot_names(void **state)
530{
531 struct state *st = (*state);
532 int ret;
533
534 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
535 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
536
537 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG);
538 assert_ptr_not_equal(st->dt1, NULL);
539
540 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
541 assert_int_equal(ret, 0);
542
543 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
544 assert_ptr_not_equal(st->dt2, NULL);
545
546 check_data_tree(st->dt1, st->dt2);
547}
548
549static void
550test_many_child_annot(void **state)
551{
552 struct state *st = (*state);
553 int ret;
554
555 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
556 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
557
558 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/many-childs-annot.xml", LYD_XML, LYD_OPT_CONFIG);
559 assert_ptr_not_equal(st->dt1, NULL);
560
561 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
562 assert_int_equal(ret, 0);
563
564 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
565 assert_ptr_not_equal(st->dt2, NULL);
566
567 check_data_tree(st->dt1, st->dt2);
568}
569
570static void
571test_union(void **state)
572{
573 struct state *st = (*state);
574 int ret;
575
576 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
577 assert_non_null(ly_ctx_load_module(st->ctx, "union", NULL));
578
579 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG);
580 assert_ptr_not_equal(st->dt1, NULL);
581
582 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
583 assert_int_equal(ret, 0);
584
585 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
586 assert_ptr_not_equal(st->dt2, NULL);
587
588 check_data_tree(st->dt1, st->dt2);
589}
590
591static void
592test_union2(void **state)
593{
594 struct state *st = (*state);
595 int ret;
596
597 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
598 assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL));
599
600 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG);
601 assert_ptr_not_equal(st->dt1, NULL);
602
603 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
604 assert_int_equal(ret, 0);
605
606 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
607 assert_ptr_not_equal(st->dt2, NULL);
608
609 check_data_tree(st->dt1, st->dt2);
610}
611
612static void
613test_collisions(void **state)
614{
615 struct state *st = (*state);
616 int ret;
617
618 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
619 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
620
621 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG);
622 assert_ptr_not_equal(st->dt1, NULL);
623
624 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
625 assert_int_equal(ret, 0);
626
627 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
628 assert_ptr_not_equal(st->dt2, NULL);
629
630 check_data_tree(st->dt1, st->dt2);
631}
632
633static void
634test_anydata(void **state)
635{
636 struct state *st = (*state);
637 const struct lys_module *mod;
638 int ret;
639 const char *test_anydata =
640 "module test-anydata {"
641 " namespace \"urn:test-anydata\";"
642 " prefix ya;"
643 ""
644 " container cont {"
645 " anydata ntf;"
646 " }"
647 "}";
648
649 assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL));
650
651 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
652 assert_ptr_not_equal(st->dt1, NULL);
653
654 / *get notification in LYB format to set as anydata content * /
655 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
656 assert_int_equal(ret, 0);
657
658 lyd_free_withsiblings(st->dt1);
659 st->dt1 = NULL;
660
661 / *now comes the real test, test anydata * /
662 mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG);
663 assert_non_null(mod);
664
665 st->dt1 = lyd_new(NULL, mod, "cont");
666 assert_non_null(st->dt1);
667
668 assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD));
669 st->mem = NULL;
670
671 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
672 assert_int_equal(ret, 0);
673
674 ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL);
675 assert_int_equal(ret, 0);
676
677 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
678 assert_ptr_not_equal(st->dt2, NULL);
679
680 check_data_tree(st->dt1, st->dt2);
681
682 /* and also test the embedded notification itself */
683 free(st->mem);
684 ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem);
685 st->mem = malloc(ret);
686 memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret);
687
688 lyd_free_withsiblings(st->dt2);
689 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL);
690 assert_ptr_not_equal(st->dt2, NULL);
691
692 /* parse the JSON again for this comparison */
693 lyd_free_withsiblings(st->dt1);
694 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
695 assert_ptr_not_equal(st->dt1, NULL);
696
697 check_data_tree(st->dt1, st->dt2);
698}
699
700static void
701test_submodule_feature(void **state)
702{
703 struct state *st = (*state);
704 const struct lys_module *mod;
705 int ret;
706
707 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
708 mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL);
709 assert_non_null(mod);
710 assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0);
711
712 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG);
713 assert_ptr_not_equal(st->dt1, NULL);
714
715 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
716 assert_int_equal(ret, 0);
717
718 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
719 assert_ptr_not_equal(st->dt2, NULL);
720
721 check_data_tree(st->dt1, st->dt2);
722}
723
724static void
725test_coliding_augments(void **state)
726{
727 struct state *st = (*state);
728 int ret;
729
730 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
731 assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL));
732 assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL));
733 assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL));
734
735 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG);
736 assert_ptr_not_equal(st->dt1, NULL);
737
738 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
739 assert_int_equal(ret, 0);
740
741 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
742 assert_ptr_not_equal(st->dt2, NULL);
743
744 check_data_tree(st->dt1, st->dt2);
745}
746
747static void
748test_leafrefs(void **state)
749{
750 struct state *st = (*state);
751 int ret;
752
753 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
754 assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL));
755
756 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT);
757 assert_ptr_not_equal(st->dt1, NULL);
758
759 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
760 assert_int_equal(ret, 0);
761
762 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
763 assert_ptr_not_equal(st->dt2, NULL);
764
765 check_data_tree(st->dt1, st->dt2);
766}
767
768#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200769
770int
771main(void)
772{
773 const struct CMUnitTest tests[] = {
aPiecek3c6cf2f2021-09-21 14:15:50 +0200774 UTEST(tests_leaflist),
775 UTEST(tests_list),
776 UTEST(tests_any),
Radek Iša56ca9e42020-09-08 18:42:00 +0200777 UTEST(test_ietf_interfaces, setup),
778 UTEST(test_origin, setup),
779 UTEST(test_statements, setup),
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200780 UTEST(test_opaq, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200781#if 0
782 cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f),
Michal Vasko60ea6352020-06-29 13:39:39 +0200783 cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f),
784 cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f),
785 cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f),
786 cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f),
787 cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f),
788 cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f),
789 cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f),
790 cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f),
791 cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f),
Radek Iša56ca9e42020-09-08 18:42:00 +0200792 cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f),
793#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200794 };
795
796 return cmocka_run_group_tests(tests, NULL, NULL);
797}