blob: f542bc26f3f98fefd840b3e59093a1ce9172d5b1 [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);
Jan Kundrát34bb9282021-12-13 18:45:50 +0100107 assert_int_equal(LY_SUCCESS,
108 ly_ctx_new(TESTS_SRC PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_SRC,
109 LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
111 assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]);
112 assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]);
113}
114
115static void
116test_options(void **state)
117{
118 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200119 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200120
121 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX));
122
123 /* invalid arguments */
124 assert_int_equal(0, ly_ctx_get_options(NULL));
125 CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL);
126
127 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
128 CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL);
129 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
130 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL);
131
Radek Iša56ca9e42020-09-08 18:42:00 +0200132 /* unset */
133 /* LY_CTX_ALL_IMPLEMENTED */
134 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
135 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
136 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
137
138 /* LY_CTX_REF_IMPLEMENTED */
139 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
140 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
141 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
142
143 /* LY_CTX_DISABLE_SEARCHDIRS */
144 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
145 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
146 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
147
148 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
149 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
150 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
151 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
152
153 /* LY_CTX_PREFER_SEARCHDIRS */
154 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
155 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
156 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
157
158 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
159
160 /* set back */
161 /* LY_CTX_ALL_IMPLEMENTED */
162 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
163 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
164
165 /* LY_CTX_REF_IMPLEMENTED */
166 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
167 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
168
169 /* LY_CTX_DISABLE_SEARCHDIRS */
170 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
171 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
172
173 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
174 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
175 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
176
177 /* LY_CTX_PREFER_SEARCHDIRS */
178 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
179 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
180
181 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
182}
183
184static LY_ERR
185test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
186 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
187 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
188{
189 *module_data = user_data;
190 *format = LYS_IN_YANG;
191 *free_module_data = NULL;
192 return LY_SUCCESS;
193}
194
195static void
196test_models(void **state)
197{
198 struct ly_in *in;
199 const char *str;
200 struct lys_module *mod1, *mod2;
201 struct lys_glob_unres unres = {0};
202
203 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200204 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200205
206 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200207 assert_int_equal(0, ly_ctx_get_change_count(NULL));
208 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200209
210 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200211 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200212
213 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200214 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200215 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 ly_in_free(in, 0);
217 CHECK_LOG_CTX("Invalid schema input format.", NULL);
218
219 /* import callback */
220 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
221 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
222 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
223 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
224 assert_string_equal("test", str);
225
226 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
227 assert_null(UTEST_LYCTX->imp_clb);
228 assert_null(UTEST_LYCTX->imp_clb_data);
229
230 /* name collision of module and submodule */
231 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
232 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 +0200233 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 +0200234 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200236 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
237 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200238
239 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 +0200240 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 +0200241 ly_in_free(in, 0);
242 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200243 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 +0200244 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200246 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
247 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200248
249 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
250 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 +0200251 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 +0200252 lys_unres_glob_revert(UTEST_LYCTX, &unres);
253 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200255 CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL,
256 "Including \"y\" submodule into \"b\" failed.", NULL,
257 "Parsing submodule failed.", NULL,
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 "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 Vasko4de7d072021-07-09 09:13:18 +0200271 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &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 Vasko4de7d072021-07-09 09:13:18 +0200291 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &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{
aPiecekd4911ee2021-07-30 07:40:24 +0200301 struct lys_module *mod1, *mod2, *mod3, *import;
aPiecek9f8c7e72021-07-28 12:01:56 +0200302 char *str;
aPiecekd4911ee2021-07-30 07:40:24 +0200303 uint16_t ctx_options;
Radek Iša56ca9e42020-09-08 18:42:00 +0200304
305 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200306 ly_ctx_destroy(UTEST_LYCTX);
aPiecekd4911ee2021-07-30 07:40:24 +0200307 ctx_options = LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_NO_YANGLIBRARY;
Radek Iša56ca9e42020-09-08 18:42:00 +0200308
aPiecekd4911ee2021-07-30 07:40:24 +0200309 /* Import callback provides newer revision of module 'a',
310 * however the older revision is implemented soon and therefore it is preferred. */
311 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200312 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
313 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
314 LYS_IN_YANG, &mod1));
aPiecek8ca21bd2021-07-26 14:31:01 +0200315 assert_true(LYS_MOD_LATEST_REV & mod1->latest_revision);
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 assert_int_equal(1, mod1->implemented);
317 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
318 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200319 assert_ptr_equal(mod1, mod2->parsed->imports[0].module);
320 assert_true((LYS_MOD_LATEST_REV | LYS_MOD_IMPORTED_REV) & mod1->latest_revision);
321 assert_string_equal("2019-09-16", mod1->revision);
322 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200323 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200324 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200325
aPiecekd4911ee2021-07-30 07:40:24 +0200326 /* Import callback provides older revision of module 'a' and it is
327 * imported by another module, so it is preferred even if newer
328 * revision is implemented later. */
329 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
330 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-16;}");
Radek Iša56ca9e42020-09-08 18:42:00 +0200331 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
332 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200333 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-17;}",
334 LYS_IN_YANG, &mod1));
335 ly_log_level(LY_LLVRB);
336 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c;import a {prefix a;}}",
337 LYS_IN_YANG, &mod3));
338 CHECK_LOG("Implemented module \"a@2019-09-17\" is not used for import, revision \"2019-09-16\" is imported instead.", NULL);
339 ly_log_level(LY_LLWRN);
340 assert_true(LYS_MOD_LATEST_SEARCHDIRS & mod1->latest_revision);
341 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 import = mod2->parsed->imports[0].module;
aPiecekd4911ee2021-07-30 07:40:24 +0200343 assert_true(LYS_MOD_IMPORTED_REV & import->latest_revision);
344 assert_string_equal("2019-09-16", import->revision);
345 assert_int_equal(0, import->implemented);
346 import = mod3->parsed->imports[0].module;
347 assert_string_equal("2019-09-16", import->revision);
348 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
349 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
350 assert_string_equal("2019-09-17", ly_ctx_get_module_implemented(UTEST_LYCTX, "a")->revision);
aPiecek9f8c7e72021-07-28 12:01:56 +0200351 ly_ctx_destroy(UTEST_LYCTX);
352
353 /* check of circular dependency */
aPiecekd4911ee2021-07-30 07:40:24 +0200354 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
aPiecek9f8c7e72021-07-28 12:01:56 +0200355 str = "module a {namespace urn:a; prefix a;"
356 "import b {prefix b;}"
357 "}";
358 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, str);
359 str = "module b { yang-version 1.1; namespace urn:b; prefix b;"
360 "import a {prefix a;}"
361 "}";
362 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200363}
364
365static void
366test_get_models(void **state)
367{
368 struct lys_module *mod, *mod2;
369 const char *str0 = "module a {namespace urn:a;prefix a;}";
370 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
371 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
372 struct ly_in *in0, *in1, *in2;
373 struct lys_glob_unres unres = {0};
374
375 unsigned int index = 0;
376 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
377
378 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
379 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
380 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
381
382 /* invalid arguments */
383 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
384 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
385 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
386 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
387 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
388 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
389 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
390 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
391 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
392
393 /* internal modules */
394 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
395 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
396 assert_non_null(mod);
397 assert_non_null(mod->parsed);
398 assert_string_equal("yang", mod->name);
399 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
400 assert_ptr_equal(mod, mod2);
401 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
402 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
403 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
404 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
405
406 /* select module by revision */
Michal Vasko4de7d072021-07-09 09:13:18 +0200407 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200408 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200409 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 +0200410 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
411 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 +0200412 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200413 ly_in_reset(in1);
414 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200415 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 +0200416 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200417 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200418 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200419 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 +0200420 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200421 assert_non_null(mod2);
422 assert_ptr_not_equal(mod, mod2);
423 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
424 assert_ptr_equal(mod, mod2);
425 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
426 assert_ptr_equal(mod, mod2);
427 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200428 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 +0200429 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200430 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
431 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
432
433 str1 = "submodule b {belongs-to a {prefix a;}}";
434 ly_in_free(in1, 0);
435 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200436 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 +0200437 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200438 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200439
440 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
441 assert_string_equal(names[index - 1], mod->name);
442 }
443 assert_int_equal(9, index);
444
445 /* cleanup */
446 ly_in_free(in0, 0);
447 ly_in_free(in1, 0);
448 ly_in_free(in2, 0);
449}
450
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200451static void
452test_ylmem(void **state)
453{
454#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
455 " <module-set>\n"\
456 " <name>complete</name>\n"\
457 " <module>\n"\
458 " <name>yang</name>\n"\
459 " <revision>2021-04-07</revision>\n"\
460 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
461 " </module>\n"\
462 " <module>\n"\
463 " <name>ietf-yang-library</name>\n"\
464 " <revision>2019-01-04</revision>\n"\
465 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
466 " </module>\n"
467
468#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
469 " <name>ietf-yang-metadata</name>\n"\
470 " <revision>2016-08-05</revision>\n"\
471 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
472 " </import-only-module>\n"\
473 " <import-only-module>\n"\
474 " <name>ietf-inet-types</name>\n"\
475 " <revision>2013-07-15</revision>\n"\
476 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
477 " </import-only-module>\n"\
478 " <import-only-module>\n"\
479 " <name>ietf-yang-types</name>\n"\
480 " <revision>2013-07-15</revision>\n"\
481 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
482 " </import-only-module>\n"\
483 " <import-only-module>\n"\
484 " <name>ietf-datastores</name>\n"\
485 " <revision>2018-02-14</revision>\n"\
486 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
487 " </import-only-module>\n"
488
489#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
490 " <schema>\n"\
491 " <name>complete</name>\n"\
492 " <module-set>complete</module-set>\n"\
493 " </schema>\n"\
494 " <content-id>9</content-id>\n"\
495 "</yang-library>\n"\
496 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
497 " <module-set-id>12</module-set-id>\n"\
498 " <module>\n"\
499 " <name>ietf-yang-metadata</name>\n"\
500 " <revision>2016-08-05</revision>\n"\
501 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
502 " <conformance-type>import</conformance-type>\n"\
503 " </module>\n"\
504 " <module>\n"\
505 " <name>yang</name>\n"\
506 " <revision>2020-06-17</revision>\n"\
507 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
508 " <conformance-type>implement</conformance-type>\n"\
509 " </module>\n"\
510 " <module>\n"\
511 " <name>ietf-inet-types</name>\n"\
512 " <revision>2013-07-15</revision>\n"\
513 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
514 " <conformance-type>import</conformance-type>\n"\
515 " </module>\n"\
516 " <module>\n"\
517 " <name>ietf-yang-types</name>\n"\
518 " <revision>2013-07-15</revision>\n"\
519 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
520 " <conformance-type>import</conformance-type>\n"\
521 " </module>\n"\
522 " <module>\n"\
523 " <name>ietf-yang-library</name>\n"\
524 " <revision>2019-01-04</revision>\n"\
525 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
526 " <conformance-type>implement</conformance-type>\n"\
527 " </module>\n"\
528 " <module>\n"\
529 " <name>ietf-datastores</name>\n"\
530 " <revision>2018-02-14</revision>\n"\
531 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
532 " <conformance-type>import</conformance-type>\n"\
533 " </module>\n"
534
535 const char *yanglibrary_only =
536 DATA_YANG_LIBRARY_START
537 DATA_YANG_BASE_IMPORTS
538 DATA_YANG_SCHEMA_MODULE_STATE
539 "</modules-state>\n";
540
541 const char *with_netconf =
542 DATA_YANG_LIBRARY_START
543 " <module>\n"
544 " <name>ietf-netconf</name>\n"
545 " <revision>2011-06-01</revision>\n"
546 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
547 " </module>\n"
548 DATA_YANG_BASE_IMPORTS
549 " <import-only-module>\n"
550 " <name>ietf-netconf-acm</name>\n"
551 " <revision>2018-02-14</revision>\n"
552 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
553 " </import-only-module>\n"
554 DATA_YANG_SCHEMA_MODULE_STATE
555 " <module>\n"
556 " <name>ietf-netconf</name>\n"
557 " <revision>2011-06-01</revision>\n"
558 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
559 " <conformance-type>implement</conformance-type>\n"
560 " </module>\n"
561 " <module>\n"
562 " <name>ietf-netconf-acm</name>\n"
563 " <revision>2018-02-14</revision>\n"
564 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
565 " <conformance-type>import</conformance-type>\n"
566 " </module>\n"
567 "</modules-state>";
568
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200569 char *with_netconf_features = malloc(8096);
570 strcpy(with_netconf_features,
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200571 DATA_YANG_LIBRARY_START
572 " <module>\n"
573 " <name>ietf-netconf</name>\n"
574 " <revision>2011-06-01</revision>\n"
575 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
576 " <feature>writable-running</feature>\n"
577 " <feature>candidate</feature>\n"
578 " <feature>confirmed-commit</feature>\n"
579 " <feature>rollback-on-error</feature>\n"
580 " <feature>validate</feature>\n"
581 " <feature>startup</feature>\n"
582 " <feature>url</feature>\n"
583 " <feature>xpath</feature>\n"
584 " </module>\n"
585 " <import-only-module>\n"
586 " <name>ietf-yang-metadata</name>\n"
587 " <revision>2016-08-05</revision>\n"
588 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
589 " </import-only-module>\n"
590 " <import-only-module>\n"
591 " <name>ietf-inet-types</name>\n"
592 " <revision>2013-07-15</revision>\n"
593 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
594 " </import-only-module>\n"
595 " <import-only-module>\n"
596 " <name>ietf-yang-types</name>\n"
597 " <revision>2013-07-15</revision>\n"
598 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
599 " </import-only-module>\n"
600 " <import-only-module>\n"
601 " <name>ietf-datastores</name>\n"
602 " <revision>2018-02-14</revision>\n"
603 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
604 " </import-only-module>\n"
605 " <import-only-module>\n"
606 " <name>ietf-netconf-acm</name>\n"
607 " <revision>2018-02-14</revision>\n"
608 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200609 " </import-only-module>\n");
610 strcpy(with_netconf_features + strlen(with_netconf_features),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200611 DATA_YANG_SCHEMA_MODULE_STATE
612 " <module>\n"
613 " <name>ietf-netconf</name>\n"
614 " <revision>2011-06-01</revision>\n"
615 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
616 " <feature>writable-running</feature>\n"
617 " <feature>candidate</feature>\n"
618 " <feature>confirmed-commit</feature>\n"
619 " <feature>rollback-on-error</feature>\n"
620 " <feature>validate</feature>\n"
621 " <feature>startup</feature>\n"
622 " <feature>url</feature>\n"
623 " <feature>xpath</feature>\n"
624 " <conformance-type>implement</conformance-type>\n"
625 " </module>\n"
626 " <module>\n"
627 " <name>ietf-netconf-acm</name>\n"
628 " <revision>2018-02-14</revision>\n"
629 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
630 " <conformance-type>import</conformance-type>\n"
631 " </module>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200632 "</modules-state>");
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200633
634 const char *garbage_revision =
635 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
636 " <module-set>\n"
637 " <name>complete</name>\n"
638 " <module>\n"
639 " <name>yang</name>\n"
640 " <revision>2020-06-17</revision>\n"
641 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
642 " </module>\n"
643 " <module>\n"
644 " <name>ietf-yang-library</name>\n"
645 " <revision>2019-01-01</revision>\n"
646 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
647 " </module>\n"
648 DATA_YANG_BASE_IMPORTS
649 DATA_YANG_SCHEMA_MODULE_STATE
650 "</modules-state>\n";
651
652 const char *no_yanglibrary =
653 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
654 " <module-set>\n"
655 " <name>complete</name>\n"
656 " <module>\n"
657 " <name>yang</name>\n"
658 " <revision>2021-04-07</revision>\n"
659 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
660 " </module>\n"
661 DATA_YANG_BASE_IMPORTS
662 DATA_YANG_SCHEMA_MODULE_STATE
663 "</modules-state>\n";
664
665 (void) state;
666 /* seperate context to avoid double free during teadown */
667 struct ly_ctx *ctx_test = NULL;
668
669 /* test invalid parameters */
670 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
671 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
672 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
673
674 /* basic test with ietf-yang-library-only */
675 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 +0200676 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200677 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200678 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200679
680 /* test loading module, should also import other module */
681 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 +0200682 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200683 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200684 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200685 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
686 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 +0200687 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200688
689 /* test loading module with feature if they are present */
690 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 +0200691 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
692 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200693 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 +0200694 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200695
696 /* test with not matching revision */
697 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
698
699 /* test data containing ietf-yang-library which conflicts with the option */
700 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
701
702 /* test creating without ietf-yang-library */
703 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
704 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200705 ly_ctx_destroy(ctx_test);
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200706 free(with_netconf_features);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200707}
708
aPiecekbb96b802021-04-12 08:12:52 +0200709static LY_ERR
710check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
711{
712 const struct lysp_node *pnode;
713 const char ***iter;
714
715 pnode = (const struct lysp_node *)node->priv;
716 CHECK_POINTER(pnode, 1);
717 iter = (const char ***)data;
718 CHECK_POINTER(**iter, 1);
719 CHECK_STRING(pnode->name, **iter);
720 (*iter)++;
721
722 return LY_SUCCESS;
723}
724
725static LY_ERR
726check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
727{
728 CHECK_POINTER(node->priv, 0);
729 return LY_SUCCESS;
730}
731
aPiecekfcfec0f2021-04-23 12:47:30 +0200732static void
733check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
734{
735 LY_ARRAY_COUNT_TYPE u, v;
736 struct lysc_ext_substmt *substmts;
737 struct lysc_node *cnode;
738 const char **iter;
739 const char *check[] = {
740 "tmp_cont", "lf", NULL
741 };
742
743 LY_ARRAY_FOR(ext, u) {
744 substmts = ext[u].substmts;
745 LY_ARRAY_FOR(substmts, v) {
746 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
747 cnode = *(struct lysc_node **)substmts[v].storage;
748 iter = check;
749 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
750 }
751 }
752 }
753}
754
755static void
756check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
757{
758 LY_ARRAY_COUNT_TYPE u, v;
759 struct lysc_ext_substmt *substmts;
760 struct lysc_node *cnode;
761
762 LY_ARRAY_FOR(ext, u) {
763 substmts = ext[u].substmts;
764 LY_ARRAY_FOR(substmts, v) {
765 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
766 cnode = *(struct lysc_node **)substmts[v].storage;
767 if (cnode) {
768 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
769 }
770 }
771 }
772 }
773}
774
aPiecekbb96b802021-04-12 08:12:52 +0200775/**
776 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
777 */
778static void
779test_set_priv_parsed(void **state)
780{
Michal Vasko4de7d072021-07-09 09:13:18 +0200781 struct lys_module *mod;
aPiecekbb96b802021-04-12 08:12:52 +0200782 const char *schema_a;
783 const char **iter;
784 const char *check[] = {
785 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
786 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
787 "l2c", "l2cx", "ch", "cas", "casx", "oper",
788 "input", "inparam", "output", "outparam", "n1", NULL
789 };
790
791 /* each node must have a unique name. */
792 schema_a = "module a {\n"
793 " namespace urn:tests:a;\n"
794 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200795 "\n"
796 " import ietf-restconf {\n"
797 " prefix rc;\n"
798 " revision-date 2017-01-26;\n"
799 " }\n"
800 "\n"
801 " rc:yang-data \"tmp\" {\n"
802 " container tmp_cont {\n"
803 " leaf lf {\n"
804 " type string;\n"
805 " }\n"
806 " }\n"
807 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200808 " container cont {\n"
809 " notification contnotif;\n"
810 " leaf-list contx {\n"
811 " type string;\n"
812 " }\n"
813 " uses grp;\n"
814 " }\n"
815 " list l1 {\n"
816 " key \"l1a l1b\";\n"
817 " leaf l1a {\n"
818 " type string;\n"
819 " }\n"
820 " leaf l1b {\n"
821 " type string;\n"
822 " }\n"
823 " leaf l1c {\n"
824 " type string;\n"
825 " }\n"
826 " }\n"
827 " feature f1;\n"
828 " feature f2;\n"
829 " leaf foo1 {\n"
830 " type uint16;\n"
831 " if-feature f1;\n"
832 " }\n"
833 " leaf foo2 {\n"
834 " type uint16;\n"
835 " }\n"
836 " leaf foo3 {\n"
837 " type uint16;\n"
838 " if-feature f2;\n"
839 " }\n"
840 " leaf-list ll {\n"
841 " type string;\n"
842 " }\n"
843 " anydata any {\n"
844 " config false;\n"
845 " }\n"
846 " list l2 {\n"
847 " config false;\n"
848 " container l2c {\n"
849 " leaf l2cx {\n"
850 " type string;\n"
851 " }\n"
852 " }\n"
853 " }\n"
854 " choice ch {\n"
855 " case cas {\n"
856 " leaf casx {\n"
857 " type string;\n"
858 " }\n"
859 " }\n"
860 " }\n"
861 " rpc oper {\n"
862 " input {\n"
863 " leaf inparam {\n"
864 " type string;\n"
865 " }\n"
866 " }\n"
867 " output {\n"
868 " leaf outparam {\n"
869 " type int8;\n"
870 " }\n"
871 " }\n"
872 " }\n"
873 " notification n1;\n"
874 " grouping grp {\n"
875 " leaf grpleaf {\n"
876 " type uint16;\n"
877 " }\n"
878 " }\n"
879 " augment /cont {\n"
880 " leaf augleaf {\n"
881 " type uint16;\n"
882 " }\n"
883 " }\n"
884 " deviation /a:foo2 {\n"
885 " deviate not-supported;\n"
886 " }\n"
887 "}\n";
888
889 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200890 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200891 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200892
aPiecekbb96b802021-04-12 08:12:52 +0200893 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200894 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
895 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200896 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
897
898 print_message("[ ] create context\n");
899 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
900 iter = check;
901 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200902 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200903
904 print_message("[ ] unset option\n");
905 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
906 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
907 iter = check;
908 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200909 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200910
911 print_message("[ ] set option\n");
912 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
913 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
914 iter = check;
915 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200916 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200917}
918
Michal Vasko01db7de2021-04-16 12:23:30 +0200919static void
920test_explicit_compile(void **state)
921{
922 uint32_t i;
Michal Vasko4de7d072021-07-09 09:13:18 +0200923 struct lys_module *mod;
Michal Vasko01db7de2021-04-16 12:23:30 +0200924 const char *schema_a = "module a {\n"
925 " namespace urn:tests:a;\n"
926 " prefix a;yang-version 1.1;\n"
927 " feature f1;\n"
928 " feature f2;\n"
929 " leaf foo1 {\n"
930 " type uint16;\n"
931 " if-feature f1;\n"
932 " }\n"
933 " leaf foo2 {\n"
934 " type uint16;\n"
935 " }\n"
936 " container cont {\n"
937 " leaf foo3 {\n"
938 " type string;\n"
939 " }\n"
940 " }\n"
941 "}\n";
942 const char *schema_b = "module b {\n"
943 " namespace urn:tests:b;\n"
944 " prefix b;yang-version 1.1;\n"
945 " import a {\n"
946 " prefix a;\n"
947 " }\n"
948 " augment /a:cont {\n"
949 " leaf augleaf {\n"
950 " type uint16;\n"
951 " }\n"
952 " }\n"
953 "}\n";
954 const char *schema_c = "module c {\n"
955 " namespace urn:tests:c;\n"
956 " prefix c;yang-version 1.1;\n"
957 " import a {\n"
958 " prefix a;\n"
959 " }\n"
960 " deviation /a:foo2 {\n"
961 " deviate not-supported;\n"
962 " }\n"
963 "}\n";
964
965 /* use own context with extra flags */
966 ly_ctx_destroy(UTEST_LYCTX);
967 const char *feats[] = {"f1", NULL};
968
969 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
970 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
971 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
972 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
973 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
974
Michal Vaskodd992582021-06-10 14:34:57 +0200975 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +0200976 i = 0;
977 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +0200978 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +0200979 }
980
981 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
982
983 /* check internal modules */
984 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
985 assert_non_null(mod);
986 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
987 assert_non_null(mod);
988 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
989 assert_non_null(mod);
990
991 /* check test modules */
992 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
993 assert_non_null(mod);
994 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
995 assert_non_null(mod);
996 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
997 assert_non_null(mod);
998}
999
Radek Iša56ca9e42020-09-08 18:42:00 +02001000int
1001main(void)
1002{
1003 const struct CMUnitTest tests[] = {
1004 UTEST(test_searchdirs),
1005 UTEST(test_options),
1006 UTEST(test_models),
1007 UTEST(test_imports),
1008 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +02001009 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +02001010 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +02001011 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +02001012 };
1013
1014 return cmocka_run_group_tests(tests, NULL, NULL);
1015}