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