Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1 | /* |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_context.c |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 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 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
| 16 | |
| 17 | #include "common.h" |
| 18 | #include "context.h" |
| 19 | #include "in.h" |
| 20 | #include "schema_compile.h" |
Radek Krejci | ef5f767 | 2021-04-01 17:04:12 +0200 | [diff] [blame] | 21 | #include "tests_config.h" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 22 | #include "tree_schema_internal.h" |
Jan Kundrát | e2789d3 | 2021-12-13 18:28:45 +0100 | [diff] [blame] | 23 | #ifdef _WIN32 |
| 24 | static void |
| 25 | slashes_to_backslashes(char *path) |
| 26 | { |
| 27 | while ((path = strchr(path, '/'))) { |
| 28 | *path++ = '\\'; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static void |
| 33 | test_searchdirs(void **state) |
| 34 | { |
| 35 | const char * const *list; |
| 36 | char *path1 = strdup(TESTS_BIN "/utests"); |
| 37 | char *path2 = strdup(TESTS_SRC); |
| 38 | |
| 39 | slashes_to_backslashes(path1); |
| 40 | slashes_to_backslashes(path2); |
| 41 | |
| 42 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 43 | CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL); |
| 44 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 45 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL); |
| 46 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL)); |
| 47 | CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL); |
| 48 | |
| 49 | /* correct path */ |
| 50 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path1)); |
| 51 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 52 | assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]); |
| 53 | |
| 54 | /* duplicated paths */ |
| 55 | assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, path1)); |
| 56 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 57 | assert_string_equal(path1, UTEST_LYCTX->search_paths.objs[0]); |
| 58 | |
| 59 | /* another path */ |
| 60 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, path2)); |
| 61 | assert_int_equal(2, UTEST_LYCTX->search_paths.count); |
| 62 | assert_string_equal(path2, UTEST_LYCTX->search_paths.objs[1]); |
| 63 | |
| 64 | /* get searchpaths */ |
| 65 | list = ly_ctx_get_searchdirs(UTEST_LYCTX); |
| 66 | assert_non_null(list); |
| 67 | assert_string_equal(path1, list[0]); |
| 68 | assert_string_equal(path2, list[1]); |
| 69 | assert_null(list[2]); |
| 70 | |
| 71 | /* removing searchpaths */ |
| 72 | /* nonexisting */ |
| 73 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile")); |
| 74 | CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL); |
| 75 | |
| 76 | /* first */ |
| 77 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path1)); |
| 78 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 79 | assert_string_not_equal(path1, list[0]); |
| 80 | |
| 81 | /* second */ |
| 82 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, path2)); |
| 83 | assert_int_equal(0, UTEST_LYCTX->search_paths.count); |
| 84 | |
| 85 | free(path1); |
| 86 | free(path2); |
| 87 | } |
| 88 | |
| 89 | #else |
| 90 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 91 | static void |
| 92 | test_searchdirs(void **state) |
| 93 | { |
| 94 | const char * const *list; |
| 95 | |
| 96 | /* invalid arguments */ |
| 97 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 98 | CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL); |
| 99 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 100 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL); |
| 101 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL)); |
| 102 | CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL); |
| 103 | |
| 104 | /* readable and executable, but not a directory */ |
| 105 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context")); |
| 106 | CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 107 | /* not existing */ |
| 108 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile")); |
| 109 | CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL); |
| 110 | |
| 111 | /* ly_set_add() fails */ |
| 112 | /* no change */ |
| 113 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL)); |
| 114 | |
| 115 | /* correct path */ |
| 116 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 117 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 118 | assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]); |
| 119 | |
| 120 | /* duplicated paths */ |
| 121 | assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 122 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 123 | assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]); |
| 124 | |
| 125 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 126 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles")); |
| 127 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src")); |
| 128 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules")); |
| 129 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc")); |
| 130 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC)); |
| 131 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN)); |
| 132 | assert_int_equal(7, UTEST_LYCTX->search_paths.count); |
| 133 | |
| 134 | /* get searchpaths */ |
| 135 | list = ly_ctx_get_searchdirs(UTEST_LYCTX); |
| 136 | assert_non_null(list); |
| 137 | assert_string_equal(TESTS_BIN "/utests", list[0]); |
| 138 | assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]); |
| 139 | assert_string_equal(TESTS_SRC, list[5]); |
| 140 | assert_string_equal(TESTS_BIN, list[6]); |
| 141 | assert_null(list[7]); |
| 142 | |
| 143 | /* removing searchpaths */ |
| 144 | /* nonexisting */ |
| 145 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile")); |
| 146 | CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL); |
| 147 | /* first */ |
| 148 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 149 | assert_string_not_equal(TESTS_BIN "/utests", list[0]); |
| 150 | assert_int_equal(6, UTEST_LYCTX->search_paths.count); |
| 151 | /* middle */ |
| 152 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC)); |
| 153 | assert_int_equal(5, UTEST_LYCTX->search_paths.count); |
| 154 | /* last */ |
| 155 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN)); |
| 156 | assert_int_equal(4, UTEST_LYCTX->search_paths.count); |
| 157 | /* all */ |
| 158 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL)); |
| 159 | assert_int_equal(0, UTEST_LYCTX->search_paths.count); |
| 160 | |
| 161 | /* again - no change */ |
| 162 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL)); |
| 163 | |
| 164 | /* cleanup */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 165 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 166 | |
| 167 | /* test searchdir list in ly_ctx_new() */ |
| 168 | assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX)); |
| 169 | CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL); |
Jan Kundrát | 34bb928 | 2021-12-13 18:45:50 +0100 | [diff] [blame] | 170 | assert_int_equal(LY_SUCCESS, |
| 171 | ly_ctx_new(TESTS_SRC PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_BIN PATH_SEPARATOR TESTS_SRC, |
| 172 | LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 173 | assert_int_equal(2, UTEST_LYCTX->search_paths.count); |
| 174 | assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]); |
| 175 | assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]); |
| 176 | } |
| 177 | |
Jan Kundrát | e2789d3 | 2021-12-13 18:28:45 +0100 | [diff] [blame] | 178 | #endif |
| 179 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 180 | static void |
| 181 | test_options(void **state) |
| 182 | { |
| 183 | /* use own context with extra flags */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 184 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 185 | |
| 186 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX)); |
| 187 | |
| 188 | /* invalid arguments */ |
| 189 | assert_int_equal(0, ly_ctx_get_options(NULL)); |
| 190 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL); |
| 191 | |
| 192 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0)); |
| 193 | CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL); |
| 194 | assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0)); |
| 195 | CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL); |
| 196 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 197 | /* unset */ |
| 198 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 199 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 200 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED)); |
| 201 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 202 | |
| 203 | /* LY_CTX_REF_IMPLEMENTED */ |
| 204 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 205 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED)); |
| 206 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 207 | |
| 208 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 209 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 210 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS)); |
| 211 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 212 | |
| 213 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 214 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 215 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| 216 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 217 | |
| 218 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 219 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 220 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS)); |
| 221 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 222 | |
| 223 | assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX)); |
| 224 | |
| 225 | /* set back */ |
| 226 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 227 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED)); |
| 228 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 229 | |
| 230 | /* LY_CTX_REF_IMPLEMENTED */ |
| 231 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED)); |
| 232 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 233 | |
| 234 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 235 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS)); |
| 236 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 237 | |
| 238 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 239 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| 240 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 241 | |
| 242 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 243 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS)); |
| 244 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 245 | |
| 246 | assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX)); |
| 247 | } |
| 248 | |
| 249 | static LY_ERR |
| 250 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 251 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 252 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 253 | { |
| 254 | *module_data = user_data; |
| 255 | *format = LYS_IN_YANG; |
| 256 | *free_module_data = NULL; |
| 257 | return LY_SUCCESS; |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | test_models(void **state) |
| 262 | { |
| 263 | struct ly_in *in; |
| 264 | const char *str; |
| 265 | struct lys_module *mod1, *mod2; |
| 266 | struct lys_glob_unres unres = {0}; |
| 267 | |
| 268 | /* use own context with extra flags */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 269 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 270 | |
| 271 | /* invalid arguments */ |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 272 | assert_int_equal(0, ly_ctx_get_change_count(NULL)); |
| 273 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 274 | |
| 275 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX)); |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 276 | assert_int_equal(UTEST_LYCTX->change_count, ly_ctx_get_change_count(UTEST_LYCTX)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 277 | |
| 278 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 279 | assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in, 4, NULL, NULL, &unres.creating, &mod1)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 280 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 281 | ly_in_free(in, 0); |
| 282 | CHECK_LOG_CTX("Invalid schema input format.", NULL); |
| 283 | |
| 284 | /* import callback */ |
| 285 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test")); |
| 286 | assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb); |
| 287 | assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data); |
| 288 | assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str)); |
| 289 | assert_string_equal("test", str); |
| 290 | |
| 291 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL); |
| 292 | assert_null(UTEST_LYCTX->imp_clb); |
| 293 | assert_null(UTEST_LYCTX->imp_clb_data); |
| 294 | |
| 295 | /* name collision of module and submodule */ |
| 296 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}"); |
| 297 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 298 | assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 299 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 300 | ly_in_free(in, 0); |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 301 | CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL, |
| 302 | "Name collision between module and submodule of name \"y\".", "Line number 1."); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 303 | |
| 304 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 305 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 306 | ly_in_free(in, 0); |
| 307 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 308 | assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 309 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 310 | ly_in_free(in, 0); |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 311 | CHECK_LOG_CTX("Parsing module \"y\" failed.", NULL, |
| 312 | "Name collision between module and submodule of name \"y\".", "Line number 1."); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 313 | |
| 314 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}"); |
| 315 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 316 | assert_int_equal(LY_EVALID, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod1)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 317 | lys_unres_glob_revert(UTEST_LYCTX, &unres); |
| 318 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 319 | ly_in_free(in, 0); |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 320 | CHECK_LOG_CTX("Parsing module \"b\" failed.", NULL, |
| 321 | "Including \"y\" submodule into \"b\" failed.", NULL, |
| 322 | "Parsing submodule failed.", NULL, |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 323 | "Name collision between submodules of name \"y\".", "Line number 1."); |
| 324 | |
| 325 | /* selecting correct revision of the submodules */ |
| 326 | ly_ctx_reset_latests(UTEST_LYCTX); |
| 327 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}"); |
| 328 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 329 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 330 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 331 | ly_in_free(in, 0); |
| 332 | assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date); |
| 333 | |
| 334 | /* reloading module in case only the compiled module resists in the context */ |
| 335 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in)); |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 336 | assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod1)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 337 | ly_in_free(in, 0); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 338 | assert_non_null(mod1->compiled); |
| 339 | assert_non_null(mod1->parsed); |
| 340 | |
| 341 | #if 0 |
| 342 | /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */ |
| 343 | |
| 344 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in)); |
| 345 | /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */ |
| 346 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL); |
| 347 | assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
| 348 | /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/ |
| 349 | CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL); |
| 350 | assert_null(mod2); |
| 351 | ly_in_free(in, 0); |
| 352 | #endif |
| 353 | |
| 354 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module z {namespace urn:z;prefix z;import w {prefix w;revision-date 2018-10-24;}}", &in)); |
| 355 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}"); |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 356 | assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in, LYS_IN_YANG, NULL, &mod2)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 357 | ly_in_free(in, 0); |
| 358 | assert_non_null(mod2); |
| 359 | assert_non_null(mod1->parsed); |
| 360 | assert_string_equal("w", mod1->name); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | test_imports(void **state) |
| 365 | { |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 366 | struct lys_module *mod1, *mod2, *mod3, *import; |
aPiecek | 9f8c7e7 | 2021-07-28 12:01:56 +0200 | [diff] [blame] | 367 | char *str; |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 368 | uint16_t ctx_options; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 369 | |
| 370 | /* use own context with extra flags */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 371 | ly_ctx_destroy(UTEST_LYCTX); |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 372 | ctx_options = LY_CTX_DISABLE_SEARCHDIRS | LY_CTX_NO_YANGLIBRARY; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 373 | |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 374 | /* Import callback provides newer revision of module 'a', |
| 375 | * however the older revision is implemented soon and therefore it is preferred. */ |
| 376 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 377 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
| 378 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", |
| 379 | LYS_IN_YANG, &mod1)); |
aPiecek | 8ca21bd | 2021-07-26 14:31:01 +0200 | [diff] [blame] | 380 | assert_true(LYS_MOD_LATEST_REV & mod1->latest_revision); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 381 | assert_int_equal(1, mod1->implemented); |
| 382 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 383 | LYS_IN_YANG, &mod2)); |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 384 | assert_ptr_equal(mod1, mod2->parsed->imports[0].module); |
| 385 | assert_true((LYS_MOD_LATEST_REV | LYS_MOD_IMPORTED_REV) & mod1->latest_revision); |
| 386 | assert_string_equal("2019-09-16", mod1->revision); |
| 387 | assert_int_equal(1, mod1->implemented); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 388 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16")); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 389 | ly_ctx_destroy(UTEST_LYCTX); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 390 | |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 391 | /* Import callback provides older revision of module 'a' and it is |
| 392 | * imported by another module, so it is preferred even if newer |
| 393 | * revision is implemented later. */ |
| 394 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX)); |
| 395 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-16;}"); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 396 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 397 | LYS_IN_YANG, &mod2)); |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 398 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-17;}", |
| 399 | LYS_IN_YANG, &mod1)); |
| 400 | ly_log_level(LY_LLVRB); |
| 401 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module c {namespace urn:c;prefix c;import a {prefix a;}}", |
| 402 | LYS_IN_YANG, &mod3)); |
| 403 | CHECK_LOG("Implemented module \"a@2019-09-17\" is not used for import, revision \"2019-09-16\" is imported instead.", NULL); |
| 404 | ly_log_level(LY_LLWRN); |
| 405 | assert_true(LYS_MOD_LATEST_SEARCHDIRS & mod1->latest_revision); |
| 406 | assert_int_equal(1, mod1->implemented); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 407 | import = mod2->parsed->imports[0].module; |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 408 | assert_true(LYS_MOD_IMPORTED_REV & import->latest_revision); |
| 409 | assert_string_equal("2019-09-16", import->revision); |
| 410 | assert_int_equal(0, import->implemented); |
| 411 | import = mod3->parsed->imports[0].module; |
| 412 | assert_string_equal("2019-09-16", import->revision); |
| 413 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16")); |
| 414 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17")); |
| 415 | assert_string_equal("2019-09-17", ly_ctx_get_module_implemented(UTEST_LYCTX, "a")->revision); |
aPiecek | 9f8c7e7 | 2021-07-28 12:01:56 +0200 | [diff] [blame] | 416 | ly_ctx_destroy(UTEST_LYCTX); |
| 417 | |
| 418 | /* check of circular dependency */ |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 419 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, ctx_options, &UTEST_LYCTX)); |
aPiecek | 9f8c7e7 | 2021-07-28 12:01:56 +0200 | [diff] [blame] | 420 | str = "module a {namespace urn:a; prefix a;" |
| 421 | "import b {prefix b;}" |
| 422 | "}"; |
| 423 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, str); |
| 424 | str = "module b { yang-version 1.1; namespace urn:b; prefix b;" |
| 425 | "import a {prefix a;}" |
| 426 | "}"; |
| 427 | assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, str, LYS_IN_YANG, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void |
| 431 | test_get_models(void **state) |
| 432 | { |
| 433 | struct lys_module *mod, *mod2; |
| 434 | const char *str0 = "module a {namespace urn:a;prefix a;}"; |
| 435 | const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}"; |
| 436 | const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}"; |
| 437 | struct ly_in *in0, *in1, *in2; |
| 438 | struct lys_glob_unres unres = {0}; |
| 439 | |
| 440 | unsigned int index = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 441 | const char *names[] = { |
| 442 | "ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-yang-schema-mount", |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 443 | "ietf-yang-structure-ext", "ietf-datastores", "ietf-yang-library", "a", "a", "a" |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 444 | }; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 445 | |
| 446 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0)); |
| 447 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| 448 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2)); |
| 449 | |
| 450 | /* invalid arguments */ |
| 451 | assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL)); |
| 452 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL); |
| 453 | assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL)); |
| 454 | CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL); |
| 455 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL)); |
| 456 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL); |
| 457 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL)); |
| 458 | CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL); |
| 459 | assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL)); |
| 460 | |
| 461 | /* internal modules */ |
| 462 | assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types")); |
| 463 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang"); |
| 464 | assert_non_null(mod); |
| 465 | assert_non_null(mod->parsed); |
| 466 | assert_string_equal("yang", mod->name); |
| 467 | mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns); |
| 468 | assert_ptr_equal(mod, mod2); |
| 469 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05")); |
| 470 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15")); |
| 471 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15")); |
| 472 | assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14")); |
| 473 | |
| 474 | /* select module by revision */ |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 475 | assert_int_equal(LY_SUCCESS, lys_parse(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, &mod)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 476 | /* invalid attempts - implementing module of the same name and inserting the same module */ |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 477 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in2, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2)); |
Michal Vasko | 4e205e8 | 2021-06-08 14:01:47 +0200 | [diff] [blame] | 478 | assert_int_equal(LY_EDENIED, lys_implement(mod2, NULL, &unres)); |
Michal Vasko | 978532b | 2022-01-21 14:13:40 +0100 | [diff] [blame] | 479 | CHECK_LOG_CTX("Module \"a@2018-10-24\" is already implemented in revision \"2018-10-23\".", NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 480 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 481 | ly_in_reset(in1); |
| 482 | /* it is already there, fine */ |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 483 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, NULL, &unres.creating, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 484 | /* insert the second module only as imported, not implemented */ |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 485 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 486 | ly_in_reset(in2); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 487 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in2, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod2)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 488 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 489 | assert_non_null(mod2); |
| 490 | assert_ptr_not_equal(mod, mod2); |
| 491 | mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a"); |
| 492 | assert_ptr_equal(mod, mod2); |
| 493 | mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns); |
| 494 | assert_ptr_equal(mod, mod2); |
| 495 | /* work with module with no revision */ |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 496 | assert_int_equal(LY_SUCCESS, lys_parse_in(UTEST_LYCTX, in0, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod)); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 497 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 498 | assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 499 | assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a")); |
| 500 | |
| 501 | str1 = "submodule b {belongs-to a {prefix a;}}"; |
| 502 | ly_in_free(in1, 0); |
| 503 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 504 | assert_int_equal(LY_EINVAL, lys_parse_in(UTEST_LYCTX, in1, LYS_IN_YANG, NULL, NULL, &unres.creating, &mod)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 505 | CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL); |
Michal Vasko | f4258e1 | 2021-06-15 12:11:42 +0200 | [diff] [blame] | 506 | lys_unres_glob_erase(&unres); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 507 | |
| 508 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) { |
| 509 | assert_string_equal(names[index - 1], mod->name); |
| 510 | } |
Michal Vasko | edb0fa5 | 2022-10-04 10:36:00 +0200 | [diff] [blame] | 511 | assert_int_equal(11, index); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 512 | |
| 513 | /* cleanup */ |
| 514 | ly_in_free(in0, 0); |
| 515 | ly_in_free(in1, 0); |
| 516 | ly_in_free(in2, 0); |
| 517 | } |
| 518 | |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 519 | static void |
| 520 | test_ylmem(void **state) |
| 521 | { |
| 522 | #define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\ |
| 523 | " <module-set>\n"\ |
| 524 | " <name>complete</name>\n"\ |
| 525 | " <module>\n"\ |
| 526 | " <name>yang</name>\n"\ |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 527 | " <revision>2022-06-16</revision>\n"\ |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 528 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\ |
| 529 | " </module>\n"\ |
| 530 | " <module>\n"\ |
| 531 | " <name>ietf-yang-library</name>\n"\ |
| 532 | " <revision>2019-01-04</revision>\n"\ |
| 533 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\ |
| 534 | " </module>\n" |
| 535 | |
| 536 | #define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\ |
| 537 | " <name>ietf-yang-metadata</name>\n"\ |
| 538 | " <revision>2016-08-05</revision>\n"\ |
| 539 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\ |
| 540 | " </import-only-module>\n"\ |
| 541 | " <import-only-module>\n"\ |
| 542 | " <name>ietf-inet-types</name>\n"\ |
| 543 | " <revision>2013-07-15</revision>\n"\ |
| 544 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\ |
| 545 | " </import-only-module>\n"\ |
| 546 | " <import-only-module>\n"\ |
| 547 | " <name>ietf-yang-types</name>\n"\ |
| 548 | " <revision>2013-07-15</revision>\n"\ |
| 549 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\ |
| 550 | " </import-only-module>\n"\ |
| 551 | " <import-only-module>\n"\ |
| 552 | " <name>ietf-datastores</name>\n"\ |
| 553 | " <revision>2018-02-14</revision>\n"\ |
| 554 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\ |
| 555 | " </import-only-module>\n" |
| 556 | |
| 557 | #define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\ |
| 558 | " <schema>\n"\ |
| 559 | " <name>complete</name>\n"\ |
| 560 | " <module-set>complete</module-set>\n"\ |
| 561 | " </schema>\n"\ |
| 562 | " <content-id>9</content-id>\n"\ |
| 563 | "</yang-library>\n"\ |
| 564 | "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\ |
| 565 | " <module-set-id>12</module-set-id>\n"\ |
| 566 | " <module>\n"\ |
| 567 | " <name>ietf-yang-metadata</name>\n"\ |
| 568 | " <revision>2016-08-05</revision>\n"\ |
| 569 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\ |
| 570 | " <conformance-type>import</conformance-type>\n"\ |
| 571 | " </module>\n"\ |
| 572 | " <module>\n"\ |
| 573 | " <name>yang</name>\n"\ |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 574 | " <revision>2022-06-16</revision>\n"\ |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 575 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\ |
| 576 | " <conformance-type>implement</conformance-type>\n"\ |
| 577 | " </module>\n"\ |
| 578 | " <module>\n"\ |
| 579 | " <name>ietf-inet-types</name>\n"\ |
| 580 | " <revision>2013-07-15</revision>\n"\ |
| 581 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\ |
| 582 | " <conformance-type>import</conformance-type>\n"\ |
| 583 | " </module>\n"\ |
| 584 | " <module>\n"\ |
| 585 | " <name>ietf-yang-types</name>\n"\ |
| 586 | " <revision>2013-07-15</revision>\n"\ |
| 587 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\ |
| 588 | " <conformance-type>import</conformance-type>\n"\ |
| 589 | " </module>\n"\ |
| 590 | " <module>\n"\ |
| 591 | " <name>ietf-yang-library</name>\n"\ |
| 592 | " <revision>2019-01-04</revision>\n"\ |
| 593 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\ |
| 594 | " <conformance-type>implement</conformance-type>\n"\ |
| 595 | " </module>\n"\ |
| 596 | " <module>\n"\ |
| 597 | " <name>ietf-datastores</name>\n"\ |
| 598 | " <revision>2018-02-14</revision>\n"\ |
| 599 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\ |
| 600 | " <conformance-type>import</conformance-type>\n"\ |
| 601 | " </module>\n" |
| 602 | |
| 603 | const char *yanglibrary_only = |
| 604 | DATA_YANG_LIBRARY_START |
| 605 | DATA_YANG_BASE_IMPORTS |
| 606 | DATA_YANG_SCHEMA_MODULE_STATE |
| 607 | "</modules-state>\n"; |
| 608 | |
| 609 | const char *with_netconf = |
| 610 | DATA_YANG_LIBRARY_START |
| 611 | " <module>\n" |
| 612 | " <name>ietf-netconf</name>\n" |
| 613 | " <revision>2011-06-01</revision>\n" |
| 614 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 615 | " </module>\n" |
| 616 | DATA_YANG_BASE_IMPORTS |
| 617 | " <import-only-module>\n" |
| 618 | " <name>ietf-netconf-acm</name>\n" |
| 619 | " <revision>2018-02-14</revision>\n" |
| 620 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 621 | " </import-only-module>\n" |
| 622 | DATA_YANG_SCHEMA_MODULE_STATE |
| 623 | " <module>\n" |
| 624 | " <name>ietf-netconf</name>\n" |
| 625 | " <revision>2011-06-01</revision>\n" |
| 626 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 627 | " <conformance-type>implement</conformance-type>\n" |
| 628 | " </module>\n" |
| 629 | " <module>\n" |
| 630 | " <name>ietf-netconf-acm</name>\n" |
| 631 | " <revision>2018-02-14</revision>\n" |
| 632 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 633 | " <conformance-type>import</conformance-type>\n" |
| 634 | " </module>\n" |
| 635 | "</modules-state>"; |
| 636 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 637 | char *with_netconf_features = malloc(8096); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 638 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 639 | strcpy(with_netconf_features, |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 640 | DATA_YANG_LIBRARY_START |
| 641 | " <module>\n" |
| 642 | " <name>ietf-netconf</name>\n" |
| 643 | " <revision>2011-06-01</revision>\n" |
| 644 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 645 | " <feature>writable-running</feature>\n" |
| 646 | " <feature>candidate</feature>\n" |
| 647 | " <feature>confirmed-commit</feature>\n" |
| 648 | " <feature>rollback-on-error</feature>\n" |
| 649 | " <feature>validate</feature>\n" |
| 650 | " <feature>startup</feature>\n" |
| 651 | " <feature>url</feature>\n" |
| 652 | " <feature>xpath</feature>\n" |
| 653 | " </module>\n" |
| 654 | " <import-only-module>\n" |
| 655 | " <name>ietf-yang-metadata</name>\n" |
| 656 | " <revision>2016-08-05</revision>\n" |
| 657 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n" |
| 658 | " </import-only-module>\n" |
| 659 | " <import-only-module>\n" |
| 660 | " <name>ietf-inet-types</name>\n" |
| 661 | " <revision>2013-07-15</revision>\n" |
| 662 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n" |
| 663 | " </import-only-module>\n" |
| 664 | " <import-only-module>\n" |
| 665 | " <name>ietf-yang-types</name>\n" |
| 666 | " <revision>2013-07-15</revision>\n" |
| 667 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n" |
| 668 | " </import-only-module>\n" |
| 669 | " <import-only-module>\n" |
| 670 | " <name>ietf-datastores</name>\n" |
| 671 | " <revision>2018-02-14</revision>\n" |
| 672 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n" |
| 673 | " </import-only-module>\n" |
| 674 | " <import-only-module>\n" |
| 675 | " <name>ietf-netconf-acm</name>\n" |
| 676 | " <revision>2018-02-14</revision>\n" |
| 677 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 678 | " </import-only-module>\n"); |
| 679 | strcpy(with_netconf_features + strlen(with_netconf_features), |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 680 | DATA_YANG_SCHEMA_MODULE_STATE |
| 681 | " <module>\n" |
| 682 | " <name>ietf-netconf</name>\n" |
| 683 | " <revision>2011-06-01</revision>\n" |
| 684 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 685 | " <feature>writable-running</feature>\n" |
| 686 | " <feature>candidate</feature>\n" |
| 687 | " <feature>confirmed-commit</feature>\n" |
| 688 | " <feature>rollback-on-error</feature>\n" |
| 689 | " <feature>validate</feature>\n" |
| 690 | " <feature>startup</feature>\n" |
| 691 | " <feature>url</feature>\n" |
| 692 | " <feature>xpath</feature>\n" |
| 693 | " <conformance-type>implement</conformance-type>\n" |
| 694 | " </module>\n" |
| 695 | " <module>\n" |
| 696 | " <name>ietf-netconf-acm</name>\n" |
| 697 | " <revision>2018-02-14</revision>\n" |
| 698 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 699 | " <conformance-type>import</conformance-type>\n" |
| 700 | " </module>\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 701 | "</modules-state>"); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 702 | |
| 703 | const char *garbage_revision = |
| 704 | "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 705 | " <module-set>\n" |
| 706 | " <name>complete</name>\n" |
| 707 | " <module>\n" |
| 708 | " <name>yang</name>\n" |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 709 | " <revision>2022-06-16</revision>\n" |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 710 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 711 | " </module>\n" |
| 712 | " <module>\n" |
| 713 | " <name>ietf-yang-library</name>\n" |
| 714 | " <revision>2019-01-01</revision>\n" |
| 715 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n" |
| 716 | " </module>\n" |
| 717 | DATA_YANG_BASE_IMPORTS |
| 718 | DATA_YANG_SCHEMA_MODULE_STATE |
| 719 | "</modules-state>\n"; |
| 720 | |
| 721 | const char *no_yanglibrary = |
| 722 | "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 723 | " <module-set>\n" |
| 724 | " <name>complete</name>\n" |
| 725 | " <module>\n" |
| 726 | " <name>yang</name>\n" |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 727 | " <revision>2022-06-16</revision>\n" |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 728 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 729 | " </module>\n" |
| 730 | DATA_YANG_BASE_IMPORTS |
| 731 | DATA_YANG_SCHEMA_MODULE_STATE |
| 732 | "</modules-state>\n"; |
| 733 | |
| 734 | (void) state; |
| 735 | /* seperate context to avoid double free during teadown */ |
| 736 | struct ly_ctx *ctx_test = NULL; |
| 737 | |
| 738 | /* test invalid parameters */ |
| 739 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test)); |
| 740 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL)); |
| 741 | assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test)); |
| 742 | |
| 743 | /* basic test with ietf-yang-library-only */ |
| 744 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", yanglibrary_only, LYD_XML, 0, &ctx_test)); |
Michal Vasko | ad8d0f5 | 2021-04-26 13:21:24 +0200 | [diff] [blame] | 745 | assert_non_null(ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04")); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 746 | assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 747 | ly_ctx_destroy(ctx_test); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 748 | ctx_test = NULL; |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 749 | |
| 750 | /* test loading module, should also import other module */ |
| 751 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf, LYD_XML, 0, &ctx_test)); |
Michal Vasko | ad8d0f5 | 2021-04-26 13:21:24 +0200 | [diff] [blame] | 752 | assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 753 | assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented); |
Michal Vasko | ad8d0f5 | 2021-04-26 13:21:24 +0200 | [diff] [blame] | 754 | assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 755 | assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented); |
| 756 | assert_int_equal(LY_ENOT, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url")); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 757 | ly_ctx_destroy(ctx_test); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 758 | ctx_test = NULL; |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 759 | |
| 760 | /* test loading module with feature if they are present */ |
| 761 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, 0, &ctx_test)); |
Michal Vasko | ad8d0f5 | 2021-04-26 13:21:24 +0200 | [diff] [blame] | 762 | assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
| 763 | assert_non_null(ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 764 | assert_int_equal(LY_SUCCESS, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url")); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 765 | ly_ctx_destroy(ctx_test); |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 766 | ctx_test = NULL; |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 767 | |
| 768 | /* test with not matching revision */ |
| 769 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test)); |
| 770 | |
| 771 | /* test data containing ietf-yang-library which conflicts with the option */ |
| 772 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test)); |
| 773 | |
| 774 | /* test creating without ietf-yang-library */ |
| 775 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test)); |
| 776 | assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04")); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 777 | ly_ctx_destroy(ctx_test); |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 778 | free(with_netconf_features); |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 779 | } |
| 780 | |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 781 | static LY_ERR |
| 782 | check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue)) |
| 783 | { |
| 784 | const struct lysp_node *pnode; |
| 785 | const char ***iter; |
| 786 | |
| 787 | pnode = (const struct lysp_node *)node->priv; |
| 788 | CHECK_POINTER(pnode, 1); |
| 789 | iter = (const char ***)data; |
| 790 | CHECK_POINTER(**iter, 1); |
| 791 | CHECK_STRING(pnode->name, **iter); |
| 792 | (*iter)++; |
| 793 | |
| 794 | return LY_SUCCESS; |
| 795 | } |
| 796 | |
| 797 | static LY_ERR |
| 798 | check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue)) |
| 799 | { |
| 800 | CHECK_POINTER(node->priv, 0); |
| 801 | return LY_SUCCESS; |
| 802 | } |
| 803 | |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 804 | static void |
| 805 | check_ext_instance_priv_parsed_is_set(struct lysc_ext_instance *ext) |
| 806 | { |
| 807 | LY_ARRAY_COUNT_TYPE u, v; |
| 808 | struct lysc_ext_substmt *substmts; |
| 809 | struct lysc_node *cnode; |
| 810 | const char **iter; |
| 811 | const char *check[] = { |
| 812 | "tmp_cont", "lf", NULL |
| 813 | }; |
| 814 | |
| 815 | LY_ARRAY_FOR(ext, u) { |
| 816 | substmts = ext[u].substmts; |
| 817 | LY_ARRAY_FOR(substmts, v) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 818 | if (substmts && substmts[v].storage && (substmts[v].stmt & LY_STMT_DATA_NODE_MASK)) { |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 819 | cnode = *(struct lysc_node **)substmts[v].storage; |
| 820 | iter = check; |
| 821 | assert_int_equal(LY_SUCCESS, lysc_tree_dfs_full(cnode, check_node_priv_parsed_is_set, &iter)); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static void |
| 828 | check_ext_instance_priv_parsed_not_set(struct lysc_ext_instance *ext) |
| 829 | { |
| 830 | LY_ARRAY_COUNT_TYPE u, v; |
| 831 | struct lysc_ext_substmt *substmts; |
| 832 | struct lysc_node *cnode; |
| 833 | |
| 834 | LY_ARRAY_FOR(ext, u) { |
| 835 | substmts = ext[u].substmts; |
| 836 | LY_ARRAY_FOR(substmts, v) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 837 | if (substmts && substmts[v].storage && (substmts[v].stmt & LY_STMT_DATA_NODE_MASK)) { |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 838 | cnode = *(struct lysc_node **)substmts[v].storage; |
| 839 | if (cnode) { |
| 840 | CHECK_POINTER((struct lysp_node *)cnode->priv, 0); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 847 | /** |
| 848 | * @brief Testing of LY_CTX_SET_PRIV_PARSED. |
| 849 | */ |
| 850 | static void |
| 851 | test_set_priv_parsed(void **state) |
| 852 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 853 | struct lys_module *mod; |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 854 | const char *schema_a; |
| 855 | const char **iter; |
| 856 | const char *check[] = { |
Michal Vasko | 95f736c | 2022-06-08 12:03:31 +0200 | [diff] [blame] | 857 | "cont", "contnotif", "contx", "grpleaf", "augleaf", "l1", |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 858 | "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2", |
| 859 | "l2c", "l2cx", "ch", "cas", "casx", "oper", |
| 860 | "input", "inparam", "output", "outparam", "n1", NULL |
| 861 | }; |
| 862 | |
| 863 | /* each node must have a unique name. */ |
| 864 | schema_a = "module a {\n" |
| 865 | " namespace urn:tests:a;\n" |
| 866 | " prefix a;yang-version 1.1;\n" |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 867 | "\n" |
| 868 | " import ietf-restconf {\n" |
| 869 | " prefix rc;\n" |
| 870 | " revision-date 2017-01-26;\n" |
| 871 | " }\n" |
| 872 | "\n" |
| 873 | " rc:yang-data \"tmp\" {\n" |
| 874 | " container tmp_cont {\n" |
| 875 | " leaf lf {\n" |
| 876 | " type string;\n" |
| 877 | " }\n" |
| 878 | " }\n" |
| 879 | " }\n" |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 880 | " container cont {\n" |
| 881 | " notification contnotif;\n" |
| 882 | " leaf-list contx {\n" |
| 883 | " type string;\n" |
| 884 | " }\n" |
| 885 | " uses grp;\n" |
| 886 | " }\n" |
| 887 | " list l1 {\n" |
| 888 | " key \"l1a l1b\";\n" |
| 889 | " leaf l1a {\n" |
| 890 | " type string;\n" |
| 891 | " }\n" |
| 892 | " leaf l1b {\n" |
| 893 | " type string;\n" |
| 894 | " }\n" |
| 895 | " leaf l1c {\n" |
| 896 | " type string;\n" |
| 897 | " }\n" |
| 898 | " }\n" |
| 899 | " feature f1;\n" |
| 900 | " feature f2;\n" |
| 901 | " leaf foo1 {\n" |
| 902 | " type uint16;\n" |
| 903 | " if-feature f1;\n" |
| 904 | " }\n" |
| 905 | " leaf foo2 {\n" |
| 906 | " type uint16;\n" |
| 907 | " }\n" |
| 908 | " leaf foo3 {\n" |
| 909 | " type uint16;\n" |
| 910 | " if-feature f2;\n" |
| 911 | " }\n" |
| 912 | " leaf-list ll {\n" |
| 913 | " type string;\n" |
| 914 | " }\n" |
| 915 | " anydata any {\n" |
| 916 | " config false;\n" |
| 917 | " }\n" |
| 918 | " list l2 {\n" |
| 919 | " config false;\n" |
| 920 | " container l2c {\n" |
| 921 | " leaf l2cx {\n" |
| 922 | " type string;\n" |
| 923 | " }\n" |
| 924 | " }\n" |
| 925 | " }\n" |
| 926 | " choice ch {\n" |
| 927 | " case cas {\n" |
| 928 | " leaf casx {\n" |
| 929 | " type string;\n" |
| 930 | " }\n" |
| 931 | " }\n" |
| 932 | " }\n" |
| 933 | " rpc oper {\n" |
| 934 | " input {\n" |
| 935 | " leaf inparam {\n" |
| 936 | " type string;\n" |
| 937 | " }\n" |
| 938 | " }\n" |
| 939 | " output {\n" |
| 940 | " leaf outparam {\n" |
| 941 | " type int8;\n" |
| 942 | " }\n" |
| 943 | " }\n" |
| 944 | " }\n" |
| 945 | " notification n1;\n" |
| 946 | " grouping grp {\n" |
| 947 | " leaf grpleaf {\n" |
| 948 | " type uint16;\n" |
| 949 | " }\n" |
| 950 | " }\n" |
| 951 | " augment /cont {\n" |
| 952 | " leaf augleaf {\n" |
| 953 | " type uint16;\n" |
| 954 | " }\n" |
| 955 | " }\n" |
| 956 | " deviation /a:foo2 {\n" |
| 957 | " deviate not-supported;\n" |
| 958 | " }\n" |
| 959 | "}\n"; |
| 960 | |
| 961 | /* use own context with extra flags */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 962 | ly_ctx_destroy(UTEST_LYCTX); |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 963 | const char *feats[] = {"f1", NULL}; |
Radek Krejci | ddbc481 | 2021-04-13 21:18:02 +0200 | [diff] [blame] | 964 | |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 965 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX)); |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 966 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
| 967 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL)); |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 968 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL); |
| 969 | |
| 970 | print_message("[ ] create context\n"); |
| 971 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 972 | iter = check; |
| 973 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter)); |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 974 | check_ext_instance_priv_parsed_is_set(mod->compiled->exts); |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 975 | |
| 976 | print_message("[ ] unset option\n"); |
| 977 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED)); |
| 978 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 979 | iter = check; |
| 980 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter)); |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 981 | check_ext_instance_priv_parsed_not_set(mod->compiled->exts); |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 982 | |
| 983 | print_message("[ ] set option\n"); |
| 984 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED)); |
| 985 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 986 | iter = check; |
| 987 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter)); |
aPiecek | fcfec0f | 2021-04-23 12:47:30 +0200 | [diff] [blame] | 988 | check_ext_instance_priv_parsed_is_set(mod->compiled->exts); |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 991 | static void |
| 992 | test_explicit_compile(void **state) |
| 993 | { |
| 994 | uint32_t i; |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 995 | struct lys_module *mod; |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 996 | const char *schema_a = "module a {\n" |
| 997 | " namespace urn:tests:a;\n" |
| 998 | " prefix a;yang-version 1.1;\n" |
| 999 | " feature f1;\n" |
| 1000 | " feature f2;\n" |
| 1001 | " leaf foo1 {\n" |
| 1002 | " type uint16;\n" |
| 1003 | " if-feature f1;\n" |
| 1004 | " }\n" |
| 1005 | " leaf foo2 {\n" |
| 1006 | " type uint16;\n" |
| 1007 | " }\n" |
| 1008 | " container cont {\n" |
| 1009 | " leaf foo3 {\n" |
| 1010 | " type string;\n" |
| 1011 | " }\n" |
| 1012 | " }\n" |
| 1013 | "}\n"; |
| 1014 | const char *schema_b = "module b {\n" |
| 1015 | " namespace urn:tests:b;\n" |
| 1016 | " prefix b;yang-version 1.1;\n" |
| 1017 | " import a {\n" |
| 1018 | " prefix a;\n" |
| 1019 | " }\n" |
| 1020 | " augment /a:cont {\n" |
| 1021 | " leaf augleaf {\n" |
| 1022 | " type uint16;\n" |
| 1023 | " }\n" |
| 1024 | " }\n" |
| 1025 | "}\n"; |
| 1026 | const char *schema_c = "module c {\n" |
| 1027 | " namespace urn:tests:c;\n" |
| 1028 | " prefix c;yang-version 1.1;\n" |
| 1029 | " import a {\n" |
| 1030 | " prefix a;\n" |
| 1031 | " }\n" |
| 1032 | " deviation /a:foo2 {\n" |
| 1033 | " deviate not-supported;\n" |
| 1034 | " }\n" |
| 1035 | "}\n"; |
| 1036 | |
| 1037 | /* use own context with extra flags */ |
| 1038 | ly_ctx_destroy(UTEST_LYCTX); |
| 1039 | const char *feats[] = {"f1", NULL}; |
| 1040 | |
| 1041 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_EXPLICIT_COMPILE, &UTEST_LYCTX)); |
| 1042 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod); |
| 1043 | UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL); |
| 1044 | UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL); |
| 1045 | assert_int_equal(LY_SUCCESS, lys_set_implemented((struct lys_module *)mod, feats)); |
| 1046 | |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1047 | /* none of the modules should be compiled */ |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 1048 | i = 0; |
| 1049 | while ((mod = ly_ctx_get_module_iter(UTEST_LYCTX, &i))) { |
Michal Vasko | dd99258 | 2021-06-10 14:34:57 +0200 | [diff] [blame] | 1050 | assert_null(mod->compiled); |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | assert_int_equal(LY_SUCCESS, ly_ctx_compile(UTEST_LYCTX)); |
| 1054 | |
| 1055 | /* check internal modules */ |
| 1056 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang"); |
| 1057 | assert_non_null(mod); |
| 1058 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-datastores"); |
| 1059 | assert_non_null(mod); |
| 1060 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-library"); |
| 1061 | assert_non_null(mod); |
| 1062 | |
| 1063 | /* check test modules */ |
| 1064 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "a"); |
| 1065 | assert_non_null(mod); |
| 1066 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "b"); |
| 1067 | assert_non_null(mod); |
| 1068 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c"); |
| 1069 | assert_non_null(mod); |
| 1070 | } |
| 1071 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1072 | int |
| 1073 | main(void) |
| 1074 | { |
| 1075 | const struct CMUnitTest tests[] = { |
| 1076 | UTEST(test_searchdirs), |
| 1077 | UTEST(test_options), |
| 1078 | UTEST(test_models), |
| 1079 | UTEST(test_imports), |
| 1080 | UTEST(test_get_models), |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 1081 | UTEST(test_ylmem), |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame] | 1082 | UTEST(test_set_priv_parsed), |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 1083 | UTEST(test_explicit_compile), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1084 | }; |
| 1085 | |
| 1086 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1087 | } |