blob: 10470a26484d31b1c131e8a87eacb94834fc26fa [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
Radek Iša56ca9e42020-09-08 18:42:00 +020020#define CHECK_PARSE_LYD(INPUT, MODEL) \
21 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0,LY_SUCCESS, MODEL)
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
26static int
Radek Iša56ca9e42020-09-08 18:42:00 +020027setup(void **state)
Michal Vasko60ea6352020-06-29 13:39:39 +020028{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 UTEST_SETUP;
30 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Michal Vasko60ea6352020-06-29 13:39:39 +020031
32 return 0;
33}
34
35static void
36test_ietf_interfaces(void **state)
37{
Michal Vasko60ea6352020-06-29 13:39:39 +020038 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039 "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
40 " <interface>\n"
41 " <name>eth0</name>\n"
42 " <description>Ethernet 0</description>\n"
43 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
44 " <enabled>true</enabled>\n"
45 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
46 " <enabled>true</enabled>\n"
47 " <mtu>1500</mtu>\n"
48 " <address>\n"
49 " <ip>192.168.2.100</ip>\n"
50 " <prefix-length>24</prefix-length>\n"
51 " </address>\n"
52 " </ipv4>\n"
53 " </interface>\n"
54 " <interface>\n"
55 " <name>eth1</name>\n"
56 " <description>Ethernet 1</description>\n"
57 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
58 " <enabled>true</enabled>\n"
59 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
60 " <enabled>true</enabled>\n"
61 " <mtu>1500</mtu>\n"
62 " <address>\n"
63 " <ip>10.10.1.5</ip>\n"
64 " <prefix-length>16</prefix-length>\n"
65 " </address>\n"
66 " </ipv4>\n"
67 " </interface>\n"
68 " <interface>\n"
69 " <name>gigaeth0</name>\n"
70 " <description>GigabitEthernet 0</description>\n"
71 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
72 " <enabled>false</enabled>\n"
73 " </interface>\n"
74 "</interfaces>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +020075 struct lyd_node *tree_1;
76 struct lyd_node *tree_2;
77 char *xml_out; /* tree_2 */
Michal Vasko60ea6352020-06-29 13:39:39 +020078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-ip", NULL, NULL));
80 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "iana-if-type", NULL, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +020081
Radek Iša56ca9e42020-09-08 18:42:00 +020082 CHECK_PARSE_LYD(data_xml, tree_1);
Michal Vasko60ea6352020-06-29 13:39:39 +020083
Radek Iša56ca9e42020-09-08 18:42:00 +020084 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
Michal Vasko60ea6352020-06-29 13:39:39 +020085
Radek Iša56ca9e42020-09-08 18:42:00 +020086 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2));
87 assert_non_null(tree_2);
Michal Vasko60ea6352020-06-29 13:39:39 +020088
Radek Iša56ca9e42020-09-08 18:42:00 +020089 /* compare models */
90 CHECK_LYD(tree_1, tree_2);
91
92 /* clean */
93 free(xml_out);
94 lyd_free_all(tree_1);
95 lyd_free_all(tree_2);
Michal Vasko60ea6352020-06-29 13:39:39 +020096}
97
98static void
99test_origin(void **state)
100{
Michal Vasko60ea6352020-06-29 13:39:39 +0200101 const char *origin_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100102 "module test-origin {"
103 " namespace \"urn:test-origin\";"
104 " prefix to;"
105 " import ietf-origin {"
106 " prefix or;"
107 " }"
108 ""
109 " container cont {"
110 " leaf leaf1 {"
111 " type string;"
112 " }"
113 " leaf leaf2 {"
114 " type string;"
115 " }"
116 " leaf leaf3 {"
117 " type uint8;"
118 " }"
119 " }"
120 "}";
Michal Vasko60ea6352020-06-29 13:39:39 +0200121 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100122 "<cont xmlns=\"urn:test-origin\">\n"
123 " <leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>\n"
124 " <leaf2>value2</leaf2>\n"
125 " <leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>\n"
126 "</cont>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200127 struct lyd_node *tree_1;
128 struct lyd_node *tree_2;
129 char *xml_out; /* tree_2 */
Michal Vasko60ea6352020-06-29 13:39:39 +0200130
Radek Iša56ca9e42020-09-08 18:42:00 +0200131 UTEST_ADD_MODULE(origin_yang, LYS_IN_YANG, NULL, NULL);
132 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 +0200133
Radek Iša56ca9e42020-09-08 18:42:00 +0200134 CHECK_PARSE_LYD(data_xml, tree_1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200135
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200137
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2));
139 assert_non_null(tree_2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200140
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 /* compare models */
142 CHECK_LYD(tree_1, tree_2);
143
144 /* clean */
145 free(xml_out);
146 lyd_free_all(tree_1);
147 lyd_free_all(tree_2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200148}
149
150static void
151test_statements(void **state)
152{
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 const char *links_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100154 "module links {\n"
155 " yang-version 1.1;\n"
156 " namespace \"urn:module2\";\n"
157 " prefix mod2;\n"
158 "\n"
159 " identity just-another-identity;\n"
160 "\n"
161 " leaf one-leaf {\n"
162 " type string;\n"
163 " }\n"
164 "\n"
165 " list list-for-augment {\n"
166 " key keyleaf;\n"
167 "\n"
168 " leaf keyleaf {\n"
169 " type string;\n"
170 " }\n"
171 "\n"
172 " leaf just-leaf {\n"
173 " type int32;\n"
174 " }\n"
175 " }\n"
176 "\n"
177 " leaf rleaf {\n"
178 " type string;\n"
179 " }\n"
180 "\n"
181 " leaf-list llist {\n"
182 " type string;\n"
183 " min-elements 0;\n"
184 " max-elements 100;\n"
185 " ordered-by user;\n"
186 " }\n"
187 "\n"
188 " grouping rgroup {\n"
189 " leaf rg1 {\n"
190 " type string;\n"
191 " }\n"
192 "\n"
193 " leaf rg2 {\n"
194 " type string;\n"
195 " }\n"
196 " }\n"
197 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200198
199 const char *statements_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100200 "module statements {\n"
201 " namespace \"urn:module\";\n"
202 " prefix mod;\n"
203 " yang-version 1.1;\n"
204 "\n"
205 " import links {\n"
206 " prefix mod2;\n"
207 " }\n"
208 "\n"
209 " identity random-identity {\n"
210 " base \"mod2:just-another-identity\";\n"
211 " base \"another-identity\";\n"
212 " }\n"
213 "\n"
214 " identity another-identity {\n"
215 " base \"mod2:just-another-identity\";\n"
216 " }\n"
217 "\n"
218 " typedef percent {\n"
219 " type uint8 {\n"
220 " range \"0 .. 100\";\n"
221 " }\n"
222 " units percent;\n"
223 " }\n"
224 "\n"
225 " container ice-cream-shop {\n"
226 " container employees {\n"
227 " list employee {\n"
228 " config true;\n"
229 " key id;\n"
230 " unique name;\n"
231 " min-elements 0;\n"
232 " max-elements 100;\n"
233 "\n"
234 " leaf id {\n"
235 " type uint64;\n"
236 " mandatory true;\n"
237 " }\n"
238 "\n"
239 " leaf name {\n"
240 " type string;\n"
241 " }\n"
242 "\n"
243 " leaf age {\n"
244 " type uint32;\n"
245 " }\n"
246 " }\n"
247 " }\n"
248 " }\n"
249 "\n"
250 " container random {\n"
251 " choice switch {\n"
252 " case a {\n"
253 " leaf aleaf {\n"
254 " type string;\n"
255 " default aaa;\n"
256 " }\n"
257 " }\n"
258 "\n"
259 " case c {\n"
260 " leaf cleaf {\n"
261 " type string;\n"
262 " }\n"
263 " }\n"
264 " }\n"
265 "\n"
266 " anyxml xml-data;\n"
267 " anydata any-data;\n"
268 " leaf-list leaflist {\n"
269 " type string;\n"
270 " min-elements 0;\n"
271 " max-elements 20;\n"
272 " ordered-by system;\n"
273 " }\n"
274 "\n"
275 " grouping group {\n"
276 " leaf g1 {\n"
277 " mandatory false;\n"
278 " type percent;\n"
279 " }\n"
280 "\n"
281 " leaf g2 {\n"
282 " type string;\n"
283 " }\n"
284 " }\n"
285 "\n"
286 " uses group;\n"
287 " uses mod2:rgroup;\n"
288 "\n"
289 " leaf lref {\n"
290 " type leafref {\n"
291 " path \"/mod2:one-leaf\";\n"
292 " }\n"
293 " }\n"
294 "\n"
295 " leaf iref {\n"
296 " type identityref {\n"
297 " base \"mod2:just-another-identity\";\n"
298 " }\n"
299 " }\n"
300 " }\n"
301 "\n"
302 " augment \"/random\" {\n"
303 " leaf aug-leaf {\n"
304 " type string;\n"
305 " }\n"
306 " }\n"
307 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200308
309 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100310 "<ice-cream-shop xmlns=\"urn:module\">\n"
311 " <employees>\n"
312 " <employee>\n"
313 " <id>0</id>\n"
314 " <name>John Doe</name>\n"
315 " <age>28</age>\n"
316 " </employee>\n"
317 " <employee>\n"
318 " <id>1</id>\n"
319 " <name>Dohn Joe</name>\n"
320 " <age>20</age>\n"
321 " </employee>\n"
322 " </employees>\n"
323 "</ice-cream-shop>\n"
324 "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>\n"
325 "<random xmlns=\"urn:module\">\n"
326 " <aleaf>string</aleaf>\n"
327 " <xml-data><anyxml>data</anyxml></xml-data>\n"
328 " <any-data><data>any data</data></any-data>\n"
329 " <leaflist>l0</leaflist>\n"
330 " <leaflist>l1</leaflist>\n"
331 " <leaflist>l2</leaflist>\n"
332 " <g1>40</g1>\n"
333 " <g2>string</g2>\n"
334 " <aug-leaf>string</aug-leaf>\n"
335 " <rg1>string</rg1>\n"
336 " <rg2>string</rg2>\n"
337 " <lref>reference leaf</lref>\n"
338 " <iref>random-identity</iref>\n"
339 "</random>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 struct lyd_node *tree_1;
341 struct lyd_node *tree_2;
342 char *xml_out; /* tree_2 */
Michal Vasko60ea6352020-06-29 13:39:39 +0200343
Radek Iša56ca9e42020-09-08 18:42:00 +0200344 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
345 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200346
Radek Iša56ca9e42020-09-08 18:42:00 +0200347 CHECK_PARSE_LYD(data_xml, tree_1);
348
349 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
350
351 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2));
352 assert_non_null(tree_2);
353
354 /* compare models */
355 CHECK_LYD(tree_1, tree_2);
356
357 /* clean */
358 free(xml_out);
359 lyd_free_all(tree_1);
360 lyd_free_all(tree_2);
361}
362
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200363static void
364test_opaq(void **state)
365{
366 const char *nc_feats[] = {"writable-running", NULL};
367 const char *data_xml =
368 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
369 " <target>\n"
370 " <running/>\n"
371 " </target>\n"
372 " <config>\n"
373 " <top xmlns=\"urn:ed\">\n"
374 " <first>TestFirst</first>\n"
375 " </top>\n"
376 " </config>\n"
377 "</edit-config>\n";
378 struct ly_in *in;
379 struct lyd_node *tree_1;
380 struct lyd_node *tree_2;
381 char *xml_out; /* tree_2 */
382 LY_ERR rc;
383
384 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", NULL, nc_feats));
385
386 ly_in_new_memory(data_xml, &in);
387 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree_1, NULL);
388 ly_in_free(in, 0);
389 assert_int_equal(rc, LY_SUCCESS);
390
391 assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0);
392
393 ly_in_new_memory(xml_out, &in);
394 rc = lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_LYB, LYD_TYPE_RPC_YANG, &tree_2, NULL);
395 ly_in_free(in, 0);
396 assert_int_equal(rc, LY_SUCCESS);
397
398 /* compare models */
399 CHECK_LYD(tree_1, tree_2);
400
401 /* clean */
402 free(xml_out);
403 lyd_free_all(tree_1);
404 lyd_free_all(tree_2);
405}
406
Radek Iša56ca9e42020-09-08 18:42:00 +0200407#if 0
408
409static void
410test_types(void **state)
411{
412 struct state *st = (*state);
413 int ret;
414
415 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
416 assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL));
417
418 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 +0200419 assert_ptr_not_equal(st->dt1, NULL);
420
Radek Iša56ca9e42020-09-08 18:42:00 +0200421 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200422 assert_int_equal(ret, 0);
423
Radek Iša56ca9e42020-09-08 18:42:00 +0200424 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 assert_ptr_not_equal(st->dt2, NULL);
426
427 check_data_tree(st->dt1, st->dt2);
428}
429
Radek Iša56ca9e42020-09-08 18:42:00 +0200430static void
431test_annotations(void **state)
432{
433 struct state *st = (*state);
434 int ret;
435
436 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
437 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
438
439 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG);
440 assert_ptr_not_equal(st->dt1, NULL);
441
442 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
443 assert_int_equal(ret, 0);
444
445 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
446 assert_ptr_not_equal(st->dt2, NULL);
447
448 check_data_tree(st->dt1, st->dt2);
449}
450
451static void
452test_similar_annot_names(void **state)
453{
454 struct state *st = (*state);
455 int ret;
456
457 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
458 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
459
460 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG);
461 assert_ptr_not_equal(st->dt1, NULL);
462
463 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
464 assert_int_equal(ret, 0);
465
466 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
467 assert_ptr_not_equal(st->dt2, NULL);
468
469 check_data_tree(st->dt1, st->dt2);
470}
471
472static void
473test_many_child_annot(void **state)
474{
475 struct state *st = (*state);
476 int ret;
477
478 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
479 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
480
481 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/many-childs-annot.xml", LYD_XML, LYD_OPT_CONFIG);
482 assert_ptr_not_equal(st->dt1, NULL);
483
484 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
485 assert_int_equal(ret, 0);
486
487 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
488 assert_ptr_not_equal(st->dt2, NULL);
489
490 check_data_tree(st->dt1, st->dt2);
491}
492
493static void
494test_union(void **state)
495{
496 struct state *st = (*state);
497 int ret;
498
499 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
500 assert_non_null(ly_ctx_load_module(st->ctx, "union", NULL));
501
502 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG);
503 assert_ptr_not_equal(st->dt1, NULL);
504
505 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
506 assert_int_equal(ret, 0);
507
508 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
509 assert_ptr_not_equal(st->dt2, NULL);
510
511 check_data_tree(st->dt1, st->dt2);
512}
513
514static void
515test_union2(void **state)
516{
517 struct state *st = (*state);
518 int ret;
519
520 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
521 assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL));
522
523 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG);
524 assert_ptr_not_equal(st->dt1, NULL);
525
526 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
527 assert_int_equal(ret, 0);
528
529 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
530 assert_ptr_not_equal(st->dt2, NULL);
531
532 check_data_tree(st->dt1, st->dt2);
533}
534
535static void
536test_collisions(void **state)
537{
538 struct state *st = (*state);
539 int ret;
540
541 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
542 assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
543
544 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG);
545 assert_ptr_not_equal(st->dt1, NULL);
546
547 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
548 assert_int_equal(ret, 0);
549
550 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
551 assert_ptr_not_equal(st->dt2, NULL);
552
553 check_data_tree(st->dt1, st->dt2);
554}
555
556static void
557test_anydata(void **state)
558{
559 struct state *st = (*state);
560 const struct lys_module *mod;
561 int ret;
562 const char *test_anydata =
563 "module test-anydata {"
564 " namespace \"urn:test-anydata\";"
565 " prefix ya;"
566 ""
567 " container cont {"
568 " anydata ntf;"
569 " }"
570 "}";
571
572 assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL));
573
574 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
575 assert_ptr_not_equal(st->dt1, NULL);
576
577 / *get notification in LYB format to set as anydata content * /
578 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
579 assert_int_equal(ret, 0);
580
581 lyd_free_withsiblings(st->dt1);
582 st->dt1 = NULL;
583
584 / *now comes the real test, test anydata * /
585 mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG);
586 assert_non_null(mod);
587
588 st->dt1 = lyd_new(NULL, mod, "cont");
589 assert_non_null(st->dt1);
590
591 assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD));
592 st->mem = NULL;
593
594 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
595 assert_int_equal(ret, 0);
596
597 ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL);
598 assert_int_equal(ret, 0);
599
600 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
601 assert_ptr_not_equal(st->dt2, NULL);
602
603 check_data_tree(st->dt1, st->dt2);
604
605 /* and also test the embedded notification itself */
606 free(st->mem);
607 ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem);
608 st->mem = malloc(ret);
609 memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret);
610
611 lyd_free_withsiblings(st->dt2);
612 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL);
613 assert_ptr_not_equal(st->dt2, NULL);
614
615 /* parse the JSON again for this comparison */
616 lyd_free_withsiblings(st->dt1);
617 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
618 assert_ptr_not_equal(st->dt1, NULL);
619
620 check_data_tree(st->dt1, st->dt2);
621}
622
623static void
624test_submodule_feature(void **state)
625{
626 struct state *st = (*state);
627 const struct lys_module *mod;
628 int ret;
629
630 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
631 mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL);
632 assert_non_null(mod);
633 assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0);
634
635 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG);
636 assert_ptr_not_equal(st->dt1, NULL);
637
638 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
639 assert_int_equal(ret, 0);
640
641 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
642 assert_ptr_not_equal(st->dt2, NULL);
643
644 check_data_tree(st->dt1, st->dt2);
645}
646
647static void
648test_coliding_augments(void **state)
649{
650 struct state *st = (*state);
651 int ret;
652
653 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
654 assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL));
655 assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL));
656 assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL));
657
658 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG);
659 assert_ptr_not_equal(st->dt1, NULL);
660
661 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
662 assert_int_equal(ret, 0);
663
664 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
665 assert_ptr_not_equal(st->dt2, NULL);
666
667 check_data_tree(st->dt1, st->dt2);
668}
669
670static void
671test_leafrefs(void **state)
672{
673 struct state *st = (*state);
674 int ret;
675
676 ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files");
677 assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL));
678
679 st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT);
680 assert_ptr_not_equal(st->dt1, NULL);
681
682 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
683 assert_int_equal(ret, 0);
684
685 st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
686 assert_ptr_not_equal(st->dt2, NULL);
687
688 check_data_tree(st->dt1, st->dt2);
689}
690
691#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200692
693int
694main(void)
695{
696 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200697 UTEST(test_ietf_interfaces, setup),
698 UTEST(test_origin, setup),
699 UTEST(test_statements, setup),
Michal Vaskoffc92bf2021-06-21 08:15:14 +0200700 UTEST(test_opaq, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200701#if 0
702 cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f),
Michal Vasko60ea6352020-06-29 13:39:39 +0200703 cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f),
704 cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f),
705 cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f),
706 cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f),
707 cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f),
708 cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f),
709 cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f),
710 cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f),
711 cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f),
Radek Iša56ca9e42020-09-08 18:42:00 +0200712 cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f),
713#endif
Michal Vasko60ea6352020-06-29 13:39:39 +0200714 };
715
716 return cmocka_run_group_tests(tests, NULL, NULL);
717}