s4:torture/smb2: replace torture:cn_max_buffer_size option with the negotiated max...
[samba.git] / source4 / torture / smbtorture.c
index cfa8b0f87db2a2df8f9b6471371bd3cd57d4c96a..018f39f00d9ee42e6000ce6cf09f9f71cd76b30a 100644 (file)
@@ -32,8 +32,9 @@
 #include "librpc/rpc/dcerpc.h"
 #include "auth/gensec/gensec.h"
 #include "param/param.h"
+#include "lib/util/samba_modules.h"
 
-#if HAVE_READLINE_HISTORY_H
+#ifdef HAVE_READLINE_HISTORY_H
 #include <readline/history.h>
 #endif
 
@@ -149,7 +150,9 @@ bool torture_run_named_tests(struct torture_context *torture, const char *name,
        return ret;
 }
 
-bool torture_parse_target(struct loadparm_context *lp_ctx, const char *target)
+bool torture_parse_target(TALLOC_CTX *ctx,
+                               struct loadparm_context *lp_ctx,
+                               const char *target)
 {
        char *host = NULL, *share = NULL;
        struct dcerpc_binding *binding_struct;
@@ -157,12 +160,20 @@ bool torture_parse_target(struct loadparm_context *lp_ctx, const char *target)
 
        /* see if its a RPC transport specifier */
        if (!smbcli_parse_unc(target, NULL, &host, &share)) {
-               status = dcerpc_parse_binding(talloc_autofree_context(), target, &binding_struct);
+               const char *h;
+
+               status = dcerpc_parse_binding(ctx, target, &binding_struct);
                if (NT_STATUS_IS_ERR(status)) {
                        d_printf("Invalid option: %s is not a valid torture target (share or binding string)\n\n", target);
                        return false;
                }
-               lpcfg_set_cmdline(lp_ctx, "torture:host", binding_struct->host);
+
+               h = dcerpc_binding_get_string_option(binding_struct, "host");
+               host = discard_const_p(char, h);
+               if (host != NULL) {
+                       lpcfg_set_cmdline(lp_ctx, "torture:host", host);
+               }
+
                if (lpcfg_parm_string(lp_ctx, NULL, "torture", "share") == NULL)
                        lpcfg_set_cmdline(lp_ctx, "torture:share", "IPC$");
                lpcfg_set_cmdline(lp_ctx, "torture:binding", target);
@@ -285,7 +296,7 @@ void torture_print_testsuites(bool structured)
        }
 }
 
-_NORETURN_ static void usage(poptContext pc)
+static void usage(poptContext pc)
 {
        poptPrintUsage(pc, stdout, 0);
        printf("\n");
@@ -341,7 +352,6 @@ _NORETURN_ static void usage(poptContext pc)
 
        print_structured_testsuite_list();
 
-       exit(1);
 }
 
 _NORETURN_ static void max_runtime_handler(int sig)
@@ -350,73 +360,10 @@ _NORETURN_ static void max_runtime_handler(int sig)
        exit(1);
 }
 
-struct timeval last_suite_started;
-
-static void simple_suite_start(struct torture_context *ctx,
-                              struct torture_suite *suite)
-{
-       last_suite_started = timeval_current();
-       printf("Running %s\n", suite->name);
-}
-
-static void simple_suite_finish(struct torture_context *ctx,
-                               struct torture_suite *suite)
-{
-
-       printf("%s took %g secs\n\n", suite->name, 
-                  timeval_elapsed(&last_suite_started));
-}
-
-static void simple_test_result(struct torture_context *context, 
-                              enum torture_result res, const char *reason)
-{
-       switch (res) {
-       case TORTURE_OK:
-               if (reason)
-                       printf("OK: %s\n", reason);
-               break;
-       case TORTURE_FAIL:
-               printf("TEST %s FAILED! - %s\n", context->active_test->name, reason);
-               break;
-       case TORTURE_ERROR:
-               printf("ERROR IN TEST %s! - %s\n", context->active_test->name, reason); 
-               break;
-       case TORTURE_SKIP:
-               printf("SKIP: %s - %s\n", context->active_test->name, reason);
-               break;
-       }
-}
-
-static void simple_comment(struct torture_context *test, 
-                          const char *comment)
-{
-       printf("%s", comment);
-}
-
-static void simple_warning(struct torture_context *test, 
-                          const char *comment)
-{
-       fprintf(stderr, "WARNING: %s\n", comment);
-}
-
-static void simple_progress(struct torture_context *test,
-       int offset, enum torture_progress_whence whence)
-{
-}
-
-const static struct torture_ui_ops std_ui_ops = {
-       .comment = simple_comment,
-       .warning = simple_warning,
-       .suite_start = simple_suite_start,
-       .suite_finish = simple_suite_finish,
-       .test_result = simple_test_result,
-       .progress = simple_progress,
-};
-
 /****************************************************************************
   main program
 ****************************************************************************/
-int main(int argc,char *argv[])
+int main(int argc, const char *argv[])
 {
        int opt, i;
        bool correct = true;
@@ -436,12 +383,13 @@ int main(int argc,char *argv[])
        const char *extra_module = NULL;
        static int list_tests = 0, list_testsuites = 0;
        int num_extra_users = 0;
-       char **restricted = NULL;
+       const char **restricted = NULL;
        int num_restricted = -1;
        const char *load_list = NULL;
        enum {OPT_LOADFILE=1000,OPT_UNCLIST,OPT_TIMELIMIT,OPT_DNS, OPT_LIST,
              OPT_DANGEROUS,OPT_SMB_PORTS,OPT_ASYNC,OPT_NUMPROGS,
              OPT_EXTRA_USER,};
+       TALLOC_CTX *mem_ctx = NULL;
 
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -483,10 +431,18 @@ int main(int argc,char *argv[])
 
        setlinebuf(stdout);
 
+       mem_ctx = talloc_named_const(NULL, 0, "torture_ctx");
+       if (mem_ctx == NULL) {
+               printf("Unable to allocate torture_ctx\n");
+               exit(1);
+       }
+
+       printf("smbtorture %s\n", samba_version_string());
+
        /* we are never interested in SIGPIPE */
        BlockSignals(true, SIGPIPE);
 
-       pc = poptGetContext("smbtorture", argc, (const char **) argv, long_options, 
+       pc = poptGetContext("smbtorture", argc, argv, long_options,
                            POPT_CONTEXT_KEEP_FIRST);
 
        poptSetOtherOptionHelp(pc, "<binding>|<unc> TEST1 TEST2 ...");
@@ -519,9 +475,15 @@ int main(int argc,char *argv[])
                        break;
                case OPT_EXTRA_USER:
                        {
-                               char *option = talloc_asprintf(NULL, "torture:extra_user%u",
-                                                              ++num_extra_users);
+                               char *option = talloc_asprintf(mem_ctx,
+                                               "torture:extra_user%u",
+                                               ++num_extra_users);
                                const char *value = poptGetOptArg(pc);
+                               if (option == NULL) {
+                                       printf("talloc fail\n");
+                                       talloc_free(mem_ctx);
+                                       exit(1);
+                               }
                                lpcfg_set_cmdline(cmdline_lp_ctx, option, value);
                                talloc_free(option);
                        }
@@ -529,16 +491,19 @@ int main(int argc,char *argv[])
                default:
                        if (opt < 0) {
                                printf("bad command line option %d\n", opt);
+                               talloc_free(mem_ctx);
                                exit(1);
                        }
                }
        }
 
        if (load_list != NULL) {
-               restricted = file_lines_load(load_list, &num_restricted, 0,
-                                                                        talloc_autofree_context());
+               char **r;
+               r = file_lines_load(load_list, &num_restricted, 0, mem_ctx);
+               restricted = discard_const_p(const char *, r);
                if (restricted == NULL) {
                        printf("Unable to read load list file '%s'\n", load_list);
+                       talloc_free(mem_ctx);
                        exit(1);
                }
        }
@@ -548,6 +513,9 @@ int main(int argc,char *argv[])
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:resume_key_support", "false");
        } else if (strcmp(target, "samba4") == 0) {
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:samba4", "true");
+       } else if (strcmp(target, "samba4-ntvfs") == 0) {
+               lpcfg_set_cmdline(cmdline_lp_ctx, "torture:samba4", "true");
+               lpcfg_set_cmdline(cmdline_lp_ctx, "torture:samba4-ntvfs", "true");
        } else if (strcmp(target, "winxp") == 0) {
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:winxp", "true");
        } else if (strcmp(target, "w2k3") == 0) {
@@ -556,10 +524,10 @@ int main(int argc,char *argv[])
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:w2k8", "true");
                lpcfg_set_cmdline(cmdline_lp_ctx,
                    "torture:invalid_lock_range_support", "false");
+       } else if (strcmp(target, "w2k12") == 0) {
+               lpcfg_set_cmdline(cmdline_lp_ctx, "torture:w2k12", "true");
        } else if (strcmp(target, "win7") == 0) {
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:win7", "true");
-               lpcfg_set_cmdline(cmdline_lp_ctx, "torture:cn_max_buffer_size",
-                   "0x00010000");
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:resume_key_support", "false");
                lpcfg_set_cmdline(cmdline_lp_ctx, "torture:rewind_support", "false");
 
@@ -602,23 +570,25 @@ int main(int argc,char *argv[])
        }
 
        if (extra_module != NULL) {
-           init_module_fn fn = load_module(talloc_autofree_context(), poptGetOptArg(pc));
+               init_module_fn fn = load_module(poptGetOptArg(pc), false, NULL);
 
                if (fn == NULL) 
                        d_printf("Unable to load module from %s\n", poptGetOptArg(pc));
                else {
-                       status = fn();
+                       status = fn(mem_ctx);
                        if (NT_STATUS_IS_ERR(status)) {
                                d_printf("Error initializing module %s: %s\n", 
                                        poptGetOptArg(pc), nt_errstr(status));
                        }
                }
        } else { 
-               torture_init();
+               torture_init(mem_ctx);
        }
 
        if (list_testsuites) {
                print_testsuite_list();
+               talloc_free(mem_ctx);
+               popt_free_cmdline_credentials();
                return 0;
        }
 
@@ -640,6 +610,8 @@ int main(int argc,char *argv[])
                                print_test_list(torture_root, NULL, argv_new[i]);
                        }
                }
+               talloc_free(mem_ctx);
+               popt_free_cmdline_credentials();
                return 0;
        }
 
@@ -650,21 +622,23 @@ int main(int argc,char *argv[])
        srandom(torture_seed);
 
        if (!strcmp(ui_ops_name, "simple")) {
-               ui_ops = &std_ui_ops;
+               ui_ops = &torture_simple_ui_ops;
        } else if (!strcmp(ui_ops_name, "subunit")) {
                ui_ops = &torture_subunit_ui_ops;
        } else {
                printf("Unknown output format '%s'\n", ui_ops_name);
+               talloc_free(mem_ctx);
                exit(1);
        }
 
-       results = torture_results_init(talloc_autofree_context(), ui_ops);
+       results = torture_results_init(mem_ctx, ui_ops);
 
-       torture = torture_context_init(s4_event_context_init(talloc_autofree_context()),
+       torture = torture_context_init(s4_event_context_init(mem_ctx),
                                       results);
        if (basedir != NULL) {
                if (basedir[0] != '/') {
                        fprintf(stderr, "Please specify an absolute path to --basedir\n");
+                       talloc_free(mem_ctx);
                        return 1;
                }
                outputdir = talloc_asprintf(torture, "%s/smbtortureXXXXXX", basedir);
@@ -672,22 +646,26 @@ int main(int argc,char *argv[])
                char *pwd = talloc_size(torture, PATH_MAX);
                if (!getcwd(pwd, PATH_MAX)) {
                        fprintf(stderr, "Unable to determine current working directory\n");
+                       talloc_free(mem_ctx);
                        return 1;
                }
                outputdir = talloc_asprintf(torture, "%s/smbtortureXXXXXX", pwd);
        }
        if (!outputdir) {
                fprintf(stderr, "Could not allocate per-run output dir\n");
+               talloc_free(mem_ctx);
                return 1;
        }
        torture->outputdir = mkdtemp(outputdir);
        if (!torture->outputdir) {
                perror("Failed to make temp output dir");
+               talloc_free(mem_ctx);
+               return 1;
        }
 
        torture->lp_ctx = cmdline_lp_ctx;
 
-       gensec_init(cmdline_lp_ctx);
+       gensec_init();
 
        if (shell) {
                /* In shell mode, just ignore any remaining test names. */
@@ -702,19 +680,18 @@ int main(int argc,char *argv[])
                if (argc_new < 3) {
                        printf("You must specify a test to run, or 'ALL'\n");
                        usage(pc);
-                       exit(1);
-               }
-
-               /* Take the target name or binding. */
-               if (!torture_parse_target(cmdline_lp_ctx, argv_new[1])) {
+                       torture->results->returncode = 1;
+               } else if (!torture_parse_target(torture,
+                                       cmdline_lp_ctx, argv_new[1])) {
+                       /* Take the target name or binding. */
                        usage(pc);
-                       exit(1);
-               }
-
-               for (i=2;i<argc_new;i++) {
-                       if (!torture_run_named_tests(torture, argv_new[i],
-                                   (const char **)restricted)) {
-                               correct = false;
+                       torture->results->returncode = 1;
+               } else {
+                       for (i=2;i<argc_new;i++) {
+                               if (!torture_run_named_tests(torture, argv_new[i],
+                                           (const char **)restricted)) {
+                                       correct = false;
+                               }
                        }
                }
        }
@@ -723,8 +700,11 @@ int main(int argc,char *argv[])
        torture_deltree_outputdir(torture);
 
        if (torture->results->returncode && correct) {
+               talloc_free(mem_ctx);
+               popt_free_cmdline_credentials();
                return(0);
        } else {
+               talloc_free(mem_ctx);
                return(1);
        }
 }