Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
| 15 | #include <inttypes.h> |
| 16 | #include <setjmp.h> |
| 17 | #include <stdarg.h> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <cmocka.h> |
| 22 | |
| 23 | #include "hash_table.h" |
| 24 | #include "libyang.h" |
| 25 | #include "tests/config.h" |
| 26 | |
| 27 | struct state { |
| 28 | struct ly_ctx *ctx; |
| 29 | struct lyd_node *dt1, *dt2; |
| 30 | char *mem; |
| 31 | }; |
| 32 | |
| 33 | static void |
| 34 | check_data_tree_next(struct lyd_node **start, struct lyd_node **next, struct lyd_node **elem) |
| 35 | { |
| 36 | if (*elem) { |
| 37 | goto loop_next; |
| 38 | } |
| 39 | |
| 40 | loop_begin: |
| 41 | LYD_TREE_DFS_BEGIN(*start, *next, *elem) { |
| 42 | return; |
| 43 | loop_next: |
| 44 | LYD_TREE_DFS_END(*start, *next, *elem); |
| 45 | } |
| 46 | |
| 47 | if (!*next) { |
| 48 | /* top-level siblings */ |
| 49 | *start = (*start)->next; |
| 50 | if (!(*start)) { |
| 51 | *elem = NULL; |
| 52 | return; |
| 53 | } |
| 54 | goto loop_begin; |
| 55 | } |
| 56 | |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | check_data_tree(struct lyd_node *root1, struct lyd_node *root2) |
| 62 | { |
| 63 | struct lyd_node *next1, *next2, *elem1 = NULL, *elem2 = NULL, *iter; |
| 64 | struct lyd_meta *meta1, *meta2; |
| 65 | struct lyd_node_inner *in1, *in2; |
| 66 | uint32_t i1, i2; |
| 67 | |
| 68 | for (check_data_tree_next(&root1, &next1, &elem1), check_data_tree_next(&root2, &next2, &elem2); |
| 69 | elem1 && elem2; |
| 70 | check_data_tree_next(&root1, &next1, &elem1), check_data_tree_next(&root2, &next2, &elem2)) { |
| 71 | |
| 72 | if (elem1->schema != elem2->schema) { |
| 73 | fprintf(stderr, "Schema mismatch (\"%s\" and \"%s\").\n", elem1->schema->name, elem2->schema->name); |
| 74 | fail(); |
| 75 | } |
| 76 | |
| 77 | /* check common data node attributes */ |
| 78 | if (elem1->flags != elem2->flags) { |
| 79 | fprintf(stderr, "\"%s\": flags mismatch (\"%u\" and \"%u\").\n", elem1->schema->name, elem1->flags, elem2->flags); |
| 80 | fail(); |
| 81 | } |
| 82 | |
| 83 | /* check data node attributes */ |
| 84 | for (meta1 = elem1->meta, meta2 = elem2->meta; meta1 && meta2; meta1 = meta1->next, meta2 = meta2->next) { |
| 85 | if (meta1->annotation != meta2->annotation) { |
| 86 | fprintf(stderr, "\"%s\": meta annotation mismatch.\n", elem1->schema->name); |
| 87 | fail(); |
| 88 | } |
| 89 | if (strcmp(meta1->name, meta2->name)) { |
| 90 | fprintf(stderr, "\"%s\": meta name mismatch (\"%s\" and \"%s\").\n", elem1->schema->name, meta1->name, meta2->name); |
| 91 | fail(); |
| 92 | } |
| 93 | if (lyd_compare_meta(meta1, meta2)) { |
| 94 | fprintf(stderr, "\"%s\": meta value mismatch.\n", elem1->schema->name); |
| 95 | fail(); |
| 96 | } |
| 97 | } |
| 98 | if (meta1) { |
| 99 | fprintf(stderr, "\"%s\": meta mismatch (\"%s\" and \"NULL\").\n", elem1->schema->name, meta1->name); |
| 100 | fail(); |
| 101 | } |
| 102 | if (meta2) { |
| 103 | fprintf(stderr, "\"%s\": meta mismatch (\"NULL\" and \"%s\").\n", elem1->schema->name, meta2->name); |
| 104 | fail(); |
| 105 | } |
| 106 | |
| 107 | /* check specific data node attributes */ |
| 108 | switch (elem1->schema->nodetype) { |
| 109 | case LYS_CONTAINER: |
| 110 | case LYS_LIST: |
| 111 | case LYS_RPC: |
| 112 | case LYS_ACTION: |
| 113 | case LYS_NOTIF: |
| 114 | in1 = (struct lyd_node_inner *)elem1; |
| 115 | in2 = (struct lyd_node_inner *)elem2; |
| 116 | |
| 117 | i1 = 0; |
| 118 | LY_LIST_FOR(in1->child, iter) { |
| 119 | ++i1; |
| 120 | } |
| 121 | |
| 122 | i2 = 0; |
| 123 | LY_LIST_FOR(in2->child, iter) { |
| 124 | ++i2; |
| 125 | } |
| 126 | |
| 127 | if (i1 != i2) { |
| 128 | fprintf(stderr, "\"%s\": child count mismatch (%u and %u).\n", elem1->schema->name, i1, i2); |
| 129 | fail(); |
| 130 | } |
| 131 | |
| 132 | if (i1 >= LYD_HT_MIN_ITEMS) { |
| 133 | if (!in1->children_ht || !in2->children_ht) { |
| 134 | fprintf(stderr, "\"%s\": missing hash table (%p and %p).\n", elem1->schema->name, in1->children_ht, |
| 135 | in2->children_ht); |
| 136 | fail(); |
| 137 | } |
| 138 | |
| 139 | LY_LIST_FOR(in1->child, iter) { |
| 140 | if (lyht_find(in1->children_ht, &iter, iter->hash, NULL)) { |
| 141 | fprintf(stderr, "\"%s\": missing child \"%s\" in the hash table 1.\n", elem1->schema->name, iter->schema->name); |
| 142 | fail(); |
| 143 | } |
| 144 | } |
| 145 | LY_LIST_FOR(in2->child, iter) { |
| 146 | if (lyht_find(in2->children_ht, &iter, iter->hash, NULL)) { |
| 147 | fprintf(stderr, "\"%s\": missing child \"%s\" in the hash table 2.\n", elem1->schema->name, iter->schema->name); |
| 148 | fail(); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | break; |
| 153 | case LYS_LEAF: |
| 154 | case LYS_LEAFLIST: |
| 155 | case LYS_ANYDATA: |
| 156 | case LYS_ANYXML: |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 157 | if (lyd_compare_single(elem1, elem2, 0)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 158 | fprintf(stderr, "\"%s\": value mismatch.\n", elem1->schema->name); |
| 159 | fail(); |
| 160 | } |
| 161 | break; |
| 162 | default: |
| 163 | fprintf(stderr, "Unexpected data node type.\n"); |
| 164 | fail(); |
| 165 | } |
| 166 | |
| 167 | if (!elem1->hash) { |
| 168 | fprintf(stderr, "\"%s\": hash not calculated.\n", elem1->schema->name); |
| 169 | fail(); |
| 170 | } |
| 171 | if (elem1->hash != elem2->hash) { |
| 172 | fprintf(stderr, "\"%s\": hashes do not match (%u and %u).\n", elem1->schema->name, elem1->hash, elem2->hash); |
| 173 | fail(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (elem1) { |
| 178 | fprintf(stderr, "Schema mismatch (\"%s\" and \"NULL\").\n", elem1->schema->name); |
| 179 | fail(); |
| 180 | } |
| 181 | if (elem2) { |
| 182 | fprintf(stderr, "Schema mismatch (\"NULL\" and \"%s\").\n", elem2->schema->name); |
| 183 | fail(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static int |
| 188 | setup_f(void **state) |
| 189 | { |
| 190 | struct state *st; |
| 191 | |
| 192 | (*state) = st = calloc(1, sizeof *st); |
| 193 | assert_non_null(st); |
| 194 | |
| 195 | /* libyang context */ |
| 196 | assert_int_equal(ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &st->ctx), LY_SUCCESS); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int |
| 202 | teardown_f(void **state) |
| 203 | { |
| 204 | struct state *st = (*state); |
| 205 | |
| 206 | lyd_free_siblings(st->dt1); |
| 207 | lyd_free_siblings(st->dt2); |
| 208 | ly_ctx_destroy(st->ctx, NULL); |
| 209 | free(st->mem); |
| 210 | free(st); |
| 211 | (*state) = NULL; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | test_ietf_interfaces(void **state) |
| 218 | { |
| 219 | struct state *st = (*state); |
| 220 | int ret; |
| 221 | const char *data_xml = |
| 222 | "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">" |
| 223 | "<interface>" |
| 224 | "<name>eth0</name>" |
| 225 | "<description>Ethernet 0</description>" |
| 226 | "<type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>" |
| 227 | "<enabled>true</enabled>" |
| 228 | "<ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">" |
| 229 | "<enabled>true</enabled>" |
| 230 | "<mtu>1500</mtu>" |
| 231 | "<address>" |
| 232 | "<ip>192.168.2.100</ip>" |
| 233 | "<prefix-length>24</prefix-length>" |
| 234 | "</address>" |
| 235 | "</ipv4>" |
| 236 | "</interface>" |
| 237 | "<interface>" |
| 238 | "<name>eth1</name>" |
| 239 | "<description>Ethernet 1</description>" |
| 240 | "<type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>" |
| 241 | "<enabled>true</enabled>" |
| 242 | "<ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">" |
| 243 | "<enabled>true</enabled>" |
| 244 | "<mtu>1500</mtu>" |
| 245 | "<address>" |
| 246 | "<ip>10.10.1.5</ip>" |
| 247 | "<prefix-length>16</prefix-length>" |
| 248 | "</address>" |
| 249 | "</ipv4>" |
| 250 | "</interface>" |
| 251 | "<interface>" |
| 252 | "<name>gigaeth0</name>" |
| 253 | "<description>GigabitEthernet 0</description>" |
| 254 | "<type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>" |
| 255 | "<enabled>false</enabled>" |
| 256 | "</interface>" |
| 257 | "</interfaces>"; |
| 258 | |
| 259 | |
| 260 | assert_non_null(ly_ctx_load_module(st->ctx, "ietf-ip", NULL)); |
| 261 | assert_non_null(ly_ctx_load_module(st->ctx, "iana-if-type", NULL)); |
| 262 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 263 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 264 | assert_ptr_not_equal(st->dt1, NULL); |
| 265 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 266 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 267 | assert_int_equal(ret, 0); |
| 268 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 269 | 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 Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 270 | assert_ptr_not_equal(st->dt2, NULL); |
| 271 | |
| 272 | check_data_tree(st->dt1, st->dt2); |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | test_origin(void **state) |
| 277 | { |
| 278 | struct state *st = (*state); |
| 279 | int ret; |
| 280 | const char *origin_yang = |
| 281 | "module test-origin {" |
| 282 | " namespace \"urn:test-origin\";" |
| 283 | " prefix to;" |
| 284 | " import ietf-origin {" |
| 285 | " prefix or;" |
| 286 | " }" |
| 287 | "" |
| 288 | " container cont {" |
| 289 | " leaf leaf1 {" |
| 290 | " type string;" |
| 291 | " }" |
| 292 | " leaf leaf2 {" |
| 293 | " type string;" |
| 294 | " }" |
| 295 | " leaf leaf3 {" |
| 296 | " type uint8;" |
| 297 | " }" |
| 298 | " }" |
| 299 | "}"; |
| 300 | const char *data_xml = |
| 301 | "<cont xmlns=\"urn:test-origin\">" |
| 302 | "<leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>" |
| 303 | "<leaf2>value2</leaf2>" |
| 304 | "<leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>" |
| 305 | "</cont>"; |
| 306 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 307 | assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, origin_yang, LYS_IN_YANG, NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 308 | lys_set_implemented(ly_ctx_get_module_latest(st->ctx, "ietf-origin")); |
| 309 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 310 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 311 | assert_ptr_not_equal(st->dt1, NULL); |
| 312 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 313 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 314 | assert_int_equal(ret, 0); |
| 315 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 316 | 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 Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 317 | assert_ptr_not_equal(st->dt2, NULL); |
| 318 | |
| 319 | check_data_tree(st->dt1, st->dt2); |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | test_statements(void **state) |
| 324 | { |
| 325 | struct state *st = (*state); |
| 326 | int ret; |
| 327 | const char *links_yang = |
| 328 | "module links {" |
| 329 | "yang-version 1.1;" |
| 330 | "namespace \"urn:module2\";" |
| 331 | "prefix mod2;" |
| 332 | |
| 333 | "identity just-another-identity {" |
| 334 | |
| 335 | "}" |
| 336 | |
| 337 | "leaf one-leaf {" |
| 338 | "type string;" |
| 339 | "}" |
| 340 | |
| 341 | "list list-for-augment {" |
| 342 | "key keyleaf;" |
| 343 | |
| 344 | "leaf keyleaf {" |
| 345 | "type string;" |
| 346 | "}" |
| 347 | |
| 348 | "leaf just-leaf {" |
| 349 | "type int32;" |
| 350 | "}" |
| 351 | "}" |
| 352 | |
| 353 | "leaf rleaf {" |
| 354 | "type string;" |
| 355 | "}" |
| 356 | |
| 357 | "leaf-list llist {" |
| 358 | "type string;" |
| 359 | "min-elements 0;" |
| 360 | "max-elements 100;" |
| 361 | "ordered-by user;" |
| 362 | "}" |
| 363 | |
| 364 | "grouping rgroup {" |
| 365 | "leaf rg1 {" |
| 366 | "type string;" |
| 367 | "}" |
| 368 | |
| 369 | "leaf rg2 {" |
| 370 | "type string;" |
| 371 | "}" |
| 372 | "}" |
| 373 | "}"; |
| 374 | |
| 375 | const char *statements_yang = |
| 376 | "module statements {" |
| 377 | "namespace \"urn:module\";" |
| 378 | "prefix mod;" |
| 379 | "yang-version 1.1;" |
| 380 | |
| 381 | "import links {" |
| 382 | "prefix mod2;" |
| 383 | "}" |
| 384 | |
| 385 | "identity random-identity {" |
| 386 | "base \"mod2:just-another-identity\";" |
| 387 | "base \"another-identity\";" |
| 388 | "}" |
| 389 | |
| 390 | "identity another-identity {" |
| 391 | "base \"mod2:just-another-identity\";" |
| 392 | "}" |
| 393 | |
| 394 | "typedef percent {" |
| 395 | "type uint8 {" |
| 396 | "range \"0 .. 100\";" |
| 397 | "}" |
| 398 | "units percent;" |
| 399 | "}" |
| 400 | |
| 401 | "container ice-cream-shop {" |
| 402 | "container employees {" |
| 403 | "list employee {" |
| 404 | "config true;" |
| 405 | "key id;" |
| 406 | "unique name;" |
| 407 | "min-elements 0;" |
| 408 | "max-elements 100;" |
| 409 | |
| 410 | "leaf id {" |
| 411 | "type uint64;" |
| 412 | "mandatory true;" |
| 413 | "}" |
| 414 | |
| 415 | "leaf name {" |
| 416 | "type string;" |
| 417 | "}" |
| 418 | |
| 419 | "leaf age {" |
| 420 | "type uint32;" |
| 421 | "}" |
| 422 | "}" |
| 423 | "}" |
| 424 | "}" |
| 425 | |
| 426 | "container random {" |
| 427 | "choice switch {" |
| 428 | "case a {" |
| 429 | "leaf aleaf {" |
| 430 | "type string;" |
| 431 | "default aaa;" |
| 432 | "}" |
| 433 | "}" |
| 434 | |
| 435 | "case c {" |
| 436 | "leaf cleaf {" |
| 437 | "type string;" |
| 438 | "}" |
| 439 | "}" |
| 440 | "}" |
| 441 | |
| 442 | "anyxml xml-data;" |
| 443 | "anydata any-data;" |
| 444 | "leaf-list leaflist {" |
| 445 | "type string;" |
| 446 | "min-elements 0;" |
| 447 | "max-elements 20;" |
| 448 | "ordered-by system;" |
| 449 | "}" |
| 450 | |
| 451 | "grouping group {" |
| 452 | "leaf g1 {" |
| 453 | "mandatory false;" |
| 454 | "type percent;" |
| 455 | "}" |
| 456 | |
| 457 | "leaf g2 {" |
| 458 | "type string;" |
| 459 | "}" |
| 460 | "}" |
| 461 | |
| 462 | "uses group;" |
| 463 | "uses mod2:rgroup;" |
| 464 | |
| 465 | "leaf lref {" |
| 466 | "type leafref {" |
| 467 | "path \"/mod2:one-leaf\";" |
| 468 | "}" |
| 469 | "}" |
| 470 | |
| 471 | "leaf iref {" |
| 472 | "type identityref {" |
| 473 | "base \"mod2:just-another-identity\";" |
| 474 | "}" |
| 475 | "}" |
| 476 | "}" |
| 477 | |
| 478 | "augment \"/random\" {" |
| 479 | "leaf aug-leaf {" |
| 480 | "type string;" |
| 481 | "}" |
| 482 | "}" |
| 483 | "}"; |
| 484 | |
| 485 | const char *data_xml = |
| 486 | "<ice-cream-shop xmlns=\"urn:module\">" |
| 487 | "<employees>" |
| 488 | "<employee>" |
| 489 | "<id>0</id>" |
| 490 | "<name>John Doe</name>" |
| 491 | "<age>28</age>" |
| 492 | "</employee>" |
| 493 | "<employee>" |
| 494 | "<id>1</id>" |
| 495 | "<name>Dohn Joe</name>" |
| 496 | "<age>20</age>" |
| 497 | "</employee>" |
| 498 | "</employees>" |
| 499 | "</ice-cream-shop>" |
| 500 | "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>" |
| 501 | "<random xmlns=\"urn:module\">" |
| 502 | "<aleaf>string</aleaf>" |
| 503 | "<xml-data><anyxml>data</anyxml></xml-data>" |
| 504 | "<any-data><data>any data</data></any-data>" |
| 505 | "<leaflist>l0</leaflist>" |
| 506 | "<leaflist>l1</leaflist>" |
| 507 | "<leaflist>l2</leaflist>" |
| 508 | "<g1>40</g1>" |
| 509 | "<g2>string</g2>" |
| 510 | "<aug-leaf>string</aug-leaf>" |
| 511 | "<rg1>string</rg1>" |
| 512 | "<rg2>string</rg2>" |
| 513 | "<lref>reference leaf</lref>" |
| 514 | "<iref>random-identity</iref>" |
| 515 | "</random>"; |
| 516 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 517 | assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, links_yang, LYS_IN_YANG, NULL)); |
| 518 | assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, statements_yang, LYS_IN_YANG, NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 519 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 520 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, data_xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->dt1)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 521 | assert_ptr_not_equal(st->dt1, NULL); |
| 522 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 523 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYD_PRINT_WITHSIBLINGS); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 524 | assert_int_equal(ret, 0); |
| 525 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 526 | 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 Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 527 | assert_ptr_not_equal(st->dt2, NULL); |
| 528 | |
| 529 | check_data_tree(st->dt1, st->dt2); |
| 530 | } |
| 531 | |
| 532 | // static void |
| 533 | // test_types(void **state) |
| 534 | // { |
| 535 | // struct state *st = (*state); |
| 536 | // int ret; |
| 537 | // |
| 538 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 539 | // assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL)); |
| 540 | // |
| 541 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/types.xml", LYD_XML, LYD_OPT_CONFIG); |
| 542 | // assert_ptr_not_equal(st->dt1, NULL); |
| 543 | // |
| 544 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 545 | // assert_int_equal(ret, 0); |
| 546 | // |
| 547 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 548 | // assert_ptr_not_equal(st->dt2, NULL); |
| 549 | // |
| 550 | // check_data_tree(st->dt1, st->dt2); |
| 551 | // } |
| 552 | // |
| 553 | // static void |
| 554 | // test_annotations(void **state) |
| 555 | // { |
| 556 | // struct state *st = (*state); |
| 557 | // int ret; |
| 558 | // |
| 559 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 560 | // assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 561 | // |
| 562 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG); |
| 563 | // assert_ptr_not_equal(st->dt1, NULL); |
| 564 | // |
| 565 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 566 | // assert_int_equal(ret, 0); |
| 567 | // |
| 568 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 569 | // assert_ptr_not_equal(st->dt2, NULL); |
| 570 | // |
| 571 | // check_data_tree(st->dt1, st->dt2); |
| 572 | // } |
| 573 | // |
| 574 | // static void |
| 575 | // test_similar_annot_names(void **state) |
| 576 | // { |
| 577 | // struct state *st = (*state); |
| 578 | // int ret; |
| 579 | // |
| 580 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 581 | // assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 582 | // |
| 583 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG); |
| 584 | // assert_ptr_not_equal(st->dt1, NULL); |
| 585 | // |
| 586 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 587 | // assert_int_equal(ret, 0); |
| 588 | // |
| 589 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 590 | // assert_ptr_not_equal(st->dt2, NULL); |
| 591 | // |
| 592 | // check_data_tree(st->dt1, st->dt2); |
| 593 | // } |
| 594 | // |
| 595 | // static void |
| 596 | // test_many_child_annot(void **state) |
| 597 | // { |
| 598 | // struct state *st = (*state); |
| 599 | // int ret; |
| 600 | // |
| 601 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 602 | // assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 603 | // |
| 604 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/many-childs-annot.xml", LYD_XML, 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 | // |
| 616 | // static void |
| 617 | // test_union(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, "union", NULL)); |
| 624 | // |
| 625 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG); |
| 626 | // assert_ptr_not_equal(st->dt1, NULL); |
| 627 | // |
| 628 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 629 | // assert_int_equal(ret, 0); |
| 630 | // |
| 631 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 632 | // assert_ptr_not_equal(st->dt2, NULL); |
| 633 | // |
| 634 | // check_data_tree(st->dt1, st->dt2); |
| 635 | // } |
| 636 | // |
| 637 | // static void |
| 638 | // test_union2(void **state) |
| 639 | // { |
| 640 | // struct state *st = (*state); |
| 641 | // int ret; |
| 642 | // |
| 643 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 644 | // assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL)); |
| 645 | // |
| 646 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG); |
| 647 | // assert_ptr_not_equal(st->dt1, NULL); |
| 648 | // |
| 649 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 650 | // assert_int_equal(ret, 0); |
| 651 | // |
| 652 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 653 | // assert_ptr_not_equal(st->dt2, NULL); |
| 654 | // |
| 655 | // check_data_tree(st->dt1, st->dt2); |
| 656 | // } |
| 657 | // |
| 658 | // static void |
| 659 | // test_collisions(void **state) |
| 660 | // { |
| 661 | // struct state *st = (*state); |
| 662 | // int ret; |
| 663 | // |
| 664 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 665 | // assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 666 | // |
| 667 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG); |
| 668 | // assert_ptr_not_equal(st->dt1, NULL); |
| 669 | // |
| 670 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 671 | // assert_int_equal(ret, 0); |
| 672 | // |
| 673 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 674 | // assert_ptr_not_equal(st->dt2, NULL); |
| 675 | // |
| 676 | // check_data_tree(st->dt1, st->dt2); |
| 677 | // } |
| 678 | // |
| 679 | // static void |
| 680 | // test_anydata(void **state) |
| 681 | // { |
| 682 | // struct state *st = (*state); |
| 683 | // const struct lys_module *mod; |
| 684 | // int ret; |
| 685 | // const char *test_anydata = |
| 686 | // "module test-anydata {" |
| 687 | // " namespace \"urn:test-anydata\";" |
| 688 | // " prefix ya;" |
| 689 | // "" |
| 690 | // " container cont {" |
| 691 | // " anydata ntf;" |
| 692 | // " }" |
| 693 | // "}"; |
| 694 | // |
| 695 | // assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL)); |
| 696 | // |
| 697 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL); |
| 698 | // assert_ptr_not_equal(st->dt1, NULL); |
| 699 | // |
| 700 | // /* get notification in LYB format to set as anydata content */ |
| 701 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 702 | // assert_int_equal(ret, 0); |
| 703 | // |
| 704 | // lyd_free_withsiblings(st->dt1); |
| 705 | // st->dt1 = NULL; |
| 706 | // |
| 707 | // /* now comes the real test, test anydata */ |
| 708 | // mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG); |
| 709 | // assert_non_null(mod); |
| 710 | // |
| 711 | // st->dt1 = lyd_new(NULL, mod, "cont"); |
| 712 | // assert_non_null(st->dt1); |
| 713 | // |
| 714 | // assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD)); |
| 715 | // st->mem = NULL; |
| 716 | // |
| 717 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 718 | // assert_int_equal(ret, 0); |
| 719 | // |
| 720 | // ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL); |
| 721 | // assert_int_equal(ret, 0); |
| 722 | // |
| 723 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 724 | // assert_ptr_not_equal(st->dt2, NULL); |
| 725 | // |
| 726 | // check_data_tree(st->dt1, st->dt2); |
| 727 | // |
| 728 | // /* and also test the embedded notification itself */ |
| 729 | // free(st->mem); |
| 730 | // ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem); |
| 731 | // st->mem = malloc(ret); |
| 732 | // memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret); |
| 733 | // |
| 734 | // lyd_free_withsiblings(st->dt2); |
| 735 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL); |
| 736 | // assert_ptr_not_equal(st->dt2, NULL); |
| 737 | // |
| 738 | // /* parse the JSON again for this comparison */ |
| 739 | // lyd_free_withsiblings(st->dt1); |
| 740 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL); |
| 741 | // assert_ptr_not_equal(st->dt1, NULL); |
| 742 | // |
| 743 | // check_data_tree(st->dt1, st->dt2); |
| 744 | // } |
| 745 | // |
| 746 | // static void |
| 747 | // test_submodule_feature(void **state) |
| 748 | // { |
| 749 | // struct state *st = (*state); |
| 750 | // const struct lys_module *mod; |
| 751 | // int ret; |
| 752 | // |
| 753 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 754 | // mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL); |
| 755 | // assert_non_null(mod); |
| 756 | // assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0); |
| 757 | // |
| 758 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG); |
| 759 | // assert_ptr_not_equal(st->dt1, NULL); |
| 760 | // |
| 761 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 762 | // assert_int_equal(ret, 0); |
| 763 | // |
| 764 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 765 | // assert_ptr_not_equal(st->dt2, NULL); |
| 766 | // |
| 767 | // check_data_tree(st->dt1, st->dt2); |
| 768 | // } |
| 769 | // |
| 770 | // static void |
| 771 | // test_coliding_augments(void **state) |
| 772 | // { |
| 773 | // struct state *st = (*state); |
| 774 | // int ret; |
| 775 | // |
| 776 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 777 | // assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL)); |
| 778 | // assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL)); |
| 779 | // assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL)); |
| 780 | // |
| 781 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG); |
| 782 | // assert_ptr_not_equal(st->dt1, NULL); |
| 783 | // |
| 784 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 785 | // assert_int_equal(ret, 0); |
| 786 | // |
| 787 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 788 | // assert_ptr_not_equal(st->dt2, NULL); |
| 789 | // |
| 790 | // check_data_tree(st->dt1, st->dt2); |
| 791 | // } |
| 792 | // |
| 793 | // static void |
| 794 | // test_leafrefs(void **state) |
| 795 | // { |
| 796 | // struct state *st = (*state); |
| 797 | // int ret; |
| 798 | // |
| 799 | // ly_ctx_set_searchdir(st->ctx, TESTS_DIR"/data/files"); |
| 800 | // assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL)); |
| 801 | // |
| 802 | // st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 803 | // assert_ptr_not_equal(st->dt1, NULL); |
| 804 | // |
| 805 | // ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 806 | // assert_int_equal(ret, 0); |
| 807 | // |
| 808 | // st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 809 | // assert_ptr_not_equal(st->dt2, NULL); |
| 810 | // |
| 811 | // check_data_tree(st->dt1, st->dt2); |
| 812 | // } |
| 813 | |
| 814 | int |
| 815 | main(void) |
| 816 | { |
| 817 | const struct CMUnitTest tests[] = { |
| 818 | cmocka_unit_test_setup_teardown(test_ietf_interfaces, setup_f, teardown_f), |
| 819 | cmocka_unit_test_setup_teardown(test_origin, setup_f, teardown_f), |
| 820 | cmocka_unit_test_setup_teardown(test_statements, setup_f, teardown_f), |
| 821 | /*cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f), |
| 822 | cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f), |
| 823 | cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f), |
| 824 | cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f), |
| 825 | cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f), |
| 826 | cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f), |
| 827 | cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f), |
| 828 | cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f), |
| 829 | cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f), |
| 830 | cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f), |
| 831 | cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f),*/ |
| 832 | }; |
| 833 | |
| 834 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 835 | } |