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