blob: 7defd9c18306659e999ae850fe065be873f5e8b0 [file] [log] [blame]
Michal Vasko1b2a3f42022-12-20 09:38:28 +01001/**
Radek Krejci509e2592019-05-15 16:30:48 +02002 * @file test_parser_xml.c
Michal Vasko1b2a3f42022-12-20 09:38:28 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci509e2592019-05-15 16:30:48 +02005 * @brief unit tests for functions from parser_xml.c
6 *
Michal Vasko1b2a3f42022-12-20 09:38:28 +01007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejci509e2592019-05-15 16:30:48 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
Radek Krejci509e2592019-05-15 16:30:48 +020017
Radek Krejci70593c12020-06-13 20:48:09 +020018#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "printer_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "tree_data_internal.h"
24#include "tree_schema.h"
Radek Krejci509e2592019-05-15 16:30:48 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010030 "module a {\n"
31 " namespace urn:tests:a;\n"
32 " prefix a;\n"
33 " yang-version 1.1;\n"
Michal Vaskodbf3e652022-10-21 08:46:25 +020034 " list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
35 " leaf d {type string;}"
36 " container cont {leaf e {type boolean;}}"
37 " }"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010038 " leaf foo { type string;}\n"
39 " container c {\n"
40 " leaf x {type string;}\n"
41 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }\n"
42 " notification n1 { leaf nl {type string;}}}\n"
43 " container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}\n"
44 " anydata any {config false;}\n"
45 " leaf foo2 { type string; default \"default-val\"; }\n"
46 " leaf foo3 { type uint32; }\n"
47 " notification n2;}";
Radek Krejci509e2592019-05-15 16:30:48 +020048
Radek Iša56ca9e42020-09-08 18:42:00 +020049 UTEST_SETUP;
Radek Krejci509e2592019-05-15 16:30:48 +020050
Radek Iša56ca9e42020-09-08 18:42:00 +020051 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
52 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci509e2592019-05-15 16:30:48 +020053
54 return 0;
55}
56
Radek Iša56ca9e42020-09-08 18:42:00 +020057#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
58 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci509e2592019-05-15 16:30:48 +020059
Radek Iša56ca9e42020-09-08 18:42:00 +020060#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
61 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
62 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
63 assert_null(MODEL)
Radek Krejci509e2592019-05-15 16:30:48 +020064
Radek Iša56ca9e42020-09-08 18:42:00 +020065#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
66 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
Radek Krejci509e2592019-05-15 16:30:48 +020067
Radek Krejci509e2592019-05-15 16:30:48 +020068static void
69test_leaf(void **state)
70{
Radek Krejci509e2592019-05-15 16:30:48 +020071 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
72 struct lyd_node *tree;
73 struct lyd_node_term *leaf;
74
Radek Iša56ca9e42020-09-08 18:42:00 +020075 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejci509e2592019-05-15 16:30:48 +020076
Radek Iša56ca9e42020-09-08 18:42:00 +020077 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
78 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
79 leaf = (struct lyd_node_term *)tree;
80 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
81
82 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
83 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010084 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020085 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010086 assert_true(leaf->flags & LYD_DEFAULT);
Radek Krejci509e2592019-05-15 16:30:48 +020087 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +010088
89 /* make foo2 explicit */
90 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020091 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +010092 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010093 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020094 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
95 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010096 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020097 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010098 assert_false(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +010099 lyd_free_all(tree);
100
Radek Krejci1798aae2020-07-14 13:26:06 +0200101 /* parse foo2 but make it implicit, skip metadata xxx from missing schema */
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" "
103 "wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
104 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100105 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100106 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
108 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100109 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100111 assert_true(leaf->flags & LYD_DEFAULT);
Michal Vasko8d544252020-03-02 10:19:52 +0100112 lyd_free_all(tree);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200113
114 /* invalid value */
115 data = "<l1 xmlns=\"urn:tests:a\"><a>val-a</a><b>val-b</b><c>1</c><cont><e>0</e></cont></l1>";
116 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
117 "Invalid boolean value \"0\".",
118 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
Radek Krejci509e2592019-05-15 16:30:48 +0200119}
120
Radek Krejciee4cab22019-07-17 17:07:47 +0200121static void
122test_anydata(void **state)
123{
Michal Vasko52927e22020-03-16 17:26:14 +0100124 const char *data;
Radek Krejciee4cab22019-07-17 17:07:47 +0200125 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200126
Radek Iša56ca9e42020-09-08 18:42:00 +0200127 data = "<any xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100128 " <element1>\n"
129 " <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
130 " </element1>\n"
131 " <element1a/>\n"
132 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200133 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200134 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100135 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
137 1, LYS_ANYDATA, 0, 0, NULL, 0);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200138
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 const char *data_expected =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100140 "<any xmlns=\"urn:tests:a\">\n"
141 " <element1>\n"
142 " <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
143 " </element1>\n"
144 " <element1a/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200145 "</any>\n";
146
147 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Radek Krejciee4cab22019-07-17 17:07:47 +0200148 lyd_free_all(tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200149}
150
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200151static void
152test_list(void **state)
153{
Radek Iša56ca9e42020-09-08 18:42:00 +0200154 const char *data;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200155 struct lyd_node *tree, *iter;
156 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200157 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200158
Radek Krejci710226d2019-07-24 17:24:59 +0200159 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200160 data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
161 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
162 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
163 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100164 list = (struct lyd_node_inner *)tree;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200165 LY_LIST_FOR(list->child, iter) {
166 assert_int_not_equal(0, iter->hash);
167 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200168 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200169
Michal Vasko9f96a052020-03-10 09:41:45 +0100170 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200171 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 +0200172 "List instance is missing its key \"a\".", "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100173
Radek Iša56ca9e42020-09-08 18:42:00 +0200174 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 +0200175 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100176
Radek Iša56ca9e42020-09-08 18:42:00 +0200177 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 +0200178 "List instance is missing its key \"c\".", "Data location \"/a:l1[a='a'][b='b']\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100179
180 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200181 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 +0200182 "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200183 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100184
Radek Krejci710226d2019-07-24 17:24:59 +0200185 /* keys order */
Radek Iša56ca9e42020-09-08 18:42:00 +0200186 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);
187 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
188 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100189 list = (struct lyd_node_inner *)tree;
190 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 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 +0100192 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 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 +0100194 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 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 +0100196 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200197 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 +0200198 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200199 lyd_free_all(tree);
200
Michal Vasko44685da2020-03-17 15:38:06 +0100201 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
203 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 +0100204 list = (struct lyd_node_inner *)tree;
205 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 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 +0100207 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 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 +0100209 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
211 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200212 lyd_free_all(tree);
213
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200215 "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 +0200216}
217
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200218static void
219test_container(void **state)
220{
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200221 struct lyd_node *tree;
222 struct lyd_node_inner *cont;
223
Radek Iša56ca9e42020-09-08 18:42:00 +0200224 CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
225 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 +0100226 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200227 assert_true(cont->flags & LYD_DEFAULT);
228 lyd_free_all(tree);
229
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200231 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100232 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100233 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 +0200234 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100235 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200236 assert_false(cont->flags & LYD_DEFAULT);
237 lyd_free_all(tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200238}
239
Michal Vasko44685da2020-03-17 15:38:06 +0100240static void
241test_opaq(void **state)
242{
Michal Vasko44685da2020-03-17 15:38:06 +0100243 const char *data;
Michal Vasko44685da2020-03-17 15:38:06 +0100244 struct lyd_node *tree;
245
246 /* invalid value, no flags */
247 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200249 "Invalid type uint32 empty value.", "Schema location \"/a:foo3\", line number 1.");
Michal Vasko44685da2020-03-17 15:38:06 +0100250
251 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200253 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "foo3", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
Michal Vasko44685da2020-03-17 15:38:06 +0100255 lyd_free_all(tree);
256
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200257 /* list, opaq flag */
258 data = "<l1 xmlns=\"urn:tests:a\"/>";
259 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
260 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
261 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<l1 xmlns=\"urn:tests:a\"/>\n");
262 lyd_free_all(tree);
263
Michal Vasko44685da2020-03-17 15:38:06 +0100264 /* missing key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 data = "<l1 xmlns=\"urn:tests:a\">\n"
266 " <a>val_a</a>\n"
267 " <b>val_b</b>\n"
268 " <d>val_d</d>\n"
269 "</l1>\n";
270 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200271 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200272 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 5.");
Michal Vasko44685da2020-03-17 15:38:06 +0100273
274 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200275 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200276 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100278 lyd_free_all(tree);
279
280 /* invalid key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 data = "<l1 xmlns=\"urn:tests:a\">\n"
282 " <a>val_a</a>\n"
283 " <b>val_b</b>\n"
284 " <c>val_c</c>\n"
285 "</l1>\n";
286 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200287 "Invalid type int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200288 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 4.");
Michal Vasko44685da2020-03-17 15:38:06 +0100289
290 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200291 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200292 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200293 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100294 lyd_free_all(tree);
295
Michal Vasko413c7f22020-05-05 12:34:06 +0200296 /* opaq flag and fail */
Radek Iša56ca9e42020-09-08 18:42:00 +0200297 assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
298 "<a xmlns=\"ns\">\n"
299 " <b>x</b>\n"
Michal Vaskoe137fc42021-07-22 11:53:13 +0200300 " <c xmld:id=\"D\">1</c>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200301 "</a>\n",
302 LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskoe137fc42021-07-22 11:53:13 +0200303 CHECK_LOG_CTX("Unknown XML prefix \"xmld\".", "Line number 3.");
Michal Vasko44685da2020-03-17 15:38:06 +0100304}
305
Michal Vaskob36053d2020-03-26 15:49:30 +0100306static void
307test_rpc(void **state)
308{
Michal Vaskob36053d2020-03-26 15:49:30 +0100309 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100311 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100312 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200313 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
314 "configuration to the specified target configuration.";
315 const char *ref = "RFC 6241, Section 7.2";
316 const char *feats[] = {"writable-running", NULL};
Michal Vaskob36053d2020-03-26 15:49:30 +0100317
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100319
Michal Vasko2552ea32020-12-08 15:32:34 +0100320 data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
321 " <target>\n"
322 " <running/>\n"
323 " </target>\n"
324 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
325 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
326 " <a>val_a</a>\n"
327 " <b>val_b</b>\n"
328 " <c>val_c</c>\n"
329 " </l1>\n"
330 " <cp xmlns=\"urn:tests:a\">\n"
331 " <z nc:operation=\"delete\"/>\n"
332 " </cp>\n"
333 " </config>\n"
334 "</edit-config>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100336 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 +0200337 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100338
339 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200340
Radek Krejci2a9fc652021-01-22 17:44:34 +0100341 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 1, 0, 0, 1, "edit-config", LYS_RPC,
343 0, 0, 0, 0, 0, ref, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100344
Michal Vaskob36053d2020-03-26 15:49:30 +0100345 assert_non_null(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200346
Michal Vasko2552ea32020-12-08 15:32:34 +0100347 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100348 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200349 1, 0, 0, 1, "edit-config", LYS_RPC,
350 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200351 node = lyd_child(node)->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200352 dsc = "Inline Config content.";
Michal Vaskod1e53b92021-01-28 13:11:06 +0100353 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 +0200354
Michal Vasko1bf09392020-03-27 12:38:10 +0100355 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100356 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 +0200357 1, LYS_CONTAINER, 0, 0, NULL, 0);
358
Radek Krejcia1c1e542020-09-29 16:06:52 +0200359 node = lyd_child(node);
Michal Vasko1bf09392020-03-27 12:38:10 +0100360 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200361 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 0, "");
Michal Vaskob104f112020-07-17 09:54:54 +0200362 node = node->parent->next;
363 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200364 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100365
Radek Iša56ca9e42020-09-08 18:42:00 +0200366 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100367 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
368 " <target>\n"
369 " <running/>\n"
370 " </target>\n"
371 " <config>\n"
372 " <cp xmlns=\"urn:tests:a\">\n"
373 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
374 " </cp>\n"
375 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
376 " <a>val_a</a>\n"
377 " <b>val_b</b>\n"
378 " <c>val_c</c>\n"
379 " </l1>\n"
380 " </config>\n"
381 "</edit-config>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200382
Michal Vaskob36053d2020-03-26 15:49:30 +0100383 lyd_free_all(tree);
384
385 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100386 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100387}
388
389static void
390test_action(void **state)
391{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100392 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200393 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100394 struct lyd_node *tree, *op;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100395
Michal Vasko2552ea32020-12-08 15:32:34 +0100396 data = "<c xmlns=\"urn:tests:a\">\n"
397 " <act>\n"
398 " <al>value</al>\n"
399 " </act>\n"
400 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200401 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100402 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 +0200403 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100404
405 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100406 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200407 1, 0, 0, 1, "act", LYS_ACTION,
408 1, 0, 0, 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100409
Radek Iša56ca9e42020-09-08 18:42:00 +0200410 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100411 "<c xmlns=\"urn:tests:a\">\n"
412 " <act>\n"
413 " <al>value</al>\n"
414 " </act>\n"
415 "</c>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200416
Michal Vaskoa8edff02020-03-27 14:47:01 +0100417 lyd_free_all(tree);
418
419 /* wrong namespace, element name, whatever... */
420 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100421}
422
423static void
424test_notification(void **state)
425{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100426 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200427 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100428 struct lyd_node *tree, *ntf;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100429
Michal Vasko2552ea32020-12-08 15:32:34 +0100430 data = "<c xmlns=\"urn:tests:a\">\n"
431 " <n1>\n"
432 " <nl>value</nl>\n"
433 " </n1>\n"
434 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200435 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100436 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 +0200437 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100438
439 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100440 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 +0100441
Michal Vasko2552ea32020-12-08 15:32:34 +0100442 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 +0100443
Radek Iša56ca9e42020-09-08 18:42:00 +0200444 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100445 lyd_free_all(tree);
446
447 /* top-level notif without envelope */
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 data = "<n2 xmlns=\"urn:tests:a\"/>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100450 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 +0200451 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100452
453 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100454 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 +0100455
456 assert_non_null(tree);
457 assert_ptr_equal(ntf, tree);
458
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100460 lyd_free_all(tree);
461
462 /* wrong namespace, element name, whatever... */
463 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100464}
465
Michal Vasko1ce933a2020-03-30 12:38:22 +0200466static void
467test_reply(void **state)
468{
Michal Vasko1ce933a2020-03-30 12:38:22 +0200469 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100471 struct lyd_node *tree, *op;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200472 const struct lyd_node *node;
473
Michal Vasko79135ae2020-12-16 10:08:35 +0100474 data = "<c xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100475 " <act>\n"
Michal Vasko2552ea32020-12-08 15:32:34 +0100476 " <al>25</al>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100477 " </act>\n"
478 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200479 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100480 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 +0200481 ly_in_free(in, 0);
482
Michal Vasko1ce933a2020-03-30 12:38:22 +0200483 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200484
Radek Krejci2a9fc652021-01-22 17:44:34 +0100485 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 1, 0, 0, 1, "act", LYS_ACTION,
487 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200488 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100489 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 +0200490
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 Vasko1ce933a2020-03-30 12:38:22 +0200492
493 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200494 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_WITHSIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
Michal Vasko1ce933a2020-03-30 12:38:22 +0200495 lyd_free_all(tree);
496
497 /* wrong namespace, element name, whatever... */
498 /* TODO */
Michal Vasko1ce933a2020-03-30 12:38:22 +0200499}
500
Michal Vaskoe0665742021-02-11 11:08:44 +0100501static void
502test_netconf_rpc(void **state)
503{
504 const char *data;
505 struct ly_in *in;
506 struct lyd_node *tree, *op;
507 const struct lyd_node *node;
508 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
509 "configuration to the specified target configuration.";
510 const char *ref = "RFC 6241, Section 7.2";
511 const char *feats[] = {"writable-running", NULL};
512
513 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
514
515 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
516 "<edit-config>\n"
517 " <target>\n"
518 " <running/>\n"
519 " </target>\n"
520 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
521 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
522 " <a>val_a</a>\n"
523 " <b>val_b</b>\n"
524 " <c>val_c</c>\n"
525 " </l1>\n"
526 " <cp xmlns=\"urn:tests:a\">\n"
527 " <z nc:operation=\"delete\"/>\n"
528 " </cp>\n"
529 " </config>\n"
530 "</edit-config>\n"
531 "</rpc>\n";
532 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_RPC_NETCONF, &tree, &op));
Michal Vaskoe0665742021-02-11 11:08:44 +0100534 ly_in_free(in, 0);
535
536 assert_non_null(op);
537
538 node = tree;
Radek Krejci8df109d2021-04-23 12:19:08 +0200539 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 +0100540
541 assert_non_null(tree);
542
543 node = op;
544 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
545 1, 0, 0, 1, "edit-config", LYS_RPC,
546 0, 0, 0, 0, 0, ref, 0);
547 node = lyd_child(node)->next;
548 dsc = "Inline Config content.";
549 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
550
551 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100552 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Michal Vaskoe0665742021-02-11 11:08:44 +0100553 1, LYS_CONTAINER, 0, 0, NULL, 0);
554
555 node = lyd_child(node);
556 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200557 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_XML, "z", 0, 0, NULL, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100558 node = node->parent->next;
559 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200560 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_XML, "l1", 0, 0, NULL, 0, "");
Michal Vaskoe0665742021-02-11 11:08:44 +0100561
562 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
563 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
564 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
565 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
566 " <target>\n"
567 " <running/>\n"
568 " </target>\n"
569 " <config>\n"
570 " <cp xmlns=\"urn:tests:a\">\n"
571 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
572 " </cp>\n"
573 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
574 " <a>val_a</a>\n"
575 " <b>val_b</b>\n"
576 " <c>val_c</c>\n"
577 " </l1>\n"
578 " </config>\n"
579 "</edit-config>\n");
580
581 lyd_free_all(tree);
582 lyd_free_all(op);
583
584 /* wrong namespace, element name, whatever... */
585 /* TODO */
586}
587
588static void
589test_netconf_action(void **state)
590{
591 const char *data;
592 struct ly_in *in;
593 struct lyd_node *tree, *op;
594
595 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
596 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
597 "<c xmlns=\"urn:tests:a\">\n"
598 " <act>\n"
599 " <al>value</al>\n"
600 " </act>\n"
601 "</c>\n"
602 "</action>\n"
603 "</rpc>\n";
604 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100605 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 +0100606 ly_in_free(in, 0);
607
Radek Krejci8df109d2021-04-23 12:19:08 +0200608 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc", 0, 0, 0, 0, "");
609 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 +0100610
611 assert_non_null(op);
612 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
613 1, 0, 0, 1, "act", LYS_ACTION,
614 1, 0, 0, 1, 0, NULL, 0);
615
616 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
617 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
618 " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
619 "</rpc>\n");
620 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
621 "<act xmlns=\"urn:tests:a\">\n"
622 " <al>value</al>\n"
623 "</act>\n");
624
625 lyd_free_all(tree);
626 lyd_free_all(op);
627
628 /* wrong namespace, element name, whatever... */
629 /* TODO */
630}
631
632static void
633test_netconf_reply_or_notification(void **state)
634{
635 const char *data;
636 struct ly_in *in;
637 struct lyd_node *action, *tree, *op, *op2;
638
639 /* parse the action */
640 data = "<c xmlns=\"urn:tests:a\">\n"
641 " <act>\n"
642 " <al>value</al>\n"
643 " </act>\n"
644 "</c>\n";
645 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100646 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 +0100647 ly_in_free(in, 0);
648
649 /* parse notification first */
650 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
651 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
652 "<c xmlns=\"urn:tests:a\">\n"
653 " <n1>\n"
654 " <nl>value</nl>\n"
655 " </n1>\n"
656 "</c>\n"
657 "</notification>\n";
658 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100659 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 +0100660 ly_in_free(in, 0);
661
Radek Krejci8df109d2021-04-23 12:19:08 +0200662 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_VALUE_XML, "notification", 0, 0, 0, 0, "");
663 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 +0100664 "2010-12-06T08:00:01Z");
665
666 assert_non_null(op2);
667 CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
668
669 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
670 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
671 " <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
672 "</notification>\n");
673 CHECK_LYD_STRING(op2, LYD_PRINT_WITHSIBLINGS,
674 "<n1 xmlns=\"urn:tests:a\">\n"
675 " <nl>value</nl>\n"
676 "</n1>\n");
677
678 lyd_free_all(tree);
679 lyd_free_all(op2);
680
681 /* parse a data reply */
682 data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
683 " <al xmlns=\"urn:tests:a\">25</al>\n"
684 "</rpc-reply>\n";
685 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100686 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 +0100687 ly_in_free(in, 0);
688
Radek Krejci8df109d2021-04-23 12:19:08 +0200689 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 +0100690
Michal Vaskoe0665742021-02-11 11:08:44 +0100691 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
692 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
Michal Vaskoe0665742021-02-11 11:08:44 +0100693
694 lyd_free_all(tree);
695 /* it was connected to the action, do not free */
696
697 /* parse an ok reply */
698 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
699 " <ok/>\n"
700 "</rpc-reply>\n";
701 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100702 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 +0100703 ly_in_free(in, 0);
704
Radek Krejci8df109d2021-04-23 12:19:08 +0200705 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
706 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 +0100707
Michal Vaskoe0665742021-02-11 11:08:44 +0100708 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
709
710 lyd_free_all(tree);
711
712 /* parse an error reply */
713 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
714 " <rpc-error>\n"
715 " <error-type>rpc</error-type>\n"
716 " <error-tag>missing-attribute</error-tag>\n"
717 " <error-severity>error</error-severity>\n"
718 " <error-info>\n"
719 " <bad-attribute>message-id</bad-attribute>\n"
720 " <bad-element>rpc</bad-element>\n"
721 " </error-info>\n"
722 " </rpc-error>\n"
723 "</rpc-reply>\n";
724 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100725 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 +0100726 ly_in_free(in, 0);
727
Radek Krejci8df109d2021-04-23 12:19:08 +0200728 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_VALUE_XML, "rpc-reply", 0, 0, 0, 0, "");
729 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 +0100730
Michal Vaskoe0665742021-02-11 11:08:44 +0100731 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
732
733 lyd_free_all(tree);
734
735 lyd_free_all(action);
736
737 /* wrong namespace, element name, whatever... */
738 /* TODO */
739}
740
aPiecek9cdb9e62021-05-18 09:46:20 +0200741static void
Michal Vasko45791ad2021-06-17 08:45:03 +0200742test_filter_attributes(void **state)
743{
744 const char *data;
745 struct ly_in *in;
746 struct lyd_node *tree;
747 const struct lyd_node *node;
748 const char *dsc;
749 const char *ref = "RFC 6241, Section 7.7";
750 const char *feats[] = {"writable-running", NULL};
751
752 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100753 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "notifications", "2008-07-14", NULL)));
Michal Vasko45791ad2021-06-17 08:45:03 +0200754
755 data = "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
756 " <filter type=\"xpath\" select=\"/*\"/>\n"
757 "</get>\n";
758 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
759 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
760 ly_in_free(in, 0);
761 assert_non_null(tree);
762
763 node = tree;
764 dsc = "Retrieve running configuration and device state information.";
765 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
766 1, 0, 0, 1, "get", LYS_RPC,
767 1, 0, 0, 0, 0, ref, 0);
768 node = lyd_child(node);
769 dsc = "This parameter specifies the portion of the system\nconfiguration and state data to retrieve.";
770 CHECK_LYSC_NODE(node->schema, dsc, 1, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "filter", 0, LYS_ANYXML, 1, 0, NULL, 0);
771
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100772 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
773 lyd_free_all(tree);
Michal Vasko45791ad2021-06-17 08:45:03 +0200774
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100775 data = "<create-subscription xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
776 " <filter type=\"subtree\">\n"
777 " <inner-node xmlns=\"my:urn\"/>\n"
778 " </filter>\n"
779 "</create-subscription>\n";
780 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
781 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_XML, LYD_TYPE_RPC_YANG, &tree, NULL));
782 ly_in_free(in, 0);
783 assert_non_null(tree);
784
785 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko45791ad2021-06-17 08:45:03 +0200786 lyd_free_all(tree);
787}
788
789static void
aPiecek9cdb9e62021-05-18 09:46:20 +0200790test_data_skip(void **state)
791{
792 const char *data;
793 struct lyd_node *tree;
794 struct lyd_node_term *leaf;
795
796 /* add invalid data to a module that is not implemented */
797 data = "<foo xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"><u/></foo>";
798 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
799 assert_null(tree);
800
801 /* add invalid data to a module that is implemented */
802 data = "<fooX xmlns=\"urn:tests:a\"><u/><list><value/></list></fooX>";
803 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(_UC->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
804 assert_null(tree);
805
806 /* first invalid, next valid */
807 data = "<fooX xmlns=\"urn:tests:a\"><u/></fooX> <foo xmlns=\"urn:tests:a\">foo value</foo>";
808 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
809 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
810 leaf = (struct lyd_node_term *)tree;
811 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
812 lyd_free_all(tree);
813}
814
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100815int
816main(void)
Radek Krejci509e2592019-05-15 16:30:48 +0200817{
818 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200819 UTEST(test_leaf, setup),
820 UTEST(test_anydata, setup),
821 UTEST(test_list, setup),
822 UTEST(test_container, setup),
823 UTEST(test_opaq, setup),
824 UTEST(test_rpc, setup),
825 UTEST(test_action, setup),
826 UTEST(test_notification, setup),
827 UTEST(test_reply, setup),
Michal Vaskoe0665742021-02-11 11:08:44 +0100828 UTEST(test_netconf_rpc, setup),
829 UTEST(test_netconf_action, setup),
830 UTEST(test_netconf_reply_or_notification, setup),
Michal Vasko45791ad2021-06-17 08:45:03 +0200831 UTEST(test_filter_attributes, setup),
aPiecek9cdb9e62021-05-18 09:46:20 +0200832 UTEST(test_data_skip, setup),
Radek Krejci509e2592019-05-15 16:30:48 +0200833 };
834
835 return cmocka_run_group_tests(tests, NULL, NULL);
836}