blob: d63fafaea0c0d0e0811aa0e1082a8bcca3f66a9b [file] [log] [blame]
Michal Vasko1b2a3f42022-12-20 09:38:28 +01001/**
Radek Krejci509e2592019-05-15 16:30:48 +02002 * @file test_parser_xml.c
Michal Vasko1b2a3f42022-12-20 09:38:28 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci509e2592019-05-15 16:30:48 +02005 * @brief unit tests for functions from parser_xml.c
6 *
Michal Vasko1b2a3f42022-12-20 09:38:28 +01007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejci509e2592019-05-15 16:30:48 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
Radek Krejci509e2592019-05-15 16:30:48 +020017
Radek Krejci70593c12020-06-13 20:48:09 +020018#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "printer_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "tree_data_internal.h"
24#include "tree_schema.h"
Radek Krejci509e2592019-05-15 16:30:48 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010030 "module a {\n"
31 " namespace urn:tests:a;\n"
32 " prefix a;\n"
33 " yang-version 1.1;\n"
Michal Vaskodbf3e652022-10-21 08:46:25 +020034 " list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
35 " leaf d {type string;}"
36 " container cont {leaf e {type boolean;}}"
37 " }"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010038 " leaf foo { type string;}\n"
39 " container c {\n"
40 " leaf x {type string;}\n"
41 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }\n"
42 " notification n1 { leaf nl {type string;}}}\n"
43 " container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}\n"
44 " anydata any {config false;}\n"
Michal Vasko5b414dd2023-04-13 10:29:58 +020045 " anyxml anyx;\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010046 " leaf foo2 { type string; default \"default-val\"; }\n"
47 " leaf foo3 { type uint32; }\n"
48 " notification n2;}";
Radek Krejci509e2592019-05-15 16:30:48 +020049
Radek Iša56ca9e42020-09-08 18:42:00 +020050 UTEST_SETUP;
Radek Krejci509e2592019-05-15 16:30:48 +020051
Radek Iša56ca9e42020-09-08 18:42:00 +020052 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
53 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci509e2592019-05-15 16:30:48 +020054
55 return 0;
56}
57
Radek Iša56ca9e42020-09-08 18:42:00 +020058#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
59 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci509e2592019-05-15 16:30:48 +020060
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
62 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
63 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
64 assert_null(MODEL)
Radek Krejci509e2592019-05-15 16:30:48 +020065
Radek Iša56ca9e42020-09-08 18:42:00 +020066#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
67 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
Radek Krejci509e2592019-05-15 16:30:48 +020068
Radek Krejci509e2592019-05-15 16:30:48 +020069static void
70test_leaf(void **state)
71{
Radek Krejci509e2592019-05-15 16:30:48 +020072 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
73 struct lyd_node *tree;
74 struct lyd_node_term *leaf;
75
Radek Iša56ca9e42020-09-08 18:42:00 +020076 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejci509e2592019-05-15 16:30:48 +020077
Radek Iša56ca9e42020-09-08 18:42:00 +020078 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
79 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
80 leaf = (struct lyd_node_term *)tree;
81 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
82
83 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
84 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020086 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010087 assert_true(leaf->flags & LYD_DEFAULT);
Radek Krejci509e2592019-05-15 16:30:48 +020088 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +010089
90 /* make foo2 explicit */
91 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020092 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +010093 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010094 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020095 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
96 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010097 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020098 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010099 assert_false(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +0100100 lyd_free_all(tree);
101
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 /* parse foo2 but make it implicit, skip metadata xxx from missing schema */
Radek Iša56ca9e42020-09-08 18:42:00 +0200103 data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" "
104 "wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
105 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100106 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100107 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
109 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100110 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100112 assert_true(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +0100113 lyd_free_all(tree);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200114
115 /* invalid value */
116 data = "<l1 xmlns=\"urn:tests:a\"><a>val-a</a><b>val-b</b><c>1</c><cont><e>0</e></cont></l1>";
117 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
118 "Invalid boolean value \"0\".",
119 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
Radek Krejci509e2592019-05-15 16:30:48 +0200120}
121
Radek Krejciee4cab22019-07-17 17:07:47 +0200122static void
123test_anydata(void **state)
124{
Michal Vasko52927e22020-03-16 17:26:14 +0100125 const char *data;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200126 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200127 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200128
Radek Iša56ca9e42020-09-08 18:42:00 +0200129 data = "<any xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100130 " <element1>\n"
131 " <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
132 " </element1>\n"
133 " <element1a/>\n"
134 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200136 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100137 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
139 1, LYS_ANYDATA, 0, 0, NULL, 0);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200140
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 const char *data_expected =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100142 "<any xmlns=\"urn:tests:a\">\n"
143 " <element1>\n"
144 " <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
145 " </element1>\n"
146 " <element1a/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 "</any>\n";
148
149 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Michal Vasko5b414dd2023-04-13 10:29:58 +0200150
151 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
152 lyd_free_all(tree);
153
154 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
155 free(str);
156 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
157 lyd_free_all(tree);
158}
159
160static void
161test_anyxml(void **state)
162{
163 const char *data;
164 char *str;
165 struct lyd_node *tree;
166
167 data = "<anyx xmlns=\"urn:tests:a\">\n"
168 " <element1>\n"
169 " <element2 x:attr2=\"test\" xmlns:x=\"urn:x\">data</element2>\n"
170 " </element1>\n"
171 " <element1a/>\n"
172 "</anyx>\n";
173 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
174 assert_non_null(tree);
175 tree = tree->next;
176
177 const char *data_expected =
178 "<anyx xmlns=\"urn:tests:a\">\n"
179 " <element1>\n"
180 " <element2 xmlns:x=\"urn:x\" x:attr2=\"test\">data</element2>\n"
181 " </element1>\n"
182 " <element1a/>\n"
183 "</anyx>\n";
184
185 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
186
187 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
188 lyd_free_all(tree);
189
190 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
191 free(str);
192 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Radek Krejciee4cab22019-07-17 17:07:47 +0200193 lyd_free_all(tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200194}
195
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200196static void
197test_list(void **state)
198{
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 const char *data;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200200 struct lyd_node *tree, *iter;
201 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200202 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200203
Radek Krejci710226d2019-07-24 17:24:59 +0200204 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
206 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
207 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
208 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100209 list = (struct lyd_node_inner *)tree;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200210 LY_LIST_FOR(list->child, iter) {
211 assert_int_not_equal(0, iter->hash);
212 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200213 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200214
Michal Vasko9f96a052020-03-10 09:41:45 +0100215 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200217 "List instance is missing its key \"a\".", "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Michal Vasko62af3692023-02-09 14:00:09 +0100218 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +0100219
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200221 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100222
Radek Iša56ca9e42020-09-08 18:42:00 +0200223 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200224 "List instance is missing its key \"c\".", "Data location \"/a:l1[a='a'][b='b']\", line number 1.");
Michal Vasko62af3692023-02-09 14:00:09 +0100225 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +0100226
227 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a><c>1</c></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200229 "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200230 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Michal Vasko62af3692023-02-09 14:00:09 +0100231 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
232 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +0100233
Radek Krejci710226d2019-07-24 17:24:59 +0200234 /* keys order */
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 CHECK_PARSE_LYD("<l1 xmlns=\"urn:tests:a\"><d>d</d><a>a</a><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree);
236 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
237 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100238 list = (struct lyd_node_inner *)tree;
239 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100241 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100243 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100245 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200246 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200247 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200248 lyd_free_all(tree);
249
Michal Vasko44685da2020-03-17 15:38:06 +0100250 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200251 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
252 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1", 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100253 list = (struct lyd_node_inner *)tree;
254 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100256 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100258 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200259 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
260 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Michal Vasko62af3692023-02-09 14:00:09 +0100261 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200262 lyd_free_all(tree);
263
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200265 "Invalid position of the key \"b\" in a list.", "Data location \"/a:l1[c='1']/b\", line number 1.");
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200266}
267
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200268static void
269test_container(void **state)
270{
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200271 struct lyd_node *tree;
272 struct lyd_node_inner *cont;
273
Radek Iša56ca9e42020-09-08 18:42:00 +0200274 CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
275 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100276 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200277 assert_true(cont->flags & LYD_DEFAULT);
278 lyd_free_all(tree);
279
Radek Iša56ca9e42020-09-08 18:42:00 +0200280 CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200281 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100282 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100283 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200284 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100285 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200286 assert_false(cont->flags & LYD_DEFAULT);
287 lyd_free_all(tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200288}
289
Michal Vasko44685da2020-03-17 15:38:06 +0100290static void
291test_opaq(void **state)
292{
Michal Vasko44685da2020-03-17 15:38:06 +0100293 const char *data;
Michal Vasko44685da2020-03-17 15:38:06 +0100294 struct lyd_node *tree;
295
296 /* invalid value, no flags */
297 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200298 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200299 "Invalid type uint32 empty value.", "Schema location \"/a:foo3\", line number 1.");
Michal Vasko44685da2020-03-17 15:38:06 +0100300
301 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200302 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200303 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "foo3", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200304 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
Michal Vasko44685da2020-03-17 15:38:06 +0100305 lyd_free_all(tree);
306
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200307 /* list, opaq flag */
308 data = "<l1 xmlns=\"urn:tests:a\"/>";
309 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
310 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
311 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<l1 xmlns=\"urn:tests:a\"/>\n");
312 lyd_free_all(tree);
313
Michal Vasko44685da2020-03-17 15:38:06 +0100314 /* missing key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 data = "<l1 xmlns=\"urn:tests:a\">\n"
316 " <a>val_a</a>\n"
317 " <b>val_b</b>\n"
318 " <d>val_d</d>\n"
319 "</l1>\n";
320 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200321 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200322 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 5.");
Michal Vasko44685da2020-03-17 15:38:06 +0100323
324 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200325 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200326 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200327 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100328 lyd_free_all(tree);
329
330 /* invalid key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200331 data = "<l1 xmlns=\"urn:tests:a\">\n"
332 " <a>val_a</a>\n"
333 " <b>val_b</b>\n"
334 " <c>val_c</c>\n"
335 "</l1>\n";
336 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200337 "Invalid type int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200338 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 4.");
Michal Vasko44685da2020-03-17 15:38:06 +0100339
340 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200341 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200342 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200343 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100344 lyd_free_all(tree);
345
Michal Vasko413c7f22020-05-05 12:34:06 +0200346 /* opaq flag and fail */
Radek Iša56ca9e42020-09-08 18:42:00 +0200347 assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
348 "<a xmlns=\"ns\">\n"
349 " <b>x</b>\n"
Michal Vaskoe137fc42021-07-22 11:53:13 +0200350 " <c xmld:id=\"D\">1</c>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200351 "</a>\n",
352 LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoe137fc42021-07-22 11:53:13 +0200353 CHECK_LOG_CTX("Unknown XML prefix \"xmld\".", "Line number 3.");
Michal Vasko44685da2020-03-17 15:38:06 +0100354}
355
Michal Vaskob36053d2020-03-26 15:49:30 +0100356static void
357test_rpc(void **state)
358{
Michal Vaskob36053d2020-03-26 15:49:30 +0100359 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200360 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100361 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100362 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200363 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
364 "configuration to the specified target configuration.";
365 const char *ref = "RFC 6241, Section 7.2";
366 const char *feats[] = {"writable-running", NULL};
Michal Vaskob36053d2020-03-26 15:49:30 +0100367
Radek Iša56ca9e42020-09-08 18:42:00 +0200368 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100369
Michal Vasko2552ea32020-12-08 15:32:34 +0100370 data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
371 " <target>\n"
372 " <running/>\n"
373 " </target>\n"
374 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
375 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
376 " <a>val_a</a>\n"
377 " <b>val_b</b>\n"
378 " <c>val_c</c>\n"
379 " </l1>\n"
380 " <cp xmlns=\"urn:tests:a\">\n"
381 " <z nc:operation=\"delete\"/>\n"
382 " </cp>\n"
383 " </config>\n"
384 "</edit-config>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200385 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100386 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op));
Michal Vasko63f3d842020-07-08 10:10:14 +0200387 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100388
389 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200390
Radek Krejci2a9fc652021-01-22 17:44:34 +0100391 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200392 1, 0, 0, 1, "edit-config", LYS_RPC,
393 0, 0, 0, 0, 0, ref, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100394
Michal Vaskob36053d2020-03-26 15:49:30 +0100395 assert_non_null(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200396
Michal Vasko2552ea32020-12-08 15:32:34 +0100397 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100398 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200399 1, 0, 0, 1, "edit-config", LYS_RPC,
400 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200401 node = lyd_child(node)->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200402 dsc = "Inline Config content.";
Michal Vaskod1e53b92021-01-28 13:11:06 +0100403 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
Michal Vaskob104f112020-07-17 09:54:54 +0200404
Michal Vasko1bf09392020-03-27 12:38:10 +0100405 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100406 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200407 1, LYS_CONTAINER, 0, 0, NULL, 0);
408
Radek Krejcia1c1e542020-09-29 16:06:52 +0200409 node = lyd_child(node);
Michal Vasko1bf09392020-03-27 12:38:10 +0100410 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200411 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 0, "");
Michal Vaskob104f112020-07-17 09:54:54 +0200412 node = node->parent->next;
413 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200414 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100415
Radek Iša56ca9e42020-09-08 18:42:00 +0200416 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100417 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
418 " <target>\n"
419 " <running/>\n"
420 " </target>\n"
421 " <config>\n"
422 " <cp xmlns=\"urn:tests:a\">\n"
423 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
424 " </cp>\n"
425 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
426 " <a>val_a</a>\n"
427 " <b>val_b</b>\n"
428 " <c>val_c</c>\n"
429 " </l1>\n"
430 " </config>\n"
431 "</edit-config>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200432
Michal Vaskob36053d2020-03-26 15:49:30 +0100433 lyd_free_all(tree);
434
435 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100436 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100437}
438
439static void
440test_action(void **state)
441{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100442 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100444 struct lyd_node *tree, *op;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100445
Michal Vasko2552ea32020-12-08 15:32:34 +0100446 data = "<c xmlns=\"urn:tests:a\">\n"
447 " <act>\n"
448 " <al>value</al>\n"
449 " </act>\n"
450 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200451 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100452 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, &op));
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100454
455 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100456 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200457 1, 0, 0, 1, "act", LYS_ACTION,
458 1, 0, 0, 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100459
Radek Iša56ca9e42020-09-08 18:42:00 +0200460 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100461 "<c xmlns=\"urn:tests:a\">\n"
462 " <act>\n"
463 " <al>value</al>\n"
464 " </act>\n"
465 "</c>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200466
Michal Vaskoa8edff02020-03-27 14:47:01 +0100467 lyd_free_all(tree);
468
469 /* wrong namespace, element name, whatever... */
470 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100471}
472
473static void
474test_notification(void **state)
475{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100476 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200477 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100478 struct lyd_node *tree, *ntf;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100479
Michal Vasko2552ea32020-12-08 15:32:34 +0100480 data = "<c xmlns=\"urn:tests:a\">\n"
481 " <n1>\n"
482 " <nl>value</nl>\n"
483 " </n1>\n"
484 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100486 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_YANG, &tree, &ntf));
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100488
489 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100490 CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100491
Michal Vasko2552ea32020-12-08 15:32:34 +0100492 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100493
Radek Iša56ca9e42020-09-08 18:42:00 +0200494 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100495 lyd_free_all(tree);
496
497 /* top-level notif without envelope */
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 data = "<n2 xmlns=\"urn:tests:a\"/>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200499 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100500 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_YANG, &tree, &ntf));
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100502
503 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100504 CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 0, NULL, 0, 0x4, 1, 0, "n2", 0, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100505
506 assert_non_null(tree);
507 assert_ptr_equal(ntf, tree);
508
Radek Iša56ca9e42020-09-08 18:42:00 +0200509 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100510 lyd_free_all(tree);
511
512 /* wrong namespace, element name, whatever... */
513 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100514}
515
Michal Vasko1ce933a2020-03-30 12:38:22 +0200516static void
517test_reply(void **state)
518{
Michal Vasko1ce933a2020-03-30 12:38:22 +0200519 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200520 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100521 struct lyd_node *tree, *op;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200522 const struct lyd_node *node;
523
Michal Vasko79135ae2020-12-16 10:08:35 +0100524 data = "<c xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100525 " <act>\n"
Michal Vasko2552ea32020-12-08 15:32:34 +0100526 " <al>25</al>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100527 " </act>\n"
528 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200529 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100530 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_REPLY_YANG, &tree, &op));
Michal Vasko63f3d842020-07-08 10:10:14 +0200531 ly_in_free(in, 0);
532
Michal Vasko1ce933a2020-03-30 12:38:22 +0200533 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200534
Radek Krejci2a9fc652021-01-22 17:44:34 +0100535 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200536 1, 0, 0, 1, "act", LYS_ACTION,
537 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200538 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100539 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_STATUS_CURR | LYS_IS_OUTPUT, 1, "al", 0, LYS_LEAF, 1, 0, NULL, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200540
Michal Vasko2552ea32020-12-08 15:32:34 +0100541 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200542
543 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200544 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_WITHSIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
Michal Vasko1ce933a2020-03-30 12:38:22 +0200545 lyd_free_all(tree);
546
547 /* wrong namespace, element name, whatever... */
548 /* TODO */
Michal Vasko1ce933a2020-03-30 12:38:22 +0200549}
550
Michal Vaskoe0665742021-02-11 11:08:44 +0100551static void
552test_netconf_rpc(void **state)
553{
554 const char *data;
555 struct ly_in *in;
556 struct lyd_node *tree, *op;
557 const struct lyd_node *node;
558 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
559 "configuration to the specified target configuration.";
560 const char *ref = "RFC 6241, Section 7.2";
561 const char *feats[] = {"writable-running", NULL};
562
563 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
564
565 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
566 "<edit-config>\n"
567 " <target>\n"
568 " <running/>\n"
569 " </target>\n"
570 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
571 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
572 " <a>val_a</a>\n"
573 " <b>val_b</b>\n"
574 " <c>val_c</c>\n"
575 " </l1>\n"
576 " <cp xmlns=\"urn:tests:a\">\n"
577 " <z nc:operation=\"delete\"/>\n"
578 " </cp>\n"
579 " </config>\n"
580 "</edit-config>\n"
581 "</rpc>\n";
582 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100583 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, &tree, &op));
Michal Vaskoe0665742021-02-11 11:08:44 +0100584 ly_in_free(in, 0);
585
586 assert_non_null(op);
587
588 node = tree;
Radek Krejci8df109d2021-04-23 12:19:08 +0200589 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 1, 0, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100590
591 assert_non_null(tree);
592
593 node = op;
594 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
595 1, 0, 0, 1, "edit-config", LYS_RPC,
596 0, 0, 0, 0, 0, ref, 0);
597 node = lyd_child(node)->next;
598 dsc = "Inline Config content.";
599 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
600
601 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100602 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Michal Vaskoe0665742021-02-11 11:08:44 +0100603 1, LYS_CONTAINER, 0, 0, NULL, 0);
604
605 node = lyd_child(node);
606 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200607 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100608 node = node->parent->next;
609 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200610 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100611
612 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
613 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
614 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
615 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
616 " <target>\n"
617 " <running/>\n"
618 " </target>\n"
619 " <config>\n"
620 " <cp xmlns=\"urn:tests:a\">\n"
621 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
622 " </cp>\n"
623 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
624 " <a>val_a</a>\n"
625 " <b>val_b</b>\n"
626 " <c>val_c</c>\n"
627 " </l1>\n"
628 " </config>\n"
629 "</edit-config>\n");
630
631 lyd_free_all(tree);
632 lyd_free_all(op);
633
634 /* wrong namespace, element name, whatever... */
635 /* TODO */
636}
637
638static void
639test_netconf_action(void **state)
640{
641 const char *data;
642 struct ly_in *in;
643 struct lyd_node *tree, *op;
644
645 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
646 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
647 "<c xmlns=\"urn:tests:a\">\n"
648 " <act>\n"
649 " <al>value</al>\n"
650 " </act>\n"
651 "</c>\n"
652 "</action>\n"
653 "</rpc>\n";
654 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100655 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, &tree, &op));
Michal Vaskoe0665742021-02-11 11:08:44 +0100656 ly_in_free(in, 0);
657
Radek Krejci8df109d2021-04-23 12:19:08 +0200658 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
659 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "action", 0, 0, 0, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100660
661 assert_non_null(op);
662 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
663 1, 0, 0, 1, "act", LYS_ACTION,
664 1, 0, 0, 1, 0, NULL, 0);
665
666 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
667 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
668 " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
669 "</rpc>\n");
670 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
671 "<act xmlns=\"urn:tests:a\">\n"
672 " <al>value</al>\n"
673 "</act>\n");
674
675 lyd_free_all(tree);
676 lyd_free_all(op);
677
678 /* wrong namespace, element name, whatever... */
679 /* TODO */
680}
681
682static void
683test_netconf_reply_or_notification(void **state)
684{
685 const char *data;
686 struct ly_in *in;
687 struct lyd_node *action, *tree, *op, *op2;
688
689 /* parse the action */
690 data = "<c xmlns=\"urn:tests:a\">\n"
691 " <act>\n"
692 " <al>value</al>\n"
693 " </act>\n"
694 "</c>\n";
695 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100696 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &action, &op));
Michal Vaskoe0665742021-02-11 11:08:44 +0100697 ly_in_free(in, 0);
698
699 /* parse notification first */
700 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
701 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
702 "<c xmlns=\"urn:tests:a\">\n"
703 " <n1>\n"
704 " <nl>value</nl>\n"
705 " </n1>\n"
706 "</c>\n"
707 "</notification>\n";
708 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100709 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_NETCONF, &tree, &op2));
Michal Vaskoe0665742021-02-11 11:08:44 +0100710 ly_in_free(in, 0);
711
Radek Krejci8df109d2021-04-23 12:19:08 +0200712 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_VALUE_XML, "notification", 0, 0, 0, 0, "");
713 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "eventTime", 0, 0, 0, 0,
Michal Vaskoe0665742021-02-11 11:08:44 +0100714 "2010-12-06T08:00:01Z");
715
716 assert_non_null(op2);
717 CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
718
719 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
720 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
721 " <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
722 "</notification>\n");
723 CHECK_LYD_STRING(op2, LYD_PRINT_WITHSIBLINGS,
724 "<n1 xmlns=\"urn:tests:a\">\n"
725 " <nl>value</nl>\n"
726 "</n1>\n");
727
728 lyd_free_all(tree);
729 lyd_free_all(op2);
730
731 /* parse a data reply */
732 data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
733 " <al xmlns=\"urn:tests:a\">25</al>\n"
734 "</rpc-reply>\n";
735 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100736 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, &tree, NULL));
Michal Vaskoe0665742021-02-11 11:08:44 +0100737 ly_in_free(in, 0);
738
Radek Krejci8df109d2021-04-23 12:19:08 +0200739 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 0, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100740
Michal Vaskoe0665742021-02-11 11:08:44 +0100741 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
742 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
Michal Vaskoe0665742021-02-11 11:08:44 +0100743
744 lyd_free_all(tree);
745 /* it was connected to the action, do not free */
746
747 /* parse an ok reply */
748 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
749 " <ok/>\n"
750 "</rpc-reply>\n";
751 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100752 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, &tree, NULL));
Michal Vaskoe0665742021-02-11 11:08:44 +0100753 ly_in_free(in, 0);
754
Radek Krejci8df109d2021-04-23 12:19:08 +0200755 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
756 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_VALUE_XML, "ok", 0, 0, 0, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100757
Michal Vaskoe0665742021-02-11 11:08:44 +0100758 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
759
760 lyd_free_all(tree);
761
762 /* parse an error reply */
763 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
764 " <rpc-error>\n"
765 " <error-type>rpc</error-type>\n"
766 " <error-tag>missing-attribute</error-tag>\n"
767 " <error-severity>error</error-severity>\n"
768 " <error-info>\n"
769 " <bad-attribute>message-id</bad-attribute>\n"
770 " <bad-element>rpc</bad-element>\n"
771 " </error-info>\n"
772 " </rpc-error>\n"
773 "</rpc-reply>\n";
774 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100775 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_XML, LYD_TYPE_REPLY_NETCONF, &tree, NULL));
Michal Vaskoe0665742021-02-11 11:08:44 +0100776 ly_in_free(in, 0);
777
Radek Krejci8df109d2021-04-23 12:19:08 +0200778 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
779 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 1, LY_VALUE_XML, "rpc-error", 0, 0, 0, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100780
Michal Vaskoe0665742021-02-11 11:08:44 +0100781 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
782
783 lyd_free_all(tree);
784
785 lyd_free_all(action);
786
787 /* wrong namespace, element name, whatever... */
788 /* TODO */
789}
790
aPiecek9cdb9e62021-05-18 09:46:20 +0200791static void
Michal Vasko45791ad2021-06-17 08:45:03 +0200792test_filter_attributes(void **state)
793{
794 const char *data;
795 struct ly_in *in;
796 struct lyd_node *tree;
797 const struct lyd_node *node;
798 const char *dsc;
799 const char *ref = "RFC 6241, Section 7.7";
800 const char *feats[] = {"writable-running", NULL};
801
802 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100803 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "notifications", "2008-07-14", NULL)));
Michal Vasko45791ad2021-06-17 08:45:03 +0200804
805 data = "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
806 " <filter type=\"xpath\" select=\"/*\"/>\n"
807 "</get>\n";
808 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
809 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
810 ly_in_free(in, 0);
811 assert_non_null(tree);
812
813 node = tree;
814 dsc = "Retrieve running configuration and device state information.";
815 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
816 1, 0, 0, 1, "get", LYS_RPC,
817 1, 0, 0, 0, 0, ref, 0);
818 node = lyd_child(node);
819 dsc = "This parameter specifies the portion of the system\nconfiguration and state data to retrieve.";
820 CHECK_LYSC_NODE(node->schema, dsc, 1, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "filter", 0, LYS_ANYXML, 1, 0, NULL, 0);
821
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100822 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
823 lyd_free_all(tree);
Michal Vasko45791ad2021-06-17 08:45:03 +0200824
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100825 data = "<create-subscription xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
826 " <filter type=\"subtree\">\n"
827 " <inner-node xmlns=\"my:urn\"/>\n"
828 " </filter>\n"
829 "</create-subscription>\n";
830 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
831 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
832 ly_in_free(in, 0);
833 assert_non_null(tree);
834
835 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko45791ad2021-06-17 08:45:03 +0200836 lyd_free_all(tree);
837}
838
839static void
aPiecek9cdb9e62021-05-18 09:46:20 +0200840test_data_skip(void **state)
841{
842 const char *data;
843 struct lyd_node *tree;
844 struct lyd_node_term *leaf;
845
846 /* add invalid data to a module that is not implemented */
847 data = "<foo xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"><u/></foo>";
848 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
849 assert_null(tree);
850
851 /* add invalid data to a module that is implemented */
852 data = "<fooX xmlns=\"urn:tests:a\"><u/><list><value/></list></fooX>";
853 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
854 assert_null(tree);
855
856 /* first invalid, next valid */
857 data = "<fooX xmlns=\"urn:tests:a\"><u/></fooX> <foo xmlns=\"urn:tests:a\">foo value</foo>";
858 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
859 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
860 leaf = (struct lyd_node_term *)tree;
861 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
862 lyd_free_all(tree);
863}
864
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100865int
866main(void)
Radek Krejci509e2592019-05-15 16:30:48 +0200867{
868 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200869 UTEST(test_leaf, setup),
870 UTEST(test_anydata, setup),
Michal Vasko5b414dd2023-04-13 10:29:58 +0200871 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200872 UTEST(test_list, setup),
873 UTEST(test_container, setup),
874 UTEST(test_opaq, setup),
875 UTEST(test_rpc, setup),
876 UTEST(test_action, setup),
877 UTEST(test_notification, setup),
878 UTEST(test_reply, setup),
Michal Vaskoe0665742021-02-11 11:08:44 +0100879 UTEST(test_netconf_rpc, setup),
880 UTEST(test_netconf_action, setup),
881 UTEST(test_netconf_reply_or_notification, setup),
Michal Vasko45791ad2021-06-17 08:45:03 +0200882 UTEST(test_filter_attributes, setup),
aPiecek9cdb9e62021-05-18 09:46:20 +0200883 UTEST(test_data_skip, setup),
Radek Krejci509e2592019-05-15 16:30:48 +0200884 };
885
886 return cmocka_run_group_tests(tests, NULL, NULL);
887}