blob: 35c966420f5e88f786f070feac06b7c044331340 [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 Vaskoafac7822020-10-20 14:22:26 +020025#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "tests/config.h"
27#include "tree_schema_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020028#include "schema_compile.h"
Radek Krejci1e880032018-09-20 12:17:12 +020029
30#define BUFSIZE 1024
31char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010032int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020033
34/* set to 0 to printing error messages to stderr instead of checking them in code */
35#define ENABLE_LOGGER_CHECKING 1
36
37static void
38logger(LY_LOG_LEVEL level, const char *msg, const char *path)
39{
40 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010041 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010042 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010043 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
44 } else {
45 strncpy(logbuf, msg, BUFSIZE - 1);
46 }
47 if (store > 0) {
48 --store;
49 }
50 }
Radek Krejci1e880032018-09-20 12:17:12 +020051}
52
53static int
54logger_setup(void **state)
55{
56 (void) state; /* unused */
57#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010058 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020059#endif
60 return 0;
61}
62
Radek Krejci0bcdaed2019-01-10 10:21:34 +010063static int
64logger_teardown(void **state)
65{
66 (void) state; /* unused */
67#if ENABLE_LOGGER_CHECKING
68 if (*state) {
69 fprintf(stderr, "%s\n", logbuf);
70 }
71#endif
72 return 0;
73}
74
Radek Krejci1e880032018-09-20 12:17:12 +020075#if ENABLE_LOGGER_CHECKING
76# define logbuf_assert(str) assert_string_equal(logbuf, str)
77#else
78# define logbuf_assert(str)
79#endif
80
Radek Krejci1e880032018-09-20 12:17:12 +020081static void
82test_searchdirs(void **state)
83{
Radek Krejci0bcdaed2019-01-10 10:21:34 +010084 *state = test_searchdirs;
Radek Krejci1e880032018-09-20 12:17:12 +020085
86 struct ly_ctx *ctx;
87 const char * const *list;
88
89 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
90
91 /* invalid arguments */
92 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
93 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
94 assert_null(ly_ctx_get_searchdirs(NULL));
95 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejcie58f97f2020-08-18 11:45:08 +020096 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL));
97 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdir()).");
Radek Krejci1e880032018-09-20 12:17:12 +020098
99 /* readable and executable, but not a directory */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200100 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utest_context"));
101 logbuf_assert("Given search directory \""TESTS_BIN"/utest_context\" is not a directory.");
Radek Krejci1e880032018-09-20 12:17:12 +0200102 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +0200103 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
Radek Krejci3a077b92019-04-09 17:10:35 +0200104 logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied).");
Radek Krejci1e880032018-09-20 12:17:12 +0200105 /* not existing */
106 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
Radek Krejci3a077b92019-04-09 17:10:35 +0200107 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejci1e880032018-09-20 12:17:12 +0200108
109 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200110 /* no change */
111 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
112
Radek Krejci1e880032018-09-20 12:17:12 +0200113 /* correct path */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200114 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200115 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200116 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200117
Radek Krejci14946ab2018-09-20 13:42:06 +0200118 /* duplicated paths */
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200119 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1e880032018-09-20 12:17:12 +0200120 assert_int_equal(1, ctx->search_paths.count);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200121 assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200122
123 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
127 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
128 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
129 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100130 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200131 assert_int_equal(8, ctx->search_paths.count);
132
133 /* get searchpaths */
134 list = ly_ctx_get_searchdirs(ctx);
135 assert_non_null(list);
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200136 assert_string_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200137 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
138 assert_string_equal(TESTS_SRC, list[5]);
139 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100140 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200141 assert_null(list[8]);
142
143 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200144 /* nonexisting */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200145 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(ctx, "/nonexistingfile"));
146 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdir()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200147 /* first */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200148 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200149 assert_string_not_equal(TESTS_BIN"/utests", list[0]);
Radek Krejci1e880032018-09-20 12:17:12 +0200150 assert_int_equal(7, ctx->search_paths.count);
151 /* middle */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200152 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200153 assert_int_equal(6, ctx->search_paths.count);
154 /* last */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200155 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200156 assert_int_equal(5, ctx->search_paths.count);
157 /* all */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200158 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200159 assert_int_equal(0, ctx->search_paths.count);
160
Radek Krejci555c9bb2018-09-20 12:45:13 +0200161 /* again - no change */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200162 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200163
164 /* cleanup */
165 ly_ctx_destroy(ctx, NULL);
166
167 /* test searchdir list in ly_ctx_new() */
168 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
Radek Krejci3a077b92019-04-09 17:10:35 +0200169 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejcib3289d62019-09-18 12:21:39 +0200170 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 +0200171 assert_int_equal(2, ctx->search_paths.count);
172 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100173 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200174
175 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100176 *state = NULL;
Radek Krejci1e880032018-09-20 12:17:12 +0200177 ly_ctx_destroy(ctx, NULL);
178}
179
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200180static void
181test_options(void **state)
182{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100183 *state = test_options;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200184
185 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200186
Radek Krejci1deb5be2020-08-26 16:43:36 +0200187 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &ctx));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200188
Radek Krejci962a8102018-09-20 14:21:06 +0200189 /* invalid arguments */
190 assert_int_equal(0, ly_ctx_get_options(NULL));
191 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
192
Radek Krejci3fa46b62019-09-11 10:47:30 +0200193 assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0));
194 logbuf_assert("Invalid argument ctx (ly_ctx_set_options()).");
195 assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0));
196 logbuf_assert("Invalid argument ctx (ly_ctx_unset_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200197
198 /* option not allowed to be changed */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200199 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NO_YANGLIBRARY));
Radek Krejci3fa46b62019-09-11 10:47:30 +0200200 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
Michal Vasko25d6ad02020-10-22 12:20:22 +0200201 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NO_YANGLIBRARY));
Radek Krejci3fa46b62019-09-11 10:47:30 +0200202 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200203
204
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200205 /* unset */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200206 /* LY_CTX_ALL_IMPLEMENTED */
207 assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
208 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALL_IMPLEMENTED));
209 assert_int_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
210
211 /* LY_CTX_REF_IMPLEMENTED */
212 assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
213 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_REF_IMPLEMENTED));
214 assert_int_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200215
216 /* LY_CTX_DISABLE_SEARCHDIRS */
217 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200218 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200219 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
220
221 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
222 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200223 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200224 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
225
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200226 /* LY_CTX_PREFER_SEARCHDIRS */
227 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200228 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200229 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
230
Radek Krejci962a8102018-09-20 14:21:06 +0200231 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
232
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200233 /* set back */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200234 /* LY_CTX_ALL_IMPLEMENTED */
235 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALL_IMPLEMENTED));
236 assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
237
238 /* LY_CTX_REF_IMPLEMENTED */
239 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_REF_IMPLEMENTED));
240 assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200241
242 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200243 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200244 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
245
246 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200247 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200248 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
249
250 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200251 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200252 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
253
Radek Krejci962a8102018-09-20 14:21:06 +0200254 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
255
256 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100257 *state = NULL;
Radek Krejci962a8102018-09-20 14:21:06 +0200258 ly_ctx_destroy(ctx, NULL);
259}
260
Radek Krejci2d31ea72018-10-25 15:46:42 +0200261static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
262 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
263 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
264{
265 *module_data = user_data;
266 *format = LYS_IN_YANG;
267 *free_module_data = NULL;
268 return LY_SUCCESS;
269}
270
Radek Krejci962a8102018-09-20 14:21:06 +0200271static void
272test_models(void **state)
273{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100274 *state = test_models;
Radek Krejci962a8102018-09-20 14:21:06 +0200275
Radek Krejcib7db73a2018-10-24 14:18:40 +0200276 struct ly_ctx *ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200277 struct ly_in *in;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200278 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100279 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200280
Radek Krejci962a8102018-09-20 14:21:06 +0200281 /* invalid arguments */
282 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
283 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
284
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100285 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200286 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
287
Michal Vasko63f3d842020-07-08 10:10:14 +0200288 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200289 assert_int_equal(LY_EINVAL, lys_create_module(ctx, in, 4, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200290 ly_in_free(in, 0);
Radek Krejci9ed7a192018-10-31 16:23:51 +0100291 logbuf_assert("Invalid schema input format.");
292
Radek Krejci2d31ea72018-10-25 15:46:42 +0200293 /* import callback */
294 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
295 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
296 assert_ptr_equal(str, ctx->imp_clb_data);
297 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
298 assert_string_equal("test", str);
299
300 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
301 assert_null(ctx->imp_clb);
302 assert_null(ctx->imp_clb_data);
303
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100304 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100305 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 +0200306 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200307 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200308 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100309 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));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200312 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200313 ly_in_free(in, 0);
314 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200315 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100317 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
318
319 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100320 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200321 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200322 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100324 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
325 store = -1;
326
Radek Krejcie9e987e2018-10-31 12:50:27 +0100327 /* selecting correct revision of the submodules */
328 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100329 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 +0200330 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200331 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod2));
Michal Vasko63f3d842020-07-08 10:10:14 +0200332 ly_in_free(in, 0);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100333 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
334
Radek Krejci9c536962018-10-31 14:52:13 +0100335 /* reloading module in case only the compiled module resists in the context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200336 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200337 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200338 ly_in_free(in, 0);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200339 mod1->implemented = 1;
Radek Krejci90d4e922020-10-12 15:55:33 +0200340 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0));
Radek Krejci9c536962018-10-31 14:52:13 +0100341 assert_non_null(mod1->compiled);
Radek Krejci90d4e922020-10-12 15:55:33 +0200342 assert_non_null(mod1->parsed);
343
344#if 0
345 /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */
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));
Radek Krejci9c536962018-10-31 14:52:13 +0100348 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100349 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200350 assert_int_equal(LY_ENOTFOUND, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200351 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
352 logbuf_assert("Recompilation of module \"w\" failed.");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200353 assert_null(mod2);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200354 ly_in_free(in, 0);
Radek Krejci90d4e922020-10-12 15:55:33 +0200355#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200356
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));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100358 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Michal Vasko7a0b0762020-09-02 16:37:01 +0200359 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
360 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200361 assert_non_null(mod2);
Radek Krejci9c536962018-10-31 14:52:13 +0100362 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100363 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100364
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200365 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100366 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200367 ly_ctx_destroy(ctx, NULL);
368}
369
Radek Krejcib7db73a2018-10-24 14:18:40 +0200370static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200371test_imports(void **state)
372{
373 *state = test_imports;
374
375 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200376 const struct lys_module *mod1, *mod2, *import;
Radek Krejcib3289d62019-09-18 12:21:39 +0200377
378 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
379 * from the callback should be loaded into the context and used as an import */
380 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
381 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 +0200382 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
383 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200384 assert_int_equal(1, mod1->latest_revision);
385 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200386 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
387 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200388 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200389 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;}");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200401 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
402 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200403 assert_int_equal(1, mod1->latest_revision);
404 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200405 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
406 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200407 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200408 assert_ptr_equal(mod1, import);
409 assert_int_equal(2, import->latest_revision);
410 assert_int_equal(1, import->implemented);
411 assert_string_equal("2019-09-18", import->revision);
412 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
413 ly_ctx_destroy(ctx, NULL);
414
415 /* cleanup */
416 *state = NULL;
417}
418
419static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200420test_get_models(void **state)
421{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100422 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423
424 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200425 struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200426 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200427 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
428 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 +0200429 struct ly_in *in0, *in1, *in2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200430
Radek Krejci2390fb52019-04-30 13:27:43 +0200431 unsigned int index = 0;
432 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
433
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
435 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
436 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200437 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
438
439 /* invalid arguments */
440 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
441 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
442 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
443 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
444 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
445 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
446 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
447 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
448 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
449
450 /* internal modules */
451 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
452 mod = ly_ctx_get_module_implemented(ctx, "yang");
453 assert_non_null(mod);
454 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100455 assert_string_equal("yang", mod->name);
456 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200457 assert_ptr_equal(mod, mod2);
458 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
459 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
460 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200461 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 +0200462
463 /* select module by revision */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200464 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200465 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200466 assert_int_equal(LY_EDENIED, lys_create_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200467 logbuf_assert("Module \"a\" is already implemented in the context.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 ly_in_reset(in1);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200469 assert_int_equal(LY_EEXIST, lys_create_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200470 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
471 /* insert the second module only as imported, not implemented */
Michal Vasko63f3d842020-07-08 10:10:14 +0200472 ly_in_reset(in2);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200473 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL, &mod2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200474 assert_non_null(mod2);
475 assert_ptr_not_equal(mod, mod2);
476 mod = ly_ctx_get_module_latest(ctx, "a");
477 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100478 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200479 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200480 /* work with module with no revision */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200481 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL, &mod));
Radek Krejci5e163df2018-10-24 14:42:15 +0200482 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));
Michal Vasko7a0b0762020-09-02 16:37:01 +0200488 assert_int_equal(LY_EINVAL, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod));
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
Michal Vasko3a41dff2020-07-15 14:30:28 +0200491 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(ctx, &index))) {
Radek Krejci2390fb52019-04-30 13:27:43 +0200492 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}