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