Debian Codegraph @

source4/torture/raw/lock.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for various lock operations
4 Copyright (C) Andrew Tridgell 2003
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28 #include "libcli/composite/composite.h"
29 #include "libcli/smb_composite/smb_composite.h"
30 #include "lib/cmdline/cmdline.h"
31 #include "param/param.h"
32 #include "torture/raw/proto.h"
33
34 #define CHECK_STATUS(status, correct) do { \
35 if (!NT_STATUS_EQUAL(status, correct)) { \
36 torture_result(tctx, TORTURE_FAIL, \
37 "(%s) Incorrect status %s - should be %s\n", \
38 __location__, nt_errstr(status), nt_errstr(correct)); \
39 ret = false; \
40 goto done; \
41 }} while (0)
42
43 #define CHECK_STATUS_CONT(status, correct) do { \
44 if (!NT_STATUS_EQUAL(status, correct)) { \
45 torture_result(tctx, TORTURE_FAIL, \
46 "(%s) Incorrect status %s - should be %s\n", \
47 __location__, nt_errstr(status), nt_errstr(correct)); \
48 ret = false; \
49 }} while (0)
50
51 #define CHECK_STATUS_OR(status, correct1, correct2) do { \
52 if ((!NT_STATUS_EQUAL(status, correct1)) && \
53 (!NT_STATUS_EQUAL(status, correct2))) { \
54 torture_result(tctx, TORTURE_FAIL, \
55 "(%s) Incorrect status %s - should be %s or %s\n", \
56 __location__, nt_errstr(status), nt_errstr(correct1), \
57 nt_errstr(correct2)); \
58 ret = false; \
59 goto done; \
60 }} while (0)
61
62 #define CHECK_STATUS_OR_CONT(status, correct1, correct2) do { \
63 if ((!NT_STATUS_EQUAL(status, correct1)) && \
64 (!NT_STATUS_EQUAL(status, correct2))) { \
65 torture_result(tctx, TORTURE_FAIL, \
66 "(%s) Incorrect status %s - should be %s or %s\n", \
67 __location__, nt_errstr(status), nt_errstr(correct1), \
68 nt_errstr(correct2)); \
69 ret = false; \
70 }} while (0)
71 #define BASEDIR "\\testlock"
72
73 #define TARGET_IS_W2K8(_tctx) (torture_setting_bool(_tctx, "w2k8", false))
74 #define TARGET_IS_WIN7(_tctx) (torture_setting_bool(_tctx, "win7", false))
75 #define TARGET_IS_SAMBA3(_tctx) (torture_setting_bool(_tctx, "samba3", false))
76 #define TARGET_IS_SAMBA4(_tctx) (torture_setting_bool(_tctx, "samba4", false))
77
78 #define TARGET_SUPPORTS_INVALID_LOCK_RANGE(_tctx) \
79 (torture_setting_bool(_tctx, "invalid_lock_range_support", true))
80 #define TARGET_SUPPORTS_SMBEXIT(_tctx) \
81 (torture_setting_bool(_tctx, "smbexit_pdu_support", true))
82 #define TARGET_SUPPORTS_SMBLOCK(_tctx) \
83 (torture_setting_bool(_tctx, "smblock_pdu_support", true))
84 #define TARGET_SUPPORTS_OPENX_DENY_DOS(_tctx) \
85 (torture_setting_bool(_tctx, "openx_deny_dos_support", true))
86 #define TARGET_RETURNS_RANGE_NOT_LOCKED(_tctx) \
87 (torture_setting_bool(_tctx, "range_not_locked_on_file_close", true))
88 /*
89 test SMBlock and SMBunlock ops
90 */
91 static bool test_lock(struct torture_context *tctx, struct smbcli_state *cli)
92 {
93 union smb_lock io;
94 NTSTATUS status;
95 bool ret = true;
96 int fnum;
97 const char *fname = BASEDIR "\\test.txt";
98
99 if (!TARGET_SUPPORTS_SMBLOCK(tctx))
100 torture_skip(tctx, "Target does not support the SMBlock PDU");
101
102 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
103
104 torture_comment(tctx, "Testing RAW_LOCK_LOCK\n");
105 io.generic.level = RAW_LOCK_LOCK;
106
107 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
108 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
109 "Failed to create %s - %s\n",
110 fname, smbcli_errstr(cli->tree)));
111
112 torture_comment(tctx, "Trying 0/0 lock\n");
113 io.lock.level = RAW_LOCK_LOCK;
114 io.lock.in.file.fnum = fnum;
115 io.lock.in.count = 0;
116 io.lock.in.offset = 0;
117 status = smb_raw_lock(cli->tree, &io);
118 CHECK_STATUS(status, NT_STATUS_OK);
119 cli->session->pid++;
120 status = smb_raw_lock(cli->tree, &io);
121 CHECK_STATUS(status, NT_STATUS_OK);
122 cli->session->pid--;
123 io.lock.level = RAW_LOCK_UNLOCK;
124 status = smb_raw_lock(cli->tree, &io);
125 CHECK_STATUS(status, NT_STATUS_OK);
126
127 torture_comment(tctx, "Trying 0/1 lock\n");
128 io.lock.level = RAW_LOCK_LOCK;
129 io.lock.in.file.fnum = fnum;
130 io.lock.in.count = 1;
131 io.lock.in.offset = 0;
132 status = smb_raw_lock(cli->tree, &io);
133 CHECK_STATUS(status, NT_STATUS_OK);
134 cli->session->pid++;
135 status = smb_raw_lock(cli->tree, &io);
136 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
137 cli->session->pid--;
138 io.lock.level = RAW_LOCK_UNLOCK;
139 status = smb_raw_lock(cli->tree, &io);
140 CHECK_STATUS(status, NT_STATUS_OK);
141 io.lock.level = RAW_LOCK_UNLOCK;
142 status = smb_raw_lock(cli->tree, &io);
143 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
144
145 torture_comment(tctx, "Trying 0xEEFFFFFF lock\n");
146 io.lock.level = RAW_LOCK_LOCK;
147 io.lock.in.file.fnum = fnum;
148 io.lock.in.count = 4000;
149 io.lock.in.offset = 0xEEFFFFFF;
150 status = smb_raw_lock(cli->tree, &io);
151 CHECK_STATUS(status, NT_STATUS_OK);
152 cli->session->pid++;
153 status = smb_raw_lock(cli->tree, &io);
154 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
155 cli->session->pid--;
156 io.lock.level = RAW_LOCK_UNLOCK;
157 status = smb_raw_lock(cli->tree, &io);
158 CHECK_STATUS(status, NT_STATUS_OK);
159 io.lock.level = RAW_LOCK_UNLOCK;
160 status = smb_raw_lock(cli->tree, &io);
161 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
162
163 torture_comment(tctx, "Trying 0xEEFFFFFF lock\n");
164 io.lock.level = RAW_LOCK_LOCK;
165 io.lock.in.file.fnum = fnum;
166 io.lock.in.count = 4000;
167 io.lock.in.offset = 0xEEFFFFFF;
168 status = smb_raw_lock(cli->tree, &io);
169 CHECK_STATUS(status, NT_STATUS_OK);
170 cli->session->pid++;
171 status = smb_raw_lock(cli->tree, &io);
172 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
173 cli->session->pid--;
174 io.lock.level = RAW_LOCK_UNLOCK;
175 status = smb_raw_lock(cli->tree, &io);
176 CHECK_STATUS(status, NT_STATUS_OK);
177 io.lock.level = RAW_LOCK_UNLOCK;
178 status = smb_raw_lock(cli->tree, &io);
179 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
180
181 torture_comment(tctx, "Trying max lock\n");
182 io.lock.level = RAW_LOCK_LOCK;
183 io.lock.in.file.fnum = fnum;
184 io.lock.in.count = 4000;
185 io.lock.in.offset = 0xEF000000;
186 status = smb_raw_lock(cli->tree, &io);
187 CHECK_STATUS(status, NT_STATUS_OK);
188 cli->session->pid++;
189 status = smb_raw_lock(cli->tree, &io);
190 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
191 cli->session->pid--;
192 io.lock.level = RAW_LOCK_UNLOCK;
193 status = smb_raw_lock(cli->tree, &io);
194 CHECK_STATUS(status, NT_STATUS_OK);
195 io.lock.level = RAW_LOCK_UNLOCK;
196 status = smb_raw_lock(cli->tree, &io);
197 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
198
199 torture_comment(tctx, "Trying wrong pid unlock\n");
200 io.lock.level = RAW_LOCK_LOCK;
201 io.lock.in.file.fnum = fnum;
202 io.lock.in.count = 4002;
203 io.lock.in.offset = 10001;
204 status = smb_raw_lock(cli->tree, &io);
205 CHECK_STATUS(status, NT_STATUS_OK);
206 cli->session->pid++;
207 io.lock.level = RAW_LOCK_UNLOCK;
208 status = smb_raw_lock(cli->tree, &io);
209 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
210 cli->session->pid--;
211 status = smb_raw_lock(cli->tree, &io);
212 CHECK_STATUS(status, NT_STATUS_OK);
213
214 done:
215 smbcli_close(cli->tree, fnum);
216 smb_raw_exit(cli->session);
217 smbcli_deltree(cli->tree, BASEDIR);
218 return ret;
219 }
220
221
222 /*
223 test locking&X ops
224 */
225 static bool test_lockx(struct torture_context *tctx, struct smbcli_state *cli)
226 {
227 union smb_lock io;
228 struct smb_lock_entry lock[1];
229 NTSTATUS status;
230 bool ret = true;
231 int fnum;
232 const char *fname = BASEDIR "\\test.txt";
233
234 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
235
236 torture_comment(tctx, "Testing RAW_LOCK_LOCKX\n");
237 io.generic.level = RAW_LOCK_LOCKX;
238
239 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
240 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
241 "Failed to create %s - %s\n",
242 fname, smbcli_errstr(cli->tree)));
243
244 io.lockx.level = RAW_LOCK_LOCKX;
245 io.lockx.in.file.fnum = fnum;
246 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
247 io.lockx.in.timeout = 0;
248 io.lockx.in.ulock_cnt = 0;
249 io.lockx.in.lock_cnt = 1;
250 lock[0].pid = cli->session->pid;
251 lock[0].offset = 10;
252 lock[0].count = 1;
253 io.lockx.in.locks = &lock[0];
254 status = smb_raw_lock(cli->tree, &io);
255 CHECK_STATUS(status, NT_STATUS_OK);
256
257
258 torture_comment(tctx, "Trying 0xEEFFFFFF lock\n");
259 io.lockx.in.ulock_cnt = 0;
260 io.lockx.in.lock_cnt = 1;
261 lock[0].count = 4000;
262 lock[0].offset = 0xEEFFFFFF;
263 status = smb_raw_lock(cli->tree, &io);
264 CHECK_STATUS(status, NT_STATUS_OK);
265 lock[0].pid++;
266 status = smb_raw_lock(cli->tree, &io);
267 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
268 lock[0].pid--;
269 io.lockx.in.ulock_cnt = 1;
270 io.lockx.in.lock_cnt = 0;
271 status = smb_raw_lock(cli->tree, &io);
272 CHECK_STATUS(status, NT_STATUS_OK);
273 status = smb_raw_lock(cli->tree, &io);
274 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
275
276 torture_comment(tctx, "Trying 0xEF000000 lock\n");
277 io.lockx.in.ulock_cnt = 0;
278 io.lockx.in.lock_cnt = 1;
279 lock[0].count = 4000;
280 lock[0].offset = 0xEF000000;
281 status = smb_raw_lock(cli->tree, &io);
282 CHECK_STATUS(status, NT_STATUS_OK);
283 lock[0].pid++;
284 status = smb_raw_lock(cli->tree, &io);
285 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
286 lock[0].pid--;
287 io.lockx.in.ulock_cnt = 1;
288 io.lockx.in.lock_cnt = 0;
289 status = smb_raw_lock(cli->tree, &io);
290 CHECK_STATUS(status, NT_STATUS_OK);
291 status = smb_raw_lock(cli->tree, &io);
292 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
293
294 torture_comment(tctx, "Trying zero lock\n");
295 io.lockx.in.ulock_cnt = 0;
296 io.lockx.in.lock_cnt = 1;
297 lock[0].count = 0;
298 lock[0].offset = ~0;
299 status = smb_raw_lock(cli->tree, &io);
300 CHECK_STATUS(status, NT_STATUS_OK);
301 lock[0].pid++;
302 status = smb_raw_lock(cli->tree, &io);
303 CHECK_STATUS(status, NT_STATUS_OK);
304 lock[0].pid--;
305 io.lockx.in.ulock_cnt = 1;
306 io.lockx.in.lock_cnt = 0;
307 status = smb_raw_lock(cli->tree, &io);
308 CHECK_STATUS(status, NT_STATUS_OK);
309 status = smb_raw_lock(cli->tree, &io);
310 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
311
312 torture_comment(tctx, "Trying max lock\n");
313 io.lockx.in.ulock_cnt = 0;
314 io.lockx.in.lock_cnt = 1;
315 lock[0].count = 0;
316 lock[0].offset = ~0;
317 status = smb_raw_lock(cli->tree, &io);
318 CHECK_STATUS(status, NT_STATUS_OK);
319 lock[0].pid++;
320 status = smb_raw_lock(cli->tree, &io);
321 CHECK_STATUS(status, NT_STATUS_OK);
322 lock[0].pid--;
323 io.lockx.in.ulock_cnt = 1;
324 io.lockx.in.lock_cnt = 0;
325 status = smb_raw_lock(cli->tree, &io);
326 CHECK_STATUS(status, NT_STATUS_OK);
327 status = smb_raw_lock(cli->tree, &io);
328 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
329
330 torture_comment(tctx, "Trying 2^63\n");
331 io.lockx.in.ulock_cnt = 0;
332 io.lockx.in.lock_cnt = 1;
333 lock[0].count = 1;
334 lock[0].offset = 1;
335 lock[0].offset <<= 63;
336 status = smb_raw_lock(cli->tree, &io);
337 CHECK_STATUS(status, NT_STATUS_OK);
338 lock[0].pid++;
339 status = smb_raw_lock(cli->tree, &io);
340 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
341 lock[0].pid--;
342 io.lockx.in.ulock_cnt = 1;
343 io.lockx.in.lock_cnt = 0;
344 status = smb_raw_lock(cli->tree, &io);
345 CHECK_STATUS(status, NT_STATUS_OK);
346 status = smb_raw_lock(cli->tree, &io);
347 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
348
349 torture_comment(tctx, "Trying 2^63 - 1\n");
350 io.lockx.in.ulock_cnt = 0;
351 io.lockx.in.lock_cnt = 1;
352 lock[0].count = 1;
353 lock[0].offset = 1;
354 lock[0].offset <<= 63;
355 lock[0].offset--;
356 status = smb_raw_lock(cli->tree, &io);
357 CHECK_STATUS(status, NT_STATUS_OK);
358 lock[0].pid++;
359 status = smb_raw_lock(cli->tree, &io);
360 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
361 lock[0].pid--;
362 io.lockx.in.ulock_cnt = 1;
363 io.lockx.in.lock_cnt = 0;
364 status = smb_raw_lock(cli->tree, &io);
365 CHECK_STATUS(status, NT_STATUS_OK);
366 status = smb_raw_lock(cli->tree, &io);
367 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
368
369 torture_comment(tctx, "Trying max lock 2\n");
370 io.lockx.in.ulock_cnt = 0;
371 io.lockx.in.lock_cnt = 1;
372 lock[0].count = 1;
373 lock[0].offset = ~0;
374 status = smb_raw_lock(cli->tree, &io);
375 CHECK_STATUS(status, NT_STATUS_OK);
376 lock[0].pid++;
377 lock[0].count = 2;
378 status = smb_raw_lock(cli->tree, &io);
379 if (TARGET_SUPPORTS_INVALID_LOCK_RANGE(tctx))
380 CHECK_STATUS(status, NT_STATUS_INVALID_LOCK_RANGE);
381 else
382 CHECK_STATUS(status, NT_STATUS_OK);
383 lock[0].pid--;
384 io.lockx.in.ulock_cnt = 1;
385 io.lockx.in.lock_cnt = 0;
386 lock[0].count = 1;
387 status = smb_raw_lock(cli->tree, &io);
388
389 CHECK_STATUS(status, NT_STATUS_OK);
390 status = smb_raw_lock(cli->tree, &io);
391 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
392
393 done:
394 smbcli_close(cli->tree, fnum);
395 smb_raw_exit(cli->session);
396 smbcli_deltree(cli->tree, BASEDIR);
397 return ret;
398 }
399
400 /*
401 test high pid
402 */
403 static bool test_pidhigh(struct torture_context *tctx,
404 struct smbcli_state *cli)
405 {
406 union smb_lock io;
407 struct smb_lock_entry lock[1];
408 NTSTATUS status;
409 bool ret = true;
410 int fnum;
411 const char *fname = BASEDIR "\\test.txt";
412 uint8_t c = 1;
413
414 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
415
416 torture_comment(tctx, "Testing high pid\n");
417 io.generic.level = RAW_LOCK_LOCKX;
418
419 cli->session->pid = 1;
420
421 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
422 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
423 "Failed to create %s - %s\n",
424 fname, smbcli_errstr(cli->tree)));
425
426 if (smbcli_write(cli->tree, fnum, 0, &c, 0, 1) != 1) {
427 torture_result(tctx, TORTURE_FAIL,
428 "Failed to write 1 byte - %s\n",
429 smbcli_errstr(cli->tree));
430 ret = false;
431 goto done;
432 }
433
434 io.lockx.level = RAW_LOCK_LOCKX;
435 io.lockx.in.file.fnum = fnum;
436 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
437 io.lockx.in.timeout = 0;
438 io.lockx.in.ulock_cnt = 0;
439 io.lockx.in.lock_cnt = 1;
440 lock[0].pid = cli->session->pid;
441 lock[0].offset = 0;
442 lock[0].count = 0xFFFFFFFF;
443 io.lockx.in.locks = &lock[0];
444 status = smb_raw_lock(cli->tree, &io);
445 CHECK_STATUS(status, NT_STATUS_OK);
446
447 if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
448 torture_result(tctx, TORTURE_FAIL,
449 "Failed to read 1 byte - %s\n",
450 smbcli_errstr(cli->tree));
451 ret = false;
452 goto done;
453 }
454
455 cli->session->pid = 2;
456
457 if (smbcli_read(cli->tree, fnum, &c, 0, 1) == 1) {
458 torture_result(tctx, TORTURE_FAIL,
459 "pid is incorrect handled for read with lock!\n");
460 ret = false;
461 goto done;
462 }
463
464 cli->session->pid = 0x10001;
465
466 if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
467 torture_result(tctx, TORTURE_FAIL,
468 "High pid is used on this server!\n");
469 ret = false;
470 } else {
471 torture_warning(tctx, "High pid is not used on this server (correct)\n");
472 }
473
474 done:
475 smbcli_close(cli->tree, fnum);
476 smb_raw_exit(cli->session);
477 smbcli_deltree(cli->tree, BASEDIR);
478 return ret;
479 }
480
481
482 /*
483 test locking&X async operation
484 */
485 static bool test_async(struct torture_context *tctx,
486 struct smbcli_state *cli)
487 {
488 struct smbcli_session *session;
489 struct smb_composite_sesssetup setup;
490 struct smbcli_tree *tree;
491 union smb_tcon tcon;
492 const char *host, *share;
493 union smb_lock io;
494 struct smb_lock_entry lock[2];
495 NTSTATUS status;
496 bool ret = true;
497 int fnum;
498 const char *fname = BASEDIR "\\test.txt";
499 time_t t;
500 struct smbcli_request *req, *req2;
501 struct smbcli_session_options options;
502
503 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
504
505 lpcfg_smbcli_session_options(tctx->lp_ctx, &options);
506
507 torture_comment(tctx, "Testing LOCKING_ANDX_CANCEL_LOCK\n");
508 io.generic.level = RAW_LOCK_LOCKX;
509
510 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
511 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
512 "Failed to create %s - %s\n",
513 fname, smbcli_errstr(cli->tree)));
514
515 io.lockx.level = RAW_LOCK_LOCKX;
516 io.lockx.in.file.fnum = fnum;
517 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
518 io.lockx.in.timeout = 0;
519 io.lockx.in.ulock_cnt = 0;
520 io.lockx.in.lock_cnt = 1;
521 lock[0].pid = cli->session->pid;
522 lock[0].offset = 100;
523 lock[0].count = 10;
524 lock[1].pid = cli->session->pid;
525 lock[1].offset = 110;
526 lock[1].count = 10;
527 io.lockx.in.locks = &lock[0];
528 status = smb_raw_lock(cli->tree, &io);
529 CHECK_STATUS(status, NT_STATUS_OK);
530
531 t = time_mono(NULL);
532
533 torture_comment(tctx, "Testing cancel by CANCEL_LOCK\n");
534
535 /* setup a timed lock */
536 io.lockx.in.timeout = 10000;
537 req = smb_raw_lock_send(cli->tree, &io);
538 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
539 "Failed to setup timed lock (%s)\n", __location__));
540
541 /* cancel the wrong range */
542 lock[0].offset = 0;
543 io.lockx.in.timeout = 0;
544 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
545 status = smb_raw_lock(cli->tree, &io);
546 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
547
548 /* cancel with the wrong bits set */
549 lock[0].offset = 100;
550 io.lockx.in.timeout = 0;
551 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
552 status = smb_raw_lock(cli->tree, &io);
553 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
554
555 /* cancel the right range */
556 lock[0].offset = 100;
557 io.lockx.in.timeout = 0;
558 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
559 status = smb_raw_lock(cli->tree, &io);
560 CHECK_STATUS(status, NT_STATUS_OK);
561
562 /* receive the failed lock request */
563 status = smbcli_request_simple_recv(req);
564 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
565
566 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
567 "lock cancel was not immediate (%s)\n", __location__));
568
569 /* MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
570 * if the lock vector contains one entry. When given multiple cancel
571 * requests in a single PDU we expect the server to return an
572 * error. Samba4 handles this correctly. Windows servers seem to
573 * accept the request but only cancel the first lock. Samba3
574 * now does what Windows does (JRA).
575 */
576 torture_comment(tctx, "Testing multiple cancel\n");
577
578 /* acquire second lock */
579 io.lockx.in.timeout = 0;
580 io.lockx.in.ulock_cnt = 0;
581 io.lockx.in.lock_cnt = 1;
582 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
583 io.lockx.in.locks = &lock[1];
584 status = smb_raw_lock(cli->tree, &io);
585 CHECK_STATUS(status, NT_STATUS_OK);
586
587 /* setup 2 timed locks */
588 t = time_mono(NULL);
589 io.lockx.in.timeout = 10000;
590 io.lockx.in.lock_cnt = 1;
591 io.lockx.in.locks = &lock[0];
592 req = smb_raw_lock_send(cli->tree, &io);
593 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
594 "Failed to setup timed lock (%s)\n", __location__));
595 io.lockx.in.locks = &lock[1];
596 req2 = smb_raw_lock_send(cli->tree, &io);
597 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
598 "Failed to setup timed lock (%s)\n", __location__));
599
600 /* try to cancel both locks in the same packet */
601 io.lockx.in.timeout = 0;
602 io.lockx.in.lock_cnt = 2;
603 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
604 io.lockx.in.locks = lock;
605 status = smb_raw_lock(cli->tree, &io);
606 CHECK_STATUS(status, NT_STATUS_OK);
607
608 torture_warning(tctx, "Target server accepted a lock cancel "
609 "request with multiple locks. This violates "
610 "MS-CIFS 2.2.4.32.1.\n");
611
612 /* receive the failed lock requests */
613 status = smbcli_request_simple_recv(req);
614 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
615
616 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
617 "first lock was not cancelled immediately (%s)\n",
618 __location__));
619
620 /* send cancel to second lock */
621 io.lockx.in.timeout = 0;
622 io.lockx.in.lock_cnt = 1;
623 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK |
624 LOCKING_ANDX_LARGE_FILES;
625 io.lockx.in.locks = &lock[1];
626 status = smb_raw_lock(cli->tree, &io);
627 CHECK_STATUS(status, NT_STATUS_OK);
628
629 status = smbcli_request_simple_recv(req2);
630 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
631
632 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
633 "second lock was not cancelled immediately (%s)\n",
634 __location__));
635
636 /* cleanup the second lock */
637 io.lockx.in.ulock_cnt = 1;
638 io.lockx.in.lock_cnt = 0;
639 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
640 io.lockx.in.locks = &lock[1];
641 status = smb_raw_lock(cli->tree, &io);
642 CHECK_STATUS(status, NT_STATUS_OK);
643
644 /* If a lock request contained multiple ranges and we are cancelling
645 * one while it's still pending, what happens? */
646 torture_comment(tctx, "Testing cancel 1/2 lock request\n");
647
648 /* Send request with two ranges */
649 io.lockx.in.timeout = -1;
650 io.lockx.in.ulock_cnt = 0;
651 io.lockx.in.lock_cnt = 2;
652 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
653 io.lockx.in.locks = lock;
654 req = smb_raw_lock_send(cli->tree, &io);
655 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
656 "Failed to setup pending lock (%s)\n", __location__));
657
658 /* Try to cancel the first lock range */
659 io.lockx.in.timeout = 0;
660 io.lockx.in.lock_cnt = 1;
661 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
662 io.lockx.in.locks = &lock[0];
663 status = smb_raw_lock(cli->tree, &io);
664 CHECK_STATUS(status, NT_STATUS_OK);
665
666 /* Locking request should've failed and second range should be
667 * unlocked */
668 status = smbcli_request_simple_recv(req);
669 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
670
671 io.lockx.in.timeout = 0;
672 io.lockx.in.ulock_cnt = 0;
673 io.lockx.in.lock_cnt = 1;
674 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
675 io.lockx.in.locks = &lock[1];
676 status = smb_raw_lock(cli->tree, &io);
677 CHECK_STATUS(status, NT_STATUS_OK);
678
679 /* Cleanup both locks */
680 io.lockx.in.ulock_cnt = 2;
681 io.lockx.in.lock_cnt = 0;
682 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
683 io.lockx.in.locks = lock;
684 status = smb_raw_lock(cli->tree, &io);
685 CHECK_STATUS(status, NT_STATUS_OK);
686
687 torture_comment(tctx, "Testing cancel 2/2 lock request\n");
688
689 /* Lock second range so it contends */
690 io.lockx.in.timeout = 0;
691 io.lockx.in.ulock_cnt = 0;
692 io.lockx.in.lock_cnt = 1;
693 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
694 io.lockx.in.locks = &lock[1];
695 status = smb_raw_lock(cli->tree, &io);
696 CHECK_STATUS(status, NT_STATUS_OK);
697
698 /* Send request with two ranges */
699 io.lockx.in.timeout = -1;
700 io.lockx.in.ulock_cnt = 0;
701 io.lockx.in.lock_cnt = 2;
702 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
703 io.lockx.in.locks = lock;
704 req = smb_raw_lock_send(cli->tree, &io);
705 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
706 "Failed to setup pending lock (%s)\n", __location__));
707
708 /* Try to cancel the second lock range */
709 io.lockx.in.timeout = 0;
710 io.lockx.in.lock_cnt = 1;
711 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
712 io.lockx.in.locks = &lock[1];
713 status = smb_raw_lock(cli->tree, &io);
714 CHECK_STATUS(status, NT_STATUS_OK);
715
716 /* Locking request should've failed and first range should be
717 * unlocked */
718 status = smbcli_request_simple_recv(req);
719 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
720
721 io.lockx.in.timeout = 0;
722 io.lockx.in.ulock_cnt = 0;
723 io.lockx.in.lock_cnt = 1;
724 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
725 io.lockx.in.locks = &lock[0];
726 status = smb_raw_lock(cli->tree, &io);
727 CHECK_STATUS(status, NT_STATUS_OK);
728
729 /* Cleanup both locks */
730 io.lockx.in.ulock_cnt = 2;
731 io.lockx.in.lock_cnt = 0;
732 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
733 io.lockx.in.locks = lock;
734 status = smb_raw_lock(cli->tree, &io);
735 CHECK_STATUS(status, NT_STATUS_OK);
736
737 torture_comment(tctx, "Testing cancel by unlock\n");
738 io.lockx.in.ulock_cnt = 0;
739 io.lockx.in.lock_cnt = 1;
740 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
741 io.lockx.in.timeout = 0;
742 io.lockx.in.locks = &lock[0];
743 status = smb_raw_lock(cli->tree, &io);
744 CHECK_STATUS(status, NT_STATUS_OK);
745
746 io.lockx.in.timeout = 5000;
747 req = smb_raw_lock_send(cli->tree, &io);
748 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
749 "Failed to setup timed lock (%s)\n", __location__));
750
751 io.lockx.in.ulock_cnt = 1;
752 io.lockx.in.lock_cnt = 0;
753 status = smb_raw_lock(cli->tree, &io);
754 CHECK_STATUS(status, NT_STATUS_OK);
755
756 t = time_mono(NULL);
757 status = smbcli_request_simple_recv(req);
758 CHECK_STATUS(status, NT_STATUS_OK);
759
760 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
761 "lock cancel by unlock was not immediate (%s) - took %d secs\n",
762 __location__, (int)(time_mono(NULL)-t)));
763
764 torture_comment(tctx, "Testing cancel by close\n");
765 io.lockx.in.ulock_cnt = 0;
766 io.lockx.in.lock_cnt = 1;
767 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
768 io.lockx.in.timeout = 0;
769 status = smb_raw_lock(cli->tree, &io);
770 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
771
772 {
773 /*
774 * Make the test block on the second lock
775 * request. This is to regression-test 64c0367.
776 */
777 uint64_t tmp = lock[1].offset;
778 lock[1].offset = lock[0].offset;
779 lock[0].offset = tmp;
780 }
781
782 t = time_mono(NULL);
783 io.lockx.in.timeout = 10000;
784 io.lockx.in.lock_cnt = 2;
785 req = smb_raw_lock_send(cli->tree, &io);
786 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
787 "Failed to setup timed lock (%s)\n", __location__));
788
789 status = smbcli_close(cli->tree, fnum);
790 CHECK_STATUS(status, NT_STATUS_OK);
791
792 status = smbcli_request_simple_recv(req);
793 if (TARGET_RETURNS_RANGE_NOT_LOCKED(tctx))
794 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
795 else
796 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
797
798 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
799 "lock cancel by close was not immediate (%s)\n", __location__));
800
801 {
802 /*
803 * Undo the change for 64c0367
804 */
805 uint64_t tmp = lock[1].offset;
806 lock[1].offset = lock[0].offset;
807 lock[0].offset = tmp;
808 }
809
810 torture_comment(tctx, "create a new sessions\n");
811 session = smbcli_session_init(cli->transport, tctx, false, options);
812 setup.in.sesskey = cli->transport->negotiate.sesskey;
813 setup.in.capabilities = cli->transport->negotiate.capabilities;
814 setup.in.workgroup = lpcfg_workgroup(tctx->lp_ctx);
815 setup.in.credentials = samba_cmdline_get_creds();
816 setup.in.gensec_settings = lpcfg_gensec_settings(tctx, tctx->lp_ctx);
817 status = smb_composite_sesssetup(session, &setup);
818 CHECK_STATUS(status, NT_STATUS_OK);
819 session->vuid = setup.out.vuid;
820
821 torture_comment(tctx, "create new tree context\n");
822 share = torture_setting_string(tctx, "share", NULL);
823 host = torture_setting_string(tctx, "host", NULL);
824 tree = smbcli_tree_init(session, tctx, false);
825 tcon.generic.level = RAW_TCON_TCONX;
826 tcon.tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
827 tcon.tconx.in.password = data_blob(NULL, 0);
828 tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
829 tcon.tconx.in.device = "A:";
830 status = smb_raw_tcon(tree, tctx, &tcon);
831 CHECK_STATUS(status, NT_STATUS_OK);
832 tree->tid = tcon.tconx.out.tid;
833
834 torture_comment(tctx, "Testing cancel by exit\n");
835 if (TARGET_SUPPORTS_SMBEXIT(tctx)) {
836 fname = BASEDIR "\\test_exit.txt";
837 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
838 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
839 "Failed to reopen %s - %s\n",
840 fname, smbcli_errstr(tree)));
841
842 io.lockx.level = RAW_LOCK_LOCKX;
843 io.lockx.in.file.fnum = fnum;
844 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
845 io.lockx.in.timeout = 0;
846 io.lockx.in.ulock_cnt = 0;
847 io.lockx.in.lock_cnt = 1;
848 lock[0].pid = session->pid;
849 lock[0].offset = 100;
850 lock[0].count = 10;
851 io.lockx.in.locks = &lock[0];
852 status = smb_raw_lock(tree, &io);
853 CHECK_STATUS(status, NT_STATUS_OK);
854
855 io.lockx.in.ulock_cnt = 0;
856 io.lockx.in.lock_cnt = 1;
857 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
858 io.lockx.in.timeout = 0;
859 status = smb_raw_lock(tree, &io);
860 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
861
862 io.lockx.in.timeout = 10000;
863 t = time_mono(NULL);
864 req = smb_raw_lock_send(tree, &io);
865 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
866 "Failed to setup timed lock (%s)\n",
867 __location__));
868
869 status = smb_raw_exit(session);
870 CHECK_STATUS(status, NT_STATUS_OK);
871
872 status = smbcli_request_simple_recv(req);
873 if (TARGET_RETURNS_RANGE_NOT_LOCKED(tctx))
874 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
875 else
876 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
877
878 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
879 "lock cancel by exit was not immediate (%s)\n",
880 __location__));
881 }
882 else {
883 torture_comment(tctx,
884 " skipping test, SMBExit not supported\n");
885 }
886
887 torture_comment(tctx, "Testing cancel by ulogoff\n");
888 fname = BASEDIR "\\test_ulogoff.txt";
889 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
890 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
891 "Failed to reopen %s - %s\n",
892 fname, smbcli_errstr(tree)));
893
894 io.lockx.level = RAW_LOCK_LOCKX;
895 io.lockx.in.file.fnum = fnum;
896 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
897 io.lockx.in.timeout = 0;
898 io.lockx.in.ulock_cnt = 0;
899 io.lockx.in.lock_cnt = 1;
900 lock[0].pid = session->pid;
901 lock[0].offset = 100;
902 lock[0].count = 10;
903 io.lockx.in.locks = &lock[0];
904 status = smb_raw_lock(tree, &io);
905 CHECK_STATUS(status, NT_STATUS_OK);
906
907 io.lockx.in.ulock_cnt = 0;
908 io.lockx.in.lock_cnt = 1;
909 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
910 io.lockx.in.timeout = 0;
911 status = smb_raw_lock(tree, &io);
912 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
913
914 io.lockx.in.timeout = 10000;
915 t = time_mono(NULL);
916 req = smb_raw_lock_send(tree, &io);
917 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
918 "Failed to setup timed lock (%s)\n", __location__));
919
920 status = smb_raw_ulogoff(session);
921 CHECK_STATUS(status, NT_STATUS_OK);
922
923 status = smbcli_request_simple_recv(req);
924 if (TARGET_RETURNS_RANGE_NOT_LOCKED(tctx)) {
925 if (NT_STATUS_EQUAL(NT_STATUS_FILE_LOCK_CONFLICT, status)) {
926 torture_result(tctx, TORTURE_FAIL,
927 "lock not canceled by ulogoff - %s "
928 "(ignored because of vfs_vifs fails it)\n",
929 nt_errstr(status));
930 smb_tree_disconnect(tree);
931 smb_raw_exit(session);
932 goto done;
933 }
934 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
935 } else {
936 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
937 }
938
939 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
940 "lock cancel by ulogoff was not immediate (%s)\n", __location__));
941
942 torture_comment(tctx, "Testing cancel by tdis\n");
943 tree->session = cli->session;
944
945 fname = BASEDIR "\\test_tdis.txt";
946 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
947 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
948 "Failed to reopen %s - %s\n",
949 fname, smbcli_errstr(tree)));
950
951 io.lockx.level = RAW_LOCK_LOCKX;
952 io.lockx.in.file.fnum = fnum;
953 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
954 io.lockx.in.timeout = 0;
955 io.lockx.in.ulock_cnt = 0;
956 io.lockx.in.lock_cnt = 1;
957 lock[0].pid = cli->session->pid;
958 lock[0].offset = 100;
959 lock[0].count = 10;
960 io.lockx.in.locks = &lock[0];
961 status = smb_raw_lock(tree, &io);
962 CHECK_STATUS(status, NT_STATUS_OK);
963
964 status = smb_raw_lock(tree, &io);
965 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
966
967 io.lockx.in.timeout = 10000;
968 t = time_mono(NULL);
969 req = smb_raw_lock_send(tree, &io);
970 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
971 "Failed to setup timed lock (%s)\n", __location__));
972
973 status = smb_tree_disconnect(tree);
974 CHECK_STATUS(status, NT_STATUS_OK);
975
976 status = smbcli_request_simple_recv(req);
977 if (TARGET_RETURNS_RANGE_NOT_LOCKED(tctx))
978 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
979 else
980 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
981
982 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
983 "lock cancel by tdis was not immediate (%s)\n", __location__));
984
985 done:
986 smb_raw_exit(cli->session);
987 smbcli_deltree(cli->tree, BASEDIR);
988 return ret;
989 }
990
991 /*
992 test NT_STATUS_LOCK_NOT_GRANTED vs. NT_STATUS_FILE_LOCK_CONFLICT
993 */
994 static bool test_errorcode(struct torture_context *tctx,
995 struct smbcli_state *cli)
996 {
997 union smb_lock io;
998 union smb_open op;
999 struct smb_lock_entry lock[2];
1000 NTSTATUS status;
1001 bool ret = true;
1002 int fnum, fnum2;
1003 const char *fname;
1004 struct smbcli_request *req;
1005 time_t start;
1006 int t;
1007 int delay;
1008 uint16_t deny_mode = 0;
1009
1010 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1011
1012 torture_comment(tctx, "Testing LOCK_NOT_GRANTED vs. FILE_LOCK_CONFLICT\n");
1013
1014 torture_comment(tctx, "Testing with timeout = 0\n");
1015 fname = BASEDIR "\\test0.txt";
1016 t = 0;
1017
1018 /*
1019 * the first run is with t = 0,
1020 * the second with t > 0 (=1)
1021 */
1022 next_run:
1023 /*
1024 * use the DENY_DOS mode, that creates two fnum's of one low-level
1025 * file handle, this demonstrates that the cache is per fnum, not
1026 * per file handle
1027 */
1028 if (TARGET_SUPPORTS_OPENX_DENY_DOS(tctx))
1029 deny_mode = OPENX_MODE_DENY_DOS;
1030 else
1031 deny_mode = OPENX_MODE_DENY_NONE;
1032
1033 op.openx.level = RAW_OPEN_OPENX;
1034 op.openx.in.fname = fname;
1035 op.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
1036 op.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | deny_mode;
1037 op.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
1038 op.openx.in.search_attrs = 0;
1039 op.openx.in.file_attrs = 0;
1040 op.openx.in.write_time = 0;
1041 op.openx.in.size = 0;
1042 op.openx.in.timeout = 0;
1043
1044 status = smb_raw_open(cli->tree, tctx, &op);
1045 CHECK_STATUS(status, NT_STATUS_OK);
1046 fnum = op.openx.out.file.fnum;
1047
1048 status = smb_raw_open(cli->tree, tctx, &op);
1049 CHECK_STATUS(status, NT_STATUS_OK);
1050 fnum2 = op.openx.out.file.fnum;
1051
1052 io.lockx.level = RAW_LOCK_LOCKX;
1053 io.lockx.in.file.fnum = fnum;
1054 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1055 io.lockx.in.timeout = t;
1056 io.lockx.in.ulock_cnt = 0;
1057 io.lockx.in.lock_cnt = 1;
1058 lock[0].pid = cli->session->pid;
1059 lock[0].offset = 100;
1060 lock[0].count = 10;
1061 io.lockx.in.locks = &lock[0];
1062 status = smb_raw_lock(cli->tree, &io);
1063 CHECK_STATUS(status, NT_STATUS_OK);
1064
1065 /*
1066 * demonstrate that the first conflicting lock on each handle give LOCK_NOT_GRANTED
1067 * this also demonstrates that the error code cache is per file handle
1068 * (LOCK_NOT_GRANTED is only be used when timeout is 0!)
1069 */
1070 io.lockx.in.file.fnum = fnum2;
1071 status = smb_raw_lock(cli->tree, &io);
1072 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1073
1074 io.lockx.in.file.fnum = fnum;
1075 status = smb_raw_lock(cli->tree, &io);
1076 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1077
1078 /* demonstrate that each following conflict gives FILE_LOCK_CONFLICT */
1079 io.lockx.in.file.fnum = fnum;
1080 status = smb_raw_lock(cli->tree, &io);
1081 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1082
1083 io.lockx.in.file.fnum = fnum2;
1084 status = smb_raw_lock(cli->tree, &io);
1085 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1086
1087 io.lockx.in.file.fnum = fnum;
1088 status = smb_raw_lock(cli->tree, &io);
1089 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1090
1091 io.lockx.in.file.fnum = fnum2;
1092 status = smb_raw_lock(cli->tree, &io);
1093 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1094
1095 /* demonstrate that the smbpid doesn't matter */
1096 lock[0].pid++;
1097 io.lockx.in.file.fnum = fnum;
1098 status = smb_raw_lock(cli->tree, &io);
1099 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1100
1101 io.lockx.in.file.fnum = fnum2;
1102 status = smb_raw_lock(cli->tree, &io);
1103 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1104 lock[0].pid--;
1105
1106 /*
1107 * demonstrate that a successful lock with count = 0 and the same offset,
1108 * doesn't reset the error cache
1109 */
1110 lock[0].offset = 100;
1111 lock[0].count = 0;
1112 io.lockx.in.file.fnum = fnum;
1113 status = smb_raw_lock(cli->tree, &io);
1114 CHECK_STATUS(status, NT_STATUS_OK);
1115
1116 io.lockx.in.file.fnum = fnum2;
1117 status = smb_raw_lock(cli->tree, &io);
1118 CHECK_STATUS(status, NT_STATUS_OK);
1119
1120 lock[0].offset = 100;
1121 lock[0].count = 10;
1122 io.lockx.in.file.fnum = fnum;
1123 status = smb_raw_lock(cli->tree, &io);
1124 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1125
1126 io.lockx.in.file.fnum = fnum2;
1127 status = smb_raw_lock(cli->tree, &io);
1128 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1129
1130 /*
1131 * demonstrate that a successful lock with count = 0 and outside the locked range,
1132 * doesn't reset the error cache
1133 */
1134 lock[0].offset = 110;
1135 lock[0].count = 0;
1136 io.lockx.in.file.fnum = fnum;
1137 status = smb_raw_lock(cli->tree, &io);
1138 CHECK_STATUS(status, NT_STATUS_OK);
1139
1140 io.lockx.in.file.fnum = fnum2;
1141 status = smb_raw_lock(cli->tree, &io);
1142 CHECK_STATUS(status, NT_STATUS_OK);
1143
1144 lock[0].offset = 100;
1145 lock[0].count = 10;
1146 io.lockx.in.file.fnum = fnum;
1147 status = smb_raw_lock(cli->tree, &io);
1148 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1149
1150 io.lockx.in.file.fnum = fnum2;
1151 status = smb_raw_lock(cli->tree, &io);
1152 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1153
1154 lock[0].offset = 99;
1155 lock[0].count = 0;
1156 io.lockx.in.file.fnum = fnum;
1157 status = smb_raw_lock(cli->tree, &io);
1158 CHECK_STATUS(status, NT_STATUS_OK);
1159
1160 io.lockx.in.file.fnum = fnum2;
1161 status = smb_raw_lock(cli->tree, &io);
1162 CHECK_STATUS(status, NT_STATUS_OK);
1163
1164 lock[0].offset = 100;
1165 lock[0].count = 10;
1166 io.lockx.in.file.fnum = fnum;
1167 status = smb_raw_lock(cli->tree, &io);
1168 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1169
1170 io.lockx.in.file.fnum = fnum2;
1171 status = smb_raw_lock(cli->tree, &io);
1172 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1173
1174 /* demonstrate that a changing count doesn't reset the error cache */
1175 lock[0].offset = 100;
1176 lock[0].count = 5;
1177 io.lockx.in.file.fnum = fnum;
1178 status = smb_raw_lock(cli->tree, &io);
1179 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1180
1181 io.lockx.in.file.fnum = fnum2;
1182 status = smb_raw_lock(cli->tree, &io);
1183 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1184
1185 lock[0].offset = 100;
1186 lock[0].count = 15;
1187 io.lockx.in.file.fnum = fnum;
1188 status = smb_raw_lock(cli->tree, &io);
1189 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1190
1191 io.lockx.in.file.fnum = fnum2;
1192 status = smb_raw_lock(cli->tree, &io);
1193 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1194
1195 /*
1196 * demonstrate that a lock with count = 0 and inside the locked range,
1197 * fails and resets the error cache
1198 */
1199 lock[0].offset = 101;
1200 lock[0].count = 0;
1201 io.lockx.in.file.fnum = fnum;
1202 status = smb_raw_lock(cli->tree, &io);
1203 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1204 status = smb_raw_lock(cli->tree, &io);
1205 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1206
1207 io.lockx.in.file.fnum = fnum2;
1208 status = smb_raw_lock(cli->tree, &io);
1209 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1210 status = smb_raw_lock(cli->tree, &io);
1211 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1212
1213 lock[0].offset = 100;
1214 lock[0].count = 10;
1215 io.lockx.in.file.fnum = fnum;
1216 status = smb_raw_lock(cli->tree, &io);
1217 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1218 status = smb_raw_lock(cli->tree, &io);
1219 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1220
1221 io.lockx.in.file.fnum = fnum2;
1222 status = smb_raw_lock(cli->tree, &io);
1223 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1224 status = smb_raw_lock(cli->tree, &io);
1225 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1226
1227 /* demonstrate that a changing offset resets the error cache */
1228 lock[0].offset = 105;
1229 lock[0].count = 10;
1230 io.lockx.in.file.fnum = fnum;
1231 status = smb_raw_lock(cli->tree, &io);
1232 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1233 status = smb_raw_lock(cli->tree, &io);
1234 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1235
1236 io.lockx.in.file.fnum = fnum2;
1237 status = smb_raw_lock(cli->tree, &io);
1238 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1239 status = smb_raw_lock(cli->tree, &io);
1240 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1241
1242 lock[0].offset = 100;
1243 lock[0].count = 10;
1244 io.lockx.in.file.fnum = fnum;
1245 status = smb_raw_lock(cli->tree, &io);
1246 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1247 status = smb_raw_lock(cli->tree, &io);
1248 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1249
1250 io.lockx.in.file.fnum = fnum2;
1251 status = smb_raw_lock(cli->tree, &io);
1252 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1253 status = smb_raw_lock(cli->tree, &io);
1254 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1255
1256 lock[0].offset = 95;
1257 lock[0].count = 9;
1258 io.lockx.in.file.fnum = fnum;
1259 status = smb_raw_lock(cli->tree, &io);
1260 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1261 status = smb_raw_lock(cli->tree, &io);
1262 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1263
1264 io.lockx.in.file.fnum = fnum2;
1265 status = smb_raw_lock(cli->tree, &io);
1266 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1267 status = smb_raw_lock(cli->tree, &io);
1268 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1269
1270 lock[0].offset = 100;
1271 lock[0].count = 10;
1272 io.lockx.in.file.fnum = fnum;
1273 status = smb_raw_lock(cli->tree, &io);
1274 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1275 status = smb_raw_lock(cli->tree, &io);
1276 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1277
1278 io.lockx.in.file.fnum = fnum2;
1279 status = smb_raw_lock(cli->tree, &io);
1280 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1281 status = smb_raw_lock(cli->tree, &io);
1282 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1283
1284 /*
1285 * demonstrate that a successful lock in a different range
1286 * doesn't reset the cache, the failing lock on the 2nd handle
1287 * resets the cache
1288 */
1289 lock[0].offset = 120;
1290 lock[0].count = 15;
1291 io.lockx.in.file.fnum = fnum;
1292 status = smb_raw_lock(cli->tree, &io);
1293 CHECK_STATUS(status, NT_STATUS_OK);
1294
1295 io.lockx.in.file.fnum = fnum2;
1296 status = smb_raw_lock(cli->tree, &io);
1297 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1298
1299 lock[0].offset = 100;
1300 lock[0].count = 10;
1301 io.lockx.in.file.fnum = fnum;
1302 status = smb_raw_lock(cli->tree, &io);
1303 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1304 status = smb_raw_lock(cli->tree, &io);
1305 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1306
1307 io.lockx.in.file.fnum = fnum2;
1308 status = smb_raw_lock(cli->tree, &io);
1309 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1310 status = smb_raw_lock(cli->tree, &io);
1311 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1312
1313 /* end of the loop */
1314 if (t == 0) {
1315 smb_raw_exit(cli->session);
1316 t = 1;
1317 torture_comment(tctx, "Testing with timeout > 0 (=%d)\n",
1318 t);
1319 fname = BASEDIR "\\test1.txt";
1320 goto next_run;
1321 }
1322
1323 t = 4000;
1324 torture_comment(tctx, "Testing special cases with timeout > 0 (=%d)\n",
1325 t);
1326
1327 /*
1328 * the following 3 test sections demonstrate that
1329 * the cache is only set when the error is reported
1330 * to the client (after the timeout went by)
1331 */
1332 smb_raw_exit(cli->session);
1333 torture_comment(tctx, "Testing a conflict while a lock is pending\n");
1334 fname = BASEDIR "\\test2.txt";
1335 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1336 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
1337 "Failed to reopen %s - %s\n",
1338 fname, smbcli_errstr(cli->tree)));
1339
1340 io.lockx.level = RAW_LOCK_LOCKX;
1341 io.lockx.in.file.fnum = fnum;
1342 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1343 io.lockx.in.timeout = 0;
1344 io.lockx.in.ulock_cnt = 0;
1345 io.lockx.in.lock_cnt = 1;
1346 lock[0].pid = cli->session->pid;
1347 lock[0].offset = 100;
1348 lock[0].count = 10;
1349 io.lockx.in.locks = &lock[0];
1350 status = smb_raw_lock(cli->tree, &io);
1351 CHECK_STATUS(status, NT_STATUS_OK);
1352
1353 start = time_mono(NULL);
1354 io.lockx.in.timeout = t;
1355 req = smb_raw_lock_send(cli->tree, &io);
1356 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
1357 "Failed to setup timed lock (%s)\n", __location__));
1358
1359 io.lockx.in.timeout = 0;
1360 lock[0].offset = 105;
1361 lock[0].count = 10;
1362 status = smb_raw_lock(cli->tree, &io);
1363 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1364
1365 status = smbcli_request_simple_recv(req);
1366 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1367
1368 delay = t / 1000;
1369 if (TARGET_IS_W2K8(tctx) || TARGET_IS_WIN7(tctx)) {
1370 delay /= 2;
1371 }
1372
1373 torture_assert(tctx,!(time_mono(NULL) < start+delay), talloc_asprintf(tctx,
1374 "lock comes back to early timeout[%d] delay[%d]"
1375 "(%s)\n", t, delay, __location__));
1376
1377 status = smb_raw_lock(cli->tree, &io);
1378 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1379
1380 smbcli_close(cli->tree, fnum);
1381 fname = BASEDIR "\\test3.txt";
1382 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1383 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
1384 "Failed to reopen %s - %s\n",
1385 fname, smbcli_errstr(cli->tree)));
1386
1387 io.lockx.level = RAW_LOCK_LOCKX;
1388 io.lockx.in.file.fnum = fnum;
1389 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1390 io.lockx.in.timeout = 0;
1391 io.lockx.in.ulock_cnt = 0;
1392 io.lockx.in.lock_cnt = 1;
1393 lock[0].pid = cli->session->pid;
1394 lock[0].offset = 100;
1395 lock[0].count = 10;
1396 io.lockx.in.locks = &lock[0];
1397 status = smb_raw_lock(cli->tree, &io);
1398 CHECK_STATUS(status, NT_STATUS_OK);
1399
1400 start = time_mono(NULL);
1401 io.lockx.in.timeout = t;
1402 req = smb_raw_lock_send(cli->tree, &io);
1403 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
1404 "Failed to setup timed lock (%s)\n", __location__));
1405
1406 io.lockx.in.timeout = 0;
1407 lock[0].offset = 105;
1408 lock[0].count = 10;
1409 status = smb_raw_lock(cli->tree, &io);
1410 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1411
1412 status = smbcli_request_simple_recv(req);
1413 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1414
1415 delay = t / 1000;
1416 if (TARGET_IS_W2K8(tctx) || TARGET_IS_WIN7(tctx)) {
1417 delay /= 2;
1418 }
1419
1420 torture_assert(tctx,!(time_mono(NULL) < start+delay), talloc_asprintf(tctx,
1421 "lock comes back to early timeout[%d] delay[%d]"
1422 "(%s)\n", t, delay, __location__));
1423
1424 lock[0].offset = 100;
1425 lock[0].count = 10;
1426 status = smb_raw_lock(cli->tree, &io);
1427 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1428
1429 smbcli_close(cli->tree, fnum);
1430 fname = BASEDIR "\\test4.txt";
1431 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1432 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
1433 "Failed to reopen %s - %s\n",
1434 fname, smbcli_errstr(cli->tree)));
1435
1436 io.lockx.level = RAW_LOCK_LOCKX;
1437 io.lockx.in.file.fnum = fnum;
1438 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1439 io.lockx.in.timeout = 0;
1440 io.lockx.in.ulock_cnt = 0;
1441 io.lockx.in.lock_cnt = 1;
1442 lock[0].pid = cli->session->pid;
1443 lock[0].offset = 100;
1444 lock[0].count = 10;
1445 io.lockx.in.locks = &lock[0];
1446 status = smb_raw_lock(cli->tree, &io);
1447 CHECK_STATUS(status, NT_STATUS_OK);
1448
1449 start = time_mono(NULL);
1450 io.lockx.in.timeout = t;
1451 req = smb_raw_lock_send(cli->tree, &io);
1452 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
1453 "Failed to setup timed lock (%s)\n", __location__));
1454
1455 io.lockx.in.timeout = 0;
1456 status = smb_raw_lock(cli->tree, &io);
1457 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1458
1459 status = smbcli_request_simple_recv(req);
1460 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1461
1462 delay = t / 1000;
1463 if (TARGET_IS_W2K8(tctx) || TARGET_IS_WIN7(tctx)) {
1464 delay /= 2;
1465 }
1466
1467 torture_assert(tctx,!(time_mono(NULL) < start+delay), talloc_asprintf(tctx,
1468 "lock comes back to early timeout[%d] delay[%d]"
1469 "(%s)\n", t, delay, __location__));
1470
1471 status = smb_raw_lock(cli->tree, &io);
1472 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1473
1474 done:
1475 smb_raw_exit(cli->session);
1476 smbcli_deltree(cli->tree, BASEDIR);
1477 return ret;
1478 }
1479
1480
1481 /*
1482 test LOCKING_ANDX_CHANGE_LOCKTYPE
1483 */
1484 static bool test_changetype(struct torture_context *tctx,
1485 struct smbcli_state *cli)
1486 {
1487 union smb_lock io;
1488 struct smb_lock_entry lock[2];
1489 NTSTATUS status;
1490 bool ret = true;
1491 int fnum;
1492 uint8_t c = 0;
1493 const char *fname = BASEDIR "\\test.txt";
1494
1495 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1496
1497 torture_comment(tctx, "Testing LOCKING_ANDX_CHANGE_LOCKTYPE\n");
1498 io.generic.level = RAW_LOCK_LOCKX;
1499
1500 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1501 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
1502 "Failed to create %s - %s\n",
1503 fname, smbcli_errstr(cli->tree)));
1504
1505 io.lockx.level = RAW_LOCK_LOCKX;
1506 io.lockx.in.file.fnum = fnum;
1507 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1508 io.lockx.in.timeout = 0;
1509 io.lockx.in.ulock_cnt = 0;
1510 io.lockx.in.lock_cnt = 1;
1511 lock[0].pid = cli->session->pid;
1512 lock[0].offset = 100;
1513 lock[0].count = 10;
1514 io.lockx.in.locks = &lock[0];
1515 status = smb_raw_lock(cli->tree, &io);
1516 CHECK_STATUS(status, NT_STATUS_OK);
1517
1518 if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
1519 torture_result(tctx, TORTURE_FAIL,
1520 "allowed write on read locked region (%s)\n", __location__);
1521 ret = false;
1522 goto done;
1523 }
1524
1525 /* windows server don't seem to support this */
1526 io.lockx.in.mode = LOCKING_ANDX_CHANGE_LOCKTYPE;
1527 status = smb_raw_lock(cli->tree, &io);
1528 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
1529
1530 if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
1531 torture_result(tctx, TORTURE_FAIL,
1532 "allowed write after lock change (%s)\n", __location__);
1533 ret = false;
1534 goto done;
1535 }
1536
1537 done:
1538 smbcli_close(cli->tree, fnum);
1539 smb_raw_exit(cli->session);
1540 smbcli_deltree(cli->tree, BASEDIR);
1541 return ret;
1542 }
1543
1544 struct double_lock_test {
1545 struct smb_lock_entry lock1;
1546 struct smb_lock_entry lock2;
1547 NTSTATUS exp_status;
1548 };
1549
1550 /**
1551 * Tests zero byte locks.
1552 */
1553 static struct double_lock_test zero_byte_tests[] = {
1554 /* {pid, offset, count}, {pid, offset, count}, status */
1555
1556 /** First, takes a zero byte lock at offset 10. Then:
1557 * - Taking 0 byte lock at 10 should succeed.
1558 * - Taking 1 byte locks at 9,10,11 should succeed.
1559 * - Taking 2 byte lock at 9 should fail.
1560 * - Taking 2 byte lock at 10 should succeed.
1561 * - Taking 3 byte lock at 9 should fail.
1562 */
1563 {{1000, 10, 0}, {1001, 10, 0}, NT_STATUS_OK},
1564 {{1000, 10, 0}, {1001, 9, 1}, NT_STATUS_OK},
1565 {{1000, 10, 0}, {1001, 10, 1}, NT_STATUS_OK},
1566 {{1000, 10, 0}, {1001, 11, 1}, NT_STATUS_OK},
1567 {{1000, 10, 0}, {1001, 9, 2}, NT_STATUS_LOCK_NOT_GRANTED},
1568 {{1000, 10, 0}, {1001, 10, 2}, NT_STATUS_OK},
1569 {{1000, 10, 0}, {1001, 9, 3}, NT_STATUS_LOCK_NOT_GRANTED},
1570
1571 /** Same, but opposite order. */
1572 {{1001, 10, 0}, {1000, 10, 0}, NT_STATUS_OK},
1573 {{1001, 9, 1}, {1000, 10, 0}, NT_STATUS_OK},
1574 {{1001, 10, 1}, {1000, 10, 0}, NT_STATUS_OK},
1575 {{1001, 11, 1}, {1000, 10, 0}, NT_STATUS_OK},
1576 {{1001, 9, 2}, {1000, 10, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1577 {{1001, 10, 2}, {1000, 10, 0}, NT_STATUS_OK},
1578 {{1001, 9, 3}, {1000, 10, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1579
1580 /** Zero zero case. */
1581 {{1000, 0, 0}, {1001, 0, 0}, NT_STATUS_OK},
1582 };
1583
1584 static bool test_zerobytelocks(struct torture_context *tctx, struct smbcli_state *cli)
1585 {
1586 union smb_lock io;
1587 NTSTATUS status;
1588 bool ret = true;
1589 int fnum, i;
1590 const char *fname = BASEDIR "\\zero.txt";
1591
1592 torture_comment(tctx, "Testing zero length byte range locks:\n");
1593
1594 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1595
1596 io.generic.level = RAW_LOCK_LOCKX;
1597
1598 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1599 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
1600 "Failed to create %s - %s\n",
1601 fname, smbcli_errstr(cli->tree)));
1602
1603 /* Setup initial parameters */
1604 io.lockx.level = RAW_LOCK_LOCKX;
1605 io.lockx.in.file.fnum = fnum;
1606 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES; /* Exclusive */
1607 io.lockx.in.timeout = 0;
1608
1609 /* Try every combination of locks in zero_byte_tests. The first lock is
1610 * assumed to succeed. The second lock may contend, depending on the
1611 * expected status. */
1612 for (i = 0;
1613 i < ARRAY_SIZE(zero_byte_tests);
1614 i++) {
1615 torture_comment(tctx, " ... {%d, %llu, %llu} + {%d, %llu, %llu} = %s\n",
1616 zero_byte_tests[i].lock1.pid,
1617 (unsigned long long) zero_byte_tests[i].lock1.offset,
1618 (unsigned long long) zero_byte_tests[i].lock1.count,
1619 zero_byte_tests[i].lock2.pid,
1620 (unsigned long long) zero_byte_tests[i].lock2.offset,
1621 (unsigned long long) zero_byte_tests[i].lock2.count,
1622 nt_errstr(zero_byte_tests[i].exp_status));
1623
1624 /* Lock both locks. */
1625 io.lockx.in.ulock_cnt = 0;
1626 io.lockx.in.lock_cnt = 1;
1627
1628 io.lockx.in.locks = discard_const_p(struct smb_lock_entry,
1629 &zero_byte_tests[i].lock1);
1630 status = smb_raw_lock(cli->tree, &io);
1631 CHECK_STATUS(status, NT_STATUS_OK);
1632
1633 io.lockx.in.locks = discard_const_p(struct smb_lock_entry,
1634 &zero_byte_tests[i].lock2);
1635 status = smb_raw_lock(cli->tree, &io);
1636
1637 if (NT_STATUS_EQUAL(zero_byte_tests[i].exp_status,
1638 NT_STATUS_LOCK_NOT_GRANTED)) {
1639 /* Allow either of the failure messages and keep going
1640 * if we see the wrong status. */
1641 CHECK_STATUS_OR_CONT(status,
1642 NT_STATUS_LOCK_NOT_GRANTED,
1643 NT_STATUS_FILE_LOCK_CONFLICT);
1644
1645 } else {
1646 CHECK_STATUS_CONT(status,
1647 zero_byte_tests[i].exp_status);
1648 }
1649
1650 /* Unlock both locks. */
1651 io.lockx.in.ulock_cnt = 1;
1652 io.lockx.in.lock_cnt = 0;
1653
1654 if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1655 status = smb_raw_lock(cli->tree, &io);
1656 CHECK_STATUS(status, NT_STATUS_OK);
1657 }
1658
1659 io.lockx.in.locks = discard_const_p(struct smb_lock_entry,
1660 &zero_byte_tests[i].lock1);
1661 status = smb_raw_lock(cli->tree, &io);
1662 CHECK_STATUS(status, NT_STATUS_OK);
1663 }
1664
1665 done:
1666 smbcli_close(cli->tree, fnum);
1667 smb_raw_exit(cli->session);
1668 smbcli_deltree(cli->tree, BASEDIR);
1669 return ret;
1670 }
1671
1672 static bool test_unlock(struct torture_context *tctx, struct smbcli_state *cli)
1673 {
1674 union smb_lock io;
1675 NTSTATUS status;
1676 bool ret = true;
1677 int fnum1, fnum2;
1678 const char *fname = BASEDIR "\\unlock.txt";
1679 struct smb_lock_entry lock1;
1680 struct smb_lock_entry lock2;
1681
1682 torture_comment(tctx, "Testing LOCKX unlock:\n");
1683
1684 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1685
1686 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1687 torture_assert(tctx,(fnum1 != -1), talloc_asprintf(tctx,
1688 "Failed to create %s - %s\n",
1689 fname, smbcli_errstr(cli->tree)));
1690
1691 fnum2 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1692 torture_assert(tctx,(fnum2 != -1), talloc_asprintf(tctx,
1693 "Failed to create %s - %s\n",
1694 fname, smbcli_errstr(cli->tree)));
1695
1696 /* Setup initial parameters */
1697 io.lockx.level = RAW_LOCK_LOCKX;
1698 io.lockx.in.timeout = 0;
1699
1700 lock1.pid = cli->session->pid;
1701 lock1.offset = 0;
1702 lock1.count = 10;
1703 lock2.pid = cli->session->pid - 1;
1704 lock2.offset = 0;
1705 lock2.count = 10;
1706
1707 /**
1708 * Take exclusive lock, then unlock it with a shared-unlock call.
1709 */
1710 torture_comment(tctx, " taking exclusive lock.\n");
1711 io.lockx.in.ulock_cnt = 0;
1712 io.lockx.in.lock_cnt = 1;
1713 io.lockx.in.mode = 0;
1714 io.lockx.in.file.fnum = fnum1;
1715 io.lockx.in.locks = &lock1;
1716 status = smb_raw_lock(cli->tree, &io);
1717 CHECK_STATUS(status, NT_STATUS_OK);
1718
1719 torture_comment(tctx, " unlock the exclusive with a shared unlock call.\n");
1720 io.lockx.in.ulock_cnt = 1;
1721 io.lockx.in.lock_cnt = 0;
1722 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1723 io.lockx.in.file.fnum = fnum1;
1724 io.lockx.in.locks = &lock1;
1725 status = smb_raw_lock(cli->tree, &io);
1726 CHECK_STATUS(status, NT_STATUS_OK);
1727
1728 torture_comment(tctx, " try shared lock on pid2/fnum2, testing the unlock.\n");
1729 io.lockx.in.ulock_cnt = 0;
1730 io.lockx.in.lock_cnt = 1;
1731 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1732 io.lockx.in.file.fnum = fnum2;
1733 io.lockx.in.locks = &lock2;
1734 status = smb_raw_lock(cli->tree, &io);
1735 CHECK_STATUS(status, NT_STATUS_OK);
1736
1737 /**
1738 * Unlock a shared lock with an exclusive-unlock call.
1739 */
1740 torture_comment(tctx, " unlock new shared lock with exclusive unlock call.\n");
1741 io.lockx.in.ulock_cnt = 1;
1742 io.lockx.in.lock_cnt = 0;
1743 io.lockx.in.mode = 0;
1744 io.lockx.in.file.fnum = fnum2;
1745 io.lockx.in.locks = &lock2;
1746 status = smb_raw_lock(cli->tree, &io);
1747 CHECK_STATUS(status, NT_STATUS_OK);
1748
1749 torture_comment(tctx, " try exclusive lock on pid1, testing the unlock.\n");
1750 io.lockx.in.ulock_cnt = 0;
1751 io.lockx.in.lock_cnt = 1;
1752 io.lockx.in.mode = 0;
1753 io.lockx.in.file.fnum = fnum1;
1754 io.lockx.in.locks = &lock1;
1755 status = smb_raw_lock(cli->tree, &io);
1756 CHECK_STATUS(status, NT_STATUS_OK);
1757
1758 /* cleanup */
1759 io.lockx.in.ulock_cnt = 1;
1760 io.lockx.in.lock_cnt = 0;
1761 status = smb_raw_lock(cli->tree, &io);
1762 CHECK_STATUS(status, NT_STATUS_OK);
1763
1764 /**
1765 * Test unlocking of 0-byte locks.
1766 */
1767
1768 torture_comment(tctx, " lock shared and exclusive 0-byte locks, testing that Windows "
1769 "always unlocks the exclusive first.\n");
1770 lock1.pid = cli->session->pid;
1771 lock1.offset = 10;
1772 lock1.count = 0;
1773 lock2.pid = cli->session->pid;
1774 lock2.offset = 5;
1775 lock2.count = 10;
1776 io.lockx.in.ulock_cnt = 0;
1777 io.lockx.in.lock_cnt = 1;
1778 io.lockx.in.file.fnum = fnum1;
1779 io.lockx.in.locks = &lock1;
1780
1781 /* lock 0-byte shared
1782 * Note: Order of the shared/exclusive locks doesn't matter. */
1783 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1784 status = smb_raw_lock(cli->tree, &io);
1785 CHECK_STATUS(status, NT_STATUS_OK);
1786
1787 /* lock 0-byte exclusive */
1788 io.lockx.in.mode = 0;
1789 status = smb_raw_lock(cli->tree, &io);
1790 CHECK_STATUS(status, NT_STATUS_OK);
1791
1792 /* test contention */
1793 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1794 io.lockx.in.locks = &lock2;
1795 io.lockx.in.file.fnum = fnum2;
1796 status = smb_raw_lock(cli->tree, &io);
1797 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1798 NT_STATUS_FILE_LOCK_CONFLICT);
1799
1800 /* unlock */
1801 io.lockx.in.ulock_cnt = 1;
1802 io.lockx.in.lock_cnt = 0;
1803 io.lockx.in.file.fnum = fnum1;
1804 io.lockx.in.locks = &lock1;
1805 status = smb_raw_lock(cli->tree, &io);
1806 CHECK_STATUS(status, NT_STATUS_OK);
1807
1808 /* test - can we take a shared lock? */
1809 io.lockx.in.ulock_cnt = 0;
1810 io.lockx.in.lock_cnt = 1;
1811 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1812 io.lockx.in.file.fnum = fnum2;
1813 io.lockx.in.locks = &lock2;
1814 status = smb_raw_lock(cli->tree, &io);
1815
1816 /* XXX Samba 3 will fail this test. This is temporary(because this isn't
1817 * new to Win7, it succeeds in WinXP too), until I can come to a
1818 * resolution as to whether Samba should support this or not. There is
1819 * code to preference unlocking exclusive locks before shared locks,
1820 * but its wrapped with "#ifdef ZERO_ZERO". -zkirsch */
1821 if (TARGET_IS_SAMBA3(tctx)) {
1822 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1823 NT_STATUS_FILE_LOCK_CONFLICT);
1824 } else {
1825 CHECK_STATUS(status, NT_STATUS_OK);
1826 }
1827
1828 /* cleanup */
1829 io.lockx.in.ulock_cnt = 1;
1830 io.lockx.in.lock_cnt = 0;
1831 status = smb_raw_lock(cli->tree, &io);
1832
1833 /* XXX Same as above. */
1834 if (TARGET_IS_SAMBA3(tctx)) {
1835 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1836 } else {
1837 CHECK_STATUS(status, NT_STATUS_OK);
1838 }
1839
1840 io.lockx.in.file.fnum = fnum1;
1841 io.lockx.in.locks = &lock1;
1842 status = smb_raw_lock(cli->tree, &io);
1843 CHECK_STATUS(status, NT_STATUS_OK);
1844
1845 done:
1846 smbcli_close(cli->tree, fnum1);
1847 smbcli_close(cli->tree, fnum2);
1848 smb_raw_exit(cli->session);
1849 smbcli_deltree(cli->tree, BASEDIR);
1850 return ret;
1851 }
1852
1853 static bool test_multiple_unlock(struct torture_context *tctx, struct smbcli_state *cli)
1854 {
1855 union smb_lock io;
1856 NTSTATUS status;
1857 bool ret = true;
1858 int fnum1;
1859 const char *fname = BASEDIR "\\unlock_multiple.txt";
1860 struct smb_lock_entry lock1;
1861 struct smb_lock_entry lock2;
1862 struct smb_lock_entry locks[2];
1863
1864 torture_comment(tctx, "Testing LOCKX multiple unlock:\n");
1865
1866 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
1867
1868 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1869 torture_assert(tctx,(fnum1 != -1), talloc_asprintf(tctx,
1870 "Failed to create %s - %s\n",
1871 fname, smbcli_errstr(cli->tree)));
1872
1873 /* Setup initial parameters */
1874 io.lockx.level = RAW_LOCK_LOCKX;
1875 io.lockx.in.timeout = 0;
1876
1877 lock1.pid = cli->session->pid;
1878 lock1.offset = 0;
1879 lock1.count = 10;
1880 lock2.pid = cli->session->pid;
1881 lock2.offset = 10;
1882 lock2.count = 10;
1883
1884 locks[0] = lock1;
1885 locks[1] = lock2;
1886
1887 io.lockx.in.file.fnum = fnum1;
1888 io.lockx.in.mode = 0; /* exclusive */
1889
1890 /** Test1: Take second lock, but not first. */
1891 torture_comment(tctx, " unlock 2 locks, first one not locked. Expect no locks "
1892 "unlocked. \n");
1893
1894 io.lockx.in.ulock_cnt = 0;
1895 io.lockx.in.lock_cnt = 1;
1896 io.lockx.in.locks = &lock2;
1897 status = smb_raw_lock(cli->tree, &io);
1898 CHECK_STATUS(status, NT_STATUS_OK);
1899
1900 /* Try to unlock both locks. */
1901 io.lockx.in.ulock_cnt = 2;
1902 io.lockx.in.lock_cnt = 0;
1903 io.lockx.in.locks = locks;
1904
1905 status = smb_raw_lock(cli->tree, &io);
1906 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1907
1908 /* Second lock should not be unlocked. */
1909 io.lockx.in.ulock_cnt = 0;
1910 io.lockx.in.lock_cnt = 1;
1911 io.lockx.in.locks = &lock2;
1912 status = smb_raw_lock(cli->tree, &io);
1913 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1914
1915 /* cleanup */
1916 io.lockx.in.ulock_cnt = 1;
1917 io.lockx.in.lock_cnt = 0;
1918 io.lockx.in.locks = &lock2;
1919 status = smb_raw_lock(cli->tree, &io);
1920 CHECK_STATUS(status, NT_STATUS_OK);
1921
1922 /** Test2: Take first lock, but not second. */
1923 torture_comment(tctx, " unlock 2 locks, second one not locked. Expect first lock "
1924 "unlocked.\n");
1925
1926 io.lockx.in.ulock_cnt = 0;
1927 io.lockx.in.lock_cnt = 1;
1928 io.lockx.in.locks = &lock1;
1929 status = smb_raw_lock(cli->tree, &io);
1930 CHECK_STATUS(status, NT_STATUS_OK);
1931
1932 /* Try to unlock both locks. */
1933 io.lockx.in.ulock_cnt = 2;
1934 io.lockx.in.lock_cnt = 0;
1935 io.lockx.in.locks = locks;
1936
1937 status = smb_raw_lock(cli->tree, &io);
1938 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1939
1940 /* First lock should be unlocked. */
1941 io.lockx.in.ulock_cnt = 0;
1942 io.lockx.in.lock_cnt = 1;
1943 io.lockx.in.locks = &lock1;
1944 status = smb_raw_lock(cli->tree, &io);
1945 CHECK_STATUS(status, NT_STATUS_OK);
1946
1947 /* cleanup */
1948 io.lockx.in.ulock_cnt = 1;
1949 io.lockx.in.lock_cnt = 0;
1950 io.lockx.in.locks = &lock1;
1951 status = smb_raw_lock(cli->tree, &io);
1952 CHECK_STATUS(status, NT_STATUS_OK);
1953
1954 /* Test3: Request 2 locks, second will contend. What happens to the
1955 * first? */
1956 torture_comment(tctx, " request 2 locks, second one will contend. "
1957 "Expect both to fail.\n");
1958
1959 /* Lock the second range */
1960 io.lockx.in.ulock_cnt = 0;
1961 io.lockx.in.lock_cnt = 1;
1962 io.lockx.in.locks = &lock2;
1963 status = smb_raw_lock(cli->tree, &io);
1964 CHECK_STATUS(status, NT_STATUS_OK);
1965
1966 /* Request both locks */
1967 io.lockx.in.ulock_cnt = 0;
1968 io.lockx.in.lock_cnt = 2;
1969 io.lockx.in.locks = locks;
1970
1971 status = smb_raw_lock(cli->tree, &io);
1972 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1973
1974 /* First lock should be unlocked. */
1975 io.lockx.in.ulock_cnt = 0;
1976 io.lockx.in.lock_cnt = 1;
1977 io.lockx.in.locks = &lock1;
1978 status = smb_raw_lock(cli->tree, &io);
1979 CHECK_STATUS(status, NT_STATUS_OK);
1980
1981 /* cleanup */
1982 io.lockx.in.ulock_cnt = 2;
1983 io.lockx.in.lock_cnt = 0;
1984 io.lockx.in.locks = locks;
1985 status = smb_raw_lock(cli->tree, &io);
1986 CHECK_STATUS(status, NT_STATUS_OK);
1987
1988 /* Test4: Request unlock and lock. The lock contends, is the unlock
1989 * then re-locked? */
1990 torture_comment(tctx, " request unlock and lock, second one will "
1991 "contend. Expect the unlock to succeed.\n");
1992
1993 /* Lock both ranges */
1994 io.lockx.in.ulock_cnt = 0;
1995 io.lockx.in.lock_cnt = 2;
1996 io.lockx.in.locks = locks;
1997 status = smb_raw_lock(cli->tree, &io);
1998 CHECK_STATUS(status, NT_STATUS_OK);
1999
2000 /* Attempt to unlock the first range and lock the second */
2001 io.lockx.in.ulock_cnt = 1;
2002 io.lockx.in.lock_cnt = 1;
2003 io.lockx.in.locks = locks;
2004 status = smb_raw_lock(cli->tree, &io);
2005 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2006
2007 /* The first lock should've been unlocked */
2008 io.lockx.in.ulock_cnt = 0;
2009 io.lockx.in.lock_cnt = 1;
2010 io.lockx.in.locks = &lock1;
2011 status = smb_raw_lock(cli->tree, &io);
2012 CHECK_STATUS(status, NT_STATUS_OK);
2013
2014 /* cleanup */
2015 io.lockx.in.ulock_cnt = 2;
2016 io.lockx.in.lock_cnt = 0;
2017 io.lockx.in.locks = locks;
2018 status = smb_raw_lock(cli->tree, &io);
2019 CHECK_STATUS(status, NT_STATUS_OK);
2020
2021 done:
2022 smbcli_close(cli->tree, fnum1);
2023 smb_raw_exit(cli->session);
2024 smbcli_deltree(cli->tree, BASEDIR);
2025 return ret;
2026 }
2027
2028 /**
2029 * torture_locktest5 covers stacking pretty well, but its missing two tests:
2030 * - stacking an exclusive on top of shared fails
2031 * - stacking two exclusives fail
2032 */
2033 static bool test_stacking(struct torture_context *tctx, struct smbcli_state *cli)
2034 {
2035 union smb_lock io;
2036 NTSTATUS status;
2037 bool ret = true;
2038 int fnum1;
2039 const char *fname = BASEDIR "\\stacking.txt";
2040 struct smb_lock_entry lock1;
2041
2042 torture_comment(tctx, "Testing stacking:\n");
2043
2044 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
2045
2046 io.generic.level = RAW_LOCK_LOCKX;
2047
2048 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2049 torture_assert(tctx,(fnum1 != -1), talloc_asprintf(tctx,
2050 "Failed to create %s - %s\n",
2051 fname, smbcli_errstr(cli->tree)));
2052
2053 /* Setup initial parameters */
2054 io.lockx.level = RAW_LOCK_LOCKX;
2055 io.lockx.in.timeout = 0;
2056
2057 lock1.pid = cli->session->pid;
2058 lock1.offset = 0;
2059 lock1.count = 10;
2060
2061 /**
2062 * Try to take a shared lock, then stack an exclusive.
2063 */
2064 torture_comment(tctx, " stacking an exclusive on top of a shared lock fails.\n");
2065 io.lockx.in.file.fnum = fnum1;
2066 io.lockx.in.locks = &lock1;
2067
2068 io.lockx.in.ulock_cnt = 0;
2069 io.lockx.in.lock_cnt = 1;
2070 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
2071 status = smb_raw_lock(cli->tree, &io);
2072 CHECK_STATUS(status, NT_STATUS_OK);
2073
2074 io.lockx.in.ulock_cnt = 0;
2075 io.lockx.in.lock_cnt = 1;
2076 io.lockx.in.mode = 0;
2077 status = smb_raw_lock(cli->tree, &io);
2078 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
2079 NT_STATUS_FILE_LOCK_CONFLICT);
2080
2081 /* cleanup */
2082 io.lockx.in.ulock_cnt = 1;
2083 io.lockx.in.lock_cnt = 0;
2084 status = smb_raw_lock(cli->tree, &io);
2085 CHECK_STATUS(status, NT_STATUS_OK);
2086
2087 /**
2088 * Prove that two exclusive locks do not stack.
2089 */
2090 torture_comment(tctx, " two exclusive locks do not stack.\n");
2091 io.lockx.in.ulock_cnt = 0;
2092 io.lockx.in.lock_cnt = 1;
2093 io.lockx.in.mode = 0;
2094 status = smb_raw_lock(cli->tree, &io);
2095 CHECK_STATUS(status, NT_STATUS_OK);
2096 status = smb_raw_lock(cli->tree, &io);
2097 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
2098 NT_STATUS_FILE_LOCK_CONFLICT);
2099
2100 /* cleanup */
2101 io.lockx.in.ulock_cnt = 1;
2102 io.lockx.in.lock_cnt = 0;
2103 status = smb_raw_lock(cli->tree, &io);
2104 CHECK_STATUS(status, NT_STATUS_OK);
2105
2106 done:
2107 smbcli_close(cli->tree, fnum1);
2108 smb_raw_exit(cli->session);
2109 smbcli_deltree(cli->tree, BASEDIR);
2110 return ret;
2111 }
2112
2113 /**
2114 * Test how 0-byte read requests contend with byte range locks
2115 */
2116 static bool test_zerobyteread(struct torture_context *tctx,
2117 struct smbcli_state *cli)
2118 {
2119 union smb_lock io;
2120 union smb_read rd;
2121 NTSTATUS status;
2122 bool ret = true;
2123 int fnum1, fnum2;
2124 const char *fname = BASEDIR "\\zerobyteread.txt";
2125 struct smb_lock_entry lock1;
2126 uint8_t c = 1;
2127
2128 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
2129
2130 io.generic.level = RAW_LOCK_LOCKX;
2131
2132 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2133 torture_assert(tctx,(fnum1 != -1), talloc_asprintf(tctx,
2134 "Failed to create %s - %s\n",
2135 fname, smbcli_errstr(cli->tree)));
2136
2137 fnum2 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2138 torture_assert(tctx,(fnum2 != -1), talloc_asprintf(tctx,
2139 "Failed to create %s - %s\n",
2140 fname, smbcli_errstr(cli->tree)));
2141
2142 /* Setup initial parameters */
2143 io.lockx.level = RAW_LOCK_LOCKX;
2144 io.lockx.in.timeout = 0;
2145
2146 lock1.pid = cli->session->pid;
2147 lock1.offset = 0;
2148 lock1.count = 10;
2149
2150 ZERO_STRUCT(rd);
2151 rd.readx.level = RAW_READ_READX;
2152
2153 torture_comment(tctx, "Testing zero byte read on lock range:\n");
2154
2155 /* Take an exclusive lock */
2156 torture_comment(tctx, " taking exclusive lock.\n");
2157 io.lockx.in.ulock_cnt = 0;
2158 io.lockx.in.lock_cnt = 1;
2159 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2160 io.lockx.in.file.fnum = fnum1;
2161 io.lockx.in.locks = &lock1;
2162 status = smb_raw_lock(cli->tree, &io);
2163 CHECK_STATUS(status, NT_STATUS_OK);
2164
2165 /* Try a zero byte read */
2166 torture_comment(tctx, " reading 0 bytes.\n");
2167 rd.readx.in.file.fnum = fnum2;
2168 rd.readx.in.offset = 5;
2169 rd.readx.in.mincnt = 0;
2170 rd.readx.in.maxcnt = 0;
2171 rd.readx.in.remaining = 0;
2172 rd.readx.in.read_for_execute = false;
2173 rd.readx.out.data = &c;
2174 status = smb_raw_read(cli->tree, &rd);
2175 torture_assert_int_equal_goto(tctx, rd.readx.out.nread, 0, ret, done,
2176 "zero byte read did not return 0 bytes");
2177 CHECK_STATUS(status, NT_STATUS_OK);
2178
2179 /* Unlock lock */
2180 io.lockx.in.ulock_cnt = 1;
2181 io.lockx.in.lock_cnt = 0;
2182 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2183 io.lockx.in.file.fnum = fnum1;
2184 io.lockx.in.locks = &lock1;
2185 status = smb_raw_lock(cli->tree, &io);
2186 CHECK_STATUS(status, NT_STATUS_OK);
2187
2188 torture_comment(tctx, "Testing zero byte read on zero byte lock "
2189 "range:\n");
2190
2191 /* Take an exclusive lock */
2192 torture_comment(tctx, " taking exclusive 0-byte lock.\n");
2193 io.lockx.in.ulock_cnt = 0;
2194 io.lockx.in.lock_cnt = 1;
2195 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2196 io.lockx.in.file.fnum = fnum1;
2197 io.lockx.in.locks = &lock1;
2198 lock1.offset = 5;
2199 lock1.count = 0;
2200 status = smb_raw_lock(cli->tree, &io);
2201 CHECK_STATUS(status, NT_STATUS_OK);
2202
2203 /* Try a zero byte read before the lock */
2204 torture_comment(tctx, " reading 0 bytes before the lock.\n");
2205 rd.readx.in.file.fnum = fnum2;
2206 rd.readx.in.offset = 4;
2207 rd.readx.in.mincnt = 0;
2208 rd.readx.in.maxcnt = 0;
2209 rd.readx.in.remaining = 0;
2210 rd.readx.in.read_for_execute = false;
2211 rd.readx.out.data = &c;
2212 status = smb_raw_read(cli->tree, &rd);
2213 torture_assert_int_equal_goto(tctx, rd.readx.out.nread, 0, ret, done,
2214 "zero byte read did not return 0 bytes");
2215 CHECK_STATUS(status, NT_STATUS_OK);
2216
2217 /* Try a zero byte read on the lock */
2218 torture_comment(tctx, " reading 0 bytes on the lock.\n");
2219 rd.readx.in.file.fnum = fnum2;
2220 rd.readx.in.offset = 5;
2221 rd.readx.in.mincnt = 0;
2222 rd.readx.in.maxcnt = 0;
2223 rd.readx.in.remaining = 0;
2224 rd.readx.in.read_for_execute = false;
2225 rd.readx.out.data = &c;
2226 status = smb_raw_read(cli->tree, &rd);
2227 torture_assert_int_equal_goto(tctx, rd.readx.out.nread, 0, ret, done,
2228 "zero byte read did not return 0 bytes");
2229 CHECK_STATUS(status, NT_STATUS_OK);
2230
2231 /* Try a zero byte read after the lock */
2232 torture_comment(tctx, " reading 0 bytes after the lock.\n");
2233 rd.readx.in.file.fnum = fnum2;
2234 rd.readx.in.offset = 6;
2235 rd.readx.in.mincnt = 0;
2236 rd.readx.in.maxcnt = 0;
2237 rd.readx.in.remaining = 0;
2238 rd.readx.in.read_for_execute = false;
2239 rd.readx.out.data = &c;
2240 status = smb_raw_read(cli->tree, &rd);
2241 torture_assert_int_equal_goto(tctx, rd.readx.out.nread, 0, ret, done,
2242 "zero byte read did not return 0 bytes");
2243 CHECK_STATUS(status, NT_STATUS_OK);
2244
2245 /* Unlock lock */
2246 io.lockx.in.ulock_cnt = 1;
2247 io.lockx.in.lock_cnt = 0;
2248 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2249 io.lockx.in.file.fnum = fnum1;
2250 io.lockx.in.locks = &lock1;
2251 status = smb_raw_lock(cli->tree, &io);
2252 CHECK_STATUS(status, NT_STATUS_OK);
2253
2254 done:
2255 smbcli_close(cli->tree, fnum1);
2256 smbcli_close(cli->tree, fnum2);
2257 smb_raw_exit(cli->session);
2258 smbcli_deltree(cli->tree, BASEDIR);
2259 return ret;
2260 }
2261 /*
2262 test multi Locking&X operation
2263 */
2264 static bool test_multilock(struct torture_context *tctx,
2265 struct smbcli_state *cli)
2266 {
2267 union smb_lock io;
2268 struct smb_lock_entry lock[2];
2269 NTSTATUS status;
2270 bool ret = true;
2271 int fnum;
2272 const char *fname = BASEDIR "\\multilock_test.txt";
2273 time_t t;
2274 struct smbcli_request *req;
2275 struct smbcli_session_options options;
2276
2277 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
2278
2279 lpcfg_smbcli_session_options(tctx->lp_ctx, &options);
2280
2281 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock\n");
2282 io.generic.level = RAW_LOCK_LOCKX;
2283
2284 /* Create the test file. */
2285 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2286 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
2287 "Failed to create %s - %s\n",
2288 fname, smbcli_errstr(cli->tree)));
2289
2290 /*
2291 * Lock regions 100->109, 120->129 as
2292 * two separate write locks in one request.
2293 */
2294 io.lockx.level = RAW_LOCK_LOCKX;
2295 io.lockx.in.file.fnum = fnum;
2296 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2297 io.lockx.in.timeout = 0;
2298 io.lockx.in.ulock_cnt = 0;
2299 io.lockx.in.lock_cnt = 2;
2300 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2301 lock[0].pid = cli->session->pid;
2302 lock[0].offset = 100;
2303 lock[0].count = 10;
2304 lock[1].pid = cli->session->pid;
2305 lock[1].offset = 120;
2306 lock[1].count = 10;
2307 io.lockx.in.locks = &lock[0];
2308 status = smb_raw_lock(cli->tree, &io);
2309 CHECK_STATUS(status, NT_STATUS_OK);
2310
2311 /*
2312 * Now request the same locks on a different
2313 * context as blocking locks with infinite timeout.
2314 */
2315
2316 io.lockx.in.timeout = 20000;
2317 lock[0].pid = cli->session->pid+1;
2318 lock[1].pid = cli->session->pid+1;
2319 req = smb_raw_lock_send(cli->tree, &io);
2320 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
2321 "Failed to setup timed locks (%s)\n", __location__));
2322
2323 /* Unlock lock[0] */
2324 io.lockx.in.timeout = 0;
2325 io.lockx.in.ulock_cnt = 1;
2326 io.lockx.in.lock_cnt = 0;
2327 io.lockx.in.locks = &lock[0];
2328 lock[0].pid = cli->session->pid;
2329 status = smb_raw_lock(cli->tree, &io);
2330 CHECK_STATUS(status, NT_STATUS_OK);
2331
2332 /* Start the clock. */
2333 t = time_mono(NULL);
2334
2335 /* Unlock lock[1] */
2336 io.lockx.in.timeout = 0;
2337 io.lockx.in.ulock_cnt = 1;
2338 io.lockx.in.lock_cnt = 0;
2339 io.lockx.in.locks = &lock[1];
2340 lock[1].pid = cli->session->pid;
2341 status = smb_raw_lock(cli->tree, &io);
2342 CHECK_STATUS(status, NT_STATUS_OK);
2343
2344 /* receive the successful blocked lock requests */
2345 status = smbcli_request_simple_recv(req);
2346 CHECK_STATUS(status, NT_STATUS_OK);
2347
2348 /* Fail if this took more than 2 seconds. */
2349 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
2350 "Blocking locks were not granted immediately (%s)\n",
2351 __location__));
2352 done:
2353 smb_raw_exit(cli->session);
2354 smbcli_deltree(cli->tree, BASEDIR);
2355 return ret;
2356 }
2357
2358 /*
2359 test multi2 Locking&X operation
2360 This test is designed to show that
2361 lock precedence on the server is based
2362 on the order received, not on the ability
2363 to grant. For example:
2364
2365 A blocked lock request containing 2 locks
2366 will be satisfied before a subsequent blocked
2367 lock request over one of the same regions,
2368 even if that region is then unlocked. E.g.
2369
2370 (a) lock 100->109, 120->129 (granted)
2371 (b) lock 100->109, 120-129 (blocks)
2372 (c) lock 100->109 (blocks)
2373 (d) unlock 100->109
2374
2375 lock (c) will not be granted as lock (b)
2376 will take precedence.
2377 */
2378 static bool test_multilock2(struct torture_context *tctx,
2379 struct smbcli_state *cli)
2380 {
2381 union smb_lock io;
2382 struct smb_lock_entry lock[2];
2383 NTSTATUS status;
2384 bool ret = true;
2385 int fnum;
2386 const char *fname = BASEDIR "\\multilock2_test.txt";
2387 time_t t;
2388 struct smbcli_request *req;
2389 struct smbcli_request *req2;
2390 struct smbcli_session_options options;
2391
2392 torture_assert(tctx, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
2393
2394 lpcfg_smbcli_session_options(tctx->lp_ctx, &options);
2395
2396 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock 2\n");
2397 io.generic.level = RAW_LOCK_LOCKX;
2398
2399 /* Create the test file. */
2400 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2401 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
2402 "Failed to create %s - %s\n",
2403 fname, smbcli_errstr(cli->tree)));
2404
2405 /*
2406 * Lock regions 100->109, 120->129 as
2407 * two separate write locks in one request.
2408 */
2409 io.lockx.level = RAW_LOCK_LOCKX;
2410 io.lockx.in.file.fnum = fnum;
2411 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2412 io.lockx.in.timeout = 0;
2413 io.lockx.in.ulock_cnt = 0;
2414 io.lockx.in.lock_cnt = 2;
2415 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2416 lock[0].pid = cli->session->pid;
2417 lock[0].offset = 100;
2418 lock[0].count = 10;
2419 lock[1].pid = cli->session->pid;
2420 lock[1].offset = 120;
2421 lock[1].count = 10;
2422 io.lockx.in.locks = &lock[0];
2423 status = smb_raw_lock(cli->tree, &io);
2424 CHECK_STATUS(status, NT_STATUS_OK);
2425
2426 /*
2427 * Now request the same locks on a different
2428 * context as blocking locks.
2429 */
2430
2431 io.lockx.in.timeout = 20000;
2432 lock[0].pid = cli->session->pid+1;
2433 lock[1].pid = cli->session->pid+1;
2434 req = smb_raw_lock_send(cli->tree, &io);
2435 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
2436 "Failed to setup timed locks (%s)\n", __location__));
2437
2438 /*
2439 * Request the first lock again on a separate context.
2440 * Wait 2 seconds. This should time out (the previous
2441 * multi-lock request should take precedence).
2442 */
2443
2444 io.lockx.in.timeout = 2000;
2445 lock[0].pid = cli->session->pid+2;
2446 io.lockx.in.lock_cnt = 1;
2447 req2 = smb_raw_lock_send(cli->tree, &io);
2448 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
2449 "Failed to setup timed locks (%s)\n", __location__));
2450
2451 /* Unlock lock[0] */
2452 io.lockx.in.timeout = 0;
2453 io.lockx.in.ulock_cnt = 1;
2454 io.lockx.in.lock_cnt = 0;
2455 io.lockx.in.locks = &lock[0];
2456 lock[0].pid = cli->session->pid;
2457 status = smb_raw_lock(cli->tree, &io);
2458 CHECK_STATUS(status, NT_STATUS_OK);
2459
2460 /* Did the second lock complete (should time out) ? */
2461 status = smbcli_request_simple_recv(req2);
2462 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2463
2464 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2465 "req should still wait");
2466
2467 /* Start the clock. */
2468 t = time_mono(NULL);
2469
2470 /* Unlock lock[1] */
2471 io.lockx.in.timeout = 0;
2472 io.lockx.in.ulock_cnt = 1;
2473 io.lockx.in.lock_cnt = 0;
2474 io.lockx.in.locks = &lock[1];
2475 lock[1].pid = cli->session->pid;
2476 status = smb_raw_lock(cli->tree, &io);
2477 CHECK_STATUS(status, NT_STATUS_OK);
2478
2479 /* receive the successful blocked lock requests */
2480 status = smbcli_request_simple_recv(req);
2481 CHECK_STATUS(status, NT_STATUS_OK);
2482
2483 /* Fail if this took more than 2 seconds. */
2484 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
2485 "Blocking locks were not granted immediately (%s)\n",
2486 __location__));
2487 done:
2488 smb_raw_exit(cli->session);
2489 smbcli_deltree(cli->tree, BASEDIR);
2490 return ret;
2491 }
2492
2493 /*
2494 test multi3 Locking&X operation
2495 This test is designed to show that
2496 lock precedence on the server is based
2497 on the order received, not on the ability
2498 to grant.
2499
2500 Compared to test_multilock2() (above)
2501 this test demonstrates that completely
2502 unrelated ranges work independently.
2503
2504 For example:
2505
2506 A blocked lock request containing 2 locks
2507 will be satisfied before a subsequent blocked
2508 lock request over one of the same regions,
2509 even if that region is then unlocked. But
2510 a lock of a different region goes through. E.g.
2511
2512 All locks are LOCKING_ANDX_EXCLUSIVE_LOCK (rw).
2513
2514 (a) lock 100->109, 120->129 (granted)
2515 (b) lock 100->109, 120->129 (blocks, timeout=20s)
2516 (c) lock 100->109 (blocks, timeout=2s)
2517 (d) lock 110->119 (granted)
2518 (e) lock 110->119 (blocks, timeout=20s)
2519 (f) unlock 100->109 (a)
2520 (g) lock 100->109 (not granted, blocked by (b))
2521 (h) lock 100->109 (not granted, blocked by itself (b))
2522 (i) lock (c) will not be granted(conflict, times out)
2523 as lock (b) will take precedence.
2524 (j) unlock 110-119 (d)
2525 (k) lock (e) completes and is not blocked by (a) nor (b)
2526 (l) lock 100->109 (not granted(conflict), blocked by (b))
2527 (m) lock 100->109 (not granted(conflict), blocked by itself (b))
2528 (n) unlock 120-129 (a)
2529 (o) lock (b) completes
2530 */
2531 static bool test_multilock3(struct torture_context *tctx,
2532 struct smbcli_state *cli)
2533 {
2534 union smb_lock io;
2535 struct smb_lock_entry lock[2];
2536 union smb_lock io3;
2537 struct smb_lock_entry lock3[1];
2538 NTSTATUS status;
2539 bool ret = true;
2540 int fnum;
2541 const char *fname = BASEDIR "\\multilock3_test.txt";
2542 time_t t;
2543 struct smbcli_request *req = NULL;
2544 struct smbcli_request *req2 = NULL;
2545 struct smbcli_request *req4 = NULL;
2546
2547 torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
2548 "Failed to setup up test directory: " BASEDIR);
2549
2550 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock 3\n");
2551 io.generic.level = RAW_LOCK_LOCKX;
2552
2553 /* Create the test file. */
2554 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2555 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
2556 "Failed to create %s - %s\n",
2557 fname, smbcli_errstr(cli->tree)));
2558
2559 /*
2560 * a)
2561 * Lock regions 100->109, 120->129 as
2562 * two separate write locks in one request.
2563 */
2564 io.lockx.level = RAW_LOCK_LOCKX;
2565 io.lockx.in.file.fnum = fnum;
2566 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2567 io.lockx.in.timeout = 0;
2568 io.lockx.in.ulock_cnt = 0;
2569 io.lockx.in.lock_cnt = 2;
2570 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2571 lock[0].pid = cli->session->pid;
2572 lock[0].offset = 100;
2573 lock[0].count = 10;
2574 lock[1].pid = cli->session->pid;
2575 lock[1].offset = 120;
2576 lock[1].count = 10;
2577 io.lockx.in.locks = &lock[0];
2578 status = smb_raw_lock(cli->tree, &io);
2579 CHECK_STATUS(status, NT_STATUS_OK);
2580
2581 /*
2582 * b)
2583 * Now request the same locks on a different
2584 * context as blocking locks.
2585 */
2586 io.lockx.in.timeout = 20000;
2587 lock[0].pid = cli->session->pid+1;
2588 lock[1].pid = cli->session->pid+1;
2589 req = smb_raw_lock_send(cli->tree, &io);
2590 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
2591 "Failed to setup timed locks (%s)\n", __location__));
2592
2593 /*
2594 * c)
2595 * Request the first lock again on a separate context.
2596 * Wait 2 seconds. This should time out (the previous
2597 * multi-lock request should take precedence).
2598 */
2599 io.lockx.in.timeout = 2000;
2600 lock[0].pid = cli->session->pid+2;
2601 io.lockx.in.lock_cnt = 1;
2602 req2 = smb_raw_lock_send(cli->tree, &io);
2603 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
2604 "Failed to setup timed locks (%s)\n", __location__));
2605
2606 /*
2607 * d)
2608 * Lock regions 110->119
2609 */
2610 io3.lockx.level = RAW_LOCK_LOCKX;
2611 io3.lockx.in.file.fnum = fnum;
2612 io3.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2613 io3.lockx.in.timeout = 0;
2614 io3.lockx.in.ulock_cnt = 0;
2615 io3.lockx.in.lock_cnt = 1;
2616 io3.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2617 lock3[0].pid = cli->session->pid+3;
2618 lock3[0].offset = 110;
2619 lock3[0].count = 10;
2620 io3.lockx.in.locks = &lock3[0];
2621 status = smb_raw_lock(cli->tree, &io3);
2622 CHECK_STATUS(status, NT_STATUS_OK);
2623
2624 /*
2625 * e)
2626 * try 110-119 again
2627 */
2628 io3.lockx.in.timeout = 20000;
2629 lock3[0].pid = cli->session->pid+4;
2630 req4 = smb_raw_lock_send(cli->tree, &io3);
2631 torture_assert(tctx,(req4 != NULL), talloc_asprintf(tctx,
2632 "Failed to setup timed locks (%s)\n", __location__));
2633
2634 /*
2635 * f)
2636 * Unlock (a) lock[0] 100-109
2637 */
2638 io.lockx.in.timeout = 0;
2639 io.lockx.in.ulock_cnt = 1;
2640 io.lockx.in.lock_cnt = 0;
2641 io.lockx.in.locks = &lock[0];
2642 lock[0].pid = cli->session->pid;
2643 status = smb_raw_lock(cli->tree, &io);
2644 CHECK_STATUS(status, NT_STATUS_OK);
2645
2646 /*
2647 * g)
2648 * try to lock lock[0] 100-109 again
2649 */
2650 lock[0].pid = cli->session->pid+5;
2651 io.lockx.in.ulock_cnt = 0;
2652 io.lockx.in.lock_cnt = 1;
2653 status = smb_raw_lock(cli->tree, &io);
2654 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2655
2656 /*
2657 * h)
2658 * try to lock lock[0] 100-109 again with
2659 * the pid that's still waiting
2660 */
2661 lock[0].pid = cli->session->pid+1;
2662 io.lockx.in.ulock_cnt = 0;
2663 io.lockx.in.lock_cnt = 1;
2664 status = smb_raw_lock(cli->tree, &io);
2665 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2666
2667 torture_assert(tctx, req2->state <= SMBCLI_REQUEST_RECV,
2668 "req2 should still wait");
2669
2670 /*
2671 * i)
2672 * Did the second lock complete (should time out) ?
2673 */
2674 status = smbcli_request_simple_recv(req2);
2675 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2676
2677 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2678 "req should still wait");
2679 torture_assert(tctx, req4->state <= SMBCLI_REQUEST_RECV,
2680 "req4 should still wait");
2681
2682 /*
2683 * j)
2684 * Unlock (d) lock[0] 110-119
2685 */
2686 io3.lockx.in.timeout = 0;
2687 io3.lockx.in.ulock_cnt = 1;
2688 io3.lockx.in.lock_cnt = 0;
2689 lock3[0].pid = cli->session->pid+3;
2690 status = smb_raw_lock(cli->tree, &io3);
2691 CHECK_STATUS(status, NT_STATUS_OK);
2692
2693 /*
2694 * k)
2695 * receive the successful blocked lock request (e)
2696 * on 110-119 while the 100-109/120-129 is still waiting.
2697 */
2698 status = smbcli_request_simple_recv(req4);
2699 CHECK_STATUS(status, NT_STATUS_OK);
2700
2701 /*
2702 * l)
2703 * try to lock lock[0] 100-109 again
2704 */
2705 lock[0].pid = cli->session->pid+6;
2706 io.lockx.in.ulock_cnt = 0;
2707 io.lockx.in.lock_cnt = 1;
2708 status = smb_raw_lock(cli->tree, &io);
2709 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2710
2711 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2712 "req should still wait");
2713
2714 /*
2715 * m)
2716 * try to lock lock[0] 100-109 again with
2717 * the pid that's still waiting
2718 */
2719 lock[0].pid = cli->session->pid+1;
2720 io.lockx.in.ulock_cnt = 0;
2721 io.lockx.in.lock_cnt = 1;
2722 status = smb_raw_lock(cli->tree, &io);
2723 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2724
2725 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2726 "req should still wait");
2727
2728 /* Start the clock. */
2729 t = time_mono(NULL);
2730
2731 /*
2732 * n)
2733 * Unlock lock[1] 120-129 */
2734 io.lockx.in.timeout = 0;
2735 io.lockx.in.ulock_cnt = 1;
2736 io.lockx.in.lock_cnt = 0;
2737 io.lockx.in.locks = &lock[1];
2738 lock[1].pid = cli->session->pid;
2739 status = smb_raw_lock(cli->tree, &io);
2740 CHECK_STATUS(status, NT_STATUS_OK);
2741
2742 /*
2743 * o)
2744 * receive the successful blocked lock request (b)
2745 */
2746 status = smbcli_request_simple_recv(req);
2747 CHECK_STATUS(status, NT_STATUS_OK);
2748
2749 /* Fail if this took more than 2 seconds. */
2750 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
2751 "Blocking locks were not granted immediately (%s)\n",
2752 __location__));
2753 done:
2754 smb_raw_exit(cli->session);
2755 smbcli_deltree(cli->tree, BASEDIR);
2756 return ret;
2757 }
2758
2759 /*
2760 test multi4 Locking&X operation
2761 This test is designed to show that
2762 lock precedence on the server is based
2763 on the order received, not on the ability
2764 to grant.
2765
2766 Compared to test_multilock3() (above)
2767 this test demonstrates that pending read-only/shared
2768 locks doesn't block shared locks others.
2769
2770 The outstanding requests build an implicit
2771 database that's checked before checking
2772 the already granted locks in the real database.
2773
2774 For example:
2775
2776 A blocked read-lock request containing 2 locks
2777 will be still be blocked, while one region
2778 is still write-locked. While it doesn't block
2779 other read-lock requests for the other region. E.g.
2780
2781 (a) lock(rw) 100->109, 120->129 (granted)
2782 (b) lock(ro) 100->109, 120->129 (blocks, timeout=20s)
2783 (c) lock(ro) 100->109 (blocks, timeout=MAX)
2784 (d) lock(rw) 110->119 (granted)
2785 (e) lock(rw) 110->119 (blocks, timeout=20s)
2786 (f) unlock 100->109 (a)
2787 (g) lock(ro) (c) completes and is not blocked by (a) nor (b)
2788 (h) lock(rw) 100->109 (not granted, blocked by (c))
2789 (i) lock(rw) 100->109 (pid (b)) (not granted(conflict), blocked by (c))
2790 (j) unlock 110-119
2791 (k) lock (e) completes and is not blocked by (a) nor (b)
2792 (l) lock 100->109 (not granted(conflict), blocked by (b))
2793 (m) lock 100->109 (pid (b)) (not granted(conflict), blocked by itself (b))
2794 (n) unlock 120-129 (a)
2795 (o) lock (b) completes
2796 */
2797 static bool test_multilock4(struct torture_context *tctx,
2798 struct smbcli_state *cli)
2799 {
2800 union smb_lock io;
2801 struct smb_lock_entry lock[2];
2802 union smb_lock io3;
2803 struct smb_lock_entry lock3[1];
2804 NTSTATUS status;
2805 bool ret = true;
2806 int fnum;
2807 const char *fname = BASEDIR "\\multilock4_test.txt";
2808 time_t t;
2809 struct smbcli_request *req = NULL;
2810 struct smbcli_request *req2 = NULL;
2811 struct smbcli_request *req4 = NULL;
2812
2813 torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
2814 "Failed to setup up test directory: " BASEDIR);
2815
2816 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock 4\n");
2817 io.generic.level = RAW_LOCK_LOCKX;
2818
2819 /* Create the test file. */
2820 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
2821 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
2822 "Failed to create %s - %s\n",
2823 fname, smbcli_errstr(cli->tree)));
2824
2825 /*
2826 * a)
2827 * Lock regions 100->109, 120->129 as
2828 * two separate write locks in one request.
2829 */
2830 io.lockx.level = RAW_LOCK_LOCKX;
2831 io.lockx.in.file.fnum = fnum;
2832 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2833 io.lockx.in.timeout = 0;
2834 io.lockx.in.ulock_cnt = 0;
2835 io.lockx.in.lock_cnt = 2;
2836 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2837 lock[0].pid = cli->session->pid;
2838 lock[0].offset = 100;
2839 lock[0].count = 10;
2840 lock[1].pid = cli->session->pid;
2841 lock[1].offset = 120;
2842 lock[1].count = 10;
2843 io.lockx.in.locks = &lock[0];
2844 status = smb_raw_lock(cli->tree, &io);
2845 CHECK_STATUS(status, NT_STATUS_OK);
2846
2847 /*
2848 * b)
2849 * Now request the same locks on a different
2850 * context as blocking locks. But readonly.
2851 */
2852 io.lockx.in.timeout = 20000;
2853 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
2854 lock[0].pid = cli->session->pid+1;
2855 lock[1].pid = cli->session->pid+1;
2856 req = smb_raw_lock_send(cli->tree, &io);
2857 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
2858 "Failed to setup timed locks (%s)\n", __location__));
2859
2860 /*
2861 * c)
2862 * Request the first lock again on a separate context.
2863 * Wait forever. The previous multi-lock request (b)
2864 * should take precedence. Also readonly.
2865 */
2866 io.lockx.in.timeout = UINT32_MAX;
2867 lock[0].pid = cli->session->pid+2;
2868 io.lockx.in.lock_cnt = 1;
2869 req2 = smb_raw_lock_send(cli->tree, &io);
2870 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
2871 "Failed to setup timed locks (%s)\n", __location__));
2872
2873 /*
2874 * d)
2875 * Lock regions 110->119
2876 */
2877 io3.lockx.level = RAW_LOCK_LOCKX;
2878 io3.lockx.in.file.fnum = fnum;
2879 io3.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
2880 io3.lockx.in.timeout = 0;
2881 io3.lockx.in.ulock_cnt = 0;
2882 io3.lockx.in.lock_cnt = 1;
2883 io3.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2884 lock3[0].pid = cli->session->pid+3;
2885 lock3[0].offset = 110;
2886 lock3[0].count = 10;
2887 io3.lockx.in.locks = &lock3[0];
2888 status = smb_raw_lock(cli->tree, &io3);
2889 CHECK_STATUS(status, NT_STATUS_OK);
2890
2891 /*
2892 * e)
2893 * try 110-119 again
2894 */
2895 io3.lockx.in.timeout = 20000;
2896 lock3[0].pid = cli->session->pid+4;
2897 req4 = smb_raw_lock_send(cli->tree, &io3);
2898 torture_assert(tctx,(req4 != NULL), talloc_asprintf(tctx,
2899 "Failed to setup timed locks (%s)\n", __location__));
2900
2901 /*
2902 * f)
2903 * Unlock (a) lock[0] 100-109
2904 */
2905 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
2906 io.lockx.in.timeout = 0;
2907 io.lockx.in.ulock_cnt = 1;
2908 io.lockx.in.lock_cnt = 0;
2909 io.lockx.in.locks = &lock[0];
2910 lock[0].pid = cli->session->pid;
2911 status = smb_raw_lock(cli->tree, &io);
2912 CHECK_STATUS(status, NT_STATUS_OK);
2913
2914 /*
2915 * g)
2916 * receive the successful blocked lock request (c)
2917 * on 110-119 while (b) 100-109/120-129 is still waiting.
2918 */
2919 status = smbcli_request_simple_recv(req2);
2920 CHECK_STATUS(status, NT_STATUS_OK);
2921
2922 /*
2923 * h)
2924 * try to lock lock[0] 100-109 again
2925 * (read/write)
2926 */
2927 lock[0].pid = cli->session->pid+5;
2928 io.lockx.in.ulock_cnt = 0;
2929 io.lockx.in.lock_cnt = 1;
2930 status = smb_raw_lock(cli->tree, &io);
2931 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2932
2933 /*
2934 * i)
2935 * try to lock lock[0] 100-109 again with the pid (b)
2936 * that's still waiting.
2937 */
2938 lock[0].pid = cli->session->pid+1;
2939 io.lockx.in.ulock_cnt = 0;
2940 io.lockx.in.lock_cnt = 1;
2941 status = smb_raw_lock(cli->tree, &io);
2942 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2943
2944 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2945 "req should still wait");
2946 torture_assert(tctx, req4->state <= SMBCLI_REQUEST_RECV,
2947 "req4 should still wait");
2948
2949 /*
2950 * j)
2951 * Unlock (d) lock[0] 110-119
2952 */
2953 io3.lockx.in.timeout = 0;
2954 io3.lockx.in.ulock_cnt = 1;
2955 io3.lockx.in.lock_cnt = 0;
2956 lock3[0].pid = cli->session->pid+3;
2957 status = smb_raw_lock(cli->tree, &io3);
2958 CHECK_STATUS(status, NT_STATUS_OK);
2959
2960 /*
2961 * k)
2962 * receive the successful blocked
2963 * lock request (e) on 110-119.
2964 */
2965 status = smbcli_request_simple_recv(req4);
2966 CHECK_STATUS(status, NT_STATUS_OK);
2967
2968 /*
2969 * l)
2970 * try to lock lock[0] 100-109 again
2971 */
2972 lock[0].pid = cli->session->pid+6;
2973 io.lockx.in.ulock_cnt = 0;
2974 io.lockx.in.lock_cnt = 1;
2975 status = smb_raw_lock(cli->tree, &io);
2976 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2977
2978 /*
2979 * m)
2980 * try to lock lock[0] 100-109 again with the pid (b)
2981 * that's still waiting
2982 */
2983 lock[0].pid = cli->session->pid+1;
2984 io.lockx.in.ulock_cnt = 0;
2985 io.lockx.in.lock_cnt = 1;
2986 status = smb_raw_lock(cli->tree, &io);
2987 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
2988
2989 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
2990 "req should still wait");
2991
2992 /* Start the clock. */
2993 t = time_mono(NULL);
2994
2995 /*
2996 * n)
2997 * Unlock (a) lock[1] 120-129
2998 */
2999 io.lockx.in.timeout = 0;
3000 io.lockx.in.ulock_cnt = 1;
3001 io.lockx.in.lock_cnt = 0;
3002 io.lockx.in.locks = &lock[1];
3003 lock[1].pid = cli->session->pid;
3004 status = smb_raw_lock(cli->tree, &io);
3005 CHECK_STATUS(status, NT_STATUS_OK);
3006
3007 /*
3008 * o)
3009 * receive the successful blocked lock request (b)
3010 */
3011 status = smbcli_request_simple_recv(req);
3012 CHECK_STATUS(status, NT_STATUS_OK);
3013
3014 /* Fail if this took more than 2 seconds. */
3015 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
3016 "Blocking locks were not granted immediately (%s)\n",
3017 __location__));
3018 done:
3019 smb_raw_exit(cli->session);
3020 smbcli_deltree(cli->tree, BASEDIR);
3021 return ret;
3022 }
3023
3024 /*
3025 test multi5 Locking&X operation
3026 This test is designed to show that
3027 lock precedence on the server is based
3028 on the order received, not on the ability
3029 to grant.
3030
3031 Compared to test_multilock3() (above)
3032 this test demonstrates that the initial
3033 lock request that block the following
3034 exclusive locks can be a shared lock.
3035
3036 For example:
3037
3038 All locks except (a) are LOCKING_ANDX_EXCLUSIVE_LOCK (rw).
3039
3040 (a) lock(ro) 100->109, 120->129 (granted)
3041 (b) lock 100->109, 120->129 (blocks, timeout=20s)
3042 (c) lock 100->109 (blocks, timeout=2s)
3043 (d) lock 110->119 (granted)
3044 (e) lock 110->119 (blocks, timeout=20s)
3045 (f) unlock 100->109 (a)
3046 (g) lock 100->109 (not granted, blocked by (b))
3047 (h) lock 100->109 (not granted, blocked by itself (b))
3048 (i) lock (c) will not be granted(conflict, times out)
3049 as lock (b) will take precedence.
3050 (j) unlock 110-119 (d)
3051 (k) lock (e) completes and is not blocked by (a) nor (b)
3052 (l) lock 100->109 (not granted(conflict), blocked by (b))
3053 (m) lock 100->109 (not granted(conflict), blocked by itself (b))
3054 (n) unlock 120-129 (a)
3055 (o) lock (b) completes
3056 */
3057 static bool test_multilock5(struct torture_context *tctx,
3058 struct smbcli_state *cli)
3059 {
3060 union smb_lock io;
3061 struct smb_lock_entry lock[2];
3062 union smb_lock io3;
3063 struct smb_lock_entry lock3[1];
3064 NTSTATUS status;
3065 bool ret = true;
3066 int fnum;
3067 const char *fname = BASEDIR "\\multilock5_test.txt";
3068 time_t t;
3069 struct smbcli_request *req = NULL;
3070 struct smbcli_request *req2 = NULL;
3071 struct smbcli_request *req4 = NULL;
3072
3073 torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
3074 "Failed to setup up test directory: " BASEDIR);
3075
3076 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock 5\n");
3077 io.generic.level = RAW_LOCK_LOCKX;
3078
3079 /* Create the test file. */
3080 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
3081 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
3082 "Failed to create %s - %s\n",
3083 fname, smbcli_errstr(cli->tree)));
3084
3085 /*
3086 * a)
3087 * Lock regions 100->109, 120->129 as
3088 * two separate write locks in one request.
3089 * (read only)
3090 */
3091 io.lockx.level = RAW_LOCK_LOCKX;
3092 io.lockx.in.file.fnum = fnum;
3093 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
3094 io.lockx.in.timeout = 0;
3095 io.lockx.in.ulock_cnt = 0;
3096 io.lockx.in.lock_cnt = 2;
3097 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
3098 lock[0].pid = cli->session->pid;
3099 lock[0].offset = 100;
3100 lock[0].count = 10;
3101 lock[1].pid = cli->session->pid;
3102 lock[1].offset = 120;
3103 lock[1].count = 10;
3104 io.lockx.in.locks = &lock[0];
3105 status = smb_raw_lock(cli->tree, &io);
3106 CHECK_STATUS(status, NT_STATUS_OK);
3107
3108 /*
3109 * b)
3110 * Now request the same locks on a different
3111 * context as blocking locks.
3112 * (read write)
3113 */
3114 io.lockx.in.timeout = 20000;
3115 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
3116 lock[0].pid = cli->session->pid+1;
3117 lock[1].pid = cli->session->pid+1;
3118 req = smb_raw_lock_send(cli->tree, &io);
3119 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
3120 "Failed to setup timed locks (%s)\n", __location__));
3121
3122 /*
3123 * c)
3124 * Request the first lock again on a separate context.
3125 * Wait 2 seconds. This should time out (the previous
3126 * multi-lock request should take precedence).
3127 * (read write)
3128 */
3129 io.lockx.in.timeout = 2000;
3130 lock[0].pid = cli->session->pid+2;
3131 io.lockx.in.lock_cnt = 1;
3132 req2 = smb_raw_lock_send(cli->tree, &io);
3133 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
3134 "Failed to setup timed locks (%s)\n", __location__));
3135
3136 /*
3137 * d)
3138 * Lock regions 110->119
3139 */
3140 io3.lockx.level = RAW_LOCK_LOCKX;
3141 io3.lockx.in.file.fnum = fnum;
3142 io3.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
3143 io3.lockx.in.timeout = 0;
3144 io3.lockx.in.ulock_cnt = 0;
3145 io3.lockx.in.lock_cnt = 1;
3146 io3.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
3147 lock3[0].pid = cli->session->pid+3;
3148 lock3[0].offset = 110;
3149 lock3[0].count = 10;
3150 io3.lockx.in.locks = &lock3[0];
3151 status = smb_raw_lock(cli->tree, &io3);
3152 CHECK_STATUS(status, NT_STATUS_OK);
3153
3154 /*
3155 * e)
3156 * try 110-119 again
3157 */
3158 io3.lockx.in.timeout = 20000;
3159 lock3[0].pid = cli->session->pid+4;
3160 req4 = smb_raw_lock_send(cli->tree, &io3);
3161 torture_assert(tctx,(req4 != NULL), talloc_asprintf(tctx,
3162 "Failed to setup timed locks (%s)\n", __location__));
3163
3164 /*
3165 * f)
3166 * Unlock (a) lock[0] 100-109
3167 *
3168 * Note we send LOCKING_ANDX_EXCLUSIVE_LOCK
3169 * while the lock used LOCKING_ANDX_SHARED_LOCK
3170 * to check if that also works.
3171 */
3172 io.lockx.in.timeout = 0;
3173 io.lockx.in.ulock_cnt = 1;
3174 io.lockx.in.lock_cnt = 0;
3175 io.lockx.in.locks = &lock[0];
3176 lock[0].pid = cli->session->pid;
3177 status = smb_raw_lock(cli->tree, &io);
3178 CHECK_STATUS(status, NT_STATUS_OK);
3179
3180 /*
3181 * g)
3182 * try to lock lock[0] 100-109 again
3183 */
3184 lock[0].pid = cli->session->pid+5;
3185 io.lockx.in.ulock_cnt = 0;
3186 io.lockx.in.lock_cnt = 1;
3187 status = smb_raw_lock(cli->tree, &io);
3188 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
3189
3190 /*
3191 * h)
3192 * try to lock lock[0] 100-109 again with the pid (b)
3193 * that's still waiting.
3194 * (read write)
3195 */
3196 lock[0].pid = cli->session->pid+1;
3197 io.lockx.in.ulock_cnt = 0;
3198 io.lockx.in.lock_cnt = 1;
3199 status = smb_raw_lock(cli->tree, &io);
3200 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3201
3202 torture_assert(tctx, req2->state <= SMBCLI_REQUEST_RECV,
3203 "req2 should still wait");
3204
3205 /*
3206 * i)
3207 * Did the second lock complete (should time out) ?
3208 */
3209 status = smbcli_request_simple_recv(req2);
3210 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3211
3212 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
3213 "req should still wait");
3214 torture_assert(tctx, req4->state <= SMBCLI_REQUEST_RECV,
3215 "req4 should still wait");
3216
3217 /*
3218 * j)
3219 * Unlock (d) lock[0] 110-119
3220 */
3221 io3.lockx.in.timeout = 0;
3222 io3.lockx.in.ulock_cnt = 1;
3223 io3.lockx.in.lock_cnt = 0;
3224 lock3[0].pid = cli->session->pid+3;
3225 status = smb_raw_lock(cli->tree, &io3);
3226 CHECK_STATUS(status, NT_STATUS_OK);
3227
3228 /*
3229 * k)
3230 * receive the successful blocked lock requests
3231 * on 110-119 while the 100-109/120-129 is still waiting.
3232 */
3233 status = smbcli_request_simple_recv(req4);
3234 CHECK_STATUS(status, NT_STATUS_OK);
3235
3236 /*
3237 * l)
3238 * try to lock lock[0] 100-109 again
3239 */
3240 lock[0].pid = cli->session->pid+6;
3241 io.lockx.in.ulock_cnt = 0;
3242 io.lockx.in.lock_cnt = 1;
3243 status = smb_raw_lock(cli->tree, &io);
3244 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3245
3246 /*
3247 * m)
3248 * try to lock lock[0] 100-109 again with the pid (b)
3249 * that's still waiting
3250 */
3251 lock[0].pid = cli->session->pid+1;
3252 io.lockx.in.ulock_cnt = 0;
3253 io.lockx.in.lock_cnt = 1;
3254 status = smb_raw_lock(cli->tree, &io);
3255 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3256
3257 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
3258 "req should still wait");
3259
3260 /* Start the clock. */
3261 t = time_mono(NULL);
3262
3263 /*
3264 * n)
3265 * Unlock (a) lock[1] 120-129
3266 */
3267 io.lockx.in.timeout = 0;
3268 io.lockx.in.ulock_cnt = 1;
3269 io.lockx.in.lock_cnt = 0;
3270 io.lockx.in.locks = &lock[1];
3271 lock[1].pid = cli->session->pid;
3272 status = smb_raw_lock(cli->tree, &io);
3273 CHECK_STATUS(status, NT_STATUS_OK);
3274
3275 /*
3276 * o)
3277 * receive the successful blocked lock request (b)
3278 */
3279 status = smbcli_request_simple_recv(req);
3280 CHECK_STATUS(status, NT_STATUS_OK);
3281
3282 /* Fail if this took more than 2 seconds. */
3283 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
3284 "Blocking locks were not granted immediately (%s)\n",
3285 __location__));
3286 done:
3287 smb_raw_exit(cli->session);
3288 smbcli_deltree(cli->tree, BASEDIR);
3289 return ret;
3290 }
3291
3292 /*
3293 test multi6 Locking&X operation
3294 This test is designed to show that
3295 lock precedence on the server is based
3296 on the order received, not on the ability
3297 to grant.
3298
3299 Compared to test_multilock4() (above)
3300 this test demonstrates the behavior if
3301 only just the first blocking lock
3302 being a shared lock.
3303
3304 For example:
3305
3306 All locks except (b) are LOCKING_ANDX_EXCLUSIVE_LOCK (rw).
3307
3308 (a) lock 100->109, 120->129 (granted)
3309 (b) lock(ro) 100->109, 120->129 (blocks, timeout=20s)
3310 (c) lock 100->109 (blocks, timeout=2s)
3311 (d) lock 110->119 (granted)
3312 (e) lock 110->119 (blocks, timeout=20s)
3313 (f) unlock 100->109 (a)
3314 (g) lock 100->109 (not granted, blocked by (b))
3315 (h) lock 100->109 (not granted, blocked by itself (b))
3316 (i) lock (c) will not be granted(conflict, times out)
3317 as lock (b) will take precedence.
3318 (j) unlock 110-119 (d)
3319 (k) lock (e) completes and is not blocked by (a) nor (b)
3320 (l) lock 100->109 (not granted(conflict), blocked by (b))
3321 (m) lock 100->109 (not granted(conflict), blocked by itself (b))
3322 (n) unlock 120-129 (a)
3323 (o) lock (b) completes
3324 */
3325 static bool test_multilock6(struct torture_context *tctx,
3326 struct smbcli_state *cli)
3327 {
3328 union smb_lock io;
3329 struct smb_lock_entry lock[2];
3330 union smb_lock io3;
3331 struct smb_lock_entry lock3[1];
3332 NTSTATUS status;
3333 bool ret = true;
3334 int fnum;
3335 const char *fname = BASEDIR "\\multilock6_test.txt";
3336 time_t t;
3337 struct smbcli_request *req = NULL;
3338 struct smbcli_request *req2 = NULL;
3339 struct smbcli_request *req4 = NULL;
3340
3341 torture_assert(tctx, torture_setup_dir(cli, BASEDIR),
3342 "Failed to setup up test directory: " BASEDIR);
3343
3344 torture_comment(tctx, "Testing LOCKING_ANDX multi-lock 6\n");
3345 io.generic.level = RAW_LOCK_LOCKX;
3346
3347 /* Create the test file. */
3348 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
3349 torture_assert(tctx,(fnum != -1), talloc_asprintf(tctx,
3350 "Failed to create %s - %s\n",
3351 fname, smbcli_errstr(cli->tree)));
3352
3353 /*
3354 * a)
3355 * Lock regions 100->109, 120->129 as
3356 * two separate write locks in one request.
3357 */
3358 io.lockx.level = RAW_LOCK_LOCKX;
3359 io.lockx.in.file.fnum = fnum;
3360 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
3361 io.lockx.in.timeout = 0;
3362 io.lockx.in.ulock_cnt = 0;
3363 io.lockx.in.lock_cnt = 2;
3364 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
3365 lock[0].pid = cli->session->pid;
3366 lock[0].offset = 100;
3367 lock[0].count = 10;
3368 lock[1].pid = cli->session->pid;
3369 lock[1].offset = 120;
3370 lock[1].count = 10;
3371 io.lockx.in.locks = &lock[0];
3372 status = smb_raw_lock(cli->tree, &io);
3373 CHECK_STATUS(status, NT_STATUS_OK);
3374
3375 /*
3376 * b)
3377 * Now request the same locks on a different
3378 * context as blocking locks.
3379 * (read only)
3380 */
3381 io.lockx.in.timeout = 20000;
3382 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
3383 lock[0].pid = cli->session->pid+1;
3384 lock[1].pid = cli->session->pid+1;
3385 req = smb_raw_lock_send(cli->tree, &io);
3386 torture_assert(tctx,(req != NULL), talloc_asprintf(tctx,
3387 "Failed to setup timed locks (%s)\n", __location__));
3388
3389 /*
3390 * c)
3391 * Request the first lock again on a separate context.
3392 * Wait 2 seconds. This should time out (the previous
3393 * multi-lock request should take precedence).
3394 */
3395 io.lockx.in.timeout = 2000;
3396 io.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
3397 lock[0].pid = cli->session->pid+2;
3398 io.lockx.in.lock_cnt = 1;
3399 req2 = smb_raw_lock_send(cli->tree, &io);
3400 torture_assert(tctx,(req2 != NULL), talloc_asprintf(tctx,
3401 "Failed to setup timed locks (%s)\n", __location__));
3402
3403 /*
3404 * d)
3405 * Lock regions 110->119
3406 */
3407 io3.lockx.level = RAW_LOCK_LOCKX;
3408 io3.lockx.in.file.fnum = fnum;
3409 io3.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
3410 io3.lockx.in.timeout = 0;
3411 io3.lockx.in.ulock_cnt = 0;
3412 io3.lockx.in.lock_cnt = 1;
3413 io3.lockx.in.mode = LOCKING_ANDX_EXCLUSIVE_LOCK;
3414 lock3[0].pid = cli->session->pid+3;
3415 lock3[0].offset = 110;
3416 lock3[0].count = 10;
3417 io3.lockx.in.locks = &lock3[0];
3418 status = smb_raw_lock(cli->tree, &io3);
3419 CHECK_STATUS(status, NT_STATUS_OK);
3420
3421 /*
3422 * e)
3423 * try 110-119 again
3424 */
3425 io3.lockx.in.timeout = 20000;
3426 lock3[0].pid = cli->session->pid+4;
3427 req4 = smb_raw_lock_send(cli->tree, &io3);
3428 torture_assert(tctx,(req4 != NULL), talloc_asprintf(tctx,
3429 "Failed to setup timed locks (%s)\n", __location__));
3430
3431 /*
3432 * f)
3433 * Unlock (a) lock[0] 100-109
3434 */
3435 io.lockx.in.timeout = 0;
3436 io.lockx.in.ulock_cnt = 1;
3437 io.lockx.in.lock_cnt = 0;
3438 io.lockx.in.locks = &lock[0];
3439 lock[0].pid = cli->session->pid;
3440 status = smb_raw_lock(cli->tree, &io);
3441 CHECK_STATUS(status, NT_STATUS_OK);
3442
3443 /*
3444 * g)
3445 * try to lock lock[0] 100-109 again
3446 */
3447 lock[0].pid = cli->session->pid+5;
3448 io.lockx.in.ulock_cnt = 0;
3449 io.lockx.in.lock_cnt = 1;
3450 status = smb_raw_lock(cli->tree, &io);
3451 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
3452
3453 /*
3454 * h)
3455 * try to lock lock[0] 100-109 again with the pid (b)
3456 * that's still waiting
3457 */
3458 lock[0].pid = cli->session->pid+1;
3459 io.lockx.in.ulock_cnt = 0;
3460 io.lockx.in.lock_cnt = 1;
3461 status = smb_raw_lock(cli->tree, &io);
3462 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3463
3464 torture_assert(tctx, req2->state <= SMBCLI_REQUEST_RECV,
3465 "req2 should still wait");
3466
3467 /*
3468 * i)
3469 * Did the second lock (c) complete (should time out) ?
3470 */
3471 status = smbcli_request_simple_recv(req2);
3472 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3473
3474 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
3475 "req should still wait");
3476 torture_assert(tctx, req4->state <= SMBCLI_REQUEST_RECV,
3477 "req4 should still wait");
3478
3479 /*
3480 * j)
3481 * Unlock (d) lock[0] 110-119
3482 */
3483 io3.lockx.in.timeout = 0;
3484 io3.lockx.in.ulock_cnt = 1;
3485 io3.lockx.in.lock_cnt = 0;
3486 lock3[0].pid = cli->session->pid+3;
3487 status = smb_raw_lock(cli->tree, &io3);
3488 CHECK_STATUS(status, NT_STATUS_OK);
3489
3490 /*
3491 * k)
3492 * receive the successful blocked lock request (e)
3493 * on 110-119 while (b) 100-109/120-129 is still waiting.
3494 */
3495 status = smbcli_request_simple_recv(req4);
3496 CHECK_STATUS(status, NT_STATUS_OK);
3497
3498 /*
3499 * l)
3500 * try to lock lock[0] 100-109 again
3501 */
3502 lock[0].pid = cli->session->pid+6;
3503 io.lockx.in.ulock_cnt = 0;
3504 io.lockx.in.lock_cnt = 1;
3505 status = smb_raw_lock(cli->tree, &io);
3506 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3507
3508 /*
3509 * m)
3510 * try to lock lock[0] 100-109 again with the pid (b)
3511 * that's still waiting
3512 */
3513 lock[0].pid = cli->session->pid+1;
3514 io.lockx.in.ulock_cnt = 0;
3515 io.lockx.in.lock_cnt = 1;
3516 status = smb_raw_lock(cli->tree, &io);
3517 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
3518
3519 torture_assert(tctx, req->state <= SMBCLI_REQUEST_RECV,
3520 "req should still wait");
3521
3522 /* Start the clock. */
3523 t = time_mono(NULL);
3524
3525 /*
3526 * n)
3527 * Unlock (a) lock[1] 120-129
3528 */
3529 io.lockx.in.timeout = 0;
3530 io.lockx.in.ulock_cnt = 1;
3531 io.lockx.in.lock_cnt = 0;
3532 io.lockx.in.locks = &lock[1];
3533 lock[1].pid = cli->session->pid;
3534 status = smb_raw_lock(cli->tree, &io);
3535 CHECK_STATUS(status, NT_STATUS_OK);
3536
3537 /*
3538 * o)
3539 * receive the successful blocked lock request (b)
3540 */
3541 status = smbcli_request_simple_recv(req);
3542 CHECK_STATUS(status, NT_STATUS_OK);
3543
3544 /* Fail if this took more than 2 seconds. */
3545 torture_assert(tctx,!(time_mono(NULL) > t+2), talloc_asprintf(tctx,
3546 "Blocking locks were not granted immediately (%s)\n",
3547 __location__));
3548 done:
3549 smb_raw_exit(cli->session);
3550 smbcli_deltree(cli->tree, BASEDIR);
3551 return ret;
3552 }
3553
3554 /*
3555 basic testing of lock calls
3556 */
3557 struct torture_suite *torture_raw_lock(TALLOC_CTX *mem_ctx)
3558 {
3559 struct torture_suite *suite = torture_suite_create(mem_ctx, "lock");
3560
3561 torture_suite_add_1smb_test(suite, "lockx", test_lockx);
3562 torture_suite_add_1smb_test(suite, "lock", test_lock);
3563 torture_suite_add_1smb_test(suite, "pidhigh", test_pidhigh);
3564 torture_suite_add_1smb_test(suite, "async", test_async);
3565 torture_suite_add_1smb_test(suite, "errorcode", test_errorcode);
3566 torture_suite_add_1smb_test(suite, "changetype", test_changetype);
3567
3568 torture_suite_add_1smb_test(suite, "stacking", test_stacking);
3569 torture_suite_add_1smb_test(suite, "unlock", test_unlock);
3570 torture_suite_add_1smb_test(suite, "multiple_unlock",
3571 test_multiple_unlock);
3572 torture_suite_add_1smb_test(suite, "zerobytelocks", test_zerobytelocks);
3573 torture_suite_add_1smb_test(suite, "zerobyteread", test_zerobyteread);
3574 torture_suite_add_1smb_test(suite, "multilock", test_multilock);
3575 torture_suite_add_1smb_test(suite, "multilock2", test_multilock2);
3576 torture_suite_add_1smb_test(suite, "multilock3", test_multilock3);
3577 torture_suite_add_1smb_test(suite, "multilock4", test_multilock4);
3578 torture_suite_add_1smb_test(suite, "multilock5", test_multilock5);
3579 torture_suite_add_1smb_test(suite, "multilock6", test_multilock6);
3580
3581 return suite;
3582 }