blob: 4d392ed43473a0e74c823ec45cf01d55f7df62c3 [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));
Michal Vaskodd992582021-06-10 14:34:57 +0200218 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200219 lys_unres_glob_erase(&unres);
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));
Michal Vaskodd992582021-06-10 14:34:57 +0200237 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200238 lys_unres_glob_erase(&unres);
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));
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 ly_in_free(in, 0);
245 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200246 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200247 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 ly_in_free(in, 0);
249 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
250
251 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
252 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 +0200253 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200254 lys_unres_glob_revert(UTEST_LYCTX, &unres);
255 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 ly_in_free(in, 0);
257 CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL,
258 "Name collision between submodules of name \"y\".", "Line number 1.");
259
260 /* selecting correct revision of the submodules */
261 ly_ctx_reset_latests(UTEST_LYCTX);
262 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
263 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200264 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
Michal Vaskof4258e12021-06-15 12:11:42 +0200265 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200266 ly_in_free(in, 0);
267 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
268
269 /* reloading module in case only the compiled module resists in the context */
270 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200271 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, (const struct lys_module **)&mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 ly_in_free(in, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 assert_non_null(mod1->compiled);
274 assert_non_null(mod1->parsed);
275
276#if 0
277 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
278
279 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));
280 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
281 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
282 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
283 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
284 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
285 assert_null(mod2);
286 ly_in_free(in, 0);
287#endif
288
289 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));
290 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200291 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, (const struct lys_module **)&mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 ly_in_free(in, 0);
293 assert_non_null(mod2);
294 assert_non_null(mod1->parsed);
295 assert_string_equal("w", mod1->name);
296}
297
298static void
299test_imports(void **state)
300{
301 const struct lys_module *mod1, *mod2, *import;
302
303 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200304 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200305
306 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
307 * from the callback should be loaded into the context and used as an import */
308 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
309 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
310 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
311 LYS_IN_YANG, &mod1));
312 assert_int_equal(1, mod1->latest_revision);
313 assert_int_equal(1, mod1->implemented);
314 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
315 LYS_IN_YANG, &mod2));
316 import = mod2->parsed->imports[0].module;
317 assert_int_equal(2, import->latest_revision);
318 assert_int_equal(0, mod1->latest_revision);
319 assert_ptr_not_equal(mod1, import);
320 assert_string_equal("2019-09-17", import->revision);
321 assert_int_equal(0, import->implemented);
322 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200323 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200324
325 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
326 * already present in the context should be selected and the callback's revision should not be loaded into the context */
327 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
328 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
329 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
330 LYS_IN_YANG, &mod1));
331 assert_int_equal(1, mod1->latest_revision);
332 assert_int_equal(1, mod1->implemented);
333 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
334 LYS_IN_YANG, &mod2));
335 import = mod2->parsed->imports[0].module;
336 assert_ptr_equal(mod1, import);
337 assert_int_equal(2, import->latest_revision);
338 assert_int_equal(1, import->implemented);
339 assert_string_equal("2019-09-18", import->revision);
340 assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
341}
342
343static void
344test_get_models(void **state)
345{
346 struct lys_module *mod, *mod2;
347 const char *str0 = "module a {namespace urn:a;prefix a;}";
348 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
349 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
350 struct ly_in *in0, *in1, *in2;
351 struct lys_glob_unres unres = {0};
352
353 unsigned int index = 0;
354 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
355
356 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
357 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
358 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
359
360 /* invalid arguments */
361 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
362 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
363 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
364 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
365 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
366 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
367 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
368 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
369 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
370
371 /* internal modules */
372 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
373 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
374 assert_non_null(mod);
375 assert_non_null(mod->parsed);
376 assert_string_equal("yang", mod->name);
377 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
378 assert_ptr_equal(mod, mod2);
379 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
380 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
381 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
382 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
383
384 /* select module by revision */
Michal Vaskoa9f807e2021-06-15 12:07:16 +0200385 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, (const struct lys_module **)&mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200386 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200387 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 +0200388 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
389 CHECK_LOG_CTX("Module \"a@2018-10-24\" is present in the context in other implemented revision (2018-10-23).", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200390 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200391 ly_in_reset(in1);
392 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200393 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 +0200394 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200395 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200396 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200397 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in2, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2));
Michal Vaskof4258e12021-06-15 12:11:42 +0200398 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200399 assert_non_null(mod2);
400 assert_ptr_not_equal(mod, mod2);
401 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
402 assert_ptr_equal(mod, mod2);
403 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
404 assert_ptr_equal(mod, mod2);
405 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200406 assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in0, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod));
Michal Vaskof4258e12021-06-15 12:11:42 +0200407 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200408 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
409 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
410
411 str1 = "submodule b {belongs-to a {prefix a;}}";
412 ly_in_free(in1, 0);
413 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200414 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 +0200415 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200416 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200417
418 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
419 assert_string_equal(names[index - 1], mod->name);
420 }
421 assert_int_equal(9, index);
422
423 /* cleanup */
424 ly_in_free(in0, 0);
425 ly_in_free(in1, 0);
426 ly_in_free(in2, 0);
427}
428
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200429static void
430test_ylmem(void **state)
431{
432#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
433 " <module-set>\n"\
434 " <name>complete</name>\n"\
435 " <module>\n"\
436 " <name>yang</name>\n"\
437 " <revision>2021-04-07</revision>\n"\
438 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
439 " </module>\n"\
440 " <module>\n"\
441 " <name>ietf-yang-library</name>\n"\
442 " <revision>2019-01-04</revision>\n"\
443 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
444 " </module>\n"
445
446#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
447 " <name>ietf-yang-metadata</name>\n"\
448 " <revision>2016-08-05</revision>\n"\
449 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
450 " </import-only-module>\n"\
451 " <import-only-module>\n"\
452 " <name>ietf-inet-types</name>\n"\
453 " <revision>2013-07-15</revision>\n"\
454 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
455 " </import-only-module>\n"\
456 " <import-only-module>\n"\
457 " <name>ietf-yang-types</name>\n"\
458 " <revision>2013-07-15</revision>\n"\
459 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
460 " </import-only-module>\n"\
461 " <import-only-module>\n"\
462 " <name>ietf-datastores</name>\n"\
463 " <revision>2018-02-14</revision>\n"\
464 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
465 " </import-only-module>\n"
466
467#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
468 " <schema>\n"\
469 " <name>complete</name>\n"\
470 " <module-set>complete</module-set>\n"\
471 " </schema>\n"\
472 " <content-id>9</content-id>\n"\
473 "</yang-library>\n"\
474 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
475 " <module-set-id>12</module-set-id>\n"\
476 " <module>\n"\
477 " <name>ietf-yang-metadata</name>\n"\
478 " <revision>2016-08-05</revision>\n"\
479 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
480 " <conformance-type>import</conformance-type>\n"\
481 " </module>\n"\
482 " <module>\n"\
483 " <name>yang</name>\n"\
484 " <revision>2020-06-17</revision>\n"\
485 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
486 " <conformance-type>implement</conformance-type>\n"\
487 " </module>\n"\
488 " <module>\n"\
489 " <name>ietf-inet-types</name>\n"\
490 " <revision>2013-07-15</revision>\n"\
491 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
492 " <conformance-type>import</conformance-type>\n"\
493 " </module>\n"\
494 " <module>\n"\
495 " <name>ietf-yang-types</name>\n"\
496 " <revision>2013-07-15</revision>\n"\
497 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
498 " <conformance-type>import</conformance-type>\n"\
499 " </module>\n"\
500 " <module>\n"\
501 " <name>ietf-yang-library</name>\n"\
502 " <revision>2019-01-04</revision>\n"\
503 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
504 " <conformance-type>implement</conformance-type>\n"\
505 " </module>\n"\
506 " <module>\n"\
507 " <name>ietf-datastores</name>\n"\
508 " <revision>2018-02-14</revision>\n"\
509 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
510 " <conformance-type>import</conformance-type>\n"\
511 " </module>\n"
512
513 const char *yanglibrary_only =
514 DATA_YANG_LIBRARY_START
515 DATA_YANG_BASE_IMPORTS
516 DATA_YANG_SCHEMA_MODULE_STATE
517 "</modules-state>\n";
518
519 const char *with_netconf =
520 DATA_YANG_LIBRARY_START
521 " <module>\n"
522 " <name>ietf-netconf</name>\n"
523 " <revision>2011-06-01</revision>\n"
524 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
525 " </module>\n"
526 DATA_YANG_BASE_IMPORTS
527 " <import-only-module>\n"
528 " <name>ietf-netconf-acm</name>\n"
529 " <revision>2018-02-14</revision>\n"
530 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
531 " </import-only-module>\n"
532 DATA_YANG_SCHEMA_MODULE_STATE
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 " <conformance-type>implement</conformance-type>\n"
538 " </module>\n"
539 " <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 " <conformance-type>import</conformance-type>\n"
544 " </module>\n"
545 "</modules-state>";
546
547 const char *with_netconf_features =
548 DATA_YANG_LIBRARY_START
549 " <module>\n"
550 " <name>ietf-netconf</name>\n"
551 " <revision>2011-06-01</revision>\n"
552 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
553 " <feature>writable-running</feature>\n"
554 " <feature>candidate</feature>\n"
555 " <feature>confirmed-commit</feature>\n"
556 " <feature>rollback-on-error</feature>\n"
557 " <feature>validate</feature>\n"
558 " <feature>startup</feature>\n"
559 " <feature>url</feature>\n"
560 " <feature>xpath</feature>\n"
561 " </module>\n"
562 " <import-only-module>\n"
563 " <name>ietf-yang-metadata</name>\n"
564 " <revision>2016-08-05</revision>\n"
565 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
566 " </import-only-module>\n"
567 " <import-only-module>\n"
568 " <name>ietf-inet-types</name>\n"
569 " <revision>2013-07-15</revision>\n"
570 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
571 " </import-only-module>\n"
572 " <import-only-module>\n"
573 " <name>ietf-yang-types</name>\n"
574 " <revision>2013-07-15</revision>\n"
575 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
576 " </import-only-module>\n"
577 " <import-only-module>\n"
578 " <name>ietf-datastores</name>\n"
579 " <revision>2018-02-14</revision>\n"
580 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
581 " </import-only-module>\n"
582 " <import-only-module>\n"
583 " <name>ietf-netconf-acm</name>\n"
584 " <revision>2018-02-14</revision>\n"
585 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
586 " </import-only-module>\n"
587 DATA_YANG_SCHEMA_MODULE_STATE
588 " <module>\n"
589 " <name>ietf-netconf</name>\n"
590 " <revision>2011-06-01</revision>\n"
591 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
592 " <feature>writable-running</feature>\n"
593 " <feature>candidate</feature>\n"
594 " <feature>confirmed-commit</feature>\n"
595 " <feature>rollback-on-error</feature>\n"
596 " <feature>validate</feature>\n"
597 " <feature>startup</feature>\n"
598 " <feature>url</feature>\n"
599 " <feature>xpath</feature>\n"
600 " <conformance-type>implement</conformance-type>\n"
601 " </module>\n"
602 " <module>\n"
603 " <name>ietf-netconf-acm</name>\n"
604 " <revision>2018-02-14</revision>\n"
605 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
606 " <conformance-type>import</conformance-type>\n"
607 " </module>\n"
608 "</modules-state>";
609
610 const char *garbage_revision =
611 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
612 " <module-set>\n"
613 " <name>complete</name>\n"
614 " <module>\n"
615 " <name>yang</name>\n"
616 " <revision>2020-06-17</revision>\n"
617 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
618 " </module>\n"
619 " <module>\n"
620 " <name>ietf-yang-library</name>\n"
621 " <revision>2019-01-01</revision>\n"
622 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
623 " </module>\n"
624 DATA_YANG_BASE_IMPORTS
625 DATA_YANG_SCHEMA_MODULE_STATE
626 "</modules-state>\n";
627
628 const char *no_yanglibrary =
629 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
630 " <module-set>\n"
631 " <name>complete</name>\n"
632 " <module>\n"
633 " <name>yang</name>\n"
634 " <revision>2021-04-07</revision>\n"
635 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
636 " </module>\n"
637 DATA_YANG_BASE_IMPORTS
638 DATA_YANG_SCHEMA_MODULE_STATE
639 "</modules-state>\n";
640
641 (void) state;
642 /* seperate context to avoid double free during teadown */
643 struct ly_ctx *ctx_test = NULL;
644
645 /* test invalid parameters */
646 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
647 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
648 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
649
650 /* basic test with ietf-yang-library-only */
651 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 +0200652 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200653 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200654 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200655
656 /* test loading module, should also import other module */
657 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 +0200658 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200659 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200660 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200661 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
662 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 +0200663 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200664
665 /* test loading module with feature if they are present */
666 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 +0200667 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
668 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200669 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 +0200670 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200671
672 /* test with not matching revision */
673 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
674
675 /* test data containing ietf-yang-library which conflicts with the option */
676 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
677
678 /* test creating without ietf-yang-library */
679 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
680 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200681 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200682}
683
aPiecekbb96b802021-04-12 08:12:52 +0200684static LY_ERR
685check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
686{
687 const struct lysp_node *pnode;
688 const char ***iter;
689
690 pnode = (const struct lysp_node *)node->priv;
691 CHECK_POINTER(pnode, 1);
692 iter = (const char ***)data;
693 CHECK_POINTER(**iter, 1);
694 CHECK_STRING(pnode->name, **iter);
695 (*iter)++;
696
697 return LY_SUCCESS;
698}
699
700static LY_ERR
701check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
702{
703 CHECK_POINTER(node->priv, 0);
704 return LY_SUCCESS;
705}
706
aPiecekfcfec0f2021-04-23 12:47:30 +0200707static void
708check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
709{
710 LY_ARRAY_COUNT_TYPE u, v;
711 struct lysc_ext_substmt *substmts;
712 struct lysc_node *cnode;
713 const char **iter;
714 const char *check[] = {
715 "tmp_cont", "lf", NULL
716 };
717
718 LY_ARRAY_FOR(ext, u) {
719 substmts = ext[u].substmts;
720 LY_ARRAY_FOR(substmts, v) {
721 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
722 cnode = *(struct lysc_node **)substmts[v].storage;
723 iter = check;
724 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
725 }
726 }
727 }
728}
729
730static void
731check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
732{
733 LY_ARRAY_COUNT_TYPE u, v;
734 struct lysc_ext_substmt *substmts;
735 struct lysc_node *cnode;
736
737 LY_ARRAY_FOR(ext, u) {
738 substmts = ext[u].substmts;
739 LY_ARRAY_FOR(substmts, v) {
740 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
741 cnode = *(struct lysc_node **)substmts[v].storage;
742 if (cnode) {
743 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
744 }
745 }
746 }
747 }
748}
749
aPiecekbb96b802021-04-12 08:12:52 +0200750/**
751 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
752 */
753static void
754test_set_priv_parsed(void **state)
755{
756 const struct lys_module *mod;
757 const char *schema_a;
758 const char **iter;
759 const char *check[] = {
760 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
761 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
762 "l2c", "l2cx", "ch", "cas", "casx", "oper",
763 "input", "inparam", "output", "outparam", "n1", NULL
764 };
765
766 /* each node must have a unique name. */
767 schema_a = "module a {\n"
768 " namespace urn:tests:a;\n"
769 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200770 "\n"
771 " import ietf-restconf {\n"
772 " prefix rc;\n"
773 " revision-date 2017-01-26;\n"
774 " }\n"
775 "\n"
776 " rc:yang-data \"tmp\" {\n"
777 " container tmp_cont {\n"
778 " leaf lf {\n"
779 " type string;\n"
780 " }\n"
781 " }\n"
782 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200783 " container cont {\n"
784 " notification contnotif;\n"
785 " leaf-list contx {\n"
786 " type string;\n"
787 " }\n"
788 " uses grp;\n"
789 " }\n"
790 " list l1 {\n"
791 " key \"l1a l1b\";\n"
792 " leaf l1a {\n"
793 " type string;\n"
794 " }\n"
795 " leaf l1b {\n"
796 " type string;\n"
797 " }\n"
798 " leaf l1c {\n"
799 " type string;\n"
800 " }\n"
801 " }\n"
802 " feature f1;\n"
803 " feature f2;\n"
804 " leaf foo1 {\n"
805 " type uint16;\n"
806 " if-feature f1;\n"
807 " }\n"
808 " leaf foo2 {\n"
809 " type uint16;\n"
810 " }\n"
811 " leaf foo3 {\n"
812 " type uint16;\n"
813 " if-feature f2;\n"
814 " }\n"
815 " leaf-list ll {\n"
816 " type string;\n"
817 " }\n"
818 " anydata any {\n"
819 " config false;\n"
820 " }\n"
821 " list l2 {\n"
822 " config false;\n"
823 " container l2c {\n"
824 " leaf l2cx {\n"
825 " type string;\n"
826 " }\n"
827 " }\n"
828 " }\n"
829 " choice ch {\n"
830 " case cas {\n"
831 " leaf casx {\n"
832 " type string;\n"
833 " }\n"
834 " }\n"
835 " }\n"
836 " rpc oper {\n"
837 " input {\n"
838 " leaf inparam {\n"
839 " type string;\n"
840 " }\n"
841 " }\n"
842 " output {\n"
843 " leaf outparam {\n"
844 " type int8;\n"
845 " }\n"
846 " }\n"
847 " }\n"
848 " notification n1;\n"
849 " grouping grp {\n"
850 " leaf grpleaf {\n"
851 " type uint16;\n"
852 " }\n"
853 " }\n"
854 " augment /cont {\n"
855 " leaf augleaf {\n"
856 " type uint16;\n"
857 " }\n"
858 " }\n"
859 " deviation /a:foo2 {\n"
860 " deviate not-supported;\n"
861 " }\n"
862 "}\n";
863
864 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200865 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200866 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200867
aPiecekbb96b802021-04-12 08:12:52 +0200868 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200869 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
870 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200871 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
872
873 print_message("[ ] create context\n");
874 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
875 iter = check;
876 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200877 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200878
879 print_message("[ ] unset option\n");
880 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
881 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
882 iter = check;
883 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200884 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200885
886 print_message("[ ] set option\n");
887 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
888 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
889 iter = check;
890 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200891 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200892}
893
Michal Vasko01db7de2021-04-16 12:23:30 +0200894static void
895test_explicit_compile(void **state)
896{
897 uint32_t i;
898 const struct lys_module *mod;
899 const char *schema_a = "module a {\n"
900 " namespace urn:tests:a;\n"
901 " prefix a;yang-version 1.1;\n"
902 " feature f1;\n"
903 " feature f2;\n"
904 " leaf foo1 {\n"
905 " type uint16;\n"
906 " if-feature f1;\n"
907 " }\n"
908 " leaf foo2 {\n"
909 " type uint16;\n"
910 " }\n"
911 " container cont {\n"
912 " leaf foo3 {\n"
913 " type string;\n"
914 " }\n"
915 " }\n"
916 "}\n";
917 const char *schema_b = "module b {\n"
918 " namespace urn:tests:b;\n"
919 " prefix b;yang-version 1.1;\n"
920 " import a {\n"
921 " prefix a;\n"
922 " }\n"
923 " augment /a:cont {\n"
924 " leaf augleaf {\n"
925 " type uint16;\n"
926 " }\n"
927 " }\n"
928 "}\n";
929 const char *schema_c = "module c {\n"
930 " namespace urn:tests:c;\n"
931 " prefix c;yang-version 1.1;\n"
932 " import a {\n"
933 " prefix a;\n"
934 " }\n"
935 " deviation /a:foo2 {\n"
936 " deviate not-supported;\n"
937 " }\n"
938 "}\n";
939
940 /* use own context with extra flags */
941 ly_ctx_destroy(UTEST_LYCTX);
942 const char *feats[] = {"f1", NULL};
943
944 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
945 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
946 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
947 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
948 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
949
Michal Vaskodd992582021-06-10 14:34:57 +0200950 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +0200951 i = 0;
952 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +0200953 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +0200954 }
955
956 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
957
958 /* check internal modules */
959 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
960 assert_non_null(mod);
961 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
962 assert_non_null(mod);
963 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
964 assert_non_null(mod);
965
966 /* check test modules */
967 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
968 assert_non_null(mod);
969 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
970 assert_non_null(mod);
971 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
972 assert_non_null(mod);
973}
974
Radek Iša56ca9e42020-09-08 18:42:00 +0200975int
976main(void)
977{
978 const struct CMUnitTest tests[] = {
979 UTEST(test_searchdirs),
980 UTEST(test_options),
981 UTEST(test_models),
982 UTEST(test_imports),
983 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200984 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +0200985 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +0200986 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +0200987 };
988
989 return cmocka_run_group_tests(tests, NULL, NULL);
990}