blob: 7730381f69fc62af42de0d9638571e9673b16b20 [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 */
14
Michal Vasko60ea6352020-06-29 13:39:39 +020015#include "hash_table.h"
16#include "libyang.h"
17#include "tests/config.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010018#include "utests.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020019
20struct state {
21 struct ly_ctx *ctx;
22 struct lyd_node *dt1, *dt2;
23 char *mem;
24};
25
26static void
27check_data_tree_next(struct lyd_node **start, struct lyd_node **next, struct lyd_node **elem)
28{
29 if (*elem) {
30 goto loop_next;
31 }
32
33loop_begin:
Michal Vasko56daf732020-08-10 10:57:18 +020034 /* LYD_TREE_DFS_BEGIN */
35 for (*elem = *next = *start; *elem; *elem = *next) {
Michal Vasko60ea6352020-06-29 13:39:39 +020036 return;
37loop_next:
Michal Vasko56daf732020-08-10 10:57:18 +020038 /* LYD_TREE_DFS_END */
39
40 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +020041 *next = lyd_child(*elem);
Michal Vasko56daf732020-08-10 10:57:18 +020042 if (!*next) {
43 /* no children */
44 if (*elem == *start) {
45 /* we are done, (START) has no children */
46 break;
47 }
48 /* try siblings */
49 *next = (*elem)->next;
50 }
51 while (!*next) {
52 /* parent is already processed, go to its sibling */
53 *elem = (struct lyd_node *)(*elem)->parent;
54 /* no siblings, go back through parents */
55 if ((*elem)->parent == (*start)->parent) {
56 /* we are done, no next element to process */
57 break;
58 }
59 *next = (*elem)->next;
60 }
Michal Vasko60ea6352020-06-29 13:39:39 +020061 }
62
63 if (!*next) {
64 /* top-level siblings */
65 *start = (*start)->next;
66 if (!(*start)) {
67 *elem = NULL;
68 return;
69 }
70 goto loop_begin;
71 }
72
73 return;
74}
75
76static void
77check_data_tree(struct lyd_node *root1, struct lyd_node *root2)
78{
79 struct lyd_node *next1, *next2, *elem1 = NULL, *elem2 = NULL, *iter;
80 struct lyd_meta *meta1, *meta2;
81 struct lyd_node_inner *in1, *in2;
82 uint32_t i1, i2;
83
84 for (check_data_tree_next(&root1, &next1, &elem1), check_data_tree_next(&root2, &next2, &elem2);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 elem1 && elem2;
86 check_data_tree_next(&root1, &next1, &elem1), check_data_tree_next(&root2, &next2, &elem2)) {
Michal Vasko60ea6352020-06-29 13:39:39 +020087
88 if (elem1->schema != elem2->schema) {
89 fprintf(stderr, "Schema mismatch (\"%s\" and \"%s\").\n", elem1->schema->name, elem2->schema->name);
90 fail();
91 }
92
93 /* check common data node attributes */
94 if (elem1->flags != elem2->flags) {
95 fprintf(stderr, "\"%s\": flags mismatch (\"%u\" and \"%u\").\n", elem1->schema->name, elem1->flags, elem2->flags);
96 fail();
97 }
98
99 /* check data node attributes */
100 for (meta1 = elem1->meta, meta2 = elem2->meta; meta1 && meta2; meta1 = meta1->next, meta2 = meta2->next) {
101 if (meta1->annotation != meta2->annotation) {
102 fprintf(stderr, "\"%s\": meta annotation mismatch.\n", elem1->schema->name);
103 fail();
104 }
105 if (strcmp(meta1->name, meta2->name)) {
106 fprintf(stderr, "\"%s\": meta name mismatch (\"%s\" and \"%s\").\n", elem1->schema->name, meta1->name, meta2->name);
107 fail();
108 }
109 if (lyd_compare_meta(meta1, meta2)) {
110 fprintf(stderr, "\"%s\": meta value mismatch.\n", elem1->schema->name);
111 fail();
112 }
113 }
114 if (meta1) {
115 fprintf(stderr, "\"%s\": meta mismatch (\"%s\" and \"NULL\").\n", elem1->schema->name, meta1->name);
116 fail();
117 }
118 if (meta2) {
119 fprintf(stderr, "\"%s\": meta mismatch (\"NULL\" and \"%s\").\n", elem1->schema->name, meta2->name);
120 fail();
121 }
122
123 /* check specific data node attributes */
124 switch (elem1->schema->nodetype) {
125 case LYS_CONTAINER:
126 case LYS_LIST:
127 case LYS_RPC:
128 case LYS_ACTION:
129 case LYS_NOTIF:
130 in1 = (struct lyd_node_inner *)elem1;
131 in2 = (struct lyd_node_inner *)elem2;
132
133 i1 = 0;
134 LY_LIST_FOR(in1->child, iter) {
135 ++i1;
136 }
137
138 i2 = 0;
139 LY_LIST_FOR(in2->child, iter) {
140 ++i2;
141 }
142
143 if (i1 != i2) {
144 fprintf(stderr, "\"%s\": child count mismatch (%u and %u).\n", elem1->schema->name, i1, i2);
145 fail();
146 }
147
148 if (i1 >= LYD_HT_MIN_ITEMS) {
149 if (!in1->children_ht || !in2->children_ht) {
150 fprintf(stderr, "\"%s\": missing hash table (%p and %p).\n", elem1->schema->name, in1->children_ht,
151 in2->children_ht);
152 fail();
153 }
154
155 LY_LIST_FOR(in1->child, iter) {
156 if (lyht_find(in1->children_ht, &iter, iter->hash, NULL)) {
157 fprintf(stderr, "\"%s\": missing child \"%s\" in the hash table 1.\n", elem1->schema->name, iter->schema->name);
158 fail();
159 }
160 }
161 LY_LIST_FOR(in2->child, iter) {
162 if (lyht_find(in2->children_ht, &iter, iter->hash, NULL)) {
163 fprintf(stderr, "\"%s\": missing child \"%s\" in the hash table 2.\n", elem1->schema->name, iter->schema->name);
164 fail();
165 }
166 }
167 }
168 break;
169 case LYS_LEAF:
170 case LYS_LEAFLIST:
171 case LYS_ANYDATA:
172 case LYS_ANYXML:
Michal Vasko8f359bf2020-07-28 10:41:15 +0200173 if (lyd_compare_single(elem1, elem2, 0)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200174 fprintf(stderr, "\"%s\": value mismatch.\n", elem1->schema->name);
175 fail();
176 }
177 break;
178 default:
179 fprintf(stderr, "Unexpected data node type.\n");
180 fail();
181 }
182
183 if (!elem1->hash) {
184 fprintf(stderr, "\"%s\": hash not calculated.\n", elem1->schema->name);
185 fail();
186 }
187 if (elem1->hash != elem2->hash) {
188 fprintf(stderr, "\"%s\": hashes do not match (%u and %u).\n", elem1->schema->name, elem1->hash, elem2->hash);
189 fail();
190 }
191 }
192
193 if (elem1) {
194 fprintf(stderr, "Schema mismatch (\"%s\" and \"NULL\").\n", elem1->schema->name);
195 fail();
196 }
197 if (elem2) {
198 fprintf(stderr, "Schema mismatch (\"NULL\" and \"%s\").\n", elem2->schema->name);
199 fail();
200 }
201}
202
203static int
204setup_f(void **state)
205{
206 struct state *st;
207
208 (*state) = st = calloc(1, sizeof *st);
209 assert_non_null(st);
210
211 /* libyang context */
212 assert_int_equal(ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &st->ctx), LY_SUCCESS);
213
214 return 0;
215}
216
217static int
218teardown_f(void **state)
219{
220 struct state *st = (*state);
221
222 lyd_free_siblings(st->dt1);
223 lyd_free_siblings(st->dt2);
224 ly_ctx_destroy(st->ctx, NULL);
225 free(st->mem);
226 free(st);
227 (*state) = NULL;
228
229 return 0;
230}
231
232static void
233test_ietf_interfaces(void **state)
234{
235 struct state *st = (*state);
236 int ret;
237 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100238 "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n"
239 " <interface>\n"
240 " <name>eth0</name>\n"
241 " <description>Ethernet 0</description>\n"
242 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
243 " <enabled>true</enabled>\n"
244 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
245 " <enabled>true</enabled>\n"
246 " <mtu>1500</mtu>\n"
247 " <address>\n"
248 " <ip>192.168.2.100</ip>\n"
249 " <prefix-length>24</prefix-length>\n"
250 " </address>\n"
251 " </ipv4>\n"
252 " </interface>\n"
253 " <interface>\n"
254 " <name>eth1</name>\n"
255 " <description>Ethernet 1</description>\n"
256 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
257 " <enabled>true</enabled>\n"
258 " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n"
259 " <enabled>true</enabled>\n"
260 " <mtu>1500</mtu>\n"
261 " <address>\n"
262 " <ip>10.10.1.5</ip>\n"
263 " <prefix-length>16</prefix-length>\n"
264 " </address>\n"
265 " </ipv4>\n"
266 " </interface>\n"
267 " <interface>\n"
268 " <name>gigaeth0</name>\n"
269 " <description>GigabitEthernet 0</description>\n"
270 " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n"
271 " <enabled>false</enabled>\n"
272 " </interface>\n"
273 "</interfaces>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200274
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100275 assert_non_null(ly_ctx_load_module(st->ctx, "ietf-ip", NULL, NULL));
276 assert_non_null(ly_ctx_load_module(st->ctx, "iana-if-type", NULL, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200277
Radek Krejci7931b192020-06-25 17:05:03 +0200278 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200279 assert_ptr_not_equal(st->dt1, NULL);
280
Radek Krejci7931b192020-06-25 17:05:03 +0200281 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200282 assert_int_equal(ret, 0);
283
Radek Krejci7931b192020-06-25 17:05:03 +0200284 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, st->mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &st->dt2));
Michal Vasko60ea6352020-06-29 13:39:39 +0200285 assert_ptr_not_equal(st->dt2, NULL);
286
287 check_data_tree(st->dt1, st->dt2);
288}
289
290static void
291test_origin(void **state)
292{
293 struct state *st = (*state);
294 int ret;
295 const char *origin_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100296 "module test-origin {"
297 " namespace \"urn:test-origin\";"
298 " prefix to;"
299 " import ietf-origin {"
300 " prefix or;"
301 " }"
302 ""
303 " container cont {"
304 " leaf leaf1 {"
305 " type string;"
306 " }"
307 " leaf leaf2 {"
308 " type string;"
309 " }"
310 " leaf leaf3 {"
311 " type uint8;"
312 " }"
313 " }"
314 "}";
Michal Vasko60ea6352020-06-29 13:39:39 +0200315 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100316 "<cont xmlns=\"urn:test-origin\">\n"
317 " <leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>\n"
318 " <leaf2>value2</leaf2>\n"
319 " <leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>\n"
320 "</cont>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200321
Michal Vasko3a41dff2020-07-15 14:30:28 +0200322 assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, origin_yang, LYS_IN_YANG, NULL));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100323 lys_set_implemented(ly_ctx_get_module_latest(st->ctx, "ietf-origin"), NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200324
Radek Krejci7931b192020-06-25 17:05:03 +0200325 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 assert_ptr_not_equal(st->dt1, NULL);
327
Radek Krejci7931b192020-06-25 17:05:03 +0200328 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 assert_int_equal(ret, 0);
330
Radek Krejci7931b192020-06-25 17:05:03 +0200331 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, st->mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &st->dt2));
Michal Vasko60ea6352020-06-29 13:39:39 +0200332 assert_ptr_not_equal(st->dt2, NULL);
333
334 check_data_tree(st->dt1, st->dt2);
335}
336
337static void
338test_statements(void **state)
339{
340 struct state *st = (*state);
341 int ret;
342 const char *links_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100343 "module links {\n"
344 " yang-version 1.1;\n"
345 " namespace \"urn:module2\";\n"
346 " prefix mod2;\n"
347 "\n"
348 " identity just-another-identity;\n"
349 "\n"
350 " leaf one-leaf {\n"
351 " type string;\n"
352 " }\n"
353 "\n"
354 " list list-for-augment {\n"
355 " key keyleaf;\n"
356 "\n"
357 " leaf keyleaf {\n"
358 " type string;\n"
359 " }\n"
360 "\n"
361 " leaf just-leaf {\n"
362 " type int32;\n"
363 " }\n"
364 " }\n"
365 "\n"
366 " leaf rleaf {\n"
367 " type string;\n"
368 " }\n"
369 "\n"
370 " leaf-list llist {\n"
371 " type string;\n"
372 " min-elements 0;\n"
373 " max-elements 100;\n"
374 " ordered-by user;\n"
375 " }\n"
376 "\n"
377 " grouping rgroup {\n"
378 " leaf rg1 {\n"
379 " type string;\n"
380 " }\n"
381 "\n"
382 " leaf rg2 {\n"
383 " type string;\n"
384 " }\n"
385 " }\n"
386 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200387
388 const char *statements_yang =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100389 "module statements {\n"
390 " namespace \"urn:module\";\n"
391 " prefix mod;\n"
392 " yang-version 1.1;\n"
393 "\n"
394 " import links {\n"
395 " prefix mod2;\n"
396 " }\n"
397 "\n"
398 " identity random-identity {\n"
399 " base \"mod2:just-another-identity\";\n"
400 " base \"another-identity\";\n"
401 " }\n"
402 "\n"
403 " identity another-identity {\n"
404 " base \"mod2:just-another-identity\";\n"
405 " }\n"
406 "\n"
407 " typedef percent {\n"
408 " type uint8 {\n"
409 " range \"0 .. 100\";\n"
410 " }\n"
411 " units percent;\n"
412 " }\n"
413 "\n"
414 " container ice-cream-shop {\n"
415 " container employees {\n"
416 " list employee {\n"
417 " config true;\n"
418 " key id;\n"
419 " unique name;\n"
420 " min-elements 0;\n"
421 " max-elements 100;\n"
422 "\n"
423 " leaf id {\n"
424 " type uint64;\n"
425 " mandatory true;\n"
426 " }\n"
427 "\n"
428 " leaf name {\n"
429 " type string;\n"
430 " }\n"
431 "\n"
432 " leaf age {\n"
433 " type uint32;\n"
434 " }\n"
435 " }\n"
436 " }\n"
437 " }\n"
438 "\n"
439 " container random {\n"
440 " choice switch {\n"
441 " case a {\n"
442 " leaf aleaf {\n"
443 " type string;\n"
444 " default aaa;\n"
445 " }\n"
446 " }\n"
447 "\n"
448 " case c {\n"
449 " leaf cleaf {\n"
450 " type string;\n"
451 " }\n"
452 " }\n"
453 " }\n"
454 "\n"
455 " anyxml xml-data;\n"
456 " anydata any-data;\n"
457 " leaf-list leaflist {\n"
458 " type string;\n"
459 " min-elements 0;\n"
460 " max-elements 20;\n"
461 " ordered-by system;\n"
462 " }\n"
463 "\n"
464 " grouping group {\n"
465 " leaf g1 {\n"
466 " mandatory false;\n"
467 " type percent;\n"
468 " }\n"
469 "\n"
470 " leaf g2 {\n"
471 " type string;\n"
472 " }\n"
473 " }\n"
474 "\n"
475 " uses group;\n"
476 " uses mod2:rgroup;\n"
477 "\n"
478 " leaf lref {\n"
479 " type leafref {\n"
480 " path \"/mod2:one-leaf\";\n"
481 " }\n"
482 " }\n"
483 "\n"
484 " leaf iref {\n"
485 " type identityref {\n"
486 " base \"mod2:just-another-identity\";\n"
487 " }\n"
488 " }\n"
489 " }\n"
490 "\n"
491 " augment \"/random\" {\n"
492 " leaf aug-leaf {\n"
493 " type string;\n"
494 " }\n"
495 " }\n"
496 "}\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200497
498 const char *data_xml =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100499 "<ice-cream-shop xmlns=\"urn:module\">\n"
500 " <employees>\n"
501 " <employee>\n"
502 " <id>0</id>\n"
503 " <name>John Doe</name>\n"
504 " <age>28</age>\n"
505 " </employee>\n"
506 " <employee>\n"
507 " <id>1</id>\n"
508 " <name>Dohn Joe</name>\n"
509 " <age>20</age>\n"
510 " </employee>\n"
511 " </employees>\n"
512 "</ice-cream-shop>\n"
513 "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>\n"
514 "<random xmlns=\"urn:module\">\n"
515 " <aleaf>string</aleaf>\n"
516 " <xml-data><anyxml>data</anyxml></xml-data>\n"
517 " <any-data><data>any data</data></any-data>\n"
518 " <leaflist>l0</leaflist>\n"
519 " <leaflist>l1</leaflist>\n"
520 " <leaflist>l2</leaflist>\n"
521 " <g1>40</g1>\n"
522 " <g2>string</g2>\n"
523 " <aug-leaf>string</aug-leaf>\n"
524 " <rg1>string</rg1>\n"
525 " <rg2>string</rg2>\n"
526 " <lref>reference leaf</lref>\n"
527 " <iref>random-identity</iref>\n"
528 "</random>\n";
Michal Vasko60ea6352020-06-29 13:39:39 +0200529
Michal Vasko3a41dff2020-07-15 14:30:28 +0200530 assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, links_yang, LYS_IN_YANG, NULL));
531 assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, statements_yang, LYS_IN_YANG, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200532
Radek Krejci7931b192020-06-25 17:05:03 +0200533 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1));
Michal Vasko60ea6352020-06-29 13:39:39 +0200534 assert_ptr_not_equal(st->dt1, NULL);
535
Radek Krejci7931b192020-06-25 17:05:03 +0200536 ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS);
Michal Vasko60ea6352020-06-29 13:39:39 +0200537 assert_int_equal(ret, 0);
538
Radek Krejci7931b192020-06-25 17:05:03 +0200539 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, st->mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &st->dt2));
Michal Vasko60ea6352020-06-29 13:39:39 +0200540 assert_ptr_not_equal(st->dt2, NULL);
541
542 check_data_tree(st->dt1, st->dt2);
543}
544
545// static void
546// test_types(void **state)
547// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100548// struct state *st = (*state);
549// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200550//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100551// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
552// assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200553//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100554// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/types.xml", LYD_XML, LYD_OPT_CONFIG);
555// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200556//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100557// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
558// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200559//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100560// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
561// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200562//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100563// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200564// }
565//
566// static void
567// test_annotations(void **state)
568// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100569// struct state *st = (*state);
570// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200571//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100572// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
573// assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200574//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100575// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG);
576// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200577//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100578// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
579// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200580//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100581// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
582// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200583//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100584// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200585// }
586//
587// static void
588// test_similar_annot_names(void **state)
589// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100590// struct state *st = (*state);
591// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200592//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100593// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
594// assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200595//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100596// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG);
597// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200598//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100599// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
600// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200601//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100602// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
603// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200604//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100605// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200606// }
607//
608// static void
609// test_many_child_annot(void **state)
610// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100611// struct state *st = (*state);
612// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200613//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100614// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
615// assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200616//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100617// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/many-childs-annot.xml", LYD_XML, LYD_OPT_CONFIG);
618// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200619//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100620// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
621// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200622//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100623// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
624// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200625//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100626// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200627// }
628//
629// static void
630// test_union(void **state)
631// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100632// struct state *st = (*state);
633// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200634//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100635// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
636// assert_non_null(ly_ctx_load_module(st->ctx, "union", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200637//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100638// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG);
639// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200640//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100641// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
642// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200643//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100644// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
645// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200646//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100647// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200648// }
649//
650// static void
651// test_union2(void **state)
652// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100653// struct state *st = (*state);
654// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200655//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100656// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
657// assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200658//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100659// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG);
660// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200661//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100662// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
663// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200664//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100665// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
666// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200667//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100668// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200669// }
670//
671// static void
672// test_collisions(void **state)
673// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100674// struct state *st = (*state);
675// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200676//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100677// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
678// assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200679//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100680// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG);
681// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200682//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100683// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
684// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200685//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100686// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
687// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200688//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100689// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200690// }
691//
692// static void
693// test_anydata(void **state)
694// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100695// struct state *st = (*state);
696// const struct lys_module *mod;
697// int ret;
698// const char *test_anydata =
699// "module test-anydata {"
700// " namespace \"urn:test-anydata\";"
701// " prefix ya;"
702// ""
703// " container cont {"
704// " anydata ntf;"
705// " }"
706// "}";
Michal Vasko60ea6352020-06-29 13:39:39 +0200707//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100708// assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200709//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100710// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
711// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200712//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100713/// * get notification in LYB format to set as anydata content */
714// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
715// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200716//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100717// lyd_free_withsiblings(st->dt1);
718// st->dt1 = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200719//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100720/// * now comes the real test, test anydata */
721// mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG);
722// assert_non_null(mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200723//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100724// st->dt1 = lyd_new(NULL, mod, "cont");
725// assert_non_null(st->dt1);
Michal Vasko60ea6352020-06-29 13:39:39 +0200726//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100727// assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD));
728// st->mem = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200729//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100730// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
731// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200732//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100733// ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL);
734// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200735//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100736// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
737// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200738//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100739// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200740//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100741/// * and also test the embedded notification itself */
742// free(st->mem);
743// ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem);
744// st->mem = malloc(ret);
745// memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200746//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100747// lyd_free_withsiblings(st->dt2);
748// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL);
749// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200750//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100751/// * parse the JSON again for this comparison */
752// lyd_free_withsiblings(st->dt1);
753// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL);
754// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200755//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100756// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200757// }
758//
759// static void
760// test_submodule_feature(void **state)
761// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100762// struct state *st = (*state);
763// const struct lys_module *mod;
764// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200765//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100766// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
767// mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL);
768// assert_non_null(mod);
769// assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200770//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100771// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG);
772// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200773//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100774// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
775// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200776//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100777// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
778// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200779//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100780// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200781// }
782//
783// static void
784// test_coliding_augments(void **state)
785// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100786// struct state *st = (*state);
787// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200788//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100789// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
790// assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL));
791// assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL));
792// assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200793//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100794// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG);
795// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200796//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100797// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
798// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200799//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100800// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
801// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200802//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100803// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200804// }
805//
806// static void
807// test_leafrefs(void **state)
808// {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100809// struct state *st = (*state);
810// int ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200811//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100812// ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files");
813// assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200814//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100815// st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT);
816// assert_ptr_not_equal(st->dt1, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200817//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100818// ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS);
819// assert_int_equal(ret, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200820//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100821// st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT);
822// assert_ptr_not_equal(st->dt2, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200823//
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100824// check_data_tree(st->dt1, st->dt2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200825// }
826
827int
828main(void)
829{
830 const struct CMUnitTest tests[] = {
831 cmocka_unit_test_setup_teardown(test_ietf_interfaces, setup_f, teardown_f),
832 cmocka_unit_test_setup_teardown(test_origin, setup_f, teardown_f),
833 cmocka_unit_test_setup_teardown(test_statements, setup_f, teardown_f),
834 /*cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f),
835 cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f),
836 cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f),
837 cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f),
838 cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f),
839 cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f),
840 cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f),
841 cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f),
842 cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f),
843 cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f),
844 cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f),*/
845 };
846
847 return cmocka_run_group_tests(tests, NULL, NULL);
848}