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