blob: bb56e8d38d6ea04822026c678da1be5742fda799 [file] [log] [blame]
Radek Išaa76902f2021-03-29 08:52:51 +02001/**
2 * @file list.c
3 * @author Radek Iša <isa@cesnet.cz>
4 * @brief test for list node
5 *
6 * Copyright (c) 2021 CESNET, z.s.p.o.
7 *
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 */
14
15/* INCLUDE UTEST HEADER */
16#define _UTEST_MAIN_
17#include "../utests.h"
18
19/* GLOBAL INCLUDE HEADERS */
20#include <ctype.h>
21
22/* LOCAL INCLUDE HEADERS */
23#include "libyang.h"
24#include "path.h"
25
26#define LYD_TREE_CREATE(INPUT, MODEL) \
27 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, MODEL)
28
29#define MODULE_CREATE_YIN(MOD_NAME, NODES) \
30 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
31 "<module name=\"" MOD_NAME "\"\n" \
32 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" \
33 " xmlns:pref=\"urn:tests:" MOD_NAME "\">\n" \
34 " <yang-version value=\"1.1\"/>\n" \
35 " <namespace uri=\"urn:tests:" MOD_NAME "\"/>\n" \
36 " <prefix value=\"pref\"/>\n" \
37 NODES \
38 "</module>\n"
39
40#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
41 "module " MOD_NAME " {\n" \
42 " yang-version 1.1;\n" \
43 " namespace \"urn:tests:" MOD_NAME "\";\n" \
44 " prefix pref;\n" \
45 NODES \
46 "}\n"
47
48static void
49test_schema_yang(void **state)
50{
51 const char *schema;
Michal Vasko4de7d072021-07-09 09:13:18 +020052 struct lys_module *mod;
Radek Išaa76902f2021-03-29 08:52:51 +020053 struct lysc_node_list *lysc_leaf;
54 struct lysc_node *lysc_node;
Radek Išaa76902f2021-03-29 08:52:51 +020055
56 schema = MODULE_CREATE_YANG("T0", "list user {"
57 "key uid;"
58 "unique name;"
59 "leaf uid{type int32;}"
60 "leaf name{type string;}"
61 "leaf group{type string;}"
62 "}");
63 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
64 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +020065 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +020066 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
67 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +020068 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +020069 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +020070 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +020071 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +020072 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +020073
74 schema = MODULE_CREATE_YANG("T1", "list user {"
75 "key uid;"
76 "container name{"
77 " leaf fist {type string;}"
78 " container second{leaf sub { type int32;}}"
79 "}"
80 "leaf uid{type int32;}"
81 "}");
82 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
83 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +020084 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +020085 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
86 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +020087 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +020088 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +020089 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "name", 0, LYS_CONTAINER, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +020090
91 schema = MODULE_CREATE_YANG("T2", "list grup {"
92 "key \"guid\";"
93 "leaf guid{type int32;}"
94 "list users{ key name; leaf name {type string;}}"
95 "}");
96 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
97 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +020098 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "grup", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +020099 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
100 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200101 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "guid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200102 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200103 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "users", 0, LYS_LIST, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200104
105 /* restriction */
106 schema = MODULE_CREATE_YANG("T3", "list grup {"
107 "key guid;"
108 "min-elements 10;"
109 "max-elements 20;"
110 "leaf guid{type int32;}"
111 "list users{ key name; leaf name {type string;}}"
112 "}");
113 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
114 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200115 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_ORDBY_SYSTEM, 1, \
116 "grup", 0, 0, 0, 0, 0, 1, 20, 10, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200117 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200118 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "guid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200119 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200120 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "users", 0, LYS_LIST, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200121
122 schema = MODULE_CREATE_YANG("T4", "list user {"
123 "key \"uid name\";"
124 "unique name;"
125 "leaf uid{type int32;}"
126 "leaf name{type string;}"
127 "leaf group{type string;}"
128 "}");
129 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
130 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200131 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200132 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
133 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200134 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200135 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200136 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE | LYS_KEY, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200137 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200138 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200139
140 schema = MODULE_CREATE_YANG("T5", "list rule {"
141 "key \"id\";"
142 "unique \"name\";"
143 "unique \"ip port\";"
144 "leaf id{type int32;}"
145 "leaf name{type string;}"
146 "leaf ip{type string;}"
147 "leaf port{type int16;}"
148 "}");
149 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
150 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200151 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "rule", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200152 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 2, 0);
153 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200154 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "id", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200155 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200156 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200157 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200158 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "ip", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200159 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200160 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200161
162 /* test error */
163 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
164 "key uid;"
165 "min-elements 10;"
166 "max-elements -1;"
167 "leaf uid{type int32;}"
168 "leaf name{type string;}"
169 "leaf group{type string;}"
170 "}");
171 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100172 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
173 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200174
175 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
176 "key uid;"
177 "min-elements 10;"
178 "max-elements 4294967298;"
179 "leaf uid{type int32;}"
180 "leaf name{type string;}"
181 "leaf group{type string;}"
182 "}");
183 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100184 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
185 CHECK_LOG_CTX("Value \"4294967298\" is out of \"max-elements\" bounds.", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200186
187 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
188 "key uid;"
189 "min-elements 20;"
190 "max-elements 10;"
191 "leaf uid{type int32;}"
192 "leaf name{type string;}"
193 "leaf group{type string;}"
194 "}");
195 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200196 CHECK_LOG_CTX("List min-elements 20 is bigger than max-elements 10.", "/TERR_0:user");
Radek Išaa76902f2021-03-29 08:52:51 +0200197
198 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
199 "key uid;"
200 "min-elements -1;"
201 "max-elements 20;"
202 "leaf uid{type int32;}"
203 "leaf name{type string;}"
204 "leaf group{type string;}"
205 "}");
206 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100207 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
208 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200209
210 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
211 "key uid;"
212 "key name;"
213 "leaf uid{type int32;}"
214 "leaf name{type string;}"
215 "leaf group{type string;}"
216 "}");
217 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100218 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
219 CHECK_LOG_CTX("Duplicate keyword \"key\".", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200220
221 schema = MODULE_CREATE_YANG("T6", "list user {"
222 "config false;"
223 "leaf uid{type int32;}"
224 "leaf name{type string;}"
225 "leaf group{type string;}"
226 "}");
227 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
228 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200229 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_KEYLESS | LYS_SET_CONFIG, \
230 1, "user", 0, 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200231 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200232 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200233 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200234 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200235 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200236 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200237
238 schema = MODULE_CREATE_YANG("T7", "list user {"
239 "key uid;"
240 "unique name;"
241 "ordered-by user;"
242 "leaf uid{type int32;}"
243 "leaf name{type string;}"
244 "leaf group{type string;}"
245 "}");
246 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
247 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200248 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200249 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
250 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200251 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200252 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200253 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200254 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200255 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200256
257 schema = MODULE_CREATE_YANG("T8", "list user {"
258 "key uid;"
259 "unique name;"
260 "ordered-by system;"
261 "leaf uid{type int32;}"
262 "leaf name{type string;}"
263 "leaf group{type string;}"
264 "}");
265 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
266 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200267 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200268 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
269 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200270 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200271 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200272 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200273 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200274 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200275
276 schema = MODULE_CREATE_YANG("TERROR0", "list user {"
277 "key uid;"
278 "unique name;"
279 "ordered-by systeme;"
280 "leaf uid{type int32;}"
281 "leaf name{type string;}"
282 "leaf group{type string;}"
283 "}");
284 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100285 CHECK_LOG_CTX("Parsing module \"TERROR0\" failed.", NULL);
286 CHECK_LOG_CTX("Invalid value \"systeme\" of \"ordered-by\".", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200287
288 schema = MODULE_CREATE_YANG("TERROR0", "list \"\" {"
289 "key uid;"
290 "unique name;"
291 "ordered-by system;"
292 "leaf uid{type int32;}"
293 "leaf name{type string;}"
294 "leaf group{type string;}"
295 "}");
296 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100297 CHECK_LOG_CTX("Parsing module \"TERROR0\" failed.", NULL);
298 CHECK_LOG_CTX("Statement argument is required.", "Line number 5.");
Radek Išaa76902f2021-03-29 08:52:51 +0200299
300 schema = MODULE_CREATE_YANG("T9", "list user {"
301 "key uid;"
302 "unique name;"
303 "ordered-by system;"
304 "leaf uid{type int32;}"
305 "leaf name{type string;}"
306 "leaf group{type string; default \"abcd\";}"
307 "}");
308 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
309 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200310 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200311 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
312 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200313 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200314 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200315 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200316 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200317 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200318
319 schema = MODULE_CREATE_YANG("T10", "list user {"
320 "key uid;"
321 "leaf uid{type int32; default \"25\";}"
322 "leaf name{type string;}"
323 "leaf group{type string;}"
324 "}");
325 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
326 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200327 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200328 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
329 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200330 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY | LYS_SET_DFLT, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200331 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200332 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200333 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200334 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200335
336 schema = MODULE_CREATE_YANG("T11",
337 "typedef my_type {"
338 " type int8; default \"25\";"
339 "}"
340 "list user {"
341 " key uid;"
342 " leaf uid{type my_type;}"
343 " leaf name{type string;}"
344 " leaf group{type string;}"
345 "}");
346 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
347 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200348 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200349 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
350 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200351 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200352 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200353 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200354 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200355 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200356
357}
358
359static void
360test_schema_yin(void **state)
361{
362 const char *schema;
Michal Vasko4de7d072021-07-09 09:13:18 +0200363 struct lys_module *mod;
Radek Išaa76902f2021-03-29 08:52:51 +0200364 struct lysc_node_list *lysc_leaf;
365 struct lysc_node *lysc_node;
Radek Išaa76902f2021-03-29 08:52:51 +0200366
367 schema = MODULE_CREATE_YIN("T0", "<list name=\"user\">"
368 " <key value=\"uid\"/>"
369 " <unique tag=\"name\"/>"
370 " <leaf name=\"uid\"><type name=\"int32\"/></leaf>"
371 " <leaf name=\"name\"><type name=\"string\"/></leaf>"
372 " <leaf name=\"group\"><type name=\"string\"/></leaf>"
373 "</list>");
374 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
375 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200376 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200377 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
378 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200379 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200380 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200381 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200382 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200383 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200384
385 schema = MODULE_CREATE_YIN("T00", "<list name=\"user\">"
386 " <key value=\"u&lt;id\"/>"
387 " <leaf name=\"uid\"><type name=\"int32\"/></leaf>"
388 " <leaf name=\"name\"><type name=\"string\"/></leaf>"
389 " <leaf name=\"group\"><type name=\"string\"/></leaf>"
390 "</list>");
391 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200392 CHECK_LOG_CTX("The list's key \"u<id\" not found.", "/T00:user");
Radek Išaa76902f2021-03-29 08:52:51 +0200393
394 schema = MODULE_CREATE_YIN("T1", "<list name=\"user\"> "
395 " <key value=\"uid\"/>"
396 " <container name=\"name\">"
397 " <leaf name=\"fist\"> <type name=\"string\"/> </leaf>"
398 " <container name=\"second\">"
399 " <leaf name=\"sub\"> <type name=\"int32\"/></leaf>"
400 " </container>"
401 " </container>"
402 " <leaf name=\"uid\"> <type name=\"int32\"/></leaf>"
403 "</list>");
404 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
405 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200406 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200407 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
408 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200409 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200410 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200411 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "name", 0, LYS_CONTAINER, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200412
413 schema = MODULE_CREATE_YIN("T2", "<list name=\"grup\">"
414 "<key value=\"guid\"/>"
415 "<leaf name=\"guid\"> <type name=\"int32\"/> </leaf>"
416 "<list name=\"users\">"
417 " <key value=\"name\"/>"
418 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
419 "</list>"
420 "</list>");
421 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
422 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200423 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "grup", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200424 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
425 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200426 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "guid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200427 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200428 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "users", 0, LYS_LIST, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200429
430 /* restriction */
431 schema = MODULE_CREATE_YIN("T3",
432 "<list name = \"grup\">"
433 " <key value=\"guid\"/>"
434 " <min-elements value=\"10\"/>"
435 " <max-elements value=\"20\"/>"
436 " <leaf name=\"guid\"> <type name=\"int32\"/> </leaf>"
437 " <list name=\"users\"> <key value=\"name\"/>"
438 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
439 " </list>"
440 "</list>");
441 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
442 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200443 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_ORDBY_SYSTEM, 1, "grup", \
444 0, 0, 0, 0, 0, 1, 20, 10, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200445 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200446 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "guid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200447 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200448 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "users", 0, LYS_LIST, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200449
Radek Išabe8a6532021-03-29 08:54:22 +0200450 schema = MODULE_CREATE_YIN("T4",
Radek Išaa76902f2021-03-29 08:52:51 +0200451 "<list name=\"user\">"
452 " <key value=\"uid name\"/>"
453 " <unique tag=\"name\"/>"
454 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
455 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
456 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
457 "</list>");
458 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
459 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200460 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200461 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
462 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200463 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200464 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200465 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE | LYS_KEY, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200466 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200467 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200468
469 schema = MODULE_CREATE_YIN("T5",
470 "<list name=\"rule\">"
471 " <key value=\"id\"/>"
472 " <unique tag=\"name\"/>"
473 " <unique tag=\"ip port\"/>"
474 " <leaf name=\"id\"> <type name=\"int32\"/></leaf>"
475 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
476 " <leaf name=\"ip\"> <type name=\"string\"/> </leaf>"
477 " <leaf name=\"port\"> <type name=\"int16\"/> </leaf>"
478 "</list>");
479 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
480 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200481 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "rule", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200482 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 2, 0);
483 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200484 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "id", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200485 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200486 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200487 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200488 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "ip", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200489 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200490 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "port", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200491
492 /* test error */
493 schema = MODULE_CREATE_YIN("TERR_0",
494 "<list name=\"user\">"
495 " <key value=\"uid\"/>"
496 " <min-elements value=\"10\"/>"
497 " <max-elements value=\"-1\"/>"
498 " <leaf name=\"uid\"> <type name=\"int32\"> </leaf>"
499 "</list>");
500 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100501 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
502 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"max-elements\" element.", "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200503
504 schema = MODULE_CREATE_YIN("TERR_0",
505 "<list name=\"user\">"
506 " <key value=\"uid\"/>"
507 " <min-elements value=\"10\"/>"
508 " <max-elements value=\"4294967298\"/>"
509 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
510 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
511 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
512 "</list>");
513 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100514 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
515 CHECK_LOG_CTX("Value \"4294967298\" of \"value\" attribute in \"max-elements\" element is out of bounds.", "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200516
517 schema = MODULE_CREATE_YIN("TERR_0",
518 "<list name=\"user\">"
519 " <key value=\"uid\"/>"
520 " <min-elements value=\"20\"/>"
521 " <max-elements value=\"10\"/>"
522 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
523 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
524 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
525 "</list>");
526 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100527 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
528 CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 20 is bigger than the max value 10.",
529 "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200530
531 schema = MODULE_CREATE_YIN("TERR_0",
532 "<list name=\"user\">"
533 " <key value=\"uid\"/>"
534 " <min-elements value=\"-1\"/>"
535 " <max-elements value=\"20\"/>"
536 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
537 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
538 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
539 "</list>");
540 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100541 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
542 CHECK_LOG_CTX("Value \"-1\" of \"value\" attribute in \"min-elements\" element is out of bounds.", "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200543
544 schema = MODULE_CREATE_YIN("TERR_0",
545 "<list name=\"user\">"
546 " <key value=\"uid\"/>"
547 " <key value=\"name\"/>"
548 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
549 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
550 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
551 "</list>");
552 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100553 CHECK_LOG_CTX("Parsing module \"TERR_0\" failed.", NULL);
554 CHECK_LOG_CTX("Redefinition of \"key\" sub-element in \"list\" element.", "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200555
556 schema = MODULE_CREATE_YIN("T6",
557 "<list name=\"user\">"
558 " <config value=\"false\"/>"
559 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
560 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
561 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
562 "</list>");
563 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
564 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200565 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_KEYLESS | LYS_SET_CONFIG, \
566 1, "user", 0, 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200567 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200568 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200569 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200570 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200571 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200572 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200573
574 schema = MODULE_CREATE_YIN("T7",
575 "<list name=\"user\">"
576 " <key value=\"uid\"/>"
577 " <unique tag=\"name\"/>"
578 " <ordered-by value=\"user\"/>"
579 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
580 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
581 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
582 "</list>");
583 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
584 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200585 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200586 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
587 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200588 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200589 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200590 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200591 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200592 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200593
594 schema = MODULE_CREATE_YIN("T8",
595 "<list name=\"user\">"
596 " <key value=\"uid\"/>"
597 " <unique tag=\"name\"/>"
598 " <ordered-by value=\"system\"/>"
599 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
600 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
601 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
602 "</list>");
603 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
604 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200605 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200606 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
607 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200608 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200609 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200610 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200611 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200612 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200613
614 schema = MODULE_CREATE_YIN("TERROR0",
615 "<list name=\"user\">"
616 " <key value=\"uid\"/>"
617 " <unique tag=\"name\"/>"
618 " <ordered-by value=\"systeme\"/>"
619 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
620 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
621 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
622 "</list>");
623 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100624 CHECK_LOG_CTX("Parsing module \"TERROR0\" failed.", NULL);
625 CHECK_LOG_CTX("Invalid value \"systeme\" of \"value\" attribute in \"ordered-by\" element. Valid values are \"system\" and \"user\".",
Michal Vasko4fd91922021-09-15 08:51:06 +0200626 "Line number 8.");
Radek Išaa76902f2021-03-29 08:52:51 +0200627
628 schema = MODULE_CREATE_YIN("T_DEFS1",
629 "<list name=\"user\">"
630 " <key value=\"uid\"/>"
631 " <unique tag=\"name\"/>"
632 " <ordered-by value=\"system\"/>"
633 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
634 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
635 " <leaf name=\"group\"><type name=\"string\"/> <default value=\"ath\"/> </leaf>"
636 "</list>");
637 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
638 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200639 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "user", 0, \
Radek Išaa76902f2021-03-29 08:52:51 +0200640 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
641 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200642 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "uid", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200643 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200644 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_UNIQUE, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200645 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200646 CHECK_LYSC_NODE(lysc_node, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "group", 0, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200647
648}
649
650static void
651test_schema_print(void **state)
652{
653 const char *schema_yang, *schema_yin;
654 char *printed;
Michal Vasko4de7d072021-07-09 09:13:18 +0200655 struct lys_module *mod;
Radek Išaa76902f2021-03-29 08:52:51 +0200656
657 /* test print yang to yin */
658 schema_yang = MODULE_CREATE_YANG("PRINT0",
659 "list user {"
660 " min-elements 10;"
661 " max-elements 20;"
662 " key \"uid name\";"
663 " unique name;"
664 " leaf uid{type int32;}"
665 " leaf name{type string;}"
666 " leaf group{type string;}"
667 "}");
668 schema_yin = MODULE_CREATE_YIN("PRINT0",
669 " <list name=\"user\">\n"
670 " <key value=\"uid name\"/>\n"
671 " <unique tag=\"name\"/>\n"
672 " <min-elements value=\"10\"/>\n"
673 " <max-elements value=\"20\"/>\n"
674 " <leaf name=\"uid\">\n"
675 " <type name=\"int32\"/>\n"
676 " </leaf>\n"
677 " <leaf name=\"name\">\n"
678 " <type name=\"string\"/>\n"
679 " </leaf>\n"
680 " <leaf name=\"group\">\n"
681 " <type name=\"string\"/>\n"
682 " </leaf>\n"
683 " </list>\n");
684
685 UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod);
686 assert_non_null(mod);
687 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
688 assert_string_equal(printed, schema_yin);
689 free(printed);
690
691 /* test print yin to yang */
692 schema_yang = MODULE_CREATE_YANG("PRINT1",
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200693 "\n"
Radek Išaa76902f2021-03-29 08:52:51 +0200694 " list user {\n"
695 " key \"uid name\";\n"
696 " unique \"name\";\n"
697 " min-elements 10;\n"
698 " max-elements 20;\n"
699 " leaf uid {\n"
700 " type int32;\n"
701 " }\n"
702 " leaf name {\n"
703 " type string;\n"
704 " }\n"
705 " leaf group {\n"
706 " type string;\n"
707 " }\n"
708 " }\n");
709 schema_yin = MODULE_CREATE_YIN("PRINT1",
710 " <list name=\"user\">\n"
711 " <key value=\"uid name\"/>\n"
712 " <unique tag=\"name\"/>\n"
713 " <min-elements value=\"10\"/>\n"
714 " <max-elements value=\"20\"/>\n"
715 " <leaf name=\"uid\">\n"
716 " <type name=\"int32\"/>\n"
717 " </leaf>\n"
718 " <leaf name=\"name\">\n"
719 " <type name=\"string\"/>\n"
720 " </leaf>\n"
721 " <leaf name=\"group\">\n"
722 " <type name=\"string\"/>\n"
723 " </leaf>\n"
724 " </list>\n");
725
726 UTEST_ADD_MODULE(schema_yin, LYS_IN_YIN, NULL, &mod);
727 assert_non_null(mod);
728 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
729 assert_string_equal(printed, schema_yang);
730 free(printed);
731
732 /* test no segmentation fold due ignoring default value */
733 schema_yang = MODULE_CREATE_YANG("PRINT2", "list user {"
734 "key uid;"
735 "leaf uid{type int32; default \"25\";}"
736 "leaf name{type string;}"
737 "leaf group{type string;}"
738 "}");
739
740 UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod);
741 assert_non_null(mod);
742 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
743 free(printed);
744}
745
746static void
747test_xml(void **state)
748{
749 struct lyd_node *tree;
750 const char *data, *schema;
751 struct lyd_node_inner *list_tree;
752 struct lyd_node_term *list_leaf;
753
754 schema = MODULE_CREATE_YANG("T0", "list user {"
755 "key uid;"
756 "unique \"name group\";"
757 "leaf uid{type uint32;}"
758 "leaf name{type string;}"
759 "leaf group{type string;}"
760 "}");
761 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
762
763 /* add data */
764 data =
765 "<user xmlns=\"urn:tests:T0\">"
766 " <uid>0</uid>"
767 " <name>Tomáš Novák</name>"
768 " <group>User</group>"
769 "</user>"
770 "<user xmlns=\"urn:tests:T0\">"
771 " <uid>1</uid>"
772 " <name>Martin Novák</name>"
773 " <group>User</group>"
774 "</user>";
775 /* check first item */
776 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
777 list_tree = (void *)tree;
778 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
779 list_leaf = (void *) list_tree->child;
780 assert_string_equal(list_leaf->schema->name, "uid");
781 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
782 list_leaf = (void *) list_leaf->next;
783 assert_string_equal(list_leaf->schema->name, "name");
784 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
785 list_leaf = (void *) list_leaf->next;
786 assert_string_equal(list_leaf->schema->name, "group");
787 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
788 /* check second item */
789 list_tree = (void *) list_tree->next;
790 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
791 list_leaf = (void *) list_tree->child;
792 assert_string_equal(list_leaf->schema->name, "uid");
793 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
794 list_leaf = (void *) list_leaf->next;
795 assert_string_equal(list_leaf->schema->name, "name");
796 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Martin Novák");
797 list_leaf = (void *) list_leaf->next;
798 assert_string_equal(list_leaf->schema->name, "group");
799 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
800 lyd_free_all(tree);
801
802 /* add data */
803 data =
804 "<user xmlns=\"urn:tests:T0\">"
805 " <uid>0</uid>"
806 " <name>Tomáš Novák</name>"
807 " <group>User</group>"
808 "</user>"
809 "<user xmlns=\"urn:tests:T0\">"
810 " <uid>1</uid>"
811 " <name>Tomáš Novák</name>"
812 " <group>Admin</group>"
813 "</user>";
814 /* check first item */
815 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
816 list_tree = (void *)tree;
817 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
818 list_leaf = (void *) list_tree->child;
819 assert_string_equal(list_leaf->schema->name, "uid");
820 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
821 list_leaf = (void *) list_leaf->next;
822 assert_string_equal(list_leaf->schema->name, "name");
823 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
824 list_leaf = (void *) list_leaf->next;
825 assert_string_equal(list_leaf->schema->name, "group");
826 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
827 /* check second item */
828 list_tree = (void *) list_tree->next;
829 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
830 list_leaf = (void *) list_tree->child;
831 assert_string_equal(list_leaf->schema->name, "uid");
832 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
833 list_leaf = (void *) list_leaf->next;
834 assert_string_equal(list_leaf->schema->name, "name");
835 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
836 list_leaf = (void *) list_leaf->next;
837 assert_string_equal(list_leaf->schema->name, "group");
838 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
839 lyd_free_all(tree);
840
841 data =
842 "<user xmlns=\"urn:tests:T0\">"
843 " <uid>0</uid>"
844 " <name>Tomáš Novák</name>"
845 " <group>User</group>"
846 "</user>"
847 "<user xmlns=\"urn:tests:T0\">"
848 " <uid>0</uid>"
849 " <name>Tomáš Novák</name>"
850 " <group>Admin</group>"
851 "</user>";
852 /* check first item */
853 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
854 assert_null(tree);
855 CHECK_LOG_CTX("Duplicate instance of \"user\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200856 "Data location \"/T0:user[uid='0']\".");
Radek Išaa76902f2021-03-29 08:52:51 +0200857
858 data =
859 "<user xmlns=\"urn:tests:T0\">"
860 " <uid>0</uid>"
861 " <name>Tomáš Novák</name>"
862 " <group>User</group>"
863 "</user>"
864 "<user xmlns=\"urn:tests:T0\">"
865 " <uid>1</uid>"
866 " <name>Tomáš Novák</name>"
867 " <group>User</group>"
868 "</user>";
869 /* check first item */
870 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
871 assert_null(tree);
872 CHECK_LOG_CTX("Unique data leaf(s) \"name group\" not satisfied in \"/T0:user[uid='0']\" and \"/T0:user[uid='1']\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200873 "Data location \"/T0:user[uid='1']\".");
Radek Išaa76902f2021-03-29 08:52:51 +0200874
875 /* double key */
876 schema = MODULE_CREATE_YANG("T1", "list user {"
877 "key \"uid group\";"
878 "leaf uid{type uint32;}"
879 "leaf name{type string;}"
880 "leaf group{type string;}"
881 "}");
882 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
883 data =
884 "<user xmlns=\"urn:tests:T1\">"
885 " <uid>0</uid>"
886 " <name>Tomáš Novák</name>"
887 " <group>User</group>"
888 "</user>"
889 "<user xmlns=\"urn:tests:T1\">"
890 " <uid>0</uid>"
891 " <name>Tomáš Novák</name>"
892 " <group>Admin</group>"
893 "</user>";
894 /* check first item */
895 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
896 list_tree = (void *)tree;
897 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
898 list_leaf = (void *) list_tree->child;
899 assert_string_equal(list_leaf->schema->name, "uid");
900 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
901 list_leaf = (void *) list_leaf->next;
902 assert_string_equal(list_leaf->schema->name, "group");
903 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "User");
904 list_leaf = (void *) list_leaf->next;
905 assert_string_equal(list_leaf->schema->name, "name");
906 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Tomáš Novák");
907 /* check second item */
908 list_tree = (void *) list_tree->next;
909 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
910 list_leaf = (void *) list_tree->child;
911 assert_string_equal(list_leaf->schema->name, "uid");
912 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
913 list_leaf = (void *) list_leaf->next;
914 assert_string_equal(list_leaf->schema->name, "group");
915 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Admin");
916 list_leaf = (void *) list_leaf->next;
917 assert_string_equal(list_leaf->schema->name, "name");
918 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Tomáš Novák");
919 lyd_free_all(tree);
920
921 data =
922 "<user xmlns=\"urn:tests:T1\">"
923 " <uid>0</uid>"
924 " <name>Tomáš Novák</name>"
925 " <group>User</group>"
926 "</user>"
927 "<user xmlns=\"urn:tests:T1\">"
928 " <uid>0</uid>"
929 " <name>Tomáš Novák</name>"
930 " <group>User</group>"
931 "</user>";
932 /* check first item */
933 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
934 assert_null(tree);
935 CHECK_LOG_CTX("Duplicate instance of \"user\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200936 "Data location \"/T1:user[uid='0'][group='User']\".");
Radek Išaa76902f2021-03-29 08:52:51 +0200937
938 /* min elements max elements */
939 schema = MODULE_CREATE_YANG("T2",
940 "list user {"
941 " key uid;"
942 " min-elements 3;"
943 " max-elements 5;"
944 " leaf uid{type uint32;}"
945 " leaf name{type string;}"
946 " leaf group{type string;}"
947 "}");
948 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
949 data =
950 "<user xmlns=\"urn:tests:T2\">"
951 " <uid>0</uid>"
952 " <name>Tomáš Novák</name>"
953 " <group>User</group>"
954 "</user>"
955 "<user xmlns=\"urn:tests:T2\">"
956 " <uid>1</uid>"
957 " <name>Tomáš Novák</name>"
958 " <group>Admin</group>"
959 "</user>"
960 "<user xmlns=\"urn:tests:T2\">"
961 " <uid>2</uid>"
962 " <name>Tomáš Jak</name>"
963 " <group>Admin</group>"
964 "</user>";
965 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
966 list_tree = (void *)tree;
967 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
968 list_leaf = (void *) list_tree->child;
969 assert_string_equal(list_leaf->schema->name, "uid");
970 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
971 list_leaf = (void *) list_leaf->next;
972 assert_string_equal(list_leaf->schema->name, "name");
973 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
974 list_leaf = (void *) list_leaf->next;
975 assert_string_equal(list_leaf->schema->name, "group");
976 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
977 /* check second item */
978 list_tree = (void *) list_tree->next;
979 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
980 list_leaf = (void *) list_tree->child;
981 assert_string_equal(list_leaf->schema->name, "uid");
982 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
983 list_leaf = (void *) list_leaf->next;
984 assert_string_equal(list_leaf->schema->name, "name");
985 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
986 list_leaf = (void *) list_leaf->next;
987 assert_string_equal(list_leaf->schema->name, "group");
988 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
989 /* check third item */
990 list_tree = (void *) list_tree->next;
991 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
992 list_leaf = (void *) list_tree->child;
993 assert_string_equal(list_leaf->schema->name, "uid");
994 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "2", 2);
995 list_leaf = (void *) list_leaf->next;
996 assert_string_equal(list_leaf->schema->name, "name");
997 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Jak");
998 list_leaf = (void *) list_leaf->next;
999 assert_string_equal(list_leaf->schema->name, "group");
1000 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1001 lyd_free_all(tree);
1002
1003 data =
1004 "<user xmlns=\"urn:tests:T2\">"
1005 " <uid>0</uid>"
1006 " <name>Tomáš Novák</name>"
1007 " <group>User</group>"
1008 "</user>"
1009 "<user xmlns=\"urn:tests:T2\">"
1010 " <uid>1</uid>"
1011 " <name>Tomáš Novák</name>"
1012 " <group>Admin</group>"
1013 "</user>"
1014 "<user xmlns=\"urn:tests:T2\">"
1015 " <uid>2</uid>"
1016 " <name>Tomáš Novák</name>"
1017 " <group>Admin</group>"
1018 "</user>"
1019 "<user xmlns=\"urn:tests:T2\">"
1020 " <uid>3</uid>"
1021 " <name>Tomáš Novák</name>"
1022 " <group>Admin</group>"
1023 "</user>"
1024 "<user xmlns=\"urn:tests:T2\">"
1025 " <uid>4</uid>"
1026 " <name>Tomáš Novák</name>"
1027 " <group>Admin</group>"
1028 "</user>";
1029 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1030 list_tree = (void *)tree;
1031 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1032 list_tree = (void *) list_tree->next;
1033 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1034 list_tree = (void *) list_tree->next;
1035 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1036 list_tree = (void *) list_tree->next;
1037 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1038 list_tree = (void *) list_tree->next;
1039 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1040 lyd_free_all(tree);
1041
1042 /* check wrong number of items */
1043 data =
1044 "<user xmlns=\"urn:tests:T2\">"
1045 " <uid>0</uid>"
1046 " <name>Tomáš Novák</name>"
1047 " <group>User</group>"
1048 "</user>"
1049 "<user xmlns=\"urn:tests:T2\">"
1050 " <uid>1</uid>"
1051 " <name>Tomáš Novák</name>"
1052 " <group>Admin</group>"
1053 "</user>";
1054 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1055 assert_null(tree);
1056 CHECK_LOG_CTX("Too few \"user\" instances.",
Michal Vasko959f8d82022-06-16 07:51:50 +02001057 "Schema location \"/T2:user\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001058
1059 data =
1060 "<user xmlns=\"urn:tests:T2\">"
1061 " <uid>0</uid>"
1062 " <name>Tomáš Novák</name>"
1063 " <group>User</group>"
1064 "</user>"
1065 "<user xmlns=\"urn:tests:T2\">"
1066 " <uid>1</uid>"
1067 " <name>Tomáš Novák</name>"
1068 " <group>Admin</group>"
1069 "</user>"
1070 "<user xmlns=\"urn:tests:T2\">"
1071 " <uid>2</uid>"
1072 " <name>Tomáš Novák</name>"
1073 " <group>Admin</group>"
1074 "</user>"
1075 "<user xmlns=\"urn:tests:T2\">"
1076 " <uid>3</uid>"
1077 " <name>Tomáš Novák</name>"
1078 " <group>Admin</group>"
1079 "</user>"
1080 "<user xmlns=\"urn:tests:T2\">"
1081 " <uid>4</uid>"
1082 " <name>Tomáš Novák</name>"
1083 " <group>Admin</group>"
1084 "</user>"
1085 "<user xmlns=\"urn:tests:T2\">"
1086 " <uid>5</uid>"
1087 " <name>Tomáš Novák</name>"
1088 " <group>Admin</group>"
1089 "</user>";
1090 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1091 assert_null(tree);
1092 CHECK_LOG_CTX("Too many \"user\" instances.",
Michal Vaskodbf3e652022-10-21 08:46:25 +02001093 "Data location \"/T2:user[uid='5']\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001094
1095 /* empty list */
1096 schema = MODULE_CREATE_YANG("T_EMPTY_LIST",
1097 "container user_list {"
1098 " list user {"
1099 " key uid;"
1100 " unique \"name group\";"
1101 " leaf uid{type uint32;}"
1102 " leaf name{type string;}"
1103 " leaf group{type string;}"
1104 "}"
1105 "}");
1106 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1107
1108 /* empty list */
1109 data = "<user_list xmlns=\"urn:tests:T_EMPTY_LIST\"/>";
1110 /* check first item */
1111 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1112 list_tree = (void *)tree;
1113 CHECK_LYD_NODE_INNER(list_tree, 0, 0, 0, 1, 0, 0, 0, 1);
1114 lyd_free_all(tree);
1115}
1116
1117static void
1118test_json(void **state)
1119{
1120 struct lyd_node *tree;
1121 const char *data, *schema;
1122 struct lyd_node_inner *list_tree;
1123 struct lyd_node_term *list_leaf;
1124
1125 schema = MODULE_CREATE_YANG("T0", "list user {"
1126 "key uid;"
1127 "unique \"name group\";"
1128 "leaf uid{type uint32;}"
1129 "leaf name{type string;}"
1130 "leaf group{type string;}"
1131 "}");
1132 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1133
1134 /* add data */
1135 data =
1136 "{\"T0:user\": ["
1137 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1138 " {\"uid\":1, \"name\":\"Martin Novák\", \"group\":\"User\"}"
1139 "]}";
1140
1141 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1142 list_tree = (void *)tree;
1143 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1144 list_leaf = (void *) list_tree->child;
1145 assert_string_equal(list_leaf->schema->name, "uid");
1146 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1147 list_leaf = (void *) list_leaf->next;
1148 assert_string_equal(list_leaf->schema->name, "name");
1149 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1150 list_leaf = (void *) list_leaf->next;
1151 assert_string_equal(list_leaf->schema->name, "group");
1152 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1153 /* check second item */
1154 list_tree = (void *) list_tree->next;
1155 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1156 list_leaf = (void *) list_tree->child;
1157 assert_string_equal(list_leaf->schema->name, "uid");
1158 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1159 list_leaf = (void *) list_leaf->next;
1160 assert_string_equal(list_leaf->schema->name, "name");
1161 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Martin Novák");
1162 list_leaf = (void *) list_leaf->next;
1163 assert_string_equal(list_leaf->schema->name, "group");
1164 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1165 lyd_free_all(tree);
1166
1167 /* Unique */
1168 data =
1169 "{\"T0:user\": ["
1170 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1171 " {\"uid\":1, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1172 "]}";
1173
1174 /* check first item */
1175 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1176 list_tree = (void *)tree;
1177 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1178 list_leaf = (void *) list_tree->child;
1179 assert_string_equal(list_leaf->schema->name, "uid");
1180 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1181 list_leaf = (void *) list_leaf->next;
1182 assert_string_equal(list_leaf->schema->name, "name");
1183 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1184 list_leaf = (void *) list_leaf->next;
1185 assert_string_equal(list_leaf->schema->name, "group");
1186 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1187 /* check second item */
1188 list_tree = (void *) list_tree->next;
1189 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1190 list_leaf = (void *) list_tree->child;
1191 assert_string_equal(list_leaf->schema->name, "uid");
1192 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1193 list_leaf = (void *) list_leaf->next;
1194 assert_string_equal(list_leaf->schema->name, "name");
1195 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1196 list_leaf = (void *) list_leaf->next;
1197 assert_string_equal(list_leaf->schema->name, "group");
1198 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1199 lyd_free_all(tree);
1200
1201 data =
1202 "{\"T0:user\": ["
1203 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1204 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1205 "]}";
1206 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1207 assert_null(tree);
1208 CHECK_LOG_CTX("Duplicate instance of \"user\".",
Michal Vaskof8ebf132022-11-21 14:06:48 +01001209 "Data location \"/T0:user[uid='0']\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001210
1211 data =
1212 "{\"T0:user\": ["
1213 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1214 " {\"uid\":1, \"name\":\"Jan Kuba\", \"group\":\"User\"}"
1215 "]}";
1216 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1217 assert_null(tree);
1218 CHECK_LOG_CTX("Unique data leaf(s) \"name group\" not satisfied in \"/T0:user[uid='0']\" and \"/T0:user[uid='1']\".",
Michal Vaskof8ebf132022-11-21 14:06:48 +01001219 "Data location \"/T0:user[uid='1']\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001220
1221 /* double key */
1222 schema = MODULE_CREATE_YANG("T1", "list user {"
1223 "key \"uid group\";"
1224 "leaf uid{type uint32;}"
1225 "leaf name{type string;}"
1226 "leaf group{type string;}"
1227 "}");
1228 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1229 data =
1230 "{\"T1:user\": ["
1231 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1232 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1233 "]}";
1234 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1235 list_tree = (void *)tree;
1236 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1237 list_leaf = (void *) list_tree->child;
1238 assert_string_equal(list_leaf->schema->name, "uid");
1239 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1240 list_leaf = (void *) list_leaf->next;
1241 assert_string_equal(list_leaf->schema->name, "group");
1242 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "User");
1243 list_leaf = (void *) list_leaf->next;
1244 assert_string_equal(list_leaf->schema->name, "name");
1245 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Jan Kuba");
1246 /* check second item */
1247 list_tree = (void *) list_tree->next;
1248 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1249 list_leaf = (void *) list_tree->child;
1250 assert_string_equal(list_leaf->schema->name, "uid");
1251 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1252 list_leaf = (void *) list_leaf->next;
1253 assert_string_equal(list_leaf->schema->name, "group");
1254 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Admin");
1255 list_leaf = (void *) list_leaf->next;
1256 assert_string_equal(list_leaf->schema->name, "name");
1257 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Jan Kuba");
1258 lyd_free_all(tree);
1259
1260 data =
1261 "{\"T1:user\": ["
1262 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1263 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"}"
1264 "]}";
1265 /* check first item */
1266 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1267 assert_null(tree);
1268 CHECK_LOG_CTX("Duplicate instance of \"user\".",
Michal Vaskof8ebf132022-11-21 14:06:48 +01001269 "Data location \"/T1:user[uid='0'][group='User']\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001270
1271 /* min elements max elements */
1272 schema = MODULE_CREATE_YANG("T2",
1273 "list user {"
1274 " key uid;"
1275 " min-elements 3;"
1276 " max-elements 5;"
1277 " leaf uid{type uint32;}"
1278 " leaf name{type string;}"
1279 " leaf group{type string;}"
1280 "}");
1281 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1282 data =
1283 "{\"T2:user\": ["
1284 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1285 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1286 " {\"uid\":2, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1287 "]}";
1288
1289 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1290 list_tree = (void *)tree;
1291 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1292 list_leaf = (void *) list_tree->child;
1293 assert_string_equal(list_leaf->schema->name, "uid");
1294 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1295 list_leaf = (void *) list_leaf->next;
1296 assert_string_equal(list_leaf->schema->name, "name");
1297 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1298 list_leaf = (void *) list_leaf->next;
1299 assert_string_equal(list_leaf->schema->name, "group");
1300 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1301 /* check second item */
1302 list_tree = (void *) list_tree->next;
1303 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1304 list_leaf = (void *) list_tree->child;
1305 assert_string_equal(list_leaf->schema->name, "uid");
1306 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1307 list_leaf = (void *) list_leaf->next;
1308 assert_string_equal(list_leaf->schema->name, "name");
1309 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Antonín Kuba");
1310 list_leaf = (void *) list_leaf->next;
1311 assert_string_equal(list_leaf->schema->name, "group");
1312 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1313 /* check third item */
1314 list_tree = (void *) list_tree->next;
1315 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1316 list_leaf = (void *) list_tree->child;
1317 assert_string_equal(list_leaf->schema->name, "uid");
1318 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "2", 2);
1319 list_leaf = (void *) list_leaf->next;
1320 assert_string_equal(list_leaf->schema->name, "name");
1321 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
1322 list_leaf = (void *) list_leaf->next;
1323 assert_string_equal(list_leaf->schema->name, "group");
1324 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1325 lyd_free_all(tree);
1326
1327 data =
1328 "{\"T2:user\": ["
1329 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1330 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1331 " {\"uid\":2, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1332 " {\"uid\":3, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1333 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1334 "]}";
1335 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1336 list_tree = (void *)tree;
1337 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1338 list_tree = (void *) list_tree->next;
1339 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1340 list_tree = (void *) list_tree->next;
1341 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1342 list_tree = (void *) list_tree->next;
1343 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1344 list_tree = (void *) list_tree->next;
1345 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1346 lyd_free_all(tree);
1347
1348 /* check wrong number of items */
1349 data =
1350 "{\"T2:user\": ["
1351 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1352 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1353 "]}";
1354 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1355 assert_null(tree);
1356 CHECK_LOG_CTX("Too few \"user\" instances.",
Michal Vaskof8ebf132022-11-21 14:06:48 +01001357 "Schema location \"/T2:user\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001358
1359 data =
1360 "{\"T2:user\": ["
1361 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1362 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1363 " {\"uid\":2, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1364 " {\"uid\":3, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1365 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"},"
1366 " {\"uid\":5, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1367 "]}";
1368 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1369 assert_null(tree);
1370 CHECK_LOG_CTX("Too many \"user\" instances.",
Michal Vaskof8ebf132022-11-21 14:06:48 +01001371 "Data location \"/T2:user[uid='5']\".");
Radek Išaa76902f2021-03-29 08:52:51 +02001372
1373 schema = MODULE_CREATE_YANG("T_EMPTY_LIST",
1374 "container user_list {"
1375 " list user {"
1376 " key uid;"
1377 " unique \"name group\";"
1378 " leaf uid{type uint32;}"
1379 " leaf name{type string;}"
1380 " leaf group{type string;}"
1381 "}"
1382 "}");
1383 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1384
1385 /* empty list */
1386 data =
1387 "{\"T_EMPTY_LIST:user_list\": {}"
1388 "}";
1389
1390 /* check first item */
1391 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1392 list_tree = (void *)tree;
1393 CHECK_LYD_NODE_INNER(list_tree, 0, 0, 0, 1, 0, 0, 0, 1);
1394 lyd_free_all(tree);
1395}
1396
1397static void
1398test_diff(void **state)
1399{
Radek Krejciaa0b1db2021-04-01 15:26:53 +02001400 const char *schema;
Radek Išaa76902f2021-03-29 08:52:51 +02001401 struct lyd_node *model_1, *model_2;
1402 struct lyd_node *diff;
Radek Išaa76902f2021-03-29 08:52:51 +02001403 const char *data_1;
1404 const char *data_2;
1405 const char *diff_expected;
1406
1407 schema = MODULE_CREATE_YANG("T0", "list user {"
1408 "key uid;"
1409 "unique \"name group\";"
1410 "leaf uid{type uint32;}"
1411 "leaf name{type string;}"
1412 "leaf group{type string;}"
1413 "}");
1414 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1415
1416 /* delete item */
1417 data_1 =
1418 "<user xmlns=\"urn:tests:T0\">"
1419 " <uid>0</uid>"
1420 " <name>Tomáš Novák</name>"
1421 " <group>User</group>"
1422 "</user>"
1423 "<user xmlns=\"urn:tests:T0\">"
1424 " <uid>1</uid>"
1425 " <name>Martin Novák</name>"
1426 " <group>User</group>"
1427 "</user>";
1428
1429 data_2 =
1430 "<user xmlns=\"urn:tests:T0\">"
1431 " <uid>0</uid>"
1432 " <name>Tomáš Novák</name>"
1433 " <group>User</group>"
1434 "</user>";
1435
1436 diff_expected =
1437 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1438 " yang:operation=\"delete\">"
1439 "<uid>1</uid>"
1440 "<name>Martin Novák</name>"
1441 "<group>User</group>"
1442 "</user>";
1443 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1444 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1445 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1446 assert_non_null(diff);
1447 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1448 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1449 CHECK_LYD(model_1, model_2);
1450 lyd_free_all(model_1);
1451 lyd_free_all(model_2);
1452 lyd_free_all(diff);
1453
1454 /* add item */
1455 data_1 =
1456 "<user xmlns=\"urn:tests:T0\">"
1457 " <uid>0</uid>"
1458 " <name>Tomáš Novák</name>"
1459 " <group>User</group>"
1460 "</user>";
1461
1462 data_2 =
1463 "<user xmlns=\"urn:tests:T0\">"
1464 " <uid>0</uid>"
1465 " <name>Tomáš Novák</name>"
1466 " <group>User</group>"
1467 "</user>"
1468 "<user xmlns=\"urn:tests:T0\">"
1469 " <uid>1</uid>"
1470 " <name>Martin Novák</name>"
1471 " <group>User</group>"
1472 "</user>";
1473
1474 diff_expected =
1475 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1476 " yang:operation=\"create\">"
1477 "<uid>1</uid><name>Martin Novák</name><group>User</group>"
1478 "</user>";
1479 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1480 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1481 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1482 assert_non_null(diff);
1483 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1484 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1485 CHECK_LYD(model_1, model_2);
1486 lyd_free_all(model_1);
1487 lyd_free_all(model_2);
1488 lyd_free_all(diff);
1489
1490 /* diff one */
1491 data_1 =
1492 "<user xmlns=\"urn:tests:T0\">"
1493 " <uid>0</uid>"
1494 " <name>Tomáš Novák</name>"
1495 " <group>User</group>"
1496 "</user>"
1497 "<user xmlns=\"urn:tests:T0\">"
1498 " <uid>1</uid>"
1499 " <name>Martin Novák</name>"
1500 " <group>User</group>"
1501 "</user>";
1502
1503 data_2 =
1504 "<user xmlns=\"urn:tests:T0\">"
1505 " <uid>0</uid>"
1506 " <name>Tomáš Novák</name>"
1507 " <group>Admin</group>"
1508 "</user>"
1509 "<user xmlns=\"urn:tests:T0\">"
1510 " <uid>1</uid>"
1511 " <name>Martin Novák</name>"
1512 " <group>User</group>"
1513 "</user>";
1514
1515 diff_expected =
1516 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1517 " yang:operation=\"none\">"
1518 "<uid>0</uid>"
1519 "<group yang:operation=\"replace\" yang:orig-default=\"false\""
1520 " yang:orig-value=\"User\">Admin</group></user>";
1521 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1522 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1523 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1524 assert_non_null(diff);
1525 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1526 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1527 CHECK_LYD(model_1, model_2);
1528 lyd_free_all(model_1);
1529 lyd_free_all(model_2);
1530 lyd_free_all(diff);
1531
1532 /* diff same */
1533 data_1 =
1534 "<user xmlns=\"urn:tests:T0\">"
1535 " <uid>0</uid>"
1536 " <name>Tomáš Novák</name>"
1537 " <group>User</group>"
1538 "</user>"
1539 "<user xmlns=\"urn:tests:T0\">"
1540 " <uid>1</uid>"
1541 " <name>Martin Novák</name>"
1542 " <group>User</group>"
1543 "</user>";
1544
1545 data_2 =
1546 "<user xmlns=\"urn:tests:T0\">"
1547 " <uid>0</uid>"
1548 " <name>Tomáš Novák</name>"
1549 " <group>User</group>"
1550 "</user>"
1551 "<user xmlns=\"urn:tests:T0\">"
1552 " <uid>1</uid>"
1553 " <name>Martin Novák</name>"
1554 " <group>User</group>"
1555 "</user>";
1556
1557 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1558 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1559 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1560 assert_null(diff);
1561 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1562 CHECK_LYD(model_1, model_2);
1563 lyd_free_all(model_1);
1564 lyd_free_all(model_2);
1565 lyd_free_all(diff);
Radek Išaa76902f2021-03-29 08:52:51 +02001566}
1567
1568static void
1569test_print(void **state)
1570{
1571
1572 const char *schema;
1573 const char *expected_string;
1574
1575 schema = MODULE_CREATE_YANG("T0",
1576 "list user {"
1577 "key uid;"
1578 "leaf uid{type uint32;}"
1579 "leaf name{type string;}"
1580 "leaf group{type string; default \"User\";}"
1581 "}");
1582 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1583
1584 struct lyd_node *model_1;
1585 const char *data_1 =
1586 "<user xmlns=\"urn:tests:T0\">"
1587 " <uid>0</uid>"
1588 " <name>Tomáš Novák</name>"
1589 "</user>"
1590 "<user xmlns=\"urn:tests:T0\">"
1591 " <uid>1</uid>"
1592 " <name>Martin Novák</name>"
1593 " <group>Admin</group>"
1594 "</user>";
1595
1596 LYD_TREE_CREATE(data_1, model_1);
1597
1598 /* XML */
1599 expected_string =
1600 "<user xmlns=\"urn:tests:T0\">"
1601 "<uid>0</uid><name>Tomáš Novák</name>"
1602 "</user>"
1603 "<user xmlns=\"urn:tests:T0\">"
1604 "<uid>1</uid><name>Martin Novák</name><group>Admin</group>"
1605 "</user>";
1606 CHECK_LYD_STRING_PARAM(model_1, expected_string, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1607
1608 /* JSON */
1609 expected_string = "{\"T0:user\":["
1610 "{\"uid\":0,\"name\":\"Tomáš Novák\"},"
1611 "{\"uid\":1,\"name\":\"Martin Novák\",\"group\":\"Admin\"}"
1612 "]}";
1613 CHECK_LYD_STRING_PARAM(model_1, expected_string, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1614
1615 lyd_free_all(model_1);
1616}
1617
1618int
1619main(void)
1620{
1621 const struct CMUnitTest tests[] = {
1622 UTEST(test_schema_yang),
1623 UTEST(test_schema_yin),
1624 UTEST(test_schema_print),
1625
1626 UTEST(test_xml),
1627 UTEST(test_json),
1628 UTEST(test_diff),
1629 UTEST(test_print),
1630 };
1631
1632 return cmocka_run_group_tests(tests, NULL, NULL);
1633}