blob: 95c28d0b6bd600b6cf48f8c99656b54384c8fc97 [file] [log] [blame]
Radek Krejci509e2592019-05-15 16:30:48 +02001/*
2 * @file test_parser_xml.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_xml.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
Michal Vasko52927e22020-03-16 17:26:14 +010015#include "tests/config.h"
16
Radek Krejci509e2592019-05-15 16:30:48 +020017#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
Radek Krejci2d7a47b2019-05-16 13:34:10 +020025#include "../../src/context.h"
26#include "../../src/tree_data_internal.h"
Michal Vasko52927e22020-03-16 17:26:14 +010027#include "../../src/printer_data.h"
Radek Krejci509e2592019-05-15 16:30:48 +020028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31int store = -1; /* negative for infinite logging, positive for limited logging */
32
33struct ly_ctx *ctx; /* context for tests */
34
35/* set to 0 to printing error messages to stderr instead of checking them in code */
36#define ENABLE_LOGGER_CHECKING 1
37
38#if ENABLE_LOGGER_CHECKING
39static void
40logger(LY_LOG_LEVEL level, const char *msg, const char *path)
41{
42 (void) level; /* unused */
43 if (store) {
44 if (path && path[0]) {
45 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
46 } else {
47 strncpy(logbuf, msg, BUFSIZE - 1);
48 }
49 if (store > 0) {
50 --store;
51 }
52 }
53}
54#endif
55
56static int
57setup(void **state)
58{
59 (void) state; /* unused */
60
61 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
Michal Vasko44685da2020-03-17 15:38:06 +010062 "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;} leaf d {type string;}}"
Radek Krejciee4cab22019-07-17 17:07:47 +020063 "leaf foo { type string;}"
Radek Krejcib6f7ae52019-07-19 10:31:42 +020064 "container c { leaf x {type string;}}"
65 "container cp {presence \"container switch\"; leaf y {type string;}}"
Michal Vasko8d544252020-03-02 10:19:52 +010066 "anydata any {config false;}"
Michal Vasko44685da2020-03-17 15:38:06 +010067 "leaf foo2 { type string; default \"default-val\"; }"
68 "leaf foo3 { type uint32; }}";
Radek Krejci509e2592019-05-15 16:30:48 +020069
70#if ENABLE_LOGGER_CHECKING
71 ly_set_log_clb(logger, 1);
72#endif
73
Michal Vasko8d544252020-03-02 10:19:52 +010074 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
75 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Radek Krejci509e2592019-05-15 16:30:48 +020076 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
77
78 return 0;
79}
80
81static int
82teardown(void **state)
83{
84#if ENABLE_LOGGER_CHECKING
85 if (*state) {
86 fprintf(stderr, "%s\n", logbuf);
87 }
88#else
89 (void) state; /* unused */
90#endif
91
92 ly_ctx_destroy(ctx, NULL);
93 ctx = NULL;
94
95 return 0;
96}
97
98void
99logbuf_clean(void)
100{
101 logbuf[0] = '\0';
102}
103
104#if ENABLE_LOGGER_CHECKING
105# define logbuf_assert(str) assert_string_equal(logbuf, str)
106#else
107# define logbuf_assert(str)
108#endif
109
Radek Krejci509e2592019-05-15 16:30:48 +0200110static void
111test_leaf(void **state)
112{
113 *state = test_leaf;
114
115 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
116 struct lyd_node *tree;
117 struct lyd_node_term *leaf;
118
Michal Vasko9f96a052020-03-10 09:41:45 +0100119 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci509e2592019-05-15 16:30:48 +0200120 assert_non_null(tree);
121 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
122 assert_string_equal("foo", tree->schema->name);
123 leaf = (struct lyd_node_term*)tree;
Radek Krejci950f6a52019-09-12 17:15:32 +0200124 assert_string_equal("foo value", leaf->value.original);
Radek Krejci509e2592019-05-15 16:30:48 +0200125
Michal Vasko8d544252020-03-02 10:19:52 +0100126 assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype);
127 assert_string_equal("foo2", tree->next->next->schema->name);
128 leaf = (struct lyd_node_term*)tree->next->next;
129 assert_string_equal("default-val", leaf->value.original);
130 assert_true(leaf->flags & LYD_DEFAULT);
131
Radek Krejci509e2592019-05-15 16:30:48 +0200132 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100133
134 /* make foo2 explicit */
135 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100136 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100137 assert_non_null(tree);
138 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
139 assert_string_equal("foo2", tree->schema->name);
140 leaf = (struct lyd_node_term*)tree;
141 assert_string_equal("default-val", leaf->value.original);
142 assert_false(leaf->flags & LYD_DEFAULT);
143
144 lyd_free_all(tree);
145
146 /* parse foo2 but make it implicit */
147 data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" wd:default=\"true\">default-val</foo2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100148 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100149 assert_non_null(tree);
150 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
151 assert_string_equal("foo2", tree->schema->name);
152 leaf = (struct lyd_node_term*)tree;
153 assert_string_equal("default-val", leaf->value.original);
154 assert_true(leaf->flags & LYD_DEFAULT);
155
156 lyd_free_all(tree);
157
Radek Krejci509e2592019-05-15 16:30:48 +0200158 *state = NULL;
159}
160
Radek Krejciee4cab22019-07-17 17:07:47 +0200161static void
162test_anydata(void **state)
163{
164 *state = test_anydata;
165
Michal Vasko52927e22020-03-16 17:26:14 +0100166 const char *data;
167 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200168 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200169
Michal Vasko52927e22020-03-16 17:26:14 +0100170 data =
171 "<any xmlns=\"urn:tests:a\">"
172 "<element1>"
173 "<x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>"
174 "</element1>"
175 "<element1a/>"
176 "</any>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100177 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejciee4cab22019-07-17 17:07:47 +0200178 assert_non_null(tree);
179 assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
180 assert_string_equal("any", tree->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100181
182 lyd_print_mem(&str, tree, LYD_XML, 0);
183 assert_string_equal(str,
184 "<any xmlns=\"urn:tests:a\">"
185 "<element1>"
186 "<element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>"
187 "</element1>"
188 "<element1a/>"
189 "</any>"
190 );
191 free(str);
Radek Krejciee4cab22019-07-17 17:07:47 +0200192
193 lyd_free_all(tree);
194 *state = NULL;
195}
196
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200197static void
198test_list(void **state)
199{
200 *state = test_list;
201
Michal Vasko44685da2020-03-17 15:38:06 +0100202 const char *data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200203 struct lyd_node *tree, *iter;
204 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200205 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200206
Radek Krejci710226d2019-07-24 17:24:59 +0200207 /* check hashes */
Michal Vasko9f96a052020-03-10 09:41:45 +0100208 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200209 assert_non_null(tree);
210 assert_int_equal(LYS_LIST, tree->schema->nodetype);
211 assert_string_equal("l1", tree->schema->name);
212 list = (struct lyd_node_inner*)tree;
213 LY_LIST_FOR(list->child, iter) {
214 assert_int_not_equal(0, iter->hash);
215 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200216 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200217
Michal Vasko9f96a052020-03-10 09:41:45 +0100218 /* missing keys */
Michal Vasko44685da2020-03-17 15:38:06 +0100219 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100220 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100221 logbuf_assert("List instance is missing its key \"a\". /a:l1[b='b'][c='1']");
Michal Vasko9f96a052020-03-10 09:41:45 +0100222
223 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>";
224 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
225 logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']");
226
227 data = "<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>";
228 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
229 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']");
230
231 /* key duplicate */
Michal Vasko44685da2020-03-17 15:38:06 +0100232 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a><c>1</c></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100233 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100234 logbuf_assert("Duplicate instance of \"c\". /a:l1[a='a'][b='b'][c='1'][c='1']/c");
Michal Vasko9f96a052020-03-10 09:41:45 +0100235
Radek Krejci710226d2019-07-24 17:24:59 +0200236 /* keys order */
Michal Vasko44685da2020-03-17 15:38:06 +0100237 data = "<l1 xmlns=\"urn:tests:a\"><d>d</d><a>a</a><c>1</c><b>b</b></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100238 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200239 assert_non_null(tree);
240 assert_int_equal(LYS_LIST, tree->schema->nodetype);
241 assert_string_equal("l1", tree->schema->name);
242 list = (struct lyd_node_inner*)tree;
243 assert_non_null(leaf = (struct lyd_node_term*)list->child);
244 assert_string_equal("a", leaf->schema->name);
245 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
246 assert_string_equal("b", leaf->schema->name);
247 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
248 assert_string_equal("c", leaf->schema->name);
249 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
250 assert_string_equal("d", leaf->schema->name);
251 logbuf_assert("Invalid position of the key \"b\" in a list.");
252 lyd_free_all(tree);
253
Michal Vasko44685da2020-03-17 15:38:06 +0100254 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100255 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200256 assert_non_null(tree);
257 assert_int_equal(LYS_LIST, tree->schema->nodetype);
258 assert_string_equal("l1", tree->schema->name);
259 list = (struct lyd_node_inner*)tree;
260 assert_non_null(leaf = (struct lyd_node_term*)list->child);
261 assert_string_equal("a", leaf->schema->name);
262 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
263 assert_string_equal("b", leaf->schema->name);
264 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
265 assert_string_equal("c", leaf->schema->name);
266 logbuf_assert("Invalid position of the key \"a\" in a list.");
267 logbuf_clean();
268 lyd_free_all(tree);
269
Michal Vasko9f96a052020-03-10 09:41:45 +0100270 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_STRICT, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200271 logbuf_assert("Invalid position of the key \"b\" in a list. Line number 1.");
272
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200273 *state = NULL;
274}
275
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200276static void
277test_container(void **state)
278{
279 *state = test_container;
280
281 const char *data = "<c xmlns=\"urn:tests:a\"/>";
282 struct lyd_node *tree;
283 struct lyd_node_inner *cont;
284
Michal Vasko9f96a052020-03-10 09:41:45 +0100285 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200286 assert_non_null(tree);
287 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
288 assert_string_equal("c", tree->schema->name);
289 cont = (struct lyd_node_inner*)tree;
290 assert_true(cont->flags & LYD_DEFAULT);
291 lyd_free_all(tree);
292
293 data = "<cp xmlns=\"urn:tests:a\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100294 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200295 assert_non_null(tree);
296 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
297 assert_string_equal("cp", tree->schema->name);
298 cont = (struct lyd_node_inner*)tree;
299 assert_false(cont->flags & LYD_DEFAULT);
300 lyd_free_all(tree);
301
302 *state = NULL;
303}
304
Michal Vasko44685da2020-03-17 15:38:06 +0100305static void
306test_opaq(void **state)
307{
308 *state = test_opaq;
309
310 const char *data;
311 char *str;
312 struct lyd_node *tree;
313
314 /* invalid value, no flags */
315 data = "<foo3 xmlns=\"urn:tests:a\"/>";
316 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
317 logbuf_assert("Invalid empty uint32 value. /");
318 assert_null(tree);
319
320 /* opaq flag */
321 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
322 assert_non_null(tree);
323 assert_null(tree->schema);
324 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3");
325 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
326
327 lyd_print_mem(&str, tree, LYD_XML, 0);
328 assert_string_equal(str, "<foo3 xmlns=\"urn:tests:a\"/>");
329 free(str);
330 lyd_free_all(tree);
331
332 /* missing key, no flags */
333 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><d>val_d</d></l1>";
334 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
335 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']");
336 assert_null(tree);
337
338 /* opaq flag */
339 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
340 assert_non_null(tree);
341 assert_null(tree->schema);
342 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
343 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
344
345 lyd_print_mem(&str, tree, LYD_XML, 0);
346 assert_string_equal(str, data);
347 free(str);
348 lyd_free_all(tree);
349
350 /* invalid key, no flags */
351 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><c>val_c</c></l1>";
352 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
353 logbuf_assert("Invalid int16 value \"val_c\". /");
354 assert_null(tree);
355
356 /* opaq flag */
357 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
358 assert_non_null(tree);
359 assert_null(tree->schema);
360 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
361 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
362
363 lyd_print_mem(&str, tree, LYD_XML, 0);
364 assert_string_equal(str, data);
365 free(str);
366 lyd_free_all(tree);
367
368 *state = NULL;
369}
370
Radek Krejci509e2592019-05-15 16:30:48 +0200371int main(void)
372{
373 const struct CMUnitTest tests[] = {
374 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
Radek Krejciee4cab22019-07-17 17:07:47 +0200375 cmocka_unit_test_setup_teardown(test_anydata, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200376 cmocka_unit_test_setup_teardown(test_list, setup, teardown),
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200377 cmocka_unit_test_setup_teardown(test_container, setup, teardown),
Michal Vasko44685da2020-03-17 15:38:06 +0100378 cmocka_unit_test_setup_teardown(test_opaq, setup, teardown),
Radek Krejci509e2592019-05-15 16:30:48 +0200379 };
380
381 return cmocka_run_group_tests(tests, NULL, NULL);
382}