Line data Source code
1 : /*
2 : Authors:
3 : Jakub Hrozek <jhrozek@redhat.com>
4 :
5 : Copyright (C) 2015 Red Hat
6 :
7 : SSSD tests: IPA subdomain util tests
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 <talloc.h>
24 : #include <tevent.h>
25 : #include <errno.h>
26 : #include <popt.h>
27 : #include <arpa/inet.h>
28 : #include <netinet/in.h>
29 : #include <sys/types.h>
30 : #include <stdarg.h>
31 : #include <stdlib.h>
32 :
33 : #include "providers/ipa/ipa_subdomains.h"
34 : #include "tests/cmocka/common_mock.h"
35 : #include "tests/cmocka/common_mock_resp.h"
36 :
37 : struct test_ipa_subdom_ctx {
38 : struct ldb_context *ldb;
39 : };
40 :
41 3 : static int test_ipa_subdom_setup(void **state)
42 : {
43 : struct test_ipa_subdom_ctx *test_ctx;
44 :
45 3 : assert_true(leak_check_setup());
46 :
47 3 : test_ctx = talloc_zero(global_talloc_context, struct test_ipa_subdom_ctx);
48 3 : assert_non_null(test_ctx);
49 :
50 3 : test_ctx->ldb = ldb_init(test_ctx, NULL);
51 3 : assert_non_null(test_ctx->ldb);
52 :
53 3 : check_leaks_push(test_ctx);
54 3 : *state = test_ctx;
55 3 : return 0;
56 : }
57 :
58 3 : static int test_ipa_subdom_teardown(void **state)
59 : {
60 : struct test_ipa_subdom_ctx *test_ctx;
61 :
62 3 : test_ctx = talloc_get_type(*state, struct test_ipa_subdom_ctx);
63 3 : assert_non_null(test_ctx);
64 :
65 3 : assert_true(check_leaks_pop(test_ctx) == true);
66 3 : talloc_free(test_ctx);
67 3 : assert_true(leak_check_teardown());
68 3 : return 0;
69 : }
70 :
71 6 : static struct sysdb_attrs *dn_attrs(TALLOC_CTX *mem_ctx, const char *dn)
72 : {
73 : struct sysdb_attrs *attrs;
74 : int rv;
75 :
76 6 : attrs = sysdb_new_attrs(mem_ctx);
77 6 : assert_non_null(attrs);
78 :
79 6 : rv = sysdb_attrs_add_string(attrs, SYSDB_ORIG_DN, dn);
80 6 : assert_int_equal(rv, EOK);
81 :
82 6 : return attrs;
83 : }
84 :
85 1 : static void test_ipa_subdom_ldb_dn(void **state)
86 : {
87 : struct ldb_dn *dn;
88 : struct sysdb_attrs *attrs;
89 : struct test_ipa_subdom_ctx *test_ctx;
90 :
91 1 : test_ctx = talloc_get_type(*state, struct test_ipa_subdom_ctx);
92 1 : assert_non_null(test_ctx);
93 :
94 1 : attrs = dn_attrs(test_ctx, "dc=foo,dc=bar");
95 1 : assert_non_null(attrs);
96 :
97 1 : dn = ipa_subdom_ldb_dn(test_ctx, test_ctx->ldb, attrs);
98 1 : assert_non_null(dn);
99 1 : assert_string_equal(ldb_dn_get_linearized(dn), "dc=foo,dc=bar");
100 :
101 1 : talloc_free(dn);
102 1 : talloc_free(attrs);
103 1 : }
104 :
105 1 : static void test_ipa_subdom_ldb_dn_fail(void **state)
106 : {
107 : struct ldb_dn *dn;
108 : struct sysdb_attrs *attrs;
109 : struct test_ipa_subdom_ctx *test_ctx;
110 :
111 1 : test_ctx = talloc_get_type(*state, struct test_ipa_subdom_ctx);
112 1 : assert_non_null(test_ctx);
113 :
114 1 : attrs = dn_attrs(test_ctx, "notadn");
115 1 : assert_non_null(attrs);
116 :
117 1 : dn = ipa_subdom_ldb_dn(test_ctx, NULL, NULL);
118 1 : assert_null(dn);
119 :
120 1 : dn = ipa_subdom_ldb_dn(test_ctx, test_ctx->ldb, attrs);
121 1 : assert_null(dn);
122 1 : talloc_free(attrs);
123 :
124 1 : attrs = sysdb_new_attrs(test_ctx);
125 1 : assert_non_null(attrs);
126 1 : dn = ipa_subdom_ldb_dn(test_ctx, test_ctx->ldb, attrs);
127 1 : assert_null(dn);
128 1 : talloc_free(attrs);
129 1 : }
130 :
131 4 : static struct ldb_dn *get_dn(TALLOC_CTX *mem_ctx,
132 : struct ldb_context *ldb,
133 : const char *strdn)
134 : {
135 : struct ldb_dn *dn;
136 : struct sysdb_attrs *attrs;
137 :
138 4 : attrs = dn_attrs(mem_ctx, strdn);
139 4 : assert_non_null(attrs);
140 :
141 4 : dn = ipa_subdom_ldb_dn(mem_ctx, ldb, attrs);
142 4 : talloc_free(attrs);
143 4 : assert_non_null(dn);
144 :
145 4 : return dn;
146 : }
147 :
148 1 : static void test_ipa_subdom_is_member_dom(void **state)
149 : {
150 : struct ldb_dn *dn;
151 : struct test_ipa_subdom_ctx *test_ctx;
152 : bool is_member;
153 :
154 1 : test_ctx = talloc_get_type(*state, struct test_ipa_subdom_ctx);
155 :
156 1 : dn = get_dn(test_ctx, test_ctx->ldb,
157 : "cn=SUB.AD.DOM,cn=AD.DOM,cn=ad,cn=trusts,dc=example,dc=com");
158 1 : is_member = ipa_subdom_is_member_dom(dn);
159 1 : talloc_free(dn);
160 1 : assert_true(is_member);
161 :
162 1 : dn = get_dn(test_ctx, test_ctx->ldb,
163 : "cn=AD.DOM,cn=ad,cn=trusts,dc=example,dc=com");
164 1 : is_member = ipa_subdom_is_member_dom(dn);
165 1 : talloc_free(dn);
166 1 : assert_false(is_member);
167 :
168 1 : dn = get_dn(test_ctx, test_ctx->ldb,
169 : "cn=SUB.AD.DOM,cn=AD.DOM,cn=ad,cn=XXX,dc=example,dc=com");
170 1 : is_member = ipa_subdom_is_member_dom(dn);
171 1 : talloc_free(dn);
172 1 : assert_false(is_member);
173 :
174 1 : dn = get_dn(test_ctx, test_ctx->ldb,
175 : "cn=SUB.AD.DOM,cn=AD.DOM,cn=YYY,cn=trusts,dc=example,dc=com");
176 1 : is_member = ipa_subdom_is_member_dom(dn);
177 1 : talloc_free(dn);
178 1 : assert_false(is_member);
179 1 : }
180 :
181 1 : int main(int argc, const char *argv[])
182 : {
183 : int rv;
184 : poptContext pc;
185 : int opt;
186 6 : struct poptOption long_options[] = {
187 : POPT_AUTOHELP
188 5 : SSSD_DEBUG_OPTS
189 : POPT_TABLEEND
190 : };
191 :
192 1 : const struct CMUnitTest tests[] = {
193 : cmocka_unit_test_setup_teardown(test_ipa_subdom_ldb_dn,
194 : test_ipa_subdom_setup,
195 : test_ipa_subdom_teardown),
196 : cmocka_unit_test_setup_teardown(test_ipa_subdom_ldb_dn_fail,
197 : test_ipa_subdom_setup,
198 : test_ipa_subdom_teardown),
199 : cmocka_unit_test_setup_teardown(test_ipa_subdom_is_member_dom,
200 : test_ipa_subdom_setup,
201 : test_ipa_subdom_teardown),
202 : };
203 :
204 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
205 1 : debug_level = SSSDBG_INVALID;
206 :
207 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
208 1 : while((opt = poptGetNextOpt(pc)) != -1) {
209 : switch(opt) {
210 : default:
211 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
212 : poptBadOption(pc, 0), poptStrerror(opt));
213 0 : poptPrintUsage(pc, stderr, 0);
214 0 : return 1;
215 : }
216 : }
217 1 : poptFreeContext(pc);
218 :
219 1 : DEBUG_CLI_INIT(debug_level);
220 :
221 : /* Even though normally the tests should clean up after themselves
222 : * they might not after a failed run. Remove the old db to be sure */
223 1 : tests_set_cwd();
224 :
225 1 : rv = cmocka_run_group_tests(tests, NULL, NULL);
226 1 : return rv;
227 : }
|