r24872: Use torture API a bit more
[kai/samba.git] / source4 / torture / basic / disconnect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test server handling of unexpected client disconnects
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "system/filesys.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28
29 #define BASEDIR "\\test_disconnect"
30
31 #define CHECK_STATUS(status, correct) do { \
32         if (!NT_STATUS_EQUAL(status, correct)) { \
33                 printf("(%s) Incorrect status %s - should be %s\n", \
34                        __location__, nt_errstr(status), nt_errstr(correct)); \
35                 talloc_free(cli); \
36                 return False; \
37         }} while (0)
38
39 /*
40   test disconnect after async open
41 */
42 static BOOL test_disconnect_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
43 {
44         union smb_open io;
45         NTSTATUS status;
46         struct smbcli_request *req1, *req2;
47
48         printf("trying open/disconnect\n");
49
50         io.generic.level = RAW_OPEN_NTCREATEX;
51         io.ntcreatex.in.root_fid = 0;
52         io.ntcreatex.in.flags = 0;
53         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
54         io.ntcreatex.in.create_options = 0;
55         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
56         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
57         io.ntcreatex.in.alloc_size = 0;
58         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
59         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
60         io.ntcreatex.in.security_flags = 0;
61         io.ntcreatex.in.fname = BASEDIR "\\open.dat";
62         status = smb_raw_open(cli->tree, mem_ctx, &io);
63         CHECK_STATUS(status, NT_STATUS_OK);
64
65         io.ntcreatex.in.share_access = 0;
66         req1 = smb_raw_open_send(cli->tree, &io);
67         req2 = smb_raw_open_send(cli->tree, &io);
68
69         status = smbcli_chkpath(cli->tree, "\\");
70         CHECK_STATUS(status, NT_STATUS_OK);
71         
72         talloc_free(cli);
73
74         return True;
75 }
76
77
78 /*
79   test disconnect with timed lock
80 */
81 static BOOL test_disconnect_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
82 {
83         union smb_lock io;
84         NTSTATUS status;
85         int fnum;
86         struct smbcli_request *req;
87         struct smb_lock_entry lock[1];
88
89         printf("trying disconnect with async lock\n");
90
91         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", 
92                            O_RDWR | O_CREAT, DENY_NONE);
93         if (fnum == -1) {
94                 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
95                 return False;
96         }
97
98         io.lockx.level = RAW_LOCK_LOCKX;
99         io.lockx.in.file.fnum = fnum;
100         io.lockx.in.mode = 0;
101         io.lockx.in.timeout = 0;
102         io.lockx.in.lock_cnt = 1;
103         io.lockx.in.ulock_cnt = 0;
104         lock[0].pid = 1;
105         lock[0].offset = 0;
106         lock[0].count = 4;
107         io.lockx.in.locks = &lock[0];
108
109         status = smb_raw_lock(cli->tree, &io);
110         CHECK_STATUS(status, NT_STATUS_OK);
111
112         lock[0].pid = 2;
113         io.lockx.in.timeout = 3000;
114         req = smb_raw_lock_send(cli->tree, &io);
115
116         status = smbcli_chkpath(cli->tree, "\\");
117         CHECK_STATUS(status, NT_STATUS_OK);
118
119         talloc_free(cli);
120
121         return True;
122 }
123
124
125
126 /* 
127    basic testing of disconnects
128 */
129 BOOL torture_disconnect(struct torture_context *torture)
130 {
131         BOOL ret = True;
132         TALLOC_CTX *mem_ctx;
133         int i;
134         extern int torture_numops;
135         struct smbcli_state *cli;
136
137         mem_ctx = talloc_init("torture_raw_mux");
138
139         if (!torture_open_connection(&cli, 0)) {
140                 return False;
141         }
142
143         if (!torture_setup_dir(cli, BASEDIR)) {
144                 return False;
145         }
146
147         for (i=0;i<torture_numops;i++) {
148                 ret &= test_disconnect_lock(cli, mem_ctx);
149                 if (!torture_open_connection(&cli, 0)) {
150                         return False;
151                 }
152
153                 ret &= test_disconnect_open(cli, mem_ctx);
154                 if (!torture_open_connection(&cli, 0)) {
155                         return False;
156                 }
157
158                 if (torture_setting_bool(torture, "samba3", False)) {
159                         /*
160                          * In Samba3 it might happen that the old smbd from
161                          * test_disconnect_lock is not scheduled before the
162                          * new process comes in. Try to get rid of the random
163                          * failures in the build farm.
164                          */
165                         msleep(200);
166                 }
167         }
168
169         smb_raw_exit(cli->session);
170         smbcli_deltree(cli->tree, BASEDIR);
171         talloc_free(mem_ctx);
172         return ret;
173 }