s4:torture/smb2: don't expect a DHnQ response in a DHnC request
[Samba/gebeck_regimport.git] / source4 / torture / smb2 / durable_open.c
blobfaad8666fc2bea982b41348f04fac0b9fc016a69
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)
63 /**
64 * basic durable_open test.
65 * durable state should only be granted when requested
66 * along with a batch oplock or a handle lease.
68 * This test tests durable open with all possible oplock types.
71 struct durable_open_vs_oplock {
72 const char *level;
73 const char *share_mode;
74 bool expected;
77 #define NUM_OPLOCK_TYPES 4
78 #define NUM_SHARE_MODES 8
79 #define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
80 static struct durable_open_vs_oplock durable_open_vs_oplock_table[NUM_OPLOCK_OPEN_TESTS] =
82 { "", "", false },
83 { "", "R", false },
84 { "", "W", false },
85 { "", "D", false },
86 { "", "RD", false },
87 { "", "RW", false },
88 { "", "WD", false },
89 { "", "RWD", false },
91 { "s", "", false },
92 { "s", "R", false },
93 { "s", "W", false },
94 { "s", "D", false },
95 { "s", "RD", false },
96 { "s", "RW", false },
97 { "s", "WD", false },
98 { "s", "RWD", false },
100 { "x", "", false },
101 { "x", "R", false },
102 { "x", "W", false },
103 { "x", "D", false },
104 { "x", "RD", false },
105 { "x", "RW", false },
106 { "x", "WD", false },
107 { "x", "RWD", false },
109 { "b", "", true },
110 { "b", "R", true },
111 { "b", "W", true },
112 { "b", "D", true },
113 { "b", "RD", true },
114 { "b", "RW", true },
115 { "b", "WD", true },
116 { "b", "RWD", true },
119 static bool test_one_durable_open_open_oplock(struct torture_context *tctx,
120 struct smb2_tree *tree,
121 const char *fname,
122 struct durable_open_vs_oplock test)
124 NTSTATUS status;
125 TALLOC_CTX *mem_ctx = talloc_new(tctx);
126 struct smb2_handle _h;
127 struct smb2_handle *h = NULL;
128 bool ret = true;
129 struct smb2_create io;
131 smb2_util_unlink(tree, fname);
133 smb2_oplock_create_share(&io, fname,
134 smb2_util_share_access(test.share_mode),
135 smb2_util_oplock_level(test.level));
136 io.in.durable_open = true;
138 status = smb2_create(tree, mem_ctx, &io);
139 CHECK_STATUS(status, NT_STATUS_OK);
140 _h = io.out.file.handle;
141 h = &_h;
142 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
143 CHECK_VAL(io.out.durable_open, test.expected);
144 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(test.level));
146 done:
147 if (h != NULL) {
148 smb2_util_close(tree, *h);
150 smb2_util_unlink(tree, fname);
151 talloc_free(mem_ctx);
153 return ret;
156 bool test_durable_open_open_oplock(struct torture_context *tctx,
157 struct smb2_tree *tree)
159 TALLOC_CTX *mem_ctx = talloc_new(tctx);
160 char fname[256];
161 bool ret = true;
162 int i;
164 /* Choose a random name in case the state is left a little funky. */
165 snprintf(fname, 256, "durable_open_open_oplock_%s.dat", generate_random_str(tctx, 8));
167 smb2_util_unlink(tree, fname);
169 /* test various oplock levels with durable open */
171 for (i = 0; i < NUM_OPLOCK_OPEN_TESTS; i++) {
172 ret = test_one_durable_open_open_oplock(tctx,
173 tree,
174 fname,
175 durable_open_vs_oplock_table[i]);
176 if (ret == false) {
177 goto done;
181 done:
182 smb2_util_unlink(tree, fname);
183 talloc_free(tree);
184 talloc_free(mem_ctx);
186 return ret;
190 * basic durable_open test.
191 * durable state should only be granted when requested
192 * along with a batch oplock or a handle lease.
194 * This test tests durable open with all valid lease types.
197 struct durable_open_vs_lease {
198 const char *type;
199 const char *share_mode;
200 bool expected;
203 #define NUM_LEASE_TYPES 5
204 #define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
205 static struct durable_open_vs_lease durable_open_vs_lease_table[NUM_LEASE_OPEN_TESTS] =
207 { "", "", false },
208 { "", "R", false },
209 { "", "W", false },
210 { "", "D", false },
211 { "", "RW", false },
212 { "", "RD", false },
213 { "", "WD", false },
214 { "", "RWD", false },
216 { "R", "", false },
217 { "R", "R", false },
218 { "R", "W", false },
219 { "R", "D", false },
220 { "R", "RW", false },
221 { "R", "RD", false },
222 { "R", "DW", false },
223 { "R", "RWD", false },
225 { "RW", "", false },
226 { "RW", "R", false },
227 { "RW", "W", false },
228 { "RW", "D", false },
229 { "RW", "RW", false },
230 { "RW", "RD", false },
231 { "RW", "WD", false },
232 { "RW", "RWD", false },
234 { "RH", "", true },
235 { "RH", "R", true },
236 { "RH", "W", true },
237 { "RH", "D", true },
238 { "RH", "RW", true },
239 { "RH", "RD", true },
240 { "RH", "WD", true },
241 { "RH", "RWD", true },
243 { "RHW", "", true },
244 { "RHW", "R", true },
245 { "RHW", "W", true },
246 { "RHW", "D", true },
247 { "RHW", "RW", true },
248 { "RHW", "RD", true },
249 { "RHW", "WD", true },
250 { "RHW", "RWD", true },
253 static bool test_one_durable_open_open_lease(struct torture_context *tctx,
254 struct smb2_tree *tree,
255 const char *fname,
256 struct durable_open_vs_lease test)
258 NTSTATUS status;
259 TALLOC_CTX *mem_ctx = talloc_new(tctx);
260 struct smb2_handle _h;
261 struct smb2_handle *h = NULL;
262 bool ret = true;
263 struct smb2_create io;
264 struct smb2_lease ls;
265 uint64_t lease;
266 uint32_t caps;
268 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
269 if (!(caps & SMB2_CAP_LEASING)) {
270 torture_skip(tctx, "leases are not supported");
273 smb2_util_unlink(tree, fname);
275 lease = random();
277 smb2_lease_create_share(&io, &ls, false /* dir */, fname,
278 smb2_util_share_access(test.share_mode),
279 lease,
280 smb2_util_lease_state(test.type));
281 io.in.durable_open = true;
283 status = smb2_create(tree, mem_ctx, &io);
284 CHECK_STATUS(status, NT_STATUS_OK);
285 _h = io.out.file.handle;
286 h = &_h;
287 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
288 CHECK_VAL(io.out.durable_open, test.expected);
289 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
290 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
291 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
292 CHECK_VAL(io.out.lease_response.lease_state,
293 smb2_util_lease_state(test.type));
294 done:
295 if (h != NULL) {
296 smb2_util_close(tree, *h);
298 smb2_util_unlink(tree, fname);
299 talloc_free(mem_ctx);
301 return ret;
304 bool test_durable_open_open_lease(struct torture_context *tctx,
305 struct smb2_tree *tree)
307 TALLOC_CTX *mem_ctx = talloc_new(tctx);
308 char fname[256];
309 bool ret = true;
310 int i;
311 uint32_t caps;
313 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
314 if (!(caps & SMB2_CAP_LEASING)) {
315 torture_skip(tctx, "leases are not supported");
318 /* Choose a random name in case the state is left a little funky. */
319 snprintf(fname, 256, "durable_open_open_lease_%s.dat", generate_random_str(tctx, 8));
321 smb2_util_unlink(tree, fname);
324 /* test various oplock levels with durable open */
326 for (i = 0; i < NUM_LEASE_OPEN_TESTS; i++) {
327 ret = test_one_durable_open_open_lease(tctx,
328 tree,
329 fname,
330 durable_open_vs_lease_table[i]);
331 if (ret == false) {
332 goto done;
336 done:
337 smb2_util_unlink(tree, fname);
338 talloc_free(tree);
339 talloc_free(mem_ctx);
341 return ret;
345 * basic test for doing a durable open
346 * and do a durable reopen on the same connection
347 * while the first open is still active (fails)
349 bool test_durable_open_reopen1(struct torture_context *tctx,
350 struct smb2_tree *tree)
352 NTSTATUS status;
353 TALLOC_CTX *mem_ctx = talloc_new(tctx);
354 char fname[256];
355 struct smb2_handle _h;
356 struct smb2_handle *h = NULL;
357 struct smb2_create io1, io2;
358 bool ret = true;
360 /* Choose a random name in case the state is left a little funky. */
361 snprintf(fname, 256, "durable_open_reopen1_%s.dat",
362 generate_random_str(tctx, 8));
364 smb2_util_unlink(tree, fname);
366 smb2_oplock_create_share(&io1, fname,
367 smb2_util_share_access(""),
368 smb2_util_oplock_level("b"));
369 io1.in.durable_open = true;
371 status = smb2_create(tree, mem_ctx, &io1);
372 CHECK_STATUS(status, NT_STATUS_OK);
373 _h = io1.out.file.handle;
374 h = &_h;
375 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
376 CHECK_VAL(io1.out.durable_open, true);
377 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
379 /* try a durable reconnect while the file is still open */
380 ZERO_STRUCT(io2);
381 io2.in.fname = fname;
382 io2.in.durable_handle = h;
384 status = smb2_create(tree, mem_ctx, &io2);
385 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
387 done:
388 if (h != NULL) {
389 smb2_util_close(tree, *h);
392 smb2_util_unlink(tree, fname);
394 talloc_free(tree);
396 talloc_free(mem_ctx);
398 return ret;
402 * basic test for doing a durable open
403 * tcp disconnect, reconnect, do a durable reopen (succeeds)
405 bool test_durable_open_reopen2(struct torture_context *tctx,
406 struct smb2_tree *tree)
408 NTSTATUS status;
409 TALLOC_CTX *mem_ctx = talloc_new(tctx);
410 char fname[256];
411 struct smb2_handle _h;
412 struct smb2_handle *h = NULL;
413 struct smb2_create io1, io2;
414 bool ret = true;
416 /* Choose a random name in case the state is left a little funky. */
417 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
418 generate_random_str(tctx, 8));
420 smb2_util_unlink(tree, fname);
422 smb2_oplock_create_share(&io1, fname,
423 smb2_util_share_access(""),
424 smb2_util_oplock_level("b"));
425 io1.in.durable_open = true;
427 status = smb2_create(tree, mem_ctx, &io1);
428 CHECK_STATUS(status, NT_STATUS_OK);
429 _h = io1.out.file.handle;
430 h = &_h;
431 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
432 CHECK_VAL(io1.out.durable_open, true);
433 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
435 /* disconnect, reconnect and then do durable reopen */
436 talloc_free(tree);
437 tree = NULL;
439 if (!torture_smb2_connection(tctx, &tree)) {
440 torture_warning(tctx, "couldn't reconnect, bailing\n");
441 ret = false;
442 goto done;
445 ZERO_STRUCT(io2);
446 io2.in.fname = fname;
447 io2.in.durable_handle = h;
448 h = NULL;
450 status = smb2_create(tree, mem_ctx, &io2);
451 CHECK_STATUS(status, NT_STATUS_OK);
452 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
453 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
454 _h = io2.out.file.handle;
455 h = &_h;
457 done:
458 if (h != NULL) {
459 smb2_util_close(tree, *h);
462 smb2_util_unlink(tree, fname);
464 talloc_free(tree);
466 talloc_free(mem_ctx);
468 return ret;
472 * basic test for doing a durable open
473 * tcp disconnect, reconnect with a session reconnect and
474 * do a durable reopen (succeeds)
476 bool test_durable_open_reopen2a(struct torture_context *tctx,
477 struct smb2_tree *tree)
479 NTSTATUS status;
480 TALLOC_CTX *mem_ctx = talloc_new(tctx);
481 char fname[256];
482 struct smb2_handle _h;
483 struct smb2_handle *h = NULL;
484 struct smb2_create io1, io2;
485 uint64_t previous_session_id;
486 bool ret = true;
488 /* Choose a random name in case the state is left a little funky. */
489 snprintf(fname, 256, "durable_open_reopen2_%s.dat",
490 generate_random_str(tctx, 8));
492 smb2_util_unlink(tree, fname);
494 smb2_oplock_create_share(&io1, fname,
495 smb2_util_share_access(""),
496 smb2_util_oplock_level("b"));
497 io1.in.durable_open = true;
499 status = smb2_create(tree, mem_ctx, &io1);
500 CHECK_STATUS(status, NT_STATUS_OK);
501 _h = io1.out.file.handle;
502 h = &_h;
503 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
504 CHECK_VAL(io1.out.durable_open, true);
505 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
507 /* disconnect, reconnect and then do durable reopen */
508 previous_session_id = smb2cli_session_current_id(tree->session->smbXcli);
509 talloc_free(tree);
510 tree = NULL;
512 if (!torture_smb2_connection_ext(tctx, previous_session_id, &tree)) {
513 torture_warning(tctx, "couldn't reconnect, bailing\n");
514 ret = false;
515 goto done;
518 ZERO_STRUCT(io2);
519 io2.in.fname = fname;
520 io2.in.durable_handle = h;
521 h = NULL;
523 status = smb2_create(tree, mem_ctx, &io2);
524 CHECK_STATUS(status, NT_STATUS_OK);
525 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
526 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
527 _h = io2.out.file.handle;
528 h = &_h;
530 done:
531 if (h != NULL) {
532 smb2_util_close(tree, *h);
535 smb2_util_unlink(tree, fname);
537 talloc_free(tree);
539 talloc_free(mem_ctx);
541 return ret;
546 * basic test for doing a durable open:
547 * tdis, new tcon, try durable reopen (fails)
549 bool test_durable_open_reopen3(struct torture_context *tctx,
550 struct smb2_tree *tree)
552 NTSTATUS status;
553 TALLOC_CTX *mem_ctx = talloc_new(tctx);
554 char fname[256];
555 struct smb2_handle _h;
556 struct smb2_handle *h = NULL;
557 struct smb2_create io1, io2;
558 bool ret = true;
559 struct smb2_tree *tree2;
561 /* Choose a random name in case the state is left a little funky. */
562 snprintf(fname, 256, "durable_open_reopen3_%s.dat",
563 generate_random_str(tctx, 8));
565 smb2_util_unlink(tree, fname);
567 smb2_oplock_create_share(&io1, fname,
568 smb2_util_share_access(""),
569 smb2_util_oplock_level("b"));
570 io1.in.durable_open = true;
572 status = smb2_create(tree, mem_ctx, &io1);
573 CHECK_STATUS(status, NT_STATUS_OK);
574 _h = io1.out.file.handle;
575 h = &_h;
576 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
577 CHECK_VAL(io1.out.durable_open, true);
578 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
580 /* disconnect, reconnect and then do durable reopen */
581 status = smb2_tdis(tree);
582 CHECK_STATUS(status, NT_STATUS_OK);
584 if (!torture_smb2_tree_connect(tctx, tree->session, mem_ctx, &tree2)) {
585 torture_warning(tctx, "couldn't reconnect to share, bailing\n");
586 ret = false;
587 goto done;
591 ZERO_STRUCT(io2);
592 io2.in.fname = fname;
593 io2.in.durable_handle = h;
595 status = smb2_create(tree2, mem_ctx, &io2);
596 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
598 done:
599 if (h != NULL) {
600 smb2_util_close(tree, *h);
603 smb2_util_unlink(tree2, fname);
605 talloc_free(tree);
607 talloc_free(mem_ctx);
609 return ret;
613 * basic test for doing a durable open:
614 * logoff, create a new session, do a durable reopen (succeeds)
616 bool test_durable_open_reopen4(struct torture_context *tctx,
617 struct smb2_tree *tree)
619 NTSTATUS status;
620 TALLOC_CTX *mem_ctx = talloc_new(tctx);
621 char fname[256];
622 struct smb2_handle _h;
623 struct smb2_handle *h = NULL;
624 struct smb2_create io1, io2;
625 bool ret = true;
626 struct smb2_transport *transport;
627 struct smb2_session *session2;
628 struct smb2_tree *tree2;
630 /* Choose a random name in case the state is left a little funky. */
631 snprintf(fname, 256, "durable_open_reopen4_%s.dat",
632 generate_random_str(tctx, 8));
634 smb2_util_unlink(tree, fname);
636 smb2_oplock_create_share(&io1, fname,
637 smb2_util_share_access(""),
638 smb2_util_oplock_level("b"));
639 io1.in.durable_open = true;
640 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
642 status = smb2_create(tree, mem_ctx, &io1);
643 CHECK_STATUS(status, NT_STATUS_OK);
644 _h = io1.out.file.handle;
645 h = &_h;
646 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
647 CHECK_VAL(io1.out.durable_open, true);
648 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
651 * do a session logoff, establish a new session and tree
652 * connect on the same transport, and try a durable reopen
654 transport = tree->session->transport;
655 status = smb2_logoff(tree->session);
656 CHECK_STATUS(status, NT_STATUS_OK);
658 if (!torture_smb2_session_setup(tctx, transport,
659 0, /* previous_session_id */
660 mem_ctx, &session2))
662 torture_warning(tctx, "session setup failed.\n");
663 ret = false;
664 goto done;
668 * the session setup has talloc-stolen the transport,
669 * so we can safely free the old tree+session for clarity
671 TALLOC_FREE(tree);
673 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
674 torture_warning(tctx, "tree connect failed.\n");
675 ret = false;
676 goto done;
679 ZERO_STRUCT(io2);
680 io2.in.fname = fname;
681 io2.in.durable_handle = h;
682 h = NULL;
684 status = smb2_create(tree2, mem_ctx, &io2);
685 CHECK_STATUS(status, NT_STATUS_OK);
687 _h = io2.out.file.handle;
688 h = &_h;
689 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
690 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
692 done:
693 if (h != NULL) {
694 smb2_util_close(tree2, *h);
697 smb2_util_unlink(tree2, fname);
699 talloc_free(tree);
701 talloc_free(mem_ctx);
703 return ret;
706 bool test_durable_open_delete_on_close1(struct torture_context *tctx,
707 struct smb2_tree *tree)
709 NTSTATUS status;
710 TALLOC_CTX *mem_ctx = talloc_new(tctx);
711 char fname[256];
712 struct smb2_handle _h;
713 struct smb2_handle *h = NULL;
714 struct smb2_create io1, io2, io3;
715 bool ret = true;
716 struct smb2_transport *transport;
717 struct smb2_session *session2;
718 struct smb2_tree *tree2;
719 union smb_fileinfo info1, info2;
721 /* Choose a random name in case the state is left a little funky. */
722 snprintf(fname, 256, "durable_open_delete_on_close1_%s.dat",
723 generate_random_str(tctx, 8));
725 smb2_util_unlink(tree, fname);
727 smb2_oplock_create_share(&io1, fname,
728 smb2_util_share_access(""),
729 smb2_util_oplock_level("b"));
730 io1.in.durable_open = true;
731 io1.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
733 status = smb2_create(tree, mem_ctx, &io1);
734 CHECK_STATUS(status, NT_STATUS_OK);
735 _h = io1.out.file.handle;
736 h = &_h;
737 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
738 CHECK_VAL(io1.out.durable_open, true);
739 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
741 ZERO_STRUCT(info1);
742 info1.internal_information.level = RAW_FILEINFO_INTERNAL_INFORMATION;
743 info1.internal_information.in.file.handle = _h;
744 status = smb2_getinfo_file(tree, tree, &info1);
745 CHECK_STATUS(status, NT_STATUS_OK);
748 * do a session logoff, establish a new session and tree
749 * connect on the same transport, and try a durable reopen
751 transport = tree->session->transport;
752 status = smb2_logoff(tree->session);
753 CHECK_STATUS(status, NT_STATUS_OK);
755 if (!torture_smb2_session_setup(tctx, transport,
756 0, /* previous_session_id */
757 mem_ctx, &session2))
759 torture_warning(tctx, "session setup failed.\n");
760 ret = false;
761 goto done;
765 * the session setup has talloc-stolen the transport,
766 * so we can safely free the old tree+session for clarity
768 TALLOC_FREE(tree);
770 if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
771 torture_warning(tctx, "tree connect failed.\n");
772 ret = false;
773 goto done;
776 ZERO_STRUCT(io3);
777 io3.in.fname = fname;
778 io3.in.durable_handle = h;
779 h = NULL;
781 smb2_oplock_create_share(&io2, fname,
782 smb2_util_share_access(""),
783 smb2_util_oplock_level("b"));
784 io2.in.create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
786 status = smb2_create(tree2, mem_ctx, &io2);
787 CHECK_STATUS(status, NT_STATUS_OK);
788 _h = io2.out.file.handle;
789 h = &_h;
790 CHECK_CREATED(&io2, CREATED, FILE_ATTRIBUTE_ARCHIVE);
791 CHECK_VAL(io2.out.durable_open, false);
792 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
794 ZERO_STRUCT(info2);
795 info2.internal_information.level = RAW_FILEINFO_INTERNAL_INFORMATION;
796 info2.internal_information.in.file.handle = _h;
797 status = smb2_getinfo_file(tree2, tree2, &info2);
798 CHECK_STATUS(status, NT_STATUS_OK);
800 CHECK_NOT_VAL(info1.internal_information.out.file_id,
801 info2.internal_information.out.file_id);
803 status = smb2_create(tree2, mem_ctx, &io3);
804 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
806 done:
807 if (h != NULL) {
808 smb2_util_close(tree2, *h);
811 smb2_util_unlink(tree2, fname);
813 talloc_free(tree);
814 talloc_free(tree2);
816 talloc_free(mem_ctx);
818 return ret;
822 basic testing of SMB2 durable opens
823 regarding the position information on the handle
825 bool test_durable_open_file_position(struct torture_context *tctx,
826 struct smb2_tree *tree1,
827 struct smb2_tree *tree2)
829 TALLOC_CTX *mem_ctx = talloc_new(tctx);
830 struct smb2_handle h1, h2;
831 struct smb2_create io1, io2;
832 NTSTATUS status;
833 const char *fname = "durable_open_position.dat";
834 union smb_fileinfo qfinfo;
835 union smb_setfileinfo sfinfo;
836 bool ret = true;
837 uint64_t pos;
839 smb2_util_unlink(tree1, fname);
841 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
842 io1.in.durable_open = true;
844 status = smb2_create(tree1, mem_ctx, &io1);
845 CHECK_STATUS(status, NT_STATUS_OK);
846 h1 = io1.out.file.handle;
847 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
848 CHECK_VAL(io1.out.durable_open, true);
849 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
851 /* TODO: check extra blob content */
853 ZERO_STRUCT(qfinfo);
854 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
855 qfinfo.generic.in.file.handle = h1;
856 status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
857 CHECK_STATUS(status, NT_STATUS_OK);
858 CHECK_VAL(qfinfo.position_information.out.position, 0);
859 pos = qfinfo.position_information.out.position;
860 torture_comment(tctx, "position: %llu\n",
861 (unsigned long long)pos);
863 ZERO_STRUCT(sfinfo);
864 sfinfo.generic.level = RAW_SFILEINFO_POSITION_INFORMATION;
865 sfinfo.generic.in.file.handle = h1;
866 sfinfo.position_information.in.position = 0x1000;
867 status = smb2_setinfo_file(tree1, &sfinfo);
868 CHECK_STATUS(status, NT_STATUS_OK);
870 ZERO_STRUCT(qfinfo);
871 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
872 qfinfo.generic.in.file.handle = h1;
873 status = smb2_getinfo_file(tree1, mem_ctx, &qfinfo);
874 CHECK_STATUS(status, NT_STATUS_OK);
875 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
876 pos = qfinfo.position_information.out.position;
877 torture_comment(tctx, "position: %llu\n",
878 (unsigned long long)pos);
880 talloc_free(tree1);
881 tree1 = NULL;
883 ZERO_STRUCT(qfinfo);
884 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
885 qfinfo.generic.in.file.handle = h1;
886 status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
887 CHECK_STATUS(status, NT_STATUS_FILE_CLOSED);
889 ZERO_STRUCT(io2);
890 io2.in.fname = fname;
891 io2.in.durable_handle = &h1;
893 status = smb2_create(tree2, mem_ctx, &io2);
894 CHECK_STATUS(status, NT_STATUS_OK);
895 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
896 CHECK_VAL(io2.out.reserved, 0x00);
897 CHECK_VAL(io2.out.create_action, NTCREATEX_ACTION_EXISTED);
898 CHECK_VAL(io2.out.alloc_size, 0);
899 CHECK_VAL(io2.out.size, 0);
900 CHECK_VAL(io2.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
901 CHECK_VAL(io2.out.reserved2, 0);
903 h2 = io2.out.file.handle;
905 ZERO_STRUCT(qfinfo);
906 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
907 qfinfo.generic.in.file.handle = h2;
908 status = smb2_getinfo_file(tree2, mem_ctx, &qfinfo);
909 CHECK_STATUS(status, NT_STATUS_OK);
910 CHECK_VAL(qfinfo.position_information.out.position, 0x1000);
911 pos = qfinfo.position_information.out.position;
912 torture_comment(tctx, "position: %llu\n",
913 (unsigned long long)pos);
915 smb2_util_close(tree2, h2);
917 talloc_free(mem_ctx);
919 smb2_util_unlink(tree2, fname);
920 done:
921 talloc_free(tree1);
922 talloc_free(tree2);
924 return ret;
928 Open, disconnect, oplock break, reconnect.
930 bool test_durable_open_oplock(struct torture_context *tctx,
931 struct smb2_tree *tree1,
932 struct smb2_tree *tree2)
934 TALLOC_CTX *mem_ctx = talloc_new(tctx);
935 struct smb2_create io1, io2;
936 struct smb2_handle h1, h2;
937 NTSTATUS status;
938 char fname[256];
939 bool ret = true;
941 /* Choose a random name in case the state is left a little funky. */
942 snprintf(fname, 256, "durable_open_oplock_%s.dat", generate_random_str(tctx, 8));
944 /* Clean slate */
945 smb2_util_unlink(tree1, fname);
947 /* Create with batch oplock */
948 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
949 io1.in.durable_open = true;
951 io2 = io1;
952 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
954 status = smb2_create(tree1, mem_ctx, &io1);
955 CHECK_STATUS(status, NT_STATUS_OK);
956 h1 = io1.out.file.handle;
957 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
958 CHECK_VAL(io1.out.durable_open, true);
959 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
961 /* Disconnect after getting the batch */
962 talloc_free(tree1);
963 tree1 = NULL;
966 * Windows7 (build 7000) will break a batch oplock immediately if the
967 * original client is gone. (ZML: This seems like a bug. It should give
968 * some time for the client to reconnect!)
970 status = smb2_create(tree2, mem_ctx, &io2);
971 CHECK_STATUS(status, NT_STATUS_OK);
972 h2 = io2.out.file.handle;
973 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
974 CHECK_VAL(io2.out.durable_open, true);
975 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
977 /* What if tree1 tries to come back and reclaim? */
978 if (!torture_smb2_connection(tctx, &tree1)) {
979 torture_warning(tctx, "couldn't reconnect, bailing\n");
980 ret = false;
981 goto done;
984 ZERO_STRUCT(io1);
985 io1.in.fname = fname;
986 io1.in.durable_handle = &h1;
988 status = smb2_create(tree1, mem_ctx, &io1);
989 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
991 done:
992 smb2_util_close(tree2, h2);
993 smb2_util_unlink(tree2, fname);
995 talloc_free(tree1);
996 talloc_free(tree2);
998 return ret;
1002 Open, disconnect, lease break, reconnect.
1004 bool test_durable_open_lease(struct torture_context *tctx,
1005 struct smb2_tree *tree1,
1006 struct smb2_tree *tree2)
1008 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1009 struct smb2_create io1, io2;
1010 struct smb2_lease ls1, ls2;
1011 struct smb2_handle h1, h2;
1012 NTSTATUS status;
1013 char fname[256];
1014 bool ret = true;
1015 uint64_t lease1, lease2;
1016 uint32_t caps;
1018 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1019 if (!(caps & SMB2_CAP_LEASING)) {
1020 torture_skip(tctx, "leases are not supported");
1024 * Choose a random name and random lease in case the state is left a
1025 * little funky.
1027 lease1 = random();
1028 lease2 = random();
1029 snprintf(fname, 256, "durable_open_lease_%s.dat", generate_random_str(tctx, 8));
1031 /* Clean slate */
1032 smb2_util_unlink(tree1, fname);
1034 /* Create with lease */
1035 smb2_lease_create(&io1, &ls1, false /* dir */, fname,
1036 lease1, smb2_util_lease_state("RHW"));
1037 io1.in.durable_open = true;
1039 smb2_lease_create(&io2, &ls2, false /* dir */, fname,
1040 lease2, smb2_util_lease_state("RHW"));
1041 io2.in.durable_open = true;
1042 io2.in.create_disposition = NTCREATEX_DISP_OPEN;
1044 status = smb2_create(tree1, mem_ctx, &io1);
1045 CHECK_STATUS(status, NT_STATUS_OK);
1046 h1 = io1.out.file.handle;
1047 CHECK_VAL(io1.out.durable_open, true);
1048 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1050 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1051 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease1);
1052 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease1);
1053 CHECK_VAL(io1.out.lease_response.lease_state,
1054 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1056 /* Disconnect after getting the lease */
1057 talloc_free(tree1);
1058 tree1 = NULL;
1061 * Windows7 (build 7000) will grant an RH lease immediate (not an RHW?)
1062 * even if the original client is gone. (ZML: This seems like a bug. It
1063 * should give some time for the client to reconnect! And why RH?)
1065 * obnox: Current windows 7 and w2k8r2 grant RHW instead of RH.
1066 * Test is adapted accordingly.
1068 status = smb2_create(tree2, mem_ctx, &io2);
1069 CHECK_STATUS(status, NT_STATUS_OK);
1070 h2 = io2.out.file.handle;
1071 CHECK_VAL(io2.out.durable_open, true);
1072 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
1074 CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1075 CHECK_VAL(io2.out.lease_response.lease_key.data[0], lease2);
1076 CHECK_VAL(io2.out.lease_response.lease_key.data[1], ~lease2);
1077 CHECK_VAL(io2.out.lease_response.lease_state,
1078 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1080 /* What if tree1 tries to come back and reclaim? */
1081 if (!torture_smb2_connection(tctx, &tree1)) {
1082 torture_warning(tctx, "couldn't reconnect, bailing\n");
1083 ret = false;
1084 goto done;
1087 ZERO_STRUCT(io1);
1088 io1.in.fname = fname;
1089 io1.in.durable_handle = &h1;
1090 io1.in.lease_request = &ls1;
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;
1105 bool test_durable_open_lock_oplock(struct torture_context *tctx,
1106 struct smb2_tree *tree)
1108 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1109 struct smb2_create io;
1110 struct smb2_handle h;
1111 struct smb2_lock lck;
1112 struct smb2_lock_element el[2];
1113 NTSTATUS status;
1114 char fname[256];
1115 bool ret = true;
1119 snprintf(fname, 256, "durable_open_oplock_lock_%s.dat", generate_random_str(tctx, 8));
1121 /* Clean slate */
1122 smb2_util_unlink(tree, fname);
1124 /* Create with lease */
1126 smb2_oplock_create_share(&io, fname,
1127 smb2_util_share_access(""),
1128 smb2_util_oplock_level("b"));
1129 io.in.durable_open = true;
1131 status = smb2_create(tree, mem_ctx, &io);
1132 CHECK_STATUS(status, NT_STATUS_OK);
1133 h = io.out.file.handle;
1134 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1136 CHECK_VAL(io.out.durable_open, true);
1137 CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("b"));
1139 ZERO_STRUCT(lck);
1140 ZERO_STRUCT(el);
1141 lck.in.locks = el;
1142 lck.in.lock_count = 0x0001;
1143 lck.in.lock_sequence = 0x00000000;
1144 lck.in.file.handle = h;
1145 el[0].offset = 0;
1146 el[0].length = 1;
1147 el[0].reserved = 0x00000000;
1148 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1149 status = smb2_lock(tree, &lck);
1150 CHECK_STATUS(status, NT_STATUS_OK);
1152 /* Disconnect/Reconnect. */
1153 talloc_free(tree);
1154 tree = NULL;
1156 if (!torture_smb2_connection(tctx, &tree)) {
1157 torture_warning(tctx, "couldn't reconnect, bailing\n");
1158 ret = false;
1159 goto done;
1162 ZERO_STRUCT(io);
1163 io.in.fname = fname;
1164 io.in.durable_handle = &h;
1166 status = smb2_create(tree, mem_ctx, &io);
1167 CHECK_STATUS(status, NT_STATUS_OK);
1168 h = io.out.file.handle;
1170 lck.in.file.handle = h;
1171 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1172 status = smb2_lock(tree, &lck);
1173 CHECK_STATUS(status, NT_STATUS_OK);
1175 done:
1176 smb2_util_close(tree, h);
1177 smb2_util_unlink(tree, fname);
1178 talloc_free(tree);
1180 return ret;
1184 Open, take BRL, disconnect, reconnect.
1186 bool test_durable_open_lock_lease(struct torture_context *tctx,
1187 struct smb2_tree *tree)
1189 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1190 struct smb2_create io;
1191 struct smb2_lease ls;
1192 struct smb2_handle h;
1193 struct smb2_lock lck;
1194 struct smb2_lock_element el[2];
1195 NTSTATUS status;
1196 char fname[256];
1197 bool ret = true;
1198 uint64_t lease;
1199 uint32_t caps;
1201 caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
1202 if (!(caps & SMB2_CAP_LEASING)) {
1203 torture_skip(tctx, "leases are not supported");
1207 * Choose a random name and random lease in case the state is left a
1208 * little funky.
1210 lease = random();
1211 snprintf(fname, 256, "durable_open_lease_lock_%s.dat", generate_random_str(tctx, 8));
1213 /* Clean slate */
1214 smb2_util_unlink(tree, fname);
1216 /* Create with lease */
1218 smb2_lease_create(&io, &ls, false /* dir */, fname, lease,
1219 smb2_util_lease_state("RWH"));
1220 io.in.durable_open = true;
1222 status = smb2_create(tree, mem_ctx, &io);
1223 CHECK_STATUS(status, NT_STATUS_OK);
1224 h = io.out.file.handle;
1225 CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1227 CHECK_VAL(io.out.durable_open, true);
1228 CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1229 CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
1230 CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
1231 CHECK_VAL(io.out.lease_response.lease_state,
1232 SMB2_LEASE_READ|SMB2_LEASE_HANDLE|SMB2_LEASE_WRITE);
1234 ZERO_STRUCT(lck);
1235 ZERO_STRUCT(el);
1236 lck.in.locks = el;
1237 lck.in.lock_count = 0x0001;
1238 lck.in.lock_sequence = 0x00000000;
1239 lck.in.file.handle = h;
1240 el[0].offset = 0;
1241 el[0].length = 1;
1242 el[0].reserved = 0x00000000;
1243 el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
1244 status = smb2_lock(tree, &lck);
1245 CHECK_STATUS(status, NT_STATUS_OK);
1247 /* Disconnect/Reconnect. */
1248 talloc_free(tree);
1249 tree = NULL;
1251 if (!torture_smb2_connection(tctx, &tree)) {
1252 torture_warning(tctx, "couldn't reconnect, bailing\n");
1253 ret = false;
1254 goto done;
1257 ZERO_STRUCT(io);
1258 io.in.fname = fname;
1259 io.in.durable_handle = &h;
1260 io.in.lease_request = &ls;
1262 status = smb2_create(tree, mem_ctx, &io);
1263 CHECK_STATUS(status, NT_STATUS_OK);
1264 h = io.out.file.handle;
1266 lck.in.file.handle = h;
1267 el[0].flags = SMB2_LOCK_FLAG_UNLOCK;
1268 status = smb2_lock(tree, &lck);
1269 CHECK_STATUS(status, NT_STATUS_OK);
1271 done:
1272 smb2_util_close(tree, h);
1273 smb2_util_unlink(tree, fname);
1274 talloc_free(tree);
1276 return ret;
1280 * Open with a RH lease, disconnect, open in another tree, reconnect.
1282 * This test actually demonstrates a minimum level of respect for the durable
1283 * open in the face of another open. As long as this test shows an inability to
1284 * reconnect after an open, the oplock/lease tests above will certainly
1285 * demonstrate an error on reconnect.
1287 bool test_durable_open_open2_lease(struct torture_context *tctx,
1288 struct smb2_tree *tree1,
1289 struct smb2_tree *tree2)
1291 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1292 struct smb2_create io1, io2;
1293 struct smb2_lease ls;
1294 struct smb2_handle h1, h2;
1295 NTSTATUS status;
1296 char fname[256];
1297 bool ret = true;
1298 uint64_t lease;
1299 uint32_t caps;
1301 caps = smb2cli_conn_server_capabilities(tree1->session->transport->conn);
1302 if (!(caps & SMB2_CAP_LEASING)) {
1303 torture_skip(tctx, "leases are not supported");
1307 * Choose a random name and random lease in case the state is left a
1308 * little funky.
1310 lease = random();
1311 snprintf(fname, 256, "durable_open_open2_lease_%s.dat",
1312 generate_random_str(tctx, 8));
1314 /* Clean slate */
1315 smb2_util_unlink(tree1, fname);
1317 /* Create with lease */
1318 smb2_lease_create_share(&io1, &ls, false /* dir */, fname,
1319 smb2_util_share_access(""),
1320 lease,
1321 smb2_util_lease_state("RH"));
1322 io1.in.durable_open = true;
1324 status = smb2_create(tree1, mem_ctx, &io1);
1325 CHECK_STATUS(status, NT_STATUS_OK);
1326 h1 = io1.out.file.handle;
1327 CHECK_VAL(io1.out.durable_open, true);
1328 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1330 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
1331 CHECK_VAL(io1.out.lease_response.lease_key.data[0], lease);
1332 CHECK_VAL(io1.out.lease_response.lease_key.data[1], ~lease);
1333 CHECK_VAL(io1.out.lease_response.lease_state,
1334 smb2_util_lease_state("RH"));
1336 /* Disconnect */
1337 talloc_free(tree1);
1338 tree1 = NULL;
1340 /* Open the file in tree2 */
1341 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1343 status = smb2_create(tree2, mem_ctx, &io2);
1344 CHECK_STATUS(status, NT_STATUS_OK);
1345 h2 = io2.out.file.handle;
1346 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1348 /* Reconnect */
1349 if (!torture_smb2_connection(tctx, &tree1)) {
1350 torture_warning(tctx, "couldn't reconnect, bailing\n");
1351 ret = false;
1352 goto done;
1355 ZERO_STRUCT(io1);
1356 io1.in.fname = fname;
1357 io1.in.durable_handle = &h1;
1358 io1.in.lease_request = &ls;
1361 * Windows7 (build 7000) will give away an open immediately if the
1362 * original client is gone. (ZML: This seems like a bug. It should give
1363 * some time for the client to reconnect!)
1365 status = smb2_create(tree1, mem_ctx, &io1);
1366 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1367 h1 = io1.out.file.handle;
1369 done:
1370 smb2_util_close(tree2, h2);
1371 smb2_util_unlink(tree2, fname);
1372 smb2_util_close(tree1, h1);
1373 smb2_util_unlink(tree1, fname);
1375 talloc_free(tree1);
1376 talloc_free(tree2);
1378 return ret;
1382 * Open with a batch oplock, disconnect, open in another tree, reconnect.
1384 * This test actually demonstrates a minimum level of respect for the durable
1385 * open in the face of another open. As long as this test shows an inability to
1386 * reconnect after an open, the oplock/lease tests above will certainly
1387 * demonstrate an error on reconnect.
1389 bool test_durable_open_open2_oplock(struct torture_context *tctx,
1390 struct smb2_tree *tree1,
1391 struct smb2_tree *tree2)
1393 TALLOC_CTX *mem_ctx = talloc_new(tctx);
1394 struct smb2_create io1, io2;
1395 struct smb2_handle h1, h2;
1396 NTSTATUS status;
1397 char fname[256];
1398 bool ret = true;
1401 * Choose a random name and random lease in case the state is left a
1402 * little funky.
1404 snprintf(fname, 256, "durable_open_open2_oplock_%s.dat",
1405 generate_random_str(tctx, 8));
1407 /* Clean slate */
1408 smb2_util_unlink(tree1, fname);
1410 /* Create with batch oplock */
1411 smb2_oplock_create(&io1, fname, SMB2_OPLOCK_LEVEL_BATCH);
1412 io1.in.durable_open = true;
1414 status = smb2_create(tree1, mem_ctx, &io1);
1415 CHECK_STATUS(status, NT_STATUS_OK);
1416 h1 = io1.out.file.handle;
1417 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1418 CHECK_VAL(io1.out.durable_open, true);
1419 CHECK_VAL(io1.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
1421 /* Disconnect */
1422 talloc_free(tree1);
1423 tree1 = NULL;
1425 /* Open the file in tree2 */
1426 smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
1428 status = smb2_create(tree2, mem_ctx, &io2);
1429 CHECK_STATUS(status, NT_STATUS_OK);
1430 h2 = io2.out.file.handle;
1431 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
1433 /* Reconnect */
1434 if (!torture_smb2_connection(tctx, &tree1)) {
1435 torture_warning(tctx, "couldn't reconnect, bailing\n");
1436 ret = false;
1437 goto done;
1440 ZERO_STRUCT(io1);
1441 io1.in.fname = fname;
1442 io1.in.durable_handle = &h1;
1444 status = smb2_create(tree1, mem_ctx, &io1);
1445 CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
1446 h1 = io1.out.file.handle;
1448 done:
1449 smb2_util_close(tree2, h2);
1450 smb2_util_unlink(tree2, fname);
1451 smb2_util_close(tree1, h1);
1452 smb2_util_unlink(tree1, fname);
1454 talloc_free(tree1);
1455 talloc_free(tree2);
1457 return ret;
1460 struct torture_suite *torture_smb2_durable_open_init(void)
1462 struct torture_suite *suite =
1463 torture_suite_create(talloc_autofree_context(), "durable-open");
1465 torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
1466 torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
1467 torture_suite_add_1smb2_test(suite, "reopen1", test_durable_open_reopen1);
1468 torture_suite_add_1smb2_test(suite, "reopen2", test_durable_open_reopen2);
1469 torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
1470 torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
1471 torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
1472 torture_suite_add_1smb2_test(suite, "delete_on_close1",
1473 test_durable_open_delete_on_close1);
1474 torture_suite_add_2smb2_test(suite, "file-position",
1475 test_durable_open_file_position);
1476 torture_suite_add_2smb2_test(suite, "oplock", test_durable_open_oplock);
1477 torture_suite_add_2smb2_test(suite, "lease", test_durable_open_lease);
1478 torture_suite_add_1smb2_test(suite, "lock-oplock", test_durable_open_lock_oplock);
1479 torture_suite_add_1smb2_test(suite, "lock-lease", test_durable_open_lock_lease);
1480 torture_suite_add_2smb2_test(suite, "open2-lease",
1481 test_durable_open_open2_lease);
1482 torture_suite_add_2smb2_test(suite, "open2-oplock",
1483 test_durable_open_open2_oplock);
1485 suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
1487 return suite;