blob: 9a09dee98c303ae3508516de763254164b7195f2 [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));
288 assert_null(lys_parse_mem_module(ctx, in, 4, 1, NULL, NULL));
289 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));
306 assert_null(lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL));
307 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100308 assert_int_equal(LY_EVALID, ly_errcode(ctx));
309 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
310
Michal Vasko63f3d842020-07-08 10:10:14 +0200311 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in));
312 assert_non_null(lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL));
313 ly_in_free(in, 0);
314 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
315 assert_null(lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL));
316 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100317 assert_int_equal(LY_EVALID, ly_errcode(ctx));
318 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
319
320 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100321 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
323 assert_null(lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL));
324 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100325 assert_int_equal(LY_EVALID, ly_errcode(ctx));
326 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
327 store = -1;
328
Radek Krejcie9e987e2018-10-31 12:50:27 +0100329 /* selecting correct revision of the submodules */
330 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100331 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 +0200332 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
333 mod2 = lys_parse_mem_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL);
334 ly_in_free(in, 0);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100335 assert_non_null(mod2);
336 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
337
Radek Krejci9c536962018-10-31 14:52:13 +0100338 /* reloading module in case only the compiled module resists in the context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200339 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
340 mod1 = lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL);
341 ly_in_free(in, 0);
Radek Krejci9c536962018-10-31 14:52:13 +0100342 assert_non_null(mod1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200343 assert_int_equal(LY_SUCCESS, lys_compile(&mod1, LYSC_OPT_FREE_SP));
Radek Krejci9c536962018-10-31 14:52:13 +0100344 assert_non_null(mod1->compiled);
345 assert_null(mod1->parsed);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200346
Michal Vasko63f3d842020-07-08 10:10:14 +0200347 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));
348 mod2 = lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL);
349 ly_in_free(in, 0);
Radek Krejci9c536962018-10-31 14:52:13 +0100350 assert_non_null(mod2);
351 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100352 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200353 assert_int_equal(LY_ENOTFOUND, lys_compile(&mod2, 0));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100354 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200355 assert_null(mod2);
356
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 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));
358 mod2 = lys_parse_mem_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL);
359 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200360 assert_non_null(mod2);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100361 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 +0200362 assert_int_equal(LY_SUCCESS, lys_compile(&mod2, 0));
363 assert_non_null(mod2);
Radek Krejci9c536962018-10-31 14:52:13 +0100364 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100365 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100366
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200367 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100368 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200369 ly_ctx_destroy(ctx, NULL);
370}
371
Radek Krejcib7db73a2018-10-24 14:18:40 +0200372static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200373test_imports(void **state)
374{
375 *state = test_imports;
376
377 struct ly_ctx *ctx;
378 struct lys_module *mod1, *mod2, *import;
379
380 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
381 * from the callback should be loaded into the context and used as an import */
382 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
383 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
384 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", LYS_IN_YANG));
385 assert_int_equal(1, mod1->latest_revision);
386 assert_int_equal(1, mod1->implemented);
387 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
388 import = mod2->compiled->imports[0].module;
389 assert_int_equal(2, import->latest_revision);
390 assert_int_equal(0, mod1->latest_revision);
391 assert_ptr_not_equal(mod1, import);
392 assert_string_equal("2019-09-17", import->revision);
393 assert_int_equal(0, import->implemented);
394 assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16"));
395 ly_ctx_destroy(ctx, NULL);
396
397 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
398 * already present in the context should be selected and the callback's revision should not be loaded into the context */
399 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
400 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
401 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", LYS_IN_YANG));
402 assert_int_equal(1, mod1->latest_revision);
403 assert_int_equal(1, mod1->implemented);
404 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
405 import = mod2->compiled->imports[0].module;
406 assert_ptr_equal(mod1, import);
407 assert_int_equal(2, import->latest_revision);
408 assert_int_equal(1, import->implemented);
409 assert_string_equal("2019-09-18", import->revision);
410 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
411 ly_ctx_destroy(ctx, NULL);
412
413 /* cleanup */
414 *state = NULL;
415}
416
417static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200418test_get_models(void **state)
419{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100420 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200421
422 struct ly_ctx *ctx;
423 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200424 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200425 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
426 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 +0200427 struct ly_in *in0, *in1, *in2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200428
Radek Krejci2390fb52019-04-30 13:27:43 +0200429 unsigned int index = 0;
430 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
431
Michal Vasko63f3d842020-07-08 10:10:14 +0200432 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
433 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
434 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200435 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
436
437 /* invalid arguments */
438 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
439 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
440 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
441 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
442 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
443 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
444 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
445 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
446 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
447
448 /* internal modules */
449 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
450 mod = ly_ctx_get_module_implemented(ctx, "yang");
451 assert_non_null(mod);
452 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100453 assert_string_equal("yang", mod->name);
454 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200455 assert_ptr_equal(mod, mod2);
456 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
457 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
458 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200459 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 +0200460
461 /* select module by revision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200462 mod = lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200463 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vasko63f3d842020-07-08 10:10:14 +0200464 assert_null(lys_parse_mem_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200465 logbuf_assert("Module \"a\" is already implemented in the context.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 ly_in_reset(in1);
467 assert_null(lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200468 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
469 /* insert the second module only as imported, not implemented */
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 ly_in_reset(in2);
471 mod2 = lys_parse_mem_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200472 assert_non_null(mod);
473 assert_non_null(mod2);
474 assert_ptr_not_equal(mod, mod2);
475 mod = ly_ctx_get_module_latest(ctx, "a");
476 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100477 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200478 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200479 /* work with module with no revision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 mod = lys_parse_mem_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200481 assert_non_null(mod);
482 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
483 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200484
Radek Krejci313d9902018-11-08 09:42:58 +0100485 str1 = "submodule b {belongs-to a {prefix a;}}";
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 ly_in_free(in1, 0);
487 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
488 assert_null(lys_parse_mem_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100489 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200490
Radek Krejci2390fb52019-04-30 13:27:43 +0200491 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
492 assert_string_equal(names[index - 1], mod->name);
493 }
494 assert_int_equal(9, index);
495
Radek Krejcib7db73a2018-10-24 14:18:40 +0200496 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100497 *state = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200498 ly_in_free(in0, 0);
499 ly_in_free(in1, 0);
500 ly_in_free(in2, 0);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200501 ly_ctx_destroy(ctx, NULL);
502}
503
Radek Krejci1e880032018-09-20 12:17:12 +0200504int main(void)
505{
506 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100507 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
508 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
509 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
Radek Krejcib3289d62019-09-18 12:21:39 +0200510 cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100511 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200512 };
513
514 return cmocka_run_group_tests(tests, NULL, NULL);
515}