works for SMB1 and SMB2
[metze/samba/wip.git] / source4 / torture / smb2 / durable_open.c
index 2e68170077ab90129b10f2fbd59967df63b62462..63eb7e99922a84bbfd834862260d02522e941521 100644 (file)
@@ -25,8 +25,9 @@
 #include "libcli/smb2/smb2_calls.h"
 #include "../libcli/smb/smbXcli_base.h"
 #include "torture/torture.h"
+#include "torture/util.h"
 #include "torture/smb2/proto.h"
-#include "../libcli/smb/smbXcli_base.h"
+#include "../lib/util/time_basic.h"
 
 #define CHECK_VAL(v, correct) do { \
        if ((v) != (correct)) { \
@@ -2752,11 +2753,410 @@ done:
        return ret;
 }
 
+#define COMPARE_TIME_CMP(given, gelem, correct, celem, cmp) do { \
+       const uint64_t _r = 10*1000*1000; \
+       NTTIME _g = (given).basic_info.out.gelem; \
+       NTTIME _gr = (_g / _r) * _r; \
+       NTTIME _c = (correct).basic_info.out.celem; \
+       NTTIME _cr = (_c / _r) * _r; \
+       bool _strict = torture_setting_bool(tctx, "strict mode", false); \
+       const char *_err = NULL; \
+       if (_strict && (_g cmp _c)) { \
+               _err = "strict"; \
+       } else if ((_g cmp _c) && (_gr cmp _cr)) { \
+               /* handle filesystem without high resolution timestamps */ \
+               _err = "rounded"; \
+       } \
+       if (_err != NULL) { \
+               struct timeval _gtv; \
+               struct timeval _ctv; \
+               struct timeval_buf _gtvb; \
+               struct timeval_buf _ctvb; \
+               nttime_to_timeval(&_gtv, _g); \
+               nttime_to_timeval(&_ctv, _c); \
+               torture_result(tctx, TORTURE_FAIL, \
+                              __location__": %s wrong (%s.%s)%s %s (%s.%s)%s", \
+                              _err, \
+                              #given, #gelem, \
+                              timeval_str_buf(&_gtv, false, true, &_gtvb), \
+                              #cmp, \
+                              #correct, #celem, \
+                              timeval_str_buf(&_ctv, false, true, &_ctvb)); \
+               ret = false; \
+               goto done; \
+       } \
+} while (0)
+#define COMPARE_WRITE_TIME_CMP(given, correct, cmp) do { \
+       COMPARE_TIME_CMP(given, write_time, correct, write_time, cmp); \
+} while (0)
+#define COMPARE_WRITE_TIME_EQUAL(given,correct) \
+       COMPARE_WRITE_TIME_CMP(given,correct,!=)
+#define COMPARE_WRITE_TIME_GREATER(given,correct) \
+       COMPARE_WRITE_TIME_CMP(given,correct,<=)
+
+#define COMPARE_ACCESS_TIME_CMP(given, correct, cmp) do { \
+       COMPARE_TIME_CMP(given, access_time, correct, access_time, cmp); \
+} while (0)
+#define COMPARE_ACCESS_TIME_EQUAL(given,correct) \
+       COMPARE_ACCESS_TIME_CMP(given,correct,!=)
+#define COMPARE_ACCESS_TIME_GREATER(given,correct) \
+       COMPARE_ACCESS_TIME_CMP(given,correct,<=)
+
+#define COMPARE_CHANGE_TIME_CMP(given, correct, cmp) do { \
+       COMPARE_TIME_CMP(given, change_time, correct, change_time, cmp); \
+} while (0)
+#define COMPARE_CHANGE_TIME_EQUAL(given,correct) \
+       COMPARE_CHANGE_TIME_CMP(given,correct,!=)
+#define COMPARE_CHANGE_TIME_GREATER(given,correct) \
+       COMPARE_CHANGE_TIME_CMP(given,correct,<=)
+
+#define COMPARE_CREATE_TIME_CMP(given, correct, cmp) do { \
+       COMPARE_TIME_CMP(given, create_time, correct, create_time, cmp); \
+} while (0)
+#define COMPARE_CREATE_TIME_EQUAL(given,correct) \
+       COMPARE_CREATE_TIME_CMP(given,correct,!=)
+
+#define COMPARE_ALL_TIMES_EQUAL(given,correct) do { \
+       COMPARE_WRITE_TIME_EQUAL(given,correct); \
+       COMPARE_CHANGE_TIME_EQUAL(given,correct); \
+       COMPARE_ACCESS_TIME_EQUAL(given,correct); \
+       COMPARE_CREATE_TIME_EQUAL(given,correct); \
+} while (0)
+
+#define COMPARE_TIMES_AFTER_WRITE(given,correct) do { \
+       COMPARE_WRITE_TIME_GREATER(given,correct); \
+       COMPARE_CHANGE_TIME_GREATER(given,correct); \
+       COMPARE_ACCESS_TIME_EQUAL(given,correct); \
+       COMPARE_CREATE_TIME_EQUAL(given,correct); \
+       COMPARE_TIME_CMP(given, change_time, given, write_time, !=); \
+} while (0)
+
+#define COMPARE_TIMES_AFTER_CLOSE(given,correct) do { \
+       COMPARE_WRITE_TIME_GREATER(given,correct); \
+       COMPARE_CHANGE_TIME_GREATER(given,correct); \
+       COMPARE_ACCESS_TIME_GREATER(given,correct); \
+       COMPARE_CREATE_TIME_EQUAL(given,correct); \
+       COMPARE_TIME_CMP(given, change_time, given, write_time, !=); \
+       COMPARE_TIME_CMP(given, access_time, given, write_time, !=); \
+} while (0)
+
+#define GET_INFO_FILE(tree, finfo) do { \
+       struct timeval _atv; \
+       struct timeval _wtv; \
+       struct timeval_buf _atvb; \
+       struct timeval_buf _wtvb; \
+       NTSTATUS _status; \
+       _status = smb2_getinfo_file(tree, tctx, &finfo); \
+       if (!NT_STATUS_IS_OK(_status)) { \
+               ret = false; \
+               torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", \
+                              nt_errstr(_status)); \
+               goto done; \
+       } \
+       nttime_to_timeval(&_atv, finfo.basic_info.out.access_time); \
+       nttime_to_timeval(&_wtv, finfo.basic_info.out.write_time); \
+       torture_comment(tctx, "fileinfo(%s,%s): Access(%s) Write(%s)\n", \
+                       #tree, #finfo, \
+                       timeval_str_buf(&_atv, false, true, &_atvb), \
+                       timeval_str_buf(&_wtv, false, true, &_wtvb)); \
+} while (0)
+
+#define GET_INFO_BOTH(finfo1, finfo2) do { \
+       GET_INFO_FILE(tree2, finfo2); \
+       GET_INFO_FILE(tree1, finfo1); \
+       COMPARE_ALL_TIMES_EQUAL(finfo1, finfo2); \
+} while (0)
+
+#define SET_INFO_FILE_EX(finfo, wrtime, tree, tfnum) do { \
+       NTSTATUS _status; \
+       union smb_setfileinfo sfinfo; \
+       sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFO; \
+       sfinfo.basic_info.in.file.fnum = tfnum; \
+       sfinfo.basic_info.in.create_time = 0; \
+       sfinfo.basic_info.in.access_time = 0; \
+       unix_to_nt_time(&sfinfo.basic_info.in.write_time, (wrtime)); \
+       sfinfo.basic_info.in.change_time = 0; \
+       sfinfo.basic_info.in.attrib = finfo1.basic_info.out.attrib; \
+       _status = smb_raw_setfileinfo(tree, &sfinfo); \
+       if (!NT_STATUS_IS_OK(_status)) { \
+               torture_result(tctx, TORTURE_FAIL, __location__": setfileinfo failed: %s", \
+                              nt_errstr(_status)); \
+               ret = false; \
+               goto done; \
+       } \
+} while (0)
+#define SET_INFO_FILE(finfo, wrtime) \
+       SET_INFO_FILE_EX(finfo, wrtime, cli->tree, fnum1)
+
+#define SET_INFO_FILE_NS(finfo, wrtime, ns, tree, tfnum) do { \
+       NTSTATUS _status; \
+       union smb_setfileinfo sfinfo; \
+       sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFO; \
+       sfinfo.basic_info.in.file.fnum = tfnum; \
+       sfinfo.basic_info.in.create_time = 0; \
+       sfinfo.basic_info.in.access_time = 0; \
+       unix_to_nt_time(&sfinfo.basic_info.in.write_time, (wrtime)); \
+       sfinfo.basic_info.in.write_time += (ns); \
+       sfinfo.basic_info.in.change_time = 0; \
+       sfinfo.basic_info.in.attrib = finfo1.basic_info.out.attrib; \
+       _status = smb_raw_setfileinfo(tree, &sfinfo); \
+       if (!NT_STATUS_IS_OK(_status)) { \
+               torture_result(tctx, TORTURE_FAIL, __location__": setfileinfo failed: %s", \
+                              nt_errstr(_status)); \
+               ret = false; \
+               goto done; \
+       } \
+} while (0)
+
+struct test_durable_open_delaywrite1_state {
+       struct torture_context *tctx;
+       struct smb2_tree *tree1;
+       struct smb2_tree *tree2;
+       struct smb2_handle *h1;
+       struct smb2_handle *h2;
+};
+
+static bool test_durable_open_delaywrite1_get_info(void *private_data,
+                                                  union smb_fileinfo *finfo)
+{
+       struct test_durable_open_delaywrite1_state *state =
+               (struct test_durable_open_delaywrite1_state *)private_data;
+       struct torture_context *tctx = state->tctx;
+       union smb_fileinfo t1finfo;
+       union smb_fileinfo t2finfo;
+       bool ret = true;
+
+       ZERO_STRUCTP(finfo);
+
+       ZERO_STRUCT(t1finfo);
+       t1finfo.basic_info.level = RAW_FILEINFO_BASIC_INFORMATION;
+       t1finfo.basic_info.in.file.handle = *state->h1;
+
+       ZERO_STRUCT(t2finfo);
+       t2finfo.basic_info.level = RAW_FILEINFO_BASIC_INFORMATION;
+       t2finfo.basic_info.in.file.handle = *state->h2;
+
+       GET_INFO_FILE(state->tree2, t2finfo);
+       GET_INFO_FILE(state->tree1, t1finfo);
+       if (t1finfo.basic_info.out.write_time != t2finfo.basic_info.out.write_time) {
+               /*
+                * There was a race, get it again on handle 2,
+                * but then they have to match.
+                */
+               GET_INFO_FILE(state->tree2, t2finfo);
+       }
+       COMPARE_ALL_TIMES_EQUAL(t1finfo, t2finfo);
+
+       finfo->basic_info.out = t1finfo.basic_info.out;
+done:
+       return ret;
+}
+
+static bool test_durable_open_delaywrite1_write_data(void *private_data)
+{
+       struct test_durable_open_delaywrite1_state *state =
+               (struct test_durable_open_delaywrite1_state *)private_data;
+       struct torture_context *tctx = state->tctx;
+       struct smb2_write wr;
+       NTSTATUS status;
+       bool ret = true;
+
+       ZERO_STRUCT(wr);
+       wr.in.file.handle = *state->h1;
+       wr.in.offset      = 0;
+       wr.in.data        = data_blob_const("x", 1);
+       status = smb2_write(state->tree1, &wr);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       torture_assert_int_equal_goto(tctx, wr.out.nwritten, 1,
+                                     ret, done, "smb2_write");
+
+done:
+       return ret;
+}
+
+static bool test_durable_open_delaywrite1_close(void *private_data,
+                                               union smb_fileinfo *finfo)
+{
+       struct test_durable_open_delaywrite1_state *state =
+               (struct test_durable_open_delaywrite1_state *)private_data;
+       struct torture_context *tctx = state->tctx;
+       union smb_fileinfo t1finfoCL;
+       union smb_fileinfo t2finfoCL;
+       struct smb2_close cl1;
+       struct smb2_close cl2;
+       union smb_fileinfo t2finfo;
+       NTSTATUS status;
+       bool ret = true;
+
+       ZERO_STRUCTP(finfo);
+
+       ZERO_STRUCT(cl1);
+       cl1.in.file.handle = *state->h1;
+       cl1.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
+       status = smb2_close(state->tree1, &cl1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       state->h1 = NULL;
+       ZERO_STRUCT(t1finfoCL);
+       t1finfoCL.basic_info.out.create_time = cl1.out.create_time;
+       t1finfoCL.basic_info.out.access_time = cl1.out.access_time;
+       t1finfoCL.basic_info.out.write_time = cl1.out.write_time;
+       t1finfoCL.basic_info.out.change_time = cl1.out.change_time;
+       t1finfoCL.basic_info.out.attrib = cl1.out.file_attr;
+
+       ZERO_STRUCT(t2finfo);
+       t2finfo.basic_info.level = RAW_FILEINFO_BASIC_INFORMATION;
+       t2finfo.basic_info.in.file.handle = *state->h2;
+
+       GET_INFO_FILE(state->tree2, t2finfo);
+       COMPARE_ALL_TIMES_EQUAL(t2finfo, t1finfoCL);
+
+       ZERO_STRUCT(cl2);
+       cl2.in.file.handle = *state->h2;
+       cl2.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
+       status = smb2_close(state->tree2, &cl2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       state->h2 = NULL;
+       ZERO_STRUCT(t2finfoCL);
+       t2finfoCL.basic_info.out.create_time = cl2.out.create_time;
+       t2finfoCL.basic_info.out.access_time = cl2.out.access_time;
+       t2finfoCL.basic_info.out.write_time = cl2.out.write_time;
+       t2finfoCL.basic_info.out.change_time = cl2.out.change_time;
+       t2finfoCL.basic_info.out.attrib = cl2.out.file_attr;
+       COMPARE_ALL_TIMES_EQUAL(t2finfoCL, t1finfoCL);
+
+       finfo->basic_info.out = t1finfoCL.basic_info.out;
+
+done:
+       return ret;
+}
+
+static bool test_durable_open_delaywrite1(struct torture_context *tctx,
+                                         struct smb2_tree *tree1,
+                                         struct smb2_tree *tree2)
+{
+       struct test_durable_open_delaywrite1_state state = {
+               .tctx = tctx,
+               .tree1 = tree1,
+               .tree2 = tree2,
+       };
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx = talloc_new(tctx);
+       char fname[256];
+       struct smb2_handle _h1;
+       struct smb2_handle _h2;
+       struct smb2_create cr1;
+       struct smb2_create cr2;
+       union smb_fileinfo c1finfoCR, c1finfo0;
+       union smb_fileinfo c2finfoCR, c2finfo0;
+       //double used_delay = torture_setting_int(tctx, "writetimeupdatedelay", 2000000);
+       //double used_delay = torture_setting_int(tctx, "writetimeupdatedelay", 1000000);
+       double normal_delay = 1000000;
+       double used_delay = torture_setting_int(tctx, "writetimeupdatedelay", normal_delay);
+       //double normal_delay = 1000000;
+       //int normal_delay = 2000000;
+       bool ret = true;
+       bool ok;
+
+       /* Choose a random name in case the state is left a little funky. */
+       snprintf(fname, 256, "durable_open_delaywrite1_%s.dat",
+                generate_random_str(tctx, 8));
+
+       smb2_util_unlink(tree1, fname);
+
+       smb2_oplock_create_share(&cr1, fname,
+                                smb2_util_share_access(""),
+                                smb2_util_oplock_level("b"));
+       cr1.in.durable_open = true;
+
+       status = smb2_create(tree1, mem_ctx, &cr1);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       _h1 = cr1.out.file.handle;
+       state.h1 = &_h1;
+       CHECK_CREATED(&cr1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(cr1.out.oplock_level, smb2_util_oplock_level("b"));
+       CHECK_VAL(cr1.out.durable_open, true);
+       CHECK_VAL(cr1.out.durable_open_v2, false);
+       CHECK_VAL(cr1.out.persistent_open, false);
+
+       cr2 = cr1;
+       cr2.in.desired_access = SEC_FILE_READ_ATTRIBUTE;
+       cr2.in.durable_open = false;
+       cr2.in.oplock_level = 0;
+       cr2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       status = smb2_create(tree2, mem_ctx, &cr2);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       _h2 = cr2.out.file.handle;
+       state.h2 = &_h2;
+       CHECK_CREATED(&cr2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
+       CHECK_VAL(cr2.out.oplock_level, 0);
+       CHECK_VAL(cr2.out.durable_open, false);
+       CHECK_VAL(cr2.out.durable_open_v2, false);
+       CHECK_VAL(cr2.out.persistent_open, false);
+
+       ZERO_STRUCT(c1finfoCR);
+       c1finfoCR.basic_info.out.create_time = cr1.out.create_time;
+       c1finfoCR.basic_info.out.access_time = cr1.out.access_time;
+       c1finfoCR.basic_info.out.write_time = cr1.out.write_time;
+       c1finfoCR.basic_info.out.change_time = cr1.out.change_time;
+       c1finfoCR.basic_info.out.attrib = cr1.out.file_attr;
+
+       ZERO_STRUCT(c2finfoCR);
+       c2finfoCR.basic_info.out.create_time = cr2.out.create_time;
+       c2finfoCR.basic_info.out.access_time = cr2.out.access_time;
+       c2finfoCR.basic_info.out.write_time = cr2.out.write_time;
+       c2finfoCR.basic_info.out.change_time = cr2.out.change_time;
+       c2finfoCR.basic_info.out.attrib = cr2.out.file_attr;
+
+       COMPARE_ALL_TIMES_EQUAL(c1finfoCR, c2finfoCR);
+
+       ZERO_STRUCT(c1finfo0);
+       c1finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFORMATION;
+       c1finfo0.basic_info.in.file.handle = *state.h1;
+
+       ZERO_STRUCT(c2finfo0);
+       c2finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFORMATION;
+       c2finfo0.basic_info.in.file.handle = *state.h2;
+
+       GET_INFO_BOTH(c1finfo0, c2finfo0);
+       COMPARE_ALL_TIMES_EQUAL(c1finfo0, c1finfoCR);
+
+       ok = test_delay_writetime1(tctx, used_delay, normal_delay,
+                       "run1",
+                       test_durable_open_delaywrite1_get_info,
+                       test_durable_open_delaywrite1_write_data,
+                       NULL, /* close_cb */
+                       &state);
+       torture_assert_goto(tctx, ok, ret, done, "test_delay_writetime1(1)");
+       ok = test_delay_writetime1(tctx, used_delay, normal_delay,
+                       "run2",
+                       test_durable_open_delaywrite1_get_info,
+                       test_durable_open_delaywrite1_write_data,
+                       test_durable_open_delaywrite1_close,
+                       &state);
+       torture_assert_goto(tctx, ok, ret, done, "test_delay_writetime1(2)");
+
+done:
+       if (state.h1 != NULL) {
+               smb2_util_close(tree1, *state.h1);
+       }
+       if (state.h2 != NULL) {
+               smb2_util_close(tree2, *state.h2);
+       }
+
+       smb2_util_unlink(tree1, fname);
+
+       talloc_free(tree1);
+       talloc_free(tree2);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
 
-struct torture_suite *torture_smb2_durable_open_init(void)
+struct torture_suite *torture_smb2_durable_open_init(TALLOC_CTX *ctx)
 {
        struct torture_suite *suite =
-           torture_suite_create(talloc_autofree_context(), "durable-open");
+           torture_suite_create(ctx, "durable-open");
 
        torture_suite_add_1smb2_test(suite, "open-oplock", test_durable_open_open_oplock);
        torture_suite_add_1smb2_test(suite, "open-lease", test_durable_open_open_lease);
@@ -2787,16 +3187,18 @@ struct torture_suite *torture_smb2_durable_open_init(void)
                                     test_durable_open_alloc_size);
        torture_suite_add_1smb2_test(suite, "read-only",
                                     test_durable_open_read_only);
+       torture_suite_add_2smb2_test(suite, "delaywrite1",
+                                    test_durable_open_delaywrite1);
 
        suite->description = talloc_strdup(suite, "SMB2-DURABLE-OPEN tests");
 
        return suite;
 }
 
-struct torture_suite *torture_smb2_durable_open_disconnect_init(void)
+struct torture_suite *torture_smb2_durable_open_disconnect_init(TALLOC_CTX *ctx)
 {
        struct torture_suite *suite =
-           torture_suite_create(talloc_autofree_context(),
+           torture_suite_create(ctx,
                                 "durable-open-disconnect");
 
        torture_suite_add_1smb2_test(suite, "open-oplock-disconnect",