s4-smbtorture: Make test names lowercase and dot-separated.
[Samba.git] / source4 / torture / rap / sam.c
blobd452a53ee8e618999b94859f39f4afd35bba16ab
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for RAP sam operations
5 Copyright (C) Guenther Deschner 2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "libcli/libcli.h"
23 #include "torture/torture.h"
24 #include "torture/util.h"
25 #include "torture/smbtorture.h"
26 #include "torture/util.h"
27 #include "../librpc/gen_ndr/rap.h"
28 #include "torture/rap/proto.h"
29 #include "param/param.h"
30 #include "../lib/crypto/crypto.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "torture/rpc/torture_rpc.h"
34 #define TEST_RAP_USER "torture_rap_user"
36 static char *samr_rand_pass(TALLOC_CTX *mem_ctx, int min_len)
38 size_t len = MAX(8, min_len);
39 char *s = generate_random_password(mem_ctx, len, len+6);
40 printf("Generated password '%s'\n", s);
41 return s;
44 static bool test_userpasswordset2_args(struct torture_context *tctx,
45 struct smbcli_state *cli,
46 const char *username,
47 const char **password)
49 struct rap_NetUserPasswordSet2 r;
50 char *newpass = samr_rand_pass(tctx, 8);
52 ZERO_STRUCT(r);
54 r.in.UserName = username;
56 memcpy(r.in.OldPassword, *password, MIN(strlen(*password), 16));
57 memcpy(r.in.NewPassword, newpass, MIN(strlen(newpass), 16));
58 r.in.EncryptedPassword = 0;
59 r.in.RealPasswordLength = strlen(newpass);
61 torture_comment(tctx, "Testing rap_NetUserPasswordSet2(%s)\n", r.in.UserName);
63 torture_assert_ntstatus_ok(tctx,
64 smbcli_rap_netuserpasswordset2(cli->tree, lpcfg_iconv_convenience(tctx->lp_ctx), tctx, &r),
65 "smbcli_rap_netuserpasswordset2 failed");
66 if (!W_ERROR_IS_OK(W_ERROR(r.out.status))) {
67 torture_warning(tctx, "RAP NetUserPasswordSet2 gave: %s\n",
68 win_errstr(W_ERROR(r.out.status)));
69 } else {
70 *password = newpass;
73 return true;
76 static bool test_userpasswordset2_crypt_args(struct torture_context *tctx,
77 struct smbcli_state *cli,
78 const char *username,
79 const char **password)
81 struct rap_NetUserPasswordSet2 r;
82 char *newpass = samr_rand_pass(tctx, 8);
84 r.in.UserName = username;
86 E_deshash(*password, r.in.OldPassword);
87 E_deshash(newpass, r.in.NewPassword);
89 r.in.RealPasswordLength = strlen(newpass);
90 r.in.EncryptedPassword = 1;
92 torture_comment(tctx, "Testing rap_NetUserPasswordSet2(%s)\n", r.in.UserName);
94 torture_assert_ntstatus_ok(tctx,
95 smbcli_rap_netuserpasswordset2(cli->tree, lpcfg_iconv_convenience(tctx->lp_ctx), tctx, &r),
96 "smbcli_rap_netuserpasswordset2 failed");
97 if (!W_ERROR_IS_OK(W_ERROR(r.out.status))) {
98 torture_warning(tctx, "RAP NetUserPasswordSet2 gave: %s\n",
99 win_errstr(W_ERROR(r.out.status)));
100 } else {
101 *password = newpass;
104 return true;
107 static bool test_userpasswordset2(struct torture_context *tctx,
108 struct smbcli_state *cli)
110 struct test_join *join_ctx;
111 const char *password;
112 bool ret = true;
114 join_ctx = torture_create_testuser_max_pwlen(tctx, TEST_RAP_USER,
115 torture_setting_string(tctx, "workgroup", NULL),
116 ACB_NORMAL,
117 &password, 14);
118 if (join_ctx == NULL) {
119 torture_fail(tctx, "failed to create user\n");
122 ret &= test_userpasswordset2_args(tctx, cli, TEST_RAP_USER, &password);
123 ret &= test_userpasswordset2_crypt_args(tctx, cli, TEST_RAP_USER, &password);
125 torture_leave_domain(tctx, join_ctx);
127 return ret;
130 static bool test_oemchangepassword_args(struct torture_context *tctx,
131 struct smbcli_state *cli,
132 const char *username,
133 const char **password)
135 struct rap_NetOEMChangePassword r;
137 const char *oldpass = *password;
138 char *newpass = samr_rand_pass(tctx, 9);
139 uint8_t old_pw_hash[16];
140 uint8_t new_pw_hash[16];
142 r.in.UserName = username;
144 E_deshash(oldpass, old_pw_hash);
145 E_deshash(newpass, new_pw_hash);
147 encode_pw_buffer(r.in.crypt_password, newpass, STR_ASCII);
148 arcfour_crypt(r.in.crypt_password, old_pw_hash, 516);
149 E_old_pw_hash(new_pw_hash, old_pw_hash, r.in.password_hash);
151 torture_comment(tctx, "Testing rap_NetOEMChangePassword(%s)\n", r.in.UserName);
153 torture_assert_ntstatus_ok(tctx,
154 smbcli_rap_netoemchangepassword(cli->tree, lpcfg_iconv_convenience(tctx->lp_ctx), tctx, &r),
155 "smbcli_rap_netoemchangepassword failed");
156 if (!W_ERROR_IS_OK(W_ERROR(r.out.status))) {
157 torture_warning(tctx, "RAP NetOEMChangePassword gave: %s\n",
158 win_errstr(W_ERROR(r.out.status)));
159 } else {
160 *password = newpass;
163 return true;
166 static bool test_oemchangepassword(struct torture_context *tctx,
167 struct smbcli_state *cli)
170 struct test_join *join_ctx;
171 const char *password;
172 bool ret;
174 join_ctx = torture_create_testuser_max_pwlen(tctx, TEST_RAP_USER,
175 torture_setting_string(tctx, "workgroup", NULL),
176 ACB_NORMAL,
177 &password, 14);
178 if (join_ctx == NULL) {
179 torture_fail(tctx, "failed to create user\n");
182 ret = test_oemchangepassword_args(tctx, cli, TEST_RAP_USER, &password);
184 torture_leave_domain(tctx, join_ctx);
186 return ret;
189 static bool test_usergetinfo_byname(struct torture_context *tctx,
190 struct smbcli_state *cli,
191 const char *UserName)
193 struct rap_NetUserGetInfo r;
194 int i;
195 uint16_t levels[] = { 0, 1, 2, 10, 11 };
197 for (i=0; i < ARRAY_SIZE(levels); i++) {
199 r.in.UserName = UserName;
200 r.in.level = levels[i];
201 r.in.bufsize = 8192;
203 torture_comment(tctx,
204 "Testing rap_NetUserGetInfo(%s) level %d\n", r.in.UserName, r.in.level);
206 torture_assert_ntstatus_ok(tctx,
207 smbcli_rap_netusergetinfo(cli->tree, tctx, &r),
208 "smbcli_rap_netusergetinfo failed");
209 torture_assert_werr_ok(tctx, W_ERROR(r.out.status),
210 "smbcli_rap_netusergetinfo failed");
213 return true;
216 static bool test_usergetinfo(struct torture_context *tctx,
217 struct smbcli_state *cli)
220 struct test_join *join_ctx;
221 const char *password;
222 bool ret;
224 join_ctx = torture_create_testuser_max_pwlen(tctx, TEST_RAP_USER,
225 torture_setting_string(tctx, "workgroup", NULL),
226 ACB_NORMAL,
227 &password, 14);
228 if (join_ctx == NULL) {
229 torture_fail(tctx, "failed to create user\n");
232 ret = test_usergetinfo_byname(tctx, cli, TEST_RAP_USER);
234 torture_leave_domain(tctx, join_ctx);
236 return ret;
239 struct torture_suite *torture_rap_sam(TALLOC_CTX *mem_ctx)
241 struct torture_suite *suite = torture_suite_create(mem_ctx, "sam");
243 torture_suite_add_1smb_test(suite, "userpasswordset2", test_userpasswordset2);
244 torture_suite_add_1smb_test(suite, "oemchangepassword", test_oemchangepassword);
245 torture_suite_add_1smb_test(suite, "usergetinfo", test_usergetinfo);
247 return suite;