1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
|
/**
* @file test_parser_xml.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief unit tests for functions from parser_xml.c
*
* Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _UTEST_MAIN_
#include "utests.h"
#include "context.h"
#include "in.h"
#include "out.h"
#include "parser_data.h"
#include "printer_data.h"
#include "tree_data_internal.h"
#include "tree_schema.h"
static int
setup(void **state)
{
const char *schema =
"module a {\n"
" namespace urn:tests:a;\n"
" prefix a;\n"
" yang-version 1.1;\n"
" import ietf-yang-metadata {prefix md;}"
" list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
" leaf d {type string;}"
" container cont {leaf e {type boolean;}}"
" }"
" leaf foo { type string;}\n"
" container c {\n"
" leaf x {type string;}\n"
" action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }\n"
" notification n1 { leaf nl {type string;}}}\n"
" container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}\n"
" anydata any {config false;}\n"
" anyxml anyx;\n"
" leaf foo2 { type string; default \"default-val\"; }\n"
" leaf foo3 { type uint32; }\n"
" notification n2;"
" md:annotation attr {type enumeration {enum val;}}"
"}";
UTEST_SETUP;
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
return 0;
}
#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH, ERR_LINE) \
assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH, ERR_LINE);\
assert_null(MODEL)
#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
static void
test_leaf(void **state)
{
const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
struct lyd_node *tree;
struct lyd_node_term *leaf;
assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
leaf = (struct lyd_node_term *)tree;
CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
1, LYS_LEAF, 0, 0, NULL, 0);
leaf = (struct lyd_node_term *)tree->next->next;
CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
assert_true(leaf->flags & LYD_DEFAULT);
lyd_free_all(tree);
/* make foo2 explicit */
data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
1, LYS_LEAF, 0, 0, NULL, 0);
leaf = (struct lyd_node_term *)tree;
CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
assert_false(leaf->flags & LYD_DEFAULT);
lyd_free_all(tree);
/* parse foo2 but make it implicit, skip metadata xxx from missing schema */
data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:netconf:default:1.0\" "
"wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
1, LYS_LEAF, 0, 0, NULL, 0);
leaf = (struct lyd_node_term *)tree;
CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
assert_true(leaf->flags & LYD_DEFAULT);
lyd_free_all(tree);
/* invalid value */
data = "<l1 xmlns=\"urn:tests:a\"><a>val-a</a><b>val-b</b><c>1</c><cont><e>0</e></cont></l1>";
PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Invalid boolean value \"0\".", "/a:l1[a='val-a'][b='val-b'][c='1']/cont/e", 1);
}
static void
test_anydata(void **state)
{
const char *data;
char *str;
struct lyd_node *tree;
data = "<any xmlns=\"urn:tests:a\">\n"
" <element1>\n"
" <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
" </element1>\n"
" <element1a/>\n"
"</any>\n";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
1, LYS_ANYDATA, 0, 0, NULL, 0);
const char *data_expected =
"<any xmlns=\"urn:tests:a\">\n"
" <element1>\n"
" <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
" </element1>\n"
" <element1a/>\n"
"</any>\n";
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
lyd_free_all(tree);
assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
free(str);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
lyd_free_all(tree);
}
static void
test_anydata_strict_validation(void **state)
{
const char *data_without_schema;
const char *data_invalid;
const char *data_valid;
char *str;
struct lyd_node *tree;
// no shcema defiend for "urn:tests:no:schema" in the parsing context
data_without_schema = "<any xmlns=\"urn:tests:a\">\n"
" <x:element1 xmlns:x=\"urn:tests:no:schema\">\n"
" <x:element2>default-val</x:element2>\n"
" </x:element1>\n"
"</any>\n";
PARSER_CHECK_ERROR(data_without_schema, LYD_PARSE_ANYDATA_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"No module with namespace \"urn:tests:no:schema\" in the context.", "/a:any", 3);
// anydata value are based on "module a" defined in the setup function and loaded into the parsing context.
// However, the value passed in the anydata subtree is not defined in "module a".
data_invalid = "<any xmlns=\"urn:tests:a\">\n"
" <element1>default-val</element1>\n"
"</any>\n";
PARSER_CHECK_ERROR(data_invalid, LYD_PARSE_ANYDATA_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Node \"element1\" not found in the \"a\" module.", "/a:any", 2);
// anydata value are based on "module a" defined in the setup function and loaded into the parsing context
data_valid = "<any xmlns=\"urn:tests:a\">\n"
" <foo>default-val</foo>\n"
"</any>\n";
CHECK_PARSE_LYD(data_valid, LYD_PARSE_ANYDATA_STRICT, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
1, LYS_ANYDATA, 0, 0, NULL, 0);
const char *data_expected =
"<any xmlns=\"urn:tests:a\">\n"
" <foo>default-val</foo>\n"
"</any>\n";
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
lyd_free_all(tree);
assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
free(str);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
lyd_free_all(tree);
}
static void
test_anyxml(void **state)
{
const char *data;
char *str;
struct lyd_node *tree;
data = "<anyx xmlns=\"urn:tests:a\">\n"
" <element1>\n"
" <element2 x:attr2=\"test\" xmlns:x=\"urn:x\">data</element2>\n"
" </element1>\n"
" <element1a/>\n"
"</anyx>\n";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
const char *data_expected =
"<anyx xmlns=\"urn:tests:a\">\n"
" <element1>\n"
" <element2 xmlns:x=\"urn:x\" x:attr2=\"test\">data</element2>\n"
" </element1>\n"
" <element1a/>\n"
"</anyx>\n";
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
lyd_free_all(tree);
assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
free(str);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data_expected);
lyd_free_all(tree);
data = "<anyx xmlns=\"urn:tests:a\"><x>1</x><x>0</x><x>-1</x><x>4294967295</x><x>4294967296</x><x>-2147483648</x><x>-2147483649</x></anyx>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, tree, LYD_XML, LYD_PRINT_SHRINK));
CHECK_STRING(str, data);
free(str);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, tree, LYD_JSON, LYD_PRINT_SHRINK));
CHECK_STRING(str, "{\"a:anyx\":{\"x\":[1,0,-1,4294967295,\"4294967296\",-2147483648,\"-2147483649\"]}}");
free(str);
lyd_free_all(tree);
}
static void
test_list(void **state)
{
const char *data;
struct lyd_node *tree, *iter;
struct lyd_node_inner *list;
struct lyd_node_term *leaf;
/* check hashes */
data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
1, LYS_LIST, 0, 0, NULL, 0);
list = (struct lyd_node_inner *)tree;
LY_LIST_FOR(list->child, iter) {
assert_int_not_equal(0, iter->hash);
}
lyd_free_all(tree);
/* missing keys */
PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"List instance is missing its key \"a\".", "/a:l1[b='b'][c='1']", 1);
CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"List instance is missing its key \"b\".", "/a:l1[a='a']", 1);
PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"List instance is missing its key \"c\".", "/a:l1[a='a'][b='b']", 1);
CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
/* key duplicate */
PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a><c>1</c></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Duplicate instance of \"c\".", "/a:l1[a='a'][b='b'][c='1'][c='1']/c", 1);
CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
/* keys order */
CHECK_PARSE_LYD("<l1 xmlns=\"urn:tests:a\"><d>d</d><a>a</a><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
1, LYS_LIST, 0, 0, NULL, 0);
list = (struct lyd_node_inner *)tree;
assert_non_null(leaf = (struct lyd_node_term *)list->child);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0);
assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", 1, LYS_LEAF, 1, 0, NULL, 0);
assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0);
CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
lyd_free_all(tree);
data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1", 1, LYS_LIST, 0, 0, NULL, 0);
list = (struct lyd_node_inner *)tree;
assert_non_null(leaf = (struct lyd_node_term *)list->child);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0);
assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", 1, LYS_LEAF, 1, 0, NULL, 0);
assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
lyd_free_all(tree);
PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Invalid position of the key \"b\" in a list.", "/a:l1[c='1']/b", 1);
}
static void
test_container(void **state)
{
struct lyd_node *tree;
struct lyd_node_inner *cont;
CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
cont = (struct lyd_node_inner *)tree;
assert_true(cont->flags & LYD_DEFAULT);
lyd_free_all(tree);
CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
1, LYS_CONTAINER, 0, 0, NULL, 0);
cont = (struct lyd_node_inner *)tree;
assert_false(cont->flags & LYD_DEFAULT);
lyd_free_all(tree);
}
static void
test_opaq(void **state)
{
const char *data;
struct lyd_node *tree;
/* invalid value, no flags */
data = "<foo3 xmlns=\"urn:tests:a\"/>";
PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Invalid type uint32 empty value.", "/a:foo3", 1);
/* opaq flag */
CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "foo3", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
lyd_free_all(tree);
/* list, opaq flag */
data = "<l1 xmlns=\"urn:tests:a\"/>";
CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, "<l1 xmlns=\"urn:tests:a\"/>\n");
lyd_free_all(tree);
/* missing key, no flags */
data = "<l1 xmlns=\"urn:tests:a\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <d>val_d</d>\n"
"</l1>\n";
PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"List instance is missing its key \"c\".", "/a:l1[a='val_a'][b='val_b']", 5);
/* opaq flag */
CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
/* invalid key, no flags */
data = "<l1 xmlns=\"urn:tests:a\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>val_c</c>\n"
"</l1>\n";
PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
"Invalid type int16 value \"val_c\".", "/a:l1[a='val_a'][b='val_b']/c", 4);
/* opaq flag */
CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
/* opaq flag and fail */
assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
"<a xmlns=\"ns\">\n"
" <b>x</b>\n"
" <c xmld:id=\"D\">1</c>\n"
"</a>\n",
LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
CHECK_LOG_CTX("Unknown XML prefix \"xmld\" at attribute \"id\".", "/a", 3);
}
static void
test_rpc(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *op;
const struct lyd_node *node;
const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
"configuration to the specified target configuration.";
const char *ref = "RFC 6241, Section 7.2";
const char *feats[] = {"writable-running", NULL};
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <target>\n"
" <running/>\n"
" </target>\n"
" <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>val_c</c>\n"
" </l1>\n"
" <cp xmlns=\"urn:tests:a\">\n"
" <z nc:operation=\"delete\"/>\n"
" </cp>\n"
" </config>\n"
"</edit-config>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
assert_non_null(op);
CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "edit-config", LYS_RPC,
0, 0, 0, 0, 0, ref, 0);
assert_non_null(tree);
node = tree;
CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "edit-config", LYS_RPC,
0, 0, 0, 0, 0, ref, 0);
node = lyd_child(node)->next;
dsc = "Inline Config content.";
CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
node = ((struct lyd_node_any *)node)->value.tree;
CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
1, LYS_CONTAINER, 0, 0, NULL, 0);
node = lyd_child(node);
/* z has no value */
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 1, "");
node = node->parent->next;
/* l1 key c has invalid value so it is at the end */
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <target>\n"
" <running/>\n"
" </target>\n"
" <config>\n"
" <cp xmlns=\"urn:tests:a\">\n"
" <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
" </cp>\n"
" <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>val_c</c>\n"
" </l1>\n"
" </config>\n"
"</edit-config>\n");
lyd_free_all(tree);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_action(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *op;
data = "<c xmlns=\"urn:tests:a\">\n"
" <act>\n"
" <al>value</al>\n"
" </act>\n"
"</c>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
assert_non_null(op);
CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "act", LYS_ACTION,
1, 0, 0, 1, 0, NULL, 0);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<c xmlns=\"urn:tests:a\">\n"
" <act>\n"
" <al>value</al>\n"
" </act>\n"
"</c>\n");
lyd_free_all(tree);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_notification(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *ntf;
data = "<c xmlns=\"urn:tests:a\">\n"
" <n1>\n"
" <nl>value</nl>\n"
" </n1>\n"
"</c>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_YANG, LYD_PARSE_STRICT,
&tree, &ntf));
ly_in_free(in, 0);
assert_non_null(ntf);
CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
/* top-level notif without envelope */
data = "<n2 xmlns=\"urn:tests:a\"/>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_YANG, LYD_PARSE_STRICT,
&tree, &ntf));
ly_in_free(in, 0);
assert_non_null(ntf);
CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 0, NULL, 0, 0x4, 1, 0, "n2", 0, 0, NULL, 0);
assert_non_null(tree);
assert_ptr_equal(ntf, tree);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_reply(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *op;
const struct lyd_node *node;
data = "<c xmlns=\"urn:tests:a\">\n"
" <act>\n"
" <al>25</al>\n"
" </act>\n"
"</c>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_REPLY_YANG, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
assert_non_null(op);
CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "act", LYS_ACTION,
1, 0, 0, 1, 0, NULL, 0);
node = lyd_child(op);
CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_STATUS_CURR | LYS_IS_OUTPUT, 1, "al", 0, LYS_LEAF, 1, 0, NULL, 0);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
/* TODO print only rpc-reply node and then output subtree */
CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
lyd_free_all(tree);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_netconf_rpc(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *op;
const struct lyd_node *node;
const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
"configuration to the specified target configuration.";
const char *ref = "RFC 6241, Section 7.2";
const char *feats[] = {"writable-running", NULL};
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
"<edit-config>\n"
" <target>\n"
" <running/>\n"
" </target>\n"
" <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>val_c</c>\n"
" </l1>\n"
" <cp xmlns=\"urn:tests:a\">\n"
" <z nc:operation=\"delete\"/>\n"
" </cp>\n"
" </config>\n"
"</edit-config>\n"
"</rpc>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
assert_non_null(op);
node = tree;
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 1, 0, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
assert_non_null(tree);
node = op;
CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "edit-config", LYS_RPC,
0, 0, 0, 0, 0, ref, 0);
node = lyd_child(node)->next;
dsc = "Inline Config content.";
CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
node = ((struct lyd_node_any *)node)->value.tree;
CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
1, LYS_CONTAINER, 0, 0, NULL, 0);
node = lyd_child(node);
/* z has no value */
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 1, "");
node = node->parent->next;
/* l1 key c has invalid value so it is at the end */
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
CHECK_LYD_STRING(op, LYD_PRINT_SIBLINGS,
"<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <target>\n"
" <running/>\n"
" </target>\n"
" <config>\n"
" <cp xmlns=\"urn:tests:a\">\n"
" <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
" </cp>\n"
" <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>val_c</c>\n"
" </l1>\n"
" </config>\n"
"</edit-config>\n");
lyd_free_all(tree);
lyd_free_all(op);
/* invalid anyxml nested metadata value */
data = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"1\" pid=\"4114692032\">\n"
" <copy-config>\n"
" <target>\n"
" <running/>\n"
" </target>\n"
" <source>\n"
" <config>\n"
" <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>5</c>\n"
" <cont nc:operation=\"merge\">\n"
" <e nc:operation=\"merge2\">false</e>\n"
" </cont>\n"
" </l1>\n"
" </config>\n"
" </source>\n"
" </copy-config>\n"
"</rpc>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
CHECK_LOG_CTX("Invalid enumeration value \"merge2\".",
"/ietf-netconf:copy-config/source/config/a:l1[a='val_a'][b='val_b'][c='5']/cont/e/@ietf-netconf:operation", 13);
lyd_free_all(tree);
assert_null(op);
}
static void
test_netconf_action(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *op;
data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
"<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
"<c xmlns=\"urn:tests:a\">\n"
" <act>\n"
" <al>value</al>\n"
" </act>\n"
"</c>\n"
"</action>\n"
"</rpc>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, LYD_PARSE_STRICT,
&tree, &op));
ly_in_free(in, 0);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "action", 0, 0, 0, 0, "");
assert_non_null(op);
CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "act", LYS_ACTION,
1, 0, 0, 1, 0, NULL, 0);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
" <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
"</rpc>\n");
CHECK_LYD_STRING(op, LYD_PRINT_SIBLINGS,
"<act xmlns=\"urn:tests:a\">\n"
" <al>value</al>\n"
"</act>\n");
lyd_free_all(tree);
lyd_free_all(op);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_netconf_reply_or_notification(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *action, *tree, *op, *op2;
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", NULL)));
/* parse the action */
data = "<c xmlns=\"urn:tests:a\">\n"
" <act>\n"
" <al>value</al>\n"
" </act>\n"
"</c>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, LYD_PARSE_STRICT,
&action, &op));
ly_in_free(in, 0);
/* parse notification first */
data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
"<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
"<c xmlns=\"urn:tests:a\">\n"
" <n1>\n"
" <nl>value</nl>\n"
" </n1>\n"
"</c>\n"
"</notification>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_NETCONF, LYD_PARSE_STRICT,
&tree, &op2));
ly_in_free(in, 0);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_VALUE_XML, "notification", 0, 0, 0, 0, "");
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "eventTime", 0, 0, 0, 0,
"2010-12-06T08:00:01Z");
assert_non_null(op2);
CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
" <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
"</notification>\n");
CHECK_LYD_STRING(op2, LYD_PRINT_SIBLINGS,
"<n1 xmlns=\"urn:tests:a\">\n"
" <nl>value</nl>\n"
"</n1>\n");
lyd_free_all(tree);
lyd_free_all(op2);
/* notification with a different order */
data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
"<c xmlns=\"urn:tests:a\">\n"
" <n1>\n"
" <nl>value</nl>\n"
" </n1>\n"
"</c>\n"
"<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
"</notification>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_NETCONF, LYD_PARSE_STRICT,
&tree, &op2));
ly_in_free(in, 0);
lyd_free_all(tree);
lyd_free_all(op2);
/* parse a data reply */
data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <al xmlns=\"urn:tests:a\">25</al>\n"
"</rpc-reply>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, LYD_PARSE_STRICT,
&tree, NULL));
ly_in_free(in, 0);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 0, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS,
"<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
lyd_free_all(tree);
/* it was connected to the action, do not free */
/* parse an ok reply */
data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
" <ok/>\n"
"</rpc-reply>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, LYD_PARSE_STRICT,
&tree, NULL));
ly_in_free(in, 0);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "ok", 0, 0, 0, 0, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
/* parse an error reply */
data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
" <rpc-error>\n"
" <error-type>rpc</error-type>\n"
" <error-tag>missing-attribute</error-tag>\n"
" <error-severity>error</error-severity>\n"
" <error-path xmlns:a=\"urn:tests:a\">/a:c/a:x</error-path>\n"
" <error-info>\n"
" <bad-attribute>message-id</bad-attribute>\n"
" <bad-element>rpc</bad-element>\n"
" </error-info>\n"
" </rpc-error>\n"
"</rpc-reply>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, LYD_PARSE_STRICT,
&tree, NULL));
ly_in_free(in, 0);
CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
lyd_free_all(action);
/* wrong namespace, element name, whatever... */
/* TODO */
}
static void
test_restconf_rpc(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *envp;
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
data = "<input xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
"<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
"<config>"
"<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
"<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
"<a>val_a</a><b>val_b</b><c>val_c</c>"
"</l1>"
"</config></input>";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_XML, LYD_TYPE_RPC_RESTCONF, LYD_PARSE_STRICT,
&envp, NULL));
ly_in_free(in, 0);
/* the same just connected to the edit-data RPC */
data = "<edit-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
"<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
"<config>"
"<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
"<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
"<a>val_a</a><b>val_b</b><c>val_c</c>"
"</l1>"
"</config></edit-data>";
CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
lyd_free_all(envp);
}
static void
test_restconf_reply(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree, *envp;
assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
data = "<output xmlns=\"urn:tests:a\"><al>25</al></output>";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_XML, LYD_TYPE_REPLY_RESTCONF,
LYD_PARSE_STRICT, &envp, NULL));
ly_in_free(in, 0);
/* connected to the RPC with the parent */
data = "<c xmlns=\"urn:tests:a\"><act><al>25</al></act></c>";
CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
lyd_free_all(envp);
}
static void
test_filter_attributes(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree;
const struct lyd_node *node;
const char *dsc;
const char *ref = "RFC 6241, Section 7.7";
const char *feats[] = {"writable-running", NULL};
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "notifications", "2008-07-14", NULL)));
data = "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <filter type=\"xpath\" select=\"/*\"/>\n"
"</get>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, LYD_PARSE_STRICT,
&tree, NULL));
ly_in_free(in, 0);
assert_non_null(tree);
node = tree;
dsc = "Retrieve running configuration and device state information.";
CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
1, 0, 0, 1, "get", LYS_RPC,
1, 0, 0, 0, 0, ref, 0);
node = lyd_child(node);
dsc = "This parameter specifies the portion of the system\nconfiguration and state data to retrieve.";
CHECK_LYSC_NODE(node->schema, dsc, 1, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "filter", 0, LYS_ANYXML, 1, 0, NULL, 0);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
data = "<create-subscription xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
" <filter type=\"subtree\">\n"
" <inner-node xmlns=\"my:urn\"/>\n"
" </filter>\n"
"</create-subscription>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, LYD_PARSE_STRICT,
&tree, NULL));
ly_in_free(in, 0);
assert_non_null(tree);
CHECK_LYD_STRING(tree, LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);
}
static void
test_data_skip(void **state)
{
const char *data;
struct lyd_node *tree;
struct lyd_node_term *leaf;
/* add invalid data to a module that is not implemented */
data = "<foo xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"><u/></foo>";
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
assert_null(tree);
/* add invalid data to a module that is implemented */
data = "<fooX xmlns=\"urn:tests:a\"><u/><list><value/></list></fooX>";
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
assert_null(tree);
/* first invalid, next valid */
data = "<fooX xmlns=\"urn:tests:a\"><u/></fooX> <foo xmlns=\"urn:tests:a\">foo value</foo>";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
leaf = (struct lyd_node_term *)tree;
CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
lyd_free_all(tree);
}
static void
test_metadata(void **state)
{
const char *data;
struct lyd_node *tree;
/* invalid metadata value */
data = "<c xmlns=\"urn:tests:a\" xmlns:a=\"urn:tests:a\"><x a:attr=\"value\">xval</x></c>";
assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
assert_null(tree);
CHECK_LOG_CTX("Invalid enumeration value \"value\".", "/a:c/x/@a:attr", 1);
}
static void
test_subtree(void **state)
{
const char *data;
struct ly_in *in;
struct lyd_node *tree;
/* prepare data with the parent */
data = "<l1 xmlns=\"urn:tests:a\">\n"
" <a>val_a</a>\n"
" <b>val_b</b>\n"
" <c>1</c>\n"
"</l1>\n";
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
/* parse a subtree of it */
data = "<cont xmlns=\"urn:tests:a\">\n"
" <e>true</e>\n"
"</cont>\n";
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_SUCCESS, lyd_parse_data(UTEST_LYCTX, tree, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, NULL));
ly_in_free(in, 0);
/* parse another container, fails */
assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
assert_int_equal(LY_EVALID, lyd_parse_data(UTEST_LYCTX, tree, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, NULL));
ly_in_free(in, 0);
CHECK_LOG_CTX("Duplicate instance of \"cont\".", "/a:l1[a='val_a'][b='val_b'][c='1']/cont", 0);
lyd_free_all(tree);
}
int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_leaf, setup),
UTEST(test_anydata, setup),
UTEST(test_anydata_strict_validation, setup),
UTEST(test_anyxml, setup),
UTEST(test_list, setup),
UTEST(test_container, setup),
UTEST(test_opaq, setup),
UTEST(test_rpc, setup),
UTEST(test_action, setup),
UTEST(test_notification, setup),
UTEST(test_reply, setup),
UTEST(test_netconf_rpc, setup),
UTEST(test_netconf_action, setup),
UTEST(test_netconf_reply_or_notification, setup),
UTEST(test_restconf_rpc, setup),
UTEST(test_restconf_reply, setup),
UTEST(test_filter_attributes, setup),
UTEST(test_data_skip, setup),
UTEST(test_metadata, setup),
UTEST(test_subtree, setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|