s3:winbindd: s/struct event_context/struct tevent_context
[Samba/gebeck_regimport.git] / source4 / torture / raw / mux.c
blobc82a6c3636766b4e5f913e2ed6733241a092acf1
1 /*
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/>.
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"
28 #define BASEDIR "\\test_mux"
30 #define CHECK_STATUS(status, correct) do { \
31 if (!NT_STATUS_EQUAL(status, correct)) { \
32 printf("(%s) Incorrect status %s - should be %s\n", \
33 __location__, nt_errstr(status), nt_errstr(correct)); \
34 ret = false; \
35 goto done; \
36 }} while (0)
40 test the delayed reply to a open that leads to a sharing violation
42 static bool test_mux_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
44 union smb_open io;
45 NTSTATUS status;
46 int fnum1, fnum2;
47 bool ret = true;
48 struct smbcli_request *req1, *req2;
49 struct timeval tv;
50 double d;
52 printf("Testing multiplexed open/open/close\n");
54 printf("send first open\n");
55 io.generic.level = RAW_OPEN_NTCREATEX;
56 io.ntcreatex.in.root_fid.fnum = 0;
57 io.ntcreatex.in.flags = 0;
58 io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
59 io.ntcreatex.in.create_options = 0;
60 io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
61 io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
62 io.ntcreatex.in.alloc_size = 0;
63 io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
64 io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
65 io.ntcreatex.in.security_flags = 0;
66 io.ntcreatex.in.fname = BASEDIR "\\open.dat";
67 status = smb_raw_open(cli->tree, mem_ctx, &io);
68 CHECK_STATUS(status, NT_STATUS_OK);
69 fnum1 = io.ntcreatex.out.file.fnum;
71 printf("send 2nd open, non-conflicting\n");
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_OK);
75 fnum2 = io.ntcreatex.out.file.fnum;
77 tv = timeval_current();
79 printf("send 3rd open, conflicting\n");
80 io.ntcreatex.in.share_access = 0;
81 status = smb_raw_open(cli->tree, mem_ctx, &io);
82 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
84 d = timeval_elapsed(&tv);
85 if (d < 0.5 || d > 1.5) {
86 printf("bad timeout for conflict - %.2f should be 1.0\n", d);
87 } else {
88 printf("open delay %.2f\n", d);
91 printf("send async open, conflicting\n");
92 tv = timeval_current();
93 req1 = smb_raw_open_send(cli->tree, &io);
95 printf("send 2nd async open, conflicting\n");
96 tv = timeval_current();
97 req2 = smb_raw_open_send(cli->tree, &io);
99 printf("close first sync open\n");
100 smbcli_close(cli->tree, fnum1);
102 printf("cancel 2nd async open (should be ignored)\n");
103 smb_raw_ntcancel(req2);
105 d = timeval_elapsed(&tv);
106 if (d > 0.25) {
107 printf("bad timeout after cancel - %.2f should be <0.25\n", d);
108 ret = false;
111 printf("close the 2nd sync open\n");
112 smbcli_close(cli->tree, fnum2);
114 printf("see if the 1st async open now succeeded\n");
115 status = smb_raw_open_recv(req1, mem_ctx, &io);
116 CHECK_STATUS(status, NT_STATUS_OK);
118 d = timeval_elapsed(&tv);
119 if (d > 0.25) {
120 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
121 ret = false;
122 } else {
123 printf("async open delay %.2f\n", d);
126 printf("2nd async open should have timed out\n");
127 status = smb_raw_open_recv(req2, mem_ctx, &io);
128 CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
129 d = timeval_elapsed(&tv);
130 if (d < 0.8) {
131 printf("bad timeout for async conflict - %.2f should be 1.0\n", d);
134 printf("close the 1st async open\n");
135 smbcli_close(cli->tree, io.ntcreatex.out.file.fnum);
137 done:
138 return ret;
143 test a write that hits a byte range lock and send the close after the write
145 static bool test_mux_write(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
147 union smb_write io;
148 NTSTATUS status;
149 int fnum;
150 bool ret = true;
151 struct smbcli_request *req;
153 printf("Testing multiplexed lock/write/close\n");
155 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
156 if (fnum == -1) {
157 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
158 ret = false;
159 goto done;
162 cli->session->pid = 1;
164 /* lock a range */
165 if (NT_STATUS_IS_ERR(smbcli_lock(cli->tree, fnum, 0, 4, 0, WRITE_LOCK))) {
166 printf("lock failed in mux_write - %s\n", smbcli_errstr(cli->tree));
167 ret = false;
168 goto done;
171 cli->session->pid = 2;
173 /* send an async write */
174 io.generic.level = RAW_WRITE_WRITEX;
175 io.writex.in.file.fnum = fnum;
176 io.writex.in.offset = 0;
177 io.writex.in.wmode = 0;
178 io.writex.in.remaining = 0;
179 io.writex.in.count = 4;
180 io.writex.in.data = (const uint8_t *)&fnum;
181 req = smb_raw_write_send(cli->tree, &io);
183 /* unlock the range */
184 cli->session->pid = 1;
185 smbcli_unlock(cli->tree, fnum, 0, 4);
187 /* and recv the async write reply */
188 status = smb_raw_write_recv(req, &io);
189 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
191 smbcli_close(cli->tree, fnum);
193 done:
194 return ret;
199 test a lock that conflicts with an existing lock
201 static bool test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
203 union smb_lock io;
204 NTSTATUS status;
205 int fnum;
206 bool ret = true;
207 struct smbcli_request *req;
208 struct smb_lock_entry lock[1];
209 struct timeval t;
211 printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
213 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
214 if (fnum == -1) {
215 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
216 ret = false;
217 goto done;
220 printf("establishing a lock\n");
221 io.lockx.level = RAW_LOCK_LOCKX;
222 io.lockx.in.file.fnum = fnum;
223 io.lockx.in.mode = 0;
224 io.lockx.in.timeout = 0;
225 io.lockx.in.lock_cnt = 1;
226 io.lockx.in.ulock_cnt = 0;
227 lock[0].pid = 1;
228 lock[0].offset = 0;
229 lock[0].count = 4;
230 io.lockx.in.locks = &lock[0];
232 status = smb_raw_lock(cli->tree, &io);
233 CHECK_STATUS(status, NT_STATUS_OK);
235 printf("the second lock will conflict with the first\n");
236 lock[0].pid = 2;
237 io.lockx.in.timeout = 1000;
238 status = smb_raw_lock(cli->tree, &io);
239 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
241 printf("this will too, but we'll unlock while waiting\n");
242 t = timeval_current();
243 req = smb_raw_lock_send(cli->tree, &io);
245 printf("unlock the first range\n");
246 lock[0].pid = 1;
247 io.lockx.in.ulock_cnt = 1;
248 io.lockx.in.lock_cnt = 0;
249 io.lockx.in.timeout = 0;
250 status = smb_raw_lock(cli->tree, &io);
251 CHECK_STATUS(status, NT_STATUS_OK);
253 printf("recv the async reply\n");
254 status = smbcli_request_simple_recv(req);
255 CHECK_STATUS(status, NT_STATUS_OK);
257 printf("async lock took %.2f msec\n", timeval_elapsed(&t) * 1000);
258 if (timeval_elapsed(&t) > 0.1) {
259 printf("failed to trigger early lock retry\n");
260 return false;
263 printf("reopening with an exit\n");
264 smb_raw_exit(cli->session);
265 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
267 printf("Now trying with a cancel\n");
269 io.lockx.level = RAW_LOCK_LOCKX;
270 io.lockx.in.file.fnum = fnum;
271 io.lockx.in.mode = 0;
272 io.lockx.in.timeout = 0;
273 io.lockx.in.lock_cnt = 1;
274 io.lockx.in.ulock_cnt = 0;
275 lock[0].pid = 1;
276 lock[0].offset = 0;
277 lock[0].count = 4;
278 io.lockx.in.locks = &lock[0];
280 status = smb_raw_lock(cli->tree, &io);
281 CHECK_STATUS(status, NT_STATUS_OK);
283 lock[0].pid = 2;
284 io.lockx.in.timeout = 1000;
285 status = smb_raw_lock(cli->tree, &io);
286 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
288 req = smb_raw_lock_send(cli->tree, &io);
290 /* cancel the blocking lock */
291 smb_raw_ntcancel(req);
293 printf("sending 2nd cancel\n");
294 /* the 2nd cancel is totally harmless, but tests the server trying to
295 cancel an already cancelled request */
296 smb_raw_ntcancel(req);
298 printf("sent 2nd cancel\n");
300 lock[0].pid = 1;
301 io.lockx.in.ulock_cnt = 1;
302 io.lockx.in.lock_cnt = 0;
303 io.lockx.in.timeout = 0;
304 status = smb_raw_lock(cli->tree, &io);
305 CHECK_STATUS(status, NT_STATUS_OK);
307 status = smbcli_request_simple_recv(req);
308 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
310 printf("cancel a lock using exit to close file\n");
311 lock[0].pid = 1;
312 io.lockx.in.ulock_cnt = 0;
313 io.lockx.in.lock_cnt = 1;
314 io.lockx.in.timeout = 1000;
316 status = smb_raw_lock(cli->tree, &io);
317 CHECK_STATUS(status, NT_STATUS_OK);
319 t = timeval_current();
320 lock[0].pid = 2;
321 req = smb_raw_lock_send(cli->tree, &io);
323 smb_raw_exit(cli->session);
324 smb_raw_exit(cli->session);
325 smb_raw_exit(cli->session);
326 smb_raw_exit(cli->session);
328 printf("recv the async reply\n");
329 status = smbcli_request_simple_recv(req);
330 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
331 printf("async lock exit took %.2f msec\n", timeval_elapsed(&t) * 1000);
332 if (timeval_elapsed(&t) > 0.1) {
333 printf("failed to trigger early lock failure\n");
334 return false;
337 done:
338 return ret;
344 basic testing of multiplexing notify
346 bool torture_raw_mux(struct torture_context *torture, struct smbcli_state *cli)
348 bool ret = true;
350 torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
352 ret &= test_mux_open(cli, torture);
353 ret &= test_mux_write(cli, torture);
354 ret &= test_mux_lock(cli, torture);
356 smb_raw_exit(cli->session);
357 smbcli_deltree(cli->tree, BASEDIR);
358 return ret;