r20595: W2k and XP don't always show the top directory as hidden, w2k3 does. I think
[kai/samba.git] / source4 / torture / raw / mux.c
1 /* 
2    Unix SMB/CIFS implementation.
3    basic raw test suite for multiplexing
4    Copyright (C) Andrew Tridgell 2003
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/libcli.h"
26 #include "torture/util.h"
27
28 #define BASEDIR "\\test_mux"
29
30 #define CHECK_STATUS(status, correct) do { \
31         if (!NT_STATUS_EQUAL(status, correct)) { \
32                 printf("(%s) Incorrect status %s - should be %s\n", \
33                        __location__, nt_errstr(status), nt_errstr(correct)); \
34                 ret = False; \
35                 goto done; \
36         }} while (0)
37
38
39 /*
40   test the delayed reply to a open that leads to a sharing violation
41 */
42 static BOOL test_mux_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
43 {
44         union smb_open io;
45         NTSTATUS status;
46         int fnum1, fnum2;
47         BOOL ret = True;
48         struct smbcli_request *req1, *req2;
49         struct timeval tv;
50         double d;
51
52         printf("testing multiplexed open/open/close\n");
53
54         printf("send first open\n");
55         io.generic.level = RAW_OPEN_NTCREATEX;
56         io.ntcreatex.in.root_fid = 0;
57         io.ntcreatex.in.flags = 0;
58         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
59         io.ntcreatex.in.create_options = 0;
60         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
61         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
62         io.ntcreatex.in.alloc_size = 0;
63         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
64         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
65         io.ntcreatex.in.security_flags = 0;
66         io.ntcreatex.in.fname = BASEDIR "\\open.dat";
67         status = smb_raw_open(cli->tree, mem_ctx, &io);
68         CHECK_STATUS(status, NT_STATUS_OK);
69         fnum1 = io.ntcreatex.out.file.fnum;
70
71         printf("send 2nd open, non-conflicting\n");
72         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
73         status = smb_raw_open(cli->tree, mem_ctx, &io);
74         CHECK_STATUS(status, NT_STATUS_OK);
75         fnum2 = io.ntcreatex.out.file.fnum;
76
77         tv = timeval_current();
78
79         printf("send 3rd open, conflicting\n");
80         io.ntcreatex.in.share_access = 0;
81         status = smb_raw_open(cli->tree, mem_ctx, &io);
82         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
83
84         d = timeval_elapsed(&tv);
85         if (d < 0.5 || d > 1.5) {
86                 printf("bad timeout for conflict - %.2f should be 1.0\n", d);
87         } else {
88                 printf("open delay %.2f\n", d);
89         }
90
91         printf("send async open, conflicting\n");
92         tv = timeval_current();
93         req1 = smb_raw_open_send(cli->tree, &io);
94
95         printf("send 2nd async open, conflicting\n");
96         tv = timeval_current();
97         req2 = smb_raw_open_send(cli->tree, &io);
98         
99         printf("close first sync open\n");
100         smbcli_close(cli->tree, fnum1);
101
102         printf("cancel 2nd async open (should be ignored)\n");
103         smb_raw_ntcancel(req2);
104
105         d = timeval_elapsed(&tv);
106         if (d > 0.25) {
107                 printf("bad timeout after cancel - %.2f should be <0.25\n", d);
108                 ret = False;
109         }
110
111         printf("close the 2nd sync open\n");
112         smbcli_close(cli->tree, fnum2);
113
114         printf("see if the 1st async open now succeeded\n");
115         status = smb_raw_open_recv(req1, mem_ctx, &io);
116         CHECK_STATUS(status, NT_STATUS_OK);
117
118         d = timeval_elapsed(&tv);
119         if (d > 0.25) {
120                 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
121                 ret = False;
122         } else {
123                 printf("async open delay %.2f\n", d);
124         }
125
126         printf("2nd async open should have timed out\n");
127         status = smb_raw_open_recv(req2, mem_ctx, &io);
128         CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
129         d = timeval_elapsed(&tv);
130         if (d < 0.8) {
131                 printf("bad timeout for async conflict - %.2f should be 1.0\n", d);
132         }
133
134         printf("close the 1st async open\n");
135         smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
136
137 done:
138         return ret;
139 }
140
141
142 /*
143   test a write that hits a byte range lock and send the close after the write
144 */
145 static BOOL test_mux_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
146 {
147         union smb_write io;
148         NTSTATUS status;
149         int fnum;
150         BOOL ret = True;
151         struct smbcli_request *req;
152
153         printf("testing multiplexed lock/write/close\n");
154
155         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
156         if (fnum == -1) {
157                 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
158                 ret = False;
159                 goto done;
160         }
161
162         cli->session->pid = 1;
163
164         /* lock a range */
165         if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK))) {
166                 printf("lock failed in mux_write - %s\n", smbcli_errstr(cli->tree));
167                 ret = False;
168                 goto done;
169         }
170
171         cli->session->pid = 2;
172
173         /* send an async write */
174         io.generic.level = RAW_WRITE_WRITEX;
175         io.writex.in.file.fnum = fnum;
176         io.writex.in.offset = 0;
177         io.writex.in.wmode = 0;
178         io.writex.in.remaining = 0;
179         io.writex.in.count = 4;
180         io.writex.in.data = (void *)&fnum;      
181         req = smb_raw_write_send(cli->tree, &io);
182
183         /* unlock the range */
184         cli->session->pid = 1;
185         smbcli_unlock(cli->tree, fnum, 0, 4);
186
187         /* and recv the async write reply */
188         status = smb_raw_write_recv(req, &io);
189         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
190
191         smbcli_close(cli->tree, fnum);
192
193 done:
194         return ret;
195 }
196
197
198 /*
199   test a lock that conflicts with an existing lock
200 */
201 static BOOL test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
202 {
203         union smb_lock io;
204         NTSTATUS status;
205         int fnum;
206         BOOL ret = True;
207         struct smbcli_request *req;
208         struct smb_lock_entry lock[1];
209
210         printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
211
212         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
213         if (fnum == -1) {
214                 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
215                 ret = False;
216                 goto done;
217         }
218
219         printf("establishing a lock\n");
220         io.lockx.level = RAW_LOCK_LOCKX;
221         io.lockx.in.file.fnum = fnum;
222         io.lockx.in.mode = 0;
223         io.lockx.in.timeout = 0;
224         io.lockx.in.lock_cnt = 1;
225         io.lockx.in.ulock_cnt = 0;
226         lock[0].pid = 1;
227         lock[0].offset = 0;
228         lock[0].count = 4;
229         io.lockx.in.locks = &lock[0];
230
231         status = smb_raw_lock(cli->tree, &io);
232         CHECK_STATUS(status, NT_STATUS_OK);
233
234         printf("the second lock will conflict with the first\n");
235         lock[0].pid = 2;
236         io.lockx.in.timeout = 1000;
237         status = smb_raw_lock(cli->tree, &io);
238         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
239
240         printf("this will too, but we'll unlock while waiting\n");
241         req = smb_raw_lock_send(cli->tree, &io);
242
243         printf("unlock the first range\n");
244         lock[0].pid = 1;
245         io.lockx.in.ulock_cnt = 1;
246         io.lockx.in.lock_cnt = 0;
247         io.lockx.in.timeout = 0;
248         status = smb_raw_lock(cli->tree, &io);
249         CHECK_STATUS(status, NT_STATUS_OK);
250
251         printf("recv the async reply\n");
252         status = smbcli_request_simple_recv(req);
253         CHECK_STATUS(status, NT_STATUS_OK);     
254
255         printf("reopening with an exit\n");
256         smb_raw_exit(cli->session);
257         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
258
259         printf("Now trying with a cancel\n");
260
261         io.lockx.level = RAW_LOCK_LOCKX;
262         io.lockx.in.file.fnum = fnum;
263         io.lockx.in.mode = 0;
264         io.lockx.in.timeout = 0;
265         io.lockx.in.lock_cnt = 1;
266         io.lockx.in.ulock_cnt = 0;
267         lock[0].pid = 1;
268         lock[0].offset = 0;
269         lock[0].count = 4;
270         io.lockx.in.locks = &lock[0];
271
272         status = smb_raw_lock(cli->tree, &io);
273         CHECK_STATUS(status, NT_STATUS_OK);
274
275         lock[0].pid = 2;
276         io.lockx.in.timeout = 1000;
277         status = smb_raw_lock(cli->tree, &io);
278         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
279
280         req = smb_raw_lock_send(cli->tree, &io);
281
282         /* cancel the blocking lock */
283         smb_raw_ntcancel(req);
284
285         /* the 2nd cancel is totally harmless, but tests the server trying to 
286            cancel an already cancelled request */
287         smb_raw_ntcancel(req);
288
289         lock[0].pid = 1;
290         io.lockx.in.ulock_cnt = 1;
291         io.lockx.in.lock_cnt = 0;
292         io.lockx.in.timeout = 0;
293         status = smb_raw_lock(cli->tree, &io);
294         CHECK_STATUS(status, NT_STATUS_OK);
295
296         status = smbcli_request_simple_recv(req);
297         CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);     
298
299         smbcli_close(cli->tree, fnum);
300
301 done:
302         return ret;
303 }
304
305
306
307 /* 
308    basic testing of multiplexing notify
309 */
310 BOOL torture_raw_mux(struct torture_context *torture)
311 {
312         struct smbcli_state *cli;
313         BOOL ret = True;
314         TALLOC_CTX *mem_ctx;
315                 
316         if (!torture_open_connection(&cli, 0)) {
317                 return False;
318         }
319
320         mem_ctx = talloc_init("torture_raw_mux");
321
322         if (!torture_setup_dir(cli, BASEDIR)) {
323                 return False;
324         }
325
326         if (!test_mux_open(cli, mem_ctx)) {
327                 ret = False;
328         }
329
330         if (!test_mux_write(cli, mem_ctx)) {
331                 ret = False;
332         }
333
334         if (!test_mux_lock(cli, mem_ctx)) {
335                 ret = False;
336         }
337
338         smb_raw_exit(cli->session);
339         smbcli_deltree(cli->tree, BASEDIR);
340         torture_close_connection(cli);
341         talloc_free(mem_ctx);
342         return ret;
343 }