blob: e0357d7afeb50ed851806509060dcbfdc9136a9d [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;}"
Michal Vaskoa8edff02020-03-27 14:47:01 +010064 "container c {"
65 "leaf x {type string;}"
Michal Vasko1ce933a2020-03-30 12:38:22 +020066 "action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
Michal Vaskoa8edff02020-03-27 14:47:01 +010067 "notification n1 { leaf nl {type string;} }"
68 "}"
Michal Vaskob36053d2020-03-26 15:49:30 +010069 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
Michal Vasko8d544252020-03-02 10:19:52 +010070 "anydata any {config false;}"
Michal Vasko44685da2020-03-17 15:38:06 +010071 "leaf foo2 { type string; default \"default-val\"; }"
Michal Vaskoa8edff02020-03-27 14:47:01 +010072 "leaf foo3 { type uint32; }"
73 "notification n2;}";
Michal Vaskob36053d2020-03-26 15:49:30 +010074 const struct lys_module *mod;
Radek Krejci509e2592019-05-15 16:30:48 +020075
76#if ENABLE_LOGGER_CHECKING
77 ly_set_log_clb(logger, 1);
78#endif
79
Michal Vasko8d544252020-03-02 10:19:52 +010080 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
81 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskob36053d2020-03-26 15:49:30 +010082 assert_non_null((mod = ly_ctx_load_module(ctx, "ietf-netconf", "2011-06-01")));
83 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "writable-running"));
Radek Krejci509e2592019-05-15 16:30:48 +020084 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
85
86 return 0;
87}
88
89static int
90teardown(void **state)
91{
92#if ENABLE_LOGGER_CHECKING
93 if (*state) {
94 fprintf(stderr, "%s\n", logbuf);
95 }
96#else
97 (void) state; /* unused */
98#endif
99
100 ly_ctx_destroy(ctx, NULL);
101 ctx = NULL;
102
103 return 0;
104}
105
106void
107logbuf_clean(void)
108{
109 logbuf[0] = '\0';
110}
111
112#if ENABLE_LOGGER_CHECKING
113# define logbuf_assert(str) assert_string_equal(logbuf, str)
114#else
115# define logbuf_assert(str)
116#endif
117
Radek Krejci509e2592019-05-15 16:30:48 +0200118static void
119test_leaf(void **state)
120{
121 *state = test_leaf;
122
123 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
124 struct lyd_node *tree;
125 struct lyd_node_term *leaf;
126
Michal Vasko9f96a052020-03-10 09:41:45 +0100127 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci509e2592019-05-15 16:30:48 +0200128 assert_non_null(tree);
129 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
130 assert_string_equal("foo", tree->schema->name);
131 leaf = (struct lyd_node_term*)tree;
Radek Krejci950f6a52019-09-12 17:15:32 +0200132 assert_string_equal("foo value", leaf->value.original);
Radek Krejci509e2592019-05-15 16:30:48 +0200133
Michal Vasko8d544252020-03-02 10:19:52 +0100134 assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype);
135 assert_string_equal("foo2", tree->next->next->schema->name);
136 leaf = (struct lyd_node_term*)tree->next->next;
137 assert_string_equal("default-val", leaf->value.original);
138 assert_true(leaf->flags & LYD_DEFAULT);
139
Radek Krejci509e2592019-05-15 16:30:48 +0200140 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100141
142 /* make foo2 explicit */
143 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100144 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100145 assert_non_null(tree);
146 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
147 assert_string_equal("foo2", tree->schema->name);
148 leaf = (struct lyd_node_term*)tree;
149 assert_string_equal("default-val", leaf->value.original);
150 assert_false(leaf->flags & LYD_DEFAULT);
151
152 lyd_free_all(tree);
153
154 /* parse foo2 but make it implicit */
155 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 +0100156 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100157 assert_non_null(tree);
158 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
159 assert_string_equal("foo2", tree->schema->name);
160 leaf = (struct lyd_node_term*)tree;
161 assert_string_equal("default-val", leaf->value.original);
162 assert_true(leaf->flags & LYD_DEFAULT);
163
164 lyd_free_all(tree);
165
Radek Krejci509e2592019-05-15 16:30:48 +0200166 *state = NULL;
167}
168
Radek Krejciee4cab22019-07-17 17:07:47 +0200169static void
170test_anydata(void **state)
171{
172 *state = test_anydata;
173
Michal Vasko52927e22020-03-16 17:26:14 +0100174 const char *data;
175 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200176 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200177
Michal Vasko52927e22020-03-16 17:26:14 +0100178 data =
179 "<any xmlns=\"urn:tests:a\">"
180 "<element1>"
181 "<x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>"
182 "</element1>"
183 "<element1a/>"
184 "</any>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100185 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejciee4cab22019-07-17 17:07:47 +0200186 assert_non_null(tree);
187 assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
188 assert_string_equal("any", tree->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100189
190 lyd_print_mem(&str, tree, LYD_XML, 0);
191 assert_string_equal(str,
192 "<any xmlns=\"urn:tests:a\">"
193 "<element1>"
194 "<element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>"
195 "</element1>"
196 "<element1a/>"
197 "</any>"
198 );
199 free(str);
Radek Krejciee4cab22019-07-17 17:07:47 +0200200
201 lyd_free_all(tree);
202 *state = NULL;
203}
204
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200205static void
206test_list(void **state)
207{
208 *state = test_list;
209
Michal Vasko44685da2020-03-17 15:38:06 +0100210 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 +0200211 struct lyd_node *tree, *iter;
212 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200213 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200214
Radek Krejci710226d2019-07-24 17:24:59 +0200215 /* check hashes */
Michal Vasko9f96a052020-03-10 09:41:45 +0100216 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200217 assert_non_null(tree);
218 assert_int_equal(LYS_LIST, tree->schema->nodetype);
219 assert_string_equal("l1", tree->schema->name);
220 list = (struct lyd_node_inner*)tree;
221 LY_LIST_FOR(list->child, iter) {
222 assert_int_not_equal(0, iter->hash);
223 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200224 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200225
Michal Vasko9f96a052020-03-10 09:41:45 +0100226 /* missing keys */
Michal Vasko44685da2020-03-17 15:38:06 +0100227 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100228 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100229 logbuf_assert("List instance is missing its key \"a\". /a:l1[b='b'][c='1']");
Michal Vasko9f96a052020-03-10 09:41:45 +0100230
231 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>";
232 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
233 logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']");
234
235 data = "<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>";
236 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
237 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']");
238
239 /* key duplicate */
Michal Vasko44685da2020-03-17 15:38:06 +0100240 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 +0100241 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100242 logbuf_assert("Duplicate instance of \"c\". /a:l1[a='a'][b='b'][c='1'][c='1']/c");
Michal Vasko9f96a052020-03-10 09:41:45 +0100243
Radek Krejci710226d2019-07-24 17:24:59 +0200244 /* keys order */
Michal Vasko44685da2020-03-17 15:38:06 +0100245 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 +0100246 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200247 assert_non_null(tree);
248 assert_int_equal(LYS_LIST, tree->schema->nodetype);
249 assert_string_equal("l1", tree->schema->name);
250 list = (struct lyd_node_inner*)tree;
251 assert_non_null(leaf = (struct lyd_node_term*)list->child);
252 assert_string_equal("a", leaf->schema->name);
253 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
254 assert_string_equal("b", leaf->schema->name);
255 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
256 assert_string_equal("c", leaf->schema->name);
257 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
258 assert_string_equal("d", leaf->schema->name);
259 logbuf_assert("Invalid position of the key \"b\" in a list.");
260 lyd_free_all(tree);
261
Michal Vasko44685da2020-03-17 15:38:06 +0100262 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100263 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200264 assert_non_null(tree);
265 assert_int_equal(LYS_LIST, tree->schema->nodetype);
266 assert_string_equal("l1", tree->schema->name);
267 list = (struct lyd_node_inner*)tree;
268 assert_non_null(leaf = (struct lyd_node_term*)list->child);
269 assert_string_equal("a", leaf->schema->name);
270 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
271 assert_string_equal("b", leaf->schema->name);
272 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
273 assert_string_equal("c", leaf->schema->name);
274 logbuf_assert("Invalid position of the key \"a\" in a list.");
275 logbuf_clean();
276 lyd_free_all(tree);
277
Michal Vasko9f96a052020-03-10 09:41:45 +0100278 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_STRICT, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200279 logbuf_assert("Invalid position of the key \"b\" in a list. Line number 1.");
280
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200281 *state = NULL;
282}
283
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200284static void
285test_container(void **state)
286{
287 *state = test_container;
288
289 const char *data = "<c xmlns=\"urn:tests:a\"/>";
290 struct lyd_node *tree;
291 struct lyd_node_inner *cont;
292
Michal Vasko9f96a052020-03-10 09:41:45 +0100293 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200294 assert_non_null(tree);
295 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
296 assert_string_equal("c", tree->schema->name);
297 cont = (struct lyd_node_inner*)tree;
298 assert_true(cont->flags & LYD_DEFAULT);
299 lyd_free_all(tree);
300
301 data = "<cp xmlns=\"urn:tests:a\"/>";
Michal Vasko9f96a052020-03-10 09:41:45 +0100302 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200303 assert_non_null(tree);
304 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
305 assert_string_equal("cp", tree->schema->name);
306 cont = (struct lyd_node_inner*)tree;
307 assert_false(cont->flags & LYD_DEFAULT);
308 lyd_free_all(tree);
309
310 *state = NULL;
311}
312
Michal Vasko44685da2020-03-17 15:38:06 +0100313static void
314test_opaq(void **state)
315{
316 *state = test_opaq;
317
318 const char *data;
319 char *str;
320 struct lyd_node *tree;
321
322 /* invalid value, no flags */
323 data = "<foo3 xmlns=\"urn:tests:a\"/>";
324 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko1ce933a2020-03-30 12:38:22 +0200325 logbuf_assert("Invalid empty uint32 value. /a:foo3");
Michal Vasko44685da2020-03-17 15:38:06 +0100326 assert_null(tree);
327
328 /* opaq flag */
329 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
330 assert_non_null(tree);
331 assert_null(tree->schema);
332 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3");
333 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
334
335 lyd_print_mem(&str, tree, LYD_XML, 0);
336 assert_string_equal(str, "<foo3 xmlns=\"urn:tests:a\"/>");
337 free(str);
338 lyd_free_all(tree);
339
340 /* missing key, no flags */
341 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><d>val_d</d></l1>";
342 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
343 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']");
344 assert_null(tree);
345
346 /* opaq flag */
347 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
348 assert_non_null(tree);
349 assert_null(tree->schema);
350 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
351 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
352
353 lyd_print_mem(&str, tree, LYD_XML, 0);
354 assert_string_equal(str, data);
355 free(str);
356 lyd_free_all(tree);
357
358 /* invalid key, no flags */
359 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><c>val_c</c></l1>";
360 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree));
Michal Vasko1ce933a2020-03-30 12:38:22 +0200361 logbuf_assert("Invalid int16 value \"val_c\". /a:c");
Michal Vasko44685da2020-03-17 15:38:06 +0100362 assert_null(tree);
363
364 /* opaq flag */
365 assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
366 assert_non_null(tree);
367 assert_null(tree->schema);
368 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
369 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
370
371 lyd_print_mem(&str, tree, LYD_XML, 0);
372 assert_string_equal(str, data);
373 free(str);
374 lyd_free_all(tree);
375
Michal Vasko413c7f22020-05-05 12:34:06 +0200376 /* opaq flag and fail */
377 assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, "<a xmlns=\"ns\"><b>x</b><c xml:id=\"D\">1</c></a>",
378 LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree));
379 assert_null(tree);
380
Michal Vasko44685da2020-03-17 15:38:06 +0100381 *state = NULL;
382}
383
Michal Vaskob36053d2020-03-26 15:49:30 +0100384static void
385test_rpc(void **state)
386{
387 *state = test_rpc;
388
389 const char *data;
390 char *str;
391 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100392 const struct lyd_node *node;
Michal Vaskob36053d2020-03-26 15:49:30 +0100393
394 data =
395 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
396 "<edit-config>"
397 "<target>"
398 "<running/>"
Michal Vaskob36053d2020-03-26 15:49:30 +0100399 "</target>"
Michal Vasko1bf09392020-03-27 12:38:10 +0100400 "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
401 "<l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">"
402 "<a>val_a</a>"
403 "<b>val_b</b>"
404 "<c>val_c</c>"
405 "</l1>"
406 "<cp xmlns=\"urn:tests:a\">"
407 "<z nc:operation=\"delete\"/>"
408 "</cp>"
409 "</config>"
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 "</edit-config>"
411 "</rpc>";
Michal Vasko1bf09392020-03-27 12:38:10 +0100412 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op));
413
414 assert_non_null(op);
415 assert_string_equal(op->schema->name, "edit-config");
416
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 assert_non_null(tree);
418 assert_null(tree->schema);
Michal Vasko1bf09392020-03-27 12:38:10 +0100419 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc");
420 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
421 node = lyd_node_children(tree);
422 assert_string_equal(node->schema->name, "edit-config");
423 node = lyd_node_children(node)->next;
424 assert_string_equal(node->schema->name, "config");
425 node = ((struct lyd_node_any *)node)->value.tree;
426 /* l1 key c has invalid value */
427 assert_null(node->schema);
428 assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1");
429 node = node->next;
430 assert_non_null(node->schema);
431 assert_string_equal(node->schema->name, "cp");
432 node = lyd_node_children(node);
433 /* z has no value */
434 assert_null(node->schema);
435 assert_string_equal(((struct lyd_node_opaq *)node)->name, "z");
Michal Vaskob36053d2020-03-26 15:49:30 +0100436
437 lyd_print_mem(&str, tree, LYD_XML, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100438 assert_string_equal(str,
439 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
440 "<edit-config>"
441 "<target>"
442 "<running/>"
443 "</target>"
444 "<config>"
445 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
446 "<a>val_a</a>"
447 "<b>val_b</b>"
448 "<c>val_c</c>"
449 "</l1>"
450 "<cp xmlns=\"urn:tests:a\">"
451 "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>"
452 "</cp>"
453 "</config>"
454 "</edit-config>"
455 "</rpc>");
Michal Vaskob36053d2020-03-26 15:49:30 +0100456 free(str);
457 lyd_free_all(tree);
458
459 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100460 /* TODO */
461
462 *state = NULL;
463}
464
465static void
466test_action(void **state)
467{
468 *state = test_action;
469
470 const char *data;
471 char *str;
472 struct lyd_node *tree, *op;
473 const struct lyd_node *node;
474
475 data =
476 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
477 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
478 "<c xmlns=\"urn:tests:a\">"
479 "<act>"
480 "<al>value</al>"
481 "</act>"
482 "</c>"
483 "</action>"
484 "</rpc>";
485 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op));
486
487 assert_non_null(op);
488 assert_string_equal(op->schema->name, "act");
489
490 assert_non_null(tree);
491 assert_null(tree->schema);
492 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc");
493 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
494 node = lyd_node_children(tree);
495 assert_null(node->schema);
496 assert_string_equal(((struct lyd_node_opaq *)node)->name, "action");
497 assert_null(((struct lyd_node_opaq *)node)->attr);
498
499 lyd_print_mem(&str, tree, LYD_XML, 0);
500 assert_string_equal(str,
501 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
502 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
503 "<c xmlns=\"urn:tests:a\">"
504 "<act>"
505 "<al>value</al>"
506 "</act>"
507 "</c>"
508 "</action>"
509 "</rpc>");
510 free(str);
511 lyd_free_all(tree);
512
513 /* wrong namespace, element name, whatever... */
514 /* TODO */
515
516 *state = NULL;
517}
518
519static void
520test_notification(void **state)
521{
522 *state = test_notification;
523
524 const char *data;
525 char *str;
526 struct lyd_node *tree, *ntf;
527 const struct lyd_node *node;
528
529 data =
530 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">"
531 "<eventTime>2037-07-08T00:01:00Z</eventTime>"
532 "<c xmlns=\"urn:tests:a\">"
533 "<n1>"
534 "<nl>value</nl>"
535 "</n1>"
536 "</c>"
537 "</notification>";
538 assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf));
539
540 assert_non_null(ntf);
541 assert_string_equal(ntf->schema->name, "n1");
542
543 assert_non_null(tree);
544 assert_null(tree->schema);
545 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "notification");
546 assert_null(((struct lyd_node_opaq *)tree)->attr);
547 node = lyd_node_children(tree);
548 assert_null(node->schema);
549 assert_string_equal(((struct lyd_node_opaq *)node)->name, "eventTime");
550 assert_string_equal(((struct lyd_node_opaq *)node)->value, "2037-07-08T00:01:00Z");
551 assert_null(((struct lyd_node_opaq *)node)->attr);
552 node = node->next;
553 assert_non_null(node->schema);
554 assert_string_equal(node->schema->name, "c");
555
556 lyd_print_mem(&str, tree, LYD_XML, 0);
557 assert_string_equal(str, data);
558 free(str);
559 lyd_free_all(tree);
560
561 /* top-level notif without envelope */
562 data = "<n2 xmlns=\"urn:tests:a\"/>";
563 assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf));
564
565 assert_non_null(ntf);
566 assert_string_equal(ntf->schema->name, "n2");
567
568 assert_non_null(tree);
569 assert_ptr_equal(ntf, tree);
570
571 lyd_print_mem(&str, tree, LYD_XML, 0);
572 assert_string_equal(str, data);
573 free(str);
574 lyd_free_all(tree);
575
576 /* wrong namespace, element name, whatever... */
577 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100578
579 *state = NULL;
580}
581
Michal Vasko1ce933a2020-03-30 12:38:22 +0200582static void
583test_reply(void **state)
584{
585 *state = test_reply;
586
587 const char *data;
588 char *str;
589 struct lyd_node *request, *tree, *op;
590 const struct lyd_node *node;
591
592 data =
593 "<c xmlns=\"urn:tests:a\">"
594 "<act>"
595 "<al>value</al>"
596 "</act>"
597 "</c>";
598 assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &request, NULL));
599 data =
600 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\">"
601 "<al xmlns=\"urn:tests:a\">25</al>"
602 "</rpc-reply>";
603 assert_int_equal(LY_SUCCESS, lyd_parse_xml_reply(request, data, &tree, &op));
604 lyd_free_all(request);
605
606 assert_non_null(op);
607 assert_string_equal(op->schema->name, "act");
608 node = lyd_node_children(op);
609 assert_non_null(node->schema);
610 assert_string_equal(node->schema->name, "al");
611 assert_true(node->schema->flags & LYS_CONFIG_R);
612
613 assert_non_null(tree);
614 assert_null(tree->schema);
615 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc-reply");
616 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
617 node = lyd_node_children(tree);
618 assert_non_null(node->schema);
619 assert_string_equal(node->schema->name, "c");
620
621 /* TODO print only rpc-reply node and then output subtree */
622 lyd_print_mem(&str, lyd_node_children(op), LYD_XML, 0);
623 assert_string_equal(str,
624 "<al xmlns=\"urn:tests:a\">25</al>");
625 free(str);
626 lyd_free_all(tree);
627
628 /* wrong namespace, element name, whatever... */
629 /* TODO */
630
631 *state = NULL;
632}
633
Radek Krejci509e2592019-05-15 16:30:48 +0200634int main(void)
635{
636 const struct CMUnitTest tests[] = {
637 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
Radek Krejciee4cab22019-07-17 17:07:47 +0200638 cmocka_unit_test_setup_teardown(test_anydata, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200639 cmocka_unit_test_setup_teardown(test_list, setup, teardown),
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200640 cmocka_unit_test_setup_teardown(test_container, setup, teardown),
Michal Vasko44685da2020-03-17 15:38:06 +0100641 cmocka_unit_test_setup_teardown(test_opaq, setup, teardown),
Michal Vasko1bf09392020-03-27 12:38:10 +0100642 cmocka_unit_test_setup_teardown(test_rpc, setup, teardown),
Michal Vaskoa8edff02020-03-27 14:47:01 +0100643 cmocka_unit_test_setup_teardown(test_action, setup, teardown),
644 cmocka_unit_test_setup_teardown(test_notification, setup, teardown),
Michal Vasko1ce933a2020-03-30 12:38:22 +0200645 cmocka_unit_test_setup_teardown(test_reply, setup, teardown),
Radek Krejci509e2592019-05-15 16:30:48 +0200646 };
647
648 return cmocka_run_group_tests(tests, NULL, NULL);
649}