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