blob: 7899079238f66a7ad2885a3815ac4bb16628b6eb [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
Jan Kundráte2789d32021-12-13 18:28:45 +010025#ifdef _WIN32
26static void
27slashes_to_backslashes(char *path)
28{
29 while ((path = strchr(path, '/'))) {
30 *path++ = '\\';
31 }
32}
33
34static void
35test_searchdirs(void **state)
36{
37 const char * const *list;
38 char *path1 = strdup(TESTS_BIN "/utests");
39 char *path2 = strdup(TESTS_SRC);
40
41 slashes_to_backslashes(path1);
42 slashes_to_backslashes(path2);
43
44 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
45 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
46 assert_null(ly_ctx_get_searchdirs(NULL));
47 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
48 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
49 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
50
51 /* correct path */
52 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path1));
53 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
54 assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]);
55
56 /* duplicated paths */
57 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, path1));
58 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
59 assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]);
60
61 /* another path */
62 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path2));
63 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
64 assert_string_equal(path2, UTEST_LYCTX->search_paths.objs[1]);
65
66 /* get searchpaths */
67 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
68 assert_non_null(list);
69 assert_string_equal(path1, list[0]);
70 assert_string_equal(path2, list[1]);
71 assert_null(list[2]);
72
73 /* removing searchpaths */
74 /* nonexisting */
75 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
76 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
77
78 /* first */
79 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path1));
80 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
81 assert_string_not_equal(path1, list[0]);
82
83 /* second */
84 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path2));
85 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
86
87 free(path1);
88 free(path2);
89}
90
91#else
92
Radek Iša56ca9e42020-09-08 18:42:00 +020093static void
94test_searchdirs(void **state)
95{
96 const char * const *list;
97
98 /* invalid arguments */
99 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
100 CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL);
101 assert_null(ly_ctx_get_searchdirs(NULL));
102 CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL);
103 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
104 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL);
105
106 /* readable and executable, but not a directory */
107 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context"));
108 CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 /* not existing */
110 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile"));
111 CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
112
113 /* ly_set_add() fails */
114 /* no change */
115 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL));
116
117 /* correct path */
118 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
119 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
120 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
121
122 /* duplicated paths */
123 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
124 assert_int_equal(1, UTEST_LYCTX->search_paths.count);
125 assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]);
126
127 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
128 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles"));
129 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src"));
130 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules"));
131 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc"));
132 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC));
133 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN));
134 assert_int_equal(7, UTEST_LYCTX->search_paths.count);
135
136 /* get searchpaths */
137 list = ly_ctx_get_searchdirs(UTEST_LYCTX);
138 assert_non_null(list);
139 assert_string_equal(TESTS_BIN "/utests", list[0]);
140 assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]);
141 assert_string_equal(TESTS_SRC, list[5]);
142 assert_string_equal(TESTS_BIN, list[6]);
143 assert_null(list[7]);
144
145 /* removing searchpaths */
146 /* nonexisting */
147 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile"));
148 CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL);
149 /* first */
150 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests"));
151 assert_string_not_equal(TESTS_BIN "/utests", list[0]);
152 assert_int_equal(6, UTEST_LYCTX->search_paths.count);
153 /* middle */
154 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC));
155 assert_int_equal(5, UTEST_LYCTX->search_paths.count);
156 /* last */
157 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN));
158 assert_int_equal(4, UTEST_LYCTX->search_paths.count);
159 /* all */
160 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
161 assert_int_equal(0, UTEST_LYCTX->search_paths.count);
162
163 /* again - no change */
164 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL));
165
166 /* cleanup */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200167 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200168
169 /* test searchdir list in ly_ctx_new() */
170 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX));
171 CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
Jan Kundrát34bb9282021-12-13 18:45:50 +0100172 assert_int_equal(LY_SUCCESS,
173 ly_ctx_new(TESTS_SRC PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_SRC,
174 LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 assert_int_equal(2, UTEST_LYCTX->search_paths.count);
176 assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]);
177 assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]);
178}
179
Jan Kundráte2789d32021-12-13 18:28:45 +0100180#endif
181
Radek Iša56ca9e42020-09-08 18:42:00 +0200182static void
183test_options(void **state)
184{
185 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200186 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200187
188 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX));
189
190 /* invalid arguments */
191 assert_int_equal(0, ly_ctx_get_options(NULL));
192 CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL);
193
194 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
195 CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL);
196 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
197 CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL);
198
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 /* unset */
200 /* LY_CTX_ALL_IMPLEMENTED */
201 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
202 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
203 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
204
205 /* LY_CTX_REF_IMPLEMENTED */
206 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
207 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
208 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
209
210 /* LY_CTX_DISABLE_SEARCHDIRS */
211 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
212 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
213 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
214
215 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
216 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
217 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
218 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
219
220 /* LY_CTX_PREFER_SEARCHDIRS */
221 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
222 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
223 assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
224
225 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
226
227 /* set back */
228 /* LY_CTX_ALL_IMPLEMENTED */
229 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED));
230 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED);
231
232 /* LY_CTX_REF_IMPLEMENTED */
233 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED));
234 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED);
235
236 /* LY_CTX_DISABLE_SEARCHDIRS */
237 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS));
238 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS);
239
240 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
241 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD));
242 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
243
244 /* LY_CTX_PREFER_SEARCHDIRS */
245 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
246 assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
247
248 assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
249}
250
251static LY_ERR
252test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
253 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
254 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
255{
256 *module_data = user_data;
257 *format = LYS_IN_YANG;
258 *free_module_data = NULL;
259 return LY_SUCCESS;
260}
261
262static void
263test_models(void **state)
264{
265 struct ly_in *in;
266 const char *str;
267 struct lys_module *mod1, *mod2;
268 struct lys_glob_unres unres = {0};
269
270 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200271 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200272
273 /* invalid arguments */
Michal Vasko794ab4b2021-03-31 09:42:19 +0200274 assert_int_equal(0, ly_ctx_get_change_count(NULL));
275 CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200276
277 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
Michal Vasko794ab4b2021-03-31 09:42:19 +0200278 assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200279
280 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200281 assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1));
Michal Vaskof4258e12021-06-15 12:11:42 +0200282 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 ly_in_free(in, 0);
284 CHECK_LOG_CTX("Invalid schema input format.", NULL);
285
286 /* import callback */
287 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test"));
288 assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb);
289 assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data);
290 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str));
291 assert_string_equal("test", str);
292
293 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
294 assert_null(UTEST_LYCTX->imp_clb);
295 assert_null(UTEST_LYCTX->imp_clb_data);
296
297 /* name collision of module and submodule */
298 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
299 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 +0200300 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 +0200301 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200302 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200303 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
304 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200305
306 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 +0200307 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 +0200308 ly_in_free(in, 0);
309 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vaskodd992582021-06-10 14:34:57 +0200310 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 +0200311 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200312 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200313 CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL,
314 "Name collision between module and submodule of name \"y\".", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200315
316 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
317 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 +0200318 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 +0200319 lys_unres_glob_revert(UTEST_LYCTX, &unres);
320 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200321 ly_in_free(in, 0);
Radek Krejci8297b792020-08-16 14:49:05 +0200322 CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL,
323 "Including \"y\" submodule into \"b\" failed.", NULL,
324 "Parsing submodule failed.", NULL,
Radek Iša56ca9e42020-09-08 18:42:00 +0200325 "Name collision between submodules of name \"y\".", "Line number 1.");
326
327 /* selecting correct revision of the submodules */
328 ly_ctx_reset_latests(UTEST_LYCTX);
329 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
330 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 +0200331 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 +0200332 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200333 ly_in_free(in, 0);
334 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
335
336 /* reloading module in case only the compiled module resists in the context */
337 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 +0200338 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod1));
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 ly_in_free(in, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 assert_non_null(mod1->compiled);
341 assert_non_null(mod1->parsed);
342
343#if 0
344 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
345
346 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));
347 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
348 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
349 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
350 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
351 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
352 assert_null(mod2);
353 ly_in_free(in, 0);
354#endif
355
356 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));
357 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 +0200358 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200359 ly_in_free(in, 0);
360 assert_non_null(mod2);
361 assert_non_null(mod1->parsed);
362 assert_string_equal("w", mod1->name);
363}
364
365static void
366test_imports(void **state)
367{
aPiecekd4911ee2021-07-30 07:40:24 +0200368 struct lys_module *mod1, *mod2, *mod3, *import;
aPiecek9f8c7e72021-07-28 12:01:56 +0200369 char *str;
aPiecekd4911ee2021-07-30 07:40:24 +0200370 uint16_t ctx_options;
Radek Iša56ca9e42020-09-08 18:42:00 +0200371
372 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200373 ly_ctx_destroy(UTEST_LYCTX);
aPiecekd4911ee2021-07-30 07:40:24 +0200374 ctx_options = LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_NO_YANGLIBRARY;
Radek Iša56ca9e42020-09-08 18:42:00 +0200375
aPiecekd4911ee2021-07-30 07:40:24 +0200376 /* Import callback provides newer revision of module 'a',
377 * however the older revision is implemented soon and therefore it is preferred. */
378 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
Radek Iša56ca9e42020-09-08 18:42:00 +0200379 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
380 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
381 LYS_IN_YANG, &mod1));
aPiecek8ca21bd2021-07-26 14:31:01 +0200382 assert_true(LYS_MOD_LATEST_REV & mod1->latest_revision);
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 assert_int_equal(1, mod1->implemented);
384 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
385 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200386 assert_ptr_equal(mod1, mod2->parsed->imports[0].module);
387 assert_true((LYS_MOD_LATEST_REV | LYS_MOD_IMPORTED_REV) & mod1->latest_revision);
388 assert_string_equal("2019-09-16", mod1->revision);
389 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200391 ly_ctx_destroy(UTEST_LYCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200392
aPiecekd4911ee2021-07-30 07:40:24 +0200393 /* Import callback provides older revision of module 'a' and it is
394 * imported by another module, so it is preferred even if newer
395 * revision is implemented later. */
396 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
397 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 +0200398 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
399 LYS_IN_YANG, &mod2));
aPiecekd4911ee2021-07-30 07:40:24 +0200400 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-17;}",
401 LYS_IN_YANG, &mod1));
402 ly_log_level(LY_LLVRB);
403 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c;import a {prefix a;}}",
404 LYS_IN_YANG, &mod3));
405 CHECK_LOG("Implemented module \"a@2019-09-17\" is not used for import, revision \"2019-09-16\" is imported instead.", NULL);
406 ly_log_level(LY_LLWRN);
407 assert_true(LYS_MOD_LATEST_SEARCHDIRS & mod1->latest_revision);
408 assert_int_equal(1, mod1->implemented);
Radek Iša56ca9e42020-09-08 18:42:00 +0200409 import = mod2->parsed->imports[0].module;
aPiecekd4911ee2021-07-30 07:40:24 +0200410 assert_true(LYS_MOD_IMPORTED_REV & import->latest_revision);
411 assert_string_equal("2019-09-16", import->revision);
412 assert_int_equal(0, import->implemented);
413 import = mod3->parsed->imports[0].module;
414 assert_string_equal("2019-09-16", import->revision);
415 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
416 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
417 assert_string_equal("2019-09-17", ly_ctx_get_module_implemented(UTEST_LYCTX, "a")->revision);
aPiecek9f8c7e72021-07-28 12:01:56 +0200418 ly_ctx_destroy(UTEST_LYCTX);
419
420 /* check of circular dependency */
aPiecekd4911ee2021-07-30 07:40:24 +0200421 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX));
aPiecek9f8c7e72021-07-28 12:01:56 +0200422 str = "module a {namespace urn:a; prefix a;"
423 "import b {prefix b;}"
424 "}";
425 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, str);
426 str = "module b { yang-version 1.1; namespace urn:b; prefix b;"
427 "import a {prefix a;}"
428 "}";
429 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200430}
431
432static void
433test_get_models(void **state)
434{
435 struct lys_module *mod, *mod2;
436 const char *str0 = "module a {namespace urn:a;prefix a;}";
437 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
438 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
439 struct ly_in *in0, *in1, *in2;
440 struct lys_glob_unres unres = {0};
441
442 unsigned int index = 0;
443 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
444
445 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
446 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
447 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
448
449 /* invalid arguments */
450 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
451 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
452 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
453 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
454 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
455 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
456 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
457 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
458 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
459
460 /* internal modules */
461 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
462 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
463 assert_non_null(mod);
464 assert_non_null(mod->parsed);
465 assert_string_equal("yang", mod->name);
466 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
467 assert_ptr_equal(mod, mod2);
468 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
469 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
470 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
471 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
472
473 /* select module by revision */
Michal Vasko4de7d072021-07-09 09:13:18 +0200474 assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vaskodd992582021-06-10 14:34:57 +0200476 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 +0200477 assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres));
Michal Vasko978532b2022-01-21 14:13:40 +0100478 CHECK_LOG_CTX("Module \"a@2018-10-24\" is already implemented in revision \"2018-10-23\".", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200479 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200480 ly_in_reset(in1);
481 /* it is already there, fine */
Michal Vaskodd992582021-06-10 14:34:57 +0200482 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 +0200483 /* insert the second module only as imported, not implemented */
Michal Vaskof4258e12021-06-15 12:11:42 +0200484 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200485 ly_in_reset(in2);
Michal Vaskodd992582021-06-10 14:34:57 +0200486 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 +0200487 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200488 assert_non_null(mod2);
489 assert_ptr_not_equal(mod, mod2);
490 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
491 assert_ptr_equal(mod, mod2);
492 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
493 assert_ptr_equal(mod, mod2);
494 /* work with module with no revision */
Michal Vaskodd992582021-06-10 14:34:57 +0200495 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 +0200496 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
498 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
499
500 str1 = "submodule b {belongs-to a {prefix a;}}";
501 ly_in_free(in1, 0);
502 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vaskodd992582021-06-10 14:34:57 +0200503 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 +0200504 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +0200505 lys_unres_glob_erase(&unres);
Radek Iša56ca9e42020-09-08 18:42:00 +0200506
507 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
508 assert_string_equal(names[index - 1], mod->name);
509 }
510 assert_int_equal(9, index);
511
512 /* cleanup */
513 ly_in_free(in0, 0);
514 ly_in_free(in1, 0);
515 ly_in_free(in2, 0);
516}
517
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200518static void
519test_ylmem(void **state)
520{
521#define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
522 " <module-set>\n"\
523 " <name>complete</name>\n"\
524 " <module>\n"\
525 " <name>yang</name>\n"\
526 " <revision>2021-04-07</revision>\n"\
527 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
528 " </module>\n"\
529 " <module>\n"\
530 " <name>ietf-yang-library</name>\n"\
531 " <revision>2019-01-04</revision>\n"\
532 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
533 " </module>\n"
534
535#define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\
536 " <name>ietf-yang-metadata</name>\n"\
537 " <revision>2016-08-05</revision>\n"\
538 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
539 " </import-only-module>\n"\
540 " <import-only-module>\n"\
541 " <name>ietf-inet-types</name>\n"\
542 " <revision>2013-07-15</revision>\n"\
543 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
544 " </import-only-module>\n"\
545 " <import-only-module>\n"\
546 " <name>ietf-yang-types</name>\n"\
547 " <revision>2013-07-15</revision>\n"\
548 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
549 " </import-only-module>\n"\
550 " <import-only-module>\n"\
551 " <name>ietf-datastores</name>\n"\
552 " <revision>2018-02-14</revision>\n"\
553 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
554 " </import-only-module>\n"
555
556#define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\
557 " <schema>\n"\
558 " <name>complete</name>\n"\
559 " <module-set>complete</module-set>\n"\
560 " </schema>\n"\
561 " <content-id>9</content-id>\n"\
562 "</yang-library>\n"\
563 "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\
564 " <module-set-id>12</module-set-id>\n"\
565 " <module>\n"\
566 " <name>ietf-yang-metadata</name>\n"\
567 " <revision>2016-08-05</revision>\n"\
568 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\
569 " <conformance-type>import</conformance-type>\n"\
570 " </module>\n"\
571 " <module>\n"\
572 " <name>yang</name>\n"\
573 " <revision>2020-06-17</revision>\n"\
574 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\
575 " <conformance-type>implement</conformance-type>\n"\
576 " </module>\n"\
577 " <module>\n"\
578 " <name>ietf-inet-types</name>\n"\
579 " <revision>2013-07-15</revision>\n"\
580 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\
581 " <conformance-type>import</conformance-type>\n"\
582 " </module>\n"\
583 " <module>\n"\
584 " <name>ietf-yang-types</name>\n"\
585 " <revision>2013-07-15</revision>\n"\
586 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\
587 " <conformance-type>import</conformance-type>\n"\
588 " </module>\n"\
589 " <module>\n"\
590 " <name>ietf-yang-library</name>\n"\
591 " <revision>2019-01-04</revision>\n"\
592 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\
593 " <conformance-type>implement</conformance-type>\n"\
594 " </module>\n"\
595 " <module>\n"\
596 " <name>ietf-datastores</name>\n"\
597 " <revision>2018-02-14</revision>\n"\
598 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\
599 " <conformance-type>import</conformance-type>\n"\
600 " </module>\n"
601
602 const char *yanglibrary_only =
603 DATA_YANG_LIBRARY_START
604 DATA_YANG_BASE_IMPORTS
605 DATA_YANG_SCHEMA_MODULE_STATE
606 "</modules-state>\n";
607
608 const char *with_netconf =
609 DATA_YANG_LIBRARY_START
610 " <module>\n"
611 " <name>ietf-netconf</name>\n"
612 " <revision>2011-06-01</revision>\n"
613 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
614 " </module>\n"
615 DATA_YANG_BASE_IMPORTS
616 " <import-only-module>\n"
617 " <name>ietf-netconf-acm</name>\n"
618 " <revision>2018-02-14</revision>\n"
619 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
620 " </import-only-module>\n"
621 DATA_YANG_SCHEMA_MODULE_STATE
622 " <module>\n"
623 " <name>ietf-netconf</name>\n"
624 " <revision>2011-06-01</revision>\n"
625 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
626 " <conformance-type>implement</conformance-type>\n"
627 " </module>\n"
628 " <module>\n"
629 " <name>ietf-netconf-acm</name>\n"
630 " <revision>2018-02-14</revision>\n"
631 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
632 " <conformance-type>import</conformance-type>\n"
633 " </module>\n"
634 "</modules-state>";
635
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200636 char *with_netconf_features = malloc(8096);
637 strcpy(with_netconf_features,
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200638 DATA_YANG_LIBRARY_START
639 " <module>\n"
640 " <name>ietf-netconf</name>\n"
641 " <revision>2011-06-01</revision>\n"
642 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
643 " <feature>writable-running</feature>\n"
644 " <feature>candidate</feature>\n"
645 " <feature>confirmed-commit</feature>\n"
646 " <feature>rollback-on-error</feature>\n"
647 " <feature>validate</feature>\n"
648 " <feature>startup</feature>\n"
649 " <feature>url</feature>\n"
650 " <feature>xpath</feature>\n"
651 " </module>\n"
652 " <import-only-module>\n"
653 " <name>ietf-yang-metadata</name>\n"
654 " <revision>2016-08-05</revision>\n"
655 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"
656 " </import-only-module>\n"
657 " <import-only-module>\n"
658 " <name>ietf-inet-types</name>\n"
659 " <revision>2013-07-15</revision>\n"
660 " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"
661 " </import-only-module>\n"
662 " <import-only-module>\n"
663 " <name>ietf-yang-types</name>\n"
664 " <revision>2013-07-15</revision>\n"
665 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"
666 " </import-only-module>\n"
667 " <import-only-module>\n"
668 " <name>ietf-datastores</name>\n"
669 " <revision>2018-02-14</revision>\n"
670 " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"
671 " </import-only-module>\n"
672 " <import-only-module>\n"
673 " <name>ietf-netconf-acm</name>\n"
674 " <revision>2018-02-14</revision>\n"
675 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200676 " </import-only-module>\n");
677 strcpy(with_netconf_features + strlen(with_netconf_features),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200678 DATA_YANG_SCHEMA_MODULE_STATE
679 " <module>\n"
680 " <name>ietf-netconf</name>\n"
681 " <revision>2011-06-01</revision>\n"
682 " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n"
683 " <feature>writable-running</feature>\n"
684 " <feature>candidate</feature>\n"
685 " <feature>confirmed-commit</feature>\n"
686 " <feature>rollback-on-error</feature>\n"
687 " <feature>validate</feature>\n"
688 " <feature>startup</feature>\n"
689 " <feature>url</feature>\n"
690 " <feature>xpath</feature>\n"
691 " <conformance-type>implement</conformance-type>\n"
692 " </module>\n"
693 " <module>\n"
694 " <name>ietf-netconf-acm</name>\n"
695 " <revision>2018-02-14</revision>\n"
696 " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n"
697 " <conformance-type>import</conformance-type>\n"
698 " </module>\n"
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200699 "</modules-state>");
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200700
701 const char *garbage_revision =
702 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
703 " <module-set>\n"
704 " <name>complete</name>\n"
705 " <module>\n"
706 " <name>yang</name>\n"
707 " <revision>2020-06-17</revision>\n"
708 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
709 " </module>\n"
710 " <module>\n"
711 " <name>ietf-yang-library</name>\n"
712 " <revision>2019-01-01</revision>\n"
713 " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"
714 " </module>\n"
715 DATA_YANG_BASE_IMPORTS
716 DATA_YANG_SCHEMA_MODULE_STATE
717 "</modules-state>\n";
718
719 const char *no_yanglibrary =
720 "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"
721 " <module-set>\n"
722 " <name>complete</name>\n"
723 " <module>\n"
724 " <name>yang</name>\n"
725 " <revision>2021-04-07</revision>\n"
726 " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"
727 " </module>\n"
728 DATA_YANG_BASE_IMPORTS
729 DATA_YANG_SCHEMA_MODULE_STATE
730 "</modules-state>\n";
731
732 (void) state;
733 /* seperate context to avoid double free during teadown */
734 struct ly_ctx *ctx_test = NULL;
735
736 /* test invalid parameters */
737 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test));
738 assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL));
739 assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test));
740
741 /* basic test with ietf-yang-library-only */
742 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 +0200743 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200744 assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200745 ly_ctx_destroy(ctx_test);
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);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200755
756 /* test loading module with feature if they are present */
757 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 +0200758 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"));
759 assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14"));
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200760 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 +0200761 ly_ctx_destroy(ctx_test);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200762
763 /* test with not matching revision */
764 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test));
765
766 /* test data containing ietf-yang-library which conflicts with the option */
767 assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
768
769 /* test creating without ietf-yang-library */
770 assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test));
771 assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04"));
Radek Krejci90ed21e2021-04-12 14:47:46 +0200772 ly_ctx_destroy(ctx_test);
Michal Vasko22e0f3a2021-09-22 12:19:23 +0200773 free(with_netconf_features);
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +0200774}
775
aPiecekbb96b802021-04-12 08:12:52 +0200776static LY_ERR
777check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
778{
779 const struct lysp_node *pnode;
780 const char ***iter;
781
782 pnode = (const struct lysp_node *)node->priv;
783 CHECK_POINTER(pnode, 1);
784 iter = (const char ***)data;
785 CHECK_POINTER(**iter, 1);
786 CHECK_STRING(pnode->name, **iter);
787 (*iter)++;
788
789 return LY_SUCCESS;
790}
791
792static LY_ERR
793check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue))
794{
795 CHECK_POINTER(node->priv, 0);
796 return LY_SUCCESS;
797}
798
aPiecekfcfec0f2021-04-23 12:47:30 +0200799static void
800check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext)
801{
802 LY_ARRAY_COUNT_TYPE u, v;
803 struct lysc_ext_substmt *substmts;
804 struct lysc_node *cnode;
805 const char **iter;
806 const char *check[] = {
807 "tmp_cont", "lf", NULL
808 };
809
810 LY_ARRAY_FOR(ext, u) {
811 substmts = ext[u].substmts;
812 LY_ARRAY_FOR(substmts, v) {
813 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
814 cnode = *(struct lysc_node **)substmts[v].storage;
815 iter = check;
816 assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter));
817 }
818 }
819 }
820}
821
822static void
823check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext)
824{
825 LY_ARRAY_COUNT_TYPE u, v;
826 struct lysc_ext_substmt *substmts;
827 struct lysc_node *cnode;
828
829 LY_ARRAY_FOR(ext, u) {
830 substmts = ext[u].substmts;
831 LY_ARRAY_FOR(substmts, v) {
832 if (substmts && substmts[v].storage && LY_STMT_IS_NODE(substmts[v].stmt)) {
833 cnode = *(struct lysc_node **)substmts[v].storage;
834 if (cnode) {
835 CHECK_POINTER((struct lysp_node *)cnode->priv, 0);
836 }
837 }
838 }
839 }
840}
841
aPiecekbb96b802021-04-12 08:12:52 +0200842/**
843 * @brief Testing of LY_CTX_SET_PRIV_PARSED.
844 */
845static void
846test_set_priv_parsed(void **state)
847{
Michal Vasko4de7d072021-07-09 09:13:18 +0200848 struct lys_module *mod;
aPiecekbb96b802021-04-12 08:12:52 +0200849 const char *schema_a;
850 const char **iter;
851 const char *check[] = {
852 "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1",
853 "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2",
854 "l2c", "l2cx", "ch", "cas", "casx", "oper",
855 "input", "inparam", "output", "outparam", "n1", NULL
856 };
857
858 /* each node must have a unique name. */
859 schema_a = "module a {\n"
860 " namespace urn:tests:a;\n"
861 " prefix a;yang-version 1.1;\n"
aPiecekfcfec0f2021-04-23 12:47:30 +0200862 "\n"
863 " import ietf-restconf {\n"
864 " prefix rc;\n"
865 " revision-date 2017-01-26;\n"
866 " }\n"
867 "\n"
868 " rc:yang-data \"tmp\" {\n"
869 " container tmp_cont {\n"
870 " leaf lf {\n"
871 " type string;\n"
872 " }\n"
873 " }\n"
874 " }\n"
aPiecekbb96b802021-04-12 08:12:52 +0200875 " container cont {\n"
876 " notification contnotif;\n"
877 " leaf-list contx {\n"
878 " type string;\n"
879 " }\n"
880 " uses grp;\n"
881 " }\n"
882 " list l1 {\n"
883 " key \"l1a l1b\";\n"
884 " leaf l1a {\n"
885 " type string;\n"
886 " }\n"
887 " leaf l1b {\n"
888 " type string;\n"
889 " }\n"
890 " leaf l1c {\n"
891 " type string;\n"
892 " }\n"
893 " }\n"
894 " feature f1;\n"
895 " feature f2;\n"
896 " leaf foo1 {\n"
897 " type uint16;\n"
898 " if-feature f1;\n"
899 " }\n"
900 " leaf foo2 {\n"
901 " type uint16;\n"
902 " }\n"
903 " leaf foo3 {\n"
904 " type uint16;\n"
905 " if-feature f2;\n"
906 " }\n"
907 " leaf-list ll {\n"
908 " type string;\n"
909 " }\n"
910 " anydata any {\n"
911 " config false;\n"
912 " }\n"
913 " list l2 {\n"
914 " config false;\n"
915 " container l2c {\n"
916 " leaf l2cx {\n"
917 " type string;\n"
918 " }\n"
919 " }\n"
920 " }\n"
921 " choice ch {\n"
922 " case cas {\n"
923 " leaf casx {\n"
924 " type string;\n"
925 " }\n"
926 " }\n"
927 " }\n"
928 " rpc oper {\n"
929 " input {\n"
930 " leaf inparam {\n"
931 " type string;\n"
932 " }\n"
933 " }\n"
934 " output {\n"
935 " leaf outparam {\n"
936 " type int8;\n"
937 " }\n"
938 " }\n"
939 " }\n"
940 " notification n1;\n"
941 " grouping grp {\n"
942 " leaf grpleaf {\n"
943 " type uint16;\n"
944 " }\n"
945 " }\n"
946 " augment /cont {\n"
947 " leaf augleaf {\n"
948 " type uint16;\n"
949 " }\n"
950 " }\n"
951 " deviation /a:foo2 {\n"
952 " deviate not-supported;\n"
953 " }\n"
954 "}\n";
955
956 /* use own context with extra flags */
Radek Krejci90ed21e2021-04-12 14:47:46 +0200957 ly_ctx_destroy(UTEST_LYCTX);
aPiecekbb96b802021-04-12 08:12:52 +0200958 const char *feats[] = {"f1", NULL};
Radek Krejciddbc4812021-04-13 21:18:02 +0200959
aPiecekbb96b802021-04-12 08:12:52 +0200960 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX));
aPiecekfcfec0f2021-04-23 12:47:30 +0200961 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
962 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
aPiecekbb96b802021-04-12 08:12:52 +0200963 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL);
964
965 print_message("[ ] create context\n");
966 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
967 iter = check;
968 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200969 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200970
971 print_message("[ ] unset option\n");
972 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
973 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
974 iter = check;
975 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200976 check_ext_instance_priv_parsed_not_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200977
978 print_message("[ ] set option\n");
979 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED));
980 mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL);
981 iter = check;
982 assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter));
aPiecekfcfec0f2021-04-23 12:47:30 +0200983 check_ext_instance_priv_parsed_is_set(mod->compiled->exts);
aPiecekbb96b802021-04-12 08:12:52 +0200984}
985
Michal Vasko01db7de2021-04-16 12:23:30 +0200986static void
987test_explicit_compile(void **state)
988{
989 uint32_t i;
Michal Vasko4de7d072021-07-09 09:13:18 +0200990 struct lys_module *mod;
Michal Vasko01db7de2021-04-16 12:23:30 +0200991 const char *schema_a = "module a {\n"
992 " namespace urn:tests:a;\n"
993 " prefix a;yang-version 1.1;\n"
994 " feature f1;\n"
995 " feature f2;\n"
996 " leaf foo1 {\n"
997 " type uint16;\n"
998 " if-feature f1;\n"
999 " }\n"
1000 " leaf foo2 {\n"
1001 " type uint16;\n"
1002 " }\n"
1003 " container cont {\n"
1004 " leaf foo3 {\n"
1005 " type string;\n"
1006 " }\n"
1007 " }\n"
1008 "}\n";
1009 const char *schema_b = "module b {\n"
1010 " namespace urn:tests:b;\n"
1011 " prefix b;yang-version 1.1;\n"
1012 " import a {\n"
1013 " prefix a;\n"
1014 " }\n"
1015 " augment /a:cont {\n"
1016 " leaf augleaf {\n"
1017 " type uint16;\n"
1018 " }\n"
1019 " }\n"
1020 "}\n";
1021 const char *schema_c = "module c {\n"
1022 " namespace urn:tests:c;\n"
1023 " prefix c;yang-version 1.1;\n"
1024 " import a {\n"
1025 " prefix a;\n"
1026 " }\n"
1027 " deviation /a:foo2 {\n"
1028 " deviate not-supported;\n"
1029 " }\n"
1030 "}\n";
1031
1032 /* use own context with extra flags */
1033 ly_ctx_destroy(UTEST_LYCTX);
1034 const char *feats[] = {"f1", NULL};
1035
1036 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX));
1037 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
1038 UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
1039 UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL);
1040 assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats));
1041
Michal Vaskodd992582021-06-10 14:34:57 +02001042 /* none of the modules should be compiled */
Michal Vasko01db7de2021-04-16 12:23:30 +02001043 i = 0;
1044 while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) {
Michal Vaskodd992582021-06-10 14:34:57 +02001045 assert_null(mod->compiled);
Michal Vasko01db7de2021-04-16 12:23:30 +02001046 }
1047
1048 assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX));
1049
1050 /* check internal modules */
1051 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
1052 assert_non_null(mod);
1053 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores");
1054 assert_non_null(mod);
1055 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library");
1056 assert_non_null(mod);
1057
1058 /* check test modules */
1059 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a");
1060 assert_non_null(mod);
1061 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b");
1062 assert_non_null(mod);
1063 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c");
1064 assert_non_null(mod);
1065}
1066
Radek Iša56ca9e42020-09-08 18:42:00 +02001067int
1068main(void)
1069{
1070 const struct CMUnitTest tests[] = {
1071 UTEST(test_searchdirs),
1072 UTEST(test_options),
1073 UTEST(test_models),
1074 UTEST(test_imports),
1075 UTEST(test_get_models),
Tadeáš Vintrlíkea268182021-04-07 13:53:32 +02001076 UTEST(test_ylmem),
aPiecekbb96b802021-04-12 08:12:52 +02001077 UTEST(test_set_priv_parsed),
Michal Vasko01db7de2021-04-16 12:23:30 +02001078 UTEST(test_explicit_compile),
Radek Iša56ca9e42020-09-08 18:42:00 +02001079 };
1080
1081 return cmocka_run_group_tests(tests, NULL, NULL);
1082}