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