Line data Source code
1 : /*
2 : Authors:
3 : Pavel Březina <pbrezina@redhat.com>
4 :
5 : Copyright (C) 2014 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <talloc.h>
22 : #include <tevent.h>
23 : #include <errno.h>
24 : #include <popt.h>
25 :
26 : #include "tests/cmocka/common_mock.h"
27 : #include "tests/cmocka/common_mock_resp.h"
28 : #include "db/sysdb.h"
29 : #include "responder/common/responder_cache_req.h"
30 :
31 : #define TESTS_PATH "tp_" BASE_FILE_STEM
32 : #define TEST_CONF_DB "test_responder_cache_req_conf.ldb"
33 : #define TEST_DOM_NAME "responder_cache_req_test"
34 : #define TEST_ID_PROVIDER "ldap"
35 :
36 : #define TEST_USER_NAME "test-user"
37 : #define TEST_UPN "upn@upndomain.com"
38 : #define TEST_USER_ID 1000
39 : #define TEST_GROUP_NAME "test-group"
40 : #define TEST_GROUP_ID 1000
41 :
42 : #define TEST_USER_NAME2 "test-user2"
43 : #define TEST_GROUP_NAME2 "test-group2"
44 :
45 : #define new_single_domain_test(test) \
46 : cmocka_unit_test_setup_teardown(test_ ## test, \
47 : test_single_domain_setup, \
48 : test_single_domain_teardown)
49 :
50 : #define new_multi_domain_test(test) \
51 : cmocka_unit_test_setup_teardown(test_ ## test, \
52 : test_multi_domain_setup, \
53 : test_multi_domain_teardown)
54 :
55 : #define run_cache_req(ctx, send_fn, done_fn, dom, crp, lookup, expret) do { \
56 : TALLOC_CTX *req_mem_ctx; \
57 : struct tevent_req *req; \
58 : errno_t ret; \
59 : \
60 : req_mem_ctx = talloc_new(global_talloc_context); \
61 : check_leaks_push(req_mem_ctx); \
62 : \
63 : req = send_fn(req_mem_ctx, ctx->tctx->ev, ctx->rctx, \
64 : ctx->ncache, 10, crp, \
65 : (dom == NULL ? NULL : dom->name), lookup); \
66 : assert_non_null(req); \
67 : tevent_req_set_callback(req, done_fn, ctx); \
68 : \
69 : ret = test_ev_loop(ctx->tctx); \
70 : assert_int_equal(ret, expret); \
71 : assert_true(check_leaks_pop(req_mem_ctx)); \
72 : \
73 : talloc_free(req_mem_ctx); \
74 : } while (0)
75 :
76 : struct cache_req_test_ctx {
77 : struct sss_test_ctx *tctx;
78 : struct resp_ctx *rctx;
79 : struct sss_nc_ctx *ncache;
80 :
81 : struct ldb_result *result;
82 : struct sss_domain_info *domain;
83 : char *name;
84 : bool dp_called;
85 : bool create_user;
86 : bool create_group;
87 : };
88 :
89 : const char *domains[] = {"responder_cache_req_test_a",
90 : "responder_cache_req_test_b",
91 : "responder_cache_req_test_c",
92 : "responder_cache_req_test_d",
93 : NULL};
94 :
95 0 : struct cli_protocol_version *register_cli_protocol_version(void)
96 : {
97 : static struct cli_protocol_version version[] = {
98 : { 0, NULL, NULL }
99 : };
100 :
101 0 : return version;
102 : }
103 :
104 17 : static void cache_req_user_by_name_test_done(struct tevent_req *req)
105 : {
106 17 : struct cache_req_test_ctx *ctx = NULL;
107 :
108 17 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
109 :
110 17 : ctx->tctx->error = cache_req_user_by_name_recv(ctx, req,
111 : &ctx->result,
112 : &ctx->domain,
113 : &ctx->name);
114 17 : talloc_zfree(req);
115 :
116 17 : ctx->tctx->done = true;
117 17 : }
118 :
119 8 : static void cache_req_user_by_id_test_done(struct tevent_req *req)
120 : {
121 8 : struct cache_req_test_ctx *ctx = NULL;
122 :
123 8 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
124 :
125 8 : ctx->tctx->error = cache_req_user_by_id_recv(ctx, req,
126 : &ctx->result, &ctx->domain);
127 8 : talloc_zfree(req);
128 :
129 8 : ctx->tctx->done = true;
130 8 : }
131 :
132 9 : static void cache_req_group_by_name_test_done(struct tevent_req *req)
133 : {
134 9 : struct cache_req_test_ctx *ctx = NULL;
135 :
136 9 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
137 :
138 9 : ctx->tctx->error = cache_req_group_by_name_recv(ctx, req,
139 : &ctx->result,
140 : &ctx->domain,
141 : &ctx->name);
142 9 : talloc_zfree(req);
143 :
144 9 : ctx->tctx->done = true;
145 9 : }
146 :
147 8 : static void cache_req_group_by_id_test_done(struct tevent_req *req)
148 : {
149 8 : struct cache_req_test_ctx *ctx = NULL;
150 :
151 8 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
152 :
153 8 : ctx->tctx->error = cache_req_group_by_id_recv(ctx, req,
154 : &ctx->result, &ctx->domain);
155 8 : talloc_zfree(req);
156 :
157 8 : ctx->tctx->done = true;
158 8 : }
159 :
160 17 : static void prepare_user(TALLOC_CTX *mem_ctx,
161 : struct sss_domain_info *domain,
162 : uint64_t timeout,
163 : time_t time)
164 : {
165 : struct sysdb_attrs *attrs;
166 : errno_t ret;
167 :
168 17 : attrs = sysdb_new_attrs(mem_ctx);
169 17 : assert_non_null(attrs);
170 :
171 17 : ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, TEST_UPN);
172 17 : assert_int_equal(ret, EOK);
173 :
174 17 : ret = sysdb_store_user(domain, TEST_USER_NAME, "pwd",
175 : TEST_USER_ID, TEST_GROUP_ID, NULL, NULL, NULL,
176 : "cn=test-user,dc=test", attrs, NULL,
177 : timeout, time);
178 17 : assert_int_equal(ret, EOK);
179 17 : }
180 :
181 8 : static void run_user_by_name(struct cache_req_test_ctx *test_ctx,
182 : struct sss_domain_info *domain,
183 : int cache_refresh_percent,
184 : errno_t exp_ret)
185 : {
186 8 : run_cache_req(test_ctx, cache_req_user_by_name_send,
187 : cache_req_user_by_name_test_done, domain,
188 : cache_refresh_percent, TEST_USER_NAME, exp_ret);
189 8 : }
190 :
191 8 : static void run_user_by_upn(struct cache_req_test_ctx *test_ctx,
192 : struct sss_domain_info *domain,
193 : int cache_refresh_percent,
194 : errno_t exp_ret)
195 : {
196 8 : run_cache_req(test_ctx, cache_req_user_by_name_send,
197 : cache_req_user_by_name_test_done, domain,
198 : cache_refresh_percent, TEST_UPN, exp_ret);
199 8 : }
200 :
201 8 : static void run_user_by_id(struct cache_req_test_ctx *test_ctx,
202 : struct sss_domain_info *domain,
203 : int cache_refresh_percent,
204 : errno_t exp_ret)
205 : {
206 8 : run_cache_req(test_ctx, cache_req_user_by_id_send,
207 : cache_req_user_by_id_test_done, domain,
208 : cache_refresh_percent, TEST_USER_ID, exp_ret);
209 8 : }
210 :
211 16 : static void check_user(struct cache_req_test_ctx *test_ctx,
212 : struct sss_domain_info *exp_dom)
213 : {
214 : const char *ldbname;
215 : const char *ldbupn;
216 : uid_t ldbuid;
217 :
218 16 : assert_non_null(test_ctx->result);
219 16 : assert_int_equal(test_ctx->result->count, 1);
220 16 : assert_non_null(test_ctx->result->msgs);
221 16 : assert_non_null(test_ctx->result->msgs[0]);
222 :
223 16 : ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
224 : SYSDB_NAME, NULL);
225 16 : assert_non_null(ldbname);
226 16 : assert_string_equal(ldbname, TEST_USER_NAME);
227 :
228 16 : ldbupn = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
229 : SYSDB_UPN, NULL);
230 16 : assert_non_null(ldbupn);
231 16 : assert_string_equal(ldbupn, TEST_UPN);
232 :
233 16 : ldbuid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
234 : SYSDB_UIDNUM, 0);
235 16 : assert_int_equal(ldbuid, TEST_USER_ID);
236 :
237 16 : assert_non_null(test_ctx->domain);
238 16 : assert_string_equal(exp_dom->name, test_ctx->domain->name);
239 16 : }
240 :
241 9 : static void prepare_group(TALLOC_CTX *mem_ctx,
242 : struct sss_domain_info *domain,
243 : uint64_t timeout,
244 : time_t time)
245 : {
246 : errno_t ret;
247 :
248 9 : ret = sysdb_store_group(domain, TEST_GROUP_NAME, TEST_GROUP_ID, NULL,
249 : timeout, time);
250 9 : assert_int_equal(ret, EOK);
251 9 : }
252 :
253 8 : static void run_group_by_name(struct cache_req_test_ctx *test_ctx,
254 : struct sss_domain_info *domain,
255 : int cache_refresh_percent,
256 : errno_t exp_ret)
257 : {
258 8 : run_cache_req(test_ctx, cache_req_group_by_name_send,
259 : cache_req_group_by_name_test_done, domain,
260 : cache_refresh_percent, TEST_GROUP_NAME, exp_ret);
261 8 : }
262 :
263 8 : static void run_group_by_id(struct cache_req_test_ctx *test_ctx,
264 : struct sss_domain_info *domain,
265 : int cache_refresh_percent,
266 : errno_t exp_ret)
267 : {
268 8 : run_cache_req(test_ctx, cache_req_group_by_id_send,
269 : cache_req_group_by_id_test_done, domain,
270 : cache_refresh_percent, TEST_GROUP_ID, exp_ret);
271 8 : }
272 :
273 11 : static void check_group(struct cache_req_test_ctx *test_ctx,
274 : struct sss_domain_info *exp_dom)
275 : {
276 : const char *ldbname;
277 : gid_t ldbgid;
278 :
279 11 : assert_non_null(test_ctx->result);
280 11 : assert_int_equal(test_ctx->result->count, 1);
281 11 : assert_non_null(test_ctx->result->msgs);
282 11 : assert_non_null(test_ctx->result->msgs[0]);
283 :
284 11 : ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
285 : SYSDB_NAME, NULL);
286 11 : assert_non_null(ldbname);
287 11 : assert_string_equal(ldbname, TEST_GROUP_NAME);
288 :
289 11 : ldbgid = ldb_msg_find_attr_as_uint(test_ctx->result->msgs[0],
290 : SYSDB_GIDNUM, 0);
291 11 : assert_int_equal(ldbgid, TEST_USER_ID);
292 :
293 11 : assert_non_null(test_ctx->domain);
294 11 : assert_string_equal(exp_dom->name, test_ctx->domain->name);
295 11 : }
296 :
297 :
298 : struct tevent_req *
299 60 : __wrap_sss_dp_get_account_send(TALLOC_CTX *mem_ctx,
300 : struct resp_ctx *rctx,
301 : struct sss_domain_info *dom,
302 : bool fast_reply,
303 : enum sss_dp_acct_type type,
304 : const char *opt_name,
305 : uint32_t opt_id,
306 : const char *extra)
307 : {
308 60 : struct cache_req_test_ctx *ctx = NULL;
309 : errno_t ret;
310 :
311 60 : ctx = sss_mock_ptr_type(struct cache_req_test_ctx*);
312 60 : ctx->dp_called = true;
313 :
314 60 : if (ctx->create_user) {
315 4 : prepare_user(ctx, ctx->tctx->dom, 1000, time(NULL));
316 : }
317 :
318 60 : if (ctx->create_group) {
319 2 : ret = sysdb_store_group(ctx->tctx->dom, TEST_GROUP_NAME,
320 : TEST_GROUP_ID, NULL, 1000, time(NULL));
321 2 : assert_int_equal(ret, EOK);
322 : }
323 :
324 60 : return test_req_succeed_send(mem_ctx, rctx->ev);
325 : }
326 :
327 33 : static int test_single_domain_setup(void **state)
328 : {
329 33 : struct cache_req_test_ctx *test_ctx = NULL;
330 : errno_t ret;
331 :
332 33 : test_dom_suite_setup(TESTS_PATH);
333 :
334 33 : test_ctx = talloc_zero(NULL, struct cache_req_test_ctx);
335 33 : assert_non_null(test_ctx);
336 33 : *state = test_ctx;
337 :
338 33 : test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH, TEST_CONF_DB,
339 : TEST_DOM_NAME, TEST_ID_PROVIDER, NULL);
340 33 : assert_non_null(test_ctx->tctx);
341 :
342 33 : test_ctx->rctx = mock_rctx(test_ctx, test_ctx->tctx->ev,
343 33 : test_ctx->tctx->dom, NULL);
344 33 : assert_non_null(test_ctx->rctx);
345 :
346 33 : ret = sss_ncache_init(test_ctx, &test_ctx->ncache);
347 33 : assert_int_equal(ret, EOK);
348 33 : return 0;
349 : }
350 :
351 33 : static int test_single_domain_teardown(void **state)
352 : {
353 33 : talloc_zfree(*state);
354 33 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
355 33 : return 0;
356 : }
357 :
358 14 : static int test_multi_domain_setup(void **state)
359 : {
360 14 : struct cache_req_test_ctx *test_ctx = NULL;
361 : errno_t ret;
362 :
363 14 : test_dom_suite_setup(TESTS_PATH);
364 :
365 14 : test_ctx = talloc_zero(NULL, struct cache_req_test_ctx);
366 14 : assert_non_null(test_ctx);
367 14 : *state = test_ctx;
368 :
369 14 : test_ctx->tctx = create_multidom_test_ctx(test_ctx, TESTS_PATH,
370 : TEST_CONF_DB, domains,
371 : TEST_ID_PROVIDER, NULL);
372 14 : assert_non_null(test_ctx->tctx);
373 :
374 14 : test_ctx->rctx = mock_rctx(test_ctx, test_ctx->tctx->ev,
375 14 : test_ctx->tctx->dom, NULL);
376 14 : assert_non_null(test_ctx->rctx);
377 :
378 14 : ret = sss_ncache_init(test_ctx, &test_ctx->ncache);
379 14 : assert_int_equal(ret, EOK);
380 14 : return 0;
381 : }
382 :
383 14 : static int test_multi_domain_teardown(void **state)
384 : {
385 14 : talloc_zfree(*state);
386 14 : test_multidom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, domains);
387 14 : return 0;
388 : }
389 :
390 1 : void test_user_by_name_multiple_domains_found(void **state)
391 : {
392 1 : struct cache_req_test_ctx *test_ctx = NULL;
393 1 : struct sss_domain_info *domain = NULL;
394 :
395 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
396 :
397 : /* Setup user. */
398 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
399 : "responder_cache_req_test_d", true);
400 1 : assert_non_null(domain);
401 :
402 1 : prepare_user(test_ctx, domain, 1000, time(NULL));
403 :
404 : /* Mock values. */
405 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
406 1 : will_return_always(sss_dp_get_account_recv, 0);
407 1 : mock_parse_inp(TEST_USER_NAME, NULL, ERR_OK);
408 :
409 : /* Test. */
410 1 : run_user_by_name(test_ctx, NULL, 0, ERR_OK);
411 1 : assert_true(test_ctx->dp_called);
412 1 : check_user(test_ctx, domain);
413 1 : }
414 :
415 1 : void test_user_by_name_multiple_domains_notfound(void **state)
416 : {
417 1 : struct cache_req_test_ctx *test_ctx = NULL;
418 :
419 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
420 :
421 : /* Mock values. */
422 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
423 1 : will_return_always(sss_dp_get_account_recv, 0);
424 1 : mock_parse_inp(TEST_USER_NAME, NULL, ERR_OK);
425 :
426 : /* Test. */
427 1 : run_user_by_name(test_ctx, NULL, 0, ENOENT);
428 1 : assert_true(test_ctx->dp_called);
429 1 : }
430 :
431 1 : void test_user_by_name_multiple_domains_parse(void **state)
432 : {
433 1 : struct cache_req_test_ctx *test_ctx = NULL;
434 1 : struct sss_domain_info *domain = NULL;
435 1 : TALLOC_CTX *req_mem_ctx = NULL;
436 1 : struct tevent_req *req = NULL;
437 1 : const char *name = TEST_USER_NAME;
438 1 : const char *fqn = NULL;
439 : errno_t ret;
440 :
441 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
442 :
443 : /* Add user to the first domain with different uid then test user. */
444 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
445 : "responder_cache_req_test_a", true);
446 1 : assert_non_null(domain);
447 :
448 1 : ret = sysdb_store_user(domain, name, "pwd", 2000, 1000,
449 : NULL, NULL, NULL, "cn=test-user,dc=test", NULL,
450 : NULL, 1000, time(NULL));
451 1 : assert_int_equal(ret, EOK);
452 :
453 : /* Add test user to the last domain. */
454 :
455 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
456 : "responder_cache_req_test_d", true);
457 1 : assert_non_null(domain);
458 :
459 1 : prepare_user(test_ctx, domain, 1000, time(NULL));
460 :
461 : /* Append domain name to the username. */
462 1 : fqn = talloc_asprintf(test_ctx, "%s@%s", name,
463 : "responder_cache_req_test_d");
464 1 : assert_non_null(fqn);
465 :
466 : /* Mock values. */
467 1 : mock_parse_inp(name, "responder_cache_req_test_d", ERR_OK);
468 :
469 :
470 : /* Test. */
471 1 : req_mem_ctx = talloc_new(global_talloc_context);
472 1 : check_leaks_push(req_mem_ctx);
473 :
474 1 : req = cache_req_user_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
475 : test_ctx->rctx, test_ctx->ncache, 10, 0,
476 : NULL, fqn);
477 1 : assert_non_null(req);
478 1 : tevent_req_set_callback(req, cache_req_user_by_name_test_done, test_ctx);
479 :
480 1 : ret = test_ev_loop(test_ctx->tctx);
481 1 : assert_int_equal(ret, ERR_OK);
482 1 : assert_true(check_leaks_pop(req_mem_ctx));
483 1 : assert_false(test_ctx->dp_called);
484 :
485 1 : check_user(test_ctx, domain);
486 :
487 1 : assert_non_null(test_ctx->name);
488 1 : assert_string_equal(name, test_ctx->name);
489 1 : }
490 :
491 1 : void test_user_by_name_cache_valid(void **state)
492 : {
493 1 : struct cache_req_test_ctx *test_ctx = NULL;
494 :
495 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
496 :
497 : /* Setup user. */
498 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
499 :
500 : /* Test. */
501 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
502 1 : check_user(test_ctx, test_ctx->tctx->dom);
503 1 : }
504 :
505 1 : void test_user_by_name_cache_expired(void **state)
506 : {
507 1 : struct cache_req_test_ctx *test_ctx = NULL;
508 :
509 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
510 :
511 : /* Setup user. */
512 1 : prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
513 :
514 : /* Mock values. */
515 : /* DP should be contacted */
516 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
517 1 : mock_account_recv_simple();
518 :
519 : /* Test. */
520 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
521 1 : assert_true(test_ctx->dp_called);
522 1 : check_user(test_ctx, test_ctx->tctx->dom);
523 1 : }
524 :
525 1 : void test_user_by_name_cache_midpoint(void **state)
526 : {
527 1 : struct cache_req_test_ctx *test_ctx = NULL;
528 :
529 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
530 :
531 : /* Setup user. */
532 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
533 :
534 : /* Mock values. */
535 : /* DP should be contacted without callback */
536 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
537 :
538 : /* Test. */
539 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
540 1 : assert_true(test_ctx->dp_called);
541 1 : check_user(test_ctx, test_ctx->tctx->dom);
542 1 : }
543 :
544 1 : void test_user_by_name_ncache(void **state)
545 : {
546 1 : struct cache_req_test_ctx *test_ctx = NULL;
547 : errno_t ret;
548 :
549 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
550 :
551 : /* Setup user. */
552 1 : ret = sss_ncache_set_user(test_ctx->ncache, false,
553 1 : test_ctx->tctx->dom, TEST_USER_NAME);
554 1 : assert_int_equal(ret, EOK);
555 :
556 : /* Test. */
557 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
558 1 : assert_false(test_ctx->dp_called);
559 1 : }
560 :
561 1 : void test_user_by_name_missing_found(void **state)
562 : {
563 1 : struct cache_req_test_ctx *test_ctx = NULL;
564 :
565 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
566 :
567 : /* Mock values. */
568 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
569 1 : mock_account_recv_simple();
570 :
571 1 : test_ctx->create_user = true;
572 :
573 : /* Test. */
574 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
575 1 : assert_true(test_ctx->dp_called);
576 1 : check_user(test_ctx, test_ctx->tctx->dom);
577 1 : }
578 :
579 1 : void test_user_by_name_missing_notfound(void **state)
580 : {
581 1 : struct cache_req_test_ctx *test_ctx = NULL;
582 :
583 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
584 :
585 : /* Mock values. */
586 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
587 1 : mock_account_recv_simple();
588 :
589 : /* Test. */
590 1 : run_user_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
591 1 : assert_true(test_ctx->dp_called);
592 1 : }
593 :
594 1 : void test_user_by_upn_multiple_domains_found(void **state)
595 : {
596 1 : struct cache_req_test_ctx *test_ctx = NULL;
597 1 : struct sss_domain_info *domain = NULL;
598 :
599 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
600 :
601 : /* Setup user. */
602 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
603 : "responder_cache_req_test_d", true);
604 1 : assert_non_null(domain);
605 :
606 1 : prepare_user(test_ctx, domain, 1000, time(NULL));
607 :
608 : /* Mock values. */
609 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
610 1 : will_return_always(sss_dp_get_account_recv, 0);
611 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
612 :
613 : /* Test. */
614 1 : run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
615 1 : assert_true(test_ctx->dp_called);
616 1 : check_user(test_ctx, domain);
617 1 : }
618 :
619 1 : void test_user_by_upn_multiple_domains_notfound(void **state)
620 : {
621 1 : struct cache_req_test_ctx *test_ctx = NULL;
622 :
623 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
624 :
625 : /* Mock values. */
626 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
627 1 : will_return_always(sss_dp_get_account_recv, 0);
628 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
629 :
630 : /* Test. */
631 1 : run_user_by_upn(test_ctx, NULL, 0, ENOENT);
632 1 : assert_true(test_ctx->dp_called);
633 1 : }
634 :
635 1 : void test_user_by_upn_cache_valid(void **state)
636 : {
637 1 : struct cache_req_test_ctx *test_ctx = NULL;
638 :
639 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
640 :
641 : /* Setup user. */
642 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
643 :
644 : /* Mock values. */
645 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
646 :
647 : /* Test. */
648 1 : run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
649 1 : check_user(test_ctx, test_ctx->tctx->dom);
650 1 : }
651 :
652 1 : void test_user_by_upn_cache_expired(void **state)
653 : {
654 1 : struct cache_req_test_ctx *test_ctx = NULL;
655 :
656 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
657 :
658 : /* Setup user. */
659 1 : prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
660 :
661 : /* Mock values. */
662 : /* DP should be contacted */
663 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
664 1 : mock_account_recv_simple();
665 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
666 :
667 : /* Test. */
668 1 : run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
669 1 : assert_true(test_ctx->dp_called);
670 1 : check_user(test_ctx, test_ctx->tctx->dom);
671 1 : }
672 :
673 1 : void test_user_by_upn_cache_midpoint(void **state)
674 : {
675 1 : struct cache_req_test_ctx *test_ctx = NULL;
676 :
677 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
678 :
679 : /* Setup user. */
680 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
681 :
682 : /* Mock values. */
683 : /* DP should be contacted without callback */
684 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
685 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
686 :
687 : /* Test. */
688 1 : run_user_by_upn(test_ctx, NULL, 50, ERR_OK);
689 1 : assert_true(test_ctx->dp_called);
690 1 : check_user(test_ctx, test_ctx->tctx->dom);
691 1 : }
692 :
693 1 : void test_user_by_upn_ncache(void **state)
694 : {
695 1 : struct cache_req_test_ctx *test_ctx = NULL;
696 : errno_t ret;
697 :
698 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
699 :
700 : /* Setup user. */
701 1 : ret = sss_ncache_set_user(test_ctx->ncache, false,
702 1 : test_ctx->tctx->dom, TEST_UPN);
703 1 : assert_int_equal(ret, EOK);
704 :
705 : /* Mock values. */
706 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
707 :
708 : /* Test. */
709 1 : run_user_by_upn(test_ctx, NULL, 0, ENOENT);
710 1 : assert_false(test_ctx->dp_called);
711 1 : }
712 :
713 1 : void test_user_by_upn_missing_found(void **state)
714 : {
715 1 : struct cache_req_test_ctx *test_ctx = NULL;
716 :
717 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
718 :
719 : /* Mock values. */
720 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
721 1 : mock_account_recv_simple();
722 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
723 :
724 1 : test_ctx->create_user = true;
725 :
726 : /* Test. */
727 1 : run_user_by_upn(test_ctx, NULL, 0, ERR_OK);
728 1 : assert_true(test_ctx->dp_called);
729 1 : check_user(test_ctx, test_ctx->tctx->dom);
730 1 : }
731 :
732 1 : void test_user_by_upn_missing_notfound(void **state)
733 : {
734 1 : struct cache_req_test_ctx *test_ctx = NULL;
735 :
736 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
737 :
738 : /* Mock values. */
739 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
740 1 : mock_account_recv_simple();
741 1 : mock_parse_inp(NULL, NULL, ERR_DOMAIN_NOT_FOUND);
742 :
743 : /* Test. */
744 1 : run_user_by_upn(test_ctx, NULL, 0, ENOENT);
745 1 : assert_true(test_ctx->dp_called);
746 1 : }
747 :
748 1 : void test_user_by_id_multiple_domains_found(void **state)
749 : {
750 1 : struct cache_req_test_ctx *test_ctx = NULL;
751 1 : struct sss_domain_info *domain = NULL;
752 :
753 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
754 :
755 : /* Setup user. */
756 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
757 : "responder_cache_req_test_d", true);
758 1 : assert_non_null(domain);
759 :
760 1 : prepare_user(test_ctx, domain, 1000, time(NULL));
761 :
762 : /* Mock values. */
763 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
764 1 : will_return_always(sss_dp_get_account_recv, 0);
765 :
766 : /* Test. */
767 1 : run_user_by_id(test_ctx, NULL, 0, ERR_OK);
768 1 : assert_true(test_ctx->dp_called);
769 1 : check_user(test_ctx, domain);
770 1 : }
771 :
772 1 : void test_user_by_id_multiple_domains_notfound(void **state)
773 : {
774 1 : struct cache_req_test_ctx *test_ctx = NULL;
775 :
776 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
777 :
778 : /* Mock values. */
779 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
780 1 : will_return_always(sss_dp_get_account_recv, 0);
781 :
782 : /* Test. */
783 1 : run_user_by_id(test_ctx, NULL, 0, ENOENT);
784 1 : assert_true(test_ctx->dp_called);
785 1 : }
786 :
787 1 : void test_user_by_id_cache_valid(void **state)
788 : {
789 1 : struct cache_req_test_ctx *test_ctx = NULL;
790 :
791 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
792 :
793 : /* Setup user. */
794 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
795 :
796 : /* Test. */
797 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
798 1 : check_user(test_ctx, test_ctx->tctx->dom);
799 1 : }
800 :
801 1 : void test_user_by_id_cache_expired(void **state)
802 : {
803 1 : struct cache_req_test_ctx *test_ctx = NULL;
804 :
805 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
806 :
807 : /* Setup user. */
808 1 : prepare_user(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
809 :
810 : /* Mock values. */
811 : /* DP should be contacted. */
812 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
813 1 : mock_account_recv_simple();
814 :
815 : /* Test. */
816 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
817 1 : assert_true(test_ctx->dp_called);
818 1 : check_user(test_ctx, test_ctx->tctx->dom);
819 1 : }
820 :
821 1 : void test_user_by_id_cache_midpoint(void **state)
822 : {
823 1 : struct cache_req_test_ctx *test_ctx = NULL;
824 :
825 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
826 :
827 : /* Setup user. */
828 1 : prepare_user(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
829 :
830 : /* Mock values. */
831 : /* DP should be contacted without callback */
832 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
833 :
834 : /* Test. */
835 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
836 1 : assert_true(test_ctx->dp_called);
837 1 : check_user(test_ctx, test_ctx->tctx->dom);
838 1 : }
839 :
840 1 : void test_user_by_id_ncache(void **state)
841 : {
842 1 : struct cache_req_test_ctx *test_ctx = NULL;
843 : errno_t ret;
844 :
845 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
846 :
847 : /* Setup user. */
848 1 : ret = sss_ncache_set_uid(test_ctx->ncache, false, NULL, TEST_USER_ID);
849 1 : assert_int_equal(ret, EOK);
850 :
851 : /* Test. */
852 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
853 1 : assert_false(test_ctx->dp_called);
854 1 : }
855 :
856 1 : void test_user_by_id_missing_found(void **state)
857 : {
858 1 : struct cache_req_test_ctx *test_ctx = NULL;
859 :
860 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
861 :
862 : /* Mock values. */
863 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
864 1 : mock_account_recv_simple();
865 :
866 1 : test_ctx->create_user = true;
867 :
868 : /* Test. */
869 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
870 1 : assert_true(test_ctx->dp_called);
871 1 : check_user(test_ctx, test_ctx->tctx->dom);
872 1 : }
873 :
874 1 : void test_user_by_id_missing_notfound(void **state)
875 : {
876 1 : struct cache_req_test_ctx *test_ctx = NULL;
877 :
878 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
879 :
880 : /* Mock values. */
881 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
882 1 : mock_account_recv_simple();
883 :
884 : /* Test. */
885 1 : run_user_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
886 1 : assert_true(test_ctx->dp_called);
887 1 : }
888 :
889 1 : void test_group_by_name_multiple_domains_found(void **state)
890 : {
891 1 : struct cache_req_test_ctx *test_ctx = NULL;
892 1 : struct sss_domain_info *domain = NULL;
893 :
894 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
895 :
896 : /* Setup group. */
897 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
898 : "responder_cache_req_test_d", true);
899 1 : assert_non_null(domain);
900 1 : prepare_group(test_ctx, domain, 1000, time(NULL));
901 :
902 : /* Mock values. */
903 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
904 1 : will_return_always(sss_dp_get_account_recv, 0);
905 1 : mock_parse_inp(TEST_GROUP_NAME, NULL, ERR_OK);
906 :
907 : /* Test. */
908 1 : run_group_by_name(test_ctx, NULL, 0, ERR_OK);
909 1 : assert_true(test_ctx->dp_called);
910 1 : check_group(test_ctx, domain);
911 1 : }
912 :
913 1 : void test_group_by_name_multiple_domains_notfound(void **state)
914 : {
915 1 : struct cache_req_test_ctx *test_ctx = NULL;
916 :
917 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
918 :
919 : /* Mock values. */
920 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
921 1 : will_return_always(sss_dp_get_account_recv, 0);
922 1 : mock_parse_inp(TEST_GROUP_NAME, NULL, ERR_OK);
923 :
924 : /* Test. */
925 1 : run_group_by_name(test_ctx, NULL, 0, ENOENT);
926 1 : assert_true(test_ctx->dp_called);
927 1 : }
928 :
929 1 : void test_group_by_name_multiple_domains_parse(void **state)
930 : {
931 1 : struct cache_req_test_ctx *test_ctx = NULL;
932 1 : struct sss_domain_info *domain = NULL;
933 1 : TALLOC_CTX *req_mem_ctx = NULL;
934 1 : struct tevent_req *req = NULL;
935 1 : const char *name = TEST_GROUP_NAME;
936 1 : const char *fqn = NULL;
937 : errno_t ret;
938 :
939 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
940 :
941 : /* Add group to the first domain. */
942 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
943 : "responder_cache_req_test_a", true);
944 1 : assert_non_null(domain);
945 :
946 1 : ret = sysdb_store_group(domain, name, 2000, NULL,
947 : 1000, time(NULL));
948 1 : assert_int_equal(ret, EOK);
949 :
950 : /* Add group to the last domain, with different gid. */
951 :
952 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
953 : "responder_cache_req_test_d", true);
954 1 : assert_non_null(domain);
955 :
956 1 : prepare_group(test_ctx, domain, 1000, time(NULL));
957 :
958 : /* Append domain name to the username. */
959 1 : fqn = talloc_asprintf(test_ctx, "%s@%s", name,
960 : "responder_cache_req_test_d");
961 1 : assert_non_null(fqn);
962 :
963 : /* Test. */
964 1 : req_mem_ctx = talloc_new(global_talloc_context);
965 1 : check_leaks_push(req_mem_ctx);
966 :
967 1 : mock_parse_inp(name, "responder_cache_req_test_d", ERR_OK);
968 :
969 1 : req = cache_req_group_by_name_send(req_mem_ctx, test_ctx->tctx->ev,
970 : test_ctx->rctx, test_ctx->ncache, 10, 0,
971 : NULL, fqn);
972 1 : assert_non_null(req);
973 1 : tevent_req_set_callback(req, cache_req_group_by_name_test_done, test_ctx);
974 :
975 1 : ret = test_ev_loop(test_ctx->tctx);
976 1 : assert_int_equal(ret, ERR_OK);
977 1 : assert_true(check_leaks_pop(req_mem_ctx));
978 1 : assert_false(test_ctx->dp_called);
979 :
980 1 : check_group(test_ctx, domain);
981 :
982 1 : assert_non_null(test_ctx->name);
983 1 : assert_string_equal(name, test_ctx->name);
984 1 : }
985 :
986 1 : void test_group_by_name_cache_valid(void **state)
987 : {
988 1 : struct cache_req_test_ctx *test_ctx = NULL;
989 :
990 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
991 :
992 : /* Setup group. */
993 1 : prepare_group(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
994 :
995 : /* Test. */
996 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
997 1 : check_group(test_ctx, test_ctx->tctx->dom);
998 1 : }
999 :
1000 1 : void test_group_by_name_cache_expired(void **state)
1001 : {
1002 1 : struct cache_req_test_ctx *test_ctx = NULL;
1003 :
1004 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1005 :
1006 : /* Setup group. */
1007 1 : prepare_group(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
1008 :
1009 : /* Mock values. */
1010 : /* DP should be contacted */
1011 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1012 1 : mock_account_recv_simple();
1013 :
1014 : /* Test. */
1015 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
1016 1 : assert_true(test_ctx->dp_called);
1017 1 : check_group(test_ctx, test_ctx->tctx->dom);
1018 1 : }
1019 :
1020 1 : void test_group_by_name_cache_midpoint(void **state)
1021 : {
1022 1 : struct cache_req_test_ctx *test_ctx = NULL;
1023 :
1024 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1025 :
1026 : /* Setup group. */
1027 1 : prepare_group(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
1028 :
1029 : /* Mock values. */
1030 : /* DP should be contacted without callback */
1031 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1032 :
1033 : /* Test. */
1034 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
1035 1 : assert_true(test_ctx->dp_called);
1036 1 : check_group(test_ctx, test_ctx->tctx->dom);
1037 1 : }
1038 :
1039 1 : void test_group_by_name_ncache(void **state)
1040 : {
1041 1 : struct cache_req_test_ctx *test_ctx = NULL;
1042 : errno_t ret;
1043 :
1044 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1045 :
1046 : /* Setup group. */
1047 1 : ret = sss_ncache_set_group(test_ctx->ncache, false,
1048 1 : test_ctx->tctx->dom, TEST_GROUP_NAME);
1049 1 : assert_int_equal(ret, EOK);
1050 :
1051 : /* Test. */
1052 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
1053 1 : assert_false(test_ctx->dp_called);
1054 1 : }
1055 :
1056 1 : void test_group_by_name_missing_found(void **state)
1057 : {
1058 1 : struct cache_req_test_ctx *test_ctx = NULL;
1059 :
1060 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1061 :
1062 : /* Mock values. */
1063 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1064 1 : mock_account_recv_simple();
1065 :
1066 1 : test_ctx->create_group = true;
1067 :
1068 : /* Test. */
1069 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
1070 1 : assert_true(test_ctx->dp_called);
1071 1 : check_group(test_ctx, test_ctx->tctx->dom);
1072 1 : }
1073 :
1074 1 : void test_group_by_name_missing_notfound(void **state)
1075 : {
1076 1 : struct cache_req_test_ctx *test_ctx = NULL;
1077 :
1078 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1079 :
1080 : /* Mock values. */
1081 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1082 1 : mock_account_recv_simple();
1083 :
1084 : /* Test. */
1085 1 : run_group_by_name(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
1086 1 : assert_true(test_ctx->dp_called);
1087 1 : }
1088 :
1089 1 : void test_group_by_id_multiple_domains_found(void **state)
1090 : {
1091 1 : struct cache_req_test_ctx *test_ctx = NULL;
1092 1 : struct sss_domain_info *domain = NULL;
1093 :
1094 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1095 :
1096 : /* Setup group. */
1097 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
1098 : "responder_cache_req_test_d", true);
1099 1 : assert_non_null(domain);
1100 1 : prepare_group(test_ctx, domain, 1000, time(NULL));
1101 :
1102 : /* Mock values. */
1103 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
1104 1 : will_return_always(sss_dp_get_account_recv, 0);
1105 :
1106 : /* Test. */
1107 1 : run_group_by_id(test_ctx, NULL, 0, ERR_OK);
1108 1 : assert_true(test_ctx->dp_called);
1109 1 : check_group(test_ctx, domain);
1110 1 : }
1111 :
1112 1 : void test_group_by_id_multiple_domains_notfound(void **state)
1113 : {
1114 1 : struct cache_req_test_ctx *test_ctx = NULL;
1115 :
1116 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1117 :
1118 : /* Mock values. */
1119 1 : will_return_always(__wrap_sss_dp_get_account_send, test_ctx);
1120 1 : will_return_always(sss_dp_get_account_recv, 0);
1121 :
1122 : /* Test. */
1123 1 : run_group_by_id(test_ctx, NULL, 0, ENOENT);
1124 1 : assert_true(test_ctx->dp_called);
1125 1 : }
1126 :
1127 1 : void test_group_by_id_cache_valid(void **state)
1128 : {
1129 1 : struct cache_req_test_ctx *test_ctx = NULL;
1130 :
1131 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1132 :
1133 : /* Setup group. */
1134 1 : prepare_group(test_ctx, test_ctx->tctx->dom, 1000, time(NULL));
1135 :
1136 : /* Test. */
1137 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
1138 1 : check_group(test_ctx, test_ctx->tctx->dom);
1139 1 : }
1140 :
1141 1 : void test_group_by_id_cache_expired(void **state)
1142 : {
1143 1 : struct cache_req_test_ctx *test_ctx = NULL;
1144 :
1145 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1146 :
1147 : /* Setup group. */
1148 1 : prepare_group(test_ctx, test_ctx->tctx->dom, -1000, time(NULL));
1149 :
1150 : /* Mock values. */
1151 : /* DP should be contacted */
1152 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1153 1 : mock_account_recv_simple();
1154 :
1155 : /* Test. */
1156 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
1157 1 : assert_true(test_ctx->dp_called);
1158 1 : check_group(test_ctx, test_ctx->tctx->dom);
1159 1 : }
1160 :
1161 1 : void test_group_by_id_cache_midpoint(void **state)
1162 : {
1163 1 : struct cache_req_test_ctx *test_ctx = NULL;
1164 :
1165 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1166 :
1167 : /* Setup group. */
1168 1 : prepare_group(test_ctx, test_ctx->tctx->dom, 50, time(NULL) - 26);
1169 :
1170 : /* Mock values. */
1171 : /* DP should be contacted without callback */
1172 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1173 :
1174 : /* Test. */
1175 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 50, ERR_OK);
1176 1 : assert_true(test_ctx->dp_called);
1177 1 : check_group(test_ctx, test_ctx->tctx->dom);
1178 1 : }
1179 :
1180 1 : void test_group_by_id_ncache(void **state)
1181 : {
1182 1 : struct cache_req_test_ctx *test_ctx = NULL;
1183 : errno_t ret;
1184 :
1185 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1186 :
1187 : /* Setup group. */
1188 1 : ret = sss_ncache_set_gid(test_ctx->ncache, false, NULL, TEST_GROUP_ID);
1189 1 : assert_int_equal(ret, EOK);
1190 :
1191 : /* Test. */
1192 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
1193 1 : assert_false(test_ctx->dp_called);
1194 1 : }
1195 :
1196 1 : void test_group_by_id_missing_found(void **state)
1197 : {
1198 1 : struct cache_req_test_ctx *test_ctx = NULL;
1199 :
1200 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1201 :
1202 : /* Mock values. */
1203 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1204 1 : mock_account_recv_simple();
1205 :
1206 1 : test_ctx->create_group = true;
1207 :
1208 : /* Test. */
1209 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ERR_OK);
1210 1 : assert_true(test_ctx->dp_called);
1211 1 : check_group(test_ctx, test_ctx->tctx->dom);
1212 1 : }
1213 :
1214 1 : void test_group_by_id_missing_notfound(void **state)
1215 : {
1216 1 : struct cache_req_test_ctx *test_ctx = NULL;
1217 :
1218 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1219 :
1220 : /* Mock values. */
1221 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1222 1 : mock_account_recv_simple();
1223 :
1224 : /* Test. */
1225 1 : run_group_by_id(test_ctx, test_ctx->tctx->dom, 0, ENOENT);
1226 1 : assert_true(test_ctx->dp_called);
1227 1 : }
1228 :
1229 3 : static void cache_req_user_by_filter_test_done(struct tevent_req *req)
1230 : {
1231 3 : struct cache_req_test_ctx *ctx = NULL;
1232 :
1233 3 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
1234 :
1235 3 : ctx->tctx->error = cache_req_user_by_filter_recv(ctx, req,
1236 : &ctx->result,
1237 : &ctx->domain);
1238 3 : talloc_zfree(req);
1239 3 : ctx->tctx->done = true;
1240 3 : }
1241 :
1242 1 : void test_users_by_filter_filter_old(void **state)
1243 : {
1244 1 : struct cache_req_test_ctx *test_ctx = NULL;
1245 1 : TALLOC_CTX *req_mem_ctx = NULL;
1246 1 : struct tevent_req *req = NULL;
1247 1 : const char *ldbname = NULL;
1248 : errno_t ret;
1249 :
1250 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1251 1 : test_ctx->create_user = true;
1252 :
1253 : /* This user was updated in distant past, so it wont't be reported by
1254 : * the filter search */
1255 1 : ret = sysdb_store_user(test_ctx->tctx->dom, TEST_USER_NAME2, "pwd", 1001, 1001,
1256 : NULL, NULL, NULL, "cn="TEST_USER_NAME2",dc=test", NULL,
1257 : NULL, 1000, 1);
1258 1 : assert_int_equal(ret, EOK);
1259 :
1260 1 : req_mem_ctx = talloc_new(global_talloc_context);
1261 1 : check_leaks_push(req_mem_ctx);
1262 :
1263 : /* Filters always go to DP */
1264 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1265 1 : mock_account_recv_simple();
1266 :
1267 1 : req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
1268 : test_ctx->rctx,
1269 1 : test_ctx->tctx->dom->name,
1270 : "test*");
1271 1 : assert_non_null(req);
1272 1 : tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
1273 :
1274 1 : ret = test_ev_loop(test_ctx->tctx);
1275 1 : assert_int_equal(ret, ERR_OK);
1276 1 : assert_true(check_leaks_pop(req_mem_ctx));
1277 :
1278 1 : assert_non_null(test_ctx->result);
1279 1 : assert_int_equal(test_ctx->result->count, 1);
1280 :
1281 1 : ldbname = ldb_msg_find_attr_as_string(test_ctx->result->msgs[0],
1282 : SYSDB_NAME, NULL);
1283 1 : assert_non_null(ldbname);
1284 1 : assert_string_equal(ldbname, TEST_USER_NAME);
1285 1 : }
1286 :
1287 1 : void test_users_by_filter_notfound(void **state)
1288 : {
1289 1 : struct cache_req_test_ctx *test_ctx = NULL;
1290 1 : TALLOC_CTX *req_mem_ctx = NULL;
1291 1 : struct tevent_req *req = NULL;
1292 : errno_t ret;
1293 :
1294 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1295 :
1296 1 : req_mem_ctx = talloc_new(global_talloc_context);
1297 1 : check_leaks_push(req_mem_ctx);
1298 :
1299 : /* Filters always go to DP */
1300 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1301 1 : mock_account_recv_simple();
1302 :
1303 1 : req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
1304 : test_ctx->rctx,
1305 1 : test_ctx->tctx->dom->name,
1306 : "nosuchuser*");
1307 1 : assert_non_null(req);
1308 1 : tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
1309 :
1310 1 : ret = test_ev_loop(test_ctx->tctx);
1311 1 : assert_int_equal(ret, ENOENT);
1312 1 : assert_true(check_leaks_pop(req_mem_ctx));
1313 1 : }
1314 :
1315 1 : static void test_users_by_filter_multiple_domains_notfound(void **state)
1316 : {
1317 1 : struct cache_req_test_ctx *test_ctx = NULL;
1318 1 : struct sss_domain_info *domain = NULL;
1319 1 : TALLOC_CTX *req_mem_ctx = NULL;
1320 1 : struct tevent_req *req = NULL;
1321 : errno_t ret;
1322 :
1323 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1324 :
1325 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
1326 : "responder_cache_req_test_d", true);
1327 1 : assert_non_null(domain);
1328 :
1329 1 : req_mem_ctx = talloc_new(global_talloc_context);
1330 1 : check_leaks_push(req_mem_ctx);
1331 :
1332 : /* Filters always go to DP */
1333 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1334 1 : mock_account_recv_simple();
1335 :
1336 1 : req = cache_req_user_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
1337 : test_ctx->rctx,
1338 1 : domain->name,
1339 : "nosuchuser*");
1340 1 : assert_non_null(req);
1341 1 : tevent_req_set_callback(req, cache_req_user_by_filter_test_done, test_ctx);
1342 :
1343 1 : ret = test_ev_loop(test_ctx->tctx);
1344 1 : assert_int_equal(ret, ENOENT);
1345 1 : assert_true(check_leaks_pop(req_mem_ctx));
1346 1 : }
1347 :
1348 2 : static void cache_req_group_by_filter_test_done(struct tevent_req *req)
1349 : {
1350 2 : struct cache_req_test_ctx *ctx = NULL;
1351 :
1352 2 : ctx = tevent_req_callback_data(req, struct cache_req_test_ctx);
1353 :
1354 2 : ctx->tctx->error = cache_req_group_by_filter_recv(ctx, req,
1355 : &ctx->result,
1356 : &ctx->domain);
1357 2 : talloc_zfree(req);
1358 2 : ctx->tctx->done = true;
1359 2 : }
1360 :
1361 1 : void test_groups_by_filter_notfound(void **state)
1362 : {
1363 1 : struct cache_req_test_ctx *test_ctx = NULL;
1364 1 : TALLOC_CTX *req_mem_ctx = NULL;
1365 1 : struct tevent_req *req = NULL;
1366 : errno_t ret;
1367 :
1368 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1369 :
1370 1 : req_mem_ctx = talloc_new(global_talloc_context);
1371 1 : check_leaks_push(req_mem_ctx);
1372 :
1373 : /* Filters always go to DP */
1374 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1375 1 : mock_account_recv_simple();
1376 :
1377 1 : req = cache_req_group_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
1378 : test_ctx->rctx,
1379 1 : test_ctx->tctx->dom->name,
1380 : "nosuchgroup*");
1381 1 : assert_non_null(req);
1382 1 : tevent_req_set_callback(req, cache_req_group_by_filter_test_done, test_ctx);
1383 :
1384 1 : ret = test_ev_loop(test_ctx->tctx);
1385 1 : assert_int_equal(ret, ENOENT);
1386 1 : assert_true(check_leaks_pop(req_mem_ctx));
1387 1 : }
1388 :
1389 1 : void test_groups_by_filter_multiple_domains_notfound(void **state)
1390 : {
1391 1 : struct cache_req_test_ctx *test_ctx = NULL;
1392 1 : struct sss_domain_info *domain = NULL;
1393 1 : TALLOC_CTX *req_mem_ctx = NULL;
1394 1 : struct tevent_req *req = NULL;
1395 : errno_t ret;
1396 :
1397 1 : test_ctx = talloc_get_type_abort(*state, struct cache_req_test_ctx);
1398 1 : domain = find_domain_by_name(test_ctx->tctx->dom,
1399 : "responder_cache_req_test_d", true);
1400 1 : assert_non_null(domain);
1401 :
1402 1 : req_mem_ctx = talloc_new(global_talloc_context);
1403 1 : check_leaks_push(req_mem_ctx);
1404 :
1405 : /* Filters always go to DP */
1406 1 : will_return(__wrap_sss_dp_get_account_send, test_ctx);
1407 1 : mock_account_recv_simple();
1408 :
1409 1 : req = cache_req_group_by_filter_send(req_mem_ctx, test_ctx->tctx->ev,
1410 : test_ctx->rctx,
1411 1 : domain->name,
1412 : "nosuchgroup*");
1413 1 : assert_non_null(req);
1414 1 : tevent_req_set_callback(req, cache_req_group_by_filter_test_done, test_ctx);
1415 :
1416 1 : ret = test_ev_loop(test_ctx->tctx);
1417 1 : assert_int_equal(ret, ENOENT);
1418 1 : assert_true(check_leaks_pop(req_mem_ctx));
1419 1 : }
1420 :
1421 1 : int main(int argc, const char *argv[])
1422 : {
1423 : poptContext pc;
1424 : int opt;
1425 6 : struct poptOption long_options[] = {
1426 : POPT_AUTOHELP
1427 5 : SSSD_DEBUG_OPTS
1428 : POPT_TABLEEND
1429 : };
1430 :
1431 1 : const struct CMUnitTest tests[] = {
1432 : new_single_domain_test(user_by_name_cache_valid),
1433 : new_single_domain_test(user_by_name_cache_expired),
1434 : new_single_domain_test(user_by_name_cache_midpoint),
1435 : new_single_domain_test(user_by_name_ncache),
1436 : new_single_domain_test(user_by_name_missing_found),
1437 : new_single_domain_test(user_by_name_missing_notfound),
1438 : new_multi_domain_test(user_by_name_multiple_domains_found),
1439 : new_multi_domain_test(user_by_name_multiple_domains_notfound),
1440 : new_multi_domain_test(user_by_name_multiple_domains_parse),
1441 :
1442 : new_single_domain_test(user_by_upn_cache_valid),
1443 : new_single_domain_test(user_by_upn_cache_expired),
1444 : new_single_domain_test(user_by_upn_cache_midpoint),
1445 : new_single_domain_test(user_by_upn_ncache),
1446 : new_single_domain_test(user_by_upn_missing_found),
1447 : new_single_domain_test(user_by_upn_missing_notfound),
1448 : new_multi_domain_test(user_by_upn_multiple_domains_found),
1449 : new_multi_domain_test(user_by_upn_multiple_domains_notfound),
1450 :
1451 : new_single_domain_test(user_by_id_cache_valid),
1452 : new_single_domain_test(user_by_id_cache_expired),
1453 : new_single_domain_test(user_by_id_cache_midpoint),
1454 : new_single_domain_test(user_by_id_ncache),
1455 : new_single_domain_test(user_by_id_missing_found),
1456 : new_single_domain_test(user_by_id_missing_notfound),
1457 : new_multi_domain_test(user_by_id_multiple_domains_found),
1458 : new_multi_domain_test(user_by_id_multiple_domains_notfound),
1459 :
1460 : new_single_domain_test(group_by_name_cache_valid),
1461 : new_single_domain_test(group_by_name_cache_expired),
1462 : new_single_domain_test(group_by_name_cache_midpoint),
1463 : new_single_domain_test(group_by_name_ncache),
1464 : new_single_domain_test(group_by_name_missing_found),
1465 : new_single_domain_test(group_by_name_missing_notfound),
1466 : new_multi_domain_test(group_by_name_multiple_domains_found),
1467 : new_multi_domain_test(group_by_name_multiple_domains_notfound),
1468 : new_multi_domain_test(group_by_name_multiple_domains_parse),
1469 :
1470 : new_single_domain_test(group_by_id_cache_valid),
1471 : new_single_domain_test(group_by_id_cache_expired),
1472 : new_single_domain_test(group_by_id_cache_midpoint),
1473 : new_single_domain_test(group_by_id_ncache),
1474 : new_single_domain_test(group_by_id_missing_found),
1475 : new_single_domain_test(group_by_id_missing_notfound),
1476 : new_multi_domain_test(group_by_id_multiple_domains_found),
1477 : new_multi_domain_test(group_by_id_multiple_domains_notfound),
1478 :
1479 : new_single_domain_test(users_by_filter_filter_old),
1480 : new_single_domain_test(users_by_filter_notfound),
1481 : new_multi_domain_test(users_by_filter_multiple_domains_notfound),
1482 : new_single_domain_test(groups_by_filter_notfound),
1483 : new_multi_domain_test(groups_by_filter_multiple_domains_notfound),
1484 : };
1485 :
1486 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
1487 1 : debug_level = SSSDBG_INVALID;
1488 :
1489 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
1490 1 : while((opt = poptGetNextOpt(pc)) != -1) {
1491 : switch(opt) {
1492 : default:
1493 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
1494 : poptBadOption(pc, 0), poptStrerror(opt));
1495 0 : poptPrintUsage(pc, stderr, 0);
1496 0 : return 1;
1497 : }
1498 : }
1499 1 : poptFreeContext(pc);
1500 :
1501 1 : DEBUG_CLI_INIT(debug_level);
1502 :
1503 : /* Even though normally the tests should clean up after themselves
1504 : * they might not after a failed run. Remove the old db to be sure */
1505 1 : tests_set_cwd();
1506 1 : test_multidom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, domains);
1507 1 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
1508 :
1509 1 : return cmocka_run_group_tests(tests, NULL, NULL);
1510 : }
|