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 "libcli/raw/libcliraw.h"
24 #define BASEDIR "\\test_mux"
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)); \
36 test the delayed reply to a open that leads to a sharing violation
38 static BOOL test_mux_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
44 struct smbcli_request *req;
48 printf("testing multiplexed open/open/close\n");
51 file open with no share access
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 = SA_RIGHT_FILE_READ_DATA;
57 io.ntcreatex.in.create_options = 0;
58 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
59 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
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 fnum1 = io.ntcreatex.out.fnum;
69 /* and a 2nd open, this will not conflict */
70 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
71 status = smb_raw_open(cli->tree, mem_ctx, &io);
72 CHECK_STATUS(status, NT_STATUS_OK);
73 fnum2 = io.ntcreatex.out.fnum;
75 tv = timeval_current();
77 /* send an open that will conflict */
78 io.ntcreatex.in.share_access = 0;
79 status = smb_raw_open(cli->tree, mem_ctx, &io);
80 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
82 d = timeval_elapsed(&tv);
83 if (d < 0.5 || d > 1.5) {
84 printf("bad timeout for conflict - %.2f should be 1.0\n", d);
87 printf("open delay %.2f\n", d);
91 same request, but async
93 tv = timeval_current();
94 req = smb_raw_open_send(cli->tree, &io);
96 /* and close the first file */
97 smbcli_close(cli->tree, fnum1);
99 /* then the 2nd file */
100 smbcli_close(cli->tree, fnum2);
102 /* see if the async open succeeded */
103 status = smb_raw_open_recv(req, mem_ctx, &io);
104 CHECK_STATUS(status, NT_STATUS_OK);
106 d = timeval_elapsed(&tv);
108 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
111 printf("async open delay %.2f\n", d);
114 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
122 test a write that hits a byte range lock and send the close after the write
124 static BOOL test_mux_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
130 struct smbcli_request *req;
132 printf("testing multiplexed lock/write/close\n");
134 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
136 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
141 cli->session->pid = 1;
144 if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK))) {
145 printf("lock failed in mux_write - %s\n", smbcli_errstr(cli->tree));
150 cli->session->pid = 2;
152 /* send an async write */
153 io.generic.level = RAW_WRITE_WRITEX;
154 io.writex.in.fnum = fnum;
155 io.writex.in.offset = 0;
156 io.writex.in.wmode = 0;
157 io.writex.in.remaining = 0;
158 io.writex.in.count = 4;
159 io.writex.in.data = (void *)&fnum;
160 req = smb_raw_write_send(cli->tree, &io);
162 /* unlock the range */
163 cli->session->pid = 1;
164 smbcli_unlock(cli->tree, fnum, 0, 4);
166 /* and recv the async write reply */
167 status = smb_raw_write_recv(req, &io);
168 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
170 smbcli_close(cli->tree, fnum);
178 test a lock that conflicts with an existing lock
180 static BOOL test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
186 struct smbcli_request *req;
187 struct smb_lock_entry lock[1];
189 printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
191 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
193 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
198 printf("establishing a lock\n");
199 io.lockx.level = RAW_LOCK_LOCKX;
200 io.lockx.in.fnum = fnum;
201 io.lockx.in.mode = 0;
202 io.lockx.in.timeout = 0;
203 io.lockx.in.lock_cnt = 1;
204 io.lockx.in.ulock_cnt = 0;
208 io.lockx.in.locks = &lock[0];
210 status = smb_raw_lock(cli->tree, &io);
211 CHECK_STATUS(status, NT_STATUS_OK);
213 printf("the second lock will conflict with the first\n");
215 io.lockx.in.timeout = 1000;
216 status = smb_raw_lock(cli->tree, &io);
217 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
219 printf("this will too, but we'll unlock while waiting\n");
220 req = smb_raw_lock_send(cli->tree, &io);
222 printf("unlock the first range\n");
224 io.lockx.in.ulock_cnt = 1;
225 io.lockx.in.lock_cnt = 0;
226 io.lockx.in.timeout = 0;
227 status = smb_raw_lock(cli->tree, &io);
228 CHECK_STATUS(status, NT_STATUS_OK);
230 printf("recv the async reply\n");
231 status = smbcli_request_simple_recv(req);
232 CHECK_STATUS(status, NT_STATUS_OK);
234 printf("reopening with an exit\n");
235 smb_raw_exit(cli->session);
236 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
238 printf("Now trying with a cancel\n");
240 io.lockx.level = RAW_LOCK_LOCKX;
241 io.lockx.in.fnum = fnum;
242 io.lockx.in.mode = 0;
243 io.lockx.in.timeout = 0;
244 io.lockx.in.lock_cnt = 1;
245 io.lockx.in.ulock_cnt = 0;
249 io.lockx.in.locks = &lock[0];
251 status = smb_raw_lock(cli->tree, &io);
252 CHECK_STATUS(status, NT_STATUS_OK);
255 io.lockx.in.timeout = 1000;
256 status = smb_raw_lock(cli->tree, &io);
257 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
259 req = smb_raw_lock_send(cli->tree, &io);
261 /* cancel the blocking lock */
262 smb_raw_ntcancel(req);
264 /* the 2nd cancel is totally harmless, but tests the server trying to
265 cancel an already cancelled request */
266 smb_raw_ntcancel(req);
269 io.lockx.in.ulock_cnt = 1;
270 io.lockx.in.lock_cnt = 0;
271 io.lockx.in.timeout = 0;
272 status = smb_raw_lock(cli->tree, &io);
273 CHECK_STATUS(status, NT_STATUS_OK);
275 status = smbcli_request_simple_recv(req);
276 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
278 smbcli_close(cli->tree, fnum);
287 basic testing of multiplexing notify
289 BOOL torture_raw_mux(void)
291 struct smbcli_state *cli;
295 if (!torture_open_connection(&cli)) {
299 mem_ctx = talloc_init("torture_raw_mux");
302 if (smbcli_deltree(cli->tree, BASEDIR) == -1) {
303 printf("Failed to cleanup " BASEDIR "\n");
309 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
310 printf("Failed to create %s\n", BASEDIR);
315 if (!test_mux_open(cli, mem_ctx)) {
319 if (!test_mux_write(cli, mem_ctx)) {
323 if (!test_mux_lock(cli, mem_ctx)) {
328 smb_raw_exit(cli->session);
329 smbcli_deltree(cli->tree, BASEDIR);
330 torture_close_connection(cli);
331 talloc_destroy(mem_ctx);