blob: 96a5a0752a9580843e5d634b7dcda7bbbb9ef8c8 [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
51test_ietf_interfaces(void **state)
52{
Michal Vasko60ea6352020-06-29 13:39:39 +020053 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010054 "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
55 " <interface>\n"
56 " <name>eth0</name>\n"
57 " <description>Ethernet 0</description>\n"
58 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
59 " <enabled>true</enabled>\n"
60 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
61 " <enabled>true</enabled>\n"
62 " <mtu>1500</mtu>\n"
63 " <address>\n"
64 " <ip>192.168.2.100</ip>\n"
65 " <prefix-length>24</prefix-length>\n"
66 " </address>\n"
67 " </ipv4>\n"
68 " </interface>\n"
69 " <interface>\n"
70 " <name>eth1</name>\n"
71 " <description>Ethernet 1</description>\n"
72 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
73 " <enabled>true</enabled>\n"
74 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
75 " <enabled>true</enabled>\n"
76 " <mtu>1500</mtu>\n"
77 " <address>\n"
78 " <ip>10.10.1.5</ip>\n"
79 " <prefix-length>16</prefix-length>\n"
80 " </address>\n"
81 " </ipv4>\n"
82 " </interface>\n"
83 " <interface>\n"
84 " <name>gigaeth0</name>\n"
85 " <description>GigabitEthernet 0</description>\n"
86 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
87 " <enabled>false</enabled>\n"
88 " </interface>\n"
89 "</interfaces>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +020090
Radek Iša56ca9e42020-09-08 18:42:00 +020091 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-ip", NULL, NULL));
92 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "iana-if-type", NULL, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +020093
aPiecekfbfcba42021-09-13 13:53:44 +020094 CHECK_PRINT_THEN_PARSE(data_xml);
Michal Vasko60ea6352020-06-29 13:39:39 +020095}
96
97static void
98test_origin(void **state)
99{
Michal Vasko60ea6352020-06-29 13:39:39 +0200100 const char *origin_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100101 "module test-origin {"
102 " namespace \"urn:test-origin\";"
103 " prefix to;"
104 " import ietf-origin {"
105 " prefix or;"
106 " }"
107 ""
108 " container cont {"
109 " leaf leaf1 {"
110 " type string;"
111 " }"
112 " leaf leaf2 {"
113 " type string;"
114 " }"
115 " leaf leaf3 {"
116 " type uint8;"
117 " }"
118 " }"
119 "}";
Michal Vasko60ea6352020-06-29 13:39:39 +0200120 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100121 "<cont xmlns=\"urn:test-origin\">\n"
122 " <leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>\n"
123 " <leaf2>value2</leaf2>\n"
124 " <leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>\n"
125 "</cont>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200126
Radek Iša56ca9e42020-09-08 18:42:00 +0200127 UTEST_ADD_MODULE(origin_yang, LYS_IN_YANG, NULL, NULL);
128 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 +0200129
aPiecekfbfcba42021-09-13 13:53:44 +0200130 CHECK_PRINT_THEN_PARSE(data_xml);
Michal Vasko60ea6352020-06-29 13:39:39 +0200131}
132
133static void
134test_statements(void **state)
135{
Michal Vasko60ea6352020-06-29 13:39:39 +0200136 const char *links_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100137 "module links {\n"
138 " yang-version 1.1;\n"
139 " namespace \"urn:module2\";\n"
140 " prefix mod2;\n"
141 "\n"
142 " identity just-another-identity;\n"
143 "\n"
144 " leaf one-leaf {\n"
145 " type string;\n"
146 " }\n"
147 "\n"
148 " list list-for-augment {\n"
149 " key keyleaf;\n"
150 "\n"
151 " leaf keyleaf {\n"
152 " type string;\n"
153 " }\n"
154 "\n"
155 " leaf just-leaf {\n"
156 " type int32;\n"
157 " }\n"
158 " }\n"
159 "\n"
160 " leaf rleaf {\n"
161 " type string;\n"
162 " }\n"
163 "\n"
164 " leaf-list llist {\n"
165 " type string;\n"
166 " min-elements 0;\n"
167 " max-elements 100;\n"
168 " ordered-by user;\n"
169 " }\n"
170 "\n"
171 " grouping rgroup {\n"
172 " leaf rg1 {\n"
173 " type string;\n"
174 " }\n"
175 "\n"
176 " leaf rg2 {\n"
177 " type string;\n"
178 " }\n"
179 " }\n"
180 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200181
182 const char *statements_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100183 "module statements {\n"
184 " namespace \"urn:module\";\n"
185 " prefix mod;\n"
186 " yang-version 1.1;\n"
187 "\n"
188 " import links {\n"
189 " prefix mod2;\n"
190 " }\n"
191 "\n"
192 " identity random-identity {\n"
193 " base \"mod2:just-another-identity\";\n"
194 " base \"another-identity\";\n"
195 " }\n"
196 "\n"
197 " identity another-identity {\n"
198 " base \"mod2:just-another-identity\";\n"
199 " }\n"
200 "\n"
201 " typedef percent {\n"
202 " type uint8 {\n"
203 " range \"0 .. 100\";\n"
204 " }\n"
205 " units percent;\n"
206 " }\n"
207 "\n"
208 " container ice-cream-shop {\n"
209 " container employees {\n"
210 " list employee {\n"
211 " config true;\n"
212 " key id;\n"
213 " unique name;\n"
214 " min-elements 0;\n"
215 " max-elements 100;\n"
216 "\n"
217 " leaf id {\n"
218 " type uint64;\n"
219 " mandatory true;\n"
220 " }\n"
221 "\n"
222 " leaf name {\n"
223 " type string;\n"
224 " }\n"
225 "\n"
226 " leaf age {\n"
227 " type uint32;\n"
228 " }\n"
229 " }\n"
230 " }\n"
231 " }\n"
232 "\n"
233 " container random {\n"
234 " choice switch {\n"
235 " case a {\n"
236 " leaf aleaf {\n"
237 " type string;\n"
238 " default aaa;\n"
239 " }\n"
240 " }\n"
241 "\n"
242 " case c {\n"
243 " leaf cleaf {\n"
244 " type string;\n"
245 " }\n"
246 " }\n"
247 " }\n"
248 "\n"
249 " anyxml xml-data;\n"
250 " anydata any-data;\n"
251 " leaf-list leaflist {\n"
252 " type string;\n"
253 " min-elements 0;\n"
254 " max-elements 20;\n"
255 " ordered-by system;\n"
256 " }\n"
257 "\n"
258 " grouping group {\n"
259 " leaf g1 {\n"
260 " mandatory false;\n"
261 " type percent;\n"
262 " }\n"
263 "\n"
264 " leaf g2 {\n"
265 " type string;\n"
266 " }\n"
267 " }\n"
268 "\n"
269 " uses group;\n"
270 " uses mod2:rgroup;\n"
271 "\n"
272 " leaf lref {\n"
273 " type leafref {\n"
274 " path \"/mod2:one-leaf\";\n"
275 " }\n"
276 " }\n"
277 "\n"
278 " leaf iref {\n"
279 " type identityref {\n"
280 " base \"mod2:just-another-identity\";\n"
281 " }\n"
282 " }\n"
283 " }\n"
284 "\n"
Michal Vasko02ed9d82021-07-15 14:58:04 +0200285 " notification notif;\n"
286 "\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100287 " augment \"/random\" {\n"
288 " leaf aug-leaf {\n"
289 " type string;\n"
290 " }\n"
291 " }\n"
292 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200293
294 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100295 "<ice-cream-shop xmlns=\"urn:module\">\n"
296 " <employees>\n"
297 " <employee>\n"
298 " <id>0</id>\n"
299 " <name>John Doe</name>\n"
300 " <age>28</age>\n"
301 " </employee>\n"
302 " <employee>\n"
303 " <id>1</id>\n"
304 " <name>Dohn Joe</name>\n"
305 " <age>20</age>\n"
306 " </employee>\n"
307 " </employees>\n"
308 "</ice-cream-shop>\n"
309 "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>\n"
310 "<random xmlns=\"urn:module\">\n"
311 " <aleaf>string</aleaf>\n"
312 " <xml-data><anyxml>data</anyxml></xml-data>\n"
Michal Vasko02ed9d82021-07-15 14:58:04 +0200313 " <any-data><notif/></any-data>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100314 " <leaflist>l0</leaflist>\n"
315 " <leaflist>l1</leaflist>\n"
316 " <leaflist>l2</leaflist>\n"
317 " <g1>40</g1>\n"
318 " <g2>string</g2>\n"
319 " <aug-leaf>string</aug-leaf>\n"
320 " <rg1>string</rg1>\n"
321 " <rg2>string</rg2>\n"
322 " <lref>reference leaf</lref>\n"
323 " <iref>random-identity</iref>\n"
324 "</random>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200325
Radek Iša56ca9e42020-09-08 18:42:00 +0200326 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
327 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200328
aPiecekfbfcba42021-09-13 13:53:44 +0200329 CHECK_PRINT_THEN_PARSE(data_xml);
Radek Iša56ca9e42020-09-08 18:42:00 +0200330}
331
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200332static void
333test_opaq(void **state)
334{
335 const char *nc_feats[] = {"writable-running", NULL};
336 const char *data_xml =
337 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
338 " <target>\n"
339 " <running/>\n"
340 " </target>\n"
341 " <config>\n"
342 " <top xmlns=\"urn:ed\">\n"
343 " <first>TestFirst</first>\n"
344 " </top>\n"
345 " </config>\n"
346 "</edit-config>\n";
347 struct ly_in *in;
348 struct lyd_node *tree_1;
349 struct lyd_node *tree_2;
350 char *xml_out; /* tree_2 */
351 LY_ERR rc;
352
353 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", NULL, nc_feats));
354
355 ly_in_new_memory(data_xml, &in);
356 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree_1, NULL);
357 ly_in_free(in, 0);
358 assert_int_equal(rc, LY_SUCCESS);
359
360 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
361
362 ly_in_new_memory(xml_out, &in);
363 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_LYB, LYD_TYPE_RPC_YANG, &tree_2, NULL);
364 ly_in_free(in, 0);
365 assert_int_equal(rc, LY_SUCCESS);
366
367 /* compare models */
368 CHECK_LYD(tree_1, tree_2);
369
370 /* clean */
371 free(xml_out);
372 lyd_free_all(tree_1);
373 lyd_free_all(tree_2);
374}
375
Radek Iša56ca9e42020-09-08 18:42:00 +0200376#if 0
377
378static void
379test_types(void **state)
380{
381 struct state *st = (*state);
382 int ret;
383
384 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
385 assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL));
386
387 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 +0200388 assert_ptr_not_equal(st->dt1, NULL);
389
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 assert_int_equal(ret, 0);
392
Radek Iša56ca9e42020-09-08 18:42:00 +0200393 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 assert_ptr_not_equal(st->dt2, NULL);
395
396 check_data_tree(st->dt1, st->dt2);
397}
398
Radek Iša56ca9e42020-09-08 18:42:00 +0200399static void
400test_annotations(void **state)
401{
402 struct state *st = (*state);
403 int ret;
404
405 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
406 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
407
408 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG);
409 assert_ptr_not_equal(st->dt1, NULL);
410
411 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
412 assert_int_equal(ret, 0);
413
414 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
415 assert_ptr_not_equal(st->dt2, NULL);
416
417 check_data_tree(st->dt1, st->dt2);
418}
419
420static void
421test_similar_annot_names(void **state)
422{
423 struct state *st = (*state);
424 int ret;
425
426 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
427 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
428
429 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG);
430 assert_ptr_not_equal(st->dt1, NULL);
431
432 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
433 assert_int_equal(ret, 0);
434
435 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
436 assert_ptr_not_equal(st->dt2, NULL);
437
438 check_data_tree(st->dt1, st->dt2);
439}
440
441static void
442test_many_child_annot(void **state)
443{
444 struct state *st = (*state);
445 int ret;
446
447 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
448 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
449
450 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/many-childs-annot.xml", LYD_XML, LYD_OPT_CONFIG);
451 assert_ptr_not_equal(st->dt1, NULL);
452
453 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
454 assert_int_equal(ret, 0);
455
456 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
457 assert_ptr_not_equal(st->dt2, NULL);
458
459 check_data_tree(st->dt1, st->dt2);
460}
461
462static void
463test_union(void **state)
464{
465 struct state *st = (*state);
466 int ret;
467
468 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
469 assert_non_null(ly_ctx_load_module(st->ctx, "union", NULL));
470
471 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG);
472 assert_ptr_not_equal(st->dt1, NULL);
473
474 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
475 assert_int_equal(ret, 0);
476
477 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
478 assert_ptr_not_equal(st->dt2, NULL);
479
480 check_data_tree(st->dt1, st->dt2);
481}
482
483static void
484test_union2(void **state)
485{
486 struct state *st = (*state);
487 int ret;
488
489 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
490 assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL));
491
492 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG);
493 assert_ptr_not_equal(st->dt1, NULL);
494
495 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
496 assert_int_equal(ret, 0);
497
498 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
499 assert_ptr_not_equal(st->dt2, NULL);
500
501 check_data_tree(st->dt1, st->dt2);
502}
503
504static void
505test_collisions(void **state)
506{
507 struct state *st = (*state);
508 int ret;
509
510 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
511 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
512
513 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG);
514 assert_ptr_not_equal(st->dt1, NULL);
515
516 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
517 assert_int_equal(ret, 0);
518
519 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
520 assert_ptr_not_equal(st->dt2, NULL);
521
522 check_data_tree(st->dt1, st->dt2);
523}
524
525static void
526test_anydata(void **state)
527{
528 struct state *st = (*state);
529 const struct lys_module *mod;
530 int ret;
531 const char *test_anydata =
532 "module test-anydata {"
533 " namespace \"urn:test-anydata\";"
534 " prefix ya;"
535 ""
536 " container cont {"
537 " anydata ntf;"
538 " }"
539 "}";
540
541 assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL));
542
543 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
544 assert_ptr_not_equal(st->dt1, NULL);
545
546 / *get notification in LYB format to set as anydata content * /
547 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
548 assert_int_equal(ret, 0);
549
550 lyd_free_withsiblings(st->dt1);
551 st->dt1 = NULL;
552
553 / *now comes the real test, test anydata * /
554 mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG);
555 assert_non_null(mod);
556
557 st->dt1 = lyd_new(NULL, mod, "cont");
558 assert_non_null(st->dt1);
559
560 assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD));
561 st->mem = NULL;
562
563 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
564 assert_int_equal(ret, 0);
565
566 ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL);
567 assert_int_equal(ret, 0);
568
569 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
570 assert_ptr_not_equal(st->dt2, NULL);
571
572 check_data_tree(st->dt1, st->dt2);
573
574 /* and also test the embedded notification itself */
575 free(st->mem);
576 ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem);
577 st->mem = malloc(ret);
578 memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret);
579
580 lyd_free_withsiblings(st->dt2);
581 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL);
582 assert_ptr_not_equal(st->dt2, NULL);
583
584 /* parse the JSON again for this comparison */
585 lyd_free_withsiblings(st->dt1);
586 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
587 assert_ptr_not_equal(st->dt1, NULL);
588
589 check_data_tree(st->dt1, st->dt2);
590}
591
592static void
593test_submodule_feature(void **state)
594{
595 struct state *st = (*state);
596 const struct lys_module *mod;
597 int ret;
598
599 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
600 mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL);
601 assert_non_null(mod);
602 assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0);
603
604 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG);
605 assert_ptr_not_equal(st->dt1, NULL);
606
607 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
608 assert_int_equal(ret, 0);
609
610 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
611 assert_ptr_not_equal(st->dt2, NULL);
612
613 check_data_tree(st->dt1, st->dt2);
614}
615
616static void
617test_coliding_augments(void **state)
618{
619 struct state *st = (*state);
620 int ret;
621
622 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
623 assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL));
624 assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL));
625 assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL));
626
627 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG);
628 assert_ptr_not_equal(st->dt1, NULL);
629
630 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
631 assert_int_equal(ret, 0);
632
633 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
634 assert_ptr_not_equal(st->dt2, NULL);
635
636 check_data_tree(st->dt1, st->dt2);
637}
638
639static void
640test_leafrefs(void **state)
641{
642 struct state *st = (*state);
643 int ret;
644
645 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
646 assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL));
647
648 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT);
649 assert_ptr_not_equal(st->dt1, NULL);
650
651 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
652 assert_int_equal(ret, 0);
653
654 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
655 assert_ptr_not_equal(st->dt2, NULL);
656
657 check_data_tree(st->dt1, st->dt2);
658}
659
660#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200661
662int
663main(void)
664{
665 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200666 UTEST(test_ietf_interfaces, setup),
667 UTEST(test_origin, setup),
668 UTEST(test_statements, setup),
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200669 UTEST(test_opaq, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200670#if 0
671 cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f),
Michal Vasko60ea6352020-06-29 13:39:39 +0200672 cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f),
673 cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f),
674 cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f),
675 cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f),
676 cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f),
677 cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f),
678 cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f),
679 cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f),
680 cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f),
Radek Iša56ca9e42020-09-08 18:42:00 +0200681 cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f),
682#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200683 };
684
685 return cmocka_run_group_tests(tests, NULL, NULL);
686}