2 Unix SMB/CIFS implementation.
3 basic raw test suite for multiplexing
4 Copyright (C) Andrew Tridgell 2003
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.
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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/libcli.h"
27 #define BASEDIR "\\test_mux"
29 #define CHECK_STATUS(status, correct) do { \
30 if (!NT_STATUS_EQUAL(status, correct)) { \
31 printf("(%s) Incorrect status %s - should be %s\n", \
32 __location__, nt_errstr(status), nt_errstr(correct)); \
39 test the delayed reply to a open that leads to a sharing violation
41 static BOOL test_mux_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
47 struct smbcli_request *req1, *req2;
51 printf("testing multiplexed open/open/close\n");
53 printf("send first open\n");
54 io.generic.level = RAW_OPEN_NTCREATEX;
55 io.ntcreatex.in.root_fid = 0;
56 io.ntcreatex.in.flags = 0;
57 io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
58 io.ntcreatex.in.create_options = 0;
59 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
60 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
61 io.ntcreatex.in.alloc_size = 0;
62 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
63 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
64 io.ntcreatex.in.security_flags = 0;
65 io.ntcreatex.in.fname = BASEDIR "\\open.dat";
66 status = smb_raw_open(cli->tree, mem_ctx, &io);
67 CHECK_STATUS(status, NT_STATUS_OK);
68 fnum1 = io.ntcreatex.file.fnum;
70 printf("send 2nd open, non-conflicting\n");
71 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
72 status = smb_raw_open(cli->tree, mem_ctx, &io);
73 CHECK_STATUS(status, NT_STATUS_OK);
74 fnum2 = io.ntcreatex.file.fnum;
76 tv = timeval_current();
78 printf("send 3rd open, conflicting\n");
79 io.ntcreatex.in.share_access = 0;
80 status = smb_raw_open(cli->tree, mem_ctx, &io);
81 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
83 d = timeval_elapsed(&tv);
84 if (d < 0.5 || d > 1.5) {
85 printf("bad timeout for conflict - %.2f should be 1.0\n", d);
87 printf("open delay %.2f\n", d);
90 printf("send async open, conflicting\n");
91 tv = timeval_current();
92 req1 = smb_raw_open_send(cli->tree, &io);
94 printf("send 2nd async open, conflicting\n");
95 tv = timeval_current();
96 req2 = smb_raw_open_send(cli->tree, &io);
98 printf("close first sync open\n");
99 smbcli_close(cli->tree, fnum1);
101 printf("cancel 2nd async open (should be ignored)\n");
102 smb_raw_ntcancel(req2);
104 d = timeval_elapsed(&tv);
106 printf("bad timeout after cancel - %.2f should be <0.25\n", d);
110 printf("close the 2nd sync open\n");
111 smbcli_close(cli->tree, fnum2);
113 printf("see if the 1st async open now succeeded\n");
114 status = smb_raw_open_recv(req1, mem_ctx, &io);
115 CHECK_STATUS(status, NT_STATUS_OK);
117 d = timeval_elapsed(&tv);
119 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
122 printf("async open delay %.2f\n", d);
125 printf("2nd async open should have timed out\n");
126 status = smb_raw_open_recv(req2, mem_ctx, &io);
127 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
128 d = timeval_elapsed(&tv);
130 printf("bad timeout for async conflict - %.2f should be 1.0\n", d);
133 printf("close the 1st async open\n");
134 smbcli_close(cli->tree, io.ntcreatex.file.fnum);
142 test a write that hits a byte range lock and send the close after the write
144 static BOOL test_mux_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
150 struct smbcli_request *req;
152 printf("testing multiplexed lock/write/close\n");
154 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
156 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
161 cli->session->pid = 1;
164 if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK))) {
165 printf("lock failed in mux_write - %s\n", smbcli_errstr(cli->tree));
170 cli->session->pid = 2;
172 /* send an async write */
173 io.generic.level = RAW_WRITE_WRITEX;
174 io.writex.file.fnum = fnum;
175 io.writex.in.offset = 0;
176 io.writex.in.wmode = 0;
177 io.writex.in.remaining = 0;
178 io.writex.in.count = 4;
179 io.writex.in.data = (void *)&fnum;
180 req = smb_raw_write_send(cli->tree, &io);
182 /* unlock the range */
183 cli->session->pid = 1;
184 smbcli_unlock(cli->tree, fnum, 0, 4);
186 /* and recv the async write reply */
187 status = smb_raw_write_recv(req, &io);
188 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
190 smbcli_close(cli->tree, fnum);
198 test a lock that conflicts with an existing lock
200 static BOOL test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
206 struct smbcli_request *req;
207 struct smb_lock_entry lock[1];
209 printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
211 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
213 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
218 printf("establishing a lock\n");
219 io.lockx.level = RAW_LOCK_LOCKX;
220 io.lockx.file.fnum = fnum;
221 io.lockx.in.mode = 0;
222 io.lockx.in.timeout = 0;
223 io.lockx.in.lock_cnt = 1;
224 io.lockx.in.ulock_cnt = 0;
228 io.lockx.in.locks = &lock[0];
230 status = smb_raw_lock(cli->tree, &io);
231 CHECK_STATUS(status, NT_STATUS_OK);
233 printf("the second lock will conflict with the first\n");
235 io.lockx.in.timeout = 1000;
236 status = smb_raw_lock(cli->tree, &io);
237 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
239 printf("this will too, but we'll unlock while waiting\n");
240 req = smb_raw_lock_send(cli->tree, &io);
242 printf("unlock the first range\n");
244 io.lockx.in.ulock_cnt = 1;
245 io.lockx.in.lock_cnt = 0;
246 io.lockx.in.timeout = 0;
247 status = smb_raw_lock(cli->tree, &io);
248 CHECK_STATUS(status, NT_STATUS_OK);
250 printf("recv the async reply\n");
251 status = smbcli_request_simple_recv(req);
252 CHECK_STATUS(status, NT_STATUS_OK);
254 printf("reopening with an exit\n");
255 smb_raw_exit(cli->session);
256 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
258 printf("Now trying with a cancel\n");
260 io.lockx.level = RAW_LOCK_LOCKX;
261 io.lockx.file.fnum = fnum;
262 io.lockx.in.mode = 0;
263 io.lockx.in.timeout = 0;
264 io.lockx.in.lock_cnt = 1;
265 io.lockx.in.ulock_cnt = 0;
269 io.lockx.in.locks = &lock[0];
271 status = smb_raw_lock(cli->tree, &io);
272 CHECK_STATUS(status, NT_STATUS_OK);
275 io.lockx.in.timeout = 1000;
276 status = smb_raw_lock(cli->tree, &io);
277 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
279 req = smb_raw_lock_send(cli->tree, &io);
281 /* cancel the blocking lock */
282 smb_raw_ntcancel(req);
284 /* the 2nd cancel is totally harmless, but tests the server trying to
285 cancel an already cancelled request */
286 smb_raw_ntcancel(req);
289 io.lockx.in.ulock_cnt = 1;
290 io.lockx.in.lock_cnt = 0;
291 io.lockx.in.timeout = 0;
292 status = smb_raw_lock(cli->tree, &io);
293 CHECK_STATUS(status, NT_STATUS_OK);
295 status = smbcli_request_simple_recv(req);
296 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
298 smbcli_close(cli->tree, fnum);
307 basic testing of multiplexing notify
309 BOOL torture_raw_mux(void)
311 struct smbcli_state *cli;
315 if (!torture_open_connection(&cli)) {
319 mem_ctx = talloc_init("torture_raw_mux");
321 if (!torture_setup_dir(cli, BASEDIR)) {
325 if (!test_mux_open(cli, mem_ctx)) {
329 if (!test_mux_write(cli, mem_ctx)) {
333 if (!test_mux_lock(cli, mem_ctx)) {
337 smb_raw_exit(cli->session);
338 smbcli_deltree(cli->tree, BASEDIR);
339 torture_close_connection(cli);
340 talloc_free(mem_ctx);