blob: b1df7abbbdd4839521fb6f06746715171ffd09c9 [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"
25#include "printer.h"
26#include "printer_data.h"
Michal Vasko6f4cbb62020-02-28 11:15:47 +010027#include "tests/config.h"
Radek Krejci70593c12020-06-13 20:48:09 +020028#include "tree_schema.h"
Radek Krejci26a5dfb2019-07-26 14:51:06 +020029
30#define BUFSIZE 1024
31char logbuf[BUFSIZE] = {0};
32int store = -1; /* negative for infinite logging, positive for limited logging */
33
34struct 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
43static void
44logger(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
60static int
61setup(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 Vasko6f4cbb62020-02-28 11:15:47 +0100106 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 Krejci26a5dfb2019-07-26 14:51:06 +0200124
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 Vasko6f4cbb62020-02-28 11:15:47 +0100132 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 Krejci26a5dfb2019-07-26 14:51:06 +0200134 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 Vasko6f4cbb62020-02-28 11:15:47 +0100136 assert_non_null(lys_parse_mem(s->ctx, schema_c, LYS_IN_YANG));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200137
138 *state = s;
139
140 return 0;
141}
142
143static int
144teardown(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
160void
161logbuf_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
172static void
173test_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 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>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100188 assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY));
Radek Krejcia5bba312020-01-09 15:41:20 +0100189 assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200190 assert_int_equal(len, strlen(printed));
191 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;
205 ssize_t len;
Radek Krejci241f6b52020-05-21 18:13:49 +0200206 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200207
208 s->func = test_anydata;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200209 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200210
211 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100212 assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY));
Radek Krejcia5bba312020-01-09 15:41:20 +0100213 assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200214 assert_int_equal(len, strlen(printed));
Michal Vasko52927e22020-03-16 17:26:14 +0100215 /* canonized */
216 data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>";
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200217 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200218 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200219 lyd_free_all(tree);
220
221 data = "<any xmlns=\"urn:tests:types\"/>";
Michal Vasko9b368d32020-02-14 13:53:31 +0100222 assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY));
Radek Krejcia5bba312020-01-09 15:41:20 +0100223 assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200224 assert_int_equal(len, strlen(printed));
225 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200226 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200227 lyd_free_all(tree);
228
Michal Vasko52927e22020-03-16 17:26:14 +0100229 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 Krejcia5bba312020-01-09 15:41:20 +0100245 assert_true((len = lyd_print(out, tree, LYD_XML, 0)) >= 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100246 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 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;
273 ssize_t len;
Radek Krejci241f6b52020-05-21 18:13:49 +0200274 struct ly_out *out;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100275
276 s->func = test_defaults;
277
Radek Krejci84ce7b12020-06-11 17:28:25 +0200278 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100279
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100280 /* 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 Krejcia5bba312020-01-09 15:41:20 +0100284 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100285 assert_int_equal(len, strlen(printed));
286 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200287 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100288
Radek Krejcia5bba312020-01-09 15:41:20 +0100289 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100290 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 Krejci241f6b52020-05-21 18:13:49 +0200293 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100294
Radek Krejcia5bba312020-01-09 15:41:20 +0100295 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100296 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 Krejci241f6b52020-05-21 18:13:49 +0200301 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100302
Radek Krejcia5bba312020-01-09 15:41:20 +0100303 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100304 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 Krejci241f6b52020-05-21 18:13:49 +0200309 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100310
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 Krejcia5bba312020-01-09 15:41:20 +0100317 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100318 assert_int_equal(len, strlen(printed));
319 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200320 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100321
Radek Krejcia5bba312020-01-09 15:41:20 +0100322 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100323 assert_int_equal(len, strlen(printed));
324 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200325 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100326
Radek Krejcia5bba312020-01-09 15:41:20 +0100327 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100328 assert_int_equal(len, strlen(printed));
329 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200330 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100331
Radek Krejcia5bba312020-01-09 15:41:20 +0100332 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100333 assert_int_equal(len, strlen(printed));
334 assert_string_equal(printed, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200335 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100336
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 Krejcia5bba312020-01-09 15:41:20 +0100343 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_TRIM)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100344 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 Krejci241f6b52020-05-21 18:13:49 +0200347 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100348
Radek Krejcia5bba312020-01-09 15:41:20 +0100349 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100350 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 Krejci241f6b52020-05-21 18:13:49 +0200353 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100354
Radek Krejcia5bba312020-01-09 15:41:20 +0100355 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_ALL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100356 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 Krejci241f6b52020-05-21 18:13:49 +0200362 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100363
Radek Krejcia5bba312020-01-09 15:41:20 +0100364 assert_true((len = lyd_print(out, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG)) >= 0);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100365 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 Krejci241f6b52020-05-21 18:13:49 +0200368 ly_out_reset(out);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100369
370 lyd_free_all(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +0200371 ly_out_free(out, NULL, 1);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100372
373 s->func = NULL;
374}
375
Michal Vaskoa3881362020-01-21 15:57:35 +0100376#if 0
377
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200378static void
379test_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 Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200390
391 s->func = test_rpc;
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 assert_non_null(out = ly_out_new_memory(&printed, 0));
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200393
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 Krejcia5bba312020-01-09 15:41:20 +0100398 assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200399 assert_int_equal(len, strlen(printed));
400 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200401 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200402 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 Krejcia5bba312020-01-09 15:41:20 +0100404 assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200405 assert_int_equal(len, strlen(printed));
406 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200407 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200408 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 Krejcia5bba312020-01-09 15:41:20 +0100417 assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200418 assert_int_equal(len, strlen(printed));
419 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200420 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200421 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 Krejcia5bba312020-01-09 15:41:20 +0100423 assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200424 assert_int_equal(len, strlen(printed));
425 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200426 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200427 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 Krejcia5bba312020-01-09 15:41:20 +0100441 assert_true((len = lyd_print(out, tree1, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200442 assert_int_equal(len, strlen(printed));
443 assert_string_equal(printed, request);
Radek Krejci241f6b52020-05-21 18:13:49 +0200444 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200445 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 Krejcia5bba312020-01-09 15:41:20 +0100447 assert_true((len = lyd_print(out, tree2, LYD_XML, 0)) >= 0);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200448 assert_int_equal(len, strlen(printed));
449 assert_string_equal(printed, result);
Radek Krejci241f6b52020-05-21 18:13:49 +0200450 ly_out_reset(out);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200451 lyd_trees_free(trees, 0);
452 lyd_free_all(tree1);
453 lyd_free_all(tree2);
454
Radek Krejci241f6b52020-05-21 18:13:49 +0200455 ly_out_free(out, NULL, 1);
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200456 s->func = NULL;
457}
458
Michal Vaskoa3881362020-01-21 15:57:35 +0100459#endif
460
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200461int 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 Vasko6f4cbb62020-02-28 11:15:47 +0100466 cmocka_unit_test_setup_teardown(test_defaults, setup, teardown),
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200467 };
468
469 return cmocka_run_group_tests(tests, NULL, NULL);
470}