tdb: introduce tdb->hdr_ofs
[Samba.git] / source3 / torture / test_cleanup.c
bloba831ed74a7f44c12332274657711bfaa2321a2be
1 /*
2 Unix SMB/CIFS implementation.
3 Test cleanup behaviour
4 Copyright (C) Volker Lendecke 2011
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 "locking/proto.h"
22 #include "torture/proto.h"
23 #include "system/filesys.h"
24 #include "system/select.h"
25 #include "libsmb/libsmb.h"
26 #include "libcli/smb/smbXcli_base.h"
27 #include "libcli/security/security.h"
28 #include "librpc/gen_ndr/open_files.h"
30 bool run_cleanup1(int dummy)
32 struct cli_state *cli;
33 const char *fname = "\\cleanup1";
34 uint16_t fnum;
35 NTSTATUS status;
37 printf("CLEANUP1: Checking that a conflicting share mode is cleaned "
38 "up\n");
40 if (!torture_open_connection(&cli, 0)) {
41 return false;
43 status = cli_openx(cli, fname, O_RDWR|O_CREAT, DENY_ALL, &fnum);
44 if (!NT_STATUS_IS_OK(status)) {
45 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
46 return false;
48 status = smbXcli_conn_samba_suicide(cli->conn, 1);
49 if (!NT_STATUS_IS_OK(status)) {
50 printf("smbXcli_conn_samba_suicide failed: %s\n",
51 nt_errstr(status));
52 return false;
55 if (!torture_open_connection(&cli, 1)) {
56 return false;
58 status = cli_ntcreate(
59 cli, fname, 0,
60 FILE_GENERIC_READ|FILE_GENERIC_WRITE|DELETE_ACCESS,
61 FILE_ATTRIBUTE_NORMAL,
62 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
63 FILE_OPEN, FILE_DELETE_ON_CLOSE, 0, &fnum, NULL);
64 if (!NT_STATUS_IS_OK(status)) {
65 printf("2nd open of %s failed (%s)\n", fname,
66 nt_errstr(status));
67 return false;
69 cli_close(cli, fnum);
71 torture_close_connection(cli);
72 return NT_STATUS_IS_OK(status);
75 bool run_cleanup2(int dummy)
77 struct cli_state *cli1, *cli2;
78 const char *fname = "\\cleanup2";
79 uint16_t fnum1, fnum2;
80 NTSTATUS status;
81 char buf;
83 printf("CLEANUP2: Checking that a conflicting brlock is cleaned up\n");
85 if (!torture_open_connection(&cli1, 0)) {
86 return false;
88 status = cli_ntcreate(
89 cli1, fname, 0, FILE_GENERIC_READ|FILE_GENERIC_WRITE,
90 FILE_ATTRIBUTE_NORMAL,
91 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
92 FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
93 if (!NT_STATUS_IS_OK(status)) {
94 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
95 return false;
97 status = cli_lock32(cli1, fnum1, 0, 1, 0, WRITE_LOCK);
98 if (!NT_STATUS_IS_OK(status)) {
99 printf("lock failed (%s)\n", nt_errstr(status));
100 return false;
104 * Check the file is indeed locked
106 if (!torture_open_connection(&cli2, 0)) {
107 return false;
109 status = cli_ntcreate(
110 cli2, fname, 0, FILE_GENERIC_READ|FILE_GENERIC_WRITE,
111 FILE_ATTRIBUTE_NORMAL,
112 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
113 FILE_OPEN, 0, 0, &fnum2, NULL);
114 if (!NT_STATUS_IS_OK(status)) {
115 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
116 return false;
118 buf = 'x';
119 status = cli_smbwrite(cli2, fnum2, &buf, 0, 1, NULL);
120 if (!NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
121 printf("write succeeded\n");
122 return false;
126 * Kill the lock holder
128 status = smbXcli_conn_samba_suicide(cli1->conn, 1);
129 if (!NT_STATUS_IS_OK(status)) {
130 printf("smbXcli_conn_samba_suicide failed: %s\n",
131 nt_errstr(status));
132 return false;
136 * Right now we don't clean up immediately. Re-open the 2nd connection.
138 #if 1
139 cli_shutdown(cli2);
140 if (!torture_open_connection(&cli2, 0)) {
141 return false;
143 status = cli_ntcreate(
144 cli2, fname, 0, FILE_GENERIC_READ|FILE_GENERIC_WRITE,
145 FILE_ATTRIBUTE_NORMAL,
146 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
147 FILE_OPEN, 0, 0, &fnum2, NULL);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
150 return false;
152 #endif
153 status = cli_smbwrite(cli2, fnum2, &buf, 0, 1, NULL);
154 if (!NT_STATUS_IS_OK(status)) {
155 printf("write failed: %s\n", nt_errstr(status));
156 return false;
158 return true;
161 static bool create_stale_share_mode_entry(const char *fname,
162 struct file_id *p_id)
164 struct cli_state *cli;
165 uint16_t fnum;
166 NTSTATUS status;
167 SMB_STRUCT_STAT sbuf;
168 struct file_id id;
170 if (!torture_open_connection(&cli, 0)) {
171 return false;
174 status = torture_setup_unix_extensions(cli);
175 if (!NT_STATUS_IS_OK(status)) {
176 printf("torture_setup_unix_extensions failed: %s\n",
177 nt_errstr(status));
178 return false;
180 status = cli_openx(cli, fname, O_RDWR|O_CREAT, DENY_ALL, &fnum);
181 if (!NT_STATUS_IS_OK(status)) {
182 printf("open of %s failed (%s)\n", fname, nt_errstr(status));
183 return false;
185 status = cli_posix_stat(cli, fname, &sbuf);
186 if (!NT_STATUS_IS_OK(status)) {
187 printf("cli_posix_stat failed: %s\n", nt_errstr(status));
188 return false;
190 status = smbXcli_conn_samba_suicide(cli->conn, 1);
191 if (!NT_STATUS_IS_OK(status)) {
192 printf("smbXcli_conn_samba_suicide failed: %s\n",
193 nt_errstr(status));
194 return false;
197 id.devid = sbuf.st_ex_rdev;
198 id.inode = sbuf.st_ex_ino;
199 id.extid = 0;
201 poll(NULL, 0, 1000);
203 *p_id = id;
204 return true;
207 static bool corrupt_dummy(struct share_mode_data *d)
209 return true;
212 static bool invalidate_sharemode(struct share_mode_data *d)
214 d->share_modes[0].op_type =
215 OPLOCK_EXCLUSIVE|OPLOCK_BATCH|OPLOCK_LEVEL_II;
216 d->modified = true;
217 return true;
220 static bool duplicate_entry(struct share_mode_data *d, int i)
222 struct share_mode_entry *tmp;
224 if (i >= d->num_share_modes) {
225 return false;
228 tmp = talloc_realloc(d, d->share_modes, struct share_mode_entry,
229 d->num_share_modes + 1);
230 if (tmp == NULL) {
231 return false;
233 d->share_modes = tmp;
234 d->num_share_modes += 1;
235 d->share_modes[d->num_share_modes-1] = d->share_modes[i];
236 d->modified = true;
237 return true;
240 static bool create_duplicate_batch(struct share_mode_data *d)
242 if (d->num_share_modes != 1) {
243 return false;
245 d->share_modes[0].op_type = OPLOCK_BATCH;
246 if (!duplicate_entry(d, 0)) {
247 return false;
249 return true;
252 struct corruption_fns {
253 bool (*fn)(struct share_mode_data *d);
254 const char *descr;
257 bool run_cleanup3(int dummy)
259 struct cli_state *cli;
260 const char *fname = "cleanup3";
261 uint16_t fnum;
262 NTSTATUS status;
263 struct share_mode_lock *lck;
264 struct file_id id;
265 size_t i;
267 struct corruption_fns fns[] = {
268 { corrupt_dummy, "no corruption" },
269 { invalidate_sharemode, "invalidate_sharemode" },
270 { create_duplicate_batch, "create_duplicate_batch" },
273 printf("CLEANUP3: Checking that a share mode is cleaned up on "
274 "conflict\n");
276 for (i=0; i<ARRAY_SIZE(fns); i++) {
278 printf("testing %s\n", fns[i].descr);
280 if (!create_stale_share_mode_entry(fname, &id)) {
281 printf("create_stale_entry failed\n");
282 return false;
285 printf("%d %d %d\n", (int)id.devid, (int)id.inode,
286 (int)id.extid);
288 if (!locking_init()) {
289 printf("locking_init failed\n");
290 return false;
292 lck = get_existing_share_mode_lock(talloc_tos(), id);
293 if (lck == NULL) {
294 printf("get_existing_share_mode_lock failed\n");
295 return false;
297 if (lck->data->num_share_modes != 1) {
298 printf("get_existing_share_mode_lock did clean up\n");
299 return false;
302 fns[i].fn(lck->data);
304 TALLOC_FREE(lck);
306 if (!torture_open_connection(&cli, 0)) {
307 return false;
309 status = cli_openx(cli, fname, O_RDWR|O_CREAT, DENY_ALL,
310 &fnum);
311 if (!NT_STATUS_IS_OK(status)) {
312 printf("open of %s failed (%s)\n", fname,
313 nt_errstr(status));
314 return false;
316 lck = get_existing_share_mode_lock(talloc_tos(), id);
317 if (lck == NULL) {
318 printf("get_existing_share_mode_lock failed\n");
319 return false;
321 if (lck->data->num_share_modes != 1) {
322 printf("conflicting open did not clean up\n");
323 return false;
325 TALLOC_FREE(lck);
327 torture_close_connection(cli);
330 return true;
333 bool run_cleanup4(int dummy)
335 struct cli_state *cli1, *cli2;
336 const char *fname = "\\cleanup4";
337 uint16_t fnum1, fnum2;
338 NTSTATUS status;
340 printf("CLEANUP4: Checking that a conflicting share mode is cleaned "
341 "up\n");
343 if (!torture_open_connection(&cli1, 0)) {
344 return false;
346 if (!torture_open_connection(&cli2, 0)) {
347 return false;
350 status = cli_ntcreate(
351 cli1, fname, 0,
352 FILE_GENERIC_READ|DELETE_ACCESS,
353 FILE_ATTRIBUTE_NORMAL,
354 FILE_SHARE_READ|FILE_SHARE_DELETE,
355 FILE_OVERWRITE_IF, 0, 0, &fnum1, NULL);
356 if (!NT_STATUS_IS_OK(status)) {
357 printf("creating file failed: %s\n",
358 nt_errstr(status));
359 return false;
362 status = cli_ntcreate(
363 cli2, fname, 0,
364 FILE_GENERIC_READ|DELETE_ACCESS,
365 FILE_ATTRIBUTE_NORMAL,
366 FILE_SHARE_READ|FILE_SHARE_DELETE,
367 FILE_OPEN, 0, 0, &fnum2, NULL);
368 if (!NT_STATUS_IS_OK(status)) {
369 printf("opening file 1st time failed: %s\n",
370 nt_errstr(status));
371 return false;
374 status = smbXcli_conn_samba_suicide(cli1->conn, 1);
375 if (!NT_STATUS_IS_OK(status)) {
376 printf("smbXcli_conn_samba_suicide failed: %s\n",
377 nt_errstr(status));
378 return false;
382 * The next open will conflict with both opens above. The first open
383 * above will be correctly cleaned up. A bug in smbd iterating over
384 * the share mode array made it skip the share conflict check for the
385 * second open. Trigger this bug.
388 status = cli_ntcreate(
389 cli2, fname, 0,
390 FILE_GENERIC_WRITE|DELETE_ACCESS,
391 FILE_ATTRIBUTE_NORMAL,
392 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
393 FILE_OPEN, 0, 0, &fnum2, NULL);
394 if (!NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
395 printf("opening file 2nd time returned: %s\n",
396 nt_errstr(status));
397 return false;
400 return true;