2 Unix SMB/CIFS implementation.
4 test suite for SMB2 durable opens
6 Copyright (C) Stefan Metzmacher 2008
7 Copyright (C) Michael Adam 2011-2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "../libcli/smb/smbXcli_base.h"
27 #include "torture/torture.h"
28 #include "torture/smb2/proto.h"
29 #include "../libcli/smb/smbXcli_base.h"
31 #define CHECK_VAL(v, correct) do { \
32 if ((v) != (correct)) { \
33 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%llx - should be 0x%llx\n", \
34 __location__, #v, (unsigned long long)v, (unsigned long long)correct); \
38 #define CHECK_NOT_VAL(v, correct) do { \
39 if ((v) == (correct)) { \
40 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%llx - should not be 0x%llx\n", \
41 __location__, #v, (unsigned long long)v, (unsigned long long)correct); \
45 #define CHECK_STATUS(status, correct) do { \
46 if (!NT_STATUS_EQUAL(status, correct)) { \
47 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
48 nt_errstr(status), nt_errstr(correct)); \
53 #define CHECK_CREATED(__io, __created, __attribute) \
55 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
56 CHECK_VAL((__io)->out.alloc_size, 0); \
57 CHECK_VAL((__io)->out.size, 0); \
58 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
59 CHECK_VAL((__io)->out.reserved2, 0); \
62 #define CHECK_CREATED_SIZE(__io, __created, __attribute, __alloc_size, __size) \
64 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
65 CHECK_VAL((__io)->out.alloc_size, (__alloc_size)); \
66 CHECK_VAL((__io)->out.size, (__size)); \
67 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
68 CHECK_VAL((__io)->out.reserved2, 0); \
74 * basic durable_open test.
75 * durable state should only be granted when requested
76 * along with a batch oplock or a handle lease.
78 * This test tests durable open with all possible oplock types.
81 struct durable_open_vs_oplock
{
83 const char *share_mode
;
87 #define NUM_OPLOCK_TYPES 4
88 #define NUM_SHARE_MODES 8
89 #define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
90 static struct durable_open_vs_oplock durable_open_vs_oplock_table
[NUM_OPLOCK_OPEN_TESTS
] =
105 { "s", "RD", false },
106 { "s", "RW", false },
107 { "s", "WD", false },
108 { "s", "RWD", false },
114 { "x", "RD", false },
115 { "x", "RW", false },
116 { "x", "WD", false },
117 { "x", "RWD", false },
126 { "b", "RWD", true },
129 static bool test_one_durable_open_open_oplock(struct torture_context
*tctx
,
130 struct smb2_tree
*tree
,
132 struct durable_open_vs_oplock test
)
135 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
136 struct smb2_handle _h
;
137 struct smb2_handle
*h
= NULL
;
139 struct smb2_create io
;
141 smb2_util_unlink(tree
, fname
);
143 smb2_oplock_create_share(&io
, fname
,
144 smb2_util_share_access(test
.share_mode
),
145 smb2_util_oplock_level(test
.level
));
146 io
.in
.durable_open
= true;
148 status
= smb2_create(tree
, mem_ctx
, &io
);
149 CHECK_STATUS(status
, NT_STATUS_OK
);
150 _h
= io
.out
.file
.handle
;
152 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
153 CHECK_VAL(io
.out
.durable_open
, test
.expected
);
154 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level(test
.level
));
158 smb2_util_close(tree
, *h
);
160 smb2_util_unlink(tree
, fname
);
161 talloc_free(mem_ctx
);
166 static bool test_durable_open_open_oplock(struct torture_context
*tctx
,
167 struct smb2_tree
*tree
)
169 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
174 /* Choose a random name in case the state is left a little funky. */
175 snprintf(fname
, 256, "durable_open_open_oplock_%s.dat", generate_random_str(tctx
, 8));
177 smb2_util_unlink(tree
, fname
);
179 /* test various oplock levels with durable open */
181 for (i
= 0; i
< NUM_OPLOCK_OPEN_TESTS
; i
++) {
182 ret
= test_one_durable_open_open_oplock(tctx
,
185 durable_open_vs_oplock_table
[i
]);
192 smb2_util_unlink(tree
, fname
);
194 talloc_free(mem_ctx
);
200 * basic durable_open test.
201 * durable state should only be granted when requested
202 * along with a batch oplock or a handle lease.
204 * This test tests durable open with all valid lease types.
207 struct durable_open_vs_lease
{
209 const char *share_mode
;
213 #define NUM_LEASE_TYPES 5
214 #define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
215 static struct durable_open_vs_lease durable_open_vs_lease_table
[NUM_LEASE_OPEN_TESTS
] =
224 { "", "RWD", false },
230 { "R", "RW", false },
231 { "R", "RD", false },
232 { "R", "DW", false },
233 { "R", "RWD", false },
236 { "RW", "R", false },
237 { "RW", "W", false },
238 { "RW", "D", false },
239 { "RW", "RW", false },
240 { "RW", "RD", false },
241 { "RW", "WD", false },
242 { "RW", "RWD", false },
248 { "RH", "RW", true },
249 { "RH", "RD", true },
250 { "RH", "WD", true },
251 { "RH", "RWD", true },
254 { "RHW", "R", true },
255 { "RHW", "W", true },
256 { "RHW", "D", true },
257 { "RHW", "RW", true },
258 { "RHW", "RD", true },
259 { "RHW", "WD", true },
260 { "RHW", "RWD", true },
263 static bool test_one_durable_open_open_lease(struct torture_context
*tctx
,
264 struct smb2_tree
*tree
,
266 struct durable_open_vs_lease test
)
269 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
270 struct smb2_handle _h
;
271 struct smb2_handle
*h
= NULL
;
273 struct smb2_create io
;
274 struct smb2_lease ls
;
278 caps
= smb2cli_conn_server_capabilities(tree
->session
->transport
->conn
);
279 if (!(caps
& SMB2_CAP_LEASING
)) {
280 torture_skip(tctx
, "leases are not supported");
283 smb2_util_unlink(tree
, fname
);
287 smb2_lease_create_share(&io
, &ls
, false /* dir */, fname
,
288 smb2_util_share_access(test
.share_mode
),
290 smb2_util_lease_state(test
.type
));
291 io
.in
.durable_open
= true;
293 status
= smb2_create(tree
, mem_ctx
, &io
);
294 CHECK_STATUS(status
, NT_STATUS_OK
);
295 _h
= io
.out
.file
.handle
;
297 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
298 CHECK_VAL(io
.out
.durable_open
, test
.expected
);
299 CHECK_VAL(io
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_LEASE
);
300 CHECK_VAL(io
.out
.lease_response
.lease_key
.data
[0], lease
);
301 CHECK_VAL(io
.out
.lease_response
.lease_key
.data
[1], ~lease
);
302 CHECK_VAL(io
.out
.lease_response
.lease_state
,
303 smb2_util_lease_state(test
.type
));
306 smb2_util_close(tree
, *h
);
308 smb2_util_unlink(tree
, fname
);
309 talloc_free(mem_ctx
);
314 static bool test_durable_open_open_lease(struct torture_context
*tctx
,
315 struct smb2_tree
*tree
)
317 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
323 caps
= smb2cli_conn_server_capabilities(tree
->session
->transport
->conn
);
324 if (!(caps
& SMB2_CAP_LEASING
)) {
325 torture_skip(tctx
, "leases are not supported");
328 /* Choose a random name in case the state is left a little funky. */
329 snprintf(fname
, 256, "durable_open_open_lease_%s.dat", generate_random_str(tctx
, 8));
331 smb2_util_unlink(tree
, fname
);
334 /* test various oplock levels with durable open */
336 for (i
= 0; i
< NUM_LEASE_OPEN_TESTS
; i
++) {
337 ret
= test_one_durable_open_open_lease(tctx
,
340 durable_open_vs_lease_table
[i
]);
347 smb2_util_unlink(tree
, fname
);
349 talloc_free(mem_ctx
);
355 * basic test for doing a durable open
356 * and do a durable reopen on the same connection
357 * while the first open is still active (fails)
359 static bool test_durable_open_reopen1(struct torture_context
*tctx
,
360 struct smb2_tree
*tree
)
363 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
365 struct smb2_handle _h
;
366 struct smb2_handle
*h
= NULL
;
367 struct smb2_create io1
, io2
;
370 /* Choose a random name in case the state is left a little funky. */
371 snprintf(fname
, 256, "durable_open_reopen1_%s.dat",
372 generate_random_str(tctx
, 8));
374 smb2_util_unlink(tree
, fname
);
376 smb2_oplock_create_share(&io1
, fname
,
377 smb2_util_share_access(""),
378 smb2_util_oplock_level("b"));
379 io1
.in
.durable_open
= true;
381 status
= smb2_create(tree
, mem_ctx
, &io1
);
382 CHECK_STATUS(status
, NT_STATUS_OK
);
383 _h
= io1
.out
.file
.handle
;
385 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
386 CHECK_VAL(io1
.out
.durable_open
, true);
387 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
389 /* try a durable reconnect while the file is still open */
391 io2
.in
.fname
= fname
;
392 io2
.in
.durable_handle
= h
;
394 status
= smb2_create(tree
, mem_ctx
, &io2
);
395 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
399 smb2_util_close(tree
, *h
);
402 smb2_util_unlink(tree
, fname
);
406 talloc_free(mem_ctx
);
412 * basic test for doing a durable open
413 * tcp disconnect, reconnect, do a durable reopen (succeeds)
415 static bool test_durable_open_reopen2(struct torture_context
*tctx
,
416 struct smb2_tree
*tree
)
419 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
421 struct smb2_handle _h
;
422 struct smb2_handle
*h
= NULL
;
423 struct smb2_create io1
, io2
;
426 /* Choose a random name in case the state is left a little funky. */
427 snprintf(fname
, 256, "durable_open_reopen2_%s.dat",
428 generate_random_str(tctx
, 8));
430 smb2_util_unlink(tree
, fname
);
432 smb2_oplock_create_share(&io1
, fname
,
433 smb2_util_share_access(""),
434 smb2_util_oplock_level("b"));
435 io1
.in
.durable_open
= true;
437 status
= smb2_create(tree
, mem_ctx
, &io1
);
438 CHECK_STATUS(status
, NT_STATUS_OK
);
439 _h
= io1
.out
.file
.handle
;
441 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
442 CHECK_VAL(io1
.out
.durable_open
, true);
443 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
445 /* disconnect, reconnect and then do durable reopen */
449 if (!torture_smb2_connection(tctx
, &tree
)) {
450 torture_warning(tctx
, "couldn't reconnect, bailing\n");
456 /* the path name is ignored by the server */
457 io2
.in
.fname
= "__non_existing_fname__";
458 io2
.in
.durable_handle
= h
;
461 status
= smb2_create(tree
, mem_ctx
, &io2
);
462 CHECK_STATUS(status
, NT_STATUS_OK
);
463 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
464 CHECK_VAL(io2
.out
.oplock_level
, smb2_util_oplock_level("b"));
465 _h
= io2
.out
.file
.handle
;
471 smb2_util_close(tree
, *h
);
474 smb2_util_unlink(tree
, fname
);
479 talloc_free(mem_ctx
);
485 * basic test for doing a durable open
486 * tcp disconnect, reconnect with a session reconnect and
487 * do a durable reopen (succeeds)
489 static bool test_durable_open_reopen2a(struct torture_context
*tctx
,
490 struct smb2_tree
*tree
)
493 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
495 struct smb2_handle _h
;
496 struct smb2_handle
*h
= NULL
;
497 struct smb2_create io1
, io2
;
498 uint64_t previous_session_id
;
501 /* Choose a random name in case the state is left a little funky. */
502 snprintf(fname
, 256, "durable_open_reopen2_%s.dat",
503 generate_random_str(tctx
, 8));
505 smb2_util_unlink(tree
, fname
);
507 smb2_oplock_create_share(&io1
, fname
,
508 smb2_util_share_access(""),
509 smb2_util_oplock_level("b"));
510 io1
.in
.durable_open
= true;
512 status
= smb2_create(tree
, mem_ctx
, &io1
);
513 CHECK_STATUS(status
, NT_STATUS_OK
);
514 _h
= io1
.out
.file
.handle
;
516 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
517 CHECK_VAL(io1
.out
.durable_open
, true);
518 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
520 /* disconnect, reconnect and then do durable reopen */
521 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
525 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
526 torture_warning(tctx
, "couldn't reconnect, bailing\n");
532 io2
.in
.fname
= fname
;
533 io2
.in
.durable_handle
= h
;
536 status
= smb2_create(tree
, mem_ctx
, &io2
);
537 CHECK_STATUS(status
, NT_STATUS_OK
);
538 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
539 CHECK_VAL(io2
.out
.oplock_level
, smb2_util_oplock_level("b"));
540 _h
= io2
.out
.file
.handle
;
546 smb2_util_close(tree
, *h
);
549 smb2_util_unlink(tree
, fname
);
554 talloc_free(mem_ctx
);
561 * basic test for doing a durable open:
562 * tdis, new tcon, try durable reopen (fails)
564 static bool test_durable_open_reopen3(struct torture_context
*tctx
,
565 struct smb2_tree
*tree
)
568 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
570 struct smb2_handle _h
;
571 struct smb2_handle
*h
= NULL
;
572 struct smb2_create io1
, io2
;
574 struct smb2_tree
*tree2
;
576 /* Choose a random name in case the state is left a little funky. */
577 snprintf(fname
, 256, "durable_open_reopen3_%s.dat",
578 generate_random_str(tctx
, 8));
580 smb2_util_unlink(tree
, fname
);
582 smb2_oplock_create_share(&io1
, fname
,
583 smb2_util_share_access(""),
584 smb2_util_oplock_level("b"));
585 io1
.in
.durable_open
= true;
587 status
= smb2_create(tree
, mem_ctx
, &io1
);
588 CHECK_STATUS(status
, NT_STATUS_OK
);
589 _h
= io1
.out
.file
.handle
;
591 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
592 CHECK_VAL(io1
.out
.durable_open
, true);
593 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
595 /* disconnect, reconnect and then do durable reopen */
596 status
= smb2_tdis(tree
);
597 CHECK_STATUS(status
, NT_STATUS_OK
);
599 if (!torture_smb2_tree_connect(tctx
, tree
->session
, mem_ctx
, &tree2
)) {
600 torture_warning(tctx
, "couldn't reconnect to share, bailing\n");
607 io2
.in
.fname
= fname
;
608 io2
.in
.durable_handle
= h
;
610 status
= smb2_create(tree2
, mem_ctx
, &io2
);
611 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
616 smb2_util_close(tree
, *h
);
619 smb2_util_unlink(tree2
, fname
);
624 talloc_free(mem_ctx
);
630 * basic test for doing a durable open:
631 * logoff, create a new session, do a durable reopen (succeeds)
633 static bool test_durable_open_reopen4(struct torture_context
*tctx
,
634 struct smb2_tree
*tree
)
637 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
639 struct smb2_handle _h
;
640 struct smb2_handle
*h
= NULL
;
641 struct smb2_create io1
, io2
;
643 struct smb2_transport
*transport
;
644 struct smb2_session
*session2
;
645 struct smb2_tree
*tree2
;
647 /* Choose a random name in case the state is left a little funky. */
648 snprintf(fname
, 256, "durable_open_reopen4_%s.dat",
649 generate_random_str(tctx
, 8));
651 smb2_util_unlink(tree
, fname
);
653 smb2_oplock_create_share(&io1
, fname
,
654 smb2_util_share_access(""),
655 smb2_util_oplock_level("b"));
656 io1
.in
.durable_open
= true;
657 io1
.in
.create_options
|= NTCREATEX_OPTIONS_DELETE_ON_CLOSE
;
659 status
= smb2_create(tree
, mem_ctx
, &io1
);
660 CHECK_STATUS(status
, NT_STATUS_OK
);
661 _h
= io1
.out
.file
.handle
;
663 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
664 CHECK_VAL(io1
.out
.durable_open
, true);
665 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
668 * do a session logoff, establish a new session and tree
669 * connect on the same transport, and try a durable reopen
671 transport
= tree
->session
->transport
;
672 status
= smb2_logoff(tree
->session
);
673 CHECK_STATUS(status
, NT_STATUS_OK
);
675 if (!torture_smb2_session_setup(tctx
, transport
,
676 0, /* previous_session_id */
679 torture_warning(tctx
, "session setup failed.\n");
685 * the session setup has talloc-stolen the transport,
686 * so we can safely free the old tree+session for clarity
690 if (!torture_smb2_tree_connect(tctx
, session2
, mem_ctx
, &tree2
)) {
691 torture_warning(tctx
, "tree connect failed.\n");
697 io2
.in
.fname
= fname
;
698 io2
.in
.durable_handle
= h
;
701 status
= smb2_create(tree2
, mem_ctx
, &io2
);
702 CHECK_STATUS(status
, NT_STATUS_OK
);
704 _h
= io2
.out
.file
.handle
;
706 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
707 CHECK_VAL(io2
.out
.oplock_level
, smb2_util_oplock_level("b"));
712 smb2_util_close(tree2
, *h
);
715 smb2_util_unlink(tree2
, fname
);
720 talloc_free(mem_ctx
);
725 static bool test_durable_open_delete_on_close1(struct torture_context
*tctx
,
726 struct smb2_tree
*tree
)
729 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
731 struct smb2_handle _h
;
732 struct smb2_handle
*h
= NULL
;
733 struct smb2_create io1
, io2
;
737 /* Choose a random name in case the state is left a little funky. */
738 snprintf(fname
, 256, "durable_open_delete_on_close1_%s.dat",
739 generate_random_str(tctx
, 8));
741 smb2_util_unlink(tree
, fname
);
743 smb2_oplock_create_share(&io1
, fname
,
744 smb2_util_share_access(""),
745 smb2_util_oplock_level("b"));
746 io1
.in
.durable_open
= true;
747 io1
.in
.create_options
|= NTCREATEX_OPTIONS_DELETE_ON_CLOSE
;
749 status
= smb2_create(tree
, mem_ctx
, &io1
);
750 CHECK_STATUS(status
, NT_STATUS_OK
);
751 _h
= io1
.out
.file
.handle
;
753 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
754 CHECK_VAL(io1
.out
.durable_open
, true);
755 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
757 status
= smb2_util_write(tree
, *h
, &b
, 0, 1);
758 CHECK_STATUS(status
, NT_STATUS_OK
);
760 /* disconnect, leaving the durable handle in place */
763 if (!torture_smb2_connection(tctx
, &tree
)) {
764 torture_warning(tctx
, "could not reconnect, bailing\n");
770 * Open the file on the new connection again
771 * and check that it has been newly created,
772 * i.e. delete on close was effective on the disconnected handle.
773 * Also check that the file is really empty,
774 * the previously written byte gone.
776 smb2_oplock_create_share(&io2
, fname
,
777 smb2_util_share_access(""),
778 smb2_util_oplock_level("b"));
779 io2
.in
.create_options
|= NTCREATEX_OPTIONS_DELETE_ON_CLOSE
;
781 status
= smb2_create(tree
, mem_ctx
, &io2
);
782 CHECK_STATUS(status
, NT_STATUS_OK
);
783 _h
= io2
.out
.file
.handle
;
785 CHECK_CREATED_SIZE(&io2
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
, 0, 0);
786 CHECK_VAL(io2
.out
.durable_open
, false);
787 CHECK_VAL(io2
.out
.oplock_level
, smb2_util_oplock_level("b"));
792 smb2_util_close(tree
, *h
);
795 smb2_util_unlink(tree
, fname
);
800 talloc_free(mem_ctx
);
806 static bool test_durable_open_delete_on_close2(struct torture_context
*tctx
,
807 struct smb2_tree
*tree
)
810 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
812 struct smb2_handle _h
;
813 struct smb2_handle
*h
= NULL
;
814 struct smb2_create io
;
817 uint64_t previous_session_id
;
818 uint64_t alloc_size_step
;
820 /* Choose a random name in case the state is left a little funky. */
821 snprintf(fname
, 256, "durable_open_delete_on_close2_%s.dat",
822 generate_random_str(tctx
, 8));
824 smb2_util_unlink(tree
, fname
);
826 smb2_oplock_create_share(&io
, fname
,
827 smb2_util_share_access(""),
828 smb2_util_oplock_level("b"));
829 io
.in
.durable_open
= true;
830 io
.in
.create_options
|= NTCREATEX_OPTIONS_DELETE_ON_CLOSE
;
832 status
= smb2_create(tree
, mem_ctx
, &io
);
833 CHECK_STATUS(status
, NT_STATUS_OK
);
834 _h
= io
.out
.file
.handle
;
836 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
837 CHECK_VAL(io
.out
.durable_open
, true);
838 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
840 status
= smb2_util_write(tree
, *h
, &b
, 0, 1);
841 CHECK_STATUS(status
, NT_STATUS_OK
);
843 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
845 /* disconnect, leaving the durable handle in place */
848 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
849 torture_warning(tctx
, "could not reconnect, bailing\n");
856 io
.in
.durable_handle
= h
;
858 status
= smb2_create(tree
, mem_ctx
, &io
);
859 CHECK_STATUS(status
, NT_STATUS_OK
);
860 _h
= io
.out
.file
.handle
;
862 alloc_size_step
= io
.out
.alloc_size
;
863 CHECK_CREATED_SIZE(&io
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
, alloc_size_step
, 1);
864 CHECK_VAL(io
.out
.durable_open
, false);
865 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
867 /* close the file, thereby deleting it */
868 smb2_util_close(tree
, *h
);
869 status
= smb2_logoff(tree
->session
);
872 if (!torture_smb2_connection(tctx
, &tree
)) {
873 torture_warning(tctx
, "could not reconnect, bailing\n");
879 * Open the file on the new connection again
880 * and check that it has been newly created,
881 * i.e. delete on close was effective on the reconnected handle.
882 * Also check that the file is really empty,
883 * the previously written byte gone.
885 smb2_oplock_create_share(&io
, fname
,
886 smb2_util_share_access(""),
887 smb2_util_oplock_level("b"));
888 io
.in
.durable_open
= true;
889 io
.in
.create_options
|= NTCREATEX_OPTIONS_DELETE_ON_CLOSE
;
891 status
= smb2_create(tree
, mem_ctx
, &io
);
892 CHECK_STATUS(status
, NT_STATUS_OK
);
893 _h
= io
.out
.file
.handle
;
895 CHECK_CREATED_SIZE(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
, 0, 0);
896 CHECK_VAL(io
.out
.durable_open
, true);
897 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
902 smb2_util_close(tree
, *h
);
905 smb2_util_unlink(tree
, fname
);
910 talloc_free(mem_ctx
);
916 basic testing of SMB2 durable opens
917 regarding the position information on the handle
919 static bool test_durable_open_file_position(struct torture_context
*tctx
,
920 struct smb2_tree
*tree
)
922 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
923 struct smb2_handle h
;
924 struct smb2_create io
;
926 const char *fname
= "durable_open_position.dat";
927 union smb_fileinfo qfinfo
;
928 union smb_setfileinfo sfinfo
;
931 uint64_t previous_session_id
;
933 smb2_util_unlink(tree
, fname
);
935 smb2_oplock_create(&io
, fname
, SMB2_OPLOCK_LEVEL_BATCH
);
936 io
.in
.durable_open
= true;
938 status
= smb2_create(tree
, mem_ctx
, &io
);
939 CHECK_STATUS(status
, NT_STATUS_OK
);
940 h
= io
.out
.file
.handle
;
941 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
942 CHECK_VAL(io
.out
.durable_open
, true);
943 CHECK_VAL(io
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
945 /* TODO: check extra blob content */
948 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
949 qfinfo
.generic
.in
.file
.handle
= h
;
950 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
951 CHECK_STATUS(status
, NT_STATUS_OK
);
952 CHECK_VAL(qfinfo
.position_information
.out
.position
, 0);
953 pos
= qfinfo
.position_information
.out
.position
;
954 torture_comment(tctx
, "position: %llu\n",
955 (unsigned long long)pos
);
958 sfinfo
.generic
.level
= RAW_SFILEINFO_POSITION_INFORMATION
;
959 sfinfo
.generic
.in
.file
.handle
= h
;
960 sfinfo
.position_information
.in
.position
= 0x1000;
961 status
= smb2_setinfo_file(tree
, &sfinfo
);
962 CHECK_STATUS(status
, NT_STATUS_OK
);
965 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
966 qfinfo
.generic
.in
.file
.handle
= h
;
967 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
968 CHECK_STATUS(status
, NT_STATUS_OK
);
969 CHECK_VAL(qfinfo
.position_information
.out
.position
, 0x1000);
970 pos
= qfinfo
.position_information
.out
.position
;
971 torture_comment(tctx
, "position: %llu\n",
972 (unsigned long long)pos
);
974 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
980 /* do a session reconnect */
981 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
982 torture_warning(tctx
, "couldn't reconnect, bailing\n");
988 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
989 qfinfo
.generic
.in
.file
.handle
= h
;
990 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
991 CHECK_STATUS(status
, NT_STATUS_FILE_CLOSED
);
995 io
.in
.durable_handle
= &h
;
997 status
= smb2_create(tree
, mem_ctx
, &io
);
998 CHECK_STATUS(status
, NT_STATUS_OK
);
999 CHECK_VAL(io
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
1000 CHECK_VAL(io
.out
.reserved
, 0x00);
1001 CHECK_VAL(io
.out
.create_action
, NTCREATEX_ACTION_EXISTED
);
1002 CHECK_VAL(io
.out
.alloc_size
, 0);
1003 CHECK_VAL(io
.out
.size
, 0);
1004 CHECK_VAL(io
.out
.file_attr
, FILE_ATTRIBUTE_ARCHIVE
);
1005 CHECK_VAL(io
.out
.reserved2
, 0);
1007 h
= io
.out
.file
.handle
;
1009 ZERO_STRUCT(qfinfo
);
1010 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
1011 qfinfo
.generic
.in
.file
.handle
= h
;
1012 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
1013 CHECK_STATUS(status
, NT_STATUS_OK
);
1014 CHECK_VAL(qfinfo
.position_information
.out
.position
, 0x1000);
1015 pos
= qfinfo
.position_information
.out
.position
;
1016 torture_comment(tctx
, "position: %llu\n",
1017 (unsigned long long)pos
);
1019 smb2_util_close(tree
, h
);
1021 talloc_free(mem_ctx
);
1023 smb2_util_unlink(tree
, fname
);
1032 Open, disconnect, oplock break, reconnect.
1034 static bool test_durable_open_oplock(struct torture_context
*tctx
,
1035 struct smb2_tree
*tree1
,
1036 struct smb2_tree
*tree2
)
1038 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1039 struct smb2_create io1
, io2
;
1040 struct smb2_handle h1
, h2
;
1045 /* Choose a random name in case the state is left a little funky. */
1046 snprintf(fname
, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx
, 8));
1049 smb2_util_unlink(tree1
, fname
);
1051 /* Create with batch oplock */
1052 smb2_oplock_create(&io1
, fname
, SMB2_OPLOCK_LEVEL_BATCH
);
1053 io1
.in
.durable_open
= true;
1056 io2
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
1058 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1059 CHECK_STATUS(status
, NT_STATUS_OK
);
1060 h1
= io1
.out
.file
.handle
;
1061 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1062 CHECK_VAL(io1
.out
.durable_open
, true);
1063 CHECK_VAL(io1
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
1065 /* Disconnect after getting the batch */
1070 * Windows7 (build 7000) will break a batch oplock immediately if the
1071 * original client is gone. (ZML: This seems like a bug. It should give
1072 * some time for the client to reconnect!)
1074 status
= smb2_create(tree2
, mem_ctx
, &io2
);
1075 CHECK_STATUS(status
, NT_STATUS_OK
);
1076 h2
= io2
.out
.file
.handle
;
1077 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
1078 CHECK_VAL(io2
.out
.durable_open
, true);
1079 CHECK_VAL(io2
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
1081 /* What if tree1 tries to come back and reclaim? */
1082 if (!torture_smb2_connection(tctx
, &tree1
)) {
1083 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1089 io1
.in
.fname
= fname
;
1090 io1
.in
.durable_handle
= &h1
;
1092 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1093 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
1096 smb2_util_close(tree2
, h2
);
1097 smb2_util_unlink(tree2
, fname
);
1106 Open, disconnect, lease break, reconnect.
1108 static bool test_durable_open_lease(struct torture_context
*tctx
,
1109 struct smb2_tree
*tree1
,
1110 struct smb2_tree
*tree2
)
1112 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1113 struct smb2_create io1
, io2
;
1114 struct smb2_lease ls1
, ls2
;
1115 struct smb2_handle h1
, h2
;
1119 uint64_t lease1
, lease2
;
1122 caps
= smb2cli_conn_server_capabilities(tree1
->session
->transport
->conn
);
1123 if (!(caps
& SMB2_CAP_LEASING
)) {
1124 torture_skip(tctx
, "leases are not supported");
1128 * Choose a random name and random lease in case the state is left a
1133 snprintf(fname
, 256, "durable_open_lease_%s.dat", generate_random_str(tctx
, 8));
1136 smb2_util_unlink(tree1
, fname
);
1138 /* Create with lease */
1139 smb2_lease_create(&io1
, &ls1
, false /* dir */, fname
,
1140 lease1
, smb2_util_lease_state("RHW"));
1141 io1
.in
.durable_open
= true;
1143 smb2_lease_create(&io2
, &ls2
, false /* dir */, fname
,
1144 lease2
, smb2_util_lease_state("RHW"));
1145 io2
.in
.durable_open
= true;
1146 io2
.in
.create_disposition
= NTCREATEX_DISP_OPEN
;
1148 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1149 CHECK_STATUS(status
, NT_STATUS_OK
);
1150 h1
= io1
.out
.file
.handle
;
1151 CHECK_VAL(io1
.out
.durable_open
, true);
1152 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1154 CHECK_VAL(io1
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_LEASE
);
1155 CHECK_VAL(io1
.out
.lease_response
.lease_key
.data
[0], lease1
);
1156 CHECK_VAL(io1
.out
.lease_response
.lease_key
.data
[1], ~lease1
);
1157 CHECK_VAL(io1
.out
.lease_response
.lease_state
,
1158 SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1160 /* Disconnect after getting the lease */
1165 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1166 * even if the original client is gone. (ZML: This seems like a bug. It
1167 * should give some time for the client to reconnect! And why RH?)
1169 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1170 * Test is adapted accordingly.
1172 status
= smb2_create(tree2
, mem_ctx
, &io2
);
1173 CHECK_STATUS(status
, NT_STATUS_OK
);
1174 h2
= io2
.out
.file
.handle
;
1175 CHECK_VAL(io2
.out
.durable_open
, true);
1176 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
1178 CHECK_VAL(io2
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_LEASE
);
1179 CHECK_VAL(io2
.out
.lease_response
.lease_key
.data
[0], lease2
);
1180 CHECK_VAL(io2
.out
.lease_response
.lease_key
.data
[1], ~lease2
);
1181 CHECK_VAL(io2
.out
.lease_response
.lease_state
,
1182 SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1184 /* What if tree1 tries to come back and reclaim? */
1185 if (!torture_smb2_connection(tctx
, &tree1
)) {
1186 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1192 io1
.in
.fname
= fname
;
1193 io1
.in
.durable_handle
= &h1
;
1194 io1
.in
.lease_request
= &ls1
;
1196 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1197 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
1200 smb2_util_close(tree2
, h2
);
1201 smb2_util_unlink(tree2
, fname
);
1209 static bool test_durable_open_lock_oplock(struct torture_context
*tctx
,
1210 struct smb2_tree
*tree
)
1212 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1213 struct smb2_create io
;
1214 struct smb2_handle h
;
1215 struct smb2_lock lck
;
1216 struct smb2_lock_element el
[2];
1223 snprintf(fname
, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx
, 8));
1226 smb2_util_unlink(tree
, fname
);
1228 /* Create with lease */
1230 smb2_oplock_create_share(&io
, fname
,
1231 smb2_util_share_access(""),
1232 smb2_util_oplock_level("b"));
1233 io
.in
.durable_open
= true;
1235 status
= smb2_create(tree
, mem_ctx
, &io
);
1236 CHECK_STATUS(status
, NT_STATUS_OK
);
1237 h
= io
.out
.file
.handle
;
1238 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1240 CHECK_VAL(io
.out
.durable_open
, true);
1241 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1246 lck
.in
.lock_count
= 0x0001;
1247 lck
.in
.lock_sequence
= 0x00000000;
1248 lck
.in
.file
.handle
= h
;
1251 el
[0].reserved
= 0x00000000;
1252 el
[0].flags
= SMB2_LOCK_FLAG_EXCLUSIVE
;
1253 status
= smb2_lock(tree
, &lck
);
1254 CHECK_STATUS(status
, NT_STATUS_OK
);
1256 /* Disconnect/Reconnect. */
1260 if (!torture_smb2_connection(tctx
, &tree
)) {
1261 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1267 io
.in
.fname
= fname
;
1268 io
.in
.durable_handle
= &h
;
1270 status
= smb2_create(tree
, mem_ctx
, &io
);
1271 CHECK_STATUS(status
, NT_STATUS_OK
);
1272 h
= io
.out
.file
.handle
;
1274 lck
.in
.file
.handle
= h
;
1275 el
[0].flags
= SMB2_LOCK_FLAG_UNLOCK
;
1276 status
= smb2_lock(tree
, &lck
);
1277 CHECK_STATUS(status
, NT_STATUS_OK
);
1280 smb2_util_close(tree
, h
);
1281 smb2_util_unlink(tree
, fname
);
1288 Open, take BRL, disconnect, reconnect.
1290 static bool test_durable_open_lock_lease(struct torture_context
*tctx
,
1291 struct smb2_tree
*tree
)
1293 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1294 struct smb2_create io
;
1295 struct smb2_lease ls
;
1296 struct smb2_handle h
;
1297 struct smb2_lock lck
;
1298 struct smb2_lock_element el
[2];
1305 caps
= smb2cli_conn_server_capabilities(tree
->session
->transport
->conn
);
1306 if (!(caps
& SMB2_CAP_LEASING
)) {
1307 torture_skip(tctx
, "leases are not supported");
1311 * Choose a random name and random lease in case the state is left a
1315 snprintf(fname
, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx
, 8));
1318 smb2_util_unlink(tree
, fname
);
1320 /* Create with lease */
1322 smb2_lease_create(&io
, &ls
, false /* dir */, fname
, lease
,
1323 smb2_util_lease_state("RWH"));
1324 io
.in
.durable_open
= true;
1326 status
= smb2_create(tree
, mem_ctx
, &io
);
1327 CHECK_STATUS(status
, NT_STATUS_OK
);
1328 h
= io
.out
.file
.handle
;
1329 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1331 CHECK_VAL(io
.out
.durable_open
, true);
1332 CHECK_VAL(io
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_LEASE
);
1333 CHECK_VAL(io
.out
.lease_response
.lease_key
.data
[0], lease
);
1334 CHECK_VAL(io
.out
.lease_response
.lease_key
.data
[1], ~lease
);
1335 CHECK_VAL(io
.out
.lease_response
.lease_state
,
1336 SMB2_LEASE_READ
|SMB2_LEASE_HANDLE
|SMB2_LEASE_WRITE
);
1341 lck
.in
.lock_count
= 0x0001;
1342 lck
.in
.lock_sequence
= 0x00000000;
1343 lck
.in
.file
.handle
= h
;
1346 el
[0].reserved
= 0x00000000;
1347 el
[0].flags
= SMB2_LOCK_FLAG_EXCLUSIVE
;
1348 status
= smb2_lock(tree
, &lck
);
1349 CHECK_STATUS(status
, NT_STATUS_OK
);
1351 /* Disconnect/Reconnect. */
1355 if (!torture_smb2_connection(tctx
, &tree
)) {
1356 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1362 io
.in
.fname
= fname
;
1363 io
.in
.durable_handle
= &h
;
1364 io
.in
.lease_request
= &ls
;
1366 status
= smb2_create(tree
, mem_ctx
, &io
);
1367 CHECK_STATUS(status
, NT_STATUS_OK
);
1368 h
= io
.out
.file
.handle
;
1370 lck
.in
.file
.handle
= h
;
1371 el
[0].flags
= SMB2_LOCK_FLAG_UNLOCK
;
1372 status
= smb2_lock(tree
, &lck
);
1373 CHECK_STATUS(status
, NT_STATUS_OK
);
1376 smb2_util_close(tree
, h
);
1377 smb2_util_unlink(tree
, fname
);
1384 * Open with a RH lease, disconnect, open in another tree, reconnect.
1386 * This test actually demonstrates a minimum level of respect for the durable
1387 * open in the face of another open. As long as this test shows an inability to
1388 * reconnect after an open, the oplock/lease tests above will certainly
1389 * demonstrate an error on reconnect.
1391 static bool test_durable_open_open2_lease(struct torture_context
*tctx
,
1392 struct smb2_tree
*tree1
,
1393 struct smb2_tree
*tree2
)
1395 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1396 struct smb2_create io1
, io2
;
1397 struct smb2_lease ls
;
1398 struct smb2_handle h1
, h2
;
1405 caps
= smb2cli_conn_server_capabilities(tree1
->session
->transport
->conn
);
1406 if (!(caps
& SMB2_CAP_LEASING
)) {
1407 torture_skip(tctx
, "leases are not supported");
1411 * Choose a random name and random lease in case the state is left a
1415 snprintf(fname
, 256, "durable_open_open2_lease_%s.dat",
1416 generate_random_str(tctx
, 8));
1419 smb2_util_unlink(tree1
, fname
);
1421 /* Create with lease */
1422 smb2_lease_create_share(&io1
, &ls
, false /* dir */, fname
,
1423 smb2_util_share_access(""),
1425 smb2_util_lease_state("RH"));
1426 io1
.in
.durable_open
= true;
1428 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1429 CHECK_STATUS(status
, NT_STATUS_OK
);
1430 h1
= io1
.out
.file
.handle
;
1431 CHECK_VAL(io1
.out
.durable_open
, true);
1432 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1434 CHECK_VAL(io1
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_LEASE
);
1435 CHECK_VAL(io1
.out
.lease_response
.lease_key
.data
[0], lease
);
1436 CHECK_VAL(io1
.out
.lease_response
.lease_key
.data
[1], ~lease
);
1437 CHECK_VAL(io1
.out
.lease_response
.lease_state
,
1438 smb2_util_lease_state("RH"));
1444 /* Open the file in tree2 */
1445 smb2_oplock_create(&io2
, fname
, SMB2_OPLOCK_LEVEL_NONE
);
1447 status
= smb2_create(tree2
, mem_ctx
, &io2
);
1448 CHECK_STATUS(status
, NT_STATUS_OK
);
1449 h2
= io2
.out
.file
.handle
;
1450 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1453 if (!torture_smb2_connection(tctx
, &tree1
)) {
1454 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1460 io1
.in
.fname
= fname
;
1461 io1
.in
.durable_handle
= &h1
;
1462 io1
.in
.lease_request
= &ls
;
1465 * Windows7 (build 7000) will give away an open immediately if the
1466 * original client is gone. (ZML: This seems like a bug. It should give
1467 * some time for the client to reconnect!)
1469 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1470 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
1471 h1
= io1
.out
.file
.handle
;
1474 smb2_util_close(tree2
, h2
);
1475 smb2_util_unlink(tree2
, fname
);
1476 smb2_util_close(tree1
, h1
);
1477 smb2_util_unlink(tree1
, fname
);
1486 * Open with a batch oplock, disconnect, open in another tree, reconnect.
1488 * This test actually demonstrates a minimum level of respect for the durable
1489 * open in the face of another open. As long as this test shows an inability to
1490 * reconnect after an open, the oplock/lease tests above will certainly
1491 * demonstrate an error on reconnect.
1493 static bool test_durable_open_open2_oplock(struct torture_context
*tctx
,
1494 struct smb2_tree
*tree1
,
1495 struct smb2_tree
*tree2
)
1497 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1498 struct smb2_create io1
, io2
;
1499 struct smb2_handle h1
, h2
;
1505 * Choose a random name and random lease in case the state is left a
1508 snprintf(fname
, 256, "durable_open_open2_oplock_%s.dat",
1509 generate_random_str(tctx
, 8));
1512 smb2_util_unlink(tree1
, fname
);
1514 /* Create with batch oplock */
1515 smb2_oplock_create(&io1
, fname
, SMB2_OPLOCK_LEVEL_BATCH
);
1516 io1
.in
.durable_open
= true;
1518 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1519 CHECK_STATUS(status
, NT_STATUS_OK
);
1520 h1
= io1
.out
.file
.handle
;
1521 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1522 CHECK_VAL(io1
.out
.durable_open
, true);
1523 CHECK_VAL(io1
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
1529 /* Open the file in tree2 */
1530 smb2_oplock_create(&io2
, fname
, SMB2_OPLOCK_LEVEL_NONE
);
1532 status
= smb2_create(tree2
, mem_ctx
, &io2
);
1533 CHECK_STATUS(status
, NT_STATUS_OK
);
1534 h2
= io2
.out
.file
.handle
;
1535 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1538 if (!torture_smb2_connection(tctx
, &tree1
)) {
1539 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1545 io1
.in
.fname
= fname
;
1546 io1
.in
.durable_handle
= &h1
;
1548 status
= smb2_create(tree1
, mem_ctx
, &io1
);
1549 CHECK_STATUS(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
);
1550 h1
= io1
.out
.file
.handle
;
1553 smb2_util_close(tree2
, h2
);
1554 smb2_util_unlink(tree2
, fname
);
1555 if (tree1
!= NULL
) {
1556 smb2_util_close(tree1
, h1
);
1557 smb2_util_unlink(tree1
, fname
);
1567 * test behaviour with initial allocation size
1569 static bool test_durable_open_alloc_size(struct torture_context
*tctx
,
1570 struct smb2_tree
*tree
)
1573 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1575 struct smb2_handle _h
;
1576 struct smb2_handle
*h
= NULL
;
1577 struct smb2_create io
;
1579 uint64_t previous_session_id
;
1580 uint64_t alloc_size_step
;
1581 uint64_t initial_alloc_size
= 0x100;
1582 const uint8_t *b
= NULL
;
1584 /* Choose a random name in case the state is left a little funky. */
1585 snprintf(fname
, 256, "durable_open_alloc_size_%s.dat",
1586 generate_random_str(tctx
, 8));
1588 smb2_util_unlink(tree
, fname
);
1590 smb2_oplock_create_share(&io
, fname
,
1591 smb2_util_share_access(""),
1592 smb2_util_oplock_level("b"));
1593 io
.in
.durable_open
= true;
1594 io
.in
.alloc_size
= initial_alloc_size
;
1596 status
= smb2_create(tree
, mem_ctx
, &io
);
1597 CHECK_STATUS(status
, NT_STATUS_OK
);
1598 _h
= io
.out
.file
.handle
;
1600 CHECK_NOT_VAL(io
.out
.alloc_size
, 0);
1601 alloc_size_step
= io
.out
.alloc_size
;
1602 CHECK_CREATED_SIZE(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
,
1603 alloc_size_step
, 0);
1604 CHECK_VAL(io
.out
.durable_open
, true);
1605 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1607 /* prepare buffer */
1608 b
= talloc_zero_size(mem_ctx
, alloc_size_step
);
1609 CHECK_NOT_VAL(b
, NULL
);
1611 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
1613 /* disconnect, reconnect and then do durable reopen */
1617 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
1618 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1624 io
.in
.fname
= fname
;
1625 io
.in
.durable_handle
= h
;
1628 status
= smb2_create(tree
, mem_ctx
, &io
);
1629 CHECK_STATUS(status
, NT_STATUS_OK
);
1630 CHECK_CREATED_SIZE(&io
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
,
1631 alloc_size_step
, 0);
1632 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1633 _h
= io
.out
.file
.handle
;
1636 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
1638 /* write one byte */
1639 status
= smb2_util_write(tree
, *h
, b
, 0, 1);
1640 CHECK_STATUS(status
, NT_STATUS_OK
);
1642 /* disconnect, reconnect and then do durable reopen */
1646 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
1647 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1653 io
.in
.fname
= fname
;
1654 io
.in
.durable_handle
= h
;
1657 status
= smb2_create(tree
, mem_ctx
, &io
);
1658 CHECK_STATUS(status
, NT_STATUS_OK
);
1659 CHECK_CREATED_SIZE(&io
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
,
1660 alloc_size_step
, 1);
1661 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1662 _h
= io
.out
.file
.handle
;
1665 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
1667 /* write more byte than initial allocation size */
1668 status
= smb2_util_write(tree
, *h
, b
, 1, alloc_size_step
);
1670 /* disconnect, reconnect and then do durable reopen */
1674 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
1675 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1681 io
.in
.fname
= fname
;
1682 io
.in
.durable_handle
= h
;
1685 status
= smb2_create(tree
, mem_ctx
, &io
);
1686 CHECK_STATUS(status
, NT_STATUS_OK
);
1687 CHECK_CREATED_SIZE(&io
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
,
1688 alloc_size_step
* 2, alloc_size_step
+ 1);
1689 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1690 _h
= io
.out
.file
.handle
;
1695 smb2_util_close(tree
, *h
);
1698 smb2_util_unlink(tree
, fname
);
1702 talloc_free(mem_ctx
);
1708 * test behaviour when a disconnect happens while creating a read-only file
1710 static bool test_durable_open_read_only(struct torture_context
*tctx
,
1711 struct smb2_tree
*tree
)
1714 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1716 struct smb2_handle _h
;
1717 struct smb2_handle
*h
= NULL
;
1718 struct smb2_create io
;
1720 uint64_t previous_session_id
;
1721 const uint8_t b
= 0;
1722 uint64_t alloc_size
= 0;
1724 /* Choose a random name in case the state is left a little funky. */
1725 snprintf(fname
, 256, "durable_open_initial_alloc_%s.dat",
1726 generate_random_str(tctx
, 8));
1728 smb2_util_unlink(tree
, fname
);
1730 smb2_oplock_create_share(&io
, fname
,
1731 smb2_util_share_access(""),
1732 smb2_util_oplock_level("b"));
1733 io
.in
.durable_open
= true;
1734 io
.in
.file_attributes
= FILE_ATTRIBUTE_READONLY
;
1736 status
= smb2_create(tree
, mem_ctx
, &io
);
1737 CHECK_STATUS(status
, NT_STATUS_OK
);
1738 _h
= io
.out
.file
.handle
;
1740 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_ARCHIVE
);
1741 CHECK_VAL(io
.out
.durable_open
, true);
1742 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1744 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
1746 /* write one byte */
1747 status
= smb2_util_write(tree
, *h
, &b
, 0, 1);
1748 CHECK_STATUS(status
, NT_STATUS_OK
);
1750 /* disconnect, reconnect and then do durable reopen */
1754 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree
)) {
1755 torture_warning(tctx
, "couldn't reconnect, bailing\n");
1761 io
.in
.fname
= fname
;
1762 io
.in
.durable_handle
= h
;
1765 status
= smb2_create(tree
, mem_ctx
, &io
);
1766 CHECK_STATUS(status
, NT_STATUS_OK
);
1767 alloc_size
= io
.out
.alloc_size
;
1768 CHECK_CREATED_SIZE(&io
, EXISTED
,
1769 FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_ARCHIVE
,
1771 CHECK_VAL(io
.out
.oplock_level
, smb2_util_oplock_level("b"));
1772 _h
= io
.out
.file
.handle
;
1775 /* write one byte */
1776 status
= smb2_util_write(tree
, *h
, &b
, 1, 1);
1777 CHECK_STATUS(status
, NT_STATUS_OK
);
1781 union smb_setfileinfo sfinfo
;
1783 ZERO_STRUCT(sfinfo
);
1784 sfinfo
.basic_info
.level
= RAW_SFILEINFO_BASIC_INFORMATION
;
1785 sfinfo
.basic_info
.in
.file
.handle
= *h
;
1786 sfinfo
.basic_info
.in
.attrib
= FILE_ATTRIBUTE_NORMAL
;
1787 smb2_setinfo_file(tree
, &sfinfo
);
1789 smb2_util_close(tree
, *h
);
1792 smb2_util_unlink(tree
, fname
);
1796 talloc_free(mem_ctx
);
1802 * durable open with oplock, disconnect, exit
1804 static bool test_durable_open_oplock_disconnect(struct torture_context
*tctx
,
1805 struct smb2_tree
*tree
)
1807 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
1808 struct smb2_create io
;
1809 struct smb2_handle _h
;
1810 struct smb2_handle
*h
= NULL
;
1815 snprintf(fname
, 256, "durable_open_oplock_disconnect_%s.dat",
1816 generate_random_str(mem_ctx
, 8));
1818 smb2_util_unlink(tree
, fname
);
1820 smb2_oplock_create(&io
, fname
, SMB2_OPLOCK_LEVEL_BATCH
);
1821 io
.in
.durable_open
= true;
1823 status
= smb2_create(tree
, mem_ctx
, &io
);
1824 CHECK_STATUS(status
, NT_STATUS_OK
);
1826 _h
= io
.out
.file
.handle
;
1829 CHECK_CREATED(&io
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
1830 CHECK_VAL(io
.out
.durable_open
, true);
1831 CHECK_VAL(io
.out
.oplock_level
, SMB2_OPLOCK_LEVEL_BATCH
);
1840 smb2_util_close(tree
, *h
);
1842 smb2_util_unlink(tree
, fname
);
1844 talloc_free(mem_ctx
);
1849 struct torture_suite
*torture_smb2_durable_open_init(void)
1851 struct torture_suite
*suite
=
1852 torture_suite_create(talloc_autofree_context(), "durable-open");
1854 torture_suite_add_1smb2_test(suite
, "open-oplock", test_durable_open_open_oplock
);
1855 torture_suite_add_1smb2_test(suite
, "open-lease", test_durable_open_open_lease
);
1856 torture_suite_add_1smb2_test(suite
, "reopen1", test_durable_open_reopen1
);
1857 torture_suite_add_1smb2_test(suite
, "reopen2", test_durable_open_reopen2
);
1858 torture_suite_add_1smb2_test(suite
, "reopen2a", test_durable_open_reopen2a
);
1859 torture_suite_add_1smb2_test(suite
, "reopen3", test_durable_open_reopen3
);
1860 torture_suite_add_1smb2_test(suite
, "reopen4", test_durable_open_reopen4
);
1861 torture_suite_add_1smb2_test(suite
, "delete_on_close1",
1862 test_durable_open_delete_on_close1
);
1863 torture_suite_add_1smb2_test(suite
, "delete_on_close2",
1864 test_durable_open_delete_on_close2
);
1865 torture_suite_add_1smb2_test(suite
, "file-position",
1866 test_durable_open_file_position
);
1867 torture_suite_add_2smb2_test(suite
, "oplock", test_durable_open_oplock
);
1868 torture_suite_add_2smb2_test(suite
, "lease", test_durable_open_lease
);
1869 torture_suite_add_1smb2_test(suite
, "lock-oplock", test_durable_open_lock_oplock
);
1870 torture_suite_add_1smb2_test(suite
, "lock-lease", test_durable_open_lock_lease
);
1871 torture_suite_add_2smb2_test(suite
, "open2-lease",
1872 test_durable_open_open2_lease
);
1873 torture_suite_add_2smb2_test(suite
, "open2-oplock",
1874 test_durable_open_open2_oplock
);
1875 torture_suite_add_1smb2_test(suite
, "alloc-size",
1876 test_durable_open_alloc_size
);
1877 torture_suite_add_1smb2_test(suite
, "read-only",
1878 test_durable_open_read_only
);
1880 suite
->description
= talloc_strdup(suite
, "SMB2-DURABLE-OPEN tests");
1885 struct torture_suite
*torture_smb2_durable_open_disconnect_init(void)
1887 struct torture_suite
*suite
=
1888 torture_suite_create(talloc_autofree_context(),
1889 "durable-open-disconnect");
1891 torture_suite_add_1smb2_test(suite
, "open-oplock-disconnect",
1892 test_durable_open_oplock_disconnect
);
1894 suite
->description
= talloc_strdup(suite
,
1895 "SMB2-DURABLE-OPEN-DISCONNECT tests");