blob: 839ec027dd039c48577a3e3f4eb4f16d8ee96675 [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 Krejcif3f47842018-11-15 11:22:15 +010016#include "../../src/common.c"
17#include "../../src/log.c"
18#include "../../src/set.c"
19#include "../../src/hash_table.c"
20#include "../../src/xpath.c"
21#include "../../src/parser_yang.c"
Radek Krejci1e880032018-09-20 12:17:12 +020022#include "../../src/context.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010023#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "../../src/tree_schema_free.c"
25#include "../../src/tree_schema_compile.c"
Radek Krejcib7db73a2018-10-24 14:18:40 +020026#include "../../src/tree_schema.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "../../src/plugins_types.c"
Radek Krejci1e880032018-09-20 12:17:12 +020028
29#include <stdarg.h>
30#include <stddef.h>
31#include <setjmp.h>
32#include <cmocka.h>
33
34#include <string.h>
35#include <stdio.h>
36
37#include "libyang.h"
38
39#define BUFSIZE 1024
40char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010041int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020042
43/* set to 0 to printing error messages to stderr instead of checking them in code */
44#define ENABLE_LOGGER_CHECKING 1
45
46static void
47logger(LY_LOG_LEVEL level, const char *msg, const char *path)
48{
49 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010050 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010051 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010052 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
53 } else {
54 strncpy(logbuf, msg, BUFSIZE - 1);
55 }
56 if (store > 0) {
57 --store;
58 }
59 }
Radek Krejci1e880032018-09-20 12:17:12 +020060}
61
62static int
63logger_setup(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010067 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020068#endif
69 return 0;
70}
71
Radek Krejci0bcdaed2019-01-10 10:21:34 +010072static int
73logger_teardown(void **state)
74{
75 (void) state; /* unused */
76#if ENABLE_LOGGER_CHECKING
77 if (*state) {
78 fprintf(stderr, "%s\n", logbuf);
79 }
80#endif
81 return 0;
82}
83
Radek Krejci1e880032018-09-20 12:17:12 +020084#if ENABLE_LOGGER_CHECKING
85# define logbuf_assert(str) assert_string_equal(logbuf, str)
86#else
87# define logbuf_assert(str)
88#endif
89
Radek Krejci1e880032018-09-20 12:17:12 +020090static void
91test_searchdirs(void **state)
92{
Radek Krejci0bcdaed2019-01-10 10:21:34 +010093 *state = test_searchdirs;
Radek Krejci1e880032018-09-20 12:17:12 +020094
95 struct ly_ctx *ctx;
96 const char * const *list;
97
98 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
99
100 /* invalid arguments */
101 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
102 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
103 assert_null(ly_ctx_get_searchdirs(NULL));
104 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +0200105 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200106 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200107
108 /* readable and executable, but not a directory */
109 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context"));
110 logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory.");
111 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +0200112 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
Radek Krejci3a077b92019-04-09 17:10:35 +0200113 logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied).");
Radek Krejci1e880032018-09-20 12:17:12 +0200114 /* not existing */
115 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
Radek Krejci3a077b92019-04-09 17:10:35 +0200116 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejci1e880032018-09-20 12:17:12 +0200117
118 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200119 /* no change */
120 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
121
Radek Krejci1e880032018-09-20 12:17:12 +0200122 /* correct path */
Radek Krejci1e880032018-09-20 12:17:12 +0200123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
124 assert_int_equal(1, ctx->search_paths.count);
125 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
126
Radek Krejci14946ab2018-09-20 13:42:06 +0200127 /* duplicated paths */
128 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200129 assert_int_equal(1, ctx->search_paths.count);
130 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
131
132 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
133 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
134 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
135 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
136 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
137 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
138 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100139 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200140 assert_int_equal(8, ctx->search_paths.count);
141
142 /* get searchpaths */
143 list = ly_ctx_get_searchdirs(ctx);
144 assert_non_null(list);
145 assert_string_equal(TESTS_BIN"/src", list[0]);
146 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
147 assert_string_equal(TESTS_SRC, list[5]);
148 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100149 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200150 assert_null(list[8]);
151
152 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200153 /* nonexisting */
154 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
155 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200156 /* first */
Radek Krejci0759b792018-09-20 13:53:15 +0200157 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200158 assert_string_not_equal(TESTS_BIN"/src", list[0]);
159 assert_int_equal(7, ctx->search_paths.count);
160 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200161 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200162 assert_int_equal(6, ctx->search_paths.count);
163 /* last */
Radek Krejcief6867f2018-11-27 11:32:00 +0100164 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200165 assert_int_equal(5, ctx->search_paths.count);
166 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200167 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200168 assert_int_equal(0, ctx->search_paths.count);
169
Radek Krejci555c9bb2018-09-20 12:45:13 +0200170 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200171 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200172
173 /* cleanup */
174 ly_ctx_destroy(ctx, NULL);
175
176 /* test searchdir list in ly_ctx_new() */
177 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
Radek Krejci3a077b92019-04-09 17:10:35 +0200178 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory).");
Radek Krejcief6867f2018-11-27 11:32:00 +0100179 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, 0, &ctx));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200180 assert_int_equal(2, ctx->search_paths.count);
181 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100182 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200183
184 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100185 *state = NULL;
Radek Krejci1e880032018-09-20 12:17:12 +0200186 ly_ctx_destroy(ctx, NULL);
187}
188
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200189static void
190test_options(void **state)
191{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100192 *state = test_options;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200193
194 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200195
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200196 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
197
Radek Krejci962a8102018-09-20 14:21:06 +0200198 /* invalid arguments */
199 assert_int_equal(0, ly_ctx_get_options(NULL));
200 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
201
202 assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0));
203 logbuf_assert("Invalid argument ctx (ly_ctx_set_option()).");
204 assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0));
205 logbuf_assert("Invalid argument ctx (ly_ctx_unset_option()).");
206
207 /* option not allowed to be changed */
208 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
209 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
210 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
211 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
212
213
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200214 /* unset */
215 /* LY_CTX_ALLIMPLEMENTED */
216 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200217 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200218 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
219
220 /* LY_CTX_DISABLE_SEARCHDIRS */
221 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200222 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200223 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
224
225 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
226 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci962a8102018-09-20 14:21:06 +0200227 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200228 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
229
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200230 /* LY_CTX_PREFER_SEARCHDIRS */
231 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200232 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200233 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
234
235 /* LY_CTX_TRUSTED */
236 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200237 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200238 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
239
Radek Krejci962a8102018-09-20 14:21:06 +0200240 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
241
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200242 /* set back */
243 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200244 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200245 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
246
247 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200248 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200249 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
250
251 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci962a8102018-09-20 14:21:06 +0200252 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200253 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
254
255 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200256 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200257 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
258
259 /* LY_CTX_TRUSTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200260 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200261 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
262
Radek Krejci962a8102018-09-20 14:21:06 +0200263 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
264
265 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100266 *state = NULL;
Radek Krejci962a8102018-09-20 14:21:06 +0200267 ly_ctx_destroy(ctx, NULL);
268}
269
Radek Krejci2d31ea72018-10-25 15:46:42 +0200270static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
271 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
272 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
273{
274 *module_data = user_data;
275 *format = LYS_IN_YANG;
276 *free_module_data = NULL;
277 return LY_SUCCESS;
278}
279
Radek Krejci962a8102018-09-20 14:21:06 +0200280static void
281test_models(void **state)
282{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100283 *state = test_models;
Radek Krejci962a8102018-09-20 14:21:06 +0200284
Radek Krejcib7db73a2018-10-24 14:18:40 +0200285 struct ly_ctx *ctx;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200286 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100287 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200288
Radek Krejci962a8102018-09-20 14:21:06 +0200289 /* invalid arguments */
290 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
291 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
292
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100293 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200294 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
295
Radek Krejci096235c2019-01-11 11:12:19 +0100296 assert_null(lys_parse_mem_module(ctx, "module x {namespace urn:x;prefix x;}", 3, 1, NULL, NULL));
Radek Krejci9ed7a192018-10-31 16:23:51 +0100297 logbuf_assert("Invalid schema input format.");
298
Radek Krejci2d31ea72018-10-25 15:46:42 +0200299 /* import callback */
300 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
301 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
302 assert_ptr_equal(str, ctx->imp_clb_data);
303 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
304 assert_string_equal("test", str);
305
306 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
307 assert_null(ctx->imp_clb);
308 assert_null(ctx->imp_clb_data);
309
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100310 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100311 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 +0100312 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 +0100313 assert_int_equal(LY_EVALID, ly_errcode(ctx));
314 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
315
Radek Krejci096235c2019-01-11 11:12:19 +0100316 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));
317 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 +0100318 assert_int_equal(LY_EVALID, ly_errcode(ctx));
319 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
320
321 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100322 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Radek Krejci096235c2019-01-11 11:12:19 +0100323 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 +0100324 assert_int_equal(LY_EVALID, ly_errcode(ctx));
325 logbuf_assert("Name collision between submodules of name \"y\". Line number 1.");
326 store = -1;
327
Radek Krejcie9e987e2018-10-31 12:50:27 +0100328 /* selecting correct revision of the submodules */
329 ly_ctx_reset_latests(ctx);
Radek Krejci313d9902018-11-08 09:42:58 +0100330 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 +0100331 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 +0100332 assert_non_null(mod2);
333 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 */
Radek Krejci096235c2019-01-11 11:12:19 +0100336 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 +0100337 assert_non_null(mod1);
338 assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP));
339 assert_non_null(mod1->compiled);
340 assert_null(mod1->parsed);
Radek Krejci096235c2019-01-11 11:12:19 +0100341 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 +0100342 assert_non_null(mod2);
343 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100344 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
345 assert_int_equal(LY_ENOTFOUND, lys_compile(mod2, 0));
346 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
347 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 +0100348 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
349 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100350 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100351
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200352 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100353 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200354 ly_ctx_destroy(ctx, NULL);
355}
356
Radek Krejcib7db73a2018-10-24 14:18:40 +0200357static void
358test_get_models(void **state)
359{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100360 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200361
362 struct ly_ctx *ctx;
363 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200364 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200365 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
366 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
367
Radek Krejci2390fb52019-04-30 13:27:43 +0200368 unsigned int index = 0;
369 const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
370
Radek Krejcib7db73a2018-10-24 14:18:40 +0200371 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
372
373 /* invalid arguments */
374 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
375 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
376 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
377 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
378 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
379 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
380 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
381 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
382 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
383
384 /* internal modules */
385 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
386 mod = ly_ctx_get_module_implemented(ctx, "yang");
387 assert_non_null(mod);
388 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100389 assert_string_equal("yang", mod->name);
390 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200391 assert_ptr_equal(mod, mod2);
392 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
393 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
394 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Radek Krejci5e163df2018-10-24 14:42:15 +0200395 assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2017-08-17"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200396
397 /* select module by revision */
Radek Krejci096235c2019-01-11 11:12:19 +0100398 mod = lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399 /* invalid attempts - implementing module of the same name and inserting the same module */
Radek Krejci096235c2019-01-11 11:12:19 +0100400 assert_null(lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200401 logbuf_assert("Module \"a\" is already implemented in the context.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100402 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 0, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200403 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
404 /* insert the second module only as imported, not implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100405 mod2 = lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200406 assert_non_null(mod);
407 assert_non_null(mod2);
408 assert_ptr_not_equal(mod, mod2);
409 mod = ly_ctx_get_module_latest(ctx, "a");
410 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100411 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200412 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200413 /* work with module with no revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100414 mod = lys_parse_mem_module(ctx, str0, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200415 assert_non_null(mod);
416 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
417 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200418
Radek Krejci313d9902018-11-08 09:42:58 +0100419 str1 = "submodule b {belongs-to a {prefix a;}}";
Radek Krejci096235c2019-01-11 11:12:19 +0100420 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100421 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200422
Radek Krejci2390fb52019-04-30 13:27:43 +0200423 while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
424 assert_string_equal(names[index - 1], mod->name);
425 }
426 assert_int_equal(9, index);
427
Radek Krejcib7db73a2018-10-24 14:18:40 +0200428 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100429 *state = NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200430 ly_ctx_destroy(ctx, NULL);
431}
432
Radek Krejci1e880032018-09-20 12:17:12 +0200433int main(void)
434{
435 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100436 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
437 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
438 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
439 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200440 };
441
442 return cmocka_run_group_tests(tests, NULL, NULL);
443}