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" |
| 17 | |
| 18 | #include <stdarg.h> |
| 19 | #include <stddef.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <cmocka.h> |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "libyang.h" |
| 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | |
| 31 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 32 | #define ENABLE_LOGGER_CHECKING 1 |
| 33 | |
| 34 | static void |
| 35 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 36 | { |
| 37 | (void) level; /* unused */ |
| 38 | (void) path; /* unused */ |
| 39 | |
| 40 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | logger_setup(void **state) |
| 45 | { |
| 46 | (void) state; /* unused */ |
| 47 | #if ENABLE_LOGGER_CHECKING |
| 48 | ly_set_log_clb(logger, 0); |
| 49 | #endif |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | #if ENABLE_LOGGER_CHECKING |
| 54 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 55 | #else |
| 56 | # define logbuf_assert(str) |
| 57 | #endif |
| 58 | |
| 59 | int __real_ly_set_add(struct ly_set *set, void *object, int options); |
| 60 | int __wrap_ly_set_add(struct ly_set *set, void *object, int options) |
| 61 | { |
| 62 | int wrap = mock_type(int); |
| 63 | |
| 64 | if (wrap) { |
| 65 | /* error */ |
| 66 | return -1; |
| 67 | } else { |
| 68 | return __real_ly_set_add(set, object, options); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | test_searchdirs(void **state) |
| 74 | { |
| 75 | (void) state; /* unused */ |
| 76 | |
| 77 | struct ly_ctx *ctx; |
| 78 | const char * const *list; |
| 79 | |
| 80 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 81 | |
| 82 | /* invalid arguments */ |
| 83 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 84 | logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir())."); |
| 85 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 86 | logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs())."); |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 88 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 89 | |
| 90 | /* readable and executable, but not a directory */ |
| 91 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context")); |
| 92 | logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory."); |
| 93 | /* not executable */ |
| 94 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_SRC"/src/context.c")); |
| 95 | logbuf_assert("Unable to use search directory \""TESTS_SRC"/src/context.c\" (Permission denied)"); |
| 96 | /* not existing */ |
| 97 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile")); |
| 98 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)"); |
| 99 | |
| 100 | /* ly_set_add() fails */ |
| 101 | will_return(__wrap_ly_set_add, 1); |
| 102 | assert_int_equal(LY_EMEM, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 103 | |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 104 | /* no change */ |
| 105 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL)); |
| 106 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 107 | /* correct path */ |
| 108 | will_return_always(__wrap_ly_set_add, 0); |
| 109 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src")); |
| 110 | assert_int_equal(1, ctx->search_paths.count); |
| 111 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 112 | |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 113 | /* duplicated paths */ |
| 114 | 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] | 115 | assert_int_equal(1, ctx->search_paths.count); |
| 116 | assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]); |
| 117 | |
| 118 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 119 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles")); |
| 120 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src")); |
| 121 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules")); |
| 122 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc")); |
| 123 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC)); |
| 124 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN)); |
| 125 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/tmp")); |
| 126 | assert_int_equal(8, ctx->search_paths.count); |
| 127 | |
| 128 | /* get searchpaths */ |
| 129 | list = ly_ctx_get_searchdirs(ctx); |
| 130 | assert_non_null(list); |
| 131 | assert_string_equal(TESTS_BIN"/src", list[0]); |
| 132 | assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]); |
| 133 | assert_string_equal(TESTS_SRC, list[5]); |
| 134 | assert_string_equal(TESTS_BIN, list[6]); |
| 135 | assert_string_equal("/tmp", list[7]); |
| 136 | assert_null(list[8]); |
| 137 | |
| 138 | /* removing searchpaths */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 139 | /* nonexisting */ |
| 140 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile")); |
| 141 | logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 142 | /* first */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 143 | 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] | 144 | assert_string_not_equal(TESTS_BIN"/src", list[0]); |
| 145 | assert_int_equal(7, ctx->search_paths.count); |
| 146 | /* middle */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 147 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 148 | assert_int_equal(6, ctx->search_paths.count); |
| 149 | /* last */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 150 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/tmp")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 151 | assert_int_equal(5, ctx->search_paths.count); |
| 152 | /* all */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 153 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 154 | assert_int_equal(0, ctx->search_paths.count); |
| 155 | |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 156 | /* again - no change */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 157 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 158 | |
| 159 | /* cleanup */ |
| 160 | ly_ctx_destroy(ctx, NULL); |
| 161 | |
| 162 | /* test searchdir list in ly_ctx_new() */ |
| 163 | assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx)); |
| 164 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)"); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 165 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/tmp:/tmp", 0, &ctx)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 166 | assert_int_equal(2, ctx->search_paths.count); |
| 167 | assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]); |
| 168 | assert_string_equal("/tmp", ctx->search_paths.objs[1]); |
| 169 | |
| 170 | /* cleanup */ |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 171 | ly_ctx_destroy(ctx, NULL); |
| 172 | } |
| 173 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 174 | static void |
| 175 | test_options(void **state) |
| 176 | { |
| 177 | (void) state; /* unused */ |
| 178 | |
| 179 | struct ly_ctx *ctx; |
| 180 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx)); |
| 181 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 182 | /* invalid arguments */ |
| 183 | assert_int_equal(0, ly_ctx_get_options(NULL)); |
| 184 | logbuf_assert("Invalid argument ctx (ly_ctx_get_options())."); |
| 185 | |
| 186 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0)); |
| 187 | logbuf_assert("Invalid argument ctx (ly_ctx_set_option())."); |
| 188 | assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0)); |
| 189 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_option())."); |
| 190 | |
| 191 | /* option not allowed to be changed */ |
| 192 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY)); |
| 193 | logbuf_assert("Invalid argument option (ly_ctx_set_option())."); |
| 194 | assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY)); |
| 195 | logbuf_assert("Invalid argument option (ly_ctx_set_option())."); |
| 196 | |
| 197 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 198 | /* unset */ |
| 199 | /* LY_CTX_ALLIMPLEMENTED */ |
| 200 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 201 | 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] | 202 | assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| 203 | |
| 204 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 205 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 206 | 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] | 207 | assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 208 | |
| 209 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 210 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 211 | 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] | 212 | assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 213 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 214 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 215 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 216 | 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] | 217 | assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 218 | |
| 219 | /* LY_CTX_TRUSTED */ |
| 220 | assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 221 | 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] | 222 | assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| 223 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 224 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 225 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 226 | /* set back */ |
| 227 | /* LY_CTX_ALLIMPLEMENTED */ |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 228 | 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] | 229 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED); |
| 230 | |
| 231 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
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_DISABLE_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 233 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 234 | |
| 235 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
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_SEARCHDIR_CWD)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 237 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 238 | |
| 239 | /* LY_CTX_PREFER_SEARCHDIRS */ |
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_PREFER_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 241 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 242 | |
| 243 | /* LY_CTX_TRUSTED */ |
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_TRUSTED)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 245 | assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED); |
| 246 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 247 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 248 | |
| 249 | /* cleanup */ |
| 250 | ly_ctx_destroy(ctx, NULL); |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | test_models(void **state) |
| 255 | { |
| 256 | (void) state; /* unused */ |
| 257 | |
| 258 | /* invalid arguments */ |
| 259 | assert_int_equal(0, ly_ctx_get_module_set_id(NULL)); |
| 260 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id())."); |
| 261 | |
| 262 | struct ly_ctx *ctx; |
| 263 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 264 | assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx)); |
| 265 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 266 | /* cleanup */ |
| 267 | ly_ctx_destroy(ctx, NULL); |
| 268 | } |
| 269 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 270 | int main(void) |
| 271 | { |
| 272 | const struct CMUnitTest tests[] = { |
| 273 | cmocka_unit_test_setup(test_searchdirs, logger_setup), |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 274 | cmocka_unit_test_setup(test_options, logger_setup), |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame^] | 275 | cmocka_unit_test_setup(test_models, logger_setup), |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 279 | } |