blob: 36eb241145e177747c770275128629605ab8dad4 [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
182 /* unset */
183 /* LY_CTX_ALLIMPLEMENTED */
184 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
185 ly_ctx_unset_allimplemented(ctx);
186 assert_int_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
187
188 /* LY_CTX_DISABLE_SEARCHDIRS */
189 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
190 ly_ctx_unset_disable_searchdirs(ctx);
191 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
192
193 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
194 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
195 ly_ctx_unset_disable_searchdir_cwd(ctx);
196 assert_int_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
197
198 /* LY_CTX_NOYANGLIBRARY (not possible to unset) */
199 assert_int_not_equal(0, ctx->flags & LY_CTX_NOYANGLIBRARY);
200
201 /* LY_CTX_PREFER_SEARCHDIRS */
202 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
203 ly_ctx_unset_prefer_searchdirs(ctx);
204 assert_int_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
205
206 /* LY_CTX_TRUSTED */
207 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
208 ly_ctx_unset_trusted(ctx);
209 assert_int_equal(0, ctx->flags & LY_CTX_TRUSTED);
210
211 /* set back */
212 /* LY_CTX_ALLIMPLEMENTED */
213 ly_ctx_set_allimplemented(ctx);
214 assert_int_not_equal(0, ctx->flags & LY_CTX_ALLIMPLEMENTED);
215
216 /* LY_CTX_DISABLE_SEARCHDIRS */
217 ly_ctx_set_disable_searchdirs(ctx);
218 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIRS);
219
220 /* LY_CTX_DISABLE_SEARCHDIR_CWD */
221 ly_ctx_set_disable_searchdir_cwd(ctx);
222 assert_int_not_equal(0, ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD);
223
224 /* LY_CTX_PREFER_SEARCHDIRS */
225 ly_ctx_set_prefer_searchdirs(ctx);
226 assert_int_not_equal(0, ctx->flags & LY_CTX_PREFER_SEARCHDIRS);
227
228 /* LY_CTX_TRUSTED */
229 ly_ctx_set_trusted(ctx);
230 assert_int_not_equal(0, ctx->flags & LY_CTX_TRUSTED);
231
232 /* cleanup */
233 ly_ctx_destroy(ctx, NULL);
234}
235
Radek Krejci1e880032018-09-20 12:17:12 +0200236int main(void)
237{
238 const struct CMUnitTest tests[] = {
239 cmocka_unit_test_setup(test_searchdirs, logger_setup),
Radek Krejci0c4b22b2018-09-20 12:55:46 +0200240 cmocka_unit_test_setup(test_options, logger_setup),
Radek Krejci1e880032018-09-20 12:17:12 +0200241 };
242
243 return cmocka_run_group_tests(tests, NULL, NULL);
244}