Revert "HACKS *.oplock.batch9 ... this is very strange with stat opens and different...
[metze/samba/wip.git] / source4 / torture / smb2 / oplock.c
index d6156eb4d8cf0e78e31ffe1a5f8ced8652dbe1a5..d39b904cc7cddfa30b642bf08eb2c38155db1206 100644 (file)
@@ -26,6 +26,7 @@
 #include "libcli/smb2/smb2_calls.h"
 #include "libcli/smb_composite/smb_composite.h"
 #include "libcli/resolve/resolve.h"
+#include "libcli/smb/smbXcli_base.h"
 
 #include "lib/cmdline/popt_common.h"
 #include "lib/events/events.h"
@@ -304,60 +305,6 @@ static bool open_smb2_connection_no_level2_oplocks(struct torture_context *tctx,
        return true;
 }
 
-/*
-   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_oplock_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 oplock break */
-       ne = tevent_timeval_current_ofs(0, 100000);
-
-       if ((te = tevent_add_timer(tctx->ev, tmp_ctx, ne, timeout_cb, &timesup))
-           == 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;
-}
-
 static bool test_smb2_oplock_exclusive1(struct torture_context *tctx,
                                        struct smb2_tree *tree1,
                                        struct smb2_tree *tree2)
@@ -1544,9 +1491,11 @@ static bool test_smb2_oplock_batch9(struct torture_context *tctx,
                                SEC_FILE_WRITE_ATTRIBUTE |
                                SEC_STD_SYNCHRONIZE;
        io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
        status = smb2_create(tree1, tctx, &(io.smb2));
        torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
        h1 = io.smb2.out.file.handle;
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_CREATED);
        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
 
        torture_comment(tctx, "Subsequent normal open should break oplock on "
@@ -1558,6 +1507,7 @@ static bool test_smb2_oplock_batch9(struct torture_context *tctx,
        io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
        io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
        io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
        status = smb2_create(tree2, tctx, &(io.smb2));
        torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
        h2 = io.smb2.out.file.handle;
@@ -1566,6 +1516,7 @@ static bool test_smb2_oplock_batch9(struct torture_context *tctx,
        CHECK_VAL(break_info.handle.data[0], h1.data[0]);
        CHECK_VAL(break_info.failures, 0);
        CHECK_VAL(break_info.level, SMB2_OPLOCK_LEVEL_II);
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_OPENED);
        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
        smb2_util_close(tree2, h2);
 
@@ -3949,6 +3900,78 @@ static void levelII501_timeout_cb(struct tevent_context *ev,
        state->done = true;
 }
 
+static bool test_smb2_oplock_levelII502(struct torture_context *tctx,
+                                       struct smb2_tree *tree1,
+                                       struct smb2_tree *tree2)
+
+{
+       const char *fname = BASEDIR "\\test_levelII502.dat";
+       NTSTATUS status;
+       union smb_open io;
+       struct smb2_close closeio;
+       struct smb2_handle h;
+
+       status = torture_smb2_testdir(tree1, BASEDIR, &h);
+       torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+       /* cleanup */
+       smb2_util_unlink(tree1, fname);
+
+       /*
+         base ntcreatex parms
+       */
+       ZERO_STRUCT(io.smb2);
+       io.generic.level = RAW_OPEN_SMB2;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.alloc_size = 0;
+       io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       io.smb2.in.create_options = 0;
+       io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       io.smb2.in.security_flags = 0;
+       io.smb2.in.fname = fname;
+
+       torture_comment(
+               tctx,
+               "LEVELII502: Open a stale LEVEL2 oplock with OVERWRITE");
+
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_READ |
+                               SEC_RIGHTS_FILE_WRITE;
+       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+                               NTCREATEX_SHARE_ACCESS_WRITE;
+       io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_II;
+       status = smb2_create(tree1, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+       torture_assert(tctx,
+                      io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_II,
+                      "Did not get LEVEL_II oplock\n");
+
+       status = smbXcli_conn_samba_suicide(
+               tree1->session->transport->conn, 93);
+       torture_assert_ntstatus_ok(tctx, status, "suicide failed");
+
+       sleep(1);
+
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+
+       status = smb2_create(tree2, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+       torture_assert(tctx,
+                      io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_BATCH,
+                      "Did not get BATCH oplock\n");
+
+       closeio = (struct smb2_close) {
+               .in.file.handle = io.smb2.out.file.handle,
+       };
+       status = smb2_close(tree2, &closeio);
+       torture_assert_ntstatus_equal(
+               tctx, status, NT_STATUS_OK, "close failed");
+
+       return true;
+}
+
 struct torture_suite *torture_smb2_oplocks_init(TALLOC_CTX *ctx)
 {
        struct torture_suite *suite =
@@ -3995,6 +4018,8 @@ struct torture_suite *torture_smb2_oplocks_init(TALLOC_CTX *ctx)
        torture_suite_add_1smb2_test(suite, "levelii500", test_smb2_oplock_levelII500);
        torture_suite_add_2smb2_test(suite, "levelii501",
                                     test_smb2_oplock_levelII501);
+       torture_suite_add_2smb2_test(suite, "levelii502",
+                                    test_smb2_oplock_levelII502);
        suite->description = talloc_strdup(suite, "SMB2-OPLOCK tests");
 
        return suite;