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 3 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, see <http://www.gnu.org/licenses/>.
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"
28 #define BASEDIR "\\test_mux"
31 test the delayed reply to a open that leads to a sharing violation
33 static bool test_mux_open(struct torture_context
*tctx
, struct smbcli_state
*cli
, TALLOC_CTX
*mem_ctx
)
39 struct smbcli_request
*req1
, *req2
;
43 torture_comment(tctx
, "Testing multiplexed open/open/close\n");
45 torture_comment(tctx
, "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
;
62 torture_comment(tctx
, "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
;
68 tv
= timeval_current();
70 torture_comment(tctx
, "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");
75 d
= timeval_elapsed(&tv
);
76 if (d
< 0.5 || d
> 1.5) {
77 torture_comment(tctx
, "bad timeout for conflict - %.2f should be 1.0\n", d
);
79 torture_comment(tctx
, "open delay %.2f\n", d
);
82 torture_comment(tctx
, "send async open, conflicting\n");
83 tv
= timeval_current();
84 req1
= smb_raw_open_send(cli
->tree
, &io
);
86 torture_comment(tctx
, "send 2nd async open, conflicting\n");
87 tv
= timeval_current();
88 req2
= smb_raw_open_send(cli
->tree
, &io
);
90 torture_comment(tctx
, "close first sync open\n");
91 smbcli_close(cli
->tree
, fnum1
);
93 torture_comment(tctx
, "cancel 2nd async open (should be ignored)\n");
94 smb_raw_ntcancel(req2
);
96 d
= timeval_elapsed(&tv
);
98 torture_comment(tctx
, "bad timeout after cancel - %.2f should be <0.25\n", d
);
99 torture_assert(tctx
, d
<= 0.25, "bad timeout after cancel");
102 torture_comment(tctx
, "close the 2nd sync open\n");
103 smbcli_close(cli
->tree
, fnum2
);
105 torture_comment(tctx
, "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");
109 d
= timeval_elapsed(&tv
);
111 torture_comment(tctx
, "bad timeout for async conflict - %.2f should be <0.25\n", d
);
112 torture_assert(tctx
, d
<= 0.25, "bad timeout for async conflict");
114 torture_comment(tctx
, "async open delay %.2f\n", d
);
117 torture_comment(tctx
, "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
);
122 torture_comment(tctx
, "bad timeout for async conflict - %.2f should be 1.0\n", d
);
125 torture_comment(tctx
, "close the 1st async open\n");
126 smbcli_close(cli
->tree
, io
.ntcreatex
.out
.file
.fnum
);
133 test a write that hits a byte range lock and send the close after the write
135 static bool test_mux_write(struct torture_context
*tctx
, struct smbcli_state
*cli
, TALLOC_CTX
*mem_ctx
)
141 struct smbcli_request
*req
;
143 torture_comment(tctx
, "Testing multiplexed lock/write/close\n");
145 fnum
= smbcli_open(cli
->tree
, BASEDIR
"\\write.dat", O_RDWR
| O_CREAT
, DENY_NONE
);
147 torture_comment(tctx
, "open failed in mux_write - %s\n", smbcli_errstr(cli
->tree
));
148 torture_assert(tctx
, fnum
!= -1, "open failed in mux_write");
151 cli
->session
->pid
= 1;
153 status
= smbcli_lock(cli
->tree
, fnum
, 0, 4, 0, WRITE_LOCK
);
156 if (NT_STATUS_IS_ERR(status
)) {
157 torture_assert_ntstatus_ok(tctx
, status
, "lock failed in mux_write");
160 cli
->session
->pid
= 2;
162 /* send an async write */
163 io
.generic
.level
= RAW_WRITE_WRITEX
;
164 io
.writex
.in
.file
.fnum
= fnum
;
165 io
.writex
.in
.offset
= 0;
166 io
.writex
.in
.wmode
= 0;
167 io
.writex
.in
.remaining
= 0;
168 io
.writex
.in
.count
= 4;
169 io
.writex
.in
.data
= (const uint8_t *)&fnum
;
170 req
= smb_raw_write_send(cli
->tree
, &io
);
172 /* unlock the range */
173 cli
->session
->pid
= 1;
174 smbcli_unlock(cli
->tree
, fnum
, 0, 4);
176 /* and recv the async write reply */
177 status
= smb_raw_write_recv(req
, &io
);
178 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_FILE_LOCK_CONFLICT
, "recv the async write reply");
180 smbcli_close(cli
->tree
, fnum
);
187 test a lock that conflicts with an existing lock
189 static bool test_mux_lock(struct torture_context
*tctx
, struct smbcli_state
*cli
, TALLOC_CTX
*mem_ctx
)
195 struct smbcli_request
*req
;
196 struct smb_lock_entry lock
[1];
199 torture_comment(tctx
, "TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
201 fnum
= smbcli_open(cli
->tree
, BASEDIR
"\\write.dat", O_RDWR
| O_CREAT
, DENY_NONE
);
203 torture_comment(tctx
, "open failed in mux_lock - %s\n", smbcli_errstr(cli
->tree
));
204 torture_assert(tctx
, fnum
!= -1, "open failed in mux_lock");
207 torture_comment(tctx
, "establishing a lock\n");
208 io
.lockx
.level
= RAW_LOCK_LOCKX
;
209 io
.lockx
.in
.file
.fnum
= fnum
;
210 io
.lockx
.in
.mode
= 0;
211 io
.lockx
.in
.timeout
= 0;
212 io
.lockx
.in
.lock_cnt
= 1;
213 io
.lockx
.in
.ulock_cnt
= 0;
217 io
.lockx
.in
.locks
= &lock
[0];
219 status
= smb_raw_lock(cli
->tree
, &io
);
220 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "establishing a lock");
222 torture_comment(tctx
, "the second lock will conflict with the first\n");
224 io
.lockx
.in
.timeout
= 1000;
225 status
= smb_raw_lock(cli
->tree
, &io
);
226 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_FILE_LOCK_CONFLICT
, "the second lock will conflict with the first");
228 torture_comment(tctx
, "this will too, but we'll unlock while waiting\n");
229 t
= timeval_current();
230 req
= smb_raw_lock_send(cli
->tree
, &io
);
232 torture_comment(tctx
, "unlock the first range\n");
234 io
.lockx
.in
.ulock_cnt
= 1;
235 io
.lockx
.in
.lock_cnt
= 0;
236 io
.lockx
.in
.timeout
= 0;
237 status
= smb_raw_lock(cli
->tree
, &io
);
238 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "unlock the first range");
240 torture_comment(tctx
, "recv the async reply\n");
241 status
= smbcli_request_simple_recv(req
);
242 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "recv the async reply");
244 torture_comment(tctx
, "async lock took %.2f msec\n", timeval_elapsed(&t
) * 1000);
245 torture_assert(tctx
, timeval_elapsed(&t
) <= 0.1, "failed to trigger early lock retry\n");
247 torture_comment(tctx
, "reopening with an exit\n");
248 smb_raw_exit(cli
->session
);
249 fnum
= smbcli_open(cli
->tree
, BASEDIR
"\\write.dat", O_RDWR
| O_CREAT
, DENY_NONE
);
251 torture_comment(tctx
, "Now trying with a cancel\n");
253 io
.lockx
.level
= RAW_LOCK_LOCKX
;
254 io
.lockx
.in
.file
.fnum
= fnum
;
255 io
.lockx
.in
.mode
= 0;
256 io
.lockx
.in
.timeout
= 0;
257 io
.lockx
.in
.lock_cnt
= 1;
258 io
.lockx
.in
.ulock_cnt
= 0;
262 io
.lockx
.in
.locks
= &lock
[0];
264 status
= smb_raw_lock(cli
->tree
, &io
);
265 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "Now trying with a cancel");
268 io
.lockx
.in
.timeout
= 1000;
269 status
= smb_raw_lock(cli
->tree
, &io
);
270 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_FILE_LOCK_CONFLICT
, "Now trying with a cancel pid 2");
272 req
= smb_raw_lock_send(cli
->tree
, &io
);
274 /* cancel the blocking lock */
275 smb_raw_ntcancel(req
);
277 torture_comment(tctx
, "sending 2nd cancel\n");
278 /* the 2nd cancel is totally harmless, but tests the server trying to
279 cancel an already cancelled request */
280 smb_raw_ntcancel(req
);
282 torture_comment(tctx
, "sent 2nd cancel\n");
285 io
.lockx
.in
.ulock_cnt
= 1;
286 io
.lockx
.in
.lock_cnt
= 0;
287 io
.lockx
.in
.timeout
= 0;
288 status
= smb_raw_lock(cli
->tree
, &io
);
289 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "clear lock");
291 status
= smbcli_request_simple_recv(req
);
292 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_FILE_LOCK_CONFLICT
, "recv 2nd cancel");
294 torture_comment(tctx
, "cancel a lock using exit to close file\n");
296 io
.lockx
.in
.ulock_cnt
= 0;
297 io
.lockx
.in
.lock_cnt
= 1;
298 io
.lockx
.in
.timeout
= 1000;
300 status
= smb_raw_lock(cli
->tree
, &io
);
301 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_OK
, "cancel a lock using exit to close file");
303 t
= timeval_current();
305 req
= smb_raw_lock_send(cli
->tree
, &io
);
307 smb_raw_exit(cli
->session
);
308 smb_raw_exit(cli
->session
);
309 smb_raw_exit(cli
->session
);
310 smb_raw_exit(cli
->session
);
312 torture_comment(tctx
, "recv the async reply\n");
313 status
= smbcli_request_simple_recv(req
);
314 torture_assert_ntstatus_equal(tctx
, status
, NT_STATUS_RANGE_NOT_LOCKED
, "recv the async reply");
315 torture_comment(tctx
, "async lock exit took %.2f msec\n", timeval_elapsed(&t
) * 1000);
316 torture_assert(tctx
, timeval_elapsed(&t
) <= 0.1, "failed to trigger early lock failure\n");
324 basic testing of multiplexing notify
326 bool torture_raw_mux(struct torture_context
*torture
, struct smbcli_state
*cli
)
331 torture_assert(torture
, torture_setup_dir(cli
, BASEDIR
), "Failed to setup up test directory: " BASEDIR
);
332 frame
= talloc_stackframe();
334 ret
&= test_mux_open(torture
, cli
, frame
);
335 ret
&= test_mux_write(torture
, cli
, frame
);
336 ret
&= test_mux_lock(torture
, cli
, frame
);
338 smb_raw_exit(cli
->session
);
339 smbcli_deltree(cli
->tree
, BASEDIR
);