blob: 6213a37da547e1d7c511dca69438c75f6a83d5c2 [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 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
Radek Krejci26a5dfb2019-07-26 14:51:06 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020016
Radek Krejci26a5dfb2019-07-26 14:51:06 +020017#include <string.h>
18
Radek Krejci70593c12020-06-13 20:48:09 +020019#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020023#include "tests_config.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "tree_schema.h"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema_defs = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020030 "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +020031 const char *schema_types = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020032 "feature f; identity gigabit-ethernet { base defs:ethernet;}"
33 "container cont {leaf leaftarget {type empty;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010034 " list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}"
35 " action test {input {leaf a {type string;}} output {leaf b {type string;}}}}"
36 " leaf-list leaflisttarget {type uint8; max-elements 5;}}"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020037 "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}"
38 "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}"
39 "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}"
40 "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}"
41 "leaf-list leaflisttarget {type string;}"
42 "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}"
43 "leaf binary-norestr {type binary;}"
44 "leaf int8 {type int8 {range 10..20;}}"
45 "leaf uint8 {type uint8 {range 150..200;}}"
46 "leaf int16 {type int16 {range -20..-10;}}"
47 "leaf uint16 {type uint16 {range 150..200;}}"
48 "leaf int32 {type int32;}"
49 "leaf uint32 {type uint32;}"
50 "leaf int64 {type int64;}"
51 "leaf uint64 {type uint64;}"
52 "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}"
53 "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}"
54 "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}"
55 "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}"
56 "leaf str {type string {length 8..10; pattern '[a-z ]*';}}"
57 "leaf str-norestr {type string;}"
58 "leaf bool {type boolean;}"
59 "leaf empty {type empty;}"
60 "leaf ident {type identityref {base defs:interface-type;}}"
61 "leaf inst {type instance-identifier {require-instance true;}}"
62 "leaf inst-noreq {type instance-identifier {require-instance false;}}"
63 "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}"
64 "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}"
65 "leaf un1 {type union {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010066 " type leafref {path /int8; require-instance true;}"
67 " type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }"
68 " type string {length 1..20;}}}"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020069 "anydata any;"
70 "rpc sum {input {leaf x {type uint8;} leaf y {type uint8;}} output {leaf result {type uint16;}}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +020071 const char *schema_defaults =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010072 "module defaults {\n"
73 " namespace \"urn:defaults\";\n"
74 " prefix d;\n"
75 " leaf a {\n"
76 " type union {\n"
77 " type instance-identifier;\n"
78 " type string;\n"
79 " }\n"
80 " default \"/d:b\";\n"
81 " }\n"
82 " leaf b {\n"
83 " type string;\n"
84 " }\n"
85 " leaf c {\n"
86 " type string;\n"
87 " }\n"
88 "}";
Radek Krejci26a5dfb2019-07-26 14:51:06 +020089
Radek Iša56ca9e42020-09-08 18:42:00 +020090 UTEST_SETUP;
Radek Krejci26a5dfb2019-07-26 14:51:06 +020091
Radek Iša56ca9e42020-09-08 18:42:00 +020092 UTEST_ADD_MODULE(schema_defs, LYS_IN_YANG, NULL, NULL);
93 UTEST_ADD_MODULE(schema_types, LYS_IN_YANG, NULL, NULL);
94 UTEST_ADD_MODULE(schema_defaults, LYS_IN_YANG, NULL, NULL);
Radek Krejci26a5dfb2019-07-26 14:51:06 +020095
96 return 0;
97}
98
Radek Iša56ca9e42020-09-08 18:42:00 +020099#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
100 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200101
Radek Iša56ca9e42020-09-08 18:42:00 +0200102#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
103 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200104
105static void
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200106test_anydata(void **state)
107{
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200108 struct lyd_node *tree;
109 const char *data;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200110
111 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko52927e22020-03-16 17:26:14 +0100113 /* canonized */
114 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200116 lyd_free_all(tree);
117
118 data = "<any xmlns=\"urn:tests:types\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
120 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200121 lyd_free_all(tree);
122
Radek Iša56ca9e42020-09-08 18:42:00 +0200123 data = "<any xmlns=\"urn:tests:types\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100124 " <cont>\n"
125 " <defs:elem1 xmlns:defs=\"urn:tests:defs\">\n"
126 " <elem2 xmlns:defaults=\"urn:defaults\" defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\">\n"
127 " </elem2>\n"
128 " </defs:elem1>\n"
129 " </cont>\n"
130 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200131 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
132 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100133 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200134 /* cont should be normally parsed */
135 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "any", 0, LYS_ANYDATA, 0, 0, NULL, 0);
136 CHECK_LYD_NODE_ANY((struct lyd_node_any *)tree, 0, 0, 0, LYD_ANYDATA_DATATREE);
137 struct lyd_node *tree_tmp = ((struct lyd_node_any *)tree)->value.tree;
138
139 CHECK_LYSC_NODE(tree_tmp->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "cont", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100140 /* but its children not */
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 assert_null(((struct lyd_node_inner *)tree_tmp)->child->schema);
Michal Vasko52927e22020-03-16 17:26:14 +0100142 /* canonized */
143 data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100144 "<any xmlns=\"urn:tests:types\">\n"
145 " <cont>\n"
146 " <elem1 xmlns=\"urn:tests:defs\">\n"
147 " <elem2 xmlns=\"urn:tests:types\" xmlns:defs=\"urn:tests:defs\" xmlns:defaults=\"urn:defaults\" "
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200148 "defs:attr1=\"defaults:val\" attr2=\"/defaults:node/defs:node2\">\n"
149 " </elem2>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100150 " </elem1>\n"
151 " </cont>\n"
152 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200153 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko52927e22020-03-16 17:26:14 +0100154 lyd_free_all(tree);
Radek Iša6a7d6cd2021-03-04 14:11:24 +0100155
156 data = "<any xmlns=\"urn:tests:types\">\n"
Radek Išae8f15c12021-03-08 09:38:38 +0100157 " <ahoj attr=\"&lt;test\">\n"
Radek Iša6a7d6cd2021-03-04 14:11:24 +0100158 " ahoj jak se vede &lt; how are you"
Radek Išae8f15c12021-03-08 09:38:38 +0100159 " </ahoj>\n"
Radek Iša6a7d6cd2021-03-04 14:11:24 +0100160 "</any>\n";
161 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
162 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
163 lyd_free_all(tree);
164
Radek Išae8f15c12021-03-08 09:38:38 +0100165 data = "<any xmlns=\"urn:tests:types\">\n"
166 " <leaflisttarget> ahoj </leaflisttarget>\n"
167 " <leaflisttarget> nazdar </leaflisttarget>\n"
168 " <leaflisttarget> Čau </leaflisttarget>\n"
169 "</any>\n";
170 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
171 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
172 lyd_free_all(tree);
173
174 data = "<any xmlns=\"urn:tests:types\">\n"
175 " <cont2/>\n"
176 "</any>\n";
177 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
178 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
179 lyd_free_all(tree);
180
181 data = "<any xmlns=\"urn:tests:types\">\n"
182 " <cont>\n"
183 " &lt; how are you"
184 " </cont>\n"
185 "</any>\n";
186 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
187 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
188 lyd_free_all(tree);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200189}
190
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100191static void
192test_defaults(void **state)
193{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100194 struct lyd_node *tree;
195 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +0200196 const char *data_trim;
197 const char *data_all;
198 const char *data_all_tag;
199 const char *data_impl_tag;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100200
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
202 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcia5bba312020-01-09 15:41:20 +0100203
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100204 /* standard default value */
205 data = "<c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
207 CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100208
Michal Vaskob104f112020-07-17 09:54:54 +0200209 data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100211
Michal Vaskob104f112020-07-17 09:54:54 +0200212 data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100213 " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
214 "<c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200215 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100216
Michal Vaskob104f112020-07-17 09:54:54 +0200217 data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100218 " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
219 "<c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100221 lyd_free_all(tree);
222
223 /* 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 +0200224 data = "<a xmlns=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200225 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
226 CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
227 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
228 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
229 CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100230 lyd_free_all(tree);
231
232 /* instance-identifier value equal to the default, should be considered equal */
Michal Vaskob104f112020-07-17 09:54:54 +0200233 data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 data_trim = "<b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
235 data_all = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
236 data_all_tag = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100237 " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
238 "<b xmlns=\"urn:defaults\">val</b>"
239 "<c xmlns=\"urn:defaults\">aa</c>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 data_impl_tag = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100241
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
243 CHECK_LYD_STRING(tree, LYD_PRINT_WD_TRIM | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_trim);
244 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_all);
245 CHECK_LYD_STRING(tree, LYD_PRINT_WD_ALL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_all_tag);
246 CHECK_LYD_STRING(tree, LYD_PRINT_WD_IMPL_TAG | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data_impl_tag);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100247 lyd_free_all(tree);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100248}
249
Michal Vaskoa3881362020-01-21 15:57:35 +0100250#if 0
251
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200252static void
253test_rpc(void **state)
254{
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100255 struct state_s *s = (struct state_s *)(*state);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200256 struct lyd_node *tree1;
257 struct lyd_node *tree2;
258 const struct lyd_node **trees;
259 const char *request;
260 const char *reply, *result;
261 char *printed;
262 ssize_t len;
Radek Krejci241f6b52020-05-21 18:13:49 +0200263 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200264
265 s->func = test_rpc;
Radek Krejci241f6b52020-05-21 18:13:49 +0200266 assert_non_null(out = ly_out_new_memory(&printed, 0));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200267
268 request = "<sum xmlns=\"urn:tests:types\"><x>10</x><y>20</y></sum>";
269 reply = "<result xmlns=\"urn:tests:types\">30</result>";
270 result = "<sum xmlns=\"urn:tests:types\"><result>30</result></sum>";
271 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200272 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200273 assert_int_equal(len, strlen(printed));
274 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200275 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200276 assert_non_null(trees = lyd_trees_new(1, tree1));
277 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200278 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200279 assert_int_equal(len, strlen(printed));
280 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200281 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200282 lyd_trees_free(trees, 0);
283 lyd_free_all(tree1);
284 lyd_free_all(tree2);
285
286 /* no arguments */
287 request = "<sum xmlns=\"urn:tests:types\"/>";
288 reply = "";
289 result = "<sum xmlns=\"urn:tests:types\"/>";
290 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200291 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200292 assert_int_equal(len, strlen(printed));
293 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200294 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200295 assert_non_null(trees = lyd_trees_new(1, tree1));
296 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200297 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200298 assert_int_equal(len, strlen(printed));
299 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200300 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200301 lyd_trees_free(trees, 0);
302 lyd_free_all(tree1);
303 lyd_free_all(tree2);
304
305 /* action
306 * "container cont {leaf leaftarget {type empty;}"
307 "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}"
308 "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}"
309 "leaf-list leaflisttarget {type uint8; max-elements 5;}}"
310 */
311 request = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><a>test</a></test></listtarget></cont>";
312 reply = "<b xmlns=\"urn:tests:types\">test-reply</b>";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100313 result = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><b>test-reply</b></test></listtarget></cont>";
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200314 assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL));
Radek Krejci52f65552020-09-01 17:03:35 +0200315 assert_true((len = lyd_print_tree(out, tree1, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200316 assert_int_equal(len, strlen(printed));
317 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200318 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200319 assert_non_null(trees = lyd_trees_new(1, tree1));
320 assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees));
Radek Krejci52f65552020-09-01 17:03:35 +0200321 assert_true((len = lyd_print_tree(out, tree2, LYD_XML, LYD_PRINT_SHRINK)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200322 assert_int_equal(len, strlen(printed));
323 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200324 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200325 lyd_trees_free(trees, 0);
326 lyd_free_all(tree1);
327 lyd_free_all(tree2);
328
Radek Krejci241f6b52020-05-21 18:13:49 +0200329 ly_out_free(out, NULL, 1);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200330 s->func = NULL;
331}
332
Michal Vaskoa3881362020-01-21 15:57:35 +0100333#endif
334
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100335int
336main(void)
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200337{
338 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 UTEST(test_anydata, setup),
340 UTEST(test_defaults, setup),
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200341 };
342
343 return cmocka_run_group_tests(tests, NULL, NULL);
344}