Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_printer_xml.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from printer_yang.c |
| 5 | * |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 CESNET, z.s.p.o. |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 17 | #include <string.h> |
| 18 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 20 | #include "out.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 21 | #include "parser_data.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 22 | #include "printer_data.h" |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 23 | #include "tests/config.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 24 | #include "tree_schema.h" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 25 | |
| 26 | static int |
| 27 | setup(void **state) |
| 28 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 29 | const char *schema_defs = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 30 | "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 31 | const char *schema_types = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 32 | "feature f; identity gigabit-ethernet { base defs:ethernet;}" |
| 33 | "container cont {leaf leaftarget {type empty;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 34 | " list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 35 | " action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 36 | " leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 37 | "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}" |
| 38 | "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}" |
| 39 | "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}" |
| 40 | "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}" |
| 41 | "leaf-list leaflisttarget {type string;}" |
| 42 | "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}" |
| 43 | "leaf binary-norestr {type binary;}" |
| 44 | "leaf int8 {type int8 {range 10..20;}}" |
| 45 | "leaf uint8 {type uint8 {range 150..200;}}" |
| 46 | "leaf int16 {type int16 {range -20..-10;}}" |
| 47 | "leaf uint16 {type uint16 {range 150..200;}}" |
| 48 | "leaf int32 {type int32;}" |
| 49 | "leaf uint32 {type uint32;}" |
| 50 | "leaf int64 {type int64;}" |
| 51 | "leaf uint64 {type uint64;}" |
| 52 | "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}" |
| 53 | "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}" |
| 54 | "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}" |
| 55 | "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}" |
| 56 | "leaf str {type string {length 8..10; pattern '[a-z ]*';}}" |
| 57 | "leaf str-norestr {type string;}" |
| 58 | "leaf bool {type boolean;}" |
| 59 | "leaf empty {type empty;}" |
| 60 | "leaf ident {type identityref {base defs:interface-type;}}" |
| 61 | "leaf inst {type instance-identifier {require-instance true;}}" |
| 62 | "leaf inst-noreq {type instance-identifier {require-instance false;}}" |
| 63 | "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}" |
| 64 | "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}" |
| 65 | "leaf un1 {type union {" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 66 | " type leafref {path /int8; require-instance true;}" |
| 67 | " type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }" |
| 68 | " type string {length 1..20;}}}" |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 69 | "anydata any;" |
| 70 | "rpc sum {input {leaf x {type uint8;} leaf y {type uint8;}} output {leaf result {type uint16;}}}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 71 | const char *schema_defaults = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 72 | "module defaults {\n" |
| 73 | " namespace \"urn:defaults\";\n" |
| 74 | " prefix d;\n" |
| 75 | " leaf a {\n" |
| 76 | " type union {\n" |
| 77 | " type instance-identifier;\n" |
| 78 | " type string;\n" |
| 79 | " }\n" |
| 80 | " default \"/d:b\";\n" |
| 81 | " }\n" |
| 82 | " leaf b {\n" |
| 83 | " type string;\n" |
| 84 | " }\n" |
| 85 | " leaf c {\n" |
| 86 | " type string;\n" |
| 87 | " }\n" |
| 88 | "}"; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 89 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 90 | UTEST_SETUP; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 91 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 92 | UTEST_ADD_MODULE(schema_defs, LYS_IN_YANG, NULL, NULL); |
| 93 | UTEST_ADD_MODULE(schema_types, LYS_IN_YANG, NULL, NULL); |
| 94 | UTEST_ADD_MODULE(schema_defaults, LYS_IN_YANG, NULL, NULL); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 99 | #define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \ |
| 100 | CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE) |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 101 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 102 | #define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \ |
| 103 | CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION) |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 104 | |
| 105 | static void |
| 106 | test_leaf(void **state) |
| 107 | { |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 108 | struct lyd_node *tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 109 | const char *data, *result; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 110 | |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 111 | #if 0 |
| 112 | /* test on print int8 is in file ./tests/utests/types/int8.c */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 113 | data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n </int8>"; |
| 114 | result = "<int8 xmlns=\"urn:tests:types\">15</int8>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 115 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 116 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, result); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 117 | lyd_free_all(tree); |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 118 | #endif |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
| 122 | test_anydata(void **state) |
| 123 | { |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 124 | struct lyd_node *tree; |
| 125 | const char *data; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 126 | |
| 127 | data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 128 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 129 | /* canonized */ |
| 130 | data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 131 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 132 | lyd_free_all(tree); |
| 133 | |
| 134 | data = "<any xmlns=\"urn:tests:types\"/>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 135 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 136 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 137 | lyd_free_all(tree); |
| 138 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 139 | data = "<any xmlns=\"urn:tests:types\">\n" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 140 | " <cont>\n" |
| 141 | " <defs:elem1 xmlns:defs=\"urn:tests:defs\">\n" |
| 142 | " <elem2 xmlns:defaults=\"urn:defaults\" defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\">\n" |
| 143 | " </elem2>\n" |
| 144 | " </defs:elem1>\n" |
| 145 | " </cont>\n" |
| 146 | "</any>\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 147 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 148 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 149 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 150 | /* cont should be normally parsed */ |
| 151 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "any", 0, LYS_ANYDATA, 0, 0, NULL, 0); |
| 152 | CHECK_LYD_NODE_ANY((struct lyd_node_any *)tree, 0, 0, 0, LYD_ANYDATA_DATATREE); |
| 153 | struct lyd_node *tree_tmp = ((struct lyd_node_any *)tree)->value.tree; |
| 154 | |
| 155 | CHECK_LYSC_NODE(tree_tmp->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "cont", 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 156 | /* but its children not */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 157 | assert_null(((struct lyd_node_inner *)tree_tmp)->child->schema); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 158 | /* canonized */ |
| 159 | data = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 160 | "<any xmlns=\"urn:tests:types\">\n" |
| 161 | " <cont>\n" |
| 162 | " <elem1 xmlns=\"urn:tests:defs\">\n" |
| 163 | " <elem2 xmlns=\"urn:tests:types\" xmlns:defs=\"urn:tests:defs\" xmlns:defaults=\"urn:defaults\" " |
| 164 | "defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\"/>\n" |
| 165 | " </elem1>\n" |
| 166 | " </cont>\n" |
| 167 | "</any>\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 168 | CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 169 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 170 | lyd_free_all(tree); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 173 | static void |
| 174 | test_defaults(void **state) |
| 175 | { |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 176 | struct lyd_node *tree; |
| 177 | const char *data; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 178 | const char *data_trim; |
| 179 | const char *data_all; |
| 180 | const char *data_all_tag; |
| 181 | const char *data_impl_tag; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 182 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 183 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
| 184 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 185 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 186 | /* standard default value */ |
| 187 | data = "<c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 188 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 189 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 190 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 191 | data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 192 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 193 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 194 | data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 195 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>" |
| 196 | "<c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 197 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 198 | |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 199 | data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 200 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>" |
| 201 | "<c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 202 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 203 | lyd_free_all(tree); |
| 204 | |
| 205 | /* string value equal to the default but default is an unresolved instance-identifier, so they are not considered equal */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 206 | data = "<a xmlns=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 207 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 208 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 209 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 210 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 211 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 212 | lyd_free_all(tree); |
| 213 | |
| 214 | /* instance-identifier value equal to the default, should be considered equal */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 215 | data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 216 | data_trim = "<b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>"; |
| 217 | data_all = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>"; |
| 218 | data_all_tag = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 219 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>" |
| 220 | "<b xmlns=\"urn:defaults\">val</b>" |
| 221 | "<c xmlns=\"urn:defaults\">aa</c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 222 | data_impl_tag = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>"; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 223 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 224 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 225 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_trim); |
| 226 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_all); |
| 227 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_all_tag); |
| 228 | CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_impl_tag); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 229 | lyd_free_all(tree); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 232 | #if 0 |
| 233 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 234 | static void |
| 235 | test_rpc(void **state) |
| 236 | { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 237 | struct state_s *s = (struct state_s *)(*state); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 238 | struct lyd_node *tree1; |
| 239 | struct lyd_node *tree2; |
| 240 | const struct lyd_node **trees; |
| 241 | const char *request; |
| 242 | const char *reply, *result; |
| 243 | char *printed; |
| 244 | ssize_t len; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 245 | struct ly_out *out; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 246 | |
| 247 | s->func = test_rpc; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 248 | assert_non_null(out = ly_out_new_memory(&printed, 0)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 249 | |
| 250 | request = "<sum xmlns=\"urn:tests:types\"><x>10</x><y>20</y></sum>"; |
| 251 | reply = "<result xmlns=\"urn:tests:types\">30</result>"; |
| 252 | result = "<sum xmlns=\"urn:tests:types\"><result>30</result></sum>"; |
| 253 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 254 | assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 255 | assert_int_equal(len, strlen(printed)); |
| 256 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 257 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 258 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 259 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 260 | assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 261 | assert_int_equal(len, strlen(printed)); |
| 262 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 263 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 264 | lyd_trees_free(trees, 0); |
| 265 | lyd_free_all(tree1); |
| 266 | lyd_free_all(tree2); |
| 267 | |
| 268 | /* no arguments */ |
| 269 | request = "<sum xmlns=\"urn:tests:types\"/>"; |
| 270 | reply = ""; |
| 271 | result = "<sum xmlns=\"urn:tests:types\"/>"; |
| 272 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 273 | assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 274 | assert_int_equal(len, strlen(printed)); |
| 275 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 276 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 277 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 278 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 279 | assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 280 | assert_int_equal(len, strlen(printed)); |
| 281 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 282 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 283 | lyd_trees_free(trees, 0); |
| 284 | lyd_free_all(tree1); |
| 285 | lyd_free_all(tree2); |
| 286 | |
| 287 | /* action |
| 288 | * "container cont {leaf leaftarget {type empty;}" |
| 289 | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 290 | "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 291 | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
| 292 | */ |
| 293 | request = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><a>test</a></test></listtarget></cont>"; |
| 294 | reply = "<b xmlns=\"urn:tests:types\">test-reply</b>"; |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 295 | result = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><b>test-reply</b></test></listtarget></cont>"; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 296 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 297 | assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 298 | assert_int_equal(len, strlen(printed)); |
| 299 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 300 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 301 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 302 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 303 | assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 304 | assert_int_equal(len, strlen(printed)); |
| 305 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 306 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 307 | lyd_trees_free(trees, 0); |
| 308 | lyd_free_all(tree1); |
| 309 | lyd_free_all(tree2); |
| 310 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 311 | ly_out_free(out, NULL, 1); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 312 | s->func = NULL; |
| 313 | } |
| 314 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 315 | #endif |
| 316 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 317 | int |
| 318 | main(void) |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 319 | { |
| 320 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 321 | UTEST(test_leaf, setup), |
| 322 | UTEST(test_anydata, setup), |
| 323 | UTEST(test_defaults, setup), |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 327 | } |