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