blob: b605858a270813b123cce1ec2ea4a87565fa3d70 [file] [log] [blame]
Radek Iša56ca9e42020-09-08 18:42:00 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_context.c
Radek Iša56ca9e42020-09-08 18:42:00 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from context.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17#include "common.h"
18#include "context.h"
19#include "in.h"
20#include "schema_compile.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020021#include "tests_config.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020022#include "tree_schema_internal.h"
23#include "utests.h"
24
25static void
26test_searchdirs(void **state)
27{
28 const char * const *list;
29
30 /* invalid arguments */
31 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
32 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
33 assert_null(ly_ctx_get_searchdirs(NULL));
34 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
35 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
36 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
37
38 /* readable and executable, but not a directory */
39 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context"));
40 CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL);
41 /* not executable */
42 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, __FILE__));
43 CHECK_LOG_CTX("Unable to fully access search directory \""__FILE__ "\" (Permission denied).", NULL);
44 /* not existing */
45 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile"));
46 CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
47
48 /* ly_set_add() fails */
49 /* no change */
50 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL));
51
52 /* correct path */
53 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
54 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
55 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
56
57 /* duplicated paths */
58 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
59 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
60 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
61
62 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
63 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles"));
64 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src"));
65 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules"));
66 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc"));
67 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC));
68 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN));
69 assert_int_equal(7, UTEST_LYCTX->search_paths.count);
70
71 /* get searchpaths */
72 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
73 assert_non_null(list);
74 assert_string_equal(TESTS_BIN "/utests", list[0]);
75 assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]);
76 assert_string_equal(TESTS_SRC, list[5]);
77 assert_string_equal(TESTS_BIN, list[6]);
78 assert_null(list[7]);
79
80 /* removing searchpaths */
81 /* nonexisting */
82 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
83 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
84 /* first */
85 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
86 assert_string_not_equal(TESTS_BIN "/utests", list[0]);
87 assert_int_equal(6, UTEST_LYCTX->search_paths.count);
88 /* middle */
89 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC));
90 assert_int_equal(5, UTEST_LYCTX->search_paths.count);
91 /* last */
92 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN));
93 assert_int_equal(4, UTEST_LYCTX->search_paths.count);
94 /* all */
95 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
96 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
97
98 /* again - no change */
99 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
100
101 /* cleanup */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200102 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200103
104 /* test searchdir list in ly_ctx_new() */
105 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX));
106 CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
107 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC ":"TESTS_BIN ":"TESTS_BIN ":"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
108 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
109 assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]);
110 assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]);
111}
112
113static void
114test_options(void **state)
115{
116 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200117 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200118
119 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX));
120
121 /* invalid arguments */
122 assert_int_equal(0, ly_ctx_get_options(NULL));
123 CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL);
124
125 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
126 CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL);
127 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
128 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL);
129
130 /* option not allowed to be changed */
131 assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY));
132 CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL);
133 assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY));
134 CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL);
135
136 /* unset */
137 /* LY_CTX_ALL_IMPLEMENTED */
138 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
139 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
140 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
141
142 /* LY_CTX_REF_IMPLEMENTED */
143 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
144 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
145 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
146
147 /* LY_CTX_DISABLE_SEARCHDIRS */
148 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
149 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
150 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
151
152 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
153 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
154 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
155 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
156
157 /* LY_CTX_PREFER_SEARCHDIRS */
158 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
159 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
160 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
161
162 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
163
164 /* set back */
165 /* LY_CTX_ALL_IMPLEMENTED */
166 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
167 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
168
169 /* LY_CTX_REF_IMPLEMENTED */
170 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
171 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
172
173 /* LY_CTX_DISABLE_SEARCHDIRS */
174 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
175 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
176
177 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
178 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
179 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
180
181 /* LY_CTX_PREFER_SEARCHDIRS */
182 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
183 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
184
185 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
186}
187
188static LY_ERR
189test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
190 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
191 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
192{
193 *module_data = user_data;
194 *format = LYS_IN_YANG;
195 *free_module_data = NULL;
196 return LY_SUCCESS;
197}
198
199static void
200test_models(void **state)
201{
202 struct ly_in *in;
203 const char *str;
204 struct lys_module *mod1, *mod2;
205 struct lys_glob_unres unres = {0};
206
207 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200208 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200209
210 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200211 assert_int_equal(0, ly_ctx_get_change_count(NULL));
212 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200213
214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200215 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200216
217 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200218 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200219 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 ly_in_free(in, 0);
221 CHECK_LOG_CTX("Invalid schema input format.", NULL);
222
223 /* import callback */
224 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
225 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
226 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
227 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
228 assert_string_equal("test", str);
229
230 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
231 assert_null(UTEST_LYCTX->imp_clb);
232 assert_null(UTEST_LYCTX->imp_clb_data);
233
234 /* name collision of module and submodule */
235 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
236 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200237 assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200238 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200240 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
241 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200242
243 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 +0200244 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 +0200245 ly_in_free(in, 0);
246 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200247 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 +0200248 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200250 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
251 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200252
253 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
254 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 +0200255 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 +0200256 lys_unres_glob_revert(UTEST_LYCTX, &unres);
257 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200259 CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL,
260 "Including \"y\" submodule into \"b\" failed.", NULL,
261 "Parsing submodule failed.", NULL,
Radek Iša56ca9e42020-09-08 18:42:00 +0200262 "Name collision between submodules of name \"y\".", "Line number 1.");
263
264 /* selecting correct revision of the submodules */
265 ly_ctx_reset_latests(UTEST_LYCTX);
266 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
267 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 +0200268 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 +0200269 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200270 ly_in_free(in, 0);
271 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
272
273 /* reloading module in case only the compiled module resists in the context */
274 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 +0200275 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200276 ly_in_free(in, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 assert_non_null(mod1->compiled);
278 assert_non_null(mod1->parsed);
279
280#if 0
281 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
282
283 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));
284 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
285 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
286 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
287 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
288 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
289 assert_null(mod2);
290 ly_in_free(in, 0);
291#endif
292
293 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));
294 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 +0200295 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200296 ly_in_free(in, 0);
297 assert_non_null(mod2);
298 assert_non_null(mod1->parsed);
299 assert_string_equal("w", mod1->name);
300}
301
302static void
303test_imports(void **state)
304{
Michal Vasko4de7d072021-07-09 09:13:18 +0200305 struct lys_module *mod1, *mod2, *import;
Radek Iša56ca9e42020-09-08 18:42:00 +0200306
307 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200308 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200309
310 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
311 * from the callback should be loaded into the context and used as an import */
312 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
313 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
314 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
315 LYS_IN_YANG, &mod1));
316 assert_int_equal(1, mod1->latest_revision);
317 assert_int_equal(1, mod1->implemented);
318 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
319 LYS_IN_YANG, &mod2));
320 import = mod2->parsed->imports[0].module;
321 assert_int_equal(2, import->latest_revision);
322 assert_int_equal(0, mod1->latest_revision);
323 assert_ptr_not_equal(mod1, import);
324 assert_string_equal("2019-09-17", import->revision);
325 assert_int_equal(0, import->implemented);
326 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200327 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200328
329 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
330 * already present in the context should be selected and the callback's revision should not be loaded into the context */
331 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
332 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
333 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
334 LYS_IN_YANG, &mod1));
335 assert_int_equal(1, mod1->latest_revision);
336 assert_int_equal(1, mod1->implemented);
337 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
338 LYS_IN_YANG, &mod2));
339 import = mod2->parsed->imports[0].module;
340 assert_ptr_equal(mod1, import);
341 assert_int_equal(2, import->latest_revision);
342 assert_int_equal(1, import->implemented);
343 assert_string_equal("2019-09-18", import->revision);
344 assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
345}
346
347static void
348test_get_models(void **state)
349{
350 struct lys_module *mod, *mod2;
351 const char *str0 = "module a {namespace urn:a;prefix a;}";
352 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
353 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
354 struct ly_in *in0, *in1, *in2;
355 struct lys_glob_unres unres = {0};
356
357 unsigned int index = 0;
358 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
359
360 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
361 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
362 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
363
364 /* invalid arguments */
365 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
366 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
367 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
368 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
369 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
370 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
371 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
372 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
373 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
374
375 /* internal modules */
376 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
377 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
378 assert_non_null(mod);
379 assert_non_null(mod->parsed);
380 assert_string_equal("yang", mod->name);
381 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
382 assert_ptr_equal(mod, mod2);
383 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
384 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
385 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
386 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
387
388 /* select module by revision */
Michal Vasko4de7d072021-07-09 09:13:18 +0200389 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200391 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 +0200392 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
393 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 +0200394 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 ly_in_reset(in1);
396 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200397 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 +0200398 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200399 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200400 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200401 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 +0200402 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 assert_non_null(mod2);
404 assert_ptr_not_equal(mod, mod2);
405 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
406 assert_ptr_equal(mod, mod2);
407 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
408 assert_ptr_equal(mod, mod2);
409 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200410 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 +0200411 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
413 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
414
415 str1 = "submodule b {belongs-to a {prefix a;}}";
416 ly_in_free(in1, 0);
417 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200418 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 +0200419 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200420 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200421
422 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
423 assert_string_equal(names[index - 1], mod->name);
424 }
425 assert_int_equal(9, index);
426
427 /* cleanup */
428 ly_in_free(in0, 0);
429 ly_in_free(in1, 0);
430 ly_in_free(in2, 0);
431}
432
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200433static void
434test_ylmem(void **state)
435{
436#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
437 " <module-set>\n"\
438 " <name>complete</name>\n"\
439 " <module>\n"\
440 " <name>yang</name>\n"\
441 " <revision>2021-04-07</revision>\n"\
442 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
443 " </module>\n"\
444 " <module>\n"\
445 " <name>ietf-yang-library</name>\n"\
446 " <revision>2019-01-04</revision>\n"\
447 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
448 " </module>\n"
449
450#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
451 " <name>ietf-yang-metadata</name>\n"\
452 " <revision>2016-08-05</revision>\n"\
453 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
454 " </import-only-module>\n"\
455 " <import-only-module>\n"\
456 " <name>ietf-inet-types</name>\n"\
457 " <revision>2013-07-15</revision>\n"\
458 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
459 " </import-only-module>\n"\
460 " <import-only-module>\n"\
461 " <name>ietf-yang-types</name>\n"\
462 " <revision>2013-07-15</revision>\n"\
463 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
464 " </import-only-module>\n"\
465 " <import-only-module>\n"\
466 " <name>ietf-datastores</name>\n"\
467 " <revision>2018-02-14</revision>\n"\
468 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
469 " </import-only-module>\n"
470
471#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
472 " <schema>\n"\
473 " <name>complete</name>\n"\
474 " <module-set>complete</module-set>\n"\
475 " </schema>\n"\
476 " <content-id>9</content-id>\n"\
477 "</yang-library>\n"\
478 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
479 " <module-set-id>12</module-set-id>\n"\
480 " <module>\n"\
481 " <name>ietf-yang-metadata</name>\n"\
482 " <revision>2016-08-05</revision>\n"\
483 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
484 " <conformance-type>import</conformance-type>\n"\
485 " </module>\n"\
486 " <module>\n"\
487 " <name>yang</name>\n"\
488 " <revision>2020-06-17</revision>\n"\
489 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
490 " <conformance-type>implement</conformance-type>\n"\
491 " </module>\n"\
492 " <module>\n"\
493 " <name>ietf-inet-types</name>\n"\
494 " <revision>2013-07-15</revision>\n"\
495 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
496 " <conformance-type>import</conformance-type>\n"\
497 " </module>\n"\
498 " <module>\n"\
499 " <name>ietf-yang-types</name>\n"\
500 " <revision>2013-07-15</revision>\n"\
501 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
502 " <conformance-type>import</conformance-type>\n"\
503 " </module>\n"\
504 " <module>\n"\
505 " <name>ietf-yang-library</name>\n"\
506 " <revision>2019-01-04</revision>\n"\
507 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
508 " <conformance-type>implement</conformance-type>\n"\
509 " </module>\n"\
510 " <module>\n"\
511 " <name>ietf-datastores</name>\n"\
512 " <revision>2018-02-14</revision>\n"\
513 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
514 " <conformance-type>import</conformance-type>\n"\
515 " </module>\n"
516
517 const char *yanglibrary_only =
518 DATA_YANG_LIBRARY_START
519 DATA_YANG_BASE_IMPORTS
520 DATA_YANG_SCHEMA_MODULE_STATE
521 "</modules-state>\n";
522
523 const char *with_netconf =
524 DATA_YANG_LIBRARY_START
525 " <module>\n"
526 " <name>ietf-netconf</name>\n"
527 " <revision>2011-06-01</revision>\n"
528 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
529 " </module>\n"
530 DATA_YANG_BASE_IMPORTS
531 " <import-only-module>\n"
532 " <name>ietf-netconf-acm</name>\n"
533 " <revision>2018-02-14</revision>\n"
534 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
535 " </import-only-module>\n"
536 DATA_YANG_SCHEMA_MODULE_STATE
537 " <module>\n"
538 " <name>ietf-netconf</name>\n"
539 " <revision>2011-06-01</revision>\n"
540 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
541 " <conformance-type>implement</conformance-type>\n"
542 " </module>\n"
543 " <module>\n"
544 " <name>ietf-netconf-acm</name>\n"
545 " <revision>2018-02-14</revision>\n"
546 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
547 " <conformance-type>import</conformance-type>\n"
548 " </module>\n"
549 "</modules-state>";
550
551 const char *with_netconf_features =
552 DATA_YANG_LIBRARY_START
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 " <feature>writable-running</feature>\n"
558 " <feature>candidate</feature>\n"
559 " <feature>confirmed-commit</feature>\n"
560 " <feature>rollback-on-error</feature>\n"
561 " <feature>validate</feature>\n"
562 " <feature>startup</feature>\n"
563 " <feature>url</feature>\n"
564 " <feature>xpath</feature>\n"
565 " </module>\n"
566 " <import-only-module>\n"
567 " <name>ietf-yang-metadata</name>\n"
568 " <revision>2016-08-05</revision>\n"
569 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
570 " </import-only-module>\n"
571 " <import-only-module>\n"
572 " <name>ietf-inet-types</name>\n"
573 " <revision>2013-07-15</revision>\n"
574 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
575 " </import-only-module>\n"
576 " <import-only-module>\n"
577 " <name>ietf-yang-types</name>\n"
578 " <revision>2013-07-15</revision>\n"
579 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
580 " </import-only-module>\n"
581 " <import-only-module>\n"
582 " <name>ietf-datastores</name>\n"
583 " <revision>2018-02-14</revision>\n"
584 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
585 " </import-only-module>\n"
586 " <import-only-module>\n"
587 " <name>ietf-netconf-acm</name>\n"
588 " <revision>2018-02-14</revision>\n"
589 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
590 " </import-only-module>\n"
591 DATA_YANG_SCHEMA_MODULE_STATE
592 " <module>\n"
593 " <name>ietf-netconf</name>\n"
594 " <revision>2011-06-01</revision>\n"
595 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
596 " <feature>writable-running</feature>\n"
597 " <feature>candidate</feature>\n"
598 " <feature>confirmed-commit</feature>\n"
599 " <feature>rollback-on-error</feature>\n"
600 " <feature>validate</feature>\n"
601 " <feature>startup</feature>\n"
602 " <feature>url</feature>\n"
603 " <feature>xpath</feature>\n"
604 " <conformance-type>implement</conformance-type>\n"
605 " </module>\n"
606 " <module>\n"
607 " <name>ietf-netconf-acm</name>\n"
608 " <revision>2018-02-14</revision>\n"
609 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
610 " <conformance-type>import</conformance-type>\n"
611 " </module>\n"
612 "</modules-state>";
613
614 const char *garbage_revision =
615 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
616 " <module-set>\n"
617 " <name>complete</name>\n"
618 " <module>\n"
619 " <name>yang</name>\n"
620 " <revision>2020-06-17</revision>\n"
621 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
622 " </module>\n"
623 " <module>\n"
624 " <name>ietf-yang-library</name>\n"
625 " <revision>2019-01-01</revision>\n"
626 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
627 " </module>\n"
628 DATA_YANG_BASE_IMPORTS
629 DATA_YANG_SCHEMA_MODULE_STATE
630 "</modules-state>\n";
631
632 const char *no_yanglibrary =
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>2021-04-07</revision>\n"
639 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
640 " </module>\n"
641 DATA_YANG_BASE_IMPORTS
642 DATA_YANG_SCHEMA_MODULE_STATE
643 "</modules-state>\n";
644
645 (void) state;
646 /* seperate context to avoid double free during teadown */
647 struct ly_ctx *ctx_test = NULL;
648
649 /* test invalid parameters */
650 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
651 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
652 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
653
654 /* basic test with ietf-yang-library-only */
655 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 +0200656 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200657 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200658 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200659
660 /* test loading module, should also import other module */
661 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 +0200662 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200663 assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented);
Michal Vaskoad8d0f52021-04-26 13:21:24 +0200664 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200665 assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented);
666 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 +0200667 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200668
669 /* test loading module with feature if they are present */
670 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 +0200671 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
672 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200673 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 +0200674 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200675
676 /* test with not matching revision */
677 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
678
679 /* test data containing ietf-yang-library which conflicts with the option */
680 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
681
682 /* test creating without ietf-yang-library */
683 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
684 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200685 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200686}
687
aPiecekbb96b802021-04-12 08:12:52 +0200688static LY_ERR
689check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
690{
691 const struct lysp_node *pnode;
692 const char ***iter;
693
694 pnode = (const struct lysp_node *)node->priv;
695 CHECK_POINTER(pnode, 1);
696 iter = (const char ***)data;
697 CHECK_POINTER(**iter, 1);
698 CHECK_STRING(pnode->name, **iter);
699 (*iter)++;
700
701 return LY_SUCCESS;
702}
703
704static LY_ERR
705check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
706{
707 CHECK_POINTER(node->priv, 0);
708 return LY_SUCCESS;
709}
710
aPiecekfcfec0f2021-04-23 12:47:30 +0200711static void
712check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
713{
714 LY_ARRAY_COUNT_TYPE u, v;
715 struct lysc_ext_substmt *substmts;
716 struct lysc_node *cnode;
717 const char **iter;
718 const char *check[] = {
719 "tmp_cont", "lf", NULL
720 };
721
722 LY_ARRAY_FOR(ext, u) {
723 substmts = ext[u].substmts;
724 LY_ARRAY_FOR(substmts, v) {
725 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
726 cnode = *(struct lysc_node **)substmts[v].storage;
727 iter = check;
728 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
729 }
730 }
731 }
732}
733
734static void
735check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
736{
737 LY_ARRAY_COUNT_TYPE u, v;
738 struct lysc_ext_substmt *substmts;
739 struct lysc_node *cnode;
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 if (cnode) {
747 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
748 }
749 }
750 }
751 }
752}
753
aPiecekbb96b802021-04-12 08:12:52 +0200754/**
755 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
756 */
757static void
758test_set_priv_parsed(void **state)
759{
Michal Vasko4de7d072021-07-09 09:13:18 +0200760 struct lys_module *mod;
aPiecekbb96b802021-04-12 08:12:52 +0200761 const char *schema_a;
762 const char **iter;
763 const char *check[] = {
764 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
765 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
766 "l2c", "l2cx", "ch", "cas", "casx", "oper",
767 "input", "inparam", "output", "outparam", "n1", NULL
768 };
769
770 /* each node must have a unique name. */
771 schema_a = "module a {\n"
772 " namespace urn:tests:a;\n"
773 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200774 "\n"
775 " import ietf-restconf {\n"
776 " prefix rc;\n"
777 " revision-date 2017-01-26;\n"
778 " }\n"
779 "\n"
780 " rc:yang-data \"tmp\" {\n"
781 " container tmp_cont {\n"
782 " leaf lf {\n"
783 " type string;\n"
784 " }\n"
785 " }\n"
786 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200787 " container cont {\n"
788 " notification contnotif;\n"
789 " leaf-list contx {\n"
790 " type string;\n"
791 " }\n"
792 " uses grp;\n"
793 " }\n"
794 " list l1 {\n"
795 " key \"l1a l1b\";\n"
796 " leaf l1a {\n"
797 " type string;\n"
798 " }\n"
799 " leaf l1b {\n"
800 " type string;\n"
801 " }\n"
802 " leaf l1c {\n"
803 " type string;\n"
804 " }\n"
805 " }\n"
806 " feature f1;\n"
807 " feature f2;\n"
808 " leaf foo1 {\n"
809 " type uint16;\n"
810 " if-feature f1;\n"
811 " }\n"
812 " leaf foo2 {\n"
813 " type uint16;\n"
814 " }\n"
815 " leaf foo3 {\n"
816 " type uint16;\n"
817 " if-feature f2;\n"
818 " }\n"
819 " leaf-list ll {\n"
820 " type string;\n"
821 " }\n"
822 " anydata any {\n"
823 " config false;\n"
824 " }\n"
825 " list l2 {\n"
826 " config false;\n"
827 " container l2c {\n"
828 " leaf l2cx {\n"
829 " type string;\n"
830 " }\n"
831 " }\n"
832 " }\n"
833 " choice ch {\n"
834 " case cas {\n"
835 " leaf casx {\n"
836 " type string;\n"
837 " }\n"
838 " }\n"
839 " }\n"
840 " rpc oper {\n"
841 " input {\n"
842 " leaf inparam {\n"
843 " type string;\n"
844 " }\n"
845 " }\n"
846 " output {\n"
847 " leaf outparam {\n"
848 " type int8;\n"
849 " }\n"
850 " }\n"
851 " }\n"
852 " notification n1;\n"
853 " grouping grp {\n"
854 " leaf grpleaf {\n"
855 " type uint16;\n"
856 " }\n"
857 " }\n"
858 " augment /cont {\n"
859 " leaf augleaf {\n"
860 " type uint16;\n"
861 " }\n"
862 " }\n"
863 " deviation /a:foo2 {\n"
864 " deviate not-supported;\n"
865 " }\n"
866 "}\n";
867
868 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200869 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200870 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200871
aPiecekbb96b802021-04-12 08:12:52 +0200872 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200873 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
874 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200875 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
876
877 print_message("[ ] create context\n");
878 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
879 iter = check;
880 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200881 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200882
883 print_message("[ ] unset option\n");
884 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
885 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
886 iter = check;
887 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200888 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200889
890 print_message("[ ] set option\n");
891 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
892 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
893 iter = check;
894 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200895 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200896}
897
Michal Vasko01db7de2021-04-16 12:23:30 +0200898static void
899test_explicit_compile(void **state)
900{
901 uint32_t i;
Michal Vasko4de7d072021-07-09 09:13:18 +0200902 struct lys_module *mod;
Michal Vasko01db7de2021-04-16 12:23:30 +0200903 const char *schema_a = "module a {\n"
904 " namespace urn:tests:a;\n"
905 " prefix a;yang-version 1.1;\n"
906 " feature f1;\n"
907 " feature f2;\n"
908 " leaf foo1 {\n"
909 " type uint16;\n"
910 " if-feature f1;\n"
911 " }\n"
912 " leaf foo2 {\n"
913 " type uint16;\n"
914 " }\n"
915 " container cont {\n"
916 " leaf foo3 {\n"
917 " type string;\n"
918 " }\n"
919 " }\n"
920 "}\n";
921 const char *schema_b = "module b {\n"
922 " namespace urn:tests:b;\n"
923 " prefix b;yang-version 1.1;\n"
924 " import a {\n"
925 " prefix a;\n"
926 " }\n"
927 " augment /a:cont {\n"
928 " leaf augleaf {\n"
929 " type uint16;\n"
930 " }\n"
931 " }\n"
932 "}\n";
933 const char *schema_c = "module c {\n"
934 " namespace urn:tests:c;\n"
935 " prefix c;yang-version 1.1;\n"
936 " import a {\n"
937 " prefix a;\n"
938 " }\n"
939 " deviation /a:foo2 {\n"
940 " deviate not-supported;\n"
941 " }\n"
942 "}\n";
943
944 /* use own context with extra flags */
945 ly_ctx_destroy(UTEST_LYCTX);
946 const char *feats[] = {"f1", NULL};
947
948 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
949 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
950 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
951 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
952 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
953
Michal Vaskodd992582021-06-10 14:34:57 +0200954 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +0200955 i = 0;
956 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +0200957 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +0200958 }
959
960 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
961
962 /* check internal modules */
963 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
964 assert_non_null(mod);
965 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
966 assert_non_null(mod);
967 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
968 assert_non_null(mod);
969
970 /* check test modules */
971 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
972 assert_non_null(mod);
973 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
974 assert_non_null(mod);
975 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
976 assert_non_null(mod);
977}
978
Radek Iša56ca9e42020-09-08 18:42:00 +0200979int
980main(void)
981{
982 const struct CMUnitTest tests[] = {
983 UTEST(test_searchdirs),
984 UTEST(test_options),
985 UTEST(test_models),
986 UTEST(test_imports),
987 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200988 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +0200989 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +0200990 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +0200991 };
992
993 return cmocka_run_group_tests(tests, NULL, NULL);
994}