blob: d1fa2ad02e394cacdfbb7dc5c93fb805642cec40 [file] [log] [blame]
Michal Vaskof20ad112023-02-10 15:12:12 +01001/**
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"
Jan Kundráte2789d32021-12-13 18:28:45 +010023#ifdef _WIN32
24static void
25slashes_to_backslashes(char *path)
26{
27 while ((path = strchr(path, '/'))) {
28 *path++ = '\\';
29 }
30}
31
32static void
33test_searchdirs(void **state)
34{
35 const char * const *list;
36 char *path1 = strdup(TESTS_BIN "/utests");
37 char *path2 = strdup(TESTS_SRC);
38
39 slashes_to_backslashes(path1);
40 slashes_to_backslashes(path2);
41
42 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
43 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
44 assert_null(ly_ctx_get_searchdirs(NULL));
45 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
46 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
47 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
48
49 /* correct path */
50 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path1));
51 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
52 assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]);
53
54 /* duplicated paths */
55 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, path1));
56 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
57 assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]);
58
59 /* another path */
60 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path2));
61 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
62 assert_string_equal(path2, UTEST_LYCTX->search_paths.objs[1]);
63
64 /* get searchpaths */
65 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
66 assert_non_null(list);
67 assert_string_equal(path1, list[0]);
68 assert_string_equal(path2, list[1]);
69 assert_null(list[2]);
70
71 /* removing searchpaths */
72 /* nonexisting */
73 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
74 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
75
76 /* first */
77 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path1));
78 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
79 assert_string_not_equal(path1, list[0]);
80
81 /* second */
82 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path2));
83 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
84
85 free(path1);
86 free(path2);
87}
88
89#else
90
Radek Iša56ca9e42020-09-08 18:42:00 +020091static void
92test_searchdirs(void **state)
93{
94 const char * const *list;
95
96 /* invalid arguments */
97 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
98 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
99 assert_null(ly_ctx_get_searchdirs(NULL));
100 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
101 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
102 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
103
104 /* readable and executable, but not a directory */
105 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context"));
106 CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 /* not existing */
108 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile"));
109 CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
110
111 /* ly_set_add() fails */
112 /* no change */
113 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL));
114
115 /* correct path */
116 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
117 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
118 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
119
120 /* duplicated paths */
121 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
122 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
123 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
124
125 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles"));
127 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src"));
128 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules"));
129 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc"));
130 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC));
131 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN));
132 assert_int_equal(7, UTEST_LYCTX->search_paths.count);
133
134 /* get searchpaths */
135 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
136 assert_non_null(list);
137 assert_string_equal(TESTS_BIN "/utests", list[0]);
138 assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]);
139 assert_string_equal(TESTS_SRC, list[5]);
140 assert_string_equal(TESTS_BIN, list[6]);
141 assert_null(list[7]);
142
143 /* removing searchpaths */
144 /* nonexisting */
145 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
146 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
147 /* first */
148 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
149 assert_string_not_equal(TESTS_BIN "/utests", list[0]);
150 assert_int_equal(6, UTEST_LYCTX->search_paths.count);
151 /* middle */
152 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC));
153 assert_int_equal(5, UTEST_LYCTX->search_paths.count);
154 /* last */
155 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN));
156 assert_int_equal(4, UTEST_LYCTX->search_paths.count);
157 /* all */
158 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
159 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
160
161 /* again - no change */
162 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
163
164 /* cleanup */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200165 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200166
167 /* test searchdir list in ly_ctx_new() */
168 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX));
169 CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
Jan Kundrát34bb9282021-12-13 18:45:50 +0100170 assert_int_equal(LY_SUCCESS,
171 ly_ctx_new(TESTS_SRC PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_SRC,
172 LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200173 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
174 assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]);
175 assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]);
176}
177
Jan Kundráte2789d32021-12-13 18:28:45 +0100178#endif
179
Radek Iša56ca9e42020-09-08 18:42:00 +0200180static void
181test_options(void **state)
182{
183 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200184 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200185
186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX));
187
188 /* invalid arguments */
189 assert_int_equal(0, ly_ctx_get_options(NULL));
190 CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL);
191
192 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
193 CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL);
194 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
195 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL);
196
Radek Iša56ca9e42020-09-08 18:42:00 +0200197 /* unset */
198 /* LY_CTX_ALL_IMPLEMENTED */
199 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
200 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
201 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
202
203 /* LY_CTX_REF_IMPLEMENTED */
204 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
205 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
206 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
207
208 /* LY_CTX_DISABLE_SEARCHDIRS */
209 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
210 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
211 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
212
213 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
214 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
215 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
216 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
217
218 /* LY_CTX_PREFER_SEARCHDIRS */
219 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
220 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
221 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
222
stewegf9041a22024-01-18 13:29:12 +0100223 /* LY_CTX_LEAFREF_LINKING */
224 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
225 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_LEAFREF_LINKING));
226 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
227
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
229
230 /* set back */
231 /* LY_CTX_ALL_IMPLEMENTED */
232 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
233 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
234
235 /* LY_CTX_REF_IMPLEMENTED */
236 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
237 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
238
239 /* LY_CTX_DISABLE_SEARCHDIRS */
240 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
241 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
242
243 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
244 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
245 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
246
247 /* LY_CTX_PREFER_SEARCHDIRS */
248 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
249 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
250
stewegf9041a22024-01-18 13:29:12 +0100251 /* LY_CTX_LEAFREF_LINKING */
252 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_LEAFREF_LINKING));
253 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
254
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
256}
257
258static LY_ERR
259test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
260 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
261 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
262{
263 *module_data = user_data;
264 *format = LYS_IN_YANG;
265 *free_module_data = NULL;
266 return LY_SUCCESS;
267}
268
269static void
270test_models(void **state)
271{
272 struct ly_in *in;
273 const char *str;
274 struct lys_module *mod1, *mod2;
275 struct lys_glob_unres unres = {0};
276
277 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200278 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200279
280 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200281 assert_int_equal(0, ly_ctx_get_change_count(NULL));
282 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200283
284 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200285 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200286
287 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200288 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200289 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200290 ly_in_free(in, 0);
291 CHECK_LOG_CTX("Invalid schema input format.", NULL);
292
293 /* import callback */
294 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
295 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
296 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
297 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
298 assert_string_equal("test", str);
299
300 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
301 assert_null(UTEST_LYCTX->imp_clb);
302 assert_null(UTEST_LYCTX->imp_clb_data);
303
304 /* name collision of module and submodule */
305 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
306 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 +0200307 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 +0200308 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200309 ly_in_free(in, 0);
Michal Vasko62af3692023-02-09 14:00:09 +0100310 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL);
311 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200312
313 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 +0200314 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 +0200315 ly_in_free(in, 0);
316 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200317 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 +0200318 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200319 ly_in_free(in, 0);
Michal Vasko62af3692023-02-09 14:00:09 +0100320 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL);
321 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200322
323 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
324 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 +0200325 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 +0200326 lys_unres_glob_revert(UTEST_LYCTX, &unres);
327 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200328 ly_in_free(in, 0);
Michal Vasko62af3692023-02-09 14:00:09 +0100329 CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL);
330 CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL);
331 CHECK_LOG_CTX("Parsing submodule failed.", NULL);
332 CHECK_LOG_CTX("Name collision between submodules of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200333
334 /* selecting correct revision of the submodules */
335 ly_ctx_reset_latests(UTEST_LYCTX);
336 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
337 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 +0200338 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 +0200339 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 ly_in_free(in, 0);
341 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
342
343 /* reloading module in case only the compiled module resists in the context */
344 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 +0200345 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200346 ly_in_free(in, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200347 assert_non_null(mod1->compiled);
348 assert_non_null(mod1->parsed);
349
Radek Iša56ca9e42020-09-08 18:42:00 +0200350 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));
351 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 +0200352 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200353 ly_in_free(in, 0);
354 assert_non_null(mod2);
355 assert_non_null(mod1->parsed);
356 assert_string_equal("w", mod1->name);
357}
358
359static void
360test_imports(void **state)
361{
aPiecekd4911ee2021-07-30 07:40:24 +0200362 struct lys_module *mod1, *mod2, *mod3, *import;
aPiecek9f8c7e72021-07-28 12:01:56 +0200363 char *str;
aPiecekd4911ee2021-07-30 07:40:24 +0200364 uint16_t ctx_options;
Radek Iša56ca9e42020-09-08 18:42:00 +0200365
366 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200367 ly_ctx_destroy(UTEST_LYCTX);
aPiecekd4911ee2021-07-30 07:40:24 +0200368 ctx_options = LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_NO_YANGLIBRARY;
Radek Iša56ca9e42020-09-08 18:42:00 +0200369
aPiecekd4911ee2021-07-30 07:40:24 +0200370 /* Import callback provides newer revision of module 'a',
371 * however the older revision is implemented soon and therefore it is preferred. */
372 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200373 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
374 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
375 LYS_IN_YANG, &mod1));
aPiecek8ca21bd2021-07-26 14:31:01 +0200376 assert_true(LYS_MOD_LATEST_REV & mod1->latest_revision);
Radek Iša56ca9e42020-09-08 18:42:00 +0200377 assert_int_equal(1, mod1->implemented);
378 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
379 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200380 assert_ptr_equal(mod1, mod2->parsed->imports[0].module);
381 assert_true((LYS_MOD_LATEST_REV | LYS_MOD_IMPORTED_REV) & mod1->latest_revision);
382 assert_string_equal("2019-09-16", mod1->revision);
383 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200384 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200385 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200386
aPiecekd4911ee2021-07-30 07:40:24 +0200387 /* Import callback provides older revision of module 'a' and it is
388 * imported by another module, so it is preferred even if newer
389 * revision is implemented later. */
390 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
391 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 +0200392 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
393 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200394 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-17;}",
395 LYS_IN_YANG, &mod1));
396 ly_log_level(LY_LLVRB);
397 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c;import a {prefix a;}}",
398 LYS_IN_YANG, &mod3));
399 CHECK_LOG("Implemented module \"a@2019-09-17\" is not used for import, revision \"2019-09-16\" is imported instead.", NULL);
400 ly_log_level(LY_LLWRN);
401 assert_true(LYS_MOD_LATEST_SEARCHDIRS & mod1->latest_revision);
402 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 import = mod2->parsed->imports[0].module;
aPiecekd4911ee2021-07-30 07:40:24 +0200404 assert_true(LYS_MOD_IMPORTED_REV & import->latest_revision);
405 assert_string_equal("2019-09-16", import->revision);
406 assert_int_equal(0, import->implemented);
407 import = mod3->parsed->imports[0].module;
408 assert_string_equal("2019-09-16", import->revision);
409 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
410 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
411 assert_string_equal("2019-09-17", ly_ctx_get_module_implemented(UTEST_LYCTX, "a")->revision);
aPiecek9f8c7e72021-07-28 12:01:56 +0200412 ly_ctx_destroy(UTEST_LYCTX);
413
414 /* check of circular dependency */
aPiecekd4911ee2021-07-30 07:40:24 +0200415 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
aPiecek9f8c7e72021-07-28 12:01:56 +0200416 str = "module a {namespace urn:a; prefix a;"
417 "import b {prefix b;}"
418 "}";
419 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, str);
420 str = "module b { yang-version 1.1; namespace urn:b; prefix b;"
421 "import a {prefix a;}"
422 "}";
423 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Michal Vasko62af3692023-02-09 14:00:09 +0100424 ly_err_clean(UTEST_LYCTX, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200425}
426
427static void
428test_get_models(void **state)
429{
430 struct lys_module *mod, *mod2;
431 const char *str0 = "module a {namespace urn:a;prefix a;}";
432 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
433 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
434 struct ly_in *in0, *in1, *in2;
435 struct lys_glob_unres unres = {0};
436
437 unsigned int index = 0;
Michal Vaskoddd76592022-01-17 13:34:48 +0100438 const char *names[] = {
439 "ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-yang-schema-mount",
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200440 "ietf-yang-structure-ext", "ietf-datastores", "ietf-yang-library", "a", "a", "a"
Michal Vaskoddd76592022-01-17 13:34:48 +0100441 };
Radek Iša56ca9e42020-09-08 18:42:00 +0200442
443 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
444 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
445 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
446
447 /* invalid arguments */
448 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
449 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
450 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
451 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
452 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
453 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
454 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
455 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
456 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
457
458 /* internal modules */
459 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
460 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
461 assert_non_null(mod);
462 assert_non_null(mod->parsed);
463 assert_string_equal("yang", mod->name);
464 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
465 assert_ptr_equal(mod, mod2);
466 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
467 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
468 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
469 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
470
471 /* select module by revision */
Michal Vasko4de7d072021-07-09 09:13:18 +0200472 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200473 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200474 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 +0200475 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
Michal Vasko978532b2022-01-21 14:13:40 +0100476 CHECK_LOG_CTX("Module \"a@2018-10-24\" is already implemented in revision \"2018-10-23\".", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200477 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200478 ly_in_reset(in1);
479 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200480 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 +0200481 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200482 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200483 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200484 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 +0200485 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 assert_non_null(mod2);
487 assert_ptr_not_equal(mod, mod2);
488 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
489 assert_ptr_equal(mod, mod2);
490 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
491 assert_ptr_equal(mod, mod2);
492 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200493 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 +0200494 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200495 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
496 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
497
498 str1 = "submodule b {belongs-to a {prefix a;}}";
499 ly_in_free(in1, 0);
500 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200501 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 +0200502 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200503 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200504
505 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
506 assert_string_equal(names[index - 1], mod->name);
507 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200508 assert_int_equal(11, index);
Radek Iša56ca9e42020-09-08 18:42:00 +0200509
510 /* cleanup */
511 ly_in_free(in0, 0);
512 ly_in_free(in1, 0);
513 ly_in_free(in2, 0);
514}
515
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200516static void
517test_ylmem(void **state)
518{
519#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
520 " <module-set>\n"\
521 " <name>complete</name>\n"\
522 " <module>\n"\
523 " <name>yang</name>\n"\
Michal Vasko79a7a872022-06-17 09:00:48 +0200524 " <revision>2022-06-16</revision>\n"\
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200525 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
526 " </module>\n"\
527 " <module>\n"\
528 " <name>ietf-yang-library</name>\n"\
529 " <revision>2019-01-04</revision>\n"\
530 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
531 " </module>\n"
532
533#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
534 " <name>ietf-yang-metadata</name>\n"\
535 " <revision>2016-08-05</revision>\n"\
536 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
537 " </import-only-module>\n"\
538 " <import-only-module>\n"\
539 " <name>ietf-inet-types</name>\n"\
540 " <revision>2013-07-15</revision>\n"\
541 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
542 " </import-only-module>\n"\
543 " <import-only-module>\n"\
544 " <name>ietf-yang-types</name>\n"\
545 " <revision>2013-07-15</revision>\n"\
546 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
547 " </import-only-module>\n"\
548 " <import-only-module>\n"\
549 " <name>ietf-datastores</name>\n"\
550 " <revision>2018-02-14</revision>\n"\
551 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
552 " </import-only-module>\n"
553
554#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
555 " <schema>\n"\
556 " <name>complete</name>\n"\
557 " <module-set>complete</module-set>\n"\
558 " </schema>\n"\
559 " <content-id>9</content-id>\n"\
560 "</yang-library>\n"\
561 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
562 " <module-set-id>12</module-set-id>\n"\
563 " <module>\n"\
564 " <name>ietf-yang-metadata</name>\n"\
565 " <revision>2016-08-05</revision>\n"\
566 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
567 " <conformance-type>import</conformance-type>\n"\
568 " </module>\n"\
569 " <module>\n"\
570 " <name>yang</name>\n"\
Michal Vasko79a7a872022-06-17 09:00:48 +0200571 " <revision>2022-06-16</revision>\n"\
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200572 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
573 " <conformance-type>implement</conformance-type>\n"\
574 " </module>\n"\
575 " <module>\n"\
576 " <name>ietf-inet-types</name>\n"\
577 " <revision>2013-07-15</revision>\n"\
578 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
579 " <conformance-type>import</conformance-type>\n"\
580 " </module>\n"\
581 " <module>\n"\
582 " <name>ietf-yang-types</name>\n"\
583 " <revision>2013-07-15</revision>\n"\
584 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
585 " <conformance-type>import</conformance-type>\n"\
586 " </module>\n"\
587 " <module>\n"\
588 " <name>ietf-yang-library</name>\n"\
589 " <revision>2019-01-04</revision>\n"\
590 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
591 " <conformance-type>implement</conformance-type>\n"\
592 " </module>\n"\
593 " <module>\n"\
594 " <name>ietf-datastores</name>\n"\
595 " <revision>2018-02-14</revision>\n"\
596 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
597 " <conformance-type>import</conformance-type>\n"\
598 " </module>\n"
599
600 const char *yanglibrary_only =
601 DATA_YANG_LIBRARY_START
602 DATA_YANG_BASE_IMPORTS
603 DATA_YANG_SCHEMA_MODULE_STATE
604 "</modules-state>\n";
605
606 const char *with_netconf =
607 DATA_YANG_LIBRARY_START
608 " <module>\n"
609 " <name>ietf-netconf</name>\n"
610 " <revision>2011-06-01</revision>\n"
611 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
612 " </module>\n"
613 DATA_YANG_BASE_IMPORTS
614 " <import-only-module>\n"
615 " <name>ietf-netconf-acm</name>\n"
616 " <revision>2018-02-14</revision>\n"
617 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
618 " </import-only-module>\n"
619 DATA_YANG_SCHEMA_MODULE_STATE
620 " <module>\n"
621 " <name>ietf-netconf</name>\n"
622 " <revision>2011-06-01</revision>\n"
623 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\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"
632 "</modules-state>";
633
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200634 char *with_netconf_features = malloc(8096);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100635
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200636 strcpy(with_netconf_features,
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200637 DATA_YANG_LIBRARY_START
638 " <module>\n"
639 " <name>ietf-netconf</name>\n"
640 " <revision>2011-06-01</revision>\n"
641 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
642 " <feature>writable-running</feature>\n"
643 " <feature>candidate</feature>\n"
644 " <feature>confirmed-commit</feature>\n"
645 " <feature>rollback-on-error</feature>\n"
646 " <feature>validate</feature>\n"
647 " <feature>startup</feature>\n"
648 " <feature>url</feature>\n"
649 " <feature>xpath</feature>\n"
650 " </module>\n"
651 " <import-only-module>\n"
652 " <name>ietf-yang-metadata</name>\n"
653 " <revision>2016-08-05</revision>\n"
654 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
655 " </import-only-module>\n"
656 " <import-only-module>\n"
657 " <name>ietf-inet-types</name>\n"
658 " <revision>2013-07-15</revision>\n"
659 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
660 " </import-only-module>\n"
661 " <import-only-module>\n"
662 " <name>ietf-yang-types</name>\n"
663 " <revision>2013-07-15</revision>\n"
664 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
665 " </import-only-module>\n"
666 " <import-only-module>\n"
667 " <name>ietf-datastores</name>\n"
668 " <revision>2018-02-14</revision>\n"
669 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
670 " </import-only-module>\n"
671 " <import-only-module>\n"
672 " <name>ietf-netconf-acm</name>\n"
673 " <revision>2018-02-14</revision>\n"
674 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200675 " </import-only-module>\n");
676 strcpy(with_netconf_features + strlen(with_netconf_features),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200677 DATA_YANG_SCHEMA_MODULE_STATE
678 " <module>\n"
679 " <name>ietf-netconf</name>\n"
680 " <revision>2011-06-01</revision>\n"
681 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
682 " <feature>writable-running</feature>\n"
683 " <feature>candidate</feature>\n"
684 " <feature>confirmed-commit</feature>\n"
685 " <feature>rollback-on-error</feature>\n"
686 " <feature>validate</feature>\n"
687 " <feature>startup</feature>\n"
688 " <feature>url</feature>\n"
689 " <feature>xpath</feature>\n"
690 " <conformance-type>implement</conformance-type>\n"
691 " </module>\n"
692 " <module>\n"
693 " <name>ietf-netconf-acm</name>\n"
694 " <revision>2018-02-14</revision>\n"
695 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
696 " <conformance-type>import</conformance-type>\n"
697 " </module>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200698 "</modules-state>");
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200699
700 const char *garbage_revision =
701 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
702 " <module-set>\n"
703 " <name>complete</name>\n"
704 " <module>\n"
705 " <name>yang</name>\n"
Michal Vasko79a7a872022-06-17 09:00:48 +0200706 " <revision>2022-06-16</revision>\n"
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200707 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
708 " </module>\n"
709 " <module>\n"
710 " <name>ietf-yang-library</name>\n"
711 " <revision>2019-01-01</revision>\n"
712 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
713 " </module>\n"
714 DATA_YANG_BASE_IMPORTS
715 DATA_YANG_SCHEMA_MODULE_STATE
716 "</modules-state>\n";
717
718 const char *no_yanglibrary =
719 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
720 " <module-set>\n"
721 " <name>complete</name>\n"
722 " <module>\n"
723 " <name>yang</name>\n"
Michal Vasko79a7a872022-06-17 09:00:48 +0200724 " <revision>2022-06-16</revision>\n"
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200725 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
726 " </module>\n"
727 DATA_YANG_BASE_IMPORTS
728 DATA_YANG_SCHEMA_MODULE_STATE
729 "</modules-state>\n";
730
731 (void) state;
732 /* seperate context to avoid double free during teadown */
733 struct ly_ctx *ctx_test = NULL;
734
735 /* test invalid parameters */
736 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
737 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
738 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
739
740 /* basic test with ietf-yang-library-only */
741 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 +0200742 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200743 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200744 ly_ctx_destroy(ctx_test);
ekinzie0ab8b302022-10-10 03:03:57 -0400745 ctx_test = NULL;
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200746
747 /* test loading module, should also import other module */
748 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 +0200749 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200750 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200751 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200752 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
753 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 +0200754 ly_ctx_destroy(ctx_test);
ekinzie0ab8b302022-10-10 03:03:57 -0400755 ctx_test = NULL;
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200756
757 /* test loading module with feature if they are present */
758 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 +0200759 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
760 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200761 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 +0200762 ly_ctx_destroy(ctx_test);
ekinzie0ab8b302022-10-10 03:03:57 -0400763 ctx_test = NULL;
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200764
765 /* test with not matching revision */
766 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
767
768 /* test data containing ietf-yang-library which conflicts with the option */
769 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
770
771 /* test creating without ietf-yang-library */
772 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
773 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200774 ly_ctx_destroy(ctx_test);
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200775 free(with_netconf_features);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200776}
777
aPiecekbb96b802021-04-12 08:12:52 +0200778static LY_ERR
779check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
780{
781 const struct lysp_node *pnode;
782 const char ***iter;
783
784 pnode = (const struct lysp_node *)node->priv;
785 CHECK_POINTER(pnode, 1);
786 iter = (const char ***)data;
787 CHECK_POINTER(**iter, 1);
788 CHECK_STRING(pnode->name, **iter);
789 (*iter)++;
790
791 return LY_SUCCESS;
792}
793
794static LY_ERR
795check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
796{
797 CHECK_POINTER(node->priv, 0);
798 return LY_SUCCESS;
799}
800
aPiecekfcfec0f2021-04-23 12:47:30 +0200801static void
802check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
803{
804 LY_ARRAY_COUNT_TYPE u, v;
805 struct lysc_ext_substmt *substmts;
806 struct lysc_node *cnode;
807 const char **iter;
808 const char *check[] = {
809 "tmp_cont", "lf", NULL
810 };
811
812 LY_ARRAY_FOR(ext, u) {
813 substmts = ext[u].substmts;
814 LY_ARRAY_FOR(substmts, v) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200815 if (substmts && substmts[v].storage && (substmts[v].stmt & LY_STMT_DATA_NODE_MASK)) {
aPiecekfcfec0f2021-04-23 12:47:30 +0200816 cnode = *(struct lysc_node **)substmts[v].storage;
817 iter = check;
818 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
819 }
820 }
821 }
822}
823
824static void
825check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
826{
827 LY_ARRAY_COUNT_TYPE u, v;
828 struct lysc_ext_substmt *substmts;
829 struct lysc_node *cnode;
830
831 LY_ARRAY_FOR(ext, u) {
832 substmts = ext[u].substmts;
833 LY_ARRAY_FOR(substmts, v) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200834 if (substmts && substmts[v].storage && (substmts[v].stmt & LY_STMT_DATA_NODE_MASK)) {
aPiecekfcfec0f2021-04-23 12:47:30 +0200835 cnode = *(struct lysc_node **)substmts[v].storage;
836 if (cnode) {
837 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
838 }
839 }
840 }
841 }
842}
843
aPiecekbb96b802021-04-12 08:12:52 +0200844/**
845 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
846 */
847static void
848test_set_priv_parsed(void **state)
849{
Michal Vasko4de7d072021-07-09 09:13:18 +0200850 struct lys_module *mod;
aPiecekbb96b802021-04-12 08:12:52 +0200851 const char *schema_a;
852 const char **iter;
853 const char *check[] = {
Michal Vasko95f736c2022-06-08 12:03:31 +0200854 "cont", "contnotif", "contx", "grpleaf", "augleaf", "l1",
aPiecekbb96b802021-04-12 08:12:52 +0200855 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
856 "l2c", "l2cx", "ch", "cas", "casx", "oper",
857 "input", "inparam", "output", "outparam", "n1", NULL
858 };
859
860 /* each node must have a unique name. */
861 schema_a = "module a {\n"
862 " namespace urn:tests:a;\n"
863 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200864 "\n"
865 " import ietf-restconf {\n"
866 " prefix rc;\n"
867 " revision-date 2017-01-26;\n"
868 " }\n"
869 "\n"
870 " rc:yang-data \"tmp\" {\n"
871 " container tmp_cont {\n"
872 " leaf lf {\n"
873 " type string;\n"
874 " }\n"
875 " }\n"
876 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200877 " container cont {\n"
878 " notification contnotif;\n"
879 " leaf-list contx {\n"
880 " type string;\n"
881 " }\n"
882 " uses grp;\n"
883 " }\n"
884 " list l1 {\n"
885 " key \"l1a l1b\";\n"
886 " leaf l1a {\n"
887 " type string;\n"
888 " }\n"
889 " leaf l1b {\n"
890 " type string;\n"
891 " }\n"
892 " leaf l1c {\n"
893 " type string;\n"
894 " }\n"
895 " }\n"
896 " feature f1;\n"
897 " feature f2;\n"
898 " leaf foo1 {\n"
899 " type uint16;\n"
900 " if-feature f1;\n"
901 " }\n"
902 " leaf foo2 {\n"
903 " type uint16;\n"
904 " }\n"
905 " leaf foo3 {\n"
906 " type uint16;\n"
907 " if-feature f2;\n"
908 " }\n"
909 " leaf-list ll {\n"
910 " type string;\n"
911 " }\n"
912 " anydata any {\n"
913 " config false;\n"
914 " }\n"
915 " list l2 {\n"
916 " config false;\n"
917 " container l2c {\n"
918 " leaf l2cx {\n"
919 " type string;\n"
920 " }\n"
921 " }\n"
922 " }\n"
923 " choice ch {\n"
924 " case cas {\n"
925 " leaf casx {\n"
926 " type string;\n"
927 " }\n"
928 " }\n"
929 " }\n"
930 " rpc oper {\n"
931 " input {\n"
932 " leaf inparam {\n"
933 " type string;\n"
934 " }\n"
935 " }\n"
936 " output {\n"
937 " leaf outparam {\n"
938 " type int8;\n"
939 " }\n"
940 " }\n"
941 " }\n"
942 " notification n1;\n"
943 " grouping grp {\n"
944 " leaf grpleaf {\n"
945 " type uint16;\n"
946 " }\n"
947 " }\n"
948 " augment /cont {\n"
949 " leaf augleaf {\n"
950 " type uint16;\n"
951 " }\n"
952 " }\n"
953 " deviation /a:foo2 {\n"
954 " deviate not-supported;\n"
955 " }\n"
956 "}\n";
957
958 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200959 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200960 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200961
aPiecekbb96b802021-04-12 08:12:52 +0200962 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200963 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
964 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200965 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
966
967 print_message("[ ] create context\n");
968 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
969 iter = check;
970 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200971 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200972
973 print_message("[ ] unset option\n");
974 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
975 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
976 iter = check;
977 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200978 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200979
980 print_message("[ ] set option\n");
981 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
982 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
983 iter = check;
984 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200985 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200986}
987
Michal Vasko01db7de2021-04-16 12:23:30 +0200988static void
989test_explicit_compile(void **state)
990{
991 uint32_t i;
Michal Vasko4de7d072021-07-09 09:13:18 +0200992 struct lys_module *mod;
Michal Vasko01db7de2021-04-16 12:23:30 +0200993 const char *schema_a = "module a {\n"
994 " namespace urn:tests:a;\n"
995 " prefix a;yang-version 1.1;\n"
996 " feature f1;\n"
997 " feature f2;\n"
998 " leaf foo1 {\n"
999 " type uint16;\n"
1000 " if-feature f1;\n"
1001 " }\n"
1002 " leaf foo2 {\n"
1003 " type uint16;\n"
1004 " }\n"
1005 " container cont {\n"
1006 " leaf foo3 {\n"
1007 " type string;\n"
1008 " }\n"
1009 " }\n"
1010 "}\n";
1011 const char *schema_b = "module b {\n"
1012 " namespace urn:tests:b;\n"
1013 " prefix b;yang-version 1.1;\n"
1014 " import a {\n"
1015 " prefix a;\n"
1016 " }\n"
1017 " augment /a:cont {\n"
1018 " leaf augleaf {\n"
1019 " type uint16;\n"
1020 " }\n"
1021 " }\n"
1022 "}\n";
1023 const char *schema_c = "module c {\n"
1024 " namespace urn:tests:c;\n"
1025 " prefix c;yang-version 1.1;\n"
1026 " import a {\n"
1027 " prefix a;\n"
1028 " }\n"
1029 " deviation /a:foo2 {\n"
1030 " deviate not-supported;\n"
1031 " }\n"
1032 "}\n";
1033
1034 /* use own context with extra flags */
1035 ly_ctx_destroy(UTEST_LYCTX);
1036 const char *feats[] = {"f1", NULL};
1037
1038 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
1039 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
1040 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
1041 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
1042 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
1043
Michal Vaskodd992582021-06-10 14:34:57 +02001044 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +02001045 i = 0;
1046 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +02001047 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +02001048 }
1049
1050 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
1051
1052 /* check internal modules */
1053 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
1054 assert_non_null(mod);
1055 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
1056 assert_non_null(mod);
1057 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
1058 assert_non_null(mod);
1059
1060 /* check test modules */
1061 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
1062 assert_non_null(mod);
1063 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
1064 assert_non_null(mod);
1065 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
1066 assert_non_null(mod);
1067}
1068
Radek Iša56ca9e42020-09-08 18:42:00 +02001069int
1070main(void)
1071{
1072 const struct CMUnitTest tests[] = {
1073 UTEST(test_searchdirs),
1074 UTEST(test_options),
1075 UTEST(test_models),
1076 UTEST(test_imports),
1077 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +02001078 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +02001079 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +02001080 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +02001081 };
1082
1083 return cmocka_run_group_tests(tests, NULL, NULL);
1084}