Revert "cli_credentials: add a helper to parse user or group names"
[samba.git] / auth / credentials / tests / test_creds.c
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 static int setup_talloc_context(void **state)
30 {
31         TALLOC_CTX *frame = talloc_stackframe();
32
33         *state = frame;
34         return 0;
35 }
36
37 static int teardown_talloc_context(void **state)
38 {
39         TALLOC_CTX *frame = *state;
40         TALLOC_FREE(frame);
41         return 0;
42 }
43
44 static void torture_creds_init(void **state)
45 {
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;
51         bool ok;
52
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);
57
58         domain = cli_credentials_get_domain(creds);
59         assert_null(domain);
60         ok = cli_credentials_set_domain(creds, "WURST", CRED_SPECIFIED);
61         assert_true(ok);
62         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
63         domain = cli_credentials_get_domain(creds);
64         assert_string_equal(domain, "WURST");
65
66         username = cli_credentials_get_username(creds);
67         assert_null(username);
68         ok = cli_credentials_set_username(creds, "brot", CRED_SPECIFIED);
69         assert_true(ok);
70         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
71         username = cli_credentials_get_username(creds);
72         assert_string_equal(username, "brot");
73
74         password = cli_credentials_get_password(creds);
75         assert_null(password);
76         ok = cli_credentials_set_password(creds, "SECRET", CRED_SPECIFIED);
77         assert_true(ok);
78         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
79         password = cli_credentials_get_password(creds);
80         assert_string_equal(password, "SECRET");
81 }
82
83 static void torture_creds_init_anonymous(void **state)
84 {
85         TALLOC_CTX *mem_ctx = *state;
86         struct cli_credentials *creds = NULL;
87
88         creds = cli_credentials_init_anon(mem_ctx);
89         assert_non_null(creds);
90
91         assert_string_equal(creds->domain, "");
92         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
93
94         assert_string_equal(creds->username, "");
95         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
96
97         assert_null(creds->password);
98         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
99 }
100
101 static void torture_creds_guess(void **state)
102 {
103         TALLOC_CTX *mem_ctx = *state;
104         struct cli_credentials *creds = NULL;
105         const char *env_user = getenv("USER");
106
107         creds = cli_credentials_init(mem_ctx);
108         assert_non_null(creds);
109
110         setenv("PASSWD", "SECRET", 1);
111         cli_credentials_guess(creds, NULL);
112
113         assert_string_equal(creds->username, env_user);
114         assert_int_equal(creds->username_obtained, CRED_GUESS_ENV);
115
116         assert_string_equal(creds->password, "SECRET");
117         assert_int_equal(creds->password_obtained, CRED_GUESS_ENV);
118         unsetenv("PASSWD");
119 }
120
121 static void torture_creds_anon_guess(void **state)
122 {
123         TALLOC_CTX *mem_ctx = *state;
124         struct cli_credentials *creds = NULL;
125
126         creds = cli_credentials_init_anon(mem_ctx);
127         assert_non_null(creds);
128
129         setenv("PASSWD", "SECRET", 1);
130         cli_credentials_guess(creds, NULL);
131
132         assert_string_equal(creds->username, "");
133         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
134
135         assert_null(creds->password);
136         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
137         unsetenv("PASSWD");
138 }
139
140 static void torture_creds_parse_string(void **state)
141 {
142         TALLOC_CTX *mem_ctx = *state;
143         struct cli_credentials *creds = NULL;
144
145         creds = cli_credentials_init(mem_ctx);
146         assert_non_null(creds);
147
148         /* Anonymous */
149         cli_credentials_parse_string(creds, "%", CRED_SPECIFIED);
150
151         assert_string_equal(creds->domain, "");
152         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
153
154         assert_string_equal(creds->username, "");
155         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
156
157         assert_null(creds->password);
158         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
159
160         /* Username + password */
161         cli_credentials_parse_string(creds, "wurst%BROT", CRED_SPECIFIED);
162
163         assert_string_equal(creds->domain, "");
164         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
165
166         assert_string_equal(creds->username, "wurst");
167         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
168
169         assert_string_equal(creds->password, "BROT");
170         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
171
172         /* Domain + username + password */
173         cli_credentials_parse_string(creds, "XXL\\wurst%BROT", CRED_SPECIFIED);
174
175         assert_string_equal(creds->domain, "XXL");
176         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
177
178         assert_string_equal(creds->username, "wurst");
179         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
180
181         assert_string_equal(creds->password, "BROT");
182         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
183
184         /* Principal */
185         cli_credentials_parse_string(creds, "wurst@brot.realm", CRED_SPECIFIED);
186
187         assert_string_equal(creds->domain, "");
188         assert_int_equal(creds->domain_obtained, CRED_SPECIFIED);
189
190         assert_string_equal(creds->username, "wurst@brot.realm");
191         assert_int_equal(creds->username_obtained, CRED_SPECIFIED);
192
193         assert_string_equal(creds->principal, "wurst@brot.realm");
194         assert_int_equal(creds->principal_obtained, CRED_SPECIFIED);
195
196         assert_string_equal(creds->password, "BROT");
197         assert_int_equal(creds->password_obtained, CRED_SPECIFIED);
198 }
199
200 int main(int argc, char *argv[])
201 {
202         int rc;
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),
209         };
210
211         if (argc == 2) {
212                 cmocka_set_test_filter(argv[1]);
213         }
214         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
215
216         rc = cmocka_run_group_tests(tests,
217                                     setup_talloc_context,
218                                     teardown_talloc_context);
219
220         return rc;
221 }