s3: Change sockaddr util function names for consistency
[samba.git] / source3 / torture / torture.c
index ad57470c61001cdea818aa602bdf6d1ffb53e1e1..382b3b65ebc4aeaf372ce72b8f7460f6c0571816 100644 (file)
@@ -37,6 +37,7 @@ static const char *client_txt = "client_oplocks.txt";
 static bool use_kerberos;
 static fstring multishare_conn_fname;
 static bool use_multishare_conn = False;
+static bool do_encrypt;
 
 bool torture_showall = False;
 
@@ -95,6 +96,57 @@ void *shm_setup(int size)
        return ret;
 }
 
+/********************************************************************
+ Ensure a connection is encrypted.
+********************************************************************/
+
+static bool force_cli_encryption(struct cli_state *c,
+                       const char *sharename)
+{
+       uint16 major, minor;
+       uint32 caplow, caphigh;
+       NTSTATUS status;
+
+       if (!SERVER_HAS_UNIX_CIFS(c)) {
+               d_printf("Encryption required and "
+                       "server that doesn't support "
+                       "UNIX extensions - failing connect\n");
+                       return false;
+       }
+
+       if (!cli_unix_extensions_version(c, &major, &minor, &caplow, &caphigh)) {
+               d_printf("Encryption required and "
+                       "can't get UNIX CIFS extensions "
+                       "version from server.\n");
+               return false;
+       }
+
+       if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
+               d_printf("Encryption required and "
+                       "share %s doesn't support "
+                       "encryption.\n", sharename);
+               return false;
+       }
+
+       if (c->use_kerberos) {
+               status = cli_gss_smb_encryption_start(c);
+       } else {
+               status = cli_raw_ntlm_smb_encryption_start(c,
+                                               username,
+                                               password,
+                                               workgroup);
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Encryption required and "
+                       "setup failed with error %s.\n",
+                       nt_errstr(status));
+               return false;
+       }
+
+       return true;
+}
+
 
 static struct cli_state *open_nbt_connection(void)
 {
@@ -106,7 +158,7 @@ static struct cli_state *open_nbt_connection(void)
        make_nmb_name(&calling, myname, 0x0);
        make_nmb_name(&called , host, 0x20);
 
-        zero_addr(&ss);
+        zero_sockaddr(&ss);
 
        if (!(c = cli_initialise())) {
                printf("Failed initialize cli_struct to connect with %s\n", host);
@@ -235,16 +287,11 @@ static bool torture_open_connection_share(struct cli_state **c,
        if (use_level_II_oplocks) (*c)->use_level_II_oplocks = True;
        (*c)->timeout = 120000; /* set a really long timeout (2 minutes) */
 
-       return True;
-}
-
-void torture_open_connection_free_unclist(char **unc_list)
-{
-       if (unc_list!=NULL)
-       {
-               SAFE_FREE(unc_list[0]);
-               SAFE_FREE(unc_list);
+       if (do_encrypt) {
+               return force_cli_encryption(*c,
+                                       sharename);
        }
+       return True;
 }
 
 bool torture_open_connection(struct cli_state **c, int conn_index)
@@ -255,7 +302,7 @@ bool torture_open_connection(struct cli_state **c, int conn_index)
 
        if (use_multishare_conn==True) {
                char *h, *s;
-               unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0);
+               unc_list = file_lines_load(multishare_conn_fname, &num_unc_names, 0, NULL);
                if (!unc_list || num_unc_names <= 0) {
                        printf("Failed to load unc names list from '%s'\n", multishare_conn_fname);
                        exit(1);
@@ -265,14 +312,14 @@ bool torture_open_connection(struct cli_state **c, int conn_index)
                                      NULL, &h, &s)) {
                        printf("Failed to parse UNC name %s\n",
                               unc_list[conn_index % num_unc_names]);
-                       torture_open_connection_free_unclist(unc_list);
+                       TALLOC_FREE(unc_list);
                        exit(1);
                }
 
                result = torture_open_connection_share(c, h, s);
 
                /* h, s were copied earlier */
-               torture_open_connection_free_unclist(unc_list);
+               TALLOC_FREE(unc_list);
                return result;
        }
 
@@ -834,6 +881,7 @@ static bool run_netbench(int client)
        }
 
        while (fgets(line, sizeof(line)-1, f)) {
+               char *saveptr;
                line_count++;
 
                line[strlen(line)-1] = 0;
@@ -843,9 +891,9 @@ static bool run_netbench(int client)
                all_string_sub(line,"client1", cname, sizeof(line));
 
                /* parse the command parameters */
-               params[0] = strtok(line," ");
+               params[0] = strtok_r(line, " ", &saveptr);
                i = 0;
-               while (params[i]) params[++i] = strtok(NULL," ");
+               while (params[i]) params[++i] = strtok_r(NULL, " ", &saveptr);
 
                params[i] = "";
 
@@ -3628,7 +3676,8 @@ static bool run_rename(int dummy)
        }
 
        if (!cli_rename(cli1, fname, fname1)) {
-               printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have failed ! \n");
+               printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
+                       cli_errstr(cli1));
                correct = False;
        } else {
                printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
@@ -4839,6 +4888,117 @@ static bool subst_test(const char *str, const char *user, const char *domain,
        return result;
 }
 
+static void chain1_open_completion(struct async_req *req)
+{
+       int fnum;
+       NTSTATUS status;
+
+       status = cli_open_recv(req, &fnum);
+       TALLOC_FREE(req);
+
+       d_printf("cli_open_recv returned %s: %d\n",
+                nt_errstr(status),
+                NT_STATUS_IS_OK(status) ? fnum : -1);
+}
+
+static void chain1_read_completion(struct async_req *req)
+{
+       NTSTATUS status;
+       ssize_t received;
+       uint8_t *rcvbuf;
+
+       status = cli_read_andx_recv(req, &received, &rcvbuf);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(req);
+               d_printf("cli_read_andx_recv returned %s\n",
+                        nt_errstr(status));
+               return;
+       }
+
+       d_printf("got %d bytes: %.*s\n", (int)received, (int)received,
+                (char *)rcvbuf);
+       TALLOC_FREE(req);
+}
+
+static void chain1_close_completion(struct async_req *req)
+{
+       NTSTATUS status;
+
+       status = cli_close_recv(req);
+       *((bool *)(req->async.priv)) = true;
+
+       TALLOC_FREE(req);
+
+       d_printf("cli_close returned %s\n", nt_errstr(status));
+}
+
+static bool run_chain1(int dummy)
+{
+       struct cli_state *cli1;
+       struct event_context *evt = event_context_init(NULL);
+       struct async_req *reqs[4];
+       bool done = false;
+
+       printf("starting chain1 test\n");
+       if (!torture_open_connection(&cli1, 0)) {
+               return False;
+       }
+
+       cli_sockopt(cli1, sockops);
+
+       cli_chain_cork(cli1, evt, 0);
+       reqs[0] = cli_open_send(talloc_tos(), evt, cli1, "\\test",
+                               O_CREAT|O_RDWR, 0);
+       reqs[0]->async.fn = chain1_open_completion;
+       reqs[1] = cli_read_andx_send(talloc_tos(), evt, cli1, 0, 0, 10);
+       reqs[1]->async.fn = chain1_read_completion;
+       reqs[2] = cli_read_andx_send(talloc_tos(), evt, cli1, 0, 1, 10);
+       reqs[2]->async.fn = chain1_read_completion;
+       reqs[3] = cli_close_send(talloc_tos(), evt, cli1, 0);
+       reqs[3]->async.fn = chain1_close_completion;
+       reqs[3]->async.priv = (void *)&done;
+       cli_chain_uncork(cli1);
+
+       while (!done) {
+               event_loop_once(evt);
+       }
+
+       torture_close_connection(cli1);
+       return True;
+}
+
+static bool run_cli_echo(int dummy)
+{
+       struct cli_state *cli;
+       struct event_context *ev = event_context_init(NULL);
+       struct async_req *req;
+       NTSTATUS status;
+
+       printf("starting chain1 test\n");
+       if (!torture_open_connection(&cli, 0)) {
+               return false;
+       }
+       cli_sockopt(cli, sockops);
+
+       req = cli_echo_send(ev, ev, cli, 5, data_blob_const("hello", 5));
+       if (req == NULL) {
+               d_printf("cli_echo_send failed\n");
+               return false;
+       }
+
+       while (req->state < ASYNC_REQ_DONE) {
+               event_loop_once(ev);
+       }
+
+       status = cli_echo_recv(req);
+       d_printf("cli_echo returned %s\n", nt_errstr(status));
+
+       TALLOC_FREE(req);
+
+       torture_close_connection(cli);
+       return NT_STATUS_IS_OK(status);
+}
+
 static bool run_local_substitute(int dummy)
 {
        bool ok = true;
@@ -4906,7 +5066,7 @@ static bool run_local_gencache(int dummy)
                return False;
        }
 
-       blob = data_blob_string_const("bar");
+       blob = data_blob_string_const_null("bar");
        tm = time(NULL);
 
        if (!gencache_set_data_blob("foo", &blob, tm)) {
@@ -5042,6 +5202,180 @@ static bool run_local_rbtree(int dummy)
        return ret;
 }
 
+static bool test_stream_name(const char *fname, const char *expected_base,
+                            const char *expected_stream,
+                            NTSTATUS expected_status)
+{
+       NTSTATUS status;
+       char *base = NULL;
+       char *stream = NULL;
+
+       status = split_ntfs_stream_name(talloc_tos(), fname, &base, &stream);
+       if (!NT_STATUS_EQUAL(status, expected_status)) {
+               goto error;
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return true;
+       }
+
+       if (base == NULL) goto error;
+
+       if (strcmp(expected_base, base) != 0) goto error;
+
+       if ((expected_stream != NULL) && (stream == NULL)) goto error;
+       if ((expected_stream == NULL) && (stream != NULL)) goto error;
+
+       if ((stream != NULL) && (strcmp(expected_stream, stream) != 0))
+               goto error;
+
+       TALLOC_FREE(base);
+       TALLOC_FREE(stream);
+       return true;
+
+ error:
+       d_fprintf(stderr, "test_stream(%s, %s, %s, %s)\n",
+                 fname, expected_base ? expected_base : "<NULL>",
+                 expected_stream ? expected_stream : "<NULL>",
+                 nt_errstr(expected_status));
+       d_fprintf(stderr, "-> base=%s, stream=%s, status=%s\n",
+                 base ? base : "<NULL>", stream ? stream : "<NULL>",
+                 nt_errstr(status));
+       TALLOC_FREE(base);
+       TALLOC_FREE(stream);
+       return false;
+}
+
+static bool run_local_stream_name(int dummy)
+{
+       bool ret = true;
+
+       ret &= test_stream_name(
+               "bla", "bla", NULL, NT_STATUS_OK);
+       ret &= test_stream_name(
+               "bla::$DATA", "bla", NULL, NT_STATUS_OK);
+       ret &= test_stream_name(
+               "bla:blub:", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
+       ret &= test_stream_name(
+               "bla::", NULL, NULL, NT_STATUS_OBJECT_NAME_INVALID);
+       ret &= test_stream_name(
+               "bla::123", "bla", NULL, NT_STATUS_OBJECT_NAME_INVALID);
+       ret &= test_stream_name(
+               "bla:$DATA", "bla", "$DATA:$DATA", NT_STATUS_OK);
+       ret &= test_stream_name(
+               "bla:x:$DATA", "bla", "x:$DATA", NT_STATUS_OK);
+       ret &= test_stream_name(
+               "bla:x", "bla", "x:$DATA", NT_STATUS_OK);
+
+       return ret;
+}
+
+static bool data_blob_equal(DATA_BLOB a, DATA_BLOB b)
+{
+       if (a.length != b.length) {
+               printf("a.length=%d != b.length=%d\n",
+                      (int)a.length, (int)b.length);
+               return false;
+       }
+       if (memcmp(a.data, b.data, a.length) != 0) {
+               printf("a.data and b.data differ\n");
+               return false;
+       }
+       return true;
+}
+
+static bool run_local_memcache(int dummy)
+{
+       struct memcache *cache;
+       DATA_BLOB k1, k2;
+       DATA_BLOB d1, d2, d3;
+       DATA_BLOB v1, v2, v3;
+
+       TALLOC_CTX *mem_ctx;
+       char *str1, *str2;
+       size_t size1, size2;
+       bool ret = false;
+
+       cache = memcache_init(NULL, 100);
+
+       if (cache == NULL) {
+               printf("memcache_init failed\n");
+               return false;
+       }
+
+       d1 = data_blob_const("d1", 2);
+       d2 = data_blob_const("d2", 2);
+       d3 = data_blob_const("d3", 2);
+
+       k1 = data_blob_const("d1", 2);
+       k2 = data_blob_const("d2", 2);
+
+       memcache_add(cache, STAT_CACHE, k1, d1);
+       memcache_add(cache, GETWD_CACHE, k2, d2);
+
+       if (!memcache_lookup(cache, STAT_CACHE, k1, &v1)) {
+               printf("could not find k1\n");
+               return false;
+       }
+       if (!data_blob_equal(d1, v1)) {
+               return false;
+       }
+
+       if (!memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
+               printf("could not find k2\n");
+               return false;
+       }
+       if (!data_blob_equal(d2, v2)) {
+               return false;
+       }
+
+       memcache_add(cache, STAT_CACHE, k1, d3);
+
+       if (!memcache_lookup(cache, STAT_CACHE, k1, &v3)) {
+               printf("could not find replaced k1\n");
+               return false;
+       }
+       if (!data_blob_equal(d3, v3)) {
+               return false;
+       }
+
+       memcache_add(cache, GETWD_CACHE, k1, d1);
+
+       if (memcache_lookup(cache, GETWD_CACHE, k2, &v2)) {
+               printf("Did find k2, should have been purged\n");
+               return false;
+       }
+
+       TALLOC_FREE(cache);
+
+       cache = memcache_init(NULL, 0);
+
+       mem_ctx = talloc_init("foo");
+
+       str1 = talloc_strdup(mem_ctx, "string1");
+       str2 = talloc_strdup(mem_ctx, "string2");
+
+       memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
+                           data_blob_string_const("torture"), &str1);
+       size1 = talloc_total_size(cache);
+
+       memcache_add_talloc(cache, SINGLETON_CACHE_TALLOC,
+                           data_blob_string_const("torture"), &str2);
+       size2 = talloc_total_size(cache);
+
+       printf("size1=%d, size2=%d\n", (int)size1, (int)size2);
+
+       if (size2 > size1) {
+               printf("memcache leaks memory!\n");
+               goto fail;
+       }
+
+       ret = true;
+ fail:
+       TALLOC_FREE(cache);
+       return ret;
+}
+
 static double create_procs(bool (*fn)(int), bool *result)
 {
        int i, status;
@@ -5193,9 +5527,13 @@ static struct {
        {"FDSESS", run_fdsesstest, 0},
        { "EATEST", run_eatest, 0},
        { "SESSSETUP_BENCH", run_sesssetup_bench, 0},
+       { "CHAIN1", run_chain1, 0},
+       { "CLI_ECHO", run_cli_echo, 0},
        { "LOCAL-SUBSTITUTE", run_local_substitute, 0},
        { "LOCAL-GENCACHE", run_local_gencache, 0},
        { "LOCAL-RBTREE", run_local_rbtree, 0},
+       { "LOCAL-MEMCACHE", run_local_memcache, 0},
+       { "LOCAL-STREAM-NAME", run_local_stream_name, 0},
        {NULL, NULL, 0}};
 
 
@@ -5300,6 +5638,7 @@ static void usage(void)
        int gotpass = 0;
        bool correct = True;
        TALLOC_CTX *frame = talloc_stackframe();
+       int seed = time(NULL);
 
        dbf = x_stdout;
 
@@ -5332,7 +5671,7 @@ static void usage(void)
        *p = 0;
        fstrcpy(share, p+1);
 
-       fstrcpy(myname, get_myname(talloc_tos()));
+       fstrcpy(myname, talloc_get_myname(talloc_tos()));
        if (!*myname) {
                fprintf(stderr, "Failed to get my hostname.\n");
                return 1;
@@ -5345,17 +5684,15 @@ static void usage(void)
        argc--;
        argv++;
 
-       srandom(time(NULL));
-
        fstrcpy(workgroup, lp_workgroup());
 
-       while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ld:Ac:ks:b:")) != EOF) {
+       while ((opt = getopt(argc, argv, "p:hW:U:n:N:O:o:m:Ld:Aec:ks:b:")) != EOF) {
                switch (opt) {
                case 'p':
                        port_to_use = atoi(optarg);
                        break;
                case 's':
-                       srandom(atoi(optarg));
+                       seed = atoi(optarg);
                        break;
                case 'W':
                        fstrcpy(workgroup,optarg);
@@ -5387,6 +5724,9 @@ static void usage(void)
                case 'c':
                        client_txt = optarg;
                        break;
+               case 'e':
+                       do_encrypt = true;
+                       break;
                case 'k':
 #ifdef HAVE_KRB5
                        use_kerberos = True;
@@ -5415,6 +5755,10 @@ static void usage(void)
                }
        }
 
+       d_printf("using seed %d\n", seed);
+
+       srandom(seed);
+
        if(use_kerberos && !gotuser) gotpass = True;
 
        while (!gotpass) {