blob: 89fd762f1666833f6a91ad754a411d5f2d4422d1 [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 Krejcicc9688c2020-11-30 16:33:10 +0100130 assert_int_equal(7, ctx->search_paths.count);
Radek Krejci1e880032018-09-20 12:17:12 +0200131
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 Krejcicc9688c2020-11-30 16:33:10 +0100139 assert_null(list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200140
141 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200142 /* nonexisting */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200143 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(ctx, "/nonexistingfile"));
144 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdir()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200145 /* first */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200146 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_BIN"/utests"));
Radek Krejci1fd5ad62020-05-15 15:48:42 +0200147 assert_string_not_equal(TESTS_BIN"/utests", list[0]);
Radek Krejcicc9688c2020-11-30 16:33:10 +0100148 assert_int_equal(6, ctx->search_paths.count);
Radek Krejci1e880032018-09-20 12:17:12 +0200149 /* middle */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200150 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200151 assert_int_equal(5, ctx->search_paths.count);
Radek Krejcicc9688c2020-11-30 16:33:10 +0100152 /* last */
153 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_BIN));
154 assert_int_equal(4, ctx->search_paths.count);
Radek Krejci1e880032018-09-20 12:17:12 +0200155 /* all */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200156 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(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 Krejcie58f97f2020-08-18 11:45:08 +0200160 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(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 Krejcicc9688c2020-11-30 16:33:10 +0100168 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":"TESTS_BIN":"TESTS_BIN":"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 Krejcicc9688c2020-11-30 16:33:10 +0100171 assert_string_equal(TESTS_BIN, 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 Krejci1deb5be2020-08-26 16:43:36 +0200185 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &ctx));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200186
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 */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200197 assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NO_YANGLIBRARY));
Radek Krejci3fa46b62019-09-11 10:47:30 +0200198 logbuf_assert("Invalid argument option (ly_ctx_set_options()).");
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()).");
Radek Krejci962a8102018-09-20 14:21:06 +0200201
202
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200203 /* unset */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200204 /* LY_CTX_ALL_IMPLEMENTED */
205 assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
206 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALL_IMPLEMENTED));
207 assert_int_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
208
209 /* LY_CTX_REF_IMPLEMENTED */
210 assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
211 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_REF_IMPLEMENTED));
212 assert_int_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200213
214 /* LY_CTX_DISABLE_SEARCHDIRS */
215 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200216 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200217 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
218
219 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
220 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200221 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200222 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
223
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200224 /* LY_CTX_PREFER_SEARCHDIRS */
225 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci3fa46b62019-09-11 10:47:30 +0200226 assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200227 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
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 */
Michal Vasko25d6ad02020-10-22 12:20:22 +0200232 /* LY_CTX_ALL_IMPLEMENTED */
233 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALL_IMPLEMENTED));
234 assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED);
235
236 /* LY_CTX_REF_IMPLEMENTED */
237 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_REF_IMPLEMENTED));
238 assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED);
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200239
240 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200241 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200242 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
243
244 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200245 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200246 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
247
248 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci3fa46b62019-09-11 10:47:30 +0200249 assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200250 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
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;
Michal Vasko63f3d842020-07-08 10:10:14 +0200275 struct ly_in *in;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200276 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100277 struct lys_module *mod1, *mod2;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100278 struct lys_glob_unres unres = {0};
Radek Krejcib7db73a2018-10-24 14:18:40 +0200279
Radek Krejci962a8102018-09-20 14:21:06 +0200280 /* invalid arguments */
281 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
282 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
283
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100284 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200285 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
286
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100288 assert_int_equal(LY_EINVAL, lys_create_module(ctx, in, 4, 1, NULL, NULL, NULL, &unres, &mod1));
289 lys_compile_unres_glob_erase(ctx, &unres);
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 Vasko405cc9e2020-12-01 12:01:27 +0100307 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
308 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200309 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100310 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
311
Michal Vasko63f3d842020-07-08 10:10:14 +0200312 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100313 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
314 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(ctx, &unres));
315 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 ly_in_free(in, 0);
317 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100318 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
319 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100321 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
322
323 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100324 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200325 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100326 assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1));
327 lys_compile_unres_glob_revert(ctx, &unres);
328 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200329 ly_in_free(in, 0);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100330 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
331 store = -1;
332
Radek Krejcie9e987e2018-10-31 12:50:27 +0100333 /* selecting correct revision of the submodules */
334 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100335 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 +0200336 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100337 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
338 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200339 ly_in_free(in, 0);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100340 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
341
Radek Krejci9c536962018-10-31 14:52:13 +0100342 /* reloading module in case only the compiled module resists in the context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100344 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 ly_in_free(in, 0);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200346 mod1->implemented = 1;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100347 assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0, &unres));
348 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(ctx, &unres));
349 lys_compile_unres_glob_erase(ctx, &unres);
Radek Krejci9c536962018-10-31 14:52:13 +0100350 assert_non_null(mod1->compiled);
Radek Krejci90d4e922020-10-12 15:55:33 +0200351 assert_non_null(mod1->parsed);
352
353#if 0
354 /* 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 +0200355
Michal Vasko63f3d842020-07-08 10:10:14 +0200356 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 +0100357 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100358 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200359 assert_int_equal(LY_ENOTFOUND, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200360 /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/
361 logbuf_assert("Recompilation of module \"w\" failed.");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200362 assert_null(mod2);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200363 ly_in_free(in, 0);
Radek Krejci90d4e922020-10-12 15:55:33 +0200364#endif
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200365
Michal Vasko63f3d842020-07-08 10:10:14 +0200366 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 +0100367 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}");
Michal Vasko405cc9e2020-12-01 12:01:27 +0100368 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod2));
369 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(ctx, &unres));
370 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200371 ly_in_free(in, 0);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200372 assert_non_null(mod2);
Radek Krejci9c536962018-10-31 14:52:13 +0100373 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100374 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100375
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200376 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100377 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200378 ly_ctx_destroy(ctx, NULL);
379}
380
Radek Krejcib7db73a2018-10-24 14:18:40 +0200381static void
Radek Krejcib3289d62019-09-18 12:21:39 +0200382test_imports(void **state)
383{
384 *state = test_imports;
385
386 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200387 const struct lys_module *mod1, *mod2, *import;
Radek Krejcib3289d62019-09-18 12:21:39 +0200388
389 /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision
390 * from the callback should be loaded into the context and used as an import */
391 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
392 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 +0200393 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}",
394 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200395 assert_int_equal(1, mod1->latest_revision);
396 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200397 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
398 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200399 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200400 assert_int_equal(2, import->latest_revision);
401 assert_int_equal(0, mod1->latest_revision);
402 assert_ptr_not_equal(mod1, import);
403 assert_string_equal("2019-09-17", import->revision);
404 assert_int_equal(0, import->implemented);
405 assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16"));
406 ly_ctx_destroy(ctx, NULL);
407
408 /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision
409 * already present in the context should be selected and the callback's revision should not be loaded into the context */
410 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
411 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 +0200412 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}",
413 LYS_IN_YANG, &mod1));
Radek Krejcib3289d62019-09-18 12:21:39 +0200414 assert_int_equal(1, mod1->latest_revision);
415 assert_int_equal(1, mod1->implemented);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200416 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}",
417 LYS_IN_YANG, &mod2));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200418 import = mod2->parsed->imports[0].module;
Radek Krejcib3289d62019-09-18 12:21:39 +0200419 assert_ptr_equal(mod1, import);
420 assert_int_equal(2, import->latest_revision);
421 assert_int_equal(1, import->implemented);
422 assert_string_equal("2019-09-18", import->revision);
423 assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17"));
424 ly_ctx_destroy(ctx, NULL);
425
426 /* cleanup */
427 *state = NULL;
428}
429
430static void
Radek Krejcib7db73a2018-10-24 14:18:40 +0200431test_get_models(void **state)
432{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100433 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200434
435 struct ly_ctx *ctx;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200436 struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200437 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200438 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
439 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 +0200440 struct ly_in *in0, *in1, *in2;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100441 struct lys_glob_unres unres = {0};
Radek Krejcib7db73a2018-10-24 14:18:40 +0200442
Radek Krejci2390fb52019-04-30 13:27:43 +0200443 unsigned int index = 0;
444 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
445
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0));
447 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
448 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200449 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
450
451 /* invalid arguments */
452 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
453 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
454 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
455 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
456 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
457 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
458 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
459 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
460 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
461
462 /* internal modules */
463 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
464 mod = ly_ctx_get_module_implemented(ctx, "yang");
465 assert_non_null(mod);
466 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100467 assert_string_equal("yang", mod->name);
468 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200469 assert_ptr_equal(mod, mod2);
470 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
471 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
472 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Michal Vaskof872e202020-05-27 11:49:06 +0200473 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 +0200474
475 /* select module by revision */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100476 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
477 assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(ctx, &unres));
478 lys_compile_unres_glob_erase(ctx, &unres);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200479 /* invalid attempts - implementing module of the same name and inserting the same module */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100480 assert_int_equal(LY_EDENIED, lys_create_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200481 logbuf_assert("Module \"a\" is already implemented in the context.");
Michal Vasko405cc9e2020-12-01 12:01:27 +0100482 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200483 ly_in_reset(in1);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100484 assert_int_equal(LY_EEXIST, lys_create_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200485 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
486 /* insert the second module only as imported, not implemented */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100487 lys_compile_unres_glob_erase(ctx, &unres);
Michal Vasko63f3d842020-07-08 10:10:14 +0200488 ly_in_reset(in2);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100489 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2));
490 lys_compile_unres_glob_erase(ctx, &unres);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200491 assert_non_null(mod2);
492 assert_ptr_not_equal(mod, mod2);
493 mod = ly_ctx_get_module_latest(ctx, "a");
494 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100495 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200496 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200497 /* work with module with no revision */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100498 assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod));
499 lys_compile_unres_glob_erase(ctx, &unres);
Radek Krejci5e163df2018-10-24 14:42:15 +0200500 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
501 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200502
Radek Krejci313d9902018-11-08 09:42:58 +0100503 str1 = "submodule b {belongs-to a {prefix a;}}";
Michal Vasko63f3d842020-07-08 10:10:14 +0200504 ly_in_free(in1, 0);
505 assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1));
Michal Vasko405cc9e2020-12-01 12:01:27 +0100506 assert_int_equal(LY_EINVAL, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100507 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Michal Vasko405cc9e2020-12-01 12:01:27 +0100508 lys_compile_unres_glob_erase(ctx, &unres);
Radek Krejcid33273d2018-10-25 14:55:52 +0200509
Michal Vasko3a41dff2020-07-15 14:30:28 +0200510 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(ctx, &index))) {
Radek Krejci2390fb52019-04-30 13:27:43 +0200511 assert_string_equal(names[index - 1], mod->name);
512 }
513 assert_int_equal(9, index);
514
Radek Krejcib7db73a2018-10-24 14:18:40 +0200515 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100516 *state = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200517 ly_in_free(in0, 0);
518 ly_in_free(in1, 0);
519 ly_in_free(in2, 0);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200520 ly_ctx_destroy(ctx, NULL);
521}
522
Radek Krejci1e880032018-09-20 12:17:12 +0200523int main(void)
524{
525 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100526 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
527 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
528 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
Radek Krejcib3289d62019-09-18 12:21:39 +0200529 cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100530 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200531 };
532
533 return cmocka_run_group_tests(tests, NULL, NULL);
534}