2 Unix SMB/CIFS implementation.
3 test suite for various lock operations
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "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/popt_common.h"
31 #include "param/param.h"
33 #define CHECK_STATUS(status, correct) do { \
34 if (!NT_STATUS_EQUAL(status, correct)) { \
35 printf("(%s) Incorrect status %s - should be %s\n", \
36 __location__, nt_errstr(status), nt_errstr(correct)); \
41 #define CHECK_STATUS_CONT(status, correct) do { \
42 if (!NT_STATUS_EQUAL(status, correct)) { \
43 printf("(%s) Incorrect status %s - should be %s\n", \
44 __location__, nt_errstr(status), nt_errstr(correct)); \
48 #define CHECK_STATUS_OR(status, correct1, correct2) do { \
49 if ((!NT_STATUS_EQUAL(status, correct1)) && \
50 (!NT_STATUS_EQUAL(status, correct2))) { \
51 printf("(%s) Incorrect status %s - should be %s or %s\n", \
52 __location__, nt_errstr(status), nt_errstr(correct1), \
53 nt_errstr(correct2)); \
58 #define CHECK_STATUS_OR_CONT(status, correct1, correct2) do { \
59 if ((!NT_STATUS_EQUAL(status, correct1)) && \
60 (!NT_STATUS_EQUAL(status, correct2))) { \
61 printf("(%s) Incorrect status %s - should be %s or %s\n", \
62 __location__, nt_errstr(status), nt_errstr(correct1), \
63 nt_errstr(correct2)); \
66 #define BASEDIR "\\testlock"
68 #define TARGET_IS_WIN7(_tctx) (torture_setting_bool(_tctx, "win7", false))
71 test SMBlock and SMBunlock ops
73 static bool test_lock(struct torture_context *tctx, struct smbcli_state *cli)
79 const char *fname = BASEDIR "\\test.txt";
81 if (!torture_setup_dir(cli, BASEDIR)) {
85 printf("Testing RAW_LOCK_LOCK\n");
86 io.generic.level = RAW_LOCK_LOCK;
88 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
90 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
95 printf("Trying 0/0 lock\n");
96 io.lock.level = RAW_LOCK_LOCK;
97 io.lock.in.file.fnum = fnum;
99 io.lock.in.offset = 0;
100 status = smb_raw_lock(cli->tree, &io);
101 CHECK_STATUS(status, NT_STATUS_OK);
103 status = smb_raw_lock(cli->tree, &io);
104 CHECK_STATUS(status, NT_STATUS_OK);
106 io.lock.level = RAW_LOCK_UNLOCK;
107 status = smb_raw_lock(cli->tree, &io);
108 CHECK_STATUS(status, NT_STATUS_OK);
110 printf("Trying 0/1 lock\n");
111 io.lock.level = RAW_LOCK_LOCK;
112 io.lock.in.file.fnum = fnum;
113 io.lock.in.count = 1;
114 io.lock.in.offset = 0;
115 status = smb_raw_lock(cli->tree, &io);
116 CHECK_STATUS(status, NT_STATUS_OK);
118 status = smb_raw_lock(cli->tree, &io);
119 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
121 io.lock.level = RAW_LOCK_UNLOCK;
122 status = smb_raw_lock(cli->tree, &io);
123 CHECK_STATUS(status, NT_STATUS_OK);
124 io.lock.level = RAW_LOCK_UNLOCK;
125 status = smb_raw_lock(cli->tree, &io);
126 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
128 printf("Trying 0xEEFFFFFF lock\n");
129 io.lock.level = RAW_LOCK_LOCK;
130 io.lock.in.file.fnum = fnum;
131 io.lock.in.count = 4000;
132 io.lock.in.offset = 0xEEFFFFFF;
133 status = smb_raw_lock(cli->tree, &io);
134 CHECK_STATUS(status, NT_STATUS_OK);
136 status = smb_raw_lock(cli->tree, &io);
137 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
139 io.lock.level = RAW_LOCK_UNLOCK;
140 status = smb_raw_lock(cli->tree, &io);
141 CHECK_STATUS(status, NT_STATUS_OK);
142 io.lock.level = RAW_LOCK_UNLOCK;
143 status = smb_raw_lock(cli->tree, &io);
144 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
146 printf("Trying 0xEF000000 lock\n");
147 io.lock.level = RAW_LOCK_LOCK;
148 io.lock.in.file.fnum = fnum;
149 io.lock.in.count = 4000;
150 io.lock.in.offset = 0xEEFFFFFF;
151 status = smb_raw_lock(cli->tree, &io);
152 CHECK_STATUS(status, NT_STATUS_OK);
154 status = smb_raw_lock(cli->tree, &io);
155 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
157 io.lock.level = RAW_LOCK_UNLOCK;
158 status = smb_raw_lock(cli->tree, &io);
159 CHECK_STATUS(status, NT_STATUS_OK);
160 io.lock.level = RAW_LOCK_UNLOCK;
161 status = smb_raw_lock(cli->tree, &io);
162 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
164 printf("Trying max lock\n");
165 io.lock.level = RAW_LOCK_LOCK;
166 io.lock.in.file.fnum = fnum;
167 io.lock.in.count = 4000;
168 io.lock.in.offset = 0xEF000000;
169 status = smb_raw_lock(cli->tree, &io);
170 CHECK_STATUS(status, NT_STATUS_OK);
172 status = smb_raw_lock(cli->tree, &io);
173 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
175 io.lock.level = RAW_LOCK_UNLOCK;
176 status = smb_raw_lock(cli->tree, &io);
177 CHECK_STATUS(status, NT_STATUS_OK);
178 io.lock.level = RAW_LOCK_UNLOCK;
179 status = smb_raw_lock(cli->tree, &io);
180 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
182 printf("Trying wrong pid unlock\n");
183 io.lock.level = RAW_LOCK_LOCK;
184 io.lock.in.file.fnum = fnum;
185 io.lock.in.count = 4002;
186 io.lock.in.offset = 10001;
187 status = smb_raw_lock(cli->tree, &io);
188 CHECK_STATUS(status, NT_STATUS_OK);
190 io.lock.level = RAW_LOCK_UNLOCK;
191 status = smb_raw_lock(cli->tree, &io);
192 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
194 status = smb_raw_lock(cli->tree, &io);
195 CHECK_STATUS(status, NT_STATUS_OK);
198 smbcli_close(cli->tree, fnum);
199 smb_raw_exit(cli->session);
200 smbcli_deltree(cli->tree, BASEDIR);
208 static bool test_lockx(struct torture_context *tctx, struct smbcli_state *cli)
211 struct smb_lock_entry lock[1];
215 const char *fname = BASEDIR "\\test.txt";
217 if (!torture_setup_dir(cli, BASEDIR)) {
221 printf("Testing RAW_LOCK_LOCKX\n");
222 io.generic.level = RAW_LOCK_LOCKX;
224 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
226 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
231 io.lockx.level = RAW_LOCK_LOCKX;
232 io.lockx.in.file.fnum = fnum;
233 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
234 io.lockx.in.timeout = 0;
235 io.lockx.in.ulock_cnt = 0;
236 io.lockx.in.lock_cnt = 1;
237 lock[0].pid = cli->session->pid;
240 io.lockx.in.locks = &lock[0];
241 status = smb_raw_lock(cli->tree, &io);
242 CHECK_STATUS(status, NT_STATUS_OK);
245 printf("Trying 0xEEFFFFFF lock\n");
246 io.lockx.in.ulock_cnt = 0;
247 io.lockx.in.lock_cnt = 1;
248 lock[0].count = 4000;
249 lock[0].offset = 0xEEFFFFFF;
250 status = smb_raw_lock(cli->tree, &io);
251 CHECK_STATUS(status, NT_STATUS_OK);
253 status = smb_raw_lock(cli->tree, &io);
254 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
256 io.lockx.in.ulock_cnt = 1;
257 io.lockx.in.lock_cnt = 0;
258 status = smb_raw_lock(cli->tree, &io);
259 CHECK_STATUS(status, NT_STATUS_OK);
260 status = smb_raw_lock(cli->tree, &io);
261 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
263 printf("Trying 0xEF000000 lock\n");
264 io.lockx.in.ulock_cnt = 0;
265 io.lockx.in.lock_cnt = 1;
266 lock[0].count = 4000;
267 lock[0].offset = 0xEF000000;
268 status = smb_raw_lock(cli->tree, &io);
269 CHECK_STATUS(status, NT_STATUS_OK);
271 status = smb_raw_lock(cli->tree, &io);
272 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
274 io.lockx.in.ulock_cnt = 1;
275 io.lockx.in.lock_cnt = 0;
276 status = smb_raw_lock(cli->tree, &io);
277 CHECK_STATUS(status, NT_STATUS_OK);
278 status = smb_raw_lock(cli->tree, &io);
279 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
281 printf("Trying zero lock\n");
282 io.lockx.in.ulock_cnt = 0;
283 io.lockx.in.lock_cnt = 1;
286 status = smb_raw_lock(cli->tree, &io);
287 CHECK_STATUS(status, NT_STATUS_OK);
289 status = smb_raw_lock(cli->tree, &io);
290 CHECK_STATUS(status, NT_STATUS_OK);
292 io.lockx.in.ulock_cnt = 1;
293 io.lockx.in.lock_cnt = 0;
294 status = smb_raw_lock(cli->tree, &io);
295 CHECK_STATUS(status, NT_STATUS_OK);
296 status = smb_raw_lock(cli->tree, &io);
297 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
299 printf("Trying max lock\n");
300 io.lockx.in.ulock_cnt = 0;
301 io.lockx.in.lock_cnt = 1;
304 status = smb_raw_lock(cli->tree, &io);
305 CHECK_STATUS(status, NT_STATUS_OK);
307 status = smb_raw_lock(cli->tree, &io);
308 CHECK_STATUS(status, NT_STATUS_OK);
310 io.lockx.in.ulock_cnt = 1;
311 io.lockx.in.lock_cnt = 0;
312 status = smb_raw_lock(cli->tree, &io);
313 CHECK_STATUS(status, NT_STATUS_OK);
314 status = smb_raw_lock(cli->tree, &io);
315 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
317 printf("Trying 2^63\n");
318 io.lockx.in.ulock_cnt = 0;
319 io.lockx.in.lock_cnt = 1;
322 lock[0].offset <<= 63;
323 status = smb_raw_lock(cli->tree, &io);
324 CHECK_STATUS(status, NT_STATUS_OK);
326 status = smb_raw_lock(cli->tree, &io);
327 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
329 io.lockx.in.ulock_cnt = 1;
330 io.lockx.in.lock_cnt = 0;
331 status = smb_raw_lock(cli->tree, &io);
332 CHECK_STATUS(status, NT_STATUS_OK);
333 status = smb_raw_lock(cli->tree, &io);
334 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
336 printf("Trying 2^63 - 1\n");
337 io.lockx.in.ulock_cnt = 0;
338 io.lockx.in.lock_cnt = 1;
341 lock[0].offset <<= 63;
343 status = smb_raw_lock(cli->tree, &io);
344 CHECK_STATUS(status, NT_STATUS_OK);
346 status = smb_raw_lock(cli->tree, &io);
347 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
349 io.lockx.in.ulock_cnt = 1;
350 io.lockx.in.lock_cnt = 0;
351 status = smb_raw_lock(cli->tree, &io);
352 CHECK_STATUS(status, NT_STATUS_OK);
353 status = smb_raw_lock(cli->tree, &io);
354 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
356 printf("Trying max lock 2\n");
357 io.lockx.in.ulock_cnt = 0;
358 io.lockx.in.lock_cnt = 1;
361 status = smb_raw_lock(cli->tree, &io);
362 CHECK_STATUS(status, NT_STATUS_OK);
365 status = smb_raw_lock(cli->tree, &io);
366 if (TARGET_IS_WIN7(tctx))
367 CHECK_STATUS(status, NT_STATUS_WIN7_INVALID_RANGE);
369 CHECK_STATUS(status, NT_STATUS_OK);
371 io.lockx.in.ulock_cnt = 1;
372 io.lockx.in.lock_cnt = 0;
374 status = smb_raw_lock(cli->tree, &io);
376 /* XXX This is very strange - Win7 gives us an invalid range when we
377 * unlock the range even though the range is locked! Win7 bug? */
378 if (TARGET_IS_WIN7(tctx))
379 CHECK_STATUS(status, NT_STATUS_WIN7_INVALID_RANGE);
381 CHECK_STATUS(status, NT_STATUS_OK);
382 status = smb_raw_lock(cli->tree, &io);
383 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
387 smbcli_close(cli->tree, fnum);
388 smb_raw_exit(cli->session);
389 smbcli_deltree(cli->tree, BASEDIR);
396 static bool test_pidhigh(struct torture_context *tctx,
397 struct smbcli_state *cli)
400 struct smb_lock_entry lock[1];
404 const char *fname = BASEDIR "\\test.txt";
407 if (!torture_setup_dir(cli, BASEDIR)) {
411 printf("Testing high pid\n");
412 io.generic.level = RAW_LOCK_LOCKX;
414 cli->session->pid = 1;
416 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
418 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
423 if (smbcli_write(cli->tree, fnum, 0, &c, 0, 1) != 1) {
424 printf("Failed to write 1 byte - %s\n", smbcli_errstr(cli->tree));
429 io.lockx.level = RAW_LOCK_LOCKX;
430 io.lockx.in.file.fnum = fnum;
431 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
432 io.lockx.in.timeout = 0;
433 io.lockx.in.ulock_cnt = 0;
434 io.lockx.in.lock_cnt = 1;
435 lock[0].pid = cli->session->pid;
437 lock[0].count = 0xFFFFFFFF;
438 io.lockx.in.locks = &lock[0];
439 status = smb_raw_lock(cli->tree, &io);
440 CHECK_STATUS(status, NT_STATUS_OK);
442 if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
443 printf("Failed to read 1 byte - %s\n", smbcli_errstr(cli->tree));
448 cli->session->pid = 2;
450 if (smbcli_read(cli->tree, fnum, &c, 0, 1) == 1) {
451 printf("pid is incorrect handled for read with lock!\n");
456 cli->session->pid = 0x10001;
458 if (smbcli_read(cli->tree, fnum, &c, 0, 1) != 1) {
459 printf("High pid is used on this server!\n");
462 printf("High pid is not used on this server (correct)\n");
466 smbcli_close(cli->tree, fnum);
467 smb_raw_exit(cli->session);
468 smbcli_deltree(cli->tree, BASEDIR);
474 test locking&X async operation
476 static bool test_async(struct torture_context *tctx,
477 struct smbcli_state *cli)
479 struct smbcli_session *session;
480 struct smb_composite_sesssetup setup;
481 struct smbcli_tree *tree;
483 const char *host, *share;
485 struct smb_lock_entry lock[2];
489 const char *fname = BASEDIR "\\test.txt";
491 struct smbcli_request *req;
492 struct smbcli_session_options options;
494 if (!torture_setup_dir(cli, BASEDIR)) {
498 lp_smbcli_session_options(tctx->lp_ctx, &options);
500 printf("Testing LOCKING_ANDX_CANCEL_LOCK\n");
501 io.generic.level = RAW_LOCK_LOCKX;
503 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
505 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
510 io.lockx.level = RAW_LOCK_LOCKX;
511 io.lockx.in.file.fnum = fnum;
512 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
513 io.lockx.in.timeout = 0;
514 io.lockx.in.ulock_cnt = 0;
515 io.lockx.in.lock_cnt = 1;
516 lock[0].pid = cli->session->pid;
517 lock[0].offset = 100;
519 io.lockx.in.locks = &lock[0];
520 status = smb_raw_lock(cli->tree, &io);
521 CHECK_STATUS(status, NT_STATUS_OK);
525 printf("testing cancel by CANCEL_LOCK\n");
527 /* setup a timed lock */
528 io.lockx.in.timeout = 10000;
529 req = smb_raw_lock_send(cli->tree, &io);
531 printf("Failed to setup timed lock (%s)\n", __location__);
536 /* cancel the wrong range */
538 io.lockx.in.timeout = 0;
539 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
540 status = smb_raw_lock(cli->tree, &io);
541 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
543 /* cancel with the wrong bits set */
544 lock[0].offset = 100;
545 io.lockx.in.timeout = 0;
546 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK;
547 status = smb_raw_lock(cli->tree, &io);
548 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
550 /* cancel the right range */
551 lock[0].offset = 100;
552 io.lockx.in.timeout = 0;
553 io.lockx.in.mode = LOCKING_ANDX_CANCEL_LOCK | LOCKING_ANDX_LARGE_FILES;
554 status = smb_raw_lock(cli->tree, &io);
555 CHECK_STATUS(status, NT_STATUS_OK);
557 /* receive the failed lock request */
558 status = smbcli_request_simple_recv(req);
559 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
561 if (time(NULL) > t+2) {
562 printf("lock cancel was not immediate (%s)\n", __location__);
567 printf("testing cancel by unlock\n");
568 io.lockx.in.ulock_cnt = 0;
569 io.lockx.in.lock_cnt = 1;
570 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
571 io.lockx.in.timeout = 0;
572 status = smb_raw_lock(cli->tree, &io);
573 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
575 io.lockx.in.timeout = 5000;
576 req = smb_raw_lock_send(cli->tree, &io);
578 printf("Failed to setup timed lock (%s)\n", __location__);
583 io.lockx.in.ulock_cnt = 1;
584 io.lockx.in.lock_cnt = 0;
585 status = smb_raw_lock(cli->tree, &io);
586 CHECK_STATUS(status, NT_STATUS_OK);
589 status = smbcli_request_simple_recv(req);
590 CHECK_STATUS(status, NT_STATUS_OK);
592 if (time(NULL) > t+2) {
593 printf("lock cancel by unlock was not immediate (%s) - took %d secs\n",
594 __location__, (int)(time(NULL)-t));
599 printf("testing cancel by close\n");
600 io.lockx.in.ulock_cnt = 0;
601 io.lockx.in.lock_cnt = 1;
602 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
603 io.lockx.in.timeout = 0;
604 status = smb_raw_lock(cli->tree, &io);
605 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
608 io.lockx.in.timeout = 10000;
609 req = smb_raw_lock_send(cli->tree, &io);
611 printf("Failed to setup timed lock (%s)\n", __location__);
616 status = smbcli_close(cli->tree, fnum);
617 CHECK_STATUS(status, NT_STATUS_OK);
619 status = smbcli_request_simple_recv(req);
620 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
622 if (time(NULL) > t+2) {
623 printf("lock cancel by close was not immediate (%s)\n", __location__);
628 printf("create a new sessions\n");
629 session = smbcli_session_init(cli->transport, tctx, false, options);
630 setup.in.sesskey = cli->transport->negotiate.sesskey;
631 setup.in.capabilities = cli->transport->negotiate.capabilities;
632 setup.in.workgroup = lp_workgroup(tctx->lp_ctx);
633 setup.in.credentials = cmdline_credentials;
634 setup.in.gensec_settings = lp_gensec_settings(tctx, tctx->lp_ctx);
635 status = smb_composite_sesssetup(session, &setup);
636 CHECK_STATUS(status, NT_STATUS_OK);
637 session->vuid = setup.out.vuid;
639 printf("create new tree context\n");
640 share = torture_setting_string(tctx, "share", NULL);
641 host = torture_setting_string(tctx, "host", NULL);
642 tree = smbcli_tree_init(session, tctx, false);
643 tcon.generic.level = RAW_TCON_TCONX;
644 tcon.tconx.in.flags = 0;
645 tcon.tconx.in.password = data_blob(NULL, 0);
646 tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
647 tcon.tconx.in.device = "A:";
648 status = smb_raw_tcon(tree, tctx, &tcon);
649 CHECK_STATUS(status, NT_STATUS_OK);
650 tree->tid = tcon.tconx.out.tid;
652 printf("testing cancel by exit\n");
653 fname = BASEDIR "\\test_exit.txt";
654 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
656 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(tree));
660 io.lockx.level = RAW_LOCK_LOCKX;
661 io.lockx.in.file.fnum = fnum;
662 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
663 io.lockx.in.timeout = 0;
664 io.lockx.in.ulock_cnt = 0;
665 io.lockx.in.lock_cnt = 1;
666 lock[0].pid = session->pid;
667 lock[0].offset = 100;
669 io.lockx.in.locks = &lock[0];
670 status = smb_raw_lock(tree, &io);
671 CHECK_STATUS(status, NT_STATUS_OK);
673 io.lockx.in.ulock_cnt = 0;
674 io.lockx.in.lock_cnt = 1;
675 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
676 io.lockx.in.timeout = 0;
677 status = smb_raw_lock(tree, &io);
678 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
680 io.lockx.in.timeout = 10000;
682 req = smb_raw_lock_send(tree, &io);
684 printf("Failed to setup timed lock (%s)\n", __location__);
689 status = smb_raw_exit(session);
690 CHECK_STATUS(status, NT_STATUS_OK);
692 status = smbcli_request_simple_recv(req);
693 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
695 if (time(NULL) > t+2) {
696 printf("lock cancel by exit was not immediate (%s)\n", __location__);
701 printf("testing cancel by ulogoff\n");
702 fname = BASEDIR "\\test_ulogoff.txt";
703 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
705 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(tree));
709 io.lockx.level = RAW_LOCK_LOCKX;
710 io.lockx.in.file.fnum = fnum;
711 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
712 io.lockx.in.timeout = 0;
713 io.lockx.in.ulock_cnt = 0;
714 io.lockx.in.lock_cnt = 1;
715 lock[0].pid = session->pid;
716 lock[0].offset = 100;
718 io.lockx.in.locks = &lock[0];
719 status = smb_raw_lock(tree, &io);
720 CHECK_STATUS(status, NT_STATUS_OK);
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.timeout = 0;
726 status = smb_raw_lock(tree, &io);
727 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
729 io.lockx.in.timeout = 10000;
731 req = smb_raw_lock_send(tree, &io);
733 printf("Failed to setup timed lock (%s)\n", __location__);
738 status = smb_raw_ulogoff(session);
739 CHECK_STATUS(status, NT_STATUS_OK);
741 status = smbcli_request_simple_recv(req);
742 if (NT_STATUS_EQUAL(NT_STATUS_FILE_LOCK_CONFLICT, status)) {
743 printf("lock not canceled by ulogoff - %s (ignored because of vfs_vifs fails it)\n",
745 smb_tree_disconnect(tree);
746 smb_raw_exit(session);
749 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
751 if (time(NULL) > t+2) {
752 printf("lock cancel by ulogoff was not immediate (%s)\n", __location__);
757 printf("testing cancel by tdis\n");
758 tree->session = cli->session;
760 fname = BASEDIR "\\test_tdis.txt";
761 fnum = smbcli_open(tree, fname, O_RDWR|O_CREAT, DENY_NONE);
763 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(tree));
767 io.lockx.level = RAW_LOCK_LOCKX;
768 io.lockx.in.file.fnum = fnum;
769 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
770 io.lockx.in.timeout = 0;
771 io.lockx.in.ulock_cnt = 0;
772 io.lockx.in.lock_cnt = 1;
773 lock[0].pid = cli->session->pid;
774 lock[0].offset = 100;
776 io.lockx.in.locks = &lock[0];
777 status = smb_raw_lock(tree, &io);
778 CHECK_STATUS(status, NT_STATUS_OK);
780 status = smb_raw_lock(tree, &io);
781 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
783 io.lockx.in.timeout = 10000;
785 req = smb_raw_lock_send(tree, &io);
787 printf("Failed to setup timed lock (%s)\n", __location__);
792 status = smb_tree_disconnect(tree);
793 CHECK_STATUS(status, NT_STATUS_OK);
795 status = smbcli_request_simple_recv(req);
796 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
798 if (time(NULL) > t+2) {
799 printf("lock cancel by tdis was not immediate (%s)\n", __location__);
805 smb_raw_exit(cli->session);
806 smbcli_deltree(cli->tree, BASEDIR);
811 test NT_STATUS_LOCK_NOT_GRANTED vs. NT_STATUS_FILE_LOCK_CONFLICT
813 static bool test_errorcode(struct torture_context *tctx,
814 struct smbcli_state *cli)
818 struct smb_lock_entry lock[2];
823 struct smbcli_request *req;
827 if (!torture_setup_dir(cli, BASEDIR)) {
831 printf("Testing LOCK_NOT_GRANTED vs. FILE_LOCK_CONFLICT\n");
833 printf("testing with timeout = 0\n");
834 fname = BASEDIR "\\test0.txt";
838 * the first run is with t = 0,
839 * the second with t > 0 (=1)
843 * use the DENY_DOS mode, that creates two fnum's of one low-level file handle,
844 * this demonstrates that the cache is per fnum
846 op.openx.level = RAW_OPEN_OPENX;
847 op.openx.in.fname = fname;
848 op.openx.in.flags = OPENX_FLAGS_ADDITIONAL_INFO;
849 op.openx.in.open_mode = OPENX_MODE_ACCESS_RDWR | OPENX_MODE_DENY_DOS;
850 op.openx.in.open_func = OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE;
851 op.openx.in.search_attrs = 0;
852 op.openx.in.file_attrs = 0;
853 op.openx.in.write_time = 0;
854 op.openx.in.size = 0;
855 op.openx.in.timeout = 0;
857 status = smb_raw_open(cli->tree, tctx, &op);
858 CHECK_STATUS(status, NT_STATUS_OK);
859 fnum = op.openx.out.file.fnum;
861 status = smb_raw_open(cli->tree, tctx, &op);
862 CHECK_STATUS(status, NT_STATUS_OK);
863 fnum2 = op.openx.out.file.fnum;
865 io.lockx.level = RAW_LOCK_LOCKX;
866 io.lockx.in.file.fnum = fnum;
867 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
868 io.lockx.in.timeout = t;
869 io.lockx.in.ulock_cnt = 0;
870 io.lockx.in.lock_cnt = 1;
871 lock[0].pid = cli->session->pid;
872 lock[0].offset = 100;
874 io.lockx.in.locks = &lock[0];
875 status = smb_raw_lock(cli->tree, &io);
876 CHECK_STATUS(status, NT_STATUS_OK);
879 * demonstrate that the first conflicting lock on each handle give LOCK_NOT_GRANTED
880 * this also demonstrates that the error code cache is per file handle
881 * (LOCK_NOT_GRANTED is only be used when timeout is 0!)
883 io.lockx.in.file.fnum = fnum2;
884 status = smb_raw_lock(cli->tree, &io);
885 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
887 io.lockx.in.file.fnum = fnum;
888 status = smb_raw_lock(cli->tree, &io);
889 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
891 /* demonstrate that each following conflict gives FILE_LOCK_CONFLICT */
892 io.lockx.in.file.fnum = fnum;
893 status = smb_raw_lock(cli->tree, &io);
894 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
896 io.lockx.in.file.fnum = fnum2;
897 status = smb_raw_lock(cli->tree, &io);
898 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
900 io.lockx.in.file.fnum = fnum;
901 status = smb_raw_lock(cli->tree, &io);
902 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
904 io.lockx.in.file.fnum = fnum2;
905 status = smb_raw_lock(cli->tree, &io);
906 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
908 /* demonstrate that the smbpid doesn't matter */
910 io.lockx.in.file.fnum = fnum;
911 status = smb_raw_lock(cli->tree, &io);
912 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
914 io.lockx.in.file.fnum = fnum2;
915 status = smb_raw_lock(cli->tree, &io);
916 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
920 * demonstrate the a successful lock with count = 0 and the same offset,
921 * doesn't reset the error cache
923 lock[0].offset = 100;
925 io.lockx.in.file.fnum = fnum;
926 status = smb_raw_lock(cli->tree, &io);
927 CHECK_STATUS(status, NT_STATUS_OK);
929 io.lockx.in.file.fnum = fnum2;
930 status = smb_raw_lock(cli->tree, &io);
931 CHECK_STATUS(status, NT_STATUS_OK);
933 lock[0].offset = 100;
935 io.lockx.in.file.fnum = fnum;
936 status = smb_raw_lock(cli->tree, &io);
937 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
939 io.lockx.in.file.fnum = fnum2;
940 status = smb_raw_lock(cli->tree, &io);
941 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
944 * demonstrate the a successful lock with count = 0 and outside the locked range,
945 * doesn't reset the error cache
947 lock[0].offset = 110;
949 io.lockx.in.file.fnum = fnum;
950 status = smb_raw_lock(cli->tree, &io);
951 CHECK_STATUS(status, NT_STATUS_OK);
953 io.lockx.in.file.fnum = fnum2;
954 status = smb_raw_lock(cli->tree, &io);
955 CHECK_STATUS(status, NT_STATUS_OK);
957 lock[0].offset = 100;
959 io.lockx.in.file.fnum = fnum;
960 status = smb_raw_lock(cli->tree, &io);
961 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
963 io.lockx.in.file.fnum = fnum2;
964 status = smb_raw_lock(cli->tree, &io);
965 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
969 io.lockx.in.file.fnum = fnum;
970 status = smb_raw_lock(cli->tree, &io);
971 CHECK_STATUS(status, NT_STATUS_OK);
973 io.lockx.in.file.fnum = fnum2;
974 status = smb_raw_lock(cli->tree, &io);
975 CHECK_STATUS(status, NT_STATUS_OK);
977 lock[0].offset = 100;
979 io.lockx.in.file.fnum = fnum;
980 status = smb_raw_lock(cli->tree, &io);
981 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
983 io.lockx.in.file.fnum = fnum2;
984 status = smb_raw_lock(cli->tree, &io);
985 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
987 /* demonstrate that a changing count doesn't reset the error cache */
988 lock[0].offset = 100;
990 io.lockx.in.file.fnum = fnum;
991 status = smb_raw_lock(cli->tree, &io);
992 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
994 io.lockx.in.file.fnum = fnum2;
995 status = smb_raw_lock(cli->tree, &io);
996 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
998 lock[0].offset = 100;
1000 io.lockx.in.file.fnum = fnum;
1001 status = smb_raw_lock(cli->tree, &io);
1002 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1004 io.lockx.in.file.fnum = fnum2;
1005 status = smb_raw_lock(cli->tree, &io);
1006 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1009 * demonstrate the a lock with count = 0 and inside the locked range,
1010 * fails and resets the error cache
1012 lock[0].offset = 101;
1014 io.lockx.in.file.fnum = fnum;
1015 status = smb_raw_lock(cli->tree, &io);
1016 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1017 status = smb_raw_lock(cli->tree, &io);
1018 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1020 io.lockx.in.file.fnum = fnum2;
1021 status = smb_raw_lock(cli->tree, &io);
1022 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1023 status = smb_raw_lock(cli->tree, &io);
1024 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1026 lock[0].offset = 100;
1028 io.lockx.in.file.fnum = fnum;
1029 status = smb_raw_lock(cli->tree, &io);
1030 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1031 status = smb_raw_lock(cli->tree, &io);
1032 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1034 io.lockx.in.file.fnum = fnum2;
1035 status = smb_raw_lock(cli->tree, &io);
1036 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1037 status = smb_raw_lock(cli->tree, &io);
1038 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1040 /* demonstrate the a changing offset, resets the error cache */
1041 lock[0].offset = 105;
1043 io.lockx.in.file.fnum = fnum;
1044 status = smb_raw_lock(cli->tree, &io);
1045 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1046 status = smb_raw_lock(cli->tree, &io);
1047 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1049 io.lockx.in.file.fnum = fnum2;
1050 status = smb_raw_lock(cli->tree, &io);
1051 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1052 status = smb_raw_lock(cli->tree, &io);
1053 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1055 lock[0].offset = 100;
1057 io.lockx.in.file.fnum = fnum;
1058 status = smb_raw_lock(cli->tree, &io);
1059 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1060 status = smb_raw_lock(cli->tree, &io);
1061 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1063 io.lockx.in.file.fnum = fnum2;
1064 status = smb_raw_lock(cli->tree, &io);
1065 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1066 status = smb_raw_lock(cli->tree, &io);
1067 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1069 lock[0].offset = 95;
1071 io.lockx.in.file.fnum = fnum;
1072 status = smb_raw_lock(cli->tree, &io);
1073 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1074 status = smb_raw_lock(cli->tree, &io);
1075 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1077 io.lockx.in.file.fnum = fnum2;
1078 status = smb_raw_lock(cli->tree, &io);
1079 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1080 status = smb_raw_lock(cli->tree, &io);
1081 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1083 lock[0].offset = 100;
1085 io.lockx.in.file.fnum = fnum;
1086 status = smb_raw_lock(cli->tree, &io);
1087 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1088 status = smb_raw_lock(cli->tree, &io);
1089 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1091 io.lockx.in.file.fnum = fnum2;
1092 status = smb_raw_lock(cli->tree, &io);
1093 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1094 status = smb_raw_lock(cli->tree, &io);
1095 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1098 * demonstrate the a successful lock in a different range,
1099 * doesn't reset the cache, the failing lock on the 2nd handle
1100 * resets the resets the cache
1102 lock[0].offset = 120;
1104 io.lockx.in.file.fnum = fnum;
1105 status = smb_raw_lock(cli->tree, &io);
1106 CHECK_STATUS(status, NT_STATUS_OK);
1108 io.lockx.in.file.fnum = fnum2;
1109 status = smb_raw_lock(cli->tree, &io);
1110 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1112 lock[0].offset = 100;
1114 io.lockx.in.file.fnum = fnum;
1115 status = smb_raw_lock(cli->tree, &io);
1116 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1117 status = smb_raw_lock(cli->tree, &io);
1118 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1120 io.lockx.in.file.fnum = fnum2;
1121 status = smb_raw_lock(cli->tree, &io);
1122 CHECK_STATUS(status, (t?NT_STATUS_FILE_LOCK_CONFLICT:NT_STATUS_LOCK_NOT_GRANTED));
1123 status = smb_raw_lock(cli->tree, &io);
1124 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1126 /* end of the loop */
1128 smb_raw_exit(cli->session);
1129 printf("testing with timeout > 0 (=1)\n");
1130 fname = BASEDIR "\\test1.txt";
1136 * the following 3 test sections demonstrate that
1137 * the cache is only set when the error is reported
1138 * to the client (after the timeout went by)
1140 smb_raw_exit(cli->session);
1141 printf("testing a conflict while a lock is pending\n");
1142 fname = BASEDIR "\\test2.txt";
1143 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1145 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(cli->tree));
1149 io.lockx.level = RAW_LOCK_LOCKX;
1150 io.lockx.in.file.fnum = fnum;
1151 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1152 io.lockx.in.timeout = 0;
1153 io.lockx.in.ulock_cnt = 0;
1154 io.lockx.in.lock_cnt = 1;
1155 lock[0].pid = cli->session->pid;
1156 lock[0].offset = 100;
1158 io.lockx.in.locks = &lock[0];
1159 status = smb_raw_lock(cli->tree, &io);
1160 CHECK_STATUS(status, NT_STATUS_OK);
1163 io.lockx.in.timeout = 1000;
1164 req = smb_raw_lock_send(cli->tree, &io);
1166 printf("Failed to setup timed lock (%s)\n", __location__);
1171 io.lockx.in.timeout = 0;
1172 lock[0].offset = 105;
1174 status = smb_raw_lock(cli->tree, &io);
1175 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1177 status = smbcli_request_simple_recv(req);
1178 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1180 status = smb_raw_lock(cli->tree, &io);
1181 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1183 if (time(NULL) < start+1) {
1184 printf("lock comes back to early (%s)\n", __location__);
1189 smbcli_close(cli->tree, fnum);
1190 fname = BASEDIR "\\test3.txt";
1191 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1193 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(cli->tree));
1197 io.lockx.level = RAW_LOCK_LOCKX;
1198 io.lockx.in.file.fnum = fnum;
1199 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1200 io.lockx.in.timeout = 0;
1201 io.lockx.in.ulock_cnt = 0;
1202 io.lockx.in.lock_cnt = 1;
1203 lock[0].pid = cli->session->pid;
1204 lock[0].offset = 100;
1206 io.lockx.in.locks = &lock[0];
1207 status = smb_raw_lock(cli->tree, &io);
1208 CHECK_STATUS(status, NT_STATUS_OK);
1211 io.lockx.in.timeout = 1000;
1212 req = smb_raw_lock_send(cli->tree, &io);
1214 printf("Failed to setup timed lock (%s)\n", __location__);
1219 io.lockx.in.timeout = 0;
1220 lock[0].offset = 105;
1222 status = smb_raw_lock(cli->tree, &io);
1223 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1225 status = smbcli_request_simple_recv(req);
1226 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1228 lock[0].offset = 100;
1230 status = smb_raw_lock(cli->tree, &io);
1231 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1233 if (time(NULL) < start+1) {
1234 printf("lock comes back to early (%s)\n", __location__);
1239 smbcli_close(cli->tree, fnum);
1240 fname = BASEDIR "\\test4.txt";
1241 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1243 printf("Failed to reopen %s - %s\n", fname, smbcli_errstr(cli->tree));
1247 io.lockx.level = RAW_LOCK_LOCKX;
1248 io.lockx.in.file.fnum = fnum;
1249 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
1250 io.lockx.in.timeout = 0;
1251 io.lockx.in.ulock_cnt = 0;
1252 io.lockx.in.lock_cnt = 1;
1253 lock[0].pid = cli->session->pid;
1254 lock[0].offset = 100;
1256 io.lockx.in.locks = &lock[0];
1257 status = smb_raw_lock(cli->tree, &io);
1258 CHECK_STATUS(status, NT_STATUS_OK);
1261 io.lockx.in.timeout = 1000;
1262 req = smb_raw_lock_send(cli->tree, &io);
1264 printf("Failed to setup timed lock (%s)\n", __location__);
1269 io.lockx.in.timeout = 0;
1270 status = smb_raw_lock(cli->tree, &io);
1271 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1273 status = smbcli_request_simple_recv(req);
1274 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1276 status = smb_raw_lock(cli->tree, &io);
1277 CHECK_STATUS(status, NT_STATUS_FILE_LOCK_CONFLICT);
1279 if (time(NULL) < start+1) {
1280 printf("lock comes back to early (%s)\n", __location__);
1286 smb_raw_exit(cli->session);
1287 smbcli_deltree(cli->tree, BASEDIR);
1293 test LOCKING_ANDX_CHANGE_LOCKTYPE
1295 static bool test_changetype(struct torture_context *tctx,
1296 struct smbcli_state *cli)
1299 struct smb_lock_entry lock[2];
1304 const char *fname = BASEDIR "\\test.txt";
1306 if (!torture_setup_dir(cli, BASEDIR)) {
1310 printf("Testing LOCKING_ANDX_CHANGE_LOCKTYPE\n");
1311 io.generic.level = RAW_LOCK_LOCKX;
1313 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1315 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1320 io.lockx.level = RAW_LOCK_LOCKX;
1321 io.lockx.in.file.fnum = fnum;
1322 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1323 io.lockx.in.timeout = 0;
1324 io.lockx.in.ulock_cnt = 0;
1325 io.lockx.in.lock_cnt = 1;
1326 lock[0].pid = cli->session->pid;
1327 lock[0].offset = 100;
1329 io.lockx.in.locks = &lock[0];
1330 status = smb_raw_lock(cli->tree, &io);
1331 CHECK_STATUS(status, NT_STATUS_OK);
1333 if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
1334 printf("allowed write on read locked region (%s)\n", __location__);
1339 /* windows server don't seem to support this */
1340 io.lockx.in.mode = LOCKING_ANDX_CHANGE_LOCKTYPE;
1341 status = smb_raw_lock(cli->tree, &io);
1342 CHECK_STATUS(status, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
1344 if (smbcli_write(cli->tree, fnum, 0, &c, 100, 1) == 1) {
1345 printf("allowed write after lock change (%s)\n", __location__);
1351 smbcli_close(cli->tree, fnum);
1352 smb_raw_exit(cli->session);
1353 smbcli_deltree(cli->tree, BASEDIR);
1357 struct double_lock_test {
1358 struct smb_lock_entry lock1;
1359 struct smb_lock_entry lock2;
1360 NTSTATUS exp_status;
1364 * Tests zero byte locks.
1366 struct double_lock_test zero_byte_tests[] = {
1367 /* {pid, offset, count}, {pid, offset, count}, status */
1369 /** First, takes a zero byte lock at offset 10. Then:
1370 * - Taking 0 byte lock at 10 should succeed.
1371 * - Taking 1 byte locks at 9,10,11 should succeed.
1372 * - Taking 2 byte lock at 9 should fail.
1373 * - Taking 2 byte lock at 10 should succeed.
1374 * - Taking 3 byte lock at 9 should fail.
1376 {{1000, 10, 0}, {1001, 10, 0}, NT_STATUS_OK},
1377 {{1000, 10, 0}, {1001, 9, 1}, NT_STATUS_OK},
1378 {{1000, 10, 0}, {1001, 10, 1}, NT_STATUS_OK},
1379 {{1000, 10, 0}, {1001, 11, 1}, NT_STATUS_OK},
1380 {{1000, 10, 0}, {1001, 9, 2}, NT_STATUS_LOCK_NOT_GRANTED},
1381 {{1000, 10, 0}, {1001, 10, 2}, NT_STATUS_OK},
1382 {{1000, 10, 0}, {1001, 9, 3}, NT_STATUS_LOCK_NOT_GRANTED},
1384 /** Same, but opposite order. */
1385 {{1001, 10, 0}, {1000, 10, 0}, NT_STATUS_OK},
1386 {{1001, 9, 1}, {1000, 10, 0}, NT_STATUS_OK},
1387 {{1001, 10, 1}, {1000, 10, 0}, NT_STATUS_OK},
1388 {{1001, 11, 1}, {1000, 10, 0}, NT_STATUS_OK},
1389 {{1001, 9, 2}, {1000, 10, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1390 {{1001, 10, 2}, {1000, 10, 0}, NT_STATUS_OK},
1391 {{1001, 9, 3}, {1000, 10, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1393 /** Zero zero case. */
1394 {{1000, 0, 0}, {1001, 0, 0}, NT_STATUS_OK},
1397 static bool test_zerobytelocks(struct torture_context *tctx, struct smbcli_state *cli)
1400 struct smb_lock_entry zerozero;
1404 const char *fname = BASEDIR "\\zero.txt";
1406 printf("Testing zero length byte range locks:\n");
1408 if (!torture_setup_dir(cli, BASEDIR)) {
1412 io.generic.level = RAW_LOCK_LOCKX;
1414 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1416 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1421 /* Setup initial parameters */
1422 io.lockx.level = RAW_LOCK_LOCKX;
1423 io.lockx.in.file.fnum = fnum;
1424 io.lockx.in.mode = LOCKING_ANDX_LARGE_FILES; /* Exclusive */
1425 io.lockx.in.timeout = 0;
1427 /* Try every combination of locks in zero_byte_tests. The first lock is
1428 * assumed to succeed. The second lock may contend, depending on the
1429 * expected status. */
1431 i < sizeof(zero_byte_tests) / sizeof(struct double_lock_test);
1433 printf(" ... {%d, %llu, %llu} + {%d, %llu, %llu} = %s\n",
1434 zero_byte_tests[i].lock1.pid,
1435 zero_byte_tests[i].lock1.offset,
1436 zero_byte_tests[i].lock1.count,
1437 zero_byte_tests[i].lock2.pid,
1438 zero_byte_tests[i].lock2.offset,
1439 zero_byte_tests[i].lock2.count,
1440 nt_errstr(zero_byte_tests[i].exp_status));
1442 /* Lock both locks. */
1443 io.lockx.in.ulock_cnt = 0;
1444 io.lockx.in.lock_cnt = 1;
1446 io.lockx.in.locks = &zero_byte_tests[i].lock1;
1447 status = smb_raw_lock(cli->tree, &io);
1448 CHECK_STATUS(status, NT_STATUS_OK);
1450 io.lockx.in.locks = &zero_byte_tests[i].lock2;
1451 status = smb_raw_lock(cli->tree, &io);
1453 if (NT_STATUS_EQUAL(zero_byte_tests[i].exp_status,
1454 NT_STATUS_LOCK_NOT_GRANTED)) {
1455 /* Allow either of the failure messages and keep going
1456 * if we see the wrong status. */
1457 CHECK_STATUS_OR_CONT(status,
1458 NT_STATUS_LOCK_NOT_GRANTED,
1459 NT_STATUS_FILE_LOCK_CONFLICT);
1462 CHECK_STATUS_CONT(status,
1463 zero_byte_tests[i].exp_status);
1466 /* Unlock both locks. */
1467 io.lockx.in.ulock_cnt = 1;
1468 io.lockx.in.lock_cnt = 0;
1470 if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1471 status = smb_raw_lock(cli->tree, &io);
1472 CHECK_STATUS(status, NT_STATUS_OK);
1475 io.lockx.in.locks = &zero_byte_tests[i].lock1;
1476 status = smb_raw_lock(cli->tree, &io);
1477 CHECK_STATUS(status, NT_STATUS_OK);
1481 smbcli_close(cli->tree, fnum);
1482 smb_raw_exit(cli->session);
1483 smbcli_deltree(cli->tree, BASEDIR);
1487 static bool test_unlock(struct torture_context *tctx, struct smbcli_state *cli)
1493 const char *fname = BASEDIR "\\unlock.txt";
1494 struct smb_lock_entry lock1;
1495 struct smb_lock_entry lock2;
1497 printf("Testing LOCKX unlock:\n");
1499 if (!torture_setup_dir(cli, BASEDIR)) {
1503 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1505 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1509 fnum2 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1511 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1516 /* Setup initial parameters */
1517 io.lockx.level = RAW_LOCK_LOCKX;
1518 io.lockx.in.timeout = 0;
1520 lock1.pid = cli->session->pid;
1523 lock2.pid = cli->session->pid - 1;
1528 * Take exclusive lock, then unlock it with a shared-unlock call.
1530 printf(" taking exclusive lock.\n");
1531 io.lockx.in.ulock_cnt = 0;
1532 io.lockx.in.lock_cnt = 1;
1533 io.lockx.in.mode = 0;
1534 io.lockx.in.file.fnum = fnum1;
1535 io.lockx.in.locks = &lock1;
1536 status = smb_raw_lock(cli->tree, &io);
1537 CHECK_STATUS(status, NT_STATUS_OK);
1539 printf(" unlock the exclusive with a shared unlock call.\n");
1540 io.lockx.in.ulock_cnt = 1;
1541 io.lockx.in.lock_cnt = 0;
1542 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1543 io.lockx.in.file.fnum = fnum1;
1544 io.lockx.in.locks = &lock1;
1545 status = smb_raw_lock(cli->tree, &io);
1546 CHECK_STATUS(status, NT_STATUS_OK);
1548 printf(" try shared lock on pid2/fnum2, testing the unlock.\n");
1549 io.lockx.in.ulock_cnt = 0;
1550 io.lockx.in.lock_cnt = 1;
1551 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1552 io.lockx.in.file.fnum = fnum2;
1553 io.lockx.in.locks = &lock2;
1554 status = smb_raw_lock(cli->tree, &io);
1555 CHECK_STATUS(status, NT_STATUS_OK);
1558 * Unlock a shared lock with an exclusive-unlock call.
1560 printf(" unlock new shared lock with exclusive unlock call.\n");
1561 io.lockx.in.ulock_cnt = 1;
1562 io.lockx.in.lock_cnt = 0;
1563 io.lockx.in.mode = 0;
1564 io.lockx.in.file.fnum = fnum2;
1565 io.lockx.in.locks = &lock2;
1566 status = smb_raw_lock(cli->tree, &io);
1567 CHECK_STATUS(status, NT_STATUS_OK);
1569 printf(" try exclusive lock on pid1, testing the unlock.\n");
1570 io.lockx.in.ulock_cnt = 0;
1571 io.lockx.in.lock_cnt = 1;
1572 io.lockx.in.mode = 0;
1573 io.lockx.in.file.fnum = fnum1;
1574 io.lockx.in.locks = &lock1;
1575 status = smb_raw_lock(cli->tree, &io);
1576 CHECK_STATUS(status, NT_STATUS_OK);
1579 io.lockx.in.ulock_cnt = 1;
1580 io.lockx.in.lock_cnt = 0;
1581 status = smb_raw_lock(cli->tree, &io);
1582 CHECK_STATUS(status, NT_STATUS_OK);
1585 * Test unlocking of 0-byte locks.
1588 printf(" lock shared and exclusive 0-byte locks, testing that Windows "
1589 "always unlocks the exclusive first.\n");
1590 lock1.pid = cli->session->pid;
1593 lock2.pid = cli->session->pid;
1596 io.lockx.in.ulock_cnt = 0;
1597 io.lockx.in.lock_cnt = 1;
1598 io.lockx.in.file.fnum = fnum1;
1599 io.lockx.in.locks = &lock1;
1601 /* lock 0-byte shared
1602 * Note: Order of the shared/exclusive locks doesn't matter. */
1603 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1604 status = smb_raw_lock(cli->tree, &io);
1605 CHECK_STATUS(status, NT_STATUS_OK);
1607 /* lock 0-byte exclusive */
1608 io.lockx.in.mode = 0;
1609 status = smb_raw_lock(cli->tree, &io);
1610 CHECK_STATUS(status, NT_STATUS_OK);
1612 /* test contention */
1613 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1614 io.lockx.in.locks = &lock2;
1615 io.lockx.in.file.fnum = fnum2;
1616 status = smb_raw_lock(cli->tree, &io);
1617 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1618 NT_STATUS_FILE_LOCK_CONFLICT);
1621 io.lockx.in.ulock_cnt = 1;
1622 io.lockx.in.lock_cnt = 0;
1623 io.lockx.in.file.fnum = fnum1;
1624 io.lockx.in.locks = &lock1;
1625 status = smb_raw_lock(cli->tree, &io);
1626 CHECK_STATUS(status, NT_STATUS_OK);
1628 /* test - can we take a shared lock? */
1629 io.lockx.in.ulock_cnt = 0;
1630 io.lockx.in.lock_cnt = 1;
1631 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1632 io.lockx.in.file.fnum = fnum2;
1633 io.lockx.in.locks = &lock2;
1634 status = smb_raw_lock(cli->tree, &io);
1636 /* XXX Samba will fail this test. This is temporary(because this isn't
1637 * new to Win7, it succeeds in WinXP too), until I can come to a
1638 * resolution as to whether Samba should support this or not. There is
1639 * code to preference unlocking exclusive locks before shared locks,
1640 * but its wrapped with "#ifdef ZERO_ZERO". -zkirsch */
1641 if (TARGET_IS_WIN7(tctx))
1642 CHECK_STATUS(status, NT_STATUS_OK);
1644 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1645 NT_STATUS_FILE_LOCK_CONFLICT);
1649 io.lockx.in.ulock_cnt = 1;
1650 io.lockx.in.lock_cnt = 0;
1651 status = smb_raw_lock(cli->tree, &io);
1653 /* XXX Same as above. */
1654 if (TARGET_IS_WIN7(tctx))
1655 CHECK_STATUS(status, NT_STATUS_OK);
1657 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1659 io.lockx.in.file.fnum = fnum1;
1660 io.lockx.in.locks = &lock1;
1661 status = smb_raw_lock(cli->tree, &io);
1662 CHECK_STATUS(status, NT_STATUS_OK);
1665 smbcli_close(cli->tree, fnum1);
1666 smbcli_close(cli->tree, fnum2);
1667 smb_raw_exit(cli->session);
1668 smbcli_deltree(cli->tree, BASEDIR);
1672 static bool test_multiple_unlock(struct torture_context *tctx, struct smbcli_state *cli)
1678 const char *fname = BASEDIR "\\unlock_multiple.txt";
1679 struct smb_lock_entry lock1;
1680 struct smb_lock_entry lock2;
1681 struct smb_lock_entry locks[2];
1683 printf("Testing LOCKX multiple unlock:\n");
1685 if (!torture_setup_dir(cli, BASEDIR)) {
1689 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1691 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1696 /* Setup initial parameters */
1697 io.lockx.level = RAW_LOCK_LOCKX;
1698 io.lockx.in.timeout = 0;
1700 lock1.pid = cli->session->pid;
1703 lock2.pid = cli->session->pid;
1710 io.lockx.in.file.fnum = fnum1;
1711 io.lockx.in.mode = 0; /* exclusive */
1713 /** Test1: Take second lock, but not first. */
1714 printf(" unlock 2 locks, first one not locked. Expect no locks "
1717 io.lockx.in.ulock_cnt = 0;
1718 io.lockx.in.lock_cnt = 1;
1719 io.lockx.in.locks = &lock2;
1720 status = smb_raw_lock(cli->tree, &io);
1721 CHECK_STATUS(status, NT_STATUS_OK);
1723 /* Try to unlock both locks. */
1724 io.lockx.in.ulock_cnt = 2;
1725 io.lockx.in.lock_cnt = 0;
1726 io.lockx.in.locks = locks;
1728 status = smb_raw_lock(cli->tree, &io);
1729 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1731 /* Second lock should not be unlocked. */
1732 io.lockx.in.ulock_cnt = 0;
1733 io.lockx.in.lock_cnt = 1;
1734 io.lockx.in.locks = &lock2;
1735 status = smb_raw_lock(cli->tree, &io);
1736 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1739 io.lockx.in.ulock_cnt = 1;
1740 io.lockx.in.lock_cnt = 0;
1741 io.lockx.in.locks = &lock2;
1742 status = smb_raw_lock(cli->tree, &io);
1743 CHECK_STATUS(status, NT_STATUS_OK);
1745 /** Test2: Take first lock, but not second. */
1746 printf(" unlock 2 locks, second one not locked. Expect first lock "
1749 io.lockx.in.ulock_cnt = 0;
1750 io.lockx.in.lock_cnt = 1;
1751 io.lockx.in.locks = &lock1;
1752 status = smb_raw_lock(cli->tree, &io);
1753 CHECK_STATUS(status, NT_STATUS_OK);
1755 /* Try to unlock both locks. */
1756 io.lockx.in.ulock_cnt = 2;
1757 io.lockx.in.lock_cnt = 0;
1758 io.lockx.in.locks = locks;
1760 status = smb_raw_lock(cli->tree, &io);
1761 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1763 /* First lock should be unlocked. */
1764 io.lockx.in.ulock_cnt = 0;
1765 io.lockx.in.lock_cnt = 1;
1766 io.lockx.in.locks = &lock1;
1767 status = smb_raw_lock(cli->tree, &io);
1768 CHECK_STATUS(status, NT_STATUS_OK);
1771 io.lockx.in.ulock_cnt = 1;
1772 io.lockx.in.lock_cnt = 0;
1773 io.lockx.in.locks = &lock1;
1774 status = smb_raw_lock(cli->tree, &io);
1775 CHECK_STATUS(status, NT_STATUS_OK);
1778 smbcli_close(cli->tree, fnum1);
1779 smb_raw_exit(cli->session);
1780 smbcli_deltree(cli->tree, BASEDIR);
1785 * torture_locktest5 covers stacking pretty well, but its missing two tests:
1786 * - stacking an exclusive on top of shared fails
1787 * - stacking two exclusives fail
1789 static bool test_stacking(struct torture_context *tctx, struct smbcli_state *cli)
1795 const char *fname = BASEDIR "\\stacking.txt";
1796 struct smb_lock_entry lock1;
1797 struct smb_lock_entry lock2;
1799 printf("Testing stacking:\n");
1801 if (!torture_setup_dir(cli, BASEDIR)) {
1805 io.generic.level = RAW_LOCK_LOCKX;
1807 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1809 printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
1814 /* Setup initial parameters */
1815 io.lockx.level = RAW_LOCK_LOCKX;
1816 io.lockx.in.timeout = 0;
1818 lock1.pid = cli->session->pid;
1821 lock2.pid = cli->session->pid - 1;
1826 * Try to take a shared lock, then stack an exclusive.
1828 printf(" stacking an exclusive on top of a shared lock fails.\n");
1829 io.lockx.in.file.fnum = fnum1;
1830 io.lockx.in.locks = &lock1;
1832 io.lockx.in.ulock_cnt = 0;
1833 io.lockx.in.lock_cnt = 1;
1834 io.lockx.in.mode = LOCKING_ANDX_SHARED_LOCK;
1835 status = smb_raw_lock(cli->tree, &io);
1836 CHECK_STATUS(status, NT_STATUS_OK);
1838 io.lockx.in.ulock_cnt = 0;
1839 io.lockx.in.lock_cnt = 1;
1840 io.lockx.in.mode = 0;
1841 status = smb_raw_lock(cli->tree, &io);
1842 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1843 NT_STATUS_FILE_LOCK_CONFLICT);
1846 io.lockx.in.ulock_cnt = 1;
1847 io.lockx.in.lock_cnt = 0;
1848 status = smb_raw_lock(cli->tree, &io);
1849 CHECK_STATUS(status, NT_STATUS_OK);
1852 * Prove that two exclusive locks do not stack.
1854 printf(" two exclusive locks do not stack.\n");
1855 io.lockx.in.ulock_cnt = 0;
1856 io.lockx.in.lock_cnt = 1;
1857 io.lockx.in.mode = 0;
1858 status = smb_raw_lock(cli->tree, &io);
1859 CHECK_STATUS(status, NT_STATUS_OK);
1860 status = smb_raw_lock(cli->tree, &io);
1861 CHECK_STATUS_OR(status, NT_STATUS_LOCK_NOT_GRANTED,
1862 NT_STATUS_FILE_LOCK_CONFLICT);
1865 io.lockx.in.ulock_cnt = 1;
1866 io.lockx.in.lock_cnt = 0;
1867 status = smb_raw_lock(cli->tree, &io);
1868 CHECK_STATUS(status, NT_STATUS_OK);
1871 smbcli_close(cli->tree, fnum1);
1872 smb_raw_exit(cli->session);
1873 smbcli_deltree(cli->tree, BASEDIR);
1878 basic testing of lock calls
1880 struct torture_suite *torture_raw_lock(TALLOC_CTX *mem_ctx)
1882 struct torture_suite *suite = torture_suite_create(mem_ctx, "LOCK");
1884 torture_suite_add_1smb_test(suite, "lockx", test_lockx);
1885 torture_suite_add_1smb_test(suite, "lock", test_lock);
1886 torture_suite_add_1smb_test(suite, "pidhigh", test_pidhigh);
1887 torture_suite_add_1smb_test(suite, "async", test_async);
1888 torture_suite_add_1smb_test(suite, "errorcode", test_errorcode);
1889 torture_suite_add_1smb_test(suite, "changetype", test_changetype);
1891 torture_suite_add_1smb_test(suite, "stacking", test_stacking);
1892 torture_suite_add_1smb_test(suite, "unlock", test_unlock);
1893 torture_suite_add_1smb_test(suite, "multiple_unlock",
1894 test_multiple_unlock);
1895 torture_suite_add_1smb_test(suite, "zerobytelocks",
1896 test_zerobytelocks);