blob: 18d61f0075766b4b16aa621db3aacb13b5596520 [file] [log] [blame]
Michal Vaskoc636ea42022-09-16 10:20:31 +02001/**
Radek Krejci3a4889a2020-05-19 17:01:58 +02002 * @file test_schema.c
Michal Vaskoc636ea42022-09-16 10:20:31 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci3a4889a2020-05-19 17:01:58 +02005 * @brief unit tests for schema related functions
6 *
Michal Vaskoc636ea42022-09-16 10:20:31 +02007 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Radek Krejci3a4889a2020-05-19 17:01:58 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
Michal Vaskoc636ea42022-09-16 10:20:31 +020016#include "utests.h"
Radek Krejci18abde42020-06-13 20:04:39 +020017
Radek Krejcica376bd2020-06-11 16:04:06 +020018#include <string.h>
19
Michal Vaskoc636ea42022-09-16 10:20:31 +020020#include "compat.h"
21#include "context.h"
Radek Krejci18abde42020-06-13 20:04:39 +020022#include "log.h"
23#include "parser_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020024#include "plugins_exts.h"
Radek Krejci8297b792020-08-16 14:49:05 +020025#include "set.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020026#include "tree_edit.h"
Radek Krejci18abde42020-06-13 20:04:39 +020027#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020028#include "tree_schema_internal.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020029
Michal Vaskoc636ea42022-09-16 10:20:31 +020030static LY_ERR
Radek Krejci18abde42020-06-13 20:04:39 +020031test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
Radek Krejcib4ac5a92020-11-23 17:54:33 +010032 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
33 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejci18abde42020-06-13 20:04:39 +020034{
35 *module_data = user_data;
Radek Krejci85ac8312021-03-03 20:21:33 +010036 if ((*module_data)[0] == '<') {
37 *format = LYS_IN_YIN;
38 } else {
39 *format = LYS_IN_YANG;
40 }
Radek Krejci18abde42020-06-13 20:04:39 +020041 *free_module_data = NULL;
42 return LY_SUCCESS;
43}
Radek Krejci3a4889a2020-05-19 17:01:58 +020044
Michal Vaskoc636ea42022-09-16 10:20:31 +020045#define TEST_YANG_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
46 "module "MOD_NAME" { namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
Radek Krejci18abde42020-06-13 20:04:39 +020047
Michal Vaskoc636ea42022-09-16 10:20:31 +020048#define TEST_YANG_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
49 "module "MOD_NAME" {yang-version 1.1; namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
Radek Krejci3a4889a2020-05-19 17:01:58 +020050
Michal Vaskoc636ea42022-09-16 10:20:31 +020051#define TEST_YIN_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
52 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\">" \
53 "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
54
55#define TEST_YIN_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
56 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\"><yang-version value=\"1.1\"/>" \
57 "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
58
59#define TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, STR) \
60 if (YIN) { \
61 if (RFC7950) { \
62 STR = TEST_YIN_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
63 } else { \
64 STR = TEST_YIN_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
65 } \
66 } else { /* YANG */ \
67 if (RFC7950) { \
68 STR = TEST_YANG_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
69 } else { \
70 STR = TEST_YANG_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
71 } \
72 }
73
74#define TEST_SCHEMA_OK(RFC7950, YIN, MOD_NAME, CONTENT, RESULT) \
75 { \
76 const char *test_str__; \
77 TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
78 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, &(RESULT))); \
79 }
80
81#define TEST_SCHEMA_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
82 { \
83 const char *test_str__; \
84 TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
85 assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
86 CHECK_LOG_CTX(ERRMSG, ERRPATH); \
87 }
88
89#define TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG, ERRPATH) \
90 { \
91 const char *test_str__; \
92 TEST_SCHEMA_STR(RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
93 assert_int_not_equal(lys_parse_mem(UTEST_LYCTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
Michal Vasko62af3692023-02-09 14:00:09 +010094 CHECK_LOG_CTX("Parsing module \""MOD_NAME"\" failed.", NULL); \
95 CHECK_LOG_CTX(ERRMSG, ERRPATH); \
Michal Vaskoc636ea42022-09-16 10:20:31 +020096 }
97
98#define TEST_STMT_DUP(RFC7950, YIN, STMT, MEMBER, VALUE1, VALUE2, LINE) \
99 if (YIN) { \
100 TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", "", "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
101 } else { \
102 TEST_SCHEMA_PARSE_ERR(RFC7950, YIN, "dup", STMT"{"MEMBER" "VALUE1";"MEMBER" "VALUE2";}", \
103 "Duplicate keyword \""MEMBER"\".", "Line number "LINE"."); \
104 }
105
106#define TEST_STMT_SUBSTM_ERR(RFC7950, STMT, SUBSTMT, VALUE) ;\
107 TEST_SCHEMA_PARSE_ERR(RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", \
108 "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\".", "Line number 1.");
109
110struct module_clb_list {
111 const char *name;
112 const char *data;
113};
114
115static LY_ERR
116module_clb(const char *mod_name, const char *UNUSED(mod_rev), const char *submod_name,
117 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
118 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
119{
120 struct module_clb_list *list = (struct module_clb_list *)user_data;
121
122 for (unsigned int i = 0; list[i].data; i++) {
123
124 if ((submod_name && !strcmp(list[i].name, submod_name)) ||
125 (!submod_name && mod_name && !strcmp(list[i].name, mod_name))) {
126 *module_data = list[i].data;
127 *format = LYS_IN_YANG;
128 *free_module_data = NULL;
129 return LY_SUCCESS;
130 }
131 }
132 return LY_EINVAL;
133}
134
135static void
136test_getnext(void **state)
137{
138 struct lys_module *mod;
139 const struct lysc_node *node = NULL, *four;
140 const struct lysc_node_container *cont;
141 const struct lysc_action *rpc;
142
143 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {yang-version 1.1; namespace urn:a;prefix a;"
144 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
145 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
146 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
147 " notification nine {leaf nine-data {type string;}}}"
148 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
149 "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
150 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
151 "rpc i;"
152 "notification j {leaf i-data {type string;}}"
153 "notification k;}", LYS_IN_YANG, &mod));
154 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
155 assert_string_equal("a", node->name);
156 cont = (const struct lysc_node_container *)node;
157 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
158 assert_string_equal("b", node->name);
159 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
160 assert_string_equal("c", node->name);
161 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
162 assert_string_equal("d", node->name);
163 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
164 assert_string_equal("e", node->name);
165 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
166 assert_string_equal("f", node->name);
167 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
168 assert_string_equal("g", node->name);
169 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
170 assert_string_equal("h", node->name);
171 rpc = (const struct lysc_action *)node;
172 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
173 assert_string_equal("i", node->name);
174 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
175 assert_string_equal("j", node->name);
176 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
177 assert_string_equal("k", node->name);
178 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
179 /* Inside container */
180 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
181 assert_string_equal("one", node->name);
182 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
183 assert_string_equal("two", node->name);
184 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
185 assert_string_equal("three", node->name);
186 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
187 assert_string_equal("four", node->name);
188 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
189 assert_string_equal("five", node->name);
190 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
191 assert_string_equal("six", node->name);
192 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
193 assert_string_equal("seven", node->name);
194 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
195 assert_string_equal("eight", node->name);
196 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
197 assert_string_equal("nine", node->name);
198 assert_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, 0));
199 /* Inside RPC */
200 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
201 assert_string_equal("h-input", node->name);
202 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, 0));
203
204 /* options */
205 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
206 assert_string_equal("x", node->name);
207 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
208 assert_string_equal("seven", node->name);
209
210 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
211 assert_string_equal("seven", node->name);
212
213 assert_non_null(node = lys_getnext(four, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
214 assert_string_equal("five", node->name);
215 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
216 assert_string_equal("y", node->name);
217 assert_non_null(node = lys_getnext(node, (const struct lysc_node *)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
218 assert_string_equal("seven", node->name);
219
220 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
221 assert_string_equal("one", node->name);
222
223 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
224 assert_string_equal("h-output", node->name);
225 assert_null(node = lys_getnext(node, (const struct lysc_node *)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
226
227 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod));
228 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
229 assert_string_equal("c", node->name);
230 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
231
232 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod));
233 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
234 assert_string_equal("d", node->name);
235 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
236
237 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
238 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
239 assert_string_equal("c", node->name);
240 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
241 assert_string_equal("a", node->name);
242}
243
244static void
245test_date(void **state)
246{
247 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
248 CHECK_LOG("Invalid argument date (lysp_check_date()).", NULL);
249 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Michal Vaskob425bb32022-11-08 10:49:36 +0100250 CHECK_LOG("Invalid length 1 of a date.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200251 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
252 CHECK_LOG("Invalid value \"nonsencexx\" of \"date\".", NULL);
253 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
254 CHECK_LOG("Invalid value \"123x-11-11\" of \"date\".", NULL);
255 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
256 CHECK_LOG("Invalid value \"2018-13-11\" of \"date\".", NULL);
257 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
258 CHECK_LOG("Invalid value \"2018-11-41\" of \"date\".", NULL);
259 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
260 CHECK_LOG("Invalid value \"2018-02-29\" of \"date\".", NULL);
261 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
262 CHECK_LOG("Invalid value \"2018.02-28\" of \"date\".", NULL);
263 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
264 CHECK_LOG("Invalid value \"2018-02.28\" of \"date\".", NULL);
265
266 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
267 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
268 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
269}
270
271static void
272test_revisions(void **state)
273{
274 struct lysp_revision *revs = NULL, *rev;
275
276 /* no error, it just does nothing */
277 lysp_sort_revisions(NULL);
278 CHECK_LOG(NULL, NULL);
279
280 /* revisions are stored in wrong order - the newest is the last */
281 LY_ARRAY_NEW_RET(NULL, revs, rev, );
282 strcpy(rev->date, "2018-01-01");
283 LY_ARRAY_NEW_RET(NULL, revs, rev, );
284 strcpy(rev->date, "2018-12-31");
285
286 assert_int_equal(2, LY_ARRAY_COUNT(revs));
Michal Vasko892355e2023-03-14 13:37:48 +0100287 assert_string_equal("2018-01-01", revs[0].date);
288 assert_string_equal("2018-12-31", revs[1].date);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200289 /* the order should be fixed, so the newest revision will be the first in the array */
290 lysp_sort_revisions(revs);
Michal Vasko892355e2023-03-14 13:37:48 +0100291 assert_string_equal("2018-12-31", revs[0].date);
292 assert_string_equal("2018-01-01", revs[1].date);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200293
294 LY_ARRAY_FREE(revs);
295}
296
297static void
298test_collision_typedef(void **state)
299{
300 const char *str;
301 char *submod;
302 struct module_clb_list list[3] = {0};
303
304 list[0].name = "asub";
305 list[1].name = "bsub";
306
307 /* collision with a built-in type */
308 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
309 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100310 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
311 CHECK_LOG_CTX("Duplicate identifier \"binary\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200312 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
313 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100314 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
315 CHECK_LOG_CTX("Duplicate identifier \"bits\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200316 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
317 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100318 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
319 CHECK_LOG_CTX("Duplicate identifier \"boolean\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200320 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
321 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100322 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
323 CHECK_LOG_CTX("Duplicate identifier \"decimal64\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200324 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
325 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100326 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
327 CHECK_LOG_CTX("Duplicate identifier \"empty\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200328 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
329 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100330 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
331 CHECK_LOG_CTX("Duplicate identifier \"enumeration\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200332 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
333 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100334 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
335 CHECK_LOG_CTX("Duplicate identifier \"int8\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200336 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
337 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100338 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
339 CHECK_LOG_CTX("Duplicate identifier \"int16\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200340 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
341 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100342 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
343 CHECK_LOG_CTX("Duplicate identifier \"int32\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200344 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
345 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100346 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
347 CHECK_LOG_CTX("Duplicate identifier \"int64\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200348 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
349 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100350 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
351 CHECK_LOG_CTX("Duplicate identifier \"instance-identifier\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200352 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
353 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100354 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
355 CHECK_LOG_CTX("Duplicate identifier \"identityref\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200356 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
357 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100358 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
359 CHECK_LOG_CTX("Duplicate identifier \"leafref\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200360 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
361 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100362 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
363 CHECK_LOG_CTX("Duplicate identifier \"string\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200364 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
365 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100366 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
367 CHECK_LOG_CTX("Duplicate identifier \"union\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200368 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
369 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100370 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
371 CHECK_LOG_CTX("Duplicate identifier \"uint8\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200372 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
373 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100374 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
375 CHECK_LOG_CTX("Duplicate identifier \"uint16\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200376 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
377 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100378 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
379 CHECK_LOG_CTX("Duplicate identifier \"uint32\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200380 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
381 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100382 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
383 CHECK_LOG_CTX("Duplicate identifier \"uint64\" of typedef statement - name collision with a built-in type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200384
385 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
386 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
387 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
388 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
389 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
390 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
391
392 /* collision in node's scope */
393 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
394 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100395 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
396 CHECK_LOG_CTX("Duplicate identifier \"y\" of typedef statement - name collision with sibling type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200397
398 /* collision with parent node */
399 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
400 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100401 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
402 CHECK_LOG_CTX("Duplicate identifier \"y\" of typedef statement - name collision with another scoped type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200403
404 /* collision with module's top-level */
405 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
406 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100407 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
408 CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200409
410 /* collision of submodule's node with module's top-level */
411 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
412 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
413 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100414 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
415 CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200416
417 /* collision of module's node with submodule's top-level */
418 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
419 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
420 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100421 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
422 CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - scoped type collide with a top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200423
424 /* collision of submodule's node with another submodule's top-level */
425 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
426 list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
427 list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {typedef g {type int;}}}";
428 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
429 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100430 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
431 CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - scoped type collide with a top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200432
433 /* collision of module's top-levels */
434 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
435 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100436 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
437 CHECK_LOG_CTX("Duplicate identifier \"test\" of typedef statement - name collision with another top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200438
439 /* collision of submodule's top-levels */
440 submod = "submodule asub {belongs-to a {prefix a;} typedef g {type int;} typedef g {type int;}}";
441 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
442 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
443 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100444 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
445 CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200446
447 /* collision of module's top-level with submodule's top-levels */
448 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
449 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
450 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100451 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
452 CHECK_LOG_CTX("Duplicate identifier \"x\" of typedef statement - name collision with another top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200453
454 /* collision of submodule's top-level with another submodule's top-levels */
455 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
456 list[0].data = "submodule asub {belongs-to a {prefix a;} typedef g {type int;}}";
457 list[1].data = "submodule bsub {belongs-to a {prefix a;} typedef g {type int;}}";
458 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
459 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100460 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
461 CHECK_LOG_CTX("Duplicate identifier \"g\" of typedef statement - name collision with another top-level type.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200462
463 /* error in type-stmt */
464 str = "module a {namespace urn:a; prefix a; container c {typedef x {type t{}}";
465 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100466 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
467 CHECK_LOG_CTX("Unexpected end-of-input.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +0200468
469 /* no collision if the same names are in different scope */
470 str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
471 "container c {typedef g {type int;}} container d {typedef g {type int;}}}";
472 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
473}
474
475static void
476test_collision_grouping(void **state)
477{
478 const char *str;
479 char *submod;
480 struct module_clb_list list[3] = {0};
481
482 list[0].name = "asub";
483 list[1].name = "bsub";
484
485 /* collision in node's scope */
486 str = "module a {namespace urn:a; prefix a; container c {grouping y; grouping y;}}";
487 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100488 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
489 CHECK_LOG_CTX("Duplicate identifier \"y\" of grouping statement - name collision with sibling grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200490
491 /* collision with parent node */
492 str = "module a {namespace urn:a; prefix a; container c {container d {grouping y;} grouping y;}}";
493 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100494 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
495 CHECK_LOG_CTX("Duplicate identifier \"y\" of grouping statement - name collision with another scoped grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200496
497 /* collision with module's top-level */
498 str = "module a {namespace urn:a; prefix a; grouping x; container c {grouping x;}}";
499 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100500 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
501 CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200502
503 /* collision of submodule's node with module's top-level */
504 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {grouping x;}}");
505 str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
506 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100507 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
508 CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200509
510 /* collision of module's node with submodule's top-level */
511 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
512 str = "module a {namespace urn:a; prefix a; include b; container c {grouping x;}}";
513 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100514 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
515 CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200516
517 /* collision of submodule's node with another submodule's top-level */
518 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
519 list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
520 list[1].data = "submodule bsub {belongs-to a {prefix a;} container c {grouping g;}}";
521 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
522 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100523 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
524 CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200525
526 /* collision of module's top-levels */
527 str = "module a {namespace urn:a; prefix a; grouping test; grouping test;}";
528 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100529 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
530 CHECK_LOG_CTX("Duplicate identifier \"test\" of grouping statement - name collision with another top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200531
532 /* collision of submodule's top-levels */
533 submod = "submodule asub {belongs-to a {prefix a;} grouping g; grouping g;}";
534 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
535 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
536 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100537 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
538 CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200539
540 /* collision of module's top-level with submodule's top-levels */
541 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule b {belongs-to a {prefix a;} grouping x;}");
542 str = "module a {namespace urn:a; prefix a; include b; grouping x;}";
543 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100544 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
545 CHECK_LOG_CTX("Duplicate identifier \"x\" of grouping statement - name collision with another top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200546
547 /* collision of submodule's top-level with another submodule's top-levels */
548 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
549 list[0].data = "submodule asub {belongs-to a {prefix a;} grouping g;}";
550 list[1].data = "submodule bsub {belongs-to a {prefix a;} grouping g;}";
551 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
552 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100553 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
554 CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200555
556 /* collision in nested groupings, top-level */
557 str = "module a {namespace urn:a; prefix a; grouping g {grouping g;}}";
558 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100559 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
560 CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - scoped grouping collide with a top-level grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200561
562 /* collision in nested groupings, in node */
563 str = "module a {namespace urn:a; prefix a; container c {grouping g {grouping g;}}}";
564 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100565 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
566 CHECK_LOG_CTX("Duplicate identifier \"g\" of grouping statement - name collision with another scoped grouping.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200567
568 /* no collision if the same names are in different scope */
569 str = "module a {yang-version 1.1; namespace urn:a; prefix a;"
570 "container c {grouping g;} container d {grouping g;}}";
571 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100572 CHECK_LOG_CTX("Locally scoped grouping \"g\" not used.", NULL);
573 CHECK_LOG_CTX("Locally scoped grouping \"g\" not used.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200574}
575
576static void
577test_collision_identity(void **state)
578{
579 const char *str;
580 char *submod;
581 struct module_clb_list list[3] = {0};
582
583 list[0].name = "asub";
584 list[1].name = "bsub";
585
586 /* collision of module's top-levels */
587 str = "module a {yang-version 1.1; namespace urn:a; prefix a; identity g; identity g;}";
588 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100589 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
590 CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200591
592 /* collision of submodule's top-levels */
593 submod = "submodule asub {belongs-to a {prefix a;} identity g; identity g;}";
594 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
595 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
596 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100597 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
598 CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200599
600 /* collision of module's top-level with submodule's top-levels */
601 submod = "submodule asub {belongs-to a {prefix a;} identity g;}";
602 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; identity g;}";
603 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
604 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100605 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
606 CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200607
608 /* collision of submodule's top-level with another submodule's top-levels */
609 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
610 list[0].data = "submodule asub {belongs-to a {prefix a;} identity g;}";
611 list[1].data = "submodule bsub {belongs-to a {prefix a;} identity g;}";
612 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
613 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100614 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
615 CHECK_LOG_CTX("Duplicate identifier \"g\" of identity statement - name collision with another top-level identity.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200616}
617
618static void
619test_collision_feature(void **state)
620{
621 const char *str;
622 char *submod;
623 struct module_clb_list list[3] = {0};
624
625 list[0].name = "asub";
626 list[1].name = "bsub";
627
628 /* collision of module's top-levels */
629 str = "module a {yang-version 1.1; namespace urn:a; prefix a; feature g; feature g;}";
630 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100631 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
632 CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200633
634 /* collision of submodule's top-levels */
635 submod = "submodule asub {belongs-to a {prefix a;} feature g; feature g;}";
636 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub;}";
637 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
638 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100639 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
640 CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200641
642 /* collision of module's top-level with submodule's top-levels */
643 submod = "submodule asub {belongs-to a {prefix a;} feature g;}";
644 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; feature g;}";
645 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod);
646 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100647 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
648 CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200649
650 /* collision of submodule's top-level with another submodule's top-levels */
651 str = "module a {yang-version 1.1; namespace urn:a; prefix a; include asub; include bsub;}";
652 list[0].data = "submodule asub {belongs-to a {prefix a;} feature g;}";
653 list[1].data = "submodule bsub {belongs-to a {prefix a;} feature g;}";
654 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
655 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100656 CHECK_LOG_CTX("Parsing module \"a\" failed.", NULL);
657 CHECK_LOG_CTX("Duplicate identifier \"g\" of feature statement - name collision with another top-level feature.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200658}
659
660static void
661test_accessible_tree(void **state)
662{
663 const char *str;
664
665 /* config -> config */
666 str = "module a {\n"
667 " namespace urn:a;\n"
668 " prefix a;\n"
669 " container cont {\n"
670 " leaf l {\n"
671 " type empty;\n"
672 " }\n"
673 " }\n"
674 " container cont2 {\n"
675 " leaf l2 {\n"
676 " must ../../cont/l;\n"
677 " type leafref {\n"
678 " path /cont/l;\n"
679 " }\n"
680 " }\n"
681 " }\n"
682 "}";
683 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
684 CHECK_LOG_CTX(NULL, NULL);
685
686 /* config -> state leafref */
687 str = "module b {\n"
688 " namespace urn:b;\n"
689 " prefix b;\n"
690 " container cont {\n"
691 " config false;\n"
692 " leaf l {\n"
693 " type empty;\n"
694 " }\n"
695 " }\n"
696 " container cont2 {\n"
697 " leaf l2 {\n"
698 " type leafref {\n"
699 " path /cont/l;\n"
700 " }\n"
701 " }\n"
702 " }\n"
703 "}";
704 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
705 CHECK_LOG_CTX("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data"
706 " (as the leafref does), but it does not.", "Schema location \"/b:cont2/l2\".");
707
708 /* config -> state must */
709 str = "module b {\n"
710 " namespace urn:b;\n"
711 " prefix b;\n"
712 " container cont {\n"
713 " config false;\n"
714 " leaf l {\n"
715 " type empty;\n"
716 " }\n"
717 " }\n"
718 " container cont2 {\n"
719 " leaf l2 {\n"
720 " must ../../cont/l;\n"
721 " type empty;\n"
722 " }\n"
723 " }\n"
724 "}";
725 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
726 CHECK_LOG_CTX("Schema node \"cont\" for parent \"<config-root>\" not found; in expr \"../../cont\" "
727 "with context node \"/b:cont2/l2\".", NULL);
728
729 /* state -> config */
730 str = "module c {\n"
731 " namespace urn:c;\n"
732 " prefix c;\n"
733 " container cont {\n"
734 " leaf l {\n"
735 " type empty;\n"
736 " }\n"
737 " }\n"
738 " container cont2 {\n"
739 " config false;\n"
740 " leaf l2 {\n"
741 " must ../../cont/l;\n"
742 " type leafref {\n"
743 " path /cont/l;\n"
744 " }\n"
745 " }\n"
746 " }\n"
747 "}";
748 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
749 CHECK_LOG_CTX(NULL, NULL);
750
751 /* notif -> state */
752 str = "module d {\n"
753 " namespace urn:d;\n"
754 " prefix d;\n"
755 " container cont {\n"
756 " config false;\n"
757 " leaf l {\n"
758 " type empty;\n"
759 " }\n"
760 " }\n"
761 " notification notif {\n"
762 " leaf l2 {\n"
763 " must ../../cont/l;\n"
764 " type leafref {\n"
765 " path /cont/l;\n"
766 " }\n"
767 " }\n"
768 " }\n"
769 "}";
770 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
771 CHECK_LOG_CTX(NULL, NULL);
772
773 /* notif -> notif */
774 str = "module e {\n"
775 " namespace urn:e;\n"
776 " prefix e;\n"
777 " notification notif {\n"
778 " leaf l {\n"
779 " type empty;\n"
780 " }\n"
781 " leaf l2 {\n"
782 " must ../../notif/l;\n"
783 " type leafref {\n"
784 " path /notif/l;\n"
785 " }\n"
786 " }\n"
787 " }\n"
788 "}";
789 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
790 CHECK_LOG_CTX(NULL, NULL);
791
792 /* rpc input -> state */
793 str = "module f {\n"
794 " namespace urn:f;\n"
795 " prefix f;\n"
796 " container cont {\n"
797 " config false;\n"
798 " leaf l {\n"
799 " type empty;\n"
800 " }\n"
801 " }\n"
802 " rpc rp {\n"
803 " input {\n"
804 " leaf l2 {\n"
805 " must ../../cont/l;\n"
806 " type leafref {\n"
807 " path /cont/l;\n"
808 " }\n"
809 " }\n"
810 " }\n"
811 " }\n"
812 "}";
813 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
814 CHECK_LOG_CTX(NULL, NULL);
815
816 /* rpc input -> rpc input */
817 str = "module g {\n"
818 " namespace urn:g;\n"
819 " prefix g;\n"
820 " rpc rp {\n"
821 " input {\n"
822 " leaf l {\n"
823 " type empty;\n"
824 " }\n"
825 " leaf l2 {\n"
826 " must ../l;\n"
827 " type leafref {\n"
828 " path /rp/l;\n"
829 " }\n"
830 " }\n"
831 " }\n"
832 " }\n"
833 "}";
834 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
835 CHECK_LOG_CTX(NULL, NULL);
836
837 /* rpc input -> rpc output leafref */
838 str = "module h {\n"
839 " namespace urn:h;\n"
840 " prefix h;\n"
841 " rpc rp {\n"
842 " input {\n"
843 " leaf l2 {\n"
844 " type leafref {\n"
845 " path /rp/l;\n"
846 " }\n"
847 " }\n"
848 " }\n"
849 " output {\n"
850 " leaf l {\n"
851 " type empty;\n"
852 " }\n"
853 " }\n"
854 " }\n"
855 "}";
856 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
857 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/h:rp/input/l2\".");
858
859 /* rpc input -> rpc output must */
860 str = "module h {\n"
861 " namespace urn:h;\n"
862 " prefix h;\n"
863 " rpc rp {\n"
864 " input {\n"
865 " leaf l2 {\n"
866 " must ../l;\n"
867 " type empty;\n"
868 " }\n"
869 " }\n"
870 " output {\n"
871 " leaf l {\n"
872 " type empty;\n"
873 " }\n"
874 " }\n"
875 " }\n"
876 "}";
877 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
878 CHECK_LOG_CTX("Schema node \"l\" for parent \"/h:rp\" not found; in expr \"../l\" with context node \"/h:rp/input/l2\".", NULL);
879
880 /* rpc input -> notif leafref */
881 str = "module i {\n"
882 " namespace urn:i;\n"
883 " prefix i;\n"
884 " rpc rp {\n"
885 " input {\n"
886 " leaf l2 {\n"
887 " type leafref {\n"
888 " path ../../notif/l;\n"
889 " }\n"
890 " }\n"
891 " }\n"
892 " }\n"
893 " notification notif {\n"
894 " leaf l {\n"
895 " type empty;\n"
896 " }\n"
897 " }\n"
898 "}";
899 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
900 CHECK_LOG_CTX("Not found node \"notif\" in path.", "Schema location \"/i:rp/input/l2\".");
901
902 /* rpc input -> notif must */
903 str = "module i {\n"
904 " namespace urn:i;\n"
905 " prefix i;\n"
906 " rpc rp {\n"
907 " input {\n"
908 " leaf l2 {\n"
909 " must /notif/l;\n"
910 " type empty;\n"
911 " }\n"
912 " }\n"
913 " }\n"
914 " notification notif {\n"
915 " leaf l {\n"
916 " type empty;\n"
917 " }\n"
918 " }\n"
919 "}";
920 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
921 CHECK_LOG_CTX("Schema node \"notif\" for parent \"<root>\" not found; in expr \"/notif\" "
922 "with context node \"/i:rp/input/l2\".", NULL);
923
924 /* action output -> state */
925 str = "module j {\n"
926 " yang-version 1.1;\n"
927 " namespace urn:j;\n"
928 " prefix j;\n"
929 " container cont {\n"
930 " list ll {\n"
931 " key k;\n"
932 " leaf k {\n"
933 " type string;\n"
934 " }\n"
935 " action act {\n"
936 " output {\n"
937 " leaf l2 {\n"
938 " must /cont/l;\n"
939 " type leafref {\n"
940 " path ../../../l;\n"
941 " }\n"
942 " }\n"
943 " }\n"
944 " }\n"
945 " }\n"
946 " leaf l {\n"
947 " config false;\n"
948 " type empty;\n"
949 " }\n"
950 " }\n"
951 "}";
952 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
953 CHECK_LOG_CTX(NULL, NULL);
954
955 /* action output -> action input leafref */
956 str = "module k {\n"
957 " yang-version 1.1;\n"
958 " namespace urn:k;\n"
959 " prefix k;\n"
960 " container cont {\n"
961 " list ll {\n"
962 " key k;\n"
963 " leaf k {\n"
964 " type string;\n"
965 " }\n"
966 " action act {\n"
967 " input {\n"
968 " leaf l {\n"
969 " type empty;\n"
970 " }\n"
971 " }\n"
972 " output {\n"
973 " leaf l2 {\n"
974 " type leafref {\n"
975 " path ../l;\n"
976 " }\n"
977 " }\n"
978 " }\n"
979 " }\n"
980 " }\n"
981 " }\n"
982 "}";
983 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
984 CHECK_LOG_CTX("Not found node \"l\" in path.", "Schema location \"/k:cont/ll/act/output/l2\".");
985
986 /* action output -> action input must */
987 str = "module k {\n"
988 " yang-version 1.1;\n"
989 " namespace urn:k;\n"
990 " prefix k;\n"
991 " container cont {\n"
992 " list ll {\n"
993 " key k;\n"
994 " leaf k {\n"
995 " type string;\n"
996 " }\n"
997 " action act {\n"
998 " input {\n"
999 " leaf l {\n"
1000 " type empty;\n"
1001 " }\n"
1002 " }\n"
1003 " output {\n"
1004 " leaf l2 {\n"
1005 " must /cont/ll/act/l;\n"
1006 " type empty;\n"
1007 " }\n"
1008 " }\n"
1009 " }\n"
1010 " }\n"
1011 " }\n"
1012 "}";
1013 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_SUCCESS);
1014 CHECK_LOG_CTX("Schema node \"l\" for parent \"/k:cont/ll/act\" not found; in expr \"/cont/ll/act/l\" "
1015 "with context node \"/k:cont/ll/act/output/l2\".", NULL);
1016}
1017
1018static void
1019test_includes(void **state)
1020{
1021 struct lys_module *mod;
1022
1023 {
1024 /* YANG 1.0 - the missing include sub_a_two in main_a will be injected from sub_a_one */
1025 struct module_clb_list list[] = {
1026 {"main_a", "module main_a { namespace urn:test:main_a; prefix ma; include sub_a_one;}"},
1027 {"sub_a_one", "submodule sub_a_one { belongs-to main_a { prefix ma; } include sub_a_two;}"},
1028 {"sub_a_two", "submodule sub_a_two { belongs-to main_a { prefix ma; } }"},
1029 {NULL, NULL}
1030 };
1031
1032 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
1033 mod = ly_ctx_load_module(UTEST_LYCTX, "main_a", NULL, NULL);
1034 assert_non_null(mod);
1035 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
1036 assert_true(mod->parsed->includes[1].injected);
1037 }
1038
1039 {
1040 /* YANG 1.1 - the missing include sub_b_two in main_b is error */
1041 struct module_clb_list list[] = {
1042 {"main_b", "module main_b { yang-version 1.1; namespace urn:test:main_b; prefix mb; include sub_b_one;}"},
1043 {"sub_b_one", "submodule sub_b_one { yang-version 1.1; belongs-to main_b { prefix mb; } include sub_b_two;}"},
1044 {"sub_b_two", "submodule sub_b_two { yang-version 1.1; belongs-to main_b { prefix mb; } }"},
1045 {NULL, NULL}
1046 };
1047
1048 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
1049 mod = ly_ctx_load_module(UTEST_LYCTX, "main_b", NULL, NULL);
1050 assert_null(mod);
Michal Vasko62af3692023-02-09 14:00:09 +01001051 CHECK_LOG_CTX("Loading \"main_b\" module failed.", NULL);
1052 CHECK_LOG_CTX("Data model \"main_b\" not found in local searchdirs.", NULL);
1053 CHECK_LOG_CTX("Parsing module \"main_b\" failed.", NULL);
1054 CHECK_LOG_CTX("Including \"sub_b_one\" submodule into \"main_b\" failed.", NULL);
1055 CHECK_LOG_CTX("Data model \"sub_b_one\" not found in local searchdirs.", NULL);
1056 CHECK_LOG_CTX("Parsing submodule \"sub_b_one\" failed.", NULL);
1057 CHECK_LOG_CTX("YANG 1.1 requires all submodules to be included from main module. But submodule \"sub_b_one\" includes "
1058 "submodule \"sub_b_two\" which is not included by main module \"main_b\".", NULL);
1059 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_b_one) are not necessary.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001060 }
1061
1062 {
1063 /* YANG 1.1 - all includes are in main_c, includes in submodules are not necessary, so expect warning */
1064 struct module_clb_list list[] = {
1065 {"main_c", "module main_c { yang-version 1.1; namespace urn:test:main_c; prefix mc; include sub_c_one; include sub_c_two;}"},
1066 {"sub_c_one", "submodule sub_c_one { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_two;}"},
1067 {"sub_c_two", "submodule sub_c_two { yang-version 1.1; belongs-to main_c { prefix mc; } include sub_c_one;}"},
1068 {NULL, NULL}
1069 };
1070
1071 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list);
1072 mod = ly_ctx_load_module(UTEST_LYCTX, "main_c", NULL, NULL);
1073 assert_non_null(mod);
1074 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->includes));
1075 assert_false(mod->parsed->includes[1].injected);
1076 /* result is ok, but log includes the warning */
1077 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_two) are not necessary.", NULL);
Michal Vasko62af3692023-02-09 14:00:09 +01001078 CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (sub_c_one) are not necessary.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001079 }
1080}
1081
1082static void
1083test_key_order(void **state)
1084{
1085 struct lys_module *mod;
1086 const struct lysc_node *node;
1087
1088 struct module_clb_list list1[] = {
1089 {"a", "module a {"
1090 "yang-version 1.1;"
1091 "namespace urn:test:a;"
1092 "prefix a;"
1093 "list l {"
1094 " key \"k1 k2\";"
1095 " leaf k2 {type string;}"
1096 " leaf k1 {type string;}"
1097 "}"
1098 "}"},
1099 {NULL, NULL}
1100 };
1101
1102 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list1);
1103 mod = ly_ctx_load_module(UTEST_LYCTX, "a", NULL, NULL);
1104 assert_non_null(mod);
1105
1106 node = lysc_node_child(mod->compiled->data);
1107 assert_string_equal("k1", node->name);
1108 node = node->next;
1109 assert_string_equal("k2", node->name);
1110
1111 struct module_clb_list list2[] = {
1112 {"b", "module b {"
1113 "yang-version 1.1;"
1114 "namespace urn:test:b;"
1115 "prefix b;"
1116 "list l {"
1117 " key \"k1 k2 k3 k4\";"
1118 " leaf k4 {type string;}"
1119 " container c {"
1120 " leaf l1 {type string;}"
1121 " }"
1122 " leaf k2 {type string;}"
1123 " leaf l2 {type string;}"
1124 " leaf k1 {type string;}"
1125 " leaf k3 {type string;}"
1126 "}"
1127 "}"},
1128 {NULL, NULL}
1129 };
1130
1131 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list2);
1132 mod = ly_ctx_load_module(UTEST_LYCTX, "b", NULL, NULL);
1133 assert_non_null(mod);
1134
1135 node = lysc_node_child(mod->compiled->data);
1136 assert_string_equal("k1", node->name);
1137 node = node->next;
1138 assert_string_equal("k2", node->name);
1139 node = node->next;
1140 assert_string_equal("k3", node->name);
1141 node = node->next;
1142 assert_string_equal("k4", node->name);
1143}
1144
1145static void
1146test_disabled_enum(void **state)
1147{
1148 const char *str;
1149
1150 /* no enabled enum */
1151 str = "module a {"
1152 "yang-version 1.1;"
1153 "namespace urn:test:a;"
1154 "prefix a;"
1155 "feature f;"
1156 "leaf l {type enumeration {"
1157 " enum e1 {if-feature f;}"
1158 " enum e2 {if-feature f;}"
1159 "}}"
1160 "}";
1161 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
1162 CHECK_LOG_CTX("Enumeration type of node \"l\" without any (or all disabled) valid values.", "Schema location \"/a:l\".");
1163
1164 /* disabled default value */
1165 str = "module a {"
1166 "yang-version 1.1;"
1167 "namespace urn:test:a;"
1168 "prefix a;"
1169 "feature f;"
1170 "leaf l {"
1171 " type enumeration {"
1172 " enum e1 {if-feature f;}"
1173 " enum e2;"
1174 " }"
1175 " default e1;"
1176 "}"
1177 "}";
1178 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
1179 CHECK_LOG_CTX("Invalid default - value does not fit the type (Invalid enumeration value \"e1\".).",
1180 "Schema location \"/a:l\".");
1181}
1182
1183static void
1184test_identity(void **state)
1185{
1186 struct lys_module *mod, *mod_imp;
1187
1188 /*
1189 * parsing YANG
1190 */
1191 TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", "1");
1192 TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", "1");
1193 TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", "1");
1194
1195 /* full content */
1196 TEST_SCHEMA_OK(1, 0, "identityone",
1197 "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
1198 "identity a; identity b; extension ext; feature x; feature y;", mod);
1199 assert_non_null(mod->parsed->identities);
1200 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
1201
1202 /* invalid substatement */
1203 TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX");
1204
1205 /*
1206 * parsing YIN
1207 */
1208 /* max subelems */
1209 TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">"
1210 "<if-feature name=\"iff\"/>"
1211 "<base name=\"base-name\"/>"
1212 "<status value=\"deprecated\"/>"
1213 "<description><text>desc</text></description>"
1214 "<reference><text>ref</text></reference>"
1215 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
1216 "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
1217 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
1218 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
1219 assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
1220 assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
1221 assert_string_equal(mod->parsed->identities[0].dsc, "desc");
1222 assert_string_equal(mod->parsed->identities[0].ref, "ref");
1223 assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
1224 /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
1225 assert_non_null(mod->parsed->identities[0].exts[0].compiled);
1226 assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
1227 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
1228 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
1229
1230 /* min subelems */
1231 TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
1232 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
1233 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
1234
1235 /* invalid substatement */
1236 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
1237 "Invalid sub-elemnt \"if-feature\" of \"identity\" element - "
1238 "this sub-element is allowed only in modules with version 1.1 or newer.", "Line number 1.");
1239
1240 /*
1241 * compiling
1242 */
1243 TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp);
1244 TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}"
1245 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
1246 "identity b4 {base b:b1; base b3;}", mod);
1247 assert_non_null(mod_imp->compiled);
1248 assert_non_null(mod_imp->identities);
1249 assert_non_null(mod->identities);
1250 assert_non_null(mod_imp->identities[0].derived);
1251 assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
1252 assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
1253 assert_non_null(mod->identities[0].derived);
1254 assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
1255 assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
1256 assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
1257 assert_non_null(mod->identities[1].derived);
1258 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
1259 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
1260 assert_non_null(mod->identities[2].derived);
1261 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
1262 assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
1263
1264 TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
1265 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
1266 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
1267
1268 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} identity i1;}");
Michal Vasko6727c682023-02-17 10:40:26 +01001269 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "Path \"/inv:{identity='i1'}\".");
1270 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i1;}", "Identity \"i1\" is derived from itself.", "Path \"/inv:{identity='i1'}\".");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001271 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}",
Michal Vasko6727c682023-02-17 10:40:26 +01001272 "Identity \"i1\" is indirectly derived from itself.", "Path \"/inv:{identity='i3'}\".");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001273
1274 /* base in non-implemented module */
1275 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
1276 "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
1277 TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
1278
1279 /* default value from non-implemented module */
1280 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
1281 "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
1282 " - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
1283
1284 /* default value in typedef from non-implemented module */
1285 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
1286 "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
1287 " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
1288
1289 /*
1290 * printing
1291 */
1292
1293 /*
1294 * cleanup
1295 */
1296}
1297
1298static void
1299test_feature(void **state)
1300{
1301 struct lys_module *mod;
1302 const struct lysp_feature *f;
1303
1304 /*
1305 * parsing YANG
1306 */
1307
1308 TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", "1");
1309 TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", "1");
1310 TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", "1");
1311
1312 /* full content */
1313 TEST_SCHEMA_OK(1, 0, "featureone",
1314 "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
1315 "extension ext; feature x; feature y;", mod);
1316 assert_non_null(mod->parsed->features);
1317 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
1318
1319 /* invalid substatement */
1320 TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX");
1321
1322 /*
1323 * parsing YIN
1324 */
1325 /* max subelems */
1326 TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">"
1327 "<if-feature name=\"iff\"/>"
1328 "<status value=\"deprecated\"/>"
1329 "<description><text>desc</text></description>"
1330 "<reference><text>ref</text></reference>"
1331 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
1332 "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
1333 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
1334 assert_string_equal(mod->parsed->features[0].name, "feature-name");
1335 assert_string_equal(mod->parsed->features[0].dsc, "desc");
1336 assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
1337 assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
1338 assert_string_equal(mod->parsed->features[0].ref, "ref");
1339 /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
1340 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
1341 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
1342
1343 /* min subelems */
1344 TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
1345 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
1346 assert_string_equal(mod->parsed->features[0].name, "feature-name");
1347
1348 /* invalid substatement */
1349 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
1350 "Unexpected sub-element \"organization\" of \"feature\" element.", "Line number 1.");
1351
1352 /*
1353 * compiling
1354 */
1355
1356 TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
1357 "feature orfeature {if-feature \"f1 or f2\";}\n"
1358 "feature andfeature {if-feature \"f1 and f2\";}\n"
1359 "feature f6 {if-feature \"not f1\";}\n"
1360 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
1361 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
1362 "feature f9 {if-feature \"not not f1\";}", mod);
1363 assert_non_null(mod->parsed->features);
1364 assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
1365
1366 /* all features are disabled by default */
1367 LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
1368 assert_false(f->flags & LYS_FENABLED);
1369 }
1370
1371 /* some invalid expressions */
1372 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}",
1373 "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL);
1374 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
1375 "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL);
1376 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}",
1377 "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL);
1378 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
1379 "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL);
1380 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
1381 "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL);
1382 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
1383 "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL);
1384 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
1385 "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL);
1386
1387 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
1388 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
1389 "Feature \"f1\" is indirectly referenced from itself.", NULL);
1390 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}",
1391 "Feature \"f1\" is referenced from itself.", NULL);
1392 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}",
1393 "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL);
1394 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
1395 "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL);
1396 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
1397 "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL);
1398 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
1399 "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL);
1400 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
1401 "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL);
1402 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
1403 "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL);
1404
1405 /*
1406 * printing
1407 */
1408
1409 /*
1410 * cleanup
1411 */
1412}
1413
1414static void
1415test_extension_argument(void **state)
1416{
1417 struct lys_module *mod;
1418 const char *mod_def_yang = "module a {\n"
1419 " namespace \"urn:a\";\n"
1420 " prefix a;\n\n"
1421 " extension e {\n"
1422 " argument name;\n"
1423 " }\n\n"
1424 " a:e \"aaa\";\n"
1425 "}\n";
1426 const char *mod_def_yin =
1427 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1428 "<module name=\"a\"\n"
1429 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1430 " xmlns:a=\"urn:a\">\n"
1431 " <namespace uri=\"urn:a\"/>\n"
1432 " <prefix value=\"a\"/>\n"
1433 " <extension name=\"e\">\n"
1434 " <argument name=\"name\"/>\n"
1435 " </extension>\n"
1436 " <a:e name=\"aaa\"/>\n"
1437 "</module>\n";
1438 const char *mod_test_yin, *mod_test_yang;
1439 char *printed;
1440
1441 mod_test_yang = "module b {\n"
1442 " namespace \"urn:b\";\n"
1443 " prefix b;\n\n"
1444 " import a {\n"
1445 " prefix a;\n"
1446 " }\n\n"
1447 " a:e \"xxx\";\n"
1448 "}\n";
1449 mod_test_yin =
1450 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1451 "<module name=\"b\"\n"
1452 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1453 " xmlns:b=\"urn:b\"\n"
1454 " xmlns:a=\"urn:a\">\n"
1455 " <namespace uri=\"urn:b\"/>\n"
1456 " <prefix value=\"b\"/>\n"
1457 " <import module=\"a\">\n"
1458 " <prefix value=\"a\"/>\n"
1459 " </import>\n"
1460 " <a:e name=\"xxx\"/>\n"
1461 "</module>\n";
1462
1463 /* from YANG */
1464 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
1465 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
1466 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1467 assert_string_equal(printed, mod_test_yang);
1468 free(printed);
1469
1470 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1471 assert_string_equal(printed, mod_test_yin);
1472 free(printed);
1473
1474 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1475 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1476 assert_string_equal(printed, mod_def_yang);
1477 free(printed);
1478
1479 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1480 assert_string_equal(printed, mod_def_yin);
1481 free(printed);
1482
1483 /* context reset */
1484 ly_ctx_destroy(UTEST_LYCTX);
1485 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1486
1487 /* from YIN */
1488 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
1489 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
1490 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1491 assert_string_equal(printed, mod_test_yang);
1492 free(printed);
1493
1494 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1495 assert_string_equal(printed, mod_test_yin);
1496 free(printed);
1497
1498 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1499 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1500 assert_string_equal(printed, mod_def_yang);
1501 free(printed);
1502
1503 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1504 assert_string_equal(printed, mod_def_yin);
1505 free(printed);
1506}
1507
1508static void
1509test_extension_argument_element(void **state)
1510{
1511 struct lys_module *mod;
1512 const char *mod_def_yang = "module a {\n"
1513 " namespace \"urn:a\";\n"
1514 " prefix a;\n\n"
1515 " extension e {\n"
1516 " argument name {\n"
1517 " yin-element true;\n"
1518 " }\n"
1519 " }\n\n"
1520 " a:e \"aaa\";\n"
1521 "}\n";
1522 const char *mod_def_yin =
1523 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1524 "<module name=\"a\"\n"
1525 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1526 " xmlns:a=\"urn:a\">\n"
1527 " <namespace uri=\"urn:a\"/>\n"
1528 " <prefix value=\"a\"/>\n"
1529 " <extension name=\"e\">\n"
1530 " <argument name=\"name\">\n"
1531 " <yin-element value=\"true\"/>\n"
1532 " </argument>\n"
1533 " </extension>\n"
1534 " <a:e>\n"
1535 " <a:name>aaa</a:name>\n"
1536 " </a:e>\n"
1537 "</module>\n";
1538 const char *mod_test_yin, *mod_test_yang;
1539 char *printed;
1540
1541 mod_test_yang = "module b {\n"
1542 " namespace \"urn:b\";\n"
1543 " prefix b;\n\n"
1544 " import a {\n"
1545 " prefix a;\n"
1546 " }\n\n"
1547 " a:e \"xxx\";\n"
1548 "}\n";
1549 mod_test_yin =
1550 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1551 "<module name=\"b\"\n"
1552 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1553 " xmlns:b=\"urn:b\"\n"
1554 " xmlns:a=\"urn:a\">\n"
1555 " <namespace uri=\"urn:b\"/>\n"
1556 " <prefix value=\"b\"/>\n"
1557 " <import module=\"a\">\n"
1558 " <prefix value=\"a\"/>\n"
1559 " </import>\n"
1560 " <a:e>\n"
1561 " <a:name>xxx</a:name>\n"
1562 " </a:e>\n"
1563 "</module>\n";
1564
1565 /* from YANG */
1566 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
1567 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
1568 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1569 assert_string_equal(printed, mod_test_yang);
1570 free(printed);
1571
1572 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1573 assert_string_equal(printed, mod_test_yin);
1574 free(printed);
1575
1576 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1577 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1578 assert_string_equal(printed, mod_def_yang);
1579 free(printed);
1580
1581 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1582 assert_string_equal(printed, mod_def_yin);
1583 free(printed);
1584
1585 /* context reset */
1586 ly_ctx_destroy(UTEST_LYCTX);
1587 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1588
1589 /* from YIN */
1590 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
1591 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
1592 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1593 assert_string_equal(printed, mod_test_yang);
1594 free(printed);
1595
1596 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1597 assert_string_equal(printed, mod_test_yin);
1598 free(printed);
1599
1600 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1601 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1602 assert_string_equal(printed, mod_def_yang);
1603 free(printed);
1604
1605 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1606 assert_string_equal(printed, mod_def_yin);
1607 free(printed);
1608
1609 /* invalid */
1610 mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }";
1611 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001612 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1613 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001614
1615 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1616 "<module name=\"x\"\n"
1617 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1618 " xmlns:x=\"urn:x\"\n"
1619 " xmlns:a=\"urn:a\">\n"
1620 " <namespace uri=\"urn:x\"/>\n"
1621 " <prefix value=\"x\"/>\n"
1622 " <import module=\"a\">\n"
1623 " <prefix value=\"a\"/>\n"
1624 " </import>\n\n"
1625 " <a:e/>\n"
1626 "</module>\n";
1627 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001628 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1629 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001630
1631 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1632 "<module name=\"x\"\n"
1633 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1634 " xmlns:x=\"urn:x\"\n"
1635 " xmlns:a=\"urn:a\">\n"
1636 " <namespace uri=\"urn:x\"/>\n"
1637 " <prefix value=\"x\"/>\n"
1638 " <import module=\"a\">\n"
1639 " <prefix value=\"a\"/>\n"
1640 " </import>\n\n"
1641 " <a:e name=\"xxx\"/>\n"
1642 "</module>\n";
1643 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001644 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1645 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001646
1647 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1648 "<module name=\"x\"\n"
1649 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1650 " xmlns:x=\"urn:x\"\n"
1651 " xmlns:a=\"urn:a\">\n"
1652 " <namespace uri=\"urn:x\"/>\n"
1653 " <prefix value=\"x\"/>\n"
1654 " <import module=\"a\">\n"
1655 " <prefix value=\"a\"/>\n"
1656 " </import>\n\n"
1657 " <a:e>\n"
1658 " <x:name>xxx</x:name>\n"
1659 " </a:e>\n"
1660 "</module>\n";
1661 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001662 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1663 CHECK_LOG_CTX("Extension instance \"a:e\" element and its argument element \"name\" are expected in the same namespace, but they differ.",
Michal Vasko193dacd2022-10-13 08:43:05 +02001664 NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001665
1666 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1667 "<module name=\"x\"\n"
1668 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1669 " xmlns:x=\"urn:x\"\n"
1670 " xmlns:a=\"urn:a\">\n"
1671 " <namespace uri=\"urn:x\"/>\n"
1672 " <prefix value=\"x\"/>\n"
1673 " <import module=\"a\">\n"
1674 " <prefix value=\"a\"/>\n"
1675 " </import>\n\n"
1676 " <a:e>\n"
1677 " <a:value>xxx</a:value>\n"
1678 " </a:e>\n"
1679 "</module>\n";
1680 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001681 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1682 CHECK_LOG_CTX("Extension instance \"a:e\" expects argument element \"name\" as its first XML child, but \"value\" element found.",
Michal Vasko193dacd2022-10-13 08:43:05 +02001683 NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001684
1685}
1686
1687static void
1688test_extension_compile(void **state)
1689{
1690 struct lys_module *mod;
1691 struct lysc_ctx cctx = {0};
1692 struct lysp_ext_instance ext_p = {0};
Michal Vasko193dacd2022-10-13 08:43:05 +02001693 struct lysp_ext_substmt *substmtp;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001694 struct lysp_stmt child = {0};
1695 struct lysc_ext_instance ext_c = {0};
1696 struct lysc_ext_substmt *substmt;
1697 LY_ERR rc = LY_SUCCESS;
1698
1699 /* current module, whatever */
1700 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
1701 assert_true(mod);
1702
1703 /* compile context */
1704 cctx.ctx = UTEST_LYCTX;
1705 cctx.cur_mod = mod;
1706 cctx.pmod = mod->parsed;
1707 cctx.path_len = 1;
1708 cctx.path[0] = '/';
1709
1710 /* parsed ext instance */
1711 lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name);
1712 ext_p.format = LY_VALUE_JSON;
1713 ext_p.parent_stmt = LY_STMT_MODULE;
1714
Michal Vasko193dacd2022-10-13 08:43:05 +02001715 LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_p.substmts, substmtp, rc, cleanup);
1716
1717 substmtp->stmt = LY_STMT_ERROR_MESSAGE;
1718 substmtp->storage = &ext_p.parsed;
1719 /* fake parse */
1720 lydict_insert(UTEST_LYCTX, "my error", 0, (const char **)&ext_p.parsed);
1721
Michal Vaskoc636ea42022-09-16 10:20:31 +02001722 /* compiled ext instance */
1723 ext_c.parent_stmt = ext_p.parent_stmt;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001724 LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup);
1725
Michal Vasko193dacd2022-10-13 08:43:05 +02001726 substmt->stmt = LY_STMT_ERROR_MESSAGE;
1727 substmt->storage = &ext_c.compiled;
1728
Michal Vaskoc636ea42022-09-16 10:20:31 +02001729 /*
1730 * error-message
1731 */
1732 ext_p.child = &child;
1733 lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt);
1734 lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg);
1735 child.format = LY_VALUE_JSON;
1736 child.kw = LY_STMT_ERROR_MESSAGE;
1737
Michal Vaskoc636ea42022-09-16 10:20:31 +02001738 /* compile */
Michal Vasko193dacd2022-10-13 08:43:05 +02001739 assert_int_equal(LY_SUCCESS, lyplg_ext_compile_extension_instance(&cctx, &ext_p, &ext_c));
Michal Vaskoc636ea42022-09-16 10:20:31 +02001740
1741 /* check */
Michal Vasko193dacd2022-10-13 08:43:05 +02001742 assert_string_equal(ext_c.compiled, "my error");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001743
1744cleanup:
1745 lydict_remove(UTEST_LYCTX, ext_p.name);
1746 lydict_remove(UTEST_LYCTX, child.stmt);
1747 lydict_remove(UTEST_LYCTX, child.arg);
Michal Vasko193dacd2022-10-13 08:43:05 +02001748 LY_ARRAY_FREE(ext_p.substmts);
1749 lydict_remove(UTEST_LYCTX, ext_p.parsed);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001750 LY_ARRAY_FREE(ext_c.substmts);
Michal Vasko193dacd2022-10-13 08:43:05 +02001751 lydict_remove(UTEST_LYCTX, ext_c.compiled);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001752 if (rc) {
1753 fail();
1754 }
1755}
1756
1757static void
1758test_ext_recursive(void **state)
1759{
1760 const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin;
1761
1762 mod_imp_yang = "module b {\n"
1763 " namespace \"urn:b\";\n"
1764 " prefix b;\n\n"
1765 " extension use-in {\n"
1766 " argument name {\n"
1767 " b:arg-type {\n"
1768 " type string;\n"
1769 " }\n"
1770 " }\n"
1771 " b:use-in \"extension\";\n"
1772 " b:occurence \"*\";\n"
1773 " }\n"
1774 "\n"
1775 " extension substatement {\n"
1776 " argument name {\n"
1777 " b:arg-type {\n"
1778 " type string;\n"
1779 " }\n"
1780 " }\n"
1781 " b:use-in \"extension\";\n"
1782 " b:occurence \"*\";\n"
1783 " b:substatement \"b:occurence\";\n"
1784 " }\n"
1785 "\n"
1786 " extension arg-type {\n"
1787 " b:use-in \"argument\";\n"
1788 " b:substatement \"type\" {\n"
1789 " b:occurence \"1\";\n"
1790 " }\n"
1791 " b:substatement \"default\";\n"
1792 " }\n"
1793 "\n"
1794 " extension occurence {\n"
1795 " argument value {\n"
1796 " b:arg-type {\n"
1797 " type enumeration {\n"
1798 " enum \"?\";\n"
1799 " enum \"*\";\n"
1800 " enum \"+\";\n"
1801 " enum \"1\";\n"
1802 " }\n"
1803 " }\n"
1804 " }\n"
1805 " b:use-in \"extension\";\n"
1806 " }\n"
1807 "}\n";
1808
1809 mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1810 "<module name=\"b\"\n"
1811 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1812 " xmlns:b=\"urn:b\"\n"
1813 " xmlns:a=\"urn:a\">\n"
1814 " <namespace uri=\"urn:b\"/>\n"
1815 " <prefix value=\"b\"/>\n"
1816 " <import module=\"a\">\n"
1817 " <prefix value=\"a\"/>\n"
1818 " </import>\n\n"
1819 " <a:e name=\"xxx\"/>\n"
1820 "</module>\n";
1821
1822 mod_base_yang = "module a {\n"
1823 " namespace \"urn:a\";\n"
1824 " prefix a;\n\n"
1825 " import b {\n"
1826 " prefix b;\n"
1827 " }\n"
1828 "\n"
1829 " extension abstract {\n"
1830 " b:use-in \"identity\";\n"
1831 " }\n"
1832 "\n"
1833 " identity mount-id;\n"
1834 "\n"
1835 " identity yang-lib-id {\n"
1836 " base mount-id;\n"
1837 " a:abstract;\n"
1838 " }\n"
1839 "}\n";
1840
1841 mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1842 "<module name=\"a\"\n"
1843 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1844 " xmlns:a=\"urn:a\">\n"
1845 " <namespace uri=\"urn:a\"/>\n"
1846 " <prefix value=\"a\"/>\n\n"
1847 " <extension name=\"e\">\n"
1848 " <argument name=\"name\"/>\n"
1849 " </extension>\n\n"
1850 " <a:e name=\"aaa\"/>\n"
1851 "</module>\n";
1852
1853 /* from YANG */
1854 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang);
1855 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL));
1856
1857 /* context reset */
1858 ly_ctx_destroy(UTEST_LYCTX);
1859 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1860
1861 /* from YIN */
1862 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin);
1863 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL));
1864}
Radek Krejci85ac8312021-03-03 20:21:33 +01001865
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001866int
1867main(void)
Radek Krejci3a4889a2020-05-19 17:01:58 +02001868{
1869 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001870 UTEST(test_getnext),
1871 UTEST(test_date),
1872 UTEST(test_revisions),
aPiecek4f49a142021-06-29 15:32:39 +02001873 UTEST(test_collision_typedef),
aPiecek0b365612021-06-30 13:12:58 +02001874 UTEST(test_collision_grouping),
aPiecek14d07f02021-06-30 09:46:50 +02001875 UTEST(test_collision_identity),
1876 UTEST(test_collision_feature),
Radek Iša56ca9e42020-09-08 18:42:00 +02001877 UTEST(test_accessible_tree),
Radek Krejci589c5472021-01-20 10:29:06 +01001878 UTEST(test_includes),
Michal Vaskoac4450e2021-11-09 13:53:40 +01001879 UTEST(test_key_order),
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001880 UTEST(test_disabled_enum),
Radek Iša56ca9e42020-09-08 18:42:00 +02001881 UTEST(test_identity),
1882 UTEST(test_feature),
Radek Krejci85ac8312021-03-03 20:21:33 +01001883 UTEST(test_extension_argument),
1884 UTEST(test_extension_argument_element),
Michal Vasko633ae8a2022-08-25 09:52:02 +02001885 UTEST(test_extension_compile),
Michal Vasko002d4032022-08-03 12:13:32 +02001886 UTEST(test_ext_recursive),
Radek Krejci3a4889a2020-05-19 17:01:58 +02001887 };
1888
1889 return cmocka_run_group_tests(tests, NULL, NULL);
1890}