blob: 800b05f2286a7806f88a73bba8ffbee833cc9fc7 [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);
172 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".",
173 "Line number 5.");
174
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);
184 CHECK_LOG_CTX("Value \"4294967298\" is out of \"max-elements\" bounds.",
185 "Line number 5.");
186
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);
196 CHECK_LOG_CTX("List min-elements 20 is bigger than max-elements 10.",
197 "/TERR_0:user");
198
199 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
200 "key uid;"
201 "min-elements -1;"
202 "max-elements 20;"
203 "leaf uid{type int32;}"
204 "leaf name{type string;}"
205 "leaf group{type string;}"
206 "}");
207 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
208 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".",
209 "Line number 5.");
210
211 schema = MODULE_CREATE_YANG("TERR_0", "list user {"
212 "key uid;"
213 "key name;"
214 "leaf uid{type int32;}"
215 "leaf name{type string;}"
216 "leaf group{type string;}"
217 "}");
218 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
219 CHECK_LOG_CTX("Duplicate keyword \"key\".",
220 "Line number 5.");
221
222 schema = MODULE_CREATE_YANG("T6", "list user {"
223 "config false;"
224 "leaf uid{type int32;}"
225 "leaf name{type string;}"
226 "leaf group{type string;}"
227 "}");
228 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
229 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200230 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_KEYLESS | LYS_SET_CONFIG, \
231 1, "user", 0, 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200232 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200233 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 +0200234 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200235 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 +0200236 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200237 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 +0200238
239 schema = MODULE_CREATE_YANG("T7", "list user {"
240 "key uid;"
241 "unique name;"
242 "ordered-by user;"
243 "leaf uid{type int32;}"
244 "leaf name{type string;}"
245 "leaf group{type string;}"
246 "}");
247 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
248 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200249 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 +0200250 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
251 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200252 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 +0200253 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200254 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 +0200255 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200256 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 +0200257
258 schema = MODULE_CREATE_YANG("T8", "list user {"
259 "key uid;"
260 "unique name;"
261 "ordered-by system;"
262 "leaf uid{type int32;}"
263 "leaf name{type string;}"
264 "leaf group{type string;}"
265 "}");
266 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
267 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200268 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 +0200269 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
270 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200271 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 +0200272 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200273 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 +0200274 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200275 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 +0200276
277 schema = MODULE_CREATE_YANG("TERROR0", "list user {"
278 "key uid;"
279 "unique name;"
280 "ordered-by systeme;"
281 "leaf uid{type int32;}"
282 "leaf name{type string;}"
283 "leaf group{type string;}"
284 "}");
285 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
286 CHECK_LOG_CTX("Invalid value \"systeme\" of \"ordered-by\".",
287 "Line number 5.");
288
289 schema = MODULE_CREATE_YANG("TERROR0", "list \"\" {"
290 "key uid;"
291 "unique name;"
292 "ordered-by system;"
293 "leaf uid{type int32;}"
294 "leaf name{type string;}"
295 "leaf group{type string;}"
296 "}");
297 UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
298 CHECK_LOG_CTX("Statement argument is required.",
299 "Line number 5.");
300
301 schema = MODULE_CREATE_YANG("T9", "list user {"
302 "key uid;"
303 "unique name;"
304 "ordered-by system;"
305 "leaf uid{type int32;}"
306 "leaf name{type string;}"
307 "leaf group{type string; default \"abcd\";}"
308 "}");
309 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
310 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200311 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 +0200312 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
313 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200314 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 +0200315 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200316 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 +0200317 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200318 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 +0200319
320 schema = MODULE_CREATE_YANG("T10", "list user {"
321 "key uid;"
322 "leaf uid{type int32; default \"25\";}"
323 "leaf name{type string;}"
324 "leaf group{type string;}"
325 "}");
326 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
327 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200328 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 +0200329 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
330 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200331 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 +0200332 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200333 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 +0200334 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200335 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 +0200336
337 schema = MODULE_CREATE_YANG("T11",
338 "typedef my_type {"
339 " type int8; default \"25\";"
340 "}"
341 "list user {"
342 " key uid;"
343 " leaf uid{type my_type;}"
344 " leaf name{type string;}"
345 " leaf group{type string;}"
346 "}");
347 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
348 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200349 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 +0200350 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
351 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200352 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 +0200353 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200354 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 +0200355 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200356 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 +0200357
358}
359
360static void
361test_schema_yin(void **state)
362{
363 const char *schema;
Michal Vasko4de7d072021-07-09 09:13:18 +0200364 struct lys_module *mod;
Radek Išaa76902f2021-03-29 08:52:51 +0200365 struct lysc_node_list *lysc_leaf;
366 struct lysc_node *lysc_node;
Radek Išaa76902f2021-03-29 08:52:51 +0200367
368 schema = MODULE_CREATE_YIN("T0", "<list name=\"user\">"
369 " <key value=\"uid\"/>"
370 " <unique tag=\"name\"/>"
371 " <leaf name=\"uid\"><type name=\"int32\"/></leaf>"
372 " <leaf name=\"name\"><type name=\"string\"/></leaf>"
373 " <leaf name=\"group\"><type name=\"string\"/></leaf>"
374 "</list>");
375 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
376 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200377 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 +0200378 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
379 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200380 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 +0200381 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200382 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 +0200383 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200384 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 +0200385
386 schema = MODULE_CREATE_YIN("T00", "<list name=\"user\">"
387 " <key value=\"u&lt;id\"/>"
388 " <leaf name=\"uid\"><type name=\"int32\"/></leaf>"
389 " <leaf name=\"name\"><type name=\"string\"/></leaf>"
390 " <leaf name=\"group\"><type name=\"string\"/></leaf>"
391 "</list>");
392 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
393 CHECK_LOG_CTX("The list's key \"u<id\" not found.",
394 "/T00:user");
395
396 schema = MODULE_CREATE_YIN("T1", "<list name=\"user\"> "
397 " <key value=\"uid\"/>"
398 " <container name=\"name\">"
399 " <leaf name=\"fist\"> <type name=\"string\"/> </leaf>"
400 " <container name=\"second\">"
401 " <leaf name=\"sub\"> <type name=\"int32\"/></leaf>"
402 " </container>"
403 " </container>"
404 " <leaf name=\"uid\"> <type name=\"int32\"/></leaf>"
405 "</list>");
406 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
407 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200408 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 +0200409 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
410 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200411 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 +0200412 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200413 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 +0200414
415 schema = MODULE_CREATE_YIN("T2", "<list name=\"grup\">"
416 "<key value=\"guid\"/>"
417 "<leaf name=\"guid\"> <type name=\"int32\"/> </leaf>"
418 "<list name=\"users\">"
419 " <key value=\"name\"/>"
420 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
421 "</list>"
422 "</list>");
423 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
424 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200425 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 +0200426 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
427 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200428 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 +0200429 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200430 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 +0200431
432 /* restriction */
433 schema = MODULE_CREATE_YIN("T3",
434 "<list name = \"grup\">"
435 " <key value=\"guid\"/>"
436 " <min-elements value=\"10\"/>"
437 " <max-elements value=\"20\"/>"
438 " <leaf name=\"guid\"> <type name=\"int32\"/> </leaf>"
439 " <list name=\"users\"> <key value=\"name\"/>"
440 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
441 " </list>"
442 "</list>");
443 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
444 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200445 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_ORDBY_SYSTEM, 1, "grup", \
446 0, 0, 0, 0, 0, 1, 20, 10, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200447 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200448 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 +0200449 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200450 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 +0200451
Radek Išabe8a6532021-03-29 08:54:22 +0200452 schema = MODULE_CREATE_YIN("T4",
Radek Išaa76902f2021-03-29 08:52:51 +0200453 "<list name=\"user\">"
454 " <key value=\"uid name\"/>"
455 " <unique tag=\"name\"/>"
456 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
457 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
458 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
459 "</list>");
460 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
461 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200462 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 +0200463 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
464 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200465 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 +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 | LYS_UNIQUE | LYS_KEY, 1, "name", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200468 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200469 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 +0200470
471 schema = MODULE_CREATE_YIN("T5",
472 "<list name=\"rule\">"
473 " <key value=\"id\"/>"
474 " <unique tag=\"name\"/>"
475 " <unique tag=\"ip port\"/>"
476 " <leaf name=\"id\"> <type name=\"int32\"/></leaf>"
477 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
478 " <leaf name=\"ip\"> <type name=\"string\"/> </leaf>"
479 " <leaf name=\"port\"> <type name=\"int16\"/> </leaf>"
480 "</list>");
481 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
482 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200483 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 +0200484 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 2, 0);
485 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200486 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 +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, "name", 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, "ip", 1, LYS_LEAF, 1, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200491 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200492 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 +0200493
494 /* test error */
495 schema = MODULE_CREATE_YIN("TERR_0",
496 "<list name=\"user\">"
497 " <key value=\"uid\"/>"
498 " <min-elements value=\"10\"/>"
499 " <max-elements value=\"-1\"/>"
500 " <leaf name=\"uid\"> <type name=\"int32\"> </leaf>"
501 "</list>");
502 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
503 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"max-elements\" element.",
504 "Line number 10.");
505
506 schema = MODULE_CREATE_YIN("TERR_0",
507 "<list name=\"user\">"
508 " <key value=\"uid\"/>"
509 " <min-elements value=\"10\"/>"
510 " <max-elements value=\"4294967298\"/>"
511 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
512 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
513 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
514 "</list>");
515 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
516 CHECK_LOG_CTX("Value \"4294967298\" of \"value\" attribute in \"max-elements\" element is out of bounds.",
517 "Line number 10.");
518
519 schema = MODULE_CREATE_YIN("TERR_0",
520 "<list name=\"user\">"
521 " <key value=\"uid\"/>"
522 " <min-elements value=\"20\"/>"
523 " <max-elements value=\"10\"/>"
524 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
525 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
526 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
527 "</list>");
528 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
529 CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 20 is bigger than the max value 10.",
530 "Line number 10.");
531
532 schema = MODULE_CREATE_YIN("TERR_0",
533 "<list name=\"user\">"
534 " <key value=\"uid\"/>"
535 " <min-elements value=\"-1\"/>"
536 " <max-elements value=\"20\"/>"
537 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
538 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
539 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
540 "</list>");
541 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
542 CHECK_LOG_CTX("Value \"-1\" of \"value\" attribute in \"min-elements\" element is out of bounds.",
543 "Line number 10.");
544
545 schema = MODULE_CREATE_YIN("TERR_0",
546 "<list name=\"user\">"
547 " <key value=\"uid\"/>"
548 " <key value=\"name\"/>"
549 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
550 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
551 " <leaf name=\"group\"> <type name=\"string\"/> </leaf>"
552 "</list>");
553 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
554 CHECK_LOG_CTX("Redefinition of \"key\" sub-element in \"list\" element.",
555 "Line number 10.");
556
557 schema = MODULE_CREATE_YIN("T6",
558 "<list name=\"user\">"
559 " <config value=\"false\"/>"
560 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
561 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
562 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
563 "</list>");
564 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
565 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200566 CHECK_LYSC_NODE_LIST(lysc_leaf, 0, 0, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_KEYLESS | LYS_SET_CONFIG, \
567 1, "user", 0, 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 0, 0);
Radek Išaa76902f2021-03-29 08:52:51 +0200568 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200569 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 +0200570 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200571 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 +0200572 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200573 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 +0200574
575 schema = MODULE_CREATE_YIN("T7",
576 "<list name=\"user\">"
577 " <key value=\"uid\"/>"
578 " <unique tag=\"name\"/>"
579 " <ordered-by value=\"user\"/>"
580 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
581 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
582 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
583 "</list>");
584 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
585 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200586 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 +0200587 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
588 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200589 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 +0200590 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200591 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 +0200592 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200593 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 +0200594
595 schema = MODULE_CREATE_YIN("T8",
596 "<list name=\"user\">"
597 " <key value=\"uid\"/>"
598 " <unique tag=\"name\"/>"
599 " <ordered-by value=\"system\"/>"
600 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
601 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
602 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
603 "</list>");
604 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
605 lysc_leaf = (void *)mod->compiled->data;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200606 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 +0200607 0, 0, 0, 0, 1, 0xffffffff, 0, 0, 0, 1, 0);
608 lysc_node = lysc_leaf->child;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200609 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 +0200610 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200611 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 +0200612 lysc_node = lysc_node->next;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200613 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 +0200614
615 schema = MODULE_CREATE_YIN("TERROR0",
616 "<list name=\"user\">"
617 " <key value=\"uid\"/>"
618 " <unique tag=\"name\"/>"
619 " <ordered-by value=\"systeme\"/>"
620 " <leaf name=\"uid\"> <type name=\"int32\"/> </leaf>"
621 " <leaf name=\"name\"> <type name=\"string\"/> </leaf>"
622 " <leaf name=\"group\"><type name=\"string\"/> </leaf>"
623 "</list>");
624 UTEST_INVALID_MODULE(schema, LYS_IN_YIN, NULL, LY_EVALID);
625 CHECK_LOG_CTX("Invalid value \"systeme\" of \"value\" attribute in \"ordered-by\" element. Valid values are \"system\" and \"user\".",
626 "Line number 10.");
627
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\".",
856 "Schema location /T0:user, data location /T0:user[uid='0'].");
857
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']\".",
873 "Schema location /T0:user, data location /T0:user[uid='1'].");
874
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\".",
936 "Schema location /T1:user, data location /T1:user[uid='0'][group='User'].");
937
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.",
1057 "Schema location /T2:user.");
1058
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.",
1093 "Schema location /T2:user, data location /T2:user[uid='5'].");
1094
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\".",
1209 "Schema location /T0:user, data location /T0:user[uid='0'], line number 1.");
1210
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']\".",
1219 "Schema location /T0:user, data location /T0:user[uid='1'], line number 1.");
1220
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\".",
1269 "Schema location /T1:user, data location /T1:user[uid='0'][group='User'], line number 1.");
1270
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.",
1357 "Schema location /T2:user, line number 1.");
1358
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.",
1371 "Schema location /T2:user, data location /T2:user[uid='5'], line number 1.");
1372
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}