selftest: Test hide new files timeout
[amitay/samba.git] / source3 / torture / torture.c
index 232e8ff2bbb29c420c8fabde8e58cf8db613f2cc..22810f081b843a10089053459245f6b0c52bed88 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "includes.h"
 #include "system/shmem.h"
+#include "libsmb/namequery.h"
 #include "wbc_async.h"
 #include "torture/proto.h"
 #include "libcli/security/security.h"
@@ -42,6 +43,9 @@
 #include "../libcli/smb/smbXcli_base.h"
 #include "lib/util/sys_rw_data.h"
 #include "lib/util/base64.h"
+#include "lib/util/time.h"
+#include "lib/crypto/md5.h"
+#include "lib/gencache.h"
 
 extern char *optarg;
 extern int optind;
@@ -2157,7 +2161,7 @@ static bool run_locktest5(int dummy)
        ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, False);
 
-       printf("a different processs %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
+       printf("a different process %s get a read lock on the first process lock stack\n", ret?"can":"cannot");
 
        /* Unlock the process 2 lock. */
        cli_unlock(cli2, fnum2, 0, 4);
@@ -2166,7 +2170,7 @@ static bool run_locktest5(int dummy)
        ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, False);
 
-       printf("the same processs on a different fnum %s get a read lock\n", ret?"can":"cannot");
+       printf("the same process on a different fnum %s get a read lock\n", ret?"can":"cannot");
 
        /* Unlock the process 1 fnum3 lock. */
        cli_unlock(cli1, fnum3, 0, 4);
@@ -2209,7 +2213,7 @@ static bool run_locktest5(int dummy)
        ret = NT_STATUS_IS_OK(status);
        EXPECTED(ret, True);
 
-       printf("a different processs %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
+       printf("a different process %s get a write lock on the unlocked stack\n", ret?"can":"cannot");
 
 
  fail:
@@ -4559,6 +4563,78 @@ static bool run_deletetest(int dummy)
        return correct;
 }
 
+/*
+  Exercise delete on close semantics - use on the PRINT1 share in torture
+  testing.
+ */
+static bool run_delete_print_test(int dummy)
+{
+       struct cli_state *cli1 = NULL;
+       const char *fname = "print_delete.file";
+       uint16_t fnum1 = (uint16_t)-1;
+       bool correct = false;
+       const char *buf = "print file data\n";
+       NTSTATUS status;
+
+       printf("starting print delete test\n");
+
+       if (!torture_open_connection(&cli1, 0)) {
+               return false;
+       }
+
+       smbXcli_conn_set_sockopt(cli1->conn, sockops);
+
+       status = cli_ntcreate(cli1, fname, 0, GENERIC_ALL_ACCESS|DELETE_ACCESS,
+                             FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                             0, 0, &fnum1, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("open of %s failed (%s)\n",
+                       fname,
+                       nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_writeall(cli1,
+                       fnum1,
+                       0,
+                       (const uint8_t *)buf,
+                       0, /* offset */
+                       strlen(buf), /* size */
+                       NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("writing print file data failed (%s)\n",
+                       nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_nt_delete_on_close(cli1, fnum1, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("setting delete_on_close failed (%s)\n",
+                       nt_errstr(status));
+               goto fail;
+       }
+
+       status = cli_close(cli1, fnum1);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("close failed (%s)\n", nt_errstr(status));
+               goto fail;
+       }
+
+       printf("finished print delete test\n");
+
+       correct = true;
+
+  fail:
+
+       if (fnum1 != (uint16_t)-1) {
+               cli_close(cli1, fnum1);
+       }
+
+       if (cli1 && !torture_close_connection(cli1)) {
+               correct = false;
+       }
+       return correct;
+}
 
 /*
   Test wildcard delete.
@@ -8045,6 +8121,7 @@ static bool run_chain1(int dummy)
        struct tevent_req *reqs[3], *smbreqs[3];
        bool done = false;
        const char *str = "foobar";
+       const char *fname = "\\test_chain";
        NTSTATUS status;
 
        printf("starting chain1 test\n");
@@ -8054,7 +8131,9 @@ static bool run_chain1(int dummy)
 
        smbXcli_conn_set_sockopt(cli1->conn, sockops);
 
-       reqs[0] = cli_openx_create(talloc_tos(), evt, cli1, "\\test",
+       cli_unlink(cli1, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+       reqs[0] = cli_openx_create(talloc_tos(), evt, cli1, fname,
                                  O_CREAT|O_RDWR, 0, &smbreqs[0]);
        if (reqs[0] == NULL) return false;
        tevent_req_set_callback(reqs[0], chain1_open_completion, NULL);
@@ -9166,6 +9245,157 @@ static bool run_cli_echo(int dummy)
        return NT_STATUS_IS_OK(status);
 }
 
+static int splice_status(off_t written, void *priv)
+{
+        return true;
+}
+
+static bool run_cli_splice(int dummy)
+{
+       uint8_t *buf = NULL;
+       struct cli_state *cli1 = NULL;
+       bool correct = false;
+       const char *fname_src = "\\splice_src.dat";
+       const char *fname_dst = "\\splice_dst.dat";
+       NTSTATUS status;
+       uint16_t fnum1 = UINT16_MAX;
+       uint16_t fnum2 = UINT16_MAX;
+       size_t file_size = 2*1024*1024;
+       size_t splice_size = 1*1024*1024 + 713;
+       MD5_CTX md5_ctx;
+       uint8_t digest1[16], digest2[16];
+       off_t written = 0;
+       size_t nread = 0;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       printf("starting cli_splice test\n");
+
+       if (!torture_open_connection(&cli1, 0)) {
+               goto out;
+       }
+
+       cli_unlink(cli1, fname_src,
+               FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli1, fname_dst,
+               FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+       /* Create a file */
+       status = cli_ntcreate(cli1, fname_src, 0, GENERIC_ALL_ACCESS,
+                       FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                       0, 0, &fnum1, NULL);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("open %s failed: %s\n", fname_src, nt_errstr(status));
+               goto out;
+       }
+
+       /* Write file_size bytes - must be bigger than splice_size. */
+       buf = talloc_zero_array(frame, uint8_t, file_size);
+       if (buf == NULL) {
+               d_printf("talloc_fail\n");
+               goto out;
+       }
+
+       /* Fill it with random numbers. */
+       generate_random_buffer(buf, file_size);
+
+       /* MD5 the first 1MB + 713 bytes. */
+       MD5Init(&md5_ctx);
+       MD5Update(&md5_ctx, buf, splice_size);
+       MD5Final(digest1, &md5_ctx);
+
+       status = cli_writeall(cli1,
+                             fnum1,
+                             0,
+                             buf,
+                             0,
+                             file_size,
+                             NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_writeall failed: %s\n", nt_errstr(status));
+               goto out;
+       }
+
+       status = cli_ntcreate(cli1, fname_dst, 0, GENERIC_ALL_ACCESS,
+                       FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF,
+                       0, 0, &fnum2, NULL);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("open %s failed: %s\n", fname_dst, nt_errstr(status));
+               goto out;
+       }
+
+       /* Now splice 1MB + 713 bytes. */
+       status = cli_splice(cli1,
+                               cli1,
+                               fnum1,
+                               fnum2,
+                               splice_size,
+                               0,
+                               0,
+                               &written,
+                               splice_status,
+                               NULL);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_splice failed: %s\n", nt_errstr(status));
+               goto out;
+       }
+
+       /* Clear the old buffer. */
+       memset(buf, '\0', file_size);
+
+       /* Read the new file. */
+       status = cli_read(cli1, fnum2, (char *)buf, 0, splice_size, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_read failed: %s\n", nt_errstr(status));
+               goto out;
+       }
+       if (nread != splice_size) {
+               d_printf("bad read of 0x%x, should be 0x%x\n",
+                       (unsigned int)nread,
+                       (unsigned int)splice_size);
+               goto out;
+       }
+
+       /* MD5 the first 1MB + 713 bytes. */
+       MD5Init(&md5_ctx);
+       MD5Update(&md5_ctx, buf, splice_size);
+       MD5Final(digest2, &md5_ctx);
+
+       /* Must be the same. */
+       if (memcmp(digest1, digest2, 16) != 0) {
+               d_printf("bad MD5 compare\n");
+               goto out;
+       }
+
+       correct = true;
+       printf("Success on cli_splice test\n");
+
+  out:
+
+       if (cli1) {
+               if (fnum1 != UINT16_MAX) {
+                       cli_close(cli1, fnum1);
+               }
+               if (fnum2 != UINT16_MAX) {
+                       cli_close(cli1, fnum2);
+               }
+
+               cli_unlink(cli1, fname_src,
+                       FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+               cli_unlink(cli1, fname_dst,
+                       FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+
+               if (!torture_close_connection(cli1)) {
+                       correct = false;
+               }
+       }
+
+       TALLOC_FREE(frame);
+       return correct;
+}
+
 static bool run_uid_regression_test(int dummy)
 {
        static struct cli_state *cli;
@@ -9531,8 +9761,8 @@ https://bugzilla.samba.org/show_bug.cgi?id=7084
 static bool run_dir_createtime(int dummy)
 {
        struct cli_state *cli;
-       const char *dname = "\\testdir";
-       const char *fname = "\\testdir\\testfile";
+       const char *dname = "\\testdir_createtime";
+       const char *fname = "\\testdir_createtime\\testfile";
        NTSTATUS status;
        struct timespec create_time;
        struct timespec create_time1;
@@ -9599,9 +9829,9 @@ static bool run_dir_createtime(int dummy)
 static bool run_streamerror(int dummy)
 {
        struct cli_state *cli;
-       const char *dname = "\\testdir";
+       const char *dname = "\\testdir_streamerror";
        const char *streamname =
-               "testdir:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
+               "testdir_streamerror:{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}:$DATA";
        NTSTATUS status;
        time_t change_time, access_time, write_time;
        off_t size;
@@ -9612,7 +9842,7 @@ static bool run_streamerror(int dummy)
                return false;
        }
 
-       cli_unlink(cli, "\\testdir\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
+       cli_unlink(cli, "\\testdir_streamerror\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        cli_rmdir(cli, dname);
 
        status = cli_mkdir(cli, dname);
@@ -9906,6 +10136,54 @@ static bool run_symlink_open_test(int dummy)
        return correct;
 }
 
+/*
+ * Only testing minimal time strings, as the others
+ * need (locale-dependent) guessing at what strftime does and
+ * even may differ in builds.
+ */
+static bool timesubst_test(void)
+{
+       TALLOC_CTX *ctx = NULL;
+       /* Sa 23. Dez 04:33:20 CET 2017 */
+       const struct timeval tv = { 1514000000, 123 };
+       const char* expect_minimal = "20171223_033320";
+       const char* expect_minus   = "20171223_033320_000123";
+       char *s;
+       char *env_tz, *orig_tz = NULL;
+       bool result = true;
+
+       ctx = talloc_new(NULL);
+
+       env_tz = getenv("TZ");
+       if(env_tz) {
+               orig_tz = talloc_strdup(ctx, env_tz);
+       }
+       setenv("TZ", "UTC", 1);
+
+       s = minimal_timeval_string(ctx, &tv, false);
+
+       if(!s || strcmp(s, expect_minimal)) {
+               printf("minimal_timeval_string(ctx, tv, false) returned [%s], expected "
+                      "[%s]\n", s ? s : "<nil>", expect_minimal);
+               result = false;
+       }
+       TALLOC_FREE(s);
+       s = minimal_timeval_string(ctx, &tv, true);
+       if(!s || strcmp(s, expect_minus)) {
+               printf("minimal_timeval_string(ctx, tv, true) returned [%s], expected "
+                      "[%s]\n", s ? s : "<nil>", expect_minus);
+               result = false;
+       }
+       TALLOC_FREE(s);
+
+       if(orig_tz) {
+               setenv("TZ", orig_tz, 1);
+       }
+
+       TALLOC_FREE(ctx);
+       return result;
+}
+
 static bool run_local_substitute(int dummy)
 {
        bool ok = true;
@@ -9918,6 +10196,10 @@ static bool run_local_substitute(int dummy)
        ok &= subst_test("%G", "", "", -1, 0, gidtoname(0));
        ok &= subst_test("%D%u", "u", "dom", -1, 0, "domu");
        ok &= subst_test("%i %I", "", "", -1, -1, "0.0.0.0 0.0.0.0");
+       ok &= subst_test("%j %J", "", "", -1, -1, "0_0_0_0 0_0_0_0");
+       /* Substitution depends on current time, so better test the underlying
+          formatting function. At least covers %t. */
+       ok &= timesubst_test();
 
        /* Different captialization rules in sub_basic... */
 
@@ -9960,7 +10242,9 @@ static bool run_local_base64(int dummy)
        return ret;
 }
 
-static void parse_fn(time_t timeout, DATA_BLOB blob, void *private_data)
+static void parse_fn(const struct gencache_timeout *t,
+                    DATA_BLOB blob,
+                    void *private_data)
 {
        return;
 }
@@ -11257,7 +11541,7 @@ static bool run_local_canonicalize_path(int dummy)
                }
                if (strcmp(d, dst[i]) != 0) {
                        d_fprintf(stderr,
-                               "canonicalize missmatch %s -> %s != %s",
+                               "canonicalize mismatch %s -> %s != %s",
                                src[i], d, dst[i]);
                        return false;
                }
@@ -11497,6 +11781,7 @@ static struct {
        {"RENAME-ACCESS", run_rename_access, 0},
        {"OWNER-RIGHTS", run_owner_rights, 0},
        {"DELETE", run_deletetest, 0},
+       {"DELETE-PRINT", run_delete_print_test, 0},
        {"WILDDELETE", run_wild_deletetest, 0},
        {"DELETE-LN", run_deletetest_ln, 0},
        {"PROPERTIES", run_properties, 0},
@@ -11524,6 +11809,7 @@ static struct {
        { "NTTRANS-CREATE", run_nttrans_create, 0},
        { "NTTRANS-FSCTL", run_nttrans_fsctl, 0},
        { "CLI_ECHO", run_cli_echo, 0},
+       { "CLI_SPLICE", run_cli_splice, 0},
        { "TLDAP", run_tldap },
        { "STREAMERROR", run_streamerror },
        { "NOTIFY-BENCH", run_notify_bench },
@@ -11535,11 +11821,13 @@ static struct {
        { "NOTIFY-ONLINE", run_notify_online },
        { "SMB2-BASIC", run_smb2_basic },
        { "SMB2-NEGPROT", run_smb2_negprot },
+       { "SMB2-ANONYMOUS", run_smb2_anonymous },
        { "SMB2-SESSION-RECONNECT", run_smb2_session_reconnect },
        { "SMB2-TCON-DEPENDENCE", run_smb2_tcon_dependence },
        { "SMB2-MULTI-CHANNEL", run_smb2_multi_channel },
        { "SMB2-SESSION-REAUTH", run_smb2_session_reauth },
        { "SMB2-FTRUNCATE", run_smb2_ftruncate },
+       { "SMB2-DIR-FSYNC", run_smb2_dir_fsync },
        { "CLEANUP1", run_cleanup1 },
        { "CLEANUP2", run_cleanup2 },
        { "CLEANUP3", run_cleanup3 },
@@ -11585,10 +11873,12 @@ static struct {
        { "LOCAL-G-LOCK3", run_g_lock3, 0 },
        { "LOCAL-G-LOCK4", run_g_lock4, 0 },
        { "LOCAL-G-LOCK5", run_g_lock5, 0 },
+       { "LOCAL-G-LOCK6", run_g_lock6, 0 },
        { "LOCAL-G-LOCK-PING-PONG", run_g_lock_ping_pong, 0 },
        { "LOCAL-CANONICALIZE-PATH", run_local_canonicalize_path, 0 },
        { "LOCAL-NAMEMAP-CACHE1", run_local_namemap_cache1, 0 },
        { "qpathinfo-bufsize", run_qpathinfo_bufsize, 0 },
+       { "hide-new-files-timeout", run_hidenewfiles, 0 },
        {NULL, NULL, 0}};
 
 /****************************************************************************