s4:rpc_server/lsa: prepare dcesrv_lsa_LookupSids* for async processing
[Samba.git] / selftest / selftest.pl
blobfad0191e89e6323c67877eab367d02b7e7ac498e
1 #!/usr/bin/perl
2 # Bootstrap Samba and run a number of tests against it.
3 # Copyright (C) 2005-2010 Jelmer Vernooij <jelmer@samba.org>
4 # Copyright (C) 2007-2009 Stefan Metzmacher <metze@samba.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 use strict;
21 use FindBin qw($RealBin $Script);
22 use File::Spec;
23 use File::Temp qw(tempfile);
24 use Getopt::Long;
25 use POSIX;
26 use Cwd qw(abs_path);
27 use lib "$RealBin";
28 use Subunit;
29 use SocketWrapper;
30 use target::Samba;
32 eval {
33 require Time::HiRes;
34 Time::HiRes->import("time");
36 if ($@) {
37 print "You don't have Time::Hires installed !\n";
40 my $opt_help = 0;
41 my $opt_target = "samba";
42 my $opt_quick = 0;
43 my $opt_socket_wrapper = 0;
44 my $opt_socket_wrapper_pcap = undef;
45 my $opt_socket_wrapper_keep_pcap = undef;
46 my $opt_random_order = 0;
47 my $opt_one = 0;
48 my @opt_exclude = ();
49 my @opt_include = ();
50 my @opt_exclude_env = ();
51 my @opt_include_env = ();
52 my $opt_testenv = 0;
53 my $opt_list = 0;
54 my $opt_mitkrb5 = 0;
55 my $ldap = undef;
56 my $opt_resetup_env = undef;
57 my $opt_load_list = undef;
58 my $opt_libnss_wrapper_so_path = "";
59 my $opt_libresolv_wrapper_so_path = "";
60 my $opt_libsocket_wrapper_so_path = "";
61 my $opt_libuid_wrapper_so_path = "";
62 my $opt_use_dns_faking = 0;
63 my @testlists = ();
65 my $srcdir = ".";
66 my $bindir = "./bin";
67 my $prefix = "./st";
69 my @includes = ();
70 my @excludes = ();
72 sub find_in_list($$)
74 my ($list, $fullname) = @_;
76 foreach (@$list) {
77 if ($fullname =~ /$$_[0]/) {
78 return ($$_[1]) if ($$_[1]);
79 return "";
83 return undef;
86 sub skip
88 my ($name, $envname) = @_;
89 my ($env_basename, $env_localpart) = split(/:/, $envname);
91 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
92 return "environment $envname is disabled as this build does not include an AD DC";
95 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
96 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
97 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
98 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
101 return find_in_list(\@excludes, $name);
104 sub getlog_env($);
106 sub setup_pcap($)
108 my ($name) = @_;
110 return unless ($opt_socket_wrapper_pcap);
111 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
113 my $fname = $name;
114 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
116 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
118 SocketWrapper::setup_pcap($pcap_file);
120 return $pcap_file;
123 sub cleanup_pcap($$)
125 my ($pcap_file, $exitcode) = @_;
127 return unless ($opt_socket_wrapper_pcap);
128 return if ($opt_socket_wrapper_keep_pcap);
129 return unless ($exitcode == 0);
130 return unless defined($pcap_file);
132 unlink($pcap_file);
135 # expand strings from %ENV
136 sub expand_environment_strings($)
138 my $s = shift;
139 # we use a reverse sort so we do the longer ones first
140 foreach my $k (sort { $b cmp $a } keys %ENV) {
141 $s =~ s/\$$k/$ENV{$k}/g;
143 return $s;
146 sub run_testsuite($$$$$)
148 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
149 my $pcap_file = setup_pcap($name);
151 Subunit::start_testsuite($name);
152 Subunit::progress_push();
153 Subunit::report_time(time());
154 system($cmd);
155 Subunit::report_time(time());
156 Subunit::progress_pop();
158 if ($? == -1) {
159 print "command: $cmd\n";
160 printf "expanded command: %s\n", expand_environment_strings($cmd);
161 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
162 exit(1);
163 } elsif ($? & 127) {
164 print "command: $cmd\n";
165 printf "expanded command: %s\n", expand_environment_strings($cmd);
166 Subunit::end_testsuite($name, "error",
167 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
168 exit(1);
171 my $exitcode = $? >> 8;
173 my $envlog = getlog_env($envname);
174 if ($envlog ne "") {
175 print "envlog: $envlog\n";
178 print "command: $cmd\n";
179 printf "expanded command: %s\n", expand_environment_strings($cmd);
181 if ($exitcode == 0) {
182 Subunit::end_testsuite($name, "success");
183 } else {
184 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
187 cleanup_pcap($pcap_file, $exitcode);
189 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
190 print "PCAP FILE: $pcap_file\n";
193 if ($exitcode != 0) {
194 exit(1) if ($opt_one);
197 return $exitcode;
200 sub ShowHelp()
202 print "Samba test runner
203 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
204 Copyright (C) Stefan Metzmacher <metze\@samba.org>
206 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
208 Generic options:
209 --help this help page
210 --target=samba[3]|win Samba version to target
211 --testlist=FILE file to read available tests from
212 --exclude=FILE Exclude tests listed in the file
213 --include=FILE Include tests listed in the file
214 --exclude-env=ENV Exclude tests for the specified environment
215 --include-env=ENV Include tests for the specified environment
217 Paths:
218 --prefix=DIR prefix to run tests in [st]
219 --srcdir=DIR source directory [.]
220 --bindir=DIR binaries directory [./bin]
222 Preload cwrap:
223 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
224 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
225 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
226 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
228 DNS:
229 --use-dns-faking Fake DNS entries rather than talking to our
230 DNS implementation.
232 Target Specific:
233 --socket-wrapper-pcap save traffic to pcap directories
234 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
235 failed
236 --socket-wrapper enable socket wrapper
238 Samba4 Specific:
239 --ldap=openldap|fedora-ds back samba onto specified ldap server
241 Behaviour:
242 --quick run quick overall test
243 --one abort when the first test fails
244 --testenv run a shell in the requested test environment
245 --list list available tests
247 exit(0);
250 my $result = GetOptions (
251 'help|h|?' => \$opt_help,
252 'target=s' => \$opt_target,
253 'prefix=s' => \$prefix,
254 'socket-wrapper' => \$opt_socket_wrapper,
255 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
256 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
257 'quick' => \$opt_quick,
258 'one' => \$opt_one,
259 'exclude=s' => \@opt_exclude,
260 'include=s' => \@opt_include,
261 'exclude-env=s' => \@opt_exclude_env,
262 'include-env=s' => \@opt_include_env,
263 'srcdir=s' => \$srcdir,
264 'bindir=s' => \$bindir,
265 'testenv' => \$opt_testenv,
266 'list' => \$opt_list,
267 'mitkrb5' => \$opt_mitkrb5,
268 'ldap:s' => \$ldap,
269 'resetup-environment' => \$opt_resetup_env,
270 'testlist=s' => \@testlists,
271 'random-order' => \$opt_random_order,
272 'load-list=s' => \$opt_load_list,
273 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
274 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
275 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
276 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
277 'use-dns-faking' => \$opt_use_dns_faking
280 exit(1) if (not $result);
282 ShowHelp() if ($opt_help);
284 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
286 # we want unbuffered output
287 $| = 1;
289 my @tests = @ARGV;
291 # quick hack to disable rpc validation when using valgrind - its way too slow
292 unless (defined($ENV{VALGRIND})) {
293 $ENV{VALIDATE} = "validate";
294 $ENV{MALLOC_CHECK_} = 3;
297 # make all our python scripts unbuffered
298 $ENV{PYTHONUNBUFFERED} = 1;
300 my $bindir_abs = abs_path($bindir);
302 # Backwards compatibility:
303 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
304 if (defined($ENV{FEDORA_DS_ROOT})) {
305 $ldap = "fedora-ds";
306 } else {
307 $ldap = "openldap";
311 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
312 if ($ldap) {
313 # LDAP is slow
314 $torture_maxtime *= 2;
317 $prefix =~ s+//+/+;
318 $prefix =~ s+/./+/+;
319 $prefix =~ s+/$++;
321 die("using an empty prefix isn't allowed") unless $prefix ne "";
323 # Ensure we have the test prefix around.
325 # We need restrictive
326 # permissions on this as some subdirectories in this tree will have
327 # wider permissions (ie 0777) and this would allow other users on the
328 # host to subvert the test process.
329 umask 0077;
330 mkdir($prefix, 0700) unless -d $prefix;
331 chmod 0700, $prefix;
332 # We need to have no umask limitations for the tests.
333 umask 0000;
335 my $prefix_abs = abs_path($prefix);
336 my $tmpdir_abs = abs_path("$prefix/tmp");
337 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
339 my $srcdir_abs = abs_path($srcdir);
341 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
342 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
344 $ENV{SAMBA_SELFTEST} = "1";
346 $ENV{PREFIX} = $prefix;
347 $ENV{PREFIX_ABS} = $prefix_abs;
348 $ENV{SRCDIR} = $srcdir;
349 $ENV{SRCDIR_ABS} = $srcdir_abs;
350 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
351 $ENV{BINDIR} = $bindir_abs;
353 my $tls_enabled = not $opt_quick;
354 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
356 sub prefix_pathvar($$)
358 my ($name, $newpath) = @_;
359 if (defined($ENV{$name})) {
360 $ENV{$name} = "$newpath:$ENV{$name}";
361 } else {
362 $ENV{$name} = $newpath;
365 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
366 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
368 if ($opt_socket_wrapper_keep_pcap) {
369 # Socket wrapper keep pcap implies socket wrapper pcap
370 $opt_socket_wrapper_pcap = 1;
373 if ($opt_socket_wrapper_pcap) {
374 # Socket wrapper pcap implies socket wrapper
375 $opt_socket_wrapper = 1;
378 my $ld_preload = $ENV{LD_PRELOAD};
380 if ($opt_libnss_wrapper_so_path) {
381 if ($ld_preload) {
382 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
383 } else {
384 $ld_preload = "$opt_libnss_wrapper_so_path";
388 if ($opt_libresolv_wrapper_so_path) {
389 if ($ld_preload) {
390 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
391 } else {
392 $ld_preload = "$opt_libresolv_wrapper_so_path";
396 if ($opt_libsocket_wrapper_so_path) {
397 if ($ld_preload) {
398 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
399 } else {
400 $ld_preload = "$opt_libsocket_wrapper_so_path";
404 if ($opt_libuid_wrapper_so_path) {
405 if ($ld_preload) {
406 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
407 } else {
408 $ld_preload = "$opt_libuid_wrapper_so_path";
412 $ENV{LD_PRELOAD} = $ld_preload;
413 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
415 # Enable uid_wrapper globally
416 $ENV{UID_WRAPPER} = 1;
418 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
420 # This is needed in order to allow the ldb_*ldap module
421 # to work with a preloaded socket wrapper.
422 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
424 my $socket_wrapper_dir;
425 if ($opt_socket_wrapper) {
426 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
427 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
428 } elsif (not $opt_list) {
429 unless ($< == 0) {
430 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
434 if ($opt_use_dns_faking) {
435 print "DNS: Faking nameserver\n";
436 $ENV{SAMBA_DNS_FAKING} = 1;
439 my $target;
440 my $testenv_default = "none";
442 if ($opt_mitkrb5 == 1) {
443 $ENV{MITKRB5} = $opt_mitkrb5;
446 # After this many seconds, the server will self-terminate. All tests
447 # must terminate in this time, and testenv will only stay alive this
448 # long
450 my $server_maxtime;
451 if ($opt_testenv) {
452 # 1 year should be enough :-)
453 $server_maxtime = 365 * 24 * 60 * 60;
454 } else {
455 # make test should run under 5 hours
456 $server_maxtime = 5 * 60 * 60;
459 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
460 $server_maxtime = $ENV{SMBD_MAXTIME};
463 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
464 unless ($opt_list) {
465 if ($opt_target eq "samba") {
466 $testenv_default = "ad_dc";
467 } elsif ($opt_target eq "samba3") {
468 $testenv_default = "nt4_member";
472 sub read_test_regexes($)
474 my ($name) = @_;
475 my @ret = ();
476 open(LF, "<$name") or die("unable to read $name: $!");
477 while (<LF>) {
478 chomp;
479 next if (/^#/);
480 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
481 push (@ret, [$1, $4]);
482 } else {
483 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
484 push (@ret, [$_, undef]);
487 close(LF);
488 return @ret;
491 foreach (@opt_exclude) {
492 push (@excludes, read_test_regexes($_));
495 foreach (@opt_include) {
496 push (@includes, read_test_regexes($_));
499 my $interfaces = join(',', ("127.0.0.11/8",
500 "127.0.0.12/8",
501 "127.0.0.13/8",
502 "127.0.0.14/8",
503 "127.0.0.15/8",
504 "127.0.0.16/8"));
506 my $clientdir = "$prefix_abs/client";
508 my $conffile = "$clientdir/client.conf";
509 $ENV{SMB_CONF_PATH} = $conffile;
511 sub write_clientconf($$$)
513 my ($conffile, $clientdir, $vars) = @_;
515 mkdir("$clientdir", 0777) unless -d "$clientdir";
517 if ( -d "$clientdir/private" ) {
518 unlink <$clientdir/private/*>;
519 } else {
520 mkdir("$clientdir/private", 0777);
523 if ( -d "$clientdir/bind-dns" ) {
524 unlink <$clientdir/bind-dns/*>;
525 } else {
526 mkdir("$clientdir/bind-dns", 0777);
529 if ( -d "$clientdir/lockdir" ) {
530 unlink <$clientdir/lockdir/*>;
531 } else {
532 mkdir("$clientdir/lockdir", 0777);
535 if ( -d "$clientdir/statedir" ) {
536 unlink <$clientdir/statedir/*>;
537 } else {
538 mkdir("$clientdir/statedir", 0777);
541 if ( -d "$clientdir/cachedir" ) {
542 unlink <$clientdir/cachedir/*>;
543 } else {
544 mkdir("$clientdir/cachedir", 0777);
547 # this is ugly, but the ncalrpcdir needs exactly 0755
548 # otherwise tests fail.
549 my $mask = umask;
550 umask 0022;
551 if ( -d "$clientdir/ncalrpcdir/np" ) {
552 unlink <$clientdir/ncalrpcdir/np/*>;
553 rmdir "$clientdir/ncalrpcdir/np";
555 if ( -d "$clientdir/ncalrpcdir" ) {
556 unlink <$clientdir/ncalrpcdir/*>;
557 rmdir "$clientdir/ncalrpcdir";
559 mkdir("$clientdir/ncalrpcdir", 0755);
560 umask $mask;
562 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
563 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
564 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
565 my $ca_users_dir = "$cadir/Users";
567 if ( -d "$clientdir/pkinit" ) {
568 unlink <$clientdir/pkinit/*>;
569 } else {
570 mkdir("$clientdir/pkinit", 0700);
573 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
574 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
575 # We make a copy here and make the certificated easily
576 # accessable in the client environment.
577 my $mask = umask;
578 umask 0077;
579 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
580 for my $d (readdir USERS) {
581 my $user_dir = "${ca_users_dir}/${d}";
582 next if ${d} =~ /^\./;
583 next if (! -d "${user_dir}");
584 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
585 for my $l (readdir USER) {
586 my $user_link = "${user_dir}/${l}";
587 next if ${l} =~ /^\./;
588 next if (! -l "${user_link}");
590 my $dest = "${clientdir}/pkinit/${l}";
591 Samba::copy_file_content(${user_link}, ${dest});
593 closedir USER;
595 closedir USERS;
596 umask $mask;
598 open(CF, ">$conffile");
599 print CF "[global]\n";
600 print CF "\tnetbios name = client\n";
601 if (defined($vars->{DOMAIN})) {
602 print CF "\tworkgroup = $vars->{DOMAIN}\n";
604 if (defined($vars->{REALM})) {
605 print CF "\trealm = $vars->{REALM}\n";
607 if ($opt_socket_wrapper) {
608 print CF "\tinterfaces = $interfaces\n";
610 print CF "
611 private dir = $clientdir/private
612 binddns dir = $clientdir/bind-dns
613 lock dir = $clientdir/lockdir
614 state directory = $clientdir/statedir
615 cache directory = $clientdir/cachedir
616 ncalrpc dir = $clientdir/ncalrpcdir
617 panic action = $RealBin/gdb_backtrace \%d
618 max xmit = 32K
619 notify:inotify = false
620 ldb:nosync = true
621 system:anonymous = true
622 client lanman auth = Yes
623 log level = 1
624 torture:basedir = $clientdir
625 #We don't want to pass our self-tests if the PAC code is wrong
626 gensec:require_pac = true
627 #We don't want to run 'speed' tests for very long
628 torture:timelimit = 1
629 winbind separator = /
630 tls cafile = ${cacert}
631 tls crlfile = ${cacrl_pem}
632 tls verify peer = no_check
633 include system krb5 conf = no
635 close(CF);
638 my @todo = ();
640 sub should_run_test($)
642 my $name = shift;
643 if ($#tests == -1) {
644 return 1;
646 for (my $i=0; $i <= $#tests; $i++) {
647 if ($name =~ /$tests[$i]/i) {
648 return 1;
651 return 0;
654 sub read_testlist($)
656 my ($filename) = @_;
658 my @ret = ();
659 open(IN, $filename) or die("Unable to open $filename: $!");
661 while (<IN>) {
662 if (/-- TEST(-LOADLIST|) --\n/) {
663 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
664 my $name = <IN>;
665 $name =~ s/\n//g;
666 my $env = <IN>;
667 $env =~ s/\n//g;
668 my $loadlist;
669 if ($supports_loadlist) {
670 $loadlist = <IN>;
671 $loadlist =~ s/\n//g;
673 my $cmdline = <IN>;
674 $cmdline =~ s/\n//g;
675 if (should_run_test($name) == 1) {
676 push (@ret, [$name, $env, $cmdline, $loadlist]);
678 } else {
679 print;
682 close(IN) or die("Error creating recipe from $filename");
683 return @ret;
686 if ($#testlists == -1) {
687 die("No testlists specified");
690 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
691 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
692 $ENV{TMPDIR} = "$tmpdir_abs";
693 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
694 if ($opt_socket_wrapper) {
695 $ENV{SELFTEST_INTERFACES} = $interfaces;
696 } else {
697 $ENV{SELFTEST_INTERFACES} = "";
699 if ($opt_quick) {
700 $ENV{SELFTEST_QUICK} = "1";
701 } else {
702 $ENV{SELFTEST_QUICK} = "";
704 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
706 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
707 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
709 my @available = ();
710 foreach my $fn (@testlists) {
711 foreach (read_testlist($fn)) {
712 my $name = $$_[0];
713 next if (@includes and not defined(find_in_list(\@includes, $name)));
714 push (@available, $_);
718 my $restricted = undef;
719 my $restricted_used = {};
721 if ($opt_load_list) {
722 $restricted = [];
723 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
724 while (<LOAD_LIST>) {
725 chomp;
726 push (@$restricted, $_);
728 close(LOAD_LIST);
731 my $individual_tests = undef;
732 $individual_tests = {};
734 foreach my $testsuite (@available) {
735 my $name = $$testsuite[0];
736 my $skipreason = skip(@$testsuite);
737 if (defined($restricted)) {
738 # Find the testsuite for this test
739 my $match = undef;
740 foreach my $r (@$restricted) {
741 if ($r eq $name) {
742 $individual_tests->{$name} = [];
743 $match = $r;
744 $restricted_used->{$r} = 1;
745 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
746 push(@{$individual_tests->{$name}}, $r);
747 $match = $r;
748 $restricted_used->{$r} = 1;
751 if ($match) {
752 if (defined($skipreason)) {
753 if (not $opt_list) {
754 Subunit::skip_testsuite($name, $skipreason);
756 } else {
757 push(@todo, $testsuite);
760 } elsif (defined($skipreason)) {
761 if (not $opt_list) {
762 Subunit::skip_testsuite($name, $skipreason);
764 } else {
765 push(@todo, $testsuite);
769 if (defined($restricted)) {
770 foreach (@$restricted) {
771 unless (defined($restricted_used->{$_})) {
772 print "No test or testsuite found matching $_\n";
775 } elsif ($#todo == -1) {
776 print STDERR "No tests to run\n";
777 exit(1);
780 my $suitestotal = $#todo + 1;
782 unless ($opt_list) {
783 Subunit::progress($suitestotal);
784 Subunit::report_time(time());
787 my $i = 0;
788 $| = 1;
790 my %running_envs = ();
792 sub get_running_env($)
794 my ($name) = @_;
796 my $envname = $name;
798 $envname =~ s/:.*//;
800 return $running_envs{$envname};
803 my @exported_envvars = (
804 # domain stuff
805 "DOMAIN",
806 "REALM",
808 # stuff related to a trusted domain
809 "TRUST_SERVER",
810 "TRUST_SERVER_IP",
811 "TRUST_SERVER_IPV6",
812 "TRUST_NETBIOSNAME",
813 "TRUST_USERNAME",
814 "TRUST_PASSWORD",
815 "TRUST_DOMAIN",
816 "TRUST_REALM",
818 # domain controller stuff
819 "DC_SERVER",
820 "DC_SERVER_IP",
821 "DC_SERVER_IPV6",
822 "DC_NETBIOSNAME",
823 "DC_NETBIOSALIAS",
825 # domain member
826 "MEMBER_SERVER",
827 "MEMBER_SERVER_IP",
828 "MEMBER_SERVER_IPV6",
829 "MEMBER_NETBIOSNAME",
830 "MEMBER_NETBIOSALIAS",
832 # rpc proxy controller stuff
833 "RPC_PROXY_SERVER",
834 "RPC_PROXY_SERVER_IP",
835 "RPC_PROXY_SERVER_IPV6",
836 "RPC_PROXY_NETBIOSNAME",
837 "RPC_PROXY_NETBIOSALIAS",
839 # domain controller stuff for Vampired DC
840 "VAMPIRE_DC_SERVER",
841 "VAMPIRE_DC_SERVER_IP",
842 "VAMPIRE_DC_SERVER_IPV6",
843 "VAMPIRE_DC_NETBIOSNAME",
844 "VAMPIRE_DC_NETBIOSALIAS",
846 # domain controller stuff for RODC
847 "RODC_DC_SERVER",
848 "RODC_DC_SERVER_IP",
849 "RODC_DC_SERVER_IPV6",
850 "RODC_DC_NETBIOSNAME",
852 # domain controller stuff for FL 2000 Vampired DC
853 "VAMPIRE_2000_DC_SERVER",
854 "VAMPIRE_2000_DC_SERVER_IP",
855 "VAMPIRE_2000_DC_SERVER_IPV6",
856 "VAMPIRE_2000_DC_NETBIOSNAME",
857 "VAMPIRE_2000_DC_NETBIOSALIAS",
859 "PROMOTED_DC_SERVER",
860 "PROMOTED_DC_SERVER_IP",
861 "PROMOTED_DC_SERVER_IPV6",
862 "PROMOTED_DC_NETBIOSNAME",
863 "PROMOTED_DC_NETBIOSALIAS",
865 # server stuff
866 "SERVER",
867 "SERVER_IP",
868 "SERVER_IPV6",
869 "NETBIOSNAME",
870 "NETBIOSALIAS",
872 # user stuff
873 "USERNAME",
874 "USERID",
875 "PASSWORD",
876 "DC_USERNAME",
877 "DC_PASSWORD",
879 # UID/GID for rfc2307 mapping tests
880 "UID_RFC2307TEST",
881 "GID_RFC2307TEST",
883 # misc stuff
884 "KRB5_CONFIG",
885 "KRB5CCNAME",
886 "SELFTEST_WINBINDD_SOCKET_DIR",
887 "NMBD_SOCKET_DIR",
888 "LOCAL_PATH",
889 "DNS_FORWARDER1",
890 "DNS_FORWARDER2",
891 "RESOLV_CONF",
892 "UNACCEPTABLE_PASSWORD",
893 "LOCK_DIR",
894 "SMBD_TEST_LOG",
896 # nss_wrapper
897 "NSS_WRAPPER_PASSWD",
898 "NSS_WRAPPER_GROUP",
899 "NSS_WRAPPER_HOSTS",
900 "NSS_WRAPPER_HOSTNAME",
901 "NSS_WRAPPER_MODULE_SO_PATH",
902 "NSS_WRAPPER_MODULE_FN_PREFIX",
904 # resolv_wrapper
905 "RESOLV_WRAPPER_CONF",
906 "RESOLV_WRAPPER_HOSTS",
909 sub sighandler($)
911 my $signame = shift;
913 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
914 $SIG{PIPE} = 'IGNORE';
916 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
918 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
919 teardown_env($_) foreach(keys %running_envs);
920 system("pstree -p $$");
921 print "$0: PID[$$]: Exiting...\n";
922 exit(1);
925 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
927 sub setup_env($$)
929 my ($name, $prefix) = @_;
931 my $testenv_vars = undef;
933 my $envname = $name;
934 my $option = $name;
936 $envname =~ s/:.*//;
937 $option =~ s/^[^:]*//;
938 $option =~ s/^://;
940 $option = "client" if $option eq "";
942 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
944 if (defined(get_running_env($envname))) {
945 $testenv_vars = get_running_env($envname);
946 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
947 print $testenv_vars->{target}->getlog_env($testenv_vars);
948 $testenv_vars = undef;
950 } else {
951 $testenv_vars = $target->setup_env($envname, $prefix);
952 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
953 return $testenv_vars;
954 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
955 $testenv_vars->{target} = $target;
957 if (not defined($testenv_vars)) {
958 warn("$opt_target can't start up known environment '$envname'");
962 return undef unless defined($testenv_vars);
964 $running_envs{$envname} = $testenv_vars;
966 if ($option eq "local") {
967 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
968 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
969 } elsif ($option eq "client") {
970 SocketWrapper::set_default_iface(11);
971 write_clientconf($conffile, $clientdir, $testenv_vars);
972 $ENV{SMB_CONF_PATH} = $conffile;
973 } else {
974 die("Unknown option[$option] for envname[$envname]");
977 foreach (@exported_envvars) {
978 if (defined($testenv_vars->{$_})) {
979 $ENV{$_} = $testenv_vars->{$_};
980 } else {
981 delete $ENV{$_};
985 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
986 unlink($krb5_ccache_path);
987 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
988 return $testenv_vars;
991 sub exported_envvars_str($)
993 my ($testenv_vars) = @_;
994 my $out = "";
996 foreach (@exported_envvars) {
997 next unless defined($testenv_vars->{$_});
998 $out .= $_."=".$testenv_vars->{$_}."\n";
1001 return $out;
1004 sub getlog_env($)
1006 my ($envname) = @_;
1007 return "" if ($envname eq "none");
1008 my $env = get_running_env($envname);
1009 return $env->{target}->getlog_env($env);
1012 sub check_env($)
1014 my ($envname) = @_;
1015 my $env = get_running_env($envname);
1016 return $env->{target}->check_env($env);
1019 sub teardown_env($)
1021 my ($envname) = @_;
1022 return if ($envname eq "none");
1023 print STDERR "teardown_env($envname)\n";
1024 my $env = get_running_env($envname);
1025 $env->{target}->teardown_env($env);
1026 delete $running_envs{$envname};
1029 # This 'global' file needs to be empty when we start
1030 unlink("$prefix_abs/dns_host_file");
1031 unlink("$prefix_abs/hosts");
1033 if ($opt_random_order) {
1034 require List::Util;
1035 my @newtodo = List::Util::shuffle(@todo);
1036 @todo = @newtodo;
1039 if ($opt_testenv) {
1040 my $testenv_name = $ENV{SELFTEST_TESTENV};
1041 $testenv_name = $testenv_default unless defined($testenv_name);
1043 my $testenv_vars = setup_env($testenv_name, $prefix);
1045 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1046 die("Unable to setup environment $testenv_name");
1049 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1050 $ENV{ENVNAME} = $testenv_name;
1052 my $envvarstr = exported_envvars_str($testenv_vars);
1054 my @term_args = ("echo -e \"
1055 Welcome to the Samba4 Test environment '$testenv_name'
1057 This matches the client environment used in make test
1058 server is pid `cat \$PIDDIR/samba.pid`
1060 Some useful environment variables:
1061 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1062 SMB_CONF_PATH=\$SMB_CONF_PATH
1064 $envvarstr
1065 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1066 my @term = ();
1067 if ($ENV{TERMINAL}) {
1068 @term = ($ENV{TERMINAL});
1069 } else {
1070 @term = ("xterm", "-e");
1071 unshift(@term_args, ("bash", "-c"));
1074 system(@term, @term_args);
1076 teardown_env($testenv_name);
1077 } elsif ($opt_list) {
1078 foreach (@todo) {
1079 my $name = $$_[0];
1080 my $envname = $$_[1];
1081 my $cmd = $$_[2];
1082 my $listcmd = $$_[3];
1084 unless (defined($listcmd)) {
1085 warn("Unable to list tests in $name");
1086 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1087 # a single "test".
1088 print "$name\n";
1089 next;
1092 system($listcmd);
1094 if ($? == -1) {
1095 die("Unable to run $listcmd: $!");
1096 } elsif ($? & 127) {
1097 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1100 my $exitcode = $? >> 8;
1101 if ($exitcode != 0) {
1102 die("$cmd exited with exit code $exitcode");
1105 } else {
1106 foreach (@todo) {
1107 $i++;
1108 my $cmd = $$_[2];
1109 my $name = $$_[0];
1110 my $envname = $$_[1];
1111 my $envvars = setup_env($envname, $prefix);
1113 if (not defined($envvars)) {
1114 Subunit::start_testsuite($name);
1115 Subunit::end_testsuite($name, "error",
1116 "unable to set up environment $envname - exiting");
1117 next;
1118 } elsif ($envvars eq "UNKNOWN") {
1119 Subunit::start_testsuite($name);
1120 Subunit::end_testsuite($name, "skip",
1121 "environment $envname is unknown in this test backend - skipping");
1122 next;
1125 # Generate a file with the individual tests to run, if the
1126 # test runner for this test suite supports it.
1127 if ($individual_tests and $individual_tests->{$name}) {
1128 if ($$_[3]) {
1129 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1130 foreach my $test (@{$individual_tests->{$name}}) {
1131 print $fh substr($test, length($name)+1) . "\n";
1133 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1134 } else {
1135 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1139 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1141 teardown_env($envname) if ($opt_resetup_env);
1145 print "\n";
1147 teardown_env($_) foreach (keys %running_envs);
1149 my $failed = 0;
1151 # if there were any valgrind failures, show them
1152 foreach (<$prefix/valgrind.log*>) {
1153 next unless (-s $_);
1154 print "VALGRIND FAILURE\n";
1155 $failed++;
1156 system("cat $_");
1158 exit 0;