blob: 4753121394d91134c9e160145d29e757b8989bf7 [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;
52 const struct lys_module *mod;
53 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;
364 const struct lys_module *mod;
365 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;
655 const struct lys_module *mod;
656
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",
693 " list user {\n"
694 " key \"uid name\";\n"
695 " unique \"name\";\n"
696 " min-elements 10;\n"
697 " max-elements 20;\n"
698 " leaf uid {\n"
699 " type int32;\n"
700 " }\n"
701 " leaf name {\n"
702 " type string;\n"
703 " }\n"
704 " leaf group {\n"
705 " type string;\n"
706 " }\n"
707 " }\n");
708 schema_yin = MODULE_CREATE_YIN("PRINT1",
709 " <list name=\"user\">\n"
710 " <key value=\"uid name\"/>\n"
711 " <unique tag=\"name\"/>\n"
712 " <min-elements value=\"10\"/>\n"
713 " <max-elements value=\"20\"/>\n"
714 " <leaf name=\"uid\">\n"
715 " <type name=\"int32\"/>\n"
716 " </leaf>\n"
717 " <leaf name=\"name\">\n"
718 " <type name=\"string\"/>\n"
719 " </leaf>\n"
720 " <leaf name=\"group\">\n"
721 " <type name=\"string\"/>\n"
722 " </leaf>\n"
723 " </list>\n");
724
725 UTEST_ADD_MODULE(schema_yin, LYS_IN_YIN, NULL, &mod);
726 assert_non_null(mod);
727 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
728 assert_string_equal(printed, schema_yang);
729 free(printed);
730
731 /* test no segmentation fold due ignoring default value */
732 schema_yang = MODULE_CREATE_YANG("PRINT2", "list user {"
733 "key uid;"
734 "leaf uid{type int32; default \"25\";}"
735 "leaf name{type string;}"
736 "leaf group{type string;}"
737 "}");
738
739 UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod);
740 assert_non_null(mod);
741 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
742 free(printed);
743}
744
745static void
746test_xml(void **state)
747{
748 struct lyd_node *tree;
749 const char *data, *schema;
750 struct lyd_node_inner *list_tree;
751 struct lyd_node_term *list_leaf;
752
753 schema = MODULE_CREATE_YANG("T0", "list user {"
754 "key uid;"
755 "unique \"name group\";"
756 "leaf uid{type uint32;}"
757 "leaf name{type string;}"
758 "leaf group{type string;}"
759 "}");
760 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
761
762 /* add data */
763 data =
764 "<user xmlns=\"urn:tests:T0\">"
765 " <uid>0</uid>"
766 " <name>Tomáš Novák</name>"
767 " <group>User</group>"
768 "</user>"
769 "<user xmlns=\"urn:tests:T0\">"
770 " <uid>1</uid>"
771 " <name>Martin Novák</name>"
772 " <group>User</group>"
773 "</user>";
774 /* check first item */
775 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
776 list_tree = (void *)tree;
777 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
778 list_leaf = (void *) list_tree->child;
779 assert_string_equal(list_leaf->schema->name, "uid");
780 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
781 list_leaf = (void *) list_leaf->next;
782 assert_string_equal(list_leaf->schema->name, "name");
783 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
784 list_leaf = (void *) list_leaf->next;
785 assert_string_equal(list_leaf->schema->name, "group");
786 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
787 /* check second item */
788 list_tree = (void *) list_tree->next;
789 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
790 list_leaf = (void *) list_tree->child;
791 assert_string_equal(list_leaf->schema->name, "uid");
792 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
793 list_leaf = (void *) list_leaf->next;
794 assert_string_equal(list_leaf->schema->name, "name");
795 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Martin Novák");
796 list_leaf = (void *) list_leaf->next;
797 assert_string_equal(list_leaf->schema->name, "group");
798 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
799 lyd_free_all(tree);
800
801 /* add data */
802 data =
803 "<user xmlns=\"urn:tests:T0\">"
804 " <uid>0</uid>"
805 " <name>Tomáš Novák</name>"
806 " <group>User</group>"
807 "</user>"
808 "<user xmlns=\"urn:tests:T0\">"
809 " <uid>1</uid>"
810 " <name>Tomáš Novák</name>"
811 " <group>Admin</group>"
812 "</user>";
813 /* check first item */
814 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
815 list_tree = (void *)tree;
816 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
817 list_leaf = (void *) list_tree->child;
818 assert_string_equal(list_leaf->schema->name, "uid");
819 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
820 list_leaf = (void *) list_leaf->next;
821 assert_string_equal(list_leaf->schema->name, "name");
822 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
823 list_leaf = (void *) list_leaf->next;
824 assert_string_equal(list_leaf->schema->name, "group");
825 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
826 /* check second item */
827 list_tree = (void *) list_tree->next;
828 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
829 list_leaf = (void *) list_tree->child;
830 assert_string_equal(list_leaf->schema->name, "uid");
831 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
832 list_leaf = (void *) list_leaf->next;
833 assert_string_equal(list_leaf->schema->name, "name");
834 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
835 list_leaf = (void *) list_leaf->next;
836 assert_string_equal(list_leaf->schema->name, "group");
837 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
838 lyd_free_all(tree);
839
840 data =
841 "<user xmlns=\"urn:tests:T0\">"
842 " <uid>0</uid>"
843 " <name>Tomáš Novák</name>"
844 " <group>User</group>"
845 "</user>"
846 "<user xmlns=\"urn:tests:T0\">"
847 " <uid>0</uid>"
848 " <name>Tomáš Novák</name>"
849 " <group>Admin</group>"
850 "</user>";
851 /* check first item */
852 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
853 assert_null(tree);
854 CHECK_LOG_CTX("Duplicate instance of \"user\".",
855 "Schema location /T0:user, data location /T0:user[uid='0'].");
856
857 data =
858 "<user xmlns=\"urn:tests:T0\">"
859 " <uid>0</uid>"
860 " <name>Tomáš Novák</name>"
861 " <group>User</group>"
862 "</user>"
863 "<user xmlns=\"urn:tests:T0\">"
864 " <uid>1</uid>"
865 " <name>Tomáš Novák</name>"
866 " <group>User</group>"
867 "</user>";
868 /* check first item */
869 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
870 assert_null(tree);
871 CHECK_LOG_CTX("Unique data leaf(s) \"name group\" not satisfied in \"/T0:user[uid='0']\" and \"/T0:user[uid='1']\".",
872 "Schema location /T0:user, data location /T0:user[uid='1'].");
873
874 /* double key */
875 schema = MODULE_CREATE_YANG("T1", "list user {"
876 "key \"uid group\";"
877 "leaf uid{type uint32;}"
878 "leaf name{type string;}"
879 "leaf group{type string;}"
880 "}");
881 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
882 data =
883 "<user xmlns=\"urn:tests:T1\">"
884 " <uid>0</uid>"
885 " <name>Tomáš Novák</name>"
886 " <group>User</group>"
887 "</user>"
888 "<user xmlns=\"urn:tests:T1\">"
889 " <uid>0</uid>"
890 " <name>Tomáš Novák</name>"
891 " <group>Admin</group>"
892 "</user>";
893 /* check first item */
894 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
895 list_tree = (void *)tree;
896 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
897 list_leaf = (void *) list_tree->child;
898 assert_string_equal(list_leaf->schema->name, "uid");
899 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
900 list_leaf = (void *) list_leaf->next;
901 assert_string_equal(list_leaf->schema->name, "group");
902 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "User");
903 list_leaf = (void *) list_leaf->next;
904 assert_string_equal(list_leaf->schema->name, "name");
905 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Tomáš Novák");
906 /* check second item */
907 list_tree = (void *) list_tree->next;
908 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
909 list_leaf = (void *) list_tree->child;
910 assert_string_equal(list_leaf->schema->name, "uid");
911 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
912 list_leaf = (void *) list_leaf->next;
913 assert_string_equal(list_leaf->schema->name, "group");
914 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Admin");
915 list_leaf = (void *) list_leaf->next;
916 assert_string_equal(list_leaf->schema->name, "name");
917 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Tomáš Novák");
918 lyd_free_all(tree);
919
920 data =
921 "<user xmlns=\"urn:tests:T1\">"
922 " <uid>0</uid>"
923 " <name>Tomáš Novák</name>"
924 " <group>User</group>"
925 "</user>"
926 "<user xmlns=\"urn:tests:T1\">"
927 " <uid>0</uid>"
928 " <name>Tomáš Novák</name>"
929 " <group>User</group>"
930 "</user>";
931 /* check first item */
932 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
933 assert_null(tree);
934 CHECK_LOG_CTX("Duplicate instance of \"user\".",
935 "Schema location /T1:user, data location /T1:user[uid='0'][group='User'].");
936
937 /* min elements max elements */
938 schema = MODULE_CREATE_YANG("T2",
939 "list user {"
940 " key uid;"
941 " min-elements 3;"
942 " max-elements 5;"
943 " leaf uid{type uint32;}"
944 " leaf name{type string;}"
945 " leaf group{type string;}"
946 "}");
947 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
948 data =
949 "<user xmlns=\"urn:tests:T2\">"
950 " <uid>0</uid>"
951 " <name>Tomáš Novák</name>"
952 " <group>User</group>"
953 "</user>"
954 "<user xmlns=\"urn:tests:T2\">"
955 " <uid>1</uid>"
956 " <name>Tomáš Novák</name>"
957 " <group>Admin</group>"
958 "</user>"
959 "<user xmlns=\"urn:tests:T2\">"
960 " <uid>2</uid>"
961 " <name>Tomáš Jak</name>"
962 " <group>Admin</group>"
963 "</user>";
964 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
965 list_tree = (void *)tree;
966 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
967 list_leaf = (void *) list_tree->child;
968 assert_string_equal(list_leaf->schema->name, "uid");
969 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
970 list_leaf = (void *) list_leaf->next;
971 assert_string_equal(list_leaf->schema->name, "name");
972 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
973 list_leaf = (void *) list_leaf->next;
974 assert_string_equal(list_leaf->schema->name, "group");
975 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
976 /* check second item */
977 list_tree = (void *) list_tree->next;
978 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
979 list_leaf = (void *) list_tree->child;
980 assert_string_equal(list_leaf->schema->name, "uid");
981 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
982 list_leaf = (void *) list_leaf->next;
983 assert_string_equal(list_leaf->schema->name, "name");
984 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
985 list_leaf = (void *) list_leaf->next;
986 assert_string_equal(list_leaf->schema->name, "group");
987 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
988 /* check third item */
989 list_tree = (void *) list_tree->next;
990 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
991 list_leaf = (void *) list_tree->child;
992 assert_string_equal(list_leaf->schema->name, "uid");
993 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "2", 2);
994 list_leaf = (void *) list_leaf->next;
995 assert_string_equal(list_leaf->schema->name, "name");
996 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Jak");
997 list_leaf = (void *) list_leaf->next;
998 assert_string_equal(list_leaf->schema->name, "group");
999 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1000 lyd_free_all(tree);
1001
1002 data =
1003 "<user xmlns=\"urn:tests:T2\">"
1004 " <uid>0</uid>"
1005 " <name>Tomáš Novák</name>"
1006 " <group>User</group>"
1007 "</user>"
1008 "<user xmlns=\"urn:tests:T2\">"
1009 " <uid>1</uid>"
1010 " <name>Tomáš Novák</name>"
1011 " <group>Admin</group>"
1012 "</user>"
1013 "<user xmlns=\"urn:tests:T2\">"
1014 " <uid>2</uid>"
1015 " <name>Tomáš Novák</name>"
1016 " <group>Admin</group>"
1017 "</user>"
1018 "<user xmlns=\"urn:tests:T2\">"
1019 " <uid>3</uid>"
1020 " <name>Tomáš Novák</name>"
1021 " <group>Admin</group>"
1022 "</user>"
1023 "<user xmlns=\"urn:tests:T2\">"
1024 " <uid>4</uid>"
1025 " <name>Tomáš Novák</name>"
1026 " <group>Admin</group>"
1027 "</user>";
1028 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1029 list_tree = (void *)tree;
1030 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1031 list_tree = (void *) list_tree->next;
1032 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1033 list_tree = (void *) list_tree->next;
1034 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1035 list_tree = (void *) list_tree->next;
1036 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1037 list_tree = (void *) list_tree->next;
1038 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1039 lyd_free_all(tree);
1040
1041 /* check wrong number of items */
1042 data =
1043 "<user xmlns=\"urn:tests:T2\">"
1044 " <uid>0</uid>"
1045 " <name>Tomáš Novák</name>"
1046 " <group>User</group>"
1047 "</user>"
1048 "<user xmlns=\"urn:tests:T2\">"
1049 " <uid>1</uid>"
1050 " <name>Tomáš Novák</name>"
1051 " <group>Admin</group>"
1052 "</user>";
1053 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1054 assert_null(tree);
1055 CHECK_LOG_CTX("Too few \"user\" instances.",
1056 "Schema location /T2:user.");
1057
1058 data =
1059 "<user xmlns=\"urn:tests:T2\">"
1060 " <uid>0</uid>"
1061 " <name>Tomáš Novák</name>"
1062 " <group>User</group>"
1063 "</user>"
1064 "<user xmlns=\"urn:tests:T2\">"
1065 " <uid>1</uid>"
1066 " <name>Tomáš Novák</name>"
1067 " <group>Admin</group>"
1068 "</user>"
1069 "<user xmlns=\"urn:tests:T2\">"
1070 " <uid>2</uid>"
1071 " <name>Tomáš Novák</name>"
1072 " <group>Admin</group>"
1073 "</user>"
1074 "<user xmlns=\"urn:tests:T2\">"
1075 " <uid>3</uid>"
1076 " <name>Tomáš Novák</name>"
1077 " <group>Admin</group>"
1078 "</user>"
1079 "<user xmlns=\"urn:tests:T2\">"
1080 " <uid>4</uid>"
1081 " <name>Tomáš Novák</name>"
1082 " <group>Admin</group>"
1083 "</user>"
1084 "<user xmlns=\"urn:tests:T2\">"
1085 " <uid>5</uid>"
1086 " <name>Tomáš Novák</name>"
1087 " <group>Admin</group>"
1088 "</user>";
1089 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1090 assert_null(tree);
1091 CHECK_LOG_CTX("Too many \"user\" instances.",
1092 "Schema location /T2:user, data location /T2:user[uid='5'].");
1093
1094 /* empty list */
1095 schema = MODULE_CREATE_YANG("T_EMPTY_LIST",
1096 "container user_list {"
1097 " list user {"
1098 " key uid;"
1099 " unique \"name group\";"
1100 " leaf uid{type uint32;}"
1101 " leaf name{type string;}"
1102 " leaf group{type string;}"
1103 "}"
1104 "}");
1105 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1106
1107 /* empty list */
1108 data = "<user_list xmlns=\"urn:tests:T_EMPTY_LIST\"/>";
1109 /* check first item */
1110 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1111 list_tree = (void *)tree;
1112 CHECK_LYD_NODE_INNER(list_tree, 0, 0, 0, 1, 0, 0, 0, 1);
1113 lyd_free_all(tree);
1114}
1115
1116static void
1117test_json(void **state)
1118{
1119 struct lyd_node *tree;
1120 const char *data, *schema;
1121 struct lyd_node_inner *list_tree;
1122 struct lyd_node_term *list_leaf;
1123
1124 schema = MODULE_CREATE_YANG("T0", "list user {"
1125 "key uid;"
1126 "unique \"name group\";"
1127 "leaf uid{type uint32;}"
1128 "leaf name{type string;}"
1129 "leaf group{type string;}"
1130 "}");
1131 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1132
1133 /* add data */
1134 data =
1135 "{\"T0:user\": ["
1136 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1137 " {\"uid\":1, \"name\":\"Martin Novák\", \"group\":\"User\"}"
1138 "]}";
1139
1140 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1141 list_tree = (void *)tree;
1142 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1143 list_leaf = (void *) list_tree->child;
1144 assert_string_equal(list_leaf->schema->name, "uid");
1145 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1146 list_leaf = (void *) list_leaf->next;
1147 assert_string_equal(list_leaf->schema->name, "name");
1148 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1149 list_leaf = (void *) list_leaf->next;
1150 assert_string_equal(list_leaf->schema->name, "group");
1151 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1152 /* check second item */
1153 list_tree = (void *) list_tree->next;
1154 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1155 list_leaf = (void *) list_tree->child;
1156 assert_string_equal(list_leaf->schema->name, "uid");
1157 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1158 list_leaf = (void *) list_leaf->next;
1159 assert_string_equal(list_leaf->schema->name, "name");
1160 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Martin Novák");
1161 list_leaf = (void *) list_leaf->next;
1162 assert_string_equal(list_leaf->schema->name, "group");
1163 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1164 lyd_free_all(tree);
1165
1166 /* Unique */
1167 data =
1168 "{\"T0:user\": ["
1169 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1170 " {\"uid\":1, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1171 "]}";
1172
1173 /* check first item */
1174 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1175 list_tree = (void *)tree;
1176 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1177 list_leaf = (void *) list_tree->child;
1178 assert_string_equal(list_leaf->schema->name, "uid");
1179 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1180 list_leaf = (void *) list_leaf->next;
1181 assert_string_equal(list_leaf->schema->name, "name");
1182 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1183 list_leaf = (void *) list_leaf->next;
1184 assert_string_equal(list_leaf->schema->name, "group");
1185 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1186 /* check second item */
1187 list_tree = (void *) list_tree->next;
1188 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1189 list_leaf = (void *) list_tree->child;
1190 assert_string_equal(list_leaf->schema->name, "uid");
1191 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1192 list_leaf = (void *) list_leaf->next;
1193 assert_string_equal(list_leaf->schema->name, "name");
1194 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1195 list_leaf = (void *) list_leaf->next;
1196 assert_string_equal(list_leaf->schema->name, "group");
1197 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1198 lyd_free_all(tree);
1199
1200 data =
1201 "{\"T0:user\": ["
1202 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1203 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1204 "]}";
1205 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1206 assert_null(tree);
1207 CHECK_LOG_CTX("Duplicate instance of \"user\".",
1208 "Schema location /T0:user, data location /T0:user[uid='0'], line number 1.");
1209
1210 data =
1211 "{\"T0:user\": ["
1212 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1213 " {\"uid\":1, \"name\":\"Jan Kuba\", \"group\":\"User\"}"
1214 "]}";
1215 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1216 assert_null(tree);
1217 CHECK_LOG_CTX("Unique data leaf(s) \"name group\" not satisfied in \"/T0:user[uid='0']\" and \"/T0:user[uid='1']\".",
1218 "Schema location /T0:user, data location /T0:user[uid='1'], line number 1.");
1219
1220 /* double key */
1221 schema = MODULE_CREATE_YANG("T1", "list user {"
1222 "key \"uid group\";"
1223 "leaf uid{type uint32;}"
1224 "leaf name{type string;}"
1225 "leaf group{type string;}"
1226 "}");
1227 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1228 data =
1229 "{\"T1:user\": ["
1230 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1231 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"Admin\"}"
1232 "]}";
1233 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1234 list_tree = (void *)tree;
1235 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1236 list_leaf = (void *) list_tree->child;
1237 assert_string_equal(list_leaf->schema->name, "uid");
1238 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1239 list_leaf = (void *) list_leaf->next;
1240 assert_string_equal(list_leaf->schema->name, "group");
1241 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "User");
1242 list_leaf = (void *) list_leaf->next;
1243 assert_string_equal(list_leaf->schema->name, "name");
1244 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Jan Kuba");
1245 /* check second item */
1246 list_tree = (void *) list_tree->next;
1247 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1248 list_leaf = (void *) list_tree->child;
1249 assert_string_equal(list_leaf->schema->name, "uid");
1250 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1251 list_leaf = (void *) list_leaf->next;
1252 assert_string_equal(list_leaf->schema->name, "group");
1253 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Admin");
1254 list_leaf = (void *) list_leaf->next;
1255 assert_string_equal(list_leaf->schema->name, "name");
1256 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Jan Kuba");
1257 lyd_free_all(tree);
1258
1259 data =
1260 "{\"T1:user\": ["
1261 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1262 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"}"
1263 "]}";
1264 /* check first item */
1265 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1266 assert_null(tree);
1267 CHECK_LOG_CTX("Duplicate instance of \"user\".",
1268 "Schema location /T1:user, data location /T1:user[uid='0'][group='User'], line number 1.");
1269
1270 /* min elements max elements */
1271 schema = MODULE_CREATE_YANG("T2",
1272 "list user {"
1273 " key uid;"
1274 " min-elements 3;"
1275 " max-elements 5;"
1276 " leaf uid{type uint32;}"
1277 " leaf name{type string;}"
1278 " leaf group{type string;}"
1279 "}");
1280 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1281 data =
1282 "{\"T2:user\": ["
1283 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1284 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1285 " {\"uid\":2, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1286 "]}";
1287
1288 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1289 list_tree = (void *)tree;
1290 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1291 list_leaf = (void *) list_tree->child;
1292 assert_string_equal(list_leaf->schema->name, "uid");
1293 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "0", 0);
1294 list_leaf = (void *) list_leaf->next;
1295 assert_string_equal(list_leaf->schema->name, "name");
1296 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Jan Kuba");
1297 list_leaf = (void *) list_leaf->next;
1298 assert_string_equal(list_leaf->schema->name, "group");
1299 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1300 /* check second item */
1301 list_tree = (void *) list_tree->next;
1302 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1303 list_leaf = (void *) list_tree->child;
1304 assert_string_equal(list_leaf->schema->name, "uid");
1305 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "1", 1);
1306 list_leaf = (void *) list_leaf->next;
1307 assert_string_equal(list_leaf->schema->name, "name");
1308 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Antonín Kuba");
1309 list_leaf = (void *) list_leaf->next;
1310 assert_string_equal(list_leaf->schema->name, "group");
1311 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "User");
1312 /* check third item */
1313 list_tree = (void *) list_tree->next;
1314 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1315 list_leaf = (void *) list_tree->child;
1316 assert_string_equal(list_leaf->schema->name, "uid");
1317 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, UINT32, "2", 2);
1318 list_leaf = (void *) list_leaf->next;
1319 assert_string_equal(list_leaf->schema->name, "name");
1320 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 1, 1, 1, STRING, "Tomáš Novák");
1321 list_leaf = (void *) list_leaf->next;
1322 assert_string_equal(list_leaf->schema->name, "group");
1323 CHECK_LYD_NODE_TERM(list_leaf, 0, 0, 0, 1, 1, STRING, "Admin");
1324 lyd_free_all(tree);
1325
1326 data =
1327 "{\"T2:user\": ["
1328 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1329 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1330 " {\"uid\":2, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1331 " {\"uid\":3, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1332 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1333 "]}";
1334 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1335 list_tree = (void *)tree;
1336 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1337 list_tree = (void *) list_tree->next;
1338 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1339 list_tree = (void *) list_tree->next;
1340 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1341 list_tree = (void *) list_tree->next;
1342 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 1, 0, 0, 1);
1343 list_tree = (void *) list_tree->next;
1344 CHECK_LYD_NODE_INNER(list_tree, 1, 0, 0, 0, 0, 0, 0, 1);
1345 lyd_free_all(tree);
1346
1347 /* check wrong number of items */
1348 data =
1349 "{\"T2:user\": ["
1350 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1351 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1352 "]}";
1353 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1354 assert_null(tree);
1355 CHECK_LOG_CTX("Too few \"user\" instances.",
1356 "Schema location /T2:user, line number 1.");
1357
1358 data =
1359 "{\"T2:user\": ["
1360 " {\"uid\":0, \"name\":\"Jan Kuba\", \"group\":\"User\"},"
1361 " {\"uid\":1, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1362 " {\"uid\":2, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1363 " {\"uid\":3, \"name\":\"Antonín Kuba\", \"group\":\"User\"},"
1364 " {\"uid\":4, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"},"
1365 " {\"uid\":5, \"name\":\"Tomáš Novák\", \"group\":\"Admin\"}"
1366 "]}";
1367 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
1368 assert_null(tree);
1369 CHECK_LOG_CTX("Too many \"user\" instances.",
1370 "Schema location /T2:user, data location /T2:user[uid='5'], line number 1.");
1371
1372 schema = MODULE_CREATE_YANG("T_EMPTY_LIST",
1373 "container user_list {"
1374 " list user {"
1375 " key uid;"
1376 " unique \"name group\";"
1377 " leaf uid{type uint32;}"
1378 " leaf name{type string;}"
1379 " leaf group{type string;}"
1380 "}"
1381 "}");
1382 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1383
1384 /* empty list */
1385 data =
1386 "{\"T_EMPTY_LIST:user_list\": {}"
1387 "}";
1388
1389 /* check first item */
1390 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
1391 list_tree = (void *)tree;
1392 CHECK_LYD_NODE_INNER(list_tree, 0, 0, 0, 1, 0, 0, 0, 1);
1393 lyd_free_all(tree);
1394}
1395
1396static void
1397test_diff(void **state)
1398{
Radek Krejciaa0b1db2021-04-01 15:26:53 +02001399 const char *schema;
Radek Išaa76902f2021-03-29 08:52:51 +02001400 struct lyd_node *model_1, *model_2;
1401 struct lyd_node *diff;
Radek Išaa76902f2021-03-29 08:52:51 +02001402 const char *data_1;
1403 const char *data_2;
1404 const char *diff_expected;
1405
1406 schema = MODULE_CREATE_YANG("T0", "list user {"
1407 "key uid;"
1408 "unique \"name group\";"
1409 "leaf uid{type uint32;}"
1410 "leaf name{type string;}"
1411 "leaf group{type string;}"
1412 "}");
1413 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1414
1415 /* delete item */
1416 data_1 =
1417 "<user xmlns=\"urn:tests:T0\">"
1418 " <uid>0</uid>"
1419 " <name>Tomáš Novák</name>"
1420 " <group>User</group>"
1421 "</user>"
1422 "<user xmlns=\"urn:tests:T0\">"
1423 " <uid>1</uid>"
1424 " <name>Martin Novák</name>"
1425 " <group>User</group>"
1426 "</user>";
1427
1428 data_2 =
1429 "<user xmlns=\"urn:tests:T0\">"
1430 " <uid>0</uid>"
1431 " <name>Tomáš Novák</name>"
1432 " <group>User</group>"
1433 "</user>";
1434
1435 diff_expected =
1436 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1437 " yang:operation=\"delete\">"
1438 "<uid>1</uid>"
1439 "<name>Martin Novák</name>"
1440 "<group>User</group>"
1441 "</user>";
1442 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1443 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1444 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1445 assert_non_null(diff);
1446 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1447 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1448 CHECK_LYD(model_1, model_2);
1449 lyd_free_all(model_1);
1450 lyd_free_all(model_2);
1451 lyd_free_all(diff);
1452
1453 /* add item */
1454 data_1 =
1455 "<user xmlns=\"urn:tests:T0\">"
1456 " <uid>0</uid>"
1457 " <name>Tomáš Novák</name>"
1458 " <group>User</group>"
1459 "</user>";
1460
1461 data_2 =
1462 "<user xmlns=\"urn:tests:T0\">"
1463 " <uid>0</uid>"
1464 " <name>Tomáš Novák</name>"
1465 " <group>User</group>"
1466 "</user>"
1467 "<user xmlns=\"urn:tests:T0\">"
1468 " <uid>1</uid>"
1469 " <name>Martin Novák</name>"
1470 " <group>User</group>"
1471 "</user>";
1472
1473 diff_expected =
1474 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1475 " yang:operation=\"create\">"
1476 "<uid>1</uid><name>Martin Novák</name><group>User</group>"
1477 "</user>";
1478 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1479 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1480 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1481 assert_non_null(diff);
1482 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1483 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1484 CHECK_LYD(model_1, model_2);
1485 lyd_free_all(model_1);
1486 lyd_free_all(model_2);
1487 lyd_free_all(diff);
1488
1489 /* diff one */
1490 data_1 =
1491 "<user xmlns=\"urn:tests:T0\">"
1492 " <uid>0</uid>"
1493 " <name>Tomáš Novák</name>"
1494 " <group>User</group>"
1495 "</user>"
1496 "<user xmlns=\"urn:tests:T0\">"
1497 " <uid>1</uid>"
1498 " <name>Martin Novák</name>"
1499 " <group>User</group>"
1500 "</user>";
1501
1502 data_2 =
1503 "<user xmlns=\"urn:tests:T0\">"
1504 " <uid>0</uid>"
1505 " <name>Tomáš Novák</name>"
1506 " <group>Admin</group>"
1507 "</user>"
1508 "<user xmlns=\"urn:tests:T0\">"
1509 " <uid>1</uid>"
1510 " <name>Martin Novák</name>"
1511 " <group>User</group>"
1512 "</user>";
1513
1514 diff_expected =
1515 "<user xmlns=\"urn:tests:T0\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\""
1516 " yang:operation=\"none\">"
1517 "<uid>0</uid>"
1518 "<group yang:operation=\"replace\" yang:orig-default=\"false\""
1519 " yang:orig-value=\"User\">Admin</group></user>";
1520 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1521 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1522 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1523 assert_non_null(diff);
1524 CHECK_LYD_STRING_PARAM(diff, diff_expected, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1525 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1526 CHECK_LYD(model_1, model_2);
1527 lyd_free_all(model_1);
1528 lyd_free_all(model_2);
1529 lyd_free_all(diff);
1530
1531 /* diff same */
1532 data_1 =
1533 "<user xmlns=\"urn:tests:T0\">"
1534 " <uid>0</uid>"
1535 " <name>Tomáš Novák</name>"
1536 " <group>User</group>"
1537 "</user>"
1538 "<user xmlns=\"urn:tests:T0\">"
1539 " <uid>1</uid>"
1540 " <name>Martin Novák</name>"
1541 " <group>User</group>"
1542 "</user>";
1543
1544 data_2 =
1545 "<user xmlns=\"urn:tests:T0\">"
1546 " <uid>0</uid>"
1547 " <name>Tomáš Novák</name>"
1548 " <group>User</group>"
1549 "</user>"
1550 "<user xmlns=\"urn:tests:T0\">"
1551 " <uid>1</uid>"
1552 " <name>Martin Novák</name>"
1553 " <group>User</group>"
1554 "</user>";
1555
1556 CHECK_PARSE_LYD_PARAM(data_1, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_1);
1557 CHECK_PARSE_LYD_PARAM(data_2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, model_2);
1558 assert_int_equal(LY_SUCCESS, lyd_diff_siblings(model_1, model_2, 0, &diff));
1559 assert_null(diff);
1560 assert_int_equal(LY_SUCCESS, lyd_diff_apply_all(&model_1, diff));
1561 CHECK_LYD(model_1, model_2);
1562 lyd_free_all(model_1);
1563 lyd_free_all(model_2);
1564 lyd_free_all(diff);
Radek Išaa76902f2021-03-29 08:52:51 +02001565}
1566
1567static void
1568test_print(void **state)
1569{
1570
1571 const char *schema;
1572 const char *expected_string;
1573
1574 schema = MODULE_CREATE_YANG("T0",
1575 "list user {"
1576 "key uid;"
1577 "leaf uid{type uint32;}"
1578 "leaf name{type string;}"
1579 "leaf group{type string; default \"User\";}"
1580 "}");
1581 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
1582
1583 struct lyd_node *model_1;
1584 const char *data_1 =
1585 "<user xmlns=\"urn:tests:T0\">"
1586 " <uid>0</uid>"
1587 " <name>Tomáš Novák</name>"
1588 "</user>"
1589 "<user xmlns=\"urn:tests:T0\">"
1590 " <uid>1</uid>"
1591 " <name>Martin Novák</name>"
1592 " <group>Admin</group>"
1593 "</user>";
1594
1595 LYD_TREE_CREATE(data_1, model_1);
1596
1597 /* XML */
1598 expected_string =
1599 "<user xmlns=\"urn:tests:T0\">"
1600 "<uid>0</uid><name>Tomáš Novák</name>"
1601 "</user>"
1602 "<user xmlns=\"urn:tests:T0\">"
1603 "<uid>1</uid><name>Martin Novák</name><group>Admin</group>"
1604 "</user>";
1605 CHECK_LYD_STRING_PARAM(model_1, expected_string, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1606
1607 /* JSON */
1608 expected_string = "{\"T0:user\":["
1609 "{\"uid\":0,\"name\":\"Tomáš Novák\"},"
1610 "{\"uid\":1,\"name\":\"Martin Novák\",\"group\":\"Admin\"}"
1611 "]}";
1612 CHECK_LYD_STRING_PARAM(model_1, expected_string, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK);
1613
1614 lyd_free_all(model_1);
1615}
1616
1617int
1618main(void)
1619{
1620 const struct CMUnitTest tests[] = {
1621 UTEST(test_schema_yang),
1622 UTEST(test_schema_yin),
1623 UTEST(test_schema_print),
1624
1625 UTEST(test_xml),
1626 UTEST(test_json),
1627 UTEST(test_diff),
1628 UTEST(test_print),
1629 };
1630
1631 return cmocka_run_group_tests(tests, NULL, NULL);
1632}