blob: d5336c03073177840e3c75cd3c9811f3827d1cbe [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
Michal Vasko7a266772024-01-23 11:02:38 +010064#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH, ERR_LINE) \
Radek Iša56ca9e42020-09-08 18:42:00 +020065 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
Michal Vasko7a266772024-01-23 11:02:38 +010066 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH, ERR_LINE);\
Radek Iša56ca9e42020-09-08 18:42:00 +020067 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,
Michal Vasko7a266772024-01-23 11:02:38 +0100121 "Invalid boolean value \"0\".", "/a:l1[a='val-a'][b='val-b'][c='1']/cont/e", 1);
Radek Krejci509e2592019-05-15 16:30:48 +0200122}
123
Radek Krejciee4cab22019-07-17 17:07:47 +0200124static void
125test_anydata(void **state)
126{
Michal Vasko52927e22020-03-16 17:26:14 +0100127 const char *data;
Michal Vasko5b414dd2023-04-13 10:29:58 +0200128 char *str;
Radek Krejciee4cab22019-07-17 17:07:47 +0200129 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200130
Radek Iša56ca9e42020-09-08 18:42:00 +0200131 data = "<any xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100132 " <element1>\n"
133 " <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
134 " </element1>\n"
135 " <element1a/>\n"
136 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200137 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200138 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100139 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200140 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
141 1, LYS_ANYDATA, 0, 0, NULL, 0);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200142
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 const char *data_expected =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100144 "<any xmlns=\"urn:tests:a\">\n"
145 " <element1>\n"
146 " <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
147 " </element1>\n"
148 " <element1a/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200149 "</any>\n";
150
151 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Michal Vasko5b414dd2023-04-13 10:29:58 +0200152
153 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
154 lyd_free_all(tree);
155
156 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
157 free(str);
158 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
159 lyd_free_all(tree);
160}
161
162static void
163test_anyxml(void **state)
164{
165 const char *data;
166 char *str;
167 struct lyd_node *tree;
168
169 data = "<anyx xmlns=\"urn:tests:a\">\n"
170 " <element1>\n"
171 " <element2 x:attr2=\"test\" xmlns:x=\"urn:x\">data</element2>\n"
172 " </element1>\n"
173 " <element1a/>\n"
174 "</anyx>\n";
175 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
176 assert_non_null(tree);
177 tree = tree->next;
178
179 const char *data_expected =
180 "<anyx xmlns=\"urn:tests:a\">\n"
181 " <element1>\n"
182 " <element2 xmlns:x=\"urn:x\" x:attr2=\"test\">data</element2>\n"
183 " </element1>\n"
184 " <element1a/>\n"
185 "</anyx>\n";
186
187 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
188
189 assert_int_equal(LY_SUCCESS, lyd_any_value_str(tree, &str));
190 lyd_free_all(tree);
191
192 assert_int_equal(LY_SUCCESS, lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", str, strlen(str), LYD_ANYDATA_XML, 0, &tree, NULL));
193 free(str);
194 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Radek Krejciee4cab22019-07-17 17:07:47 +0200195 lyd_free_all(tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200196}
197
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200198static void
199test_list(void **state)
200{
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 const char *data;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200202 struct lyd_node *tree, *iter;
203 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200204 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200205
Radek Krejci710226d2019-07-24 17:24:59 +0200206 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
208 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
209 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
210 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100211 list = (struct lyd_node_inner *)tree;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200212 LY_LIST_FOR(list->child, iter) {
213 assert_int_not_equal(0, iter->hash);
214 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200215 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200216
Michal Vasko9f96a052020-03-10 09:41:45 +0100217 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100219 "List instance is missing its key \"a\".", "/a:l1[b='b'][c='1']", 1);
220 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +0100221
Radek Iša56ca9e42020-09-08 18:42:00 +0200222 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100223 "List instance is missing its key \"b\".", "/a:l1[a='a']", 1);
Michal Vasko9f96a052020-03-10 09:41:45 +0100224
Radek Iša56ca9e42020-09-08 18:42:00 +0200225 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100226 "List instance is missing its key \"c\".", "/a:l1[a='a'][b='b']", 1);
227 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +0100228
229 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 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 Vasko7a266772024-01-23 11:02:38 +0100231 "Duplicate instance of \"c\".", "/a:l1[a='a'][b='b'][c='1'][c='1']/c", 1);
232 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
233 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
Michal Vasko9f96a052020-03-10 09:41:45 +0100234
Radek Krejci710226d2019-07-24 17:24:59 +0200235 /* keys order */
Radek Iša56ca9e42020-09-08 18:42:00 +0200236 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);
237 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
238 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100239 list = (struct lyd_node_inner *)tree;
240 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 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 +0100242 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
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, "b", 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, "c", 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);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200247 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0);
Michal Vasko7a266772024-01-23 11:02:38 +0100248 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
Radek Krejci710226d2019-07-24 17:24:59 +0200249 lyd_free_all(tree);
250
Michal Vasko44685da2020-03-17 15:38:06 +0100251 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
253 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 +0100254 list = (struct lyd_node_inner *)tree;
255 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 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 +0100257 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
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, "b", 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, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
Michal Vasko7a266772024-01-23 11:02:38 +0100261 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL, 0);
262 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL, 0);
Radek Krejci710226d2019-07-24 17:24:59 +0200263 lyd_free_all(tree);
264
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100266 "Invalid position of the key \"b\" in a list.", "/a:l1[c='1']/b", 1);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200267}
268
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200269static void
270test_container(void **state)
271{
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200272 struct lyd_node *tree;
273 struct lyd_node_inner *cont;
274
Radek Iša56ca9e42020-09-08 18:42:00 +0200275 CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
276 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 +0100277 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200278 assert_true(cont->flags & LYD_DEFAULT);
279 lyd_free_all(tree);
280
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200282 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100283 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100284 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 +0200285 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100286 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200287 assert_false(cont->flags & LYD_DEFAULT);
288 lyd_free_all(tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200289}
290
Michal Vasko44685da2020-03-17 15:38:06 +0100291static void
292test_opaq(void **state)
293{
Michal Vasko44685da2020-03-17 15:38:06 +0100294 const char *data;
Michal Vasko44685da2020-03-17 15:38:06 +0100295 struct lyd_node *tree;
296
297 /* invalid value, no flags */
298 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200299 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100300 "Invalid type uint32 empty value.", "/a:foo3", 1);
Michal Vasko44685da2020-03-17 15:38:06 +0100301
302 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200304 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 +0200305 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
Michal Vasko44685da2020-03-17 15:38:06 +0100306 lyd_free_all(tree);
307
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200308 /* list, opaq flag */
309 data = "<l1 xmlns=\"urn:tests:a\"/>";
310 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200311 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 +0200312 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<l1 xmlns=\"urn:tests:a\"/>\n");
313 lyd_free_all(tree);
314
Michal Vasko44685da2020-03-17 15:38:06 +0100315 /* missing key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 data = "<l1 xmlns=\"urn:tests:a\">\n"
317 " <a>val_a</a>\n"
318 " <b>val_b</b>\n"
319 " <d>val_d</d>\n"
320 "</l1>\n";
321 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100322 "List instance is missing its key \"c\".", "/a:l1[a='val_a'][b='val_b']", 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);
Michal Vasko535d21c2023-08-09 10:41:44 +0200326 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 +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 Vasko7a266772024-01-23 11:02:38 +0100337 "Invalid type int16 value \"val_c\".", "/a:l1[a='val_a'][b='val_b']/c", 4);
Michal Vasko44685da2020-03-17 15:38:06 +0100338
339 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Michal Vasko535d21c2023-08-09 10:41:44 +0200341 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 +0200342 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100343 lyd_free_all(tree);
344
Michal Vasko413c7f22020-05-05 12:34:06 +0200345 /* opaq flag and fail */
Radek Iša56ca9e42020-09-08 18:42:00 +0200346 assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
347 "<a xmlns=\"ns\">\n"
348 " <b>x</b>\n"
Michal Vaskoe137fc42021-07-22 11:53:13 +0200349 " <c xmld:id=\"D\">1</c>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200350 "</a>\n",
351 LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko7a266772024-01-23 11:02:38 +0100352 CHECK_LOG_CTX("Unknown XML prefix \"xmld\".", "/a", 3);
Michal Vasko44685da2020-03-17 15:38:06 +0100353}
354
Michal Vaskob36053d2020-03-26 15:49:30 +0100355static void
356test_rpc(void **state)
357{
Michal Vaskob36053d2020-03-26 15:49:30 +0100358 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200359 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100360 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100361 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200362 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
363 "configuration to the specified target configuration.";
364 const char *ref = "RFC 6241, Section 7.2";
365 const char *feats[] = {"writable-running", NULL};
Michal Vaskob36053d2020-03-26 15:49:30 +0100366
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100368
Michal Vasko2552ea32020-12-08 15:32:34 +0100369 data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
370 " <target>\n"
371 " <running/>\n"
372 " </target>\n"
373 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
374 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
375 " <a>val_a</a>\n"
376 " <b>val_b</b>\n"
377 " <c>val_c</c>\n"
378 " </l1>\n"
379 " <cp xmlns=\"urn:tests:a\">\n"
380 " <z nc:operation=\"delete\"/>\n"
381 " </cp>\n"
382 " </config>\n"
383 "</edit-config>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200384 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100385 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 +0200386 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100387
388 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200389
Radek Krejci2a9fc652021-01-22 17:44:34 +0100390 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200391 1, 0, 0, 1, "edit-config", LYS_RPC,
392 0, 0, 0, 0, 0, ref, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100393
Michal Vaskob36053d2020-03-26 15:49:30 +0100394 assert_non_null(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200395
Michal Vasko2552ea32020-12-08 15:32:34 +0100396 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100397 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200398 1, 0, 0, 1, "edit-config", LYS_RPC,
399 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200400 node = lyd_child(node)->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200401 dsc = "Inline Config content.";
Michal Vaskod1e53b92021-01-28 13:11:06 +0100402 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 +0200403
Michal Vasko1bf09392020-03-27 12:38:10 +0100404 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100405 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 +0200406 1, LYS_CONTAINER, 0, 0, NULL, 0);
407
Radek Krejcia1c1e542020-09-29 16:06:52 +0200408 node = lyd_child(node);
Michal Vasko1bf09392020-03-27 12:38:10 +0100409 /* z has no value */
Michal Vasko535d21c2023-08-09 10:41:44 +0200410 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 +0200411 node = node->parent->next;
412 /* l1 key c has invalid value so it is at the end */
Michal Vasko535d21c2023-08-09 10:41:44 +0200413 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 +0100414
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100416 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
417 " <target>\n"
418 " <running/>\n"
419 " </target>\n"
420 " <config>\n"
421 " <cp xmlns=\"urn:tests:a\">\n"
422 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
423 " </cp>\n"
424 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
425 " <a>val_a</a>\n"
426 " <b>val_b</b>\n"
427 " <c>val_c</c>\n"
428 " </l1>\n"
429 " </config>\n"
430 "</edit-config>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200431
Michal Vaskob36053d2020-03-26 15:49:30 +0100432 lyd_free_all(tree);
433
434 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100435 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100436}
437
438static void
439test_action(void **state)
440{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100441 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100443 struct lyd_node *tree, *op;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100444
Michal Vasko2552ea32020-12-08 15:32:34 +0100445 data = "<c xmlns=\"urn:tests:a\">\n"
446 " <act>\n"
447 " <al>value</al>\n"
448 " </act>\n"
449 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100451 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 +0200452 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100453
454 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100455 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 1, 0, 0, 1, "act", LYS_ACTION,
457 1, 0, 0, 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100458
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100460 "<c xmlns=\"urn:tests:a\">\n"
461 " <act>\n"
462 " <al>value</al>\n"
463 " </act>\n"
464 "</c>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200465
Michal Vaskoa8edff02020-03-27 14:47:01 +0100466 lyd_free_all(tree);
467
468 /* wrong namespace, element name, whatever... */
469 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100470}
471
472static void
473test_notification(void **state)
474{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100475 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200476 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100477 struct lyd_node *tree, *ntf;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100478
Michal Vasko2552ea32020-12-08 15:32:34 +0100479 data = "<c xmlns=\"urn:tests:a\">\n"
480 " <n1>\n"
481 " <nl>value</nl>\n"
482 " </n1>\n"
483 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100485 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 +0200486 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100487
488 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100489 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 +0100490
Michal Vasko2552ea32020-12-08 15:32:34 +0100491 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 +0100492
Radek Iša56ca9e42020-09-08 18:42:00 +0200493 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100494 lyd_free_all(tree);
495
496 /* top-level notif without envelope */
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 data = "<n2 xmlns=\"urn:tests:a\"/>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200498 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100499 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 +0200500 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100501
502 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100503 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 +0100504
505 assert_non_null(tree);
506 assert_ptr_equal(ntf, tree);
507
Radek Iša56ca9e42020-09-08 18:42:00 +0200508 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100509 lyd_free_all(tree);
510
511 /* wrong namespace, element name, whatever... */
512 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100513}
514
Michal Vasko1ce933a2020-03-30 12:38:22 +0200515static void
516test_reply(void **state)
517{
Michal Vasko1ce933a2020-03-30 12:38:22 +0200518 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200519 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100520 struct lyd_node *tree, *op;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200521 const struct lyd_node *node;
522
Michal Vasko79135ae2020-12-16 10:08:35 +0100523 data = "<c xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100524 " <act>\n"
Michal Vasko2552ea32020-12-08 15:32:34 +0100525 " <al>25</al>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100526 " </act>\n"
527 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200528 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100529 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 +0200530 ly_in_free(in, 0);
531
Michal Vasko1ce933a2020-03-30 12:38:22 +0200532 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200533
Radek Krejci2a9fc652021-01-22 17:44:34 +0100534 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200535 1, 0, 0, 1, "act", LYS_ACTION,
536 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200537 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100538 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 +0200539
Michal Vasko2552ea32020-12-08 15:32:34 +0100540 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 +0200541
542 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200543 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_WITHSIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
Michal Vasko1ce933a2020-03-30 12:38:22 +0200544 lyd_free_all(tree);
545
546 /* wrong namespace, element name, whatever... */
547 /* TODO */
Michal Vasko1ce933a2020-03-30 12:38:22 +0200548}
549
Michal Vaskoe0665742021-02-11 11:08:44 +0100550static void
551test_netconf_rpc(void **state)
552{
553 const char *data;
554 struct ly_in *in;
555 struct lyd_node *tree, *op;
556 const struct lyd_node *node;
557 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
558 "configuration to the specified target configuration.";
559 const char *ref = "RFC 6241, Section 7.2";
560 const char *feats[] = {"writable-running", NULL};
561
562 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
563
564 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
565 "<edit-config>\n"
566 " <target>\n"
567 " <running/>\n"
568 " </target>\n"
569 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
570 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
571 " <a>val_a</a>\n"
572 " <b>val_b</b>\n"
573 " <c>val_c</c>\n"
574 " </l1>\n"
575 " <cp xmlns=\"urn:tests:a\">\n"
576 " <z nc:operation=\"delete\"/>\n"
577 " </cp>\n"
578 " </config>\n"
579 "</edit-config>\n"
580 "</rpc>\n";
581 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100582 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 +0100583 ly_in_free(in, 0);
584
585 assert_non_null(op);
586
587 node = tree;
Radek Krejci8df109d2021-04-23 12:19:08 +0200588 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 +0100589
590 assert_non_null(tree);
591
592 node = op;
593 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
594 1, 0, 0, 1, "edit-config", LYS_RPC,
595 0, 0, 0, 0, 0, ref, 0);
596 node = lyd_child(node)->next;
597 dsc = "Inline Config content.";
598 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
599
600 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100601 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Michal Vaskoe0665742021-02-11 11:08:44 +0100602 1, LYS_CONTAINER, 0, 0, NULL, 0);
603
604 node = lyd_child(node);
605 /* z has no value */
Michal Vasko535d21c2023-08-09 10:41:44 +0200606 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 +0100607 node = node->parent->next;
608 /* l1 key c has invalid value so it is at the end */
Michal Vasko535d21c2023-08-09 10:41:44 +0200609 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 +0100610
611 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
612 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
613 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
614 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
615 " <target>\n"
616 " <running/>\n"
617 " </target>\n"
618 " <config>\n"
619 " <cp xmlns=\"urn:tests:a\">\n"
620 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
621 " </cp>\n"
622 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
623 " <a>val_a</a>\n"
624 " <b>val_b</b>\n"
625 " <c>val_c</c>\n"
626 " </l1>\n"
627 " </config>\n"
628 "</edit-config>\n");
629
630 lyd_free_all(tree);
631 lyd_free_all(op);
632
Michal Vasko85be65e2023-06-13 09:44:17 +0200633 /* invalid anyxml nested metadata value */
634 data = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"1\" pid=\"4114692032\">\n"
635 " <copy-config>\n"
636 " <target>\n"
637 " <running/>\n"
638 " </target>\n"
639 " <source>\n"
640 " <config>\n"
641 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
642 " <a>val_a</a>\n"
643 " <b>val_b</b>\n"
644 " <c>5</c>\n"
645 " <cont nc:operation=\"merge\">\n"
646 " <e nc:operation=\"merge2\">false</e>\n"
647 " </cont>\n"
648 " </l1>\n"
649 " </config>\n"
650 " </source>\n"
651 " </copy-config>\n"
652 "</rpc>\n";
653 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
654 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_NETCONF, &tree, &op));
655 ly_in_free(in, 0);
656 CHECK_LOG_CTX("Invalid enumeration value \"merge2\".",
Michal Vasko7a266772024-01-23 11:02:38 +0100657 "/ietf-netconf:copy-config/source/config/a:l1[a='val_a'][b='val_b'][c='5']/cont/e/@ietf-netconf:operation", 13);
Michal Vasko85be65e2023-06-13 09:44:17 +0200658 lyd_free_all(tree);
659 assert_null(op);
Michal Vaskoe0665742021-02-11 11:08:44 +0100660}
661
662static void
663test_netconf_action(void **state)
664{
665 const char *data;
666 struct ly_in *in;
667 struct lyd_node *tree, *op;
668
669 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
670 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
671 "<c xmlns=\"urn:tests:a\">\n"
672 " <act>\n"
673 " <al>value</al>\n"
674 " </act>\n"
675 "</c>\n"
676 "</action>\n"
677 "</rpc>\n";
678 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100679 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 +0100680 ly_in_free(in, 0);
681
Radek Krejci8df109d2021-04-23 12:19:08 +0200682 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
683 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 +0100684
685 assert_non_null(op);
686 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
687 1, 0, 0, 1, "act", LYS_ACTION,
688 1, 0, 0, 1, 0, NULL, 0);
689
690 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
691 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
692 " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
693 "</rpc>\n");
694 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
695 "<act xmlns=\"urn:tests:a\">\n"
696 " <al>value</al>\n"
697 "</act>\n");
698
699 lyd_free_all(tree);
700 lyd_free_all(op);
701
702 /* wrong namespace, element name, whatever... */
703 /* TODO */
704}
705
706static void
707test_netconf_reply_or_notification(void **state)
708{
709 const char *data;
710 struct ly_in *in;
711 struct lyd_node *action, *tree, *op, *op2;
712
713 /* parse the action */
714 data = "<c xmlns=\"urn:tests:a\">\n"
715 " <act>\n"
716 " <al>value</al>\n"
717 " </act>\n"
718 "</c>\n";
719 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100720 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 +0100721 ly_in_free(in, 0);
722
723 /* parse notification first */
724 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
725 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
726 "<c xmlns=\"urn:tests:a\">\n"
727 " <n1>\n"
728 " <nl>value</nl>\n"
729 " </n1>\n"
730 "</c>\n"
731 "</notification>\n";
732 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100733 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 +0100734 ly_in_free(in, 0);
735
Radek Krejci8df109d2021-04-23 12:19:08 +0200736 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_VALUE_XML, "notification", 0, 0, 0, 0, "");
737 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 +0100738 "2010-12-06T08:00:01Z");
739
740 assert_non_null(op2);
741 CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
742
743 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
744 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
745 " <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
746 "</notification>\n");
747 CHECK_LYD_STRING(op2, LYD_PRINT_WITHSIBLINGS,
748 "<n1 xmlns=\"urn:tests:a\">\n"
749 " <nl>value</nl>\n"
750 "</n1>\n");
751
752 lyd_free_all(tree);
753 lyd_free_all(op2);
754
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200755 /* notification with a different order */
756 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
757 "<c xmlns=\"urn:tests:a\">\n"
758 " <n1>\n"
759 " <nl>value</nl>\n"
760 " </n1>\n"
761 "</c>\n"
762 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
763 "</notification>\n";
764 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
765 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_NOTIF_NETCONF, &tree, &op2));
766 ly_in_free(in, 0);
767
768 lyd_free_all(tree);
769 lyd_free_all(op2);
770
Michal Vaskoe0665742021-02-11 11:08:44 +0100771 /* parse a data reply */
772 data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
773 " <al xmlns=\"urn:tests:a\">25</al>\n"
774 "</rpc-reply>\n";
775 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100776 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 +0100777 ly_in_free(in, 0);
778
Radek Krejci8df109d2021-04-23 12:19:08 +0200779 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 +0100780
Michal Vaskoe0665742021-02-11 11:08:44 +0100781 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
782 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
Michal Vaskoe0665742021-02-11 11:08:44 +0100783
784 lyd_free_all(tree);
785 /* it was connected to the action, do not free */
786
787 /* parse an ok reply */
788 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
789 " <ok/>\n"
790 "</rpc-reply>\n";
791 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100792 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 +0100793 ly_in_free(in, 0);
794
Radek Krejci8df109d2021-04-23 12:19:08 +0200795 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
796 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 +0100797
Michal Vaskoe0665742021-02-11 11:08:44 +0100798 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
799
800 lyd_free_all(tree);
801
802 /* parse an error reply */
803 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
804 " <rpc-error>\n"
805 " <error-type>rpc</error-type>\n"
806 " <error-tag>missing-attribute</error-tag>\n"
807 " <error-severity>error</error-severity>\n"
808 " <error-info>\n"
809 " <bad-attribute>message-id</bad-attribute>\n"
810 " <bad-element>rpc</bad-element>\n"
811 " </error-info>\n"
812 " </rpc-error>\n"
813 "</rpc-reply>\n";
814 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100815 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 +0100816 ly_in_free(in, 0);
817
Radek Krejci8df109d2021-04-23 12:19:08 +0200818 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
819 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 +0100820
Michal Vaskoe0665742021-02-11 11:08:44 +0100821 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
822
823 lyd_free_all(tree);
824
825 lyd_free_all(action);
826
827 /* wrong namespace, element name, whatever... */
828 /* TODO */
829}
830
aPiecek9cdb9e62021-05-18 09:46:20 +0200831static void
Michal Vasko820efe82023-05-12 15:47:43 +0200832test_restconf_rpc(void **state)
833{
834 const char *data;
835 struct ly_in *in;
836 struct lyd_node *tree, *envp;
837
838 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
839
840 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
841
842 data = "<input xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
843 "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
844 "<config>"
845 "<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
846 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
847 "<a>val_a</a><b>val_b</b><c>val_c</c>"
848 "</l1>"
849 "</config></input>";
850 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
851 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_XML, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
852 ly_in_free(in, 0);
853
854 /* the same just connected to the edit-data RPC */
855 data = "<edit-data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-nmda\">"
856 "<datastore xmlns:ds=\"urn:ietf:params:xml:ns:yang:ietf-datastores\">ds:running</datastore>"
857 "<config>"
858 "<cp xmlns=\"urn:tests:a\"><z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\"/></cp>"
859 "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
860 "<a>val_a</a><b>val_b</b><c>val_c</c>"
861 "</l1>"
862 "</config></edit-data>";
863 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
864 lyd_free_all(tree);
865 lyd_free_all(envp);
866}
867
868static void
869test_restconf_reply(void **state)
870{
871 const char *data;
872 struct ly_in *in;
873 struct lyd_node *tree, *envp;
874
875 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
876
877 data = "<output xmlns=\"urn:tests:a\"><al>25</al></output>";
878 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
879 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_XML, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
880 ly_in_free(in, 0);
881
882 /* connected to the RPC with the parent */
883 data = "<c xmlns=\"urn:tests:a\"><act><al>25</al></act></c>";
884 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
885 lyd_free_all(tree);
886 lyd_free_all(envp);
887}
888
889static void
Michal Vasko45791ad2021-06-17 08:45:03 +0200890test_filter_attributes(void **state)
891{
892 const char *data;
893 struct ly_in *in;
894 struct lyd_node *tree;
895 const struct lyd_node *node;
896 const char *dsc;
897 const char *ref = "RFC 6241, Section 7.7";
898 const char *feats[] = {"writable-running", NULL};
899
900 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100901 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "notifications", "2008-07-14", NULL)));
Michal Vasko45791ad2021-06-17 08:45:03 +0200902
903 data = "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
904 " <filter type=\"xpath\" select=\"/*\"/>\n"
905 "</get>\n";
906 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
907 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
908 ly_in_free(in, 0);
909 assert_non_null(tree);
910
911 node = tree;
912 dsc = "Retrieve running configuration and device state information.";
913 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
914 1, 0, 0, 1, "get", LYS_RPC,
915 1, 0, 0, 0, 0, ref, 0);
916 node = lyd_child(node);
917 dsc = "This parameter specifies the portion of the system\nconfiguration and state data to retrieve.";
918 CHECK_LYSC_NODE(node->schema, dsc, 1, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "filter", 0, LYS_ANYXML, 1, 0, NULL, 0);
919
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100920 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
921 lyd_free_all(tree);
Michal Vasko45791ad2021-06-17 08:45:03 +0200922
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100923 data = "<create-subscription xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
924 " <filter type=\"subtree\">\n"
925 " <inner-node xmlns=\"my:urn\"/>\n"
926 " </filter>\n"
927 "</create-subscription>\n";
928 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
929 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
930 ly_in_free(in, 0);
931 assert_non_null(tree);
932
933 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko45791ad2021-06-17 08:45:03 +0200934 lyd_free_all(tree);
935}
936
937static void
aPiecek9cdb9e62021-05-18 09:46:20 +0200938test_data_skip(void **state)
939{
940 const char *data;
941 struct lyd_node *tree;
942 struct lyd_node_term *leaf;
943
944 /* add invalid data to a module that is not implemented */
945 data = "<foo xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"><u/></foo>";
946 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
947 assert_null(tree);
948
949 /* add invalid data to a module that is implemented */
950 data = "<fooX xmlns=\"urn:tests:a\"><u/><list><value/></list></fooX>";
951 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
952 assert_null(tree);
953
954 /* first invalid, next valid */
955 data = "<fooX xmlns=\"urn:tests:a\"><u/></fooX> <foo xmlns=\"urn:tests:a\">foo value</foo>";
956 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
957 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
958 leaf = (struct lyd_node_term *)tree;
959 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
960 lyd_free_all(tree);
961}
962
Michal Vaskodd03ff12023-09-11 10:30:48 +0200963static void
964test_metadata(void **state)
965{
966 const char *data;
967 struct lyd_node *tree;
968
969 /* invalid metadata value */
970 data = "<c xmlns=\"urn:tests:a\" xmlns:a=\"urn:tests:a\"><x a:attr=\"value\">xval</x></c>";
971 assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
972 assert_null(tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100973 CHECK_LOG_CTX("Invalid enumeration value \"value\".", "/a:c/x/@a:attr", 1);
Michal Vaskodd03ff12023-09-11 10:30:48 +0200974}
975
Michal Vaskoa6139e02023-10-03 14:13:22 +0200976static void
977test_subtree(void **state)
978{
979 const char *data;
980 struct ly_in *in;
981 struct lyd_node *tree;
982
983 /* prepare data with the parent */
984 data = "<l1 xmlns=\"urn:tests:a\">\n"
985 " <a>val_a</a>\n"
986 " <b>val_b</b>\n"
987 " <c>1</c>\n"
988 "</l1>\n";
989 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
990
991 /* parse a subtree of it */
992 data = "<cont xmlns=\"urn:tests:a\">\n"
993 " <e>true</e>\n"
994 "</cont>\n";
995 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
996 assert_int_equal(LY_SUCCESS, lyd_parse_data(UTEST_LYCTX, tree, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, NULL));
997 ly_in_free(in, 0);
998
999 /* parse another container, fails */
1000 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
1001 assert_int_equal(LY_EVALID, lyd_parse_data(UTEST_LYCTX, tree, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, NULL));
1002 ly_in_free(in, 0);
Michal Vasko7a266772024-01-23 11:02:38 +01001003 CHECK_LOG_CTX("Duplicate instance of \"cont\".", "/a:l1[a='val_a'][b='val_b'][c='1']/cont", 0);
Michal Vaskoa6139e02023-10-03 14:13:22 +02001004
1005 lyd_free_all(tree);
1006}
1007
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001008int
1009main(void)
Radek Krejci509e2592019-05-15 16:30:48 +02001010{
1011 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001012 UTEST(test_leaf, setup),
1013 UTEST(test_anydata, setup),
Michal Vasko5b414dd2023-04-13 10:29:58 +02001014 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +02001015 UTEST(test_list, setup),
1016 UTEST(test_container, setup),
1017 UTEST(test_opaq, setup),
1018 UTEST(test_rpc, setup),
1019 UTEST(test_action, setup),
1020 UTEST(test_notification, setup),
1021 UTEST(test_reply, setup),
Michal Vaskoe0665742021-02-11 11:08:44 +01001022 UTEST(test_netconf_rpc, setup),
1023 UTEST(test_netconf_action, setup),
1024 UTEST(test_netconf_reply_or_notification, setup),
Michal Vasko820efe82023-05-12 15:47:43 +02001025 UTEST(test_restconf_rpc, setup),
1026 UTEST(test_restconf_reply, setup),
Michal Vasko45791ad2021-06-17 08:45:03 +02001027 UTEST(test_filter_attributes, setup),
aPiecek9cdb9e62021-05-18 09:46:20 +02001028 UTEST(test_data_skip, setup),
Michal Vaskodd03ff12023-09-11 10:30:48 +02001029 UTEST(test_metadata, setup),
Michal Vaskoa6139e02023-10-03 14:13:22 +02001030 UTEST(test_subtree, setup),
Radek Krejci509e2592019-05-15 16:30:48 +02001031 };
1032
1033 return cmocka_run_group_tests(tests, NULL, NULL);
1034}