Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Copyright (C) 2018-2019 Andreas Schneider <asn@samba.org>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <stdarg.h>
21 : #include <stddef.h>
22 : #include <stdint.h>
23 : #include <setjmp.h>
24 : #include <cmocka.h>
25 :
26 : #include "lib/replace/replace.h"
27 : #include "auth/credentials/credentials.c"
28 :
29 1 : static int setup_talloc_context(void **state)
30 : {
31 1 : TALLOC_CTX *frame = talloc_stackframe();
32 :
33 1 : *state = frame;
34 1 : return 0;
35 : }
36 :
37 1 : static int teardown_talloc_context(void **state)
38 : {
39 1 : TALLOC_CTX *frame = *state;
40 1 : TALLOC_FREE(frame);
41 1 : return 0;
42 : }
43 :
44 1 : static void torture_creds_init(void **state)
45 : {
46 1 : TALLOC_CTX *mem_ctx = *state;
47 1 : struct cli_credentials *creds = NULL;
48 1 : const char *username = NULL;
49 1 : const char *domain = NULL;
50 1 : const char *password = NULL;
51 1 : enum credentials_obtained usr_obtained = CRED_UNINITIALISED;
52 1 : enum credentials_obtained pwd_obtained = CRED_UNINITIALISED;
53 1 : bool ok;
54 :
55 1 : creds = cli_credentials_init(mem_ctx);
56 1 : assert_non_null(creds);
57 1 : assert_null(creds->username);
58 1 : assert_int_equal(creds->username_obtained, CRED_UNINITIALISED);
59 :
60 1 : domain = cli_credentials_get_domain(creds);
61 1 : assert_null(domain);
62 1 : ok = cli_credentials_set_domain(creds, "WURST", CRED_SPECIFIED);
63 1 : assert_true(ok);
64 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
65 1 : domain = cli_credentials_get_domain(creds);
66 1 : assert_string_equal(domain, "WURST");
67 :
68 1 : username = cli_credentials_get_username(creds);
69 1 : assert_null(username);
70 1 : ok = cli_credentials_set_username(creds, "brot", CRED_SPECIFIED);
71 1 : assert_true(ok);
72 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
73 1 : username = cli_credentials_get_username(creds);
74 1 : assert_string_equal(username, "brot");
75 :
76 1 : username = cli_credentials_get_username_and_obtained(creds,
77 : &usr_obtained);
78 1 : assert_int_equal(usr_obtained, CRED_SPECIFIED);
79 1 : assert_string_equal(username, "brot");
80 :
81 1 : password = cli_credentials_get_password(creds);
82 1 : assert_null(password);
83 1 : ok = cli_credentials_set_password(creds, "SECRET", CRED_SPECIFIED);
84 1 : assert_true(ok);
85 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
86 1 : password = cli_credentials_get_password(creds);
87 1 : assert_string_equal(password, "SECRET");
88 :
89 1 : password = cli_credentials_get_password_and_obtained(creds,
90 : &pwd_obtained);
91 1 : assert_int_equal(pwd_obtained, CRED_SPECIFIED);
92 1 : assert_string_equal(password, "SECRET");
93 :
94 : /* Run dump to check it works */
95 1 : cli_credentials_dump(creds);
96 1 : }
97 :
98 1 : static void torture_creds_init_anonymous(void **state)
99 : {
100 1 : TALLOC_CTX *mem_ctx = *state;
101 1 : struct cli_credentials *creds = NULL;
102 :
103 1 : creds = cli_credentials_init_anon(mem_ctx);
104 1 : assert_non_null(creds);
105 :
106 1 : assert_string_equal(creds->domain, "");
107 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
108 :
109 1 : assert_string_equal(creds->username, "");
110 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
111 :
112 1 : assert_null(creds->password);
113 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
114 1 : }
115 :
116 1 : static void torture_creds_guess(void **state)
117 : {
118 1 : TALLOC_CTX *mem_ctx = *state;
119 1 : struct cli_credentials *creds = NULL;
120 1 : const char *env_user = getenv("USER");
121 1 : bool ok;
122 :
123 1 : creds = cli_credentials_init(mem_ctx);
124 1 : assert_non_null(creds);
125 :
126 1 : setenv("PASSWD", "SECRET", 1);
127 1 : ok = cli_credentials_guess(creds, NULL);
128 1 : assert_true(ok);
129 :
130 1 : assert_string_equal(creds->username, env_user);
131 1 : assert_int_equal(creds->username_obtained, CRED_GUESS_ENV);
132 :
133 1 : assert_string_equal(creds->password, "SECRET");
134 1 : assert_int_equal(creds->password_obtained, CRED_GUESS_ENV);
135 1 : unsetenv("PASSWD");
136 1 : }
137 :
138 1 : static void torture_creds_anon_guess(void **state)
139 : {
140 1 : TALLOC_CTX *mem_ctx = *state;
141 1 : struct cli_credentials *creds = NULL;
142 1 : bool ok;
143 :
144 1 : creds = cli_credentials_init_anon(mem_ctx);
145 1 : assert_non_null(creds);
146 :
147 1 : setenv("PASSWD", "SECRET", 1);
148 1 : ok = cli_credentials_guess(creds, NULL);
149 1 : assert_true(ok);
150 :
151 1 : assert_string_equal(creds->username, "");
152 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
153 :
154 1 : assert_null(creds->password);
155 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
156 1 : unsetenv("PASSWD");
157 1 : }
158 :
159 1 : static void torture_creds_parse_string(void **state)
160 : {
161 1 : TALLOC_CTX *mem_ctx = *state;
162 1 : struct cli_credentials *creds = NULL;
163 :
164 1 : creds = cli_credentials_init(mem_ctx);
165 1 : assert_non_null(creds);
166 :
167 : /* Anonymous */
168 1 : cli_credentials_parse_string(creds, "%", CRED_SPECIFIED);
169 :
170 1 : assert_string_equal(creds->domain, "");
171 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
172 :
173 1 : assert_string_equal(creds->username, "");
174 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
175 :
176 1 : assert_null(creds->password);
177 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
178 :
179 : /* Username + password */
180 1 : cli_credentials_parse_string(creds, "wurst%BROT", CRED_SPECIFIED);
181 :
182 1 : assert_string_equal(creds->domain, "");
183 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
184 :
185 1 : assert_string_equal(creds->username, "wurst");
186 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
187 :
188 1 : assert_string_equal(creds->password, "BROT");
189 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
190 :
191 : /* Domain + username + password */
192 1 : cli_credentials_parse_string(creds, "XXL\\wurst%BROT", CRED_SPECIFIED);
193 :
194 1 : assert_string_equal(creds->domain, "XXL");
195 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
196 :
197 1 : assert_string_equal(creds->username, "wurst");
198 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
199 :
200 1 : assert_string_equal(creds->password, "BROT");
201 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
202 :
203 : /* Principal */
204 1 : cli_credentials_parse_string(creds, "wurst@brot.realm", CRED_SPECIFIED);
205 :
206 1 : assert_string_equal(creds->domain, "");
207 1 : assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
208 :
209 1 : assert_string_equal(creds->username, "wurst@brot.realm");
210 1 : assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
211 :
212 1 : assert_string_equal(creds->principal, "wurst@brot.realm");
213 1 : assert_int_equal(creds->principal_obtained, CRED_SPECIFIED);
214 :
215 1 : assert_string_equal(creds->password, "BROT");
216 1 : assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
217 1 : }
218 :
219 1 : static void torture_creds_krb5_state(void **state)
220 : {
221 1 : TALLOC_CTX *mem_ctx = *state;
222 1 : struct cli_credentials *creds = NULL;
223 1 : struct loadparm_context *lp_ctx = NULL;
224 1 : bool ok;
225 :
226 1 : lp_ctx = loadparm_init_global(true);
227 1 : assert_non_null(lp_ctx);
228 :
229 1 : creds = cli_credentials_init(mem_ctx);
230 1 : assert_non_null(creds);
231 1 : assert_int_equal(creds->kerberos_state_obtained, CRED_UNINITIALISED);
232 1 : assert_int_equal(creds->kerberos_state, CRED_USE_KERBEROS_DESIRED);
233 :
234 1 : ok = cli_credentials_set_conf(creds, lp_ctx);
235 1 : assert_true(ok);
236 1 : assert_int_equal(creds->kerberos_state_obtained, CRED_SMB_CONF);
237 1 : assert_int_equal(creds->kerberos_state, CRED_USE_KERBEROS_DESIRED);
238 :
239 1 : ok = cli_credentials_guess(creds, lp_ctx);
240 1 : assert_true(ok);
241 1 : assert_int_equal(creds->kerberos_state_obtained, CRED_SMB_CONF);
242 1 : assert_int_equal(creds->kerberos_state, CRED_USE_KERBEROS_DESIRED);
243 1 : assert_int_equal(creds->ccache_obtained, CRED_GUESS_FILE);
244 1 : assert_non_null(creds->ccache);
245 :
246 1 : ok = cli_credentials_set_kerberos_state(creds,
247 : CRED_USE_KERBEROS_REQUIRED,
248 : CRED_SPECIFIED);
249 1 : assert_true(ok);
250 1 : assert_int_equal(creds->kerberos_state_obtained, CRED_SPECIFIED);
251 1 : assert_int_equal(creds->kerberos_state, CRED_USE_KERBEROS_REQUIRED);
252 :
253 1 : ok = cli_credentials_set_kerberos_state(creds,
254 : CRED_USE_KERBEROS_DISABLED,
255 : CRED_SMB_CONF);
256 1 : assert_false(ok);
257 1 : assert_int_equal(creds->kerberos_state_obtained, CRED_SPECIFIED);
258 1 : assert_int_equal(creds->kerberos_state, CRED_USE_KERBEROS_REQUIRED);
259 :
260 1 : }
261 :
262 1 : static void torture_creds_gensec_feature(void **state)
263 : {
264 1 : TALLOC_CTX *mem_ctx = *state;
265 1 : struct cli_credentials *creds = NULL;
266 1 : bool ok;
267 :
268 1 : creds = cli_credentials_init(mem_ctx);
269 1 : assert_non_null(creds);
270 1 : assert_int_equal(creds->gensec_features_obtained, CRED_UNINITIALISED);
271 1 : assert_int_equal(creds->gensec_features, 0);
272 :
273 1 : ok = cli_credentials_set_gensec_features(creds,
274 : GENSEC_FEATURE_SIGN,
275 : CRED_SPECIFIED);
276 1 : assert_true(ok);
277 1 : assert_int_equal(creds->gensec_features_obtained, CRED_SPECIFIED);
278 1 : assert_int_equal(creds->gensec_features, GENSEC_FEATURE_SIGN);
279 :
280 1 : ok = cli_credentials_set_gensec_features(creds,
281 : GENSEC_FEATURE_SEAL,
282 : CRED_SMB_CONF);
283 1 : assert_false(ok);
284 1 : assert_int_equal(creds->gensec_features_obtained, CRED_SPECIFIED);
285 1 : assert_int_equal(creds->gensec_features, GENSEC_FEATURE_SIGN);
286 1 : }
287 :
288 1 : int main(int argc, char *argv[])
289 : {
290 1 : int rc;
291 1 : const struct CMUnitTest tests[] = {
292 : cmocka_unit_test(torture_creds_init),
293 : cmocka_unit_test(torture_creds_init_anonymous),
294 : cmocka_unit_test(torture_creds_guess),
295 : cmocka_unit_test(torture_creds_anon_guess),
296 : cmocka_unit_test(torture_creds_parse_string),
297 : cmocka_unit_test(torture_creds_krb5_state),
298 : cmocka_unit_test(torture_creds_gensec_feature),
299 : };
300 :
301 1 : if (argc == 2) {
302 0 : cmocka_set_test_filter(argv[1]);
303 : }
304 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
305 :
306 1 : rc = cmocka_run_group_tests(tests,
307 : setup_talloc_context,
308 : teardown_talloc_context);
309 :
310 1 : return rc;
311 : }
|