blob: d651253bf0fa5b5835dec5c1bfd0a604500fbf16 [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 Vaskodd03ff12023-09-11 10:30:48 +020034 " import ietf-yang-metadata {prefix md;}"
Michal Vaskodbf3e652022-10-21 08:46:25 +020035 " list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
36 " leaf d {type string;}"
37 " container cont {leaf e {type boolean;}}"
38 " }"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039 " leaf foo { type string;}\n"
40 " container c {\n"
41 " leaf x {type string;}\n"
42 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }\n"
43 " notification n1 { leaf nl {type string;}}}\n"
44 " container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}\n"
45 " anydata any {config false;}\n"
Michal Vasko5b414dd2023-04-13 10:29:58 +020046 " anyxml anyx;\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010047 " leaf foo2 { type string; default \"default-val\"; }\n"
48 " leaf foo3 { type uint32; }\n"
Michal Vaskodd03ff12023-09-11 10:30:48 +020049 " notification n2;"
50 " md:annotation attr {type enumeration {enum val;}}"
51 "}";
Radek Krejci509e2592019-05-15 16:30:48 +020052
Radek Iša56ca9e42020-09-08 18:42:00 +020053 UTEST_SETUP;
Radek Krejci509e2592019-05-15 16:30:48 +020054
Radek Iša56ca9e42020-09-08 18:42:00 +020055 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
56 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci509e2592019-05-15 16:30:48 +020057
58 return 0;
59}
60
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
62 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci509e2592019-05-15 16:30:48 +020063
Radek Iša56ca9e42020-09-08 18:42:00 +020064#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
65 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
66 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
67 assert_null(MODEL)
Radek Krejci509e2592019-05-15 16:30:48 +020068
Radek Iša56ca9e42020-09-08 18:42:00 +020069#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
70 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
Radek Krejci509e2592019-05-15 16:30:48 +020071
Radek Krejci509e2592019-05-15 16:30:48 +020072static void
73test_leaf(void **state)
74{
Radek Krejci509e2592019-05-15 16:30:48 +020075 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
76 struct lyd_node *tree;
77 struct lyd_node_term *leaf;
78
Radek Iša56ca9e42020-09-08 18:42:00 +020079 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejci509e2592019-05-15 16:30:48 +020080
Radek Iša56ca9e42020-09-08 18:42:00 +020081 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
82 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
83 leaf = (struct lyd_node_term *)tree;
84 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
85
86 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
87 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010088 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020089 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010090 assert_true(leaf->flags & LYD_DEFAULT);
Radek Krejci509e2592019-05-15 16:30:48 +020091 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +010092
93 /* make foo2 explicit */
94 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020095 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +010096 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010097 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020098 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
99 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100100 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100102 assert_false(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +0100103 lyd_free_all(tree);
104
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 /* parse foo2 but make it implicit, skip metadata xxx from missing schema */
Radek Iša56ca9e42020-09-08 18:42:00 +0200106 data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" "
107 "wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
108 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100109 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100110 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
112 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100113 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200114 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100115 assert_true(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +0100116 lyd_free_all(tree);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200117
118 /* invalid value */
119 data = "<l1 xmlns=\"urn:tests:a\"><a>val-a</a><b>val-b</b><c>1</c><cont><e>0</e></cont></l1>";
120 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
121 "Invalid boolean value \"0\".",
122 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
Radek Krejci509e2592019-05-15 16:30:48 +0200123}
124
Radek Krejciee4cab22019-07-17 17:07:47 +0200125static void
126test_anydata(void **state)
127{
Michal Vasko52927e22020-03-16 17:26:14 +0100128 const char *data;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200129 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200130 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200131
Radek Iša56ca9e42020-09-08 18:42:00 +0200132 data = "<any xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100133 " <element1>\n"
134 " <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
135 " </element1>\n"
136 " <element1a/>\n"
137 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200139 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100140 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
142 1, LYS_ANYDATA, 0, 0, NULL, 0);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200143
Radek Iša56ca9e42020-09-08 18:42:00 +0200144 const char *data_expected =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100145 "<any xmlns=\"urn:tests:a\">\n"
146 " <element1>\n"
147 " <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
148 " </element1>\n"
149 " <element1a/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 "</any>\n";
151
152 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Michal Vasko5b414dd2023-04-13 10:29:58 +0200153
154 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
155 lyd_free_all(tree);
156
157 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
158 free(str);
159 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
160 lyd_free_all(tree);
161}
162
163static void
164test_anyxml(void **state)
165{
166 const char *data;
167 char *str;
168 struct lyd_node *tree;
169
170 data = "<anyx xmlns=\"urn:tests:a\">\n"
171 " <element1>\n"
172 " <element2 x:attr2=\"test\" xmlns:x=\"urn:x\">data</element2>\n"
173 " </element1>\n"
174 " <element1a/>\n"
175 "</anyx>\n";
176 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
177 assert_non_null(tree);
178 tree = tree->next;
179
180 const char *data_expected =
181 "<anyx xmlns=\"urn:tests:a\">\n"
182 " <element1>\n"
183 " <element2 xmlns:x=\"urn:x\" x:attr2=\"test\">data</element2>\n"
184 " </element1>\n"
185 " <element1a/>\n"
186 "</anyx>\n";
187
188 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
189
190 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
191 lyd_free_all(tree);
192
193 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
194 free(str);
195 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Radek Krejciee4cab22019-07-17 17:07:47 +0200196 lyd_free_all(tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200197}
198
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200199static void
200test_list(void **state)
201{
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 const char *data;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200203 struct lyd_node *tree, *iter;
204 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200205 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200206
Radek Krejci710226d2019-07-24 17:24:59 +0200207 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
209 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
210 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
211 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100212 list = (struct lyd_node_inner *)tree;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200213 LY_LIST_FOR(list->child, iter) {
214 assert_int_not_equal(0, iter->hash);
215 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200216 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200217
Michal Vasko9f96a052020-03-10 09:41:45 +0100218 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200219 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 +0200220 "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 +0100221 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
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\"><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 \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100225
Radek Iša56ca9e42020-09-08 18:42:00 +0200226 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 +0200227 "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 +0100228 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +0100229
230 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 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 +0200232 "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200233 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Michal Vasko62af3692023-02-09 14:00:09 +0100234 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
235 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Michal Vasko9f96a052020-03-10 09:41:45 +0100236
Radek Krejci710226d2019-07-24 17:24:59 +0200237 /* keys order */
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 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);
239 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
240 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100241 list = (struct lyd_node_inner *)tree;
242 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 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 +0100244 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 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 +0100246 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200247 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 +0100248 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200249 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 +0200250 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200251 lyd_free_all(tree);
252
Michal Vasko44685da2020-03-17 15:38:06 +0100253 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
255 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 +0100256 list = (struct lyd_node_inner *)tree;
257 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 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 +0100259 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 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 +0100261 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200262 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
263 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Michal Vasko62af3692023-02-09 14:00:09 +0100264 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200265 lyd_free_all(tree);
266
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200268 "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 +0200269}
270
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200271static void
272test_container(void **state)
273{
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200274 struct lyd_node *tree;
275 struct lyd_node_inner *cont;
276
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
278 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 +0100279 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200280 assert_true(cont->flags & LYD_DEFAULT);
281 lyd_free_all(tree);
282
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200284 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100285 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100286 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 +0200287 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100288 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200289 assert_false(cont->flags & LYD_DEFAULT);
290 lyd_free_all(tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200291}
292
Michal Vasko44685da2020-03-17 15:38:06 +0100293static void
294test_opaq(void **state)
295{
Michal Vasko44685da2020-03-17 15:38:06 +0100296 const char *data;
Michal Vasko44685da2020-03-17 15:38:06 +0100297 struct lyd_node *tree;
298
299 /* invalid value, no flags */
300 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200301 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200302 "Invalid type uint32 empty value.", "Schema location \"/a:foo3\", line number 1.");
Michal Vasko44685da2020-03-17 15:38:06 +0100303
304 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200306 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "foo3", 0, 0, NULL, 1, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200307 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
Michal Vasko44685da2020-03-17 15:38:06 +0100308 lyd_free_all(tree);
309
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200310 /* list, opaq flag */
311 data = "<l1 xmlns=\"urn:tests:a\"/>";
312 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200313 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200314 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<l1 xmlns=\"urn:tests:a\"/>\n");
315 lyd_free_all(tree);
316
Michal Vasko44685da2020-03-17 15:38:06 +0100317 /* missing key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 data = "<l1 xmlns=\"urn:tests:a\">\n"
319 " <a>val_a</a>\n"
320 " <b>val_b</b>\n"
321 " <d>val_d</d>\n"
322 "</l1>\n";
323 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200324 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200325 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 5.");
Michal Vasko44685da2020-03-17 15:38:06 +0100326
327 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200328 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200329 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100331 lyd_free_all(tree);
332
333 /* invalid key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200334 data = "<l1 xmlns=\"urn:tests:a\">\n"
335 " <a>val_a</a>\n"
336 " <b>val_b</b>\n"
337 " <c>val_c</c>\n"
338 "</l1>\n";
339 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200340 "Invalid type int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200341 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 4.");
Michal Vasko44685da2020-03-17 15:38:06 +0100342
343 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200344 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200345 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200346 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100347 lyd_free_all(tree);
348
Michal Vasko413c7f22020-05-05 12:34:06 +0200349 /* opaq flag and fail */
Radek Iša56ca9e42020-09-08 18:42:00 +0200350 assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
351 "<a xmlns=\"ns\">\n"
352 " <b>x</b>\n"
Michal Vaskoe137fc42021-07-22 11:53:13 +0200353 " <c xmld:id=\"D\">1</c>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200354 "</a>\n",
355 LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoa878a892023-08-18 12:22:07 +0200356 CHECK_LOG_CTX("Unknown XML prefix \"xmld\".", "Data location \"/a\", line number 3.");
Michal Vasko44685da2020-03-17 15:38:06 +0100357}
358
Michal Vaskob36053d2020-03-26 15:49:30 +0100359static void
360test_rpc(void **state)
361{
Michal Vaskob36053d2020-03-26 15:49:30 +0100362 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200363 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100364 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100365 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200366 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
367 "configuration to the specified target configuration.";
368 const char *ref = "RFC 6241, Section 7.2";
369 const char *feats[] = {"writable-running", NULL};
Michal Vaskob36053d2020-03-26 15:49:30 +0100370
Radek Iša56ca9e42020-09-08 18:42:00 +0200371 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100372
Michal Vasko2552ea32020-12-08 15:32:34 +0100373 data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
374 " <target>\n"
375 " <running/>\n"
376 " </target>\n"
377 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
378 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
379 " <a>val_a</a>\n"
380 " <b>val_b</b>\n"
381 " <c>val_c</c>\n"
382 " </l1>\n"
383 " <cp xmlns=\"urn:tests:a\">\n"
384 " <z nc:operation=\"delete\"/>\n"
385 " </cp>\n"
386 " </config>\n"
387 "</edit-config>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200388 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100389 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 +0200390 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100391
392 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200393
Radek Krejci2a9fc652021-01-22 17:44:34 +0100394 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 1, 0, 0, 1, "edit-config", LYS_RPC,
396 0, 0, 0, 0, 0, ref, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100397
Michal Vaskob36053d2020-03-26 15:49:30 +0100398 assert_non_null(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200399
Michal Vasko2552ea32020-12-08 15:32:34 +0100400 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100401 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200402 1, 0, 0, 1, "edit-config", LYS_RPC,
403 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200404 node = lyd_child(node)->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200405 dsc = "Inline Config content.";
Michal Vaskod1e53b92021-01-28 13:11:06 +0100406 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 +0200407
Michal Vasko1bf09392020-03-27 12:38:10 +0100408 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100409 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 +0200410 1, LYS_CONTAINER, 0, 0, NULL, 0);
411
Radek Krejcia1c1e542020-09-29 16:06:52 +0200412 node = lyd_child(node);
Michal Vasko1bf09392020-03-27 12:38:10 +0100413 /* z has no value */
Michal Vasko535d21c2023-08-09 10:41:44 +0200414 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 1, "");
Michal Vaskob104f112020-07-17 09:54:54 +0200415 node = node->parent->next;
416 /* l1 key c has invalid value so it is at the end */
Michal Vasko535d21c2023-08-09 10:41:44 +0200417 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100418
Radek Iša56ca9e42020-09-08 18:42:00 +0200419 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100420 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
421 " <target>\n"
422 " <running/>\n"
423 " </target>\n"
424 " <config>\n"
425 " <cp xmlns=\"urn:tests:a\">\n"
426 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
427 " </cp>\n"
428 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
429 " <a>val_a</a>\n"
430 " <b>val_b</b>\n"
431 " <c>val_c</c>\n"
432 " </l1>\n"
433 " </config>\n"
434 "</edit-config>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200435
Michal Vaskob36053d2020-03-26 15:49:30 +0100436 lyd_free_all(tree);
437
438 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100439 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100440}
441
442static void
443test_action(void **state)
444{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100445 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100447 struct lyd_node *tree, *op;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100448
Michal Vasko2552ea32020-12-08 15:32:34 +0100449 data = "<c xmlns=\"urn:tests:a\">\n"
450 " <act>\n"
451 " <al>value</al>\n"
452 " </act>\n"
453 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100455 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 +0200456 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100457
458 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100459 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200460 1, 0, 0, 1, "act", LYS_ACTION,
461 1, 0, 0, 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100462
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100464 "<c xmlns=\"urn:tests:a\">\n"
465 " <act>\n"
466 " <al>value</al>\n"
467 " </act>\n"
468 "</c>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200469
Michal Vaskoa8edff02020-03-27 14:47:01 +0100470 lyd_free_all(tree);
471
472 /* wrong namespace, element name, whatever... */
473 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100474}
475
476static void
477test_notification(void **state)
478{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100479 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100481 struct lyd_node *tree, *ntf;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100482
Michal Vasko2552ea32020-12-08 15:32:34 +0100483 data = "<c xmlns=\"urn:tests:a\">\n"
484 " <n1>\n"
485 " <nl>value</nl>\n"
486 " </n1>\n"
487 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200488 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100489 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 +0200490 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100491
492 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100493 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 +0100494
Michal Vasko2552ea32020-12-08 15:32:34 +0100495 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 +0100496
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100498 lyd_free_all(tree);
499
500 /* top-level notif without envelope */
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 data = "<n2 xmlns=\"urn:tests:a\"/>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100503 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 +0200504 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100505
506 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100507 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 +0100508
509 assert_non_null(tree);
510 assert_ptr_equal(ntf, tree);
511
Radek Iša56ca9e42020-09-08 18:42:00 +0200512 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100513 lyd_free_all(tree);
514
515 /* wrong namespace, element name, whatever... */
516 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100517}
518
Michal Vasko1ce933a2020-03-30 12:38:22 +0200519static void
520test_reply(void **state)
521{
Michal Vasko1ce933a2020-03-30 12:38:22 +0200522 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200523 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100524 struct lyd_node *tree, *op;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200525 const struct lyd_node *node;
526
Michal Vasko79135ae2020-12-16 10:08:35 +0100527 data = "<c xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100528 " <act>\n"
Michal Vasko2552ea32020-12-08 15:32:34 +0100529 " <al>25</al>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100530 " </act>\n"
531 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200532 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100533 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 +0200534 ly_in_free(in, 0);
535
Michal Vasko1ce933a2020-03-30 12:38:22 +0200536 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200537
Radek Krejci2a9fc652021-01-22 17:44:34 +0100538 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200539 1, 0, 0, 1, "act", LYS_ACTION,
540 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200541 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100542 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 +0200543
Michal Vasko2552ea32020-12-08 15:32:34 +0100544 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 +0200545
546 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200547 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_WITHSIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
Michal Vasko1ce933a2020-03-30 12:38:22 +0200548 lyd_free_all(tree);
549
550 /* wrong namespace, element name, whatever... */
551 /* TODO */
Michal Vasko1ce933a2020-03-30 12:38:22 +0200552}
553
Michal Vaskoe0665742021-02-11 11:08:44 +0100554static void
555test_netconf_rpc(void **state)
556{
557 const char *data;
558 struct ly_in *in;
559 struct lyd_node *tree, *op;
560 const struct lyd_node *node;
561 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
562 "configuration to the specified target configuration.";
563 const char *ref = "RFC 6241, Section 7.2";
564 const char *feats[] = {"writable-running", NULL};
565
566 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
567
568 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
569 "<edit-config>\n"
570 " <target>\n"
571 " <running/>\n"
572 " </target>\n"
573 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
574 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
575 " <a>val_a</a>\n"
576 " <b>val_b</b>\n"
577 " <c>val_c</c>\n"
578 " </l1>\n"
579 " <cp xmlns=\"urn:tests:a\">\n"
580 " <z nc:operation=\"delete\"/>\n"
581 " </cp>\n"
582 " </config>\n"
583 "</edit-config>\n"
584 "</rpc>\n";
585 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100586 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 +0100587 ly_in_free(in, 0);
588
589 assert_non_null(op);
590
591 node = tree;
Radek Krejci8df109d2021-04-23 12:19:08 +0200592 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 +0100593
594 assert_non_null(tree);
595
596 node = op;
597 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
598 1, 0, 0, 1, "edit-config", LYS_RPC,
599 0, 0, 0, 0, 0, ref, 0);
600 node = lyd_child(node)->next;
601 dsc = "Inline Config content.";
602 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
603
604 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100605 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Michal Vaskoe0665742021-02-11 11:08:44 +0100606 1, LYS_CONTAINER, 0, 0, NULL, 0);
607
608 node = lyd_child(node);
609 /* z has no value */
Michal Vasko535d21c2023-08-09 10:41:44 +0200610 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 1, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100611 node = node->parent->next;
612 /* l1 key c has invalid value so it is at the end */
Michal Vasko535d21c2023-08-09 10:41:44 +0200613 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 1, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100614
615 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
616 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
617 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
618 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
619 " <target>\n"
620 " <running/>\n"
621 " </target>\n"
622 " <config>\n"
623 " <cp xmlns=\"urn:tests:a\">\n"
624 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
625 " </cp>\n"
626 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
627 " <a>val_a</a>\n"
628 " <b>val_b</b>\n"
629 " <c>val_c</c>\n"
630 " </l1>\n"
631 " </config>\n"
632 "</edit-config>\n");
633
634 lyd_free_all(tree);
635 lyd_free_all(op);
636
Michal Vasko85be65e2023-06-13 09:44:17 +0200637 /* invalid anyxml nested metadata value */
638 data = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"1\" pid=\"4114692032\">\n"
639 " <copy-config>\n"
640 " <target>\n"
641 " <running/>\n"
642 " </target>\n"
643 " <source>\n"
644 " <config>\n"
645 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
646 " <a>val_a</a>\n"
647 " <b>val_b</b>\n"
648 " <c>5</c>\n"
649 " <cont nc:operation=\"merge\">\n"
650 " <e nc:operation=\"merge2\">false</e>\n"
651 " </cont>\n"
652 " </l1>\n"
653 " </config>\n"
654 " </source>\n"
655 " </copy-config>\n"
656 "</rpc>\n";
657 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
658 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, &tree, &op));
659 ly_in_free(in, 0);
660 CHECK_LOG_CTX("Invalid enumeration value \"merge2\".",
Michal Vaskodd03ff12023-09-11 10:30:48 +0200661 "Path \"/ietf-netconf:copy-config/source/config/a:l1[a='val_a'][b='val_b'][c='5']/cont/e/@ietf-netconf:operation\", line number 13.");
Michal Vasko85be65e2023-06-13 09:44:17 +0200662 lyd_free_all(tree);
663 assert_null(op);
Michal Vaskoe0665742021-02-11 11:08:44 +0100664}
665
666static void
667test_netconf_action(void **state)
668{
669 const char *data;
670 struct ly_in *in;
671 struct lyd_node *tree, *op;
672
673 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
674 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
675 "<c xmlns=\"urn:tests:a\">\n"
676 " <act>\n"
677 " <al>value</al>\n"
678 " </act>\n"
679 "</c>\n"
680 "</action>\n"
681 "</rpc>\n";
682 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100683 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 +0100684 ly_in_free(in, 0);
685
Radek Krejci8df109d2021-04-23 12:19:08 +0200686 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
687 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 +0100688
689 assert_non_null(op);
690 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
691 1, 0, 0, 1, "act", LYS_ACTION,
692 1, 0, 0, 1, 0, NULL, 0);
693
694 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
695 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
696 " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
697 "</rpc>\n");
698 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
699 "<act xmlns=\"urn:tests:a\">\n"
700 " <al>value</al>\n"
701 "</act>\n");
702
703 lyd_free_all(tree);
704 lyd_free_all(op);
705
706 /* wrong namespace, element name, whatever... */
707 /* TODO */
708}
709
710static void
711test_netconf_reply_or_notification(void **state)
712{
713 const char *data;
714 struct ly_in *in;
715 struct lyd_node *action, *tree, *op, *op2;
716
717 /* parse the action */
718 data = "<c xmlns=\"urn:tests:a\">\n"
719 " <act>\n"
720 " <al>value</al>\n"
721 " </act>\n"
722 "</c>\n";
723 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100724 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 +0100725 ly_in_free(in, 0);
726
727 /* parse notification first */
728 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
729 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
730 "<c xmlns=\"urn:tests:a\">\n"
731 " <n1>\n"
732 " <nl>value</nl>\n"
733 " </n1>\n"
734 "</c>\n"
735 "</notification>\n";
736 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100737 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 +0100738 ly_in_free(in, 0);
739
Radek Krejci8df109d2021-04-23 12:19:08 +0200740 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_VALUE_XML, "notification", 0, 0, 0, 0, "");
741 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 +0100742 "2010-12-06T08:00:01Z");
743
744 assert_non_null(op2);
745 CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
746
747 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
748 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
749 " <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
750 "</notification>\n");
751 CHECK_LYD_STRING(op2, LYD_PRINT_WITHSIBLINGS,
752 "<n1 xmlns=\"urn:tests:a\">\n"
753 " <nl>value</nl>\n"
754 "</n1>\n");
755
756 lyd_free_all(tree);
757 lyd_free_all(op2);
758
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200759 /* notification with a different order */
760 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
761 "<c xmlns=\"urn:tests:a\">\n"
762 " <n1>\n"
763 " <nl>value</nl>\n"
764 " </n1>\n"
765 "</c>\n"
766 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
767 "</notification>\n";
768 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
769 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_NETCONF, &tree, &op2));
770 ly_in_free(in, 0);
771
772 lyd_free_all(tree);
773 lyd_free_all(op2);
774
Michal Vaskoe0665742021-02-11 11:08:44 +0100775 /* parse a data reply */
776 data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
777 " <al xmlns=\"urn:tests:a\">25</al>\n"
778 "</rpc-reply>\n";
779 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100780 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 +0100781 ly_in_free(in, 0);
782
Radek Krejci8df109d2021-04-23 12:19:08 +0200783 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 +0100784
Michal Vaskoe0665742021-02-11 11:08:44 +0100785 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
786 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
Michal Vaskoe0665742021-02-11 11:08:44 +0100787
788 lyd_free_all(tree);
789 /* it was connected to the action, do not free */
790
791 /* parse an ok reply */
792 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
793 " <ok/>\n"
794 "</rpc-reply>\n";
795 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100796 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 +0100797 ly_in_free(in, 0);
798
Radek Krejci8df109d2021-04-23 12:19:08 +0200799 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
800 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 +0100801
Michal Vaskoe0665742021-02-11 11:08:44 +0100802 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
803
804 lyd_free_all(tree);
805
806 /* parse an error reply */
807 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
808 " <rpc-error>\n"
809 " <error-type>rpc</error-type>\n"
810 " <error-tag>missing-attribute</error-tag>\n"
811 " <error-severity>error</error-severity>\n"
812 " <error-info>\n"
813 " <bad-attribute>message-id</bad-attribute>\n"
814 " <bad-element>rpc</bad-element>\n"
815 " </error-info>\n"
816 " </rpc-error>\n"
817 "</rpc-reply>\n";
818 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100819 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 +0100820 ly_in_free(in, 0);
821
Radek Krejci8df109d2021-04-23 12:19:08 +0200822 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
823 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 +0100824
Michal Vaskoe0665742021-02-11 11:08:44 +0100825 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
826
827 lyd_free_all(tree);
828
829 lyd_free_all(action);
830
831 /* wrong namespace, element name, whatever... */
832 /* TODO */
833}
834
aPiecek9cdb9e62021-05-18 09:46:20 +0200835static void
Michal Vasko820efe82023-05-12 15:47:43 +0200836test_restconf_rpc(void **state)
837{
838 const char *data;
839 struct ly_in *in;
840 struct lyd_node *tree, *envp;
841
842 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
843
844 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
845
846 data = "<input xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
847 "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
848 "<config>"
849 "<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
850 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
851 "<a>val_a</a><b>val_b</b><c>val_c</c>"
852 "</l1>"
853 "</config></input>";
854 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
855 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_XML, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
856 ly_in_free(in, 0);
857
858 /* the same just connected to the edit-data RPC */
859 data = "<edit-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
860 "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
861 "<config>"
862 "<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
863 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
864 "<a>val_a</a><b>val_b</b><c>val_c</c>"
865 "</l1>"
866 "</config></edit-data>";
867 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
868 lyd_free_all(tree);
869 lyd_free_all(envp);
870}
871
872static void
873test_restconf_reply(void **state)
874{
875 const char *data;
876 struct ly_in *in;
877 struct lyd_node *tree, *envp;
878
879 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
880
881 data = "<output xmlns=\"urn:tests:a\"><al>25</al></output>";
882 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
883 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_XML, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
884 ly_in_free(in, 0);
885
886 /* connected to the RPC with the parent */
887 data = "<c xmlns=\"urn:tests:a\"><act><al>25</al></act></c>";
888 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
889 lyd_free_all(tree);
890 lyd_free_all(envp);
891}
892
893static void
Michal Vasko45791ad2021-06-17 08:45:03 +0200894test_filter_attributes(void **state)
895{
896 const char *data;
897 struct ly_in *in;
898 struct lyd_node *tree;
899 const struct lyd_node *node;
900 const char *dsc;
901 const char *ref = "RFC 6241, Section 7.7";
902 const char *feats[] = {"writable-running", NULL};
903
904 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100905 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "notifications", "2008-07-14", NULL)));
Michal Vasko45791ad2021-06-17 08:45:03 +0200906
907 data = "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
908 " <filter type=\"xpath\" select=\"/*\"/>\n"
909 "</get>\n";
910 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
911 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
912 ly_in_free(in, 0);
913 assert_non_null(tree);
914
915 node = tree;
916 dsc = "Retrieve running configuration and device state information.";
917 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
918 1, 0, 0, 1, "get", LYS_RPC,
919 1, 0, 0, 0, 0, ref, 0);
920 node = lyd_child(node);
921 dsc = "This parameter specifies the portion of the system\nconfiguration and state data to retrieve.";
922 CHECK_LYSC_NODE(node->schema, dsc, 1, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "filter", 0, LYS_ANYXML, 1, 0, NULL, 0);
923
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100924 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
925 lyd_free_all(tree);
Michal Vasko45791ad2021-06-17 08:45:03 +0200926
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100927 data = "<create-subscription xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
928 " <filter type=\"subtree\">\n"
929 " <inner-node xmlns=\"my:urn\"/>\n"
930 " </filter>\n"
931 "</create-subscription>\n";
932 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
933 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
934 ly_in_free(in, 0);
935 assert_non_null(tree);
936
937 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko45791ad2021-06-17 08:45:03 +0200938 lyd_free_all(tree);
939}
940
941static void
aPiecek9cdb9e62021-05-18 09:46:20 +0200942test_data_skip(void **state)
943{
944 const char *data;
945 struct lyd_node *tree;
946 struct lyd_node_term *leaf;
947
948 /* add invalid data to a module that is not implemented */
949 data = "<foo xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"><u/></foo>";
950 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
951 assert_null(tree);
952
953 /* add invalid data to a module that is implemented */
954 data = "<fooX xmlns=\"urn:tests:a\"><u/><list><value/></list></fooX>";
955 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
956 assert_null(tree);
957
958 /* first invalid, next valid */
959 data = "<fooX xmlns=\"urn:tests:a\"><u/></fooX> <foo xmlns=\"urn:tests:a\">foo value</foo>";
960 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
961 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
962 leaf = (struct lyd_node_term *)tree;
963 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
964 lyd_free_all(tree);
965}
966
Michal Vaskodd03ff12023-09-11 10:30:48 +0200967static void
968test_metadata(void **state)
969{
970 const char *data;
971 struct lyd_node *tree;
972
973 /* invalid metadata value */
974 data = "<c xmlns=\"urn:tests:a\" xmlns:a=\"urn:tests:a\"><x a:attr=\"value\">xval</x></c>";
975 assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
976 assert_null(tree);
977 CHECK_LOG_CTX("Invalid enumeration value \"value\".", "Path \"/a:c/x/@a:attr\", line number 1.");
978}
979
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100980int
981main(void)
Radek Krejci509e2592019-05-15 16:30:48 +0200982{
983 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200984 UTEST(test_leaf, setup),
985 UTEST(test_anydata, setup),
Michal Vasko5b414dd2023-04-13 10:29:58 +0200986 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200987 UTEST(test_list, setup),
988 UTEST(test_container, setup),
989 UTEST(test_opaq, setup),
990 UTEST(test_rpc, setup),
991 UTEST(test_action, setup),
992 UTEST(test_notification, setup),
993 UTEST(test_reply, setup),
Michal Vaskoe0665742021-02-11 11:08:44 +0100994 UTEST(test_netconf_rpc, setup),
995 UTEST(test_netconf_action, setup),
996 UTEST(test_netconf_reply_or_notification, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200997 UTEST(test_restconf_rpc, setup),
998 UTEST(test_restconf_reply, setup),
Michal Vasko45791ad2021-06-17 08:45:03 +0200999 UTEST(test_filter_attributes, setup),
aPiecek9cdb9e62021-05-18 09:46:20 +02001000 UTEST(test_data_skip, setup),
Michal Vaskodd03ff12023-09-11 10:30:48 +02001001 UTEST(test_metadata, setup),
Radek Krejci509e2592019-05-15 16:30:48 +02001002 };
1003
1004 return cmocka_run_group_tests(tests, NULL, NULL);
1005}