blob: 210f15a999ee92c0d5691376568f5dcd3f4a3587 [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()).");
87 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(NULL, 0));
88 logbuf_assert("Invalid argument ctx (ly_ctx_unset_searchdirs()).");
89 assert_int_equal(LY_EINVAL, ly_ctx_unset_searchdirs(ctx, 0));
90 logbuf_assert("Invalid argument index (ly_ctx_unset_searchdirs()).");
91
92 /* readable and executable, but not a directory */
93 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src_context"));
94 logbuf_assert("Given search directory \""TESTS_BIN"/src_context\" is not a directory.");
95 /* not executable */
96 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, TESTS_SRC"/src/context.c"));
97 logbuf_assert("Unable to use search directory \""TESTS_SRC"/src/context.c\" (Permission denied)");
98 /* not existing */
99 assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(ctx, "/nonexistingfile"));
100 logbuf_assert("Unable to use search directory \"/nonexistingfile\" (No such file or directory)");
101
102 /* ly_set_add() fails */
103 will_return(__wrap_ly_set_add, 1);
104 assert_int_equal(LY_EMEM, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
105
Radek Krejcia77e0e12018-09-20 12:39:15 +0200106 /* no change */
107 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, NULL));
108
Radek Krejci1e880032018-09-20 12:17:12 +0200109 /* correct path */
110 will_return_always(__wrap_ly_set_add, 0);
111 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
112 assert_int_equal(1, ctx->search_paths.count);
113 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
114
115 /* duplicated paths - function succeeds, but context still contains just a single path */
116 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/src"));
117 assert_int_equal(1, ctx->search_paths.count);
118 assert_string_equal(TESTS_BIN"/src", ctx->search_paths.objs[0]);
119
120 /* another paths - add 8 to fill the initial buffer of the searchpaths list */
121 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN"/CMakeFiles"));
122 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../src"));
123 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../CMakeModules"));
124 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC"/../doc"));
125 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_SRC));
126 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, TESTS_BIN));
127 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(ctx, "/tmp"));
128 assert_int_equal(8, ctx->search_paths.count);
129
130 /* get searchpaths */
131 list = ly_ctx_get_searchdirs(ctx);
132 assert_non_null(list);
133 assert_string_equal(TESTS_BIN"/src", list[0]);
134 assert_string_equal(TESTS_BIN"/CMakeFiles", list[1]);
135 assert_string_equal(TESTS_SRC, list[5]);
136 assert_string_equal(TESTS_BIN, list[6]);
137 assert_string_equal("/tmp", list[7]);
138 assert_null(list[8]);
139
140 /* removing searchpaths */
141 /* first */
142 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 0));
143 assert_string_not_equal(TESTS_BIN"/src", list[0]);
144 assert_int_equal(7, ctx->search_paths.count);
145 /* middle */
146 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 4));
147 assert_int_equal(6, ctx->search_paths.count);
148 /* last */
149 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, 5));
150 assert_int_equal(5, ctx->search_paths.count);
151 /* all */
152 assert_int_equal(LY_SUCCESS, ly_ctx_unset_searchdirs(ctx, -5));
153 assert_int_equal(0, ctx->search_paths.count);
154
155 ly_ctx_destroy(ctx, NULL);
156}
157
158int main(void)
159{
160 const struct CMUnitTest tests[] = {
161 cmocka_unit_test_setup(test_searchdirs, logger_setup),
162 };
163
164 return cmocka_run_group_tests(tests, NULL, NULL);
165}