s4-torture: add test for lease break after file unlink
[nivanova/samba-autobuild/.git] / source4 / torture / smb2 / lease.c
index 5678436cc4935479a8c31662491811efe6aa881a..bf45988222aebf2ddce109b8e7ae66064f80d876 100644 (file)
@@ -27,6 +27,9 @@
 #include "torture/smb2/proto.h"
 #include "torture/util.h"
 #include "libcli/smb/smbXcli_base.h"
+#include "libcli/security/security.h"
+#include "lib/param/param.h"
+#include "lease_break_handler.h"
 
 #define CHECK_VAL(v, correct) do { \
        if ((v) != (correct)) { \
@@ -119,7 +122,8 @@ static bool test_lease_request(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls;
-       struct smb2_handle h1, h2;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
        NTSTATUS status;
        const char *fname = "lease_request.dat";
        const char *fname2 = "lease_request.2.dat";
@@ -207,7 +211,8 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls;
-       struct smb2_handle h, hnew;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle hnew = {{0}};
        NTSTATUS status;
        const char *fname = "lease_upgrade.dat";
        bool ret = true;
@@ -380,140 +385,6 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
 }
 
 
-#define CHECK_LEASE_BREAK(__lb, __oldstate, __state, __key)            \
-       do {                                                            \
-               uint16_t __new = smb2_util_lease_state(__state); \
-               uint16_t __old = smb2_util_lease_state(__oldstate); \
-               CHECK_VAL((__lb)->new_lease_state, __new);      \
-               CHECK_VAL((__lb)->current_lease.lease_state, __old); \
-               CHECK_VAL((__lb)->current_lease.lease_key.data[0], (__key)); \
-               CHECK_VAL((__lb)->current_lease.lease_key.data[1], ~(__key)); \
-               if (__old & (SMB2_LEASE_WRITE | SMB2_LEASE_HANDLE)) { \
-                       CHECK_VAL((__lb)->break_flags, \
-                                 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED);   \
-               } else { \
-                       CHECK_VAL((__lb)->break_flags, 0); \
-               } \
-       } while(0)
-
-#define CHECK_LEASE_BREAK_ACK(__lba, __state, __key)                   \
-       do {                                                            \
-               CHECK_VAL((__lba)->out.reserved, 0);                    \
-               CHECK_VAL((__lba)->out.lease.lease_key.data[0], (__key)); \
-               CHECK_VAL((__lba)->out.lease.lease_key.data[1], ~(__key)); \
-               CHECK_VAL((__lba)->out.lease.lease_state, smb2_util_lease_state(__state)); \
-               CHECK_VAL((__lba)->out.lease.lease_flags, 0);           \
-               CHECK_VAL((__lba)->out.lease.lease_duration, 0);        \
-       } while(0)
-
-static struct torture_lease_break {
-       struct smb2_lease_break lease_break;
-       struct smb2_transport *lease_transport;
-       bool lease_skip_ack;
-       struct smb2_lease_break_ack lease_break_ack;
-       int count;
-       int failures;
-
-       struct smb2_handle oplock_handle;
-       uint8_t held_oplock_level;
-       uint8_t oplock_level;
-       int oplock_count;
-       int oplock_failures;
-} break_info;
-
-#define CHECK_NO_BREAK(tctx)   \
-       do {                                                            \
-               torture_wait_for_lease_break(tctx);                     \
-               CHECK_VAL(break_info.failures, 0);                      \
-               CHECK_VAL(break_info.count, 0);                         \
-               CHECK_VAL(break_info.oplock_failures, 0);               \
-               CHECK_VAL(break_info.oplock_count, 0);                  \
-       } while(0)
-
-#define CHECK_OPLOCK_BREAK(__brokento) \
-       do {                                                            \
-               torture_wait_for_lease_break(tctx);                     \
-               CHECK_VAL(break_info.oplock_count, 1);                  \
-               CHECK_VAL(break_info.oplock_failures, 0);               \
-               CHECK_VAL(break_info.oplock_level,                      \
-                         smb2_util_oplock_level(__brokento)); \
-               break_info.held_oplock_level = break_info.oplock_level; \
-       } while(0)
-
-#define _CHECK_BREAK_INFO(__oldstate, __state, __key)                  \
-       do {                                                            \
-               torture_wait_for_lease_break(tctx);                     \
-               CHECK_VAL(break_info.failures, 0);                      \
-               CHECK_VAL(break_info.count, 1);                         \
-               CHECK_LEASE_BREAK(&break_info.lease_break, (__oldstate), \
-                   (__state), (__key));                                \
-               if (!break_info.lease_skip_ack && \
-                   (break_info.lease_break.break_flags &               \
-                    SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED))        \
-               {       \
-                       torture_wait_for_lease_break(tctx);             \
-                       CHECK_LEASE_BREAK_ACK(&break_info.lease_break_ack, \
-                                             (__state), (__key));      \
-               }                                                       \
-       } while(0)
-
-#define CHECK_BREAK_INFO(__oldstate, __state, __key)                   \
-       do {                                                            \
-               _CHECK_BREAK_INFO(__oldstate, __state, __key);          \
-               CHECK_VAL(break_info.lease_break.new_epoch, 0);         \
-       } while(0)
-
-#define CHECK_BREAK_INFO_V2(__transport, __oldstate, __state, __key, __epoch) \
-       do {                                                            \
-               _CHECK_BREAK_INFO(__oldstate, __state, __key);          \
-               CHECK_VAL(break_info.lease_break.new_epoch, __epoch);   \
-               if (!TARGET_IS_SAMBA3(tctx)) {                          \
-                       CHECK_VAL((uintptr_t)break_info.lease_transport, \
-                                 (uintptr_t)__transport);              \
-               } \
-       } while(0)
-
-static void torture_lease_break_callback(struct smb2_request *req)
-{
-       NTSTATUS status;
-
-       status = smb2_lease_break_ack_recv(req, &break_info.lease_break_ack);
-       if (!NT_STATUS_IS_OK(status))
-               break_info.failures++;
-
-       return;
-}
-
-/* a lease break request handler */
-static bool torture_lease_handler(struct smb2_transport *transport,
-                                 const struct smb2_lease_break *lb,
-                                 void *private_data)
-{
-       struct smb2_tree *tree = private_data;
-       struct smb2_lease_break_ack io;
-       struct smb2_request *req;
-
-       break_info.lease_transport = transport;
-       break_info.lease_break = *lb;
-       break_info.count++;
-
-       if (break_info.lease_skip_ack) {
-               return true;
-       }
-
-       if (lb->break_flags & SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED) {
-               ZERO_STRUCT(io);
-               io.in.lease.lease_key = lb->current_lease.lease_key;
-               io.in.lease.lease_state = lb->new_lease_state;
-
-               req = smb2_lease_break_ack_send(tree, &io);
-               req->async.fn = torture_lease_break_callback;
-               req->async.private_data = NULL;
-       }
-
-       return true;
-}
-
 /**
  * upgrade3:
  * full matrix of lease upgrade combinations
@@ -603,7 +474,7 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
 
                smb2_util_unlink(tree, fname);
 
-               ZERO_STRUCT(break_info);
+               ZERO_STRUCT(lease_break_info);
 
                /* grab first lease */
                smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.held1));
@@ -622,8 +493,8 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
                h2 = io.out.file.handle;
 
                /* no break has happened */
-               CHECK_VAL(break_info.count, 0);
-               CHECK_VAL(break_info.failures, 0);
+               CHECK_VAL(lease_break_info.count, 0);
+               CHECK_VAL(lease_break_info.failures, 0);
 
                /* try to upgrade lease1 */
                smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.upgrade_to));
@@ -634,8 +505,8 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
                hnew = io.out.file.handle;
 
                /* no break has happened */
-               CHECK_VAL(break_info.count, 0);
-               CHECK_VAL(break_info.failures, 0);
+               CHECK_VAL(lease_break_info.count, 0);
+               CHECK_VAL(lease_break_info.failures, 0);
 
                smb2_util_close(tree, hnew);
                smb2_util_close(tree, h);
@@ -656,59 +527,6 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
 
 
 
-/*
-   Timer handler function notifies the registering function that time is up
-*/
-static void timeout_cb(struct tevent_context *ev,
-                      struct tevent_timer *te,
-                      struct timeval current_time,
-                      void *private_data)
-{
-       bool *timesup = (bool *)private_data;
-       *timesup = true;
-       return;
-}
-
-/*
-   Wait a short period of time to receive a single oplock break request
-*/
-static void torture_wait_for_lease_break(struct torture_context *tctx)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       struct tevent_timer *te = NULL;
-       struct timeval ne;
-       bool timesup = false;
-       int old_count = break_info.count;
-
-       /* Wait .1 seconds for an lease break */
-       ne = tevent_timeval_current_ofs(0, 100000);
-
-       te = tevent_add_timer(tctx->ev, tmp_ctx, ne, timeout_cb, &timesup);
-       if (te == NULL) {
-               torture_comment(tctx, "Failed to wait for an oplock break. "
-                                     "test results may not be accurate.");
-               goto done;
-       }
-
-       while (!timesup && break_info.count < old_count + 1) {
-               if (tevent_loop_once(tctx->ev) != 0) {
-                       torture_comment(tctx, "Failed to wait for an oplock "
-                                             "break. test results may not be "
-                                             "accurate.");
-                       goto done;
-               }
-       }
-
-done:
-       /* We don't know if the timed event fired and was freed, we received
-        * our oplock break, or some other event triggered the loop.  Thus,
-        * we create a tmp_ctx to be able to safely free/remove the timed
-        * event in all 3 cases. */
-       talloc_free(tmp_ctx);
-
-       return;
-}
-
 /*
   break_results should be read as "held lease, new lease, hold broken to, new
   grant", i.e. { "RH", "RW", "RH", "R" } means that if key1 holds RH and key2
@@ -774,7 +592,7 @@ static bool test_lease_break(struct torture_context *tctx,
                    held, smb2_util_lease_state(held), contend, smb2_util_lease_state(contend),
                    brokento, smb2_util_lease_state(brokento), granted, smb2_util_lease_state(granted));
 
-               ZERO_STRUCT(break_info);
+               ZERO_STRUCT(lease_break_info);
 
                /* Grab lease. */
                smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(held));
@@ -798,7 +616,7 @@ static bool test_lease_break(struct torture_context *tctx,
                        CHECK_NO_BREAK(tctx);
                }
 
-               ZERO_STRUCT(break_info);
+               ZERO_STRUCT(lease_break_info);
 
                /*
                  Now verify that an attempt to upgrade LEASE1 results in no
@@ -810,8 +628,8 @@ static bool test_lease_break(struct torture_context *tctx,
                h3 = io.out.file.handle;
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
                CHECK_LEASE(&io, brokento, true, LEASE1, 0);
-               CHECK_VAL(break_info.count, 0);
-               CHECK_VAL(break_info.failures, 0);
+               CHECK_VAL(lease_break_info.count, 0);
+               CHECK_VAL(lease_break_info.failures, 0);
 
                smb2_util_close(tree, h);
                smb2_util_close(tree, h2);
@@ -838,7 +656,8 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls;
-       struct smb2_handle h1, h2;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
        NTSTATUS status;
        const char *fname = "lease_nobreakself.dat";
        bool ret = true;
@@ -869,7 +688,7 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
        h2 = io.out.file.handle;
        CHECK_LEASE(&io, "R", true, LEASE2, 0);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
@@ -891,14 +710,14 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
 
        /* Now break LEASE1 via h2 */
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
        status = smb2_util_write(tree, h2, &c, 0, 1);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_BREAK_INFO("R", "", LEASE1);
 
        /* .. and break LEASE2 via h1 */
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
        status = smb2_util_write(tree, h1, &c, 0, 1);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_BREAK_INFO("R", "", LEASE2);
@@ -911,6 +730,231 @@ done:
        return ret;
 }
 
+static bool test_lease_statopen(struct torture_context *tctx,
+                                  struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       NTSTATUS status;
+       const char *fname = "lease_statopen.dat";
+       bool ret = true;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(
+               tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       /* Create file. */
+       smb2_lease_create(&io, &ls, false, fname, LEASE1,
+                         smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       smb2_util_close(tree, h1);
+
+       /* Stat open file with RWH lease. */
+       smb2_lease_create_share(&io, &ls, false, fname, 0, LEASE1,
+                         smb2_util_lease_state("RWH"));
+       io.in.desired_access = FILE_READ_ATTRIBUTES;
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io.out.file.handle;
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+       ZERO_STRUCT(lease_break_info);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       /* Ensure non-stat open doesn't break and gets same lease
+          state as existing stat open. */
+       smb2_lease_create(&io, &ls, false, fname, LEASE1,
+                         smb2_util_lease_state(""));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+       CHECK_NO_BREAK(tctx);
+       smb2_util_close(tree, h1);
+
+       /* Open with conflicting lease. stat open should break down to RH */
+       smb2_lease_create(&io, &ls, false, fname, LEASE2,
+                         smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RH", true, LEASE2, 0);
+
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+
+done:
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h1);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_statopen2(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_handle h3 = {{0}};
+       NTSTATUS status;
+       const char *fname = "lease_statopen2.dat";
+       bool ret = true;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(
+               tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+       ZERO_STRUCT(lease_break_info);
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* Open file with RWH lease. */
+       smb2_lease_create_share(&io, &ls, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       io.in.desired_access = SEC_FILE_WRITE_DATA;
+       status = smb2_create(tree, mem_ctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       h1 = io.out.file.handle;
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+       /* Stat open */
+       ZERO_STRUCT(io);
+       io.in.desired_access = FILE_READ_ATTRIBUTES;
+       io.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.in.fname = fname;
+       status = smb2_create(tree, mem_ctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       h2 = io.out.file.handle;
+
+       /* Open file with RWH lease. */
+       smb2_lease_create_share(&io, &ls, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       io.in.desired_access = SEC_FILE_WRITE_DATA;
+       status = smb2_create(tree, mem_ctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       h3 = io.out.file.handle;
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+done:
+       if (!smb2_util_handle_empty(h3)) {
+               smb2_util_close(tree, h3);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_statopen3(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       NTSTATUS status;
+       const char *fname = "lease_statopen3.dat";
+       bool ret = true;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(
+               tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+       ZERO_STRUCT(lease_break_info);
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* Stat open */
+       ZERO_STRUCT(io);
+       io.in.desired_access = FILE_READ_ATTRIBUTES;
+       io.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.in.fname = fname;
+       status = smb2_create(tree, mem_ctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       h1 = io.out.file.handle;
+
+       /* Open file with RWH lease. */
+       smb2_lease_create_share(&io, &ls, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       io.in.desired_access = SEC_FILE_WRITE_DATA;
+       status = smb2_create(tree, mem_ctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create failed\n");
+       h2 = io.out.file.handle;
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
 static void torture_oplock_break_callback(struct smb2_request *req)
 {
        NTSTATUS status;
@@ -919,7 +963,7 @@ static void torture_oplock_break_callback(struct smb2_request *req)
        ZERO_STRUCT(br);
        status = smb2_break_recv(req, &br);
        if (!NT_STATUS_IS_OK(status))
-               break_info.oplock_failures++;
+               lease_break_info.oplock_failures++;
 
        return;
 }
@@ -933,20 +977,20 @@ static bool torture_oplock_handler(struct smb2_transport *transport,
        struct smb2_request *req;
        struct smb2_break br;
 
-       break_info.oplock_handle = *handle;
-       break_info.oplock_level = level;
-       break_info.oplock_count++;
+       lease_break_info.oplock_handle = *handle;
+       lease_break_info.oplock_level   = level;
+       lease_break_info.oplock_count++;
 
        ZERO_STRUCT(br);
        br.in.file.handle = *handle;
        br.in.oplock_level = level;
 
-       if (break_info.held_oplock_level > SMB2_OPLOCK_LEVEL_II) {
+       if (lease_break_info.held_oplock_level > SMB2_OPLOCK_LEVEL_II) {
                req = smb2_break_send(tree, &br);
                req->async.fn = torture_oplock_break_callback;
                req->async.private_data = NULL;
        }
-       break_info.held_oplock_level = level;
+       lease_break_info.held_oplock_level = level;
 
        return true;
 }
@@ -1022,7 +1066,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                    held, smb2_util_lease_state(held), contend, smb2_util_oplock_level(contend),
                    brokento, smb2_util_lease_state(brokento), granted, smb2_util_oplock_level(granted));
 
-               ZERO_STRUCT(break_info);
+               ZERO_STRUCT(lease_break_info);
 
                /* Grab lease. */
                smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(held));
@@ -1039,7 +1083,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                h2 = io.out.file.handle;
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
                CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(granted));
-               break_info.held_oplock_level = io.out.oplock_level;
+               lease_break_info.held_oplock_level = io.out.oplock_level;
 
                if (smb2_util_lease_state(held) != smb2_util_lease_state(brokento)) {
                        CHECK_BREAK_INFO(held, brokento, LEASE1);
@@ -1064,7 +1108,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                    held, smb2_util_oplock_level(held), contend, smb2_util_lease_state(contend),
                    brokento, smb2_util_oplock_level(brokento), granted, smb2_util_lease_state(granted));
 
-               ZERO_STRUCT(break_info);
+               ZERO_STRUCT(lease_break_info);
 
                /* Grab an oplock. */
                smb2_oplock_create(&io, fname, smb2_util_oplock_level(held));
@@ -1073,7 +1117,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                h = io.out.file.handle;
                CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
                CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(held));
-               break_info.held_oplock_level = io.out.oplock_level;
+               lease_break_info.held_oplock_level = io.out.oplock_level;
 
                /* Grab lease. */
                smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(contend));
@@ -1113,7 +1157,9 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls;
-       struct smb2_handle h, h2, h3;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_handle h3 = {{0}};
        struct smb2_write w;
        NTSTATUS status;
        const char *fname = "lease_multibreak.dat";
@@ -1132,7 +1178,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
 
        smb2_util_unlink(tree, fname);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        /* Grab lease, upgrade to RHW .. */
        smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RH"));
@@ -1168,7 +1214,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        status = smb2_util_close(tree, h3);
        CHECK_STATUS(status, NT_STATUS_OK);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        /* Grab an R lease. */
        smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("R"));
@@ -1185,7 +1231,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        h2 = io.out.file.handle;
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
        CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("s"));
-       break_info.held_oplock_level = io.out.oplock_level;
+       lease_break_info.held_oplock_level = io.out.oplock_level;
 
        /* Verify no breaks. */
        CHECK_NO_BREAK(tctx);
@@ -1198,7 +1244,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        h3 = io.out.file.handle;
        CHECK_CREATED(&io, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
        CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(""));
-       break_info.held_oplock_level = io.out.oplock_level;
+       lease_break_info.held_oplock_level = io.out.oplock_level;
 
        /* Sleep, use a write to clear the recv queue. */
        smb_msleep(250);
@@ -1232,7 +1278,7 @@ static bool test_lease_v2_request_parent(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls;
-       struct smb2_handle h1;
+       struct smb2_handle h1 = {{0}};
        uint64_t parent = LEASE2;
        NTSTATUS status;
        const char *fname = "lease_v2_request_parent.dat";
@@ -1255,7 +1301,7 @@ static bool test_lease_v2_request_parent(struct torture_context *tctx,
 
        smb2_util_unlink(tree, fname);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_v2_create_share(&io, &ls, false, fname,
@@ -1288,7 +1334,7 @@ static bool test_lease_break_twice(struct torture_context *tctx,
        struct smb2_create io;
        struct smb2_lease ls1;
        struct smb2_lease ls2;
-       struct smb2_handle h1;
+       struct smb2_handle h1 = {{0}};
        NTSTATUS status;
        const char *fname = "lease_break_twice.dat";
        bool ret = true;
@@ -1308,7 +1354,7 @@ static bool test_lease_break_twice(struct torture_context *tctx,
 
        smb2_util_unlink(tree, fname);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
        ZERO_STRUCT(io);
 
        smb2_lease_v2_create_share(
@@ -1324,7 +1370,7 @@ static bool test_lease_break_twice(struct torture_context *tctx,
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        smb2_lease_v2_create_share(
                &io, &ls2, false, fname, smb2_util_share_access("R"),
@@ -1339,7 +1385,7 @@ static bool test_lease_break_twice(struct torture_context *tctx,
                &io, &ls2, false, fname, smb2_util_share_access("RWD"),
                LEASE2, NULL, smb2_util_lease_state("RWH"), 0x22);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
@@ -1360,7 +1406,11 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
        struct smb2_lease ls1, ls2, ls2t, ls3, ls4;
-       struct smb2_handle h1, h2, h3, h4, h5;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_handle h3 = {{0}};
+       struct smb2_handle h4 = {{0}};
+       struct smb2_handle h5 = {{0}};
        struct smb2_write w;
        NTSTATUS status;
        const char *fname = "lease_v2_request.dat";
@@ -1392,7 +1442,7 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        tree->session->transport->oplock.handler = torture_oplock_handler;
        tree->session->transport->oplock.private_data = tree;
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_v2_create_share(&io, &ls1, false, fname,
@@ -1450,7 +1500,7 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        CHECK_BREAK_INFO_V2(tree->session->transport,
                            "RH", "", LEASE2, ls2.lease_epoch + 2);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_v2_create_share(&io, &ls2t, true, dname,
@@ -1535,7 +1585,7 @@ static bool test_lease_v2_epoch1(struct torture_context *tctx,
        tree->session->transport->oplock.handler = torture_oplock_handler;
        tree->session->transport->oplock.private_data = tree;
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_v2_create_share(&io, &ls, false, fname,
@@ -1600,7 +1650,7 @@ static bool test_lease_v2_epoch2(struct torture_context *tctx,
        tree->session->transport->oplock.handler = torture_oplock_handler;
        tree->session->transport->oplock.private_data = tree;
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_v2_create_share(&io, &ls1v2, false, fname,
@@ -1705,7 +1755,7 @@ static bool test_lease_v2_epoch3(struct torture_context *tctx,
        tree->session->transport->oplock.handler = torture_oplock_handler;
        tree->session->transport->oplock.private_data = tree;
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
        ZERO_STRUCT(io);
        smb2_lease_create_share(&io, &ls1v1, false, fname,
@@ -1810,8 +1860,8 @@ static bool test_lease_breaking1(struct torture_context *tctx,
        /*
         * we defer acking the lease break.
         */
-       ZERO_STRUCT(break_info);
-       break_info.lease_skip_ack = true;
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
 
        smb2_lease_create_share(&io1, &ls1, false, fname,
                                smb2_util_share_access("RWD"),
@@ -1839,10 +1889,10 @@ static bool test_lease_breaking1(struct torture_context *tctx,
        torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
 
        ack.in.lease.lease_key =
-               break_info.lease_break.current_lease.lease_key;
+               lease_break_info.lease_break.current_lease.lease_key;
        ack.in.lease.lease_state =
-               break_info.lease_break.new_lease_state;
-       ZERO_STRUCT(break_info);
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
 
        /*
         * a open using the same lease key is still works,
@@ -1885,37 +1935,1408 @@ done:
        return ret;
 }
 
-static bool test_lease_complex1(struct torture_context *tctx,
-                               struct smb2_tree *tree1a)
+static bool test_lease_breaking2(struct torture_context *tctx,
+                                struct smb2_tree *tree)
 {
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
-       struct smb2_create io1;
-       struct smb2_create io2;
-       struct smb2_lease ls1;
-       struct smb2_lease ls2;
-       struct smb2_handle h, h2, h3;
-       struct smb2_write w;
-       NTSTATUS status;
-       const char *fname = "lease_complex1.dat";
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_handle h1a = {};
+       struct smb2_handle h1b = {};
+       struct smb2_handle h2 = {};
+       struct smb2_request *req2 = NULL;
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "lease_breaking2.dat";
        bool ret = true;
+       NTSTATUS status;
        uint32_t caps;
-       struct smb2_tree *tree1b = NULL;
-       struct smbcli_options options1;
-
-       options1 = tree1a->session->transport->options;
 
-       caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
        if (!(caps & SMB2_CAP_LEASING)) {
                torture_skip(tctx, "leases are not supported");
        }
 
-       tree1a->session->transport->lease.handler = torture_lease_handler;
-       tree1a->session->transport->lease.private_data = tree1a;
-       tree1a->session->transport->oplock.handler = torture_oplock_handler;
-       tree1a->session->transport->oplock.private_data = tree1a;
+       smb2_util_unlink(tree, fname);
 
-       /* create a new connection (same client_guid) */
-       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1a = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, 0);
+
+       /*
+        * a conflicting open is blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * we got the lease break, but defer the ack.
+        */
+       CHECK_BREAK_INFO("RWH", "", LEASE1);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       /*
+        * We ack the lease break.
+        */
+       ack.in.lease.lease_state =
+               SMB2_LEASE_READ | SMB2_LEASE_WRITE | SMB2_LEASE_HANDLE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state =
+               SMB2_LEASE_READ | SMB2_LEASE_WRITE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state =
+               SMB2_LEASE_WRITE | SMB2_LEASE_HANDLE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state =
+               SMB2_LEASE_READ | SMB2_LEASE_HANDLE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state = SMB2_LEASE_WRITE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state = SMB2_LEASE_HANDLE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       ack.in.lease.lease_state = SMB2_LEASE_READ;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_REQUEST_NOT_ACCEPTED);
+
+       /* Try again with the correct state this time. */
+       ack.in.lease.lease_state = SMB2_LEASE_NONE;;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "", LEASE1);
+
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
+
+       torture_assert(tctx, req2->cancel.can_cancel,
+                      "req2 can_cancel");
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_NO_BREAK(tctx);
+
+       /* Get state of the original handle. */
+       smb2_lease_create(&io1, &ls1, false, fname, LEASE1, smb2_util_lease_state(""));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io1, "", true, LEASE1, 0);
+       smb2_util_close(tree, io1.out.file.handle);
+
+done:
+       smb2_util_close(tree, h1a);
+       smb2_util_close(tree, h1b);
+       smb2_util_close(tree, h2);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_breaking3(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_create io3 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_handle h1a = {};
+       struct smb2_handle h1b = {};
+       struct smb2_handle h2 = {};
+       struct smb2_handle h3 = {};
+       struct smb2_request *req2 = NULL;
+       struct smb2_request *req3 = NULL;
+       struct lease_break_info lease_break_info_tmp = {};
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "lease_breaking3.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1a = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, 0);
+
+       /*
+        * a conflicting open is blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * we got the lease break, but defer the ack.
+        */
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+       smb2_util_close(tree, h1b);
+
+       /*
+        * a conflicting open with NTCREATEX_DISP_OVERWRITE
+        * doesn't trigger an immediate lease break to none.
+        */
+       lease_break_info_tmp = lease_break_info;
+       ZERO_STRUCT(lease_break_info);
+       smb2_oplock_create(&io3, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io3.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req3 = smb2_create_send(tree, &io3);
+       torture_assert(tctx, req3 != NULL, "smb2_create_send");
+       CHECK_NO_BREAK(tctx);
+       lease_break_info = lease_break_info_tmp;
+
+       torture_assert(tctx, req3->state == SMB2_REQUEST_RECV, "req3 pending");
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * We ack the lease break, but defer acking the next break (to "R")
+        */
+       lease_break_info.lease_skip_ack = true;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "RH", LEASE1);
+
+       /*
+        * We got an additional break downgrading to just "R"
+        * while we defer the ack.
+        */
+       CHECK_BREAK_INFO("RH", "R", LEASE1);
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+       torture_assert(tctx, req3->state == SMB2_REQUEST_RECV, "req3 pending");
+
+       /*
+        * We ack the downgrade to "R" and get an immediate break to none
+        */
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "R", LEASE1);
+
+       /*
+        * We get the downgrade to none.
+        */
+       CHECK_BREAK_INFO("R", "", LEASE1);
+
+       torture_assert(tctx, req2->cancel.can_cancel,
+                      "req2 can_cancel");
+       torture_assert(tctx, req3->cancel.can_cancel,
+                      "req3 can_cancel");
+
+       ZERO_STRUCT(lease_break_info);
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       status = smb2_create_recv(req3, tctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io3.out.file.handle;
+       CHECK_CREATED(&io3, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io3.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_NO_BREAK(tctx);
+done:
+       smb2_util_close(tree, h1a);
+       smb2_util_close(tree, h1b);
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h3);
+
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_v2_breaking3(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_create io3 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_handle h1a = {};
+       struct smb2_handle h1b = {};
+       struct smb2_handle h2 = {};
+       struct smb2_handle h3 = {};
+       struct smb2_request *req2 = NULL;
+       struct smb2_request *req3 = NULL;
+       struct lease_break_info lease_break_info_tmp = {};
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "v2_lease_breaking3.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+       enum protocol_types protocol;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       protocol = smbXcli_conn_protocol(tree->session->transport->conn);
+       if (protocol < PROTOCOL_SMB3_00) {
+               torture_skip(tctx, "v2 leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_v2_create_share(&io1, &ls1, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  0x11);
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1a = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       /* Epoch increases on open. */
+       ls1.lease_epoch += 1;
+       CHECK_LEASE_V2(&io1, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch);
+
+       /*
+        * a conflicting open is blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * we got the lease break, but defer the ack.
+        */
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RWH", "RH", LEASE1, ls1.lease_epoch + 1);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       /* On receiving a lease break, we must sync the new epoch. */
+       ls1.lease_epoch = lease_break_info.lease_break.new_epoch;
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io1, "RHW", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS, 0, ls1.lease_epoch);
+       smb2_util_close(tree, h1b);
+
+       /*
+        * a conflicting open with NTCREATEX_DISP_OVERWRITE
+        * doesn't trigger an immediate lease break to none.
+        */
+       lease_break_info_tmp = lease_break_info;
+       ZERO_STRUCT(lease_break_info);
+       smb2_oplock_create(&io3, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io3.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req3 = smb2_create_send(tree, &io3);
+       torture_assert(tctx, req3 != NULL, "smb2_create_send");
+       CHECK_NO_BREAK(tctx);
+       lease_break_info = lease_break_info_tmp;
+
+       torture_assert(tctx, req3->state == SMB2_REQUEST_RECV, "req3 pending");
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io1, "RHW", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS, 0, ls1.lease_epoch);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * We ack the lease break, but defer acking the next break (to "R")
+        */
+       lease_break_info.lease_skip_ack = true;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "RH", LEASE1);
+
+       /*
+        * We got an additional break downgrading to just "R"
+        * while we defer the ack.
+        */
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RH", "R", LEASE1, ls1.lease_epoch);
+       /* On receiving a lease break, we must sync the new epoch. */
+       ls1.lease_epoch = lease_break_info.lease_break.new_epoch;
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io1, "RH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS, 0, ls1.lease_epoch);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+       torture_assert(tctx, req3->state == SMB2_REQUEST_RECV, "req3 pending");
+
+       /*
+        * We ack the downgrade to "R" and get an immediate break to none
+        */
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "R", LEASE1);
+
+       /*
+        * We get the downgrade to none.
+        */
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "R", "", LEASE1, ls1.lease_epoch);
+
+       torture_assert(tctx, req2->cancel.can_cancel,
+                      "req2 can_cancel");
+       torture_assert(tctx, req3->cancel.can_cancel,
+                      "req3 can_cancel");
+
+       ZERO_STRUCT(lease_break_info);
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       status = smb2_create_recv(req3, tctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io3.out.file.handle;
+       CHECK_CREATED(&io3, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io3.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_NO_BREAK(tctx);
+done:
+       smb2_util_close(tree, h1a);
+       smb2_util_close(tree, h1b);
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h3);
+
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+
+static bool test_lease_breaking4(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_create io3 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_lease ls1t = {};
+       struct smb2_handle h1 = {};
+       struct smb2_handle h2 = {};
+       struct smb2_handle h3 = {};
+       struct smb2_request *req2 = NULL;
+       struct lease_break_info lease_break_info_tmp = {};
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "lease_breaking4.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RH"));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RH", true, LEASE1, 0);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * a conflicting open is *not* blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * We got a break from RH to NONE, we're supported to ack
+        * this downgrade
+        */
+       CHECK_BREAK_INFO("RH", "", LEASE1);
+
+       lease_break_info_tmp = lease_break_info;
+       ZERO_STRUCT(lease_break_info);
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_DONE, "req2 done");
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+       smb2_util_close(tree, h2);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * a conflicting open is *not* blocked until we ack the
+        * lease break, even if the lease is in breaking state.
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_DONE, "req2 done");
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+       smb2_util_close(tree, h2);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * We now ask the server about the current lease state
+        * which should still be "RH", but with
+        * SMB2_LEASE_FLAG_BREAK_IN_PROGRESS.
+        */
+       smb2_lease_create_share(&io3, &ls1t, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state(""));
+       status = smb2_create(tree, mem_ctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io3.out.file.handle;
+       CHECK_CREATED(&io3, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io3, "RH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+
+       /*
+        * We finally ack the lease break...
+        */
+       CHECK_NO_BREAK(tctx);
+       lease_break_info = lease_break_info_tmp;
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "", LEASE1);
+
+       CHECK_NO_BREAK(tctx);
+
+done:
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h3);
+
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_breaking5(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_create io3 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_lease ls1t = {};
+       struct smb2_handle h1 = {};
+       struct smb2_handle h2 = {};
+       struct smb2_handle h3 = {};
+       struct smb2_request *req2 = NULL;
+       struct lease_break_info lease_break_info_tmp = {};
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "lease_breaking5.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("R"));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "R", true, LEASE1, 0);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * a conflicting open is *not* blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       io2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * We got a break from RH to NONE, we're supported to ack
+        * this downgrade
+        */
+       CHECK_BREAK_INFO("R", "", LEASE1);
+
+       lease_break_info_tmp = lease_break_info;
+       ZERO_STRUCT(lease_break_info);
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_DONE, "req2 done");
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * We now ask the server about the current lease state
+        * which should still be "RH", but with
+        * SMB2_LEASE_FLAG_BREAK_IN_PROGRESS.
+        */
+       smb2_lease_create_share(&io3, &ls1t, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state(""));
+       status = smb2_create(tree, mem_ctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io3.out.file.handle;
+       CHECK_CREATED(&io3, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io3, "", true, LEASE1, 0);
+
+       /*
+        * We send an ack without without being asked.
+        */
+       CHECK_NO_BREAK(tctx);
+       lease_break_info = lease_break_info_tmp;
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(lease_break_info);
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
+
+       CHECK_NO_BREAK(tctx);
+
+done:
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h3);
+
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_breaking6(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_handle h1a = {};
+       struct smb2_handle h1b = {};
+       struct smb2_handle h2 = {};
+       struct smb2_request *req2 = NULL;
+       struct smb2_lease_break_ack ack = {};
+       const char *fname = "lease_breaking6.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1a = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, 0);
+
+       /*
+        * a conflicting open is blocked until we ack the
+        * lease break
+        */
+       smb2_oplock_create(&io2, fname, SMB2_OPLOCK_LEVEL_NONE);
+       req2 = smb2_create_send(tree, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       /*
+        * we got the lease break, but defer the ack.
+        */
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * a open using the same lease key is still works,
+        * but reports SMB2_LEASE_FLAG_BREAK_IN_PROGRESS
+        */
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1b = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, SMB2_LEASE_FLAG_BREAK_IN_PROGRESS);
+       smb2_util_close(tree, h1b);
+
+       CHECK_NO_BREAK(tctx);
+
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       /*
+        * We are asked to break to "RH", but we are allowed to
+        * break to any of "RH", "R" or NONE.
+        */
+       ack.in.lease.lease_state = SMB2_LEASE_NONE;
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "", LEASE1);
+
+       torture_assert(tctx, req2->cancel.can_cancel,
+                      "req2 can_cancel");
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_NO_BREAK(tctx);
+done:
+       smb2_util_close(tree, h1a);
+       smb2_util_close(tree, h1b);
+       smb2_util_close(tree, h2);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_lock1(struct torture_context *tctx,
+                            struct smb2_tree *tree1a,
+                            struct smb2_tree *tree2)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1 = {};
+       struct smb2_create io2 = {};
+       struct smb2_create io3 = {};
+       struct smb2_lease ls1 = {};
+       struct smb2_lease ls2 = {};
+       struct smb2_lease ls3 = {};
+       struct smb2_handle h1 = {};
+       struct smb2_handle h2 = {};
+       struct smb2_handle h3 = {};
+       struct smb2_lock lck;
+       struct smb2_lock_element el[1];
+       const char *fname = "locktest.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+       struct smbcli_options options1;
+       struct smb2_tree *tree1b = NULL;
+
+       options1 = tree1a->session->transport->options;
+
+       caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       /* Set up handlers. */
+       tree2->session->transport->lease.handler = torture_lease_handler;
+       tree2->session->transport->lease.private_data = tree2;
+       tree2->session->transport->oplock.handler = torture_oplock_handler;
+       tree2->session->transport->oplock.private_data = tree2;
+
+       tree1a->session->transport->lease.handler = torture_lease_handler;
+       tree1a->session->transport->lease.private_data = tree1a;
+       tree1a->session->transport->oplock.handler = torture_oplock_handler;
+       tree1a->session->transport->oplock.private_data = tree1a;
+
+       /* create a new connection (same client_guid) */
+       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
+               torture_warning(tctx, "couldn't reconnect, bailing\n");
+               ret = false;
+               goto done;
+       }
+
+       tree1b->session->transport->lease.handler = torture_lease_handler;
+       tree1b->session->transport->lease.private_data = tree1b;
+       tree1b->session->transport->oplock.handler = torture_oplock_handler;
+       tree1b->session->transport->oplock.private_data = tree1b;
+
+       smb2_util_unlink(tree1a, fname);
+
+       ZERO_STRUCT(lease_break_info);
+       ZERO_STRUCT(lck);
+
+       /* Open a handle on tree1a. */
+       smb2_lease_create_share(&io1, &ls1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree1a, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RWH", true, LEASE1, 0);
+
+       /* Open a second handle on tree1b. */
+       smb2_lease_create_share(&io2, &ls2, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE2,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree1b, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io2, "RH", true, LEASE2, 0);
+       /* And LEASE1 got broken to RH. */
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+       ZERO_STRUCT(lease_break_info);
+
+       /* Now open a lease on a different client guid. */
+       smb2_lease_create_share(&io3, &ls3, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE3,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree2, mem_ctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io3.out.file.handle;
+       CHECK_CREATED(&io3, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io3, "RH", true, LEASE3, 0);
+       /* Doesn't break. */
+       CHECK_NO_BREAK(tctx);
+
+       lck.in.locks            = el;
+       /*
+        * Try and get get an exclusive byte
+        * range lock on H1 (LEASE1).
+        */
+
+       lck.in.lock_count       = 1;
+       lck.in.lock_sequence    = 1;
+       lck.in.file.handle      = h1;
+       el[0].offset            = 0;
+       el[0].length            = 1;
+       el[0].reserved          = 0;
+       el[0].flags             = SMB2_LOCK_FLAG_EXCLUSIVE;
+       status = smb2_lock(tree1a, &lck);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /* LEASE2 and LEASE3 should get broken to NONE. */
+       torture_wait_for_lease_break(tctx);
+       torture_wait_for_lease_break(tctx);
+       torture_wait_for_lease_break(tctx);
+       torture_wait_for_lease_break(tctx);
+
+       CHECK_VAL(lease_break_info.failures, 0);                      \
+       CHECK_VAL(lease_break_info.count, 2);                         \
+
+       /* Get state of the H1 (LEASE1) */
+       smb2_lease_create(&io1, &ls1, false, fname, LEASE1, smb2_util_lease_state(""));
+       status = smb2_create(tree1a, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       /* Should still be RH. */
+       CHECK_LEASE(&io1, "RH", true, LEASE1, 0);
+       smb2_util_close(tree1a, io1.out.file.handle);
+
+       /* Get state of the H2 (LEASE2) */
+       smb2_lease_create(&io2, &ls2, false, fname, LEASE2, smb2_util_lease_state(""));
+       status = smb2_create(tree1b, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io2, "", true, LEASE2, 0);
+       smb2_util_close(tree1b, io2.out.file.handle);
+
+       /* Get state of the H3 (LEASE3) */
+       smb2_lease_create(&io3, &ls3, false, fname, LEASE3, smb2_util_lease_state(""));
+       status = smb2_create(tree2, mem_ctx, &io3);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io3, "", true, LEASE3, 0);
+       smb2_util_close(tree2, io3.out.file.handle);
+
+       ZERO_STRUCT(lease_break_info);
+
+       /*
+        * Try and get get an exclusive byte
+        * range lock on H3 (LEASE3).
+        */
+       lck.in.lock_count       = 1;
+       lck.in.lock_sequence    = 2;
+       lck.in.file.handle      = h3;
+       el[0].offset            = 100;
+       el[0].length            = 1;
+       el[0].reserved          = 0;
+       el[0].flags             = SMB2_LOCK_FLAG_EXCLUSIVE;
+       status = smb2_lock(tree2, &lck);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       /* LEASE1 got broken to NONE. */
+       CHECK_BREAK_INFO("RH", "", LEASE1);
+       ZERO_STRUCT(lease_break_info);
+
+done:
+       smb2_util_close(tree1a, h1);
+       smb2_util_close(tree1b, h2);
+       smb2_util_close(tree2, h3);
+
+       smb2_util_unlink(tree1a, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_complex1(struct torture_context *tctx,
+                               struct smb2_tree *tree1a)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1;
+       struct smb2_create io2;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_handle h3 = {{0}};
+       struct smb2_write w;
+       NTSTATUS status;
+       const char *fname = "lease_complex1.dat";
+       bool ret = true;
+       uint32_t caps;
+       struct smb2_tree *tree1b = NULL;
+       struct smbcli_options options1;
+
+       options1 = tree1a->session->transport->options;
+
+       caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       tree1a->session->transport->lease.handler = torture_lease_handler;
+       tree1a->session->transport->lease.private_data = tree1a;
+       tree1a->session->transport->oplock.handler = torture_oplock_handler;
+       tree1a->session->transport->oplock.private_data = tree1a;
+
+       /* create a new connection (same client_guid) */
+       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
+               torture_warning(tctx, "couldn't reconnect, bailing\n");
+               ret = false;
+               goto done;
+       }
+
+       tree1b->session->transport->lease.handler = torture_lease_handler;
+       tree1b->session->transport->lease.private_data = tree1b;
+       tree1b->session->transport->oplock.handler = torture_oplock_handler;
+       tree1b->session->transport->oplock.private_data = tree1b;
+
+       smb2_util_unlink(tree1a, fname);
+
+       ZERO_STRUCT(lease_break_info);
+
+       /* Grab R lease over connection 1a */
+       smb2_lease_create(&io1, &ls1, false, fname, LEASE1, smb2_util_lease_state("R"));
+       status = smb2_create(tree1a, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "R", true, LEASE1, 0);
+
+       /* Upgrade to RWH over connection 1b */
+       ls1.lease_state = smb2_util_lease_state("RWH");
+       status = smb2_create(tree1b, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RHW", true, LEASE1, 0);
+
+       /* close over connection 1b */
+       status = smb2_util_close(tree1b, h2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /* Contend with LEASE2. */
+       smb2_lease_create(&io2, &ls2, false, fname, LEASE2, smb2_util_lease_state("R"));
+       status = smb2_create(tree1b, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io2, "R", true, LEASE2, 0);
+
+       /* Verify that we were only sent one break. */
+       CHECK_BREAK_INFO("RHW", "RH", LEASE1);
+
+       /* again RH over connection 1b doesn't change the epoch */
+       ls1.lease_state = smb2_util_lease_state("RH");
+       status = smb2_create(tree1b, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RH", true, LEASE1, 0);
+
+       /* close over connection 1b */
+       status = smb2_util_close(tree1b, h2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ZERO_STRUCT(lease_break_info);
+
+       ZERO_STRUCT(w);
+       w.in.file.handle = h;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, 'o', w.in.data.length);
+       status = smb2_write(tree1a, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ls2.lease_epoch += 1;
+       CHECK_BREAK_INFO("R", "", LEASE2);
+
+       ZERO_STRUCT(lease_break_info);
+
+       ZERO_STRUCT(w);
+       w.in.file.handle = h3;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, 'o', w.in.data.length);
+       status = smb2_write(tree1b, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ls1.lease_epoch += 1;
+       CHECK_BREAK_INFO("RH", "", LEASE1);
+
+ done:
+       smb2_util_close(tree1a, h);
+       smb2_util_close(tree1b, h2);
+       smb2_util_close(tree1b, h3);
+
+       smb2_util_unlink(tree1a, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+static bool test_lease_v2_complex1(struct torture_context *tctx,
+                                  struct smb2_tree *tree1a)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1;
+       struct smb2_create io2;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_handle h3 = {{0}};
+       struct smb2_write w;
+       NTSTATUS status;
+       const char *fname = "lease_v2_complex1.dat";
+       bool ret = true;
+       uint32_t caps;
+       enum protocol_types protocol;
+       struct smb2_tree *tree1b = NULL;
+       struct smbcli_options options1;
+
+       options1 = tree1a->session->transport->options;
+
+       caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       protocol = smbXcli_conn_protocol(tree1a->session->transport->conn);
+       if (protocol < PROTOCOL_SMB3_00) {
+               torture_skip(tctx, "v2 leases are not supported");
+       }
+
+       tree1a->session->transport->lease.handler = torture_lease_handler;
+       tree1a->session->transport->lease.private_data = tree1a;
+       tree1a->session->transport->oplock.handler = torture_oplock_handler;
+       tree1a->session->transport->oplock.private_data = tree1a;
+
+       /* create a new connection (same client_guid) */
+       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
+               torture_warning(tctx, "couldn't reconnect, bailing\n");
+               ret = false;
+               goto done;
+       }
+
+       tree1b->session->transport->lease.handler = torture_lease_handler;
+       tree1b->session->transport->lease.private_data = tree1b;
+       tree1b->session->transport->oplock.handler = torture_oplock_handler;
+       tree1b->session->transport->oplock.private_data = tree1b;
+
+       smb2_util_unlink(tree1a, fname);
+
+       ZERO_STRUCT(lease_break_info);
+
+       /* Grab R lease over connection 1a */
+       smb2_lease_v2_create(&io1, &ls1, false, fname, LEASE1, NULL,
+                            smb2_util_lease_state("R"), 0x4711);
+       status = smb2_create(tree1a, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       ls1.lease_epoch += 1;
+       CHECK_LEASE_V2(&io1, "R", true, LEASE1,
+                      0, 0, ls1.lease_epoch);
+
+       /* Upgrade to RWH over connection 1b */
+       ls1.lease_state = smb2_util_lease_state("RWH");
+       status = smb2_create(tree1b, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       ls1.lease_epoch += 1;
+       CHECK_LEASE_V2(&io1, "RHW", true, LEASE1,
+                      0, 0, ls1.lease_epoch);
+
+       /* close over connection 1b */
+       status = smb2_util_close(tree1b, h2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /* Contend with LEASE2. */
+       smb2_lease_v2_create(&io2, &ls2, false, fname, LEASE2, NULL,
+                            smb2_util_lease_state("R"), 0x11);
+       status = smb2_create(tree1b, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h3 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       ls2.lease_epoch += 1;
+       CHECK_LEASE_V2(&io2, "R", true, LEASE2,
+                      0, 0, ls2.lease_epoch);
+
+       /* Verify that we were only sent one break. */
+       ls1.lease_epoch += 1;
+       CHECK_BREAK_INFO_V2(tree1a->session->transport,
+                           "RHW", "RH", LEASE1, ls1.lease_epoch);
+
+       /* again RH over connection 1b doesn't change the epoch */
+       ls1.lease_state = smb2_util_lease_state("RH");
+       status = smb2_create(tree1b, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io1.out.file.handle;
+       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io1, "RH", true, LEASE1,
+                      0, 0, ls1.lease_epoch);
+
+       /* close over connection 1b */
+       status = smb2_util_close(tree1b, h2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ZERO_STRUCT(lease_break_info);
+
+       ZERO_STRUCT(w);
+       w.in.file.handle = h;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, 'o', w.in.data.length);
+       status = smb2_write(tree1a, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ls2.lease_epoch += 1;
+       CHECK_BREAK_INFO_V2(tree1a->session->transport,
+                           "R", "", LEASE2, ls2.lease_epoch);
+
+       ZERO_STRUCT(lease_break_info);
+
+       ZERO_STRUCT(w);
+       w.in.file.handle = h3;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, 'o', w.in.data.length);
+       status = smb2_write(tree1b, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       ls1.lease_epoch += 1;
+       CHECK_BREAK_INFO_V2(tree1a->session->transport,
+                           "RH", "", LEASE1, ls1.lease_epoch);
+
+ done:
+       smb2_util_close(tree1a, h);
+       smb2_util_close(tree1b, h2);
+       smb2_util_close(tree1b, h3);
+
+       smb2_util_unlink(tree1a, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+static bool test_lease_v2_complex2(struct torture_context *tctx,
+                                  struct smb2_tree *tree1a)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io1;
+       struct smb2_create io2;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle h2 = {{0}};
+       struct smb2_request *req2 = NULL;
+       struct smb2_lease_break_ack ack = {};
+       NTSTATUS status;
+       const char *fname = "lease_v2_complex2.dat";
+       bool ret = true;
+       uint32_t caps;
+       enum protocol_types protocol;
+       struct smb2_tree *tree1b = NULL;
+       struct smbcli_options options1;
+
+       options1 = tree1a->session->transport->options;
+
+       caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       protocol = smbXcli_conn_protocol(tree1a->session->transport->conn);
+       if (protocol < PROTOCOL_SMB3_00) {
+               torture_skip(tctx, "v2 leases are not supported");
+       }
+
+       tree1a->session->transport->lease.handler = torture_lease_handler;
+       tree1a->session->transport->lease.private_data = tree1a;
+       tree1a->session->transport->oplock.handler = torture_oplock_handler;
+       tree1a->session->transport->oplock.private_data = tree1a;
+
+       /* create a new connection (same client_guid) */
+       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
                torture_warning(tctx, "couldn't reconnect, bailing\n");
                ret = false;
                goto done;
@@ -1928,244 +3349,638 @@ static bool test_lease_complex1(struct torture_context *tctx,
 
        smb2_util_unlink(tree1a, fname);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
-       /* Grab R lease over connection 1a */
-       smb2_lease_create(&io1, &ls1, false, fname, LEASE1, smb2_util_lease_state("R"));
+       /* Grab RWH lease over connection 1a */
+       smb2_lease_v2_create(&io1, &ls1, false, fname, LEASE1, NULL,
+                            smb2_util_lease_state("RWH"), 0x4711);
        status = smb2_create(tree1a, mem_ctx, &io1);
        CHECK_STATUS(status, NT_STATUS_OK);
        h = io1.out.file.handle;
        CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io1, "R", true, LEASE1, 0);
+       ls1.lease_epoch += 1;
+       CHECK_LEASE_V2(&io1, "RWH", true, LEASE1,
+                      0, 0, ls1.lease_epoch);
 
-       /* Upgrade to RWH over connection 1b */
-       ls1.lease_state = smb2_util_lease_state("RWH");
-       status = smb2_create(tree1b, mem_ctx, &io1);
+       /*
+        * we defer acking the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       /* Ask for RWH on connection 1b, different lease. */
+       smb2_lease_v2_create(&io2, &ls2, false, fname, LEASE2, NULL,
+                            smb2_util_lease_state("RWH"), 0x11);
+       req2 = smb2_create_send(tree1b, &io2);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+
+       ls1.lease_epoch += 1;
+
+       CHECK_BREAK_INFO_V2(tree1a->session->transport,
+                           "RWH", "RH", LEASE1, ls1.lease_epoch);
+
+       /* Send the break ACK on tree1b. */
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state = SMB2_LEASE_HANDLE|SMB2_LEASE_READ;
+
+       status = smb2_lease_break_ack(tree1b, &ack);
        CHECK_STATUS(status, NT_STATUS_OK);
-       h2 = io1.out.file.handle;
-       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io1, "RHW", true, LEASE1, 0);
+       CHECK_LEASE_BREAK_ACK(&ack, "RH", LEASE1);
+
+       ZERO_STRUCT(lease_break_info);
+
+       status = smb2_create_recv(req2, tctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io2, "RH", true, LEASE2,
+                      0, 0, ls2.lease_epoch+1);
+       h2 = io2.out.file.handle;
+
+ done:
+       smb2_util_close(tree1a, h);
+       smb2_util_close(tree1b, h2);
+
+       smb2_util_unlink(tree1a, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+
+static bool test_lease_timeout(struct torture_context *tctx,
+                               struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle hnew = {{0}};
+       struct smb2_handle h1b = {{0}};
+       NTSTATUS status;
+       const char *fname = "lease_timeout.dat";
+       bool ret = true;
+       struct smb2_lease_break_ack ack = {};
+       struct smb2_request *req2 = NULL;
+       struct smb2_write w;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       smb2_util_unlink(tree, fname);
+
+       /* Grab a RWH lease. */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       h = io.out.file.handle;
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       /*
+        * Just don't ack the lease break.
+        */
+       ZERO_STRUCT(lease_break_info);
+       lease_break_info.lease_skip_ack = true;
+
+       /* Break with a RWH request. */
+       smb2_lease_create(&io, &ls2, false, fname, LEASE2, smb2_util_lease_state("RWH"));
+       req2 = smb2_create_send(tree, &io);
+       torture_assert(tctx, req2 != NULL, "smb2_create_send");
+       torture_assert(tctx, req2->state == SMB2_REQUEST_RECV, "req2 pending");
+
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+
+       /* Copy the break request. */
+       ack.in.lease.lease_key =
+               lease_break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               lease_break_info.lease_break.new_lease_state;
+
+       /* Now wait for the timeout and get the reply. */
+       status = smb2_create_recv(req2, tctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RH", true, LEASE2, 0);
+       hnew = io.out.file.handle;
+
+       /* Ack the break after the timeout... */
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
+
+       /* Get state of the original handle. */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state(""));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io, "", true, LEASE1, 0);
+       smb2_util_close(tree, io.out.file.handle);
+
+       /* Write on the original handle and make sure it's still valid. */
+       ZERO_STRUCT(lease_break_info);
+       ZERO_STRUCT(w);
+       w.in.file.handle = h;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, '1', w.in.data.length);
+       status = smb2_write(tree, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /* Causes new handle to break to NONE. */
+       CHECK_BREAK_INFO("RH", "", LEASE2);
+
+       /* Write on the new handle. */
+       ZERO_STRUCT(lease_break_info);
+       ZERO_STRUCT(w);
+       w.in.file.handle = hnew;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 1024);
+       memset(w.in.data.data, '2', w.in.data.length);
+       status = smb2_write(tree, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       /* No break - original handle was already NONE. */
+       CHECK_NO_BREAK(tctx);
+       smb2_util_close(tree, hnew);
+
+       /* Upgrade to R on LEASE1. */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("R"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io, "R", true, LEASE1, 0);
+       h1b = io.out.file.handle;
+       smb2_util_close(tree, h1b);
+
+       /* Upgrade to RWH on LEASE1. */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       h1b = io.out.file.handle;
+       smb2_util_close(tree, h1b);
+
+ done:
+       smb2_util_close(tree, h);
+       smb2_util_close(tree, hnew);
+       smb2_util_close(tree, h1b);
+
+       smb2_util_unlink(tree, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+static bool test_lease_v2_rename(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+       struct smb2_handle h = {{0}};
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       union smb_setfileinfo sinfo;
+       const char *fname = "lease_v2_rename_src.dat";
+       const char *fname_dst = "lease_v2_rename_dst.dat";
+       bool ret = true;
+       NTSTATUS status;
+       uint32_t caps;
+       enum protocol_types protocol;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       protocol = smbXcli_conn_protocol(tree->session->transport->conn);
+       if (protocol < PROTOCOL_SMB3_00) {
+               torture_skip(tctx, "v2 leases are not supported");
+       }
 
-       /* close over connection 1b */
-       status = smb2_util_close(tree1b, h2);
-       CHECK_STATUS(status, NT_STATUS_OK);
+       smb2_util_unlink(tree, fname);
+       smb2_util_unlink(tree, fname_dst);
 
-       /* Contend with LEASE2. */
-       smb2_lease_create(&io2, &ls2, false, fname, LEASE2, smb2_util_lease_state("RHW"));
-       status = smb2_create(tree1b, mem_ctx, &io2);
-       CHECK_STATUS(status, NT_STATUS_OK);
-       h3 = io2.out.file.handle;
-       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io2, "RH", true, LEASE2, 0);
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
 
-       /* Verify that we were only sent one break. */
-       CHECK_BREAK_INFO("RHW", "RH", LEASE1);
+       ZERO_STRUCT(lease_break_info);
 
-       /* again RH over connection 1b doesn't change the epoch */
-       ls1.lease_state = smb2_util_lease_state("RH");
-       status = smb2_create(tree1b, mem_ctx, &io1);
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls1, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  0x4711);
+       status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-       h2 = io1.out.file.handle;
-       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io1, "RH", true, LEASE1, 0);
-
-       /* close over connection 1b */
-       status = smb2_util_close(tree1b, h2);
+       h = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       ls1.lease_epoch += 1;
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch);
+
+       /* Now rename - what happens ? */
+        ZERO_STRUCT(sinfo);
+       sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
+       sinfo.rename_information.in.file.handle = h;
+       sinfo.rename_information.in.overwrite = true;
+       sinfo.rename_information.in.new_name = fname_dst;
+       status = smb2_setinfo_file(tree, &sinfo);
        CHECK_STATUS(status, NT_STATUS_OK);
 
-       ZERO_STRUCT(break_info);
+       /* No lease break. */
+       CHECK_NO_BREAK(tctx);
 
-       ZERO_STRUCT(w);
-       w.in.file.handle = h;
-       w.in.offset      = 0;
-       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
-       memset(w.in.data.data, 'o', w.in.data.length);
-       status = smb2_write(tree1a, &w);
+       /* Check we can open another handle on the new name. */
+       smb2_lease_v2_create_share(&io, &ls1, false, fname_dst,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state(""),
+                                  ls1.lease_epoch);
+       status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch);
+       smb2_util_close(tree, h1);
 
+       /* Try another lease key. */
+       smb2_lease_v2_create_share(&io, &ls2, false, fname_dst,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE2, NULL,
+                                  smb2_util_lease_state("RWH"),
+                                  0x44);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
        ls2.lease_epoch += 1;
-       CHECK_BREAK_INFO("RH", "", LEASE2);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0, ls2.lease_epoch );
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RWH", "RH", LEASE1, ls1.lease_epoch + 1);
+       ls1.lease_epoch += 1;
+       ZERO_STRUCT(lease_break_info);
+
+       /* Now rename back. */
+       ZERO_STRUCT(sinfo);
+       sinfo.rename_information.level = RAW_SFILEINFO_RENAME_INFORMATION;
+       sinfo.rename_information.in.file.handle = h;
+       sinfo.rename_information.in.overwrite = true;
+       sinfo.rename_information.in.new_name = fname;
+       status = smb2_setinfo_file(tree, &sinfo);
+       CHECK_STATUS(status, NT_STATUS_OK);
 
-       ZERO_STRUCT(break_info);
+       /* Breaks to R on LEASE2. */
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RH", "R", LEASE2, ls2.lease_epoch + 1);
+       ls2.lease_epoch += 1;
 
-       ZERO_STRUCT(w);
-       w.in.file.handle = h3;
-       w.in.offset      = 0;
-       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
-       memset(w.in.data.data, 'o', w.in.data.length);
-       status = smb2_write(tree1b, &w);
+       /* Check we can open another handle on the current name. */
+       smb2_lease_v2_create_share(&io, &ls1, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state(""),
+                                  ls1.lease_epoch);
+       status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE1, 0, 0, ls1.lease_epoch);
+       smb2_util_close(tree, h1);
 
-       ls1.lease_epoch += 1;
-       CHECK_BREAK_INFO("RH", "", LEASE1);
+done:
 
- done:
-       smb2_util_close(tree1a, h);
-       smb2_util_close(tree1b, h2);
-       smb2_util_close(tree1b, h3);
+       smb2_util_close(tree, h);
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
 
-       smb2_util_unlink(tree1a, fname);
+       smb2_util_unlink(tree, fname);
+       smb2_util_unlink(tree, fname_dst);
 
+       smb2_util_unlink(tree, fname);
        talloc_free(mem_ctx);
-
        return ret;
 }
 
-static bool test_lease_v2_complex1(struct torture_context *tctx,
+
+static bool test_lease_dynamic_share(struct torture_context *tctx,
                                   struct smb2_tree *tree1a)
 {
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
-       struct smb2_create io1;
-       struct smb2_create io2;
+       struct smb2_create io;
        struct smb2_lease ls1;
-       struct smb2_lease ls2;
-       struct smb2_handle h, h2, h3;
+       struct smb2_handle h, h1, h2;
        struct smb2_write w;
        NTSTATUS status;
-       const char *fname = "lease_v2_complex1.dat";
+       const char *fname = "dynamic_path.dat";
        bool ret = true;
        uint32_t caps;
-       enum protocol_types protocol;
-       struct smb2_tree *tree1b = NULL;
-       struct smbcli_options options1;
+       struct smb2_tree *tree_2_1 = NULL;
+       struct smb2_tree *tree_3_0 = NULL;
+       struct smbcli_options options2_1;
+       struct smbcli_options options3_0;
+       const char *orig_share = NULL;
+
+       if (!TARGET_IS_SAMBA3(tctx)) {
+               torture_skip(tctx, "dynamic shares are not supported");
+               return true;
+       }
 
-       options1 = tree1a->session->transport->options;
+       options2_1 = tree1a->session->transport->options;
+       options3_0 = tree1a->session->transport->options;
 
        caps = smb2cli_conn_server_capabilities(tree1a->session->transport->conn);
        if (!(caps & SMB2_CAP_LEASING)) {
                torture_skip(tctx, "leases are not supported");
        }
 
-       protocol = smbXcli_conn_protocol(tree1a->session->transport->conn);
-       if (protocol < PROTOCOL_SMB3_00) {
-               torture_skip(tctx, "v2 leases are not supported");
+       /*
+        * Save off original share name and change it to dynamic_share.
+        * This must have been pre-created with a dynamic path containing
+        * %R.
+        */
+
+       orig_share = lpcfg_parm_string(tctx->lp_ctx, NULL, "torture", "share");
+       orig_share = talloc_strdup(tctx->lp_ctx, orig_share);
+       if (orig_share == NULL) {
+               torture_result(tctx, TORTURE_FAIL, __location__ "no memory\n");
+                ret = false;
+                goto done;
        }
+       lpcfg_set_cmdline(tctx->lp_ctx, "torture:share", "dynamic_share");
 
-       tree1a->session->transport->lease.handler = torture_lease_handler;
-       tree1a->session->transport->lease.private_data = tree1a;
-       tree1a->session->transport->oplock.handler = torture_oplock_handler;
-       tree1a->session->transport->oplock.private_data = tree1a;
+       /* Set max protocol to SMB2.1 */
+       options2_1.max_protocol = PROTOCOL_SMB2_10;
+       /* create a new connection (same client_guid) */
+       if (!torture_smb2_connection_ext(tctx, 0, &options2_1, &tree_2_1)) {
+               torture_result(tctx,  TORTURE_FAIL,
+                       __location__ "couldn't reconnect "
+                       "max protocol 2.1, bailing\n");
+               ret = false;
+               goto done;
+       }
+
+       tree_2_1->session->transport->lease.handler = torture_lease_handler;
+       tree_2_1->session->transport->lease.private_data = tree_2_1;
+       tree_2_1->session->transport->oplock.handler = torture_oplock_handler;
+       tree_2_1->session->transport->oplock.private_data = tree_2_1;
 
+       smb2_util_unlink(tree_2_1, fname);
+
+       /* Set max protocol to SMB3.0 */
+       options3_0.max_protocol = PROTOCOL_SMB3_00;
        /* create a new connection (same client_guid) */
-       if (!torture_smb2_connection_ext(tctx, 0, &options1, &tree1b)) {
-               torture_warning(tctx, "couldn't reconnect, bailing\n");
+       if (!torture_smb2_connection_ext(tctx, 0, &options3_0, &tree_3_0)) {
+               torture_result(tctx,  TORTURE_FAIL,
+                       __location__ "couldn't reconnect "
+                       "max protocol 3.0, bailing\n");
                ret = false;
                goto done;
        }
 
-       tree1b->session->transport->lease.handler = torture_lease_handler;
-       tree1b->session->transport->lease.private_data = tree1b;
-       tree1b->session->transport->oplock.handler = torture_oplock_handler;
-       tree1b->session->transport->oplock.private_data = tree1b;
+       tree_3_0->session->transport->lease.handler = torture_lease_handler;
+       tree_3_0->session->transport->lease.private_data = tree_3_0;
+       tree_3_0->session->transport->oplock.handler = torture_oplock_handler;
+       tree_3_0->session->transport->oplock.private_data = tree_3_0;
 
-       smb2_util_unlink(tree1a, fname);
+       smb2_util_unlink(tree_3_0, fname);
 
-       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(lease_break_info);
 
-       /* Grab R lease over connection 1a */
-       smb2_lease_v2_create(&io1, &ls1, false, fname, LEASE1, NULL,
-                            smb2_util_lease_state("R"), 0x4711);
-       status = smb2_create(tree1a, mem_ctx, &io1);
+       /* Get RWH lease over connection 2_1 */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_2_1, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-       h = io1.out.file.handle;
-       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       ls1.lease_epoch += 1;
-       CHECK_LEASE_V2(&io1, "R", true, LEASE1,
-                      0, 0, ls1.lease_epoch);
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       h = io.out.file.handle;
 
-       /* Upgrade to RWH over connection 1b */
-       ls1.lease_state = smb2_util_lease_state("RWH");
-       status = smb2_create(tree1b, mem_ctx, &io1);
+       /* Write some data into it. */
+       w.in.file.handle = h;
+       w.in.offset      = 0;
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
+       memset(w.in.data.data, '1', w.in.data.length);
+       status = smb2_write(tree_2_1, &w);
        CHECK_STATUS(status, NT_STATUS_OK);
-       h2 = io1.out.file.handle;
-       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       ls1.lease_epoch += 1;
-       CHECK_LEASE_V2(&io1, "RHW", true, LEASE1,
-                      0, 0, ls1.lease_epoch);
 
-       /* close over connection 1b */
-       status = smb2_util_close(tree1b, h2);
+       /* Open the same name over connection 3_0. */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_3_0, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
 
-       /* Contend with LEASE2. */
-       smb2_lease_v2_create(&io2, &ls2, false, fname, LEASE2, NULL,
-                            smb2_util_lease_state("RWH"), 0x11);
-       status = smb2_create(tree1b, mem_ctx, &io2);
-       CHECK_STATUS(status, NT_STATUS_OK);
-       h3 = io2.out.file.handle;
-       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       ls2.lease_epoch += 1;
-       CHECK_LEASE_V2(&io2, "RH", true, LEASE2,
-                      0, 0, ls2.lease_epoch);
+       /* h1 should have replied with NONE. */
+       CHECK_LEASE(&io, "", true, LEASE1, 0);
 
-       /* Verify that we were only sent one break. */
-       ls1.lease_epoch += 1;
-       CHECK_BREAK_INFO_V2(tree1a->session->transport,
-                           "RHW", "RH", LEASE1, ls1.lease_epoch);
+       /* We should have broken h to NONE. */
+       CHECK_BREAK_INFO("RWH", "", LEASE1);
 
-       /* again RH over connection 1b doesn't change the epoch */
-       ls1.lease_state = smb2_util_lease_state("RH");
-       status = smb2_create(tree1b, mem_ctx, &io1);
+       /* Try to upgrade to RWH over connection 2_1 */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_2_1, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-       h2 = io1.out.file.handle;
-       CHECK_CREATED(&io1, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE_V2(&io1, "RH", true, LEASE1,
-                      0, 0, ls1.lease_epoch);
-
-       /* close over connection 1b */
-       status = smb2_util_close(tree1b, h2);
+       h2 = io.out.file.handle;
+       CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
+       CHECK_VAL(io.out.size, 4096);
+       CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
+       /* Should have been denied. */
+       CHECK_LEASE(&io, "", true, LEASE1, 0);
+       smb2_util_close(tree_2_1, h2);
+
+       /* Try to upgrade to RWH over connection 3_0 */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_3_0, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-
-       ZERO_STRUCT(break_info);
-
-       ZERO_STRUCT(w);
-       w.in.file.handle = h;
+       h2 = io.out.file.handle;
+       CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
+       CHECK_VAL(io.out.size, 0);
+       CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
+       /* Should have been denied. */
+       CHECK_LEASE(&io, "", true, LEASE1, 0);
+       smb2_util_close(tree_3_0, h2);
+
+       /* Write some data into it. */
+       w.in.file.handle = h1;
        w.in.offset      = 0;
-       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
-       memset(w.in.data.data, 'o', w.in.data.length);
-       status = smb2_write(tree1a, &w);
+       w.in.data        = data_blob_talloc(mem_ctx, NULL, 1024);
+       memset(w.in.data.data, '2', w.in.data.length);
+       status = smb2_write(tree_3_0, &w);
        CHECK_STATUS(status, NT_STATUS_OK);
 
-       ls2.lease_epoch += 1;
-       CHECK_BREAK_INFO_V2(tree1a->session->transport,
-                           "RH", "", LEASE2, ls2.lease_epoch);
+       /* Close everything.. */
+       smb2_util_close(tree_2_1, h);
+       smb2_util_close(tree_3_0, h1);
 
-       ZERO_STRUCT(break_info);
-
-       ZERO_STRUCT(w);
-       w.in.file.handle = h3;
-       w.in.offset      = 0;
-       w.in.data        = data_blob_talloc(mem_ctx, NULL, 4096);
-       memset(w.in.data.data, 'o', w.in.data.length);
-       status = smb2_write(tree1b, &w);
+       /* And ensure we can get a lease ! */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_2_1, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-
-       ls1.lease_epoch += 1;
-       CHECK_BREAK_INFO_V2(tree1a->session->transport,
-                           "RH", "", LEASE1, ls1.lease_epoch);
+       CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
+       CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       h = io.out.file.handle;
+       /* And the file is the right size. */
+       CHECK_VAL(io.out.size, 4096);                           \
+       /* Close it. */
+       smb2_util_close(tree_2_1, h);
+
+       /* And ensure we can get a lease ! */
+       smb2_lease_create(&io, &ls1, false, fname, LEASE1, smb2_util_lease_state("RWH"));
+       status = smb2_create(tree_3_0, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
+       CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+       h = io.out.file.handle;
+       /* And the file is the right size. */
+       CHECK_VAL(io.out.size, 1024);                           \
+       /* Close it. */
+       smb2_util_close(tree_3_0, h);
 
  done:
-       smb2_util_close(tree1a, h);
-       smb2_util_close(tree1b, h2);
-       smb2_util_close(tree1b, h3);
 
-       smb2_util_unlink(tree1a, fname);
+       if (tree_2_1 != NULL) {
+               smb2_util_close(tree_2_1, h);
+               smb2_util_unlink(tree_2_1, fname);
+       }
+       if (tree_3_0 != NULL) {
+               smb2_util_close(tree_3_0, h1);
+               smb2_util_close(tree_3_0, h2);
+
+               smb2_util_unlink(tree_3_0, fname);
+       }
+
+       /* Set sharename back. */
+       lpcfg_set_cmdline(tctx->lp_ctx, "torture:share", orig_share);
 
        talloc_free(mem_ctx);
 
        return ret;
 }
 
-struct torture_suite *torture_smb2_lease_init(void)
+/*
+ * Test identifies a bug where the Samba server will not trigger a lease break
+ * for a handle caching lease held by a client when the underlying file is
+ * deleted.
+ * Test:
+ *     Connect session2.
+ *     open file in session1
+ *             session1 should have RWH lease.
+ *     open file in session2
+ *             lease break sent to session1 to downgrade lease to RH
+ *     close file in session 2
+ *     unlink file in session 2
+ *             lease break sent to session1 to downgrade lease to R
+ *     Cleanup
+ */
+static bool test_lease_unlink(struct torture_context *tctx,
+                             struct smb2_tree *tree1)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       NTSTATUS status;
+       bool ret = true;
+       struct smbcli_options transport2_options;
+       struct smb2_tree *tree2 = NULL;
+       struct smb2_transport *transport1 = tree1->session->transport;
+       struct smb2_transport *transport2;
+       struct smb2_handle h1 = {{ 0 }};
+       struct smb2_handle h2 = {{ 0 }};
+       const char *fname = "lease_unlink.dat";
+       uint32_t caps;
+       struct smb2_create io1;
+       struct smb2_create io2;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
+
+       caps = smb2cli_conn_server_capabilities(
+                       tree1->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       /* Connect 2nd connection */
+       transport2_options = transport1->options;
+       transport2_options.client_guid = GUID_random();
+       if (!torture_smb2_connection_ext(tctx, 0, &transport2_options, &tree2)) {
+               torture_warning(tctx, "couldn't reconnect, bailing\n");
+               return false;
+       }
+       transport2 = tree2->session->transport;
+
+       /* Set lease handlers */
+       transport1->lease.handler = torture_lease_handler;
+       transport1->lease.private_data = tree1;
+       transport2->lease.handler = torture_lease_handler;
+       transport2->lease.private_data = tree2;
+
+
+       smb2_lease_create(&io1, &ls1, false, fname, LEASE1,
+                               smb2_util_lease_state("RHW"));
+       smb2_lease_create(&io2, &ls2, false, fname, LEASE2,
+                               smb2_util_lease_state("RHW"));
+
+       smb2_util_unlink(tree1, fname);
+
+       torture_comment(tctx, "Client opens fname with session 1\n");
+       torture_reset_lease_break_info(tctx, &lease_break_info);
+       status = smb2_create(tree1, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h1 = io1.out.file.handle;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io1, "RHW", true, LEASE1, 0);
+       CHECK_VAL(lease_break_info.count, 0);
+
+       torture_comment(tctx, "Client opens fname with session 2\n");
+       torture_reset_lease_break_info(tctx, &lease_break_info);
+       status = smb2_create(tree2, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h2 = io2.out.file.handle;
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io2, "RH", true, LEASE2, 0);
+       CHECK_VAL(lease_break_info.count, 1);
+       CHECK_BREAK_INFO("RHW", "RH", LEASE1);
+
+       torture_comment(tctx,
+               "Client closes and then unlinks fname with session 2\n");
+       torture_reset_lease_break_info(tctx, &lease_break_info);
+       smb2_util_close(tree2, h2);
+       smb2_util_unlink(tree2, fname);
+       CHECK_VAL(lease_break_info.count, 1);
+       CHECK_BREAK_INFO("RH", "R", LEASE1);
+
+done:
+       smb2_util_close(tree1, h1);
+       smb2_util_close(tree2, h2);
+       smb2_util_unlink(tree1, fname);
+
+       return ret;
+}
+
+struct torture_suite *torture_smb2_lease_init(TALLOC_CTX *ctx)
 {
        struct torture_suite *suite =
-           torture_suite_create(talloc_autofree_context(), "lease");
+           torture_suite_create(ctx, "lease");
 
        torture_suite_add_1smb2_test(suite, "request", test_lease_request);
        torture_suite_add_1smb2_test(suite, "break_twice",
                                     test_lease_break_twice);
        torture_suite_add_1smb2_test(suite, "nobreakself",
                                     test_lease_nobreakself);
+       torture_suite_add_1smb2_test(suite, "statopen", test_lease_statopen);
+       torture_suite_add_1smb2_test(suite, "statopen2", test_lease_statopen2);
+       torture_suite_add_1smb2_test(suite, "statopen3", test_lease_statopen3);
        torture_suite_add_1smb2_test(suite, "upgrade", test_lease_upgrade);
        torture_suite_add_1smb2_test(suite, "upgrade2", test_lease_upgrade2);
        torture_suite_add_1smb2_test(suite, "upgrade3", test_lease_upgrade3);
@@ -2173,6 +3988,13 @@ struct torture_suite *torture_smb2_lease_init(void)
        torture_suite_add_1smb2_test(suite, "oplock", test_lease_oplock);
        torture_suite_add_1smb2_test(suite, "multibreak", test_lease_multibreak);
        torture_suite_add_1smb2_test(suite, "breaking1", test_lease_breaking1);
+       torture_suite_add_1smb2_test(suite, "breaking2", test_lease_breaking2);
+       torture_suite_add_1smb2_test(suite, "breaking3", test_lease_breaking3);
+       torture_suite_add_1smb2_test(suite, "v2_breaking3", test_lease_v2_breaking3);
+       torture_suite_add_1smb2_test(suite, "breaking4", test_lease_breaking4);
+       torture_suite_add_1smb2_test(suite, "breaking5", test_lease_breaking5);
+       torture_suite_add_1smb2_test(suite, "breaking6", test_lease_breaking6);
+       torture_suite_add_2smb2_test(suite, "lock1", test_lease_lock1);
        torture_suite_add_1smb2_test(suite, "complex1", test_lease_complex1);
        torture_suite_add_1smb2_test(suite, "v2_request_parent",
                                     test_lease_v2_request_parent);
@@ -2181,6 +4003,11 @@ struct torture_suite *torture_smb2_lease_init(void)
        torture_suite_add_1smb2_test(suite, "v2_epoch2", test_lease_v2_epoch2);
        torture_suite_add_1smb2_test(suite, "v2_epoch3", test_lease_v2_epoch3);
        torture_suite_add_1smb2_test(suite, "v2_complex1", test_lease_v2_complex1);
+       torture_suite_add_1smb2_test(suite, "v2_complex2", test_lease_v2_complex2);
+       torture_suite_add_1smb2_test(suite, "v2_rename", test_lease_v2_rename);
+       torture_suite_add_1smb2_test(suite, "dynamic_share", test_lease_dynamic_share);
+       torture_suite_add_1smb2_test(suite, "timeout", test_lease_timeout);
+       torture_suite_add_1smb2_test(suite, "unlink", test_lease_unlink);
 
        suite->description = talloc_strdup(suite, "SMB2-LEASE tests");