2 * Unix SMB/CIFS implementation.
4 * Copyright (C) 2018-2019 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
26 #include "lib/replace/replace.h"
27 #include "auth/credentials/credentials.c"
29 static int setup_talloc_context(void **state)
31 TALLOC_CTX *frame = talloc_stackframe();
37 static int teardown_talloc_context(void **state)
39 TALLOC_CTX *frame = *state;
44 static void torture_creds_init(void **state)
46 TALLOC_CTX *mem_ctx = *state;
47 struct cli_credentials *creds = NULL;
48 const char *username = NULL;
49 const char *domain = NULL;
50 const char *password = NULL;
53 creds = cli_credentials_init(mem_ctx);
54 assert_non_null(creds);
55 assert_null(creds->username);
56 assert_int_equal(creds->username_obtained, CRED_UNINITIALISED);
58 domain = cli_credentials_get_domain(creds);
60 ok = cli_credentials_set_domain(creds, "WURST", CRED_SPECIFIED);
62 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
63 domain = cli_credentials_get_domain(creds);
64 assert_string_equal(domain, "WURST");
66 username = cli_credentials_get_username(creds);
67 assert_null(username);
68 ok = cli_credentials_set_username(creds, "brot", CRED_SPECIFIED);
70 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
71 username = cli_credentials_get_username(creds);
72 assert_string_equal(username, "brot");
74 password = cli_credentials_get_password(creds);
75 assert_null(password);
76 ok = cli_credentials_set_password(creds, "SECRET", CRED_SPECIFIED);
78 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
79 password = cli_credentials_get_password(creds);
80 assert_string_equal(password, "SECRET");
83 static void torture_creds_init_anonymous(void **state)
85 TALLOC_CTX *mem_ctx = *state;
86 struct cli_credentials *creds = NULL;
88 creds = cli_credentials_init_anon(mem_ctx);
89 assert_non_null(creds);
91 assert_string_equal(creds->domain, "");
92 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
94 assert_string_equal(creds->username, "");
95 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
97 assert_null(creds->password);
98 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
101 static void torture_creds_guess(void **state)
103 TALLOC_CTX *mem_ctx = *state;
104 struct cli_credentials *creds = NULL;
105 const char *env_user = getenv("USER");
107 creds = cli_credentials_init(mem_ctx);
108 assert_non_null(creds);
110 setenv("PASSWD", "SECRET", 1);
111 cli_credentials_guess(creds, NULL);
113 assert_string_equal(creds->username, env_user);
114 assert_int_equal(creds->username_obtained, CRED_GUESS_ENV);
116 assert_string_equal(creds->password, "SECRET");
117 assert_int_equal(creds->password_obtained, CRED_GUESS_ENV);
121 static void torture_creds_anon_guess(void **state)
123 TALLOC_CTX *mem_ctx = *state;
124 struct cli_credentials *creds = NULL;
126 creds = cli_credentials_init_anon(mem_ctx);
127 assert_non_null(creds);
129 setenv("PASSWD", "SECRET", 1);
130 cli_credentials_guess(creds, NULL);
132 assert_string_equal(creds->username, "");
133 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
135 assert_null(creds->password);
136 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
140 static void torture_creds_parse_string(void **state)
142 TALLOC_CTX *mem_ctx = *state;
143 struct cli_credentials *creds = NULL;
145 creds = cli_credentials_init(mem_ctx);
146 assert_non_null(creds);
149 cli_credentials_parse_string(creds, "%", CRED_SPECIFIED);
151 assert_string_equal(creds->domain, "");
152 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
154 assert_string_equal(creds->username, "");
155 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
157 assert_null(creds->password);
158 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
160 /* Username + password */
161 cli_credentials_parse_string(creds, "wurst%BROT", CRED_SPECIFIED);
163 assert_string_equal(creds->domain, "");
164 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
166 assert_string_equal(creds->username, "wurst");
167 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
169 assert_string_equal(creds->password, "BROT");
170 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
172 /* Domain + username + password */
173 cli_credentials_parse_string(creds, "XXL\\wurst%BROT", CRED_SPECIFIED);
175 assert_string_equal(creds->domain, "XXL");
176 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
178 assert_string_equal(creds->username, "wurst");
179 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
181 assert_string_equal(creds->password, "BROT");
182 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
185 cli_credentials_parse_string(creds, "wurst@brot.realm", CRED_SPECIFIED);
187 assert_string_equal(creds->domain, "");
188 assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
190 assert_string_equal(creds->username, "wurst@brot.realm");
191 assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
193 assert_string_equal(creds->principal, "wurst@brot.realm");
194 assert_int_equal(creds->principal_obtained, CRED_SPECIFIED);
196 assert_string_equal(creds->password, "BROT");
197 assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
200 int main(int argc, char *argv[])
203 const struct CMUnitTest tests[] = {
204 cmocka_unit_test(torture_creds_init),
205 cmocka_unit_test(torture_creds_init_anonymous),
206 cmocka_unit_test(torture_creds_guess),
207 cmocka_unit_test(torture_creds_anon_guess),
208 cmocka_unit_test(torture_creds_parse_string),
212 cmocka_set_test_filter(argv[1]);
214 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
216 rc = cmocka_run_group_tests(tests,
217 setup_talloc_context,
218 teardown_talloc_context);