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