Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from context.c |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 23 | #include "common.h" |
| 24 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 25 | #include "in.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 26 | #include "tests/config.h" |
| 27 | #include "tree_schema_internal.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 28 | #include "schema_compile.h" |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 29 | |
| 30 | #define BUFSIZE 1024 |
| 31 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 32 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 33 | |
| 34 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 35 | #define ENABLE_LOGGER_CHECKING 1 |
| 36 | |
| 37 | static void |
| 38 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 39 | { |
| 40 | (void) level; /* unused */ |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 41 | if (store) { |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 42 | if (path && path[0]) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 43 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 44 | } else { |
| 45 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 46 | } |
| 47 | if (store > 0) { |
| 48 | --store; |
| 49 | } |
| 50 | } |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int |
| 54 | logger_setup(void **state) |
| 55 | { |
| 56 | (void) state; /* unused */ |
| 57 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 58 | ly_set_log_clb(logger, 1); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 59 | #endif |
| 60 | return 0; |
| 61 | } |
| 62 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 63 | static int |
| 64 | logger_teardown(void **state) |
| 65 | { |
| 66 | (void) state; /* unused */ |
| 67 | #if ENABLE_LOGGER_CHECKING |
| 68 | if (*state) { |
| 69 | fprintf(stderr, "%s\n", logbuf); |
| 70 | } |
| 71 | #endif |
| 72 | return 0; |
| 73 | } |
| 74 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 75 | #if ENABLE_LOGGER_CHECKING |
| 76 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 77 | #else |
| 78 | # define logbuf_assert(str) |
| 79 | #endif |
| 80 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 81 | static void |
| 82 | test_searchdirs(void **state) |
| 83 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 84 | *state = test_searchdirs; |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 85 | |
| 86 | struct ly_ctx *ctx; |
| 87 | const char * const *list; |
| 88 | |
| 89 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 90 | |
| 91 | /* invalid arguments */ |
| 92 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL)); |
| 93 | logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir())."); |
| 94 | assert_null(ly_ctx_get_searchdirs(NULL)); |
| 95 | logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs())."); |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 96 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(NULL, NULL)); |
| 97 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdir())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 98 | |
| 99 | /* readable and executable, but not a directory */ |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 100 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utest_context")); |
| 101 | logbuf_assert("Given search directory \""TESTS_BIN"/utest_context\" is not a directory."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 102 | /* not executable */ |
Radek Krejci | 9efa87a | 2018-09-26 15:14:03 +0200 | [diff] [blame] | 103 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, __FILE__)); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 104 | logbuf_assert("Unable to fully access search directory \""__FILE__"\" (Permission denied)."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 105 | /* not existing */ |
| 106 | assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile")); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 107 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 108 | |
| 109 | /* ly_set_add() fails */ |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 110 | /* no change */ |
| 111 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL)); |
| 112 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 113 | /* correct path */ |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 114 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 115 | assert_int_equal(1, ctx->search_paths.count); |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 116 | assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 117 | |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 118 | /* duplicated paths */ |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 119 | assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/utests")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 120 | assert_int_equal(1, ctx->search_paths.count); |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 121 | assert_string_equal(TESTS_BIN"/utests", ctx->search_paths.objs[0]); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 122 | |
| 123 | /* another paths - add 8 to fill the initial buffer of the searchpaths list */ |
| 124 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles")); |
| 125 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src")); |
| 126 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules")); |
| 127 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc")); |
| 128 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC)); |
| 129 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN)); |
Radek Krejci | ef6867f | 2018-11-27 11:32:00 +0100 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/home")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 131 | assert_int_equal(8, ctx->search_paths.count); |
| 132 | |
| 133 | /* get searchpaths */ |
| 134 | list = ly_ctx_get_searchdirs(ctx); |
| 135 | assert_non_null(list); |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 136 | assert_string_equal(TESTS_BIN"/utests", list[0]); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 137 | assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]); |
| 138 | assert_string_equal(TESTS_SRC, list[5]); |
| 139 | assert_string_equal(TESTS_BIN, list[6]); |
Radek Krejci | ef6867f | 2018-11-27 11:32:00 +0100 | [diff] [blame] | 140 | assert_string_equal("/home", list[7]); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 141 | assert_null(list[8]); |
| 142 | |
| 143 | /* removing searchpaths */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 144 | /* nonexisting */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 145 | assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdir(ctx, "/nonexistingfile")); |
| 146 | logbuf_assert("Invalid argument value (ly_ctx_unset_searchdir())."); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 147 | /* first */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 148 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_BIN"/utests")); |
Radek Krejci | 1fd5ad6 | 2020-05-15 15:48:42 +0200 | [diff] [blame] | 149 | assert_string_not_equal(TESTS_BIN"/utests", list[0]); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 150 | assert_int_equal(7, ctx->search_paths.count); |
| 151 | /* middle */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 152 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, TESTS_SRC)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 153 | assert_int_equal(6, ctx->search_paths.count); |
| 154 | /* last */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 155 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, "/home")); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 156 | assert_int_equal(5, ctx->search_paths.count); |
| 157 | /* all */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 158 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL)); |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 159 | assert_int_equal(0, ctx->search_paths.count); |
| 160 | |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 161 | /* again - no change */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 162 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdir(ctx, NULL)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 163 | |
| 164 | /* cleanup */ |
| 165 | ly_ctx_destroy(ctx, NULL); |
| 166 | |
| 167 | /* test searchdir list in ly_ctx_new() */ |
| 168 | assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx)); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 169 | logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)."); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/home:/home:"TESTS_SRC, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 171 | assert_int_equal(2, ctx->search_paths.count); |
| 172 | assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]); |
Radek Krejci | ef6867f | 2018-11-27 11:32:00 +0100 | [diff] [blame] | 173 | assert_string_equal("/home", ctx->search_paths.objs[1]); |
Radek Krejci | 555c9bb | 2018-09-20 12:45:13 +0200 | [diff] [blame] | 174 | |
| 175 | /* cleanup */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 176 | *state = NULL; |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 177 | ly_ctx_destroy(ctx, NULL); |
| 178 | } |
| 179 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 180 | static void |
| 181 | test_options(void **state) |
| 182 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 183 | *state = test_options; |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 184 | |
| 185 | struct ly_ctx *ctx; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 186 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 187 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffff, &ctx)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 188 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 189 | /* invalid arguments */ |
| 190 | assert_int_equal(0, ly_ctx_get_options(NULL)); |
| 191 | logbuf_assert("Invalid argument ctx (ly_ctx_get_options())."); |
| 192 | |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 193 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(NULL, 0)); |
| 194 | logbuf_assert("Invalid argument ctx (ly_ctx_set_options())."); |
| 195 | assert_int_equal(LY_EINVAL, ly_ctx_unset_options(NULL, 0)); |
| 196 | logbuf_assert("Invalid argument ctx (ly_ctx_unset_options())."); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 197 | |
| 198 | /* option not allowed to be changed */ |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 199 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NO_YANGLIBRARY)); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 200 | logbuf_assert("Invalid argument option (ly_ctx_set_options())."); |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_EINVAL, ly_ctx_set_options(ctx, LY_CTX_NO_YANGLIBRARY)); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 202 | logbuf_assert("Invalid argument option (ly_ctx_set_options())."); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 203 | |
| 204 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 205 | /* unset */ |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 206 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 207 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED); |
| 208 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_ALL_IMPLEMENTED)); |
| 209 | assert_int_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED); |
| 210 | |
| 211 | /* LY_CTX_REF_IMPLEMENTED */ |
| 212 | assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED); |
| 213 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_REF_IMPLEMENTED)); |
| 214 | assert_int_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 215 | |
| 216 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
| 217 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 218 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 219 | assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 220 | |
| 221 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
| 222 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 223 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 224 | assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 225 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 226 | /* LY_CTX_PREFER_SEARCHDIRS */ |
| 227 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 228 | assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 229 | assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 230 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 231 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 232 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 233 | /* set back */ |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 234 | /* LY_CTX_ALL_IMPLEMENTED */ |
| 235 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_ALL_IMPLEMENTED)); |
| 236 | assert_int_not_equal(0, ctx->flags & LY_CTX_ALL_IMPLEMENTED); |
| 237 | |
| 238 | /* LY_CTX_REF_IMPLEMENTED */ |
| 239 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_REF_IMPLEMENTED)); |
| 240 | assert_int_not_equal(0, ctx->flags & LY_CTX_REF_IMPLEMENTED); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 241 | |
| 242 | /* LY_CTX_DISABLE_SEARCHDIRS */ |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 243 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 244 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS); |
| 245 | |
| 246 | /* LY_CTX_DISABLE_SEARCHDIR_CWD */ |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 247 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 248 | assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD); |
| 249 | |
| 250 | /* LY_CTX_PREFER_SEARCHDIRS */ |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 251 | assert_int_equal(LY_SUCCESS, ly_ctx_set_options(ctx, LY_CTX_PREFER_SEARCHDIRS)); |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 252 | assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS); |
| 253 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 254 | assert_int_equal(ctx->flags, ly_ctx_get_options(ctx)); |
| 255 | |
| 256 | /* cleanup */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 257 | *state = NULL; |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 258 | ly_ctx_destroy(ctx, NULL); |
| 259 | } |
| 260 | |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 261 | static LY_ERR 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 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 271 | static void |
| 272 | test_models(void **state) |
| 273 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 274 | *state = test_models; |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 275 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 276 | struct ly_ctx *ctx; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 277 | struct ly_in *in; |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 278 | const char *str; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 279 | struct lys_module *mod1, *mod2; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 280 | |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 281 | /* invalid arguments */ |
| 282 | assert_int_equal(0, ly_ctx_get_module_set_id(NULL)); |
| 283 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id())."); |
| 284 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 285 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
Radek Krejci | 962a810 | 2018-09-20 14:21:06 +0200 | [diff] [blame] | 286 | assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx)); |
| 287 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 288 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module x {namespace urn:x;prefix x;}", &in)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 289 | assert_int_equal(LY_EINVAL, lys_create_module(ctx, in, 4, 1, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 290 | ly_in_free(in, 0); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 291 | logbuf_assert("Invalid schema input format."); |
| 292 | |
Radek Krejci | 2d31ea7 | 2018-10-25 15:46:42 +0200 | [diff] [blame] | 293 | /* import callback */ |
| 294 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, (void*)(str = "test")); |
| 295 | assert_ptr_equal(test_imp_clb, ctx->imp_clb); |
| 296 | assert_ptr_equal(str, ctx->imp_clb_data); |
| 297 | assert_ptr_equal(test_imp_clb, ly_ctx_get_module_imp_clb(ctx, (void**)&str)); |
| 298 | assert_string_equal("test", str); |
| 299 | |
| 300 | ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
| 301 | assert_null(ctx->imp_clb); |
| 302 | assert_null(ctx->imp_clb_data); |
| 303 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 304 | /* name collision of module and submodule */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 305 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-30;}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 306 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;include y;}", &in)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 307 | assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 308 | ly_in_free(in, 0); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 309 | logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| 310 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 311 | 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 | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 312 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 313 | ly_in_free(in, 0); |
| 314 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module y {namespace urn:y;prefix y;}", &in)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 315 | assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 316 | ly_in_free(in, 0); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 317 | logbuf_assert("Name collision between module and submodule of name \"y\". Line number 1."); |
| 318 | |
| 319 | store = 1; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 320 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to b {prefix b;}}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 321 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module b {namespace urn:b;prefix b;include y;}", &in)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 322 | assert_int_equal(LY_EVALID, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 323 | ly_in_free(in, 0); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 324 | logbuf_assert("Name collision between submodules of name \"y\". Line number 1."); |
| 325 | store = -1; |
| 326 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 327 | /* selecting correct revision of the submodules */ |
| 328 | ly_ctx_reset_latests(ctx); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 329 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule y {belongs-to a {prefix a;} revision 2018-10-31;}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 330 | 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 | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 331 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod2)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 332 | ly_in_free(in, 0); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 333 | assert_string_equal("2018-10-31", mod2->parsed->includes[0].submodule->revs[0].date); |
| 334 | |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 335 | /* reloading module in case only the compiled module resists in the context */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 336 | assert_int_equal(LY_SUCCESS, ly_in_new_memory("module w {namespace urn:w;prefix w;revision 2018-10-24;}", &in)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 337 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 0, NULL, NULL, &mod1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 338 | ly_in_free(in, 0); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 339 | mod1->implemented = 1; |
Radek Krejci | 90d4e92 | 2020-10-12 15:55:33 +0200 | [diff] [blame] | 340 | assert_int_equal(LY_SUCCESS, lys_compile(mod1, 0)); |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 341 | assert_non_null(mod1->compiled); |
Radek Krejci | 90d4e92 | 2020-10-12 15:55:33 +0200 | [diff] [blame] | 342 | assert_non_null(mod1->parsed); |
| 343 | |
| 344 | #if 0 |
| 345 | /* TODO in case we are able to remove the parsed schema, here we will test how it will handle missing import parsed schema */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 346 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 347 | 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)); |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 348 | /* mod1->parsed is necessary to compile mod2 because of possible groupings, typedefs, ... */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 349 | ly_ctx_set_module_imp_clb(ctx, NULL, NULL); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 350 | assert_int_equal(LY_ENOTFOUND, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 351 | /*logbuf_assert("Unable to reload \"w\" module to import it into \"z\", source data not found.");*/ |
| 352 | logbuf_assert("Recompilation of module \"w\" failed."); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 353 | assert_null(mod2); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 354 | ly_in_free(in, 0); |
Radek Krejci | 90d4e92 | 2020-10-12 15:55:33 +0200 | [diff] [blame] | 355 | #endif |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 356 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 357 | 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)); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 358 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module w {namespace urn:w;prefix w;revision 2018-10-24;}"); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 359 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in, LYS_IN_YANG, 1, NULL, NULL, &mod2)); |
| 360 | ly_in_free(in, 0); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 361 | assert_non_null(mod2); |
Radek Krejci | 9c53696 | 2018-10-31 14:52:13 +0100 | [diff] [blame] | 362 | assert_non_null(mod1->parsed); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 363 | assert_string_equal("w", mod1->name); |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 364 | |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 365 | /* cleanup */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 366 | *state = NULL; |
Radek Krejci | 0c4b22b | 2018-09-20 12:55:46 +0200 | [diff] [blame] | 367 | ly_ctx_destroy(ctx, NULL); |
| 368 | } |
| 369 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 370 | static void |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 371 | test_imports(void **state) |
| 372 | { |
| 373 | *state = test_imports; |
| 374 | |
| 375 | struct ly_ctx *ctx; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 376 | const struct lys_module *mod1, *mod2, *import; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 377 | |
| 378 | /* import callback provides newer revision of module 'a' than present in context, so when importing 'a', the newer revision |
| 379 | * from the callback should be loaded into the context and used as an import */ |
| 380 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 381 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 382 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-16;}", |
| 383 | LYS_IN_YANG, &mod1)); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 384 | assert_int_equal(1, mod1->latest_revision); |
| 385 | assert_int_equal(1, mod1->implemented); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 386 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 387 | LYS_IN_YANG, &mod2)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 388 | import = mod2->parsed->imports[0].module; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 389 | assert_int_equal(2, import->latest_revision); |
| 390 | assert_int_equal(0, mod1->latest_revision); |
| 391 | assert_ptr_not_equal(mod1, import); |
| 392 | assert_string_equal("2019-09-17", import->revision); |
| 393 | assert_int_equal(0, import->implemented); |
| 394 | assert_non_null(ly_ctx_get_module(ctx, "a", "2019-09-16")); |
| 395 | ly_ctx_destroy(ctx, NULL); |
| 396 | |
| 397 | /* import callback provides older revision of module 'a' than present in context, so when importing a, the newer revision |
| 398 | * already present in the context should be selected and the callback's revision should not be loaded into the context */ |
| 399 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 400 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a; prefix a; revision 2019-09-17;}"); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 401 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;revision 2019-09-18;}", |
| 402 | LYS_IN_YANG, &mod1)); |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 403 | assert_int_equal(1, mod1->latest_revision); |
| 404 | assert_int_equal(1, mod1->implemented); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 405 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}}", |
| 406 | LYS_IN_YANG, &mod2)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 407 | import = mod2->parsed->imports[0].module; |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 408 | assert_ptr_equal(mod1, import); |
| 409 | assert_int_equal(2, import->latest_revision); |
| 410 | assert_int_equal(1, import->implemented); |
| 411 | assert_string_equal("2019-09-18", import->revision); |
| 412 | assert_null(ly_ctx_get_module(ctx, "a", "2019-09-17")); |
| 413 | ly_ctx_destroy(ctx, NULL); |
| 414 | |
| 415 | /* cleanup */ |
| 416 | *state = NULL; |
| 417 | } |
| 418 | |
| 419 | static void |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 420 | test_get_models(void **state) |
| 421 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 422 | *state = test_get_models; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 423 | |
| 424 | struct ly_ctx *ctx; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 425 | struct lys_module *mod, *mod2; |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 426 | const char *str0 = "module a {namespace urn:a;prefix a;}"; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 427 | const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}"; |
| 428 | const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 429 | struct ly_in *in0, *in1, *in2; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 430 | |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 431 | unsigned int index = 0; |
| 432 | const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"}; |
| 433 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 434 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str0, &in0)); |
| 435 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
| 436 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str2, &in2)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 437 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 438 | |
| 439 | /* invalid arguments */ |
| 440 | assert_ptr_equal(NULL, ly_ctx_get_module(NULL, NULL, NULL)); |
| 441 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module())."); |
| 442 | assert_ptr_equal(NULL, ly_ctx_get_module(ctx, NULL, NULL)); |
| 443 | logbuf_assert("Invalid argument name (ly_ctx_get_module())."); |
| 444 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(NULL, NULL, NULL)); |
| 445 | logbuf_assert("Invalid argument ctx (ly_ctx_get_module_ns())."); |
| 446 | assert_ptr_equal(NULL, ly_ctx_get_module_ns(ctx, NULL, NULL)); |
| 447 | logbuf_assert("Invalid argument ns (ly_ctx_get_module_ns())."); |
| 448 | assert_null(ly_ctx_get_module(ctx, "nonsence", NULL)); |
| 449 | |
| 450 | /* internal modules */ |
| 451 | assert_null(ly_ctx_get_module_implemented(ctx, "ietf-yang-types")); |
| 452 | mod = ly_ctx_get_module_implemented(ctx, "yang"); |
| 453 | assert_non_null(mod); |
| 454 | assert_non_null(mod->parsed); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 455 | assert_string_equal("yang", mod->name); |
| 456 | mod2 = ly_ctx_get_module_implemented_ns(ctx, mod->ns); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 457 | assert_ptr_equal(mod, mod2); |
| 458 | assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-metadata", "2016-08-05")); |
| 459 | assert_non_null(ly_ctx_get_module(ctx, "ietf-yang-types", "2013-07-15")); |
| 460 | assert_non_null(ly_ctx_get_module(ctx, "ietf-inet-types", "2013-07-15")); |
Michal Vasko | f872e20 | 2020-05-27 11:49:06 +0200 | [diff] [blame] | 461 | assert_non_null(ly_ctx_get_module_ns(ctx, "urn:ietf:params:xml:ns:yang:ietf-datastores", "2018-02-14")); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 462 | |
| 463 | /* select module by revision */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 464 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 465 | /* invalid attempts - implementing module of the same name and inserting the same module */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 466 | assert_int_equal(LY_EDENIED, lys_create_module(ctx, in2, LYS_IN_YANG, 1, NULL, NULL, NULL)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 467 | logbuf_assert("Module \"a\" is already implemented in the context."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 468 | ly_in_reset(in1); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 469 | assert_int_equal(LY_EEXIST, lys_create_module(ctx, in1, LYS_IN_YANG, 0, NULL, NULL, NULL)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 470 | logbuf_assert("Module \"a\" of revision \"2018-10-23\" is already present in the context."); |
| 471 | /* insert the second module only as imported, not implemented */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 472 | ly_in_reset(in2); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 473 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in2, LYS_IN_YANG, 0, NULL, NULL, &mod2)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 474 | assert_non_null(mod2); |
| 475 | assert_ptr_not_equal(mod, mod2); |
| 476 | mod = ly_ctx_get_module_latest(ctx, "a"); |
| 477 | assert_ptr_equal(mod, mod2); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 478 | mod2 = ly_ctx_get_module_latest_ns(ctx, mod->ns); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 479 | assert_ptr_equal(mod, mod2); |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 480 | /* work with module with no revision */ |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 481 | assert_int_equal(LY_SUCCESS, lys_create_module(ctx, in0, LYS_IN_YANG, 0, NULL, NULL, &mod)); |
Radek Krejci | 5e163df | 2018-10-24 14:42:15 +0200 | [diff] [blame] | 482 | assert_ptr_equal(mod, ly_ctx_get_module(ctx, "a", NULL)); |
| 483 | assert_ptr_not_equal(mod, ly_ctx_get_module_latest(ctx, "a")); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 485 | str1 = "submodule b {belongs-to a {prefix a;}}"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 486 | ly_in_free(in1, 0); |
| 487 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(str1, &in1)); |
Michal Vasko | 7a0b076 | 2020-09-02 16:37:01 +0200 | [diff] [blame] | 488 | assert_int_equal(LY_EINVAL, lys_create_module(ctx, in1, LYS_IN_YANG, 1, NULL, NULL, &mod)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 489 | logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module."); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 490 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 491 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(ctx, &index))) { |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 492 | assert_string_equal(names[index - 1], mod->name); |
| 493 | } |
| 494 | assert_int_equal(9, index); |
| 495 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 496 | /* cleanup */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 497 | *state = NULL; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 498 | ly_in_free(in0, 0); |
| 499 | ly_in_free(in1, 0); |
| 500 | ly_in_free(in2, 0); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 501 | ly_ctx_destroy(ctx, NULL); |
| 502 | } |
| 503 | |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 504 | int main(void) |
| 505 | { |
| 506 | const struct CMUnitTest tests[] = { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 507 | cmocka_unit_test_setup_teardown(test_searchdirs, logger_setup, logger_teardown), |
| 508 | cmocka_unit_test_setup_teardown(test_options, logger_setup, logger_teardown), |
| 509 | cmocka_unit_test_setup_teardown(test_models, logger_setup, logger_teardown), |
Radek Krejci | b3289d6 | 2019-09-18 12:21:39 +0200 | [diff] [blame] | 510 | cmocka_unit_test_setup_teardown(test_imports, logger_setup, logger_teardown), |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 511 | cmocka_unit_test_setup_teardown(test_get_models, logger_setup, logger_teardown), |
Radek Krejci | 1e88003 | 2018-09-20 12:17:12 +0200 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 515 | } |