blob: 2616510f5894fdc619f05da98de94994cc405786 [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
Radek Krejci1e880032018-09-20 12:17:12 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <string.h>
21#include <stdio.h>
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "common.h"
24#include "context.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020025#include "parser.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "tests/config.h"
27#include "tree_schema_internal.h"
Radek Krejci1e880032018-09-20 12:17:12 +020028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010031int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020032
33/* set to 0 to printing error messages to stderr instead of checking them in code */
34#define ENABLE_LOGGER_CHECKING 1
35
36static void
37logger(LY_LOG_LEVEL level, const char *msg, const char *path)
38{
39 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010040 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010041 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010042 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46 if (store > 0) {
47 --store;
48 }
49 }
Radek Krejci1e880032018-09-20 12:17:12 +020050}
51
52static int
53logger_setup(void **state)
54{
55 (void) state; /* unused */
56#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010057 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020058#endif
59 return 0;
60}
61
Radek Krejci0bcdaed2019-01-10 10:21:34 +010062static int
63logger_teardown(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
67 if (*state) {
68 fprintf(stderr, "%s\n", logbuf);
69 }
70#endif
71 return 0;
72}
73
Radek Krejci1e880032018-09-20 12:17:12 +020074#if ENABLE_LOGGER_CHECKING
75# define logbuf_assert(str) assert_string_equal(logbuf, str)
76#else
77# define logbuf_assert(str)
78#endif
79
Radek Krejci1e880032018-09-20 12:17:12 +020080static void
81test_searchdirs(void **state)
82{
Radek Krejci0bcdaed2019-01-10 10:21:34 +010083 *state = test_searchdirs;
Radek Krejci1e880032018-09-20 12:17:12 +020084
85 struct ly_ctx *ctx;
86 const char * const *list;
87
88 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
89
90 /* invalid arguments */
91 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
92 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
93 assert_null(ly_ctx_get_searchdirs(NULL));
94 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +020095 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +020096 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +020097
98 /* readable and executable, but not a directory */
Radek Krejci1fd5ad62020-05-15 15:48:42 +020099 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utest_context"));
100 logbuf_assert("Given search directory \""TESTS_BIN"/utest_context\" is not a directory.");
Radek Krejci1e880032018-09-20 12:17:12 +0200101 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +0200102 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
Radek Krejci3a077b92019-04-09 17:10:35 +0200103 logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied).");
Radek Krejci1e880032018-09-20 12:17:12 +0200104 /* not existing */
105 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
Radek Krejci3a077b92019-04-09 17:10:35 +0200106 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejci1e880032018-09-20 12:17:12 +0200107
108 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200109 /* no change */
110 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
111
Radek Krejci1e880032018-09-20 12:17:12 +0200112 /* correct path */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200113 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200114 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200115 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200116
Radek Krejci14946ab2018-09-20 13:42:06 +0200117 /* duplicated paths */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200118 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200119 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200120 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200121
122 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
127 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
128 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100129 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200130 assert_int_equal(8, ctx->search_paths.count);
131
132 /* get searchpaths */
133 list = ly_ctx_get_searchdirs(ctx);
134 assert_non_null(list);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200135 assert_string_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200136 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
137 assert_string_equal(TESTS_SRC, list[5]);
138 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100139 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200140 assert_null(list[8]);
141
142 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200143 /* nonexisting */
144 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
145 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200146 /* first */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200147 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/utests"));
148 assert_string_not_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200149 assert_int_equal(7, ctx->search_paths.count);
150 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200151 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200152 assert_int_equal(6, ctx->search_paths.count);
153 /* last */
Radek Krejcief6867f2018-11-27 11:32:00 +0100154 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200155 assert_int_equal(5, ctx->search_paths.count);
156 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200157 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200158 assert_int_equal(0, ctx->search_paths.count);
159
Radek Krejci555c9bb2018-09-20 12:45:13 +0200160 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200161 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200162
163 /* cleanup */
164 ly_ctx_destroy(ctx, NULL);
165
166 /* test searchdir list in ly_ctx_new() */
167 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
Radek Krejci3a077b92019-04-09 17:10:35 +0200168 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejcib3289d62019-09-18 12:21:39 +0200169 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200170 assert_int_equal(2, ctx->search_paths.count);
171 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100172 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200173
174 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100175 *state = NULL;
Radek Krejci1e880032018-09-20 12:17:12 +0200176 ly_ctx_destroy(ctx, NULL);
177}
178
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200179static void
180test_options(void **state)
181{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100182 *state = test_options;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200183
184 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200185
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
187
Radek Krejci962a8102018-09-20 14:21:06 +0200188 /* invalid arguments */
189 assert_int_equal(0, ly_ctx_get_options(NULL));
190 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
191
Radek Krejci3fa46b62019-09-11 10:47:30 +0200192 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
193 logbuf_assert("Invalid argument ctx (ly_ctx_set_options()).");
194 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
195 logbuf_assert("Invalid argument ctx (ly_ctx_unset_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200196
197 /* option not allowed to be changed */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200198 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY));
199 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
200 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY));
201 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200202
203
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200204 /* unset */
205 /* LY_CTX_ALLIMPLEMENTED */
206 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200207 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200208 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
209
210 /* LY_CTX_DISABLE_SEARCHDIRS */
211 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200212 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200213 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
214
215 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
216 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200217 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200218 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
219
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200220 /* LY_CTX_PREFER_SEARCHDIRS */
221 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200222 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200223 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
224
225 /* LY_CTX_TRUSTED */
226 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200227 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200228 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
229
Radek Krejci962a8102018-09-20 14:21:06 +0200230 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
231
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200232 /* set back */
233 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200234 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200235 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
236
237 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200238 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200239 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
240
241 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200242 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200243 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
244
245 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200246 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200247 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
248
249 /* LY_CTX_TRUSTED */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200250 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200251 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
252
Radek Krejci962a8102018-09-20 14:21:06 +0200253 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
254
255 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100256 *state = NULL;
Radek Krejci962a8102018-09-20 14:21:06 +0200257 ly_ctx_destroy(ctx, NULL);
258}
259
Radek Krejci2d31ea72018-10-25 15:46:42 +0200260static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
261 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
262 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
263{
264 *module_data = user_data;
265 *format = LYS_IN_YANG;
266 *free_module_data = NULL;
267 return LY_SUCCESS;
268}
269
Radek Krejci962a8102018-09-20 14:21:06 +0200270static void
271test_models(void **state)
272{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100273 *state = test_models;
Radek Krejci962a8102018-09-20 14:21:06 +0200274
Radek Krejcib7db73a2018-10-24 14:18:40 +0200275 struct ly_ctx *ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 struct ly_in *in;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200277 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100278 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200279
Radek Krejci962a8102018-09-20 14:21:06 +0200280 /* invalid arguments */
281 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
282 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
283
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100284 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200285 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
286
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200288 assert_int_equal(LY_EINVAL, lys_parse_mem_module(ctx, in, 4, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200289 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100290 logbuf_assert("Invalid schema input format.");
291
Radek Krejci2d31ea72018-10-25 15:46:42 +0200292 /* import callback */
293 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
294 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
295 assert_ptr_equal(str, ctx->imp_clb_data);
296 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
297 assert_string_equal("test", str);
298
299 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
300 assert_null(ctx->imp_clb);
301 assert_null(ctx->imp_clb_data);
302
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100303 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100304 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200305 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200306 assert_int_equal(LY_EVALID, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200307 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100308 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
309
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200311 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 ly_in_free(in, 0);
313 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200314 assert_int_equal(LY_EVALID, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100316 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
317
318 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100319 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200321 assert_int_equal(LY_EVALID, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100323 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
324 store = -1;
325
Radek Krejcie9e987e2018-10-31 12:50:27 +0100326 /* selecting correct revision of the submodules */
327 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100328 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200329 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200330 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod2));
Michal Vasko63f3d842020-07-08 10:10:14 +0200331 ly_in_free(in, 0);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100332 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
333
Radek Krejci9c536962018-10-31 14:52:13 +0100334 /* reloading module in case only the compiled module resists in the context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200336 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200337 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200338 assert_int_equal(LY_SUCCESS, lys_compile(&mod1, LYSC_OPT_FREE_SP));
Radek Krejci9c536962018-10-31 14:52:13 +0100339 assert_non_null(mod1->compiled);
340 assert_null(mod1->parsed);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200341
Michal Vasko63f3d842020-07-08 10:10:14 +0200342 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));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200343 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
Michal Vasko63f3d842020-07-08 10:10:14 +0200344 ly_in_free(in, 0);
Radek Krejci9c536962018-10-31 14:52:13 +0100345 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100346 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200347 assert_int_equal(LY_ENOTFOUND, lys_compile(&mod2, 0));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100348 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200349 assert_null(mod2);
350
Michal Vasko63f3d842020-07-08 10:10:14 +0200351 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));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200352 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100354 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200355 assert_int_equal(LY_SUCCESS, lys_compile(&mod2, 0));
356 assert_non_null(mod2);
Radek Krejci9c536962018-10-31 14:52:13 +0100357 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100358 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100359
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200360 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100361 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200362 ly_ctx_destroy(ctx, NULL);
363}
364
Radek Krejcib7db73a2018-10-24 14:18:40 +0200365static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200366test_imports(void **state)
367{
368 *state = test_imports;
369
370 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200371 const struct lys_module *mod1, *mod2, *import;
Radek Krejcib3289d62019-09-18 12:21:39 +0200372
373 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
374 * from the callback should be loaded into the context and used as an import */
375 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
376 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200377 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
378 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200379 assert_int_equal(1, mod1->latest_revision);
380 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200381 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
382 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200383 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200384 assert_int_equal(2, import->latest_revision);
385 assert_int_equal(0, mod1->latest_revision);
386 assert_ptr_not_equal(mod1, import);
387 assert_string_equal("2019-09-17", import->revision);
388 assert_int_equal(0, import->implemented);
389 assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16"));
390 ly_ctx_destroy(ctx, NULL);
391
392 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
393 * already present in the context should be selected and the callback's revision should not be loaded into the context */
394 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
395 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200396 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
397 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200398 assert_int_equal(1, mod1->latest_revision);
399 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200400 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
401 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200402 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200403 assert_ptr_equal(mod1, import);
404 assert_int_equal(2, import->latest_revision);
405 assert_int_equal(1, import->implemented);
406 assert_string_equal("2019-09-18", import->revision);
407 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
408 ly_ctx_destroy(ctx, NULL);
409
410 /* cleanup */
411 *state = NULL;
412}
413
414static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200415test_get_models(void **state)
416{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100417 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200418
419 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200420 struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200421 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200422 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
423 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
Michal Vasko63f3d842020-07-08 10:10:14 +0200424 struct ly_in *in0, *in1, *in2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200425
Radek Krejci2390fb52019-04-30 13:27:43 +0200426 unsigned int index = 0;
427 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
428
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
430 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
431 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200432 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
433
434 /* invalid arguments */
435 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
436 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
437 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
438 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
439 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
440 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
441 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
442 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
443 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
444
445 /* internal modules */
446 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
447 mod = ly_ctx_get_module_implemented(ctx, "yang");
448 assert_non_null(mod);
449 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100450 assert_string_equal("yang", mod->name);
451 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452 assert_ptr_equal(mod, mod2);
453 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
454 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
455 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200456 assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200457
458 /* select module by revision */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200459 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200460 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200461 assert_int_equal(LY_EDENIED, lys_parse_mem_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200462 logbuf_assert("Module \"a\" is already implemented in the context.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 ly_in_reset(in1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200464 assert_int_equal(LY_EEXIST, lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200465 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
466 /* insert the second module only as imported, not implemented */
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 ly_in_reset(in2);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200468 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL, &mod2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200469 assert_non_null(mod2);
470 assert_ptr_not_equal(mod, mod2);
471 mod = ly_ctx_get_module_latest(ctx, "a");
472 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100473 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200475 /* work with module with no revision */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200476 assert_int_equal(LY_SUCCESS, lys_parse_mem_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL, &mod));
Radek Krejci5e163df2018-10-24 14:42:15 +0200477 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
478 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200479
Radek Krejci313d9902018-11-08 09:42:58 +0100480 str1 = "submodule b {belongs-to a {prefix a;}}";
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 ly_in_free(in1, 0);
482 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200483 assert_int_equal(LY_EINVAL, lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100484 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200485
Michal Vasko3a41dff2020-07-15 14:30:28 +0200486 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(ctx, &index))) {
Radek Krejci2390fb52019-04-30 13:27:43 +0200487 assert_string_equal(names[index - 1], mod->name);
488 }
489 assert_int_equal(9, index);
490
Radek Krejcib7db73a2018-10-24 14:18:40 +0200491 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100492 *state = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 ly_in_free(in0, 0);
494 ly_in_free(in1, 0);
495 ly_in_free(in2, 0);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200496 ly_ctx_destroy(ctx, NULL);
497}
498
Radek Krejci1e880032018-09-20 12:17:12 +0200499int main(void)
500{
501 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100502 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
503 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
504 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
Radek Krejcib3289d62019-09-18 12:21:39 +0200505 cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100506 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200507 };
508
509 return cmocka_run_group_tests(tests, NULL, NULL);
510}