s4:torture:smb2:durable-v2-open: add tests for persistent handles
authorMichael Adam <obnox@samba.org>
Wed, 29 Feb 2012 22:43:21 +0000 (23:43 +0100)
committerStefan Metzmacher <metze@samba.org>
Thu, 10 May 2012 16:46:51 +0000 (18:46 +0200)
source4/torture/smb2/durable_v2_open.c

index c1a2c883b956e1d9911298eff15ed51980c32190..c0842c108023a6b4128a7645dc32cd519ccfd87d 100644 (file)
@@ -535,6 +535,292 @@ bool test_persistent_open_lease(struct torture_context *tctx,
        return ret;
 }
 
+
+/**
+ * basic persistent open test.
+ *
+ * This test tests durable open with all possible oplock types.
+ */
+
+struct persistent_open_vs_oplock {
+       const char *level;
+       const char *share_mode;
+       bool durable;
+       bool persistent;
+};
+
+#define NUM_OPLOCK_TYPES 4
+#define NUM_SHARE_MODES 8
+#define NUM_OPLOCK_OPEN_TESTS ( NUM_OPLOCK_TYPES * NUM_SHARE_MODES )
+struct persistent_open_vs_oplock persistent_open_vs_oplock_table[NUM_OPLOCK_OPEN_TESTS] =
+{
+       { "", "", false, false },
+       { "", "R", false, false },
+       { "", "W", false, false },
+       { "", "D", false, false },
+       { "", "RD", false, false },
+       { "", "RW", false, false },
+       { "", "WD", false, false },
+       { "", "RWD", false, false },
+
+       { "s", "", false, false },
+       { "s", "R", false, false },
+       { "s", "W", false, false },
+       { "s", "D", false, false },
+       { "s", "RD", false, false },
+       { "s", "RW", false, false },
+       { "s", "WD", false, false },
+       { "s", "RWD", false, false },
+
+       { "x", "", false, false },
+       { "x", "R", false, false },
+       { "x", "W", false, false },
+       { "x", "D", false, false },
+       { "x", "RD", false, false },
+       { "x", "RW", false, false },
+       { "x", "WD", false, false },
+       { "x", "RWD", false, false },
+
+       { "b", "", true, true },
+       { "b", "R", true, true },
+       { "b", "W", true, true },
+       { "b", "D", true, true },
+       { "b", "RD", true, true },
+       { "b", "RW", true, true },
+       { "b", "WD", true, true },
+       { "b", "RWD", true, true },
+};
+
+static bool test_one_persistent_open_oplock(struct torture_context *tctx,
+                                           struct smb2_tree *tree,
+                                           const char *fname,
+                                           struct persistent_open_vs_oplock test)
+{
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_handle _h;
+       struct smb2_handle *h = NULL;
+       bool ret = true;
+       struct smb2_create io;
+
+       smb2_util_unlink(tree, fname);
+
+       smb2_oplock_create_share(&io, fname,
+                                smb2_util_share_access(test.share_mode),
+                                smb2_util_oplock_level(test.level));
+       io.in.durable_open = false;
+       io.in.durable_open_v2 = true;
+       io.in.persistent_open = true;
+       io.in.create_guid = GUID_random();
+
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       _h = io.out.file.handle;
+       h = &_h;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io.out.durable_open, false);
+       CHECK_VAL(io.out.durable_open_v2, test.durable);
+       CHECK_VAL(io.out.persistent_open, test.persistent);
+       CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level(test.level));
+
+done:
+       if (h != NULL) {
+               smb2_util_close(tree, *h);
+       }
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+bool test_persistent_open_oplock(struct torture_context *tctx,
+                                struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       char fname[256];
+       bool ret = true;
+       int i;
+
+       /* Choose a random name in case the state is left a little funky. */
+       snprintf(fname, 256, "persistent_open_oplock_%s.dat", generate_random_str(tctx, 8));
+
+       smb2_util_unlink(tree, fname);
+
+       /* test various oplock levels with durable open */
+
+       for (i = 0; i < NUM_OPLOCK_OPEN_TESTS; i++) {
+               ret = test_one_persistent_open_oplock(tctx,
+                                                     tree,
+                                                     fname,
+                                                     persistent_open_vs_oplock_table[i]);
+               if (ret == false) {
+                       goto done;
+               }
+       }
+
+done:
+       smb2_util_unlink(tree, fname);
+       talloc_free(tree);
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+/**
+ * basic persistent handle open test.
+ * persistent state should only be granted when requested
+ * along with a batch oplock or a handle lease.
+ *
+ * This test tests persistent open with all valid lease types.
+ */
+
+struct persistent_open_vs_lease {
+       const char *type;
+       const char *share_mode;
+       bool durable;
+       bool persistent;
+};
+
+#define NUM_LEASE_TYPES 5
+#define NUM_LEASE_OPEN_TESTS ( NUM_LEASE_TYPES * NUM_SHARE_MODES )
+struct persistent_open_vs_lease persistent_open_vs_lease_table[NUM_LEASE_OPEN_TESTS] =
+{
+       { "", "", false, false },
+       { "", "R", false, false },
+       { "", "W", false, false },
+       { "", "D", false, false },
+       { "", "RW", false, false },
+       { "", "RD", false, false },
+       { "", "WD", false, false },
+       { "", "RWD", false, false },
+
+       { "R", "", false, false },
+       { "R", "R", false, false },
+       { "R", "W", false, false },
+       { "R", "D", false, false },
+       { "R", "RW", false, false },
+       { "R", "RD", false, false },
+       { "R", "DW", false, false },
+       { "R", "RWD", false, false },
+
+       { "RW", "", false, false },
+       { "RW", "R", false, false },
+       { "RW", "W", false, false },
+       { "RW", "D", false, false },
+       { "RW", "RW", false, false },
+       { "RW", "RD", false, false },
+       { "RW", "WD", false, false },
+       { "RW", "RWD", false, false },
+
+       { "RH", "", true, true },
+       { "RH", "R", true, true },
+       { "RH", "W", true, true },
+       { "RH", "D", true, true },
+       { "RH", "RW", true, true },
+       { "RH", "RD", true, true },
+       { "RH", "WD", true, true },
+       { "RH", "RWD", true, true },
+
+       { "RHW", "", true, true },
+       { "RHW", "R", true, true },
+       { "RHW", "W", true, true },
+       { "RHW", "D", true, true },
+       { "RHW", "RW", true, true },
+       { "RHW", "RD", true, true },
+       { "RHW", "WD", true, true },
+       { "RHW", "RWD", true, true },
+};
+
+static bool test_one_persistent_open_lease(struct torture_context *tctx,
+                                          struct smb2_tree *tree,
+                                          const char *fname,
+                                          struct persistent_open_vs_lease test)
+{
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       struct smb2_handle _h;
+       struct smb2_handle *h = NULL;
+       bool ret = true;
+       struct smb2_create io;
+       struct smb2_lease ls;
+       uint64_t lease;
+
+       smb2_util_unlink(tree, fname);
+
+       lease = random();
+
+       smb2_lease_create_share(&io, &ls, false /* dir */, fname,
+                               smb2_util_share_access(test.share_mode),
+                               lease,
+                               smb2_util_lease_state(test.type));
+       io.in.durable_open = false;
+       io.in.durable_open_v2 = true;
+       io.in.persistent_open = true;
+       io.in.create_guid = GUID_random();
+
+       status = smb2_create(tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       _h = io.out.file.handle;
+       h = &_h;
+       CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io.out.durable_open, false);
+       CHECK_VAL(io.out.durable_open_v2, test.durable);
+       CHECK_VAL(io.out.persistent_open, test.persistent);
+       CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
+       CHECK_VAL(io.out.lease_response.lease_key.data[0], lease);
+       CHECK_VAL(io.out.lease_response.lease_key.data[1], ~lease);
+       CHECK_VAL(io.out.lease_response.lease_state,
+                 smb2_util_lease_state(test.type));
+done:
+       if (h != NULL) {
+               smb2_util_close(tree, *h);
+       }
+       smb2_util_unlink(tree, fname);
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
+bool test_persistent_open_lease(struct torture_context *tctx,
+                               struct smb2_tree *tree)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       char fname[256];
+       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");
+       }
+
+       /* Choose a random name in case the state is left a little funky. */
+       snprintf(fname, 256, "persistent_open_lease_%s.dat", generate_random_str(tctx, 8));
+
+       smb2_util_unlink(tree, fname);
+
+
+       /* test various oplock levels with persistent open */
+
+       for (i = 0; i < NUM_LEASE_OPEN_TESTS; i++) {
+               ret = test_one_persistent_open_lease(tctx,
+                                                    tree,
+                                                    fname,
+                                                    persistent_open_vs_lease_table[i]);
+               if (ret == false) {
+                       goto done;
+               }
+       }
+
+done:
+       smb2_util_unlink(tree, fname);
+       talloc_free(tree);
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
 struct torture_suite *torture_smb2_durable_v2_open_init(void)
 {
        struct torture_suite *suite =