s4:torture/smb2: accept NT_STATUS_RANGE_NOT_LOCKED after smb2_logoff/tdis
[Samba.git] / source4 / torture / smb2 / lock.c
bloba27ae9054deca8db5ca64aaeb7e45489a1a44556
1 /*
2 Unix SMB/CIFS implementation.
4 SMB2 lock test suite
6 Copyright (C) Stefan Metzmacher 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28 #include "torture/util.h"
30 #include "lib/events/events.h"
31 #include "param/param.h"
33 #define CHECK_STATUS(status, correct) do { \
34 const char *_cmt = "(" __location__ ")"; \
35 torture_assert_ntstatus_equal_goto(torture,status,correct, \
36 ret,done,_cmt); \
37 } while (0)
39 #define CHECK_STATUS_CMT(status, correct, cmt) do { \
40 torture_assert_ntstatus_equal_goto(torture,status,correct, \
41 ret,done,cmt); \
42 } while (0)
44 #define CHECK_STATUS_CONT(status, correct) do { \
45 if (!NT_STATUS_EQUAL(status, correct)) { \
46 torture_result(torture, TORTURE_FAIL, \
47 "(%s) Incorrect status %s - should be %s\n", \
48 __location__, nt_errstr(status), nt_errstr(correct)); \
49 ret = false; \
50 }} while (0)
52 #define CHECK_VALUE(v, correct) do { \
53 const char *_cmt = "(" __location__ ")"; \
54 torture_assert_int_equal_goto(torture,v,correct,ret,done,_cmt); \
55 } while (0)
57 #define BASEDIR "testlock"
59 #define TARGET_SUPPORTS_INVALID_LOCK_RANGE(_tctx) \
60 (torture_setting_bool(_tctx, "invalid_lock_range_support", true))
61 #define TARGET_IS_W2K8(_tctx) (torture_setting_bool(_tctx, "w2k8", false))
63 #define WAIT_FOR_ASYNC_RESPONSE(req) \
64 while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) { \
65 if (tevent_loop_once(torture->ev) != 0) { \
66 break; \
67 } \
70 static bool test_valid_request(struct torture_context *torture,
71 struct smb2_tree *tree)
73 bool ret = true;
74 NTSTATUS status;
75 struct smb2_handle h;
76 uint8_t buf[200];
77 struct smb2_lock lck;
78 struct smb2_lock_element el[2];
80 ZERO_STRUCT(buf);
82 status = torture_smb2_testfile(tree, "lock1.txt", &h);
83 CHECK_STATUS(status, NT_STATUS_OK);
85 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
86 CHECK_STATUS(status, NT_STATUS_OK);
88 lck.in.locks = el;
90 torture_comment(torture, "Test request with 0 locks.\n");
92 lck.in.lock_count = 0x0000;
93 lck.in.lock_sequence = 0x00000000;
94 lck.in.file.handle = h;
95 el[0].offset = 0x0000000000000000;
96 el[0].length = 0x0000000000000000;
97 el[0].reserved = 0x0000000000000000;
98 el[0].flags = 0x00000000;
99 status = smb2_lock(tree, &lck);
100 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
102 lck.in.lock_count = 0x0000;
103 lck.in.lock_sequence = 0x00000000;
104 lck.in.file.handle = h;
105 el[0].offset = 0;
106 el[0].length = 0;
107 el[0].reserved = 0x00000000;
108 el[0].flags = SMB2_LOCK_FLAG_SHARED;
109 status = smb2_lock(tree, &lck);
110 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
112 lck.in.lock_count = 0x0001;
113 lck.in.lock_sequence = 0x00000000;
114 lck.in.file.handle = h;
115 el[0].offset = 0;
116 el[0].length = 0;
117 el[0].reserved = 0x00000000;
118 el[0].flags = SMB2_LOCK_FLAG_NONE;
119 status = smb2_lock(tree, &lck);
120 if (TARGET_IS_W2K8(torture)) {
121 CHECK_STATUS(status, NT_STATUS_OK);
122 torture_warning(torture, "Target has bug validating lock flags "
123 "parameter.\n");
124 } else {
125 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
128 torture_comment(torture, "Test >63-bit lock requests.\n");
130 lck.in.file.handle.data[0] +=1;
131 status = smb2_lock(tree, &lck);
132 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
133 lck.in.file.handle.data[0] -=1;
135 lck.in.lock_count = 0x0001;
136 lck.in.lock_sequence = 0x123ab1;
137 lck.in.file.handle = h;
138 el[0].offset = UINT64_MAX;
139 el[0].length = UINT64_MAX;
140 el[0].reserved = 0x00000000;
141 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
142 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
143 status = smb2_lock(tree, &lck);
144 if (TARGET_SUPPORTS_INVALID_LOCK_RANGE(torture)) {
145 CHECK_STATUS(status, NT_STATUS_INVALID_LOCK_RANGE);
146 } else {
147 CHECK_STATUS(status, NT_STATUS_OK);
148 CHECK_VALUE(lck.out.reserved, 0);
151 lck.in.lock_sequence = 0x123ab2;
152 status = smb2_lock(tree, &lck);
153 if (TARGET_SUPPORTS_INVALID_LOCK_RANGE(torture)) {
154 CHECK_STATUS(status, NT_STATUS_INVALID_LOCK_RANGE);
155 } else {
156 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
159 torture_comment(torture, "Test basic lock stacking.\n");
161 lck.in.lock_count = 0x0001;
162 lck.in.lock_sequence = 0x12345678;
163 lck.in.file.handle = h;
164 el[0].offset = UINT32_MAX;
165 el[0].length = UINT32_MAX;
166 el[0].reserved = 0x87654321;
167 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
168 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
169 status = smb2_lock(tree, &lck);
170 CHECK_STATUS(status, NT_STATUS_OK);
171 CHECK_VALUE(lck.out.reserved, 0);
173 el[0].flags = SMB2_LOCK_FLAG_SHARED;
174 status = smb2_lock(tree, &lck);
175 CHECK_STATUS(status, NT_STATUS_OK);
176 CHECK_VALUE(lck.out.reserved, 0);
178 status = smb2_lock(tree, &lck);
179 CHECK_STATUS(status, NT_STATUS_OK);
180 CHECK_VALUE(lck.out.reserved, 0);
182 lck.in.lock_count = 0x0001;
183 lck.in.lock_sequence = 0x87654321;
184 lck.in.file.handle = h;
185 el[0].offset = 0x00000000FFFFFFFF;
186 el[0].length = 0x00000000FFFFFFFF;
187 el[0].reserved = 0x1234567;
188 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
189 status = smb2_lock(tree, &lck);
190 CHECK_STATUS(status, NT_STATUS_OK);
192 lck.in.lock_count = 0x0001;
193 lck.in.lock_sequence = 0x1234567;
194 lck.in.file.handle = h;
195 el[0].offset = 0x00000000FFFFFFFF;
196 el[0].length = 0x00000000FFFFFFFF;
197 el[0].reserved = 0x00000000;
198 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
199 status = smb2_lock(tree, &lck);
200 CHECK_STATUS(status, NT_STATUS_OK);
202 status = smb2_lock(tree, &lck);
203 CHECK_STATUS(status, NT_STATUS_OK);
204 status = smb2_lock(tree, &lck);
205 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
207 torture_comment(torture, "Test flags field permutations.\n");
209 lck.in.lock_count = 0x0001;
210 lck.in.lock_sequence = 0;
211 lck.in.file.handle = h;
212 el[0].offset = 1;
213 el[0].length = 1;
214 el[0].reserved = 0x00000000;
215 el[0].flags = ~SMB2_LOCK_FLAG_ALL_MASK;
217 status = smb2_lock(tree, &lck);
218 if (TARGET_IS_W2K8(torture)) {
219 CHECK_STATUS(status, NT_STATUS_OK);
220 torture_warning(torture, "Target has bug validating lock flags "
221 "parameter.\n");
222 } else {
223 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
226 if (TARGET_IS_W2K8(torture)) {
227 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
228 status = smb2_lock(tree, &lck);
229 CHECK_STATUS(status, NT_STATUS_OK);
232 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
233 status = smb2_lock(tree, &lck);
234 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
236 el[0].flags = SMB2_LOCK_FLAG_UNLOCK |
237 SMB2_LOCK_FLAG_EXCLUSIVE;
238 status = smb2_lock(tree, &lck);
239 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
241 el[0].flags = SMB2_LOCK_FLAG_UNLOCK |
242 SMB2_LOCK_FLAG_SHARED;
243 status = smb2_lock(tree, &lck);
244 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
246 el[0].flags = SMB2_LOCK_FLAG_UNLOCK |
247 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
248 status = smb2_lock(tree, &lck);
249 if (TARGET_IS_W2K8(torture)) {
250 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
251 torture_warning(torture, "Target has bug validating lock flags "
252 "parameter.\n");
253 } else {
254 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
257 torture_comment(torture, "Test return error when 2 locks are "
258 "requested\n");
260 lck.in.lock_count = 2;
261 lck.in.lock_sequence = 0;
262 lck.in.file.handle = h;
263 el[0].offset = 9999;
264 el[0].length = 1;
265 el[0].reserved = 0x00000000;
266 el[1].offset = 9999;
267 el[1].length = 1;
268 el[1].reserved = 0x00000000;
270 lck.in.lock_count = 2;
271 el[0].flags = 0;
272 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
273 status = smb2_lock(tree, &lck);
274 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
276 lck.in.lock_count = 2;
277 el[0].flags = 0;
278 el[1].flags = 0;
279 status = smb2_lock(tree, &lck);
280 if (TARGET_IS_W2K8(torture)) {
281 CHECK_STATUS(status, NT_STATUS_OK);
282 torture_warning(torture, "Target has bug validating lock flags "
283 "parameter.\n");
284 } else {
285 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
288 lck.in.lock_count = 2;
289 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
290 el[1].flags = 0;
291 status = smb2_lock(tree, &lck);
292 if (TARGET_IS_W2K8(torture)) {
293 CHECK_STATUS(status, NT_STATUS_OK);
294 torture_warning(torture, "Target has bug validating lock flags "
295 "parameter.\n");
296 } else {
297 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
300 lck.in.lock_count = 1;
301 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
302 status = smb2_lock(tree, &lck);
303 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
305 lck.in.lock_count = 1;
306 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
307 status = smb2_lock(tree, &lck);
308 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
310 lck.in.lock_count = 1;
311 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
312 status = smb2_lock(tree, &lck);
313 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
315 lck.in.lock_count = 1;
316 el[0].flags = SMB2_LOCK_FLAG_SHARED;
317 status = smb2_lock(tree, &lck);
318 CHECK_STATUS(status, NT_STATUS_OK);
320 status = smb2_lock(tree, &lck);
321 CHECK_STATUS(status, NT_STATUS_OK);
323 lck.in.lock_count = 2;
324 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
325 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
326 status = smb2_lock(tree, &lck);
327 CHECK_STATUS(status, NT_STATUS_OK);
329 lck.in.lock_count = 1;
330 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
331 status = smb2_lock(tree, &lck);
332 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
334 done:
335 return ret;
338 struct test_lock_read_write_state {
339 const char *fname;
340 uint32_t lock_flags;
341 NTSTATUS write_h1_status;
342 NTSTATUS read_h1_status;
343 NTSTATUS write_h2_status;
344 NTSTATUS read_h2_status;
347 static bool test_lock_read_write(struct torture_context *torture,
348 struct smb2_tree *tree,
349 struct test_lock_read_write_state *s)
351 bool ret = true;
352 NTSTATUS status;
353 struct smb2_handle h1, h2;
354 uint8_t buf[200];
355 struct smb2_lock lck;
356 struct smb2_create cr;
357 struct smb2_write wr;
358 struct smb2_read rd;
359 struct smb2_lock_element el[1];
361 lck.in.locks = el;
363 ZERO_STRUCT(buf);
365 status = torture_smb2_testfile(tree, s->fname, &h1);
366 CHECK_STATUS(status, NT_STATUS_OK);
368 status = smb2_util_write(tree, h1, buf, 0, ARRAY_SIZE(buf));
369 CHECK_STATUS(status, NT_STATUS_OK);
371 lck.in.lock_count = 0x0001;
372 lck.in.lock_sequence = 0x00000000;
373 lck.in.file.handle = h1;
374 el[0].offset = 0;
375 el[0].length = ARRAY_SIZE(buf)/2;
376 el[0].reserved = 0x00000000;
377 el[0].flags = s->lock_flags;
378 status = smb2_lock(tree, &lck);
379 CHECK_STATUS(status, NT_STATUS_OK);
380 CHECK_VALUE(lck.out.reserved, 0);
382 lck.in.lock_count = 0x0001;
383 lck.in.lock_sequence = 0x00000000;
384 lck.in.file.handle = h1;
385 el[0].offset = ARRAY_SIZE(buf)/2;
386 el[0].length = ARRAY_SIZE(buf)/2;
387 el[0].reserved = 0x00000000;
388 el[0].flags = s->lock_flags;
389 status = smb2_lock(tree, &lck);
390 CHECK_STATUS(status, NT_STATUS_OK);
391 CHECK_VALUE(lck.out.reserved, 0);
393 ZERO_STRUCT(cr);
394 cr.in.oplock_level = 0;
395 cr.in.desired_access = SEC_RIGHTS_FILE_ALL;
396 cr.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
397 cr.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
398 cr.in.share_access =
399 NTCREATEX_SHARE_ACCESS_DELETE|
400 NTCREATEX_SHARE_ACCESS_READ|
401 NTCREATEX_SHARE_ACCESS_WRITE;
402 cr.in.create_options = 0;
403 cr.in.fname = s->fname;
405 status = smb2_create(tree, tree, &cr);
406 CHECK_STATUS(status, NT_STATUS_OK);
408 h2 = cr.out.file.handle;
410 ZERO_STRUCT(wr);
411 wr.in.file.handle = h1;
412 wr.in.offset = ARRAY_SIZE(buf)/2;
413 wr.in.data = data_blob_const(buf, ARRAY_SIZE(buf)/2);
415 status = smb2_write(tree, &wr);
416 CHECK_STATUS(status, s->write_h1_status);
418 ZERO_STRUCT(rd);
419 rd.in.file.handle = h1;
420 rd.in.offset = ARRAY_SIZE(buf)/2;
421 rd.in.length = ARRAY_SIZE(buf)/2;
423 status = smb2_read(tree, tree, &rd);
424 CHECK_STATUS(status, s->read_h1_status);
426 ZERO_STRUCT(wr);
427 wr.in.file.handle = h2;
428 wr.in.offset = ARRAY_SIZE(buf)/2;
429 wr.in.data = data_blob_const(buf, ARRAY_SIZE(buf)/2);
431 status = smb2_write(tree, &wr);
432 CHECK_STATUS(status, s->write_h2_status);
434 ZERO_STRUCT(rd);
435 rd.in.file.handle = h2;
436 rd.in.offset = ARRAY_SIZE(buf)/2;
437 rd.in.length = ARRAY_SIZE(buf)/2;
439 status = smb2_read(tree, tree, &rd);
440 CHECK_STATUS(status, s->read_h2_status);
442 lck.in.lock_count = 0x0001;
443 lck.in.lock_sequence = 0x00000000;
444 lck.in.file.handle = h1;
445 el[0].offset = ARRAY_SIZE(buf)/2;
446 el[0].length = ARRAY_SIZE(buf)/2;
447 el[0].reserved = 0x00000000;
448 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
449 status = smb2_lock(tree, &lck);
450 CHECK_STATUS(status, NT_STATUS_OK);
451 CHECK_VALUE(lck.out.reserved, 0);
453 ZERO_STRUCT(wr);
454 wr.in.file.handle = h2;
455 wr.in.offset = ARRAY_SIZE(buf)/2;
456 wr.in.data = data_blob_const(buf, ARRAY_SIZE(buf)/2);
458 status = smb2_write(tree, &wr);
459 CHECK_STATUS(status, NT_STATUS_OK);
461 ZERO_STRUCT(rd);
462 rd.in.file.handle = h2;
463 rd.in.offset = ARRAY_SIZE(buf)/2;
464 rd.in.length = ARRAY_SIZE(buf)/2;
466 status = smb2_read(tree, tree, &rd);
467 CHECK_STATUS(status, NT_STATUS_OK);
469 done:
470 return ret;
473 static bool test_lock_rw_none(struct torture_context *torture,
474 struct smb2_tree *tree)
476 struct test_lock_read_write_state s = {
477 .fname = "lock_rw_none.dat",
478 .lock_flags = SMB2_LOCK_FLAG_NONE,
479 .write_h1_status = NT_STATUS_FILE_LOCK_CONFLICT,
480 .read_h1_status = NT_STATUS_OK,
481 .write_h2_status = NT_STATUS_FILE_LOCK_CONFLICT,
482 .read_h2_status = NT_STATUS_OK,
485 if (!TARGET_IS_W2K8(torture)) {
486 torture_skip(torture, "RW-NONE tests the behavior of a "
487 "NONE-type lock, which is the same as a SHARED "
488 "lock but is granted due to a bug in W2K8. If "
489 "target is not W2K8 we skip this test.\n");
492 return test_lock_read_write(torture, tree, &s);
495 static bool test_lock_rw_shared(struct torture_context *torture,
496 struct smb2_tree *tree)
498 struct test_lock_read_write_state s = {
499 .fname = "lock_rw_shared.dat",
500 .lock_flags = SMB2_LOCK_FLAG_SHARED,
501 .write_h1_status = NT_STATUS_FILE_LOCK_CONFLICT,
502 .read_h1_status = NT_STATUS_OK,
503 .write_h2_status = NT_STATUS_FILE_LOCK_CONFLICT,
504 .read_h2_status = NT_STATUS_OK,
507 return test_lock_read_write(torture, tree, &s);
510 static bool test_lock_rw_exclusive(struct torture_context *torture,
511 struct smb2_tree *tree)
513 struct test_lock_read_write_state s = {
514 .fname = "lock_rw_exclusive.dat",
515 .lock_flags = SMB2_LOCK_FLAG_EXCLUSIVE,
516 .write_h1_status = NT_STATUS_OK,
517 .read_h1_status = NT_STATUS_OK,
518 .write_h2_status = NT_STATUS_FILE_LOCK_CONFLICT,
519 .read_h2_status = NT_STATUS_FILE_LOCK_CONFLICT,
522 return test_lock_read_write(torture, tree, &s);
525 static bool test_lock_auto_unlock(struct torture_context *torture,
526 struct smb2_tree *tree)
528 bool ret = true;
529 NTSTATUS status;
530 struct smb2_handle h;
531 uint8_t buf[200];
532 struct smb2_lock lck;
533 struct smb2_lock_element el[1];
535 ZERO_STRUCT(buf);
537 status = torture_smb2_testfile(tree, "autounlock.txt", &h);
538 CHECK_STATUS(status, NT_STATUS_OK);
540 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
541 CHECK_STATUS(status, NT_STATUS_OK);
543 ZERO_STRUCT(lck);
544 ZERO_STRUCT(el[0]);
545 lck.in.locks = el;
546 lck.in.lock_count = 0x0001;
547 lck.in.file.handle = h;
548 el[0].offset = 0;
549 el[0].length = 1;
550 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
551 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
552 status = smb2_lock(tree, &lck);
553 CHECK_STATUS(status, NT_STATUS_OK);
555 status = smb2_lock(tree, &lck);
556 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
558 status = smb2_lock(tree, &lck);
559 if (TARGET_IS_W2K8(torture)) {
560 CHECK_STATUS(status, NT_STATUS_OK);
561 torture_warning(torture, "Target has \"pretty please\" bug. "
562 "A contending lock request on the same handle "
563 "unlocks the lock.\n");
564 } else {
565 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
568 status = smb2_lock(tree, &lck);
569 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
571 done:
572 return ret;
576 test different lock ranges and see if different handles conflict
578 static bool test_lock(struct torture_context *torture,
579 struct smb2_tree *tree)
581 NTSTATUS status;
582 bool ret = true;
583 struct smb2_handle h, h2;
584 uint8_t buf[200];
585 struct smb2_lock lck;
586 struct smb2_lock_element el[2];
588 const char *fname = BASEDIR "\\async.txt";
590 status = torture_smb2_testdir(tree, BASEDIR, &h);
591 CHECK_STATUS(status, NT_STATUS_OK);
592 smb2_util_close(tree, h);
594 status = torture_smb2_testfile(tree, fname, &h);
595 CHECK_STATUS(status, NT_STATUS_OK);
597 ZERO_STRUCT(buf);
598 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
599 CHECK_STATUS(status, NT_STATUS_OK);
601 status = torture_smb2_testfile(tree, fname, &h2);
602 CHECK_STATUS(status, NT_STATUS_OK);
604 lck.in.locks = el;
606 lck.in.lock_count = 0x0001;
607 lck.in.lock_sequence = 0x00000000;
608 lck.in.file.handle = h;
609 el[0].reserved = 0x00000000;
610 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
611 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
613 torture_comment(torture, "Trying 0/0 lock\n");
614 el[0].offset = 0x0000000000000000;
615 el[0].length = 0x0000000000000000;
616 status = smb2_lock(tree, &lck);
617 CHECK_STATUS(status, NT_STATUS_OK);
618 lck.in.file.handle = h2;
619 status = smb2_lock(tree, &lck);
620 CHECK_STATUS(status, NT_STATUS_OK);
621 lck.in.file.handle = h;
622 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
623 status = smb2_lock(tree, &lck);
624 CHECK_STATUS(status, NT_STATUS_OK);
626 torture_comment(torture, "Trying 0/1 lock\n");
627 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
628 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
629 el[0].offset = 0x0000000000000000;
630 el[0].length = 0x0000000000000001;
631 status = smb2_lock(tree, &lck);
632 CHECK_STATUS(status, NT_STATUS_OK);
633 lck.in.file.handle = h2;
634 status = smb2_lock(tree, &lck);
635 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
636 lck.in.file.handle = h;
637 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
638 status = smb2_lock(tree, &lck);
639 CHECK_STATUS(status, NT_STATUS_OK);
640 status = smb2_lock(tree, &lck);
641 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
643 torture_comment(torture, "Trying 0xEEFFFFF lock\n");
644 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
645 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
646 el[0].offset = 0xEEFFFFFF;
647 el[0].length = 4000;
648 status = smb2_lock(tree, &lck);
649 CHECK_STATUS(status, NT_STATUS_OK);
650 lck.in.file.handle = h2;
651 status = smb2_lock(tree, &lck);
652 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
653 lck.in.file.handle = h;
654 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
655 status = smb2_lock(tree, &lck);
656 CHECK_STATUS(status, NT_STATUS_OK);
657 status = smb2_lock(tree, &lck);
658 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
660 torture_comment(torture, "Trying 0xEF00000 lock\n");
661 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
662 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
663 el[0].offset = 0xEF000000;
664 el[0].length = 4000;
665 status = smb2_lock(tree, &lck);
666 CHECK_STATUS(status, NT_STATUS_OK);
667 lck.in.file.handle = h2;
668 status = smb2_lock(tree, &lck);
669 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
670 lck.in.file.handle = h;
671 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
672 status = smb2_lock(tree, &lck);
673 CHECK_STATUS(status, NT_STATUS_OK);
674 status = smb2_lock(tree, &lck);
675 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
677 torture_comment(torture, "Trying (2^63 - 1)/1\n");
678 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
679 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
680 el[0].offset = 1;
681 el[0].offset <<= 63;
682 el[0].offset--;
683 el[0].length = 1;
684 status = smb2_lock(tree, &lck);
685 CHECK_STATUS(status, NT_STATUS_OK);
686 lck.in.file.handle = h2;
687 status = smb2_lock(tree, &lck);
688 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
689 lck.in.file.handle = h;
690 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
691 status = smb2_lock(tree, &lck);
692 CHECK_STATUS(status, NT_STATUS_OK);
693 status = smb2_lock(tree, &lck);
694 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
696 torture_comment(torture, "Trying 2^63/1\n");
697 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
698 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
699 el[0].offset = 1;
700 el[0].offset <<= 63;
701 el[0].length = 1;
702 status = smb2_lock(tree, &lck);
703 CHECK_STATUS(status, NT_STATUS_OK);
704 lck.in.file.handle = h2;
705 status = smb2_lock(tree, &lck);
706 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
707 lck.in.file.handle = h;
708 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
709 status = smb2_lock(tree, &lck);
710 CHECK_STATUS(status, NT_STATUS_OK);
711 status = smb2_lock(tree, &lck);
712 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
714 torture_comment(torture, "Trying max/0 lock\n");
715 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
716 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
717 el[0].offset = ~0;
718 el[0].length = 0;
719 status = smb2_lock(tree, &lck);
720 CHECK_STATUS(status, NT_STATUS_OK);
721 lck.in.file.handle = h2;
722 status = smb2_lock(tree, &lck);
723 CHECK_STATUS(status, NT_STATUS_OK);
724 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
725 status = smb2_lock(tree, &lck);
726 CHECK_STATUS(status, NT_STATUS_OK);
727 lck.in.file.handle = h;
728 status = smb2_lock(tree, &lck);
729 CHECK_STATUS(status, NT_STATUS_OK);
730 status = smb2_lock(tree, &lck);
731 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
733 torture_comment(torture, "Trying max/1 lock\n");
734 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
735 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
736 el[0].offset = ~0;
737 el[0].length = 1;
738 status = smb2_lock(tree, &lck);
739 CHECK_STATUS(status, NT_STATUS_OK);
740 lck.in.file.handle = h2;
741 status = smb2_lock(tree, &lck);
742 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
743 lck.in.file.handle = h;
744 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
745 status = smb2_lock(tree, &lck);
746 CHECK_STATUS(status, NT_STATUS_OK);
747 status = smb2_lock(tree, &lck);
748 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
750 torture_comment(torture, "Trying max/2 lock\n");
751 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
752 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
753 el[0].offset = ~0;
754 el[0].length = 2;
755 status = smb2_lock(tree, &lck);
756 if (TARGET_SUPPORTS_INVALID_LOCK_RANGE(torture)) {
757 CHECK_STATUS(status, NT_STATUS_INVALID_LOCK_RANGE);
758 } else {
759 CHECK_STATUS(status, NT_STATUS_OK);
760 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
761 status = smb2_lock(tree, &lck);
762 CHECK_STATUS(status, NT_STATUS_OK);
765 torture_comment(torture, "Trying wrong handle unlock\n");
766 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
767 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
768 el[0].offset = 10001;
769 el[0].length = 40002;
770 status = smb2_lock(tree, &lck);
771 CHECK_STATUS(status, NT_STATUS_OK);
772 lck.in.file.handle = h2;
773 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
774 status = smb2_lock(tree, &lck);
775 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
776 lck.in.file.handle = h;
777 status = smb2_lock(tree, &lck);
778 CHECK_STATUS(status, NT_STATUS_OK);
780 done:
781 smb2_util_close(tree, h2);
782 smb2_util_close(tree, h);
783 smb2_deltree(tree, BASEDIR);
784 return ret;
788 test SMB2 LOCK async operation
790 static bool test_async(struct torture_context *torture,
791 struct smb2_tree *tree)
793 NTSTATUS status;
794 bool ret = true;
795 struct smb2_handle h, h2;
796 uint8_t buf[200];
797 struct smb2_lock lck;
798 struct smb2_lock_element el[2];
799 struct smb2_request *req = NULL;
801 const char *fname = BASEDIR "\\async.txt";
803 status = torture_smb2_testdir(tree, BASEDIR, &h);
804 CHECK_STATUS(status, NT_STATUS_OK);
805 smb2_util_close(tree, h);
807 status = torture_smb2_testfile(tree, fname, &h);
808 CHECK_STATUS(status, NT_STATUS_OK);
810 ZERO_STRUCT(buf);
811 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
812 CHECK_STATUS(status, NT_STATUS_OK);
814 status = torture_smb2_testfile(tree, fname, &h2);
815 CHECK_STATUS(status, NT_STATUS_OK);
817 lck.in.locks = el;
819 lck.in.lock_count = 0x0001;
820 lck.in.lock_sequence = 0x00000000;
821 lck.in.file.handle = h;
822 el[0].offset = 100;
823 el[0].length = 50;
824 el[0].reserved = 0x00000000;
825 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
827 torture_comment(torture, " Acquire first lock\n");
828 status = smb2_lock(tree, &lck);
829 CHECK_STATUS(status, NT_STATUS_OK);
831 torture_comment(torture, " Second lock should pend on first\n");
832 lck.in.file.handle = h2;
833 req = smb2_lock_send(tree, &lck);
834 WAIT_FOR_ASYNC_RESPONSE(req);
836 torture_comment(torture, " Unlock first lock\n");
837 lck.in.file.handle = h;
838 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
839 status = smb2_lock(tree, &lck);
840 CHECK_STATUS(status, NT_STATUS_OK);
842 torture_comment(torture, " Second lock should now succeed\n");
843 lck.in.file.handle = h2;
844 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
845 status = smb2_lock_recv(req, &lck);
846 CHECK_STATUS(status, NT_STATUS_OK);
848 done:
849 smb2_util_close(tree, h2);
850 smb2_util_close(tree, h);
851 smb2_deltree(tree, BASEDIR);
852 return ret;
856 test SMB2 LOCK Cancel operation
858 static bool test_cancel(struct torture_context *torture,
859 struct smb2_tree *tree)
861 NTSTATUS status;
862 bool ret = true;
863 struct smb2_handle h, h2;
864 uint8_t buf[200];
865 struct smb2_lock lck;
866 struct smb2_lock_element el[2];
867 struct smb2_request *req = NULL;
869 const char *fname = BASEDIR "\\cancel.txt";
871 status = torture_smb2_testdir(tree, BASEDIR, &h);
872 CHECK_STATUS(status, NT_STATUS_OK);
873 smb2_util_close(tree, h);
875 status = torture_smb2_testfile(tree, fname, &h);
876 CHECK_STATUS(status, NT_STATUS_OK);
878 ZERO_STRUCT(buf);
879 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
880 CHECK_STATUS(status, NT_STATUS_OK);
882 status = torture_smb2_testfile(tree, fname, &h2);
883 CHECK_STATUS(status, NT_STATUS_OK);
885 lck.in.locks = el;
887 lck.in.lock_count = 0x0001;
888 lck.in.lock_sequence = 0x00000000;
889 lck.in.file.handle = h;
890 el[0].offset = 100;
891 el[0].length = 10;
892 el[0].reserved = 0x00000000;
893 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
895 torture_comment(torture, "Testing basic cancel\n");
897 torture_comment(torture, " Acquire first lock\n");
898 status = smb2_lock(tree, &lck);
899 CHECK_STATUS(status, NT_STATUS_OK);
901 torture_comment(torture, " Second lock should pend on first\n");
902 lck.in.file.handle = h2;
903 req = smb2_lock_send(tree, &lck);
904 WAIT_FOR_ASYNC_RESPONSE(req);
906 torture_comment(torture, " Cancel the second lock\n");
907 smb2_cancel(req);
908 lck.in.file.handle = h2;
909 status = smb2_lock_recv(req, &lck);
910 CHECK_STATUS(status, NT_STATUS_CANCELLED);
912 torture_comment(torture, " Unlock first lock\n");
913 lck.in.file.handle = h;
914 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
915 status = smb2_lock(tree, &lck);
916 CHECK_STATUS(status, NT_STATUS_OK);
919 torture_comment(torture, "Testing cancel by unlock\n");
921 torture_comment(torture, " Acquire first lock\n");
922 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
923 status = smb2_lock(tree, &lck);
924 CHECK_STATUS(status, NT_STATUS_OK);
926 torture_comment(torture, " Second lock should pend on first\n");
927 lck.in.file.handle = h2;
928 req = smb2_lock_send(tree, &lck);
929 WAIT_FOR_ASYNC_RESPONSE(req);
931 torture_comment(torture, " Attempt to unlock pending second lock\n");
932 lck.in.file.handle = h2;
933 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
934 status = smb2_lock(tree, &lck);
935 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
937 torture_comment(torture, " Now cancel the second lock\n");
938 smb2_cancel(req);
939 lck.in.file.handle = h2;
940 status = smb2_lock_recv(req, &lck);
941 CHECK_STATUS(status, NT_STATUS_CANCELLED);
943 torture_comment(torture, " Unlock first lock\n");
944 lck.in.file.handle = h;
945 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
946 status = smb2_lock(tree, &lck);
947 CHECK_STATUS(status, NT_STATUS_OK);
950 torture_comment(torture, "Testing cancel by close\n");
952 torture_comment(torture, " Acquire first lock\n");
953 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
954 status = smb2_lock(tree, &lck);
955 CHECK_STATUS(status, NT_STATUS_OK);
957 torture_comment(torture, " Second lock should pend on first\n");
958 lck.in.file.handle = h2;
959 req = smb2_lock_send(tree, &lck);
960 WAIT_FOR_ASYNC_RESPONSE(req);
962 torture_comment(torture, " Close the second lock handle\n");
963 smb2_util_close(tree, h2);
964 CHECK_STATUS(status, NT_STATUS_OK);
966 torture_comment(torture, " Check pending lock reply\n");
967 status = smb2_lock_recv(req, &lck);
968 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
970 torture_comment(torture, " Unlock first lock\n");
971 lck.in.file.handle = h;
972 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
973 status = smb2_lock(tree, &lck);
974 CHECK_STATUS(status, NT_STATUS_OK);
976 done:
977 smb2_util_close(tree, h2);
978 smb2_util_close(tree, h);
979 smb2_deltree(tree, BASEDIR);
980 return ret;
984 test SMB2 LOCK Cancel by tree disconnect
986 static bool test_cancel_tdis(struct torture_context *torture,
987 struct smb2_tree *tree)
989 NTSTATUS status;
990 bool ret = true;
991 struct smb2_handle h, h2;
992 uint8_t buf[200];
993 struct smb2_lock lck;
994 struct smb2_lock_element el[2];
995 struct smb2_request *req = NULL;
997 const char *fname = BASEDIR "\\cancel_tdis.txt";
999 status = torture_smb2_testdir(tree, BASEDIR, &h);
1000 CHECK_STATUS(status, NT_STATUS_OK);
1001 smb2_util_close(tree, h);
1003 status = torture_smb2_testfile(tree, fname, &h);
1004 CHECK_STATUS(status, NT_STATUS_OK);
1006 ZERO_STRUCT(buf);
1007 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1008 CHECK_STATUS(status, NT_STATUS_OK);
1010 status = torture_smb2_testfile(tree, fname, &h2);
1011 CHECK_STATUS(status, NT_STATUS_OK);
1013 lck.in.locks = el;
1015 lck.in.lock_count = 0x0001;
1016 lck.in.lock_sequence = 0x00000000;
1017 lck.in.file.handle = h;
1018 el[0].offset = 100;
1019 el[0].length = 10;
1020 el[0].reserved = 0x00000000;
1021 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1023 torture_comment(torture, "Testing cancel by tree disconnect\n");
1025 status = torture_smb2_testfile(tree, fname, &h);
1026 CHECK_STATUS(status, NT_STATUS_OK);
1028 status = torture_smb2_testfile(tree, fname, &h2);
1029 CHECK_STATUS(status, NT_STATUS_OK);
1031 torture_comment(torture, " Acquire first lock\n");
1032 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1033 status = smb2_lock(tree, &lck);
1034 CHECK_STATUS(status, NT_STATUS_OK);
1036 torture_comment(torture, " Second lock should pend on first\n");
1037 lck.in.file.handle = h2;
1038 req = smb2_lock_send(tree, &lck);
1039 WAIT_FOR_ASYNC_RESPONSE(req);
1041 torture_comment(torture, " Disconnect the tree\n");
1042 smb2_tdis(tree);
1043 CHECK_STATUS(status, NT_STATUS_OK);
1045 torture_comment(torture, " Check pending lock reply\n");
1046 status = smb2_lock_recv(req, &lck);
1047 if (!NT_STATUS_EQUAL(status, NT_STATUS_RANGE_NOT_LOCKED)) {
1049 * The status depends on the server internals
1050 * the order in which the files are closed
1051 * by smb2_tdis().
1053 CHECK_STATUS(status, NT_STATUS_OK);
1056 torture_comment(torture, " Attempt to unlock first lock\n");
1057 lck.in.file.handle = h;
1058 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1059 status = smb2_lock(tree, &lck);
1061 * Most Windows versions have a strange order to
1062 * verify the session id, tree id and file id.
1063 * (They should be checked in that order, but windows
1064 * seems to check the file id before the others).
1066 if (!NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED)) {
1067 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
1070 done:
1071 smb2_util_close(tree, h2);
1072 smb2_util_close(tree, h);
1073 smb2_deltree(tree, BASEDIR);
1074 return ret;
1078 test SMB2 LOCK Cancel by user logoff
1080 static bool test_cancel_logoff(struct torture_context *torture,
1081 struct smb2_tree *tree)
1083 NTSTATUS status;
1084 bool ret = true;
1085 struct smb2_handle h, h2;
1086 uint8_t buf[200];
1087 struct smb2_lock lck;
1088 struct smb2_lock_element el[2];
1089 struct smb2_request *req = NULL;
1091 const char *fname = BASEDIR "\\cancel_logoff.txt";
1093 status = torture_smb2_testdir(tree, BASEDIR, &h);
1094 CHECK_STATUS(status, NT_STATUS_OK);
1095 smb2_util_close(tree, h);
1097 status = torture_smb2_testfile(tree, fname, &h);
1098 CHECK_STATUS(status, NT_STATUS_OK);
1100 ZERO_STRUCT(buf);
1101 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1102 CHECK_STATUS(status, NT_STATUS_OK);
1104 status = torture_smb2_testfile(tree, fname, &h2);
1105 CHECK_STATUS(status, NT_STATUS_OK);
1107 lck.in.locks = el;
1109 lck.in.lock_count = 0x0001;
1110 lck.in.lock_sequence = 0x00000000;
1111 lck.in.file.handle = h;
1112 el[0].offset = 100;
1113 el[0].length = 10;
1114 el[0].reserved = 0x00000000;
1115 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1117 torture_comment(torture, "Testing cancel by ulogoff\n");
1119 torture_comment(torture, " Acquire first lock\n");
1120 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1121 status = smb2_lock(tree, &lck);
1122 CHECK_STATUS(status, NT_STATUS_OK);
1124 torture_comment(torture, " Second lock should pend on first\n");
1125 lck.in.file.handle = h2;
1126 req = smb2_lock_send(tree, &lck);
1127 WAIT_FOR_ASYNC_RESPONSE(req);
1129 torture_comment(torture, " Logoff user\n");
1130 smb2_logoff(tree->session);
1132 torture_comment(torture, " Check pending lock reply\n");
1133 status = smb2_lock_recv(req, &lck);
1134 if (!NT_STATUS_EQUAL(status, NT_STATUS_RANGE_NOT_LOCKED)) {
1136 * The status depends on the server internals
1137 * the order in which the files are closed
1138 * by smb2_logoff().
1140 CHECK_STATUS(status, NT_STATUS_OK);
1143 torture_comment(torture, " Attempt to unlock first lock\n");
1144 lck.in.file.handle = h;
1145 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1146 status = smb2_lock(tree, &lck);
1148 * Most Windows versions have a strange order to
1149 * verify the session id, tree id and file id.
1150 * (They should be checked in that order, but windows
1151 * seems to check the file id before the others).
1153 if (!NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
1154 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
1157 done:
1158 smb2_util_close(tree, h2);
1159 smb2_util_close(tree, h);
1160 smb2_deltree(tree, BASEDIR);
1161 return ret;
1165 * Test NT_STATUS_LOCK_NOT_GRANTED vs. NT_STATUS_FILE_LOCK_CONFLICT
1167 * The SMBv1 protocol returns a different error code on lock acquisition
1168 * failure depending on a number of parameters, including what error code
1169 * was returned to the previous failure.
1171 * SMBv2 has cleaned up these semantics and should always return
1172 * NT_STATUS_LOCK_NOT_GRANTED to failed lock requests, and
1173 * NT_STATUS_FILE_LOCK_CONFLICT to failed read/write requests due to a lock
1174 * being held on that range.
1176 static bool test_errorcode(struct torture_context *torture,
1177 struct smb2_tree *tree)
1179 NTSTATUS status;
1180 bool ret = true;
1181 struct smb2_handle h, h2;
1182 uint8_t buf[200];
1183 struct smb2_lock lck;
1184 struct smb2_lock_element el[2];
1186 const char *fname = BASEDIR "\\errorcode.txt";
1188 status = torture_smb2_testdir(tree, BASEDIR, &h);
1189 CHECK_STATUS(status, NT_STATUS_OK);
1190 smb2_util_close(tree, h);
1192 status = torture_smb2_testfile(tree, fname, &h);
1193 CHECK_STATUS(status, NT_STATUS_OK);
1195 ZERO_STRUCT(buf);
1196 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1197 CHECK_STATUS(status, NT_STATUS_OK);
1199 status = torture_smb2_testfile(tree, fname, &h2);
1200 CHECK_STATUS(status, NT_STATUS_OK);
1202 lck.in.locks = el;
1204 lck.in.lock_count = 0x0001;
1205 lck.in.lock_sequence = 0x00000000;
1206 lck.in.file.handle = h;
1207 el[0].offset = 100;
1208 el[0].length = 10;
1209 el[0].reserved = 0x00000000;
1210 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1211 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1213 torture_comment(torture, "Testing LOCK_NOT_GRANTED vs. "
1214 "FILE_LOCK_CONFLICT\n");
1216 if (TARGET_IS_W2K8(torture)) {
1217 torture_result(torture, TORTURE_SKIP,
1218 "Target has \"pretty please\" bug. A contending lock "
1219 "request on the same handle unlocks the lock.");
1220 goto done;
1223 status = smb2_lock(tree, &lck);
1224 CHECK_STATUS(status, NT_STATUS_OK);
1226 /* Demonstrate that the first conflicting lock on each handle gives
1227 * LOCK_NOT_GRANTED. */
1228 lck.in.file.handle = h;
1229 status = smb2_lock(tree, &lck);
1230 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1232 lck.in.file.handle = h2;
1233 status = smb2_lock(tree, &lck);
1234 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1236 /* Demonstrate that each following conflict also gives
1237 * LOCK_NOT_GRANTED */
1238 lck.in.file.handle = h;
1239 status = smb2_lock(tree, &lck);
1240 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1242 lck.in.file.handle = h2;
1243 status = smb2_lock(tree, &lck);
1244 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1246 lck.in.file.handle = h;
1247 status = smb2_lock(tree, &lck);
1248 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1250 lck.in.file.handle = h2;
1251 status = smb2_lock(tree, &lck);
1252 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1254 /* Demonstrate that the smbpid doesn't matter */
1255 lck.in.file.handle = h;
1256 status = smb2_lock(tree, &lck);
1257 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1259 lck.in.file.handle = h2;
1260 status = smb2_lock(tree, &lck);
1261 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1263 /* Demonstrate that a 0-byte lock inside the locked range still
1264 * gives the same error. */
1266 el[0].offset = 102;
1267 el[0].length = 0;
1268 lck.in.file.handle = h;
1269 status = smb2_lock(tree, &lck);
1270 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1272 lck.in.file.handle = h2;
1273 status = smb2_lock(tree, &lck);
1274 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1276 /* Demonstrate that a lock inside the locked range still gives the
1277 * same error. */
1279 el[0].offset = 102;
1280 el[0].length = 5;
1281 lck.in.file.handle = h;
1282 status = smb2_lock(tree, &lck);
1283 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1285 lck.in.file.handle = h2;
1286 status = smb2_lock(tree, &lck);
1287 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1289 done:
1290 smb2_util_close(tree, h2);
1291 smb2_util_close(tree, h);
1292 smb2_deltree(tree, BASEDIR);
1293 return ret;
1297 * Tests zero byte locks.
1300 struct double_lock_test {
1301 struct smb2_lock_element lock1;
1302 struct smb2_lock_element lock2;
1303 NTSTATUS status;
1306 static struct double_lock_test zero_byte_tests[] = {
1307 /* {offset, count, reserved, flags},
1308 * {offset, count, reserved, flags},
1309 * status */
1311 /** First, takes a zero byte lock at offset 10. Then:
1312 * - Taking 0 byte lock at 10 should succeed.
1313 * - Taking 1 byte locks at 9,10,11 should succeed.
1314 * - Taking 2 byte lock at 9 should fail.
1315 * - Taking 2 byte lock at 10 should succeed.
1316 * - Taking 3 byte lock at 9 should fail.
1318 {{10, 0, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1319 {{10, 0, 0, 0}, {9, 1, 0, 0}, NT_STATUS_OK},
1320 {{10, 0, 0, 0}, {10, 1, 0, 0}, NT_STATUS_OK},
1321 {{10, 0, 0, 0}, {11, 1, 0, 0}, NT_STATUS_OK},
1322 {{10, 0, 0, 0}, {9, 2, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1323 {{10, 0, 0, 0}, {10, 2, 0, 0}, NT_STATUS_OK},
1324 {{10, 0, 0, 0}, {9, 3, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1326 /** Same, but opposite order. */
1327 {{10, 0, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1328 {{9, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1329 {{10, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1330 {{11, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1331 {{9, 2, 0, 0}, {10, 0, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1332 {{10, 2, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK},
1333 {{9, 3, 0, 0}, {10, 0, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED},
1335 /** Zero zero case. */
1336 {{0, 0, 0, 0}, {0, 0, 0, 0}, NT_STATUS_OK},
1339 static bool test_zerobytelength(struct torture_context *torture,
1340 struct smb2_tree *tree)
1342 NTSTATUS status;
1343 bool ret = true;
1344 struct smb2_handle h, h2;
1345 uint8_t buf[200];
1346 struct smb2_lock lck;
1347 int i;
1349 const char *fname = BASEDIR "\\zero.txt";
1351 torture_comment(torture, "Testing zero length byte range locks:\n");
1353 status = torture_smb2_testdir(tree, BASEDIR, &h);
1354 CHECK_STATUS(status, NT_STATUS_OK);
1355 smb2_util_close(tree, h);
1357 status = torture_smb2_testfile(tree, fname, &h);
1358 CHECK_STATUS(status, NT_STATUS_OK);
1360 ZERO_STRUCT(buf);
1361 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1362 CHECK_STATUS(status, NT_STATUS_OK);
1364 status = torture_smb2_testfile(tree, fname, &h2);
1365 CHECK_STATUS(status, NT_STATUS_OK);
1367 /* Setup initial parameters */
1368 lck.in.lock_count = 0x0001;
1369 lck.in.lock_sequence = 0x00000000;
1370 lck.in.file.handle = h;
1372 /* Try every combination of locks in zero_byte_tests, using the same
1373 * handle for both locks. The first lock is assumed to succeed. The
1374 * second lock may contend, depending on the expected status. */
1375 for (i = 0; i < ARRAY_SIZE(zero_byte_tests); i++) {
1376 torture_comment(torture,
1377 " ... {%llu, %llu} + {%llu, %llu} = %s\n",
1378 (unsigned long long) zero_byte_tests[i].lock1.offset,
1379 (unsigned long long) zero_byte_tests[i].lock1.length,
1380 (unsigned long long) zero_byte_tests[i].lock2.offset,
1381 (unsigned long long) zero_byte_tests[i].lock2.length,
1382 nt_errstr(zero_byte_tests[i].status));
1384 /* Lock both locks. */
1385 lck.in.locks = &zero_byte_tests[i].lock1;
1386 lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1387 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1388 status = smb2_lock(tree, &lck);
1389 CHECK_STATUS(status, NT_STATUS_OK);
1391 lck.in.locks = &zero_byte_tests[i].lock2;
1392 lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1393 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1394 status = smb2_lock(tree, &lck);
1395 CHECK_STATUS_CONT(status, zero_byte_tests[i].status);
1397 /* Unlock both locks in reverse order. */
1398 lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1399 if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1400 status = smb2_lock(tree, &lck);
1401 CHECK_STATUS(status, NT_STATUS_OK);
1404 lck.in.locks = &zero_byte_tests[i].lock1;
1405 lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1406 status = smb2_lock(tree, &lck);
1407 CHECK_STATUS(status, NT_STATUS_OK);
1410 /* Try every combination of locks in zero_byte_tests, using two
1411 * different handles. */
1412 for (i = 0; i < ARRAY_SIZE(zero_byte_tests); i++) {
1413 torture_comment(torture,
1414 " ... {%llu, %llu} + {%llu, %llu} = %s\n",
1415 (unsigned long long) zero_byte_tests[i].lock1.offset,
1416 (unsigned long long) zero_byte_tests[i].lock1.length,
1417 (unsigned long long) zero_byte_tests[i].lock2.offset,
1418 (unsigned long long) zero_byte_tests[i].lock2.length,
1419 nt_errstr(zero_byte_tests[i].status));
1421 /* Lock both locks. */
1422 lck.in.file.handle = h;
1423 lck.in.locks = &zero_byte_tests[i].lock1;
1424 lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1425 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1426 status = smb2_lock(tree, &lck);
1427 CHECK_STATUS(status, NT_STATUS_OK);
1429 lck.in.file.handle = h2;
1430 lck.in.locks = &zero_byte_tests[i].lock2;
1431 lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1432 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1433 status = smb2_lock(tree, &lck);
1434 CHECK_STATUS_CONT(status, zero_byte_tests[i].status);
1436 /* Unlock both locks in reverse order. */
1437 lck.in.file.handle = h2;
1438 lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1439 if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
1440 status = smb2_lock(tree, &lck);
1441 CHECK_STATUS(status, NT_STATUS_OK);
1444 lck.in.file.handle = h;
1445 lck.in.locks = &zero_byte_tests[i].lock1;
1446 lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1447 status = smb2_lock(tree, &lck);
1448 CHECK_STATUS(status, NT_STATUS_OK);
1451 done:
1452 smb2_util_close(tree, h2);
1453 smb2_util_close(tree, h);
1454 smb2_deltree(tree, BASEDIR);
1455 return ret;
1458 static bool test_zerobyteread(struct torture_context *torture,
1459 struct smb2_tree *tree)
1461 NTSTATUS status;
1462 bool ret = true;
1463 struct smb2_handle h, h2;
1464 uint8_t buf[200];
1465 struct smb2_lock lck;
1466 struct smb2_lock_element el[1];
1467 struct smb2_read rd;
1469 const char *fname = BASEDIR "\\zerobyteread.txt";
1471 status = torture_smb2_testdir(tree, BASEDIR, &h);
1472 CHECK_STATUS(status, NT_STATUS_OK);
1473 smb2_util_close(tree, h);
1475 status = torture_smb2_testfile(tree, fname, &h);
1476 CHECK_STATUS(status, NT_STATUS_OK);
1478 ZERO_STRUCT(buf);
1479 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1480 CHECK_STATUS(status, NT_STATUS_OK);
1482 status = torture_smb2_testfile(tree, fname, &h2);
1483 CHECK_STATUS(status, NT_STATUS_OK);
1485 /* Setup initial parameters */
1486 lck.in.locks = el;
1487 lck.in.lock_count = 0x0001;
1488 lck.in.lock_sequence = 0x00000000;
1489 lck.in.file.handle = h;
1491 ZERO_STRUCT(rd);
1492 rd.in.file.handle = h2;
1494 torture_comment(torture, "Testing zero byte read on lock range:\n");
1496 /* Take an exclusive lock */
1497 torture_comment(torture, " taking exclusive lock.\n");
1498 el[0].offset = 0;
1499 el[0].length = 10;
1500 el[0].reserved = 0x00000000;
1501 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1502 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1503 status = smb2_lock(tree, &lck);
1504 CHECK_STATUS(status, NT_STATUS_OK);
1505 CHECK_VALUE(lck.out.reserved, 0);
1507 /* Try a zero byte read */
1508 torture_comment(torture, " reading 0 bytes.\n");
1509 rd.in.offset = 5;
1510 rd.in.length = 0;
1511 status = smb2_read(tree, tree, &rd);
1512 torture_assert_int_equal_goto(torture, rd.out.data.length, 0, ret, done,
1513 "zero byte read did not return 0 bytes");
1514 CHECK_STATUS(status, NT_STATUS_OK);
1516 /* Unlock lock */
1517 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1518 status = smb2_lock(tree, &lck);
1519 CHECK_STATUS(status, NT_STATUS_OK);
1520 CHECK_VALUE(lck.out.reserved, 0);
1522 torture_comment(torture, "Testing zero byte read on zero byte lock "
1523 "range:\n");
1525 /* Take an exclusive lock */
1526 torture_comment(torture, " taking exclusive 0-byte lock.\n");
1527 el[0].offset = 5;
1528 el[0].length = 0;
1529 el[0].reserved = 0x00000000;
1530 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1531 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1532 status = smb2_lock(tree, &lck);
1533 CHECK_STATUS(status, NT_STATUS_OK);
1534 CHECK_VALUE(lck.out.reserved, 0);
1536 /* Try a zero byte read before the lock */
1537 torture_comment(torture, " reading 0 bytes before the lock.\n");
1538 rd.in.offset = 4;
1539 rd.in.length = 0;
1540 status = smb2_read(tree, tree, &rd);
1541 torture_assert_int_equal_goto(torture, rd.out.data.length, 0, ret, done,
1542 "zero byte read did not return 0 bytes");
1543 CHECK_STATUS(status, NT_STATUS_OK);
1545 /* Try a zero byte read on the lock */
1546 torture_comment(torture, " reading 0 bytes on the lock.\n");
1547 rd.in.offset = 5;
1548 rd.in.length = 0;
1549 status = smb2_read(tree, tree, &rd);
1550 torture_assert_int_equal_goto(torture, rd.out.data.length, 0, ret, done,
1551 "zero byte read did not return 0 bytes");
1552 CHECK_STATUS(status, NT_STATUS_OK);
1554 /* Try a zero byte read after the lock */
1555 torture_comment(torture, " reading 0 bytes after the lock.\n");
1556 rd.in.offset = 6;
1557 rd.in.length = 0;
1558 status = smb2_read(tree, tree, &rd);
1559 torture_assert_int_equal_goto(torture, rd.out.data.length, 0, ret, done,
1560 "zero byte read did not return 0 bytes");
1561 CHECK_STATUS(status, NT_STATUS_OK);
1563 /* Unlock lock */
1564 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1565 status = smb2_lock(tree, &lck);
1566 CHECK_STATUS(status, NT_STATUS_OK);
1567 CHECK_VALUE(lck.out.reserved, 0);
1569 done:
1570 smb2_util_close(tree, h2);
1571 smb2_util_close(tree, h);
1572 smb2_deltree(tree, BASEDIR);
1573 return ret;
1576 static bool test_unlock(struct torture_context *torture,
1577 struct smb2_tree *tree)
1579 NTSTATUS status;
1580 bool ret = true;
1581 struct smb2_handle h, h2;
1582 uint8_t buf[200];
1583 struct smb2_lock lck;
1584 struct smb2_lock_element el1[1];
1585 struct smb2_lock_element el2[1];
1587 const char *fname = BASEDIR "\\unlock.txt";
1589 status = torture_smb2_testdir(tree, BASEDIR, &h);
1590 CHECK_STATUS(status, NT_STATUS_OK);
1591 smb2_util_close(tree, h);
1593 status = torture_smb2_testfile(tree, fname, &h);
1594 CHECK_STATUS(status, NT_STATUS_OK);
1596 ZERO_STRUCT(buf);
1597 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1598 CHECK_STATUS(status, NT_STATUS_OK);
1600 status = torture_smb2_testfile(tree, fname, &h2);
1601 CHECK_STATUS(status, NT_STATUS_OK);
1603 /* Setup initial parameters */
1604 lck.in.locks = el1;
1605 lck.in.lock_count = 0x0001;
1606 lck.in.lock_sequence = 0x00000000;
1607 el1[0].offset = 0;
1608 el1[0].length = 10;
1609 el1[0].reserved = 0x00000000;
1611 /* Take exclusive lock, then unlock it with a shared-unlock call. */
1613 torture_comment(torture, "Testing unlock exclusive with shared\n");
1615 torture_comment(torture, " taking exclusive lock.\n");
1616 lck.in.file.handle = h;
1617 el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1618 status = smb2_lock(tree, &lck);
1619 CHECK_STATUS(status, NT_STATUS_OK);
1621 torture_comment(torture, " try to unlock the exclusive with a shared "
1622 "unlock call.\n");
1623 el1[0].flags = SMB2_LOCK_FLAG_SHARED |
1624 SMB2_LOCK_FLAG_UNLOCK;
1625 status = smb2_lock(tree, &lck);
1626 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1628 torture_comment(torture, " try shared lock on h2, to test the "
1629 "unlock.\n");
1630 lck.in.file.handle = h2;
1631 el1[0].flags = SMB2_LOCK_FLAG_SHARED |
1632 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1633 status = smb2_lock(tree, &lck);
1634 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1636 torture_comment(torture, " unlock the exclusive lock\n");
1637 lck.in.file.handle = h;
1638 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1639 status = smb2_lock(tree, &lck);
1640 CHECK_STATUS(status, NT_STATUS_OK);
1642 /* Unlock a shared lock with an exclusive-unlock call. */
1644 torture_comment(torture, "Testing unlock shared with exclusive\n");
1646 torture_comment(torture, " taking shared lock.\n");
1647 lck.in.file.handle = h;
1648 el1[0].flags = SMB2_LOCK_FLAG_SHARED;
1649 status = smb2_lock(tree, &lck);
1650 CHECK_STATUS(status, NT_STATUS_OK);
1652 torture_comment(torture, " try to unlock the shared with an exclusive "
1653 "unlock call.\n");
1654 el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1655 SMB2_LOCK_FLAG_UNLOCK;
1656 status = smb2_lock(tree, &lck);
1657 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1659 torture_comment(torture, " try exclusive lock on h2, to test the "
1660 "unlock.\n");
1661 lck.in.file.handle = h2;
1662 el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1663 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1664 status = smb2_lock(tree, &lck);
1665 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1667 torture_comment(torture, " unlock the exclusive lock\n");
1668 lck.in.file.handle = h;
1669 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1670 status = smb2_lock(tree, &lck);
1671 CHECK_STATUS(status, NT_STATUS_OK);
1673 /* Test unlocking of stacked 0-byte locks. SMB2 0-byte lock behavior
1674 * should be the same as >0-byte behavior. Exclusive locks should be
1675 * unlocked before shared. */
1677 torture_comment(torture, "Test unlocking stacked 0-byte locks\n");
1679 lck.in.locks = el1;
1680 lck.in.file.handle = h;
1681 el1[0].offset = 10;
1682 el1[0].length = 0;
1683 el1[0].reserved = 0x00000000;
1684 el2[0].offset = 5;
1685 el2[0].length = 10;
1686 el2[0].reserved = 0x00000000;
1688 /* lock 0-byte exclusive */
1689 el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1690 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1691 status = smb2_lock(tree, &lck);
1692 CHECK_STATUS(status, NT_STATUS_OK);
1694 /* lock 0-byte shared */
1695 el1[0].flags = SMB2_LOCK_FLAG_SHARED |
1696 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1697 status = smb2_lock(tree, &lck);
1698 CHECK_STATUS(status, NT_STATUS_OK);
1700 /* test contention */
1701 lck.in.locks = el2;
1702 lck.in.file.handle = h2;
1703 el2[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1704 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1705 status = smb2_lock(tree, &lck);
1706 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1708 lck.in.locks = el2;
1709 lck.in.file.handle = h2;
1710 el2[0].flags = SMB2_LOCK_FLAG_SHARED |
1711 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1712 status = smb2_lock(tree, &lck);
1713 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1715 /* unlock */
1716 lck.in.locks = el1;
1717 lck.in.file.handle = h;
1718 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1719 status = smb2_lock(tree, &lck);
1720 CHECK_STATUS(status, NT_STATUS_OK);
1722 /* test - can we take a shared lock? */
1723 lck.in.locks = el2;
1724 lck.in.file.handle = h2;
1725 el2[0].flags = SMB2_LOCK_FLAG_SHARED |
1726 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1727 status = smb2_lock(tree, &lck);
1728 CHECK_STATUS(status, NT_STATUS_OK);
1730 el2[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1731 status = smb2_lock(tree, &lck);
1732 CHECK_STATUS(status, NT_STATUS_OK);
1734 /* cleanup */
1735 lck.in.locks = el1;
1736 lck.in.file.handle = h;
1737 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1738 status = smb2_lock(tree, &lck);
1739 CHECK_STATUS(status, NT_STATUS_OK);
1741 /* Test unlocking of stacked exclusive, shared locks. Exclusive
1742 * should be unlocked before any shared. */
1744 torture_comment(torture, "Test unlocking stacked exclusive/shared "
1745 "locks\n");
1747 lck.in.locks = el1;
1748 lck.in.file.handle = h;
1749 el1[0].offset = 10;
1750 el1[0].length = 10;
1751 el1[0].reserved = 0x00000000;
1752 el2[0].offset = 5;
1753 el2[0].length = 10;
1754 el2[0].reserved = 0x00000000;
1756 /* lock exclusive */
1757 el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1758 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1759 status = smb2_lock(tree, &lck);
1760 CHECK_STATUS(status, NT_STATUS_OK);
1762 /* lock shared */
1763 el1[0].flags = SMB2_LOCK_FLAG_SHARED |
1764 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1765 status = smb2_lock(tree, &lck);
1766 CHECK_STATUS(status, NT_STATUS_OK);
1768 /* test contention */
1769 lck.in.locks = el2;
1770 lck.in.file.handle = h2;
1771 el2[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1772 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1773 status = smb2_lock(tree, &lck);
1774 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1776 lck.in.locks = el2;
1777 lck.in.file.handle = h2;
1778 el2[0].flags = SMB2_LOCK_FLAG_SHARED |
1779 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1780 status = smb2_lock(tree, &lck);
1781 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1783 /* unlock */
1784 lck.in.locks = el1;
1785 lck.in.file.handle = h;
1786 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1787 status = smb2_lock(tree, &lck);
1788 CHECK_STATUS(status, NT_STATUS_OK);
1790 /* test - can we take a shared lock? */
1791 lck.in.locks = el2;
1792 lck.in.file.handle = h2;
1793 el2[0].flags = SMB2_LOCK_FLAG_SHARED |
1794 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1795 status = smb2_lock(tree, &lck);
1796 CHECK_STATUS(status, NT_STATUS_OK);
1798 el2[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1799 status = smb2_lock(tree, &lck);
1800 CHECK_STATUS(status, NT_STATUS_OK);
1802 /* cleanup */
1803 lck.in.locks = el1;
1804 lck.in.file.handle = h;
1805 el1[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1806 status = smb2_lock(tree, &lck);
1807 CHECK_STATUS(status, NT_STATUS_OK);
1809 done:
1810 smb2_util_close(tree, h2);
1811 smb2_util_close(tree, h);
1812 smb2_deltree(tree, BASEDIR);
1813 return ret;
1816 static bool test_multiple_unlock(struct torture_context *torture,
1817 struct smb2_tree *tree)
1819 NTSTATUS status;
1820 bool ret = true;
1821 struct smb2_handle h;
1822 uint8_t buf[200];
1823 struct smb2_lock lck;
1824 struct smb2_lock_element el[2];
1826 const char *fname = BASEDIR "\\unlock_multiple.txt";
1828 status = torture_smb2_testdir(tree, BASEDIR, &h);
1829 CHECK_STATUS(status, NT_STATUS_OK);
1830 smb2_util_close(tree, h);
1832 status = torture_smb2_testfile(tree, fname, &h);
1833 CHECK_STATUS(status, NT_STATUS_OK);
1835 ZERO_STRUCT(buf);
1836 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
1837 CHECK_STATUS(status, NT_STATUS_OK);
1839 torture_comment(torture, "Testing multiple unlocks:\n");
1841 /* Setup initial parameters */
1842 lck.in.lock_count = 0x0002;
1843 lck.in.lock_sequence = 0x00000000;
1844 lck.in.file.handle = h;
1845 el[0].offset = 0;
1846 el[0].length = 10;
1847 el[0].reserved = 0x00000000;
1848 el[1].offset = 10;
1849 el[1].length = 10;
1850 el[1].reserved = 0x00000000;
1852 /* Test1: Acquire second lock, but not first. */
1853 torture_comment(torture, " unlock 2 locks, first one not locked. "
1854 "Expect no locks unlocked. \n");
1856 lck.in.lock_count = 0x0001;
1857 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1858 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1859 lck.in.locks = &el[1];
1860 status = smb2_lock(tree, &lck);
1861 CHECK_STATUS(status, NT_STATUS_OK);
1863 /* Try to unlock both locks */
1864 lck.in.lock_count = 0x0002;
1865 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1866 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
1867 lck.in.locks = el;
1868 status = smb2_lock(tree, &lck);
1869 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1871 /* Second lock should not be unlocked. */
1872 lck.in.lock_count = 0x0001;
1873 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1874 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1875 lck.in.locks = &el[1];
1876 status = smb2_lock(tree, &lck);
1877 if (TARGET_IS_W2K8(torture)) {
1878 CHECK_STATUS(status, NT_STATUS_OK);
1879 torture_warning(torture, "Target has \"pretty please\" bug. "
1880 "A contending lock request on the same handle "
1881 "unlocks the lock.\n");
1882 } else {
1883 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1886 /* cleanup */
1887 lck.in.lock_count = 0x0001;
1888 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
1889 lck.in.locks = &el[1];
1890 status = smb2_lock(tree, &lck);
1891 CHECK_STATUS(status, NT_STATUS_OK);
1893 /* Test2: Acquire first lock, but not second. */
1894 torture_comment(torture, " unlock 2 locks, second one not locked. "
1895 "Expect first lock unlocked.\n");
1897 lck.in.lock_count = 0x0001;
1898 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1899 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1900 lck.in.locks = &el[0];
1901 status = smb2_lock(tree, &lck);
1902 CHECK_STATUS(status, NT_STATUS_OK);
1904 /* Try to unlock both locks */
1905 lck.in.lock_count = 0x0002;
1906 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1907 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
1908 lck.in.locks = el;
1909 status = smb2_lock(tree, &lck);
1910 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
1912 /* First lock should be unlocked. */
1913 lck.in.lock_count = 0x0001;
1914 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1915 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1916 lck.in.locks = &el[0];
1917 status = smb2_lock(tree, &lck);
1918 CHECK_STATUS(status, NT_STATUS_OK);
1920 /* cleanup */
1921 lck.in.lock_count = 0x0001;
1922 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1923 lck.in.locks = &el[0];
1924 status = smb2_lock(tree, &lck);
1925 CHECK_STATUS(status, NT_STATUS_OK);
1927 /* Test3: Request 2 locks, second will contend. What happens to the
1928 * first? */
1929 torture_comment(torture, " request 2 locks, second one will contend. "
1930 "Expect both to fail.\n");
1932 /* Lock the second range */
1933 lck.in.lock_count = 0x0001;
1934 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1935 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1936 lck.in.locks = &el[1];
1937 status = smb2_lock(tree, &lck);
1938 CHECK_STATUS(status, NT_STATUS_OK);
1940 /* Request both locks */
1941 lck.in.lock_count = 0x0002;
1942 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1943 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1944 lck.in.locks = el;
1945 status = smb2_lock(tree, &lck);
1946 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
1948 /* First lock should be unlocked. */
1949 lck.in.lock_count = 0x0001;
1950 lck.in.locks = &el[0];
1951 status = smb2_lock(tree, &lck);
1952 CHECK_STATUS(status, NT_STATUS_OK);
1954 /* cleanup */
1955 if (TARGET_IS_W2K8(torture)) {
1956 lck.in.lock_count = 0x0001;
1957 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1958 lck.in.locks = &el[0];
1959 status = smb2_lock(tree, &lck);
1960 CHECK_STATUS(status, NT_STATUS_OK);
1961 torture_warning(torture, "Target has \"pretty please\" bug. "
1962 "A contending lock request on the same handle "
1963 "unlocks the lock.\n");
1964 } else {
1965 lck.in.lock_count = 0x0002;
1966 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1967 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
1968 lck.in.locks = el;
1969 status = smb2_lock(tree, &lck);
1970 CHECK_STATUS(status, NT_STATUS_OK);
1973 /* Test4: Request unlock and lock. The lock contends, is the unlock
1974 * then relocked? SMB2 doesn't like the lock and unlock requests in the
1975 * same packet. The unlock will succeed, but the lock will return
1976 * INVALID_PARAMETER. This behavior is described in MS-SMB2
1977 * 3.3.5.14.1 */
1978 torture_comment(torture, " request unlock and lock, second one will "
1979 "error. Expect the unlock to succeed.\n");
1981 /* Lock both ranges */
1982 lck.in.lock_count = 0x0002;
1983 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1984 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1985 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1986 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1987 lck.in.locks = el;
1988 status = smb2_lock(tree, &lck);
1989 CHECK_STATUS(status, NT_STATUS_OK);
1991 /* Attempt to unlock the first range and lock the second. The lock
1992 * request will error. */
1993 lck.in.lock_count = 0x0002;
1994 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1995 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
1996 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
1997 lck.in.locks = el;
1998 status = smb2_lock(tree, &lck);
1999 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
2001 /* The first lock should've been unlocked */
2002 lck.in.lock_count = 0x0001;
2003 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2004 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2005 lck.in.locks = &el[0];
2006 status = smb2_lock(tree, &lck);
2007 CHECK_STATUS(status, NT_STATUS_OK);
2009 /* cleanup */
2010 lck.in.lock_count = 0x0002;
2011 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2012 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
2013 lck.in.locks = el;
2014 status = smb2_lock(tree, &lck);
2015 CHECK_STATUS(status, NT_STATUS_OK);
2017 /* Test10: SMB2 only test. Request unlock and lock in same packet.
2018 * Neither contend. SMB2 doesn't like lock and unlock requests in the
2019 * same packet. The unlock will succeed, but the lock will return
2020 * INVALID_PARAMETER. */
2021 torture_comment(torture, " request unlock and lock. Unlock will "
2022 "succeed, but lock will fail.\n");
2024 /* Lock first range */
2025 lck.in.lock_count = 0x0001;
2026 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2027 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2028 lck.in.locks = &el[0];
2029 status = smb2_lock(tree, &lck);
2030 CHECK_STATUS(status, NT_STATUS_OK);
2032 /* Attempt to unlock the first range and lock the second */
2033 lck.in.lock_count = 0x0002;
2034 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2035 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2036 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2037 lck.in.locks = el;
2038 status = smb2_lock(tree, &lck);
2039 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
2041 /* Neither lock should still be locked */
2042 lck.in.lock_count = 0x0002;
2043 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2044 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2045 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2046 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2047 lck.in.locks = el;
2048 status = smb2_lock(tree, &lck);
2049 CHECK_STATUS(status, NT_STATUS_OK);
2051 /* cleanup */
2052 lck.in.lock_count = 0x0002;
2053 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2054 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
2055 lck.in.locks = el;
2056 status = smb2_lock(tree, &lck);
2057 CHECK_STATUS(status, NT_STATUS_OK);
2059 /* Test11: SMB2 only test. Request lock and unlock in same packet.
2060 * Neither contend. SMB2 doesn't like lock and unlock requests in the
2061 * same packet. The lock will succeed, the unlock will fail with
2062 * INVALID_PARAMETER, and the lock will be unlocked before return. */
2063 torture_comment(torture, " request lock and unlock. Both will "
2064 "fail.\n");
2066 /* Lock second range */
2067 lck.in.lock_count = 0x0001;
2068 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2069 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2070 lck.in.locks = &el[1];
2071 status = smb2_lock(tree, &lck);
2072 CHECK_STATUS(status, NT_STATUS_OK);
2074 /* Attempt to lock the first range and unlock the second */
2075 lck.in.lock_count = 0x0002;
2076 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2077 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2078 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
2079 lck.in.locks = el;
2080 status = smb2_lock(tree, &lck);
2081 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
2083 /* First range should be unlocked, second locked. */
2084 lck.in.lock_count = 0x0001;
2085 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2086 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2087 lck.in.locks = &el[0];
2088 status = smb2_lock(tree, &lck);
2089 CHECK_STATUS(status, NT_STATUS_OK);
2091 lck.in.lock_count = 0x0001;
2092 el[1].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2093 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2094 lck.in.locks = &el[1];
2095 status = smb2_lock(tree, &lck);
2096 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2098 /* cleanup */
2099 if (TARGET_IS_W2K8(torture)) {
2100 lck.in.lock_count = 0x0001;
2101 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2102 lck.in.locks = &el[0];
2103 status = smb2_lock(tree, &lck);
2104 CHECK_STATUS(status, NT_STATUS_OK);
2105 torture_warning(torture, "Target has \"pretty please\" bug. "
2106 "A contending lock request on the same handle "
2107 "unlocks the lock.\n");
2108 } else {
2109 lck.in.lock_count = 0x0002;
2110 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2111 el[1].flags = SMB2_LOCK_FLAG_UNLOCK;
2112 lck.in.locks = el;
2113 status = smb2_lock(tree, &lck);
2114 CHECK_STATUS(status, NT_STATUS_OK);
2117 done:
2118 smb2_util_close(tree, h);
2119 smb2_deltree(tree, BASEDIR);
2120 return ret;
2124 * Test lock stacking
2125 * - some tests ported from BASE-LOCK-LOCK5
2127 static bool test_stacking(struct torture_context *torture,
2128 struct smb2_tree *tree)
2130 NTSTATUS status;
2131 bool ret = true;
2132 struct smb2_handle h, h2;
2133 uint8_t buf[200];
2134 struct smb2_lock lck;
2135 struct smb2_lock_element el[1];
2137 const char *fname = BASEDIR "\\stacking.txt";
2139 status = torture_smb2_testdir(tree, BASEDIR, &h);
2140 CHECK_STATUS(status, NT_STATUS_OK);
2141 smb2_util_close(tree, h);
2143 status = torture_smb2_testfile(tree, fname, &h);
2144 CHECK_STATUS(status, NT_STATUS_OK);
2146 ZERO_STRUCT(buf);
2147 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2148 CHECK_STATUS(status, NT_STATUS_OK);
2150 status = torture_smb2_testfile(tree, fname, &h2);
2151 CHECK_STATUS(status, NT_STATUS_OK);
2153 torture_comment(torture, "Testing lock stacking:\n");
2155 /* Setup initial parameters */
2156 lck.in.locks = el;
2157 lck.in.lock_count = 0x0001;
2158 lck.in.lock_sequence = 0x00000000;
2159 lck.in.file.handle = h;
2160 el[0].offset = 0;
2161 el[0].length = 10;
2162 el[0].reserved = 0x00000000;
2164 /* Try to take a shared lock, then a shared lock on same handle */
2165 torture_comment(torture, " stacking a shared on top of a shared"
2166 "lock succeeds.\n");
2168 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2169 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2170 status = smb2_lock(tree, &lck);
2171 CHECK_STATUS(status, NT_STATUS_OK);
2173 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2174 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2175 status = smb2_lock(tree, &lck);
2176 CHECK_STATUS(status, NT_STATUS_OK);
2178 /* cleanup */
2179 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2180 status = smb2_lock(tree, &lck);
2181 CHECK_STATUS(status, NT_STATUS_OK);
2183 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2184 status = smb2_lock(tree, &lck);
2185 CHECK_STATUS(status, NT_STATUS_OK);
2188 /* Try to take an exclusive lock, then a shared lock on same handle */
2189 torture_comment(torture, " stacking a shared on top of an exclusive "
2190 "lock succeeds.\n");
2192 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2193 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2194 status = smb2_lock(tree, &lck);
2195 CHECK_STATUS(status, NT_STATUS_OK);
2197 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2198 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2199 status = smb2_lock(tree, &lck);
2200 CHECK_STATUS(status, NT_STATUS_OK);
2202 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2203 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2204 status = smb2_lock(tree, &lck);
2205 CHECK_STATUS(status, NT_STATUS_OK);
2207 /* stacking a shared from a different handle should fail */
2208 lck.in.file.handle = h2;
2209 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2210 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2211 status = smb2_lock(tree, &lck);
2212 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2214 /* cleanup */
2215 lck.in.file.handle = h;
2216 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2217 status = smb2_lock(tree, &lck);
2218 CHECK_STATUS(status, NT_STATUS_OK);
2220 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2221 status = smb2_lock(tree, &lck);
2222 CHECK_STATUS(status, NT_STATUS_OK);
2224 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2225 status = smb2_lock(tree, &lck);
2226 CHECK_STATUS(status, NT_STATUS_OK);
2228 /* ensure the 4th unlock fails */
2229 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2230 status = smb2_lock(tree, &lck);
2231 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
2233 /* ensure a second handle can now take an exclusive lock */
2234 lck.in.file.handle = h2;
2235 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2236 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2237 status = smb2_lock(tree, &lck);
2238 CHECK_STATUS(status, NT_STATUS_OK);
2240 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2241 status = smb2_lock(tree, &lck);
2242 CHECK_STATUS(status, NT_STATUS_OK);
2244 /* Try to take an exclusive lock, then a shared lock on a
2245 * different handle */
2246 torture_comment(torture, " stacking a shared on top of an exclusive "
2247 "lock with different handles fails.\n");
2249 lck.in.file.handle = h;
2250 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2251 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2252 status = smb2_lock(tree, &lck);
2253 CHECK_STATUS(status, NT_STATUS_OK);
2255 lck.in.file.handle = h2;
2256 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2257 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2258 status = smb2_lock(tree, &lck);
2259 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2261 /* cleanup */
2262 lck.in.file.handle = h;
2263 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2264 status = smb2_lock(tree, &lck);
2265 CHECK_STATUS(status, NT_STATUS_OK);
2267 /* Try to take a shared lock, then stack an exclusive with same
2268 * handle. */
2269 torture_comment(torture, " stacking an exclusive on top of a shared "
2270 "lock fails.\n");
2272 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2273 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2274 status = smb2_lock(tree, &lck);
2275 CHECK_STATUS(status, NT_STATUS_OK);
2277 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2278 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2279 status = smb2_lock(tree, &lck);
2280 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2282 /* cleanup */
2283 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2284 status = smb2_lock(tree, &lck);
2285 if (TARGET_IS_W2K8(torture)) {
2286 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
2287 torture_warning(torture, "Target has \"pretty please\" bug. "
2288 "A contending lock request on the same handle "
2289 "unlocks the lock.\n");
2290 } else {
2291 CHECK_STATUS(status, NT_STATUS_OK);
2294 /* Prove that two exclusive locks do not stack on the same handle. */
2295 torture_comment(torture, " two exclusive locks do not stack.\n");
2297 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2298 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2299 status = smb2_lock(tree, &lck);
2300 CHECK_STATUS(status, NT_STATUS_OK);
2302 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2303 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2304 status = smb2_lock(tree, &lck);
2305 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2307 /* cleanup */
2308 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2309 status = smb2_lock(tree, &lck);
2310 if (TARGET_IS_W2K8(torture)) {
2311 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
2312 torture_warning(torture, "Target has \"pretty please\" bug. "
2313 "A contending lock request on the same handle "
2314 "unlocks the lock.\n");
2315 } else {
2316 CHECK_STATUS(status, NT_STATUS_OK);
2319 done:
2320 smb2_util_close(tree, h2);
2321 smb2_util_close(tree, h);
2322 smb2_deltree(tree, BASEDIR);
2323 return ret;
2327 * Test lock contention
2328 * - shared lock should contend with exclusive lock on different handle
2330 static bool test_contend(struct torture_context *torture,
2331 struct smb2_tree *tree)
2333 NTSTATUS status;
2334 bool ret = true;
2335 struct smb2_handle h, h2;
2336 uint8_t buf[200];
2337 struct smb2_lock lck;
2338 struct smb2_lock_element el[1];
2340 const char *fname = BASEDIR "\\contend.txt";
2342 status = torture_smb2_testdir(tree, BASEDIR, &h);
2343 CHECK_STATUS(status, NT_STATUS_OK);
2344 smb2_util_close(tree, h);
2346 status = torture_smb2_testfile(tree, fname, &h);
2347 CHECK_STATUS(status, NT_STATUS_OK);
2349 ZERO_STRUCT(buf);
2350 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2351 CHECK_STATUS(status, NT_STATUS_OK);
2353 status = torture_smb2_testfile(tree, fname, &h2);
2354 CHECK_STATUS(status, NT_STATUS_OK);
2356 torture_comment(torture, "Testing lock contention:\n");
2358 /* Setup initial parameters */
2359 lck.in.locks = el;
2360 lck.in.lock_count = 0x0001;
2361 lck.in.lock_sequence = 0x00000000;
2362 lck.in.file.handle = h;
2363 el[0].offset = 0;
2364 el[0].length = 10;
2365 el[0].reserved = 0x00000000;
2367 /* Take an exclusive lock, then a shared lock on different handle */
2368 torture_comment(torture, " shared should contend on exclusive on "
2369 "different handle.\n");
2371 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2372 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2373 status = smb2_lock(tree, &lck);
2374 CHECK_STATUS(status, NT_STATUS_OK);
2376 lck.in.file.handle = h2;
2377 el[0].flags = SMB2_LOCK_FLAG_SHARED |
2378 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2379 status = smb2_lock(tree, &lck);
2380 CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
2382 /* cleanup */
2383 lck.in.file.handle = h;
2384 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2385 status = smb2_lock(tree, &lck);
2386 CHECK_STATUS(status, NT_STATUS_OK);
2388 done:
2389 smb2_util_close(tree, h2);
2390 smb2_util_close(tree, h);
2391 smb2_deltree(tree, BASEDIR);
2392 return ret;
2396 * Test locker context
2397 * - test that pid does not affect the locker context
2399 static bool test_context(struct torture_context *torture,
2400 struct smb2_tree *tree)
2402 NTSTATUS status;
2403 bool ret = true;
2404 struct smb2_handle h, h2;
2405 uint8_t buf[200];
2406 struct smb2_lock lck;
2407 struct smb2_lock_element el[1];
2409 const char *fname = BASEDIR "\\context.txt";
2411 status = torture_smb2_testdir(tree, BASEDIR, &h);
2412 CHECK_STATUS(status, NT_STATUS_OK);
2413 smb2_util_close(tree, h);
2415 status = torture_smb2_testfile(tree, fname, &h);
2416 CHECK_STATUS(status, NT_STATUS_OK);
2418 ZERO_STRUCT(buf);
2419 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2420 CHECK_STATUS(status, NT_STATUS_OK);
2422 status = torture_smb2_testfile(tree, fname, &h2);
2423 CHECK_STATUS(status, NT_STATUS_OK);
2425 torture_comment(torture, "Testing locker context:\n");
2427 /* Setup initial parameters */
2428 lck.in.locks = el;
2429 lck.in.lock_count = 0x0001;
2430 lck.in.lock_sequence = 0x00000000;
2431 lck.in.file.handle = h;
2432 el[0].offset = 0;
2433 el[0].length = 10;
2434 el[0].reserved = 0x00000000;
2436 /* Take an exclusive lock, then try to unlock with a different pid,
2437 * same handle. This shows that the pid doesn't affect the locker
2438 * context in SMB2. */
2439 torture_comment(torture, " pid shouldn't affect locker context\n");
2441 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2442 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2443 status = smb2_lock(tree, &lck);
2444 CHECK_STATUS(status, NT_STATUS_OK);
2446 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2447 status = smb2_lock(tree, &lck);
2448 CHECK_STATUS(status, NT_STATUS_OK);
2450 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2451 status = smb2_lock(tree, &lck);
2452 CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED);
2454 done:
2455 smb2_util_close(tree, h2);
2456 smb2_util_close(tree, h);
2457 smb2_deltree(tree, BASEDIR);
2458 return ret;
2462 * Test as much of the potential lock range as possible
2463 * - test ported from BASE-LOCK-LOCK3
2465 static bool test_range(struct torture_context *torture,
2466 struct smb2_tree *tree)
2468 NTSTATUS status;
2469 bool ret = true;
2470 struct smb2_handle h, h2;
2471 uint8_t buf[200];
2472 struct smb2_lock lck;
2473 struct smb2_lock_element el[1];
2474 uint64_t offset, i;
2475 extern int torture_numops;
2477 const char *fname = BASEDIR "\\range.txt";
2479 #define NEXT_OFFSET offset += (~(uint64_t)0) / torture_numops
2481 status = torture_smb2_testdir(tree, BASEDIR, &h);
2482 CHECK_STATUS(status, NT_STATUS_OK);
2483 smb2_util_close(tree, h);
2485 status = torture_smb2_testfile(tree, fname, &h);
2486 CHECK_STATUS(status, NT_STATUS_OK);
2488 ZERO_STRUCT(buf);
2489 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2490 CHECK_STATUS(status, NT_STATUS_OK);
2492 status = torture_smb2_testfile(tree, fname, &h2);
2493 CHECK_STATUS(status, NT_STATUS_OK);
2495 torture_comment(torture, "Testing locks spread across the 64-bit "
2496 "offset range\n");
2498 if (TARGET_IS_W2K8(torture)) {
2499 torture_result(torture, TORTURE_SKIP,
2500 "Target has \"pretty please\" bug. A contending lock "
2501 "request on the same handle unlocks the lock.");
2502 goto done;
2505 /* Setup initial parameters */
2506 lck.in.locks = el;
2507 lck.in.lock_count = 0x0001;
2508 lck.in.lock_sequence = 0x00000000;
2509 lck.in.file.handle = h;
2510 el[0].offset = 0;
2511 el[0].length = 1;
2512 el[0].reserved = 0x00000000;
2513 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2514 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2516 torture_comment(torture, " establishing %d locks\n", torture_numops);
2518 for (offset=i=0; i<torture_numops; i++) {
2519 NEXT_OFFSET;
2521 lck.in.file.handle = h;
2522 el[0].offset = offset - 1;
2523 status = smb2_lock(tree, &lck);
2524 CHECK_STATUS_CMT(status, NT_STATUS_OK,
2525 talloc_asprintf(torture,
2526 "lock h failed at offset %#llx ",
2527 (unsigned long long) el[0].offset));
2529 lck.in.file.handle = h2;
2530 el[0].offset = offset - 2;
2531 status = smb2_lock(tree, &lck);
2532 CHECK_STATUS_CMT(status, NT_STATUS_OK,
2533 talloc_asprintf(torture,
2534 "lock h2 failed at offset %#llx ",
2535 (unsigned long long) el[0].offset));
2538 torture_comment(torture, " testing %d locks\n", torture_numops);
2540 for (offset=i=0; i<torture_numops; i++) {
2541 NEXT_OFFSET;
2543 lck.in.file.handle = h;
2544 el[0].offset = offset - 1;
2545 status = smb2_lock(tree, &lck);
2546 CHECK_STATUS_CMT(status, NT_STATUS_LOCK_NOT_GRANTED,
2547 talloc_asprintf(torture,
2548 "lock h at offset %#llx should not have "
2549 "succeeded ",
2550 (unsigned long long) el[0].offset));
2552 lck.in.file.handle = h;
2553 el[0].offset = offset - 2;
2554 status = smb2_lock(tree, &lck);
2555 CHECK_STATUS_CMT(status, NT_STATUS_LOCK_NOT_GRANTED,
2556 talloc_asprintf(torture,
2557 "lock h2 at offset %#llx should not have "
2558 "succeeded ",
2559 (unsigned long long) el[0].offset));
2561 lck.in.file.handle = h2;
2562 el[0].offset = offset - 1;
2563 status = smb2_lock(tree, &lck);
2564 CHECK_STATUS_CMT(status, NT_STATUS_LOCK_NOT_GRANTED,
2565 talloc_asprintf(torture,
2566 "lock h at offset %#llx should not have "
2567 "succeeded ",
2568 (unsigned long long) el[0].offset));
2570 lck.in.file.handle = h2;
2571 el[0].offset = offset - 2;
2572 status = smb2_lock(tree, &lck);
2573 CHECK_STATUS_CMT(status, NT_STATUS_LOCK_NOT_GRANTED,
2574 talloc_asprintf(torture,
2575 "lock h2 at offset %#llx should not have "
2576 "succeeded ",
2577 (unsigned long long) el[0].offset));
2580 torture_comment(torture, " removing %d locks\n", torture_numops);
2582 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2584 for (offset=i=0; i<torture_numops; i++) {
2585 NEXT_OFFSET;
2587 lck.in.file.handle = h;
2588 el[0].offset = offset - 1;
2589 status = smb2_lock(tree, &lck);
2590 CHECK_STATUS_CMT(status, NT_STATUS_OK,
2591 talloc_asprintf(torture,
2592 "unlock from h failed at offset %#llx ",
2593 (unsigned long long) el[0].offset));
2595 lck.in.file.handle = h2;
2596 el[0].offset = offset - 2;
2597 status = smb2_lock(tree, &lck);
2598 CHECK_STATUS_CMT(status, NT_STATUS_OK,
2599 talloc_asprintf(torture,
2600 "unlock from h2 failed at offset %#llx ",
2601 (unsigned long long) el[0].offset));
2604 done:
2605 smb2_util_close(tree, h2);
2606 smb2_util_close(tree, h);
2607 smb2_deltree(tree, BASEDIR);
2608 return ret;
2611 static NTSTATUS test_smb2_lock(struct smb2_tree *tree, struct smb2_handle h,
2612 uint64_t offset, uint64_t length, bool exclusive)
2614 struct smb2_lock lck;
2615 struct smb2_lock_element el[1];
2616 NTSTATUS status;
2618 lck.in.locks = el;
2619 lck.in.lock_count = 0x0001;
2620 lck.in.lock_sequence = 0x00000000;
2621 lck.in.file.handle = h;
2622 el[0].offset = offset;
2623 el[0].length = length;
2624 el[0].reserved = 0x00000000;
2625 el[0].flags = (exclusive ?
2626 SMB2_LOCK_FLAG_EXCLUSIVE :
2627 SMB2_LOCK_FLAG_SHARED) |
2628 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2630 status = smb2_lock(tree, &lck);
2632 return status;
2635 static NTSTATUS test_smb2_unlock(struct smb2_tree *tree, struct smb2_handle h,
2636 uint64_t offset, uint64_t length)
2638 struct smb2_lock lck;
2639 struct smb2_lock_element el[1];
2640 NTSTATUS status;
2642 lck.in.locks = el;
2643 lck.in.lock_count = 0x0001;
2644 lck.in.lock_sequence = 0x00000000;
2645 lck.in.file.handle = h;
2646 el[0].offset = offset;
2647 el[0].length = length;
2648 el[0].reserved = 0x00000000;
2649 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2651 status = smb2_lock(tree, &lck);
2653 return status;
2656 #define EXPECTED(ret, v) if ((ret) != (v)) { \
2657 torture_result(torture, TORTURE_FAIL, __location__": subtest failed");\
2658 torture_comment(torture, "** "); correct = false; \
2662 * Test overlapping lock ranges from various lockers
2663 * - some tests ported from BASE-LOCK-LOCK4
2665 static bool test_overlap(struct torture_context *torture,
2666 struct smb2_tree *tree,
2667 struct smb2_tree *tree2)
2669 NTSTATUS status;
2670 bool ret = true;
2671 struct smb2_handle h, h2, h3;
2672 uint8_t buf[200];
2673 bool correct = true;
2675 const char *fname = BASEDIR "\\overlap.txt";
2677 status = torture_smb2_testdir(tree, BASEDIR, &h);
2678 CHECK_STATUS(status, NT_STATUS_OK);
2679 smb2_util_close(tree, h);
2681 status = torture_smb2_testfile(tree, fname, &h);
2682 CHECK_STATUS(status, NT_STATUS_OK);
2684 ZERO_STRUCT(buf);
2685 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2686 CHECK_STATUS(status, NT_STATUS_OK);
2688 status = torture_smb2_testfile(tree, fname, &h2);
2689 CHECK_STATUS(status, NT_STATUS_OK);
2691 status = torture_smb2_testfile(tree2, fname, &h3);
2692 CHECK_STATUS(status, NT_STATUS_OK);
2694 torture_comment(torture, "Testing overlapping locks:\n");
2696 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 0, 4, true)) &&
2697 NT_STATUS_IS_OK(test_smb2_lock(tree, h, 2, 4, true));
2698 EXPECTED(ret, false);
2699 torture_comment(torture, "the same session/handle %s set overlapping "
2700 "exclusive locks\n", ret?"can":"cannot");
2702 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 10, 4, false)) &&
2703 NT_STATUS_IS_OK(test_smb2_lock(tree, h, 12, 4, false));
2704 EXPECTED(ret, true);
2705 torture_comment(torture, "the same session/handle %s set overlapping "
2706 "shared locks\n", ret?"can":"cannot");
2708 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 20, 4, true)) &&
2709 NT_STATUS_IS_OK(test_smb2_lock(tree2, h3, 22, 4, true));
2710 EXPECTED(ret, false);
2711 torture_comment(torture, "a different session %s set overlapping "
2712 "exclusive locks\n", ret?"can":"cannot");
2714 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 30, 4, false)) &&
2715 NT_STATUS_IS_OK(test_smb2_lock(tree2, h3, 32, 4, false));
2716 EXPECTED(ret, true);
2717 torture_comment(torture, "a different session %s set overlapping "
2718 "shared locks\n", ret?"can":"cannot");
2720 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 40, 4, true)) &&
2721 NT_STATUS_IS_OK(test_smb2_lock(tree, h2, 42, 4, true));
2722 EXPECTED(ret, false);
2723 torture_comment(torture, "a different handle %s set overlapping "
2724 "exclusive locks\n", ret?"can":"cannot");
2726 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 50, 4, false)) &&
2727 NT_STATUS_IS_OK(test_smb2_lock(tree, h2, 52, 4, false));
2728 EXPECTED(ret, true);
2729 torture_comment(torture, "a different handle %s set overlapping "
2730 "shared locks\n", ret?"can":"cannot");
2732 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 110, 4, false)) &&
2733 NT_STATUS_IS_OK(test_smb2_lock(tree, h, 112, 4, false)) &&
2734 NT_STATUS_IS_OK(test_smb2_unlock(tree, h, 110, 6));
2735 EXPECTED(ret, false);
2736 torture_comment(torture, "the same handle %s coalesce read locks\n",
2737 ret?"can":"cannot");
2739 smb2_util_close(tree, h2);
2740 smb2_util_close(tree, h);
2741 status = torture_smb2_testfile(tree, fname, &h);
2742 CHECK_STATUS(status, NT_STATUS_OK);
2743 status = torture_smb2_testfile(tree, fname, &h2);
2744 CHECK_STATUS(status, NT_STATUS_OK);
2745 ret = NT_STATUS_IS_OK(test_smb2_lock(tree, h, 0, 8, false)) &&
2746 NT_STATUS_IS_OK(test_smb2_lock(tree, h2, 0, 1, false)) &&
2747 NT_STATUS_IS_OK(smb2_util_close(tree, h)) &&
2748 NT_STATUS_IS_OK(torture_smb2_testfile(tree, fname, &h)) &&
2749 NT_STATUS_IS_OK(test_smb2_lock(tree, h, 7, 1, true));
2750 EXPECTED(ret, true);
2751 torture_comment(torture, "the server %s have the NT byte range lock "
2752 "bug\n", !ret?"does":"doesn't");
2754 done:
2755 smb2_util_close(tree2, h3);
2756 smb2_util_close(tree, h2);
2757 smb2_util_close(tree, h);
2758 smb2_deltree(tree, BASEDIR);
2759 return correct;
2763 * Test truncation of locked file
2764 * - some tests ported from BASE-LOCK-LOCK7
2766 static bool test_truncate(struct torture_context *torture,
2767 struct smb2_tree *tree)
2769 NTSTATUS status;
2770 bool ret = true;
2771 struct smb2_handle h, h2;
2772 uint8_t buf[200];
2773 struct smb2_lock lck;
2774 struct smb2_lock_element el[1];
2775 struct smb2_create io;
2777 const char *fname = BASEDIR "\\truncate.txt";
2779 status = torture_smb2_testdir(tree, BASEDIR, &h);
2780 CHECK_STATUS(status, NT_STATUS_OK);
2781 smb2_util_close(tree, h);
2783 status = torture_smb2_testfile(tree, fname, &h);
2784 CHECK_STATUS(status, NT_STATUS_OK);
2786 ZERO_STRUCT(buf);
2787 status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
2788 CHECK_STATUS(status, NT_STATUS_OK);
2790 torture_comment(torture, "Testing truncation of locked file:\n");
2792 /* Setup initial parameters */
2793 lck.in.locks = el;
2794 lck.in.lock_count = 0x0001;
2795 lck.in.lock_sequence = 0x00000000;
2796 lck.in.file.handle = h;
2797 el[0].offset = 0;
2798 el[0].length = 10;
2799 el[0].reserved = 0x00000000;
2801 ZERO_STRUCT(io);
2802 io.in.oplock_level = 0;
2803 io.in.desired_access = SEC_RIGHTS_FILE_ALL;
2804 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
2805 io.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
2806 io.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE |
2807 NTCREATEX_SHARE_ACCESS_READ |
2808 NTCREATEX_SHARE_ACCESS_WRITE;
2809 io.in.create_options = 0;
2810 io.in.fname = fname;
2812 /* Take an exclusive lock */
2813 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
2814 SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
2815 status = smb2_lock(tree, &lck);
2816 CHECK_STATUS(status, NT_STATUS_OK);
2818 /* On second handle open the file with OVERWRITE disposition */
2819 torture_comment(torture, " overwrite disposition is allowed on a "
2820 "locked file.\n");
2822 io.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
2823 status = smb2_create(tree, tree, &io);
2824 CHECK_STATUS(status, NT_STATUS_OK);
2825 h2 = io.out.file.handle;
2826 smb2_util_close(tree, h2);
2828 /* On second handle open the file with SUPERSEDE disposition */
2829 torture_comment(torture, " supersede disposition is allowed on a "
2830 "locked file.\n");
2832 io.in.create_disposition = NTCREATEX_DISP_SUPERSEDE;
2833 status = smb2_create(tree, tree, &io);
2834 CHECK_STATUS(status, NT_STATUS_OK);
2835 h2 = io.out.file.handle;
2836 smb2_util_close(tree, h2);
2838 /* cleanup */
2839 lck.in.file.handle = h;
2840 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2841 status = smb2_lock(tree, &lck);
2842 CHECK_STATUS(status, NT_STATUS_OK);
2844 done:
2845 smb2_util_close(tree, h2);
2846 smb2_util_close(tree, h);
2847 smb2_deltree(tree, BASEDIR);
2848 return ret;
2851 /* basic testing of SMB2 locking
2853 struct torture_suite *torture_smb2_lock_init(void)
2855 struct torture_suite *suite =
2856 torture_suite_create(talloc_autofree_context(), "lock");
2858 torture_suite_add_1smb2_test(suite, "valid-request",
2859 test_valid_request);
2860 torture_suite_add_1smb2_test(suite, "rw-none", test_lock_rw_none);
2861 torture_suite_add_1smb2_test(suite, "rw-shared", test_lock_rw_shared);
2862 torture_suite_add_1smb2_test(suite, "rw-exclusive",
2863 test_lock_rw_exclusive);
2864 torture_suite_add_1smb2_test(suite, "auto-unlock",
2865 test_lock_auto_unlock);
2866 torture_suite_add_1smb2_test(suite, "lock", test_lock);
2867 torture_suite_add_1smb2_test(suite, "async", test_async);
2868 torture_suite_add_1smb2_test(suite, "cancel", test_cancel);
2869 torture_suite_add_1smb2_test(suite, "cancel-tdis", test_cancel_tdis);
2870 torture_suite_add_1smb2_test(suite, "cancel-logoff",
2871 test_cancel_logoff);
2872 torture_suite_add_1smb2_test(suite, "errorcode", test_errorcode);
2873 torture_suite_add_1smb2_test(suite, "zerobytelength",
2874 test_zerobytelength);
2875 torture_suite_add_1smb2_test(suite, "zerobyteread",
2876 test_zerobyteread);
2877 torture_suite_add_1smb2_test(suite, "unlock", test_unlock);
2878 torture_suite_add_1smb2_test(suite, "multiple-unlock",
2879 test_multiple_unlock);
2880 torture_suite_add_1smb2_test(suite, "stacking", test_stacking);
2881 torture_suite_add_1smb2_test(suite, "contend", test_contend);
2882 torture_suite_add_1smb2_test(suite, "context", test_context);
2883 torture_suite_add_1smb2_test(suite, "range", test_range);
2884 torture_suite_add_2smb2_test(suite, "overlap", test_overlap);
2885 torture_suite_add_1smb2_test(suite, "truncate", test_truncate);
2887 suite->description = talloc_strdup(suite, "SMB2-LOCK tests");
2889 return suite;