blob: 51232c7885f29b915bc84aa2842e382590d11cd1 [file] [log] [blame]
Radek Krejci1e880032018-09-20 12:17:12 +02001/*
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"
16#include "../../src/context.c"
17
18#include <stdarg.h>
19#include <stddef.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
23#include <string.h>
24#include <stdio.h>
25
26#include "libyang.h"
27
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34static void
35logger(LY_LOG_LEVEL level, const char *msg, const char *path)
36{
37 (void) level; /* unused */
38 (void) path; /* unused */
39
40 strncpy(logbuf, msg, BUFSIZE - 1);
41}
42
43static int
44logger_setup(void **state)
45{
46 (void) state; /* unused */
47#if ENABLE_LOGGER_CHECKING
48 ly_set_log_clb(logger, 0);
49#endif
50 return 0;
51}
52
53#if ENABLE_LOGGER_CHECKING
54# define logbuf_assert(str) assert_string_equal(logbuf, str)
55#else
56# define logbuf_assert(str)
57#endif
58
59int __real_ly_set_add(struct ly_set *set, void *object, int options);
60int __wrap_ly_set_add(struct ly_set *set, void *object, int options)
61{
62 int wrap = mock_type(int);
63
64 if (wrap) {
65 /* error */
66 return -1;
67 } else {
68 return __real_ly_set_add(set, object, options);
69 }
70}
71
72static void
73test_searchdirs(void **state)
74{
75 (void) state; /* unused */
76
77 struct ly_ctx *ctx;
78 const char * const *list;
79
80 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
81
82 /* invalid arguments */
83 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(NULL, NULL));
84 logbuf_assert("Invalid argument ctx (ly_ctx_set_searchdir()).");
85 assert_null(ly_ctx_get_searchdirs(NULL));
86 logbuf_assert("Invalid argument ctx (ly_ctx_get_searchdirs()).");
Radek Krejci0759b792018-09-20 13:53:15 +020087 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +020088 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +020089
90 /* readable and executable, but not a directory */
91 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context"));
92 logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory.");
93 /* not executable */
94 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_SRC"/src/context.c"));
95 logbuf_assert("Unable to use search directory \""TESTS_SRC"/src/context.c\" (Permission denied)");
96 /* not existing */
97 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
98 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
99
100 /* ly_set_add() fails */
101 will_return(__wrap_ly_set_add, 1);
102 assert_int_equal(LY_EMEM, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
103
Radek Krejcia77e0e12018-09-20 12:39:15 +0200104 /* no change */
105 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
106
Radek Krejci1e880032018-09-20 12:17:12 +0200107 /* correct path */
108 will_return_always(__wrap_ly_set_add, 0);
109 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
110 assert_int_equal(1, ctx->search_paths.count);
111 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
112
Radek Krejci14946ab2018-09-20 13:42:06 +0200113 /* duplicated paths */
114 assert_int_equal(LY_EEXIST, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200115 assert_int_equal(1, ctx->search_paths.count);
116 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
117
118 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
119 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
120 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
121 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
122 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/tmp"));
126 assert_int_equal(8, ctx->search_paths.count);
127
128 /* get searchpaths */
129 list = ly_ctx_get_searchdirs(ctx);
130 assert_non_null(list);
131 assert_string_equal(TESTS_BIN"/src", list[0]);
132 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
133 assert_string_equal(TESTS_SRC, list[5]);
134 assert_string_equal(TESTS_BIN, list[6]);
135 assert_string_equal("/tmp", list[7]);
136 assert_null(list[8]);
137
138 /* removing searchpaths */
Radek Krejci0759b792018-09-20 13:53:15 +0200139 /* nonexisting */
140 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, "/nonexistingfile"));
141 logbuf_assert("Invalid argument value (ly_ctx_unset_searchdirs()).");
Radek Krejci1e880032018-09-20 12:17:12 +0200142 /* first */
Radek Krejci0759b792018-09-20 13:53:15 +0200143 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_BIN"/src"));
Radek Krejci1e880032018-09-20 12:17:12 +0200144 assert_string_not_equal(TESTS_BIN"/src", list[0]);
145 assert_int_equal(7, ctx->search_paths.count);
146 /* middle */
Radek Krejci0759b792018-09-20 13:53:15 +0200147 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, TESTS_SRC));
Radek Krejci1e880032018-09-20 12:17:12 +0200148 assert_int_equal(6, ctx->search_paths.count);
149 /* last */
Radek Krejci0759b792018-09-20 13:53:15 +0200150 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, "/tmp"));
Radek Krejci1e880032018-09-20 12:17:12 +0200151 assert_int_equal(5, ctx->search_paths.count);
152 /* all */
Radek Krejci0759b792018-09-20 13:53:15 +0200153 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci1e880032018-09-20 12:17:12 +0200154 assert_int_equal(0, ctx->search_paths.count);
155
Radek Krejci555c9bb2018-09-20 12:45:13 +0200156 /* again - no change */
Radek Krejci0759b792018-09-20 13:53:15 +0200157 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, NULL));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200158
159 /* cleanup */
160 ly_ctx_destroy(ctx, NULL);
161
162 /* test searchdir list in ly_ctx_new() */
163 assert_int_equal(LY_EINVAL, ly_ctx_new("/nonexistingfile", 0, &ctx));
164 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
Radek Krejci14946ab2018-09-20 13:42:06 +0200165 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_SRC":/tmp:/tmp", 0, &ctx));
Radek Krejci555c9bb2018-09-20 12:45:13 +0200166 assert_int_equal(2, ctx->search_paths.count);
167 assert_string_equal(TESTS_SRC, ctx->search_paths.objs[0]);
168 assert_string_equal("/tmp", ctx->search_paths.objs[1]);
169
170 /* cleanup */
Radek Krejci1e880032018-09-20 12:17:12 +0200171 ly_ctx_destroy(ctx, NULL);
172}
173
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200174static void
175test_options(void **state)
176{
177 (void) state; /* unused */
178
179 struct ly_ctx *ctx;
180 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0xffffffff, &ctx));
181
Radek Krejci962a8102018-09-20 14:21:06 +0200182 /* invalid arguments */
183 assert_int_equal(0, ly_ctx_get_options(NULL));
184 logbuf_assert("Invalid argument ctx (ly_ctx_get_options()).");
185
186 assert_int_equal(LY_EINVAL, ly_ctx_set_option(NULL, 0));
187 logbuf_assert("Invalid argument ctx (ly_ctx_set_option()).");
188 assert_int_equal(LY_EINVAL, ly_ctx_unset_option(NULL, 0));
189 logbuf_assert("Invalid argument ctx (ly_ctx_unset_option()).");
190
191 /* option not allowed to be changed */
192 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
193 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
194 assert_int_equal(LY_EINVAL, ly_ctx_set_option(ctx, LY_CTX_NOYANGLIBRARY));
195 logbuf_assert("Invalid argument option (ly_ctx_set_option()).");
196
197
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200198 /* unset */
199 /* LY_CTX_ALLIMPLEMENTED */
200 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200201 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200202 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
203
204 /* LY_CTX_DISABLE_SEARCHDIRS */
205 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200206 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200207 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
208
209 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
210 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
Radek Krejci962a8102018-09-20 14:21:06 +0200211 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200212 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
213
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200214 /* LY_CTX_PREFER_SEARCHDIRS */
215 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
Radek Krejci962a8102018-09-20 14:21:06 +0200216 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200217 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
218
219 /* LY_CTX_TRUSTED */
220 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
Radek Krejci962a8102018-09-20 14:21:06 +0200221 assert_int_equal(LY_SUCCESS, ly_ctx_unset_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200222 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
223
Radek Krejci962a8102018-09-20 14:21:06 +0200224 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
225
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200226 /* set back */
227 /* LY_CTX_ALLIMPLEMENTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200228 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_ALLIMPLEMENTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200229 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
230
231 /* LY_CTX_DISABLE_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200232 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200233 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
234
235 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
Radek Krejci962a8102018-09-20 14:21:06 +0200236 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_DISABLE_SEARCHDIR_CWD));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200237 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
238
239 /* LY_CTX_PREFER_SEARCHDIRS */
Radek Krejci962a8102018-09-20 14:21:06 +0200240 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_PREFER_SEARCHDIRS));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200241 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
242
243 /* LY_CTX_TRUSTED */
Radek Krejci962a8102018-09-20 14:21:06 +0200244 assert_int_equal(LY_SUCCESS, ly_ctx_set_option(ctx, LY_CTX_TRUSTED));
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200245 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
246
Radek Krejci962a8102018-09-20 14:21:06 +0200247 assert_int_equal(ctx->flags, ly_ctx_get_options(ctx));
248
249 /* cleanup */
250 ly_ctx_destroy(ctx, NULL);
251}
252
253static void
254test_models(void **state)
255{
256 (void) state; /* unused */
257
258 /* invalid arguments */
259 assert_int_equal(0, ly_ctx_get_module_set_id(NULL));
260 logbuf_assert("Invalid argument ctx (ly_ctx_get_module_set_id()).");
261
262 struct ly_ctx *ctx;
263 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
264 assert_int_equal(ctx->module_set_id, ly_ctx_get_module_set_id(ctx));
265
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200266 /* cleanup */
267 ly_ctx_destroy(ctx, NULL);
268}
269
Radek Krejci1e880032018-09-20 12:17:12 +0200270int main(void)
271{
272 const struct CMUnitTest tests[] = {
273 cmocka_unit_test_setup(test_searchdirs, logger_setup),
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200274 cmocka_unit_test_setup(test_options, logger_setup),
Radek Krejci962a8102018-09-20 14:21:06 +0200275 cmocka_unit_test_setup(test_models, logger_setup),
Radek Krejci1e880032018-09-20 12:17:12 +0200276 };
277
278 return cmocka_run_group_tests(tests, NULL, NULL);
279}