blob: a9dae30ee2610211f96081f1c688ac567a61ab45 [file] [log] [blame]
Radek Krejci1e880032018-09-20 12:17:12 +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
15#include "tests/config.h"
Radek Krejcif3f47842018-11-15 11:22:15 +010016#include "../../src/common.c"
17#include "../../src/log.c"
18#include "../../src/set.c"
19#include "../../src/hash_table.c"
20#include "../../src/xpath.c"
21#include "../../src/parser_yang.c"
Radek Krejci1e880032018-09-20 12:17:12 +020022#include "../../src/context.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "../../src/tree_schema_free.c"
25#include "../../src/tree_schema_compile.c"
Radek Krejcib7db73a2018-10-24 14:18:40 +020026#include "../../src/tree_schema.c"
Radek Krejci1e880032018-09-20 12:17:12 +020027
28#include <stdarg.h>
29#include <stddef.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include <string.h>
34#include <stdio.h>
35
36#include "libyang.h"
37
38#define BUFSIZE 1024
39char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010040int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020041
42/* set to 0 to printing error messages to stderr instead of checking them in code */
43#define ENABLE_LOGGER_CHECKING 1
44
45static void
46logger(LY_LOG_LEVEL level, const char *msg, const char *path)
47{
48 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010049 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010050 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010051 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
52 } else {
53 strncpy(logbuf, msg, BUFSIZE - 1);
54 }
55 if (store > 0) {
56 --store;
57 }
58 }
Radek Krejci1e880032018-09-20 12:17:12 +020059}
60
61static int
62logger_setup(void **state)
63{
64 (void) state; /* unused */
65#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010066 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020067#endif
68 return 0;
69}
70
71#if ENABLE_LOGGER_CHECKING
72# define logbuf_assert(str) assert_string_equal(logbuf, str)
73#else
74# define logbuf_assert(str)
75#endif
76
Radek Krejci1e880032018-09-20 12:17:12 +020077static void
78test_searchdirs(void **state)
79{
80 (void) state; /* unused */
81
82 struct ly_ctx *ctx;
83 const char * const *list;
84
85 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
86
87 /* invalid arguments */
88 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
89 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
90 assert_null(ly_ctx_get_searchdirs(NULL));
91 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +020092 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +020093 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +020094
95 /* readable and executable, but not a directory */
96 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context"));
97 logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory.");
98 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +020099 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
100 logbuf_assert("Unable to use search directory \""__FILE__"\" (Permission denied)");
Radek Krejci1e880032018-09-20 12:17:12 +0200101 /* not existing */
102 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
103 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
104
105 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200106 /* no change */
107 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
108
Radek Krejci1e880032018-09-20 12:17:12 +0200109 /* correct path */
Radek Krejci1e880032018-09-20 12:17:12 +0200110 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
111 assert_int_equal(1, ctx->search_paths.count);
112 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
113
Radek Krejci14946ab2018-09-20 13:42:06 +0200114 /* duplicated paths */
115 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200116 assert_int_equal(1, ctx->search_paths.count);
117 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
118
119 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
120 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
121 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
122 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200127 assert_int_equal(8, ctx->search_paths.count);
128
129 /* get searchpaths */
130 list = ly_ctx_get_searchdirs(ctx);
131 assert_non_null(list);
132 assert_string_equal(TESTS_BIN"/src", list[0]);
133 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
134 assert_string_equal(TESTS_SRC, list[5]);
135 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100136 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200137 assert_null(list[8]);
138
139 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200140 /* nonexisting */
141 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
142 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200143 /* first */
Radek Krejci0759b792018-09-20 13:53:15 +0200144 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200145 assert_string_not_equal(TESTS_BIN"/src", list[0]);
146 assert_int_equal(7, ctx->search_paths.count);
147 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200148 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200149 assert_int_equal(6, ctx->search_paths.count);
150 /* last */
Radek Krejcief6867f2018-11-27 11:32:00 +0100151 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200152 assert_int_equal(5, ctx->search_paths.count);
153 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200154 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200155 assert_int_equal(0, ctx->search_paths.count);
156
Radek Krejci555c9bb2018-09-20 12:45:13 +0200157 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200158 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200159
160 /* cleanup */
161 ly_ctx_destroy(ctx, NULL);
162
163 /* test searchdir list in ly_ctx_new() */
164 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
165 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
Radek Krejcief6867f2018-11-27 11:32:00 +0100166 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, 0, &ctx));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200167 assert_int_equal(2, ctx->search_paths.count);
168 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100169 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200170
171 /* cleanup */
Radek Krejci1e880032018-09-20 12:17:12 +0200172 ly_ctx_destroy(ctx, NULL);
173}
174
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200175static void
176test_options(void **state)
177{
178 (void) state; /* unused */
179
180 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200181
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200182 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
183
Radek Krejci962a8102018-09-20 14:21:06 +0200184 /* invalid arguments */
185 assert_int_equal(0, ly_ctx_get_options(NULL));
186 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
187
188 assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0));
189 logbuf_assert("Invalid argument ctx (ly_ctx_set_option()).");
190 assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0));
191 logbuf_assert("Invalid argument ctx (ly_ctx_unset_option()).");
192
193 /* option not allowed to be changed */
194 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
195 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
196 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
197 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
198
199
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200200 /* unset */
201 /* LY_CTX_ALLIMPLEMENTED */
202 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200203 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200204 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
205
206 /* LY_CTX_DISABLE_SEARCHDIRS */
207 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200208 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200209 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
210
211 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
212 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci962a8102018-09-20 14:21:06 +0200213 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200214 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
215
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200216 /* LY_CTX_PREFER_SEARCHDIRS */
217 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200218 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200219 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
220
221 /* LY_CTX_TRUSTED */
222 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200223 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200224 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
225
Radek Krejci962a8102018-09-20 14:21:06 +0200226 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
227
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200228 /* set back */
229 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200230 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200231 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
232
233 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200234 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200235 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
236
237 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci962a8102018-09-20 14:21:06 +0200238 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200239 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
240
241 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200242 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200243 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
244
245 /* LY_CTX_TRUSTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200246 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200247 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
248
Radek Krejci962a8102018-09-20 14:21:06 +0200249 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
250
251 /* cleanup */
252 ly_ctx_destroy(ctx, NULL);
253}
254
Radek Krejci2d31ea72018-10-25 15:46:42 +0200255static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
256 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
257 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
258{
259 *module_data = user_data;
260 *format = LYS_IN_YANG;
261 *free_module_data = NULL;
262 return LY_SUCCESS;
263}
264
Radek Krejci962a8102018-09-20 14:21:06 +0200265static void
266test_models(void **state)
267{
268 (void) state; /* unused */
269
Radek Krejcib7db73a2018-10-24 14:18:40 +0200270 struct ly_ctx *ctx;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200271 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100272 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200273
Radek Krejci962a8102018-09-20 14:21:06 +0200274 /* invalid arguments */
275 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
276 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
277
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100278 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200279 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
280
Radek Krejci9ed7a192018-10-31 16:23:51 +0100281 assert_null(lys_parse_mem(ctx, "module x {namespace urn:x;prefix x;}", 3));
282 logbuf_assert("Invalid schema input format.");
283
Radek Krejci2d31ea72018-10-25 15:46:42 +0200284 /* import callback */
285 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
286 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
287 assert_ptr_equal(str, ctx->imp_clb_data);
288 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
289 assert_string_equal("test", str);
290
291 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
292 assert_null(ctx->imp_clb);
293 assert_null(ctx->imp_clb_data);
294
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100295 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100296 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100297 assert_null(lys_parse_mem(ctx, "module y {namespace urn:y;prefix y;include y;}", LYS_IN_YANG));
298 assert_int_equal(LY_EVALID, ly_errcode(ctx));
299 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
300
Radek Krejcie9e987e2018-10-31 12:50:27 +0100301 assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", LYS_IN_YANG));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100302 assert_null(lys_parse_mem(ctx, "module y {namespace urn:y;prefix y;}", LYS_IN_YANG));
303 assert_int_equal(LY_EVALID, ly_errcode(ctx));
304 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
305
306 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100307 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100308 assert_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include y;}", LYS_IN_YANG));
309 assert_int_equal(LY_EVALID, ly_errcode(ctx));
310 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
311 store = -1;
312
Radek Krejcie9e987e2018-10-31 12:50:27 +0100313 /* selecting correct revision of the submodules */
314 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100315 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
Radek Krejci3b1f9292018-11-08 10:58:35 +0100316 mod2 = lys_parse_mem_(ctx, "module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", LYS_IN_YANG, 0, NULL, NULL, NULL);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100317 assert_non_null(mod2);
318 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
319
Radek Krejci9c536962018-10-31 14:52:13 +0100320 /* reloading module in case only the compiled module resists in the context */
Radek Krejci9c536962018-10-31 14:52:13 +0100321 mod1 = lys_parse_mem(ctx, "module w {namespace urn:w;prefix w;revision 2018-10-24;}", LYS_IN_YANG);
322 assert_non_null(mod1);
323 assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP));
324 assert_non_null(mod1->compiled);
325 assert_null(mod1->parsed);
326 mod2 = lys_parse_mem(ctx, "module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", LYS_IN_YANG);
327 assert_non_null(mod2);
328 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100329 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
330 assert_int_equal(LY_ENOTFOUND, lys_compile(mod2, 0));
331 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
332 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Radek Krejci9c536962018-10-31 14:52:13 +0100333 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
334 assert_non_null(mod1->parsed);
335 assert_string_equal("w", mod1->parsed->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100336
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200337 /* cleanup */
338 ly_ctx_destroy(ctx, NULL);
339}
340
Radek Krejcib7db73a2018-10-24 14:18:40 +0200341static void
342test_get_models(void **state)
343{
344 (void) state; /* unused */
345
346 struct ly_ctx *ctx;
347 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200348 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200349 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
350 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
351
Radek Krejcib7db73a2018-10-24 14:18:40 +0200352 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
353
354 /* invalid arguments */
355 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
356 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
357 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
358 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
359 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
360 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
361 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
362 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
363 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
364
365 /* internal modules */
366 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
367 mod = ly_ctx_get_module_implemented(ctx, "yang");
368 assert_non_null(mod);
369 assert_non_null(mod->parsed);
370 assert_string_equal("yang", mod->parsed->name);
371 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->parsed->ns);
372 assert_ptr_equal(mod, mod2);
373 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
374 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
375 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Radek Krejci5e163df2018-10-24 14:42:15 +0200376 assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2017-08-17"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200377
378 /* select module by revision */
379 mod = lys_parse_mem(ctx, str1, LYS_IN_YANG);
380 /* invalid attempts - implementing module of the same name and inserting the same module */
381 assert_null(lys_parse_mem(ctx, str2, LYS_IN_YANG));
382 logbuf_assert("Module \"a\" is already implemented in the context.");
Radek Krejci3b1f9292018-11-08 10:58:35 +0100383 assert_null(lys_parse_mem_(ctx, str1, LYS_IN_YANG, 0, NULL, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200384 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
385 /* insert the second module only as imported, not implemented */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100386 mod2 = lys_parse_mem_(ctx, str2, LYS_IN_YANG, 0, NULL, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200387 assert_non_null(mod);
388 assert_non_null(mod2);
389 assert_ptr_not_equal(mod, mod2);
390 mod = ly_ctx_get_module_latest(ctx, "a");
391 assert_ptr_equal(mod, mod2);
392 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->parsed->ns);
393 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200394 /* work with module with no revision */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100395 mod = lys_parse_mem_(ctx, str0, LYS_IN_YANG, 0, NULL, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200396 assert_non_null(mod);
397 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
398 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399
Radek Krejci313d9902018-11-08 09:42:58 +0100400 str1 = "submodule b {belongs-to a {prefix a;}}";
Radek Krejcid33273d2018-10-25 14:55:52 +0200401 assert_null(lys_parse_mem(ctx, str1, LYS_IN_YANG));
402 logbuf_assert("Input data contains submodule \"b\" which cannot be parsed directly without its main module.");
403
Radek Krejcib7db73a2018-10-24 14:18:40 +0200404 /* cleanup */
405 ly_ctx_destroy(ctx, NULL);
406}
407
Radek Krejci1e880032018-09-20 12:17:12 +0200408int main(void)
409{
410 const struct CMUnitTest tests[] = {
411 cmocka_unit_test_setup(test_searchdirs, logger_setup),
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200412 cmocka_unit_test_setup(test_options, logger_setup),
Radek Krejci962a8102018-09-20 14:21:06 +0200413 cmocka_unit_test_setup(test_models, logger_setup),
Radek Krejcib7db73a2018-10-24 14:18:40 +0200414 cmocka_unit_test_setup(test_get_models, logger_setup),
Radek Krejci1e880032018-09-20 12:17:12 +0200415 };
416
417 return cmocka_run_group_tests(tests, NULL, NULL);
418}