Line data Source code
1 : /*
2 : Authors:
3 : Pavel Reichl <preichl@redhat.com>
4 :
5 : Copyright (C) 2013 Red Hat
6 :
7 : SSSD tests - Search bases
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include <stdarg.h>
24 : #include <stdlib.h>
25 : #include <stddef.h>
26 : #include <setjmp.h>
27 : #include <unistd.h>
28 : #include <sys/types.h>
29 : #include <cmocka.h>
30 : #include <ldap.h>
31 :
32 : #include "util/find_uid.h"
33 : #include "util/sss_ldap.h"
34 : #include "tests/common.h"
35 : #include "providers/ldap/ldap_common.h"
36 : #include "providers/ldap/sdap.h"
37 : #include "dhash.h"
38 : #include "tests/common_check.h"
39 :
40 : enum sss_test_get_by_dn {
41 : DN_NOT_IN_DOMS, /* dn is not in any domain */
42 : DN_IN_DOM1, /* dn is in the domain based on dns */
43 : DN_IN_DOM2, /* dn is in the domain based on dns2 */
44 : };
45 :
46 8 : static struct sdap_search_base** generate_bases(TALLOC_CTX *mem_ctx,
47 : const char** dns, size_t n)
48 : {
49 : struct sdap_search_base **search_bases;
50 : errno_t err;
51 : int i;
52 :
53 8 : search_bases = talloc_array(mem_ctx, struct sdap_search_base *, n + 1);
54 8 : assert_non_null(search_bases);
55 :
56 19 : for (i=0; i < n; ++i) {
57 22 : err = sdap_create_search_base(mem_ctx, dns[i], LDAP_SCOPE_SUBTREE,
58 11 : NULL, &search_bases[i]);
59 11 : if (err != EOK) {
60 0 : fprintf(stderr, "Failed to create search base\n");
61 : }
62 11 : assert_int_equal(err, EOK);
63 : }
64 8 : search_bases[n] = NULL;
65 8 : return search_bases;
66 : }
67 :
68 2 : static bool do_test_search_bases(const char* dn, const char** dns, size_t n)
69 : {
70 : TALLOC_CTX *tmp_ctx;
71 : struct sdap_search_base **search_bases;
72 : bool ret;
73 :
74 2 : tmp_ctx = talloc_new(NULL);
75 2 : assert_non_null(tmp_ctx);
76 :
77 2 : search_bases = generate_bases(tmp_ctx, dns, n);
78 2 : check_leaks_push(tmp_ctx);
79 2 : ret = sss_ldap_dn_in_search_bases(tmp_ctx, dn, search_bases, NULL);
80 2 : assert_true(check_leaks_pop(tmp_ctx) == true);
81 :
82 2 : talloc_free(tmp_ctx);
83 2 : return ret;
84 : }
85 :
86 1 : void test_search_bases_fail(void **state)
87 : {
88 1 : const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
89 1 : const char *dns[] = {"dc=example, dc=com", "dc=subdom, dc=ad, dc=pb"};
90 : bool ret;
91 :
92 1 : ret = do_test_search_bases(dn, dns, 2);
93 1 : assert_false(ret);
94 1 : }
95 :
96 1 : void test_search_bases_success(void **state)
97 : {
98 1 : const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
99 1 : const char *dns[] = {"", "dc=ad, dc=pb", "dc=sub, dc=ad, dc=pb"};
100 : bool ret;
101 :
102 1 : ret = do_test_search_bases(dn, dns, 3);
103 1 : assert_true(ret);
104 1 : }
105 :
106 3 : static void do_test_get_by_dn(const char *dn, const char **dns, size_t n,
107 : const char **dns2, size_t n2, int expected_result)
108 : {
109 : TALLOC_CTX *tmp_ctx;
110 : struct sdap_options *opts;
111 : struct sdap_domain *sdom;
112 : struct sdap_domain *sdom2;
113 : struct sdap_domain *res_sdom;
114 : struct sdap_search_base **search_bases;
115 : struct sdap_search_base **search_bases2;
116 3 : tmp_ctx = talloc_new(NULL);
117 3 : assert_non_null(tmp_ctx);
118 :
119 3 : search_bases = generate_bases(tmp_ctx, dns, n);
120 3 : search_bases2 = generate_bases(tmp_ctx, dns2, n2);
121 3 : sdom = talloc_zero(tmp_ctx, struct sdap_domain);
122 3 : assert_non_null(sdom);
123 3 : sdom2 = talloc_zero(tmp_ctx, struct sdap_domain);
124 3 : assert_non_null(sdom2);
125 :
126 3 : sdom->search_bases = search_bases;
127 3 : sdom->next = sdom2;
128 3 : sdom->prev = NULL;
129 3 : sdom2->search_bases = search_bases2;
130 3 : sdom2->next = NULL;
131 3 : sdom2->prev = sdom;
132 :
133 3 : opts = talloc(tmp_ctx, struct sdap_options);
134 3 : assert_non_null(opts);
135 3 : opts->sdom = sdom;
136 3 : res_sdom = sdap_domain_get_by_dn(opts, dn);
137 :
138 3 : switch (expected_result) {
139 : case DN_NOT_IN_DOMS:
140 1 : assert_null(res_sdom);
141 1 : break;
142 : case DN_IN_DOM1:
143 1 : assert_true(res_sdom == sdom);
144 1 : break;
145 : case DN_IN_DOM2:
146 1 : assert_true(res_sdom == sdom2);
147 1 : break;
148 : }
149 :
150 3 : talloc_free(tmp_ctx);
151 3 : }
152 :
153 1 : void test_get_by_dn(void **state)
154 : {
155 1 : const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
156 1 : const char *dns[] = {"dc=ad, dc=pb"};
157 1 : const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
158 :
159 1 : do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_IN_DOM2);
160 1 : }
161 :
162 1 : void test_get_by_dn2(void **state)
163 : {
164 1 : const char *dn = "cn=user, dc=ad, dc=com";
165 1 : const char *dns[] = {"dc=ad, dc=com"};
166 1 : const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
167 :
168 1 : do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_IN_DOM1);
169 1 : }
170 :
171 1 : void test_get_by_dn_fail(void **state)
172 : {
173 1 : const char *dn = "cn=user, dc=sub, dc=example, dc=com";
174 1 : const char *dns[] = {"dc=ad, dc=pb"};
175 1 : const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
176 :
177 1 : do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_NOT_IN_DOMS);
178 1 : }
179 :
180 1 : int main(void)
181 : {
182 1 : const struct CMUnitTest tests[] = {
183 : cmocka_unit_test(test_search_bases_fail),
184 : cmocka_unit_test(test_search_bases_success),
185 : cmocka_unit_test(test_get_by_dn_fail),
186 : cmocka_unit_test(test_get_by_dn),
187 : cmocka_unit_test(test_get_by_dn2)
188 : };
189 :
190 1 : return cmocka_run_group_tests(tests, NULL, NULL);
191 : }
|