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); \
64 * basic durable_open test.
65 * durable state should only be granted when requested
66 * along with a batch oplock or a handle lease.
68 * This test tests durable open with all possible oplock types.
71 struct durable_open_vs_oplock {
73 const char *share_mode;
77 #define NUM_OPLOCK_TYPES 4
78 #define NUM_SHARE_MODES 8
79 #define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
80 static struct durable_open_vs_oplock durable_open_vs_oplock_table[NUM_OPLOCK_OPEN_TESTS] =
98 { "s", "RWD", false },
104 { "x", "RD", false },
105 { "x", "RW", false },
106 { "x", "WD", false },
107 { "x", "RWD", false },
116 { "b", "RWD", true },
119 static bool test_one_durable_open_open_oplock(struct torture_context *tctx,
120 struct smb2_tree *tree,
122 struct durable_open_vs_oplock test)
125 TALLOC_CTX *mem_ctx = talloc_new(tctx);
126 struct smb2_handle _h;
127 struct smb2_handle *h = NULL;
129 struct smb2_create io;
131 smb2_util_unlink(tree, fname);
133 smb2_oplock_create_share(&io, fname,
134 smb2_util_share_access(test.share_mode),
135 smb2_util_oplock_level(test.level));
136 io.in.durable_open = true;
138 status = smb2_create(tree, mem_ctx, &io);
139 CHECK_STATUS(status, NT_STATUS_OK);
140 _h = io.out.file.handle;
142 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
143 CHECK_VAL(io.out.durable_open, test.expected);
144 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(test.level));
148 smb2_util_close(tree, *h);
150 smb2_util_unlink(tree, fname);
151 talloc_free(mem_ctx);
156 bool test_durable_open_open_oplock(struct torture_context *tctx,
157 struct smb2_tree *tree)
159 TALLOC_CTX *mem_ctx = talloc_new(tctx);
164 /* Choose a random name in case the state is left a little funky. */
165 snprintf(fname, 256, "durable_open_open_oplock_%s.dat", generate_random_str(tctx, 8));
167 smb2_util_unlink(tree, fname);
169 /* test various oplock levels with durable open */
171 for (i = 0; i < NUM_OPLOCK_OPEN_TESTS; i++) {
172 ret = test_one_durable_open_open_oplock(tctx,
175 durable_open_vs_oplock_table[i]);
182 smb2_util_unlink(tree, fname);
184 talloc_free(mem_ctx);
190 * basic durable_open test.
191 * durable state should only be granted when requested
192 * along with a batch oplock or a handle lease.
194 * This test tests durable open with all valid lease types.
197 struct durable_open_vs_lease {
199 const char *share_mode;
203 #define NUM_LEASE_TYPES 5
204 #define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
205 static struct durable_open_vs_lease durable_open_vs_lease_table[NUM_LEASE_OPEN_TESTS] =
214 { "", "RWD", false },
220 { "R", "RW", false },
221 { "R", "RD", false },
222 { "R", "DW", false },
223 { "R", "RWD", false },
226 { "RW", "R", false },
227 { "RW", "W", false },
228 { "RW", "D", false },
229 { "RW", "RW", false },
230 { "RW", "RD", false },
231 { "RW", "WD", false },
232 { "RW", "RWD", false },
238 { "RH", "RW", true },
239 { "RH", "RD", true },
240 { "RH", "WD", true },
241 { "RH", "RWD", true },
244 { "RHW", "R", true },
245 { "RHW", "W", true },
246 { "RHW", "D", true },
247 { "RHW", "RW", true },
248 { "RHW", "RD", true },
249 { "RHW", "WD", true },
250 { "RHW", "RWD", true },
253 static bool test_one_durable_open_open_lease(struct torture_context *tctx,
254 struct smb2_tree *tree,
256 struct durable_open_vs_lease test)
259 TALLOC_CTX *mem_ctx = talloc_new(tctx);
260 struct smb2_handle _h;
261 struct smb2_handle *h = NULL;
263 struct smb2_create io;
264 struct smb2_lease ls;
268 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
269 if (!(caps & SMB2_CAP_LEASING)) {
270 torture_skip(tctx, "leases are not supported");
273 smb2_util_unlink(tree, fname);
277 smb2_lease_create_share(&io, &ls, false /* dir */, fname,
278 smb2_util_share_access(test.share_mode),
280 smb2_util_lease_state(test.type));
281 io.in.durable_open = true;
283 status = smb2_create(tree, mem_ctx, &io);
284 CHECK_STATUS(status, NT_STATUS_OK);
285 _h = io.out.file.handle;
287 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
288 CHECK_VAL(io.out.durable_open, test.expected);
289 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
290 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
291 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
292 CHECK_VAL(io.out.lease_response.lease_state,
293 smb2_util_lease_state(test.type));
296 smb2_util_close(tree, *h);
298 smb2_util_unlink(tree, fname);
299 talloc_free(mem_ctx);
304 bool test_durable_open_open_lease(struct torture_context *tctx,
305 struct smb2_tree *tree)
307 TALLOC_CTX *mem_ctx = talloc_new(tctx);
313 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
314 if (!(caps & SMB2_CAP_LEASING)) {
315 torture_skip(tctx, "leases are not supported");
318 /* Choose a random name in case the state is left a little funky. */
319 snprintf(fname, 256, "durable_open_open_lease_%s.dat", generate_random_str(tctx, 8));
321 smb2_util_unlink(tree, fname);
324 /* test various oplock levels with durable open */
326 for (i = 0; i < NUM_LEASE_OPEN_TESTS; i++) {
327 ret = test_one_durable_open_open_lease(tctx,
330 durable_open_vs_lease_table[i]);
337 smb2_util_unlink(tree, fname);
339 talloc_free(mem_ctx);
345 * basic test for doing a durable open
346 * and do a durable reopen on the same connection
347 * while the first open is still active (fails)
349 bool test_durable_open_reopen1(struct torture_context *tctx,
350 struct smb2_tree *tree)
353 TALLOC_CTX *mem_ctx = talloc_new(tctx);
355 struct smb2_handle _h;
356 struct smb2_handle *h = NULL;
357 struct smb2_create io1, io2;
360 /* Choose a random name in case the state is left a little funky. */
361 snprintf(fname, 256, "durable_open_reopen1_%s.dat",
362 generate_random_str(tctx, 8));
364 smb2_util_unlink(tree, fname);
366 smb2_oplock_create_share(&io1, fname,
367 smb2_util_share_access(""),
368 smb2_util_oplock_level("b"));
369 io1.in.durable_open = true;
371 status = smb2_create(tree, mem_ctx, &io1);
372 CHECK_STATUS(status, NT_STATUS_OK);
373 _h = io1.out.file.handle;
375 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
376 CHECK_VAL(io1.out.durable_open, true);
377 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
379 /* try a durable reconnect while the file is still open */
381 io2.in.fname = fname;
382 io2.in.durable_handle = h;
384 status = smb2_create(tree, mem_ctx, &io2);
385 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
389 smb2_util_close(tree, *h);
392 smb2_util_unlink(tree, fname);
396 talloc_free(mem_ctx);
402 * basic test for doing a durable open
403 * tcp disconnect, reconnect, do a durable reopen (succeeds)
405 bool test_durable_open_reopen2(struct torture_context *tctx,
406 struct smb2_tree *tree)
409 TALLOC_CTX *mem_ctx = talloc_new(tctx);
411 struct smb2_handle _h;
412 struct smb2_handle *h = NULL;
413 struct smb2_create io1, io2;
416 /* Choose a random name in case the state is left a little funky. */
417 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
418 generate_random_str(tctx, 8));
420 smb2_util_unlink(tree, fname);
422 smb2_oplock_create_share(&io1, fname,
423 smb2_util_share_access(""),
424 smb2_util_oplock_level("b"));
425 io1.in.durable_open = true;
427 status = smb2_create(tree, mem_ctx, &io1);
428 CHECK_STATUS(status, NT_STATUS_OK);
429 _h = io1.out.file.handle;
431 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
432 CHECK_VAL(io1.out.durable_open, true);
433 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
435 /* disconnect, reconnect and then do durable reopen */
439 if (!torture_smb2_connection(tctx, &tree)) {
440 torture_warning(tctx, "couldn't reconnect, bailing\n");
446 io2.in.fname = fname;
447 io2.in.durable_handle = h;
450 status = smb2_create(tree, mem_ctx, &io2);
451 CHECK_STATUS(status, NT_STATUS_OK);
452 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
453 CHECK_VAL(io2.out.durable_open, true);
454 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
455 _h = io2.out.file.handle;
460 smb2_util_close(tree, *h);
463 smb2_util_unlink(tree, fname);
467 talloc_free(mem_ctx);
473 * basic test for doing a durable open
474 * tcp disconnect, reconnect with a session reconnect and
475 * do a durable reopen (succeeds)
477 bool test_durable_open_reopen2a(struct torture_context *tctx,
478 struct smb2_tree *tree)
481 TALLOC_CTX *mem_ctx = talloc_new(tctx);
483 struct smb2_handle _h;
484 struct smb2_handle *h = NULL;
485 struct smb2_create io1, io2;
486 uint64_t previous_session_id;
489 /* Choose a random name in case the state is left a little funky. */
490 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
491 generate_random_str(tctx, 8));
493 smb2_util_unlink(tree, fname);
495 smb2_oplock_create_share(&io1, fname,
496 smb2_util_share_access(""),
497 smb2_util_oplock_level("b"));
498 io1.in.durable_open = true;
500 status = smb2_create(tree, mem_ctx, &io1);
501 CHECK_STATUS(status, NT_STATUS_OK);
502 _h = io1.out.file.handle;
504 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
505 CHECK_VAL(io1.out.durable_open, true);
506 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
508 /* disconnect, reconnect and then do durable reopen */
509 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
513 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
514 torture_warning(tctx, "couldn't reconnect, bailing\n");
520 io2.in.fname = fname;
521 io2.in.durable_handle = h;
524 status = smb2_create(tree, mem_ctx, &io2);
525 CHECK_STATUS(status, NT_STATUS_OK);
526 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
527 CHECK_VAL(io2.out.durable_open, true);
528 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
529 _h = io2.out.file.handle;
534 smb2_util_close(tree, *h);
537 smb2_util_unlink(tree, fname);
541 talloc_free(mem_ctx);
548 * basic test for doing a durable open:
549 * tdis, new tcon, try durable reopen (fails)
551 bool test_durable_open_reopen3(struct torture_context *tctx,
552 struct smb2_tree *tree)
555 TALLOC_CTX *mem_ctx = talloc_new(tctx);
557 struct smb2_handle _h;
558 struct smb2_handle *h = NULL;
559 struct smb2_create io1, io2;
561 struct smb2_tree *tree2;
563 /* Choose a random name in case the state is left a little funky. */
564 snprintf(fname, 256, "durable_open_reopen3_%s.dat",
565 generate_random_str(tctx, 8));
567 smb2_util_unlink(tree, fname);
569 smb2_oplock_create_share(&io1, fname,
570 smb2_util_share_access(""),
571 smb2_util_oplock_level("b"));
572 io1.in.durable_open = true;
574 status = smb2_create(tree, mem_ctx, &io1);
575 CHECK_STATUS(status, NT_STATUS_OK);
576 _h = io1.out.file.handle;
578 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
579 CHECK_VAL(io1.out.durable_open, true);
580 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
582 /* disconnect, reconnect and then do durable reopen */
583 status = smb2_tdis(tree);
584 CHECK_STATUS(status, NT_STATUS_OK);
586 if (!torture_smb2_tree_connect(tctx, tree->session, mem_ctx, &tree2)) {
587 torture_warning(tctx, "couldn't reconnect to share, bailing\n");
594 io2.in.fname = fname;
595 io2.in.durable_handle = h;
597 status = smb2_create(tree2, mem_ctx, &io2);
598 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
602 smb2_util_close(tree, *h);
605 smb2_util_unlink(tree2, fname);
609 talloc_free(mem_ctx);
615 * basic test for doing a durable open:
616 * logoff, create a new session, do a durable reopen (succeeds)
618 bool test_durable_open_reopen4(struct torture_context *tctx,
619 struct smb2_tree *tree)
622 TALLOC_CTX *mem_ctx = talloc_new(tctx);
624 struct smb2_handle _h;
625 struct smb2_handle *h = NULL;
626 struct smb2_create io1, io2;
628 struct smb2_transport *transport;
629 struct smb2_session *session2;
630 struct smb2_tree *tree2;
632 /* Choose a random name in case the state is left a little funky. */
633 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
634 generate_random_str(tctx, 8));
636 smb2_util_unlink(tree, fname);
638 smb2_oplock_create_share(&io1, fname,
639 smb2_util_share_access(""),
640 smb2_util_oplock_level("b"));
641 io1.in.durable_open = true;
642 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
644 status = smb2_create(tree, mem_ctx, &io1);
645 CHECK_STATUS(status, NT_STATUS_OK);
646 _h = io1.out.file.handle;
648 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
649 CHECK_VAL(io1.out.durable_open, true);
650 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
653 * do a session logoff, establish a new session and tree
654 * connect on the same transport, and try a durable reopen
656 transport = tree->session->transport;
657 status = smb2_logoff(tree->session);
658 CHECK_STATUS(status, NT_STATUS_OK);
660 if (!torture_smb2_session_setup(tctx, transport,
661 0, /* previous_session_id */
664 torture_warning(tctx, "session setup failed.\n");
670 * the session setup has talloc-stolen the transport,
671 * so we can safely free the old tree+session for clarity
675 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
676 torture_warning(tctx, "tree connect failed.\n");
682 io2.in.fname = fname;
683 io2.in.durable_handle = h;
686 status = smb2_create(tree2, mem_ctx, &io2);
687 CHECK_STATUS(status, NT_STATUS_OK);
689 _h = io2.out.file.handle;
691 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
692 CHECK_VAL(io2.out.durable_open, true);
693 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
697 smb2_util_close(tree2, *h);
700 smb2_util_unlink(tree2, fname);
704 talloc_free(mem_ctx);
709 bool test_durable_open_delete_on_close1(struct torture_context *tctx,
710 struct smb2_tree *tree)
713 TALLOC_CTX *mem_ctx = talloc_new(tctx);
715 struct smb2_handle _h;
716 struct smb2_handle *h = NULL;
717 struct smb2_create io1, io2, io3;
719 struct smb2_transport *transport;
720 struct smb2_session *session2;
721 struct smb2_tree *tree2;
722 union smb_fileinfo info1, info2;
724 /* Choose a random name in case the state is left a little funky. */
725 snprintf(fname, 256, "durable_open_delete_on_close1_%s.dat",
726 generate_random_str(tctx, 8));
728 smb2_util_unlink(tree, fname);
730 smb2_oplock_create_share(&io1, fname,
731 smb2_util_share_access(""),
732 smb2_util_oplock_level("b"));
733 io1.in.durable_open = true;
734 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
736 status = smb2_create(tree, mem_ctx, &io1);
737 CHECK_STATUS(status, NT_STATUS_OK);
738 _h = io1.out.file.handle;
740 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
741 CHECK_VAL(io1.out.durable_open, true);
742 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
745 info1.internal_information.level = RAW_FILEINFO_INTERNAL_INFORMATION;
746 info1.internal_information.in.file.handle = _h;
747 status = smb2_getinfo_file(tree, tree, &info1);
748 CHECK_STATUS(status, NT_STATUS_OK);
751 * do a session logoff, establish a new session and tree
752 * connect on the same transport, and try a durable reopen
754 transport = tree->session->transport;
755 status = smb2_logoff(tree->session);
756 CHECK_STATUS(status, NT_STATUS_OK);
758 if (!torture_smb2_session_setup(tctx, transport,
759 0, /* previous_session_id */
762 torture_warning(tctx, "session setup failed.\n");
768 * the session setup has talloc-stolen the transport,
769 * so we can safely free the old tree+session for clarity
773 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
774 torture_warning(tctx, "tree connect failed.\n");
780 io3.in.fname = fname;
781 io3.in.durable_handle = h;
784 smb2_oplock_create_share(&io2, fname,
785 smb2_util_share_access(""),
786 smb2_util_oplock_level("b"));
787 io2.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
789 status = smb2_create(tree2, mem_ctx, &io2);
790 CHECK_STATUS(status, NT_STATUS_OK);
791 _h = io2.out.file.handle;
793 CHECK_CREATED(&io2, CREATED, FILE_ATTRIBUTE_ARCHIVE);
794 CHECK_VAL(io2.out.durable_open, false);
795 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
798 info2.internal_information.level = RAW_FILEINFO_INTERNAL_INFORMATION;
799 info2.internal_information.in.file.handle = _h;
800 status = smb2_getinfo_file(tree2, tree2, &info2);
801 CHECK_STATUS(status, NT_STATUS_OK);
803 CHECK_NOT_VAL(info1.internal_information.out.file_id,
804 info2.internal_information.out.file_id);
806 status = smb2_create(tree2, mem_ctx, &io3);
807 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
811 smb2_util_close(tree2, *h);
814 smb2_util_unlink(tree2, fname);
819 talloc_free(mem_ctx);
825 basic testing of SMB2 durable opens
826 regarding the position information on the handle
828 bool test_durable_open_file_position(struct torture_context *tctx,
829 struct smb2_tree *tree1,
830 struct smb2_tree *tree2)
832 TALLOC_CTX *mem_ctx = talloc_new(tctx);
833 struct smb2_handle h1, h2;
834 struct smb2_create io1, io2;
836 const char *fname = "durable_open_position.dat";
837 union smb_fileinfo qfinfo;
838 union smb_setfileinfo sfinfo;
842 smb2_util_unlink(tree1, fname);
844 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
845 io1.in.durable_open = true;
847 status = smb2_create(tree1, mem_ctx, &io1);
848 CHECK_STATUS(status, NT_STATUS_OK);
849 h1 = io1.out.file.handle;
850 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
851 CHECK_VAL(io1.out.durable_open, true);
852 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
854 /* TODO: check extra blob content */
857 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
858 qfinfo.generic.in.file.handle = h1;
859 status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
860 CHECK_STATUS(status, NT_STATUS_OK);
861 CHECK_VAL(qfinfo.position_information.out.position, 0);
862 pos = qfinfo.position_information.out.position;
863 torture_comment(tctx, "position: %llu\n",
864 (unsigned long long)pos);
867 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
868 sfinfo.generic.in.file.handle = h1;
869 sfinfo.position_information.in.position = 0x1000;
870 status = smb2_setinfo_file(tree1, &sfinfo);
871 CHECK_STATUS(status, NT_STATUS_OK);
874 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
875 qfinfo.generic.in.file.handle = h1;
876 status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
877 CHECK_STATUS(status, NT_STATUS_OK);
878 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
879 pos = qfinfo.position_information.out.position;
880 torture_comment(tctx, "position: %llu\n",
881 (unsigned long long)pos);
887 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
888 qfinfo.generic.in.file.handle = h1;
889 status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
890 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
893 io2.in.fname = fname;
894 io2.in.durable_handle = &h1;
896 status = smb2_create(tree2, mem_ctx, &io2);
897 CHECK_STATUS(status, NT_STATUS_OK);
898 CHECK_VAL(io2.out.durable_open, true);
899 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
900 CHECK_VAL(io2.out.reserved, 0x00);
901 CHECK_VAL(io2.out.create_action, NTCREATEX_ACTION_EXISTED);
902 CHECK_VAL(io2.out.alloc_size, 0);
903 CHECK_VAL(io2.out.size, 0);
904 CHECK_VAL(io2.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
905 CHECK_VAL(io2.out.reserved2, 0);
907 h2 = io2.out.file.handle;
910 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
911 qfinfo.generic.in.file.handle = h2;
912 status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
913 CHECK_STATUS(status, NT_STATUS_OK);
914 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
915 pos = qfinfo.position_information.out.position;
916 torture_comment(tctx, "position: %llu\n",
917 (unsigned long long)pos);
919 smb2_util_close(tree2, h2);
921 talloc_free(mem_ctx);
923 smb2_util_unlink(tree2, fname);
932 Open, disconnect, oplock break, reconnect.
934 bool test_durable_open_oplock(struct torture_context *tctx,
935 struct smb2_tree *tree1,
936 struct smb2_tree *tree2)
938 TALLOC_CTX *mem_ctx = talloc_new(tctx);
939 struct smb2_create io1, io2;
940 struct smb2_handle h1, h2;
945 /* Choose a random name in case the state is left a little funky. */
946 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
949 smb2_util_unlink(tree1, fname);
951 /* Create with batch oplock */
952 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
953 io1.in.durable_open = true;
956 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
958 status = smb2_create(tree1, mem_ctx, &io1);
959 CHECK_STATUS(status, NT_STATUS_OK);
960 h1 = io1.out.file.handle;
961 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
962 CHECK_VAL(io1.out.durable_open, true);
963 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
965 /* Disconnect after getting the batch */
970 * Windows7 (build 7000) will break a batch oplock immediately if the
971 * original client is gone. (ZML: This seems like a bug. It should give
972 * some time for the client to reconnect!)
974 status = smb2_create(tree2, mem_ctx, &io2);
975 CHECK_STATUS(status, NT_STATUS_OK);
976 h2 = io2.out.file.handle;
977 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
978 CHECK_VAL(io2.out.durable_open, true);
979 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
981 /* What if tree1 tries to come back and reclaim? */
982 if (!torture_smb2_connection(tctx, &tree1)) {
983 torture_warning(tctx, "couldn't reconnect, bailing\n");
989 io1.in.fname = fname;
990 io1.in.durable_handle = &h1;
992 status = smb2_create(tree1, mem_ctx, &io1);
993 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
996 smb2_util_close(tree2, h2);
997 smb2_util_unlink(tree2, fname);
1006 Open, disconnect, lease break, reconnect.
1008 bool test_durable_open_lease(struct torture_context *tctx,
1009 struct smb2_tree *tree1,
1010 struct smb2_tree *tree2)
1012 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1013 struct smb2_create io1, io2;
1014 struct smb2_lease ls1, ls2;
1015 struct smb2_handle h1, h2;
1019 uint64_t lease1, lease2;
1022 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1023 if (!(caps & SMB2_CAP_LEASING)) {
1024 torture_skip(tctx, "leases are not supported");
1028 * Choose a random name and random lease in case the state is left a
1033 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
1036 smb2_util_unlink(tree1, fname);
1038 /* Create with lease */
1039 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
1040 lease1, smb2_util_lease_state("RHW"));
1041 io1.in.durable_open = true;
1043 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
1044 lease2, smb2_util_lease_state("RHW"));
1045 io2.in.durable_open = true;
1046 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1048 status = smb2_create(tree1, mem_ctx, &io1);
1049 CHECK_STATUS(status, NT_STATUS_OK);
1050 h1 = io1.out.file.handle;
1051 CHECK_VAL(io1.out.durable_open, true);
1052 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1054 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1055 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
1056 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
1057 CHECK_VAL(io1.out.lease_response.lease_state,
1058 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1060 /* Disconnect after getting the lease */
1065 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1066 * even if the original client is gone. (ZML: This seems like a bug. It
1067 * should give some time for the client to reconnect! And why RH?)
1069 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1070 * Test is adapted accordingly.
1072 status = smb2_create(tree2, mem_ctx, &io2);
1073 CHECK_STATUS(status, NT_STATUS_OK);
1074 h2 = io2.out.file.handle;
1075 CHECK_VAL(io2.out.durable_open, true);
1076 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1078 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1079 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
1080 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
1081 CHECK_VAL(io2.out.lease_response.lease_state,
1082 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1084 /* What if tree1 tries to come back and reclaim? */
1085 if (!torture_smb2_connection(tctx, &tree1)) {
1086 torture_warning(tctx, "couldn't reconnect, bailing\n");
1092 io1.in.fname = fname;
1093 io1.in.durable_handle = &h1;
1094 io1.in.lease_request = &ls1;
1096 status = smb2_create(tree1, mem_ctx, &io1);
1097 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1100 smb2_util_close(tree2, h2);
1101 smb2_util_unlink(tree2, fname);
1110 Open, take BRL, disconnect, reconnect.
1112 bool test_durable_open_lock_lease(struct torture_context *tctx,
1113 struct smb2_tree *tree)
1115 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1116 struct smb2_create io;
1117 struct smb2_lease ls;
1118 struct smb2_handle h;
1119 struct smb2_lock lck;
1120 struct smb2_lock_element el[2];
1127 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1128 if (!(caps & SMB2_CAP_LEASING)) {
1129 torture_skip(tctx, "leases are not supported");
1133 * Choose a random name and random lease in case the state is left a
1137 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
1140 smb2_util_unlink(tree, fname);
1142 /* Create with lease */
1144 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
1145 smb2_util_lease_state("RWH"));
1146 io.in.durable_open = true;
1148 status = smb2_create(tree, mem_ctx, &io);
1149 CHECK_STATUS(status, NT_STATUS_OK);
1150 h = io.out.file.handle;
1151 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1153 CHECK_VAL(io.out.durable_open, true);
1154 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1155 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
1156 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
1157 CHECK_VAL(io.out.lease_response.lease_state,
1158 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1163 lck.in.lock_count = 0x0001;
1164 lck.in.lock_sequence = 0x00000000;
1165 lck.in.file.handle = h;
1168 el[0].reserved = 0x00000000;
1169 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1170 status = smb2_lock(tree, &lck);
1171 CHECK_STATUS(status, NT_STATUS_OK);
1173 /* Disconnect/Reconnect. */
1177 if (!torture_smb2_connection(tctx, &tree)) {
1178 torture_warning(tctx, "couldn't reconnect, bailing\n");
1184 io.in.fname = fname;
1185 io.in.durable_handle = &h;
1186 io.in.lease_request = &ls;
1188 status = smb2_create(tree, mem_ctx, &io);
1189 CHECK_STATUS(status, NT_STATUS_OK);
1190 h = io.out.file.handle;
1192 lck.in.file.handle = h;
1193 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1194 status = smb2_lock(tree, &lck);
1195 CHECK_STATUS(status, NT_STATUS_OK);
1198 smb2_util_close(tree, h);
1199 smb2_util_unlink(tree, fname);
1206 * Open with a RH lease, disconnect, open in another tree, reconnect.
1208 * This test actually demonstrates a minimum level of respect for the durable
1209 * open in the face of another open. As long as this test shows an inability to
1210 * reconnect after an open, the oplock/lease tests above will certainly
1211 * demonstrate an error on reconnect.
1213 bool test_durable_open_open2_lease(struct torture_context *tctx,
1214 struct smb2_tree *tree1,
1215 struct smb2_tree *tree2)
1217 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1218 struct smb2_create io1, io2;
1219 struct smb2_lease ls;
1220 struct smb2_handle h1, h2;
1227 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1228 if (!(caps & SMB2_CAP_LEASING)) {
1229 torture_skip(tctx, "leases are not supported");
1233 * Choose a random name and random lease in case the state is left a
1237 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
1238 generate_random_str(tctx, 8));
1241 smb2_util_unlink(tree1, fname);
1243 /* Create with lease */
1244 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
1245 smb2_util_share_access(""),
1247 smb2_util_lease_state("RH"));
1248 io1.in.durable_open = true;
1250 status = smb2_create(tree1, mem_ctx, &io1);
1251 CHECK_STATUS(status, NT_STATUS_OK);
1252 h1 = io1.out.file.handle;
1253 CHECK_VAL(io1.out.durable_open, true);
1254 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1256 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1257 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
1258 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
1259 CHECK_VAL(io1.out.lease_response.lease_state,
1260 smb2_util_lease_state("RH"));
1266 /* Open the file in tree2 */
1267 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1269 status = smb2_create(tree2, mem_ctx, &io2);
1270 CHECK_STATUS(status, NT_STATUS_OK);
1271 h2 = io2.out.file.handle;
1272 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1275 if (!torture_smb2_connection(tctx, &tree1)) {
1276 torture_warning(tctx, "couldn't reconnect, bailing\n");
1282 io1.in.fname = fname;
1283 io1.in.durable_handle = &h1;
1284 io1.in.lease_request = &ls;
1287 * Windows7 (build 7000) will give away an open immediately if the
1288 * original client is gone. (ZML: This seems like a bug. It should give
1289 * some time for the client to reconnect!)
1291 status = smb2_create(tree1, mem_ctx, &io1);
1292 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1293 h1 = io1.out.file.handle;
1296 smb2_util_close(tree2, h2);
1297 smb2_util_unlink(tree2, fname);
1298 smb2_util_close(tree1, h1);
1299 smb2_util_unlink(tree1, fname);
1308 * Open with a batch oplock, disconnect, open in another tree, reconnect.
1310 * This test actually demonstrates a minimum level of respect for the durable
1311 * open in the face of another open. As long as this test shows an inability to
1312 * reconnect after an open, the oplock/lease tests above will certainly
1313 * demonstrate an error on reconnect.
1315 bool test_durable_open_open2_oplock(struct torture_context *tctx,
1316 struct smb2_tree *tree1,
1317 struct smb2_tree *tree2)
1319 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1320 struct smb2_create io1, io2;
1321 struct smb2_handle h1, h2;
1327 * Choose a random name and random lease in case the state is left a
1330 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
1331 generate_random_str(tctx, 8));
1334 smb2_util_unlink(tree1, fname);
1336 /* Create with batch oplock */
1337 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1338 io1.in.durable_open = true;
1340 status = smb2_create(tree1, mem_ctx, &io1);
1341 CHECK_STATUS(status, NT_STATUS_OK);
1342 h1 = io1.out.file.handle;
1343 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1344 CHECK_VAL(io1.out.durable_open, true);
1345 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1351 /* Open the file in tree2 */
1352 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1354 status = smb2_create(tree2, mem_ctx, &io2);
1355 CHECK_STATUS(status, NT_STATUS_OK);
1356 h2 = io2.out.file.handle;
1357 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1360 if (!torture_smb2_connection(tctx, &tree1)) {
1361 torture_warning(tctx, "couldn't reconnect, bailing\n");
1367 io1.in.fname = fname;
1368 io1.in.durable_handle = &h1;
1370 status = smb2_create(tree1, mem_ctx, &io1);
1371 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1372 h1 = io1.out.file.handle;
1375 smb2_util_close(tree2, h2);
1376 smb2_util_unlink(tree2, fname);
1377 smb2_util_close(tree1, h1);
1378 smb2_util_unlink(tree1, fname);
1386 struct torture_suite *torture_smb2_durable_open_init(void)
1388 struct torture_suite *suite =
1389 torture_suite_create(talloc_autofree_context(), "durable-open");
1391 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
1392 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
1393 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
1394 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
1395 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
1396 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
1397 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
1398 torture_suite_add_1smb2_test(suite, "delete_on_close1",
1399 test_durable_open_delete_on_close1);
1400 torture_suite_add_2smb2_test(suite, "file-position",
1401 test_durable_open_file_position);
1402 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
1403 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
1404 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
1405 torture_suite_add_2smb2_test(suite, "open2-lease",
1406 test_durable_open_open2_lease);
1407 torture_suite_add_2smb2_test(suite, "open2-oplock",
1408 test_durable_open_open2_oplock);
1410 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");