Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +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 | #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" |
| 23 | #include "utests.h" |
| 24 | |
| 25 | static void |
| 26 | test_searchdirs(void **state) |
| 27 | { |
| 28 | const char * const *list; |
| 29 | |
| 30 | /* invalid arguments */ |
| 31 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 32 | CHECK_LOG("Invalid argument ctx (ly_ctx_set_searchdir()).", NULL); |
| 33 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 34 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_searchdirs()).", NULL); |
| 35 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL)); |
| 36 | CHECK_LOG("Invalid argument ctx (ly_ctx_unset_searchdir()).", NULL); |
| 37 | |
| 38 | /* readable and executable, but not a directory */ |
| 39 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context")); |
| 40 | CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL); |
| 41 | /* not executable */ |
| 42 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, __FILE__)); |
| 43 | CHECK_LOG_CTX("Unable to fully access search directory \""__FILE__ "\" (Permission denied).", NULL); |
| 44 | /* not existing */ |
| 45 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile")); |
| 46 | CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL); |
| 47 | |
| 48 | /* ly_set_add() fails */ |
| 49 | /* no change */ |
| 50 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, NULL)); |
| 51 | |
| 52 | /* correct path */ |
| 53 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 54 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 55 | assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]); |
| 56 | |
| 57 | /* duplicated paths */ |
| 58 | assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 59 | assert_int_equal(1, UTEST_LYCTX->search_paths.count); |
| 60 | assert_string_equal(TESTS_BIN "/utests", UTEST_LYCTX->search_paths.objs[0]); |
| 61 | |
| 62 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 63 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/CMakeFiles")); |
| 64 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../src")); |
| 65 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../CMakeModules")); |
| 66 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC "/../doc")); |
| 67 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_SRC)); |
| 68 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN)); |
| 69 | assert_int_equal(7, UTEST_LYCTX->search_paths.count); |
| 70 | |
| 71 | /* get searchpaths */ |
| 72 | list = ly_ctx_get_searchdirs(UTEST_LYCTX); |
| 73 | assert_non_null(list); |
| 74 | assert_string_equal(TESTS_BIN "/utests", list[0]); |
| 75 | assert_string_equal(TESTS_BIN "/CMakeFiles", list[1]); |
| 76 | assert_string_equal(TESTS_SRC, list[5]); |
| 77 | assert_string_equal(TESTS_BIN, list[6]); |
| 78 | assert_null(list[7]); |
| 79 | |
| 80 | /* removing searchpaths */ |
| 81 | /* nonexisting */ |
| 82 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(UTEST_LYCTX, "/nonexistingfile")); |
| 83 | CHECK_LOG_CTX("Invalid argument value (ly_ctx_unset_searchdir()).", NULL); |
| 84 | /* first */ |
| 85 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN "/utests")); |
| 86 | assert_string_not_equal(TESTS_BIN "/utests", list[0]); |
| 87 | assert_int_equal(6, UTEST_LYCTX->search_paths.count); |
| 88 | /* middle */ |
| 89 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_SRC)); |
| 90 | assert_int_equal(5, UTEST_LYCTX->search_paths.count); |
| 91 | /* last */ |
| 92 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, TESTS_BIN)); |
| 93 | assert_int_equal(4, UTEST_LYCTX->search_paths.count); |
| 94 | /* all */ |
| 95 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL)); |
| 96 | assert_int_equal(0, UTEST_LYCTX->search_paths.count); |
| 97 | |
| 98 | /* again - no change */ |
| 99 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(UTEST_LYCTX, NULL)); |
| 100 | |
| 101 | /* cleanup */ |
| 102 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 103 | |
| 104 | /* test searchdir list in ly_ctx_new() */ |
| 105 | assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &UTEST_LYCTX)); |
| 106 | CHECK_LOG("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL); |
| 107 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC ":"TESTS_BIN ":"TESTS_BIN ":"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX)); |
| 108 | assert_int_equal(2, UTEST_LYCTX->search_paths.count); |
| 109 | assert_string_equal(TESTS_SRC, UTEST_LYCTX->search_paths.objs[0]); |
| 110 | assert_string_equal(TESTS_BIN, UTEST_LYCTX->search_paths.objs[1]); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | test_options(void **state) |
| 115 | { |
| 116 | /* use own context with extra flags */ |
| 117 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 118 | |
| 119 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &UTEST_LYCTX)); |
| 120 | |
| 121 | /* invalid arguments */ |
| 122 | assert_int_equal(0, ly_ctx_get_options(NULL)); |
| 123 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_options()).", NULL); |
| 124 | |
| 125 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0)); |
| 126 | CHECK_LOG("Invalid argument ctx (ly_ctx_set_options()).", NULL); |
| 127 | assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0)); |
| 128 | CHECK_LOG("Invalid argument ctx (ly_ctx_unset_options()).", NULL); |
| 129 | |
| 130 | /* option not allowed to be changed */ |
| 131 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY)); |
| 132 | CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL); |
| 133 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_NO_YANGLIBRARY)); |
| 134 | CHECK_LOG_CTX("Invalid argument option (ly_ctx_set_options()).", NULL); |
| 135 | |
| 136 | /* unset */ |
| 137 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 138 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 139 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED)); |
| 140 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 141 | |
| 142 | /* LY_CTX_REF_IMPLEMENTED */ |
| 143 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 144 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED)); |
| 145 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 146 | |
| 147 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 148 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 149 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS)); |
| 150 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 151 | |
| 152 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 153 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 154 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| 155 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 156 | |
| 157 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 158 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 159 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS)); |
| 160 | assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 161 | |
| 162 | assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX)); |
| 163 | |
| 164 | /* set back */ |
| 165 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 166 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_ALL_IMPLEMENTED)); |
| 167 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_ALL_IMPLEMENTED); |
| 168 | |
| 169 | /* LY_CTX_REF_IMPLEMENTED */ |
| 170 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_REF_IMPLEMENTED)); |
| 171 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_REF_IMPLEMENTED); |
| 172 | |
| 173 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 174 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIRS)); |
| 175 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 176 | |
| 177 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 178 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
| 179 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 180 | |
| 181 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 182 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS)); |
| 183 | assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 184 | |
| 185 | assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX)); |
| 186 | } |
| 187 | |
| 188 | static LY_ERR |
| 189 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 190 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 191 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 192 | { |
| 193 | *module_data = user_data; |
| 194 | *format = LYS_IN_YANG; |
| 195 | *free_module_data = NULL; |
| 196 | return LY_SUCCESS; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | test_models(void **state) |
| 201 | { |
| 202 | struct ly_in *in; |
| 203 | const char *str; |
| 204 | struct lys_module *mod1, *mod2; |
| 205 | struct lys_glob_unres unres = {0}; |
| 206 | |
| 207 | /* use own context with extra flags */ |
| 208 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 209 | |
| 210 | /* invalid arguments */ |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 211 | assert_int_equal(0, ly_ctx_get_change_count(NULL)); |
| 212 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_change_count()).", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 213 | |
| 214 | 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] | 215 | 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] | 216 | |
| 217 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in)); |
| 218 | assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in, 4, 1, NULL, NULL, NULL, &unres, &mod1)); |
| 219 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 220 | ly_in_free(in, 0); |
| 221 | CHECK_LOG_CTX("Invalid schema input format.", NULL); |
| 222 | |
| 223 | /* import callback */ |
| 224 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, (void *)(str = "test")); |
| 225 | assert_ptr_equal(test_imp_clb, UTEST_LYCTX->imp_clb); |
| 226 | assert_ptr_equal(str, UTEST_LYCTX->imp_clb_data); |
| 227 | assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(UTEST_LYCTX, (void **)&str)); |
| 228 | assert_string_equal("test", str); |
| 229 | |
| 230 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL); |
| 231 | assert_null(UTEST_LYCTX->imp_clb); |
| 232 | assert_null(UTEST_LYCTX->imp_clb_data); |
| 233 | |
| 234 | /* name collision of module and submodule */ |
| 235 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}"); |
| 236 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in)); |
| 237 | assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1)); |
| 238 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 239 | ly_in_free(in, 0); |
| 240 | CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1."); |
| 241 | |
| 242 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y;revision 2018-10-30; }", &in)); |
| 243 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1)); |
| 244 | assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres)); |
| 245 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 246 | ly_in_free(in, 0); |
| 247 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in)); |
| 248 | assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1)); |
| 249 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 250 | ly_in_free(in, 0); |
| 251 | CHECK_LOG_CTX("Name collision between module and submodule of name \"y\".", "Line number 1."); |
| 252 | |
| 253 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to b {prefix b;}}"); |
| 254 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in)); |
| 255 | assert_int_equal(LY_EVALID, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod1)); |
| 256 | lys_compile_unres_glob_revert(UTEST_LYCTX, &unres); |
| 257 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 258 | ly_in_free(in, 0); |
| 259 | CHECK_LOG_CTX("Including \"y\" submodule into \"b\" failed.", NULL, |
| 260 | "Name collision between submodules of name \"y\".", "Line number 1."); |
| 261 | |
| 262 | /* selecting correct revision of the submodules */ |
| 263 | ly_ctx_reset_latests(UTEST_LYCTX); |
| 264 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}"); |
| 265 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module a {namespace urn:a;prefix a;include y; revision 2018-10-31;}", &in)); |
| 266 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2)); |
| 267 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 268 | ly_in_free(in, 0); |
| 269 | assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date); |
| 270 | |
| 271 | /* reloading module in case only the compiled module resists in the context */ |
| 272 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in)); |
| 273 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod1)); |
| 274 | ly_in_free(in, 0); |
| 275 | mod1->implemented = 1; |
| 276 | assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0, &unres)); |
| 277 | assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres)); |
| 278 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 279 | assert_non_null(mod1->compiled); |
| 280 | assert_non_null(mod1->parsed); |
| 281 | |
| 282 | #if 0 |
| 283 | /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */ |
| 284 | |
| 285 | 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)); |
| 286 | /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */ |
| 287 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, NULL, NULL); |
| 288 | assert_int_equal(LY_ENOTFOUND, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
| 289 | /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/ |
| 290 | CHECK_LOG_CTX("Recompilation of module \"w\" failed.", NULL); |
| 291 | assert_null(mod2); |
| 292 | ly_in_free(in, 0); |
| 293 | #endif |
| 294 | |
| 295 | 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)); |
| 296 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}"); |
| 297 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod2)); |
| 298 | assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres)); |
| 299 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 300 | ly_in_free(in, 0); |
| 301 | assert_non_null(mod2); |
| 302 | assert_non_null(mod1->parsed); |
| 303 | assert_string_equal("w", mod1->name); |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | test_imports(void **state) |
| 308 | { |
| 309 | const struct lys_module *mod1, *mod2, *import; |
| 310 | |
| 311 | /* use own context with extra flags */ |
| 312 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 313 | |
| 314 | /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision |
| 315 | * from the callback should be loaded into the context and used as an import */ |
| 316 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX)); |
| 317 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
| 318 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", |
| 319 | LYS_IN_YANG, &mod1)); |
| 320 | assert_int_equal(1, mod1->latest_revision); |
| 321 | assert_int_equal(1, mod1->implemented); |
| 322 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 323 | LYS_IN_YANG, &mod2)); |
| 324 | import = mod2->parsed->imports[0].module; |
| 325 | assert_int_equal(2, import->latest_revision); |
| 326 | assert_int_equal(0, mod1->latest_revision); |
| 327 | assert_ptr_not_equal(mod1, import); |
| 328 | assert_string_equal("2019-09-17", import->revision); |
| 329 | assert_int_equal(0, import->implemented); |
| 330 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-16")); |
| 331 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 332 | |
| 333 | /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision |
| 334 | * already present in the context should be selected and the callback's revision should not be loaded into the context */ |
| 335 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &UTEST_LYCTX)); |
| 336 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
| 337 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", |
| 338 | LYS_IN_YANG, &mod1)); |
| 339 | assert_int_equal(1, mod1->latest_revision); |
| 340 | assert_int_equal(1, mod1->implemented); |
| 341 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 342 | LYS_IN_YANG, &mod2)); |
| 343 | import = mod2->parsed->imports[0].module; |
| 344 | assert_ptr_equal(mod1, import); |
| 345 | assert_int_equal(2, import->latest_revision); |
| 346 | assert_int_equal(1, import->implemented); |
| 347 | assert_string_equal("2019-09-18", import->revision); |
| 348 | assert_null(ly_ctx_get_module(UTEST_LYCTX, "a", "2019-09-17")); |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | test_get_models(void **state) |
| 353 | { |
| 354 | struct lys_module *mod, *mod2; |
| 355 | const char *str0 = "module a {namespace urn:a;prefix a;}"; |
| 356 | const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}"; |
| 357 | const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}"; |
| 358 | struct ly_in *in0, *in1, *in2; |
| 359 | struct lys_glob_unres unres = {0}; |
| 360 | |
| 361 | unsigned int index = 0; |
| 362 | const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"}; |
| 363 | |
| 364 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0)); |
| 365 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| 366 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2)); |
| 367 | |
| 368 | /* invalid arguments */ |
| 369 | assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL)); |
| 370 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_module()).", NULL); |
| 371 | assert_ptr_equal(NULL, ly_ctx_get_module(UTEST_LYCTX, NULL, NULL)); |
| 372 | CHECK_LOG_CTX("Invalid argument name (ly_ctx_get_module()).", NULL); |
| 373 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL)); |
| 374 | CHECK_LOG("Invalid argument ctx (ly_ctx_get_module_ns()).", NULL); |
| 375 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(UTEST_LYCTX, NULL, NULL)); |
| 376 | CHECK_LOG_CTX("Invalid argument ns (ly_ctx_get_module_ns()).", NULL); |
| 377 | assert_null(ly_ctx_get_module(UTEST_LYCTX, "nonsence", NULL)); |
| 378 | |
| 379 | /* internal modules */ |
| 380 | assert_null(ly_ctx_get_module_implemented(UTEST_LYCTX, "ietf-yang-types")); |
| 381 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "yang"); |
| 382 | assert_non_null(mod); |
| 383 | assert_non_null(mod->parsed); |
| 384 | assert_string_equal("yang", mod->name); |
| 385 | mod2 = ly_ctx_get_module_implemented_ns(UTEST_LYCTX, mod->ns); |
| 386 | assert_ptr_equal(mod, mod2); |
| 387 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-metadata", "2016-08-05")); |
| 388 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-yang-types", "2013-07-15")); |
| 389 | assert_non_null(ly_ctx_get_module(UTEST_LYCTX, "ietf-inet-types", "2013-07-15")); |
| 390 | assert_non_null(ly_ctx_get_module_ns(UTEST_LYCTX, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14")); |
| 391 | |
| 392 | /* select module by revision */ |
| 393 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod)); |
| 394 | assert_int_equal(LY_SUCCESS, lys_compile_unres_glob(UTEST_LYCTX, &unres)); |
| 395 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 396 | /* invalid attempts - implementing module of the same name and inserting the same module */ |
| 397 | assert_int_equal(LY_EDENIED, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, NULL)); |
| 398 | CHECK_LOG_CTX("Module \"a@2018-10-23\" is already implemented in the context.", NULL); |
| 399 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 400 | ly_in_reset(in1); |
| 401 | /* it is already there, fine */ |
| 402 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, NULL)); |
| 403 | /* insert the second module only as imported, not implemented */ |
| 404 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 405 | ly_in_reset(in2); |
| 406 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in2, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod2)); |
| 407 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 408 | assert_non_null(mod2); |
| 409 | assert_ptr_not_equal(mod, mod2); |
| 410 | mod = ly_ctx_get_module_latest(UTEST_LYCTX, "a"); |
| 411 | assert_ptr_equal(mod, mod2); |
| 412 | mod2 = ly_ctx_get_module_latest_ns(UTEST_LYCTX, mod->ns); |
| 413 | assert_ptr_equal(mod, mod2); |
| 414 | /* work with module with no revision */ |
| 415 | assert_int_equal(LY_SUCCESS, lys_create_module(UTEST_LYCTX, in0, LYS_IN_YANG, 0, NULL, NULL, NULL, &unres, &mod)); |
| 416 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 417 | assert_ptr_equal(mod, ly_ctx_get_module(UTEST_LYCTX, "a", NULL)); |
| 418 | assert_ptr_not_equal(mod, ly_ctx_get_module_latest(UTEST_LYCTX, "a")); |
| 419 | |
| 420 | str1 = "submodule b {belongs-to a {prefix a;}}"; |
| 421 | ly_in_free(in1, 0); |
| 422 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| 423 | assert_int_equal(LY_EINVAL, lys_create_module(UTEST_LYCTX, in1, LYS_IN_YANG, 1, NULL, NULL, NULL, &unres, &mod)); |
| 424 | CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL); |
| 425 | lys_compile_unres_glob_erase(UTEST_LYCTX, &unres); |
| 426 | |
| 427 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(UTEST_LYCTX, &index))) { |
| 428 | assert_string_equal(names[index - 1], mod->name); |
| 429 | } |
| 430 | assert_int_equal(9, index); |
| 431 | |
| 432 | /* cleanup */ |
| 433 | ly_in_free(in0, 0); |
| 434 | ly_in_free(in1, 0); |
| 435 | ly_in_free(in2, 0); |
| 436 | } |
| 437 | |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 438 | static void |
| 439 | test_ylmem(void **state) |
| 440 | { |
| 441 | #define DATA_YANG_LIBRARY_START "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\ |
| 442 | " <module-set>\n"\ |
| 443 | " <name>complete</name>\n"\ |
| 444 | " <module>\n"\ |
| 445 | " <name>yang</name>\n"\ |
| 446 | " <revision>2021-04-07</revision>\n"\ |
| 447 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\ |
| 448 | " </module>\n"\ |
| 449 | " <module>\n"\ |
| 450 | " <name>ietf-yang-library</name>\n"\ |
| 451 | " <revision>2019-01-04</revision>\n"\ |
| 452 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\ |
| 453 | " </module>\n" |
| 454 | |
| 455 | #define DATA_YANG_BASE_IMPORTS " <import-only-module>\n"\ |
| 456 | " <name>ietf-yang-metadata</name>\n"\ |
| 457 | " <revision>2016-08-05</revision>\n"\ |
| 458 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\ |
| 459 | " </import-only-module>\n"\ |
| 460 | " <import-only-module>\n"\ |
| 461 | " <name>ietf-inet-types</name>\n"\ |
| 462 | " <revision>2013-07-15</revision>\n"\ |
| 463 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\ |
| 464 | " </import-only-module>\n"\ |
| 465 | " <import-only-module>\n"\ |
| 466 | " <name>ietf-yang-types</name>\n"\ |
| 467 | " <revision>2013-07-15</revision>\n"\ |
| 468 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\ |
| 469 | " </import-only-module>\n"\ |
| 470 | " <import-only-module>\n"\ |
| 471 | " <name>ietf-datastores</name>\n"\ |
| 472 | " <revision>2018-02-14</revision>\n"\ |
| 473 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\ |
| 474 | " </import-only-module>\n" |
| 475 | |
| 476 | #define DATA_YANG_SCHEMA_MODULE_STATE " </module-set>\n"\ |
| 477 | " <schema>\n"\ |
| 478 | " <name>complete</name>\n"\ |
| 479 | " <module-set>complete</module-set>\n"\ |
| 480 | " </schema>\n"\ |
| 481 | " <content-id>9</content-id>\n"\ |
| 482 | "</yang-library>\n"\ |
| 483 | "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n"\ |
| 484 | " <module-set-id>12</module-set-id>\n"\ |
| 485 | " <module>\n"\ |
| 486 | " <name>ietf-yang-metadata</name>\n"\ |
| 487 | " <revision>2016-08-05</revision>\n"\ |
| 488 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n"\ |
| 489 | " <conformance-type>import</conformance-type>\n"\ |
| 490 | " </module>\n"\ |
| 491 | " <module>\n"\ |
| 492 | " <name>yang</name>\n"\ |
| 493 | " <revision>2020-06-17</revision>\n"\ |
| 494 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n"\ |
| 495 | " <conformance-type>implement</conformance-type>\n"\ |
| 496 | " </module>\n"\ |
| 497 | " <module>\n"\ |
| 498 | " <name>ietf-inet-types</name>\n"\ |
| 499 | " <revision>2013-07-15</revision>\n"\ |
| 500 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n"\ |
| 501 | " <conformance-type>import</conformance-type>\n"\ |
| 502 | " </module>\n"\ |
| 503 | " <module>\n"\ |
| 504 | " <name>ietf-yang-types</name>\n"\ |
| 505 | " <revision>2013-07-15</revision>\n"\ |
| 506 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n"\ |
| 507 | " <conformance-type>import</conformance-type>\n"\ |
| 508 | " </module>\n"\ |
| 509 | " <module>\n"\ |
| 510 | " <name>ietf-yang-library</name>\n"\ |
| 511 | " <revision>2019-01-04</revision>\n"\ |
| 512 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n"\ |
| 513 | " <conformance-type>implement</conformance-type>\n"\ |
| 514 | " </module>\n"\ |
| 515 | " <module>\n"\ |
| 516 | " <name>ietf-datastores</name>\n"\ |
| 517 | " <revision>2018-02-14</revision>\n"\ |
| 518 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n"\ |
| 519 | " <conformance-type>import</conformance-type>\n"\ |
| 520 | " </module>\n" |
| 521 | |
| 522 | const char *yanglibrary_only = |
| 523 | DATA_YANG_LIBRARY_START |
| 524 | DATA_YANG_BASE_IMPORTS |
| 525 | DATA_YANG_SCHEMA_MODULE_STATE |
| 526 | "</modules-state>\n"; |
| 527 | |
| 528 | const char *with_netconf = |
| 529 | DATA_YANG_LIBRARY_START |
| 530 | " <module>\n" |
| 531 | " <name>ietf-netconf</name>\n" |
| 532 | " <revision>2011-06-01</revision>\n" |
| 533 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 534 | " </module>\n" |
| 535 | DATA_YANG_BASE_IMPORTS |
| 536 | " <import-only-module>\n" |
| 537 | " <name>ietf-netconf-acm</name>\n" |
| 538 | " <revision>2018-02-14</revision>\n" |
| 539 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 540 | " </import-only-module>\n" |
| 541 | DATA_YANG_SCHEMA_MODULE_STATE |
| 542 | " <module>\n" |
| 543 | " <name>ietf-netconf</name>\n" |
| 544 | " <revision>2011-06-01</revision>\n" |
| 545 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 546 | " <conformance-type>implement</conformance-type>\n" |
| 547 | " </module>\n" |
| 548 | " <module>\n" |
| 549 | " <name>ietf-netconf-acm</name>\n" |
| 550 | " <revision>2018-02-14</revision>\n" |
| 551 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 552 | " <conformance-type>import</conformance-type>\n" |
| 553 | " </module>\n" |
| 554 | "</modules-state>"; |
| 555 | |
| 556 | const char *with_netconf_features = |
| 557 | DATA_YANG_LIBRARY_START |
| 558 | " <module>\n" |
| 559 | " <name>ietf-netconf</name>\n" |
| 560 | " <revision>2011-06-01</revision>\n" |
| 561 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 562 | " <feature>writable-running</feature>\n" |
| 563 | " <feature>candidate</feature>\n" |
| 564 | " <feature>confirmed-commit</feature>\n" |
| 565 | " <feature>rollback-on-error</feature>\n" |
| 566 | " <feature>validate</feature>\n" |
| 567 | " <feature>startup</feature>\n" |
| 568 | " <feature>url</feature>\n" |
| 569 | " <feature>xpath</feature>\n" |
| 570 | " </module>\n" |
| 571 | " <import-only-module>\n" |
| 572 | " <name>ietf-yang-metadata</name>\n" |
| 573 | " <revision>2016-08-05</revision>\n" |
| 574 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n" |
| 575 | " </import-only-module>\n" |
| 576 | " <import-only-module>\n" |
| 577 | " <name>ietf-inet-types</name>\n" |
| 578 | " <revision>2013-07-15</revision>\n" |
| 579 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n" |
| 580 | " </import-only-module>\n" |
| 581 | " <import-only-module>\n" |
| 582 | " <name>ietf-yang-types</name>\n" |
| 583 | " <revision>2013-07-15</revision>\n" |
| 584 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n" |
| 585 | " </import-only-module>\n" |
| 586 | " <import-only-module>\n" |
| 587 | " <name>ietf-datastores</name>\n" |
| 588 | " <revision>2018-02-14</revision>\n" |
| 589 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-datastores</namespace>\n" |
| 590 | " </import-only-module>\n" |
| 591 | " <import-only-module>\n" |
| 592 | " <name>ietf-netconf-acm</name>\n" |
| 593 | " <revision>2018-02-14</revision>\n" |
| 594 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 595 | " </import-only-module>\n" |
| 596 | DATA_YANG_SCHEMA_MODULE_STATE |
| 597 | " <module>\n" |
| 598 | " <name>ietf-netconf</name>\n" |
| 599 | " <revision>2011-06-01</revision>\n" |
| 600 | " <namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>\n" |
| 601 | " <feature>writable-running</feature>\n" |
| 602 | " <feature>candidate</feature>\n" |
| 603 | " <feature>confirmed-commit</feature>\n" |
| 604 | " <feature>rollback-on-error</feature>\n" |
| 605 | " <feature>validate</feature>\n" |
| 606 | " <feature>startup</feature>\n" |
| 607 | " <feature>url</feature>\n" |
| 608 | " <feature>xpath</feature>\n" |
| 609 | " <conformance-type>implement</conformance-type>\n" |
| 610 | " </module>\n" |
| 611 | " <module>\n" |
| 612 | " <name>ietf-netconf-acm</name>\n" |
| 613 | " <revision>2018-02-14</revision>\n" |
| 614 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>\n" |
| 615 | " <conformance-type>import</conformance-type>\n" |
| 616 | " </module>\n" |
| 617 | "</modules-state>"; |
| 618 | |
| 619 | const char *garbage_revision = |
| 620 | "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 621 | " <module-set>\n" |
| 622 | " <name>complete</name>\n" |
| 623 | " <module>\n" |
| 624 | " <name>yang</name>\n" |
| 625 | " <revision>2020-06-17</revision>\n" |
| 626 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 627 | " </module>\n" |
| 628 | " <module>\n" |
| 629 | " <name>ietf-yang-library</name>\n" |
| 630 | " <revision>2019-01-01</revision>\n" |
| 631 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n" |
| 632 | " </module>\n" |
| 633 | DATA_YANG_BASE_IMPORTS |
| 634 | DATA_YANG_SCHEMA_MODULE_STATE |
| 635 | "</modules-state>\n"; |
| 636 | |
| 637 | const char *no_yanglibrary = |
| 638 | "<yang-library xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 639 | " <module-set>\n" |
| 640 | " <name>complete</name>\n" |
| 641 | " <module>\n" |
| 642 | " <name>yang</name>\n" |
| 643 | " <revision>2021-04-07</revision>\n" |
| 644 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 645 | " </module>\n" |
| 646 | DATA_YANG_BASE_IMPORTS |
| 647 | DATA_YANG_SCHEMA_MODULE_STATE |
| 648 | "</modules-state>\n"; |
| 649 | |
| 650 | (void) state; |
| 651 | /* seperate context to avoid double free during teadown */ |
| 652 | struct ly_ctx *ctx_test = NULL; |
| 653 | |
| 654 | /* test invalid parameters */ |
| 655 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, NULL, LYD_XML, 0, &ctx_test)); |
| 656 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylpath(NULL, TESTS_SRC, LYD_XML, 0, NULL)); |
| 657 | assert_int_equal(LY_ESYS, ly_ctx_new_ylpath(NULL, TESTS_SRC "garbage", LYD_XML, 0, &ctx_test)); |
| 658 | |
| 659 | /* basic test with ietf-yang-library-only */ |
| 660 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", yanglibrary_only, LYD_XML, 0, &ctx_test)); |
| 661 | assert_ptr_not_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04")); |
| 662 | assert_null(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
| 663 | ly_ctx_destroy(ctx_test, NULL); |
| 664 | |
| 665 | /* test loading module, should also import other module */ |
| 666 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf, LYD_XML, 0, &ctx_test)); |
| 667 | assert_int_not_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
| 668 | assert_int_equal(1, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")->implemented); |
| 669 | assert_int_not_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")); |
| 670 | assert_int_equal(0, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")->implemented); |
| 671 | assert_int_equal(LY_ENOT, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url")); |
| 672 | ly_ctx_destroy(ctx_test, NULL); |
| 673 | |
| 674 | /* test loading module with feature if they are present */ |
| 675 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, 0, &ctx_test)); |
| 676 | assert_ptr_not_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01")); |
| 677 | assert_ptr_not_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-netconf-acm", "2018-02-14")); |
| 678 | assert_int_equal(LY_SUCCESS, lys_feature_value(ly_ctx_get_module(ctx_test, "ietf-netconf", "2011-06-01"), "url")); |
| 679 | ly_ctx_destroy(ctx_test, NULL); |
| 680 | |
| 681 | /* test with not matching revision */ |
| 682 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", garbage_revision, LYD_XML, 0, &ctx_test)); |
| 683 | |
| 684 | /* test data containing ietf-yang-library which conflicts with the option */ |
| 685 | assert_int_equal(LY_EINVAL, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", with_netconf_features, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test)); |
| 686 | |
| 687 | /* test creating without ietf-yang-library */ |
| 688 | assert_int_equal(LY_SUCCESS, ly_ctx_new_ylmem(TESTS_SRC "/modules/yang/", no_yanglibrary, LYD_XML, LY_CTX_NO_YANGLIBRARY, &ctx_test)); |
| 689 | assert_int_equal(NULL, ly_ctx_get_module(ctx_test, "ietf-yang-library", "2019-01-04")); |
| 690 | ly_ctx_destroy(ctx_test, NULL); |
| 691 | } |
| 692 | |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame^] | 693 | static LY_ERR |
| 694 | check_node_priv_parsed_is_set(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue)) |
| 695 | { |
| 696 | const struct lysp_node *pnode; |
| 697 | const char ***iter; |
| 698 | |
| 699 | pnode = (const struct lysp_node *)node->priv; |
| 700 | CHECK_POINTER(pnode, 1); |
| 701 | iter = (const char ***)data; |
| 702 | CHECK_POINTER(**iter, 1); |
| 703 | CHECK_STRING(pnode->name, **iter); |
| 704 | (*iter)++; |
| 705 | |
| 706 | return LY_SUCCESS; |
| 707 | } |
| 708 | |
| 709 | static LY_ERR |
| 710 | check_node_priv_parsed_not_set(struct lysc_node *node, void *UNUSED(data), ly_bool *UNUSED(dfs_continue)) |
| 711 | { |
| 712 | CHECK_POINTER(node->priv, 0); |
| 713 | return LY_SUCCESS; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * @brief Testing of LY_CTX_SET_PRIV_PARSED. |
| 718 | */ |
| 719 | static void |
| 720 | test_set_priv_parsed(void **state) |
| 721 | { |
| 722 | const struct lys_module *mod; |
| 723 | const char *schema_a; |
| 724 | const char **iter; |
| 725 | const char *check[] = { |
| 726 | "cont", "contnotif", "augleaf", "contx", "grpleaf", "l1", |
| 727 | "l1a", "l1b", "l1c", "foo1", "ll", "any", "l2", |
| 728 | "l2c", "l2cx", "ch", "cas", "casx", "oper", |
| 729 | "input", "inparam", "output", "outparam", "n1", NULL |
| 730 | }; |
| 731 | |
| 732 | /* each node must have a unique name. */ |
| 733 | schema_a = "module a {\n" |
| 734 | " namespace urn:tests:a;\n" |
| 735 | " prefix a;yang-version 1.1;\n" |
| 736 | " container cont {\n" |
| 737 | " notification contnotif;\n" |
| 738 | " leaf-list contx {\n" |
| 739 | " type string;\n" |
| 740 | " }\n" |
| 741 | " uses grp;\n" |
| 742 | " }\n" |
| 743 | " list l1 {\n" |
| 744 | " key \"l1a l1b\";\n" |
| 745 | " leaf l1a {\n" |
| 746 | " type string;\n" |
| 747 | " }\n" |
| 748 | " leaf l1b {\n" |
| 749 | " type string;\n" |
| 750 | " }\n" |
| 751 | " leaf l1c {\n" |
| 752 | " type string;\n" |
| 753 | " }\n" |
| 754 | " }\n" |
| 755 | " feature f1;\n" |
| 756 | " feature f2;\n" |
| 757 | " leaf foo1 {\n" |
| 758 | " type uint16;\n" |
| 759 | " if-feature f1;\n" |
| 760 | " }\n" |
| 761 | " leaf foo2 {\n" |
| 762 | " type uint16;\n" |
| 763 | " }\n" |
| 764 | " leaf foo3 {\n" |
| 765 | " type uint16;\n" |
| 766 | " if-feature f2;\n" |
| 767 | " }\n" |
| 768 | " leaf-list ll {\n" |
| 769 | " type string;\n" |
| 770 | " }\n" |
| 771 | " anydata any {\n" |
| 772 | " config false;\n" |
| 773 | " }\n" |
| 774 | " list l2 {\n" |
| 775 | " config false;\n" |
| 776 | " container l2c {\n" |
| 777 | " leaf l2cx {\n" |
| 778 | " type string;\n" |
| 779 | " }\n" |
| 780 | " }\n" |
| 781 | " }\n" |
| 782 | " choice ch {\n" |
| 783 | " case cas {\n" |
| 784 | " leaf casx {\n" |
| 785 | " type string;\n" |
| 786 | " }\n" |
| 787 | " }\n" |
| 788 | " }\n" |
| 789 | " rpc oper {\n" |
| 790 | " input {\n" |
| 791 | " leaf inparam {\n" |
| 792 | " type string;\n" |
| 793 | " }\n" |
| 794 | " }\n" |
| 795 | " output {\n" |
| 796 | " leaf outparam {\n" |
| 797 | " type int8;\n" |
| 798 | " }\n" |
| 799 | " }\n" |
| 800 | " }\n" |
| 801 | " notification n1;\n" |
| 802 | " grouping grp {\n" |
| 803 | " leaf grpleaf {\n" |
| 804 | " type uint16;\n" |
| 805 | " }\n" |
| 806 | " }\n" |
| 807 | " augment /cont {\n" |
| 808 | " leaf augleaf {\n" |
| 809 | " type uint16;\n" |
| 810 | " }\n" |
| 811 | " }\n" |
| 812 | " deviation /a:foo2 {\n" |
| 813 | " deviate not-supported;\n" |
| 814 | " }\n" |
| 815 | "}\n"; |
| 816 | |
| 817 | /* use own context with extra flags */ |
| 818 | ly_ctx_destroy(UTEST_LYCTX, NULL); |
| 819 | const char *feats[] = {"f1", NULL}; |
| 820 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_SET_PRIV_PARSED, &UTEST_LYCTX)); |
| 821 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL); |
| 822 | |
| 823 | print_message("[ ] create context\n"); |
| 824 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 825 | iter = check; |
| 826 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter)); |
| 827 | |
| 828 | print_message("[ ] unset option\n"); |
| 829 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED)); |
| 830 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 831 | iter = check; |
| 832 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_not_set, &iter)); |
| 833 | |
| 834 | print_message("[ ] set option\n"); |
| 835 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_SET_PRIV_PARSED)); |
| 836 | mod = ly_ctx_get_module(UTEST_LYCTX, "a", NULL); |
| 837 | iter = check; |
| 838 | assert_int_equal(LY_SUCCESS, lysc_module_dfs_full(mod, check_node_priv_parsed_is_set, &iter)); |
| 839 | } |
| 840 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 841 | int |
| 842 | main(void) |
| 843 | { |
| 844 | const struct CMUnitTest tests[] = { |
| 845 | UTEST(test_searchdirs), |
| 846 | UTEST(test_options), |
| 847 | UTEST(test_models), |
| 848 | UTEST(test_imports), |
| 849 | UTEST(test_get_models), |
Tadeáš Vintrlík | ea26818 | 2021-04-07 13:53:32 +0200 | [diff] [blame] | 850 | UTEST(test_ylmem), |
aPiecek | bb96b80 | 2021-04-12 08:12:52 +0200 | [diff] [blame^] | 851 | UTEST(test_set_priv_parsed), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 855 | } |