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 | * |
| 6 | * Copyright (c) 2019 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 <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 23 | #include "tests/config.h" |
| 24 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 25 | #include "../../src/context.h" |
| 26 | #include "../../src/printer_data.h" |
| 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 31 | |
| 32 | struct state_s { |
| 33 | void *func; |
| 34 | struct ly_ctx *ctx; |
| 35 | }; |
| 36 | |
| 37 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 38 | #define ENABLE_LOGGER_CHECKING 1 |
| 39 | |
| 40 | #if ENABLE_LOGGER_CHECKING |
| 41 | static void |
| 42 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 43 | { |
| 44 | (void) level; /* unused */ |
| 45 | if (store) { |
| 46 | if (path && path[0]) { |
| 47 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 48 | } else { |
| 49 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 50 | } |
| 51 | if (store > 0) { |
| 52 | --store; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | static int |
| 59 | setup(void **state) |
| 60 | { |
| 61 | struct state_s *s; |
| 62 | const char *schema_a = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;" |
| 63 | "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}}"; |
| 64 | const char *schema_b = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}" |
| 65 | "feature f; identity gigabit-ethernet { base defs:ethernet;}" |
| 66 | "container cont {leaf leaftarget {type empty;}" |
| 67 | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 68 | "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 69 | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
| 70 | "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}" |
| 71 | "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}" |
| 72 | "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}" |
| 73 | "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}" |
| 74 | "leaf-list leaflisttarget {type string;}" |
| 75 | "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}" |
| 76 | "leaf binary-norestr {type binary;}" |
| 77 | "leaf int8 {type int8 {range 10..20;}}" |
| 78 | "leaf uint8 {type uint8 {range 150..200;}}" |
| 79 | "leaf int16 {type int16 {range -20..-10;}}" |
| 80 | "leaf uint16 {type uint16 {range 150..200;}}" |
| 81 | "leaf int32 {type int32;}" |
| 82 | "leaf uint32 {type uint32;}" |
| 83 | "leaf int64 {type int64;}" |
| 84 | "leaf uint64 {type uint64;}" |
| 85 | "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}" |
| 86 | "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}" |
| 87 | "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}" |
| 88 | "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}" |
| 89 | "leaf str {type string {length 8..10; pattern '[a-z ]*';}}" |
| 90 | "leaf str-norestr {type string;}" |
| 91 | "leaf bool {type boolean;}" |
| 92 | "leaf empty {type empty;}" |
| 93 | "leaf ident {type identityref {base defs:interface-type;}}" |
| 94 | "leaf inst {type instance-identifier {require-instance true;}}" |
| 95 | "leaf inst-noreq {type instance-identifier {require-instance false;}}" |
| 96 | "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}" |
| 97 | "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}" |
| 98 | "leaf un1 {type union {" |
| 99 | "type leafref {path /int8; require-instance true;}" |
| 100 | "type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }" |
| 101 | "type string {length 1..20;}}}" |
| 102 | "anydata any;" |
| 103 | "rpc sum {input {leaf x {type uint8;} leaf y {type uint8;}} output {leaf result {type uint16;}}}}"; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 104 | const char *schema_c = |
| 105 | "module defaults {" |
| 106 | "namespace \"urn:defaults\";" |
| 107 | "prefix d;" |
| 108 | "leaf a {" |
| 109 | "type union {" |
| 110 | "type instance-identifier;" |
| 111 | "type string;" |
| 112 | "}" |
| 113 | "default \"/d:b\";" |
| 114 | "}" |
| 115 | "leaf b {" |
| 116 | "type string;" |
| 117 | "}" |
| 118 | "leaf c {" |
| 119 | "type string;" |
| 120 | "}" |
| 121 | "}"; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 122 | |
| 123 | s = calloc(1, sizeof *s); |
| 124 | assert_non_null(s); |
| 125 | |
| 126 | #if ENABLE_LOGGER_CHECKING |
| 127 | ly_set_log_clb(logger, 1); |
| 128 | #endif |
| 129 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &s->ctx)); |
| 131 | assert_non_null(ly_ctx_load_module(s->ctx, "ietf-netconf-with-defaults", "2011-06-01")); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 132 | assert_non_null(lys_parse_mem(s->ctx, schema_a, LYS_IN_YANG)); |
| 133 | assert_non_null(lys_parse_mem(s->ctx, schema_b, LYS_IN_YANG)); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 134 | assert_non_null(lys_parse_mem(s->ctx, schema_c, LYS_IN_YANG)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 135 | |
| 136 | *state = s; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | teardown(void **state) |
| 143 | { |
| 144 | struct state_s *s = (struct state_s*)(*state); |
| 145 | |
| 146 | #if ENABLE_LOGGER_CHECKING |
| 147 | if (s->func) { |
| 148 | fprintf(stderr, "%s\n", logbuf); |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | ly_ctx_destroy(s->ctx, NULL); |
| 153 | free(s); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | logbuf_clean(void) |
| 160 | { |
| 161 | logbuf[0] = '\0'; |
| 162 | } |
| 163 | |
| 164 | #if ENABLE_LOGGER_CHECKING |
| 165 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 166 | #else |
| 167 | # define logbuf_assert(str) |
| 168 | #endif |
| 169 | |
| 170 | static void |
| 171 | test_leaf(void **state) |
| 172 | { |
| 173 | struct state_s *s = (struct state_s*)(*state); |
| 174 | struct lyd_node *tree; |
| 175 | const char *data; |
| 176 | const char *result; |
| 177 | char *printed; |
| 178 | ssize_t len; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 179 | struct ly_out *out; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 180 | |
| 181 | s->func = test_leaf; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 182 | assert_non_null(out = ly_out_new_memory(&printed, 0)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 183 | |
| 184 | data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n </int8>"; |
| 185 | result = "<int8 xmlns=\"urn:tests:types\">15</int8>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 186 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 187 | assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 188 | assert_int_equal(len, strlen(printed)); |
| 189 | assert_string_equal(printed, result); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 190 | lyd_free_all(tree); |
| 191 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 192 | ly_out_free(out, NULL, 1); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 193 | s->func = NULL; |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | test_anydata(void **state) |
| 198 | { |
| 199 | struct state_s *s = (struct state_s*)(*state); |
| 200 | struct lyd_node *tree; |
| 201 | const char *data; |
| 202 | char *printed; |
| 203 | ssize_t len; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 204 | struct ly_out *out; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 205 | |
| 206 | s->func = test_anydata; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 207 | assert_non_null(out = ly_out_new_memory(&printed, 0)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 208 | |
| 209 | data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 210 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 211 | assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 212 | assert_int_equal(len, strlen(printed)); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 213 | /* canonized */ |
| 214 | data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>"; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 215 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 216 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 217 | lyd_free_all(tree); |
| 218 | |
| 219 | data = "<any xmlns=\"urn:tests:types\"/>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 220 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 221 | assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 222 | assert_int_equal(len, strlen(printed)); |
| 223 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 224 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 225 | lyd_free_all(tree); |
| 226 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 227 | data = |
| 228 | "<any xmlns=\"urn:tests:types\">" |
| 229 | "<cont>" |
| 230 | "<defs:elem1 xmlns:defs=\"urn:tests:defs\">" |
| 231 | "<elem2 xmlns:defaults=\"urn:defaults\" defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\">" |
| 232 | "</elem2>" |
| 233 | "</defs:elem1>" |
| 234 | "</cont>" |
| 235 | "</any>"; |
| 236 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
| 237 | /* cont should be normally parsed */ |
| 238 | assert_string_equal(tree->schema->name, "any"); |
| 239 | assert_int_equal(((struct lyd_node_any *)tree)->value_type, LYD_ANYDATA_DATATREE); |
| 240 | assert_string_equal(((struct lyd_node_any *)tree)->value.tree->schema->name, "cont"); |
| 241 | /* but its children not */ |
| 242 | assert_null(((struct lyd_node_inner *)(((struct lyd_node_any *)tree)->value.tree))->child->schema); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 243 | assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 244 | assert_int_equal(len, strlen(printed)); |
| 245 | /* canonized */ |
| 246 | data = |
| 247 | "<any xmlns=\"urn:tests:types\">" |
| 248 | "<cont>" |
| 249 | "<elem1 xmlns=\"urn:tests:defs\">" |
| 250 | "<elem2 xmlns=\"urn:tests:types\" xmlns:defs=\"urn:tests:defs\" xmlns:defaults=\"urn:defaults\"" |
| 251 | " defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\"/>" |
| 252 | "</elem1>" |
| 253 | "</cont>" |
| 254 | "</any>"; |
| 255 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 256 | ly_out_reset(out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 257 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 258 | lyd_free_all(tree); |
| 259 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 260 | ly_out_free(out, NULL, 1); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 261 | s->func = NULL; |
| 262 | } |
| 263 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 264 | static void |
| 265 | test_defaults(void **state) |
| 266 | { |
| 267 | struct state_s *s = (struct state_s*)(*state); |
| 268 | struct lyd_node *tree; |
| 269 | const char *data; |
| 270 | char *printed; |
| 271 | ssize_t len; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 272 | struct ly_out *out; |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 273 | |
| 274 | s->func = test_defaults; |
| 275 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 276 | assert_non_null(out = ly_out_new_memory(&printed, 0)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 277 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 278 | /* standard default value */ |
| 279 | data = "<c xmlns=\"urn:defaults\">aa</c>"; |
| 280 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
| 281 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 282 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 283 | assert_int_equal(len, strlen(printed)); |
| 284 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 285 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 286 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 287 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 288 | assert_int_equal(len, strlen(printed)); |
| 289 | data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a>"; |
| 290 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 291 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 292 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 293 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 294 | assert_int_equal(len, strlen(printed)); |
| 295 | data = "<c xmlns=\"urn:defaults\">aa</c>" |
| 296 | "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
| 297 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"; |
| 298 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 299 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 300 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 301 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 302 | assert_int_equal(len, strlen(printed)); |
| 303 | data = "<c xmlns=\"urn:defaults\">aa</c>" |
| 304 | "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
| 305 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"; |
| 306 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 307 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 308 | |
| 309 | lyd_free_all(tree); |
| 310 | |
| 311 | /* string value equal to the default but default is an unresolved instance-identifier, so they are not considered equal */ |
| 312 | data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\">/d:b</a>"; |
| 313 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
| 314 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 315 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 316 | assert_int_equal(len, strlen(printed)); |
| 317 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 318 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 319 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 320 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 321 | assert_int_equal(len, strlen(printed)); |
| 322 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 323 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 324 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 325 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 326 | assert_int_equal(len, strlen(printed)); |
| 327 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 328 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 329 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 330 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 331 | assert_int_equal(len, strlen(printed)); |
| 332 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 333 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 334 | |
| 335 | lyd_free_all(tree); |
| 336 | |
| 337 | /* instance-identifier value equal to the default, should be considered equal */ |
| 338 | data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>"; |
| 339 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
| 340 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 341 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 342 | assert_int_equal(len, strlen(printed)); |
| 343 | data = "<c xmlns=\"urn:defaults\">aa</c><b xmlns=\"urn:defaults\">val</b>"; |
| 344 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 345 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 346 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 347 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 348 | assert_int_equal(len, strlen(printed)); |
| 349 | data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>"; |
| 350 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 351 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 352 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 353 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 354 | assert_int_equal(len, strlen(printed)); |
| 355 | data = "<c xmlns=\"urn:defaults\">aa</c>" |
| 356 | "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" |
| 357 | " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>" |
| 358 | "<b xmlns=\"urn:defaults\">val</b>"; |
| 359 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 360 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 361 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 362 | assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 363 | assert_int_equal(len, strlen(printed)); |
| 364 | data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>"; |
| 365 | assert_string_equal(printed, data); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 366 | ly_out_reset(out); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 367 | |
| 368 | lyd_free_all(tree); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 369 | ly_out_free(out, NULL, 1); |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 370 | |
| 371 | s->func = NULL; |
| 372 | } |
| 373 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 374 | #if 0 |
| 375 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 376 | static void |
| 377 | test_rpc(void **state) |
| 378 | { |
| 379 | struct state_s *s = (struct state_s*)(*state); |
| 380 | struct lyd_node *tree1; |
| 381 | struct lyd_node *tree2; |
| 382 | const struct lyd_node **trees; |
| 383 | const char *request; |
| 384 | const char *reply, *result; |
| 385 | char *printed; |
| 386 | ssize_t len; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 387 | struct ly_out *out; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 388 | |
| 389 | s->func = test_rpc; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 390 | assert_non_null(out = ly_out_new_memory(&printed, 0)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 391 | |
| 392 | request = "<sum xmlns=\"urn:tests:types\"><x>10</x><y>20</y></sum>"; |
| 393 | reply = "<result xmlns=\"urn:tests:types\">30</result>"; |
| 394 | result = "<sum xmlns=\"urn:tests:types\"><result>30</result></sum>"; |
| 395 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 396 | assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 397 | assert_int_equal(len, strlen(printed)); |
| 398 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 399 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 400 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 401 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 402 | assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 403 | assert_int_equal(len, strlen(printed)); |
| 404 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 405 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 406 | lyd_trees_free(trees, 0); |
| 407 | lyd_free_all(tree1); |
| 408 | lyd_free_all(tree2); |
| 409 | |
| 410 | /* no arguments */ |
| 411 | request = "<sum xmlns=\"urn:tests:types\"/>"; |
| 412 | reply = ""; |
| 413 | result = "<sum xmlns=\"urn:tests:types\"/>"; |
| 414 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 415 | assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 416 | assert_int_equal(len, strlen(printed)); |
| 417 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 418 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 419 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 420 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 421 | assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 422 | assert_int_equal(len, strlen(printed)); |
| 423 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 424 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 425 | lyd_trees_free(trees, 0); |
| 426 | lyd_free_all(tree1); |
| 427 | lyd_free_all(tree2); |
| 428 | |
| 429 | /* action |
| 430 | * "container cont {leaf leaftarget {type empty;}" |
| 431 | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 432 | "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 433 | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
| 434 | */ |
| 435 | request = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><a>test</a></test></listtarget></cont>"; |
| 436 | reply = "<b xmlns=\"urn:tests:types\">test-reply</b>"; |
| 437 | result = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><b>test-reply</b></test></listtarget></cont>";; |
| 438 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 439 | assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 440 | assert_int_equal(len, strlen(printed)); |
| 441 | assert_string_equal(printed, request); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 442 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 443 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 444 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 445 | assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 446 | assert_int_equal(len, strlen(printed)); |
| 447 | assert_string_equal(printed, result); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 448 | ly_out_reset(out); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 449 | lyd_trees_free(trees, 0); |
| 450 | lyd_free_all(tree1); |
| 451 | lyd_free_all(tree2); |
| 452 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 453 | ly_out_free(out, NULL, 1); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 454 | s->func = NULL; |
| 455 | } |
| 456 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 457 | #endif |
| 458 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 459 | int main(void) |
| 460 | { |
| 461 | const struct CMUnitTest tests[] = { |
| 462 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
| 463 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 464 | cmocka_unit_test_setup_teardown(test_defaults, setup, teardown), |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 468 | } |