source4/torture/smb2/durable_open.c
authorStefan Metzmacher <metze@samba.org>
Mon, 14 Apr 2014 20:32:30 +0000 (22:32 +0200)
committerStefan Metzmacher <metze@samba.org>
Tue, 4 Jun 2019 10:43:20 +0000 (12:43 +0200)
source4/torture/smb2/durable_open.c

index 229c39dce675fa26e0f777048f3c09728d89148c..53e0a4d6af1cc6634340611c308561ef190e1baa 100644 (file)
        } while(0)
 
 
+static struct {
+       struct smb2_handle handle;
+       uint8_t level;
+       struct smb2_break br;
+       int count;
+       int failures;
+       NTSTATUS failure_status;
+} break_info;
+
+static void torture_oplock_break_callback(struct smb2_request *req)
+{
+       NTSTATUS status;
+       struct smb2_break br;
+
+       ZERO_STRUCT(br);
+       status = smb2_break_recv(req, &break_info.br);
+       if (!NT_STATUS_IS_OK(status)) {
+               break_info.failures++;
+               break_info.failure_status = status;
+       }
+
+       return;
+}
+
+/* A general oplock break notification handler.  This should be used when a
+ * test expects to break from batch or exclusive to a lower level. */
+static bool torture_oplock_handler(struct smb2_transport *transport,
+                                  const struct smb2_handle *handle,
+                                  uint8_t level,
+                                  void *private_data)
+{
+       struct smb2_tree *tree = private_data;
+       const char *name;
+       struct smb2_request *req;
+       ZERO_STRUCT(break_info.br);
+
+       break_info.handle       = *handle;
+       break_info.level        = level;
+       break_info.count++;
+
+       switch (level) {
+       case SMB2_OPLOCK_LEVEL_II:
+               name = "level II";
+               break;
+       case SMB2_OPLOCK_LEVEL_NONE:
+               name = "none";
+               break;
+       default:
+               name = "unknown";
+               break_info.failures++;
+       }
+       printf("Acking to %s [0x%02X] in oplock handler\n", name, level);
+
+       break_info.br.in.file.handle    = *handle;
+       break_info.br.in.oplock_level   = level;
+       break_info.br.in.reserved       = 0;
+       break_info.br.in.reserved2      = 0;
+
+       req = smb2_break_send(tree, &break_info.br);
+       req->async.fn = torture_oplock_break_callback;
+       req->async.private_data = NULL;
+       return true;
+}
+
+/*
+  A handler function for oplock break notifications. Send a break to none
+  request.
+*/
+static bool torture_oplock_handler_ack_to_none(struct smb2_transport *transport,
+                                              const struct smb2_handle *handle,
+                                              uint8_t level,
+                                              void *private_data)
+{
+       struct smb2_tree *tree = private_data;
+       struct smb2_request *req;
+
+       break_info.handle = *handle;
+       break_info.level = level;
+       break_info.count++;
+
+       printf("Acking to none in oplock handler\n");
+
+       ZERO_STRUCT(break_info.br);
+       break_info.br.in.file.handle    = *handle;
+       break_info.br.in.oplock_level   = SMB2_OPLOCK_LEVEL_NONE;
+       break_info.br.in.reserved       = 0;
+       break_info.br.in.reserved2      = 0;
+
+       req = smb2_break_send(tree, &break_info.br);
+       req->async.fn = torture_oplock_break_callback;
+       req->async.private_data = NULL;
+
+       return true;
+}
+
+/*
+  A handler function for oplock break notifications. Break from level II to
+  none.  SMB2 requires that the client does not send an oplock break request to
+  the server in this case.
+*/
+static bool torture_oplock_handler_level2_to_none(
+                                              struct smb2_transport *transport,
+                                              const struct smb2_handle *handle,
+                                              uint8_t level,
+                                              void *private_data)
+{
+       break_info.handle = *handle;
+       break_info.level = level;
+       break_info.count++;
+
+       printf("Break from level II to none in oplock handler\n");
+
+       return true;
+}
+
+/* A handler function for oplock break notifications.  This should be used when
+ * test expects two break notifications, first to level II, then to none. */
+static bool torture_oplock_handler_two_notifications(
+                                       struct smb2_transport *transport,
+                                       const struct smb2_handle *handle,
+                                       uint8_t level,
+                                       void *private_data)
+{
+       struct smb2_tree *tree = private_data;
+       const char *name;
+       struct smb2_request *req;
+       ZERO_STRUCT(break_info.br);
+
+       break_info.handle       = *handle;
+       break_info.level        = level;
+       break_info.count++;
+
+       switch (level) {
+       case SMB2_OPLOCK_LEVEL_II:
+               name = "level II";
+               break;
+       case SMB2_OPLOCK_LEVEL_NONE:
+               name = "none";
+               break;
+       default:
+               name = "unknown";
+               break_info.failures++;
+       }
+       printf("Breaking to %s [0x%02X] in oplock handler\n", name, level);
+
+       if (level == SMB2_OPLOCK_LEVEL_NONE)
+               return true;
+
+       break_info.br.in.file.handle    = *handle;
+       break_info.br.in.oplock_level   = level;
+       break_info.br.in.reserved       = 0;
+       break_info.br.in.reserved2      = 0;
+
+       req = smb2_break_send(tree, &break_info.br);
+       req->async.fn = torture_oplock_break_callback;
+       req->async.private_data = NULL;
+       return true;
+}
+static void torture_oplock_handler_close_recv(struct smb2_request *req)
+{
+       if (!smb2_request_receive(req)) {
+               printf("close failed in oplock_handler_close\n");
+               break_info.failures++;
+       }
+}
+
+/*
+  a handler function for oplock break requests - close the file
+*/
+static bool torture_oplock_handler_close(struct smb2_transport *transport,
+                                        const struct smb2_handle *handle,
+                                        uint8_t level,
+                                        void *private_data)
+{
+       struct smb2_close io;
+       struct smb2_tree *tree = private_data;
+       struct smb2_request *req;
+
+       break_info.handle = *handle;
+       break_info.level = level;
+       break_info.count++;
+
+       ZERO_STRUCT(io);
+       io.in.file.handle       = *handle;
+       io.in.flags          = RAW_CLOSE_SMB2;
+       req = smb2_close_send(tree, &io);
+       if (req == NULL) {
+               printf("failed to send close in oplock_handler_close\n");
+               return false;
+       }
+
+       req->async.fn = torture_oplock_handler_close_recv;
+       req->async.private_data = NULL;
+
+       return true;
+}
+
+/*
+  a handler function for oplock break requests. Let it timeout
+*/
+static bool torture_oplock_handler_timeout(struct smb2_transport *transport,
+                                          const struct smb2_handle *handle,
+                                          uint8_t level,
+                                          void *private_data)
+{
+       break_info.handle = *handle;
+       break_info.level = level;
+       break_info.count++;
+
+       printf("Let oplock break timeout\n");
+       return true;
+}
+
 
 /**
  * basic durable_open test.
@@ -1592,6 +1805,104 @@ done:
        return ret;
 }
 
+static bool test_durable_open_reopen5(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       char fname[256];
+       struct smb2_handle _h;
+       struct smb2_handle *h = NULL;
+       struct smb2_create io1, io2;
+       bool ret = true;
+       struct smb2_transport *transport;
+       struct smb2_session *session2;
+       struct smb2_tree *tree2;
+
+       /* Choose a random name in case the state is left a little funky. */
+       snprintf(fname, 256, "durable_open_reopen5_%s.dat",
+                generate_random_str(tctx, 8));
+
+       smb2_util_unlink(tree, fname);
+
+       ZERO_STRUCT(break_info);
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+
+       smb2_oplock_create_share(&io1, fname,
+                                smb2_util_share_access("RWD"),
+                                smb2_util_oplock_level("b"));
+       io1.in.durable_open = true;
+
+       status = smb2_create(tree, mem_ctx, &io1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       _h = io1.out.file.handle;
+       h = &_h;
+       CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io1.out.durable_open, true);
+       CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
+
+       io2 = io1;
+       io2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       status = smb2_create(tree, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(io2.out.durable_open, false);
+       CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("s"));
+
+       /*
+        * do a session logoff, establish a new session and tree
+        * connect on the same transport, and try a durable reopen
+        */
+       transport = tree->session->transport;
+       status = smb2_logoff(tree->session);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       if (!torture_smb2_session_setup(tctx, transport,
+                                       0, /* previous_session_id */
+                                       mem_ctx, &session2))
+       {
+               torture_warning(tctx, "session setup failed.\n");
+               ret = false;
+               goto done;
+       }
+
+       /*
+        * the session setup has talloc-stolen the transport,
+        * so we can safely free the old tree+session for clarity
+        */
+       TALLOC_FREE(tree);
+
+       if (!torture_smb2_tree_connect(tctx, session2, mem_ctx, &tree2)) {
+               torture_warning(tctx, "tree connect failed.\n");
+               ret = false;
+               goto done;
+       }
+
+       ZERO_STRUCT(io2);
+       io2.in.fname = fname;
+       io2.in.durable_handle = h;
+       h = NULL;
+
+       status = smb2_create(tree2, mem_ctx, &io2);
+       CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
+
+done:
+       if (tree != NULL) {
+               if (h != NULL) {
+                       smb2_util_close(tree2, *h);
+               }
+
+               smb2_util_unlink(tree2, fname);
+
+               talloc_free(tree);
+       }
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
 static bool test_durable_open_delete_on_close1(struct torture_context *tctx,
                                               struct smb2_tree *tree)
 {
@@ -2768,6 +3079,7 @@ struct torture_suite *torture_smb2_durable_open_init(TALLOC_CTX *ctx)
        torture_suite_add_1smb2_test(suite, "reopen2a", test_durable_open_reopen2a);
        torture_suite_add_1smb2_test(suite, "reopen3", test_durable_open_reopen3);
        torture_suite_add_1smb2_test(suite, "reopen4", test_durable_open_reopen4);
+       torture_suite_add_1smb2_test(suite, "reopen5", test_durable_open_reopen5);
        torture_suite_add_1smb2_test(suite, "delete_on_close1",
                                     test_durable_open_delete_on_close1);
        torture_suite_add_1smb2_test(suite, "delete_on_close2",