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, incorrect) do { \
39 if ((v) == (incorrect)) { \
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)incorrect); \
45 #define CHECK_NOT_NULL(p) do { \
47 torture_result(tctx, TORTURE_FAIL, "(%s): %s is NULL but it should not be.\n", \
52 #define CHECK_STATUS(status, correct) do { \
53 if (!NT_STATUS_EQUAL(status, correct)) { \
54 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
55 nt_errstr(status), nt_errstr(correct)); \
60 #define CHECK_CREATED(__io, __created, __attribute) \
62 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
63 CHECK_VAL((__io)->out.alloc_size, 0); \
64 CHECK_VAL((__io)->out.size, 0); \
65 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
66 CHECK_VAL((__io)->out.reserved2, 0); \
69 #define CHECK_CREATED_SIZE(__io, __created, __attribute, __alloc_size, __size) \
71 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
72 CHECK_VAL((__io)->out.alloc_size, (__alloc_size)); \
73 CHECK_VAL((__io)->out.size, (__size)); \
74 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
75 CHECK_VAL((__io)->out.reserved2, 0); \
81 * basic durable_open test.
82 * durable state should only be granted when requested
83 * along with a batch oplock or a handle lease.
85 * This test tests durable open with all possible oplock types.
88 struct durable_open_vs_oplock {
90 const char *share_mode;
94 #define NUM_OPLOCK_TYPES 4
95 #define NUM_SHARE_MODES 8
96 #define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
97 static struct durable_open_vs_oplock durable_open_vs_oplock_table[NUM_OPLOCK_OPEN_TESTS] =
106 { "", "RWD", false },
112 { "s", "RD", false },
113 { "s", "RW", false },
114 { "s", "WD", false },
115 { "s", "RWD", false },
121 { "x", "RD", false },
122 { "x", "RW", false },
123 { "x", "WD", false },
124 { "x", "RWD", false },
133 { "b", "RWD", true },
136 static bool test_one_durable_open_open_oplock(struct torture_context *tctx,
137 struct smb2_tree *tree,
139 struct durable_open_vs_oplock test)
142 TALLOC_CTX *mem_ctx = talloc_new(tctx);
143 struct smb2_handle _h;
144 struct smb2_handle *h = NULL;
146 struct smb2_create io;
148 smb2_util_unlink(tree, fname);
150 smb2_oplock_create_share(&io, fname,
151 smb2_util_share_access(test.share_mode),
152 smb2_util_oplock_level(test.level));
153 io.in.durable_open = true;
155 status = smb2_create(tree, mem_ctx, &io);
156 CHECK_STATUS(status, NT_STATUS_OK);
157 _h = io.out.file.handle;
159 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
160 CHECK_VAL(io.out.durable_open, test.expected);
161 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(test.level));
165 smb2_util_close(tree, *h);
167 smb2_util_unlink(tree, fname);
168 talloc_free(mem_ctx);
173 static bool test_durable_open_open_oplock(struct torture_context *tctx,
174 struct smb2_tree *tree)
176 TALLOC_CTX *mem_ctx = talloc_new(tctx);
181 /* Choose a random name in case the state is left a little funky. */
182 snprintf(fname, 256, "durable_open_open_oplock_%s.dat", generate_random_str(tctx, 8));
184 smb2_util_unlink(tree, fname);
186 /* test various oplock levels with durable open */
188 for (i = 0; i < NUM_OPLOCK_OPEN_TESTS; i++) {
189 ret = test_one_durable_open_open_oplock(tctx,
192 durable_open_vs_oplock_table[i]);
199 smb2_util_unlink(tree, fname);
201 talloc_free(mem_ctx);
207 * basic durable_open test.
208 * durable state should only be granted when requested
209 * along with a batch oplock or a handle lease.
211 * This test tests durable open with all valid lease types.
214 struct durable_open_vs_lease {
216 const char *share_mode;
220 #define NUM_LEASE_TYPES 5
221 #define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
222 static struct durable_open_vs_lease durable_open_vs_lease_table[NUM_LEASE_OPEN_TESTS] =
231 { "", "RWD", false },
237 { "R", "RW", false },
238 { "R", "RD", false },
239 { "R", "DW", false },
240 { "R", "RWD", false },
243 { "RW", "R", false },
244 { "RW", "W", false },
245 { "RW", "D", false },
246 { "RW", "RW", false },
247 { "RW", "RD", false },
248 { "RW", "WD", false },
249 { "RW", "RWD", false },
255 { "RH", "RW", true },
256 { "RH", "RD", true },
257 { "RH", "WD", true },
258 { "RH", "RWD", true },
261 { "RHW", "R", true },
262 { "RHW", "W", true },
263 { "RHW", "D", true },
264 { "RHW", "RW", true },
265 { "RHW", "RD", true },
266 { "RHW", "WD", true },
267 { "RHW", "RWD", true },
270 static bool test_one_durable_open_open_lease(struct torture_context *tctx,
271 struct smb2_tree *tree,
273 struct durable_open_vs_lease test)
276 TALLOC_CTX *mem_ctx = talloc_new(tctx);
277 struct smb2_handle _h;
278 struct smb2_handle *h = NULL;
280 struct smb2_create io;
281 struct smb2_lease ls;
285 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
286 if (!(caps & SMB2_CAP_LEASING)) {
287 torture_skip(tctx, "leases are not supported");
290 smb2_util_unlink(tree, fname);
294 smb2_lease_create_share(&io, &ls, false /* dir */, fname,
295 smb2_util_share_access(test.share_mode),
297 smb2_util_lease_state(test.type));
298 io.in.durable_open = true;
300 status = smb2_create(tree, mem_ctx, &io);
301 CHECK_STATUS(status, NT_STATUS_OK);
302 _h = io.out.file.handle;
304 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
305 CHECK_VAL(io.out.durable_open, test.expected);
306 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
307 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
308 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
309 CHECK_VAL(io.out.lease_response.lease_state,
310 smb2_util_lease_state(test.type));
313 smb2_util_close(tree, *h);
315 smb2_util_unlink(tree, fname);
316 talloc_free(mem_ctx);
321 static bool test_durable_open_open_lease(struct torture_context *tctx,
322 struct smb2_tree *tree)
324 TALLOC_CTX *mem_ctx = talloc_new(tctx);
330 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
331 if (!(caps & SMB2_CAP_LEASING)) {
332 torture_skip(tctx, "leases are not supported");
335 /* Choose a random name in case the state is left a little funky. */
336 snprintf(fname, 256, "durable_open_open_lease_%s.dat", generate_random_str(tctx, 8));
338 smb2_util_unlink(tree, fname);
341 /* test various oplock levels with durable open */
343 for (i = 0; i < NUM_LEASE_OPEN_TESTS; i++) {
344 ret = test_one_durable_open_open_lease(tctx,
347 durable_open_vs_lease_table[i]);
354 smb2_util_unlink(tree, fname);
356 talloc_free(mem_ctx);
362 * basic test for doing a durable open
363 * and do a durable reopen on the same connection
364 * while the first open is still active (fails)
366 static bool test_durable_open_reopen1(struct torture_context *tctx,
367 struct smb2_tree *tree)
370 TALLOC_CTX *mem_ctx = talloc_new(tctx);
372 struct smb2_handle _h;
373 struct smb2_handle *h = NULL;
374 struct smb2_create io1, io2;
377 /* Choose a random name in case the state is left a little funky. */
378 snprintf(fname, 256, "durable_open_reopen1_%s.dat",
379 generate_random_str(tctx, 8));
381 smb2_util_unlink(tree, fname);
383 smb2_oplock_create_share(&io1, fname,
384 smb2_util_share_access(""),
385 smb2_util_oplock_level("b"));
386 io1.in.durable_open = true;
388 status = smb2_create(tree, mem_ctx, &io1);
389 CHECK_STATUS(status, NT_STATUS_OK);
390 _h = io1.out.file.handle;
392 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
393 CHECK_VAL(io1.out.durable_open, true);
394 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
396 /* try a durable reconnect while the file is still open */
398 io2.in.fname = fname;
399 io2.in.durable_handle = h;
401 status = smb2_create(tree, mem_ctx, &io2);
402 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
406 smb2_util_close(tree, *h);
409 smb2_util_unlink(tree, fname);
413 talloc_free(mem_ctx);
419 * Basic test for doing a durable open
420 * and do a session reconnect while the first
421 * session is still active and the handle is
422 * still open in the client.
423 * This closes the original session and a
424 * durable reconnect on the new session succeeds.
426 static bool test_durable_open_reopen1a(struct torture_context *tctx,
427 struct smb2_tree *tree)
430 TALLOC_CTX *mem_ctx = talloc_new(tctx);
432 struct smb2_handle _h;
433 struct smb2_handle *h = NULL;
434 struct smb2_create io1, io2;
436 struct smb2_tree *tree2 = NULL;
437 uint64_t previous_session_id;
438 struct smbcli_options options;
440 options = tree->session->transport->options;
442 /* Choose a random name in case the state is left a little funky. */
443 snprintf(fname, 256, "durable_open_reopen1a_%s.dat",
444 generate_random_str(tctx, 8));
446 smb2_util_unlink(tree, fname);
448 smb2_oplock_create_share(&io1, fname,
449 smb2_util_share_access(""),
450 smb2_util_oplock_level("b"));
451 io1.in.durable_open = true;
453 status = smb2_create(tree, mem_ctx, &io1);
454 CHECK_STATUS(status, NT_STATUS_OK);
455 _h = io1.out.file.handle;
457 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
458 CHECK_VAL(io1.out.durable_open, true);
459 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
462 * a session reconnect on a second tcp connection
465 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
467 ret = torture_smb2_connection_ext(tctx, previous_session_id,
469 torture_assert_goto(tctx, ret, ret, done, "could not reconnect");
472 * check that this has deleted the old session
476 io2.in.fname = fname;
477 io2.in.durable_handle = h;
479 status = smb2_create(tree, mem_ctx, &io2);
480 CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
485 * but a durable reconnect on the new session succeeds:
489 io2.in.fname = fname;
490 io2.in.durable_handle = h;
492 status = smb2_create(tree2, mem_ctx, &io2);
493 CHECK_STATUS(status, NT_STATUS_OK);
494 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
495 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
496 _h = io2.out.file.handle;
506 smb2_util_close(tree, *h);
509 smb2_util_unlink(tree, fname);
514 talloc_free(mem_ctx);
520 * basic test for doing a durable open
521 * tcp disconnect, reconnect, do a durable reopen (succeeds)
523 static bool test_durable_open_reopen2(struct torture_context *tctx,
524 struct smb2_tree *tree)
527 TALLOC_CTX *mem_ctx = talloc_new(tctx);
529 struct smb2_handle _h;
530 struct smb2_handle *h = NULL;
531 struct smb2_create io;
534 /* Choose a random name in case the state is left a little funky. */
535 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
536 generate_random_str(tctx, 8));
538 smb2_util_unlink(tree, fname);
540 smb2_oplock_create_share(&io, fname,
541 smb2_util_share_access(""),
542 smb2_util_oplock_level("b"));
543 io.in.durable_open = true;
545 status = smb2_create(tree, mem_ctx, &io);
546 CHECK_STATUS(status, NT_STATUS_OK);
547 _h = io.out.file.handle;
549 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
550 CHECK_VAL(io.out.durable_open, true);
551 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
553 /* disconnect, leaving the durable in place */
556 if (!torture_smb2_connection(tctx, &tree)) {
557 torture_warning(tctx, "couldn't reconnect, bailing\n");
563 /* the path name is ignored by the server */
565 io.in.durable_handle = h; /* durable v1 reconnect request */
568 status = smb2_create(tree, mem_ctx, &io);
569 CHECK_STATUS(status, NT_STATUS_OK);
570 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
571 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
572 _h = io.out.file.handle;
575 /* disconnect again, leaving the durable in place */
578 if (!torture_smb2_connection(tctx, &tree)) {
579 torture_warning(tctx, "couldn't reconnect, bailing\n");
585 * show that the filename and many other fields
586 * are ignored. only the reconnect request blob
590 /* the path name is ignored by the server */
591 io.in.security_flags = 0x78;
592 io.in.oplock_level = 0x78;
593 io.in.impersonation_level = 0x12345678;
594 io.in.create_flags = 0x12345678;
595 io.in.reserved = 0x12345678;
596 io.in.desired_access = 0x12345678;
597 io.in.file_attributes = 0x12345678;
598 io.in.share_access = 0x12345678;
599 io.in.create_disposition = 0x12345678;
600 io.in.create_options = 0x12345678;
601 io.in.fname = "__non_existing_fname__";
602 io.in.durable_handle = h; /* durable v1 reconnect request */
605 status = smb2_create(tree, mem_ctx, &io);
606 CHECK_STATUS(status, NT_STATUS_OK);
607 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
608 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
609 _h = io.out.file.handle;
612 /* disconnect, leaving the durable in place */
615 if (!torture_smb2_connection(tctx, &tree)) {
616 torture_warning(tctx, "couldn't reconnect, bailing\n");
622 * show that an additionally specified durable v1 request
623 * is ignored by the server.
624 * See MS-SMB2, 3.3.5.9.7
625 * Handling the SMB2_CREATE_DURABLE_HANDLE_RECONNECT Create Context
628 /* the path name is ignored by the server */
630 io.in.durable_handle = h; /* durable v1 reconnect request */
631 io.in.durable_open = true; /* durable v1 handle request */
634 status = smb2_create(tree, mem_ctx, &io);
635 CHECK_STATUS(status, NT_STATUS_OK);
636 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
637 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
638 _h = io.out.file.handle;
644 smb2_util_close(tree, *h);
647 smb2_util_unlink(tree, fname);
652 talloc_free(mem_ctx);
658 * lease variant of reopen2
659 * basic test for doing a durable open
660 * tcp disconnect, reconnect, do a durable reopen (succeeds)
662 static bool test_durable_open_reopen2_lease(struct torture_context *tctx,
663 struct smb2_tree *tree)
666 TALLOC_CTX *mem_ctx = talloc_new(tctx);
668 struct smb2_handle _h;
669 struct smb2_handle *h = NULL;
670 struct smb2_create io;
671 struct smb2_lease ls;
674 struct smbcli_options options;
677 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
678 if (!(caps & SMB2_CAP_LEASING)) {
679 torture_skip(tctx, "leases are not supported");
682 options = tree->session->transport->options;
684 /* Choose a random name in case the state is left a little funky. */
685 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
686 generate_random_str(tctx, 8));
688 smb2_util_unlink(tree, fname);
690 lease_key = random();
691 smb2_lease_create(&io, &ls, false /* dir */, fname, lease_key,
692 smb2_util_lease_state("RWH"));
693 io.in.durable_open = true;
695 status = smb2_create(tree, mem_ctx, &io);
696 CHECK_STATUS(status, NT_STATUS_OK);
697 _h = io.out.file.handle;
699 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
701 CHECK_VAL(io.out.durable_open, true);
702 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
703 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
704 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
705 CHECK_VAL(io.out.lease_response.lease_state,
706 smb2_util_lease_state("RWH"));
707 CHECK_VAL(io.out.lease_response.lease_flags, 0);
708 CHECK_VAL(io.out.lease_response.lease_duration, 0);
710 /* disconnect, reconnect and then do durable reopen */
713 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
714 torture_warning(tctx, "couldn't reconnect, bailing\n");
720 /* a few failure tests: */
723 * several attempts without lease attached:
724 * all fail with NT_STATUS_OBJECT_NAME_NOT_FOUND
725 * irrespective of file name provided
730 io.in.durable_handle = h;
731 status = smb2_create(tree, mem_ctx, &io);
732 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
735 io.in.fname = "__non_existing_fname__";
736 io.in.durable_handle = h;
737 status = smb2_create(tree, mem_ctx, &io);
738 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
742 io.in.durable_handle = h;
743 status = smb2_create(tree, mem_ctx, &io);
744 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
747 * attempt with lease provided, but
748 * with a changed lease key. => fails
752 io.in.durable_open = false;
753 io.in.durable_handle = h;
754 io.in.lease_request = &ls;
755 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
756 /* a wrong lease key lets the request fail */
757 ls.lease_key.data[0]++;
759 status = smb2_create(tree, mem_ctx, &io);
760 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
762 /* restore the correct lease key */
763 ls.lease_key.data[0]--;
766 * this last failing attempt is almost correct:
767 * only problem is: we use the wrong filename...
768 * Note that this gives INVALID_PARAMETER.
769 * This is different from oplocks!
772 io.in.fname = "__non_existing_fname__";
773 io.in.durable_open = false;
774 io.in.durable_handle = h;
775 io.in.lease_request = &ls;
776 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
778 status = smb2_create(tree, mem_ctx, &io);
779 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
782 * Now for a succeeding reconnect:
787 io.in.durable_open = false;
788 io.in.durable_handle = h;
789 io.in.lease_request = &ls;
790 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
792 /* the requested lease state is irrelevant */
793 ls.lease_state = smb2_util_lease_state("");
797 status = smb2_create(tree, mem_ctx, &io);
798 CHECK_STATUS(status, NT_STATUS_OK);
800 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
801 CHECK_VAL(io.out.durable_open, false);
802 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
803 CHECK_VAL(io.out.persistent_open, false);
804 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
805 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
806 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
807 CHECK_VAL(io.out.lease_response.lease_state,
808 smb2_util_lease_state("RWH"));
809 CHECK_VAL(io.out.lease_response.lease_flags, 0);
810 CHECK_VAL(io.out.lease_response.lease_duration, 0);
811 _h = io.out.file.handle;
814 /* disconnect one more time */
817 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
818 torture_warning(tctx, "couldn't reconnect, bailing\n");
824 * demonstrate that various parameters are ignored
830 * These are completely ignored by the server
832 io.in.security_flags = 0x78;
833 io.in.oplock_level = 0x78;
834 io.in.impersonation_level = 0x12345678;
835 io.in.create_flags = 0x12345678;
836 io.in.reserved = 0x12345678;
837 io.in.desired_access = 0x12345678;
838 io.in.file_attributes = 0x12345678;
839 io.in.share_access = 0x12345678;
840 io.in.create_disposition = 0x12345678;
841 io.in.create_options = 0x12345678;
844 * only these are checked:
846 * - io.in.durable_handle,
847 * - io.in.lease_request->lease_key
851 io.in.durable_open_v2 = false;
852 io.in.durable_handle_v2 = h;
853 io.in.lease_request = &ls;
855 /* the requested lease state is irrelevant */
856 ls.lease_state = smb2_util_lease_state("");
860 status = smb2_create(tree, mem_ctx, &io);
861 CHECK_STATUS(status, NT_STATUS_OK);
863 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
864 CHECK_VAL(io.out.durable_open, false);
865 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
866 CHECK_VAL(io.out.persistent_open, false);
867 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
868 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease_key);
869 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease_key);
870 CHECK_VAL(io.out.lease_response.lease_state,
871 smb2_util_lease_state("RWH"));
872 CHECK_VAL(io.out.lease_response.lease_flags, 0);
873 CHECK_VAL(io.out.lease_response.lease_duration, 0);
875 _h = io.out.file.handle;
881 smb2_util_close(tree, *h);
884 smb2_util_unlink(tree, fname);
889 talloc_free(mem_ctx);
895 * lease v2 variant of reopen2
896 * basic test for doing a durable open
897 * tcp disconnect, reconnect, do a durable reopen (succeeds)
899 static bool test_durable_open_reopen2_lease_v2(struct torture_context *tctx,
900 struct smb2_tree *tree)
903 TALLOC_CTX *mem_ctx = talloc_new(tctx);
905 struct smb2_handle _h;
906 struct smb2_handle *h = NULL;
907 struct smb2_create io;
908 struct smb2_lease ls;
911 struct smbcli_options options;
914 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
915 if (!(caps & SMB2_CAP_LEASING)) {
916 torture_skip(tctx, "leases are not supported");
919 options = tree->session->transport->options;
921 /* Choose a random name in case the state is left a little funky. */
922 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
923 generate_random_str(tctx, 8));
925 smb2_util_unlink(tree, fname);
927 lease_key = random();
928 smb2_lease_v2_create(&io, &ls, false /* dir */, fname,
929 lease_key, 0, /* parent lease key */
930 smb2_util_lease_state("RWH"), 0 /* lease epoch */);
931 io.in.durable_open = true;
933 status = smb2_create(tree, mem_ctx, &io);
934 CHECK_STATUS(status, NT_STATUS_OK);
935 _h = io.out.file.handle;
937 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
939 CHECK_VAL(io.out.durable_open, true);
940 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
941 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
942 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
943 CHECK_VAL(io.out.lease_response_v2.lease_state,
944 smb2_util_lease_state("RWH"));
945 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
946 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
948 /* disconnect, reconnect and then do durable reopen */
951 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
952 torture_warning(tctx, "couldn't reconnect, bailing\n");
957 /* a few failure tests: */
960 * several attempts without lease attached:
961 * all fail with NT_STATUS_OBJECT_NAME_NOT_FOUND
962 * irrespective of file name provided
967 io.in.durable_handle = h;
968 status = smb2_create(tree, mem_ctx, &io);
969 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
972 io.in.fname = "__non_existing_fname__";
973 io.in.durable_handle = h;
974 status = smb2_create(tree, mem_ctx, &io);
975 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
979 io.in.durable_handle = h;
980 status = smb2_create(tree, mem_ctx, &io);
981 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
984 * attempt with lease provided, but
985 * with a changed lease key. => fails
989 io.in.durable_open = false;
990 io.in.durable_handle = h;
991 io.in.lease_request_v2 = &ls;
992 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
993 /* a wrong lease key lets the request fail */
994 ls.lease_key.data[0]++;
996 status = smb2_create(tree, mem_ctx, &io);
997 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
999 /* restore the correct lease key */
1000 ls.lease_key.data[0]--;
1003 * this last failing attempt is almost correct:
1004 * only problem is: we use the wrong filename...
1005 * Note that this gives INVALID_PARAMETER.
1006 * This is different from oplocks!
1009 io.in.fname = "__non_existing_fname__";
1010 io.in.durable_open = false;
1011 io.in.durable_handle = h;
1012 io.in.lease_request_v2 = &ls;
1013 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
1015 status = smb2_create(tree, mem_ctx, &io);
1016 CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
1019 * Now for a succeeding reconnect:
1023 io.in.fname = fname;
1024 io.in.durable_open = false;
1025 io.in.durable_handle = h;
1026 io.in.lease_request_v2 = &ls;
1027 io.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
1029 /* the requested lease state is irrelevant */
1030 ls.lease_state = smb2_util_lease_state("");
1034 status = smb2_create(tree, mem_ctx, &io);
1035 CHECK_STATUS(status, NT_STATUS_OK);
1037 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1038 CHECK_VAL(io.out.durable_open, false);
1039 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1040 CHECK_VAL(io.out.persistent_open, false);
1041 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1042 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
1043 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
1044 CHECK_VAL(io.out.lease_response_v2.lease_state,
1045 smb2_util_lease_state("RWH"));
1046 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
1047 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
1048 _h = io.out.file.handle;
1051 /* disconnect one more time */
1054 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
1055 torture_warning(tctx, "couldn't reconnect, bailing\n");
1061 * demonstrate that various parameters are ignored
1067 * These are completely ignored by the server
1069 io.in.security_flags = 0x78;
1070 io.in.oplock_level = 0x78;
1071 io.in.impersonation_level = 0x12345678;
1072 io.in.create_flags = 0x12345678;
1073 io.in.reserved = 0x12345678;
1074 io.in.desired_access = 0x12345678;
1075 io.in.file_attributes = 0x12345678;
1076 io.in.share_access = 0x12345678;
1077 io.in.create_disposition = 0x12345678;
1078 io.in.create_options = 0x12345678;
1081 * only these are checked:
1083 * - io.in.durable_handle,
1084 * - io.in.lease_request->lease_key
1087 io.in.fname = fname;
1088 io.in.durable_open_v2 = false;
1089 io.in.durable_handle_v2 = h;
1090 io.in.lease_request_v2 = &ls;
1092 /* the requested lease state is irrelevant */
1093 ls.lease_state = smb2_util_lease_state("");
1097 status = smb2_create(tree, mem_ctx, &io);
1098 CHECK_STATUS(status, NT_STATUS_OK);
1100 CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1101 CHECK_VAL(io.out.durable_open, false);
1102 CHECK_VAL(io.out.durable_open_v2, false); /* no dh2q response blob */
1103 CHECK_VAL(io.out.persistent_open, false);
1104 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1105 CHECK_VAL(io.out.lease_response_v2.lease_key.data[0], lease_key);
1106 CHECK_VAL(io.out.lease_response_v2.lease_key.data[1], ~lease_key);
1107 CHECK_VAL(io.out.lease_response_v2.lease_state,
1108 smb2_util_lease_state("RWH"));
1109 CHECK_VAL(io.out.lease_response_v2.lease_flags, 0);
1110 CHECK_VAL(io.out.lease_response_v2.lease_duration, 0);
1112 _h = io.out.file.handle;
1118 smb2_util_close(tree, *h);
1121 smb2_util_unlink(tree, fname);
1126 talloc_free(mem_ctx);
1132 * basic test for doing a durable open
1133 * tcp disconnect, reconnect with a session reconnect and
1134 * do a durable reopen (succeeds)
1136 static bool test_durable_open_reopen2a(struct torture_context *tctx,
1137 struct smb2_tree *tree)
1140 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1142 struct smb2_handle _h;
1143 struct smb2_handle *h = NULL;
1144 struct smb2_create io1, io2;
1145 uint64_t previous_session_id;
1147 struct smbcli_options options;
1149 options = tree->session->transport->options;
1151 /* Choose a random name in case the state is left a little funky. */
1152 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
1153 generate_random_str(tctx, 8));
1155 smb2_util_unlink(tree, fname);
1157 smb2_oplock_create_share(&io1, fname,
1158 smb2_util_share_access(""),
1159 smb2_util_oplock_level("b"));
1160 io1.in.durable_open = true;
1162 status = smb2_create(tree, mem_ctx, &io1);
1163 CHECK_STATUS(status, NT_STATUS_OK);
1164 _h = io1.out.file.handle;
1166 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1167 CHECK_VAL(io1.out.durable_open, true);
1168 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1170 /* disconnect, reconnect and then do durable reopen */
1171 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1175 if (!torture_smb2_connection_ext(tctx, previous_session_id,
1178 torture_warning(tctx, "couldn't reconnect, bailing\n");
1184 io2.in.fname = fname;
1185 io2.in.durable_handle = h;
1188 status = smb2_create(tree, mem_ctx, &io2);
1189 CHECK_STATUS(status, NT_STATUS_OK);
1190 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1191 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1192 _h = io2.out.file.handle;
1198 smb2_util_close(tree, *h);
1201 smb2_util_unlink(tree, fname);
1206 talloc_free(mem_ctx);
1213 * basic test for doing a durable open:
1214 * tdis, new tcon, try durable reopen (fails)
1216 static bool test_durable_open_reopen3(struct torture_context *tctx,
1217 struct smb2_tree *tree)
1220 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1222 struct smb2_handle _h;
1223 struct smb2_handle *h = NULL;
1224 struct smb2_create io1, io2;
1226 struct smb2_tree *tree2;
1228 /* Choose a random name in case the state is left a little funky. */
1229 snprintf(fname, 256, "durable_open_reopen3_%s.dat",
1230 generate_random_str(tctx, 8));
1232 smb2_util_unlink(tree, fname);
1234 smb2_oplock_create_share(&io1, fname,
1235 smb2_util_share_access(""),
1236 smb2_util_oplock_level("b"));
1237 io1.in.durable_open = true;
1239 status = smb2_create(tree, mem_ctx, &io1);
1240 CHECK_STATUS(status, NT_STATUS_OK);
1241 _h = io1.out.file.handle;
1243 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1244 CHECK_VAL(io1.out.durable_open, true);
1245 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1247 /* disconnect, reconnect and then do durable reopen */
1248 status = smb2_tdis(tree);
1249 CHECK_STATUS(status, NT_STATUS_OK);
1251 if (!torture_smb2_tree_connect(tctx, tree->session, mem_ctx, &tree2)) {
1252 torture_warning(tctx, "couldn't reconnect to share, bailing\n");
1259 io2.in.fname = fname;
1260 io2.in.durable_handle = h;
1262 status = smb2_create(tree2, mem_ctx, &io2);
1263 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1268 smb2_util_close(tree, *h);
1271 smb2_util_unlink(tree2, fname);
1276 talloc_free(mem_ctx);
1282 * basic test for doing a durable open:
1283 * logoff, create a new session, do a durable reopen (succeeds)
1285 static bool test_durable_open_reopen4(struct torture_context *tctx,
1286 struct smb2_tree *tree)
1289 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1291 struct smb2_handle _h;
1292 struct smb2_handle *h = NULL;
1293 struct smb2_create io1, io2;
1295 struct smb2_transport *transport;
1296 struct smb2_session *session2;
1297 struct smb2_tree *tree2;
1299 /* Choose a random name in case the state is left a little funky. */
1300 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
1301 generate_random_str(tctx, 8));
1303 smb2_util_unlink(tree, fname);
1305 smb2_oplock_create_share(&io1, fname,
1306 smb2_util_share_access(""),
1307 smb2_util_oplock_level("b"));
1308 io1.in.durable_open = true;
1310 status = smb2_create(tree, mem_ctx, &io1);
1311 CHECK_STATUS(status, NT_STATUS_OK);
1312 _h = io1.out.file.handle;
1314 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1315 CHECK_VAL(io1.out.durable_open, true);
1316 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1319 * do a session logoff, establish a new session and tree
1320 * connect on the same transport, and try a durable reopen
1322 transport = tree->session->transport;
1323 status = smb2_logoff(tree->session);
1324 CHECK_STATUS(status, NT_STATUS_OK);
1326 if (!torture_smb2_session_setup(tctx, transport,
1327 0, /* previous_session_id */
1328 mem_ctx, &session2))
1330 torture_warning(tctx, "session setup failed.\n");
1336 * the session setup has talloc-stolen the transport,
1337 * so we can safely free the old tree+session for clarity
1341 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
1342 torture_warning(tctx, "tree connect failed.\n");
1348 io2.in.fname = fname;
1349 io2.in.durable_handle = h;
1352 status = smb2_create(tree2, mem_ctx, &io2);
1353 CHECK_STATUS(status, NT_STATUS_OK);
1355 _h = io2.out.file.handle;
1357 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1358 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1363 smb2_util_close(tree2, *h);
1366 smb2_util_unlink(tree2, fname);
1371 talloc_free(mem_ctx);
1376 static bool test_durable_open_delete_on_close1(struct torture_context *tctx,
1377 struct smb2_tree *tree)
1380 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1382 struct smb2_handle _h;
1383 struct smb2_handle *h = NULL;
1384 struct smb2_create io1, io2;
1388 /* Choose a random name in case the state is left a little funky. */
1389 snprintf(fname, 256, "durable_open_delete_on_close1_%s.dat",
1390 generate_random_str(tctx, 8));
1392 smb2_util_unlink(tree, fname);
1394 smb2_oplock_create_share(&io1, fname,
1395 smb2_util_share_access(""),
1396 smb2_util_oplock_level("b"));
1397 io1.in.durable_open = true;
1398 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1400 status = smb2_create(tree, mem_ctx, &io1);
1401 CHECK_STATUS(status, NT_STATUS_OK);
1402 _h = io1.out.file.handle;
1404 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1405 CHECK_VAL(io1.out.durable_open, true);
1406 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
1408 status = smb2_util_write(tree, *h, &b, 0, 1);
1409 CHECK_STATUS(status, NT_STATUS_OK);
1411 /* disconnect, leaving the durable handle in place */
1414 if (!torture_smb2_connection(tctx, &tree)) {
1415 torture_warning(tctx, "could not reconnect, bailing\n");
1421 * Open the file on the new connection again
1422 * and check that it has been newly created,
1423 * i.e. delete on close was effective on the disconnected handle.
1424 * Also check that the file is really empty,
1425 * the previously written byte gone.
1427 smb2_oplock_create_share(&io2, fname,
1428 smb2_util_share_access(""),
1429 smb2_util_oplock_level("b"));
1430 io2.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1432 status = smb2_create(tree, mem_ctx, &io2);
1433 CHECK_STATUS(status, NT_STATUS_OK);
1434 _h = io2.out.file.handle;
1436 CHECK_CREATED_SIZE(&io2, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
1437 CHECK_VAL(io2.out.durable_open, false);
1438 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
1443 smb2_util_close(tree, *h);
1446 smb2_util_unlink(tree, fname);
1451 talloc_free(mem_ctx);
1457 static bool test_durable_open_delete_on_close2(struct torture_context *tctx,
1458 struct smb2_tree *tree)
1461 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1463 struct smb2_handle _h;
1464 struct smb2_handle *h = NULL;
1465 struct smb2_create io;
1468 uint64_t previous_session_id;
1469 uint64_t alloc_size_step;
1470 struct smbcli_options options;
1472 options = tree->session->transport->options;
1474 /* Choose a random name in case the state is left a little funky. */
1475 snprintf(fname, 256, "durable_open_delete_on_close2_%s.dat",
1476 generate_random_str(tctx, 8));
1478 smb2_util_unlink(tree, fname);
1480 smb2_oplock_create_share(&io, fname,
1481 smb2_util_share_access(""),
1482 smb2_util_oplock_level("b"));
1483 io.in.durable_open = true;
1484 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1486 status = smb2_create(tree, mem_ctx, &io);
1487 CHECK_STATUS(status, NT_STATUS_OK);
1488 _h = io.out.file.handle;
1490 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1491 CHECK_VAL(io.out.durable_open, true);
1492 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1494 status = smb2_util_write(tree, *h, &b, 0, 1);
1495 CHECK_STATUS(status, NT_STATUS_OK);
1497 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1499 /* disconnect, leaving the durable handle in place */
1502 if (!torture_smb2_connection_ext(tctx, previous_session_id,
1505 torture_warning(tctx, "could not reconnect, bailing\n");
1511 io.in.fname = fname;
1512 io.in.durable_handle = h;
1514 status = smb2_create(tree, mem_ctx, &io);
1515 CHECK_STATUS(status, NT_STATUS_OK);
1516 _h = io.out.file.handle;
1518 alloc_size_step = io.out.alloc_size;
1519 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE, alloc_size_step, 1);
1520 CHECK_VAL(io.out.durable_open, false);
1521 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1523 /* close the file, thereby deleting it */
1524 smb2_util_close(tree, *h);
1525 status = smb2_logoff(tree->session);
1528 if (!torture_smb2_connection(tctx, &tree)) {
1529 torture_warning(tctx, "could not reconnect, bailing\n");
1535 * Open the file on the new connection again
1536 * and check that it has been newly created,
1537 * i.e. delete on close was effective on the reconnected handle.
1538 * Also check that the file is really empty,
1539 * the previously written byte gone.
1541 smb2_oplock_create_share(&io, fname,
1542 smb2_util_share_access(""),
1543 smb2_util_oplock_level("b"));
1544 io.in.durable_open = true;
1545 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
1547 status = smb2_create(tree, mem_ctx, &io);
1548 CHECK_STATUS(status, NT_STATUS_OK);
1549 _h = io.out.file.handle;
1551 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
1552 CHECK_VAL(io.out.durable_open, true);
1553 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1558 smb2_util_close(tree, *h);
1561 smb2_util_unlink(tree, fname);
1566 talloc_free(mem_ctx);
1572 basic testing of SMB2 durable opens
1573 regarding the position information on the handle
1575 static bool test_durable_open_file_position(struct torture_context *tctx,
1576 struct smb2_tree *tree)
1578 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1579 struct smb2_handle h;
1580 struct smb2_create io;
1582 const char *fname = "durable_open_position.dat";
1583 union smb_fileinfo qfinfo;
1584 union smb_setfileinfo sfinfo;
1587 uint64_t previous_session_id;
1588 struct smbcli_options options;
1590 options = tree->session->transport->options;
1592 smb2_util_unlink(tree, fname);
1594 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
1595 io.in.durable_open = true;
1597 status = smb2_create(tree, mem_ctx, &io);
1598 CHECK_STATUS(status, NT_STATUS_OK);
1599 h = io.out.file.handle;
1600 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1601 CHECK_VAL(io.out.durable_open, true);
1602 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1604 /* TODO: check extra blob content */
1606 ZERO_STRUCT(qfinfo);
1607 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1608 qfinfo.generic.in.file.handle = h;
1609 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1610 CHECK_STATUS(status, NT_STATUS_OK);
1611 CHECK_VAL(qfinfo.position_information.out.position, 0);
1612 pos = qfinfo.position_information.out.position;
1613 torture_comment(tctx, "position: %llu\n",
1614 (unsigned long long)pos);
1616 ZERO_STRUCT(sfinfo);
1617 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
1618 sfinfo.generic.in.file.handle = h;
1619 sfinfo.position_information.in.position = 0x1000;
1620 status = smb2_setinfo_file(tree, &sfinfo);
1621 CHECK_STATUS(status, NT_STATUS_OK);
1623 ZERO_STRUCT(qfinfo);
1624 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1625 qfinfo.generic.in.file.handle = h;
1626 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1627 CHECK_STATUS(status, NT_STATUS_OK);
1628 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
1629 pos = qfinfo.position_information.out.position;
1630 torture_comment(tctx, "position: %llu\n",
1631 (unsigned long long)pos);
1633 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1635 /* tcp disconnect */
1639 /* do a session reconnect */
1640 if (!torture_smb2_connection_ext(tctx, previous_session_id,
1643 torture_warning(tctx, "couldn't reconnect, bailing\n");
1648 ZERO_STRUCT(qfinfo);
1649 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1650 qfinfo.generic.in.file.handle = h;
1651 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1652 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
1655 io.in.fname = fname;
1656 io.in.durable_handle = &h;
1658 status = smb2_create(tree, mem_ctx, &io);
1659 CHECK_STATUS(status, NT_STATUS_OK);
1660 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1661 CHECK_VAL(io.out.reserved, 0x00);
1662 CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
1663 CHECK_VAL(io.out.alloc_size, 0);
1664 CHECK_VAL(io.out.size, 0);
1665 CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
1666 CHECK_VAL(io.out.reserved2, 0);
1668 h = io.out.file.handle;
1670 ZERO_STRUCT(qfinfo);
1671 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1672 qfinfo.generic.in.file.handle = h;
1673 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1674 CHECK_STATUS(status, NT_STATUS_OK);
1675 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
1676 pos = qfinfo.position_information.out.position;
1677 torture_comment(tctx, "position: %llu\n",
1678 (unsigned long long)pos);
1680 smb2_util_close(tree, h);
1682 talloc_free(mem_ctx);
1684 smb2_util_unlink(tree, fname);
1693 Open, disconnect, oplock break, reconnect.
1695 static bool test_durable_open_oplock(struct torture_context *tctx,
1696 struct smb2_tree *tree1,
1697 struct smb2_tree *tree2)
1699 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1700 struct smb2_create io1, io2;
1701 struct smb2_handle h1, h2;
1706 /* Choose a random name in case the state is left a little funky. */
1707 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
1710 smb2_util_unlink(tree1, fname);
1712 /* Create with batch oplock */
1713 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1714 io1.in.durable_open = true;
1717 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1719 status = smb2_create(tree1, mem_ctx, &io1);
1720 CHECK_STATUS(status, NT_STATUS_OK);
1721 h1 = io1.out.file.handle;
1722 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1723 CHECK_VAL(io1.out.durable_open, true);
1724 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1726 /* Disconnect after getting the batch */
1731 * Windows7 (build 7000) will break a batch oplock immediately if the
1732 * original client is gone. (ZML: This seems like a bug. It should give
1733 * some time for the client to reconnect!)
1735 status = smb2_create(tree2, mem_ctx, &io2);
1736 CHECK_STATUS(status, NT_STATUS_OK);
1737 h2 = io2.out.file.handle;
1738 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1739 CHECK_VAL(io2.out.durable_open, true);
1740 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1742 /* What if tree1 tries to come back and reclaim? */
1743 if (!torture_smb2_connection(tctx, &tree1)) {
1744 torture_warning(tctx, "couldn't reconnect, bailing\n");
1750 io1.in.fname = fname;
1751 io1.in.durable_handle = &h1;
1753 status = smb2_create(tree1, mem_ctx, &io1);
1754 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1757 smb2_util_close(tree2, h2);
1758 smb2_util_unlink(tree2, fname);
1767 Open, disconnect, lease break, reconnect.
1769 static bool test_durable_open_lease(struct torture_context *tctx,
1770 struct smb2_tree *tree1,
1771 struct smb2_tree *tree2)
1773 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1774 struct smb2_create io1, io2;
1775 struct smb2_lease ls1, ls2;
1776 struct smb2_handle h1, h2;
1780 uint64_t lease1, lease2;
1783 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1784 if (!(caps & SMB2_CAP_LEASING)) {
1785 torture_skip(tctx, "leases are not supported");
1789 * Choose a random name and random lease in case the state is left a
1794 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
1797 smb2_util_unlink(tree1, fname);
1799 /* Create with lease */
1800 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
1801 lease1, smb2_util_lease_state("RHW"));
1802 io1.in.durable_open = true;
1804 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
1805 lease2, smb2_util_lease_state("RHW"));
1806 io2.in.durable_open = true;
1807 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1809 status = smb2_create(tree1, mem_ctx, &io1);
1810 CHECK_STATUS(status, NT_STATUS_OK);
1811 h1 = io1.out.file.handle;
1812 CHECK_VAL(io1.out.durable_open, true);
1813 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1815 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1816 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
1817 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
1818 CHECK_VAL(io1.out.lease_response.lease_state,
1819 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1821 /* Disconnect after getting the lease */
1826 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1827 * even if the original client is gone. (ZML: This seems like a bug. It
1828 * should give some time for the client to reconnect! And why RH?)
1830 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1831 * Test is adapted accordingly.
1833 status = smb2_create(tree2, mem_ctx, &io2);
1834 CHECK_STATUS(status, NT_STATUS_OK);
1835 h2 = io2.out.file.handle;
1836 CHECK_VAL(io2.out.durable_open, true);
1837 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1839 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1840 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
1841 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
1842 CHECK_VAL(io2.out.lease_response.lease_state,
1843 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1845 /* What if tree1 tries to come back and reclaim? */
1846 if (!torture_smb2_connection(tctx, &tree1)) {
1847 torture_warning(tctx, "couldn't reconnect, bailing\n");
1853 io1.in.fname = fname;
1854 io1.in.durable_handle = &h1;
1855 io1.in.lease_request = &ls1;
1857 status = smb2_create(tree1, mem_ctx, &io1);
1858 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1861 smb2_util_close(tree2, h2);
1862 smb2_util_unlink(tree2, fname);
1870 static bool test_durable_open_lock_oplock(struct torture_context *tctx,
1871 struct smb2_tree *tree)
1873 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1874 struct smb2_create io;
1875 struct smb2_handle h;
1876 struct smb2_lock lck;
1877 struct smb2_lock_element el[2];
1884 snprintf(fname, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx, 8));
1887 smb2_util_unlink(tree, fname);
1889 /* Create with oplock */
1891 smb2_oplock_create_share(&io, fname,
1892 smb2_util_share_access(""),
1893 smb2_util_oplock_level("b"));
1894 io.in.durable_open = true;
1896 status = smb2_create(tree, mem_ctx, &io);
1897 CHECK_STATUS(status, NT_STATUS_OK);
1898 h = io.out.file.handle;
1899 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1901 CHECK_VAL(io.out.durable_open, true);
1902 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1907 lck.in.lock_count = 0x0001;
1908 lck.in.lock_sequence = 0x00000000;
1909 lck.in.file.handle = h;
1912 el[0].reserved = 0x00000000;
1913 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1914 status = smb2_lock(tree, &lck);
1915 CHECK_STATUS(status, NT_STATUS_OK);
1917 /* Disconnect/Reconnect. */
1921 if (!torture_smb2_connection(tctx, &tree)) {
1922 torture_warning(tctx, "couldn't reconnect, bailing\n");
1928 io.in.fname = fname;
1929 io.in.durable_handle = &h;
1931 status = smb2_create(tree, mem_ctx, &io);
1932 CHECK_STATUS(status, NT_STATUS_OK);
1933 h = io.out.file.handle;
1935 lck.in.file.handle = h;
1936 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1937 status = smb2_lock(tree, &lck);
1938 CHECK_STATUS(status, NT_STATUS_OK);
1941 smb2_util_close(tree, h);
1942 smb2_util_unlink(tree, fname);
1949 Open, take BRL, disconnect, reconnect.
1951 static bool test_durable_open_lock_lease(struct torture_context *tctx,
1952 struct smb2_tree *tree)
1954 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1955 struct smb2_create io;
1956 struct smb2_lease ls;
1957 struct smb2_handle h;
1958 struct smb2_lock lck;
1959 struct smb2_lock_element el[2];
1965 struct smbcli_options options;
1967 options = tree->session->transport->options;
1969 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1970 if (!(caps & SMB2_CAP_LEASING)) {
1971 torture_skip(tctx, "leases are not supported");
1975 * Choose a random name and random lease in case the state is left a
1979 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
1982 smb2_util_unlink(tree, fname);
1984 /* Create with lease */
1986 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
1987 smb2_util_lease_state("RWH"));
1988 io.in.durable_open = true;
1990 status = smb2_create(tree, mem_ctx, &io);
1991 CHECK_STATUS(status, NT_STATUS_OK);
1992 h = io.out.file.handle;
1993 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1995 CHECK_VAL(io.out.durable_open, true);
1996 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1997 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
1998 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
1999 CHECK_VAL(io.out.lease_response.lease_state,
2000 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
2005 lck.in.lock_count = 0x0001;
2006 lck.in.lock_sequence = 0x00000000;
2007 lck.in.file.handle = h;
2010 el[0].reserved = 0x00000000;
2011 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
2012 status = smb2_lock(tree, &lck);
2013 CHECK_STATUS(status, NT_STATUS_OK);
2015 /* Disconnect/Reconnect. */
2019 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree)) {
2020 torture_warning(tctx, "couldn't reconnect, bailing\n");
2026 io.in.fname = fname;
2027 io.in.durable_handle = &h;
2028 io.in.lease_request = &ls;
2030 status = smb2_create(tree, mem_ctx, &io);
2031 CHECK_STATUS(status, NT_STATUS_OK);
2032 h = io.out.file.handle;
2034 lck.in.file.handle = h;
2035 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
2036 status = smb2_lock(tree, &lck);
2037 CHECK_STATUS(status, NT_STATUS_OK);
2040 smb2_util_close(tree, h);
2041 smb2_util_unlink(tree, fname);
2048 * Open with a RH lease, disconnect, open in another tree, reconnect.
2050 * This test actually demonstrates a minimum level of respect for the durable
2051 * open in the face of another open. As long as this test shows an inability to
2052 * reconnect after an open, the oplock/lease tests above will certainly
2053 * demonstrate an error on reconnect.
2055 static bool test_durable_open_open2_lease(struct torture_context *tctx,
2056 struct smb2_tree *tree1,
2057 struct smb2_tree *tree2)
2059 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2060 struct smb2_create io1, io2;
2061 struct smb2_lease ls;
2062 struct smb2_handle h1, h2;
2068 struct smbcli_options options;
2070 options = tree1->session->transport->options;
2072 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
2073 if (!(caps & SMB2_CAP_LEASING)) {
2074 torture_skip(tctx, "leases are not supported");
2078 * Choose a random name and random lease in case the state is left a
2082 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
2083 generate_random_str(tctx, 8));
2086 smb2_util_unlink(tree1, fname);
2088 /* Create with lease */
2089 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
2090 smb2_util_share_access(""),
2092 smb2_util_lease_state("RH"));
2093 io1.in.durable_open = true;
2095 status = smb2_create(tree1, mem_ctx, &io1);
2096 CHECK_STATUS(status, NT_STATUS_OK);
2097 h1 = io1.out.file.handle;
2098 CHECK_VAL(io1.out.durable_open, true);
2099 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2101 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
2102 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
2103 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
2104 CHECK_VAL(io1.out.lease_response.lease_state,
2105 smb2_util_lease_state("RH"));
2111 /* Open the file in tree2 */
2112 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
2114 status = smb2_create(tree2, mem_ctx, &io2);
2115 CHECK_STATUS(status, NT_STATUS_OK);
2116 h2 = io2.out.file.handle;
2117 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
2120 if (!torture_smb2_connection_ext(tctx, 0, &options, &tree1)) {
2121 torture_warning(tctx, "couldn't reconnect, bailing\n");
2127 io1.in.fname = fname;
2128 io1.in.durable_handle = &h1;
2129 io1.in.lease_request = &ls;
2132 * Windows7 (build 7000) will give away an open immediately if the
2133 * original client is gone. (ZML: This seems like a bug. It should give
2134 * some time for the client to reconnect!)
2136 status = smb2_create(tree1, mem_ctx, &io1);
2137 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2138 h1 = io1.out.file.handle;
2142 smb2_util_close(tree1, h1);
2143 smb2_util_unlink(tree1, fname);
2147 smb2_util_close(tree2, h2);
2148 smb2_util_unlink(tree2, fname);
2155 * Open with a batch oplock, disconnect, open in another tree, reconnect.
2157 * This test actually demonstrates a minimum level of respect for the durable
2158 * open in the face of another open. As long as this test shows an inability to
2159 * reconnect after an open, the oplock/lease tests above will certainly
2160 * demonstrate an error on reconnect.
2162 static bool test_durable_open_open2_oplock(struct torture_context *tctx,
2163 struct smb2_tree *tree1,
2164 struct smb2_tree *tree2)
2166 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2167 struct smb2_create io1, io2;
2168 struct smb2_handle h1, h2;
2174 * Choose a random name and random lease in case the state is left a
2177 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
2178 generate_random_str(tctx, 8));
2181 smb2_util_unlink(tree1, fname);
2183 /* Create with batch oplock */
2184 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
2185 io1.in.durable_open = true;
2187 status = smb2_create(tree1, mem_ctx, &io1);
2188 CHECK_STATUS(status, NT_STATUS_OK);
2189 h1 = io1.out.file.handle;
2190 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2191 CHECK_VAL(io1.out.durable_open, true);
2192 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2198 /* Open the file in tree2 */
2199 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
2201 status = smb2_create(tree2, mem_ctx, &io2);
2202 CHECK_STATUS(status, NT_STATUS_OK);
2203 h2 = io2.out.file.handle;
2204 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2207 if (!torture_smb2_connection(tctx, &tree1)) {
2208 torture_warning(tctx, "couldn't reconnect, bailing\n");
2214 io1.in.fname = fname;
2215 io1.in.durable_handle = &h1;
2217 status = smb2_create(tree1, mem_ctx, &io1);
2218 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
2219 h1 = io1.out.file.handle;
2222 smb2_util_close(tree2, h2);
2223 smb2_util_unlink(tree2, fname);
2224 if (tree1 != NULL) {
2225 smb2_util_close(tree1, h1);
2226 smb2_util_unlink(tree1, fname);
2236 * test behaviour with initial allocation size
2238 static bool test_durable_open_alloc_size(struct torture_context *tctx,
2239 struct smb2_tree *tree)
2242 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2244 struct smb2_handle _h;
2245 struct smb2_handle *h = NULL;
2246 struct smb2_create io;
2248 uint64_t previous_session_id;
2249 uint64_t alloc_size_step;
2250 uint64_t initial_alloc_size = 0x100;
2251 const uint8_t *b = NULL;
2252 struct smbcli_options options;
2254 options = tree->session->transport->options;
2256 /* Choose a random name in case the state is left a little funky. */
2257 snprintf(fname, 256, "durable_open_alloc_size_%s.dat",
2258 generate_random_str(tctx, 8));
2260 smb2_util_unlink(tree, fname);
2262 smb2_oplock_create_share(&io, fname,
2263 smb2_util_share_access(""),
2264 smb2_util_oplock_level("b"));
2265 io.in.durable_open = true;
2266 io.in.alloc_size = initial_alloc_size;
2268 status = smb2_create(tree, mem_ctx, &io);
2269 CHECK_STATUS(status, NT_STATUS_OK);
2270 _h = io.out.file.handle;
2272 CHECK_NOT_VAL(io.out.alloc_size, 0);
2273 alloc_size_step = io.out.alloc_size;
2274 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE,
2275 alloc_size_step, 0);
2276 CHECK_VAL(io.out.durable_open, true);
2277 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2279 /* prepare buffer */
2280 b = talloc_zero_size(mem_ctx, alloc_size_step);
2283 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2285 /* disconnect, reconnect and then do durable reopen */
2289 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2292 torture_warning(tctx, "couldn't reconnect, bailing\n");
2298 io.in.fname = fname;
2299 io.in.durable_handle = h;
2302 status = smb2_create(tree, mem_ctx, &io);
2303 CHECK_STATUS(status, NT_STATUS_OK);
2304 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2305 alloc_size_step, 0);
2306 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2307 _h = io.out.file.handle;
2310 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2312 /* write one byte */
2313 status = smb2_util_write(tree, *h, b, 0, 1);
2314 CHECK_STATUS(status, NT_STATUS_OK);
2316 /* disconnect, reconnect and then do durable reopen */
2320 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2323 torture_warning(tctx, "couldn't reconnect, bailing\n");
2329 io.in.fname = fname;
2330 io.in.durable_handle = h;
2333 status = smb2_create(tree, mem_ctx, &io);
2334 CHECK_STATUS(status, NT_STATUS_OK);
2335 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2336 alloc_size_step, 1);
2337 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2338 _h = io.out.file.handle;
2341 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2343 /* write more byte than initial allocation size */
2344 status = smb2_util_write(tree, *h, b, 1, alloc_size_step);
2346 /* disconnect, reconnect and then do durable reopen */
2350 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2353 torture_warning(tctx, "couldn't reconnect, bailing\n");
2359 io.in.fname = fname;
2360 io.in.durable_handle = h;
2363 status = smb2_create(tree, mem_ctx, &io);
2364 CHECK_STATUS(status, NT_STATUS_OK);
2365 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
2366 alloc_size_step * 2, alloc_size_step + 1);
2367 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2368 _h = io.out.file.handle;
2373 smb2_util_close(tree, *h);
2376 smb2_util_unlink(tree, fname);
2380 talloc_free(mem_ctx);
2386 * test behaviour when a disconnect happens while creating a read-only file
2388 static bool test_durable_open_read_only(struct torture_context *tctx,
2389 struct smb2_tree *tree)
2392 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2394 struct smb2_handle _h;
2395 struct smb2_handle *h = NULL;
2396 struct smb2_create io;
2398 uint64_t previous_session_id;
2399 const uint8_t b = 0;
2400 uint64_t alloc_size = 0;
2401 struct smbcli_options options;
2403 options = tree->session->transport->options;
2405 /* Choose a random name in case the state is left a little funky. */
2406 snprintf(fname, 256, "durable_open_initial_alloc_%s.dat",
2407 generate_random_str(tctx, 8));
2409 smb2_util_unlink(tree, fname);
2411 smb2_oplock_create_share(&io, fname,
2412 smb2_util_share_access(""),
2413 smb2_util_oplock_level("b"));
2414 io.in.durable_open = true;
2415 io.in.file_attributes = FILE_ATTRIBUTE_READONLY;
2417 status = smb2_create(tree, mem_ctx, &io);
2418 CHECK_STATUS(status, NT_STATUS_OK);
2419 _h = io.out.file.handle;
2421 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE);
2422 CHECK_VAL(io.out.durable_open, true);
2423 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2425 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
2427 /* write one byte */
2428 status = smb2_util_write(tree, *h, &b, 0, 1);
2429 CHECK_STATUS(status, NT_STATUS_OK);
2431 /* disconnect, reconnect and then do durable reopen */
2435 if (!torture_smb2_connection_ext(tctx, previous_session_id,
2438 torture_warning(tctx, "couldn't reconnect, bailing\n");
2444 io.in.fname = fname;
2445 io.in.durable_handle = h;
2448 status = smb2_create(tree, mem_ctx, &io);
2449 CHECK_STATUS(status, NT_STATUS_OK);
2450 alloc_size = io.out.alloc_size;
2451 CHECK_CREATED_SIZE(&io, EXISTED,
2452 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE,
2454 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
2455 _h = io.out.file.handle;
2458 /* write one byte */
2459 status = smb2_util_write(tree, *h, &b, 1, 1);
2460 CHECK_STATUS(status, NT_STATUS_OK);
2464 union smb_setfileinfo sfinfo;
2466 ZERO_STRUCT(sfinfo);
2467 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
2468 sfinfo.basic_info.in.file.handle = *h;
2469 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
2470 smb2_setinfo_file(tree, &sfinfo);
2472 smb2_util_close(tree, *h);
2475 smb2_util_unlink(tree, fname);
2479 talloc_free(mem_ctx);
2485 * durable open with oplock, disconnect, exit
2487 static bool test_durable_open_oplock_disconnect(struct torture_context *tctx,
2488 struct smb2_tree *tree)
2490 TALLOC_CTX *mem_ctx = talloc_new(tctx);
2491 struct smb2_create io;
2492 struct smb2_handle _h;
2493 struct smb2_handle *h = NULL;
2498 snprintf(fname, 256, "durable_open_oplock_disconnect_%s.dat",
2499 generate_random_str(mem_ctx, 8));
2501 smb2_util_unlink(tree, fname);
2503 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
2504 io.in.durable_open = true;
2506 status = smb2_create(tree, mem_ctx, &io);
2507 CHECK_STATUS(status, NT_STATUS_OK);
2509 _h = io.out.file.handle;
2512 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
2513 CHECK_VAL(io.out.durable_open, true);
2514 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
2523 smb2_util_close(tree, *h);
2525 smb2_util_unlink(tree, fname);
2527 talloc_free(mem_ctx);
2532 struct torture_suite *torture_smb2_durable_open_init(void)
2534 struct torture_suite *suite =
2535 torture_suite_create(talloc_autofree_context(), "durable-open");
2537 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
2538 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
2539 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
2540 torture_suite_add_1smb2_test(suite, "reopen1a", test_durable_open_reopen1a);
2541 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
2542 torture_suite_add_1smb2_test(suite, "reopen2-lease", test_durable_open_reopen2_lease);
2543 torture_suite_add_1smb2_test(suite, "reopen2-lease-v2", test_durable_open_reopen2_lease_v2);
2544 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
2545 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
2546 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
2547 torture_suite_add_1smb2_test(suite, "delete_on_close1",
2548 test_durable_open_delete_on_close1);
2549 torture_suite_add_1smb2_test(suite, "delete_on_close2",
2550 test_durable_open_delete_on_close2);
2551 torture_suite_add_1smb2_test(suite, "file-position",
2552 test_durable_open_file_position);
2553 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
2554 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
2555 torture_suite_add_1smb2_test(suite, "lock-oplock", test_durable_open_lock_oplock);
2556 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
2557 torture_suite_add_2smb2_test(suite, "open2-lease",
2558 test_durable_open_open2_lease);
2559 torture_suite_add_2smb2_test(suite, "open2-oplock",
2560 test_durable_open_open2_oplock);
2561 torture_suite_add_1smb2_test(suite, "alloc-size",
2562 test_durable_open_alloc_size);
2563 torture_suite_add_1smb2_test(suite, "read-only",
2564 test_durable_open_read_only);
2566 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
2571 struct torture_suite *torture_smb2_durable_open_disconnect_init(void)
2573 struct torture_suite *suite =
2574 torture_suite_create(talloc_autofree_context(),
2575 "durable-open-disconnect");
2577 torture_suite_add_1smb2_test(suite, "open-oplock-disconnect",
2578 test_durable_open_oplock_disconnect);
2580 suite->description = talloc_strdup(suite,
2581 "SMB2-DURABLE-OPEN-DISCONNECT tests");