Line data Source code
1 : /*
2 : Authors:
3 : Pavel Reichl <preichl@redhat.com>
4 :
5 : Copyright (C) 2015 Red Hat
6 :
7 : SSSD tests - id cleanup
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include <stdarg.h>
24 : #include <stdlib.h>
25 : #include <stddef.h>
26 : #include <setjmp.h>
27 : #include <unistd.h>
28 : #include <sys/types.h>
29 : #include <cmocka.h>
30 : #include <popt.h>
31 :
32 : #include "tests/cmocka/common_mock.h"
33 : #include "providers/ldap/ldap_auth.h"
34 : #include "tests/cmocka/test_expire_common.h"
35 : #include "providers/ldap/ldap_common.h"
36 : #include "providers/ldap/ldap_opts.h"
37 : #include "providers/ipa/ipa_opts.h"
38 :
39 : #define TESTS_PATH "tp_" BASE_FILE_STEM
40 : #define TEST_CONF_FILE "tests_conf.ldb"
41 :
42 : struct sysdb_test_ctx {
43 : struct sysdb_ctx *sysdb;
44 : struct confdb_ctx *confdb;
45 : struct tevent_context *ev;
46 : struct sss_domain_info *domain;
47 : struct sdap_options *opts;
48 : };
49 :
50 1 : static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
51 : {
52 : struct sysdb_test_ctx *test_ctx;
53 : char *conf_db;
54 : int ret;
55 :
56 : const char *val[2];
57 1 : val[1] = NULL;
58 :
59 : /* Create tests directory if it doesn't exist */
60 : /* (relative to current dir) */
61 1 : ret = mkdir(TESTS_PATH, 0775);
62 1 : assert_true(ret == 0 || errno == EEXIST);
63 :
64 1 : test_ctx = talloc_zero(global_talloc_context, struct sysdb_test_ctx);
65 1 : assert_non_null(test_ctx);
66 :
67 : /* Create an event context
68 : * It will not be used except in confdb_init and sysdb_init
69 : */
70 1 : test_ctx->ev = tevent_context_init(test_ctx);
71 1 : assert_non_null(test_ctx->ev);
72 :
73 1 : conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
74 1 : assert_non_null(conf_db);
75 1 : DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);
76 :
77 : /* Connect to the conf db */
78 1 : ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
79 1 : assert_int_equal(ret, EOK);
80 :
81 1 : val[0] = "LOCAL";
82 1 : ret = confdb_add_param(test_ctx->confdb, true,
83 : "config/sssd", "domains", val);
84 1 : assert_int_equal(ret, EOK);
85 :
86 1 : val[0] = "local";
87 1 : ret = confdb_add_param(test_ctx->confdb, true,
88 : "config/domain/LOCAL", "id_provider", val);
89 1 : assert_int_equal(ret, EOK);
90 :
91 1 : val[0] = enumerate ? "TRUE" : "FALSE";
92 1 : ret = confdb_add_param(test_ctx->confdb, true,
93 : "config/domain/LOCAL", "enumerate", val);
94 1 : assert_int_equal(ret, EOK);
95 :
96 1 : val[0] = "TRUE";
97 1 : ret = confdb_add_param(test_ctx->confdb, true,
98 : "config/domain/LOCAL", "cache_credentials", val);
99 1 : assert_int_equal(ret, EOK);
100 :
101 1 : ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
102 : TESTS_PATH, &test_ctx->domain);
103 1 : assert_int_equal(ret, EOK);
104 :
105 1 : test_ctx->domain->has_views = true;
106 1 : test_ctx->sysdb = test_ctx->domain->sysdb;
107 :
108 1 : *ctx = test_ctx;
109 1 : return EOK;
110 : }
111 :
112 : #define setup_sysdb_tests(ctx) _setup_sysdb_tests((ctx), false)
113 :
114 1 : static int test_sysdb_setup(void **state)
115 : {
116 : int ret;
117 : struct sysdb_test_ctx *test_ctx;
118 :
119 1 : assert_true(leak_check_setup());
120 :
121 1 : ret = setup_sysdb_tests(&test_ctx);
122 1 : assert_int_equal(ret, EOK);
123 :
124 1 : test_ctx->domain->mpg = false;
125 :
126 : /* set options */
127 1 : test_ctx->opts = talloc_zero(test_ctx, struct sdap_options);
128 1 : assert_non_null(test_ctx->opts);
129 :
130 1 : ret = sdap_copy_map(test_ctx->opts, rfc2307_user_map,
131 1 : SDAP_OPTS_USER, &test_ctx->opts->user_map);
132 1 : assert_int_equal(ret, ERR_OK);
133 :
134 1 : ret = dp_copy_defaults(test_ctx->opts, default_basic_opts,
135 1 : SDAP_OPTS_BASIC, &test_ctx->opts->basic);
136 1 : assert_int_equal(ret, ERR_OK);
137 :
138 1 : dp_opt_set_int(test_ctx->opts->basic, SDAP_ACCOUNT_CACHE_EXPIRATION, 1);
139 :
140 1 : *state = (void *) test_ctx;
141 1 : return 0;
142 : }
143 :
144 1 : static int test_sysdb_teardown(void **state)
145 : {
146 1 : struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
147 : struct sysdb_test_ctx);
148 :
149 1 : talloc_free(test_ctx);
150 1 : assert_true(leak_check_teardown());
151 1 : return 0;
152 : }
153 :
154 4 : static errno_t invalidate_group(TALLOC_CTX *ctx,
155 : struct sss_domain_info *domain,
156 : const char *name)
157 : {
158 4 : struct sysdb_attrs *sys_attrs = NULL;
159 : errno_t ret;
160 :
161 4 : sys_attrs = sysdb_new_attrs(ctx);
162 4 : if (sys_attrs) {
163 4 : ret = sysdb_attrs_add_time_t(sys_attrs,
164 : SYSDB_CACHE_EXPIRE, 1);
165 4 : if (ret == EOK) {
166 4 : ret = sysdb_set_group_attr(domain, name, sys_attrs,
167 : SYSDB_MOD_REP);
168 : } else {
169 0 : DEBUG(SSSDBG_MINOR_FAILURE,
170 : "Could not add expiration time to attributes\n");
171 : }
172 4 : talloc_zfree(sys_attrs);
173 : } else {
174 0 : DEBUG(SSSDBG_MINOR_FAILURE, "Could not create sysdb attributes\n");
175 0 : ret = ENOMEM;
176 : }
177 4 : return ret;
178 : }
179 :
180 1 : static void test_id_cleanup_exp_group(void **state)
181 : {
182 : errno_t ret;
183 : struct ldb_message *msg;
184 : struct sdap_domain sdom;
185 1 : const char *special_grp = "special_gr*o/u\\p(2016)";
186 1 : const char *empty_special_grp = "empty_gr*o/u\\p(2016)";
187 1 : const char *empty_grp = "empty_grp";
188 1 : const char *grp = "grp";
189 : /* This timeout can be bigger because we will call invalidate_group
190 : * to expire entries without waiting. */
191 1 : const uint64_t CACHE_TIMEOUT = 30;
192 1 : struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
193 : struct sysdb_test_ctx);
194 :
195 1 : ret = sysdb_store_group(test_ctx->domain, special_grp,
196 : 10002, NULL, CACHE_TIMEOUT, 0);
197 1 : assert_int_equal(ret, EOK);
198 :
199 1 : ret = sysdb_store_group(test_ctx->domain, empty_special_grp,
200 : 10003, NULL, CACHE_TIMEOUT, 0);
201 1 : assert_int_equal(ret, EOK);
202 :
203 1 : ret = sysdb_store_group(test_ctx->domain, grp,
204 : 10004, NULL, CACHE_TIMEOUT, 0);
205 1 : assert_int_equal(ret, EOK);
206 :
207 1 : ret = sysdb_store_group(test_ctx->domain, empty_grp,
208 : 10005, NULL, CACHE_TIMEOUT, 0);
209 1 : assert_int_equal(ret, EOK);
210 :
211 1 : ret = sysdb_store_user(test_ctx->domain, "test_user", NULL,
212 : 10001, 10002, "Test user",
213 : NULL, NULL, NULL, NULL, NULL,
214 : 0, 0);
215 1 : assert_int_equal(ret, EOK);
216 :
217 1 : ret = sysdb_store_user(test_ctx->domain, "test_user2", NULL,
218 : 10002, 10004, "Test user",
219 : NULL, NULL, NULL, NULL, NULL,
220 : 0, 0);
221 1 : assert_int_equal(ret, EOK);
222 :
223 1 : sdom.dom = test_ctx->domain;
224 :
225 : /* not expired */
226 1 : ret = ldap_id_cleanup(test_ctx->opts, &sdom);
227 1 : assert_int_equal(ret, EOK);
228 :
229 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
230 : special_grp, NULL, &msg);
231 1 : assert_int_equal(ret, EOK);
232 :
233 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
234 : empty_special_grp, NULL, &msg);
235 1 : assert_int_equal(ret, EOK);
236 :
237 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
238 : grp, NULL, &msg);
239 1 : assert_int_equal(ret, EOK);
240 :
241 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
242 : empty_grp, NULL, &msg);
243 1 : assert_int_equal(ret, EOK);
244 :
245 : /* let records to expire */
246 1 : invalidate_group(test_ctx, test_ctx->domain, special_grp);
247 1 : invalidate_group(test_ctx, test_ctx->domain, empty_special_grp);
248 1 : invalidate_group(test_ctx, test_ctx->domain, grp);
249 1 : invalidate_group(test_ctx, test_ctx->domain, empty_grp);
250 :
251 1 : ret = ldap_id_cleanup(test_ctx->opts, &sdom);
252 1 : assert_int_equal(ret, EOK);
253 :
254 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
255 : special_grp, NULL, &msg);
256 1 : assert_int_equal(ret, EOK);
257 :
258 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
259 : empty_special_grp, NULL, &msg);
260 1 : assert_int_equal(ret, ENOENT);
261 :
262 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
263 : grp, NULL, &msg);
264 1 : assert_int_equal(ret, EOK);
265 :
266 1 : ret = sysdb_search_group_by_name(test_ctx, test_ctx->domain,
267 : empty_grp, NULL, &msg);
268 1 : assert_int_equal(ret, ENOENT);
269 1 : }
270 :
271 1 : int main(int argc, const char *argv[])
272 : {
273 : int rv;
274 1 : int no_cleanup = 0;
275 : poptContext pc;
276 : int opt;
277 7 : struct poptOption long_options[] = {
278 : POPT_AUTOHELP
279 5 : SSSD_DEBUG_OPTS
280 : { "no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
281 1 : _("Do not delete the test database after a test run"), NULL },
282 : POPT_TABLEEND
283 : };
284 :
285 1 : const struct CMUnitTest tests[] = {
286 : cmocka_unit_test_setup_teardown(test_id_cleanup_exp_group,
287 : test_sysdb_setup, test_sysdb_teardown),
288 : };
289 :
290 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
291 1 : debug_level = SSSDBG_INVALID;
292 :
293 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
294 1 : while ((opt = poptGetNextOpt(pc)) != -1) {
295 : switch (opt) {
296 : default:
297 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
298 : poptBadOption(pc, 0), poptStrerror(opt));
299 0 : poptPrintUsage(pc, stderr, 0);
300 0 : return 1;
301 : }
302 : }
303 1 : poptFreeContext(pc);
304 :
305 1 : DEBUG_CLI_INIT(debug_level);
306 :
307 1 : tests_set_cwd();
308 1 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
309 1 : test_dom_suite_setup(TESTS_PATH);
310 1 : rv = cmocka_run_group_tests(tests, NULL, NULL);
311 :
312 1 : if (rv == 0 && no_cleanup == 0) {
313 1 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
314 : }
315 1 : return rv;
316 : }
|