blob: 9f7961bd304fe41a12e4c01175d516501d6fa28e [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));
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200219 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 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));
Radek Iša56ca9e42020-09-08 18:42:00 +0200237 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200238 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 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));
Michal Vaskodd992582021-06-10 14:34:57 +0200243 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Michal Vasko4e205e82021-06-08 14:01:47 +0200244 assert_int_equal(LY_SUCCESS, lys_implement(mod1, NULL, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
246 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
247 ly_in_free(in, 0);
248 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200250 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200251 ly_in_free(in, 0);
252 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
253
254 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
255 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200256 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 lys_compile_unres_glob_revert(UTEST_LYCTX, &unres);
258 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
259 ly_in_free(in, 0);
260 CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL,
261 "Name collision between submodules of name \"y\".", "Line number 1.");
262
263 /* selecting correct revision of the submodules */
264 ly_ctx_reset_latests(UTEST_LYCTX);
265 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
266 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200268 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200269 ly_in_free(in, 0);
270 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
271
272 /* reloading module in case only the compiled module resists in the context */
273 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200274 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200275 ly_in_free(in, 0);
Michal Vaskodd992582021-06-10 14:34:57 +0200276 assert_int_equal(LY_SUCCESS, lys_implement(mod1, NULL, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 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;}");
Michal Vaskodd992582021-06-10 14:34:57 +0200297 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
298 assert_int_equal(LY_SUCCESS, _lys_set_implemented(mod2, NULL, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200299 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
300 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
301 ly_in_free(in, 0);
302 assert_non_null(mod2);
303 assert_non_null(mod1->parsed);
304 assert_string_equal("w", mod1->name);
305}
306
307static void
308test_imports(void **state)
309{
310 const struct lys_module *mod1, *mod2, *import;
311
312 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200313 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200314
315 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
316 * from the callback should be loaded into the context and used as an import */
317 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
318 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
319 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
320 LYS_IN_YANG, &mod1));
321 assert_int_equal(1, mod1->latest_revision);
322 assert_int_equal(1, mod1->implemented);
323 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
324 LYS_IN_YANG, &mod2));
325 import = mod2->parsed->imports[0].module;
326 assert_int_equal(2, import->latest_revision);
327 assert_int_equal(0, mod1->latest_revision);
328 assert_ptr_not_equal(mod1, import);
329 assert_string_equal("2019-09-17", import->revision);
330 assert_int_equal(0, import->implemented);
331 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200332 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200333
334 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
335 * already present in the context should be selected and the callback's revision should not be loaded into the context */
336 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
337 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
338 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
339 LYS_IN_YANG, &mod1));
340 assert_int_equal(1, mod1->latest_revision);
341 assert_int_equal(1, mod1->implemented);
342 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
343 LYS_IN_YANG, &mod2));
344 import = mod2->parsed->imports[0].module;
345 assert_ptr_equal(mod1, import);
346 assert_int_equal(2, import->latest_revision);
347 assert_int_equal(1, import->implemented);
348 assert_string_equal("2019-09-18", import->revision);
349 assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
350}
351
352static void
353test_get_models(void **state)
354{
355 struct lys_module *mod, *mod2;
356 const char *str0 = "module a {namespace urn:a;prefix a;}";
357 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
358 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
359 struct ly_in *in0, *in1, *in2;
360 struct lys_glob_unres unres = {0};
361
362 unsigned int index = 0;
363 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
364
365 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
366 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
367 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
368
369 /* invalid arguments */
370 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
371 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
372 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
373 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
374 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
375 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
376 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
377 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
378 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
379
380 /* internal modules */
381 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
382 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
383 assert_non_null(mod);
384 assert_non_null(mod->parsed);
385 assert_string_equal("yang", mod->name);
386 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
387 assert_ptr_equal(mod, mod2);
388 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
389 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
390 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
391 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
392
393 /* select module by revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200394 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod));
Michal Vasko4e205e82021-06-08 14:01:47 +0200395 assert_int_equal(LY_SUCCESS, lys_implement(mod, NULL, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200396 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
397 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
398 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200399 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in2, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
Michal Vasko4e205e82021-06-08 14:01:47 +0200400 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
401 CHECK_LOG_CTX("Module \"a@2018-10-24\" is present in the context in other implemented revision (2018-10-23).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200402 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
403 ly_in_reset(in1);
404 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200405 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, NULL, &unres.creating, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200406 /* insert the second module only as imported, not implemented */
407 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
408 ly_in_reset(in2);
Radek Iša56ca9e42020-09-08 18:42:00 +0200409 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200410 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in2, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200411 assert_non_null(mod2);
412 assert_ptr_not_equal(mod, mod2);
413 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
414 assert_ptr_equal(mod, mod2);
415 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
416 assert_ptr_equal(mod, mod2);
417 /* work with module with no revision */
Radek Iša56ca9e42020-09-08 18:42:00 +0200418 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
Michal Vaskodd992582021-06-10 14:34:57 +0200419 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in0, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200420 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
421 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
422
423 str1 = "submodule b {belongs-to a {prefix a;}}";
424 ly_in_free(in1, 0);
425 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200426 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
428 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
429
430 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
431 assert_string_equal(names[index - 1], mod->name);
432 }
433 assert_int_equal(9, index);
434
435 /* cleanup */
436 ly_in_free(in0, 0);
437 ly_in_free(in1, 0);
438 ly_in_free(in2, 0);
439}
440
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200441static void
442test_ylmem(void **state)
443{
444#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
445 " <module-set>\n"\
446 " <name>complete</name>\n"\
447 " <module>\n"\
448 " <name>yang</name>\n"\
449 " <revision>2021-04-07</revision>\n"\
450 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
451 " </module>\n"\
452 " <module>\n"\
453 " <name>ietf-yang-library</name>\n"\
454 " <revision>2019-01-04</revision>\n"\
455 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
456 " </module>\n"
457
458#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
459 " <name>ietf-yang-metadata</name>\n"\
460 " <revision>2016-08-05</revision>\n"\
461 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
462 " </import-only-module>\n"\
463 " <import-only-module>\n"\
464 " <name>ietf-inet-types</name>\n"\
465 " <revision>2013-07-15</revision>\n"\
466 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
467 " </import-only-module>\n"\
468 " <import-only-module>\n"\
469 " <name>ietf-yang-types</name>\n"\
470 " <revision>2013-07-15</revision>\n"\
471 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
472 " </import-only-module>\n"\
473 " <import-only-module>\n"\
474 " <name>ietf-datastores</name>\n"\
475 " <revision>2018-02-14</revision>\n"\
476 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
477 " </import-only-module>\n"
478
479#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
480 " <schema>\n"\
481 " <name>complete</name>\n"\
482 " <module-set>complete</module-set>\n"\
483 " </schema>\n"\
484 " <content-id>9</content-id>\n"\
485 "</yang-library>\n"\
486 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
487 " <module-set-id>12</module-set-id>\n"\
488 " <module>\n"\
489 " <name>ietf-yang-metadata</name>\n"\
490 " <revision>2016-08-05</revision>\n"\
491 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
492 " <conformance-type>import</conformance-type>\n"\
493 " </module>\n"\
494 " <module>\n"\
495 " <name>yang</name>\n"\
496 " <revision>2020-06-17</revision>\n"\
497 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
498 " <conformance-type>implement</conformance-type>\n"\
499 " </module>\n"\
500 " <module>\n"\
501 " <name>ietf-inet-types</name>\n"\
502 " <revision>2013-07-15</revision>\n"\
503 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
504 " <conformance-type>import</conformance-type>\n"\
505 " </module>\n"\
506 " <module>\n"\
507 " <name>ietf-yang-types</name>\n"\
508 " <revision>2013-07-15</revision>\n"\
509 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
510 " <conformance-type>import</conformance-type>\n"\
511 " </module>\n"\
512 " <module>\n"\
513 " <name>ietf-yang-library</name>\n"\
514 " <revision>2019-01-04</revision>\n"\
515 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
516 " <conformance-type>implement</conformance-type>\n"\
517 " </module>\n"\
518 " <module>\n"\
519 " <name>ietf-datastores</name>\n"\
520 " <revision>2018-02-14</revision>\n"\
521 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
522 " <conformance-type>import</conformance-type>\n"\
523 " </module>\n"
524
525 const char *yanglibrary_only =
526 DATA_YANG_LIBRARY_START
527 DATA_YANG_BASE_IMPORTS
528 DATA_YANG_SCHEMA_MODULE_STATE
529 "</modules-state>\n";
530
531 const char *with_netconf =
532 DATA_YANG_LIBRARY_START
533 " <module>\n"
534 " <name>ietf-netconf</name>\n"
535 " <revision>2011-06-01</revision>\n"
536 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
537 " </module>\n"
538 DATA_YANG_BASE_IMPORTS
539 " <import-only-module>\n"
540 " <name>ietf-netconf-acm</name>\n"
541 " <revision>2018-02-14</revision>\n"
542 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
543 " </import-only-module>\n"
544 DATA_YANG_SCHEMA_MODULE_STATE
545 " <module>\n"
546 " <name>ietf-netconf</name>\n"
547 " <revision>2011-06-01</revision>\n"
548 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
549 " <conformance-type>implement</conformance-type>\n"
550 " </module>\n"
551 " <module>\n"
552 " <name>ietf-netconf-acm</name>\n"
553 " <revision>2018-02-14</revision>\n"
554 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
555 " <conformance-type>import</conformance-type>\n"
556 " </module>\n"
557 "</modules-state>";
558
559 const char *with_netconf_features =
560 DATA_YANG_LIBRARY_START
561 " <module>\n"
562 " <name>ietf-netconf</name>\n"
563 " <revision>2011-06-01</revision>\n"
564 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
565 " <feature>writable-running</feature>\n"
566 " <feature>candidate</feature>\n"
567 " <feature>confirmed-commit</feature>\n"
568 " <feature>rollback-on-error</feature>\n"
569 " <feature>validate</feature>\n"
570 " <feature>startup</feature>\n"
571 " <feature>url</feature>\n"
572 " <feature>xpath</feature>\n"
573 " </module>\n"
574 " <import-only-module>\n"
575 " <name>ietf-yang-metadata</name>\n"
576 " <revision>2016-08-05</revision>\n"
577 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
578 " </import-only-module>\n"
579 " <import-only-module>\n"
580 " <name>ietf-inet-types</name>\n"
581 " <revision>2013-07-15</revision>\n"
582 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
583 " </import-only-module>\n"
584 " <import-only-module>\n"
585 " <name>ietf-yang-types</name>\n"
586 " <revision>2013-07-15</revision>\n"
587 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
588 " </import-only-module>\n"
589 " <import-only-module>\n"
590 " <name>ietf-datastores</name>\n"
591 " <revision>2018-02-14</revision>\n"
592 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
593 " </import-only-module>\n"
594 " <import-only-module>\n"
595 " <name>ietf-netconf-acm</name>\n"
596 " <revision>2018-02-14</revision>\n"
597 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
598 " </import-only-module>\n"
599 DATA_YANG_SCHEMA_MODULE_STATE
600 " <module>\n"
601 " <name>ietf-netconf</name>\n"
602 " <revision>2011-06-01</revision>\n"
603 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
604 " <feature>writable-running</feature>\n"
605 " <feature>candidate</feature>\n"
606 " <feature>confirmed-commit</feature>\n"
607 " <feature>rollback-on-error</feature>\n"
608 " <feature>validate</feature>\n"
609 " <feature>startup</feature>\n"
610 " <feature>url</feature>\n"
611 " <feature>xpath</feature>\n"
612 " <conformance-type>implement</conformance-type>\n"
613 " </module>\n"
614 " <module>\n"
615 " <name>ietf-netconf-acm</name>\n"
616 " <revision>2018-02-14</revision>\n"
617 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
618 " <conformance-type>import</conformance-type>\n"
619 " </module>\n"
620 "</modules-state>";
621
622 const char *garbage_revision =
623 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
624 " <module-set>\n"
625 " <name>complete</name>\n"
626 " <module>\n"
627 " <name>yang</name>\n"
628 " <revision>2020-06-17</revision>\n"
629 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
630 " </module>\n"
631 " <module>\n"
632 " <name>ietf-yang-library</name>\n"
633 " <revision>2019-01-01</revision>\n"
634 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
635 " </module>\n"
636 DATA_YANG_BASE_IMPORTS
637 DATA_YANG_SCHEMA_MODULE_STATE
638 "</modules-state>\n";
639
640 const char *no_yanglibrary =
641 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
642 " <module-set>\n"
643 " <name>complete</name>\n"
644 " <module>\n"
645 " <name>yang</name>\n"
646 " <revision>2021-04-07</revision>\n"
647 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
648 " </module>\n"
649 DATA_YANG_BASE_IMPORTS
650 DATA_YANG_SCHEMA_MODULE_STATE
651 "</modules-state>\n";
652
653 (void) state;
654 /* seperate context to avoid double free during teadown */
655 struct ly_ctx *ctx_test = NULL;
656
657 /* test invalid parameters */
658 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
659 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
660 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
661
662 /* basic test with ietf-yang-library-only */
663 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 +0200664 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200665 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200666 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200667
668 /* test loading module, should also import other module */
669 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 +0200670 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200671 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200672 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200673 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
674 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 +0200675 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200676
677 /* test loading module with feature if they are present */
678 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 +0200679 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
680 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200681 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 +0200682 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200683
684 /* test with not matching revision */
685 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
686
687 /* test data containing ietf-yang-library which conflicts with the option */
688 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
689
690 /* test creating without ietf-yang-library */
691 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
692 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200693 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200694}
695
aPiecekbb96b802021-04-12 08:12:52 +0200696static LY_ERR
697check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
698{
699 const struct lysp_node *pnode;
700 const char ***iter;
701
702 pnode = (const struct lysp_node *)node->priv;
703 CHECK_POINTER(pnode, 1);
704 iter = (const char ***)data;
705 CHECK_POINTER(**iter, 1);
706 CHECK_STRING(pnode->name, **iter);
707 (*iter)++;
708
709 return LY_SUCCESS;
710}
711
712static LY_ERR
713check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
714{
715 CHECK_POINTER(node->priv, 0);
716 return LY_SUCCESS;
717}
718
aPiecekfcfec0f2021-04-23 12:47:30 +0200719static void
720check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
721{
722 LY_ARRAY_COUNT_TYPE u, v;
723 struct lysc_ext_substmt *substmts;
724 struct lysc_node *cnode;
725 const char **iter;
726 const char *check[] = {
727 "tmp_cont", "lf", NULL
728 };
729
730 LY_ARRAY_FOR(ext, u) {
731 substmts = ext[u].substmts;
732 LY_ARRAY_FOR(substmts, v) {
733 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
734 cnode = *(struct lysc_node **)substmts[v].storage;
735 iter = check;
736 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
737 }
738 }
739 }
740}
741
742static void
743check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
744{
745 LY_ARRAY_COUNT_TYPE u, v;
746 struct lysc_ext_substmt *substmts;
747 struct lysc_node *cnode;
748
749 LY_ARRAY_FOR(ext, u) {
750 substmts = ext[u].substmts;
751 LY_ARRAY_FOR(substmts, v) {
752 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
753 cnode = *(struct lysc_node **)substmts[v].storage;
754 if (cnode) {
755 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
756 }
757 }
758 }
759 }
760}
761
aPiecekbb96b802021-04-12 08:12:52 +0200762/**
763 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
764 */
765static void
766test_set_priv_parsed(void **state)
767{
768 const struct lys_module *mod;
769 const char *schema_a;
770 const char **iter;
771 const char *check[] = {
772 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
773 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
774 "l2c", "l2cx", "ch", "cas", "casx", "oper",
775 "input", "inparam", "output", "outparam", "n1", NULL
776 };
777
778 /* each node must have a unique name. */
779 schema_a = "module a {\n"
780 " namespace urn:tests:a;\n"
781 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200782 "\n"
783 " import ietf-restconf {\n"
784 " prefix rc;\n"
785 " revision-date 2017-01-26;\n"
786 " }\n"
787 "\n"
788 " rc:yang-data \"tmp\" {\n"
789 " container tmp_cont {\n"
790 " leaf lf {\n"
791 " type string;\n"
792 " }\n"
793 " }\n"
794 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200795 " container cont {\n"
796 " notification contnotif;\n"
797 " leaf-list contx {\n"
798 " type string;\n"
799 " }\n"
800 " uses grp;\n"
801 " }\n"
802 " list l1 {\n"
803 " key \"l1a l1b\";\n"
804 " leaf l1a {\n"
805 " type string;\n"
806 " }\n"
807 " leaf l1b {\n"
808 " type string;\n"
809 " }\n"
810 " leaf l1c {\n"
811 " type string;\n"
812 " }\n"
813 " }\n"
814 " feature f1;\n"
815 " feature f2;\n"
816 " leaf foo1 {\n"
817 " type uint16;\n"
818 " if-feature f1;\n"
819 " }\n"
820 " leaf foo2 {\n"
821 " type uint16;\n"
822 " }\n"
823 " leaf foo3 {\n"
824 " type uint16;\n"
825 " if-feature f2;\n"
826 " }\n"
827 " leaf-list ll {\n"
828 " type string;\n"
829 " }\n"
830 " anydata any {\n"
831 " config false;\n"
832 " }\n"
833 " list l2 {\n"
834 " config false;\n"
835 " container l2c {\n"
836 " leaf l2cx {\n"
837 " type string;\n"
838 " }\n"
839 " }\n"
840 " }\n"
841 " choice ch {\n"
842 " case cas {\n"
843 " leaf casx {\n"
844 " type string;\n"
845 " }\n"
846 " }\n"
847 " }\n"
848 " rpc oper {\n"
849 " input {\n"
850 " leaf inparam {\n"
851 " type string;\n"
852 " }\n"
853 " }\n"
854 " output {\n"
855 " leaf outparam {\n"
856 " type int8;\n"
857 " }\n"
858 " }\n"
859 " }\n"
860 " notification n1;\n"
861 " grouping grp {\n"
862 " leaf grpleaf {\n"
863 " type uint16;\n"
864 " }\n"
865 " }\n"
866 " augment /cont {\n"
867 " leaf augleaf {\n"
868 " type uint16;\n"
869 " }\n"
870 " }\n"
871 " deviation /a:foo2 {\n"
872 " deviate not-supported;\n"
873 " }\n"
874 "}\n";
875
876 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200877 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200878 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200879
aPiecekbb96b802021-04-12 08:12:52 +0200880 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200881 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
882 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200883 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
884
885 print_message("[ ] create context\n");
886 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
887 iter = check;
888 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200889 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200890
891 print_message("[ ] unset option\n");
892 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
893 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
894 iter = check;
895 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200896 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200897
898 print_message("[ ] set option\n");
899 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
900 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
901 iter = check;
902 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200903 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200904}
905
Michal Vasko01db7de2021-04-16 12:23:30 +0200906static void
907test_explicit_compile(void **state)
908{
909 uint32_t i;
910 const struct lys_module *mod;
911 const char *schema_a = "module a {\n"
912 " namespace urn:tests:a;\n"
913 " prefix a;yang-version 1.1;\n"
914 " feature f1;\n"
915 " feature f2;\n"
916 " leaf foo1 {\n"
917 " type uint16;\n"
918 " if-feature f1;\n"
919 " }\n"
920 " leaf foo2 {\n"
921 " type uint16;\n"
922 " }\n"
923 " container cont {\n"
924 " leaf foo3 {\n"
925 " type string;\n"
926 " }\n"
927 " }\n"
928 "}\n";
929 const char *schema_b = "module b {\n"
930 " namespace urn:tests:b;\n"
931 " prefix b;yang-version 1.1;\n"
932 " import a {\n"
933 " prefix a;\n"
934 " }\n"
935 " augment /a:cont {\n"
936 " leaf augleaf {\n"
937 " type uint16;\n"
938 " }\n"
939 " }\n"
940 "}\n";
941 const char *schema_c = "module c {\n"
942 " namespace urn:tests:c;\n"
943 " prefix c;yang-version 1.1;\n"
944 " import a {\n"
945 " prefix a;\n"
946 " }\n"
947 " deviation /a:foo2 {\n"
948 " deviate not-supported;\n"
949 " }\n"
950 "}\n";
951
952 /* use own context with extra flags */
953 ly_ctx_destroy(UTEST_LYCTX);
954 const char *feats[] = {"f1", NULL};
955
956 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
957 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
958 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
959 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
960 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
961
Michal Vaskodd992582021-06-10 14:34:57 +0200962 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +0200963 i = 0;
964 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +0200965 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +0200966 }
967
968 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
969
970 /* check internal modules */
971 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
972 assert_non_null(mod);
973 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
974 assert_non_null(mod);
975 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
976 assert_non_null(mod);
977
978 /* check test modules */
979 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
980 assert_non_null(mod);
981 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
982 assert_non_null(mod);
983 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
984 assert_non_null(mod);
985}
986
Radek Iša56ca9e42020-09-08 18:42:00 +0200987int
988main(void)
989{
990 const struct CMUnitTest tests[] = {
991 UTEST(test_searchdirs),
992 UTEST(test_options),
993 UTEST(test_models),
994 UTEST(test_imports),
995 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200996 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +0200997 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +0200998 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +0200999 };
1000
1001 return cmocka_run_group_tests(tests, NULL, NULL);
1002}