Line data Source code
1 : /*
2 : Authors:
3 : Sumit Bose <sbose@redhat.com>
4 :
5 : Copyright (C) 2014 Red Hat
6 :
7 : SSSD tests: Unit tests for id-mapping in the IPA provider
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 <popt.h>
24 :
25 : #include "tests/cmocka/common_mock.h"
26 : #include "lib/idmap/sss_idmap.h"
27 : #include "providers/ipa/ipa_common.h"
28 : #include "providers/ldap/sdap_idmap.h"
29 :
30 : #define RANGE_NAME discard_const("range1")
31 : #define DOMAIN_SID discard_const("S-1-5-21-2-3-4")
32 : #define DOMAIN_NAME discard_const("dom.test")
33 : #define BASE_RID 111
34 : #define SECONDARY_BASE_RID 11223344
35 : #define BASE_ID 123456
36 : #define RANGE_SIZE 222222
37 : #define RANGE_MAX (BASE_ID + RANGE_SIZE - 1)
38 :
39 1 : void test_get_idmap_data_from_range(void **state)
40 : {
41 : char *dom_name;
42 : char *sid;
43 : uint32_t rid;
44 : struct sss_idmap_range range;
45 : bool external_mapping;
46 : size_t c;
47 : errno_t ret;
48 :
49 : struct test_data {
50 : struct range_info r;
51 : errno_t exp_ret;
52 : char *exp_dom_name;
53 : char *exp_sid;
54 : uint32_t exp_rid;
55 : struct sss_idmap_range exp_range;
56 : bool exp_external_mapping;
57 1 : } d[] = {
58 : /* working IPA_RANGE_LOCAL range */
59 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
60 : NULL, discard_const(IPA_RANGE_LOCAL)},
61 : EOK, DOMAIN_NAME, NULL, 0, {BASE_ID, RANGE_MAX}, true},
62 : /* working old-style IPA_RANGE_LOCAL range without range type */
63 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
64 : NULL, NULL},
65 : EOK, DOMAIN_NAME, NULL, 0, {BASE_ID, RANGE_MAX}, true},
66 : /* old-style IPA_RANGE_LOCAL without SID and secondary base rid */
67 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, NULL, NULL},
68 : EINVAL, NULL, NULL, 0, {0, 0}, false},
69 : /* old-style range with SID and secondary base rid */
70 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
71 : DOMAIN_SID, NULL},
72 : EINVAL, NULL, NULL, 0, {0, 0}, false},
73 : /* working IPA_RANGE_AD_TRUST range */
74 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID,
75 : discard_const(IPA_RANGE_AD_TRUST)},
76 : EOK, DOMAIN_SID, DOMAIN_SID, BASE_RID, {BASE_ID, RANGE_MAX}, false},
77 : /* working old-style IPA_RANGE_AD_TRUST range without range type */
78 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID, NULL},
79 : EOK, DOMAIN_SID, DOMAIN_SID, BASE_RID, {BASE_ID, RANGE_MAX}, false},
80 : /* working IPA_RANGE_AD_TRUST_POSIX range */
81 : {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID,
82 : discard_const(IPA_RANGE_AD_TRUST_POSIX)},
83 : EOK, DOMAIN_SID, DOMAIN_SID, 0, {BASE_ID, RANGE_MAX}, true},
84 : {{0}, 0, NULL, NULL, 0, {0, 0}, false}
85 : };
86 :
87 3 : for (c = 0; d[c].exp_dom_name != NULL; c++) {
88 2 : ret = get_idmap_data_from_range(&d[c].r, DOMAIN_NAME, &dom_name, &sid,
89 : &rid, &range, &external_mapping);
90 2 : assert_int_equal(ret, d[c].exp_ret);
91 2 : assert_string_equal(dom_name, d[c].exp_dom_name);
92 2 : if (d[c].exp_sid == NULL) {
93 2 : assert_null(sid);
94 : } else {
95 0 : assert_string_equal(sid, d[c].exp_sid);
96 : }
97 2 : assert_int_equal(rid, d[c].exp_rid);
98 2 : assert_int_equal(range.min, d[c].exp_range.min);
99 2 : assert_int_equal(range.max, d[c].exp_range.max);
100 2 : assert_true(external_mapping == d[c].exp_external_mapping);
101 : }
102 1 : }
103 :
104 3 : errno_t __wrap_sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
105 : size_t *range_count,
106 : struct range_info ***range_list)
107 : {
108 :
109 3 : *range_count = sss_mock_type(size_t);
110 3 : *range_list = talloc_steal(mem_ctx,
111 : sss_mock_ptr_type(struct range_info **));
112 3 : return EOK;
113 : }
114 :
115 : struct test_ctx {
116 : struct sdap_idmap_ctx *idmap_ctx;
117 : struct sdap_id_ctx *sdap_id_ctx;
118 : };
119 :
120 3 : static struct range_info **get_range_list(TALLOC_CTX *mem_ctx)
121 : {
122 : struct range_info **range_list;
123 :
124 3 : range_list = talloc_array(mem_ctx, struct range_info *, 2);
125 3 : assert_non_null(range_list);
126 :
127 3 : range_list[0] = talloc_zero(range_list, struct range_info);
128 3 : assert_non_null(range_list[0]);
129 :
130 3 : range_list[0]->name = talloc_strdup(range_list[0], RANGE_NAME);
131 3 : assert_non_null( range_list[0]->name);
132 3 : range_list[0]->base_id = BASE_ID;
133 3 : range_list[0]->id_range_size = RANGE_SIZE;
134 3 : range_list[0]->base_rid = BASE_RID;
135 3 : range_list[0]->secondary_base_rid = 0;
136 3 : range_list[0]->trusted_dom_sid = talloc_strdup(range_list[0], DOMAIN_SID);
137 3 : assert_non_null(range_list[0]->trusted_dom_sid);
138 3 : range_list[0]->range_type = talloc_strdup(range_list[0],
139 : IPA_RANGE_AD_TRUST);
140 3 : assert_non_null(range_list[0]->range_type);
141 :
142 3 : return range_list;
143 : }
144 :
145 1 : static int setup_idmap_ctx(void **state)
146 : {
147 : int ret;
148 : struct test_ctx *test_ctx;
149 :
150 1 : assert_true(leak_check_setup());
151 :
152 1 : test_ctx = talloc_zero(global_talloc_context, struct test_ctx);
153 1 : assert_non_null(test_ctx);
154 :
155 1 : test_ctx->sdap_id_ctx = talloc_zero(test_ctx,
156 : struct sdap_id_ctx);
157 1 : assert_non_null(test_ctx->sdap_id_ctx);
158 :
159 1 : test_ctx->sdap_id_ctx->be = talloc_zero(test_ctx->sdap_id_ctx,
160 : struct be_ctx);
161 1 : assert_non_null(test_ctx->sdap_id_ctx->be);
162 :
163 1 : test_ctx->sdap_id_ctx->be->domain = talloc_zero(test_ctx->sdap_id_ctx->be,
164 : struct sss_domain_info);
165 1 : assert_non_null(test_ctx->sdap_id_ctx->be->domain);
166 :
167 2 : test_ctx->sdap_id_ctx->be->domain->name =
168 1 : talloc_strdup(test_ctx->sdap_id_ctx->be->domain, DOMAIN_NAME);
169 1 : assert_non_null(test_ctx->sdap_id_ctx->be->domain->name);
170 :
171 1 : will_return(__wrap_sysdb_get_ranges, 1);
172 1 : will_return(__wrap_sysdb_get_ranges, get_range_list(global_talloc_context));
173 :
174 1 : ret = ipa_idmap_init(test_ctx, test_ctx->sdap_id_ctx,
175 : &test_ctx->idmap_ctx);
176 1 : assert_int_equal(ret, EOK);
177 :
178 1 : check_leaks_push(test_ctx);
179 1 : *state = test_ctx;
180 1 : return 0;
181 : }
182 :
183 1 : static int teardown_idmap_ctx(void **state)
184 : {
185 1 : struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
186 :
187 1 : assert_non_null(test_ctx);
188 :
189 1 : assert_true(check_leaks_pop(test_ctx) == true);
190 :
191 1 : talloc_free(test_ctx);
192 1 : assert_true(leak_check_teardown());
193 1 : return 0;
194 : }
195 :
196 1 : void test_ipa_idmap_get_ranges_from_sysdb(void **state)
197 : {
198 : int ret;
199 1 : struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
200 1 : assert_non_null(test_ctx);
201 :
202 1 : will_return(__wrap_sysdb_get_ranges, 1);
203 1 : will_return(__wrap_sysdb_get_ranges, get_range_list(test_ctx->idmap_ctx));
204 1 : ret = ipa_idmap_get_ranges_from_sysdb(test_ctx->idmap_ctx,
205 : DOMAIN_NAME, DOMAIN_SID, true);
206 1 : assert_int_equal(ret, EOK);
207 :
208 1 : will_return(__wrap_sysdb_get_ranges, 1);
209 1 : will_return(__wrap_sysdb_get_ranges, get_range_list(global_talloc_context));
210 1 : ret = ipa_idmap_get_ranges_from_sysdb(test_ctx->idmap_ctx,
211 : DOMAIN_NAME, DOMAIN_SID, false);
212 1 : assert_int_equal(ret, EIO);
213 1 : }
214 :
215 1 : int main(int argc, const char *argv[])
216 : {
217 : poptContext pc;
218 : int opt;
219 6 : struct poptOption long_options[] = {
220 : POPT_AUTOHELP
221 5 : SSSD_DEBUG_OPTS
222 : POPT_TABLEEND
223 : };
224 :
225 1 : const struct CMUnitTest tests[] = {
226 : cmocka_unit_test(test_get_idmap_data_from_range),
227 : cmocka_unit_test_setup_teardown(test_ipa_idmap_get_ranges_from_sysdb,
228 : setup_idmap_ctx, teardown_idmap_ctx),
229 : };
230 :
231 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
232 1 : debug_level = SSSDBG_INVALID;
233 :
234 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
235 1 : while((opt = poptGetNextOpt(pc)) != -1) {
236 : switch(opt) {
237 : default:
238 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
239 : poptBadOption(pc, 0), poptStrerror(opt));
240 0 : poptPrintUsage(pc, stderr, 0);
241 0 : return 1;
242 : }
243 : }
244 1 : poptFreeContext(pc);
245 :
246 1 : DEBUG_CLI_INIT(debug_level);
247 :
248 1 : tests_set_cwd();
249 :
250 1 : return cmocka_run_group_tests(tests, NULL, NULL);
251 : }
|