s3fs-printing: Fix RAW printing for normal users.
[Samba.git] / source4 / torture / raw / mux.c
blobe8418f892c7cf7ea9a57b398f53ff429b8c63e1e
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"
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)); \
33 ret = false; \
34 goto done; \
35 }} while (0)
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)
43 union smb_open io;
44 NTSTATUS status;
45 int fnum1, fnum2;
46 bool ret = true;
47 struct smbcli_request *req1, *req2;
48 struct timeval tv;
49 double d;
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.fnum = 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.out.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.out.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);
86 } else {
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);
105 if (d > 0.25) {
106 printf("bad timeout after cancel - %.2f should be <0.25\n", d);
107 ret = false;
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);
118 if (d > 0.25) {
119 printf("bad timeout for async conflict - %.2f should be <0.25\n", d);
120 ret = false;
121 } else {
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);
129 if (d < 0.8) {
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.out.file.fnum);
136 done:
137 return ret;
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)
146 union smb_write io;
147 NTSTATUS status;
148 int fnum;
149 bool ret = true;
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);
155 if (fnum == -1) {
156 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
157 ret = false;
158 goto done;
161 cli->session->pid = 1;
163 /* lock a range */
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));
166 ret = false;
167 goto done;
170 cli->session->pid = 2;
172 /* send an async write */
173 io.generic.level = RAW_WRITE_WRITEX;
174 io.writex.in.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 = (const uint8_t *)&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);
192 done:
193 return ret;
198 test a lock that conflicts with an existing lock
200 static bool test_mux_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
202 union smb_lock io;
203 NTSTATUS status;
204 int fnum;
205 bool ret = true;
206 struct smbcli_request *req;
207 struct smb_lock_entry lock[1];
208 struct timeval t;
210 printf("TESTING MULTIPLEXED LOCK/LOCK/UNLOCK\n");
212 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
213 if (fnum == -1) {
214 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
215 ret = false;
216 goto done;
219 printf("establishing a lock\n");
220 io.lockx.level = RAW_LOCK_LOCKX;
221 io.lockx.in.file.fnum = fnum;
222 io.lockx.in.mode = 0;
223 io.lockx.in.timeout = 0;
224 io.lockx.in.lock_cnt = 1;
225 io.lockx.in.ulock_cnt = 0;
226 lock[0].pid = 1;
227 lock[0].offset = 0;
228 lock[0].count = 4;
229 io.lockx.in.locks = &lock[0];
231 status = smb_raw_lock(cli->tree, &io);
232 CHECK_STATUS(status, NT_STATUS_OK);
234 printf("the second lock will conflict with the first\n");
235 lock[0].pid = 2;
236 io.lockx.in.timeout = 1000;
237 status = smb_raw_lock(cli->tree, &io);
238 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
240 printf("this will too, but we'll unlock while waiting\n");
241 t = timeval_current();
242 req = smb_raw_lock_send(cli->tree, &io);
244 printf("unlock the first range\n");
245 lock[0].pid = 1;
246 io.lockx.in.ulock_cnt = 1;
247 io.lockx.in.lock_cnt = 0;
248 io.lockx.in.timeout = 0;
249 status = smb_raw_lock(cli->tree, &io);
250 CHECK_STATUS(status, NT_STATUS_OK);
252 printf("recv the async reply\n");
253 status = smbcli_request_simple_recv(req);
254 CHECK_STATUS(status, NT_STATUS_OK);
256 printf("async lock took %.2f msec\n", timeval_elapsed(&t) * 1000);
257 if (timeval_elapsed(&t) > 0.1) {
258 printf("failed to trigger early lock retry\n");
259 return false;
262 printf("reopening with an exit\n");
263 smb_raw_exit(cli->session);
264 fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", O_RDWR | O_CREAT, DENY_NONE);
266 printf("Now trying with a cancel\n");
268 io.lockx.level = RAW_LOCK_LOCKX;
269 io.lockx.in.file.fnum = fnum;
270 io.lockx.in.mode = 0;
271 io.lockx.in.timeout = 0;
272 io.lockx.in.lock_cnt = 1;
273 io.lockx.in.ulock_cnt = 0;
274 lock[0].pid = 1;
275 lock[0].offset = 0;
276 lock[0].count = 4;
277 io.lockx.in.locks = &lock[0];
279 status = smb_raw_lock(cli->tree, &io);
280 CHECK_STATUS(status, NT_STATUS_OK);
282 lock[0].pid = 2;
283 io.lockx.in.timeout = 1000;
284 status = smb_raw_lock(cli->tree, &io);
285 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
287 req = smb_raw_lock_send(cli->tree, &io);
289 /* cancel the blocking lock */
290 smb_raw_ntcancel(req);
292 printf("sending 2nd cancel\n");
293 /* the 2nd cancel is totally harmless, but tests the server trying to
294 cancel an already cancelled request */
295 smb_raw_ntcancel(req);
297 printf("sent 2nd cancel\n");
299 lock[0].pid = 1;
300 io.lockx.in.ulock_cnt = 1;
301 io.lockx.in.lock_cnt = 0;
302 io.lockx.in.timeout = 0;
303 status = smb_raw_lock(cli->tree, &io);
304 CHECK_STATUS(status, NT_STATUS_OK);
306 status = smbcli_request_simple_recv(req);
307 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
309 printf("cancel a lock using exit to close file\n");
310 lock[0].pid = 1;
311 io.lockx.in.ulock_cnt = 0;
312 io.lockx.in.lock_cnt = 1;
313 io.lockx.in.timeout = 1000;
315 status = smb_raw_lock(cli->tree, &io);
316 CHECK_STATUS(status, NT_STATUS_OK);
318 t = timeval_current();
319 lock[0].pid = 2;
320 req = smb_raw_lock_send(cli->tree, &io);
322 smb_raw_exit(cli->session);
323 smb_raw_exit(cli->session);
324 smb_raw_exit(cli->session);
325 smb_raw_exit(cli->session);
327 printf("recv the async reply\n");
328 status = smbcli_request_simple_recv(req);
329 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
330 printf("async lock exit took %.2f msec\n", timeval_elapsed(&t) * 1000);
331 if (timeval_elapsed(&t) > 0.1) {
332 printf("failed to trigger early lock failure\n");
333 return false;
336 done:
337 return ret;
343 basic testing of multiplexing notify
345 bool torture_raw_mux(struct torture_context *torture, struct smbcli_state *cli)
347 bool ret = true;
349 if (!torture_setup_dir(cli, BASEDIR)) {
350 return false;
353 ret &= test_mux_open(cli, torture);
354 ret &= test_mux_write(cli, torture);
355 ret &= test_mux_lock(cli, torture);
357 smb_raw_exit(cli->session);
358 smbcli_deltree(cli->tree, BASEDIR);
359 return ret;