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"));
791 smb2_util_close(tree, *h);
794 smb2_util_unlink(tree, fname);
798 talloc_free(mem_ctx);
804 static bool test_durable_open_delete_on_close2(struct torture_context *tctx,
805 struct smb2_tree *tree)
808 TALLOC_CTX *mem_ctx = talloc_new(tctx);
810 struct smb2_handle _h;
811 struct smb2_handle *h = NULL;
812 struct smb2_create io;
815 uint64_t previous_session_id;
816 uint64_t alloc_size_step;
818 /* Choose a random name in case the state is left a little funky. */
819 snprintf(fname, 256, "durable_open_delete_on_close2_%s.dat",
820 generate_random_str(tctx, 8));
822 smb2_util_unlink(tree, fname);
824 smb2_oplock_create_share(&io, fname,
825 smb2_util_share_access(""),
826 smb2_util_oplock_level("b"));
827 io.in.durable_open = true;
828 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
830 status = smb2_create(tree, mem_ctx, &io);
831 CHECK_STATUS(status, NT_STATUS_OK);
832 _h = io.out.file.handle;
834 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
835 CHECK_VAL(io.out.durable_open, true);
836 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
838 status = smb2_util_write(tree, *h, &b, 0, 1);
839 CHECK_STATUS(status, NT_STATUS_OK);
841 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
843 /* disconnect, leaving the durable handle in place */
846 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
847 torture_warning(tctx, "could not reconnect, bailing\n");
854 io.in.durable_handle = h;
856 status = smb2_create(tree, mem_ctx, &io);
857 CHECK_STATUS(status, NT_STATUS_OK);
858 _h = io.out.file.handle;
860 alloc_size_step = io.out.alloc_size;
861 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE, alloc_size_step, 1);
862 CHECK_VAL(io.out.durable_open, false);
863 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
865 /* close the file, thereby deleting it */
866 smb2_util_close(tree, *h);
867 status = smb2_logoff(tree->session);
870 if (!torture_smb2_connection(tctx, &tree)) {
871 torture_warning(tctx, "could not reconnect, bailing\n");
877 * Open the file on the new connection again
878 * and check that it has been newly created,
879 * i.e. delete on close was effective on the reconnected handle.
880 * Also check that the file is really empty,
881 * the previously written byte gone.
883 smb2_oplock_create_share(&io, fname,
884 smb2_util_share_access(""),
885 smb2_util_oplock_level("b"));
886 io.in.durable_open = true;
887 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
889 status = smb2_create(tree, mem_ctx, &io);
890 CHECK_STATUS(status, NT_STATUS_OK);
891 _h = io.out.file.handle;
893 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
894 CHECK_VAL(io.out.durable_open, true);
895 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
899 smb2_util_close(tree, *h);
902 smb2_util_unlink(tree, fname);
906 talloc_free(mem_ctx);
912 basic testing of SMB2 durable opens
913 regarding the position information on the handle
915 static bool test_durable_open_file_position(struct torture_context *tctx,
916 struct smb2_tree *tree)
918 TALLOC_CTX *mem_ctx = talloc_new(tctx);
919 struct smb2_handle h;
920 struct smb2_create io;
922 const char *fname = "durable_open_position.dat";
923 union smb_fileinfo qfinfo;
924 union smb_setfileinfo sfinfo;
927 uint64_t previous_session_id;
929 smb2_util_unlink(tree, fname);
931 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
932 io.in.durable_open = true;
934 status = smb2_create(tree, mem_ctx, &io);
935 CHECK_STATUS(status, NT_STATUS_OK);
936 h = io.out.file.handle;
937 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
938 CHECK_VAL(io.out.durable_open, true);
939 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
941 /* TODO: check extra blob content */
944 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
945 qfinfo.generic.in.file.handle = h;
946 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
947 CHECK_STATUS(status, NT_STATUS_OK);
948 CHECK_VAL(qfinfo.position_information.out.position, 0);
949 pos = qfinfo.position_information.out.position;
950 torture_comment(tctx, "position: %llu\n",
951 (unsigned long long)pos);
954 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
955 sfinfo.generic.in.file.handle = h;
956 sfinfo.position_information.in.position = 0x1000;
957 status = smb2_setinfo_file(tree, &sfinfo);
958 CHECK_STATUS(status, NT_STATUS_OK);
961 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
962 qfinfo.generic.in.file.handle = h;
963 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
964 CHECK_STATUS(status, NT_STATUS_OK);
965 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
966 pos = qfinfo.position_information.out.position;
967 torture_comment(tctx, "position: %llu\n",
968 (unsigned long long)pos);
970 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
976 /* do a session reconnect */
977 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
978 torture_warning(tctx, "couldn't reconnect, bailing\n");
984 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
985 qfinfo.generic.in.file.handle = h;
986 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
987 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
991 io.in.durable_handle = &h;
993 status = smb2_create(tree, mem_ctx, &io);
994 CHECK_STATUS(status, NT_STATUS_OK);
995 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
996 CHECK_VAL(io.out.reserved, 0x00);
997 CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
998 CHECK_VAL(io.out.alloc_size, 0);
999 CHECK_VAL(io.out.size, 0);
1000 CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
1001 CHECK_VAL(io.out.reserved2, 0);
1003 h = io.out.file.handle;
1005 ZERO_STRUCT(qfinfo);
1006 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1007 qfinfo.generic.in.file.handle = h;
1008 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1009 CHECK_STATUS(status, NT_STATUS_OK);
1010 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
1011 pos = qfinfo.position_information.out.position;
1012 torture_comment(tctx, "position: %llu\n",
1013 (unsigned long long)pos);
1015 smb2_util_close(tree, h);
1017 talloc_free(mem_ctx);
1019 smb2_util_unlink(tree, fname);
1028 Open, disconnect, oplock break, reconnect.
1030 static bool test_durable_open_oplock(struct torture_context *tctx,
1031 struct smb2_tree *tree1,
1032 struct smb2_tree *tree2)
1034 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1035 struct smb2_create io1, io2;
1036 struct smb2_handle h1, h2;
1041 /* Choose a random name in case the state is left a little funky. */
1042 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
1045 smb2_util_unlink(tree1, fname);
1047 /* Create with batch oplock */
1048 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1049 io1.in.durable_open = true;
1052 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1054 status = smb2_create(tree1, mem_ctx, &io1);
1055 CHECK_STATUS(status, NT_STATUS_OK);
1056 h1 = io1.out.file.handle;
1057 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1058 CHECK_VAL(io1.out.durable_open, true);
1059 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1061 /* Disconnect after getting the batch */
1066 * Windows7 (build 7000) will break a batch oplock immediately if the
1067 * original client is gone. (ZML: This seems like a bug. It should give
1068 * some time for the client to reconnect!)
1070 status = smb2_create(tree2, mem_ctx, &io2);
1071 CHECK_STATUS(status, NT_STATUS_OK);
1072 h2 = io2.out.file.handle;
1073 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1074 CHECK_VAL(io2.out.durable_open, true);
1075 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1077 /* What if tree1 tries to come back and reclaim? */
1078 if (!torture_smb2_connection(tctx, &tree1)) {
1079 torture_warning(tctx, "couldn't reconnect, bailing\n");
1085 io1.in.fname = fname;
1086 io1.in.durable_handle = &h1;
1088 status = smb2_create(tree1, mem_ctx, &io1);
1089 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1092 smb2_util_close(tree2, h2);
1093 smb2_util_unlink(tree2, fname);
1102 Open, disconnect, lease break, reconnect.
1104 static bool test_durable_open_lease(struct torture_context *tctx,
1105 struct smb2_tree *tree1,
1106 struct smb2_tree *tree2)
1108 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1109 struct smb2_create io1, io2;
1110 struct smb2_lease ls1, ls2;
1111 struct smb2_handle h1, h2;
1115 uint64_t lease1, lease2;
1118 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1119 if (!(caps & SMB2_CAP_LEASING)) {
1120 torture_skip(tctx, "leases are not supported");
1124 * Choose a random name and random lease in case the state is left a
1129 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
1132 smb2_util_unlink(tree1, fname);
1134 /* Create with lease */
1135 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
1136 lease1, smb2_util_lease_state("RHW"));
1137 io1.in.durable_open = true;
1139 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
1140 lease2, smb2_util_lease_state("RHW"));
1141 io2.in.durable_open = true;
1142 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1144 status = smb2_create(tree1, mem_ctx, &io1);
1145 CHECK_STATUS(status, NT_STATUS_OK);
1146 h1 = io1.out.file.handle;
1147 CHECK_VAL(io1.out.durable_open, true);
1148 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1150 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1151 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
1152 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
1153 CHECK_VAL(io1.out.lease_response.lease_state,
1154 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1156 /* Disconnect after getting the lease */
1161 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1162 * even if the original client is gone. (ZML: This seems like a bug. It
1163 * should give some time for the client to reconnect! And why RH?)
1165 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1166 * Test is adapted accordingly.
1168 status = smb2_create(tree2, mem_ctx, &io2);
1169 CHECK_STATUS(status, NT_STATUS_OK);
1170 h2 = io2.out.file.handle;
1171 CHECK_VAL(io2.out.durable_open, true);
1172 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1174 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1175 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
1176 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
1177 CHECK_VAL(io2.out.lease_response.lease_state,
1178 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1180 /* What if tree1 tries to come back and reclaim? */
1181 if (!torture_smb2_connection(tctx, &tree1)) {
1182 torture_warning(tctx, "couldn't reconnect, bailing\n");
1188 io1.in.fname = fname;
1189 io1.in.durable_handle = &h1;
1190 io1.in.lease_request = &ls1;
1192 status = smb2_create(tree1, mem_ctx, &io1);
1193 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1196 smb2_util_close(tree2, h2);
1197 smb2_util_unlink(tree2, fname);
1205 static bool test_durable_open_lock_oplock(struct torture_context *tctx,
1206 struct smb2_tree *tree)
1208 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1209 struct smb2_create io;
1210 struct smb2_handle h;
1211 struct smb2_lock lck;
1212 struct smb2_lock_element el[2];
1219 snprintf(fname, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx, 8));
1222 smb2_util_unlink(tree, fname);
1224 /* Create with lease */
1226 smb2_oplock_create_share(&io, fname,
1227 smb2_util_share_access(""),
1228 smb2_util_oplock_level("b"));
1229 io.in.durable_open = true;
1231 status = smb2_create(tree, mem_ctx, &io);
1232 CHECK_STATUS(status, NT_STATUS_OK);
1233 h = io.out.file.handle;
1234 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1236 CHECK_VAL(io.out.durable_open, true);
1237 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1242 lck.in.lock_count = 0x0001;
1243 lck.in.lock_sequence = 0x00000000;
1244 lck.in.file.handle = h;
1247 el[0].reserved = 0x00000000;
1248 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1249 status = smb2_lock(tree, &lck);
1250 CHECK_STATUS(status, NT_STATUS_OK);
1252 /* Disconnect/Reconnect. */
1256 if (!torture_smb2_connection(tctx, &tree)) {
1257 torture_warning(tctx, "couldn't reconnect, bailing\n");
1263 io.in.fname = fname;
1264 io.in.durable_handle = &h;
1266 status = smb2_create(tree, mem_ctx, &io);
1267 CHECK_STATUS(status, NT_STATUS_OK);
1268 h = io.out.file.handle;
1270 lck.in.file.handle = h;
1271 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1272 status = smb2_lock(tree, &lck);
1273 CHECK_STATUS(status, NT_STATUS_OK);
1276 smb2_util_close(tree, h);
1277 smb2_util_unlink(tree, fname);
1284 Open, take BRL, disconnect, reconnect.
1286 static bool test_durable_open_lock_lease(struct torture_context *tctx,
1287 struct smb2_tree *tree)
1289 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1290 struct smb2_create io;
1291 struct smb2_lease ls;
1292 struct smb2_handle h;
1293 struct smb2_lock lck;
1294 struct smb2_lock_element el[2];
1301 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1302 if (!(caps & SMB2_CAP_LEASING)) {
1303 torture_skip(tctx, "leases are not supported");
1307 * Choose a random name and random lease in case the state is left a
1311 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
1314 smb2_util_unlink(tree, fname);
1316 /* Create with lease */
1318 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
1319 smb2_util_lease_state("RWH"));
1320 io.in.durable_open = true;
1322 status = smb2_create(tree, mem_ctx, &io);
1323 CHECK_STATUS(status, NT_STATUS_OK);
1324 h = io.out.file.handle;
1325 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1327 CHECK_VAL(io.out.durable_open, true);
1328 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1329 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
1330 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
1331 CHECK_VAL(io.out.lease_response.lease_state,
1332 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1337 lck.in.lock_count = 0x0001;
1338 lck.in.lock_sequence = 0x00000000;
1339 lck.in.file.handle = h;
1342 el[0].reserved = 0x00000000;
1343 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1344 status = smb2_lock(tree, &lck);
1345 CHECK_STATUS(status, NT_STATUS_OK);
1347 /* Disconnect/Reconnect. */
1351 if (!torture_smb2_connection(tctx, &tree)) {
1352 torture_warning(tctx, "couldn't reconnect, bailing\n");
1358 io.in.fname = fname;
1359 io.in.durable_handle = &h;
1360 io.in.lease_request = &ls;
1362 status = smb2_create(tree, mem_ctx, &io);
1363 CHECK_STATUS(status, NT_STATUS_OK);
1364 h = io.out.file.handle;
1366 lck.in.file.handle = h;
1367 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1368 status = smb2_lock(tree, &lck);
1369 CHECK_STATUS(status, NT_STATUS_OK);
1372 smb2_util_close(tree, h);
1373 smb2_util_unlink(tree, fname);
1380 * Open with a RH lease, disconnect, open in another tree, reconnect.
1382 * This test actually demonstrates a minimum level of respect for the durable
1383 * open in the face of another open. As long as this test shows an inability to
1384 * reconnect after an open, the oplock/lease tests above will certainly
1385 * demonstrate an error on reconnect.
1387 static bool test_durable_open_open2_lease(struct torture_context *tctx,
1388 struct smb2_tree *tree1,
1389 struct smb2_tree *tree2)
1391 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1392 struct smb2_create io1, io2;
1393 struct smb2_lease ls;
1394 struct smb2_handle h1, h2;
1401 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1402 if (!(caps & SMB2_CAP_LEASING)) {
1403 torture_skip(tctx, "leases are not supported");
1407 * Choose a random name and random lease in case the state is left a
1411 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
1412 generate_random_str(tctx, 8));
1415 smb2_util_unlink(tree1, fname);
1417 /* Create with lease */
1418 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
1419 smb2_util_share_access(""),
1421 smb2_util_lease_state("RH"));
1422 io1.in.durable_open = true;
1424 status = smb2_create(tree1, mem_ctx, &io1);
1425 CHECK_STATUS(status, NT_STATUS_OK);
1426 h1 = io1.out.file.handle;
1427 CHECK_VAL(io1.out.durable_open, true);
1428 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1430 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1431 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
1432 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
1433 CHECK_VAL(io1.out.lease_response.lease_state,
1434 smb2_util_lease_state("RH"));
1440 /* Open the file in tree2 */
1441 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1443 status = smb2_create(tree2, mem_ctx, &io2);
1444 CHECK_STATUS(status, NT_STATUS_OK);
1445 h2 = io2.out.file.handle;
1446 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1449 if (!torture_smb2_connection(tctx, &tree1)) {
1450 torture_warning(tctx, "couldn't reconnect, bailing\n");
1456 io1.in.fname = fname;
1457 io1.in.durable_handle = &h1;
1458 io1.in.lease_request = &ls;
1461 * Windows7 (build 7000) will give away an open immediately if the
1462 * original client is gone. (ZML: This seems like a bug. It should give
1463 * some time for the client to reconnect!)
1465 status = smb2_create(tree1, mem_ctx, &io1);
1466 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1467 h1 = io1.out.file.handle;
1470 smb2_util_close(tree2, h2);
1471 smb2_util_unlink(tree2, fname);
1472 smb2_util_close(tree1, h1);
1473 smb2_util_unlink(tree1, fname);
1482 * Open with a batch oplock, disconnect, open in another tree, reconnect.
1484 * This test actually demonstrates a minimum level of respect for the durable
1485 * open in the face of another open. As long as this test shows an inability to
1486 * reconnect after an open, the oplock/lease tests above will certainly
1487 * demonstrate an error on reconnect.
1489 static bool test_durable_open_open2_oplock(struct torture_context *tctx,
1490 struct smb2_tree *tree1,
1491 struct smb2_tree *tree2)
1493 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1494 struct smb2_create io1, io2;
1495 struct smb2_handle h1, h2;
1501 * Choose a random name and random lease in case the state is left a
1504 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
1505 generate_random_str(tctx, 8));
1508 smb2_util_unlink(tree1, fname);
1510 /* Create with batch oplock */
1511 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1512 io1.in.durable_open = true;
1514 status = smb2_create(tree1, mem_ctx, &io1);
1515 CHECK_STATUS(status, NT_STATUS_OK);
1516 h1 = io1.out.file.handle;
1517 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1518 CHECK_VAL(io1.out.durable_open, true);
1519 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1525 /* Open the file in tree2 */
1526 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1528 status = smb2_create(tree2, mem_ctx, &io2);
1529 CHECK_STATUS(status, NT_STATUS_OK);
1530 h2 = io2.out.file.handle;
1531 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1534 if (!torture_smb2_connection(tctx, &tree1)) {
1535 torture_warning(tctx, "couldn't reconnect, bailing\n");
1541 io1.in.fname = fname;
1542 io1.in.durable_handle = &h1;
1544 status = smb2_create(tree1, mem_ctx, &io1);
1545 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1546 h1 = io1.out.file.handle;
1549 smb2_util_close(tree2, h2);
1550 smb2_util_unlink(tree2, fname);
1551 smb2_util_close(tree1, h1);
1552 smb2_util_unlink(tree1, fname);
1561 * test behaviour with initial allocation size
1563 static bool test_durable_open_alloc_size(struct torture_context *tctx,
1564 struct smb2_tree *tree)
1567 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1569 struct smb2_handle _h;
1570 struct smb2_handle *h = NULL;
1571 struct smb2_create io;
1573 uint64_t previous_session_id;
1574 uint64_t alloc_size_step;
1575 uint64_t initial_alloc_size = 0x100;
1576 const uint8_t *b = NULL;
1578 /* Choose a random name in case the state is left a little funky. */
1579 snprintf(fname, 256, "durable_open_alloc_size_%s.dat",
1580 generate_random_str(tctx, 8));
1582 smb2_util_unlink(tree, fname);
1584 smb2_oplock_create_share(&io, fname,
1585 smb2_util_share_access(""),
1586 smb2_util_oplock_level("b"));
1587 io.in.durable_open = true;
1588 io.in.alloc_size = initial_alloc_size;
1590 status = smb2_create(tree, mem_ctx, &io);
1591 CHECK_STATUS(status, NT_STATUS_OK);
1592 _h = io.out.file.handle;
1594 CHECK_NOT_VAL(io.out.alloc_size, 0);
1595 alloc_size_step = io.out.alloc_size;
1596 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE,
1597 alloc_size_step, 0);
1598 CHECK_VAL(io.out.durable_open, true);
1599 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1601 /* prepare buffer */
1602 b = talloc_zero_size(mem_ctx, alloc_size_step);
1603 CHECK_NOT_VAL(b, NULL);
1605 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1607 /* disconnect, reconnect and then do durable reopen */
1611 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1612 torture_warning(tctx, "couldn't reconnect, bailing\n");
1618 io.in.fname = fname;
1619 io.in.durable_handle = h;
1622 status = smb2_create(tree, mem_ctx, &io);
1623 CHECK_STATUS(status, NT_STATUS_OK);
1624 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1625 alloc_size_step, 0);
1626 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1627 _h = io.out.file.handle;
1630 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1632 /* write one byte */
1633 status = smb2_util_write(tree, *h, b, 0, 1);
1634 CHECK_STATUS(status, NT_STATUS_OK);
1636 /* disconnect, reconnect and then do durable reopen */
1640 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1641 torture_warning(tctx, "couldn't reconnect, bailing\n");
1647 io.in.fname = fname;
1648 io.in.durable_handle = h;
1651 status = smb2_create(tree, mem_ctx, &io);
1652 CHECK_STATUS(status, NT_STATUS_OK);
1653 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1654 alloc_size_step, 1);
1655 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1656 _h = io.out.file.handle;
1659 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1661 /* write more byte than initial allocation size */
1662 status = smb2_util_write(tree, *h, b, 1, alloc_size_step);
1664 /* disconnect, reconnect and then do durable reopen */
1668 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1669 torture_warning(tctx, "couldn't reconnect, bailing\n");
1675 io.in.fname = fname;
1676 io.in.durable_handle = h;
1679 status = smb2_create(tree, mem_ctx, &io);
1680 CHECK_STATUS(status, NT_STATUS_OK);
1681 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1682 alloc_size_step * 2, alloc_size_step + 1);
1683 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1684 _h = io.out.file.handle;
1689 smb2_util_close(tree, *h);
1692 smb2_util_unlink(tree, fname);
1696 talloc_free(mem_ctx);
1702 * test behaviour when a disconnect happens while creating a read-only file
1704 static bool test_durable_open_read_only(struct torture_context *tctx,
1705 struct smb2_tree *tree)
1708 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1710 struct smb2_handle _h;
1711 struct smb2_handle *h = NULL;
1712 struct smb2_create io;
1714 uint64_t previous_session_id;
1715 const uint8_t b = 0;
1716 uint64_t alloc_size = 0;
1718 /* Choose a random name in case the state is left a little funky. */
1719 snprintf(fname, 256, "durable_open_initial_alloc_%s.dat",
1720 generate_random_str(tctx, 8));
1722 smb2_util_unlink(tree, fname);
1724 smb2_oplock_create_share(&io, fname,
1725 smb2_util_share_access(""),
1726 smb2_util_oplock_level("b"));
1727 io.in.durable_open = true;
1728 io.in.file_attributes = FILE_ATTRIBUTE_READONLY;
1730 status = smb2_create(tree, mem_ctx, &io);
1731 CHECK_STATUS(status, NT_STATUS_OK);
1732 _h = io.out.file.handle;
1734 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE);
1735 CHECK_VAL(io.out.durable_open, true);
1736 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1738 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1740 /* write one byte */
1741 status = smb2_util_write(tree, *h, &b, 0, 1);
1742 CHECK_STATUS(status, NT_STATUS_OK);
1744 /* disconnect, reconnect and then do durable reopen */
1748 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1749 torture_warning(tctx, "couldn't reconnect, bailing\n");
1755 io.in.fname = fname;
1756 io.in.durable_handle = h;
1759 status = smb2_create(tree, mem_ctx, &io);
1760 CHECK_STATUS(status, NT_STATUS_OK);
1761 alloc_size = io.out.alloc_size;
1762 CHECK_CREATED_SIZE(&io, EXISTED,
1763 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE,
1765 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1766 _h = io.out.file.handle;
1769 /* write one byte */
1770 status = smb2_util_write(tree, *h, &b, 1, 1);
1771 CHECK_STATUS(status, NT_STATUS_OK);
1775 union smb_setfileinfo sfinfo;
1777 ZERO_STRUCT(sfinfo);
1778 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
1779 sfinfo.basic_info.in.file.handle = *h;
1780 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
1781 smb2_setinfo_file(tree, &sfinfo);
1783 smb2_util_close(tree, *h);
1786 smb2_util_unlink(tree, fname);
1790 talloc_free(mem_ctx);
1796 * durable open with oplock, disconnect, exit
1798 static bool test_durable_open_oplock_disconnect(struct torture_context *tctx,
1799 struct smb2_tree *tree)
1801 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1802 struct smb2_create io;
1803 struct smb2_handle _h;
1804 struct smb2_handle *h = NULL;
1809 snprintf(fname, 256, "durable_open_oplock_disconnect_%s.dat",
1810 generate_random_str(tctx, 8));
1812 smb2_util_unlink(tree, fname);
1814 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
1815 io.in.durable_open = true;
1817 status = smb2_create(tree, mem_ctx, &io);
1818 CHECK_STATUS(status, NT_STATUS_OK);
1820 _h = io.out.file.handle;
1823 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1824 CHECK_VAL(io.out.durable_open, true);
1825 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1834 smb2_util_close(tree, *h);
1836 smb2_util_unlink(tree, fname);
1843 struct torture_suite *torture_smb2_durable_open_init(void)
1845 struct torture_suite *suite =
1846 torture_suite_create(talloc_autofree_context(), "durable-open");
1848 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
1849 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
1850 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
1851 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
1852 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
1853 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
1854 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
1855 torture_suite_add_1smb2_test(suite, "delete_on_close1",
1856 test_durable_open_delete_on_close1);
1857 torture_suite_add_1smb2_test(suite, "delete_on_close2",
1858 test_durable_open_delete_on_close2);
1859 torture_suite_add_1smb2_test(suite, "file-position",
1860 test_durable_open_file_position);
1861 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
1862 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
1863 torture_suite_add_1smb2_test(suite, "lock-oplock", test_durable_open_lock_oplock);
1864 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
1865 torture_suite_add_2smb2_test(suite, "open2-lease",
1866 test_durable_open_open2_lease);
1867 torture_suite_add_2smb2_test(suite, "open2-oplock",
1868 test_durable_open_open2_oplock);
1869 torture_suite_add_1smb2_test(suite, "alloc-size",
1870 test_durable_open_alloc_size);
1871 torture_suite_add_1smb2_test(suite, "read-only",
1872 test_durable_open_read_only);
1874 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
1879 struct torture_suite *torture_smb2_durable_open_disconnect_init(void)
1881 struct torture_suite *suite =
1882 torture_suite_create(talloc_autofree_context(),
1883 "durable-open-disconnect");
1885 torture_suite_add_1smb2_test(suite, "open-oplock-disconnect",
1886 test_durable_open_oplock_disconnect);
1888 suite->description = talloc_strdup(suite,
1889 "SMB2-DURABLE-OPEN-DISCONNECT tests");