blob: 624cf6f1d4ca411a633dc2f180dabe0325e7b887 [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 Krejci1e880032018-09-20 12:17:12 +020027
28#include <stdarg.h>
29#include <stddef.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include <string.h>
34#include <stdio.h>
35
36#include "libyang.h"
37
38#define BUFSIZE 1024
39char logbuf[BUFSIZE] = {0};
Radek Krejcifaa1eac2018-10-30 14:34:55 +010040int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci1e880032018-09-20 12:17:12 +020041
42/* set to 0 to printing error messages to stderr instead of checking them in code */
43#define ENABLE_LOGGER_CHECKING 1
44
45static void
46logger(LY_LOG_LEVEL level, const char *msg, const char *path)
47{
48 (void) level; /* unused */
Radek Krejcifaa1eac2018-10-30 14:34:55 +010049 if (store) {
Radek Krejci9ed7a192018-10-31 16:23:51 +010050 if (path && path[0]) {
Radek Krejcifaa1eac2018-10-30 14:34:55 +010051 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
52 } else {
53 strncpy(logbuf, msg, BUFSIZE - 1);
54 }
55 if (store > 0) {
56 --store;
57 }
58 }
Radek Krejci1e880032018-09-20 12:17:12 +020059}
60
61static int
62logger_setup(void **state)
63{
64 (void) state; /* unused */
65#if ENABLE_LOGGER_CHECKING
Radek Krejcifaa1eac2018-10-30 14:34:55 +010066 ly_set_log_clb(logger, 1);
Radek Krejci1e880032018-09-20 12:17:12 +020067#endif
68 return 0;
69}
70
Radek Krejci0bcdaed2019-01-10 10:21:34 +010071static int
72logger_teardown(void **state)
73{
74 (void) state; /* unused */
75#if ENABLE_LOGGER_CHECKING
76 if (*state) {
77 fprintf(stderr, "%s\n", logbuf);
78 }
79#endif
80 return 0;
81}
82
Radek Krejci1e880032018-09-20 12:17:12 +020083#if ENABLE_LOGGER_CHECKING
84# define logbuf_assert(str) assert_string_equal(logbuf, str)
85#else
86# define logbuf_assert(str)
87#endif
88
Radek Krejci1e880032018-09-20 12:17:12 +020089static void
90test_searchdirs(void **state)
91{
Radek Krejci0bcdaed2019-01-10 10:21:34 +010092 *state = test_searchdirs;
Radek Krejci1e880032018-09-20 12:17:12 +020093
94 struct ly_ctx *ctx;
95 const char * const *list;
96
97 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
98
99 /* invalid arguments */
100 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
101 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
102 assert_null(ly_ctx_get_searchdirs(NULL));
103 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +0200104 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200105 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200106
107 /* readable and executable, but not a directory */
108 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context"));
109 logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory.");
110 /* not executable */
Radek Krejci9efa87a2018-09-26 15:14:03 +0200111 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__));
112 logbuf_assert("Unable to use search directory \""__FILE__"\" (Permission denied)");
Radek Krejci1e880032018-09-20 12:17:12 +0200113 /* not existing */
114 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
115 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
116
117 /* ly_set_add() fails */
Radek Krejcia77e0e12018-09-20 12:39:15 +0200118 /* no change */
119 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
120
Radek Krejci1e880032018-09-20 12:17:12 +0200121 /* correct path */
Radek Krejci1e880032018-09-20 12:17:12 +0200122 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
123 assert_int_equal(1, ctx->search_paths.count);
124 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
125
Radek Krejci14946ab2018-09-20 13:42:06 +0200126 /* duplicated paths */
127 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200128 assert_int_equal(1, ctx->search_paths.count);
129 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
130
131 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
132 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
133 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
134 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
135 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
136 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
137 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
Radek Krejcief6867f2018-11-27 11:32:00 +0100138 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200139 assert_int_equal(8, ctx->search_paths.count);
140
141 /* get searchpaths */
142 list = ly_ctx_get_searchdirs(ctx);
143 assert_non_null(list);
144 assert_string_equal(TESTS_BIN"/src", list[0]);
145 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
146 assert_string_equal(TESTS_SRC, list[5]);
147 assert_string_equal(TESTS_BIN, list[6]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100148 assert_string_equal("/home", list[7]);
Radek Krejci1e880032018-09-20 12:17:12 +0200149 assert_null(list[8]);
150
151 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200152 /* nonexisting */
153 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
154 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200155 /* first */
Radek Krejci0759b792018-09-20 13:53:15 +0200156 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200157 assert_string_not_equal(TESTS_BIN"/src", list[0]);
158 assert_int_equal(7, ctx->search_paths.count);
159 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200160 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200161 assert_int_equal(6, ctx->search_paths.count);
162 /* last */
Radek Krejcief6867f2018-11-27 11:32:00 +0100163 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/home"));
Radek Krejci1e880032018-09-20 12:17:12 +0200164 assert_int_equal(5, ctx->search_paths.count);
165 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200166 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200167 assert_int_equal(0, ctx->search_paths.count);
168
Radek Krejci555c9bb2018-09-20 12:45:13 +0200169 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200170 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200171
172 /* cleanup */
173 ly_ctx_destroy(ctx, NULL);
174
175 /* test searchdir list in ly_ctx_new() */
176 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
177 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
Radek Krejcief6867f2018-11-27 11:32:00 +0100178 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, 0, &ctx));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200179 assert_int_equal(2, ctx->search_paths.count);
180 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
Radek Krejcief6867f2018-11-27 11:32:00 +0100181 assert_string_equal("/home", ctx->search_paths.objs[1]);
Radek Krejci555c9bb2018-09-20 12:45:13 +0200182
183 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100184 *state = NULL;
Radek Krejci1e880032018-09-20 12:17:12 +0200185 ly_ctx_destroy(ctx, NULL);
186}
187
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200188static void
189test_options(void **state)
190{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100191 *state = test_options;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200192
193 struct ly_ctx *ctx;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200194
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200195 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
196
Radek Krejci962a8102018-09-20 14:21:06 +0200197 /* invalid arguments */
198 assert_int_equal(0, ly_ctx_get_options(NULL));
199 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
200
201 assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0));
202 logbuf_assert("Invalid argument ctx (ly_ctx_set_option()).");
203 assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0));
204 logbuf_assert("Invalid argument ctx (ly_ctx_unset_option()).");
205
206 /* option not allowed to be changed */
207 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
208 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
209 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
210 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
211
212
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200213 /* unset */
214 /* LY_CTX_ALLIMPLEMENTED */
215 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200216 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200217 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
218
219 /* LY_CTX_DISABLE_SEARCHDIRS */
220 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200221 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200222 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
223
224 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
225 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci962a8102018-09-20 14:21:06 +0200226 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200227 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
228
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200229 /* LY_CTX_PREFER_SEARCHDIRS */
230 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200231 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200232 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
233
234 /* LY_CTX_TRUSTED */
235 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200236 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200237 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
238
Radek Krejci962a8102018-09-20 14:21:06 +0200239 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
240
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200241 /* set back */
242 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200243 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200244 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
245
246 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200247 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200248 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
249
250 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci962a8102018-09-20 14:21:06 +0200251 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200252 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
253
254 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200255 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200256 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
257
258 /* LY_CTX_TRUSTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200259 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200260 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
261
Radek Krejci962a8102018-09-20 14:21:06 +0200262 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
263
264 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100265 *state = NULL;
Radek Krejci962a8102018-09-20 14:21:06 +0200266 ly_ctx_destroy(ctx, NULL);
267}
268
Radek Krejci2d31ea72018-10-25 15:46:42 +0200269static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
270 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
271 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
272{
273 *module_data = user_data;
274 *format = LYS_IN_YANG;
275 *free_module_data = NULL;
276 return LY_SUCCESS;
277}
278
Radek Krejci962a8102018-09-20 14:21:06 +0200279static void
280test_models(void **state)
281{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100282 *state = test_models;
Radek Krejci962a8102018-09-20 14:21:06 +0200283
Radek Krejcib7db73a2018-10-24 14:18:40 +0200284 struct ly_ctx *ctx;
Radek Krejci2d31ea72018-10-25 15:46:42 +0200285 const char *str;
Radek Krejci1aefdf72018-11-01 11:01:39 +0100286 struct lys_module *mod1, *mod2;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200287
Radek Krejci962a8102018-09-20 14:21:06 +0200288 /* invalid arguments */
289 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
290 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
291
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100292 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
Radek Krejci962a8102018-09-20 14:21:06 +0200293 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
294
Radek Krejci096235c2019-01-11 11:12:19 +0100295 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 +0100296 logbuf_assert("Invalid schema input format.");
297
Radek Krejci2d31ea72018-10-25 15:46:42 +0200298 /* import callback */
299 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test"));
300 assert_ptr_equal(test_imp_clb, ctx->imp_clb);
301 assert_ptr_equal(str, ctx->imp_clb_data);
302 assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str));
303 assert_string_equal("test", str);
304
305 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
306 assert_null(ctx->imp_clb);
307 assert_null(ctx->imp_clb_data);
308
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100309 /* name collision of module and submodule */
Radek Krejci313d9902018-11-08 09:42:58 +0100310 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 +0100311 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 +0100312 assert_int_equal(LY_EVALID, ly_errcode(ctx));
313 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
314
Radek Krejci096235c2019-01-11 11:12:19 +0100315 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));
316 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 +0100317 assert_int_equal(LY_EVALID, ly_errcode(ctx));
318 logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1.");
319
320 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100321 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}");
Radek Krejci096235c2019-01-11 11:12:19 +0100322 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 +0100323 assert_int_equal(LY_EVALID, ly_errcode(ctx));
324 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;}");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100330 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 +0100331 assert_non_null(mod2);
332 assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date);
333
Radek Krejci9c536962018-10-31 14:52:13 +0100334 /* reloading module in case only the compiled module resists in the context */
Radek Krejci096235c2019-01-11 11:12:19 +0100335 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 +0100336 assert_non_null(mod1);
337 assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP));
338 assert_non_null(mod1->compiled);
339 assert_null(mod1->parsed);
Radek Krejci096235c2019-01-11 11:12:19 +0100340 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 +0100341 assert_non_null(mod2);
342 /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100343 ly_ctx_set_module_imp_clb(ctx, NULL, NULL);
344 assert_int_equal(LY_ENOTFOUND, lys_compile(mod2, 0));
345 logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");
346 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 +0100347 assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0));
348 assert_non_null(mod1->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100349 assert_string_equal("w", mod1->name);
Radek Krejcie9e987e2018-10-31 12:50:27 +0100350
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200351 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100352 *state = NULL;
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200353 ly_ctx_destroy(ctx, NULL);
354}
355
Radek Krejcib7db73a2018-10-24 14:18:40 +0200356static void
357test_get_models(void **state)
358{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100359 *state = test_get_models;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200360
361 struct ly_ctx *ctx;
362 const struct lys_module *mod, *mod2;
Radek Krejci5e163df2018-10-24 14:42:15 +0200363 const char *str0 = "module a {namespace urn:a;prefix a;}";
Radek Krejcib7db73a2018-10-24 14:18:40 +0200364 const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
365 const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
366
Radek Krejcib7db73a2018-10-24 14:18:40 +0200367 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
368
369 /* invalid arguments */
370 assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL));
371 logbuf_assert("Invalid argument ctx (ly_ctx_get_module()).");
372 assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL));
373 logbuf_assert("Invalid argument name (ly_ctx_get_module()).");
374 assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL));
375 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns()).");
376 assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL));
377 logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns()).");
378 assert_null(ly_ctx_get_module(ctx, "nonsence", NULL));
379
380 /* internal modules */
381 assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types"));
382 mod = ly_ctx_get_module_implemented(ctx, "yang");
383 assert_non_null(mod);
384 assert_non_null(mod->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100385 assert_string_equal("yang", mod->name);
386 mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200387 assert_ptr_equal(mod, mod2);
388 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05"));
389 assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15"));
390 assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15"));
Radek Krejci5e163df2018-10-24 14:42:15 +0200391 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 +0200392
393 /* select module by revision */
Radek Krejci096235c2019-01-11 11:12:19 +0100394 mod = lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200395 /* invalid attempts - implementing module of the same name and inserting the same module */
Radek Krejci096235c2019-01-11 11:12:19 +0100396 assert_null(lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200397 logbuf_assert("Module \"a\" is already implemented in the context.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100398 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 0, NULL, NULL));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200399 logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context.");
400 /* insert the second module only as imported, not implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100401 mod2 = lys_parse_mem_module(ctx, str2, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200402 assert_non_null(mod);
403 assert_non_null(mod2);
404 assert_ptr_not_equal(mod, mod2);
405 mod = ly_ctx_get_module_latest(ctx, "a");
406 assert_ptr_equal(mod, mod2);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100407 mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200408 assert_ptr_equal(mod, mod2);
Radek Krejci5e163df2018-10-24 14:42:15 +0200409 /* work with module with no revision */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100410 mod = lys_parse_mem_module(ctx, str0, LYS_IN_YANG, 0, NULL, NULL);
Radek Krejci5e163df2018-10-24 14:42:15 +0200411 assert_non_null(mod);
412 assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL));
413 assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a"));
Radek Krejcib7db73a2018-10-24 14:18:40 +0200414
Radek Krejci313d9902018-11-08 09:42:58 +0100415 str1 = "submodule b {belongs-to a {prefix a;}}";
Radek Krejci096235c2019-01-11 11:12:19 +0100416 assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100417 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
Radek Krejcid33273d2018-10-25 14:55:52 +0200418
Radek Krejcib7db73a2018-10-24 14:18:40 +0200419 /* cleanup */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100420 *state = NULL;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200421 ly_ctx_destroy(ctx, NULL);
422}
423
Radek Krejci1e880032018-09-20 12:17:12 +0200424int main(void)
425{
426 const struct CMUnitTest tests[] = {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100427 cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown),
428 cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown),
429 cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown),
430 cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown),
Radek Krejci1e880032018-09-20 12:17:12 +0200431 };
432
433 return cmocka_run_group_tests(tests, NULL, NULL);
434}