s4:torture/smb2: always verify the v2 lease epoch.
[obnox/samba/samba-obnox.git] / source4 / torture / smb2 / lease.c
index 87526e3f780a171cea301973eacc282af889e667..d10dcc55435bddef1f6dfa68c223d2db22d463eb 100644 (file)
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
-#include "librpc/gen_ndr/security.h"
+#include <tevent.h>
 #include "libcli/smb2/smb2.h"
 #include "libcli/smb2/smb2_calls.h"
 #include "torture/torture.h"
 #include "torture/smb2/proto.h"
-
-static inline uint32_t lease(const char *ls) {
-       uint32_t val = 0;
-       int i;
-
-       for (i = 0; i < strlen(ls); i++) {
-               switch (ls[i]) {
-               case 'R':
-                       val |= SMB2_LEASE_READ;
-                       break;
-               case 'H':
-                       val |= SMB2_LEASE_HANDLE;
-                       break;
-               case 'W':
-                       val |= SMB2_LEASE_WRITE;
-                       break;
-               }
-       }
-
-       return val;
-}
+#include "libcli/smb/smbXcli_base.h"
 
 #define CHECK_VAL(v, correct) do { \
        if ((v) != (correct)) { \
@@ -63,60 +42,6 @@ static inline uint32_t lease(const char *ls) {
                goto done; \
        }} while (0)
 
-static void smb2_generic_create(struct smb2_create *io, struct smb2_lease *ls,
-                                bool dir, const char *name, uint32_t disposition,
-                                uint32_t oplock, uint64_t leasekey,
-                                uint32_t leasestate)
-{
-       ZERO_STRUCT(*io);
-       io->in.security_flags           = 0x00;
-       io->in.oplock_level             = oplock;
-       io->in.impersonation_level      = NTCREATEX_IMPERSONATION_IMPERSONATION;
-       io->in.create_flags             = 0x00000000;
-       io->in.reserved                 = 0x00000000;
-       io->in.desired_access           = SEC_RIGHTS_FILE_ALL;
-       io->in.file_attributes          = FILE_ATTRIBUTE_NORMAL;
-       io->in.share_access             = NTCREATEX_SHARE_ACCESS_READ |
-                                         NTCREATEX_SHARE_ACCESS_WRITE |
-                                         NTCREATEX_SHARE_ACCESS_DELETE;
-       io->in.create_disposition       = disposition;
-       io->in.create_options           = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
-                                         NTCREATEX_OPTIONS_ASYNC_ALERT |
-                                         NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
-                                         0x00200000;
-       io->in.fname                    = name;
-
-       if (dir) {
-               io->in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
-               io->in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
-               io->in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
-               io->in.create_disposition = NTCREATEX_DISP_CREATE;
-       }
-
-       if (ls) {
-               ZERO_STRUCT(*ls);
-               ls->lease_key.data[0] = leasekey;
-               ls->lease_key.data[1] = ~leasekey;
-               ls->lease_state = leasestate;
-               io->in.lease_request = ls;
-       }
-}
-
-static void smb2_lease_create(struct smb2_create *io, struct smb2_lease *ls,
-                              bool dir, const char *name, uint64_t leasekey,
-                              uint32_t leasestate)
-{
-       smb2_generic_create(io, ls, dir, name, NTCREATEX_DISP_OPEN_IF,
-           SMB2_OPLOCK_LEVEL_LEASE, leasekey, leasestate);
-}
-
-static void smb2_oplock_create(struct smb2_create *io, const char *name,
-                               uint32_t oplock)
-{
-       smb2_generic_create(io, NULL, false, name, NTCREATEX_DISP_OPEN_IF,
-           oplock, 0, 0);
-}
-
 #define CHECK_CREATED(__io, __created, __attribute)                    \
        do {                                                            \
                CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
@@ -128,11 +53,12 @@ static void smb2_oplock_create(struct smb2_create *io, const char *name,
 
 #define CHECK_LEASE(__io, __state, __oplevel, __key)                   \
        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_key.data[1], ~(__key)); \
-                       CHECK_VAL((__io)->out.lease_response.lease_state, lease(__state)); \
+                       CHECK_VAL((__io)->out.lease_response.lease_state, smb2_util_lease_state(__state)); \
                } else {                                                \
                        CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_NONE); \
                        CHECK_VAL((__io)->out.lease_response.lease_key.data[0], 0); \
@@ -142,11 +68,37 @@ static void smb2_oplock_create(struct smb2_create *io, const char *name,
                                                                        \
                CHECK_VAL((__io)->out.lease_response.lease_flags, 0);   \
                CHECK_VAL((__io)->out.lease_response.lease_duration, 0); \
-       } while(0)                                                      \
+               CHECK_VAL((__io)->out.lease_response.lease_epoch, 0); \
+       } while(0)
+
+#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)); \
+                       CHECK_VAL((__io)->out.lease_response_v2.lease_key.data[1], ~(__key)); \
+                       CHECK_VAL((__io)->out.lease_response_v2.lease_state, smb2_util_lease_state(__state)); \
+               } else {                                                \
+                       CHECK_VAL((__io)->out.oplock_level, SMB2_OPLOCK_LEVEL_NONE); \
+                       CHECK_VAL((__io)->out.lease_response_v2.lease_key.data[0], 0); \
+                       CHECK_VAL((__io)->out.lease_response_v2.lease_key.data[1], 0); \
+                       CHECK_VAL((__io)->out.lease_response_v2.lease_state, 0); \
+               }                                                       \
+                                                                       \
+               CHECK_VAL((__io)->out.lease_response_v2.lease_flags, __flags); \
+               if (__flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET) { \
+                       CHECK_VAL((__io)->out.lease_response_v2.parent_lease_key.data[0], (__parent)); \
+                       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;
 static const uint64_t LEASE2 = 0xDEADBEEFFEEDBEADull;
 static const uint64_t LEASE3 = 0xDAD0FFEDD00DF00Dull;
+static const uint64_t LEASE4 = 0xBAD0FFEDD00DF00Dull;
 
 #define NREQUEST_RESULTS 8
 static const char *request_results[NREQUEST_RESULTS][2] = {
@@ -168,19 +120,25 @@ 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;
+
+       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);
        smb2_util_unlink(tree, fname2);
        smb2_util_rmdir(tree, dname);
 
        /* Win7 is happy to grant RHW leases on files. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RHW"));
+       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);
        h1 = io.out.file.handle;
@@ -188,20 +146,22 @@ static bool test_lease_request(struct torture_context *tctx,
        CHECK_LEASE(&io, "RHW", true, LEASE1);
 
        /* But will reject leases on directories. */
-       smb2_lease_create(&io, &ls, true, dname, LEASE2, lease("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, lease("RHW"));
+       smb2_lease_create(&io, &ls, true, fname2, LEASE1, smb2_util_lease_state("RHW"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);
 
        /* And grants leases on streams (with separate leasekey). */
-       smb2_lease_create(&io, &ls, false, sname, LEASE2, lease("RHW"));
+       smb2_lease_create(&io, &ls, false, sname, LEASE2, smb2_util_lease_state("RHW"));
        status = smb2_create(tree, mem_ctx, &io);
        h2 = io.out.file.handle;
        CHECK_STATUS(status, NT_STATUS_OK);
@@ -215,10 +175,10 @@ static bool test_lease_request(struct torture_context *tctx,
        for (i = 0; i < NREQUEST_RESULTS; i++) {
                torture_comment(tctx, "Requesting lease type %s(%x),"
                    " expecting %s(%x)\n",
-                   request_results[i][0], lease(request_results[i][0]),
-                   request_results[i][1], lease(request_results[i][1]));
+                   request_results[i][0], smb2_util_lease_state(request_results[i][0]),
+                   request_results[i][1], smb2_util_lease_state(request_results[i][1]));
                smb2_lease_create(&io, &ls, false, fname, LEASE1,
-                   lease(request_results[i][0]));
+                   smb2_util_lease_state(request_results[i][0]));
                status = smb2_create(tree, mem_ctx, &io);
                h2 = io.out.file.handle;
                CHECK_STATUS(status, NT_STATUS_OK);
@@ -248,13 +208,19 @@ 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;
+
+       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 RH lease. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RH"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RH"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
@@ -262,7 +228,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        h = io.out.file.handle;
 
        /* Upgrades (sidegrades?) to RW leave us with an RH. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RW"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RW"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
@@ -272,7 +238,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        smb2_util_close(tree, hnew);
 
        /* Upgrade to RHW lease. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RHW"));
+       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);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
@@ -283,7 +249,7 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        h = hnew;
 
        /* Attempt to downgrade - original lease state is maintained. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RH"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RH"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
@@ -303,12 +269,130 @@ static bool test_lease_upgrade(struct torture_context *tctx,
        return ret;
 }
 
+/**
+ * upgrade2 test.
+ * full matrix of lease upgrade combinations
+ * (non-contended case)
+ *
+ * The summary of the behaviour is this:
+ * -------------------------------------
+ * An uncontended lease upgrade results in a change
+ * if and only if the requested lease state is
+ * - valid, and
+ * - strictly a superset of the lease state already held.
+ *
+ * In that case the resulting lease state is the one
+ * requested in the upgrade.
+ */
+struct lease_upgrade2_test {
+       const char *initial;
+       const char *upgrade_to;
+       const char *expected;
+};
+
+#define NUM_LEASE_TYPES 5
+#define NUM_UPGRADE_TESTS ( NUM_LEASE_TYPES * NUM_LEASE_TYPES )
+struct lease_upgrade2_test lease_upgrade2_tests[NUM_UPGRADE_TESTS] = {
+       { "", "", "" },
+       { "", "R", "R" },
+       { "", "RH", "RH" },
+       { "", "RW", "RW" },
+       { "", "RWH", "RWH" },
+
+       { "R", "", "R" },
+       { "R", "R", "R" },
+       { "R", "RH", "RH" },
+       { "R", "RW", "RW" },
+       { "R", "RWH", "RWH" },
+
+       { "RH", "", "RH" },
+       { "RH", "R", "RH" },
+       { "RH", "RH", "RH" },
+       { "RH", "RW", "RH" },
+       { "RH", "RWH", "RWH" },
+
+       { "RW", "", "RW" },
+       { "RW", "R", "RW" },
+       { "RW", "RH", "RW" },
+       { "RW", "RW", "RW" },
+       { "RW", "RWH", "RWH" },
+
+       { "RWH", "", "RWH" },
+       { "RWH", "R", "RWH" },
+       { "RWH", "RH", "RWH" },
+       { "RWH", "RW", "RWH" },
+       { "RWH", "RWH", "RWH" },
+};
+
+static bool test_lease_upgrade2(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_handle h, hnew;
+       NTSTATUS status;
+       struct smb2_create io;
+       struct smb2_lease ls;
+       const char *fname = "lease_upgrade2.dat";
+       bool ret = true;
+       int i;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       for (i = 0; i < NUM_UPGRADE_TESTS; i++) {
+               struct lease_upgrade2_test t = lease_upgrade2_tests[i];
+
+               smb2_util_unlink(tree, fname);
+
+               /* Grab a lease. */
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.initial));
+               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);
+               h = io.out.file.handle;
+
+               /* Upgrade. */
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.upgrade_to));
+               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);
+               hnew = io.out.file.handle;
+
+               smb2_util_close(tree, hnew);
+               smb2_util_close(tree, h);
+       }
+
+ done:
+       smb2_util_close(tree, h);
+       smb2_util_close(tree, hnew);
+
+       smb2_util_unlink(tree, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+
 #define CHECK_LEASE_BREAK(__lb, __oldstate, __state, __key)            \
        do {                                                            \
-               CHECK_VAL((__lb)->new_lease_state, lease(__state));     \
-               CHECK_VAL((__lb)->current_lease.lease_state, lease(__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)                   \
@@ -316,37 +400,73 @@ static bool test_lease_upgrade(struct torture_context *tctx,
                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, lease(__state)); \
+               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 {
        struct smb2_lease_break lease_break;
+       struct smb2_transport *lease_transport;
        struct smb2_lease_break_ack lease_break_ack;
        int count;
        int failures;
 
        struct smb2_handle oplock_handle;
-       int held_oplock_level;
-       int oplock_level;
+       uint8_t held_oplock_level;
+       uint8_t oplock_level;
        int oplock_count;
        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) {        \
+                       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);   \
+               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;
@@ -367,6 +487,7 @@ 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++;
 
@@ -383,6 +504,201 @@ static bool torture_lease_handler(struct smb2_transport *transport,
        return true;
 }
 
+/**
+ * upgrade3:
+ * full matrix of lease upgrade combinations
+ * (contended case)
+ *
+ * We start with 2 leases, and check how one can
+ * be upgraded
+ *
+ * The summary of the behaviour is this:
+ * -------------------------------------
+ *
+ * If we have two leases (lease1 and lease2) on the same file,
+ * then attempt to upgrade lease1 results in a change if and only
+ * if the requested lease state:
+ * - is valid,
+ * - is strictly a superset of lease1, and
+ * - can held together with lease2.
+ *
+ * In that case, the resuling lease state of the upgraded lease1
+ * is the state requested in the upgrade. lease2 is not broken
+ * and remains unchanged.
+ *
+ * Note that this contrasts the case of directly opening with
+ * an initial requested lease state, in which case you get that
+ * portion of the requested state that can be shared with the
+ * already existing leases (or the states that they get broken to).
+ */
+struct lease_upgrade3_test {
+       const char *held1;
+       const char *held2;
+       const char *upgrade_to;
+       const char *upgraded_to;
+};
+
+#define NUM_UPGRADE3_TESTS ( 20 )
+struct lease_upgrade3_test lease_upgrade3_tests[NUM_UPGRADE3_TESTS] = {
+       {"R", "R", "", "R" },
+       {"R", "R", "R", "R" },
+       {"R", "R", "RW", "R" },
+       {"R", "R", "RH", "RH" },
+       {"R", "R", "RHW", "R" },
+
+       {"R", "RH", "", "R" },
+       {"R", "RH", "R", "R" },
+       {"R", "RH", "RW", "R" },
+       {"R", "RH", "RH", "RH" },
+       {"R", "RH", "RHW", "R" },
+
+       {"RH", "R", "", "RH" },
+       {"RH", "R", "R", "RH" },
+       {"RH", "R", "RW", "RH" },
+       {"RH", "R", "RH", "RH" },
+       {"RH", "R", "RHW", "RH" },
+
+       {"RH", "RH", "", "RH" },
+       {"RH", "RH", "R", "RH" },
+       {"RH", "RH", "RW", "RH" },
+       {"RH", "RH", "RH", "RH" },
+       {"RH", "RH", "RHW", "RH" },
+};
+
+static bool test_lease_upgrade3(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_handle h, h2, hnew;
+       NTSTATUS status;
+       struct smb2_create io;
+       struct smb2_lease ls;
+       const char *fname = "lease_upgrade3.dat";
+       bool ret = true;
+       int i;
+       uint32_t caps;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       smb2_util_unlink(tree, fname);
+
+       for (i = 0; i < NUM_UPGRADE3_TESTS; i++) {
+               struct lease_upgrade3_test t = lease_upgrade3_tests[i];
+
+               smb2_util_unlink(tree, fname);
+
+               ZERO_STRUCT(break_info);
+
+               /* grab first lease */
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.held1));
+               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);
+               h = io.out.file.handle;
+
+               /* grab second lease */
+               smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state(t.held2));
+               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);
+               h2 = io.out.file.handle;
+
+               /* no break has happened */
+               CHECK_VAL(break_info.count, 0);
+               CHECK_VAL(break_info.failures, 0);
+
+               /* try to upgrade lease1 */
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(t.upgrade_to));
+               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);
+               hnew = io.out.file.handle;
+
+               /* no break has happened */
+               CHECK_VAL(break_info.count, 0);
+               CHECK_VAL(break_info.failures, 0);
+
+               smb2_util_close(tree, hnew);
+               smb2_util_close(tree, h);
+               smb2_util_close(tree, h2);
+       }
+
+ done:
+       smb2_util_close(tree, h);
+       smb2_util_close(tree, hnew);
+       smb2_util_close(tree, h2);
+
+       smb2_util_unlink(tree, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+
+
+/*
+   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
@@ -423,9 +739,15 @@ 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;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
 
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
@@ -439,13 +761,13 @@ static bool test_lease_break(struct torture_context *tctx,
                const char *granted = break_results[i][3];
                torture_comment(tctx, "Hold %s(%x), requesting %s(%x), "
                    "expecting break to %s(%x) and grant of %s(%x)\n",
-                   held, lease(held), contend, lease(contend),
-                   brokento, lease(brokento), granted, lease(granted));
+                   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);
 
                /* Grab lease. */
-               smb2_lease_create(&io, &ls, false, fname, LEASE1, lease(held));
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(held));
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                h = io.out.file.handle;
@@ -453,18 +775,17 @@ static bool test_lease_break(struct torture_context *tctx,
                CHECK_LEASE(&io, held, true, LEASE1);
 
                /* Possibly contend lease. */
-               smb2_lease_create(&io, &ls, false, fname, LEASE2, lease(contend));
+               smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state(contend));
                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, granted, true, LEASE2);
 
-               if (lease(held) != lease(brokento)) {
+               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);
@@ -473,7 +794,7 @@ static bool test_lease_break(struct torture_context *tctx,
                  Now verify that an attempt to upgrade LEASE1 results in no
                  break and no change in LEASE1.
                 */
-               smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RHW"));
+               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);
                h3 = io.out.file.handle;
@@ -501,6 +822,85 @@ static bool test_lease_break(struct torture_context *tctx,
        return ret;
 }
 
+static bool test_lease_nobreakself(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, h2;
+       NTSTATUS status;
+       const char *fname = "lease_nobreakself.dat";
+       bool ret = true;
+       uint32_t caps;
+       char c = 0;
+
+       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);
+
+       /* Win7 is happy to grant RHW leases on files. */
+       smb2_lease_create(&io, &ls, false, fname, LEASE1,
+                         smb2_util_lease_state("R"));
+       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, "R", true, LEASE1);
+
+       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);
+
+       ZERO_STRUCT(break_info);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       /* Make sure we don't break ourselves on write */
+
+       status = smb2_util_write(tree, h1, &c, 0, 1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_BREAK_INFO("R", "", LEASE2);
+
+       /* Try the other way round. First, upgrade LEASE2 to R again */
+
+       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);
+       CHECK_LEASE(&io, "R", true, LEASE2);
+       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);
+       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);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_BREAK_INFO("R", "", LEASE2);
+
+done:
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h1);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
 static void torture_oplock_break_callback(struct smb2_request *req)
 {
        NTSTATUS status;
@@ -541,26 +941,6 @@ static bool torture_oplock_handler(struct smb2_transport *transport,
        return true;
 }
 
-static inline uint32_t oplock(const char *op) {
-       uint32_t val = SMB2_OPLOCK_LEVEL_NONE;
-       int i;
-
-       for (i = 0; i < strlen(op); i++) {
-               switch (op[i]) {
-               case 's':
-                       return SMB2_OPLOCK_LEVEL_II;
-               case 'x':
-                       return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
-               case 'b':
-                       return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
-               default:
-                       continue;
-               }
-       }
-
-       return val;
-}
-
 #define NOPLOCK_RESULTS 12
 static const char *oplock_results[NOPLOCK_RESULTS][4] = {
        {"R",   "s",    "R",    "s"},
@@ -605,9 +985,15 @@ 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;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
 
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
@@ -623,13 +1009,13 @@ static bool test_lease_oplock(struct torture_context *tctx,
                const char *granted = oplock_results[i][3];
                torture_comment(tctx, "Hold %s(%x), requesting %s(%x), "
                    "expecting break to %s(%x) and grant of %s(%x)\n",
-                   held, lease(held), contend, oplock(contend),
-                   brokento, lease(brokento), granted, oplock(granted));
+                   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);
 
                /* Grab lease. */
-               smb2_lease_create(&io, &ls, false, fname, LEASE1, lease(held));
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(held));
                status = smb2_create(tree, mem_ctx, &io);
                CHECK_STATUS(status, NT_STATUS_OK);
                h = io.out.file.handle;
@@ -637,19 +1023,18 @@ static bool test_lease_oplock(struct torture_context *tctx,
                CHECK_LEASE(&io, held, true, LEASE1);
 
                /* Does an oplock contend the lease? */
-               smb2_oplock_create(&io, fname, oplock(contend));
+               smb2_oplock_create(&io, fname, smb2_util_oplock_level(contend));
                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_VAL(io.out.oplock_level, oplock(granted));
+               CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(granted));
                break_info.held_oplock_level = io.out.oplock_level;
 
-               if (lease(held) != lease(brokento)) {
+               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);
@@ -666,36 +1051,32 @@ static bool test_lease_oplock(struct torture_context *tctx,
                const char *granted = oplock_results_2[i][3];
                torture_comment(tctx, "Hold %s(%x), requesting %s(%x), "
                    "expecting break to %s(%x) and grant of %s(%x)\n",
-                   held, oplock(held), contend, lease(contend),
-                   brokento, oplock(brokento), granted, lease(granted));
+                   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);
 
                /* Grab an oplock. */
-               smb2_oplock_create(&io, fname, oplock(held));
+               smb2_oplock_create(&io, fname, smb2_util_oplock_level(held));
                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_VAL(io.out.oplock_level, oplock(held));
+               CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(held));
                break_info.held_oplock_level = io.out.oplock_level;
 
                /* Grab lease. */
-               smb2_lease_create(&io, &ls, false, fname, LEASE1, lease(contend));
+               smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state(contend));
                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, granted, true, LEASE1);
 
-               if (oplock(held) != oplock(brokento)) {
-                       CHECK_VAL(break_info.oplock_count, 1);
-                       CHECK_VAL(break_info.oplock_failures, 0);
-                       CHECK_VAL(break_info.oplock_level, oplock(brokento));
-                       break_info.held_oplock_level = break_info.oplock_level;
+               if (smb2_util_oplock_level(held) != smb2_util_oplock_level(brokento)) {
+                       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);
@@ -725,8 +1106,14 @@ 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;
+
+       caps = smb2cli_conn_server_capabilities(tree->session->transport->conn);
+       if (!(caps & SMB2_CAP_LEASING)) {
+               torture_skip(tctx, "leases are not supported");
+       }
 
        tree->session->transport->lease.handler = torture_lease_handler;
        tree->session->transport->lease.private_data = tree;
@@ -738,14 +1125,14 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        ZERO_STRUCT(break_info);
 
        /* Grab lease, upgrade to RHW .. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RH"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RH"));
        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(&io, "RH", true, LEASE1);
 
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("RHW"));
+       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;
@@ -753,7 +1140,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_LEASE(&io, "RHW", true, LEASE1);
 
        /* Contend with LEASE2. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE2, lease("RHW"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state("RHW"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h3 = io.out.file.handle;
@@ -774,7 +1161,7 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        ZERO_STRUCT(break_info);
 
        /* Grab an R lease. */
-       smb2_lease_create(&io, &ls, false, fname, LEASE1, lease("R"));
+       smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("R"));
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h = io.out.file.handle;
@@ -782,41 +1169,39 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        CHECK_LEASE(&io, "R", true, LEASE1);
 
        /* Grab a level-II oplock. */
-       smb2_oplock_create(&io, fname, oplock("s"));
+       smb2_oplock_create(&io, fname, smb2_util_oplock_level("s"));
        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_VAL(io.out.oplock_level, oplock("s"));
+       CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("s"));
        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,
-           NTCREATEX_DISP_OVERWRITE_IF, oplock(""), 0, 0);
+           NTCREATEX_DISP_OVERWRITE_IF, smb2_util_oplock_level(""), 0, 0);
        status = smb2_create(tree, mem_ctx, &io);
        CHECK_STATUS(status, NT_STATUS_OK);
        h3 = io.out.file.handle;
        CHECK_CREATED(&io, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE);
-       CHECK_VAL(io.out.oplock_level, oplock(""));
+       CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(""));
        break_info.held_oplock_level = io.out.oplock_level;
 
        /* Sleep, use a write to clear the recv queue. */
-       msleep(250);
+       smb_msleep(250);
        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(tree, &w);
        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, oplock(""));
+       CHECK_OPLOCK_BREAK("");
        CHECK_BREAK_INFO("R", "", LEASE1);
 
  done:
@@ -831,16 +1216,370 @@ static bool test_lease_multibreak(struct torture_context *tctx,
        return ret;
 }
 
+static bool test_lease_v2_request_parent(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;
+       uint64_t parent = LEASE2;
+       NTSTATUS status;
+       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);
+
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls, false, fname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE1, &parent,
+                                  smb2_util_lease_state("RHW"),
+                                  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,
+                      ls.lease_epoch + 1);
+
+ done:
+       smb2_util_close(tree, h1);
+       smb2_util_unlink(tree, fname);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+static bool test_lease_break_twice(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 h1;
+       NTSTATUS status;
+       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);
+       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);
+
+       ZERO_STRUCT(break_info);
+       ZERO_STRUCT(io);
+
+       smb2_lease_v2_create_share(
+               &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, ls1.lease_epoch + 1);
+
+       tree->session->transport->lease.handler = torture_lease_handler;
+       tree->session->transport->lease.private_data = tree;
+
+       ZERO_STRUCT(break_info);
+
+       smb2_lease_v2_create_share(
+               &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_V2(tree->session->transport,
+                           "RWH", "RW", LEASE1, ls1.lease_epoch + 2);
+
+       smb2_lease_v2_create_share(
+               &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_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);
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+static bool test_lease_v2_request(struct torture_context *tctx,
+                                 struct smb2_tree *tree)
+{
+       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_write w;
+       NTSTATUS status;
+       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);
+
+       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, &ls1, 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);
+       h1 = io.out.file.handle;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0, 0, ls1.lease_epoch + 1);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls2, true, dname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE2, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  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, ls2.lease_epoch + 1);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls3, false, dnamefname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE3, &LEASE2,
+                                  smb2_util_lease_state("RHW"),
+                                  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,
+                      ls3.lease_epoch + 1);
+
+       CHECK_NO_BREAK(tctx);
+
+       ZERO_STRUCT(io);
+       smb2_lease_v2_create_share(&io, &ls4, false, dnamefname2,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE4, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  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, ls4.lease_epoch + 1);
+
+       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, &ls2t, true, dname,
+                                  smb2_util_share_access("RWD"),
+                                  LEASE2, NULL,
+                                  smb2_util_lease_state("RHW"),
+                                  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, ls2.lease_epoch+3);
+       smb2_util_close(tree, h5);
+
+       ZERO_STRUCT(w);
+       w.in.file.handle = h4;
+       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(tree, &w);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /*
+        * 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);
+
+       CHECK_BREAK_INFO_V2(tree->session->transport,
+                           "RH", "", LEASE2, ls2.lease_epoch+4);
+
+ done:
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
+       smb2_util_close(tree, h3);
+       smb2_util_close(tree, h4);
+       smb2_util_close(tree, h5);
+
+       smb2_util_unlink(tree, fname);
+       smb2_deltree(tree, dname);
+
+       talloc_free(mem_ctx);
+
+       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;
+}
+
 struct torture_suite *torture_smb2_lease_init(void)
 {
        struct torture_suite *suite =
-           torture_suite_create(talloc_autofree_context(), "LEASE");
-
-       torture_suite_add_1smb2_test(suite, "REQUEST", test_lease_request);
-       torture_suite_add_1smb2_test(suite, "UPGRADE", test_lease_upgrade);
-       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_create(talloc_autofree_context(), "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, "upgrade", test_lease_upgrade);
+       torture_suite_add_1smb2_test(suite, "upgrade2", test_lease_upgrade2);
+       torture_suite_add_1smb2_test(suite, "upgrade3", test_lease_upgrade3);
+       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, "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);
 
        suite->description = talloc_strdup(suite, "SMB2-LEASE tests");