auth/creds/torture: add a test showing segfault
[Samba.git] / auth / credentials / tests / simple.c
blob7f122bed3bccdf9322b318712cf92e41b7432e35
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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/>.
20 #include "includes.h"
21 #include "auth/credentials/credentials.h"
22 #include "torture/torture.h"
23 #include "torture/local/proto.h"
25 static bool test_init(struct torture_context *tctx)
27 struct cli_credentials *creds = cli_credentials_init(tctx);
29 cli_credentials_set_domain(creds, "bla", CRED_SPECIFIED);
31 torture_assert_str_equal(tctx, "BLA", cli_credentials_get_domain(creds),
32 "domain");
34 cli_credentials_set_username(creds, "someuser", CRED_SPECIFIED);
36 torture_assert_str_equal(tctx, "someuser",
37 cli_credentials_get_username(creds),
38 "username");
40 cli_credentials_set_password(creds, "p4ssw0rd", CRED_SPECIFIED);
42 torture_assert_str_equal(tctx, "p4ssw0rd",
43 cli_credentials_get_password(creds),
44 "password");
46 return true;
49 static bool test_init_anonymous(struct torture_context *tctx)
51 struct cli_credentials *creds = cli_credentials_init_anon(tctx);
53 torture_assert_str_equal(tctx, cli_credentials_get_domain(creds),
54 "", "domain");
56 torture_assert_str_equal(tctx, cli_credentials_get_username(creds),
57 "", "username");
59 torture_assert(tctx, cli_credentials_get_password(creds) == NULL,
60 "password");
62 return true;
65 static bool test_guess(struct torture_context *tctx)
67 struct cli_credentials *creds = cli_credentials_init_anon(tctx);
68 enum credentials_use_kerberos old_kerb_state = \
69 cli_credentials_get_kerberos_state(creds);
70 const char *logname = getenv("LOGNAME");
71 const char *user = getenv("USER");
72 const char *passwd = getenv("PASSWD");
73 const char *passwd_fd = getenv("PASSWD_FD");
74 const char *passwd_file = getenv("PASSWD_FILE");
76 cli_credentials_set_kerberos_state(creds, CRED_MUST_USE_KERBEROS);
78 unsetenv("USER");
79 unsetenv("PASSWD_FD");
80 unsetenv("PASSWD_FILE");
82 setenv("LOGNAME", "xx", 1);
83 setenv("PASSWD", "xx", 1);
85 cli_credentials_guess(creds, NULL);
86 if (logname != NULL) {
87 setenv("LOGNAME", logname, 1);
89 if (user != NULL) {
90 setenv("USER", user, 1);
92 if (passwd != NULL) {
93 setenv("PASSWD", passwd, 1);
95 if (passwd_fd != NULL) {
96 setenv("PASSWD_FD", passwd_fd, 1);
98 if (passwd_file != NULL) {
99 setenv("PASSWD_FILE", passwd_file, 1);
101 cli_credentials_set_kerberos_state(creds, old_kerb_state);
103 return true;
107 static bool test_parse_string(struct torture_context *tctx)
109 struct cli_credentials *creds = cli_credentials_init_anon(tctx);
111 /* anonymous */
112 cli_credentials_parse_string(creds, "%", CRED_SPECIFIED);
114 torture_assert_str_equal(tctx, cli_credentials_get_domain(creds),
115 "", "domain");
117 torture_assert_str_equal(tctx, cli_credentials_get_username(creds),
118 "", "username");
120 torture_assert(tctx, cli_credentials_get_password(creds) == NULL,
121 "password");
123 /* username + password */
124 cli_credentials_parse_string(creds, "somebody%secret",
125 CRED_SPECIFIED);
127 torture_assert_str_equal(tctx, cli_credentials_get_domain(creds),
128 "", "domain");
130 torture_assert_str_equal(tctx, cli_credentials_get_username(creds),
131 "somebody", "username");
133 torture_assert_str_equal(tctx, cli_credentials_get_password(creds),
134 "secret", "password");
136 /* principal */
137 cli_credentials_parse_string(creds, "prin@styx",
138 CRED_SPECIFIED);
140 torture_assert_str_equal(tctx, cli_credentials_get_realm(creds),
141 "STYX", "realm");
143 torture_assert_str_equal(tctx,
144 cli_credentials_get_principal(creds, tctx),
145 "prin@styx", "principal");
147 return true;
150 struct torture_suite *torture_local_credentials(TALLOC_CTX *mem_ctx)
152 struct torture_suite *suite = torture_suite_create(mem_ctx, "credentials");
154 torture_suite_add_simple_test(suite, "init", test_init);
155 torture_suite_add_simple_test(suite, "init anonymous",
156 test_init_anonymous);
157 torture_suite_add_simple_test(suite, "guess",
158 test_guess);
159 torture_suite_add_simple_test(suite, "parse_string",
160 test_parse_string);
162 return suite;