blob: de535f0bf862b38f6ba22d8803b069af22a3ca23 [file] [log] [blame]
Radek Iša56ca9e42020-09-08 18:42:00 +02001/*
2 * @file set.c
3 * @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"
21#include "tests/config.h"
22#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 */
102 ly_ctx_destroy(UTEST_LYCTX, NULL);
103
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 */
117 ly_ctx_destroy(UTEST_LYCTX, NULL);
118
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 */
208 ly_ctx_destroy(UTEST_LYCTX, NULL);
209
210 /* invalid arguments */
211 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
212 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_set_id()).", NULL);
213
214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
215 assert_int_equal(UTEST_LYCTX->module_set_id, ly_ctx_get_module_set_id(UTEST_LYCTX));
216
217 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
218 assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in, 4, 1, NULL, NULL, NULL, &unres, &mod1));
219 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
220 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));
237 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
238 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
239 ly_in_free(in, 0);
240 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
241
242 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in));
243 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
244 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
245 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
246 ly_in_free(in, 0);
247 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
248 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
249 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
250 ly_in_free(in, 0);
251 CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1.");
252
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));
255 assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
256 lys_compile_unres_glob_revert(UTEST_LYCTX, &unres);
257 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
258 ly_in_free(in, 0);
259 CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL,
260 "Name collision between submodules of name \"y\".", "Line number 1.");
261
262 /* selecting correct revision of the submodules */
263 ly_ctx_reset_latests(UTEST_LYCTX);
264 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
265 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
266 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
267 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
268 ly_in_free(in, 0);
269 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
270
271 /* reloading module in case only the compiled module resists in the context */
272 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
273 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod1));
274 ly_in_free(in, 0);
275 mod1->implemented = 1;
276 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0, &unres));
277 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
278 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
279 assert_non_null(mod1->compiled);
280 assert_non_null(mod1->parsed);
281
282#if 0
283 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
284
285 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));
286 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
287 ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL);
288 assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
289 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
290 CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL);
291 assert_null(mod2);
292 ly_in_free(in, 0);
293#endif
294
295 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));
296 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
297 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod2));
298 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
299 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
300 ly_in_free(in, 0);
301 assert_non_null(mod2);
302 assert_non_null(mod1->parsed);
303 assert_string_equal("w", mod1->name);
304}
305
306static void
307test_imports(void **state)
308{
309 const struct lys_module *mod1, *mod2, *import;
310
311 /* use own context with extra flags */
312 ly_ctx_destroy(UTEST_LYCTX, NULL);
313
314 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
315 * from the callback should be loaded into the context and used as an import */
316 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
317 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
318 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
319 LYS_IN_YANG, &mod1));
320 assert_int_equal(1, mod1->latest_revision);
321 assert_int_equal(1, mod1->implemented);
322 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
323 LYS_IN_YANG, &mod2));
324 import = mod2->parsed->imports[0].module;
325 assert_int_equal(2, import->latest_revision);
326 assert_int_equal(0, mod1->latest_revision);
327 assert_ptr_not_equal(mod1, import);
328 assert_string_equal("2019-09-17", import->revision);
329 assert_int_equal(0, import->implemented);
330 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16"));
331 ly_ctx_destroy(UTEST_LYCTX, NULL);
332
333 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
334 * already present in the context should be selected and the callback's revision should not be loaded into the context */
335 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX));
336 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
337 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
338 LYS_IN_YANG, &mod1));
339 assert_int_equal(1, mod1->latest_revision);
340 assert_int_equal(1, mod1->implemented);
341 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
342 LYS_IN_YANG, &mod2));
343 import = mod2->parsed->imports[0].module;
344 assert_ptr_equal(mod1, import);
345 assert_int_equal(2, import->latest_revision);
346 assert_int_equal(1, import->implemented);
347 assert_string_equal("2019-09-18", import->revision);
348 assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17"));
349}
350
351static void
352test_get_models(void **state)
353{
354 struct lys_module *mod, *mod2;
355 const char *str0 = "module a {namespace urn:a;prefix a;}";
356 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
357 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
358 struct ly_in *in0, *in1, *in2;
359 struct lys_glob_unres unres = {0};
360
361 unsigned int index = 0;
362 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
363
364 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
365 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
366 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
367
368 /* invalid arguments */
369 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
370 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL);
371 assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL));
372 CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL);
373 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
374 CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL);
375 assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL));
376 CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL);
377 assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL));
378
379 /* internal modules */
380 assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types"));
381 mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang");
382 assert_non_null(mod);
383 assert_non_null(mod->parsed);
384 assert_string_equal("yang", mod->name);
385 mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns);
386 assert_ptr_equal(mod, mod2);
387 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05"));
388 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15"));
389 assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15"));
390 assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
391
392 /* select module by revision */
393 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
394 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres));
395 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
396 /* invalid attempts - implementing module of the same name and inserting the same module */
397 assert_int_equal(LY_EDENIED, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, NULL));
398 CHECK_LOG_CTX("Module \"a@2018-10-23\" is already implemented in the context.", NULL);
399 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
400 ly_in_reset(in1);
401 /* it is already there, fine */
402 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, NULL));
403 /* insert the second module only as imported, not implemented */
404 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
405 ly_in_reset(in2);
406 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
407 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
408 assert_non_null(mod2);
409 assert_ptr_not_equal(mod, mod2);
410 mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a");
411 assert_ptr_equal(mod, mod2);
412 mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns);
413 assert_ptr_equal(mod, mod2);
414 /* work with module with no revision */
415 assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in0, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod));
416 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
417 assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL));
418 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a"));
419
420 str1 = "submodule b {belongs-to a {prefix a;}}";
421 ly_in_free(in1, 0);
422 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
423 assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
424 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
425 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
426
427 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) {
428 assert_string_equal(names[index - 1], mod->name);
429 }
430 assert_int_equal(9, index);
431
432 /* cleanup */
433 ly_in_free(in0, 0);
434 ly_in_free(in1, 0);
435 ly_in_free(in2, 0);
436}
437
438int
439main(void)
440{
441 const struct CMUnitTest tests[] = {
442 UTEST(test_searchdirs),
443 UTEST(test_options),
444 UTEST(test_models),
445 UTEST(test_imports),
446 UTEST(test_get_models),
447 };
448
449 return cmocka_run_group_tests(tests, NULL, NULL);
450}