blob: 17c4e4fff7dde76328d55b9e7cf00ee92d61fda8 [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[] = {
Michal Vasko84bf6f42023-05-19 11:09:48 +02001089 {
1090 "a", "module a {"
Michal Vaskoc636ea42022-09-16 10:20:31 +02001091 "yang-version 1.1;"
1092 "namespace urn:test:a;"
1093 "prefix a;"
1094 "list l {"
1095 " key \"k1 k2\";"
1096 " leaf k2 {type string;}"
1097 " leaf k1 {type string;}"
1098 "}"
Michal Vasko84bf6f42023-05-19 11:09:48 +02001099 "}"
1100 },
Michal Vaskoc636ea42022-09-16 10:20:31 +02001101 {NULL, NULL}
1102 };
1103
1104 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list1);
1105 mod = ly_ctx_load_module(UTEST_LYCTX, "a", NULL, NULL);
1106 assert_non_null(mod);
1107
1108 node = lysc_node_child(mod->compiled->data);
1109 assert_string_equal("k1", node->name);
1110 node = node->next;
1111 assert_string_equal("k2", node->name);
1112
1113 struct module_clb_list list2[] = {
Michal Vasko84bf6f42023-05-19 11:09:48 +02001114 {
1115 "b", "module b {"
Michal Vaskoc636ea42022-09-16 10:20:31 +02001116 "yang-version 1.1;"
1117 "namespace urn:test:b;"
1118 "prefix b;"
1119 "list l {"
1120 " key \"k1 k2 k3 k4\";"
1121 " leaf k4 {type string;}"
1122 " container c {"
1123 " leaf l1 {type string;}"
1124 " }"
1125 " leaf k2 {type string;}"
1126 " leaf l2 {type string;}"
1127 " leaf k1 {type string;}"
1128 " leaf k3 {type string;}"
1129 "}"
Michal Vasko84bf6f42023-05-19 11:09:48 +02001130 "}"
1131 },
Michal Vaskoc636ea42022-09-16 10:20:31 +02001132 {NULL, NULL}
1133 };
1134
1135 ly_ctx_set_module_imp_clb(UTEST_LYCTX, module_clb, list2);
1136 mod = ly_ctx_load_module(UTEST_LYCTX, "b", NULL, NULL);
1137 assert_non_null(mod);
1138
1139 node = lysc_node_child(mod->compiled->data);
1140 assert_string_equal("k1", node->name);
1141 node = node->next;
1142 assert_string_equal("k2", node->name);
1143 node = node->next;
1144 assert_string_equal("k3", node->name);
1145 node = node->next;
1146 assert_string_equal("k4", node->name);
1147}
1148
1149static void
1150test_disabled_enum(void **state)
1151{
1152 const char *str;
1153
1154 /* no enabled enum */
1155 str = "module a {"
1156 "yang-version 1.1;"
1157 "namespace urn:test:a;"
1158 "prefix a;"
1159 "feature f;"
1160 "leaf l {type enumeration {"
1161 " enum e1 {if-feature f;}"
1162 " enum e2 {if-feature f;}"
1163 "}}"
1164 "}";
1165 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko28864d52023-03-23 08:07:46 +01001166 CHECK_LOG_CTX("Node \"l\" without any (or all disabled) valid values.", "Schema location \"/a:l\".");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001167
1168 /* disabled default value */
1169 str = "module a {"
1170 "yang-version 1.1;"
1171 "namespace urn:test:a;"
1172 "prefix a;"
1173 "feature f;"
1174 "leaf l {"
1175 " type enumeration {"
1176 " enum e1 {if-feature f;}"
1177 " enum e2;"
1178 " }"
1179 " default e1;"
1180 "}"
1181 "}";
1182 assert_int_equal(lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL), LY_EVALID);
1183 CHECK_LOG_CTX("Invalid default - value does not fit the type (Invalid enumeration value \"e1\".).",
1184 "Schema location \"/a:l\".");
1185}
1186
1187static void
1188test_identity(void **state)
1189{
1190 struct lys_module *mod, *mod_imp;
1191
1192 /*
1193 * parsing YANG
1194 */
1195 TEST_STMT_DUP(1, 0, "identity id", "description", "a", "b", "1");
1196 TEST_STMT_DUP(1, 0, "identity id", "reference", "a", "b", "1");
1197 TEST_STMT_DUP(1, 0, "identity id", "status", "current", "obsolete", "1");
1198
1199 /* full content */
1200 TEST_SCHEMA_OK(1, 0, "identityone",
1201 "identity test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y; identityone:ext;}"
1202 "identity a; identity b; extension ext; feature x; feature y;", mod);
1203 assert_non_null(mod->parsed->identities);
1204 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->identities));
1205
1206 /* invalid substatement */
1207 TEST_STMT_SUBSTM_ERR(0, "identity", "organization", "XXX");
1208
1209 /*
1210 * parsing YIN
1211 */
1212 /* max subelems */
1213 TEST_SCHEMA_OK(1, 1, "identityone-yin", "<identity name=\"ident-name\">"
1214 "<if-feature name=\"iff\"/>"
1215 "<base name=\"base-name\"/>"
1216 "<status value=\"deprecated\"/>"
1217 "<description><text>desc</text></description>"
1218 "<reference><text>ref</text></reference>"
1219 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:identityone-yin\"/>" */
1220 "</identity><extension name=\"ext\"/><identity name=\"base-name\"/><feature name=\"iff\"/>", mod);
1221 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->identities));
1222 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
1223 assert_string_equal(mod->parsed->identities[0].bases[0], "base-name");
1224 assert_string_equal(mod->parsed->identities[0].iffeatures[0].str, "iff");
1225 assert_string_equal(mod->parsed->identities[0].dsc, "desc");
1226 assert_string_equal(mod->parsed->identities[0].ref, "ref");
1227 assert_true(mod->parsed->identities[0].flags & LYS_STATUS_DEPRC);
1228 /*assert_string_equal(mod->parsed->identities[0].exts[0].name, "ext");
1229 assert_non_null(mod->parsed->identities[0].exts[0].compiled);
1230 assert_int_equal(mod->parsed->identities[0].exts[0].yin, 1);
1231 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt_index, 0);
1232 assert_int_equal(mod->parsed->identities[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
1233
1234 /* min subelems */
1235 TEST_SCHEMA_OK(1, 1, "identitytwo-yin", "<identity name=\"ident-name\" />", mod);
1236 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->identities));
1237 assert_string_equal(mod->parsed->identities[0].name, "ident-name");
1238
1239 /* invalid substatement */
1240 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<identity name=\"ident-name\"><if-feature name=\"iff\"/></identity>",
1241 "Invalid sub-elemnt \"if-feature\" of \"identity\" element - "
1242 "this sub-element is allowed only in modules with version 1.1 or newer.", "Line number 1.");
1243
1244 /*
1245 * compiling
1246 */
1247 TEST_SCHEMA_OK(0, 0, "a", "identity a1;", mod_imp);
1248 TEST_SCHEMA_OK(1, 0, "b", "import a {prefix a;}"
1249 "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}"
1250 "identity b4 {base b:b1; base b3;}", mod);
1251 assert_non_null(mod_imp->compiled);
1252 assert_non_null(mod_imp->identities);
1253 assert_non_null(mod->identities);
1254 assert_non_null(mod_imp->identities[0].derived);
1255 assert_int_equal(1, LY_ARRAY_COUNT(mod_imp->identities[0].derived));
1256 assert_ptr_equal(mod_imp->identities[0].derived[0], &mod->identities[2]);
1257 assert_non_null(mod->identities[0].derived);
1258 assert_int_equal(2, LY_ARRAY_COUNT(mod->identities[0].derived));
1259 assert_ptr_equal(mod->identities[0].derived[0], &mod->identities[2]);
1260 assert_ptr_equal(mod->identities[0].derived[1], &mod->identities[3]);
1261 assert_non_null(mod->identities[1].derived);
1262 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
1263 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[2]);
1264 assert_non_null(mod->identities[2].derived);
1265 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[2].derived));
1266 assert_ptr_equal(mod->identities[2].derived[0], &mod->identities[3]);
1267
1268 TEST_SCHEMA_OK(1, 0, "c", "identity c2 {base c1;} identity c1;", mod);
1269 assert_int_equal(1, LY_ARRAY_COUNT(mod->identities[1].derived));
1270 assert_ptr_equal(mod->identities[1].derived[0], &mod->identities[0]);
1271
1272 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 +01001273 TEST_SCHEMA_ERR(0, 0, "inv", "identity i1 {base i2;}", "Unable to find base (i2) of identity \"i1\".", "Path \"/inv:{identity='i1'}\".");
1274 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 +02001275 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 +01001276 "Identity \"i1\" is indirectly derived from itself.", "Path \"/inv:{identity='i3'}\".");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001277
1278 /* base in non-implemented module */
1279 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
1280 "module base {namespace \"urn\"; prefix b; identity i1; identity i2 {base i1;}}");
1281 TEST_SCHEMA_OK(0, 0, "ident", "import base {prefix b;} identity ii {base b:i1;}", mod);
1282
1283 /* default value from non-implemented module */
1284 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} leaf l {type identityref {base b:i1;} default b:i2;}",
1285 "Invalid default - value does not fit the type (Invalid identityref \"b:i2\" value"
1286 " - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
1287
1288 /* default value in typedef from non-implemented module */
1289 TEST_SCHEMA_ERR(0, 0, "ident2", "import base {prefix b;} typedef t1 {type identityref {base b:i1;} default b:i2;}"
1290 "leaf l {type t1;}", "Invalid default - value does not fit the type (Invalid"
1291 " identityref \"b:i2\" value - identity found in non-implemented module \"base\".).", "Schema location \"/ident2:l\".");
1292
1293 /*
1294 * printing
1295 */
1296
1297 /*
1298 * cleanup
1299 */
1300}
1301
1302static void
1303test_feature(void **state)
1304{
1305 struct lys_module *mod;
1306 const struct lysp_feature *f;
1307
1308 /*
1309 * parsing YANG
1310 */
1311
1312 TEST_STMT_DUP(1, 0, "feature f", "description", "a", "b", "1");
1313 TEST_STMT_DUP(1, 0, "feature f", "reference", "a", "b", "1");
1314 TEST_STMT_DUP(1, 0, "feature f", "status", "current", "obsolete", "1");
1315
1316 /* full content */
1317 TEST_SCHEMA_OK(1, 0, "featureone",
1318 "feature test {description text;reference \'another text\';status current; if-feature x; if-feature y; featureone:ext;}"
1319 "extension ext; feature x; feature y;", mod);
1320 assert_non_null(mod->parsed->features);
1321 assert_int_equal(3, LY_ARRAY_COUNT(mod->parsed->features));
1322
1323 /* invalid substatement */
1324 TEST_STMT_SUBSTM_ERR(0, "feature", "organization", "XXX");
1325
1326 /*
1327 * parsing YIN
1328 */
1329 /* max subelems */
1330 TEST_SCHEMA_OK(0, 1, "featureone-yin", "<feature name=\"feature-name\">"
1331 "<if-feature name=\"iff\"/>"
1332 "<status value=\"deprecated\"/>"
1333 "<description><text>desc</text></description>"
1334 "<reference><text>ref</text></reference>"
1335 /* TODO yin-extension-prefix-compilation-bug "<myext:ext xmlns:myext=\"urn:libyang:test:featureone-yin\"/>" */
1336 "</feature><extension name=\"ext\"/><feature name=\"iff\"/>", mod);
1337 assert_int_equal(2, LY_ARRAY_COUNT(mod->parsed->features));
1338 assert_string_equal(mod->parsed->features[0].name, "feature-name");
1339 assert_string_equal(mod->parsed->features[0].dsc, "desc");
1340 assert_true(mod->parsed->features[0].flags & LYS_STATUS_DEPRC);
1341 assert_string_equal(mod->parsed->features[0].iffeatures[0].str, "iff");
1342 assert_string_equal(mod->parsed->features[0].ref, "ref");
1343 /*assert_string_equal(mod->parsed->features[0].exts[0].name, "ext");
1344 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt_index, 0);
1345 assert_int_equal(mod->parsed->features[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);*/
1346
1347 /* min subelems */
1348 TEST_SCHEMA_OK(0, 1, "featuretwo-yin", "<feature name=\"feature-name\"/>", mod)
1349 assert_int_equal(1, LY_ARRAY_COUNT(mod->parsed->features));
1350 assert_string_equal(mod->parsed->features[0].name, "feature-name");
1351
1352 /* invalid substatement */
1353 TEST_SCHEMA_PARSE_ERR(0, 1, "inv", "<feature name=\"feature-name\"><organization><text>org</text></organization></feature>",
1354 "Unexpected sub-element \"organization\" of \"feature\" element.", "Line number 1.");
1355
1356 /*
1357 * compiling
1358 */
1359
1360 TEST_SCHEMA_OK(1, 0, "a", "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n"
1361 "feature orfeature {if-feature \"f1 or f2\";}\n"
1362 "feature andfeature {if-feature \"f1 and f2\";}\n"
1363 "feature f6 {if-feature \"not f1\";}\n"
1364 "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n"
1365 "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n"
1366 "feature f9 {if-feature \"not not f1\";}", mod);
1367 assert_non_null(mod->parsed->features);
1368 assert_int_equal(9, LY_ARRAY_COUNT(mod->parsed->features));
1369
1370 /* all features are disabled by default */
1371 LY_ARRAY_FOR(mod->parsed->features, struct lysp_feature, f) {
1372 assert_false(f->flags & LYS_FENABLED);
1373 }
1374
1375 /* some invalid expressions */
1376 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature f1;}",
1377 "Invalid value \"f1\" of if-feature - unable to find feature \"f1\".", NULL);
1378 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f and';}",
1379 "Invalid value \"f and\" of if-feature - unexpected end of expression.", NULL);
1380 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f{if-feature 'or';}",
1381 "Invalid value \"or\" of if-feature - unexpected end of expression.", NULL);
1382 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature '(f1';}",
1383 "Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses.", NULL);
1384 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature 'f1)';}",
1385 "Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses.", NULL);
1386 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2{if-feature ---;}",
1387 "Invalid value \"---\" of if-feature - unable to find feature \"---\".", NULL);
1388 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1; feature f2{if-feature 'not f1';}",
1389 "Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", NULL);
1390
1391 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule inv_sub {belongs-to inv {prefix inv;} feature f1;}");
1392 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f2;} feature f2 {if-feature f1;}",
1393 "Feature \"f1\" is indirectly referenced from itself.", NULL);
1394 TEST_SCHEMA_PARSE_ERR(0, 0, "inv", "feature f1 {if-feature f1;}",
1395 "Feature \"f1\" is referenced from itself.", NULL);
1396 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f {if-feature ();}",
1397 "Invalid value \"()\" of if-feature - number of features in expression does not match the required number of operands for the operations.", NULL);
1398 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1(';}",
1399 "Invalid value \"f1(\" of if-feature - non-matching opening and closing parentheses.", NULL);
1400 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'and f1';}",
1401 "Invalid value \"and f1\" of if-feature - missing feature/expression before \"and\" operation.", NULL);
1402 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not ';}",
1403 "Invalid value \"f1 not \" of if-feature - unexpected end of expression.", NULL);
1404 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f {if-feature 'f1 not not ';}",
1405 "Invalid value \"f1 not not \" of if-feature - unexpected end of expression.", NULL);
1406 TEST_SCHEMA_PARSE_ERR(1, 0, "inv", "feature f1; feature f2; feature f {if-feature 'or f1 f2';}",
1407 "Invalid value \"or f1 f2\" of if-feature - missing feature/expression before \"or\" operation.", NULL);
1408
1409 /*
1410 * printing
1411 */
1412
1413 /*
1414 * cleanup
1415 */
1416}
1417
1418static void
1419test_extension_argument(void **state)
1420{
1421 struct lys_module *mod;
1422 const char *mod_def_yang = "module a {\n"
1423 " namespace \"urn:a\";\n"
1424 " prefix a;\n\n"
1425 " extension e {\n"
1426 " argument name;\n"
1427 " }\n\n"
1428 " a:e \"aaa\";\n"
1429 "}\n";
1430 const char *mod_def_yin =
1431 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1432 "<module name=\"a\"\n"
1433 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1434 " xmlns:a=\"urn:a\">\n"
1435 " <namespace uri=\"urn:a\"/>\n"
1436 " <prefix value=\"a\"/>\n"
1437 " <extension name=\"e\">\n"
1438 " <argument name=\"name\"/>\n"
1439 " </extension>\n"
1440 " <a:e name=\"aaa\"/>\n"
1441 "</module>\n";
1442 const char *mod_test_yin, *mod_test_yang;
1443 char *printed;
1444
1445 mod_test_yang = "module b {\n"
1446 " namespace \"urn:b\";\n"
1447 " prefix b;\n\n"
1448 " import a {\n"
1449 " prefix a;\n"
1450 " }\n\n"
1451 " a:e \"xxx\";\n"
1452 "}\n";
1453 mod_test_yin =
1454 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1455 "<module name=\"b\"\n"
1456 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1457 " xmlns:b=\"urn:b\"\n"
1458 " xmlns:a=\"urn:a\">\n"
1459 " <namespace uri=\"urn:b\"/>\n"
1460 " <prefix value=\"b\"/>\n"
1461 " <import module=\"a\">\n"
1462 " <prefix value=\"a\"/>\n"
1463 " </import>\n"
1464 " <a:e name=\"xxx\"/>\n"
1465 "</module>\n";
1466
1467 /* from YANG */
1468 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
1469 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
1470 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1471 assert_string_equal(printed, mod_test_yang);
1472 free(printed);
1473
1474 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1475 assert_string_equal(printed, mod_test_yin);
1476 free(printed);
1477
1478 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1479 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1480 assert_string_equal(printed, mod_def_yang);
1481 free(printed);
1482
1483 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1484 assert_string_equal(printed, mod_def_yin);
1485 free(printed);
1486
1487 /* context reset */
1488 ly_ctx_destroy(UTEST_LYCTX);
1489 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1490
1491 /* from YIN */
1492 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
1493 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
1494 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1495 assert_string_equal(printed, mod_test_yang);
1496 free(printed);
1497
1498 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1499 assert_string_equal(printed, mod_test_yin);
1500 free(printed);
1501
1502 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1503 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1504 assert_string_equal(printed, mod_def_yang);
1505 free(printed);
1506
1507 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1508 assert_string_equal(printed, mod_def_yin);
1509 free(printed);
1510}
1511
1512static void
1513test_extension_argument_element(void **state)
1514{
1515 struct lys_module *mod;
1516 const char *mod_def_yang = "module a {\n"
1517 " namespace \"urn:a\";\n"
1518 " prefix a;\n\n"
1519 " extension e {\n"
1520 " argument name {\n"
1521 " yin-element true;\n"
1522 " }\n"
1523 " }\n\n"
1524 " a:e \"aaa\";\n"
1525 "}\n";
1526 const char *mod_def_yin =
1527 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1528 "<module name=\"a\"\n"
1529 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1530 " xmlns:a=\"urn:a\">\n"
1531 " <namespace uri=\"urn:a\"/>\n"
1532 " <prefix value=\"a\"/>\n"
1533 " <extension name=\"e\">\n"
1534 " <argument name=\"name\">\n"
1535 " <yin-element value=\"true\"/>\n"
1536 " </argument>\n"
1537 " </extension>\n"
1538 " <a:e>\n"
1539 " <a:name>aaa</a:name>\n"
1540 " </a:e>\n"
1541 "</module>\n";
1542 const char *mod_test_yin, *mod_test_yang;
1543 char *printed;
1544
1545 mod_test_yang = "module b {\n"
1546 " namespace \"urn:b\";\n"
1547 " prefix b;\n\n"
1548 " import a {\n"
1549 " prefix a;\n"
1550 " }\n\n"
1551 " a:e \"xxx\";\n"
1552 "}\n";
1553 mod_test_yin =
1554 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1555 "<module name=\"b\"\n"
1556 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1557 " xmlns:b=\"urn:b\"\n"
1558 " xmlns:a=\"urn:a\">\n"
1559 " <namespace uri=\"urn:b\"/>\n"
1560 " <prefix value=\"b\"/>\n"
1561 " <import module=\"a\">\n"
1562 " <prefix value=\"a\"/>\n"
1563 " </import>\n"
1564 " <a:e>\n"
1565 " <a:name>xxx</a:name>\n"
1566 " </a:e>\n"
1567 "</module>\n";
1568
1569 /* from YANG */
1570 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yang);
1571 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, &mod));
1572 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1573 assert_string_equal(printed, mod_test_yang);
1574 free(printed);
1575
1576 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1577 assert_string_equal(printed, mod_test_yin);
1578 free(printed);
1579
1580 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1581 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1582 assert_string_equal(printed, mod_def_yang);
1583 free(printed);
1584
1585 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1586 assert_string_equal(printed, mod_def_yin);
1587 free(printed);
1588
1589 /* context reset */
1590 ly_ctx_destroy(UTEST_LYCTX);
1591 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1592
1593 /* from YIN */
1594 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_def_yin);
1595 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, &mod));
1596 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1597 assert_string_equal(printed, mod_test_yang);
1598 free(printed);
1599
1600 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1601 assert_string_equal(printed, mod_test_yin);
1602 free(printed);
1603
1604 assert_non_null(mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
1605 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
1606 assert_string_equal(printed, mod_def_yang);
1607 free(printed);
1608
1609 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
1610 assert_string_equal(printed, mod_def_yin);
1611 free(printed);
1612
1613 /* invalid */
1614 mod_test_yang = "module x { namespace \"urn:x\"; prefix x; import a { prefix a; } a:e; }";
1615 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yang, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001616 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1617 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001618
1619 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1620 "<module name=\"x\"\n"
1621 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1622 " xmlns:x=\"urn:x\"\n"
1623 " xmlns:a=\"urn:a\">\n"
1624 " <namespace uri=\"urn:x\"/>\n"
1625 " <prefix value=\"x\"/>\n"
1626 " <import module=\"a\">\n"
1627 " <prefix value=\"a\"/>\n"
1628 " </import>\n\n"
1629 " <a:e/>\n"
1630 "</module>\n";
1631 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001632 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1633 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001634
1635 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1636 "<module name=\"x\"\n"
1637 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1638 " xmlns:x=\"urn:x\"\n"
1639 " xmlns:a=\"urn:a\">\n"
1640 " <namespace uri=\"urn:x\"/>\n"
1641 " <prefix value=\"x\"/>\n"
1642 " <import module=\"a\">\n"
1643 " <prefix value=\"a\"/>\n"
1644 " </import>\n\n"
1645 " <a:e name=\"xxx\"/>\n"
1646 "</module>\n";
1647 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001648 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1649 CHECK_LOG_CTX("Extension instance \"a:e\" missing argument element \"name\".", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001650
1651 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1652 "<module name=\"x\"\n"
1653 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1654 " xmlns:x=\"urn:x\"\n"
1655 " xmlns:a=\"urn:a\">\n"
1656 " <namespace uri=\"urn:x\"/>\n"
1657 " <prefix value=\"x\"/>\n"
1658 " <import module=\"a\">\n"
1659 " <prefix value=\"a\"/>\n"
1660 " </import>\n\n"
1661 " <a:e>\n"
1662 " <x:name>xxx</x:name>\n"
1663 " </a:e>\n"
1664 "</module>\n";
1665 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001666 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1667 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 +02001668 NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001669
1670 mod_test_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1671 "<module name=\"x\"\n"
1672 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1673 " xmlns:x=\"urn:x\"\n"
1674 " xmlns:a=\"urn:a\">\n"
1675 " <namespace uri=\"urn:x\"/>\n"
1676 " <prefix value=\"x\"/>\n"
1677 " <import module=\"a\">\n"
1678 " <prefix value=\"a\"/>\n"
1679 " </import>\n\n"
1680 " <a:e>\n"
1681 " <a:value>xxx</a:value>\n"
1682 " </a:e>\n"
1683 "</module>\n";
1684 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, mod_test_yin, LYS_IN_YIN, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +01001685 CHECK_LOG_CTX("Parsing module \"x\" failed.", NULL);
1686 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 +02001687 NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001688
1689}
1690
1691static void
1692test_extension_compile(void **state)
1693{
1694 struct lys_module *mod;
1695 struct lysc_ctx cctx = {0};
1696 struct lysp_ext_instance ext_p = {0};
Michal Vasko193dacd2022-10-13 08:43:05 +02001697 struct lysp_ext_substmt *substmtp;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001698 struct lysp_stmt child = {0};
1699 struct lysc_ext_instance ext_c = {0};
1700 struct lysc_ext_substmt *substmt;
1701 LY_ERR rc = LY_SUCCESS;
1702
1703 /* current module, whatever */
1704 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
1705 assert_true(mod);
1706
1707 /* compile context */
1708 cctx.ctx = UTEST_LYCTX;
1709 cctx.cur_mod = mod;
1710 cctx.pmod = mod->parsed;
1711 cctx.path_len = 1;
1712 cctx.path[0] = '/';
1713
1714 /* parsed ext instance */
1715 lydict_insert(UTEST_LYCTX, "pref:my-ext", 0, &ext_p.name);
1716 ext_p.format = LY_VALUE_JSON;
1717 ext_p.parent_stmt = LY_STMT_MODULE;
1718
Michal Vasko193dacd2022-10-13 08:43:05 +02001719 LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_p.substmts, substmtp, rc, cleanup);
1720
1721 substmtp->stmt = LY_STMT_ERROR_MESSAGE;
1722 substmtp->storage = &ext_p.parsed;
1723 /* fake parse */
1724 lydict_insert(UTEST_LYCTX, "my error", 0, (const char **)&ext_p.parsed);
1725
Michal Vaskoc636ea42022-09-16 10:20:31 +02001726 /* compiled ext instance */
1727 ext_c.parent_stmt = ext_p.parent_stmt;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001728 LY_ARRAY_NEW_GOTO(UTEST_LYCTX, ext_c.substmts, substmt, rc, cleanup);
1729
Michal Vasko193dacd2022-10-13 08:43:05 +02001730 substmt->stmt = LY_STMT_ERROR_MESSAGE;
1731 substmt->storage = &ext_c.compiled;
1732
Michal Vaskoc636ea42022-09-16 10:20:31 +02001733 /*
1734 * error-message
1735 */
1736 ext_p.child = &child;
1737 lydict_insert(UTEST_LYCTX, "error-message", 0, &child.stmt);
1738 lydict_insert(UTEST_LYCTX, "my error", 0, &child.arg);
1739 child.format = LY_VALUE_JSON;
1740 child.kw = LY_STMT_ERROR_MESSAGE;
1741
Michal Vaskoc636ea42022-09-16 10:20:31 +02001742 /* compile */
Michal Vasko193dacd2022-10-13 08:43:05 +02001743 assert_int_equal(LY_SUCCESS, lyplg_ext_compile_extension_instance(&cctx, &ext_p, &ext_c));
Michal Vaskoc636ea42022-09-16 10:20:31 +02001744
1745 /* check */
Michal Vasko193dacd2022-10-13 08:43:05 +02001746 assert_string_equal(ext_c.compiled, "my error");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001747
1748cleanup:
1749 lydict_remove(UTEST_LYCTX, ext_p.name);
1750 lydict_remove(UTEST_LYCTX, child.stmt);
1751 lydict_remove(UTEST_LYCTX, child.arg);
Michal Vasko193dacd2022-10-13 08:43:05 +02001752 LY_ARRAY_FREE(ext_p.substmts);
1753 lydict_remove(UTEST_LYCTX, ext_p.parsed);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001754 LY_ARRAY_FREE(ext_c.substmts);
Michal Vasko193dacd2022-10-13 08:43:05 +02001755 lydict_remove(UTEST_LYCTX, ext_c.compiled);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001756 if (rc) {
1757 fail();
1758 }
1759}
1760
1761static void
1762test_ext_recursive(void **state)
1763{
1764 const char *mod_base_yang, *mod_imp_yang, *mod_base_yin, *mod_imp_yin;
1765
1766 mod_imp_yang = "module b {\n"
1767 " namespace \"urn:b\";\n"
1768 " prefix b;\n\n"
1769 " extension use-in {\n"
1770 " argument name {\n"
1771 " b:arg-type {\n"
1772 " type string;\n"
1773 " }\n"
1774 " }\n"
1775 " b:use-in \"extension\";\n"
1776 " b:occurence \"*\";\n"
1777 " }\n"
1778 "\n"
1779 " extension substatement {\n"
1780 " argument name {\n"
1781 " b:arg-type {\n"
1782 " type string;\n"
1783 " }\n"
1784 " }\n"
1785 " b:use-in \"extension\";\n"
1786 " b:occurence \"*\";\n"
1787 " b:substatement \"b:occurence\";\n"
1788 " }\n"
1789 "\n"
1790 " extension arg-type {\n"
1791 " b:use-in \"argument\";\n"
1792 " b:substatement \"type\" {\n"
1793 " b:occurence \"1\";\n"
1794 " }\n"
1795 " b:substatement \"default\";\n"
1796 " }\n"
1797 "\n"
1798 " extension occurence {\n"
1799 " argument value {\n"
1800 " b:arg-type {\n"
1801 " type enumeration {\n"
1802 " enum \"?\";\n"
1803 " enum \"*\";\n"
1804 " enum \"+\";\n"
1805 " enum \"1\";\n"
1806 " }\n"
1807 " }\n"
1808 " }\n"
1809 " b:use-in \"extension\";\n"
1810 " }\n"
1811 "}\n";
1812
1813 mod_imp_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1814 "<module name=\"b\"\n"
1815 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1816 " xmlns:b=\"urn:b\"\n"
1817 " xmlns:a=\"urn:a\">\n"
1818 " <namespace uri=\"urn:b\"/>\n"
1819 " <prefix value=\"b\"/>\n"
1820 " <import module=\"a\">\n"
1821 " <prefix value=\"a\"/>\n"
1822 " </import>\n\n"
1823 " <a:e name=\"xxx\"/>\n"
1824 "</module>\n";
1825
1826 mod_base_yang = "module a {\n"
1827 " namespace \"urn:a\";\n"
1828 " prefix a;\n\n"
1829 " import b {\n"
1830 " prefix b;\n"
1831 " }\n"
1832 "\n"
1833 " extension abstract {\n"
1834 " b:use-in \"identity\";\n"
1835 " }\n"
1836 "\n"
1837 " identity mount-id;\n"
1838 "\n"
1839 " identity yang-lib-id {\n"
1840 " base mount-id;\n"
1841 " a:abstract;\n"
1842 " }\n"
1843 "}\n";
1844
1845 mod_base_yin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1846 "<module name=\"a\"\n"
1847 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1848 " xmlns:a=\"urn:a\">\n"
1849 " <namespace uri=\"urn:a\"/>\n"
1850 " <prefix value=\"a\"/>\n\n"
1851 " <extension name=\"e\">\n"
1852 " <argument name=\"name\"/>\n"
1853 " </extension>\n\n"
1854 " <a:e name=\"aaa\"/>\n"
1855 "</module>\n";
1856
1857 /* from YANG */
1858 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yang);
1859 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yang, LYS_IN_YANG, NULL));
1860
1861 /* context reset */
1862 ly_ctx_destroy(UTEST_LYCTX);
1863 ly_ctx_new(NULL, 0, &UTEST_LYCTX);
1864
1865 /* from YIN */
1866 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)mod_imp_yin);
1867 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, mod_base_yin, LYS_IN_YIN, NULL));
1868}
Radek Krejci85ac8312021-03-03 20:21:33 +01001869
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001870int
1871main(void)
Radek Krejci3a4889a2020-05-19 17:01:58 +02001872{
1873 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001874 UTEST(test_getnext),
1875 UTEST(test_date),
1876 UTEST(test_revisions),
aPiecek4f49a142021-06-29 15:32:39 +02001877 UTEST(test_collision_typedef),
aPiecek0b365612021-06-30 13:12:58 +02001878 UTEST(test_collision_grouping),
aPiecek14d07f02021-06-30 09:46:50 +02001879 UTEST(test_collision_identity),
1880 UTEST(test_collision_feature),
Radek Iša56ca9e42020-09-08 18:42:00 +02001881 UTEST(test_accessible_tree),
Radek Krejci589c5472021-01-20 10:29:06 +01001882 UTEST(test_includes),
Michal Vaskoac4450e2021-11-09 13:53:40 +01001883 UTEST(test_key_order),
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001884 UTEST(test_disabled_enum),
Radek Iša56ca9e42020-09-08 18:42:00 +02001885 UTEST(test_identity),
1886 UTEST(test_feature),
Radek Krejci85ac8312021-03-03 20:21:33 +01001887 UTEST(test_extension_argument),
1888 UTEST(test_extension_argument_element),
Michal Vasko633ae8a2022-08-25 09:52:02 +02001889 UTEST(test_extension_compile),
Michal Vasko002d4032022-08-03 12:13:32 +02001890 UTEST(test_ext_recursive),
Radek Krejci3a4889a2020-05-19 17:01:58 +02001891 };
1892
1893 return cmocka_run_group_tests(tests, NULL, NULL);
1894}