blob: 79c85f5994c4c31c937073ce55fb9315bb634015 [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
Radek Krejci509e2592019-05-15 16:30:48 +020015#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
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "context.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020024#include "parser.h"
Radek Krejci7931b192020-06-25 17:05:03 +020025#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "printer.h"
27#include "printer_data.h"
28#include "tests/config.h"
29#include "tree_data_internal.h"
30#include "tree_schema.h"
Radek Krejci509e2592019-05-15 16:30:48 +020031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34int store = -1; /* negative for infinite logging, positive for limited logging */
35
36struct ly_ctx *ctx; /* context for tests */
37
38/* set to 0 to printing error messages to stderr instead of checking them in code */
39#define ENABLE_LOGGER_CHECKING 1
40
41#if ENABLE_LOGGER_CHECKING
42static void
43logger(LY_LOG_LEVEL level, const char *msg, const char *path)
44{
45 (void) level; /* unused */
46 if (store) {
47 if (path && path[0]) {
48 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
49 } else {
50 strncpy(logbuf, msg, BUFSIZE - 1);
51 }
52 if (store > 0) {
53 --store;
54 }
55 }
56}
57#endif
58
59static int
60setup(void **state)
61{
62 (void) state; /* unused */
63
64 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
Michal Vasko44685da2020-03-17 15:38:06 +010065 "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 +020066 "leaf foo { type string;}"
Michal Vaskoa8edff02020-03-27 14:47:01 +010067 "container c {"
68 "leaf x {type string;}"
Michal Vasko1ce933a2020-03-30 12:38:22 +020069 "action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
Michal Vaskoa8edff02020-03-27 14:47:01 +010070 "notification n1 { leaf nl {type string;} }"
71 "}"
Michal Vaskob36053d2020-03-26 15:49:30 +010072 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
Michal Vasko8d544252020-03-02 10:19:52 +010073 "anydata any {config false;}"
Michal Vasko44685da2020-03-17 15:38:06 +010074 "leaf foo2 { type string; default \"default-val\"; }"
Michal Vaskoa8edff02020-03-27 14:47:01 +010075 "leaf foo3 { type uint32; }"
76 "notification n2;}";
Michal Vaskob36053d2020-03-26 15:49:30 +010077 const struct lys_module *mod;
Radek Krejci509e2592019-05-15 16:30:48 +020078
79#if ENABLE_LOGGER_CHECKING
80 ly_set_log_clb(logger, 1);
81#endif
82
Michal Vasko8d544252020-03-02 10:19:52 +010083 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
84 assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01"));
Michal Vaskob36053d2020-03-26 15:49:30 +010085 assert_non_null((mod = ly_ctx_load_module(ctx, "ietf-netconf", "2011-06-01")));
86 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "writable-running"));
Radek Krejci509e2592019-05-15 16:30:48 +020087 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
88
89 return 0;
90}
91
92static int
93teardown(void **state)
94{
95#if ENABLE_LOGGER_CHECKING
96 if (*state) {
97 fprintf(stderr, "%s\n", logbuf);
98 }
99#else
100 (void) state; /* unused */
101#endif
102
103 ly_ctx_destroy(ctx, NULL);
104 ctx = NULL;
105
106 return 0;
107}
108
109void
110logbuf_clean(void)
111{
112 logbuf[0] = '\0';
113}
114
115#if ENABLE_LOGGER_CHECKING
116# define logbuf_assert(str) assert_string_equal(logbuf, str)
117#else
118# define logbuf_assert(str)
119#endif
120
Radek Krejci509e2592019-05-15 16:30:48 +0200121static void
122test_leaf(void **state)
123{
124 *state = test_leaf;
125
126 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
127 struct lyd_node *tree;
128 struct lyd_node_term *leaf;
129
Michal Vasko63f3d842020-07-08 10:10:14 +0200130 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci509e2592019-05-15 16:30:48 +0200131 assert_non_null(tree);
132 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
133 assert_string_equal("foo", tree->schema->name);
134 leaf = (struct lyd_node_term*)tree;
Radek Krejci950f6a52019-09-12 17:15:32 +0200135 assert_string_equal("foo value", leaf->value.original);
Radek Krejci509e2592019-05-15 16:30:48 +0200136
Michal Vasko8d544252020-03-02 10:19:52 +0100137 assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype);
138 assert_string_equal("foo2", tree->next->next->schema->name);
139 leaf = (struct lyd_node_term*)tree->next->next;
140 assert_string_equal("default-val", leaf->value.original);
141 assert_true(leaf->flags & LYD_DEFAULT);
142
Radek Krejci509e2592019-05-15 16:30:48 +0200143 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100144
145 /* make foo2 explicit */
146 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200147 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100148 assert_non_null(tree);
149 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
150 assert_string_equal("foo2", tree->schema->name);
151 leaf = (struct lyd_node_term*)tree;
152 assert_string_equal("default-val", leaf->value.original);
153 assert_false(leaf->flags & LYD_DEFAULT);
154
155 lyd_free_all(tree);
156
157 /* parse foo2 but make it implicit */
158 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 Vasko63f3d842020-07-08 10:10:14 +0200159 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko8d544252020-03-02 10:19:52 +0100160 assert_non_null(tree);
161 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
162 assert_string_equal("foo2", tree->schema->name);
163 leaf = (struct lyd_node_term*)tree;
164 assert_string_equal("default-val", leaf->value.original);
165 assert_true(leaf->flags & LYD_DEFAULT);
166
167 lyd_free_all(tree);
168
Radek Krejci509e2592019-05-15 16:30:48 +0200169 *state = NULL;
170}
171
Radek Krejciee4cab22019-07-17 17:07:47 +0200172static void
173test_anydata(void **state)
174{
175 *state = test_anydata;
176
Michal Vasko52927e22020-03-16 17:26:14 +0100177 const char *data;
178 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200179 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200180
Radek Krejci241f6b52020-05-21 18:13:49 +0200181 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200182 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100183
Michal Vasko52927e22020-03-16 17:26:14 +0100184 data =
185 "<any xmlns=\"urn:tests:a\">"
186 "<element1>"
187 "<x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>"
188 "</element1>"
189 "<element1a/>"
190 "</any>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200191 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejciee4cab22019-07-17 17:07:47 +0200192 assert_non_null(tree);
193 assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
194 assert_string_equal("any", tree->schema->name);
Michal Vasko52927e22020-03-16 17:26:14 +0100195
Radek Krejcia5bba312020-01-09 15:41:20 +0100196 lyd_print(out, tree, LYD_XML, 0);
Michal Vasko52927e22020-03-16 17:26:14 +0100197 assert_string_equal(str,
198 "<any xmlns=\"urn:tests:a\">"
199 "<element1>"
200 "<element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>"
201 "</element1>"
202 "<element1a/>"
203 "</any>"
204 );
Radek Krejci241f6b52020-05-21 18:13:49 +0200205 ly_out_reset(out);
Radek Krejciee4cab22019-07-17 17:07:47 +0200206
207 lyd_free_all(tree);
Radek Krejci241f6b52020-05-21 18:13:49 +0200208 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100209
Radek Krejciee4cab22019-07-17 17:07:47 +0200210 *state = NULL;
211}
212
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200213static void
214test_list(void **state)
215{
216 *state = test_list;
217
Michal Vasko44685da2020-03-17 15:38:06 +0100218 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 +0200219 struct lyd_node *tree, *iter;
220 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200221 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200222
Radek Krejci710226d2019-07-24 17:24:59 +0200223 /* check hashes */
Michal Vasko63f3d842020-07-08 10:10:14 +0200224 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200225 assert_non_null(tree);
226 assert_int_equal(LYS_LIST, tree->schema->nodetype);
227 assert_string_equal("l1", tree->schema->name);
228 list = (struct lyd_node_inner*)tree;
229 LY_LIST_FOR(list->child, iter) {
230 assert_int_not_equal(0, iter->hash);
231 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200232 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200233
Michal Vasko9f96a052020-03-10 09:41:45 +0100234 /* missing keys */
Michal Vasko44685da2020-03-17 15:38:06 +0100235 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200236 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100237 logbuf_assert("List instance is missing its key \"a\". /a:l1[b='b'][c='1']");
Michal Vasko9f96a052020-03-10 09:41:45 +0100238
239 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko9f96a052020-03-10 09:41:45 +0100241 logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']");
242
243 data = "<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200244 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko9f96a052020-03-10 09:41:45 +0100245 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']");
246
247 /* key duplicate */
Michal Vasko44685da2020-03-17 15:38:06 +0100248 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a><c>1</c></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200249 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100250 logbuf_assert("Duplicate instance of \"c\". /a:l1[a='a'][b='b'][c='1'][c='1']/c");
Michal Vasko9f96a052020-03-10 09:41:45 +0100251
Radek Krejci710226d2019-07-24 17:24:59 +0200252 /* keys order */
Michal Vasko44685da2020-03-17 15:38:06 +0100253 data = "<l1 xmlns=\"urn:tests:a\"><d>d</d><a>a</a><c>1</c><b>b</b></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200254 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200255 assert_non_null(tree);
256 assert_int_equal(LYS_LIST, tree->schema->nodetype);
257 assert_string_equal("l1", tree->schema->name);
258 list = (struct lyd_node_inner*)tree;
259 assert_non_null(leaf = (struct lyd_node_term*)list->child);
260 assert_string_equal("a", leaf->schema->name);
261 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
262 assert_string_equal("b", leaf->schema->name);
263 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
264 assert_string_equal("c", leaf->schema->name);
265 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
266 assert_string_equal("d", leaf->schema->name);
267 logbuf_assert("Invalid position of the key \"b\" in a list.");
268 lyd_free_all(tree);
269
Michal Vasko44685da2020-03-17 15:38:06 +0100270 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200271 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200272 assert_non_null(tree);
273 assert_int_equal(LYS_LIST, tree->schema->nodetype);
274 assert_string_equal("l1", tree->schema->name);
275 list = (struct lyd_node_inner*)tree;
276 assert_non_null(leaf = (struct lyd_node_term*)list->child);
277 assert_string_equal("a", leaf->schema->name);
278 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
279 assert_string_equal("b", leaf->schema->name);
280 assert_non_null(leaf = (struct lyd_node_term*)leaf->next);
281 assert_string_equal("c", leaf->schema->name);
282 logbuf_assert("Invalid position of the key \"a\" in a list.");
283 logbuf_clean();
284 lyd_free_all(tree);
285
Michal Vasko63f3d842020-07-08 10:10:14 +0200286 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, 0, &tree));
Radek Krejci710226d2019-07-24 17:24:59 +0200287 logbuf_assert("Invalid position of the key \"b\" in a list. Line number 1.");
288
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200289 *state = NULL;
290}
291
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200292static void
293test_container(void **state)
294{
295 *state = test_container;
296
297 const char *data = "<c xmlns=\"urn:tests:a\"/>";
298 struct lyd_node *tree;
299 struct lyd_node_inner *cont;
300
Michal Vasko63f3d842020-07-08 10:10:14 +0200301 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200302 assert_non_null(tree);
303 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
304 assert_string_equal("c", tree->schema->name);
305 cont = (struct lyd_node_inner*)tree;
306 assert_true(cont->flags & LYD_DEFAULT);
307 lyd_free_all(tree);
308
309 data = "<cp xmlns=\"urn:tests:a\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200311 assert_non_null(tree);
312 assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
313 assert_string_equal("cp", tree->schema->name);
314 cont = (struct lyd_node_inner*)tree;
315 assert_false(cont->flags & LYD_DEFAULT);
316 lyd_free_all(tree);
317
318 *state = NULL;
319}
320
Michal Vasko44685da2020-03-17 15:38:06 +0100321static void
322test_opaq(void **state)
323{
324 *state = test_opaq;
325
326 const char *data;
327 char *str;
328 struct lyd_node *tree;
329
Radek Krejci241f6b52020-05-21 18:13:49 +0200330 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200331 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100332
Michal Vasko44685da2020-03-17 15:38:06 +0100333 /* invalid value, no flags */
334 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko1ce933a2020-03-30 12:38:22 +0200336 logbuf_assert("Invalid empty uint32 value. /a:foo3");
Michal Vasko44685da2020-03-17 15:38:06 +0100337 assert_null(tree);
338
339 /* opaq flag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200340 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100341 assert_non_null(tree);
342 assert_null(tree->schema);
343 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3");
344 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
345
Radek Krejcia5bba312020-01-09 15:41:20 +0100346 lyd_print(out, tree, LYD_XML, 0);
Michal Vasko44685da2020-03-17 15:38:06 +0100347 assert_string_equal(str, "<foo3 xmlns=\"urn:tests:a\"/>");
Radek Krejci241f6b52020-05-21 18:13:49 +0200348 ly_out_reset(out);
Michal Vasko44685da2020-03-17 15:38:06 +0100349 lyd_free_all(tree);
350
351 /* missing key, no flags */
352 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><d>val_d</d></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100354 logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']");
355 assert_null(tree);
356
357 /* opaq flag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_OPAQ , LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100359 assert_non_null(tree);
360 assert_null(tree->schema);
361 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
362 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
363
Radek Krejcia5bba312020-01-09 15:41:20 +0100364 lyd_print(out, tree, LYD_XML, 0);
Michal Vasko44685da2020-03-17 15:38:06 +0100365 assert_string_equal(str, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200366 ly_out_reset(out);
Michal Vasko44685da2020-03-17 15:38:06 +0100367 lyd_free_all(tree);
368
369 /* invalid key, no flags */
370 data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><c>val_c</c></l1>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskof872e202020-05-27 11:49:06 +0200372 logbuf_assert("Invalid int16 value \"val_c\". /a:l1/c");
Michal Vasko44685da2020-03-17 15:38:06 +0100373 assert_null(tree);
374
375 /* opaq flag */
Michal Vasko63f3d842020-07-08 10:10:14 +0200376 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko44685da2020-03-17 15:38:06 +0100377 assert_non_null(tree);
378 assert_null(tree->schema);
379 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1");
380 assert_string_equal(((struct lyd_node_opaq *)tree)->value, "");
381
Radek Krejcia5bba312020-01-09 15:41:20 +0100382 lyd_print(out, tree, LYD_XML, 0);
Michal Vasko44685da2020-03-17 15:38:06 +0100383 assert_string_equal(str, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200384 ly_out_reset(out);
Michal Vasko44685da2020-03-17 15:38:06 +0100385 lyd_free_all(tree);
386
Michal Vasko413c7f22020-05-05 12:34:06 +0200387 /* opaq flag and fail */
Michal Vasko63f3d842020-07-08 10:10:14 +0200388 assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, "<a xmlns=\"ns\"><b>x</b><c xml:id=\"D\">1</c></a>", LYD_XML,
389 LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko413c7f22020-05-05 12:34:06 +0200390 assert_null(tree);
391
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko44685da2020-03-17 15:38:06 +0100394 *state = NULL;
395}
396
Michal Vaskob36053d2020-03-26 15:49:30 +0100397static void
398test_rpc(void **state)
399{
400 *state = test_rpc;
401
402 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100404 char *str;
405 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100406 const struct lyd_node *node;
Michal Vaskob36053d2020-03-26 15:49:30 +0100407
Radek Krejci241f6b52020-05-21 18:13:49 +0200408 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200409 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100410
Michal Vaskob36053d2020-03-26 15:49:30 +0100411 data =
412 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
413 "<edit-config>"
414 "<target>"
415 "<running/>"
Michal Vaskob36053d2020-03-26 15:49:30 +0100416 "</target>"
Michal Vasko1bf09392020-03-27 12:38:10 +0100417 "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
418 "<l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">"
419 "<a>val_a</a>"
420 "<b>val_b</b>"
421 "<c>val_c</c>"
422 "</l1>"
423 "<cp xmlns=\"urn:tests:a\">"
424 "<z nc:operation=\"delete\"/>"
425 "</cp>"
426 "</config>"
Michal Vaskob36053d2020-03-26 15:49:30 +0100427 "</edit-config>"
428 "</rpc>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
430 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &tree, &op));
431 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100432
433 assert_non_null(op);
434 assert_string_equal(op->schema->name, "edit-config");
435
Michal Vaskob36053d2020-03-26 15:49:30 +0100436 assert_non_null(tree);
437 assert_null(tree->schema);
Michal Vasko1bf09392020-03-27 12:38:10 +0100438 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc");
439 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200440 node = lyd_node_children(tree, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100441 assert_string_equal(node->schema->name, "edit-config");
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200442 node = lyd_node_children(node, 0)->next;
Michal Vasko1bf09392020-03-27 12:38:10 +0100443 assert_string_equal(node->schema->name, "config");
444 node = ((struct lyd_node_any *)node)->value.tree;
445 /* l1 key c has invalid value */
446 assert_null(node->schema);
447 assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1");
448 node = node->next;
449 assert_non_null(node->schema);
450 assert_string_equal(node->schema->name, "cp");
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200451 node = lyd_node_children(node, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100452 /* z has no value */
453 assert_null(node->schema);
454 assert_string_equal(((struct lyd_node_opaq *)node)->name, "z");
Michal Vaskob36053d2020-03-26 15:49:30 +0100455
Radek Krejcia5bba312020-01-09 15:41:20 +0100456 lyd_print(out, tree, LYD_XML, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100457 assert_string_equal(str,
458 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
459 "<edit-config>"
460 "<target>"
461 "<running/>"
462 "</target>"
463 "<config>"
464 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
465 "<a>val_a</a>"
466 "<b>val_b</b>"
467 "<c>val_c</c>"
468 "</l1>"
469 "<cp xmlns=\"urn:tests:a\">"
470 "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>"
471 "</cp>"
472 "</config>"
473 "</edit-config>"
474 "</rpc>");
Radek Krejci241f6b52020-05-21 18:13:49 +0200475 ly_out_reset(out);
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 lyd_free_all(tree);
477
478 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100479 /* TODO */
480
Radek Krejci241f6b52020-05-21 18:13:49 +0200481 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100482
Michal Vaskoa8edff02020-03-27 14:47:01 +0100483 *state = NULL;
484}
485
486static void
487test_action(void **state)
488{
489 *state = test_action;
490
491 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100493 char *str;
494 struct lyd_node *tree, *op;
495 const struct lyd_node *node;
496
Radek Krejci241f6b52020-05-21 18:13:49 +0200497 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200498 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100499
Michal Vaskoa8edff02020-03-27 14:47:01 +0100500 data =
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>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200510 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
511 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &tree, &op));
512 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100513
514 assert_non_null(op);
515 assert_string_equal(op->schema->name, "act");
516
517 assert_non_null(tree);
518 assert_null(tree->schema);
519 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc");
520 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200521 node = lyd_node_children(tree, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100522 assert_null(node->schema);
523 assert_string_equal(((struct lyd_node_opaq *)node)->name, "action");
524 assert_null(((struct lyd_node_opaq *)node)->attr);
525
Radek Krejcia5bba312020-01-09 15:41:20 +0100526 lyd_print(out, tree, LYD_XML, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100527 assert_string_equal(str,
528 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">"
529 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
530 "<c xmlns=\"urn:tests:a\">"
531 "<act>"
532 "<al>value</al>"
533 "</act>"
534 "</c>"
535 "</action>"
536 "</rpc>");
Radek Krejci241f6b52020-05-21 18:13:49 +0200537 ly_out_reset(out);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100538 lyd_free_all(tree);
539
540 /* wrong namespace, element name, whatever... */
541 /* TODO */
542
Radek Krejci241f6b52020-05-21 18:13:49 +0200543 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100544
Michal Vaskoa8edff02020-03-27 14:47:01 +0100545 *state = NULL;
546}
547
548static void
549test_notification(void **state)
550{
551 *state = test_notification;
552
553 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200554 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100555 char *str;
556 struct lyd_node *tree, *ntf;
557 const struct lyd_node *node;
558
Radek Krejci241f6b52020-05-21 18:13:49 +0200559 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200560 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100561
Michal Vaskoa8edff02020-03-27 14:47:01 +0100562 data =
563 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">"
564 "<eventTime>2037-07-08T00:01:00Z</eventTime>"
565 "<c xmlns=\"urn:tests:a\">"
566 "<n1>"
567 "<nl>value</nl>"
568 "</n1>"
569 "</c>"
570 "</notification>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200571 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
572 assert_int_equal(LY_SUCCESS, lyd_parse_notif(ctx, in, LYD_XML, &tree, &ntf));
573 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100574
575 assert_non_null(ntf);
576 assert_string_equal(ntf->schema->name, "n1");
577
578 assert_non_null(tree);
579 assert_null(tree->schema);
580 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "notification");
581 assert_null(((struct lyd_node_opaq *)tree)->attr);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200582 node = lyd_node_children(tree, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100583 assert_null(node->schema);
584 assert_string_equal(((struct lyd_node_opaq *)node)->name, "eventTime");
585 assert_string_equal(((struct lyd_node_opaq *)node)->value, "2037-07-08T00:01:00Z");
586 assert_null(((struct lyd_node_opaq *)node)->attr);
587 node = node->next;
588 assert_non_null(node->schema);
589 assert_string_equal(node->schema->name, "c");
590
Radek Krejcia5bba312020-01-09 15:41:20 +0100591 lyd_print(out, tree, LYD_XML, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100592 assert_string_equal(str, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200593 ly_out_reset(out);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100594 lyd_free_all(tree);
595
596 /* top-level notif without envelope */
597 data = "<n2 xmlns=\"urn:tests:a\"/>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200598 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
599 assert_int_equal(LY_SUCCESS, lyd_parse_notif(ctx, in, LYD_XML, &tree, &ntf));
600 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100601
602 assert_non_null(ntf);
603 assert_string_equal(ntf->schema->name, "n2");
604
605 assert_non_null(tree);
606 assert_ptr_equal(ntf, tree);
607
Radek Krejcia5bba312020-01-09 15:41:20 +0100608 lyd_print(out, tree, LYD_XML, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100609 assert_string_equal(str, data);
Radek Krejci241f6b52020-05-21 18:13:49 +0200610 ly_out_reset(out);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100611 lyd_free_all(tree);
612
613 /* wrong namespace, element name, whatever... */
614 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100615
Radek Krejci241f6b52020-05-21 18:13:49 +0200616 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100617
Michal Vaskob36053d2020-03-26 15:49:30 +0100618 *state = NULL;
619}
620
Michal Vasko1ce933a2020-03-30 12:38:22 +0200621static void
622test_reply(void **state)
623{
624 *state = test_reply;
625
626 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200627 struct ly_in *in;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200628 char *str;
629 struct lyd_node *request, *tree, *op;
630 const struct lyd_node *node;
631
Radek Krejci241f6b52020-05-21 18:13:49 +0200632 struct ly_out *out;
Radek Krejci84ce7b12020-06-11 17:28:25 +0200633 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100634
Michal Vasko1ce933a2020-03-30 12:38:22 +0200635 data =
636 "<c xmlns=\"urn:tests:a\">"
637 "<act>"
638 "<al>value</al>"
639 "</act>"
640 "</c>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200641 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
642 assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &request, NULL));
643 ly_in_free(in, 0);
644
Michal Vasko1ce933a2020-03-30 12:38:22 +0200645 data =
646 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\">"
647 "<al xmlns=\"urn:tests:a\">25</al>"
648 "</rpc-reply>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
650 assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_XML, &tree, &op));
651 ly_in_free(in, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200652 lyd_free_all(request);
653
654 assert_non_null(op);
655 assert_string_equal(op->schema->name, "act");
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200656 node = lyd_node_children(op, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200657 assert_non_null(node->schema);
658 assert_string_equal(node->schema->name, "al");
659 assert_true(node->schema->flags & LYS_CONFIG_R);
660
661 assert_non_null(tree);
662 assert_null(tree->schema);
663 assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc-reply");
664 assert_non_null(((struct lyd_node_opaq *)tree)->attr);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200665 node = lyd_node_children(tree, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200666 assert_non_null(node->schema);
667 assert_string_equal(node->schema->name, "c");
668
669 /* TODO print only rpc-reply node and then output subtree */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200670 lyd_print(out, lyd_node_children(op, 0), LYD_XML, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200671 assert_string_equal(str,
672 "<al xmlns=\"urn:tests:a\">25</al>");
Radek Krejci241f6b52020-05-21 18:13:49 +0200673 ly_out_reset(out);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200674 lyd_free_all(tree);
675
676 /* wrong namespace, element name, whatever... */
677 /* TODO */
678
Radek Krejci241f6b52020-05-21 18:13:49 +0200679 ly_out_free(out, NULL, 1);
Radek Krejcia5bba312020-01-09 15:41:20 +0100680
Michal Vasko1ce933a2020-03-30 12:38:22 +0200681 *state = NULL;
682}
683
Radek Krejci509e2592019-05-15 16:30:48 +0200684int main(void)
685{
686 const struct CMUnitTest tests[] = {
687 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
Radek Krejciee4cab22019-07-17 17:07:47 +0200688 cmocka_unit_test_setup_teardown(test_anydata, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200689 cmocka_unit_test_setup_teardown(test_list, setup, teardown),
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200690 cmocka_unit_test_setup_teardown(test_container, setup, teardown),
Michal Vasko44685da2020-03-17 15:38:06 +0100691 cmocka_unit_test_setup_teardown(test_opaq, setup, teardown),
Michal Vasko1bf09392020-03-27 12:38:10 +0100692 cmocka_unit_test_setup_teardown(test_rpc, setup, teardown),
Michal Vaskoa8edff02020-03-27 14:47:01 +0100693 cmocka_unit_test_setup_teardown(test_action, setup, teardown),
694 cmocka_unit_test_setup_teardown(test_notification, setup, teardown),
Michal Vasko1ce933a2020-03-30 12:38:22 +0200695 cmocka_unit_test_setup_teardown(test_reply, setup, teardown),
Radek Krejci509e2592019-05-15 16:30:48 +0200696 };
697
698 return cmocka_run_group_tests(tests, NULL, NULL);
699}