Line data Source code
1 : /*
2 : Authors:
3 : Sumit Bose <sbose@redhat.com>
4 :
5 : Copyright (C) 2013 Red Hat
6 :
7 : SSSD tests: Tests for utility functions
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 : #define _GNU_SOURCE
24 : #include <stdio.h>
25 : #include <popt.h>
26 :
27 : #include "tests/cmocka/common_mock.h"
28 : #include "util/sss_nss.h"
29 : #include "test_utils.h"
30 :
31 : #define TESTS_PATH "tp_" BASE_FILE_STEM
32 : #define TEST_CONF_DB "test_utils_conf.ldb"
33 : #define TEST_DOM_NAME "utils_test.ldb"
34 :
35 : #define DOM_COUNT 10
36 : #define DOMNAME_TMPL "name_%zu.dom"
37 : #define FLATNAME_TMPL "name_%zu"
38 : #define SID_TMPL "S-1-5-21-1-2-%zu"
39 :
40 : #define MACRO_EXPAND(tok) #tok
41 : #define STR(tok) MACRO_EXPAND(tok)
42 :
43 : #define USERNAME "sssduser"
44 : #define UID 1234
45 : #define DOMAIN "sssddomain"
46 : #define ORIGINAL_HOME "/home/user"
47 : #define FLATNAME "flatname"
48 : #define HOMEDIR_SUBSTR "/mnt/home"
49 :
50 : #define DUMMY "dummy"
51 : #define DUMMY2 "dummy2"
52 :
53 : struct dom_list_test_ctx {
54 : size_t dom_count;
55 : struct sss_domain_info *dom_list;
56 : };
57 :
58 :
59 9 : static int setup_dom_list(void **state)
60 : {
61 : struct dom_list_test_ctx *test_ctx;
62 9 : struct sss_domain_info *dom = NULL;
63 : size_t c;
64 :
65 9 : assert_true(leak_check_setup());
66 :
67 9 : test_ctx = talloc_zero(global_talloc_context, struct dom_list_test_ctx);
68 9 : assert_non_null(test_ctx);
69 :
70 9 : test_ctx->dom_count = DOM_COUNT;
71 :
72 99 : for (c = 0; c < test_ctx->dom_count; c++) {
73 90 : dom = talloc_zero(test_ctx, struct sss_domain_info);
74 90 : assert_non_null(dom);
75 :
76 90 : dom->name = talloc_asprintf(dom, DOMNAME_TMPL, c);
77 90 : assert_non_null(dom->name);
78 :
79 90 : dom->flat_name = talloc_asprintf(dom, FLATNAME_TMPL, c);
80 90 : assert_non_null(dom->flat_name);
81 :
82 90 : dom->domain_id = talloc_asprintf(dom, SID_TMPL, c);
83 90 : assert_non_null(dom->domain_id);
84 :
85 90 : DLIST_ADD(test_ctx->dom_list, dom);
86 : }
87 :
88 9 : check_leaks_push(test_ctx);
89 9 : *state = test_ctx;
90 9 : return 0;
91 : }
92 :
93 9 : static int teardown_dom_list(void **state)
94 : {
95 9 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
96 : struct dom_list_test_ctx);
97 9 : if (test_ctx == NULL) {
98 0 : DEBUG(SSSDBG_CRIT_FAILURE, "Type mismatch\n");
99 0 : return 1;
100 : }
101 :
102 9 : assert_true(check_leaks_pop(test_ctx) == true);
103 9 : talloc_free(test_ctx);
104 9 : assert_true(leak_check_teardown());
105 9 : return 0;
106 : }
107 :
108 1 : void test_find_domain_by_name_null(void **state)
109 : {
110 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
111 : struct dom_list_test_ctx);
112 : struct sss_domain_info *dom;
113 :
114 1 : dom = find_domain_by_name(NULL, NULL, false);
115 1 : assert_null(dom);
116 :
117 1 : dom = find_domain_by_name(test_ctx->dom_list, NULL, false);
118 1 : assert_null(dom);
119 :
120 1 : dom = find_domain_by_name(NULL, "test", false);
121 1 : assert_null(dom);
122 1 : }
123 :
124 1 : void test_find_domain_by_name(void **state)
125 : {
126 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
127 : struct dom_list_test_ctx);
128 : struct sss_domain_info *dom;
129 : size_t c;
130 : char *name;
131 : char *flat_name;
132 : char *sid;
133 :
134 11 : for (c = 0; c < test_ctx->dom_count; c++) {
135 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
136 10 : assert_non_null(name);
137 :
138 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
139 10 : assert_non_null(flat_name);
140 :
141 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
142 10 : assert_non_null(sid);
143 :
144 10 : dom = find_domain_by_name(test_ctx->dom_list, name, false);
145 10 : assert_non_null(dom);
146 10 : assert_string_equal(name, dom->name);
147 10 : assert_string_equal(flat_name, dom->flat_name);
148 10 : assert_string_equal(sid, dom->domain_id);
149 :
150 10 : dom = find_domain_by_name(test_ctx->dom_list, name, true);
151 10 : assert_non_null(dom);
152 10 : assert_string_equal(name, dom->name);
153 10 : assert_string_equal(flat_name, dom->flat_name);
154 10 : assert_string_equal(sid, dom->domain_id);
155 :
156 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, true);
157 10 : assert_non_null(dom);
158 10 : assert_string_equal(name, dom->name);
159 10 : assert_string_equal(flat_name, dom->flat_name);
160 10 : assert_string_equal(sid, dom->domain_id);
161 :
162 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, false);
163 10 : assert_null(dom);
164 :
165 10 : talloc_free(name);
166 10 : talloc_free(flat_name);
167 10 : talloc_free(sid);
168 : }
169 1 : }
170 :
171 1 : void test_find_domain_by_name_missing_flat_name(void **state)
172 : {
173 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
174 : struct dom_list_test_ctx);
175 : struct sss_domain_info *dom;
176 : size_t c;
177 : char *name;
178 : char *flat_name;
179 : char *sid;
180 : size_t mis;
181 :
182 1 : mis = test_ctx->dom_count/2;
183 1 : assert_true((mis >= 1 && mis < test_ctx->dom_count));
184 :
185 1 : dom = test_ctx->dom_list;
186 6 : for (c = 0; c < mis; c++) {
187 5 : assert_non_null(dom);
188 5 : dom = dom->next;
189 : }
190 1 : assert_non_null(dom);
191 1 : dom->flat_name = NULL;
192 :
193 11 : for (c = 0; c < test_ctx->dom_count; c++) {
194 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
195 10 : assert_non_null(name);
196 :
197 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
198 10 : assert_non_null(flat_name);
199 :
200 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
201 10 : assert_non_null(sid);
202 :
203 10 : dom = find_domain_by_name(test_ctx->dom_list, name, true);
204 10 : assert_non_null(dom);
205 10 : assert_string_equal(name, dom->name);
206 10 : if (c == mis - 1) {
207 1 : assert_null(dom->flat_name);
208 : } else {
209 9 : assert_string_equal(flat_name, dom->flat_name);
210 : }
211 10 : assert_string_equal(sid, dom->domain_id);
212 :
213 10 : dom = find_domain_by_name(test_ctx->dom_list, name, false);
214 10 : assert_non_null(dom);
215 10 : assert_string_equal(name, dom->name);
216 10 : if (c == mis - 1) {
217 1 : assert_null(dom->flat_name);
218 : } else {
219 9 : assert_string_equal(flat_name, dom->flat_name);
220 : }
221 10 : assert_string_equal(sid, dom->domain_id);
222 :
223 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, true);
224 10 : if (c == mis - 1) {
225 1 : assert_null(dom);
226 : } else {
227 9 : assert_non_null(dom);
228 9 : assert_string_equal(name, dom->name);
229 9 : assert_string_equal(flat_name, dom->flat_name);
230 9 : assert_string_equal(sid, dom->domain_id);
231 : }
232 :
233 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, false);
234 10 : assert_null(dom);
235 :
236 10 : talloc_free(name);
237 10 : talloc_free(flat_name);
238 10 : talloc_free(sid);
239 : }
240 1 : }
241 :
242 1 : void test_find_domain_by_name_disabled(void **state)
243 : {
244 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
245 : struct dom_list_test_ctx);
246 : struct sss_domain_info *dom;
247 : size_t c;
248 : char *name;
249 : char *flat_name;
250 : char *sid;
251 : size_t mis;
252 :
253 1 : mis = test_ctx->dom_count/2;
254 1 : assert_true((mis >= 1 && mis < test_ctx->dom_count));
255 :
256 1 : dom = test_ctx->dom_list;
257 6 : for (c = 0; c < mis; c++) {
258 5 : assert_non_null(dom);
259 5 : dom = dom->next;
260 : }
261 1 : assert_non_null(dom);
262 1 : sss_domain_set_state(dom, DOM_DISABLED);
263 :
264 11 : for (c = 0; c < test_ctx->dom_count; c++) {
265 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
266 10 : assert_non_null(name);
267 :
268 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
269 10 : assert_non_null(flat_name);
270 :
271 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
272 10 : assert_non_null(sid);
273 :
274 10 : dom = find_domain_by_name(test_ctx->dom_list, name, true);
275 10 : if (c == mis - 1) {
276 1 : assert_null(dom);
277 : } else {
278 9 : assert_non_null(dom);
279 9 : assert_string_equal(name, dom->name);
280 9 : assert_string_equal(flat_name, dom->flat_name);
281 9 : assert_string_equal(sid, dom->domain_id);
282 : }
283 :
284 10 : dom = find_domain_by_name(test_ctx->dom_list, name, false);
285 10 : if (c == mis - 1) {
286 1 : assert_null(dom);
287 : } else {
288 9 : assert_non_null(dom);
289 9 : assert_string_equal(name, dom->name);
290 9 : assert_string_equal(flat_name, dom->flat_name);
291 9 : assert_string_equal(sid, dom->domain_id);
292 : }
293 :
294 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, true);
295 10 : if (c == mis - 1) {
296 1 : assert_null(dom);
297 : } else {
298 9 : assert_non_null(dom);
299 9 : assert_string_equal(name, dom->name);
300 9 : assert_string_equal(flat_name, dom->flat_name);
301 9 : assert_string_equal(sid, dom->domain_id);
302 : }
303 :
304 10 : dom = find_domain_by_name(test_ctx->dom_list, flat_name, false);
305 10 : assert_null(dom);
306 :
307 10 : talloc_free(name);
308 10 : talloc_free(flat_name);
309 10 : talloc_free(sid);
310 : }
311 1 : }
312 :
313 1 : void test_find_domain_by_sid_null(void **state)
314 : {
315 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
316 : struct dom_list_test_ctx);
317 : struct sss_domain_info *dom;
318 :
319 1 : dom = find_domain_by_sid(NULL, NULL);
320 1 : assert_null(dom);
321 :
322 1 : dom = find_domain_by_sid(test_ctx->dom_list, NULL);
323 1 : assert_null(dom);
324 :
325 1 : dom = find_domain_by_sid(NULL, "S-1-5-21-1-2-3");
326 1 : assert_null(dom);
327 1 : }
328 :
329 1 : void test_find_domain_by_sid(void **state)
330 : {
331 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
332 : struct dom_list_test_ctx);
333 : struct sss_domain_info *dom;
334 : size_t c;
335 : char *name;
336 : char *flat_name;
337 : char *sid;
338 :
339 11 : for (c = 0; c < test_ctx->dom_count; c++) {
340 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
341 10 : assert_non_null(name);
342 :
343 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
344 10 : assert_non_null(flat_name);
345 :
346 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
347 10 : assert_non_null(sid);
348 :
349 10 : dom = find_domain_by_sid(test_ctx->dom_list, sid);
350 10 : assert_non_null(dom);
351 10 : assert_string_equal(name, dom->name);
352 10 : assert_string_equal(flat_name, dom->flat_name);
353 10 : assert_string_equal(sid, dom->domain_id);
354 :
355 10 : talloc_free(name);
356 10 : talloc_free(flat_name);
357 10 : talloc_free(sid);
358 : }
359 1 : }
360 :
361 1 : void test_find_domain_by_sid_missing_sid(void **state)
362 : {
363 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
364 : struct dom_list_test_ctx);
365 : struct sss_domain_info *dom;
366 : size_t c;
367 : char *name;
368 : char *flat_name;
369 : char *sid;
370 : size_t mis;
371 :
372 1 : mis = test_ctx->dom_count/2;
373 1 : assert_true((mis >= 1 && mis < test_ctx->dom_count));
374 :
375 1 : dom = test_ctx->dom_list;
376 6 : for (c = 0; c < mis; c++) {
377 5 : assert_non_null(dom);
378 5 : dom = dom->next;
379 : }
380 1 : assert_non_null(dom);
381 1 : dom->domain_id = NULL;
382 :
383 11 : for (c = 0; c < test_ctx->dom_count; c++) {
384 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
385 10 : assert_non_null(name);
386 :
387 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
388 10 : assert_non_null(flat_name);
389 :
390 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
391 10 : assert_non_null(sid);
392 :
393 10 : dom = find_domain_by_sid(test_ctx->dom_list, sid);
394 10 : if (c == mis - 1) {
395 1 : assert_null(dom);
396 : } else {
397 9 : assert_non_null(dom);
398 9 : assert_string_equal(name, dom->name);
399 9 : assert_string_equal(flat_name, dom->flat_name);
400 9 : assert_string_equal(sid, dom->domain_id);
401 : }
402 :
403 10 : talloc_free(name);
404 10 : talloc_free(flat_name);
405 10 : talloc_free(sid);
406 : }
407 1 : }
408 :
409 1 : void test_find_domain_by_sid_disabled(void **state)
410 : {
411 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
412 : struct dom_list_test_ctx);
413 : struct sss_domain_info *dom;
414 : size_t c;
415 : char *name;
416 : char *flat_name;
417 : char *sid;
418 : size_t mis;
419 :
420 1 : mis = test_ctx->dom_count/2;
421 1 : assert_true((mis >= 1 && mis < test_ctx->dom_count));
422 :
423 1 : dom = test_ctx->dom_list;
424 6 : for (c = 0; c < mis; c++) {
425 5 : assert_non_null(dom);
426 5 : dom = dom->next;
427 : }
428 1 : assert_non_null(dom);
429 1 : sss_domain_set_state(dom, DOM_DISABLED);
430 :
431 11 : for (c = 0; c < test_ctx->dom_count; c++) {
432 10 : name = talloc_asprintf(global_talloc_context, DOMNAME_TMPL, c);
433 10 : assert_non_null(name);
434 :
435 10 : flat_name = talloc_asprintf(global_talloc_context, FLATNAME_TMPL, c);
436 10 : assert_non_null(flat_name);
437 :
438 10 : sid = talloc_asprintf(global_talloc_context, SID_TMPL, c);
439 10 : assert_non_null(sid);
440 :
441 10 : dom = find_domain_by_sid(test_ctx->dom_list, sid);
442 10 : if (c == mis - 1) {
443 1 : assert_null(dom);
444 : } else {
445 9 : assert_non_null(dom);
446 9 : assert_string_equal(name, dom->name);
447 9 : assert_string_equal(flat_name, dom->flat_name);
448 9 : assert_string_equal(sid, dom->domain_id);
449 : }
450 :
451 10 : talloc_free(name);
452 10 : talloc_free(flat_name);
453 10 : talloc_free(sid);
454 : }
455 1 : }
456 :
457 : /*
458 : * dom1 -> sub1a
459 : * |
460 : * dom2 -> sub2a -> sub2b
461 : *
462 : */
463 3 : static int setup_dom_tree(void **state)
464 : {
465 : struct dom_list_test_ctx *test_ctx;
466 3 : struct sss_domain_info *head = NULL;
467 3 : struct sss_domain_info *dom = NULL;
468 :
469 3 : assert_true(leak_check_setup());
470 :
471 3 : test_ctx = talloc_zero(global_talloc_context, struct dom_list_test_ctx);
472 3 : assert_non_null(test_ctx);
473 :
474 3 : dom = named_domain(test_ctx, "dom1", NULL);
475 3 : assert_non_null(dom);
476 3 : head = dom;
477 :
478 3 : dom = named_domain(test_ctx, "sub1a", head);
479 3 : assert_non_null(dom);
480 3 : head->subdomains = dom;
481 :
482 3 : dom = named_domain(test_ctx, "dom2", NULL);
483 3 : assert_non_null(dom);
484 3 : head->next = dom;
485 :
486 3 : dom = named_domain(test_ctx, "sub2a", head->next);
487 3 : assert_non_null(dom);
488 3 : head->next->subdomains = dom;
489 :
490 3 : dom = named_domain(test_ctx, "sub2b", head->next);
491 3 : assert_non_null(dom);
492 3 : head->next->subdomains->next = dom;
493 :
494 3 : test_ctx->dom_count = 2;
495 3 : test_ctx->dom_list = head;
496 :
497 3 : check_leaks_push(test_ctx);
498 3 : *state = test_ctx;
499 3 : return 0;
500 : }
501 :
502 3 : static int teardown_dom_tree(void **state)
503 : {
504 3 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
505 : struct dom_list_test_ctx);
506 3 : if (test_ctx == NULL) {
507 0 : DEBUG(SSSDBG_CRIT_FAILURE, "Type mismatch\n");
508 0 : return 1;
509 : }
510 :
511 3 : assert_true(check_leaks_pop(test_ctx));
512 3 : talloc_free(test_ctx);
513 3 : assert_true(leak_check_teardown());
514 3 : return 0;
515 : }
516 :
517 1 : static void test_get_next_domain(void **state)
518 : {
519 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
520 : struct dom_list_test_ctx);
521 1 : struct sss_domain_info *dom = NULL;
522 :
523 1 : dom = get_next_domain(test_ctx->dom_list, false);
524 1 : assert_non_null(dom);
525 1 : assert_string_equal(dom->name, "dom2");
526 :
527 1 : dom = get_next_domain(dom, false);
528 1 : assert_null(dom);
529 1 : }
530 :
531 1 : static void test_get_next_domain_descend(void **state)
532 : {
533 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
534 : struct dom_list_test_ctx);
535 1 : struct sss_domain_info *dom = NULL;
536 :
537 1 : dom = get_next_domain(test_ctx->dom_list, true);
538 1 : assert_non_null(dom);
539 1 : assert_string_equal(dom->name, "sub1a");
540 :
541 1 : dom = get_next_domain(dom, true);
542 1 : assert_non_null(dom);
543 1 : assert_string_equal(dom->name, "dom2");
544 :
545 1 : dom = get_next_domain(dom, true);
546 1 : assert_non_null(dom);
547 1 : assert_string_equal(dom->name, "sub2a");
548 :
549 1 : dom = get_next_domain(dom, true);
550 1 : assert_non_null(dom);
551 1 : assert_string_equal(dom->name, "sub2b");
552 :
553 1 : dom = get_next_domain(dom, false);
554 1 : assert_null(dom);
555 1 : }
556 :
557 1 : static void test_get_next_domain_disabled(void **state)
558 : {
559 1 : struct dom_list_test_ctx *test_ctx = talloc_get_type(*state,
560 : struct dom_list_test_ctx);
561 1 : struct sss_domain_info *dom = NULL;
562 :
563 6 : for (dom = test_ctx->dom_list; dom; dom = get_next_domain(dom, true)) {
564 5 : sss_domain_set_state(dom, DOM_DISABLED);
565 : }
566 :
567 1 : dom = get_next_domain(test_ctx->dom_list, true);
568 1 : assert_null(dom);
569 1 : }
570 :
571 : struct name_init_test_ctx {
572 : struct confdb_ctx *confdb;
573 : };
574 :
575 : #define GLOBAL_FULL_NAME_FORMAT "%1$s@%2$s"
576 : #define GLOBAL_RE_EXPRESSION "(?P<name>[^@]+)@?(?P<domain>[^@]*$)"
577 :
578 : #define TEST_DOMAIN_NAME "test.dom"
579 : #define DOMAIN_FULL_NAME_FORMAT "%3$s\\%1$s"
580 : #define DOMAIN_RE_EXPRESSION "(((?P<domain>[^\\\\]+)\\\\(?P<name>.+$))|" \
581 : "((?P<name>[^@]+)@(?P<domain>.+$))|" \
582 : "(^(?P<name>[^@\\\\]+)$))"
583 :
584 2 : static int confdb_test_setup(void **state)
585 : {
586 : struct name_init_test_ctx *test_ctx;
587 2 : char *conf_db = NULL;
588 2 : char *dompath = NULL;
589 : int ret;
590 : const char *val[2];
591 2 : val[1] = NULL;
592 :
593 2 : assert_true(leak_check_setup());
594 :
595 2 : test_ctx = talloc_zero(global_talloc_context, struct name_init_test_ctx);
596 2 : assert_non_null(test_ctx);
597 :
598 2 : conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_DB);
599 2 : assert_non_null(conf_db);
600 :
601 2 : ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
602 2 : assert_int_equal(ret, EOK);
603 :
604 2 : talloc_free(conf_db);
605 :
606 2 : val[0] = TEST_DOMAIN_NAME;
607 2 : ret = confdb_add_param(test_ctx->confdb, true,
608 : "config/sssd", "domains", val);
609 2 : assert_int_equal(ret, EOK);
610 :
611 2 : val[0] = GLOBAL_FULL_NAME_FORMAT;
612 2 : ret = confdb_add_param(test_ctx->confdb, true,
613 : "config/sssd", "full_name_format", val);
614 2 : assert_int_equal(ret, EOK);
615 :
616 2 : val[0] = GLOBAL_RE_EXPRESSION;
617 2 : ret = confdb_add_param(test_ctx->confdb, true,
618 : "config/sssd", "re_expression", val);
619 2 : assert_int_equal(ret, EOK);
620 :
621 2 : dompath = talloc_asprintf(test_ctx, "config/domain/%s", TEST_DOMAIN_NAME);
622 2 : assert_non_null(dompath);
623 :
624 2 : val[0] = "ldap";
625 2 : ret = confdb_add_param(test_ctx->confdb, true,
626 : dompath, "id_provider", val);
627 2 : assert_int_equal(ret, EOK);
628 :
629 2 : val[0] = DOMAIN_FULL_NAME_FORMAT;
630 2 : ret = confdb_add_param(test_ctx->confdb, true,
631 : dompath, "full_name_format", val);
632 2 : assert_int_equal(ret, EOK);
633 :
634 2 : val[0] = DOMAIN_RE_EXPRESSION;
635 2 : ret = confdb_add_param(test_ctx->confdb, true,
636 : dompath, "re_expression", val);
637 2 : assert_int_equal(ret, EOK);
638 :
639 2 : talloc_free(dompath);
640 :
641 2 : check_leaks_push(test_ctx);
642 2 : *state = test_ctx;
643 2 : return 0;
644 : }
645 :
646 2 : static int confdb_test_teardown(void **state)
647 : {
648 : struct name_init_test_ctx *test_ctx;
649 :
650 2 : test_ctx = talloc_get_type(*state, struct name_init_test_ctx);
651 :
652 2 : assert_true(check_leaks_pop(test_ctx) == true);
653 2 : talloc_free(test_ctx);
654 2 : assert_true(leak_check_teardown());
655 2 : return 0;
656 : }
657 :
658 1 : void test_sss_names_init(void **state)
659 : {
660 : struct name_init_test_ctx *test_ctx;
661 : struct sss_names_ctx *names_ctx;
662 : int ret;
663 :
664 1 : test_ctx = talloc_get_type(*state, struct name_init_test_ctx);
665 :
666 1 : ret = sss_names_init(test_ctx, test_ctx->confdb, NULL, &names_ctx);
667 1 : assert_int_equal(ret, EOK);
668 1 : assert_non_null(names_ctx);
669 1 : assert_string_equal(names_ctx->re_pattern, GLOBAL_RE_EXPRESSION);
670 1 : assert_string_equal(names_ctx->fq_fmt, GLOBAL_FULL_NAME_FORMAT);
671 :
672 1 : talloc_free(names_ctx);
673 :
674 1 : ret = sss_names_init(test_ctx, test_ctx->confdb, TEST_DOMAIN_NAME,
675 : &names_ctx);
676 1 : assert_int_equal(ret, EOK);
677 1 : assert_non_null(names_ctx);
678 1 : assert_string_equal(names_ctx->re_pattern, DOMAIN_RE_EXPRESSION);
679 1 : assert_string_equal(names_ctx->fq_fmt, DOMAIN_FULL_NAME_FORMAT);
680 :
681 1 : talloc_free(names_ctx);
682 1 : }
683 :
684 1 : void test_well_known_sid_to_name(void **state)
685 : {
686 : int ret;
687 : const char *name;
688 : const char *dom;
689 :
690 1 : ret = well_known_sid_to_name(NULL, NULL, NULL);
691 1 : assert_int_equal(ret, EINVAL);
692 :
693 1 : ret = well_known_sid_to_name("abc", &dom, &name);
694 1 : assert_int_equal(ret, EINVAL);
695 :
696 1 : ret = well_known_sid_to_name("S-1", &dom, &name);
697 1 : assert_int_equal(ret, EINVAL);
698 :
699 1 : ret = well_known_sid_to_name("S-1-", &dom, &name);
700 1 : assert_int_equal(ret, EINVAL);
701 :
702 1 : ret = well_known_sid_to_name("S-1-0", &dom, &name);
703 1 : assert_int_equal(ret, EINVAL);
704 :
705 1 : ret = well_known_sid_to_name("S-1-0-", &dom, &name);
706 1 : assert_int_equal(ret, EINVAL);
707 :
708 1 : ret = well_known_sid_to_name("S-1-0-0", &dom, &name);
709 1 : assert_int_equal(ret, EOK);
710 1 : assert_string_equal(dom, "NULL AUTHORITY");
711 1 : assert_string_equal(name, "NULL SID");
712 :
713 1 : ret = well_known_sid_to_name("S-1-0-0-", &dom, &name);
714 1 : assert_int_equal(ret, EINVAL);
715 :
716 1 : ret = well_known_sid_to_name("S-1-5", &dom, &name);
717 1 : assert_int_equal(ret, EINVAL);
718 :
719 1 : ret = well_known_sid_to_name("S-1-5-", &dom, &name);
720 1 : assert_int_equal(ret, EINVAL);
721 :
722 1 : ret = well_known_sid_to_name("S-1-5-6", &dom, &name);
723 1 : assert_int_equal(ret, EOK);
724 1 : assert_string_equal(dom, "NT AUTHORITY");
725 1 : assert_string_equal(name, "SERVICE");
726 :
727 1 : ret = well_known_sid_to_name("S-1-5-6-", &dom, &name);
728 1 : assert_int_equal(ret, EINVAL);
729 :
730 1 : ret = well_known_sid_to_name("S-1-5-21", &dom, &name);
731 1 : assert_int_equal(ret, EINVAL);
732 :
733 1 : ret = well_known_sid_to_name("S-1-5-21-", &dom, &name);
734 1 : assert_int_equal(ret, ENOENT);
735 :
736 1 : ret = well_known_sid_to_name("S-1-5-21-abc", &dom, &name);
737 1 : assert_int_equal(ret, ENOENT);
738 :
739 1 : ret = well_known_sid_to_name("S-1-5-32", &dom, &name);
740 1 : assert_int_equal(ret, EINVAL);
741 :
742 1 : ret = well_known_sid_to_name("S-1-5-32-", &dom, &name);
743 1 : assert_int_equal(ret, EINVAL);
744 :
745 1 : ret = well_known_sid_to_name("S-1-5-32-551", &dom, &name);
746 1 : assert_int_equal(ret, EOK);
747 1 : assert_string_equal(dom, "BUILTIN");
748 1 : assert_string_equal(name, "Backup Operators");
749 :
750 1 : ret = well_known_sid_to_name("S-1-5-32-551-", &dom, &name);
751 1 : assert_int_equal(ret, EINVAL);
752 :
753 1 : }
754 :
755 1 : void test_name_to_well_known_sid(void **state)
756 : {
757 : int ret;
758 : const char *sid;
759 :
760 1 : ret = name_to_well_known_sid(NULL, NULL, NULL);
761 1 : assert_int_equal(ret, EINVAL);
762 :
763 1 : ret = name_to_well_known_sid("abc", "def", &sid);
764 1 : assert_int_equal(ret, ENOENT);
765 :
766 1 : ret = name_to_well_known_sid("", "def", &sid);
767 1 : assert_int_equal(ret, ENOENT);
768 :
769 1 : ret = name_to_well_known_sid("BUILTIN", "def", &sid);
770 1 : assert_int_equal(ret, EINVAL);
771 :
772 1 : ret = name_to_well_known_sid("NT AUTHORITY", "def", &sid);
773 1 : assert_int_equal(ret, EINVAL);
774 :
775 1 : ret = name_to_well_known_sid("LOCAL AUTHORITY", "LOCAL", &sid);
776 1 : assert_int_equal(ret, EOK);
777 1 : assert_string_equal(sid, "S-1-2-0");
778 :
779 1 : ret = name_to_well_known_sid(NULL, "LOCAL", &sid);
780 1 : assert_int_equal(ret, EINVAL);
781 :
782 1 : ret = name_to_well_known_sid("BUILTIN", "Cryptographic Operators", &sid);
783 1 : assert_int_equal(ret, EOK);
784 1 : assert_string_equal(sid, "S-1-5-32-569");
785 :
786 1 : ret = name_to_well_known_sid("NT AUTHORITY", "DIALUP", &sid);
787 1 : assert_int_equal(ret, EOK);
788 1 : assert_string_equal(sid, "S-1-5-1");
789 1 : }
790 :
791 : #define TEST_SANITIZE_INPUT "TestUser@Test.Domain"
792 : #define TEST_SANITIZE_LC_INPUT "testuser@test.domain"
793 :
794 1 : void test_sss_filter_sanitize_for_dom(void **state)
795 : {
796 : struct dom_list_test_ctx *test_ctx;
797 : int ret;
798 : char *sanitized;
799 : char *lc_sanitized;
800 : struct sss_domain_info *dom;
801 :
802 1 : test_ctx = talloc_get_type(*state, struct dom_list_test_ctx);
803 1 : dom = test_ctx->dom_list;
804 :
805 1 : dom->case_sensitive = true;
806 :
807 1 : ret = sss_filter_sanitize_for_dom(test_ctx, TEST_SANITIZE_INPUT, dom,
808 : &sanitized, &lc_sanitized);
809 1 : assert_int_equal(ret, EOK);
810 1 : assert_string_equal(sanitized, TEST_SANITIZE_INPUT);
811 1 : assert_string_equal(lc_sanitized, TEST_SANITIZE_INPUT);
812 1 : talloc_free(sanitized);
813 1 : talloc_free(lc_sanitized);
814 :
815 1 : dom->case_sensitive = false;
816 :
817 1 : ret = sss_filter_sanitize_for_dom(test_ctx, TEST_SANITIZE_INPUT, dom,
818 : &sanitized, &lc_sanitized);
819 1 : assert_int_equal(ret, EOK);
820 1 : assert_string_equal(sanitized, TEST_SANITIZE_INPUT);
821 1 : assert_string_equal(lc_sanitized, TEST_SANITIZE_LC_INPUT);
822 1 : talloc_free(sanitized);
823 1 : talloc_free(lc_sanitized);
824 1 : }
825 :
826 46 : void check_expanded_value(TALLOC_CTX *tmp_ctx,
827 : struct sss_nss_homedir_ctx *homedir_ctx,
828 : const char *template, const char *exp_val)
829 : {
830 : char *homedir;
831 :
832 46 : homedir = expand_homedir_template(tmp_ctx, template, homedir_ctx);
833 46 : if (exp_val != NULL) {
834 36 : assert_string_equal(homedir, exp_val);
835 : } else {
836 10 : assert_null(homedir);
837 : }
838 :
839 46 : talloc_free(homedir);
840 46 : }
841 :
842 1 : static int setup_homedir_ctx(void **state)
843 : {
844 : struct sss_nss_homedir_ctx *homedir_ctx;
845 :
846 1 : assert_true(leak_check_setup());
847 :
848 1 : homedir_ctx= talloc_zero(global_talloc_context,
849 : struct sss_nss_homedir_ctx);
850 1 : assert_non_null(homedir_ctx);
851 :
852 1 : homedir_ctx->username = USERNAME;
853 1 : homedir_ctx->uid = UID;
854 1 : homedir_ctx->original = ORIGINAL_HOME;
855 1 : homedir_ctx->domain = DOMAIN;
856 1 : homedir_ctx->flatname = FLATNAME;
857 1 : homedir_ctx->config_homedir_substr = HOMEDIR_SUBSTR;
858 :
859 1 : check_leaks_push(homedir_ctx);
860 1 : *state = homedir_ctx;
861 1 : return 0;
862 : }
863 :
864 1 : static int teardown_homedir_ctx(void **state)
865 : {
866 1 : struct sss_nss_homedir_ctx *homedir_ctx = talloc_get_type(*state,
867 : struct sss_nss_homedir_ctx);
868 1 : if (homedir_ctx == NULL) {
869 0 : DEBUG(SSSDBG_CRIT_FAILURE, "Type mismatch\n");
870 0 : return 1;
871 : }
872 :
873 1 : assert_true(check_leaks_pop(homedir_ctx) == true);
874 1 : talloc_free(homedir_ctx);
875 1 : assert_true(leak_check_teardown());
876 1 : return 0;
877 : }
878 :
879 1 : void test_expand_homedir_template_NULL(void **state)
880 : {
881 : TALLOC_CTX *tmp_ctx;
882 : char *homedir;
883 : struct sss_nss_homedir_ctx *homedir_ctx;
884 :
885 : /* following format strings requires data in homedir_ctx */
886 1 : const char *format_strings[] = { "%u", "%U", "%d", "%f", "%F", "%H",
887 : NULL };
888 : int i;
889 :
890 1 : tmp_ctx = talloc_new(NULL);
891 1 : assert_non_null(tmp_ctx);
892 :
893 1 : homedir_ctx = talloc_zero(tmp_ctx, struct sss_nss_homedir_ctx);
894 1 : assert_non_null(homedir_ctx);
895 :
896 1 : homedir = expand_homedir_template(tmp_ctx, NULL, NULL);
897 1 : assert_null(homedir);
898 :
899 1 : homedir = expand_homedir_template(tmp_ctx, "template", NULL);
900 1 : assert_null(homedir);
901 :
902 : /* missing data in homedir_ctx */
903 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%%", "%");
904 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%o", "");
905 :
906 7 : for (i = 0; format_strings[i] != NULL; ++i) {
907 6 : check_expanded_value(tmp_ctx, homedir_ctx, format_strings[i], NULL);
908 : }
909 :
910 : /* flatname requires domain and username */
911 1 : homedir_ctx->username = DUMMY;
912 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%f", NULL);
913 :
914 1 : homedir_ctx->username = NULL;
915 1 : homedir_ctx->domain = DUMMY;
916 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%f", NULL);
917 :
918 : /* test unknown format string */
919 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%x", NULL);
920 :
921 : /* test malformed format string */
922 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%", NULL);
923 :
924 1 : talloc_free(tmp_ctx);
925 1 : }
926 :
927 1 : void test_expand_homedir_template(void **state)
928 : {
929 1 : struct sss_nss_homedir_ctx *homedir_ctx = talloc_get_type(*state,
930 : struct sss_nss_homedir_ctx);
931 : TALLOC_CTX *tmp_ctx;
932 :
933 1 : tmp_ctx = talloc_new(NULL);
934 1 : assert_non_null(tmp_ctx);
935 :
936 : /* string without template */
937 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY, DUMMY);
938 :
939 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%u", USERNAME);
940 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%u", DUMMY USERNAME);
941 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%u"DUMMY, USERNAME DUMMY);
942 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%u"DUMMY2,
943 : DUMMY USERNAME DUMMY2);
944 :
945 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%U", STR(UID));
946 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%U", DUMMY STR(UID));
947 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%U"DUMMY, STR(UID) DUMMY);
948 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%U"DUMMY2,
949 : DUMMY STR(UID) DUMMY2);
950 :
951 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%d", DOMAIN);
952 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%d", DUMMY DOMAIN);
953 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%d"DUMMY, DOMAIN DUMMY);
954 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%d"DUMMY2,
955 : DUMMY DOMAIN DUMMY2);
956 :
957 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%f", USERNAME"@"DOMAIN);
958 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%f",
959 : DUMMY USERNAME"@"DOMAIN);
960 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%f"DUMMY,
961 : USERNAME"@"DOMAIN DUMMY);
962 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%f"DUMMY2,
963 : DUMMY USERNAME"@"DOMAIN DUMMY2);
964 :
965 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%o", ORIGINAL_HOME);
966 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%o", DUMMY ORIGINAL_HOME);
967 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%o"DUMMY, ORIGINAL_HOME DUMMY);
968 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%o"DUMMY2,
969 : DUMMY ORIGINAL_HOME DUMMY2);
970 :
971 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%F", FLATNAME);
972 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%F", DUMMY FLATNAME);
973 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%F"DUMMY, FLATNAME DUMMY);
974 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%F"DUMMY2,
975 : DUMMY FLATNAME DUMMY2);
976 :
977 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%H", HOMEDIR_SUBSTR);
978 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%H",
979 : DUMMY HOMEDIR_SUBSTR);
980 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%H"DUMMY,
981 : HOMEDIR_SUBSTR DUMMY);
982 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%H"DUMMY2,
983 : DUMMY HOMEDIR_SUBSTR DUMMY2);
984 :
985 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%%", "%");
986 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%%", DUMMY"%");
987 1 : check_expanded_value(tmp_ctx, homedir_ctx, "%%"DUMMY, "%"DUMMY);
988 1 : check_expanded_value(tmp_ctx, homedir_ctx, DUMMY"%%"DUMMY2,
989 : DUMMY"%"DUMMY2);
990 :
991 : /* test all format strings */
992 1 : check_expanded_value(tmp_ctx, homedir_ctx,
993 : DUMMY"/%u/%U/%d/%f/%o/%F/%%/%H/"DUMMY2,
994 : DUMMY"/"USERNAME"/" STR(UID) "/"DOMAIN"/"
995 : USERNAME"@"DOMAIN"/"ORIGINAL_HOME"/"FLATNAME"/%/"
996 : HOMEDIR_SUBSTR"/"DUMMY2);
997 1 : talloc_free(tmp_ctx);
998 1 : }
999 :
1000 1 : static int setup_add_strings_lists(void **state)
1001 : {
1002 1 : assert_true(leak_check_setup());
1003 :
1004 1 : check_leaks_push(global_talloc_context);
1005 1 : return 0;
1006 : }
1007 :
1008 1 : static int teardown_add_strings_lists(void **state)
1009 : {
1010 1 : assert_true(check_leaks_pop(global_talloc_context) == true);
1011 1 : assert_true(leak_check_teardown());
1012 1 : return 0;
1013 : }
1014 :
1015 1 : void test_add_strings_lists(void **state)
1016 : {
1017 1 : const char *l1[] = {"a", "b", "c", NULL};
1018 1 : const char *l2[] = {"1", "2", "3", NULL};
1019 : char **res;
1020 : int ret;
1021 : size_t c;
1022 : size_t d;
1023 :
1024 1 : ret = add_strings_lists(global_talloc_context, NULL, NULL, true, &res);
1025 1 : assert_int_equal(ret, EOK);
1026 1 : assert_non_null(res);
1027 1 : assert_null(res[0]);
1028 1 : talloc_free(res);
1029 :
1030 1 : ret = add_strings_lists(global_talloc_context, NULL, NULL, false, &res);
1031 1 : assert_int_equal(ret, EOK);
1032 1 : assert_non_null(res);
1033 1 : assert_null(res[0]);
1034 1 : talloc_free(res);
1035 :
1036 1 : ret = add_strings_lists(global_talloc_context, l1, NULL, false, &res);
1037 1 : assert_int_equal(ret, EOK);
1038 1 : assert_non_null(res);
1039 4 : for (c = 0; l1[c] != NULL; c++) {
1040 : /* 'copy_strings' is 'false', pointers must be equal */
1041 3 : assert_int_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1042 : }
1043 1 : assert_null(res[c]);
1044 1 : talloc_free(res);
1045 :
1046 1 : ret = add_strings_lists(global_talloc_context, l1, NULL, true, &res);
1047 1 : assert_int_equal(ret, EOK);
1048 1 : assert_non_null(res);
1049 4 : for (c = 0; l1[c] != NULL; c++) {
1050 : /* 'copy_strings' is 'true', pointers must be different, but strings
1051 : * must be equal */
1052 3 : assert_int_not_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1053 3 : assert_string_equal(l1[c], res[c]);
1054 : }
1055 1 : assert_null(res[c]);
1056 1 : talloc_free(res);
1057 :
1058 1 : ret = add_strings_lists(global_talloc_context, NULL, l1, false, &res);
1059 1 : assert_int_equal(ret, EOK);
1060 1 : assert_non_null(res);
1061 4 : for (c = 0; l1[c] != NULL; c++) {
1062 : /* 'copy_strings' is 'false', pointers must be equal */
1063 3 : assert_int_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1064 : }
1065 1 : assert_null(res[c]);
1066 1 : talloc_free(res);
1067 :
1068 1 : ret = add_strings_lists(global_talloc_context, NULL, l1, true, &res);
1069 1 : assert_int_equal(ret, EOK);
1070 1 : assert_non_null(res);
1071 4 : for (c = 0; l1[c] != NULL; c++) {
1072 : /* 'copy_strings' is 'true', pointers must be different, but strings
1073 : * must be equal */
1074 3 : assert_int_not_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1075 3 : assert_string_equal(l1[c], res[c]);
1076 : }
1077 1 : assert_null(res[c]);
1078 1 : talloc_free(res);
1079 :
1080 1 : ret = add_strings_lists(global_talloc_context, l1, l2, false, &res);
1081 1 : assert_int_equal(ret, EOK);
1082 1 : assert_non_null(res);
1083 4 : for (c = 0; l1[c] != NULL; c++) {
1084 : /* 'copy_strings' is 'false', pointers must be equal */
1085 3 : assert_int_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1086 : }
1087 4 : for (d = 0; l2[d] != NULL; d++) {
1088 3 : assert_int_equal(memcmp(&l2[d], &res[c+d], sizeof(char *)), 0);
1089 : }
1090 1 : assert_null(res[c+d]);
1091 1 : talloc_free(res);
1092 :
1093 1 : ret = add_strings_lists(global_talloc_context, l1, l2, true, &res);
1094 1 : assert_int_equal(ret, EOK);
1095 1 : assert_non_null(res);
1096 4 : for (c = 0; l1[c] != NULL; c++) {
1097 : /* 'copy_strings' is 'true', pointers must be different, but strings
1098 : * must be equal */
1099 3 : assert_int_not_equal(memcmp(&l1[c], &res[c], sizeof(char *)), 0);
1100 3 : assert_string_equal(l1[c], res[c]);
1101 : }
1102 4 : for (d = 0; l2[d] != NULL; d++) {
1103 3 : assert_int_not_equal(memcmp(&l2[d], &res[c+d], sizeof(char *)), 0);
1104 3 : assert_string_equal(l2[d], res[c+d]);
1105 : }
1106 1 : assert_null(res[c+d]);
1107 1 : talloc_free(res);
1108 1 : }
1109 :
1110 1 : void test_sss_write_krb5_conf_snippet(void **state)
1111 : {
1112 : int ret;
1113 : char buf[PATH_MAX];
1114 : char *cwd;
1115 : char *path;
1116 : char *file;
1117 :
1118 1 : ret = sss_write_krb5_conf_snippet(NULL);
1119 1 : assert_int_equal(ret, EINVAL);
1120 :
1121 1 : ret = sss_write_krb5_conf_snippet("abc");
1122 1 : assert_int_equal(ret, EINVAL);
1123 :
1124 1 : ret = sss_write_krb5_conf_snippet("");
1125 1 : assert_int_equal(ret, EOK);
1126 :
1127 1 : ret = sss_write_krb5_conf_snippet("none");
1128 1 : assert_int_equal(ret, EOK);
1129 :
1130 1 : cwd = getcwd(buf, PATH_MAX);
1131 1 : assert_non_null(cwd);
1132 :
1133 1 : ret = asprintf(&path, "%s/%s", cwd, TESTS_PATH);
1134 1 : assert_true(ret > 0);
1135 :
1136 1 : ret = asprintf(&file, "%s/%s/localauth_plugin", cwd, TESTS_PATH);
1137 1 : assert_true(ret > 0);
1138 :
1139 1 : ret = sss_write_krb5_conf_snippet(path);
1140 1 : assert_int_equal(ret, EOK);
1141 :
1142 : /* Check if writing a second time will work as well */
1143 1 : ret = sss_write_krb5_conf_snippet(path);
1144 1 : assert_int_equal(ret, EOK);
1145 :
1146 : #ifdef HAVE_KRB5_LOCALAUTH_PLUGIN
1147 1 : ret = unlink(file);
1148 1 : assert_int_equal(ret, EOK);
1149 : #endif
1150 :
1151 1 : free(file);
1152 1 : free(path);
1153 1 : }
1154 :
1155 :
1156 1 : void test_fix_domain_in_name_list(void **state)
1157 : {
1158 : struct name_init_test_ctx *test_ctx;
1159 :
1160 : int ret;
1161 : struct sss_domain_info *sd;
1162 : struct sss_domain_info *dom;
1163 1 : const char *in[] = { "abc@test.case.dom", "def@TEST.case.DOM", NULL};
1164 1 : char **out = NULL;
1165 :
1166 1 : test_ctx = talloc_get_type(*state, struct name_init_test_ctx);
1167 1 : assert_non_null(test_ctx);
1168 :
1169 1 : ret = confdb_get_domains(test_ctx->confdb, &dom);
1170 1 : assert_int_equal(ret, EOK);
1171 :
1172 1 : ret = sss_names_init(dom, test_ctx->confdb, NULL, &dom->names);
1173 1 : assert_int_equal(ret, EOK);
1174 :
1175 1 : sd = talloc_zero(test_ctx, struct sss_domain_info);
1176 1 : assert_non_null(sd);
1177 1 : sd->name = talloc_strdup(sd, "TesT.CasE.DoM");
1178 1 : assert_non_null(sd->name);
1179 1 : sd->names = dom->names;
1180 1 : sd->fqnames = true;
1181 1 : DLIST_ADD(dom->subdomains, sd);
1182 1 : sd->parent = dom;
1183 :
1184 1 : ret = fix_domain_in_name_list(test_ctx, dom, discard_const(in), &out);
1185 1 : assert_int_equal(ret, EOK);
1186 1 : assert_non_null(out);
1187 1 : assert_non_null(out[0]);
1188 1 : assert_string_equal(out[0], "abc@TesT.CasE.DoM");
1189 1 : assert_non_null(out[1]);
1190 1 : assert_string_equal(out[1], "def@TesT.CasE.DoM");
1191 1 : assert_null(out[2]);
1192 :
1193 1 : talloc_free(out);
1194 1 : talloc_free(sd);
1195 1 : talloc_free(dom);
1196 1 : }
1197 :
1198 : struct unique_file_test_ctx {
1199 : char *filename;
1200 : };
1201 :
1202 4 : static int unique_file_test_setup(void **state)
1203 : {
1204 : struct unique_file_test_ctx *test_ctx;
1205 :
1206 4 : assert_true(leak_check_setup());
1207 4 : check_leaks_push(global_talloc_context);
1208 :
1209 4 : test_ctx = talloc_zero(global_talloc_context, struct unique_file_test_ctx);
1210 4 : assert_non_null(test_ctx);
1211 :
1212 4 : test_ctx->filename = talloc_strdup(test_ctx, "test_unique_file_XXXXXX");
1213 4 : assert_non_null(test_ctx);
1214 :
1215 4 : *state = test_ctx;
1216 4 : return 0;
1217 : }
1218 :
1219 4 : static int unique_file_test_teardown(void **state)
1220 : {
1221 : struct unique_file_test_ctx *test_ctx;
1222 : errno_t ret;
1223 :
1224 4 : test_ctx = talloc_get_type(*state, struct unique_file_test_ctx);
1225 :
1226 4 : errno = 0;
1227 4 : ret = unlink(test_ctx->filename);
1228 4 : if (ret != 0 && errno != ENOENT) {
1229 0 : fail();
1230 : }
1231 :
1232 4 : talloc_free(test_ctx);
1233 4 : assert_true(check_leaks_pop(global_talloc_context) == true);
1234 4 : assert_true(leak_check_teardown());
1235 4 : return 0;
1236 : }
1237 :
1238 4 : static void assert_destructor(TALLOC_CTX *owner,
1239 : struct unique_file_test_ctx *test_ctx)
1240 : {
1241 : int fd;
1242 : errno_t ret;
1243 : char *check_filename;
1244 :
1245 : /* Test that the destructor works */
1246 4 : if (owner == NULL) {
1247 2 : return;
1248 : }
1249 :
1250 2 : check_filename = talloc_strdup(test_ctx, test_ctx->filename);
1251 2 : assert_non_null(check_filename);
1252 :
1253 2 : talloc_free(owner);
1254 :
1255 2 : ret = check_and_open_readonly(test_ctx->filename, &fd,
1256 : geteuid(), getegid(),
1257 : (S_IRUSR | S_IWUSR | S_IFREG), 0);
1258 2 : close(fd);
1259 2 : assert_int_not_equal(ret, EOK);
1260 : }
1261 :
1262 2 : static void sss_unique_file_test(struct unique_file_test_ctx *test_ctx,
1263 : bool test_destructor)
1264 : {
1265 : int fd;
1266 : errno_t ret;
1267 : struct stat sb;
1268 2 : TALLOC_CTX *owner = NULL;
1269 :
1270 2 : if (test_destructor) {
1271 1 : owner = talloc_new(test_ctx);
1272 1 : assert_non_null(owner);
1273 : }
1274 :
1275 2 : fd = sss_unique_file(owner, test_ctx->filename, &ret);
1276 2 : assert_int_not_equal(fd, -1);
1277 2 : assert_int_equal(ret, EOK);
1278 :
1279 2 : ret = check_fd(fd, geteuid(), getegid(),
1280 : (S_IRUSR | S_IWUSR | S_IFREG), 0, &sb);
1281 2 : close(fd);
1282 2 : assert_int_equal(ret, EOK);
1283 :
1284 2 : assert_destructor(owner, test_ctx);
1285 2 : }
1286 :
1287 1 : static void test_sss_unique_file(void **state)
1288 : {
1289 : struct unique_file_test_ctx *test_ctx;
1290 1 : test_ctx = talloc_get_type(*state, struct unique_file_test_ctx);
1291 1 : sss_unique_file_test(test_ctx, false);
1292 1 : }
1293 :
1294 1 : static void test_sss_unique_file_destruct(void **state)
1295 : {
1296 : struct unique_file_test_ctx *test_ctx;
1297 1 : test_ctx = talloc_get_type(*state, struct unique_file_test_ctx);
1298 1 : sss_unique_file_test(test_ctx, true);
1299 1 : }
1300 :
1301 1 : static void test_sss_unique_file_neg(void **state)
1302 : {
1303 : int fd;
1304 : errno_t ret;
1305 :
1306 1 : fd = sss_unique_file(NULL, discard_const("badpattern"), &ret);
1307 1 : assert_int_equal(fd, -1);
1308 1 : assert_int_equal(ret, EINVAL);
1309 1 : }
1310 :
1311 2 : static void sss_unique_filename_test(struct unique_file_test_ctx *test_ctx,
1312 : bool test_destructor)
1313 : {
1314 : int fd;
1315 : errno_t ret;
1316 : char *tmp_filename;
1317 2 : TALLOC_CTX *owner = NULL;
1318 :
1319 2 : tmp_filename = talloc_strdup(test_ctx, test_ctx->filename);
1320 2 : assert_non_null(tmp_filename);
1321 :
1322 2 : if (test_destructor) {
1323 1 : owner = talloc_new(test_ctx);
1324 1 : assert_non_null(owner);
1325 : }
1326 :
1327 2 : ret = sss_unique_filename(owner, test_ctx->filename);
1328 2 : assert_int_equal(ret, EOK);
1329 :
1330 2 : assert_int_equal(strncmp(test_ctx->filename,
1331 : tmp_filename,
1332 : strlen(tmp_filename) - sizeof("XXXXXX")),
1333 : 0);
1334 :
1335 2 : ret = check_and_open_readonly(test_ctx->filename, &fd,
1336 : geteuid(), getegid(),
1337 : (S_IRUSR | S_IWUSR | S_IFREG), 0);
1338 2 : close(fd);
1339 2 : assert_int_equal(ret, EOK);
1340 :
1341 2 : assert_destructor(owner, test_ctx);
1342 2 : }
1343 :
1344 1 : static void test_sss_unique_filename(void **state)
1345 : {
1346 : struct unique_file_test_ctx *test_ctx;
1347 :
1348 1 : test_ctx = talloc_get_type(*state, struct unique_file_test_ctx);
1349 1 : sss_unique_filename_test(test_ctx, false);
1350 1 : }
1351 :
1352 1 : static void test_sss_unique_filename_destruct(void **state)
1353 : {
1354 : struct unique_file_test_ctx *test_ctx;
1355 :
1356 1 : test_ctx = talloc_get_type(*state, struct unique_file_test_ctx);
1357 1 : sss_unique_filename_test(test_ctx, true);
1358 1 : }
1359 :
1360 1 : int main(int argc, const char *argv[])
1361 : {
1362 : poptContext pc;
1363 : int opt;
1364 : int rv;
1365 6 : struct poptOption long_options[] = {
1366 : POPT_AUTOHELP
1367 5 : SSSD_DEBUG_OPTS
1368 : POPT_TABLEEND
1369 : };
1370 :
1371 1 : const struct CMUnitTest tests[] = {
1372 : cmocka_unit_test_setup_teardown(test_find_domain_by_sid_null,
1373 : setup_dom_list, teardown_dom_list),
1374 : cmocka_unit_test_setup_teardown(test_find_domain_by_sid,
1375 : setup_dom_list, teardown_dom_list),
1376 : cmocka_unit_test_setup_teardown(test_find_domain_by_sid_missing_sid,
1377 : setup_dom_list, teardown_dom_list),
1378 : cmocka_unit_test_setup_teardown(test_find_domain_by_sid_disabled,
1379 : setup_dom_list, teardown_dom_list),
1380 : cmocka_unit_test_setup_teardown(test_find_domain_by_name_null,
1381 : setup_dom_list, teardown_dom_list),
1382 : cmocka_unit_test_setup_teardown(test_find_domain_by_name,
1383 : setup_dom_list, teardown_dom_list),
1384 : cmocka_unit_test_setup_teardown(test_find_domain_by_name_missing_flat_name,
1385 : setup_dom_list, teardown_dom_list),
1386 : cmocka_unit_test_setup_teardown(test_find_domain_by_name_disabled,
1387 : setup_dom_list, teardown_dom_list),
1388 :
1389 : cmocka_unit_test_setup_teardown(test_sss_names_init,
1390 : confdb_test_setup,
1391 : confdb_test_teardown),
1392 :
1393 : cmocka_unit_test_setup_teardown(test_get_next_domain,
1394 : setup_dom_tree, teardown_dom_tree),
1395 : cmocka_unit_test_setup_teardown(test_get_next_domain_descend,
1396 : setup_dom_tree, teardown_dom_tree),
1397 : cmocka_unit_test_setup_teardown(test_get_next_domain_disabled,
1398 : setup_dom_tree, teardown_dom_tree),
1399 :
1400 : cmocka_unit_test(test_well_known_sid_to_name),
1401 : cmocka_unit_test(test_name_to_well_known_sid),
1402 :
1403 : cmocka_unit_test_setup_teardown(test_sss_filter_sanitize_for_dom,
1404 : setup_dom_list,
1405 : teardown_dom_list),
1406 :
1407 : cmocka_unit_test(test_expand_homedir_template_NULL),
1408 : cmocka_unit_test_setup_teardown(test_expand_homedir_template,
1409 : setup_homedir_ctx,
1410 : teardown_homedir_ctx),
1411 : cmocka_unit_test(test_textual_public_key),
1412 : cmocka_unit_test(test_replace_whitespaces),
1413 : cmocka_unit_test(test_reverse_replace_whitespaces),
1414 : cmocka_unit_test(test_guid_blob_to_string_buf),
1415 : cmocka_unit_test(test_get_last_x_chars),
1416 : cmocka_unit_test_setup_teardown(test_add_strings_lists,
1417 : setup_add_strings_lists,
1418 : teardown_add_strings_lists),
1419 : cmocka_unit_test(test_sss_write_krb5_conf_snippet),
1420 : cmocka_unit_test_setup_teardown(test_fix_domain_in_name_list,
1421 : confdb_test_setup,
1422 : confdb_test_teardown),
1423 : cmocka_unit_test_setup_teardown(test_sss_unique_file,
1424 : unique_file_test_setup,
1425 : unique_file_test_teardown),
1426 : cmocka_unit_test_setup_teardown(test_sss_unique_file_destruct,
1427 : unique_file_test_setup,
1428 : unique_file_test_teardown),
1429 : cmocka_unit_test(test_sss_unique_file_neg),
1430 : cmocka_unit_test_setup_teardown(test_sss_unique_filename,
1431 : unique_file_test_setup,
1432 : unique_file_test_teardown),
1433 : cmocka_unit_test_setup_teardown(test_sss_unique_filename_destruct,
1434 : unique_file_test_setup,
1435 : unique_file_test_teardown),
1436 : };
1437 :
1438 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
1439 1 : debug_level = SSSDBG_INVALID;
1440 :
1441 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
1442 1 : while((opt = poptGetNextOpt(pc)) != -1) {
1443 : switch(opt) {
1444 : default:
1445 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
1446 : poptBadOption(pc, 0), poptStrerror(opt));
1447 0 : poptPrintUsage(pc, stderr, 0);
1448 0 : return 1;
1449 : }
1450 : }
1451 1 : poptFreeContext(pc);
1452 :
1453 1 : DEBUG_CLI_INIT(debug_level);
1454 :
1455 : /* Even though normally the tests should clean up after themselves
1456 : * they might not after a failed run. Remove the old db to be sure */
1457 1 : tests_set_cwd();
1458 1 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
1459 1 : test_dom_suite_setup(TESTS_PATH);
1460 :
1461 1 : rv = cmocka_run_group_tests(tests, NULL, NULL);
1462 1 : if (rv == 0) {
1463 1 : test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
1464 : }
1465 1 : return rv;
1466 : }
|