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