blob: cfff101b3708d2b4c4e8fd68e382c680673923ef [file] [log] [blame]
Radek Krejci509e2592019-05-15 16:30:48 +02001/*
2 * @file test_parser_xml.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_xml.c
5 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
Radek Krejci509e2592019-05-15 16:30:48 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci509e2592019-05-15 16:30:48 +020016
Radek Krejci70593c12020-06-13 20:48:09 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010020#include "parser_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "printer_data.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "tree_data_internal.h"
23#include "tree_schema.h"
Radek Krejci509e2592019-05-15 16:30:48 +020024
25static int
26setup(void **state)
27{
Radek Iša56ca9e42020-09-08 18:42:00 +020028 const char *schema =
Radek Krejcib4ac5a92020-11-23 17:54:33 +010029 "module a {\n"
30 " namespace urn:tests:a;\n"
31 " prefix a;\n"
32 " yang-version 1.1;\n"
33 " list l1 {\n"
34 " key \"a b c\";\n"
35 " leaf a {type string;}\n"
36 " leaf b {type string;}\n"
37 " leaf c {type int16;}\n"
38 " leaf d {type string;}}\n"
39 " 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"
46 " leaf foo2 { type string; default \"default-val\"; }\n"
47 " leaf foo3 { type uint32; }\n"
48 " notification n2;}";
Radek Krejci509e2592019-05-15 16:30:48 +020049
Radek Iša56ca9e42020-09-08 18:42:00 +020050 UTEST_SETUP;
Radek Krejci509e2592019-05-15 16:30:48 +020051
Radek Iša56ca9e42020-09-08 18:42:00 +020052 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
53 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci509e2592019-05-15 16:30:48 +020054
55 return 0;
56}
57
Radek Iša56ca9e42020-09-08 18:42:00 +020058#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
59 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci509e2592019-05-15 16:30:48 +020060
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
62 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
63 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
64 assert_null(MODEL)
Radek Krejci509e2592019-05-15 16:30:48 +020065
Radek Iša56ca9e42020-09-08 18:42:00 +020066#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
67 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_XML, PRINT_OPTION)
Radek Krejci509e2592019-05-15 16:30:48 +020068
Radek Krejci509e2592019-05-15 16:30:48 +020069static void
70test_leaf(void **state)
71{
Radek Krejci509e2592019-05-15 16:30:48 +020072 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
73 struct lyd_node *tree;
74 struct lyd_node_term *leaf;
75
Radek Iša56ca9e42020-09-08 18:42:00 +020076 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejci509e2592019-05-15 16:30:48 +020077
Radek Iša56ca9e42020-09-08 18:42:00 +020078 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
79 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
80 leaf = (struct lyd_node_term *)tree;
81 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
82
83 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
84 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020086 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +010087 assert_true(leaf->flags & LYD_DEFAULT);
88
Radek Krejci509e2592019-05-15 16:30:48 +020089 lyd_free_all(tree);
Michal Vasko8d544252020-03-02 10:19:52 +010090
91 /* make foo2 explicit */
92 data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020093 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +010094 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010095 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020096 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
97 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010098 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020099 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100100 assert_false(leaf->flags & LYD_DEFAULT);
101
102 lyd_free_all(tree);
103
Radek Krejci1798aae2020-07-14 13:26:06 +0200104 /* parse foo2 but make it implicit, skip metadata xxx from missing schema */
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" "
106 "wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
107 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Michal Vasko8d544252020-03-02 10:19:52 +0100108 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100109 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
111 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100112 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200113 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Michal Vasko8d544252020-03-02 10:19:52 +0100114 assert_true(leaf->flags & LYD_DEFAULT);
115
116 lyd_free_all(tree);
Radek Krejci509e2592019-05-15 16:30:48 +0200117}
118
Radek Krejciee4cab22019-07-17 17:07:47 +0200119static void
120test_anydata(void **state)
121{
Michal Vasko52927e22020-03-16 17:26:14 +0100122 const char *data;
Radek Krejciee4cab22019-07-17 17:07:47 +0200123 struct lyd_node *tree;
Radek Krejciee4cab22019-07-17 17:07:47 +0200124
Radek Iša56ca9e42020-09-08 18:42:00 +0200125 data = "<any xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100126 " <element1>\n"
127 " <x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>\n"
128 " </element1>\n"
129 " <element1a/>\n"
130 "</any>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200131 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200132 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100133 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200134 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_SET_CONFIG, 1, "any",
135 1, LYS_ANYDATA, 0, 0, NULL, 0);
136 const char *data_expected =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100137 "<any xmlns=\"urn:tests:a\">\n"
138 " <element1>\n"
139 " <element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>\n"
140 " </element1>\n"
141 " <element1a/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 "</any>\n";
143
144 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data_expected);
Radek Krejciee4cab22019-07-17 17:07:47 +0200145
146 lyd_free_all(tree);
Radek Krejciee4cab22019-07-17 17:07:47 +0200147}
148
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200149static void
150test_list(void **state)
151{
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 const char *data;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200153 struct lyd_node *tree, *iter;
154 struct lyd_node_inner *list;
Radek Krejci710226d2019-07-24 17:24:59 +0200155 struct lyd_node_term *leaf;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200156
Radek Krejci710226d2019-07-24 17:24:59 +0200157 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200158 data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>";
159 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
160 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
161 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100162 list = (struct lyd_node_inner *)tree;
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200163 LY_LIST_FOR(list->child, iter) {
164 assert_int_not_equal(0, iter->hash);
165 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200166 lyd_free_all(tree);
Radek Krejci710226d2019-07-24 17:24:59 +0200167
Michal Vasko9f96a052020-03-10 09:41:45 +0100168 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200169 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100170 "List instance is missing its key \"a\".", "Schema location /a:l1, data location /a:l1[b='b'][c='1'], line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100171
Radek Iša56ca9e42020-09-08 18:42:00 +0200172 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100173 "List instance is missing its key \"b\".", "Schema location /a:l1, data location /a:l1[a='a'], line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100174
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 PARSER_CHECK_ERROR("<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100176 "List instance is missing its key \"c\".", "Schema location /a:l1, data location /a:l1[a='a'][b='b'], line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100177
178 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 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,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100180 "Duplicate instance of \"c\".", "Schema location /a:l1/c, data location /a:l1[a='a'][b='b'][c='1'][c='1']/c, line number 1.");
Michal Vasko9f96a052020-03-10 09:41:45 +0100181
Radek Krejci710226d2019-07-24 17:24:59 +0200182 /* keys order */
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 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);
184 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
185 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100186 list = (struct lyd_node_inner *)tree;
187 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 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 +0100189 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 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 +0100191 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 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 +0100193 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200194 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 0, LYS_LEAF, 1, 0, NULL, 0);
195 CHECK_LOG_CTX("Invalid position of the key \"b\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200196 lyd_free_all(tree);
197
Michal Vasko44685da2020-03-17 15:38:06 +0100198 data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
200 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 +0100201 list = (struct lyd_node_inner *)tree;
202 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 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 +0100204 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 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 +0100206 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
208 CHECK_LOG_CTX("Invalid position of the key \"a\" in a list.", NULL);
Radek Krejci710226d2019-07-24 17:24:59 +0200209 lyd_free_all(tree);
210
Radek Iša56ca9e42020-09-08 18:42:00 +0200211 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100212 "Invalid position of the key \"b\" in a list.", "Schema location /a:l1/b, data location /a:b, line number 1.");
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200213}
214
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200215static void
216test_container(void **state)
217{
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200218 struct lyd_node *tree;
219 struct lyd_node_inner *cont;
220
Radek Iša56ca9e42020-09-08 18:42:00 +0200221 CHECK_PARSE_LYD("<c xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
222 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 +0100223 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200224 assert_true(cont->flags & LYD_DEFAULT);
225 lyd_free_all(tree);
226
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 CHECK_PARSE_LYD("<cp xmlns=\"urn:tests:a\"/>", 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200228 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100229 tree = tree->next;
Christian Hopps7cfa9612021-02-04 11:45:41 -0500230 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE | LYS_SET_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100232 cont = (struct lyd_node_inner *)tree;
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200233 assert_false(cont->flags & LYD_DEFAULT);
234 lyd_free_all(tree);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200235}
236
Michal Vasko44685da2020-03-17 15:38:06 +0100237static void
238test_opaq(void **state)
239{
Michal Vasko44685da2020-03-17 15:38:06 +0100240 const char *data;
Michal Vasko44685da2020-03-17 15:38:06 +0100241 struct lyd_node *tree;
242
243 /* invalid value, no flags */
244 data = "<foo3 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100246 "Invalid empty uint32 value.", "Schema location /a:foo3, line number 1.");
Michal Vasko44685da2020-03-17 15:38:06 +0100247
248 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
250 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_PREF_XML, "foo3", 0, 0, NULL, 0, "");
251 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, "<foo3 xmlns=\"urn:tests:a\"/>\n");
Michal Vasko44685da2020-03-17 15:38:06 +0100252 lyd_free_all(tree);
253
254 /* missing key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 data = "<l1 xmlns=\"urn:tests:a\">\n"
256 " <a>val_a</a>\n"
257 " <b>val_b</b>\n"
258 " <d>val_d</d>\n"
259 "</l1>\n";
260 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100261 "List instance is missing its key \"c\".", "Schema location /a:l1, data location /a:l1[a='val_a'][b='val_b'], line number 5.");
Michal Vasko44685da2020-03-17 15:38:06 +0100262
263 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
265 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_PREF_XML, "l1", 0, 0, NULL, 0, "");
266 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100267 lyd_free_all(tree);
268
269 /* invalid key, no flags */
Radek Iša56ca9e42020-09-08 18:42:00 +0200270 data = "<l1 xmlns=\"urn:tests:a\">\n"
271 " <a>val_a</a>\n"
272 " <b>val_b</b>\n"
273 " <c>val_c</c>\n"
274 "</l1>\n";
275 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100276 "Invalid int16 value \"val_c\".", "Schema location /a:l1/c, data location /a:l1[a='val_a'][b='val_b'], line number 4.");
Michal Vasko44685da2020-03-17 15:38:06 +0100277
278 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200279 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
280 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_PREF_XML, "l1", 0, 0, NULL, 0, "");
281 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vasko44685da2020-03-17 15:38:06 +0100282 lyd_free_all(tree);
283
Michal Vasko413c7f22020-05-05 12:34:06 +0200284 /* opaq flag and fail */
Radek Iša56ca9e42020-09-08 18:42:00 +0200285 assert_int_equal(LY_EVALID, lyd_parse_data_mem(UTEST_LYCTX,
286 "<a xmlns=\"ns\">\n"
287 " <b>x</b>\n"
288 " <c xml:id=\"D\">1</c>\n"
289 "</a>\n",
290 LYD_XML, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree));
291 CHECK_LOG_CTX("Unknown XML prefix \"xml\".", "Line number 3.");
Michal Vasko44685da2020-03-17 15:38:06 +0100292}
293
Michal Vaskob36053d2020-03-26 15:49:30 +0100294static void
295test_rpc(void **state)
296{
Michal Vaskob36053d2020-03-26 15:49:30 +0100297 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200298 struct ly_in *in;
Michal Vaskob36053d2020-03-26 15:49:30 +0100299 struct lyd_node *tree, *op;
Michal Vasko1bf09392020-03-27 12:38:10 +0100300 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200301 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
302 "configuration to the specified target configuration.";
303 const char *ref = "RFC 6241, Section 7.2";
304 const char *feats[] = {"writable-running", NULL};
Michal Vaskob36053d2020-03-26 15:49:30 +0100305
Radek Iša56ca9e42020-09-08 18:42:00 +0200306 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100307
Michal Vasko2552ea32020-12-08 15:32:34 +0100308 data = "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
309 " <target>\n"
310 " <running/>\n"
311 " </target>\n"
312 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
313 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
314 " <a>val_a</a>\n"
315 " <b>val_b</b>\n"
316 " <c>val_c</c>\n"
317 " </l1>\n"
318 " <cp xmlns=\"urn:tests:a\">\n"
319 " <z nc:operation=\"delete\"/>\n"
320 " </cp>\n"
321 " </config>\n"
322 "</edit-config>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100324 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 +0200325 ly_in_free(in, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100326
327 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200328
Radek Krejci2a9fc652021-01-22 17:44:34 +0100329 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 1, 0, 0, 1, "edit-config", LYS_RPC,
331 0, 0, 0, 0, 0, ref, 0);
Michal Vasko1bf09392020-03-27 12:38:10 +0100332
Michal Vaskob36053d2020-03-26 15:49:30 +0100333 assert_non_null(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200334
Michal Vasko2552ea32020-12-08 15:32:34 +0100335 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100336 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 1, 0, 0, 1, "edit-config", LYS_RPC,
338 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200339 node = lyd_child(node)->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 dsc = "Inline Config content.";
Michal Vaskod1e53b92021-01-28 13:11:06 +0100341 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 +0200342
Michal Vasko1bf09392020-03-27 12:38:10 +0100343 node = ((struct lyd_node_any *)node)->value.tree;
Christian Hopps7cfa9612021-02-04 11:45:41 -0500344 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE | LYS_SET_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200345 1, LYS_CONTAINER, 0, 0, NULL, 0);
346
Radek Krejcia1c1e542020-09-29 16:06:52 +0200347 node = lyd_child(node);
Michal Vasko1bf09392020-03-27 12:38:10 +0100348 /* z has no value */
Radek Iša56ca9e42020-09-08 18:42:00 +0200349 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_PREF_XML, "z", 0, 0, NULL, 0, "");
Michal Vaskob104f112020-07-17 09:54:54 +0200350 node = node->parent->next;
351 /* l1 key c has invalid value so it is at the end */
Radek Iša56ca9e42020-09-08 18:42:00 +0200352 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_PREF_XML, "l1", 0, 0, NULL, 0, "");
Michal Vaskob36053d2020-03-26 15:49:30 +0100353
Radek Iša56ca9e42020-09-08 18:42:00 +0200354 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100355 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
356 " <target>\n"
357 " <running/>\n"
358 " </target>\n"
359 " <config>\n"
360 " <cp xmlns=\"urn:tests:a\">\n"
361 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
362 " </cp>\n"
363 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
364 " <a>val_a</a>\n"
365 " <b>val_b</b>\n"
366 " <c>val_c</c>\n"
367 " </l1>\n"
368 " </config>\n"
369 "</edit-config>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200370
Michal Vaskob36053d2020-03-26 15:49:30 +0100371 lyd_free_all(tree);
372
373 /* wrong namespace, element name, whatever... */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100374 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100375}
376
377static void
378test_action(void **state)
379{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100380 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200381 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100382 struct lyd_node *tree, *op;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100383
Michal Vasko2552ea32020-12-08 15:32:34 +0100384 data = "<c xmlns=\"urn:tests:a\">\n"
385 " <act>\n"
386 " <al>value</al>\n"
387 " </act>\n"
388 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100390 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 +0200391 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100392
393 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100394 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 1, 0, 0, 1, "act", LYS_ACTION,
396 1, 0, 0, 1, 0, NULL, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100397
Radek Iša56ca9e42020-09-08 18:42:00 +0200398 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
Michal Vasko2552ea32020-12-08 15:32:34 +0100399 "<c xmlns=\"urn:tests:a\">\n"
400 " <act>\n"
401 " <al>value</al>\n"
402 " </act>\n"
403 "</c>\n");
Radek Iša56ca9e42020-09-08 18:42:00 +0200404
Michal Vaskoa8edff02020-03-27 14:47:01 +0100405 lyd_free_all(tree);
406
407 /* wrong namespace, element name, whatever... */
408 /* TODO */
Michal Vaskoa8edff02020-03-27 14:47:01 +0100409}
410
411static void
412test_notification(void **state)
413{
Michal Vaskoa8edff02020-03-27 14:47:01 +0100414 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200415 struct ly_in *in;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100416 struct lyd_node *tree, *ntf;
Michal Vaskoa8edff02020-03-27 14:47:01 +0100417
Michal Vasko2552ea32020-12-08 15:32:34 +0100418 data = "<c xmlns=\"urn:tests:a\">\n"
419 " <n1>\n"
420 " <nl>value</nl>\n"
421 " </n1>\n"
422 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200423 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100424 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 +0200425 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100426
427 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100428 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 +0100429
Michal Vasko2552ea32020-12-08 15:32:34 +0100430 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 +0100431
Radek Iša56ca9e42020-09-08 18:42:00 +0200432 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100433 lyd_free_all(tree);
434
435 /* top-level notif without envelope */
Radek Iša56ca9e42020-09-08 18:42:00 +0200436 data = "<n2 xmlns=\"urn:tests:a\"/>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200437 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100438 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 +0200439 ly_in_free(in, 0);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100440
441 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100442 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 +0100443
444 assert_non_null(tree);
445 assert_ptr_equal(ntf, tree);
446
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100448 lyd_free_all(tree);
449
450 /* wrong namespace, element name, whatever... */
451 /* TODO */
Michal Vaskob36053d2020-03-26 15:49:30 +0100452}
453
Michal Vasko1ce933a2020-03-30 12:38:22 +0200454static void
455test_reply(void **state)
456{
Michal Vasko1ce933a2020-03-30 12:38:22 +0200457 const char *data;
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100459 struct lyd_node *tree, *op;
Michal Vasko1ce933a2020-03-30 12:38:22 +0200460 const struct lyd_node *node;
461
Michal Vasko79135ae2020-12-16 10:08:35 +0100462 data = "<c xmlns=\"urn:tests:a\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100463 " <act>\n"
Michal Vasko2552ea32020-12-08 15:32:34 +0100464 " <al>25</al>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100465 " </act>\n"
466 "</c>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100468 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 +0200469 ly_in_free(in, 0);
470
Michal Vasko1ce933a2020-03-30 12:38:22 +0200471 assert_non_null(op);
Radek Iša56ca9e42020-09-08 18:42:00 +0200472
Radek Krejci2a9fc652021-01-22 17:44:34 +0100473 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200474 1, 0, 0, 1, "act", LYS_ACTION,
475 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200476 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100477 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 +0200478
Michal Vasko2552ea32020-12-08 15:32:34 +0100479 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 +0200480
481 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_WITHSIBLINGS, "<al xmlns=\"urn:tests:a\">25</al>\n");
Michal Vasko1ce933a2020-03-30 12:38:22 +0200483 lyd_free_all(tree);
484
485 /* wrong namespace, element name, whatever... */
486 /* TODO */
Michal Vasko1ce933a2020-03-30 12:38:22 +0200487}
488
Michal Vaskoe0665742021-02-11 11:08:44 +0100489static void
490test_netconf_rpc(void **state)
491{
492 const char *data;
493 struct ly_in *in;
494 struct lyd_node *tree, *op;
495 const struct lyd_node *node;
496 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
497 "configuration to the specified target configuration.";
498 const char *ref = "RFC 6241, Section 7.2";
499 const char *feats[] = {"writable-running", NULL};
500
501 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
502
503 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
504 "<edit-config>\n"
505 " <target>\n"
506 " <running/>\n"
507 " </target>\n"
508 " <config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
509 " <l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">\n"
510 " <a>val_a</a>\n"
511 " <b>val_b</b>\n"
512 " <c>val_c</c>\n"
513 " </l1>\n"
514 " <cp xmlns=\"urn:tests:a\">\n"
515 " <z nc:operation=\"delete\"/>\n"
516 " </cp>\n"
517 " </config>\n"
518 "</edit-config>\n"
519 "</rpc>\n";
520 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100521 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 +0100522 ly_in_free(in, 0);
523
524 assert_non_null(op);
525
526 node = tree;
527 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 1, 0, LY_PREF_XML, "rpc", 0, 0, 0, 0, "");
528
529 assert_non_null(tree);
530
531 node = op;
532 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
533 1, 0, 0, 1, "edit-config", LYS_RPC,
534 0, 0, 0, 0, 0, ref, 0);
535 node = lyd_child(node)->next;
536 dsc = "Inline Config content.";
537 CHECK_LYSC_NODE(node->schema, dsc, 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", 0, LYS_ANYXML, 1, 0, NULL, 0);
538
539 node = ((struct lyd_node_any *)node)->value.tree;
540 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE | LYS_SET_PRESENCE, 1, "cp",
541 1, LYS_CONTAINER, 0, 0, NULL, 0);
542
543 node = lyd_child(node);
544 /* z has no value */
545 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_PREF_XML, "z", 0, 0, NULL, 0, "");
546 node = node->parent->next;
547 /* l1 key c has invalid value so it is at the end */
548 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_PREF_XML, "l1", 0, 0, NULL, 0, "");
549
550 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
551 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\"/>\n");
552 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
553 "<edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
554 " <target>\n"
555 " <running/>\n"
556 " </target>\n"
557 " <config>\n"
558 " <cp xmlns=\"urn:tests:a\">\n"
559 " <z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>\n"
560 " </cp>\n"
561 " <l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">\n"
562 " <a>val_a</a>\n"
563 " <b>val_b</b>\n"
564 " <c>val_c</c>\n"
565 " </l1>\n"
566 " </config>\n"
567 "</edit-config>\n");
568
569 lyd_free_all(tree);
570 lyd_free_all(op);
571
572 /* wrong namespace, element name, whatever... */
573 /* TODO */
574}
575
576static void
577test_netconf_action(void **state)
578{
579 const char *data;
580 struct ly_in *in;
581 struct lyd_node *tree, *op;
582
583 data = "<rpc message-id=\"25\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
584 "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">"
585 "<c xmlns=\"urn:tests:a\">\n"
586 " <act>\n"
587 " <al>value</al>\n"
588 " </act>\n"
589 "</c>\n"
590 "</action>\n"
591 "</rpc>\n";
592 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100593 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 +0100594 ly_in_free(in, 0);
595
596 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_PREF_XML, "rpc", 0, 0, 0, 0, "");
597 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_PREF_XML, "action", 0, 0, 0, 0, "");
598
599 assert_non_null(op);
600 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
601 1, 0, 0, 1, "act", LYS_ACTION,
602 1, 0, 0, 1, 0, NULL, 0);
603
604 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
605 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"25\">\n"
606 " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\"/>\n"
607 "</rpc>\n");
608 CHECK_LYD_STRING(op, LYD_PRINT_WITHSIBLINGS,
609 "<act xmlns=\"urn:tests:a\">\n"
610 " <al>value</al>\n"
611 "</act>\n");
612
613 lyd_free_all(tree);
614 lyd_free_all(op);
615
616 /* wrong namespace, element name, whatever... */
617 /* TODO */
618}
619
620static void
621test_netconf_reply_or_notification(void **state)
622{
623 const char *data;
624 struct ly_in *in;
625 struct lyd_node *action, *tree, *op, *op2;
626
627 /* parse the action */
628 data = "<c xmlns=\"urn:tests:a\">\n"
629 " <act>\n"
630 " <al>value</al>\n"
631 " </act>\n"
632 "</c>\n";
633 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100634 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 +0100635 ly_in_free(in, 0);
636
637 /* parse notification first */
638 data = "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
639 "<eventTime>2010-12-06T08:00:01Z</eventTime>\n"
640 "<c xmlns=\"urn:tests:a\">\n"
641 " <n1>\n"
642 " <nl>value</nl>\n"
643 " </n1>\n"
644 "</c>\n"
645 "</notification>\n";
646 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100647 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 +0100648 ly_in_free(in, 0);
649
650 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 1, LY_PREF_XML, "notification", 0, 0, 0, 0, "");
651 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_PREF_XML, "eventTime", 0, 0, 0, 0,
652 "2010-12-06T08:00:01Z");
653
654 assert_non_null(op2);
655 CHECK_LYSC_NOTIF((struct lysc_node_notif *)op2->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
656
657 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
658 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
659 " <eventTime>2010-12-06T08:00:01Z</eventTime>\n"
660 "</notification>\n");
661 CHECK_LYD_STRING(op2, LYD_PRINT_WITHSIBLINGS,
662 "<n1 xmlns=\"urn:tests:a\">\n"
663 " <nl>value</nl>\n"
664 "</n1>\n");
665
666 lyd_free_all(tree);
667 lyd_free_all(op2);
668
669 /* parse a data reply */
670 data = "<rpc-reply message-id=\"55\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
671 " <al xmlns=\"urn:tests:a\">25</al>\n"
672 "</rpc-reply>\n";
673 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100674 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 +0100675 ly_in_free(in, 0);
676
677 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 0, LY_PREF_XML, "rpc-reply", 0, 0, 0, 0, "");
678
Michal Vaskoe0665742021-02-11 11:08:44 +0100679 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS,
680 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\"/>\n");
Michal Vaskoe0665742021-02-11 11:08:44 +0100681
682 lyd_free_all(tree);
683 /* it was connected to the action, do not free */
684
685 /* parse an ok reply */
686 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
687 " <ok/>\n"
688 "</rpc-reply>\n";
689 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100690 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 +0100691 ly_in_free(in, 0);
692
693 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_PREF_XML, "rpc-reply", 0, 0, 0, 0, "");
694 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 0, LY_PREF_XML, "ok", 0, 0, 0, 0, "");
695
Michal Vaskoe0665742021-02-11 11:08:44 +0100696 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
697
698 lyd_free_all(tree);
699
700 /* parse an error reply */
701 data = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"55\">\n"
702 " <rpc-error>\n"
703 " <error-type>rpc</error-type>\n"
704 " <error-tag>missing-attribute</error-tag>\n"
705 " <error-severity>error</error-severity>\n"
706 " <error-info>\n"
707 " <bad-attribute>message-id</bad-attribute>\n"
708 " <bad-element>rpc</bad-element>\n"
709 " </error-info>\n"
710 " </rpc-error>\n"
711 "</rpc-reply>\n";
712 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100713 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 +0100714 ly_in_free(in, 0);
715
716 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 1, 1, LY_PREF_XML, "rpc-reply", 0, 0, 0, 0, "");
717 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)lyd_child(tree), 0, 1, LY_PREF_XML, "rpc-error", 0, 0, 0, 0, "");
718
Michal Vaskoe0665742021-02-11 11:08:44 +0100719 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
720
721 lyd_free_all(tree);
722
723 lyd_free_all(action);
724
725 /* wrong namespace, element name, whatever... */
726 /* TODO */
727}
728
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100729int
730main(void)
Radek Krejci509e2592019-05-15 16:30:48 +0200731{
732 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200733 UTEST(test_leaf, setup),
734 UTEST(test_anydata, setup),
735 UTEST(test_list, setup),
736 UTEST(test_container, setup),
737 UTEST(test_opaq, setup),
738 UTEST(test_rpc, setup),
739 UTEST(test_action, setup),
740 UTEST(test_notification, setup),
741 UTEST(test_reply, setup),
Michal Vaskoe0665742021-02-11 11:08:44 +0100742 UTEST(test_netconf_rpc, setup),
743 UTEST(test_netconf_action, setup),
744 UTEST(test_netconf_reply_or_notification, setup),
Radek Krejci509e2592019-05-15 16:30:48 +0200745 };
746
747 return cmocka_run_group_tests(tests, NULL, NULL);
748}