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