blob: 9ec92035ce0f463dbcb5a4daf94bbaad421038ce [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
Radek Iša56ca9e42020-09-08 18:42:00 +0200130 /* unset */
131 /* LY_CTX_ALL_IMPLEMENTED */
132 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
133 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
134 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
135
136 /* LY_CTX_REF_IMPLEMENTED */
137 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
138 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
139 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
140
141 /* LY_CTX_DISABLE_SEARCHDIRS */
142 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
143 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
144 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
145
146 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
147 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
148 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
149 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
150
151 /* LY_CTX_PREFER_SEARCHDIRS */
152 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
153 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
154 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
155
156 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
157
158 /* set back */
159 /* LY_CTX_ALL_IMPLEMENTED */
160 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
161 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
162
163 /* LY_CTX_REF_IMPLEMENTED */
164 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
165 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
166
167 /* LY_CTX_DISABLE_SEARCHDIRS */
168 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
169 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
170
171 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
172 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
173 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
174
175 /* LY_CTX_PREFER_SEARCHDIRS */
176 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
177 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
178
179 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
180}
181
182static LY_ERR
183test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
184 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
185 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
186{
187 *module_data = user_data;
188 *format = LYS_IN_YANG;
189 *free_module_data = NULL;
190 return LY_SUCCESS;
191}
192
193static void
194test_models(void **state)
195{
196 struct ly_in *in;
197 const char *str;
198 struct lys_module *mod1, *mod2;
199 struct lys_glob_unres unres = {0};
200
201 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200202 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200203
204 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200205 assert_int_equal(0, ly_ctx_get_change_count(NULL));
206 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200207
208 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200209 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200210
211 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200212 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200213 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 ly_in_free(in, 0);
215 CHECK_LOG_CTX("Invalid schema input format.", NULL);
216
217 /* import callback */
218 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
219 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
220 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
221 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
222 assert_string_equal("test", str);
223
224 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
225 assert_null(UTEST_LYCTX->imp_clb);
226 assert_null(UTEST_LYCTX->imp_clb_data);
227
228 /* name collision of module and submodule */
229 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
230 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 +0200231 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 +0200232 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200233 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200234 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
235 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200236
237 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 +0200238 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 +0200239 ly_in_free(in, 0);
240 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200241 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 +0200242 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200244 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
245 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200246
247 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
248 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 +0200249 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 +0200250 lys_unres_glob_revert(UTEST_LYCTX, &unres);
251 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200253 CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL,
254 "Including \"y\" submodule into \"b\" failed.", NULL,
255 "Parsing submodule failed.", NULL,
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 "Name collision between submodules of name \"y\".", "Line number 1.");
257
258 /* selecting correct revision of the submodules */
259 ly_ctx_reset_latests(UTEST_LYCTX);
260 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
261 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 +0200262 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 +0200263 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 ly_in_free(in, 0);
265 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
266
267 /* reloading module in case only the compiled module resists in the context */
268 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 +0200269 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200270 ly_in_free(in, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200271 assert_non_null(mod1->compiled);
272 assert_non_null(mod1->parsed);
273
274#if 0
275 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
276
277 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));
278 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
279 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
280 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
281 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
282 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
283 assert_null(mod2);
284 ly_in_free(in, 0);
285#endif
286
287 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));
288 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 +0200289 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200290 ly_in_free(in, 0);
291 assert_non_null(mod2);
292 assert_non_null(mod1->parsed);
293 assert_string_equal("w", mod1->name);
294}
295
296static void
297test_imports(void **state)
298{
aPiecekd4911ee2021-07-30 07:40:24 +0200299 struct lys_module *mod1, *mod2, *mod3, *import;
aPiecek9f8c7e72021-07-28 12:01:56 +0200300 char *str;
aPiecekd4911ee2021-07-30 07:40:24 +0200301 uint16_t ctx_options;
Radek Iša56ca9e42020-09-08 18:42:00 +0200302
303 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200304 ly_ctx_destroy(UTEST_LYCTX);
aPiecekd4911ee2021-07-30 07:40:24 +0200305 ctx_options = LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_NO_YANGLIBRARY;
Radek Iša56ca9e42020-09-08 18:42:00 +0200306
aPiecekd4911ee2021-07-30 07:40:24 +0200307 /* Import callback provides newer revision of module 'a',
308 * however the older revision is implemented soon and therefore it is preferred. */
309 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200310 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
311 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
312 LYS_IN_YANG, &mod1));
aPiecek8ca21bd2021-07-26 14:31:01 +0200313 assert_true(LYS_MOD_LATEST_REV & mod1->latest_revision);
Radek Iša56ca9e42020-09-08 18:42:00 +0200314 assert_int_equal(1, mod1->implemented);
315 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
316 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200317 assert_ptr_equal(mod1, mod2->parsed->imports[0].module);
318 assert_true((LYS_MOD_LATEST_REV | LYS_MOD_IMPORTED_REV) & mod1->latest_revision);
319 assert_string_equal("2019-09-16", mod1->revision);
320 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200321 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200322 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200323
aPiecekd4911ee2021-07-30 07:40:24 +0200324 /* Import callback provides older revision of module 'a' and it is
325 * imported by another module, so it is preferred even if newer
326 * revision is implemented later. */
327 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
328 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 +0200329 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
330 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200331 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-17;}",
332 LYS_IN_YANG, &mod1));
333 ly_log_level(LY_LLVRB);
334 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c;import a {prefix a;}}",
335 LYS_IN_YANG, &mod3));
336 CHECK_LOG("Implemented module \"a@2019-09-17\" is not used for import, revision \"2019-09-16\" is imported instead.", NULL);
337 ly_log_level(LY_LLWRN);
338 assert_true(LYS_MOD_LATEST_SEARCHDIRS & mod1->latest_revision);
339 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 import = mod2->parsed->imports[0].module;
aPiecekd4911ee2021-07-30 07:40:24 +0200341 assert_true(LYS_MOD_IMPORTED_REV & import->latest_revision);
342 assert_string_equal("2019-09-16", import->revision);
343 assert_int_equal(0, import->implemented);
344 import = mod3->parsed->imports[0].module;
345 assert_string_equal("2019-09-16", import->revision);
346 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
347 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
348 assert_string_equal("2019-09-17", ly_ctx_get_module_implemented(UTEST_LYCTX, "a")->revision);
aPiecek9f8c7e72021-07-28 12:01:56 +0200349 ly_ctx_destroy(UTEST_LYCTX);
350
351 /* check of circular dependency */
aPiecekd4911ee2021-07-30 07:40:24 +0200352 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
aPiecek9f8c7e72021-07-28 12:01:56 +0200353 str = "module a {namespace urn:a; prefix a;"
354 "import b {prefix b;}"
355 "}";
356 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, str);
357 str = "module b { yang-version 1.1; namespace urn:b; prefix b;"
358 "import a {prefix a;}"
359 "}";
360 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200361}
362
363static void
364test_get_models(void **state)
365{
366 struct lys_module *mod, *mod2;
367 const char *str0 = "module a {namespace urn:a;prefix a;}";
368 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
369 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
370 struct ly_in *in0, *in1, *in2;
371 struct lys_glob_unres unres = {0};
372
373 unsigned int index = 0;
374 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
375
376 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
377 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
378 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
379
380 /* invalid arguments */
381 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
382 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
383 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
384 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
385 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
386 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
387 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
388 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
389 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
390
391 /* internal modules */
392 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
393 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
394 assert_non_null(mod);
395 assert_non_null(mod->parsed);
396 assert_string_equal("yang", mod->name);
397 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
398 assert_ptr_equal(mod, mod2);
399 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
400 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
401 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
402 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
403
404 /* select module by revision */
Michal Vasko4de7d072021-07-09 09:13:18 +0200405 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200406 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200407 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 +0200408 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
409 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 +0200410 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200411 ly_in_reset(in1);
412 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200413 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 +0200414 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200415 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200416 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200417 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 +0200418 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200419 assert_non_null(mod2);
420 assert_ptr_not_equal(mod, mod2);
421 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
422 assert_ptr_equal(mod, mod2);
423 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
424 assert_ptr_equal(mod, mod2);
425 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200426 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 +0200427 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200428 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
429 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
430
431 str1 = "submodule b {belongs-to a {prefix a;}}";
432 ly_in_free(in1, 0);
433 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200434 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 +0200435 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200436 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200437
438 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
439 assert_string_equal(names[index - 1], mod->name);
440 }
441 assert_int_equal(9, index);
442
443 /* cleanup */
444 ly_in_free(in0, 0);
445 ly_in_free(in1, 0);
446 ly_in_free(in2, 0);
447}
448
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200449static void
450test_ylmem(void **state)
451{
452#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
453 " <module-set>\n"\
454 " <name>complete</name>\n"\
455 " <module>\n"\
456 " <name>yang</name>\n"\
457 " <revision>2021-04-07</revision>\n"\
458 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
459 " </module>\n"\
460 " <module>\n"\
461 " <name>ietf-yang-library</name>\n"\
462 " <revision>2019-01-04</revision>\n"\
463 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
464 " </module>\n"
465
466#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
467 " <name>ietf-yang-metadata</name>\n"\
468 " <revision>2016-08-05</revision>\n"\
469 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
470 " </import-only-module>\n"\
471 " <import-only-module>\n"\
472 " <name>ietf-inet-types</name>\n"\
473 " <revision>2013-07-15</revision>\n"\
474 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
475 " </import-only-module>\n"\
476 " <import-only-module>\n"\
477 " <name>ietf-yang-types</name>\n"\
478 " <revision>2013-07-15</revision>\n"\
479 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
480 " </import-only-module>\n"\
481 " <import-only-module>\n"\
482 " <name>ietf-datastores</name>\n"\
483 " <revision>2018-02-14</revision>\n"\
484 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
485 " </import-only-module>\n"
486
487#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
488 " <schema>\n"\
489 " <name>complete</name>\n"\
490 " <module-set>complete</module-set>\n"\
491 " </schema>\n"\
492 " <content-id>9</content-id>\n"\
493 "</yang-library>\n"\
494 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
495 " <module-set-id>12</module-set-id>\n"\
496 " <module>\n"\
497 " <name>ietf-yang-metadata</name>\n"\
498 " <revision>2016-08-05</revision>\n"\
499 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
500 " <conformance-type>import</conformance-type>\n"\
501 " </module>\n"\
502 " <module>\n"\
503 " <name>yang</name>\n"\
504 " <revision>2020-06-17</revision>\n"\
505 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
506 " <conformance-type>implement</conformance-type>\n"\
507 " </module>\n"\
508 " <module>\n"\
509 " <name>ietf-inet-types</name>\n"\
510 " <revision>2013-07-15</revision>\n"\
511 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
512 " <conformance-type>import</conformance-type>\n"\
513 " </module>\n"\
514 " <module>\n"\
515 " <name>ietf-yang-types</name>\n"\
516 " <revision>2013-07-15</revision>\n"\
517 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
518 " <conformance-type>import</conformance-type>\n"\
519 " </module>\n"\
520 " <module>\n"\
521 " <name>ietf-yang-library</name>\n"\
522 " <revision>2019-01-04</revision>\n"\
523 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
524 " <conformance-type>implement</conformance-type>\n"\
525 " </module>\n"\
526 " <module>\n"\
527 " <name>ietf-datastores</name>\n"\
528 " <revision>2018-02-14</revision>\n"\
529 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
530 " <conformance-type>import</conformance-type>\n"\
531 " </module>\n"
532
533 const char *yanglibrary_only =
534 DATA_YANG_LIBRARY_START
535 DATA_YANG_BASE_IMPORTS
536 DATA_YANG_SCHEMA_MODULE_STATE
537 "</modules-state>\n";
538
539 const char *with_netconf =
540 DATA_YANG_LIBRARY_START
541 " <module>\n"
542 " <name>ietf-netconf</name>\n"
543 " <revision>2011-06-01</revision>\n"
544 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
545 " </module>\n"
546 DATA_YANG_BASE_IMPORTS
547 " <import-only-module>\n"
548 " <name>ietf-netconf-acm</name>\n"
549 " <revision>2018-02-14</revision>\n"
550 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
551 " </import-only-module>\n"
552 DATA_YANG_SCHEMA_MODULE_STATE
553 " <module>\n"
554 " <name>ietf-netconf</name>\n"
555 " <revision>2011-06-01</revision>\n"
556 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
557 " <conformance-type>implement</conformance-type>\n"
558 " </module>\n"
559 " <module>\n"
560 " <name>ietf-netconf-acm</name>\n"
561 " <revision>2018-02-14</revision>\n"
562 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
563 " <conformance-type>import</conformance-type>\n"
564 " </module>\n"
565 "</modules-state>";
566
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200567 char *with_netconf_features = malloc(8096);
568 strcpy(with_netconf_features,
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200569 DATA_YANG_LIBRARY_START
570 " <module>\n"
571 " <name>ietf-netconf</name>\n"
572 " <revision>2011-06-01</revision>\n"
573 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
574 " <feature>writable-running</feature>\n"
575 " <feature>candidate</feature>\n"
576 " <feature>confirmed-commit</feature>\n"
577 " <feature>rollback-on-error</feature>\n"
578 " <feature>validate</feature>\n"
579 " <feature>startup</feature>\n"
580 " <feature>url</feature>\n"
581 " <feature>xpath</feature>\n"
582 " </module>\n"
583 " <import-only-module>\n"
584 " <name>ietf-yang-metadata</name>\n"
585 " <revision>2016-08-05</revision>\n"
586 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
587 " </import-only-module>\n"
588 " <import-only-module>\n"
589 " <name>ietf-inet-types</name>\n"
590 " <revision>2013-07-15</revision>\n"
591 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
592 " </import-only-module>\n"
593 " <import-only-module>\n"
594 " <name>ietf-yang-types</name>\n"
595 " <revision>2013-07-15</revision>\n"
596 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
597 " </import-only-module>\n"
598 " <import-only-module>\n"
599 " <name>ietf-datastores</name>\n"
600 " <revision>2018-02-14</revision>\n"
601 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
602 " </import-only-module>\n"
603 " <import-only-module>\n"
604 " <name>ietf-netconf-acm</name>\n"
605 " <revision>2018-02-14</revision>\n"
606 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200607 " </import-only-module>\n");
608 strcpy(with_netconf_features + strlen(with_netconf_features),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200609 DATA_YANG_SCHEMA_MODULE_STATE
610 " <module>\n"
611 " <name>ietf-netconf</name>\n"
612 " <revision>2011-06-01</revision>\n"
613 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
614 " <feature>writable-running</feature>\n"
615 " <feature>candidate</feature>\n"
616 " <feature>confirmed-commit</feature>\n"
617 " <feature>rollback-on-error</feature>\n"
618 " <feature>validate</feature>\n"
619 " <feature>startup</feature>\n"
620 " <feature>url</feature>\n"
621 " <feature>xpath</feature>\n"
622 " <conformance-type>implement</conformance-type>\n"
623 " </module>\n"
624 " <module>\n"
625 " <name>ietf-netconf-acm</name>\n"
626 " <revision>2018-02-14</revision>\n"
627 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
628 " <conformance-type>import</conformance-type>\n"
629 " </module>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200630 "</modules-state>");
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200631
632 const char *garbage_revision =
633 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
634 " <module-set>\n"
635 " <name>complete</name>\n"
636 " <module>\n"
637 " <name>yang</name>\n"
638 " <revision>2020-06-17</revision>\n"
639 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
640 " </module>\n"
641 " <module>\n"
642 " <name>ietf-yang-library</name>\n"
643 " <revision>2019-01-01</revision>\n"
644 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
645 " </module>\n"
646 DATA_YANG_BASE_IMPORTS
647 DATA_YANG_SCHEMA_MODULE_STATE
648 "</modules-state>\n";
649
650 const char *no_yanglibrary =
651 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
652 " <module-set>\n"
653 " <name>complete</name>\n"
654 " <module>\n"
655 " <name>yang</name>\n"
656 " <revision>2021-04-07</revision>\n"
657 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
658 " </module>\n"
659 DATA_YANG_BASE_IMPORTS
660 DATA_YANG_SCHEMA_MODULE_STATE
661 "</modules-state>\n";
662
663 (void) state;
664 /* seperate context to avoid double free during teadown */
665 struct ly_ctx *ctx_test = NULL;
666
667 /* test invalid parameters */
668 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
669 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
670 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
671
672 /* basic test with ietf-yang-library-only */
673 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 +0200674 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200675 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
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, should also import other module */
679 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 +0200680 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200681 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200682 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200683 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
684 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 +0200685 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200686
687 /* test loading module with feature if they are present */
688 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 +0200689 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
690 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200691 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 +0200692 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200693
694 /* test with not matching revision */
695 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
696
697 /* test data containing ietf-yang-library which conflicts with the option */
698 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
699
700 /* test creating without ietf-yang-library */
701 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
702 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200703 ly_ctx_destroy(ctx_test);
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200704 free(with_netconf_features);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200705}
706
aPiecekbb96b802021-04-12 08:12:52 +0200707static LY_ERR
708check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
709{
710 const struct lysp_node *pnode;
711 const char ***iter;
712
713 pnode = (const struct lysp_node *)node->priv;
714 CHECK_POINTER(pnode, 1);
715 iter = (const char ***)data;
716 CHECK_POINTER(**iter, 1);
717 CHECK_STRING(pnode->name, **iter);
718 (*iter)++;
719
720 return LY_SUCCESS;
721}
722
723static LY_ERR
724check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
725{
726 CHECK_POINTER(node->priv, 0);
727 return LY_SUCCESS;
728}
729
aPiecekfcfec0f2021-04-23 12:47:30 +0200730static void
731check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
732{
733 LY_ARRAY_COUNT_TYPE u, v;
734 struct lysc_ext_substmt *substmts;
735 struct lysc_node *cnode;
736 const char **iter;
737 const char *check[] = {
738 "tmp_cont", "lf", NULL
739 };
740
741 LY_ARRAY_FOR(ext, u) {
742 substmts = ext[u].substmts;
743 LY_ARRAY_FOR(substmts, v) {
744 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
745 cnode = *(struct lysc_node **)substmts[v].storage;
746 iter = check;
747 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
748 }
749 }
750 }
751}
752
753static void
754check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
755{
756 LY_ARRAY_COUNT_TYPE u, v;
757 struct lysc_ext_substmt *substmts;
758 struct lysc_node *cnode;
759
760 LY_ARRAY_FOR(ext, u) {
761 substmts = ext[u].substmts;
762 LY_ARRAY_FOR(substmts, v) {
763 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
764 cnode = *(struct lysc_node **)substmts[v].storage;
765 if (cnode) {
766 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
767 }
768 }
769 }
770 }
771}
772
aPiecekbb96b802021-04-12 08:12:52 +0200773/**
774 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
775 */
776static void
777test_set_priv_parsed(void **state)
778{
Michal Vasko4de7d072021-07-09 09:13:18 +0200779 struct lys_module *mod;
aPiecekbb96b802021-04-12 08:12:52 +0200780 const char *schema_a;
781 const char **iter;
782 const char *check[] = {
783 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
784 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
785 "l2c", "l2cx", "ch", "cas", "casx", "oper",
786 "input", "inparam", "output", "outparam", "n1", NULL
787 };
788
789 /* each node must have a unique name. */
790 schema_a = "module a {\n"
791 " namespace urn:tests:a;\n"
792 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200793 "\n"
794 " import ietf-restconf {\n"
795 " prefix rc;\n"
796 " revision-date 2017-01-26;\n"
797 " }\n"
798 "\n"
799 " rc:yang-data \"tmp\" {\n"
800 " container tmp_cont {\n"
801 " leaf lf {\n"
802 " type string;\n"
803 " }\n"
804 " }\n"
805 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200806 " container cont {\n"
807 " notification contnotif;\n"
808 " leaf-list contx {\n"
809 " type string;\n"
810 " }\n"
811 " uses grp;\n"
812 " }\n"
813 " list l1 {\n"
814 " key \"l1a l1b\";\n"
815 " leaf l1a {\n"
816 " type string;\n"
817 " }\n"
818 " leaf l1b {\n"
819 " type string;\n"
820 " }\n"
821 " leaf l1c {\n"
822 " type string;\n"
823 " }\n"
824 " }\n"
825 " feature f1;\n"
826 " feature f2;\n"
827 " leaf foo1 {\n"
828 " type uint16;\n"
829 " if-feature f1;\n"
830 " }\n"
831 " leaf foo2 {\n"
832 " type uint16;\n"
833 " }\n"
834 " leaf foo3 {\n"
835 " type uint16;\n"
836 " if-feature f2;\n"
837 " }\n"
838 " leaf-list ll {\n"
839 " type string;\n"
840 " }\n"
841 " anydata any {\n"
842 " config false;\n"
843 " }\n"
844 " list l2 {\n"
845 " config false;\n"
846 " container l2c {\n"
847 " leaf l2cx {\n"
848 " type string;\n"
849 " }\n"
850 " }\n"
851 " }\n"
852 " choice ch {\n"
853 " case cas {\n"
854 " leaf casx {\n"
855 " type string;\n"
856 " }\n"
857 " }\n"
858 " }\n"
859 " rpc oper {\n"
860 " input {\n"
861 " leaf inparam {\n"
862 " type string;\n"
863 " }\n"
864 " }\n"
865 " output {\n"
866 " leaf outparam {\n"
867 " type int8;\n"
868 " }\n"
869 " }\n"
870 " }\n"
871 " notification n1;\n"
872 " grouping grp {\n"
873 " leaf grpleaf {\n"
874 " type uint16;\n"
875 " }\n"
876 " }\n"
877 " augment /cont {\n"
878 " leaf augleaf {\n"
879 " type uint16;\n"
880 " }\n"
881 " }\n"
882 " deviation /a:foo2 {\n"
883 " deviate not-supported;\n"
884 " }\n"
885 "}\n";
886
887 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200888 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200889 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200890
aPiecekbb96b802021-04-12 08:12:52 +0200891 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200892 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
893 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200894 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
895
896 print_message("[ ] create context\n");
897 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
898 iter = check;
899 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200900 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200901
902 print_message("[ ] unset option\n");
903 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
904 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
905 iter = check;
906 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200907 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200908
909 print_message("[ ] set option\n");
910 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
911 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
912 iter = check;
913 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200914 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200915}
916
Michal Vasko01db7de2021-04-16 12:23:30 +0200917static void
918test_explicit_compile(void **state)
919{
920 uint32_t i;
Michal Vasko4de7d072021-07-09 09:13:18 +0200921 struct lys_module *mod;
Michal Vasko01db7de2021-04-16 12:23:30 +0200922 const char *schema_a = "module a {\n"
923 " namespace urn:tests:a;\n"
924 " prefix a;yang-version 1.1;\n"
925 " feature f1;\n"
926 " feature f2;\n"
927 " leaf foo1 {\n"
928 " type uint16;\n"
929 " if-feature f1;\n"
930 " }\n"
931 " leaf foo2 {\n"
932 " type uint16;\n"
933 " }\n"
934 " container cont {\n"
935 " leaf foo3 {\n"
936 " type string;\n"
937 " }\n"
938 " }\n"
939 "}\n";
940 const char *schema_b = "module b {\n"
941 " namespace urn:tests:b;\n"
942 " prefix b;yang-version 1.1;\n"
943 " import a {\n"
944 " prefix a;\n"
945 " }\n"
946 " augment /a:cont {\n"
947 " leaf augleaf {\n"
948 " type uint16;\n"
949 " }\n"
950 " }\n"
951 "}\n";
952 const char *schema_c = "module c {\n"
953 " namespace urn:tests:c;\n"
954 " prefix c;yang-version 1.1;\n"
955 " import a {\n"
956 " prefix a;\n"
957 " }\n"
958 " deviation /a:foo2 {\n"
959 " deviate not-supported;\n"
960 " }\n"
961 "}\n";
962
963 /* use own context with extra flags */
964 ly_ctx_destroy(UTEST_LYCTX);
965 const char *feats[] = {"f1", NULL};
966
967 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
968 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
969 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
970 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
971 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
972
Michal Vaskodd992582021-06-10 14:34:57 +0200973 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +0200974 i = 0;
975 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +0200976 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +0200977 }
978
979 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
980
981 /* check internal modules */
982 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
983 assert_non_null(mod);
984 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
985 assert_non_null(mod);
986 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
987 assert_non_null(mod);
988
989 /* check test modules */
990 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
991 assert_non_null(mod);
992 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
993 assert_non_null(mod);
994 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
995 assert_non_null(mod);
996}
997
Radek Iša56ca9e42020-09-08 18:42:00 +0200998int
999main(void)
1000{
1001 const struct CMUnitTest tests[] = {
1002 UTEST(test_searchdirs),
1003 UTEST(test_options),
1004 UTEST(test_models),
1005 UTEST(test_imports),
1006 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +02001007 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +02001008 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +02001009 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +02001010 };
1011
1012 return cmocka_run_group_tests(tests, NULL, NULL);
1013}