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