blob: 5f1497259cca9ec7988f2c3eb8b04c467e3562ff [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"
25#include "tests/config.h"
26#include "tree_schema_internal.h"
Radek Krejci1e880032018-09-20 12:17:12 +020027
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010030int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020031
32/* set to 0 to printing error messages to stderr instead of checking them in code */
33#define ENABLE_LOGGER_CHECKING 1
34
35static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010039 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010040 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010041 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45 if (store > 0) {
46 --store;
47 }
48 }
Radek Krejci1e880032018-09-20 12:17:12 +020049}
50
51static int
52logger_setup(void **state)
53{
54 (void) state; /* unused */
55#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010056 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020057#endif
58 return 0;
59}
60
Radek Krejci0bcdaed2019-01-10 10:21:34 +010061static int
62logger_teardown(void **state)
63{
64 (void) state; /* unused */
65#if ENABLE_LOGGER_CHECKING
66 if (*state) {
67 fprintf(stderr, "%s\n", logbuf);
68 }
69#endif
70 return 0;
71}
72
Radek Krejci1e880032018-09-20 12:17:12 +020073#if ENABLE_LOGGER_CHECKING
74# define logbuf_assert(str) assert_string_equal(logbuf, str)
75#else
76# define logbuf_assert(str)
77#endif
78
Radek Krejci1e880032018-09-20 12:17:12 +020079static void
80test_searchdirs(void **state)
81{
Radek Krejci0bcdaed2019-01-10 10:21:34 +010082 *state = test_searchdirs;
Radek Krejci1e880032018-09-20 12:17:12 +020083
84 struct ly_ctx *ctx;
85 const char * const *list;
86
87 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
88
89 /* invalid arguments */
90 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
91 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
92 assert_null(ly_ctx_get_searchdirs(NULL));
93 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +020094 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +020095 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +020096
97 /* readable and executable, but not a directory */
Radek Krejci1fd5ad62020-05-15 15:48:42 +020098 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utest_context"));
99 logbuf_assert("Given search directory \""TESTS_BIN"/utest_context\" is not a directory.");
Radek Krejci1e880032018-09-20 12:17:12 +0200100 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +0200101 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
Radek Krejci3a077b92019-04-09 17:10:35 +0200102 logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied).");
Radek Krejci1e880032018-09-20 12:17:12 +0200103 /* not existing */
104 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
Radek Krejci3a077b92019-04-09 17:10:35 +0200105 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejci1e880032018-09-20 12:17:12 +0200106
107 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200108 /* no change */
109 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
110
Radek Krejci1e880032018-09-20 12:17:12 +0200111 /* correct path */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200112 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200113 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200114 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200115
Radek Krejci14946ab2018-09-20 13:42:06 +0200116 /* duplicated paths */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200117 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200118 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200119 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200120
121 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
122 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
127 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100128 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200129 assert_int_equal(8, ctx->search_paths.count);
130
131 /* get searchpaths */
132 list = ly_ctx_get_searchdirs(ctx);
133 assert_non_null(list);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200134 assert_string_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200135 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
136 assert_string_equal(TESTS_SRC, list[5]);
137 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100138 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200139 assert_null(list[8]);
140
141 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200142 /* nonexisting */
143 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
144 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200145 /* first */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200146 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/utests"));
147 assert_string_not_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200148 assert_int_equal(7, ctx->search_paths.count);
149 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200150 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200151 assert_int_equal(6, ctx->search_paths.count);
152 /* last */
Radek Krejcief6867f2018-11-27 11:32:00 +0100153 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200154 assert_int_equal(5, ctx->search_paths.count);
155 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200156 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200157 assert_int_equal(0, ctx->search_paths.count);
158
Radek Krejci555c9bb2018-09-20 12:45:13 +0200159 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200160 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200161
162 /* cleanup */
163 ly_ctx_destroy(ctx, NULL);
164
165 /* test searchdir list in ly_ctx_new() */
166 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
Radek Krejci3a077b92019-04-09 17:10:35 +0200167 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejcib3289d62019-09-18 12:21:39 +0200168 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 +0200169 assert_int_equal(2, ctx->search_paths.count);
170 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100171 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200172
173 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100174 *state = NULL;
Radek Krejci1e880032018-09-20 12:17:12 +0200175 ly_ctx_destroy(ctx, NULL);
176}
177
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200178static void
179test_options(void **state)
180{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100181 *state = test_options;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200182
183 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200184
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200185 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
186
Radek Krejci962a8102018-09-20 14:21:06 +0200187 /* invalid arguments */
188 assert_int_equal(0, ly_ctx_get_options(NULL));
189 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
190
Radek Krejci3fa46b62019-09-11 10:47:30 +0200191 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
192 logbuf_assert("Invalid argument ctx (ly_ctx_set_options()).");
193 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
194 logbuf_assert("Invalid argument ctx (ly_ctx_unset_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200195
196 /* option not allowed to be changed */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200197 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY));
198 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
199 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NOYANGLIBRARY));
200 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200201
202
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200203 /* unset */
204 /* LY_CTX_ALLIMPLEMENTED */
205 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200206 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200207 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
208
209 /* LY_CTX_DISABLE_SEARCHDIRS */
210 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200211 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200212 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
213
214 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
215 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200216 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200217 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
218
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200219 /* LY_CTX_PREFER_SEARCHDIRS */
220 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200221 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200222 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
223
224 /* LY_CTX_TRUSTED */
225 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200226 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200227 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
228
Radek Krejci962a8102018-09-20 14:21:06 +0200229 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
230
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200231 /* set back */
232 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200233 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200234 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
235
236 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200237 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200238 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
239
240 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200241 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200242 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
243
244 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200245 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200246 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
247
248 /* LY_CTX_TRUSTED */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200249 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200250 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
251
Radek Krejci962a8102018-09-20 14:21:06 +0200252 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
253
254 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100255 *state = NULL;
Radek Krejci962a8102018-09-20 14:21:06 +0200256 ly_ctx_destroy(ctx, NULL);
257}
258
Radek Krejci2d31ea72018-10-25 15:46:42 +0200259static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
260 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
261 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
262{
263 *module_data = user_data;
264 *format = LYS_IN_YANG;
265 *free_module_data = NULL;
266 return LY_SUCCESS;
267}
268
Radek Krejci962a8102018-09-20 14:21:06 +0200269static void
270test_models(void **state)
271{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100272 *state = test_models;
Radek Krejci962a8102018-09-20 14:21:06 +0200273
Radek Krejcib7db73a2018-10-24 14:18:40 +0200274 struct ly_ctx *ctx;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200275 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100276 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200277
Radek Krejci962a8102018-09-20 14:21:06 +0200278 /* invalid arguments */
279 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
280 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
281
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100282 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200283 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
284
David Sedlák1b623122019-08-05 15:27:49 +0200285 assert_null(lys_parse_mem_module(ctx, "module x {namespace urn:x;prefix x;}", 4, 1, NULL, NULL));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100286 logbuf_assert("Invalid schema input format.");
287
Radek Krejci2d31ea72018-10-25 15:46:42 +0200288 /* import callback */
289 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
290 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
291 assert_ptr_equal(str, ctx->imp_clb_data);
292 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
293 assert_string_equal("test", str);
294
295 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
296 assert_null(ctx->imp_clb);
297 assert_null(ctx->imp_clb_data);
298
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100299 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100300 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}");
Radek Krejci096235c2019-01-11 11:12:19 +0100301 assert_null(lys_parse_mem_module(ctx, "module y {namespace urn:y;prefix y;include y;}", LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100302 assert_int_equal(LY_EVALID, ly_errcode(ctx));
303 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
304
Radek Krejci096235c2019-01-11 11:12:19 +0100305 assert_non_null(lys_parse_mem_module(ctx, "module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", LYS_IN_YANG, 1, NULL, NULL));
306 assert_null(lys_parse_mem_module(ctx, "module y {namespace urn:y;prefix y;}", LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100307 assert_int_equal(LY_EVALID, ly_errcode(ctx));
308 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
309
310 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100311 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Radek Krejci096235c2019-01-11 11:12:19 +0100312 assert_null(lys_parse_mem_module(ctx, "module b {namespace urn:b;prefix b;include y;}", LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100313 assert_int_equal(LY_EVALID, ly_errcode(ctx));
314 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
315 store = -1;
316
Radek Krejcie9e987e2018-10-31 12:50:27 +0100317 /* selecting correct revision of the submodules */
318 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100319 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100320 mod2 = lys_parse_mem_module(ctx, "module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100321 assert_non_null(mod2);
322 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
323
Radek Krejci9c536962018-10-31 14:52:13 +0100324 /* reloading module in case only the compiled module resists in the context */
Radek Krejci096235c2019-01-11 11:12:19 +0100325 mod1 = lys_parse_mem_module(ctx, "module w {namespace urn:w;prefix w;revision 2018-10-24;}", LYS_IN_YANG, 1, NULL, NULL);
Radek Krejci9c536962018-10-31 14:52:13 +0100326 assert_non_null(mod1);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200327 assert_int_equal(LY_SUCCESS, lys_compile(&mod1, LYSC_OPT_FREE_SP));
Radek Krejci9c536962018-10-31 14:52:13 +0100328 assert_non_null(mod1->compiled);
329 assert_null(mod1->parsed);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200330
Radek Krejci096235c2019-01-11 11:12:19 +0100331 mod2 = lys_parse_mem_module(ctx, "module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", LYS_IN_YANG, 1, NULL, NULL);
Radek Krejci9c536962018-10-31 14:52:13 +0100332 assert_non_null(mod2);
333 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100334 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200335 assert_int_equal(LY_ENOTFOUND, lys_compile(&mod2, 0));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100336 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200337 assert_null(mod2);
338
339 mod2 = lys_parse_mem_module(ctx, "module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", LYS_IN_YANG, 1, NULL, NULL);
340 assert_non_null(mod2);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100341 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 +0200342 assert_int_equal(LY_SUCCESS, lys_compile(&mod2, 0));
343 assert_non_null(mod2);
Radek Krejci9c536962018-10-31 14:52:13 +0100344 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100345 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100346
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200347 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100348 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200349 ly_ctx_destroy(ctx, NULL);
350}
351
Radek Krejcib7db73a2018-10-24 14:18:40 +0200352static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200353test_imports(void **state)
354{
355 *state = test_imports;
356
357 struct ly_ctx *ctx;
358 struct lys_module *mod1, *mod2, *import;
359
360 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
361 * from the callback should be loaded into the context and used as an import */
362 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
363 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
364 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", LYS_IN_YANG));
365 assert_int_equal(1, mod1->latest_revision);
366 assert_int_equal(1, mod1->implemented);
367 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
368 import = mod2->compiled->imports[0].module;
369 assert_int_equal(2, import->latest_revision);
370 assert_int_equal(0, mod1->latest_revision);
371 assert_ptr_not_equal(mod1, import);
372 assert_string_equal("2019-09-17", import->revision);
373 assert_int_equal(0, import->implemented);
374 assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16"));
375 ly_ctx_destroy(ctx, NULL);
376
377 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
378 * already present in the context should be selected and the callback's revision should not be loaded into the context */
379 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
380 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
381 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", LYS_IN_YANG));
382 assert_int_equal(1, mod1->latest_revision);
383 assert_int_equal(1, mod1->implemented);
384 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
385 import = mod2->compiled->imports[0].module;
386 assert_ptr_equal(mod1, import);
387 assert_int_equal(2, import->latest_revision);
388 assert_int_equal(1, import->implemented);
389 assert_string_equal("2019-09-18", import->revision);
390 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
391 ly_ctx_destroy(ctx, NULL);
392
393 /* cleanup */
394 *state = NULL;
395}
396
397static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200398test_get_models(void **state)
399{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100400 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200401
402 struct ly_ctx *ctx;
403 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200404 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200405 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
406 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
407
Radek Krejci2390fb52019-04-30 13:27:43 +0200408 unsigned int index = 0;
409 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
410
Radek Krejcib7db73a2018-10-24 14:18:40 +0200411 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
412
413 /* invalid arguments */
414 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
415 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
416 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
417 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
418 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
419 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
420 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
421 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
422 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
423
424 /* internal modules */
425 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
426 mod = ly_ctx_get_module_implemented(ctx, "yang");
427 assert_non_null(mod);
428 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100429 assert_string_equal("yang", mod->name);
430 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431 assert_ptr_equal(mod, mod2);
432 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
433 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
434 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200435 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 +0200436
437 /* select module by revision */
Radek Krejci096235c2019-01-11 11:12:19 +0100438 mod = lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200439 /* invalid attempts - implementing module of the same name and inserting the same module */
Radek Krejci096235c2019-01-11 11:12:19 +0100440 assert_null(lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200441 logbuf_assert("Module \"a\" is already implemented in the context.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100442 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 0, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200443 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
444 /* insert the second module only as imported, not implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100445 mod2 = lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200446 assert_non_null(mod);
447 assert_non_null(mod2);
448 assert_ptr_not_equal(mod, mod2);
449 mod = ly_ctx_get_module_latest(ctx, "a");
450 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100451 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200452 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200453 /* work with module with no revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100454 mod = lys_parse_mem_module(ctx, str0, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200455 assert_non_null(mod);
456 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
457 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200458
Radek Krejci313d9902018-11-08 09:42:58 +0100459 str1 = "submodule b {belongs-to a {prefix a;}}";
Radek Krejci096235c2019-01-11 11:12:19 +0100460 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100461 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200462
Radek Krejci2390fb52019-04-30 13:27:43 +0200463 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
464 assert_string_equal(names[index - 1], mod->name);
465 }
466 assert_int_equal(9, index);
467
Radek Krejcib7db73a2018-10-24 14:18:40 +0200468 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100469 *state = NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200470 ly_ctx_destroy(ctx, NULL);
471}
472
Radek Krejci1e880032018-09-20 12:17:12 +0200473int main(void)
474{
475 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100476 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
477 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
478 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
Radek Krejcib3289d62019-09-18 12:21:39 +0200479 cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100480 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200481 };
482
483 return cmocka_run_group_tests(tests, NULL, NULL);
484}