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 = 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;
69 tv = timeval_current();
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);
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);
81 printf("open delay %.2f\n", d);
85 same request, but async
87 tv = timeval_current();
88 req = smb_raw_open_send(cli->tree, &io);
90 /* and close the file */
91 smbcli_close(cli->tree, fnum);
93 /* see if the async open succeeded */
94 status = smb_raw_open_recv(req, mem_ctx, &io);
95 CHECK_STATUS(status, NT_STATUS_OK);
97 d = timeval_elapsed(&tv);
99 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
102 printf("async open delay %.2f\n", d);
105 smbcli_close(cli->tree, io.ntcreatex.out.fnum);
113 test a write that hits a byte range lock and send the close after the write
115 static BOOL test_mux_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
121 struct smbcli_request *req;
123 printf("testing multiplexed lock/write/close\n");
125 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
127 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
132 cli->session->pid = 1;
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));
141 cli->session->pid = 2;
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);
153 /* unlock the range */
154 cli->session->pid = 1;
155 smbcli_unlock(cli->tree, fnum, 0, 4);
157 /* and recv the async write reply */
158 status = smb_raw_write_recv(req, &io);
159 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
161 smbcli_close(cli->tree, fnum);
169 test a lock that conflicts with an existing lock
171 static BOOL test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
177 struct smbcli_request *req;
178 struct smb_lock_entry lock[1];
180 printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
182 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
184 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
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;
199 io.lockx.in.locks = &lock[0];
201 status = smb_raw_lock(cli->tree, &io);
202 CHECK_STATUS(status, NT_STATUS_OK);
204 printf("the second lock will conflict with the first\n");
206 io.lockx.in.timeout = 1000;
207 status = smb_raw_lock(cli->tree, &io);
208 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
210 printf("this will too, but we'll unlock while waiting\n");
211 req = smb_raw_lock_send(cli->tree, &io);
213 printf("unlock the first range\n");
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);
221 printf("recv the async reply\n");
222 status = smbcli_request_simple_recv(req);
223 CHECK_STATUS(status, NT_STATUS_OK);
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);
229 printf("Now trying with a cancel\n");
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;
240 io.lockx.in.locks = &lock[0];
242 status = smb_raw_lock(cli->tree, &io);
243 CHECK_STATUS(status, NT_STATUS_OK);
246 io.lockx.in.timeout = 1000;
247 status = smb_raw_lock(cli->tree, &io);
248 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
250 req = smb_raw_lock_send(cli->tree, &io);
252 /* cancel the blocking lock */
253 smb_raw_ntcancel(req);
255 /* the 2nd cancel is totally harmless, but tests the server trying to
256 cancel an already cancelled request */
257 smb_raw_ntcancel(req);
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);
266 status = smbcli_request_simple_recv(req);
267 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
269 smbcli_close(cli->tree, fnum);
278 basic testing of multiplexing notify
280 BOOL torture_raw_mux(void)
282 struct smbcli_state *cli;
286 if (!torture_open_connection(&cli)) {
290 mem_ctx = talloc_init("torture_raw_mux");
293 if (smbcli_deltree(cli->tree, BASEDIR) == -1) {
294 printf("Failed to cleanup " BASEDIR "\n");
300 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
301 printf("Failed to create %s\n", BASEDIR);
306 if (!test_mux_open(cli, mem_ctx)) {
310 if (!test_mux_write(cli, mem_ctx)) {
314 if (!test_mux_lock(cli, mem_ctx)) {
319 smb_raw_exit(cli->session);
320 smbcli_deltree(cli->tree, BASEDIR);
321 torture_close_connection(cli);
322 talloc_destroy(mem_ctx);