s4:torture/smb2: smb2.lease.breaking6 test
[obnox/samba/samba-obnox.git] / source4 / torture / smb2 / lease.c
index 7557640da54519fffa1c1f1701dd1ab3209f98f9..5e788e10d490fc8471b37875f6a475a959c73f0f 100644 (file)
@@ -25,6 +25,7 @@
 #include "libcli/smb2/smb2_calls.h"
 #include "torture/torture.h"
 #include "torture/smb2/proto.h"
+#include "torture/util.h"
 #include "libcli/smb/smbXcli_base.h"
 
 #define CHECK_VAL(v, correct) do { \
@@ -51,8 +52,9 @@
                CHECK_VAL((__io)->out.reserved2, 0);                    \
        } while(0)
 
-#define CHECK_LEASE(__io, __state, __oplevel, __key)                   \
+#define CHECK_LEASE(__io, __state, __oplevel, __key, __flags)          \
        do {                                                            \
+               CHECK_VAL((__io)->out.lease_response.lease_version, 1); \
                if (__oplevel) {                                        \
                        CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE); \
                        CHECK_VAL((__io)->out.lease_response.lease_key.data[0], (__key)); \
                        CHECK_VAL((__io)->out.lease_response.lease_state, 0); \
                }                                                       \
                                                                        \
-               CHECK_VAL((__io)->out.lease_response.lease_flags, 0);   \
+               CHECK_VAL((__io)->out.lease_response.lease_flags, (__flags));   \
                CHECK_VAL((__io)->out.lease_response.lease_duration, 0); \
+               CHECK_VAL((__io)->out.lease_response.lease_epoch, 0); \
        } while(0)
 
-#define CHECK_LEASE_V2(__io, __state, __oplevel, __key, __flags, __parent) \
+#define CHECK_LEASE_V2(__io, __state, __oplevel, __key, __flags, __parent, __epoch) \
        do {                                                            \
+               CHECK_VAL((__io)->out.lease_response_v2.lease_version, 2); \
                if (__oplevel) {                                        \
                        CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE); \
                        CHECK_VAL((__io)->out.lease_response_v2.lease_key.data[0], (__key)); \
@@ -89,6 +93,7 @@
                        CHECK_VAL((__io)->out.lease_response_v2.parent_lease_key.data[1], ~(__parent)); \
                } \
                CHECK_VAL((__io)->out.lease_response_v2.lease_duration, 0); \
+               CHECK_VAL((__io)->out.lease_response_v2.lease_epoch, (__epoch)); \
        } while(0)
 
 static const uint64_t LEASE1 = 0xBADC0FFEE0DDF00Dull;
@@ -116,10 +121,10 @@ static bool test_lease_request(struct torture_context *tctx,
        struct smb2_lease ls;
        struct smb2_handle h1, h2;
        NTSTATUS status;
-       const char *fname = "lease.dat";
-       const char *fname2 = "lease2.dat";
-       const char *sname = "lease.dat:stream";
-       const char *dname = "lease.dir";
+       const char *fname = "lease_request.dat";
+       const char *fname2 = "lease_request.2.dat";
+       const char *sname = "lease_request.dat:stream";
+       const char *dname = "lease_request.dir";
        bool ret = true;
        int i;
        uint32_t caps;
@@ -139,15 +144,17 @@ static bool test_lease_request(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
        h1 = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RHW", true, LEASE1);
+       CHECK_LEASE(&io, "RHW", true, LEASE1, 0);
 
        /* But will reject leases on directories. */
-       smb2_lease_create(&io, &ls, true, dname, LEASE2, smb2_util_lease_state("RHW"));
-       status = smb2_create(tree, mem_ctx, &io);
-       CHECK_STATUS(status, NT_STATUS_OK);
-       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_DIRECTORY);
-       CHECK_LEASE(&io, "", false, 0);
-       smb2_util_close(tree, io.out.file.handle);
+       if (!(caps & SMB2_CAP_DIRECTORY_LEASING)) {
+               smb2_lease_create(&io, &ls, true, dname, LEASE2, smb2_util_lease_state("RHW"));
+               status = smb2_create(tree, mem_ctx, &io);
+               CHECK_STATUS(status, NT_STATUS_OK);
+               CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_DIRECTORY);
+               CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+               smb2_util_close(tree, io.out.file.handle);
+       }
 
        /* Also rejects multiple files leased under the same key. */
        smb2_lease_create(&io, &ls, true, fname2, LEASE1, smb2_util_lease_state("RHW"));
@@ -160,7 +167,7 @@ static bool test_lease_request(struct torture_context *tctx,
        h2 = io.out.file.handle;
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RHW", true, LEASE2);
+       CHECK_LEASE(&io, "RHW", true, LEASE2, 0);
        smb2_util_close(tree, h2);
 
        smb2_util_close(tree, h1);
@@ -177,7 +184,7 @@ static bool test_lease_request(struct torture_context *tctx,
                h2 = io.out.file.handle;
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, request_results[i][1], true, LEASE1);
+               CHECK_LEASE(&io, request_results[i][1], true, LEASE1, 0);
                smb2_util_close(tree, io.out.file.handle);
        }
 
@@ -202,7 +209,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        struct smb2_lease ls;
        struct smb2_handle h, hnew;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_upgrade.dat";
        bool ret = true;
        uint32_t caps;
 
@@ -218,7 +225,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RH", true, LEASE1);
+       CHECK_LEASE(&io, "RH", true, LEASE1, 0);
        h = io.out.file.handle;
 
        /* Upgrades (sidegrades?) to RW leave us with an RH. */
@@ -226,7 +233,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RH", true, LEASE1);
+       CHECK_LEASE(&io, "RH", true, LEASE1, 0);
        hnew = io.out.file.handle;
 
        smb2_util_close(tree, hnew);
@@ -236,7 +243,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RHW", true, LEASE1);
+       CHECK_LEASE(&io, "RHW", true, LEASE1, 0);
        hnew = io.out.file.handle;
 
        smb2_util_close(tree, h);
@@ -247,7 +254,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RHW", true, LEASE1);
+       CHECK_LEASE(&io, "RHW", true, LEASE1, 0);
        hnew = io.out.file.handle;
 
        smb2_util_close(tree, hnew);
@@ -326,7 +333,7 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
        NTSTATUS status;
        struct smb2_create io;
        struct smb2_lease ls;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_upgrade2.dat";
        bool ret = true;
        int i;
        uint32_t caps;
@@ -346,7 +353,7 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, t.initial, true, LEASE1);
+               CHECK_LEASE(&io, t.initial, true, LEASE1, 0);
                h = io.out.file.handle;
 
                /* Upgrade. */
@@ -354,7 +361,7 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, t.expected, true, LEASE1);
+               CHECK_LEASE(&io, t.expected, true, LEASE1, 0);
                hnew = io.out.file.handle;
 
                smb2_util_close(tree, hnew);
@@ -375,10 +382,18 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
 
 #define CHECK_LEASE_BREAK(__lb, __oldstate, __state, __key)            \
        do {                                                            \
-               CHECK_VAL((__lb)->new_lease_state, smb2_util_lease_state(__state));     \
-               CHECK_VAL((__lb)->current_lease.lease_state, smb2_util_lease_state(__oldstate)); \
+               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)                   \
@@ -391,8 +406,10 @@ static bool test_lease_upgrade2(struct torture_context *tctx,
                CHECK_VAL((__lba)->out.lease.lease_duration, 0);        \
        } while(0)
 
-static struct {
+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;
@@ -404,19 +421,58 @@ static struct {
        int oplock_failures;
 } break_info;
 
-#define CHECK_BREAK_INFO(__oldstate, __state, __key)                   \
+#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_break.break_flags &                \
-                   SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED) {        \
+               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;
@@ -437,9 +493,14 @@ static bool torture_lease_handler(struct smb2_transport *transport,
        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;
@@ -522,7 +583,7 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
        NTSTATUS status;
        struct smb2_create io;
        struct smb2_lease ls;
-       const char *fname = "upgrade3.dat";
+       const char *fname = "lease_upgrade3.dat";
        bool ret = true;
        int i;
        uint32_t caps;
@@ -549,7 +610,7 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, t.held1, true, LEASE1);
+               CHECK_LEASE(&io, t.held1, true, LEASE1, 0);
                h = io.out.file.handle;
 
                /* grab second lease */
@@ -557,7 +618,7 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, t.held2, true, LEASE2);
+               CHECK_LEASE(&io, t.held2, true, LEASE2, 0);
                h2 = io.out.file.handle;
 
                /* no break has happened */
@@ -569,7 +630,7 @@ static bool test_lease_upgrade3(struct torture_context *tctx,
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, t.upgraded_to, true, LEASE1);
+               CHECK_LEASE(&io, t.upgraded_to, true, LEASE1, 0);
                hnew = io.out.file.handle;
 
                /* no break has happened */
@@ -688,7 +749,7 @@ static bool test_lease_break(struct torture_context *tctx,
        struct smb2_lease ls;
        struct smb2_handle h, h2, h3;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_break.dat";
        bool ret = true;
        int i;
        uint32_t caps;
@@ -721,7 +782,7 @@ static bool test_lease_break(struct torture_context *tctx,
                CHECK_STATUS(status, NT_STATUS_OK);
                h = io.out.file.handle;
                CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, held, true, LEASE1);
+               CHECK_LEASE(&io, held, true, LEASE1, 0);
 
                /* Possibly contend lease. */
                smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state(contend));
@@ -729,13 +790,12 @@ static bool test_lease_break(struct torture_context *tctx,
                CHECK_STATUS(status, NT_STATUS_OK);
                h2 = io.out.file.handle;
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, granted, true, LEASE2);
+               CHECK_LEASE(&io, granted, true, LEASE2, 0);
 
                if (smb2_util_lease_state(held) != smb2_util_lease_state(brokento)) {
                        CHECK_BREAK_INFO(held, brokento, LEASE1);
                } else {
-                       CHECK_VAL(break_info.count, 0);
-                       CHECK_VAL(break_info.failures, 0);
+                       CHECK_NO_BREAK(tctx);
                }
 
                ZERO_STRUCT(break_info);
@@ -749,7 +809,7 @@ static bool test_lease_break(struct torture_context *tctx,
                CHECK_STATUS(status, NT_STATUS_OK);
                h3 = io.out.file.handle;
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, brokento, true, LEASE1);
+               CHECK_LEASE(&io, brokento, true, LEASE1, 0);
                CHECK_VAL(break_info.count, 0);
                CHECK_VAL(break_info.failures, 0);
 
@@ -780,7 +840,7 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
        struct smb2_lease ls;
        struct smb2_handle h1, h2;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_nobreakself.dat";
        bool ret = true;
        uint32_t caps;
        char c = 0;
@@ -800,14 +860,14 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
        h1 = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "R", true, LEASE1);
+       CHECK_LEASE(&io, "R", true, LEASE1, 0);
 
        smb2_lease_create(&io, &ls, false, fname, LEASE2,
                          smb2_util_lease_state("R"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h2 = io.out.file.handle;
-       CHECK_LEASE(&io, "R", true, LEASE2);
+       CHECK_LEASE(&io, "R", true, LEASE2, 0);
 
        ZERO_STRUCT(break_info);
 
@@ -817,9 +877,7 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
        /* Make sure we don't break ourselves on write */
 
        status = smb2_util_write(tree, h1, &c, 0, 1);
-       torture_wait_for_lease_break(tctx);
-       CHECK_VAL(break_info.count, 1);
-       CHECK_VAL(break_info.failures, 0);
+       CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_BREAK_INFO("R", "", LEASE2);
 
        /* Try the other way round. First, upgrade LEASE2 to R again */
@@ -828,25 +886,21 @@ static bool test_lease_nobreakself(struct torture_context *tctx,
                          smb2_util_lease_state("R"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-       CHECK_LEASE(&io, "R", true, LEASE2);
+       CHECK_LEASE(&io, "R", true, LEASE2, 0);
        smb2_util_close(tree, io.out.file.handle);
 
        /* Now break LEASE1 via h2 */
 
        ZERO_STRUCT(break_info);
        status = smb2_util_write(tree, h2, &c, 0, 1);
-       torture_wait_for_lease_break(tctx);
-       CHECK_VAL(break_info.count, 1);
-       CHECK_VAL(break_info.failures, 0);
+       CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_BREAK_INFO("R", "", LEASE1);
 
        /* .. and break LEASE2 via h1 */
 
        ZERO_STRUCT(break_info);
        status = smb2_util_write(tree, h1, &c, 0, 1);
-       torture_wait_for_lease_break(tctx);
-       CHECK_VAL(break_info.count, 1);
-       CHECK_VAL(break_info.failures, 0);
+       CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_BREAK_INFO("R", "", LEASE2);
 
 done:
@@ -941,7 +995,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
        struct smb2_lease ls;
        struct smb2_handle h, h2;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_oplock.dat";
        bool ret = true;
        int i;
        uint32_t caps;
@@ -976,7 +1030,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                CHECK_STATUS(status, NT_STATUS_OK);
                h = io.out.file.handle;
                CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, held, true, LEASE1);
+               CHECK_LEASE(&io, held, true, LEASE1, 0);
 
                /* Does an oplock contend the lease? */
                smb2_oplock_create(&io, fname, smb2_util_oplock_level(contend));
@@ -990,8 +1044,7 @@ static bool test_lease_oplock(struct torture_context *tctx,
                if (smb2_util_lease_state(held) != smb2_util_lease_state(brokento)) {
                        CHECK_BREAK_INFO(held, brokento, LEASE1);
                } else {
-                       CHECK_VAL(break_info.count, 0);
-                       CHECK_VAL(break_info.failures, 0);
+                       CHECK_NO_BREAK(tctx);
                }
 
                smb2_util_close(tree, h);
@@ -1028,16 +1081,12 @@ static bool test_lease_oplock(struct torture_context *tctx,
                CHECK_STATUS(status, NT_STATUS_OK);
                h2 = io.out.file.handle;
                CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-               CHECK_LEASE(&io, granted, true, LEASE1);
+               CHECK_LEASE(&io, granted, true, LEASE1, 0);
 
                if (smb2_util_oplock_level(held) != smb2_util_oplock_level(brokento)) {
-                       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;
+                       CHECK_OPLOCK_BREAK(brokento);
                } else {
-                       CHECK_VAL(break_info.oplock_count, 0);
-                       CHECK_VAL(break_info.oplock_failures, 0);
+                       CHECK_NO_BREAK(tctx);
                }
 
                smb2_util_close(tree, h);
@@ -1067,7 +1116,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        struct smb2_handle h, h2, h3;
        struct smb2_write w;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_multibreak.dat";
        bool ret = true;
        uint32_t caps;
 
@@ -1091,14 +1140,14 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
        h = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RH", true, LEASE1);
+       CHECK_LEASE(&io, "RH", true, LEASE1, 0);
 
        smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RHW"));
        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);
-       CHECK_LEASE(&io, "RHW", true, LEASE1);
+       CHECK_LEASE(&io, "RHW", true, LEASE1, 0);
 
        /* Contend with LEASE2. */
        smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state("RHW"));
@@ -1106,7 +1155,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
        h3 = io.out.file.handle;
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "RH", true, LEASE2);
+       CHECK_LEASE(&io, "RH", true, LEASE2, 0);
 
        /* Verify that we were only sent one break. */
        CHECK_BREAK_INFO("RHW", "RH", LEASE1);
@@ -1127,7 +1176,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
        h = io.out.file.handle;
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE(&io, "R", true, LEASE1);
+       CHECK_LEASE(&io, "R", true, LEASE1, 0);
 
        /* Grab a level-II oplock. */
        smb2_oplock_create(&io, fname, smb2_util_oplock_level("s"));
@@ -1139,8 +1188,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        break_info.held_oplock_level = io.out.oplock_level;
 
        /* Verify no breaks. */
-       CHECK_VAL(break_info.count, 0);
-       CHECK_VAL(break_info.failures, 0);
+       CHECK_NO_BREAK(tctx);
 
        /* Open for truncate, force a break. */
        smb2_generic_create(&io, NULL, false, fname,
@@ -1163,9 +1211,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_STATUS(status, NT_STATUS_OK);
 
        /* Verify one oplock break, one lease break. */
-       CHECK_VAL(break_info.oplock_count, 1);
-       CHECK_VAL(break_info.oplock_failures, 0);
-       CHECK_VAL(break_info.oplock_level, smb2_util_oplock_level(""));
+       CHECK_OPLOCK_BREAK("");
        CHECK_BREAK_INFO("R", "", LEASE1);
 
  done:
@@ -1189,8 +1235,23 @@ static bool test_lease_v2_request_parent(struct torture_context *tctx,
        struct smb2_handle h1;
        uint64_t parent = LEASE2;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_v2_request_parent.dat";
        bool ret = true;
+       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");
+       }
+       if (!(caps & SMB2_CAP_DIRECTORY_LEASING)) {
+               torture_skip(tctx, "directory 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);
 
@@ -1201,14 +1262,15 @@ static bool test_lease_v2_request_parent(struct torture_context *tctx,
                                   smb2_util_share_access("RWD"),
                                   LEASE1, &parent,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x11);
 
        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_V2(&io, "RHW", true, LEASE1,
-                      SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET, LEASE2);
+                      SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET, LEASE2,
+                      ls.lease_epoch + 1);
 
  done:
        smb2_util_close(tree, h1);
@@ -1224,12 +1286,14 @@ static bool test_lease_break_twice(struct torture_context *tctx,
 {
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
-       struct smb2_lease ls;
+       struct smb2_lease ls1;
+       struct smb2_lease ls2;
        struct smb2_handle h1;
        NTSTATUS status;
-       const char *fname = "lease.dat";
+       const char *fname = "lease_break_twice.dat";
        bool ret = true;
        uint32_t caps;
+       enum protocol_types protocol;
 
        caps = smb2cli_conn_server_capabilities(
                tree->session->transport->conn);
@@ -1237,21 +1301,25 @@ static bool test_lease_break_twice(struct torture_context *tctx,
                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);
 
        ZERO_STRUCT(break_info);
        ZERO_STRUCT(io);
-       ZERO_STRUCT(ls);
 
        smb2_lease_v2_create_share(
-               &io, &ls, false, fname, smb2_util_share_access("RWD"),
-               LEASE1, NULL, smb2_util_lease_state("RWH"), 0);
+               &io, &ls1, false, fname, smb2_util_share_access("RWD"),
+               LEASE1, NULL, smb2_util_lease_state("RWH"), 0x11);
 
        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_V2(&io, "RHW", true, LEASE1, 0, 0);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch + 1);
 
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
@@ -1259,22 +1327,25 @@ static bool test_lease_break_twice(struct torture_context *tctx,
        ZERO_STRUCT(break_info);
 
        smb2_lease_v2_create_share(
-               &io, &ls, false, fname, smb2_util_share_access("R"),
-               LEASE2, NULL, smb2_util_lease_state("RWH"), 0);
+               &io, &ls2, false, fname, smb2_util_share_access("R"),
+               LEASE2, NULL, smb2_util_lease_state("RWH"), 0x22);
 
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_SHARING_VIOLATION);
-       CHECK_BREAK_INFO("RWH", "RW", LEASE1);
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RWH", "RW", LEASE1, ls1.lease_epoch + 2);
 
        smb2_lease_v2_create_share(
-               &io, &ls, false, fname, smb2_util_share_access("RWD"),
-               LEASE2, NULL, smb2_util_lease_state("RWH"), 0);
+               &io, &ls2, false, fname, smb2_util_share_access("RWD"),
+               LEASE2, NULL, smb2_util_lease_state("RWH"), 0x22);
 
        ZERO_STRUCT(break_info);
 
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
-       CHECK_BREAK_INFO("RW", "R", LEASE1);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0, ls2.lease_epoch + 1);
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RW", "R", LEASE1, ls1.lease_epoch + 3);
 
 done:
        smb2_util_close(tree, h1);
@@ -1288,15 +1359,30 @@ static bool test_lease_v2_request(struct torture_context *tctx,
 {
        TALLOC_CTX *mem_ctx = talloc_new(tctx);
        struct smb2_create io;
-       struct smb2_lease ls;
+       struct smb2_lease ls1, ls2, ls2t, ls3, ls4;
        struct smb2_handle h1, h2, h3, h4, h5;
        struct smb2_write w;
        NTSTATUS status;
-       const char *fname = "lease.dat";
-       const char *dname = "lease.dir";
-       const char *dnamefname = "lease.dir\\lease.dat";
-       const char *dnamefname2 = "lease.dir\\lease2.dat";
+       const char *fname = "lease_v2_request.dat";
+       const char *dname = "lease_v2_request.dir";
+       const char *dnamefname = "lease_v2_request.dir\\lease.dat";
+       const char *dnamefname2 = "lease_v2_request.dir\\lease2.dat";
        bool ret = true;
+       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");
+       }
+       if (!(caps & SMB2_CAP_DIRECTORY_LEASING)) {
+               torture_skip(tctx, "directory 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);
        smb2_deltree(tree, dname);
@@ -1309,78 +1395,75 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        ZERO_STRUCT(break_info);
 
        ZERO_STRUCT(io);
-       smb2_lease_v2_create_share(&io, &ls, false, fname,
+       smb2_lease_v2_create_share(&io, &ls1, false, fname,
                                   smb2_util_share_access("RWD"),
                                   LEASE1, NULL,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x11);
 
        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_V2(&io, "RHW", true, LEASE1, 0, 0);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch + 1);
 
        ZERO_STRUCT(io);
-       smb2_lease_v2_create_share(&io, &ls, true, dname,
+       smb2_lease_v2_create_share(&io, &ls2, true, dname,
                                   smb2_util_share_access("RWD"),
                                   LEASE2, NULL,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x22);
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h2 = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_DIRECTORY);
-       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0, ls2.lease_epoch + 1);
 
        ZERO_STRUCT(io);
-       smb2_lease_v2_create_share(&io, &ls, false, dnamefname,
+       smb2_lease_v2_create_share(&io, &ls3, false, dnamefname,
                                   smb2_util_share_access("RWD"),
                                   LEASE3, &LEASE2,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x33);
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h3 = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
        CHECK_LEASE_V2(&io, "RHW", true, LEASE3,
-                      SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET, LEASE2);
+                      SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET, LEASE2,
+                      ls3.lease_epoch + 1);
 
-       torture_wait_for_lease_break(tctx);
-       CHECK_VAL(break_info.count, 0);
-       CHECK_VAL(break_info.failures, 0);
+       CHECK_NO_BREAK(tctx);
 
        ZERO_STRUCT(io);
-       smb2_lease_v2_create_share(&io, &ls, false, dnamefname2,
+       smb2_lease_v2_create_share(&io, &ls4, false, dnamefname2,
                                   smb2_util_share_access("RWD"),
                                   LEASE4, NULL,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x44);
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h4 = io.out.file.handle;
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_LEASE_V2(&io, "RHW", true, LEASE4, 0, 0);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE4, 0, 0, ls4.lease_epoch + 1);
 
-       torture_wait_for_lease_break(tctx);
-       torture_wait_for_lease_break(tctx);
-       CHECK_BREAK_INFO("RH", "", LEASE2);
-       torture_wait_for_lease_break(tctx);
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RH", "", LEASE2, ls2.lease_epoch + 2);
 
        ZERO_STRUCT(break_info);
 
        ZERO_STRUCT(io);
-       smb2_lease_v2_create_share(&io, &ls, true, dname,
+       smb2_lease_v2_create_share(&io, &ls2t, true, dname,
                                   smb2_util_share_access("RWD"),
                                   LEASE2, NULL,
                                   smb2_util_lease_state("RHW"),
-                                  0);
+                                  0x222);
        io.in.create_disposition = NTCREATEX_DISP_OPEN;
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h5 = io.out.file.handle;
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_DIRECTORY);
-       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0, 0, ls2.lease_epoch+3);
        smb2_util_close(tree, h5);
 
        ZERO_STRUCT(w);
@@ -1391,16 +1474,21 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        status = smb2_write(tree, &w);
        CHECK_STATUS(status, NT_STATUS_OK);
 
-       smb_msleep(2000);
-       torture_wait_for_lease_break(tctx);
-       CHECK_VAL(break_info.count, 0);
-       CHECK_VAL(break_info.failures, 0);
-
+       /*
+        * Wait 4 seconds in order to check if the write time
+        * was updated (after 2 seconds).
+        */
+       smb_msleep(4000);
+       CHECK_NO_BREAK(tctx);
+
+       /*
+        * only the close on the modified file break the
+        * directory lease.
+        */
        smb2_util_close(tree, h4);
-       torture_wait_for_lease_break(tctx);
-       torture_wait_for_lease_break(tctx);
-       CHECK_BREAK_INFO("RH", "", LEASE2);
-       torture_wait_for_lease_break(tctx);
+
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RH", "", LEASE2, ls2.lease_epoch+4);
 
  done:
        smb2_util_close(tree, h1);
@@ -1417,6 +1505,1367 @@ static bool test_lease_v2_request(struct torture_context *tctx,
        return ret;
 }
 
+static bool test_lease_v2_epoch1(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 h;
+       const char *fname = "lease_v2_epoch1.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;
+
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls, 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);
+       h = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls.lease_epoch + 1);
+       smb2_util_close(tree, h);
+       smb2_util_unlink(tree, fname);
+
+       smb2_lease_v2_create_share(&io, &ls, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  0x11);
+
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       h = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RWH", true, LEASE1, 0, 0, ls.lease_epoch + 1);
+       smb2_util_close(tree, h);
+
+done:
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_v2_epoch2(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls1v2, ls1v2t, ls1v1;
+       struct smb2_handle hv2 = {}, hv1 = {};
+       const char *fname = "lease_v2_epoch2.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;
+
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls1v2, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("R"),
+                                  0x4711);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv2 = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "R", true, LEASE1, 0, 0, ls1v2.lease_epoch + 1);
+
+       ZERO_STRUCT(io);
+       smb2_lease_create_share(&io, &ls1v1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RH", true, LEASE1, 0, 0, ls1v2.lease_epoch + 2);
+
+       smb2_util_close(tree, hv2);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls1v2t, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  0x11);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv2 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1v2.lease_epoch + 3);
+
+       smb2_util_close(tree, hv2);
+
+       smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_NONE);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv2 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RWH", "RH", LEASE1, ls1v2.lease_epoch + 4);
+
+       smb2_util_close(tree, hv2);
+       smb2_util_close(tree, hv1);
+
+       ZERO_STRUCT(io);
+       smb2_lease_create_share(&io, &ls1v1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RHW"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RHW", true, LEASE1, 0);
+
+       smb2_util_close(tree, hv1);
+
+done:
+       smb2_util_close(tree, hv2);
+       smb2_util_close(tree, hv1);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_v2_epoch3(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_create io;
+       struct smb2_lease ls1v1 = {}, ls1v1t = {},ls1v2 = {};
+       struct smb2_handle hv1 = {}, hv2 = {};
+       const char *fname = "lease_v2_epoch3.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;
+
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(io);
+       smb2_lease_create_share(&io, &ls1v1, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("R"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv1 = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "R", true, LEASE1, 0);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls1v2, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RW"),
+                                  0x4711);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv2 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RW", true, LEASE1, 0);
+
+       smb2_util_close(tree, hv1);
+
+       ZERO_STRUCT(io);
+       smb2_lease_create_share(&io, &ls1v1t, false, fname,
+                               smb2_util_share_access("RWD"),
+                               LEASE1,
+                               smb2_util_lease_state("RWH"));
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE(&io, "RWH", true, LEASE1, 0);
+
+       smb2_util_close(tree, hv1);
+
+       smb2_oplock_create(&io, fname, SMB2_OPLOCK_LEVEL_NONE);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv1 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+       CHECK_BREAK_INFO("RWH", "RH", LEASE1);
+
+       smb2_util_close(tree, hv1);
+       smb2_util_close(tree, hv2);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls1v2, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, NULL,
+                                  smb2_util_lease_state("RWH"),
+                                  0x4711);
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       hv2 = io.out.file.handle;
+       CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1v2.lease_epoch + 1);
+       smb2_util_close(tree, hv2);
+
+done:
+       smb2_util_close(tree, hv2);
+       smb2_util_close(tree, hv1);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_breaking1(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_breaking1.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(break_info);
+       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 =
+               break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(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.
+        */
+       status = smb2_lease_break_ack(tree, &ack);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_LEASE_BREAK_ACK(&ack, "RH", 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_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_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;
+
+       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(break_info);
+       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 =
+               break_info.lease_break.current_lease.lease_key;
+       ZERO_STRUCT(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 torture_lease_break 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(break_info);
+       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.
+        */
+       break_info_tmp = break_info;
+       ZERO_STRUCT(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);
+       break_info = break_info_tmp;
+
+       torture_assert(tctx, req3->state == SMB2_REQUEST_RECV, "req3 pending");
+
+       ack.in.lease.lease_key =
+               break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(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")
+        */
+       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 =
+               break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(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(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 torture_lease_break 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(break_info);
+       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);
+
+       break_info_tmp = break_info;
+       ZERO_STRUCT(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);
+       break_info = break_info_tmp;
+       ack.in.lease.lease_key =
+               break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(break_info);
+       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 torture_lease_break 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(break_info);
+       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);
+
+       break_info_tmp = break_info;
+       ZERO_STRUCT(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);
+       break_info = break_info_tmp;
+       ack.in.lease.lease_key =
+               break_info.lease_break.current_lease.lease_key;
+       ack.in.lease.lease_state =
+               break_info.lease_break.new_lease_state;
+       ZERO_STRUCT(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(break_info);
+       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 =
+               break_info.lease_break.current_lease.lease_key;
+       ZERO_STRUCT(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_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, h2, h3;
+       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(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("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);
+
+       /* 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(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("RH", "", LEASE2);
+
+       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);
+       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, h2, h3;
+       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(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("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);
+
+       /* 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(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,
+                           "RH", "", LEASE2, ls2.lease_epoch);
+
+       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);
+       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;
+}
+
 struct torture_suite *torture_smb2_lease_init(void)
 {
        struct torture_suite *suite =
@@ -1433,9 +2882,20 @@ struct torture_suite *torture_smb2_lease_init(void)
        torture_suite_add_1smb2_test(suite, "break", test_lease_break);
        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, "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_1smb2_test(suite, "complex1", test_lease_complex1);
        torture_suite_add_1smb2_test(suite, "v2_request_parent",
                                     test_lease_v2_request_parent);
        torture_suite_add_1smb2_test(suite, "v2_request", test_lease_v2_request);
+       torture_suite_add_1smb2_test(suite, "v2_epoch1", test_lease_v2_epoch1);
+       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);
 
        suite->description = talloc_strdup(suite, "SMB2-LEASE tests");