blob: 50fc8d75574f75a5ac1641e86be66d869ed933eb [file] [log] [blame]
Radek Iša56ca9e42020-09-08 18:42:00 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_context.c
Radek Iša56ca9e42020-09-08 18:42:00 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from context.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17#include "common.h"
18#include "context.h"
19#include "in.h"
20#include "schema_compile.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020021#include "tests_config.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020022#include "tree_schema_internal.h"
23#include "utests.h"
24
25static void
26test_searchdirs(void **state)
27{
28 const char * const *list;
29
30 /* invalid arguments */
31 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
32 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
33 assert_null(ly_ctx_get_searchdirs(NULL));
34 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
35 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
36 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
37
38 /* readable and executable, but not a directory */
39 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context"));
40 CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL);
41 /* not executable */
42 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, __FILE__));
43 CHECK_LOG_CTX("Unable to fully access search directory \""__FILE__ "\" (Permission denied).", NULL);
44 /* not existing */
45 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile"));
46 CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
47
48 /* ly_set_add() fails */
49 /* no change */
50 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL));
51
52 /* correct path */
53 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
54 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
55 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
56
57 /* duplicated paths */
58 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
59 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
60 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
61
62 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
63 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles"));
64 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src"));
65 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules"));
66 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc"));
67 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC));
68 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN));
69 assert_int_equal(7, UTEST_LYCTX->search_paths.count);
70
71 /* get searchpaths */
72 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
73 assert_non_null(list);
74 assert_string_equal(TESTS_BIN "/utests", list[0]);
75 assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]);
76 assert_string_equal(TESTS_SRC, list[5]);
77 assert_string_equal(TESTS_BIN, list[6]);
78 assert_null(list[7]);
79
80 /* removing searchpaths */
81 /* nonexisting */
82 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
83 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
84 /* first */
85 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
86 assert_string_not_equal(TESTS_BIN "/utests", list[0]);
87 assert_int_equal(6, UTEST_LYCTX->search_paths.count);
88 /* middle */
89 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC));
90 assert_int_equal(5, UTEST_LYCTX->search_paths.count);
91 /* last */
92 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN));
93 assert_int_equal(4, UTEST_LYCTX->search_paths.count);
94 /* all */
95 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
96 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
97
98 /* again - no change */
99 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
100
101 /* cleanup */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200102 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200103
104 /* test searchdir list in ly_ctx_new() */
105 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX));
106 CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
107 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC ":"TESTS_BIN ":"TESTS_BIN ":"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
108 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
109 assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]);
110 assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]);
111}
112
113static void
114test_options(void **state)
115{
116 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200117 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200118
119 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX));
120
121 /* invalid arguments */
122 assert_int_equal(0, ly_ctx_get_options(NULL));
123 CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL);
124
125 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
126 CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL);
127 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
128 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL);
129
130 /* option not allowed to be changed */
131 assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY));
132 CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL);
133 assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY));
134 CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL);
135
136 /* unset */
137 /* LY_CTX_ALL_IMPLEMENTED */
138 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
139 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
140 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
141
142 /* LY_CTX_REF_IMPLEMENTED */
143 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
144 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
145 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
146
147 /* LY_CTX_DISABLE_SEARCHDIRS */
148 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
149 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
150 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
151
152 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
153 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
154 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
155 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
156
157 /* LY_CTX_PREFER_SEARCHDIRS */
158 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
159 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
160 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
161
162 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
163
164 /* set back */
165 /* LY_CTX_ALL_IMPLEMENTED */
166 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
167 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
168
169 /* LY_CTX_REF_IMPLEMENTED */
170 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
171 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
172
173 /* LY_CTX_DISABLE_SEARCHDIRS */
174 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
175 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
176
177 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
178 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
179 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
180
181 /* LY_CTX_PREFER_SEARCHDIRS */
182 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
183 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
184
185 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
186}
187
188static LY_ERR
189test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
190 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
191 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
192{
193 *module_data = user_data;
194 *format = LYS_IN_YANG;
195 *free_module_data = NULL;
196 return LY_SUCCESS;
197}
198
199static void
200test_models(void **state)
201{
202 struct ly_in *in;
203 const char *str;
204 struct lys_module *mod1, *mod2;
205 struct lys_glob_unres unres = {0};
206
207 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200208 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200209
210 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200211 assert_int_equal(0, ly_ctx_get_change_count(NULL));
212 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200213
214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200215 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200216
217 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
218 assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in, 4, 1, NULL, NULL, NULL, &unres, &mod1));
219 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
220 ly_in_free(in, 0);
221 CHECK_LOG_CTX("Invalid schema input format.", NULL);
222
223 /* import callback */
224 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
225 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
226 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
227 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
228 assert_string_equal("test", str);
229
230 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
231 assert_null(UTEST_LYCTX->imp_clb);
232 assert_null(UTEST_LYCTX->imp_clb_data);
233
234 /* name collision of module and submodule */
235 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
236 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in));
237 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
238 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
239 ly_in_free(in, 0);
240 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
241
242 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in));
243 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
244 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
245 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
246 ly_in_free(in, 0);
247 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
248 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
249 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
250 ly_in_free(in, 0);
251 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
252
253 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
254 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
255 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
256 lys_compile_unres_glob_revert(UTEST_LYCTX, &unres);
257 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
258 ly_in_free(in, 0);
259 CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL,
260 "Name collision between submodules of name \"y\".", "Line number 1.");
261
262 /* selecting correct revision of the submodules */
263 ly_ctx_reset_latests(UTEST_LYCTX);
264 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
265 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
266 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
267 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
268 ly_in_free(in, 0);
269 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
270
271 /* reloading module in case only the compiled module resists in the context */
272 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
273 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod1));
274 ly_in_free(in, 0);
275 mod1->implemented = 1;
276 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0, &unres));
277 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
278 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
279 assert_non_null(mod1->compiled);
280 assert_non_null(mod1->parsed);
281
282#if 0
283 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
284
285 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in));
286 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
287 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
288 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
289 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
290 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
291 assert_null(mod2);
292 ly_in_free(in, 0);
293#endif
294
295 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in));
296 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
297 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod2));
298 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
299 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
300 ly_in_free(in, 0);
301 assert_non_null(mod2);
302 assert_non_null(mod1->parsed);
303 assert_string_equal("w", mod1->name);
304}
305
306static void
307test_imports(void **state)
308{
309 const struct lys_module *mod1, *mod2, *import;
310
311 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200312 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200313
314 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
315 * from the callback should be loaded into the context and used as an import */
316 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
317 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
318 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
319 LYS_IN_YANG, &mod1));
320 assert_int_equal(1, mod1->latest_revision);
321 assert_int_equal(1, mod1->implemented);
322 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
323 LYS_IN_YANG, &mod2));
324 import = mod2->parsed->imports[0].module;
325 assert_int_equal(2, import->latest_revision);
326 assert_int_equal(0, mod1->latest_revision);
327 assert_ptr_not_equal(mod1, import);
328 assert_string_equal("2019-09-17", import->revision);
329 assert_int_equal(0, import->implemented);
330 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200331 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200332
333 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
334 * already present in the context should be selected and the callback's revision should not be loaded into the context */
335 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
336 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
337 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
338 LYS_IN_YANG, &mod1));
339 assert_int_equal(1, mod1->latest_revision);
340 assert_int_equal(1, mod1->implemented);
341 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
342 LYS_IN_YANG, &mod2));
343 import = mod2->parsed->imports[0].module;
344 assert_ptr_equal(mod1, import);
345 assert_int_equal(2, import->latest_revision);
346 assert_int_equal(1, import->implemented);
347 assert_string_equal("2019-09-18", import->revision);
348 assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
349}
350
351static void
352test_get_models(void **state)
353{
354 struct lys_module *mod, *mod2;
355 const char *str0 = "module a {namespace urn:a;prefix a;}";
356 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
357 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
358 struct ly_in *in0, *in1, *in2;
359 struct lys_glob_unres unres = {0};
360
361 unsigned int index = 0;
362 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
363
364 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
365 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
366 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
367
368 /* invalid arguments */
369 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
370 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
371 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
372 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
373 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
374 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
375 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
376 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
377 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
378
379 /* internal modules */
380 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
381 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
382 assert_non_null(mod);
383 assert_non_null(mod->parsed);
384 assert_string_equal("yang", mod->name);
385 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
386 assert_ptr_equal(mod, mod2);
387 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
388 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
389 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
390 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
391
392 /* select module by revision */
393 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
394 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
395 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
396 /* invalid attempts - implementing module of the same name and inserting the same module */
397 assert_int_equal(LY_EDENIED, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, NULL));
398 CHECK_LOG_CTX("Module \"a@2018-10-23\" is already implemented in the context.", NULL);
399 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
400 ly_in_reset(in1);
401 /* it is already there, fine */
402 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, NULL));
403 /* insert the second module only as imported, not implemented */
404 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
405 ly_in_reset(in2);
406 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
407 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
408 assert_non_null(mod2);
409 assert_ptr_not_equal(mod, mod2);
410 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
411 assert_ptr_equal(mod, mod2);
412 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
413 assert_ptr_equal(mod, mod2);
414 /* work with module with no revision */
415 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in0, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod));
416 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
417 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
418 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
419
420 str1 = "submodule b {belongs-to a {prefix a;}}";
421 ly_in_free(in1, 0);
422 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
423 assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
424 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
425 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
426
427 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
428 assert_string_equal(names[index - 1], mod->name);
429 }
430 assert_int_equal(9, index);
431
432 /* cleanup */
433 ly_in_free(in0, 0);
434 ly_in_free(in1, 0);
435 ly_in_free(in2, 0);
436}
437
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200438static void
439test_ylmem(void **state)
440{
441#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
442 " <module-set>\n"\
443 " <name>complete</name>\n"\
444 " <module>\n"\
445 " <name>yang</name>\n"\
446 " <revision>2021-04-07</revision>\n"\
447 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
448 " </module>\n"\
449 " <module>\n"\
450 " <name>ietf-yang-library</name>\n"\
451 " <revision>2019-01-04</revision>\n"\
452 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
453 " </module>\n"
454
455#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
456 " <name>ietf-yang-metadata</name>\n"\
457 " <revision>2016-08-05</revision>\n"\
458 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
459 " </import-only-module>\n"\
460 " <import-only-module>\n"\
461 " <name>ietf-inet-types</name>\n"\
462 " <revision>2013-07-15</revision>\n"\
463 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
464 " </import-only-module>\n"\
465 " <import-only-module>\n"\
466 " <name>ietf-yang-types</name>\n"\
467 " <revision>2013-07-15</revision>\n"\
468 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
469 " </import-only-module>\n"\
470 " <import-only-module>\n"\
471 " <name>ietf-datastores</name>\n"\
472 " <revision>2018-02-14</revision>\n"\
473 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
474 " </import-only-module>\n"
475
476#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
477 " <schema>\n"\
478 " <name>complete</name>\n"\
479 " <module-set>complete</module-set>\n"\
480 " </schema>\n"\
481 " <content-id>9</content-id>\n"\
482 "</yang-library>\n"\
483 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
484 " <module-set-id>12</module-set-id>\n"\
485 " <module>\n"\
486 " <name>ietf-yang-metadata</name>\n"\
487 " <revision>2016-08-05</revision>\n"\
488 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
489 " <conformance-type>import</conformance-type>\n"\
490 " </module>\n"\
491 " <module>\n"\
492 " <name>yang</name>\n"\
493 " <revision>2020-06-17</revision>\n"\
494 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
495 " <conformance-type>implement</conformance-type>\n"\
496 " </module>\n"\
497 " <module>\n"\
498 " <name>ietf-inet-types</name>\n"\
499 " <revision>2013-07-15</revision>\n"\
500 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
501 " <conformance-type>import</conformance-type>\n"\
502 " </module>\n"\
503 " <module>\n"\
504 " <name>ietf-yang-types</name>\n"\
505 " <revision>2013-07-15</revision>\n"\
506 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
507 " <conformance-type>import</conformance-type>\n"\
508 " </module>\n"\
509 " <module>\n"\
510 " <name>ietf-yang-library</name>\n"\
511 " <revision>2019-01-04</revision>\n"\
512 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
513 " <conformance-type>implement</conformance-type>\n"\
514 " </module>\n"\
515 " <module>\n"\
516 " <name>ietf-datastores</name>\n"\
517 " <revision>2018-02-14</revision>\n"\
518 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
519 " <conformance-type>import</conformance-type>\n"\
520 " </module>\n"
521
522 const char *yanglibrary_only =
523 DATA_YANG_LIBRARY_START
524 DATA_YANG_BASE_IMPORTS
525 DATA_YANG_SCHEMA_MODULE_STATE
526 "</modules-state>\n";
527
528 const char *with_netconf =
529 DATA_YANG_LIBRARY_START
530 " <module>\n"
531 " <name>ietf-netconf</name>\n"
532 " <revision>2011-06-01</revision>\n"
533 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
534 " </module>\n"
535 DATA_YANG_BASE_IMPORTS
536 " <import-only-module>\n"
537 " <name>ietf-netconf-acm</name>\n"
538 " <revision>2018-02-14</revision>\n"
539 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
540 " </import-only-module>\n"
541 DATA_YANG_SCHEMA_MODULE_STATE
542 " <module>\n"
543 " <name>ietf-netconf</name>\n"
544 " <revision>2011-06-01</revision>\n"
545 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
546 " <conformance-type>implement</conformance-type>\n"
547 " </module>\n"
548 " <module>\n"
549 " <name>ietf-netconf-acm</name>\n"
550 " <revision>2018-02-14</revision>\n"
551 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
552 " <conformance-type>import</conformance-type>\n"
553 " </module>\n"
554 "</modules-state>";
555
556 const char *with_netconf_features =
557 DATA_YANG_LIBRARY_START
558 " <module>\n"
559 " <name>ietf-netconf</name>\n"
560 " <revision>2011-06-01</revision>\n"
561 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
562 " <feature>writable-running</feature>\n"
563 " <feature>candidate</feature>\n"
564 " <feature>confirmed-commit</feature>\n"
565 " <feature>rollback-on-error</feature>\n"
566 " <feature>validate</feature>\n"
567 " <feature>startup</feature>\n"
568 " <feature>url</feature>\n"
569 " <feature>xpath</feature>\n"
570 " </module>\n"
571 " <import-only-module>\n"
572 " <name>ietf-yang-metadata</name>\n"
573 " <revision>2016-08-05</revision>\n"
574 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
575 " </import-only-module>\n"
576 " <import-only-module>\n"
577 " <name>ietf-inet-types</name>\n"
578 " <revision>2013-07-15</revision>\n"
579 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
580 " </import-only-module>\n"
581 " <import-only-module>\n"
582 " <name>ietf-yang-types</name>\n"
583 " <revision>2013-07-15</revision>\n"
584 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
585 " </import-only-module>\n"
586 " <import-only-module>\n"
587 " <name>ietf-datastores</name>\n"
588 " <revision>2018-02-14</revision>\n"
589 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
590 " </import-only-module>\n"
591 " <import-only-module>\n"
592 " <name>ietf-netconf-acm</name>\n"
593 " <revision>2018-02-14</revision>\n"
594 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
595 " </import-only-module>\n"
596 DATA_YANG_SCHEMA_MODULE_STATE
597 " <module>\n"
598 " <name>ietf-netconf</name>\n"
599 " <revision>2011-06-01</revision>\n"
600 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
601 " <feature>writable-running</feature>\n"
602 " <feature>candidate</feature>\n"
603 " <feature>confirmed-commit</feature>\n"
604 " <feature>rollback-on-error</feature>\n"
605 " <feature>validate</feature>\n"
606 " <feature>startup</feature>\n"
607 " <feature>url</feature>\n"
608 " <feature>xpath</feature>\n"
609 " <conformance-type>implement</conformance-type>\n"
610 " </module>\n"
611 " <module>\n"
612 " <name>ietf-netconf-acm</name>\n"
613 " <revision>2018-02-14</revision>\n"
614 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
615 " <conformance-type>import</conformance-type>\n"
616 " </module>\n"
617 "</modules-state>";
618
619 const char *garbage_revision =
620 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
621 " <module-set>\n"
622 " <name>complete</name>\n"
623 " <module>\n"
624 " <name>yang</name>\n"
625 " <revision>2020-06-17</revision>\n"
626 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
627 " </module>\n"
628 " <module>\n"
629 " <name>ietf-yang-library</name>\n"
630 " <revision>2019-01-01</revision>\n"
631 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
632 " </module>\n"
633 DATA_YANG_BASE_IMPORTS
634 DATA_YANG_SCHEMA_MODULE_STATE
635 "</modules-state>\n";
636
637 const char *no_yanglibrary =
638 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
639 " <module-set>\n"
640 " <name>complete</name>\n"
641 " <module>\n"
642 " <name>yang</name>\n"
643 " <revision>2021-04-07</revision>\n"
644 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
645 " </module>\n"
646 DATA_YANG_BASE_IMPORTS
647 DATA_YANG_SCHEMA_MODULE_STATE
648 "</modules-state>\n";
649
650 (void) state;
651 /* seperate context to avoid double free during teadown */
652 struct ly_ctx *ctx_test = NULL;
653
654 /* test invalid parameters */
655 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
656 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
657 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
658
659 /* basic test with ietf-yang-library-only */
660 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", yanglibrary_only, LYD_XML, 0, &ctx_test));
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200661 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200662 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200663 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200664
665 /* test loading module, should also import other module */
666 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf, LYD_XML, 0, &ctx_test));
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200667 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200668 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200669 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200670 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
671 assert_int_equal(LY_ENOT, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200672 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200673
674 /* test loading module with feature if they are present */
675 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, 0, &ctx_test));
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200676 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
677 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200678 assert_int_equal(LY_SUCCESS, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200679 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200680
681 /* test with not matching revision */
682 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
683
684 /* test data containing ietf-yang-library which conflicts with the option */
685 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
686
687 /* test creating without ietf-yang-library */
688 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
689 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200690 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200691}
692
aPiecekbb96b802021-04-12 08:12:52 +0200693static LY_ERR
694check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
695{
696 const struct lysp_node *pnode;
697 const char ***iter;
698
699 pnode = (const struct lysp_node *)node->priv;
700 CHECK_POINTER(pnode, 1);
701 iter = (const char ***)data;
702 CHECK_POINTER(**iter, 1);
703 CHECK_STRING(pnode->name, **iter);
704 (*iter)++;
705
706 return LY_SUCCESS;
707}
708
709static LY_ERR
710check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
711{
712 CHECK_POINTER(node->priv, 0);
713 return LY_SUCCESS;
714}
715
aPiecekfcfec0f2021-04-23 12:47:30 +0200716static void
717check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
718{
719 LY_ARRAY_COUNT_TYPE u, v;
720 struct lysc_ext_substmt *substmts;
721 struct lysc_node *cnode;
722 const char **iter;
723 const char *check[] = {
724 "tmp_cont", "lf", NULL
725 };
726
727 LY_ARRAY_FOR(ext, u) {
728 substmts = ext[u].substmts;
729 LY_ARRAY_FOR(substmts, v) {
730 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
731 cnode = *(struct lysc_node **)substmts[v].storage;
732 iter = check;
733 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
734 }
735 }
736 }
737}
738
739static void
740check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
741{
742 LY_ARRAY_COUNT_TYPE u, v;
743 struct lysc_ext_substmt *substmts;
744 struct lysc_node *cnode;
745
746 LY_ARRAY_FOR(ext, u) {
747 substmts = ext[u].substmts;
748 LY_ARRAY_FOR(substmts, v) {
749 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
750 cnode = *(struct lysc_node **)substmts[v].storage;
751 if (cnode) {
752 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
753 }
754 }
755 }
756 }
757}
758
aPiecekbb96b802021-04-12 08:12:52 +0200759/**
760 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
761 */
762static void
763test_set_priv_parsed(void **state)
764{
765 const struct lys_module *mod;
766 const char *schema_a;
767 const char **iter;
768 const char *check[] = {
769 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
770 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
771 "l2c", "l2cx", "ch", "cas", "casx", "oper",
772 "input", "inparam", "output", "outparam", "n1", NULL
773 };
774
775 /* each node must have a unique name. */
776 schema_a = "module a {\n"
777 " namespace urn:tests:a;\n"
778 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200779 "\n"
780 " import ietf-restconf {\n"
781 " prefix rc;\n"
782 " revision-date 2017-01-26;\n"
783 " }\n"
784 "\n"
785 " rc:yang-data \"tmp\" {\n"
786 " container tmp_cont {\n"
787 " leaf lf {\n"
788 " type string;\n"
789 " }\n"
790 " }\n"
791 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200792 " container cont {\n"
793 " notification contnotif;\n"
794 " leaf-list contx {\n"
795 " type string;\n"
796 " }\n"
797 " uses grp;\n"
798 " }\n"
799 " list l1 {\n"
800 " key \"l1a l1b\";\n"
801 " leaf l1a {\n"
802 " type string;\n"
803 " }\n"
804 " leaf l1b {\n"
805 " type string;\n"
806 " }\n"
807 " leaf l1c {\n"
808 " type string;\n"
809 " }\n"
810 " }\n"
811 " feature f1;\n"
812 " feature f2;\n"
813 " leaf foo1 {\n"
814 " type uint16;\n"
815 " if-feature f1;\n"
816 " }\n"
817 " leaf foo2 {\n"
818 " type uint16;\n"
819 " }\n"
820 " leaf foo3 {\n"
821 " type uint16;\n"
822 " if-feature f2;\n"
823 " }\n"
824 " leaf-list ll {\n"
825 " type string;\n"
826 " }\n"
827 " anydata any {\n"
828 " config false;\n"
829 " }\n"
830 " list l2 {\n"
831 " config false;\n"
832 " container l2c {\n"
833 " leaf l2cx {\n"
834 " type string;\n"
835 " }\n"
836 " }\n"
837 " }\n"
838 " choice ch {\n"
839 " case cas {\n"
840 " leaf casx {\n"
841 " type string;\n"
842 " }\n"
843 " }\n"
844 " }\n"
845 " rpc oper {\n"
846 " input {\n"
847 " leaf inparam {\n"
848 " type string;\n"
849 " }\n"
850 " }\n"
851 " output {\n"
852 " leaf outparam {\n"
853 " type int8;\n"
854 " }\n"
855 " }\n"
856 " }\n"
857 " notification n1;\n"
858 " grouping grp {\n"
859 " leaf grpleaf {\n"
860 " type uint16;\n"
861 " }\n"
862 " }\n"
863 " augment /cont {\n"
864 " leaf augleaf {\n"
865 " type uint16;\n"
866 " }\n"
867 " }\n"
868 " deviation /a:foo2 {\n"
869 " deviate not-supported;\n"
870 " }\n"
871 "}\n";
872
873 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200874 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200875 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200876
aPiecekbb96b802021-04-12 08:12:52 +0200877 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200878 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
879 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200880 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
881
882 print_message("[ ] create context\n");
883 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
884 iter = check;
885 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200886 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200887
888 print_message("[ ] unset option\n");
889 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
890 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
891 iter = check;
892 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200893 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200894
895 print_message("[ ] set option\n");
896 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
897 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
898 iter = check;
899 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200900 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200901}
902
Michal Vasko01db7de2021-04-16 12:23:30 +0200903static void
904test_explicit_compile(void **state)
905{
906 uint32_t i;
907 const struct lys_module *mod;
908 const char *schema_a = "module a {\n"
909 " namespace urn:tests:a;\n"
910 " prefix a;yang-version 1.1;\n"
911 " feature f1;\n"
912 " feature f2;\n"
913 " leaf foo1 {\n"
914 " type uint16;\n"
915 " if-feature f1;\n"
916 " }\n"
917 " leaf foo2 {\n"
918 " type uint16;\n"
919 " }\n"
920 " container cont {\n"
921 " leaf foo3 {\n"
922 " type string;\n"
923 " }\n"
924 " }\n"
925 "}\n";
926 const char *schema_b = "module b {\n"
927 " namespace urn:tests:b;\n"
928 " prefix b;yang-version 1.1;\n"
929 " import a {\n"
930 " prefix a;\n"
931 " }\n"
932 " augment /a:cont {\n"
933 " leaf augleaf {\n"
934 " type uint16;\n"
935 " }\n"
936 " }\n"
937 "}\n";
938 const char *schema_c = "module c {\n"
939 " namespace urn:tests:c;\n"
940 " prefix c;yang-version 1.1;\n"
941 " import a {\n"
942 " prefix a;\n"
943 " }\n"
944 " deviation /a:foo2 {\n"
945 " deviate not-supported;\n"
946 " }\n"
947 "}\n";
948
949 /* use own context with extra flags */
950 ly_ctx_destroy(UTEST_LYCTX);
951 const char *feats[] = {"f1", NULL};
952
953 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
954 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
955 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
956 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
957 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
958
959 /* none of the modules should be implemented */
960 i = 0;
961 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
962 assert_false(mod->implemented);
963 assert_false(mod->compiled);
964 }
965
966 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
967
968 /* check internal modules */
969 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
970 assert_non_null(mod);
971 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
972 assert_non_null(mod);
973 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
974 assert_non_null(mod);
975
976 /* check test modules */
977 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
978 assert_non_null(mod);
979 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
980 assert_non_null(mod);
981 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
982 assert_non_null(mod);
983}
984
Radek Iša56ca9e42020-09-08 18:42:00 +0200985int
986main(void)
987{
988 const struct CMUnitTest tests[] = {
989 UTEST(test_searchdirs),
990 UTEST(test_options),
991 UTEST(test_models),
992 UTEST(test_imports),
993 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200994 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +0200995 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +0200996 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +0200997 };
998
999 return cmocka_run_group_tests(tests, NULL, NULL);
1000}