s3:smbd: use passed in ev in smbd_smb2_query_directory_send()
[garming/samba-autobuild/.git] / selftest / selftest.pl
index 72e9ddf0200d97e91b51dc4b7cba9ba106adde96..3ee266c4d0ac9e94c74d132d4c12e5c5b620696e 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-=pod
-
-=head1 NAME
-
-selftest - Samba test runner
-
-=head1 SYNOPSIS
-
-selftest --help
-
-selftest [--srcdir=DIR] [--bindir=DIR] [--target=samba|samba3|win] [--socket-wrapper] [--quick] [--exclude=FILE] [--include=FILE] [--one] [--prefix=prefix] [--testlist=FILE] [TESTS]
-
-=head1 DESCRIPTION
-
-A simple test runner. TESTS is a regular expression with tests to run.
-
-=head1 OPTIONS
-
-=over 4
-
-=item I<--help>
-
-Show list of available options.
-
-=item I<--srcdir=DIR>
-
-Source directory.
-
-=item I<--bindir=DIR>
-
-Built binaries directory.
-
-=item I<--prefix=DIR>
-
-Change directory to run tests in. Default is 'st'.
-
-=item I<--target samba|samba3|win>
-
-Specify test target against which to run. Default is 'samba4'.
-
-=item I<--quick>
-
-Run only a limited number of tests. Intended to run in about 30 seconds on 
-moderately recent systems.
-               
-=item I<--socket-wrapper>
-
-Use socket wrapper library for communication with server. Only works 
-when the server is running locally.
-
-Will prevent TCP and UDP ports being opened on the local host but 
-(transparently) redirects these calls to use unix domain sockets.
-
-=item I<--exclude>
-
-Specify a file containing a list of tests that should be skipped. Possible 
-candidates are tests that segfault the server, flip or don't end. 
-
-=item I<--include>
-
-Specify a file containing a list of tests that should be run. Same format 
-as the --exclude flag.
-
-Not includes specified means all tests will be run.
-
-=item I<--one>
-
-Abort as soon as one test fails.
-
-=item I<--testlist>
-
-Load a list of tests from the specified location.
-
-=back
-
-=head1 ENVIRONMENT
-
-=over 4
-
-=item I<SMBD_VALGRIND>
-
-=item I<TORTURE_MAXTIME>
-
-=item I<VALGRIND>
-
-=item I<TLS_ENABLED>
-
-=item I<srcdir>
-
-=back
-
-=head1 LICENSE
-
-selftest is licensed under the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
-
-=head1 AUTHOR
-
-Jelmer Vernooij
-
-=cut
-
 use strict;
 
 use FindBin qw($RealBin $Script);
@@ -128,6 +27,8 @@ use Cwd qw(abs_path);
 use lib "$RealBin";
 use Subunit;
 use SocketWrapper;
+use target::Samba;
+use Time::HiRes qw(time);
 
 eval {
 require Time::HiRes;
@@ -143,16 +44,24 @@ my $opt_quick = 0;
 my $opt_socket_wrapper = 0;
 my $opt_socket_wrapper_pcap = undef;
 my $opt_socket_wrapper_keep_pcap = undef;
+my $opt_random_order = 0;
 my $opt_one = 0;
 my @opt_exclude = ();
 my @opt_include = ();
-my $opt_verbose = 0;
+my @opt_exclude_env = ();
+my @opt_include_env = ();
 my $opt_testenv = 0;
 my $opt_list = 0;
+my $opt_mitkrb5 = 0;
 my $ldap = undef;
 my $opt_resetup_env = undef;
-my $opt_binary_mapping = "";
 my $opt_load_list = undef;
+my $opt_libnss_wrapper_so_path = "";
+my $opt_libresolv_wrapper_so_path = "";
+my $opt_libsocket_wrapper_so_path = "";
+my $opt_libuid_wrapper_so_path = "";
+my $opt_libasan_so_path = "";
+my $opt_use_dns_faking = 0;
 my @testlists = ();
 
 my $srcdir = ".";
@@ -162,14 +71,6 @@ my $prefix = "./st";
 my @includes = ();
 my @excludes = ();
 
-sub pipe_handler {
-       my $sig = shift @_;
-       print STDERR "Exiting early because of SIGPIPE.\n";
-       exit(1);
-}
-
-$SIG{PIPE} = \&pipe_handler;
-
 sub find_in_list($$)
 {
        my ($list, $fullname) = @_;
@@ -184,9 +85,20 @@ sub find_in_list($$)
        return undef;
 }
 
-sub skip($)
+sub skip
 {
-       my ($name) = @_;
+       my ($name, $envname) = @_;
+       my ($env_basename, $env_localpart) = split(/:/, $envname);
+
+       if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
+               return "environment $envname is disabled as this build does not include an AD DC";
+       }
+
+       if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
+               return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
+       } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
+               return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
+       }
 
        return find_in_list(\@excludes, $name);
 }
@@ -240,16 +152,19 @@ sub run_testsuite($$$$$)
 
        Subunit::start_testsuite($name);
        Subunit::progress_push();
-       Subunit::report_time(time());
+       Subunit::report_time();
        system($cmd);
-       Subunit::report_time(time());
+       Subunit::report_time();
        Subunit::progress_pop();
 
        if ($? == -1) {
-               Subunit::progress_pop();
+               print "command: $cmd\n";
+               printf "expanded command: %s\n", expand_environment_strings($cmd);
                Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
                exit(1);
        } elsif ($? & 127) {
+               print "command: $cmd\n";
+               printf "expanded command: %s\n", expand_environment_strings($cmd);
                Subunit::end_testsuite($name, "error",
                        sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
                exit(1);
@@ -290,18 +205,33 @@ sub ShowHelp()
 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
 Copyright (C) Stefan Metzmacher <metze\@samba.org>
 
-Usage: $Script [OPTIONS] TESTNAME-REGEX
+Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
 
 Generic options:
  --help                     this help page
  --target=samba[3]|win      Samba version to target
  --testlist=FILE            file to read available tests from
+ --exclude=FILE             Exclude tests listed in the file
+ --include=FILE             Include tests listed in the file
+ --exclude-env=ENV          Exclude tests for the specified environment
+ --include-env=ENV          Include tests for the specified environment
 
 Paths:
  --prefix=DIR               prefix to run tests in [st]
  --srcdir=DIR               source directory [.]
  --bindir=DIR               binaries directory [./bin]
 
+Preload cwrap:
+ --nss_wrapper_so_path=FILE the nss_wrapper library to preload
+ --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
+ --socket_wrapper_so_path=FILE the socket_wrapper library to preload
+ --uid_wrapper_so_path=FILE the uid_wrapper library to preload
+ --asan_so_path=FILE the asan library to preload
+
+DNS:
+  --use-dns-faking          Fake DNS entries rather than talking to our
+                            DNS implementation.
+
 Target Specific:
  --socket-wrapper-pcap      save traffic to pcap directories
  --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that 
@@ -314,7 +244,6 @@ Samba4 Specific:
 Behaviour:
  --quick                    run quick overall test
  --one                      abort when the first test fails
- --verbose                  be verbose
  --testenv                  run a shell in the requested test environment
  --list                     list available tests
 ";
@@ -332,16 +261,24 @@ my $result = GetOptions (
                'one' => \$opt_one,
                'exclude=s' => \@opt_exclude,
                'include=s' => \@opt_include,
+               'exclude-env=s' => \@opt_exclude_env,
+               'include-env=s' => \@opt_include_env,
                'srcdir=s' => \$srcdir,
                'bindir=s' => \$bindir,
-               'verbose' => \$opt_verbose,
                'testenv' => \$opt_testenv,
                'list' => \$opt_list,
+               'mitkrb5' => \$opt_mitkrb5,
                'ldap:s' => \$ldap,
                'resetup-environment' => \$opt_resetup_env,
                'testlist=s' => \@testlists,
+               'random-order' => \$opt_random_order,
                'load-list=s' => \$opt_load_list,
-                'binary-mapping=s' => \$opt_binary_mapping
+               'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
+               'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
+               'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
+               'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
+               'asan_so_path=s' => \$opt_libasan_so_path,
+               'use-dns-faking' => \$opt_use_dns_faking
            );
 
 exit(1) if (not $result);
@@ -358,7 +295,7 @@ my @tests = @ARGV;
 # quick hack to disable rpc validation when using valgrind - its way too slow
 unless (defined($ENV{VALGRIND})) {
        $ENV{VALIDATE} = "validate";
-       $ENV{MALLOC_CHECK_} = 2;
+       $ENV{MALLOC_CHECK_} = 3;
 }
 
 # make all our python scripts unbuffered
@@ -393,8 +330,11 @@ die("using an empty prefix isn't allowed") unless $prefix ne "";
 # permissions on this as some subdirectories in this tree will have
 # wider permissions (ie 0777) and this would allow other users on the
 # host to subvert the test process.
+umask 0077;
 mkdir($prefix, 0700) unless -d $prefix;
 chmod 0700, $prefix;
+# We need to have no umask limitations for the tests.
+umask 0000;
 
 my $prefix_abs = abs_path($prefix);
 my $tmpdir_abs = abs_path("$prefix/tmp");
@@ -405,11 +345,13 @@ my $srcdir_abs = abs_path($srcdir);
 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
 
+$ENV{SAMBA_SELFTEST} = "1";
+
 $ENV{PREFIX} = $prefix;
-$ENV{KRB5CCNAME} = "$prefix/krb5ticket";
 $ENV{PREFIX_ABS} = $prefix_abs;
 $ENV{SRCDIR} = $srcdir;
 $ENV{SRCDIR_ABS} = $srcdir_abs;
+$ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
 $ENV{BINDIR} = $bindir_abs;
 
 my $tls_enabled = not $opt_quick;
@@ -437,88 +379,108 @@ if ($opt_socket_wrapper_pcap) {
        $opt_socket_wrapper = 1;
 }
 
+my $ld_preload = $ENV{LD_PRELOAD};
+
+if ($opt_libasan_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libasan_so_path";
+       } else {
+               $ld_preload = "$opt_libasan_so_path";
+       }
+}
+
+if ($opt_libnss_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libnss_wrapper_so_path";
+       }
+}
+
+if ($opt_libresolv_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libresolv_wrapper_so_path";
+       }
+}
+
+if ($opt_libsocket_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libsocket_wrapper_so_path";
+       }
+}
+
+if ($opt_libuid_wrapper_so_path) {
+       if ($ld_preload) {
+               $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
+       } else {
+               $ld_preload = "$opt_libuid_wrapper_so_path";
+       }
+}
+
+$ENV{LD_PRELOAD} = $ld_preload;
+print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
+
+# Enable uid_wrapper globally
+$ENV{UID_WRAPPER} = 1;
+
+# Disable RTLD_DEEPBIND hack for Samba bind dlz module
+#
+# This is needed in order to allow the ldb_*ldap module
+# to work with a preloaded socket wrapper.
+$ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
+
 my $socket_wrapper_dir;
 if ($opt_socket_wrapper) {
        $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
        print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
 } elsif (not $opt_list) {
-        unless ($< == 0) { 
+        unless ($< == 0) {
                 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
         }
 }
 
+if ($opt_use_dns_faking) {
+       print "DNS: Faking nameserver\n";
+       $ENV{SAMBA_DNS_FAKING} = 1;
+}
+
 my $target;
 my $testenv_default = "none";
 
-my %binary_mapping = ();
-if ($opt_binary_mapping) {
-    my @binmapping_list = split(/,/, $opt_binary_mapping);
-    foreach my $mapping (@binmapping_list) {
-       my ($bin, $map) = split(/\:/, $mapping);
-       $binary_mapping{$bin} = $map;
-    }
+if ($opt_mitkrb5 == 1) {
+       $ENV{MITKRB5} = $opt_mitkrb5;
 }
 
-$ENV{BINARY_MAPPING} = $opt_binary_mapping;
-
 # After this many seconds, the server will self-terminate.  All tests
 # must terminate in this time, and testenv will only stay alive this
 # long
 
-my $server_maxtime = 7500;
+my $server_maxtime;
+if ($opt_testenv) {
+    # 1 year should be enough :-)
+    $server_maxtime = 365 * 24 * 60 * 60;
+} else {
+    # make test should run under 5 hours
+    $server_maxtime = 5 * 60 * 60;
+}
+
 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
     $server_maxtime = $ENV{SMBD_MAXTIME};
 }
 
+$target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
 unless ($opt_list) {
        if ($opt_target eq "samba") {
-               if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
-                       die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'.  Exiting....");
-               }
-               $testenv_default = "dc";
-               require target::Samba;
-               $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
+               $testenv_default = "ad_dc";
        } elsif ($opt_target eq "samba3") {
-               if ($opt_socket_wrapper and `$bindir/smbd -b | grep SOCKET_WRAPPER` eq "") {
-                       die("You must include --enable-socket-wrapper when compiling Samba in order to execute 'make test'.  Exiting....");
-               }
-               $testenv_default = "member";
-               require target::Samba3;
-               $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
-       } elsif ($opt_target eq "win") {
-               die("Windows tests will not run with socket wrapper enabled.") 
-                       if ($opt_socket_wrapper);
-               $testenv_default = "dc";
-               require target::Windows;
-               $target = new Windows();
+               $testenv_default = "nt4_member";
        }
 }
 
-#
-# Start a Virtual Distributed Ethernet Switch
-# Returns the pid of the switch.
-#
-sub start_vde_switch($)
-{
-       my ($path) = @_;
-
-       system("vde_switch --pidfile $path/vde.pid --sock $path/vde.sock --daemon");
-
-       open(PID, "$path/vde.pid");
-       <PID> =~ /([0-9]+)/;
-       my $pid = $1;
-       close(PID);
-
-       return $pid;
-}
-
-# Stop a Virtual Distributed Ethernet Switch
-sub stop_vde_switch($)
-{
-       my ($pid) = @_;
-       kill 9, $pid;
-}
-
 sub read_test_regexes($)
 {
        my ($name) = @_;
@@ -570,6 +532,12 @@ sub write_clientconf($$$)
                mkdir("$clientdir/private", 0777);
        }
 
+       if ( -d "$clientdir/bind-dns" ) {
+               unlink <$clientdir/bind-dns/*>;
+       } else {
+               mkdir("$clientdir/bind-dns", 0777);
+       }
+
        if ( -d "$clientdir/lockdir" ) {
                unlink <$clientdir/lockdir/*>;
        } else {
@@ -603,6 +571,42 @@ sub write_clientconf($$$)
        mkdir("$clientdir/ncalrpcdir", 0755);
        umask $mask;
 
+       my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
+       my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
+       my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
+       my $ca_users_dir = "$cadir/Users";
+
+       if ( -d "$clientdir/pkinit" ) {
+               unlink <$clientdir/pkinit/*>;
+       } else {
+               mkdir("$clientdir/pkinit", 0700);
+       }
+
+       # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
+       # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
+       # We make a copy here and make the certificated easily
+       # accessable in the client environment.
+       my $mask = umask;
+       umask 0077;
+       opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
+       for my $d (readdir USERS) {
+               my $user_dir = "${ca_users_dir}/${d}";
+               next if ${d} =~ /^\./;
+               next if (! -d "${user_dir}");
+               opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
+               for my $l (readdir USER) {
+                       my $user_link = "${user_dir}/${l}";
+                       next if ${l} =~ /^\./;
+                       next if (! -l "${user_link}");
+
+                       my $dest = "${clientdir}/pkinit/${l}";
+                       Samba::copy_file_content(${user_link}, ${dest});
+               }
+               closedir USER;
+       }
+       closedir USERS;
+       umask $mask;
+
        open(CF, ">$conffile");
        print CF "[global]\n";
        print CF "\tnetbios name = client\n";
@@ -617,11 +621,11 @@ sub write_clientconf($$$)
        }
        print CF "
        private dir = $clientdir/private
+       binddns dir = $clientdir/bind-dns
        lock dir = $clientdir/lockdir
        state directory = $clientdir/statedir
        cache directory = $clientdir/cachedir
        ncalrpc dir = $clientdir/ncalrpcdir
-       name resolve order = file bcast
        panic action = $RealBin/gdb_backtrace \%d
        max xmit = 32K
        notify:inotify = false
@@ -632,9 +636,13 @@ sub write_clientconf($$$)
        torture:basedir = $clientdir
 #We don't want to pass our self-tests if the PAC code is wrong
        gensec:require_pac = true
-       resolv:host file = $prefix_abs/dns_host_file
 #We don't want to run 'speed' tests for very long
         torture:timelimit = 1
+        winbind separator = /
+       tls cafile = ${cacert}
+       tls crlfile = ${cacrl_pem}
+       tls verify peer = no_check
+       include system krb5 conf = no
 ";
        close(CF);
 }
@@ -663,23 +671,27 @@ sub read_testlist($)
        open(IN, $filename) or die("Unable to open $filename: $!");
 
        while (<IN>) {
-               if (/-- TEST(-LOADLIST|-IDLIST|) --\n/) {
+               if (/-- TEST(-LOADLIST|) --\n/) {
                        my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
-                       my $supports_idlist = (defined($1) and $1 eq "-IDLIST");
                        my $name = <IN>;
                        $name =~ s/\n//g;
                        my $env = <IN>;
                        $env =~ s/\n//g;
+                       my $loadlist;
+                       if ($supports_loadlist) {
+                               $loadlist = <IN>;
+                               $loadlist =~ s/\n//g;
+                       }
                        my $cmdline = <IN>;
                        $cmdline =~ s/\n//g;
                        if (should_run_test($name) == 1) {
-                               push (@ret, [$name, $env, $cmdline, $supports_loadlist, $supports_idlist]);
+                               push (@ret, [$name, $env, $cmdline, $loadlist]);
                        }
                } else {
                        print;
                }
        }
-       close(IN) or die("Error creating recipe");
+       close(IN) or die("Error creating recipe from $filename");
        return @ret;
 }
 
@@ -689,17 +701,13 @@ if ($#testlists == -1) {
 
 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
+$ENV{TMPDIR} = "$tmpdir_abs";
 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
 if ($opt_socket_wrapper) {
        $ENV{SELFTEST_INTERFACES} = $interfaces;
 } else {
        $ENV{SELFTEST_INTERFACES} = "";
 }
-if ($opt_verbose) {
-       $ENV{SELFTEST_VERBOSE} = "1";
-} else {
-       $ENV{SELFTEST_VERBOSE} = "";
-}
 if ($opt_quick) {
        $ENV{SELFTEST_QUICK} = "1";
 } else {
@@ -707,6 +715,9 @@ if ($opt_quick) {
 }
 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
 
+my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
+$ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
+
 my @available = ();
 foreach my $fn (@testlists) {
        foreach (read_testlist($fn)) {
@@ -734,7 +745,7 @@ $individual_tests = {};
 
 foreach my $testsuite (@available) {
        my $name = $$testsuite[0];
-       my $skipreason = skip($name);
+       my $skipreason = skip(@$testsuite);
        if (defined($restricted)) {
                # Find the testsuite for this test
                my $match = undef;
@@ -782,7 +793,7 @@ my $suitestotal = $#todo + 1;
 
 unless ($opt_list) {
        Subunit::progress($suitestotal);
-       Subunit::report_time(time());
+       Subunit::report_time();
 }
 
 my $i = 0;
@@ -804,37 +815,75 @@ sub get_running_env($)
 my @exported_envvars = (
        # domain stuff
        "DOMAIN",
+       "DNSNAME",
        "REALM",
+       "DOMSID",
+
+       # stuff related to a trusted domain
+       "TRUST_SERVER",
+       "TRUST_SERVER_IP",
+       "TRUST_SERVER_IPV6",
+       "TRUST_NETBIOSNAME",
+       "TRUST_USERNAME",
+       "TRUST_PASSWORD",
+       "TRUST_DOMAIN",
+       "TRUST_REALM",
+       "TRUST_DOMSID",
 
        # domain controller stuff
        "DC_SERVER",
        "DC_SERVER_IP",
+       "DC_SERVER_IPV6",
        "DC_NETBIOSNAME",
        "DC_NETBIOSALIAS",
 
        # domain member
        "MEMBER_SERVER",
        "MEMBER_SERVER_IP",
+       "MEMBER_SERVER_IPV6",
        "MEMBER_NETBIOSNAME",
        "MEMBER_NETBIOSALIAS",
 
        # rpc proxy controller stuff
        "RPC_PROXY_SERVER",
        "RPC_PROXY_SERVER_IP",
+       "RPC_PROXY_SERVER_IPV6",
        "RPC_PROXY_NETBIOSNAME",
        "RPC_PROXY_NETBIOSALIAS",
 
        # domain controller stuff for Vampired DC
        "VAMPIRE_DC_SERVER",
        "VAMPIRE_DC_SERVER_IP",
+       "VAMPIRE_DC_SERVER_IPV6",
        "VAMPIRE_DC_NETBIOSNAME",
        "VAMPIRE_DC_NETBIOSALIAS",
 
+       # domain controller stuff for RODC
+       "RODC_DC_SERVER",
+       "RODC_DC_SERVER_IP",
+       "RODC_DC_SERVER_IPV6",
+       "RODC_DC_NETBIOSNAME",
+
+       # domain controller stuff for FL 2000 Vampired DC
+       "VAMPIRE_2000_DC_SERVER",
+       "VAMPIRE_2000_DC_SERVER_IP",
+       "VAMPIRE_2000_DC_SERVER_IPV6",
+       "VAMPIRE_2000_DC_NETBIOSNAME",
+       "VAMPIRE_2000_DC_NETBIOSALIAS",
+
+       "PROMOTED_DC_SERVER",
+       "PROMOTED_DC_SERVER_IP",
+       "PROMOTED_DC_SERVER_IPV6",
+       "PROMOTED_DC_NETBIOSNAME",
+       "PROMOTED_DC_NETBIOSALIAS",
+
        # server stuff
        "SERVER",
        "SERVER_IP",
+       "SERVER_IPV6",
        "NETBIOSNAME",
        "NETBIOSALIAS",
+       "SAMSID",
 
        # user stuff
        "USERNAME",
@@ -843,20 +892,54 @@ my @exported_envvars = (
        "DC_USERNAME",
        "DC_PASSWORD",
 
+       # UID/GID for rfc2307 mapping tests
+       "UID_RFC2307TEST",
+       "GID_RFC2307TEST",
+
        # misc stuff
        "KRB5_CONFIG",
-       "WINBINDD_SOCKET_DIR",
-       "WINBINDD_PRIV_PIPE_DIR",
+       "KRB5CCNAME",
+       "SELFTEST_WINBINDD_SOCKET_DIR",
        "NMBD_SOCKET_DIR",
-       "LOCAL_PATH"
+       "LOCAL_PATH",
+       "DNS_FORWARDER1",
+       "DNS_FORWARDER2",
+       "RESOLV_CONF",
+       "UNACCEPTABLE_PASSWORD",
+       "LOCK_DIR",
+       "SMBD_TEST_LOG",
+
+       # nss_wrapper
+       "NSS_WRAPPER_PASSWD",
+       "NSS_WRAPPER_GROUP",
+       "NSS_WRAPPER_HOSTS",
+       "NSS_WRAPPER_HOSTNAME",
+       "NSS_WRAPPER_MODULE_SO_PATH",
+       "NSS_WRAPPER_MODULE_FN_PREFIX",
+
+       # resolv_wrapper
+       "RESOLV_WRAPPER_CONF",
+       "RESOLV_WRAPPER_HOSTS",
 );
 
-$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { 
+sub sighandler($)
+{
        my $signame = shift;
+
+       $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
+       $SIG{PIPE} = 'IGNORE';
+
+       open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
+
+       print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
        teardown_env($_) foreach(keys %running_envs);
-       die("Received signal $signame");
+       system("pstree -p $$");
+       print "$0: PID[$$]: Exiting...\n";
+       exit(1);
 };
 
+$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
+
 sub setup_env($$)
 {
        my ($name, $prefix) = @_;
@@ -872,9 +955,18 @@ sub setup_env($$)
 
        $option = "client" if $option eq "";
 
-       if ($envname eq "none") {
-               $testenv_vars = {};
-       } elsif (defined(get_running_env($envname))) {
+       # Initially clear out the environment for the provision, so previous envs'
+       # variables don't leak in. Provisioning steps must explicitly set their
+       # necessary variables when calling out to other executables
+       foreach (@exported_envvars) {
+               delete $ENV{$_};
+       }
+       delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
+       delete $ENV{SMB_CONF_PATH};
+
+       $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
+
+       if (defined(get_running_env($envname))) {
                $testenv_vars = get_running_env($envname);
                if (not $testenv_vars->{target}->check_env($testenv_vars)) {
                        print $testenv_vars->{target}->getlog_env($testenv_vars);
@@ -892,7 +984,6 @@ sub setup_env($$)
                }
        }
 
-       
        return undef unless defined($testenv_vars);
 
        $running_envs{$envname} = $testenv_vars;
@@ -916,6 +1007,9 @@ sub setup_env($$)
                }
        }
 
+       my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
+       unlink($krb5_ccache_path);
+       $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
        return $testenv_vars;
 }
 
@@ -943,7 +1037,6 @@ sub getlog_env($)
 sub check_env($)
 {
        my ($envname) = @_;
-       return 1 if ($envname eq "none");
        my $env = get_running_env($envname);
        return $env->{target}->check_env($env);
 }
@@ -952,6 +1045,7 @@ sub teardown_env($)
 {
        my ($envname) = @_;
        return if ($envname eq "none");
+       print STDERR "teardown_env($envname)\n";
        my $env = get_running_env($envname);
        $env->{target}->teardown_env($env);
        delete $running_envs{$envname};
@@ -959,6 +1053,13 @@ sub teardown_env($)
 
 # This 'global' file needs to be empty when we start
 unlink("$prefix_abs/dns_host_file");
+unlink("$prefix_abs/hosts");
+
+if ($opt_random_order) {
+       require List::Util;
+       my @newtodo = List::Util::shuffle(@todo);
+       @todo = @newtodo;
+}
 
 if ($opt_testenv) {
        my $testenv_name = $ENV{SELFTEST_TESTENV};
@@ -966,15 +1067,16 @@ if ($opt_testenv) {
 
        my $testenv_vars = setup_env($testenv_name, $prefix);
 
-       die("Unable to setup environment $testenv_name") unless ($testenv_vars);
+       if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
+               die("Unable to setup environment $testenv_name");
+       }
 
        $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
        $ENV{ENVNAME} = $testenv_name;
 
        my $envvarstr = exported_envvars_str($testenv_vars);
 
-       my $term = ($ENV{TERMINAL} or "xterm -e");
-       system("$term 'echo -e \"
+       my @term_args = ("echo -e \"
 Welcome to the Samba4 Test environment '$testenv_name'
 
 This matches the client environment used in make test
@@ -985,27 +1087,39 @@ TORTURE_OPTIONS=\$TORTURE_OPTIONS
 SMB_CONF_PATH=\$SMB_CONF_PATH
 
 $envvarstr
-\" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash'");
+\" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
+       my @term = ();
+       if ($ENV{TERMINAL}) {
+           @term = ($ENV{TERMINAL});
+       } else {
+           @term = ("xterm", "-e");
+           unshift(@term_args, ("bash", "-c"));
+       }
+
+       system(@term, @term_args);
+
        teardown_env($testenv_name);
 } elsif ($opt_list) {
        foreach (@todo) {
-               my $cmd = $$_[2];
                my $name = $$_[0];
                my $envname = $$_[1];
+               my $cmd = $$_[2];
+               my $listcmd = $$_[3];
 
-               unless($cmd =~ /\$LISTOPT/) {
+               unless (defined($listcmd)) {
                        warn("Unable to list tests in $name");
+                       # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
+                       # a single "test".
+                       print "$name\n";
                        next;
                }
 
-               $cmd =~ s/\$LISTOPT/--list/g;
-
-               system($cmd);
+               system($listcmd);
 
                if ($? == -1) {
-                       die("Unable to run $cmd: $!");
+                       die("Unable to run $listcmd: $!");
                } elsif ($? & 127) {
-                       die(snprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
+                       die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127),  ($? & 128) ? 'with' : 'without'));
                }
 
                my $exitcode = $? >> 8;
@@ -1019,8 +1133,8 @@ $envvarstr
                my $cmd = $$_[2];
                my $name = $$_[0];
                my $envname = $$_[1];
-
                my $envvars = setup_env($envname, $prefix);
+
                if (not defined($envvars)) {
                        Subunit::start_testsuite($name);
                        Subunit::end_testsuite($name, "error",
@@ -1028,8 +1142,8 @@ $envvarstr
                        next;
                } elsif ($envvars eq "UNKNOWN") {
                        Subunit::start_testsuite($name);
-                       Subunit::end_testsuite($name, "skip",
-                               "environment $envname is unknown in this test backend - skipping");
+                       Subunit::end_testsuite($name, "error",
+                               "environment $envname is unknown - exiting");
                        next;
                }
 
@@ -1042,9 +1156,8 @@ $envvarstr
                                        print $fh substr($test, length($name)+1) . "\n";
                                }
                                $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
-                       } elsif ($$_[4]) {
-                               $cmd =~ s/\s+[^\s]+\s*$//;
-                               $cmd .= " " . join(' ', @{$individual_tests->{$name}});
+                       } else {
+                               warn("Unable to run individual tests in $name, it does not support --loadlist.");
                        }
                }