s4:torture: fix segfault in test_durable_open_open2_oplock()
[Samba/gebeck_regimport.git] / source4 / torture / smb2 / durable_open.c
blobe3d9185bdcf64f8cdd693363526bc3744314b8e9
1 /*
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/>.
23 #include "includes.h"
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); \
35 ret = false; \
36 }} while (0)
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); \
42 ret = false; \
43 }} while (0)
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)); \
49 ret = false; \
50 goto done; \
51 }} while (0)
53 #define CHECK_CREATED(__io, __created, __attribute) \
54 do { \
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); \
60 } while(0)
62 #define CHECK_CREATED_SIZE(__io, __created, __attribute, __alloc_size, __size) \
63 do { \
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); \
69 } while(0)
73 /**
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 {
82 const char *level;
83 const char *share_mode;
84 bool expected;
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] =
92 { "", "", false },
93 { "", "R", false },
94 { "", "W", false },
95 { "", "D", false },
96 { "", "RD", false },
97 { "", "RW", false },
98 { "", "WD", false },
99 { "", "RWD", false },
101 { "s", "", false },
102 { "s", "R", false },
103 { "s", "W", false },
104 { "s", "D", false },
105 { "s", "RD", false },
106 { "s", "RW", false },
107 { "s", "WD", false },
108 { "s", "RWD", false },
110 { "x", "", false },
111 { "x", "R", false },
112 { "x", "W", false },
113 { "x", "D", false },
114 { "x", "RD", false },
115 { "x", "RW", false },
116 { "x", "WD", false },
117 { "x", "RWD", false },
119 { "b", "", true },
120 { "b", "R", true },
121 { "b", "W", true },
122 { "b", "D", true },
123 { "b", "RD", true },
124 { "b", "RW", true },
125 { "b", "WD", true },
126 { "b", "RWD", true },
129 static bool test_one_durable_open_open_oplock(struct torture_context *tctx,
130 struct smb2_tree *tree,
131 const char *fname,
132 struct durable_open_vs_oplock test)
134 NTSTATUS status;
135 TALLOC_CTX *mem_ctx = talloc_new(tctx);
136 struct smb2_handle _h;
137 struct smb2_handle *h = NULL;
138 bool ret = true;
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;
151 h = &_h;
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));
156 done:
157 if (h != NULL) {
158 smb2_util_close(tree, *h);
160 smb2_util_unlink(tree, fname);
161 talloc_free(mem_ctx);
163 return ret;
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);
170 char fname[256];
171 bool ret = true;
172 int i;
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,
183 tree,
184 fname,
185 durable_open_vs_oplock_table[i]);
186 if (ret == false) {
187 goto done;
191 done:
192 smb2_util_unlink(tree, fname);
193 talloc_free(tree);
194 talloc_free(mem_ctx);
196 return ret;
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 {
208 const char *type;
209 const char *share_mode;
210 bool expected;
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] =
217 { "", "", false },
218 { "", "R", false },
219 { "", "W", false },
220 { "", "D", false },
221 { "", "RW", false },
222 { "", "RD", false },
223 { "", "WD", false },
224 { "", "RWD", false },
226 { "R", "", false },
227 { "R", "R", false },
228 { "R", "W", false },
229 { "R", "D", false },
230 { "R", "RW", false },
231 { "R", "RD", false },
232 { "R", "DW", false },
233 { "R", "RWD", false },
235 { "RW", "", 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 },
244 { "RH", "", true },
245 { "RH", "R", true },
246 { "RH", "W", true },
247 { "RH", "D", true },
248 { "RH", "RW", true },
249 { "RH", "RD", true },
250 { "RH", "WD", true },
251 { "RH", "RWD", true },
253 { "RHW", "", 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,
265 const char *fname,
266 struct durable_open_vs_lease test)
268 NTSTATUS status;
269 TALLOC_CTX *mem_ctx = talloc_new(tctx);
270 struct smb2_handle _h;
271 struct smb2_handle *h = NULL;
272 bool ret = true;
273 struct smb2_create io;
274 struct smb2_lease ls;
275 uint64_t lease;
276 uint32_t caps;
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);
285 lease = random();
287 smb2_lease_create_share(&io, &ls, false /* dir */, fname,
288 smb2_util_share_access(test.share_mode),
289 lease,
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;
296 h = &_h;
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));
304 done:
305 if (h != NULL) {
306 smb2_util_close(tree, *h);
308 smb2_util_unlink(tree, fname);
309 talloc_free(mem_ctx);
311 return ret;
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);
318 char fname[256];
319 bool ret = true;
320 int i;
321 uint32_t caps;
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,
338 tree,
339 fname,
340 durable_open_vs_lease_table[i]);
341 if (ret == false) {
342 goto done;
346 done:
347 smb2_util_unlink(tree, fname);
348 talloc_free(tree);
349 talloc_free(mem_ctx);
351 return ret;
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)
362 NTSTATUS status;
363 TALLOC_CTX *mem_ctx = talloc_new(tctx);
364 char fname[256];
365 struct smb2_handle _h;
366 struct smb2_handle *h = NULL;
367 struct smb2_create io1, io2;
368 bool ret = true;
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;
384 h = &_h;
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 */
390 ZERO_STRUCT(io2);
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);
397 done:
398 if (h != NULL) {
399 smb2_util_close(tree, *h);
402 smb2_util_unlink(tree, fname);
404 talloc_free(tree);
406 talloc_free(mem_ctx);
408 return ret;
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)
418 NTSTATUS status;
419 TALLOC_CTX *mem_ctx = talloc_new(tctx);
420 char fname[256];
421 struct smb2_handle _h;
422 struct smb2_handle *h = NULL;
423 struct smb2_create io1, io2;
424 bool ret = true;
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;
440 h = &_h;
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 */
446 talloc_free(tree);
447 tree = NULL;
449 if (!torture_smb2_connection(tctx, &tree)) {
450 torture_warning(tctx, "couldn't reconnect, bailing\n");
451 ret = false;
452 goto done;
455 ZERO_STRUCT(io2);
456 /* the path name is ignored by the server */
457 io2.in.fname = "__non_existing_fname__";
458 io2.in.durable_handle = h;
459 h = NULL;
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;
466 h = &_h;
468 done:
469 if (tree != NULL) {
470 if (h != NULL) {
471 smb2_util_close(tree, *h);
474 smb2_util_unlink(tree, fname);
476 talloc_free(tree);
479 talloc_free(mem_ctx);
481 return ret;
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)
492 NTSTATUS status;
493 TALLOC_CTX *mem_ctx = talloc_new(tctx);
494 char fname[256];
495 struct smb2_handle _h;
496 struct smb2_handle *h = NULL;
497 struct smb2_create io1, io2;
498 uint64_t previous_session_id;
499 bool ret = true;
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;
515 h = &_h;
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);
522 talloc_free(tree);
523 tree = NULL;
525 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
526 torture_warning(tctx, "couldn't reconnect, bailing\n");
527 ret = false;
528 goto done;
531 ZERO_STRUCT(io2);
532 io2.in.fname = fname;
533 io2.in.durable_handle = h;
534 h = NULL;
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;
541 h = &_h;
543 done:
544 if (tree != NULL) {
545 if (h != NULL) {
546 smb2_util_close(tree, *h);
549 smb2_util_unlink(tree, fname);
551 talloc_free(tree);
554 talloc_free(mem_ctx);
556 return ret;
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)
567 NTSTATUS status;
568 TALLOC_CTX *mem_ctx = talloc_new(tctx);
569 char fname[256];
570 struct smb2_handle _h;
571 struct smb2_handle *h = NULL;
572 struct smb2_create io1, io2;
573 bool ret = true;
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;
590 h = &_h;
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");
601 ret = false;
602 goto done;
606 ZERO_STRUCT(io2);
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);
613 done:
614 if (tree != NULL) {
615 if (h != NULL) {
616 smb2_util_close(tree, *h);
619 smb2_util_unlink(tree2, fname);
621 talloc_free(tree);
624 talloc_free(mem_ctx);
626 return ret;
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)
636 NTSTATUS status;
637 TALLOC_CTX *mem_ctx = talloc_new(tctx);
638 char fname[256];
639 struct smb2_handle _h;
640 struct smb2_handle *h = NULL;
641 struct smb2_create io1, io2;
642 bool ret = true;
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;
662 h = &_h;
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 */
677 mem_ctx, &session2))
679 torture_warning(tctx, "session setup failed.\n");
680 ret = false;
681 goto done;
685 * the session setup has talloc-stolen the transport,
686 * so we can safely free the old tree+session for clarity
688 TALLOC_FREE(tree);
690 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
691 torture_warning(tctx, "tree connect failed.\n");
692 ret = false;
693 goto done;
696 ZERO_STRUCT(io2);
697 io2.in.fname = fname;
698 io2.in.durable_handle = h;
699 h = NULL;
701 status = smb2_create(tree2, mem_ctx, &io2);
702 CHECK_STATUS(status, NT_STATUS_OK);
704 _h = io2.out.file.handle;
705 h = &_h;
706 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
707 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
709 done:
710 if (tree != NULL) {
711 if (h != NULL) {
712 smb2_util_close(tree2, *h);
715 smb2_util_unlink(tree2, fname);
717 talloc_free(tree);
720 talloc_free(mem_ctx);
722 return ret;
725 static bool test_durable_open_delete_on_close1(struct torture_context *tctx,
726 struct smb2_tree *tree)
728 NTSTATUS status;
729 TALLOC_CTX *mem_ctx = talloc_new(tctx);
730 char fname[256];
731 struct smb2_handle _h;
732 struct smb2_handle *h = NULL;
733 struct smb2_create io1, io2;
734 bool ret = true;
735 uint8_t b = 0;
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;
752 h = &_h;
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 */
761 TALLOC_FREE(tree);
763 if (!torture_smb2_connection(tctx, &tree)) {
764 torture_warning(tctx, "could not reconnect, bailing\n");
765 ret = false;
766 goto done;
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;
784 h = &_h;
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"));
789 done:
790 if (tree != NULL) {
791 if (h != NULL) {
792 smb2_util_close(tree, *h);
795 smb2_util_unlink(tree, fname);
797 talloc_free(tree);
800 talloc_free(mem_ctx);
802 return ret;
806 static bool test_durable_open_delete_on_close2(struct torture_context *tctx,
807 struct smb2_tree *tree)
809 NTSTATUS status;
810 TALLOC_CTX *mem_ctx = talloc_new(tctx);
811 char fname[256];
812 struct smb2_handle _h;
813 struct smb2_handle *h = NULL;
814 struct smb2_create io;
815 bool ret = true;
816 uint8_t b = 0;
817 uint64_t previous_session_id;
818 uint64_t alloc_size_step;
820 /* Choose a random name in case the state is left a little funky. */
821 snprintf(fname, 256, "durable_open_delete_on_close2_%s.dat",
822 generate_random_str(tctx, 8));
824 smb2_util_unlink(tree, fname);
826 smb2_oplock_create_share(&io, fname,
827 smb2_util_share_access(""),
828 smb2_util_oplock_level("b"));
829 io.in.durable_open = true;
830 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
832 status = smb2_create(tree, mem_ctx, &io);
833 CHECK_STATUS(status, NT_STATUS_OK);
834 _h = io.out.file.handle;
835 h = &_h;
836 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
837 CHECK_VAL(io.out.durable_open, true);
838 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
840 status = smb2_util_write(tree, *h, &b, 0, 1);
841 CHECK_STATUS(status, NT_STATUS_OK);
843 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
845 /* disconnect, leaving the durable handle in place */
846 TALLOC_FREE(tree);
848 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
849 torture_warning(tctx, "could not reconnect, bailing\n");
850 ret = false;
851 goto done;
854 ZERO_STRUCT(io);
855 io.in.fname = fname;
856 io.in.durable_handle = h;
858 status = smb2_create(tree, mem_ctx, &io);
859 CHECK_STATUS(status, NT_STATUS_OK);
860 _h = io.out.file.handle;
861 h = &_h;
862 alloc_size_step = io.out.alloc_size;
863 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE, alloc_size_step, 1);
864 CHECK_VAL(io.out.durable_open, false);
865 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
867 /* close the file, thereby deleting it */
868 smb2_util_close(tree, *h);
869 status = smb2_logoff(tree->session);
870 TALLOC_FREE(tree);
872 if (!torture_smb2_connection(tctx, &tree)) {
873 torture_warning(tctx, "could not reconnect, bailing\n");
874 ret = false;
875 goto done;
879 * Open the file on the new connection again
880 * and check that it has been newly created,
881 * i.e. delete on close was effective on the reconnected handle.
882 * Also check that the file is really empty,
883 * the previously written byte gone.
885 smb2_oplock_create_share(&io, fname,
886 smb2_util_share_access(""),
887 smb2_util_oplock_level("b"));
888 io.in.durable_open = true;
889 io.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
891 status = smb2_create(tree, mem_ctx, &io);
892 CHECK_STATUS(status, NT_STATUS_OK);
893 _h = io.out.file.handle;
894 h = &_h;
895 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE, 0, 0);
896 CHECK_VAL(io.out.durable_open, true);
897 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
899 done:
900 if (tree != NULL) {
901 if (h != NULL) {
902 smb2_util_close(tree, *h);
905 smb2_util_unlink(tree, fname);
907 talloc_free(tree);
910 talloc_free(mem_ctx);
912 return ret;
916 basic testing of SMB2 durable opens
917 regarding the position information on the handle
919 static bool test_durable_open_file_position(struct torture_context *tctx,
920 struct smb2_tree *tree)
922 TALLOC_CTX *mem_ctx = talloc_new(tctx);
923 struct smb2_handle h;
924 struct smb2_create io;
925 NTSTATUS status;
926 const char *fname = "durable_open_position.dat";
927 union smb_fileinfo qfinfo;
928 union smb_setfileinfo sfinfo;
929 bool ret = true;
930 uint64_t pos;
931 uint64_t previous_session_id;
933 smb2_util_unlink(tree, fname);
935 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
936 io.in.durable_open = true;
938 status = smb2_create(tree, mem_ctx, &io);
939 CHECK_STATUS(status, NT_STATUS_OK);
940 h = io.out.file.handle;
941 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
942 CHECK_VAL(io.out.durable_open, true);
943 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
945 /* TODO: check extra blob content */
947 ZERO_STRUCT(qfinfo);
948 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
949 qfinfo.generic.in.file.handle = h;
950 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
951 CHECK_STATUS(status, NT_STATUS_OK);
952 CHECK_VAL(qfinfo.position_information.out.position, 0);
953 pos = qfinfo.position_information.out.position;
954 torture_comment(tctx, "position: %llu\n",
955 (unsigned long long)pos);
957 ZERO_STRUCT(sfinfo);
958 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
959 sfinfo.generic.in.file.handle = h;
960 sfinfo.position_information.in.position = 0x1000;
961 status = smb2_setinfo_file(tree, &sfinfo);
962 CHECK_STATUS(status, NT_STATUS_OK);
964 ZERO_STRUCT(qfinfo);
965 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
966 qfinfo.generic.in.file.handle = h;
967 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
968 CHECK_STATUS(status, NT_STATUS_OK);
969 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
970 pos = qfinfo.position_information.out.position;
971 torture_comment(tctx, "position: %llu\n",
972 (unsigned long long)pos);
974 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
976 /* tcp disconnect */
977 talloc_free(tree);
978 tree = NULL;
980 /* do a session reconnect */
981 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
982 torture_warning(tctx, "couldn't reconnect, bailing\n");
983 ret = false;
984 goto done;
987 ZERO_STRUCT(qfinfo);
988 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
989 qfinfo.generic.in.file.handle = h;
990 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
991 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
993 ZERO_STRUCT(io);
994 io.in.fname = fname;
995 io.in.durable_handle = &h;
997 status = smb2_create(tree, mem_ctx, &io);
998 CHECK_STATUS(status, NT_STATUS_OK);
999 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1000 CHECK_VAL(io.out.reserved, 0x00);
1001 CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
1002 CHECK_VAL(io.out.alloc_size, 0);
1003 CHECK_VAL(io.out.size, 0);
1004 CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
1005 CHECK_VAL(io.out.reserved2, 0);
1007 h = io.out.file.handle;
1009 ZERO_STRUCT(qfinfo);
1010 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
1011 qfinfo.generic.in.file.handle = h;
1012 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
1013 CHECK_STATUS(status, NT_STATUS_OK);
1014 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
1015 pos = qfinfo.position_information.out.position;
1016 torture_comment(tctx, "position: %llu\n",
1017 (unsigned long long)pos);
1019 smb2_util_close(tree, h);
1021 talloc_free(mem_ctx);
1023 smb2_util_unlink(tree, fname);
1025 done:
1026 talloc_free(tree);
1028 return ret;
1032 Open, disconnect, oplock break, reconnect.
1034 static bool test_durable_open_oplock(struct torture_context *tctx,
1035 struct smb2_tree *tree1,
1036 struct smb2_tree *tree2)
1038 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1039 struct smb2_create io1, io2;
1040 struct smb2_handle h1, h2;
1041 NTSTATUS status;
1042 char fname[256];
1043 bool ret = true;
1045 /* Choose a random name in case the state is left a little funky. */
1046 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
1048 /* Clean slate */
1049 smb2_util_unlink(tree1, fname);
1051 /* Create with batch oplock */
1052 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1053 io1.in.durable_open = true;
1055 io2 = io1;
1056 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1058 status = smb2_create(tree1, mem_ctx, &io1);
1059 CHECK_STATUS(status, NT_STATUS_OK);
1060 h1 = io1.out.file.handle;
1061 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1062 CHECK_VAL(io1.out.durable_open, true);
1063 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1065 /* Disconnect after getting the batch */
1066 talloc_free(tree1);
1067 tree1 = NULL;
1070 * Windows7 (build 7000) will break a batch oplock immediately if the
1071 * original client is gone. (ZML: This seems like a bug. It should give
1072 * some time for the client to reconnect!)
1074 status = smb2_create(tree2, mem_ctx, &io2);
1075 CHECK_STATUS(status, NT_STATUS_OK);
1076 h2 = io2.out.file.handle;
1077 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1078 CHECK_VAL(io2.out.durable_open, true);
1079 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1081 /* What if tree1 tries to come back and reclaim? */
1082 if (!torture_smb2_connection(tctx, &tree1)) {
1083 torture_warning(tctx, "couldn't reconnect, bailing\n");
1084 ret = false;
1085 goto done;
1088 ZERO_STRUCT(io1);
1089 io1.in.fname = fname;
1090 io1.in.durable_handle = &h1;
1092 status = smb2_create(tree1, mem_ctx, &io1);
1093 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1095 done:
1096 smb2_util_close(tree2, h2);
1097 smb2_util_unlink(tree2, fname);
1099 talloc_free(tree1);
1100 talloc_free(tree2);
1102 return ret;
1106 Open, disconnect, lease break, reconnect.
1108 static bool test_durable_open_lease(struct torture_context *tctx,
1109 struct smb2_tree *tree1,
1110 struct smb2_tree *tree2)
1112 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1113 struct smb2_create io1, io2;
1114 struct smb2_lease ls1, ls2;
1115 struct smb2_handle h1, h2;
1116 NTSTATUS status;
1117 char fname[256];
1118 bool ret = true;
1119 uint64_t lease1, lease2;
1120 uint32_t caps;
1122 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1123 if (!(caps & SMB2_CAP_LEASING)) {
1124 torture_skip(tctx, "leases are not supported");
1128 * Choose a random name and random lease in case the state is left a
1129 * little funky.
1131 lease1 = random();
1132 lease2 = random();
1133 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
1135 /* Clean slate */
1136 smb2_util_unlink(tree1, fname);
1138 /* Create with lease */
1139 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
1140 lease1, smb2_util_lease_state("RHW"));
1141 io1.in.durable_open = true;
1143 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
1144 lease2, smb2_util_lease_state("RHW"));
1145 io2.in.durable_open = true;
1146 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1148 status = smb2_create(tree1, mem_ctx, &io1);
1149 CHECK_STATUS(status, NT_STATUS_OK);
1150 h1 = io1.out.file.handle;
1151 CHECK_VAL(io1.out.durable_open, true);
1152 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1154 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1155 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
1156 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
1157 CHECK_VAL(io1.out.lease_response.lease_state,
1158 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1160 /* Disconnect after getting the lease */
1161 talloc_free(tree1);
1162 tree1 = NULL;
1165 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1166 * even if the original client is gone. (ZML: This seems like a bug. It
1167 * should give some time for the client to reconnect! And why RH?)
1169 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1170 * Test is adapted accordingly.
1172 status = smb2_create(tree2, mem_ctx, &io2);
1173 CHECK_STATUS(status, NT_STATUS_OK);
1174 h2 = io2.out.file.handle;
1175 CHECK_VAL(io2.out.durable_open, true);
1176 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1178 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1179 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
1180 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
1181 CHECK_VAL(io2.out.lease_response.lease_state,
1182 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1184 /* What if tree1 tries to come back and reclaim? */
1185 if (!torture_smb2_connection(tctx, &tree1)) {
1186 torture_warning(tctx, "couldn't reconnect, bailing\n");
1187 ret = false;
1188 goto done;
1191 ZERO_STRUCT(io1);
1192 io1.in.fname = fname;
1193 io1.in.durable_handle = &h1;
1194 io1.in.lease_request = &ls1;
1196 status = smb2_create(tree1, mem_ctx, &io1);
1197 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1199 done:
1200 smb2_util_close(tree2, h2);
1201 smb2_util_unlink(tree2, fname);
1203 talloc_free(tree1);
1204 talloc_free(tree2);
1206 return ret;
1209 static bool test_durable_open_lock_oplock(struct torture_context *tctx,
1210 struct smb2_tree *tree)
1212 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1213 struct smb2_create io;
1214 struct smb2_handle h;
1215 struct smb2_lock lck;
1216 struct smb2_lock_element el[2];
1217 NTSTATUS status;
1218 char fname[256];
1219 bool ret = true;
1223 snprintf(fname, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx, 8));
1225 /* Clean slate */
1226 smb2_util_unlink(tree, fname);
1228 /* Create with lease */
1230 smb2_oplock_create_share(&io, fname,
1231 smb2_util_share_access(""),
1232 smb2_util_oplock_level("b"));
1233 io.in.durable_open = true;
1235 status = smb2_create(tree, mem_ctx, &io);
1236 CHECK_STATUS(status, NT_STATUS_OK);
1237 h = io.out.file.handle;
1238 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1240 CHECK_VAL(io.out.durable_open, true);
1241 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1243 ZERO_STRUCT(lck);
1244 ZERO_STRUCT(el);
1245 lck.in.locks = el;
1246 lck.in.lock_count = 0x0001;
1247 lck.in.lock_sequence = 0x00000000;
1248 lck.in.file.handle = h;
1249 el[0].offset = 0;
1250 el[0].length = 1;
1251 el[0].reserved = 0x00000000;
1252 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1253 status = smb2_lock(tree, &lck);
1254 CHECK_STATUS(status, NT_STATUS_OK);
1256 /* Disconnect/Reconnect. */
1257 talloc_free(tree);
1258 tree = NULL;
1260 if (!torture_smb2_connection(tctx, &tree)) {
1261 torture_warning(tctx, "couldn't reconnect, bailing\n");
1262 ret = false;
1263 goto done;
1266 ZERO_STRUCT(io);
1267 io.in.fname = fname;
1268 io.in.durable_handle = &h;
1270 status = smb2_create(tree, mem_ctx, &io);
1271 CHECK_STATUS(status, NT_STATUS_OK);
1272 h = io.out.file.handle;
1274 lck.in.file.handle = h;
1275 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1276 status = smb2_lock(tree, &lck);
1277 CHECK_STATUS(status, NT_STATUS_OK);
1279 done:
1280 smb2_util_close(tree, h);
1281 smb2_util_unlink(tree, fname);
1282 talloc_free(tree);
1284 return ret;
1288 Open, take BRL, disconnect, reconnect.
1290 static bool test_durable_open_lock_lease(struct torture_context *tctx,
1291 struct smb2_tree *tree)
1293 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1294 struct smb2_create io;
1295 struct smb2_lease ls;
1296 struct smb2_handle h;
1297 struct smb2_lock lck;
1298 struct smb2_lock_element el[2];
1299 NTSTATUS status;
1300 char fname[256];
1301 bool ret = true;
1302 uint64_t lease;
1303 uint32_t caps;
1305 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1306 if (!(caps & SMB2_CAP_LEASING)) {
1307 torture_skip(tctx, "leases are not supported");
1311 * Choose a random name and random lease in case the state is left a
1312 * little funky.
1314 lease = random();
1315 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
1317 /* Clean slate */
1318 smb2_util_unlink(tree, fname);
1320 /* Create with lease */
1322 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
1323 smb2_util_lease_state("RWH"));
1324 io.in.durable_open = true;
1326 status = smb2_create(tree, mem_ctx, &io);
1327 CHECK_STATUS(status, NT_STATUS_OK);
1328 h = io.out.file.handle;
1329 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1331 CHECK_VAL(io.out.durable_open, true);
1332 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1333 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
1334 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
1335 CHECK_VAL(io.out.lease_response.lease_state,
1336 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1338 ZERO_STRUCT(lck);
1339 ZERO_STRUCT(el);
1340 lck.in.locks = el;
1341 lck.in.lock_count = 0x0001;
1342 lck.in.lock_sequence = 0x00000000;
1343 lck.in.file.handle = h;
1344 el[0].offset = 0;
1345 el[0].length = 1;
1346 el[0].reserved = 0x00000000;
1347 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1348 status = smb2_lock(tree, &lck);
1349 CHECK_STATUS(status, NT_STATUS_OK);
1351 /* Disconnect/Reconnect. */
1352 talloc_free(tree);
1353 tree = NULL;
1355 if (!torture_smb2_connection(tctx, &tree)) {
1356 torture_warning(tctx, "couldn't reconnect, bailing\n");
1357 ret = false;
1358 goto done;
1361 ZERO_STRUCT(io);
1362 io.in.fname = fname;
1363 io.in.durable_handle = &h;
1364 io.in.lease_request = &ls;
1366 status = smb2_create(tree, mem_ctx, &io);
1367 CHECK_STATUS(status, NT_STATUS_OK);
1368 h = io.out.file.handle;
1370 lck.in.file.handle = h;
1371 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1372 status = smb2_lock(tree, &lck);
1373 CHECK_STATUS(status, NT_STATUS_OK);
1375 done:
1376 smb2_util_close(tree, h);
1377 smb2_util_unlink(tree, fname);
1378 talloc_free(tree);
1380 return ret;
1384 * Open with a RH lease, disconnect, open in another tree, reconnect.
1386 * This test actually demonstrates a minimum level of respect for the durable
1387 * open in the face of another open. As long as this test shows an inability to
1388 * reconnect after an open, the oplock/lease tests above will certainly
1389 * demonstrate an error on reconnect.
1391 static bool test_durable_open_open2_lease(struct torture_context *tctx,
1392 struct smb2_tree *tree1,
1393 struct smb2_tree *tree2)
1395 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1396 struct smb2_create io1, io2;
1397 struct smb2_lease ls;
1398 struct smb2_handle h1, h2;
1399 NTSTATUS status;
1400 char fname[256];
1401 bool ret = true;
1402 uint64_t lease;
1403 uint32_t caps;
1405 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1406 if (!(caps & SMB2_CAP_LEASING)) {
1407 torture_skip(tctx, "leases are not supported");
1411 * Choose a random name and random lease in case the state is left a
1412 * little funky.
1414 lease = random();
1415 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
1416 generate_random_str(tctx, 8));
1418 /* Clean slate */
1419 smb2_util_unlink(tree1, fname);
1421 /* Create with lease */
1422 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
1423 smb2_util_share_access(""),
1424 lease,
1425 smb2_util_lease_state("RH"));
1426 io1.in.durable_open = true;
1428 status = smb2_create(tree1, mem_ctx, &io1);
1429 CHECK_STATUS(status, NT_STATUS_OK);
1430 h1 = io1.out.file.handle;
1431 CHECK_VAL(io1.out.durable_open, true);
1432 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1434 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1435 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
1436 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
1437 CHECK_VAL(io1.out.lease_response.lease_state,
1438 smb2_util_lease_state("RH"));
1440 /* Disconnect */
1441 talloc_free(tree1);
1442 tree1 = NULL;
1444 /* Open the file in tree2 */
1445 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1447 status = smb2_create(tree2, mem_ctx, &io2);
1448 CHECK_STATUS(status, NT_STATUS_OK);
1449 h2 = io2.out.file.handle;
1450 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1452 /* Reconnect */
1453 if (!torture_smb2_connection(tctx, &tree1)) {
1454 torture_warning(tctx, "couldn't reconnect, bailing\n");
1455 ret = false;
1456 goto done;
1459 ZERO_STRUCT(io1);
1460 io1.in.fname = fname;
1461 io1.in.durable_handle = &h1;
1462 io1.in.lease_request = &ls;
1465 * Windows7 (build 7000) will give away an open immediately if the
1466 * original client is gone. (ZML: This seems like a bug. It should give
1467 * some time for the client to reconnect!)
1469 status = smb2_create(tree1, mem_ctx, &io1);
1470 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1471 h1 = io1.out.file.handle;
1473 done:
1474 smb2_util_close(tree2, h2);
1475 smb2_util_unlink(tree2, fname);
1476 smb2_util_close(tree1, h1);
1477 smb2_util_unlink(tree1, fname);
1479 talloc_free(tree1);
1480 talloc_free(tree2);
1482 return ret;
1486 * Open with a batch oplock, disconnect, open in another tree, reconnect.
1488 * This test actually demonstrates a minimum level of respect for the durable
1489 * open in the face of another open. As long as this test shows an inability to
1490 * reconnect after an open, the oplock/lease tests above will certainly
1491 * demonstrate an error on reconnect.
1493 static bool test_durable_open_open2_oplock(struct torture_context *tctx,
1494 struct smb2_tree *tree1,
1495 struct smb2_tree *tree2)
1497 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1498 struct smb2_create io1, io2;
1499 struct smb2_handle h1, h2;
1500 NTSTATUS status;
1501 char fname[256];
1502 bool ret = true;
1505 * Choose a random name and random lease in case the state is left a
1506 * little funky.
1508 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
1509 generate_random_str(tctx, 8));
1511 /* Clean slate */
1512 smb2_util_unlink(tree1, fname);
1514 /* Create with batch oplock */
1515 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1516 io1.in.durable_open = true;
1518 status = smb2_create(tree1, mem_ctx, &io1);
1519 CHECK_STATUS(status, NT_STATUS_OK);
1520 h1 = io1.out.file.handle;
1521 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1522 CHECK_VAL(io1.out.durable_open, true);
1523 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1525 /* Disconnect */
1526 talloc_free(tree1);
1527 tree1 = NULL;
1529 /* Open the file in tree2 */
1530 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1532 status = smb2_create(tree2, mem_ctx, &io2);
1533 CHECK_STATUS(status, NT_STATUS_OK);
1534 h2 = io2.out.file.handle;
1535 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1537 /* Reconnect */
1538 if (!torture_smb2_connection(tctx, &tree1)) {
1539 torture_warning(tctx, "couldn't reconnect, bailing\n");
1540 ret = false;
1541 goto done;
1544 ZERO_STRUCT(io1);
1545 io1.in.fname = fname;
1546 io1.in.durable_handle = &h1;
1548 status = smb2_create(tree1, mem_ctx, &io1);
1549 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1550 h1 = io1.out.file.handle;
1552 done:
1553 smb2_util_close(tree2, h2);
1554 smb2_util_unlink(tree2, fname);
1555 if (tree1 != NULL) {
1556 smb2_util_close(tree1, h1);
1557 smb2_util_unlink(tree1, fname);
1560 talloc_free(tree1);
1561 talloc_free(tree2);
1563 return ret;
1567 * test behaviour with initial allocation size
1569 static bool test_durable_open_alloc_size(struct torture_context *tctx,
1570 struct smb2_tree *tree)
1572 NTSTATUS status;
1573 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1574 char fname[256];
1575 struct smb2_handle _h;
1576 struct smb2_handle *h = NULL;
1577 struct smb2_create io;
1578 bool ret = true;
1579 uint64_t previous_session_id;
1580 uint64_t alloc_size_step;
1581 uint64_t initial_alloc_size = 0x100;
1582 const uint8_t *b = NULL;
1584 /* Choose a random name in case the state is left a little funky. */
1585 snprintf(fname, 256, "durable_open_alloc_size_%s.dat",
1586 generate_random_str(tctx, 8));
1588 smb2_util_unlink(tree, fname);
1590 smb2_oplock_create_share(&io, fname,
1591 smb2_util_share_access(""),
1592 smb2_util_oplock_level("b"));
1593 io.in.durable_open = true;
1594 io.in.alloc_size = initial_alloc_size;
1596 status = smb2_create(tree, mem_ctx, &io);
1597 CHECK_STATUS(status, NT_STATUS_OK);
1598 _h = io.out.file.handle;
1599 h = &_h;
1600 CHECK_NOT_VAL(io.out.alloc_size, 0);
1601 alloc_size_step = io.out.alloc_size;
1602 CHECK_CREATED_SIZE(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE,
1603 alloc_size_step, 0);
1604 CHECK_VAL(io.out.durable_open, true);
1605 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1607 /* prepare buffer */
1608 b = talloc_zero_size(mem_ctx, alloc_size_step);
1609 CHECK_NOT_VAL(b, NULL);
1611 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1613 /* disconnect, reconnect and then do durable reopen */
1614 talloc_free(tree);
1615 tree = NULL;
1617 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1618 torture_warning(tctx, "couldn't reconnect, bailing\n");
1619 ret = false;
1620 goto done;
1623 ZERO_STRUCT(io);
1624 io.in.fname = fname;
1625 io.in.durable_handle = h;
1626 h = NULL;
1628 status = smb2_create(tree, mem_ctx, &io);
1629 CHECK_STATUS(status, NT_STATUS_OK);
1630 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1631 alloc_size_step, 0);
1632 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1633 _h = io.out.file.handle;
1634 h = &_h;
1636 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1638 /* write one byte */
1639 status = smb2_util_write(tree, *h, b, 0, 1);
1640 CHECK_STATUS(status, NT_STATUS_OK);
1642 /* disconnect, reconnect and then do durable reopen */
1643 talloc_free(tree);
1644 tree = NULL;
1646 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1647 torture_warning(tctx, "couldn't reconnect, bailing\n");
1648 ret = false;
1649 goto done;
1652 ZERO_STRUCT(io);
1653 io.in.fname = fname;
1654 io.in.durable_handle = h;
1655 h = NULL;
1657 status = smb2_create(tree, mem_ctx, &io);
1658 CHECK_STATUS(status, NT_STATUS_OK);
1659 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1660 alloc_size_step, 1);
1661 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1662 _h = io.out.file.handle;
1663 h = &_h;
1665 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1667 /* write more byte than initial allocation size */
1668 status = smb2_util_write(tree, *h, b, 1, alloc_size_step);
1670 /* disconnect, reconnect and then do durable reopen */
1671 talloc_free(tree);
1672 tree = NULL;
1674 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1675 torture_warning(tctx, "couldn't reconnect, bailing\n");
1676 ret = false;
1677 goto done;
1680 ZERO_STRUCT(io);
1681 io.in.fname = fname;
1682 io.in.durable_handle = h;
1683 h = NULL;
1685 status = smb2_create(tree, mem_ctx, &io);
1686 CHECK_STATUS(status, NT_STATUS_OK);
1687 CHECK_CREATED_SIZE(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE,
1688 alloc_size_step * 2, alloc_size_step + 1);
1689 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1690 _h = io.out.file.handle;
1691 h = &_h;
1693 done:
1694 if (h != NULL) {
1695 smb2_util_close(tree, *h);
1698 smb2_util_unlink(tree, fname);
1700 talloc_free(tree);
1702 talloc_free(mem_ctx);
1704 return ret;
1708 * test behaviour when a disconnect happens while creating a read-only file
1710 static bool test_durable_open_read_only(struct torture_context *tctx,
1711 struct smb2_tree *tree)
1713 NTSTATUS status;
1714 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1715 char fname[256];
1716 struct smb2_handle _h;
1717 struct smb2_handle *h = NULL;
1718 struct smb2_create io;
1719 bool ret = true;
1720 uint64_t previous_session_id;
1721 const uint8_t b = 0;
1722 uint64_t alloc_size = 0;
1724 /* Choose a random name in case the state is left a little funky. */
1725 snprintf(fname, 256, "durable_open_initial_alloc_%s.dat",
1726 generate_random_str(tctx, 8));
1728 smb2_util_unlink(tree, fname);
1730 smb2_oplock_create_share(&io, fname,
1731 smb2_util_share_access(""),
1732 smb2_util_oplock_level("b"));
1733 io.in.durable_open = true;
1734 io.in.file_attributes = FILE_ATTRIBUTE_READONLY;
1736 status = smb2_create(tree, mem_ctx, &io);
1737 CHECK_STATUS(status, NT_STATUS_OK);
1738 _h = io.out.file.handle;
1739 h = &_h;
1740 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE);
1741 CHECK_VAL(io.out.durable_open, true);
1742 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1744 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
1746 /* write one byte */
1747 status = smb2_util_write(tree, *h, &b, 0, 1);
1748 CHECK_STATUS(status, NT_STATUS_OK);
1750 /* disconnect, reconnect and then do durable reopen */
1751 talloc_free(tree);
1752 tree = NULL;
1754 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
1755 torture_warning(tctx, "couldn't reconnect, bailing\n");
1756 ret = false;
1757 goto done;
1760 ZERO_STRUCT(io);
1761 io.in.fname = fname;
1762 io.in.durable_handle = h;
1763 h = NULL;
1765 status = smb2_create(tree, mem_ctx, &io);
1766 CHECK_STATUS(status, NT_STATUS_OK);
1767 alloc_size = io.out.alloc_size;
1768 CHECK_CREATED_SIZE(&io, EXISTED,
1769 FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_ARCHIVE,
1770 alloc_size, 1);
1771 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1772 _h = io.out.file.handle;
1773 h = &_h;
1775 /* write one byte */
1776 status = smb2_util_write(tree, *h, &b, 1, 1);
1777 CHECK_STATUS(status, NT_STATUS_OK);
1779 done:
1780 if (h != NULL) {
1781 union smb_setfileinfo sfinfo;
1783 ZERO_STRUCT(sfinfo);
1784 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION;
1785 sfinfo.basic_info.in.file.handle = *h;
1786 sfinfo.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
1787 smb2_setinfo_file(tree, &sfinfo);
1789 smb2_util_close(tree, *h);
1792 smb2_util_unlink(tree, fname);
1794 talloc_free(tree);
1796 talloc_free(mem_ctx);
1798 return ret;
1802 * durable open with oplock, disconnect, exit
1804 static bool test_durable_open_oplock_disconnect(struct torture_context *tctx,
1805 struct smb2_tree *tree)
1807 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1808 struct smb2_create io;
1809 struct smb2_handle _h;
1810 struct smb2_handle *h = NULL;
1811 NTSTATUS status;
1812 char fname[256];
1813 bool ret = true;
1815 snprintf(fname, 256, "durable_open_oplock_disconnect_%s.dat",
1816 generate_random_str(tctx, 8));
1818 smb2_util_unlink(tree, fname);
1820 smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_BATCH);
1821 io.in.durable_open = true;
1823 status = smb2_create(tree, mem_ctx, &io);
1824 CHECK_STATUS(status, NT_STATUS_OK);
1826 _h = io.out.file.handle;
1827 h = &_h;
1829 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1830 CHECK_VAL(io.out.durable_open, true);
1831 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1833 /* disconnect */
1834 talloc_free(tree);
1835 tree = NULL;
1837 done:
1838 if (tree != NULL) {
1839 if (h != NULL) {
1840 smb2_util_close(tree, *h);
1842 smb2_util_unlink(tree, fname);
1845 return ret;
1849 struct torture_suite *torture_smb2_durable_open_init(void)
1851 struct torture_suite *suite =
1852 torture_suite_create(talloc_autofree_context(), "durable-open");
1854 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
1855 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
1856 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
1857 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
1858 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
1859 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
1860 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
1861 torture_suite_add_1smb2_test(suite, "delete_on_close1",
1862 test_durable_open_delete_on_close1);
1863 torture_suite_add_1smb2_test(suite, "delete_on_close2",
1864 test_durable_open_delete_on_close2);
1865 torture_suite_add_1smb2_test(suite, "file-position",
1866 test_durable_open_file_position);
1867 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
1868 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
1869 torture_suite_add_1smb2_test(suite, "lock-oplock", test_durable_open_lock_oplock);
1870 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
1871 torture_suite_add_2smb2_test(suite, "open2-lease",
1872 test_durable_open_open2_lease);
1873 torture_suite_add_2smb2_test(suite, "open2-oplock",
1874 test_durable_open_open2_oplock);
1875 torture_suite_add_1smb2_test(suite, "alloc-size",
1876 test_durable_open_alloc_size);
1877 torture_suite_add_1smb2_test(suite, "read-only",
1878 test_durable_open_read_only);
1880 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
1882 return suite;
1885 struct torture_suite *torture_smb2_durable_open_disconnect_init(void)
1887 struct torture_suite *suite =
1888 torture_suite_create(talloc_autofree_context(),
1889 "durable-open-disconnect");
1891 torture_suite_add_1smb2_test(suite, "open-oplock-disconnect",
1892 test_durable_open_oplock_disconnect);
1894 suite->description = talloc_strdup(suite,
1895 "SMB2-DURABLE-OPEN-DISCONNECT tests");
1897 return suite;