blob: 8d7138fae17d31b4603fe87feb9b65433da625dc [file] [log] [blame]
Radek Krejci26a5dfb2019-07-26 14:51:06 +02001/*
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 Krejcica376bd2020-06-11 16:04:06 +020021#include <stdlib.h>
Radek Krejci26a5dfb2019-07-26 14:51:06 +020022#include <string.h>
23
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "context.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "out.h"
Radek Krejci70593c12020-06-13 20:48:09 +020027#include "printer_data.h"
Michal Vasko6f4cbb62020-02-28 11:15:47 +010028#include "tests/config.h"
Radek Krejci70593c12020-06-13 20:48:09 +020029#include "tree_schema.h"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020030
31#define BUFSIZE 1024
32char logbuf[BUFSIZE] = {0};
33int store = -1; /* negative for infinite logging, positive for limited logging */
34
35struct 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
44static void
45logger(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
61static int
62setup(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 Vasko6f4cbb62020-02-28 11:15:47 +0100107 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 Krejci26a5dfb2019-07-26 14:51:06 +0200125
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 Vasko6f4cbb62020-02-28 11:15:47 +0100133 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &s->ctx));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 assert_non_null(ly_ctx_load_module(s->ctx, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200135 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, schema_a, LYS_IN_YANG, NULL));
136 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, schema_b, LYS_IN_YANG, NULL));
137 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, schema_c, LYS_IN_YANG, NULL));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200138
139 *state = s;
140
141 return 0;
142}
143
144static int
145teardown(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
161void
162logbuf_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
173static void
174test_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;
Radek Krejci241f6b52020-05-21 18:13:49 +0200181 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200182
183 s->func = test_leaf;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200184 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200185
186 data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n </int8>";
187 result = "<int8 xmlns=\"urn:tests:types\">15</int8>";
Radek Krejci7931b192020-06-25 17:05:03 +0200188 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko26123192020-11-09 21:02:34 +0100189 assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 assert_int_equal(strlen(printed), ly_out_printed(out));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200191 assert_string_equal(printed, result);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200192 lyd_free_all(tree);
193
Radek Krejci241f6b52020-05-21 18:13:49 +0200194 ly_out_free(out, NULL, 1);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200195 s->func = NULL;
196}
197
198static void
199test_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;
Radek Krejci241f6b52020-05-21 18:13:49 +0200205 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200206
207 s->func = test_anydata;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200208 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200209
210 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>";
Radek Krejci7931b192020-06-25 17:05:03 +0200211 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko26123192020-11-09 21:02:34 +0100212 assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200213 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko52927e22020-03-16 17:26:14 +0100214 /* canonized */
215 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>";
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200216 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200217 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200218 lyd_free_all(tree);
219
220 data = "<any xmlns=\"urn:tests:types\"/>";
Radek Krejci7931b192020-06-25 17:05:03 +0200221 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko26123192020-11-09 21:02:34 +0100222 assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200223 assert_int_equal(strlen(printed), ly_out_printed(out));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200224 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200225 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200226 lyd_free_all(tree);
227
Michal Vasko52927e22020-03-16 17:26:14 +0100228 data =
229 "<any xmlns=\"urn:tests:types\">"
230 "<cont>"
231 "<defs:elem1 xmlns:defs=\"urn:tests:defs\">"
232 "<elem2 xmlns:defaults=\"urn:defaults\" defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\">"
233 "</elem2>"
234 "</defs:elem1>"
235 "</cont>"
236 "</any>";
Radek Krejci7931b192020-06-25 17:05:03 +0200237 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko52927e22020-03-16 17:26:14 +0100238 /* cont should be normally parsed */
Michal Vasko26123192020-11-09 21:02:34 +0100239 tree = tree->next;
Michal Vasko52927e22020-03-16 17:26:14 +0100240 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 Krejci52f65552020-09-01 17:03:35 +0200245 assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree, LYD_XML, LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200246 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko52927e22020-03-16 17:26:14 +0100247 /* 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 Krejci241f6b52020-05-21 18:13:49 +0200258 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100259
Michal Vasko52927e22020-03-16 17:26:14 +0100260 lyd_free_all(tree);
261
Radek Krejci241f6b52020-05-21 18:13:49 +0200262 ly_out_free(out, NULL, 1);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200263 s->func = NULL;
264}
265
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100266static void
267test_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;
Radek Krejci241f6b52020-05-21 18:13:49 +0200273 struct ly_out *out;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100274
275 s->func = test_defaults;
276
Radek Krejci84ce7b12020-06-11 17:28:25 +0200277 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100278
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100279 /* standard default value */
280 data = "<c xmlns=\"urn:defaults\">aa</c>";
Radek Krejci7931b192020-06-25 17:05:03 +0200281 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100282
Radek Krejci52f65552020-09-01 17:03:35 +0200283 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200284 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100285 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200286 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100287
Radek Krejci52f65552020-09-01 17:03:35 +0200288 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200289 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200290 data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100291 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200292 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100293
Radek Krejci52f65552020-09-01 17:03:35 +0200294 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200295 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200296 data = "<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 "<c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100299 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200300 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100301
Radek Krejci52f65552020-09-01 17:03:35 +0200302 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200303 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200304 data = "<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 "<c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100307 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200308 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100309
310 lyd_free_all(tree);
311
312 /* string value equal to the default but default is an unresolved instance-identifier, so they are not considered equal */
Michal Vaskob104f112020-07-17 09:54:54 +0200313 data = "<a xmlns=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
Radek Krejci7931b192020-06-25 17:05:03 +0200314 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100315
Radek Krejci52f65552020-09-01 17:03:35 +0200316 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200317 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100318 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200319 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100320
Radek Krejci52f65552020-09-01 17:03:35 +0200321 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100323 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200324 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100325
Radek Krejci52f65552020-09-01 17:03:35 +0200326 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100328 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200329 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100330
Radek Krejci52f65552020-09-01 17:03:35 +0200331 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200332 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100333 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200334 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100335
336 lyd_free_all(tree);
337
338 /* instance-identifier value equal to the default, should be considered equal */
Michal Vaskob104f112020-07-17 09:54:54 +0200339 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 Krejci7931b192020-06-25 17:05:03 +0200340 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100341
Radek Krejci52f65552020-09-01 17:03:35 +0200342 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200344 data = "<b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100345 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200346 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100347
Radek Krejci52f65552020-09-01 17:03:35 +0200348 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200349 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200350 data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100351 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200352 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100353
Radek Krejci52f65552020-09-01 17:03:35 +0200354 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200355 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200356 data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100357 " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
Michal Vaskob104f112020-07-17 09:54:54 +0200358 "<b xmlns=\"urn:defaults\">val</b>"
359 "<c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100360 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200361 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100362
Radek Krejci52f65552020-09-01 17:03:35 +0200363 assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK));
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 assert_int_equal(strlen(printed), ly_out_printed(out));
Michal Vaskob104f112020-07-17 09:54:54 +0200365 data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100366 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200367 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100368
369 lyd_free_all(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +0200370 ly_out_free(out, NULL, 1);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100371
372 s->func = NULL;
373}
374
Michal Vaskoa3881362020-01-21 15:57:35 +0100375#if 0
376
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200377static void
378test_rpc(void **state)
379{
380 struct state_s *s = (struct state_s*)(*state);
381 struct lyd_node *tree1;
382 struct lyd_node *tree2;
383 const struct lyd_node **trees;
384 const char *request;
385 const char *reply, *result;
386 char *printed;
387 ssize_t len;
Radek Krejci241f6b52020-05-21 18:13:49 +0200388 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200389
390 s->func = test_rpc;
Radek Krejci241f6b52020-05-21 18:13:49 +0200391 assert_non_null(out = ly_out_new_memory(&printed, 0));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200392
393 request = "<sum xmlns=\"urn:tests:types\"><x>10</x><y>20</y></sum>";
394 reply = "<result xmlns=\"urn:tests:types\">30</result>";
395 result = "<sum xmlns=\"urn:tests:types\"><result>30</result></sum>";
396 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200397 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200398 assert_int_equal(len, strlen(printed));
399 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200400 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200401 assert_non_null(trees = lyd_trees_new(1, tree1));
402 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200403 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200404 assert_int_equal(len, strlen(printed));
405 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200406 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200407 lyd_trees_free(trees, 0);
408 lyd_free_all(tree1);
409 lyd_free_all(tree2);
410
411 /* no arguments */
412 request = "<sum xmlns=\"urn:tests:types\"/>";
413 reply = "";
414 result = "<sum xmlns=\"urn:tests:types\"/>";
415 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200416 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200417 assert_int_equal(len, strlen(printed));
418 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200419 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200420 assert_non_null(trees = lyd_trees_new(1, tree1));
421 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200422 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200423 assert_int_equal(len, strlen(printed));
424 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200425 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200426 lyd_trees_free(trees, 0);
427 lyd_free_all(tree1);
428 lyd_free_all(tree2);
429
430 /* action
431 * "container cont {leaf leaftarget {type empty;}"
432 "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}"
433 "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}"
434 "leaf-list leaflisttarget {type uint8; max-elements 5;}}"
435 */
436 request = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><a>test</a></test></listtarget></cont>";
437 reply = "<b xmlns=\"urn:tests:types\">test-reply</b>";
438 result = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><b>test-reply</b></test></listtarget></cont>";;
439 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200440 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200441 assert_int_equal(len, strlen(printed));
442 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200443 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200444 assert_non_null(trees = lyd_trees_new(1, tree1));
445 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200446 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200447 assert_int_equal(len, strlen(printed));
448 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200449 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200450 lyd_trees_free(trees, 0);
451 lyd_free_all(tree1);
452 lyd_free_all(tree2);
453
Radek Krejci241f6b52020-05-21 18:13:49 +0200454 ly_out_free(out, NULL, 1);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200455 s->func = NULL;
456}
457
Michal Vaskoa3881362020-01-21 15:57:35 +0100458#endif
459
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200460int main(void)
461{
462 const struct CMUnitTest tests[] = {
463 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
464 cmocka_unit_test_setup_teardown(test_anydata, setup, teardown),
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100465 cmocka_unit_test_setup_teardown(test_defaults, setup, teardown),
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200466 };
467
468 return cmocka_run_group_tests(tests, NULL, NULL);
469}