Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 1 | /* |
| 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" |
| 16 | #include "../../src/context.c" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 17 | #include "../../src/tree_schema.c" |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <stddef.h> |
| 21 | #include <setjmp.h> |
| 22 | #include <cmocka.h> |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "libyang.h" |
| 28 | |
| 29 | #define BUFSIZE 1024 |
| 30 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 31 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 32 | |
| 33 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 34 | #define ENABLE_LOGGER_CHECKING 1 |
| 35 | |
| 36 | static void |
| 37 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 38 | { |
| 39 | (void) level; /* unused */ |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 40 | if (store) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 41 | if (path && path[0]) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 42 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 43 | } else { |
| 44 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 45 | } |
| 46 | if (store > 0) { |
| 47 | --store; |
| 48 | } |
| 49 | } |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int |
| 53 | logger_setup(void **state) |
| 54 | { |
| 55 | (void) state; /* unused */ |
| 56 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 57 | ly_set_log_clb(logger, 1); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 58 | #endif |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 64 | #else |
| 65 | # define logbuf_assert(str) |
| 66 | #endif |
| 67 | |
| 68 | int __real_ly_set_add(struct ly_set *set, void *object, int options); |
| 69 | int __wrap_ly_set_add(struct ly_set *set, void *object, int options) |
| 70 | { |
| 71 | int wrap = mock_type(int); |
| 72 | |
| 73 | if (wrap) { |
| 74 | /* error */ |
| 75 | return -1; |
| 76 | } else { |
| 77 | return __real_ly_set_add(set, object, options); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | test_searchdirs(void **state) |
| 83 | { |
| 84 | (void) state; /* unused */ |
| 85 | |
| 86 | struct ly_ctx *ctx; |
| 87 | const char * const *list; |
| 88 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 89 | will_return_count(__wrap_ly_set_add, 0, 6); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 90 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 91 | |
| 92 | /* invalid arguments */ |
| 93 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 94 | logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir())."); |
| 95 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 96 | logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs())."); |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 97 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 98 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 99 | |
| 100 | /* readable and executable, but not a directory */ |
| 101 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context")); |
| 102 | logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory."); |
| 103 | /* not executable */ |
Radek Krejci | 9efa87a | 2018-09-26 15:14:03 +0200 | [diff] [blame] | 104 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__)); |
| 105 | logbuf_assert("Unable to use search directory \""__FILE__"\" (Permission denied)"); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 106 | /* not existing */ |
| 107 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile")); |
| 108 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)"); |
| 109 | |
| 110 | /* ly_set_add() fails */ |
| 111 | will_return(__wrap_ly_set_add, 1); |
| 112 | assert_int_equal(LY_EMEM, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 113 | |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 114 | /* no change */ |
| 115 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL)); |
| 116 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 117 | /* correct path */ |
| 118 | will_return_always(__wrap_ly_set_add, 0); |
| 119 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 120 | assert_int_equal(1, ctx->search_paths.count); |
| 121 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 122 | |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 123 | /* duplicated paths */ |
| 124 | assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 125 | assert_int_equal(1, ctx->search_paths.count); |
| 126 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 127 | |
| 128 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 129 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles")); |
| 130 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src")); |
| 131 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules")); |
| 132 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc")); |
| 133 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC)); |
| 134 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN)); |
| 135 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/tmp")); |
| 136 | assert_int_equal(8, ctx->search_paths.count); |
| 137 | |
| 138 | /* get searchpaths */ |
| 139 | list = ly_ctx_get_searchdirs(ctx); |
| 140 | assert_non_null(list); |
| 141 | assert_string_equal(TESTS_BIN"/src", list[0]); |
| 142 | assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]); |
| 143 | assert_string_equal(TESTS_SRC, list[5]); |
| 144 | assert_string_equal(TESTS_BIN, list[6]); |
| 145 | assert_string_equal("/tmp", list[7]); |
| 146 | assert_null(list[8]); |
| 147 | |
| 148 | /* removing searchpaths */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 149 | /* nonexisting */ |
| 150 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile")); |
| 151 | logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 152 | /* first */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 153 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/src")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 154 | assert_string_not_equal(TESTS_BIN"/src", list[0]); |
| 155 | assert_int_equal(7, ctx->search_paths.count); |
| 156 | /* middle */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 157 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 158 | assert_int_equal(6, ctx->search_paths.count); |
| 159 | /* last */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 160 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/tmp")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 161 | assert_int_equal(5, ctx->search_paths.count); |
| 162 | /* all */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 163 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 164 | assert_int_equal(0, ctx->search_paths.count); |
| 165 | |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 166 | /* again - no change */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 167 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 168 | |
| 169 | /* cleanup */ |
| 170 | ly_ctx_destroy(ctx, NULL); |
| 171 | |
| 172 | /* test searchdir list in ly_ctx_new() */ |
| 173 | assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx)); |
| 174 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)"); |
Radek Krejci | 51d341d | 2018-09-20 14:26:41 +0200 | [diff] [blame] | 175 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/tmp:/tmp:"TESTS_SRC, 0, &ctx)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 176 | assert_int_equal(2, ctx->search_paths.count); |
| 177 | assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]); |
| 178 | assert_string_equal("/tmp", ctx->search_paths.objs[1]); |
| 179 | |
| 180 | /* cleanup */ |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 181 | ly_ctx_destroy(ctx, NULL); |
| 182 | } |
| 183 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 184 | static void |
| 185 | test_options(void **state) |
| 186 | { |
| 187 | (void) state; /* unused */ |
| 188 | |
| 189 | struct ly_ctx *ctx; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 190 | |
| 191 | will_return_always(__wrap_ly_set_add, 0); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 192 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx)); |
| 193 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 194 | /* invalid arguments */ |
| 195 | assert_int_equal(0, ly_ctx_get_options(NULL)); |
| 196 | logbuf_assert("Invalid argument ctx (ly_ctx_get_options())."); |
| 197 | |
| 198 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0)); |
| 199 | logbuf_assert("Invalid argument ctx (ly_ctx_set_option())."); |
| 200 | assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0)); |
| 201 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_option())."); |
| 202 | |
| 203 | /* option not allowed to be changed */ |
| 204 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY)); |
| 205 | logbuf_assert("Invalid argument option (ly_ctx_set_option())."); |
| 206 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY)); |
| 207 | logbuf_assert("Invalid argument option (ly_ctx_set_option())."); |
| 208 | |
| 209 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 210 | /* unset */ |
| 211 | /* LY_CTX_ALLIMPLEMENTED */ |
| 212 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 213 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 214 | assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| 215 | |
| 216 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 217 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 218 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 219 | 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 Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 223 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 224 | assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 225 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 226 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 227 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 228 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 229 | assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 230 | |
| 231 | /* LY_CTX_TRUSTED */ |
| 232 | assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 233 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_TRUSTED)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 234 | assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| 235 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 236 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 237 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 238 | /* set back */ |
| 239 | /* LY_CTX_ALLIMPLEMENTED */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 240 | assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 241 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| 242 | |
| 243 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 244 | assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 245 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 246 | |
| 247 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 248 | assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 249 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 250 | |
| 251 | /* LY_CTX_PREFER_SEARCHDIRS */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 252 | assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 253 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 254 | |
| 255 | /* LY_CTX_TRUSTED */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 256 | assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_TRUSTED)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 257 | assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| 258 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 259 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 260 | |
| 261 | /* cleanup */ |
| 262 | ly_ctx_destroy(ctx, NULL); |
| 263 | } |
| 264 | |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 265 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 266 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 267 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 268 | { |
| 269 | *module_data = user_data; |
| 270 | *format = LYS_IN_YANG; |
| 271 | *free_module_data = NULL; |
| 272 | return LY_SUCCESS; |
| 273 | } |
| 274 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 275 | static void |
| 276 | test_models(void **state) |
| 277 | { |
| 278 | (void) state; /* unused */ |
| 279 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 280 | struct ly_ctx *ctx; |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 281 | const char *str; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 282 | struct lys_module *mod1, *mod2; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 283 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 284 | /* invalid arguments */ |
| 285 | assert_int_equal(0, ly_ctx_get_module_set_id(NULL)); |
| 286 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id())."); |
| 287 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 288 | will_return_always(__wrap_ly_set_add, 0); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 289 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 290 | assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx)); |
| 291 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 292 | assert_null(lys_parse_mem(ctx, "module x {namespace urn:x;prefix x;}", 3)); |
| 293 | logbuf_assert("Invalid schema input format."); |
| 294 | |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 295 | /* import callback */ |
| 296 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test")); |
| 297 | assert_ptr_equal(test_imp_clb, ctx->imp_clb); |
| 298 | assert_ptr_equal(str, ctx->imp_clb_data); |
| 299 | assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str)); |
| 300 | assert_string_equal("test", str); |
| 301 | |
| 302 | ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
| 303 | assert_null(ctx->imp_clb); |
| 304 | assert_null(ctx->imp_clb_data); |
| 305 | |
| 306 | /* submodule in multiple modules */ |
| 307 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule xx {belongs-to x;}"); |
| 308 | mod1 = lys_parse_mem(ctx, "module x {namespace urn:x;prefix x;include xx;revision 2018-10-24;}", LYS_IN_YANG); |
| 309 | assert_non_null(mod1); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 310 | mod2 = lys_parse_mem_(ctx, "module x {namespace urn:x;prefix x;include xx;revision 2018-10-25;}", LYS_IN_YANG, 0, NULL, NULL); |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 311 | assert_non_null(mod2); |
| 312 | assert_ptr_equal(mod1->parsed->includes[0].submodule, mod2->parsed->includes[0].submodule); |
| 313 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 314 | /* name collision of module and submodule */ |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 315 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a; revision 2018-10-30;}"); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 316 | assert_null(lys_parse_mem(ctx, "module y {namespace urn:y;prefix y;include y;}", LYS_IN_YANG)); |
| 317 | assert_int_equal(LY_EVALID, ly_errcode(ctx)); |
| 318 | logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| 319 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 320 | assert_non_null(lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", LYS_IN_YANG)); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 321 | assert_null(lys_parse_mem(ctx, "module y {namespace urn:y;prefix y;}", LYS_IN_YANG)); |
| 322 | assert_int_equal(LY_EVALID, ly_errcode(ctx)); |
| 323 | logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| 324 | |
| 325 | store = 1; |
| 326 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b;}"); |
| 327 | assert_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include y;}", LYS_IN_YANG)); |
| 328 | assert_int_equal(LY_EVALID, ly_errcode(ctx)); |
| 329 | logbuf_assert("Name collision between submodules of name \"y\". Line number 1."); |
| 330 | store = -1; |
| 331 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 332 | /* selecting correct revision of the submodules */ |
| 333 | ly_ctx_reset_latests(ctx); |
| 334 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a; revision 2018-10-31;}"); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 335 | mod2 = lys_parse_mem_(ctx, "module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", LYS_IN_YANG, 0, NULL, NULL); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 336 | assert_non_null(mod2); |
| 337 | assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date); |
| 338 | |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 339 | /* reloading module in case only the compiled module resists in the context */ |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 340 | mod1 = lys_parse_mem(ctx, "module w {namespace urn:w;prefix w;revision 2018-10-24;}", LYS_IN_YANG); |
| 341 | assert_non_null(mod1); |
| 342 | assert_int_equal(LY_SUCCESS, lys_compile(mod1, LYSC_OPT_FREE_SP)); |
| 343 | assert_non_null(mod1->compiled); |
| 344 | assert_null(mod1->parsed); |
| 345 | mod2 = lys_parse_mem(ctx, "module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", LYS_IN_YANG); |
| 346 | assert_non_null(mod2); |
| 347 | /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 348 | ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
| 349 | assert_int_equal(LY_ENOTFOUND, lys_compile(mod2, 0)); |
| 350 | logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found."); |
| 351 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}"); |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 352 | assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0)); |
| 353 | assert_non_null(mod1->parsed); |
| 354 | assert_string_equal("w", mod1->parsed->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 355 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 356 | /* cleanup */ |
| 357 | ly_ctx_destroy(ctx, NULL); |
| 358 | } |
| 359 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 360 | static void |
| 361 | test_get_models(void **state) |
| 362 | { |
| 363 | (void) state; /* unused */ |
| 364 | |
| 365 | struct ly_ctx *ctx; |
| 366 | const struct lys_module *mod, *mod2; |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 367 | const char *str0 = "module a {namespace urn:a;prefix a;}"; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 368 | const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}"; |
| 369 | const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}"; |
| 370 | |
| 371 | will_return_always(__wrap_ly_set_add, 0); |
| 372 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 373 | |
| 374 | /* invalid arguments */ |
| 375 | assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL)); |
| 376 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module())."); |
| 377 | assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL)); |
| 378 | logbuf_assert("Invalid argument name (ly_ctx_get_module())."); |
| 379 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL)); |
| 380 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns())."); |
| 381 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL)); |
| 382 | logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns())."); |
| 383 | assert_null(ly_ctx_get_module(ctx, "nonsence", NULL)); |
| 384 | |
| 385 | /* internal modules */ |
| 386 | assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types")); |
| 387 | mod = ly_ctx_get_module_implemented(ctx, "yang"); |
| 388 | assert_non_null(mod); |
| 389 | assert_non_null(mod->parsed); |
| 390 | assert_string_equal("yang", mod->parsed->name); |
| 391 | mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->parsed->ns); |
| 392 | assert_ptr_equal(mod, mod2); |
| 393 | assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05")); |
| 394 | assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15")); |
| 395 | assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15")); |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 396 | assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2017-08-17")); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 397 | |
| 398 | /* select module by revision */ |
| 399 | mod = lys_parse_mem(ctx, str1, LYS_IN_YANG); |
| 400 | /* invalid attempts - implementing module of the same name and inserting the same module */ |
| 401 | assert_null(lys_parse_mem(ctx, str2, LYS_IN_YANG)); |
| 402 | logbuf_assert("Module \"a\" is already implemented in the context."); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 403 | assert_null(lys_parse_mem_(ctx, str1, LYS_IN_YANG, 0, NULL, NULL)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 404 | logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context."); |
| 405 | /* insert the second module only as imported, not implemented */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 406 | mod2 = lys_parse_mem_(ctx, str2, LYS_IN_YANG, 0, NULL, NULL); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 407 | assert_non_null(mod); |
| 408 | assert_non_null(mod2); |
| 409 | assert_ptr_not_equal(mod, mod2); |
| 410 | mod = ly_ctx_get_module_latest(ctx, "a"); |
| 411 | assert_ptr_equal(mod, mod2); |
| 412 | mod2 = ly_ctx_get_module_latest_ns(ctx, mod->parsed->ns); |
| 413 | assert_ptr_equal(mod, mod2); |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 414 | /* work with module with no revision */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 415 | mod = lys_parse_mem_(ctx, str0, LYS_IN_YANG, 0, NULL, NULL); |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 416 | assert_non_null(mod); |
| 417 | assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL)); |
| 418 | assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a")); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 419 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 420 | str1 = "submodule b {belongs-to a;}"; |
| 421 | assert_null(lys_parse_mem(ctx, str1, LYS_IN_YANG)); |
| 422 | logbuf_assert("Input data contains submodule \"b\" which cannot be parsed directly without its main module."); |
| 423 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 424 | /* cleanup */ |
| 425 | ly_ctx_destroy(ctx, NULL); |
| 426 | } |
| 427 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 428 | int main(void) |
| 429 | { |
| 430 | const struct CMUnitTest tests[] = { |
| 431 | cmocka_unit_test_setup(test_searchdirs, logger_setup), |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 432 | cmocka_unit_test_setup(test_options, logger_setup), |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 433 | cmocka_unit_test_setup(test_models, logger_setup), |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 434 | cmocka_unit_test_setup(test_get_models, logger_setup), |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 438 | } |