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