blob: fe06f968ac81152f0b6a4b54bc15521304517b24 [file] [log] [blame]
Radek Krejci1e880032018-09-20 12:17:12 +02001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from context.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "tests/config.h"
Radek Krejci1e880032018-09-20 12:17:12 +020016
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <string.h>
23#include <stdio.h>
24
Radek Krejci2d7a47b2019-05-16 13:34:10 +020025#include "../../src/common.h"
26#include "../../src/context.h"
27#include "../../src/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;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200276 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100277 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200278
Radek Krejci962a8102018-09-20 14:21:06 +0200279 /* invalid arguments */
280 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
281 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
282
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100283 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200284 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
285
David Sedlák1b623122019-08-05 15:27:49 +0200286 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 +0100287 logbuf_assert("Invalid schema input format.");
288
Radek Krejci2d31ea72018-10-25 15:46:42 +0200289 /* import callback */
290 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
291 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
292 assert_ptr_equal(str, ctx->imp_clb_data);
293 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
294 assert_string_equal("test", str);
295
296 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
297 assert_null(ctx->imp_clb);
298 assert_null(ctx->imp_clb_data);
299
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100300 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100301 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 +0100302 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 +0100303 assert_int_equal(LY_EVALID, ly_errcode(ctx));
304 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
305
Radek Krejci096235c2019-01-11 11:12:19 +0100306 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));
307 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 +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
311 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100312 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Radek Krejci096235c2019-01-11 11:12:19 +0100313 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 +0100314 assert_int_equal(LY_EVALID, ly_errcode(ctx));
315 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
316 store = -1;
317
Radek Krejcie9e987e2018-10-31 12:50:27 +0100318 /* selecting correct revision of the submodules */
319 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100320 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 +0100321 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 +0100322 assert_non_null(mod2);
323 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
324
Radek Krejci9c536962018-10-31 14:52:13 +0100325 /* reloading module in case only the compiled module resists in the context */
Radek Krejci096235c2019-01-11 11:12:19 +0100326 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 +0100327 assert_non_null(mod1);
328 assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP));
329 assert_non_null(mod1->compiled);
330 assert_null(mod1->parsed);
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);
335 assert_int_equal(LY_ENOTFOUND, lys_compile(mod2, 0));
336 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
337 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Radek Krejci9c536962018-10-31 14:52:13 +0100338 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
339 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100340 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100341
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200342 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100343 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200344 ly_ctx_destroy(ctx, NULL);
345}
346
Radek Krejcib7db73a2018-10-24 14:18:40 +0200347static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200348test_imports(void **state)
349{
350 *state = test_imports;
351
352 struct ly_ctx *ctx;
353 struct lys_module *mod1, *mod2, *import;
354
355 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
356 * from the callback should be loaded into the context and used as an import */
357 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
358 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
359 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", LYS_IN_YANG));
360 assert_int_equal(1, mod1->latest_revision);
361 assert_int_equal(1, mod1->implemented);
362 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
363 import = mod2->compiled->imports[0].module;
364 assert_int_equal(2, import->latest_revision);
365 assert_int_equal(0, mod1->latest_revision);
366 assert_ptr_not_equal(mod1, import);
367 assert_string_equal("2019-09-17", import->revision);
368 assert_int_equal(0, import->implemented);
369 assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16"));
370 ly_ctx_destroy(ctx, NULL);
371
372 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
373 * already present in the context should be selected and the callback's revision should not be loaded into the context */
374 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
375 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}");
376 assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", LYS_IN_YANG));
377 assert_int_equal(1, mod1->latest_revision);
378 assert_int_equal(1, mod1->implemented);
379 assert_non_null(mod2 = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", LYS_IN_YANG));
380 import = mod2->compiled->imports[0].module;
381 assert_ptr_equal(mod1, import);
382 assert_int_equal(2, import->latest_revision);
383 assert_int_equal(1, import->implemented);
384 assert_string_equal("2019-09-18", import->revision);
385 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
386 ly_ctx_destroy(ctx, NULL);
387
388 /* cleanup */
389 *state = NULL;
390}
391
392static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200393test_get_models(void **state)
394{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100395 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200396
397 struct ly_ctx *ctx;
398 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200399 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200400 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
401 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
402
Radek Krejci2390fb52019-04-30 13:27:43 +0200403 unsigned int index = 0;
404 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
405
Radek Krejcib7db73a2018-10-24 14:18:40 +0200406 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
407
408 /* invalid arguments */
409 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
410 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
411 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
412 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
413 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
414 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
415 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
416 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
417 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
418
419 /* internal modules */
420 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
421 mod = ly_ctx_get_module_implemented(ctx, "yang");
422 assert_non_null(mod);
423 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100424 assert_string_equal("yang", mod->name);
425 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200426 assert_ptr_equal(mod, mod2);
427 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
428 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
429 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200430 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 +0200431
432 /* select module by revision */
Radek Krejci096235c2019-01-11 11:12:19 +0100433 mod = lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200434 /* invalid attempts - implementing module of the same name and inserting the same module */
Radek Krejci096235c2019-01-11 11:12:19 +0100435 assert_null(lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200436 logbuf_assert("Module \"a\" is already implemented in the context.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100437 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 0, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
439 /* insert the second module only as imported, not implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100440 mod2 = lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200441 assert_non_null(mod);
442 assert_non_null(mod2);
443 assert_ptr_not_equal(mod, mod2);
444 mod = ly_ctx_get_module_latest(ctx, "a");
445 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100446 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200447 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200448 /* work with module with no revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100449 mod = lys_parse_mem_module(ctx, str0, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200450 assert_non_null(mod);
451 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
452 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200453
Radek Krejci313d9902018-11-08 09:42:58 +0100454 str1 = "submodule b {belongs-to a {prefix a;}}";
Radek Krejci096235c2019-01-11 11:12:19 +0100455 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100456 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200457
Radek Krejci2390fb52019-04-30 13:27:43 +0200458 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
459 assert_string_equal(names[index - 1], mod->name);
460 }
461 assert_int_equal(9, index);
462
Radek Krejcib7db73a2018-10-24 14:18:40 +0200463 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100464 *state = NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200465 ly_ctx_destroy(ctx, NULL);
466}
467
Radek Krejci1e880032018-09-20 12:17:12 +0200468int main(void)
469{
470 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100471 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
472 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
473 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
Radek Krejcib3289d62019-09-18 12:21:39 +0200474 cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100475 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200476 };
477
478 return cmocka_run_group_tests(tests, NULL, NULL);
479}