s3:smb2_sesssetup: add smbd_smb2_session_setup_wrap_send/recv()
[Samba.git] / selftest / selftest.pl
blob96687ea2ea58f34fa7cc2f62ad0c40f72e1f1e4c
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;
31 eval {
32 require Time::HiRes;
33 Time::HiRes->import("time");
35 if ($@) {
36 print "You don't have Time::Hires installed !\n";
39 my $opt_help = 0;
40 my $opt_target = "samba";
41 my $opt_quick = 0;
42 my $opt_socket_wrapper = 0;
43 my $opt_socket_wrapper_pcap = undef;
44 my $opt_socket_wrapper_keep_pcap = undef;
45 my $opt_random_order = 0;
46 my $opt_one = 0;
47 my @opt_exclude = ();
48 my @opt_include = ();
49 my $opt_testenv = 0;
50 my $opt_list = 0;
51 my $ldap = undef;
52 my $opt_resetup_env = undef;
53 my $opt_binary_mapping = "";
54 my $opt_load_list = undef;
55 my $opt_libnss_wrapper_so_path = "";
56 my $opt_libsocket_wrapper_so_path = "";
57 my $opt_libuid_wrapper_so_path = "";
58 my @testlists = ();
60 my $srcdir = ".";
61 my $bindir = "./bin";
62 my $prefix = "./st";
64 my @includes = ();
65 my @excludes = ();
67 sub find_in_list($$)
69 my ($list, $fullname) = @_;
71 foreach (@$list) {
72 if ($fullname =~ /$$_[0]/) {
73 return ($$_[1]) if ($$_[1]);
74 return "";
78 return undef;
81 sub skip($)
83 my ($name) = @_;
85 return find_in_list(\@excludes, $name);
88 sub getlog_env($);
90 sub setup_pcap($)
92 my ($name) = @_;
94 return unless ($opt_socket_wrapper_pcap);
95 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
97 my $fname = $name;
98 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
100 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
102 SocketWrapper::setup_pcap($pcap_file);
104 return $pcap_file;
107 sub cleanup_pcap($$)
109 my ($pcap_file, $exitcode) = @_;
111 return unless ($opt_socket_wrapper_pcap);
112 return if ($opt_socket_wrapper_keep_pcap);
113 return unless ($exitcode == 0);
114 return unless defined($pcap_file);
116 unlink($pcap_file);
119 # expand strings from %ENV
120 sub expand_environment_strings($)
122 my $s = shift;
123 # we use a reverse sort so we do the longer ones first
124 foreach my $k (sort { $b cmp $a } keys %ENV) {
125 $s =~ s/\$$k/$ENV{$k}/g;
127 return $s;
130 sub run_testsuite($$$$$)
132 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
133 my $pcap_file = setup_pcap($name);
135 Subunit::start_testsuite($name);
136 Subunit::progress_push();
137 Subunit::report_time(time());
138 system($cmd);
139 Subunit::report_time(time());
140 Subunit::progress_pop();
142 if ($? == -1) {
143 Subunit::progress_pop();
144 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
145 exit(1);
146 } elsif ($? & 127) {
147 Subunit::end_testsuite($name, "error",
148 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
149 exit(1);
152 my $exitcode = $? >> 8;
154 my $envlog = getlog_env($envname);
155 if ($envlog ne "") {
156 print "envlog: $envlog\n";
159 print "command: $cmd\n";
160 printf "expanded command: %s\n", expand_environment_strings($cmd);
162 if ($exitcode == 0) {
163 Subunit::end_testsuite($name, "success");
164 } else {
165 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
168 cleanup_pcap($pcap_file, $exitcode);
170 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
171 print "PCAP FILE: $pcap_file\n";
174 if ($exitcode != 0) {
175 exit(1) if ($opt_one);
178 return $exitcode;
181 sub ShowHelp()
183 print "Samba test runner
184 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
185 Copyright (C) Stefan Metzmacher <metze\@samba.org>
187 Usage: $Script [OPTIONS] TESTNAME-REGEX
189 Generic options:
190 --help this help page
191 --target=samba[3]|win Samba version to target
192 --testlist=FILE file to read available tests from
194 Paths:
195 --prefix=DIR prefix to run tests in [st]
196 --srcdir=DIR source directory [.]
197 --bindir=DIR binaries directory [./bin]
199 Preload cwrap:
200 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
201 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
202 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
204 Target Specific:
205 --socket-wrapper-pcap save traffic to pcap directories
206 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
207 failed
208 --socket-wrapper enable socket wrapper
210 Samba4 Specific:
211 --ldap=openldap|fedora-ds back samba onto specified ldap server
213 Behaviour:
214 --quick run quick overall test
215 --one abort when the first test fails
216 --testenv run a shell in the requested test environment
217 --list list available tests
219 exit(0);
222 my $result = GetOptions (
223 'help|h|?' => \$opt_help,
224 'target=s' => \$opt_target,
225 'prefix=s' => \$prefix,
226 'socket-wrapper' => \$opt_socket_wrapper,
227 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
228 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
229 'quick' => \$opt_quick,
230 'one' => \$opt_one,
231 'exclude=s' => \@opt_exclude,
232 'include=s' => \@opt_include,
233 'srcdir=s' => \$srcdir,
234 'bindir=s' => \$bindir,
235 'testenv' => \$opt_testenv,
236 'list' => \$opt_list,
237 'ldap:s' => \$ldap,
238 'resetup-environment' => \$opt_resetup_env,
239 'testlist=s' => \@testlists,
240 'random-order' => \$opt_random_order,
241 'load-list=s' => \$opt_load_list,
242 'binary-mapping=s' => \$opt_binary_mapping,
243 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
244 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
245 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path
248 exit(1) if (not $result);
250 ShowHelp() if ($opt_help);
252 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
254 # we want unbuffered output
255 $| = 1;
257 my @tests = @ARGV;
259 # quick hack to disable rpc validation when using valgrind - its way too slow
260 unless (defined($ENV{VALGRIND})) {
261 $ENV{VALIDATE} = "validate";
262 $ENV{MALLOC_CHECK_} = 3;
265 # make all our python scripts unbuffered
266 $ENV{PYTHONUNBUFFERED} = 1;
268 my $bindir_abs = abs_path($bindir);
270 # Backwards compatibility:
271 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
272 if (defined($ENV{FEDORA_DS_ROOT})) {
273 $ldap = "fedora-ds";
274 } else {
275 $ldap = "openldap";
279 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
280 if ($ldap) {
281 # LDAP is slow
282 $torture_maxtime *= 2;
285 $prefix =~ s+//+/+;
286 $prefix =~ s+/./+/+;
287 $prefix =~ s+/$++;
289 die("using an empty prefix isn't allowed") unless $prefix ne "";
291 # Ensure we have the test prefix around.
293 # We need restrictive
294 # permissions on this as some subdirectories in this tree will have
295 # wider permissions (ie 0777) and this would allow other users on the
296 # host to subvert the test process.
297 mkdir($prefix, 0700) unless -d $prefix;
298 chmod 0700, $prefix;
300 my $prefix_abs = abs_path($prefix);
301 my $tmpdir_abs = abs_path("$prefix/tmp");
302 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
304 my $srcdir_abs = abs_path($srcdir);
306 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
307 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
309 $ENV{PREFIX} = $prefix;
310 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
311 $ENV{PREFIX_ABS} = $prefix_abs;
312 $ENV{SRCDIR} = $srcdir;
313 $ENV{SRCDIR_ABS} = $srcdir_abs;
314 $ENV{BINDIR} = $bindir_abs;
316 my $tls_enabled = not $opt_quick;
317 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
319 sub prefix_pathvar($$)
321 my ($name, $newpath) = @_;
322 if (defined($ENV{$name})) {
323 $ENV{$name} = "$newpath:$ENV{$name}";
324 } else {
325 $ENV{$name} = $newpath;
328 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
329 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
331 if ($opt_socket_wrapper_keep_pcap) {
332 # Socket wrapper keep pcap implies socket wrapper pcap
333 $opt_socket_wrapper_pcap = 1;
336 if ($opt_socket_wrapper_pcap) {
337 # Socket wrapper pcap implies socket wrapper
338 $opt_socket_wrapper = 1;
341 my $ld_preload = $ENV{LD_PRELOAD};
343 if ($opt_libnss_wrapper_so_path) {
344 if ($ld_preload) {
345 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
346 } else {
347 $ld_preload = "$opt_libnss_wrapper_so_path";
351 if ($opt_libsocket_wrapper_so_path) {
352 if ($ld_preload) {
353 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
354 } else {
355 $ld_preload = "$opt_libsocket_wrapper_so_path";
359 if ($opt_libuid_wrapper_so_path) {
360 if ($ld_preload) {
361 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
362 } else {
363 $ld_preload = "$opt_libuid_wrapper_so_path";
367 $ENV{LD_PRELOAD} = $ld_preload;
368 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
370 # Enable uid_wrapper globally
371 $ENV{UID_WRAPPER} = 1;
373 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
375 # This is needed in order to allow the ldb_*ldap module
376 # to work with a preloaded socket wrapper.
377 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
379 my $socket_wrapper_dir;
380 if ($opt_socket_wrapper) {
381 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
382 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
383 } elsif (not $opt_list) {
384 unless ($< == 0) {
385 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
389 my $target;
390 my $testenv_default = "none";
392 my %binary_mapping = ();
393 if ($opt_binary_mapping) {
394 my @binmapping_list = split(/,/, $opt_binary_mapping);
395 foreach my $mapping (@binmapping_list) {
396 my ($bin, $map) = split(/\:/, $mapping);
397 $binary_mapping{$bin} = $map;
401 $ENV{BINARY_MAPPING} = $opt_binary_mapping;
403 # After this many seconds, the server will self-terminate. All tests
404 # must terminate in this time, and testenv will only stay alive this
405 # long
407 my $server_maxtime = 7500;
408 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
409 $server_maxtime = $ENV{SMBD_MAXTIME};
412 unless ($opt_list) {
413 if ($opt_target eq "samba") {
414 $testenv_default = "dc";
415 require target::Samba;
416 $target = new Samba($bindir, \%binary_mapping, $ldap, $srcdir, $server_maxtime);
417 } elsif ($opt_target eq "samba3") {
418 $testenv_default = "member";
419 require target::Samba3;
420 $target = new Samba3($bindir, \%binary_mapping, $srcdir_abs, $server_maxtime);
424 sub read_test_regexes($)
426 my ($name) = @_;
427 my @ret = ();
428 open(LF, "<$name") or die("unable to read $name: $!");
429 while (<LF>) {
430 chomp;
431 next if (/^#/);
432 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
433 push (@ret, [$1, $4]);
434 } else {
435 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
436 push (@ret, [$_, undef]);
439 close(LF);
440 return @ret;
443 foreach (@opt_exclude) {
444 push (@excludes, read_test_regexes($_));
447 foreach (@opt_include) {
448 push (@includes, read_test_regexes($_));
451 my $interfaces = join(',', ("127.0.0.11/8",
452 "127.0.0.12/8",
453 "127.0.0.13/8",
454 "127.0.0.14/8",
455 "127.0.0.15/8",
456 "127.0.0.16/8"));
458 my $clientdir = "$prefix_abs/client";
460 my $conffile = "$clientdir/client.conf";
461 $ENV{SMB_CONF_PATH} = $conffile;
463 sub write_clientconf($$$)
465 my ($conffile, $clientdir, $vars) = @_;
467 mkdir("$clientdir", 0777) unless -d "$clientdir";
469 if ( -d "$clientdir/private" ) {
470 unlink <$clientdir/private/*>;
471 } else {
472 mkdir("$clientdir/private", 0777);
475 if ( -d "$clientdir/lockdir" ) {
476 unlink <$clientdir/lockdir/*>;
477 } else {
478 mkdir("$clientdir/lockdir", 0777);
481 if ( -d "$clientdir/statedir" ) {
482 unlink <$clientdir/statedir/*>;
483 } else {
484 mkdir("$clientdir/statedir", 0777);
487 if ( -d "$clientdir/cachedir" ) {
488 unlink <$clientdir/cachedir/*>;
489 } else {
490 mkdir("$clientdir/cachedir", 0777);
493 # this is ugly, but the ncalrpcdir needs exactly 0755
494 # otherwise tests fail.
495 my $mask = umask;
496 umask 0022;
497 if ( -d "$clientdir/ncalrpcdir/np" ) {
498 unlink <$clientdir/ncalrpcdir/np/*>;
499 rmdir "$clientdir/ncalrpcdir/np";
501 if ( -d "$clientdir/ncalrpcdir" ) {
502 unlink <$clientdir/ncalrpcdir/*>;
503 rmdir "$clientdir/ncalrpcdir";
505 mkdir("$clientdir/ncalrpcdir", 0755);
506 umask $mask;
508 open(CF, ">$conffile");
509 print CF "[global]\n";
510 print CF "\tnetbios name = client\n";
511 if (defined($vars->{DOMAIN})) {
512 print CF "\tworkgroup = $vars->{DOMAIN}\n";
514 if (defined($vars->{REALM})) {
515 print CF "\trealm = $vars->{REALM}\n";
517 if ($opt_socket_wrapper) {
518 print CF "\tinterfaces = $interfaces\n";
520 print CF "
521 private dir = $clientdir/private
522 lock dir = $clientdir/lockdir
523 state directory = $clientdir/statedir
524 cache directory = $clientdir/cachedir
525 ncalrpc dir = $clientdir/ncalrpcdir
526 name resolve order = file bcast
527 panic action = $RealBin/gdb_backtrace \%d
528 max xmit = 32K
529 notify:inotify = false
530 ldb:nosync = true
531 system:anonymous = true
532 client lanman auth = Yes
533 log level = 1
534 torture:basedir = $clientdir
535 #We don't want to pass our self-tests if the PAC code is wrong
536 gensec:require_pac = true
537 resolv:host file = $prefix_abs/dns_host_file
538 #We don't want to run 'speed' tests for very long
539 torture:timelimit = 1
540 winbind separator = /
542 close(CF);
545 my @todo = ();
547 sub should_run_test($)
549 my $name = shift;
550 if ($#tests == -1) {
551 return 1;
553 for (my $i=0; $i <= $#tests; $i++) {
554 if ($name =~ /$tests[$i]/i) {
555 return 1;
558 return 0;
561 sub read_testlist($)
563 my ($filename) = @_;
565 my @ret = ();
566 open(IN, $filename) or die("Unable to open $filename: $!");
568 while (<IN>) {
569 if (/-- TEST(-LOADLIST|) --\n/) {
570 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
571 my $name = <IN>;
572 $name =~ s/\n//g;
573 my $env = <IN>;
574 $env =~ s/\n//g;
575 my $loadlist;
576 if ($supports_loadlist) {
577 $loadlist = <IN>;
578 $loadlist =~ s/\n//g;
580 my $cmdline = <IN>;
581 $cmdline =~ s/\n//g;
582 if (should_run_test($name) == 1) {
583 push (@ret, [$name, $env, $cmdline, $loadlist]);
585 } else {
586 print;
589 close(IN) or die("Error creating recipe from $filename");
590 return @ret;
593 if ($#testlists == -1) {
594 die("No testlists specified");
597 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
598 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
599 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
600 if ($opt_socket_wrapper) {
601 $ENV{SELFTEST_INTERFACES} = $interfaces;
602 } else {
603 $ENV{SELFTEST_INTERFACES} = "";
605 if ($opt_quick) {
606 $ENV{SELFTEST_QUICK} = "1";
607 } else {
608 $ENV{SELFTEST_QUICK} = "";
610 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
612 my @available = ();
613 foreach my $fn (@testlists) {
614 foreach (read_testlist($fn)) {
615 my $name = $$_[0];
616 next if (@includes and not defined(find_in_list(\@includes, $name)));
617 push (@available, $_);
621 my $restricted = undef;
622 my $restricted_used = {};
624 if ($opt_load_list) {
625 $restricted = [];
626 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
627 while (<LOAD_LIST>) {
628 chomp;
629 push (@$restricted, $_);
631 close(LOAD_LIST);
634 my $individual_tests = undef;
635 $individual_tests = {};
637 foreach my $testsuite (@available) {
638 my $name = $$testsuite[0];
639 my $skipreason = skip($name);
640 if (defined($restricted)) {
641 # Find the testsuite for this test
642 my $match = undef;
643 foreach my $r (@$restricted) {
644 if ($r eq $name) {
645 $individual_tests->{$name} = [];
646 $match = $r;
647 $restricted_used->{$r} = 1;
648 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
649 push(@{$individual_tests->{$name}}, $r);
650 $match = $r;
651 $restricted_used->{$r} = 1;
654 if ($match) {
655 if (defined($skipreason)) {
656 if (not $opt_list) {
657 Subunit::skip_testsuite($name, $skipreason);
659 } else {
660 push(@todo, $testsuite);
663 } elsif (defined($skipreason)) {
664 if (not $opt_list) {
665 Subunit::skip_testsuite($name, $skipreason);
667 } else {
668 push(@todo, $testsuite);
672 if (defined($restricted)) {
673 foreach (@$restricted) {
674 unless (defined($restricted_used->{$_})) {
675 print "No test or testsuite found matching $_\n";
678 } elsif ($#todo == -1) {
679 print STDERR "No tests to run\n";
680 exit(1);
683 my $suitestotal = $#todo + 1;
685 unless ($opt_list) {
686 Subunit::progress($suitestotal);
687 Subunit::report_time(time());
690 my $i = 0;
691 $| = 1;
693 my %running_envs = ();
695 sub get_running_env($)
697 my ($name) = @_;
699 my $envname = $name;
701 $envname =~ s/:.*//;
703 return $running_envs{$envname};
706 my @exported_envvars = (
707 # domain stuff
708 "DOMAIN",
709 "REALM",
711 # domain controller stuff
712 "DC_SERVER",
713 "DC_SERVER_IP",
714 "DC_SERVER_IPV6",
715 "DC_NETBIOSNAME",
716 "DC_NETBIOSALIAS",
718 # domain member
719 "MEMBER_SERVER",
720 "MEMBER_SERVER_IP",
721 "MEMBER_SERVER_IPV6",
722 "MEMBER_NETBIOSNAME",
723 "MEMBER_NETBIOSALIAS",
725 # rpc proxy controller stuff
726 "RPC_PROXY_SERVER",
727 "RPC_PROXY_SERVER_IP",
728 "RPC_PROXY_SERVER_IPV6",
729 "RPC_PROXY_NETBIOSNAME",
730 "RPC_PROXY_NETBIOSALIAS",
732 # domain controller stuff for Vampired DC
733 "VAMPIRE_DC_SERVER",
734 "VAMPIRE_DC_SERVER_IP",
735 "VAMPIRE_DC_SERVER_IPV6",
736 "VAMPIRE_DC_NETBIOSNAME",
737 "VAMPIRE_DC_NETBIOSALIAS",
739 "PROMOTED_DC_SERVER",
740 "PROMOTED_DC_SERVER_IP",
741 "PROMOTED_DC_SERVER_IPV6",
742 "PROMOTED_DC_NETBIOSNAME",
743 "PROMOTED_DC_NETBIOSALIAS",
745 # server stuff
746 "SERVER",
747 "SERVER_IP",
748 "SERVER_IPV6",
749 "NETBIOSNAME",
750 "NETBIOSALIAS",
752 # user stuff
753 "USERNAME",
754 "USERID",
755 "PASSWORD",
756 "DC_USERNAME",
757 "DC_PASSWORD",
759 # misc stuff
760 "KRB5_CONFIG",
761 "SELFTEST_WINBINDD_SOCKET_DIR",
762 "WINBINDD_PRIV_PIPE_DIR",
763 "NMBD_SOCKET_DIR",
764 "LOCAL_PATH",
766 # nss_wrapper
767 "NSS_WRAPPER_PASSWD",
768 "NSS_WRAPPER_GROUP",
770 # UID/GID for rfc2307 mapping tests
771 "UID_RFC2307TEST",
772 "GID_RFC2307TEST"
775 sub sighandler($)
777 my $signame = shift;
779 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
780 $SIG{PIPE} = 'IGNORE';
782 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
784 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
785 teardown_env($_) foreach(keys %running_envs);
786 system("pstree -p $$");
787 print "$0: PID[$$]: Exiting...\n";
788 exit(1);
791 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
793 sub setup_env($$)
795 my ($name, $prefix) = @_;
797 my $testenv_vars = undef;
799 my $envname = $name;
800 my $option = $name;
802 $envname =~ s/:.*//;
803 $option =~ s/^[^:]*//;
804 $option =~ s/^://;
806 $option = "client" if $option eq "";
808 if ($envname eq "none") {
809 $testenv_vars = {};
810 } elsif (defined(get_running_env($envname))) {
811 $testenv_vars = get_running_env($envname);
812 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
813 print $testenv_vars->{target}->getlog_env($testenv_vars);
814 $testenv_vars = undef;
816 } else {
817 $testenv_vars = $target->setup_env($envname, $prefix);
818 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
819 return $testenv_vars;
820 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
821 $testenv_vars->{target} = $target;
823 if (not defined($testenv_vars)) {
824 warn("$opt_target can't start up known environment '$envname'");
828 return undef unless defined($testenv_vars);
830 $running_envs{$envname} = $testenv_vars;
832 if ($option eq "local") {
833 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
834 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
835 } elsif ($option eq "client") {
836 SocketWrapper::set_default_iface(11);
837 write_clientconf($conffile, $clientdir, $testenv_vars);
838 $ENV{SMB_CONF_PATH} = $conffile;
839 } else {
840 die("Unknown option[$option] for envname[$envname]");
843 foreach (@exported_envvars) {
844 if (defined($testenv_vars->{$_})) {
845 $ENV{$_} = $testenv_vars->{$_};
846 } else {
847 delete $ENV{$_};
851 return $testenv_vars;
854 sub exported_envvars_str($)
856 my ($testenv_vars) = @_;
857 my $out = "";
859 foreach (@exported_envvars) {
860 next unless defined($testenv_vars->{$_});
861 $out .= $_."=".$testenv_vars->{$_}."\n";
864 return $out;
867 sub getlog_env($)
869 my ($envname) = @_;
870 return "" if ($envname eq "none");
871 my $env = get_running_env($envname);
872 return $env->{target}->getlog_env($env);
875 sub check_env($)
877 my ($envname) = @_;
878 return 1 if ($envname eq "none");
879 my $env = get_running_env($envname);
880 return $env->{target}->check_env($env);
883 sub teardown_env($)
885 my ($envname) = @_;
886 return if ($envname eq "none");
887 print STDERR "teardown_env($envname)\n";
888 my $env = get_running_env($envname);
889 $env->{target}->teardown_env($env);
890 delete $running_envs{$envname};
893 # This 'global' file needs to be empty when we start
894 unlink("$prefix_abs/dns_host_file");
895 unlink("$prefix_abs/hosts");
897 if ($opt_random_order) {
898 require List::Util;
899 my @newtodo = List::Util::shuffle(@todo);
900 @todo = @newtodo;
903 if ($opt_testenv) {
904 my $testenv_name = $ENV{SELFTEST_TESTENV};
905 $testenv_name = $testenv_default unless defined($testenv_name);
907 my $testenv_vars = setup_env($testenv_name, $prefix);
909 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
910 die("Unable to setup environment $testenv_name");
913 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
914 $ENV{ENVNAME} = $testenv_name;
916 my $envvarstr = exported_envvars_str($testenv_vars);
918 my @term_args = ("echo -e \"
919 Welcome to the Samba4 Test environment '$testenv_name'
921 This matches the client environment used in make test
922 server is pid `cat \$PIDDIR/samba.pid`
924 Some useful environment variables:
925 TORTURE_OPTIONS=\$TORTURE_OPTIONS
926 SMB_CONF_PATH=\$SMB_CONF_PATH
928 $envvarstr
929 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
930 my @term = ();
931 if ($ENV{TERMINAL}) {
932 @term = ($ENV{TERMINAL});
933 } else {
934 @term = ("xterm", "-e");
935 unshift(@term_args, ("bash", "-c"));
938 system(@term, @term_args);
940 teardown_env($testenv_name);
941 } elsif ($opt_list) {
942 foreach (@todo) {
943 my $name = $$_[0];
944 my $envname = $$_[1];
945 my $cmd = $$_[2];
946 my $listcmd = $$_[3];
948 unless (defined($listcmd)) {
949 warn("Unable to list tests in $name");
950 next;
953 system($listcmd);
955 if ($? == -1) {
956 die("Unable to run $listcmd: $!");
957 } elsif ($? & 127) {
958 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
961 my $exitcode = $? >> 8;
962 if ($exitcode != 0) {
963 die("$cmd exited with exit code $exitcode");
966 } else {
967 foreach (@todo) {
968 $i++;
969 my $cmd = $$_[2];
970 my $name = $$_[0];
971 my $envname = $$_[1];
973 my $envvars = setup_env($envname, $prefix);
974 if (not defined($envvars)) {
975 Subunit::start_testsuite($name);
976 Subunit::end_testsuite($name, "error",
977 "unable to set up environment $envname - exiting");
978 next;
979 } elsif ($envvars eq "UNKNOWN") {
980 Subunit::start_testsuite($name);
981 Subunit::end_testsuite($name, "skip",
982 "environment $envname is unknown in this test backend - skipping");
983 next;
986 # Generate a file with the individual tests to run, if the
987 # test runner for this test suite supports it.
988 if ($individual_tests and $individual_tests->{$name}) {
989 if ($$_[3]) {
990 my ($fh, $listid_file) = tempfile(UNLINK => 0);
991 foreach my $test (@{$individual_tests->{$name}}) {
992 print $fh substr($test, length($name)+1) . "\n";
994 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
995 } else {
996 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1000 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1002 teardown_env($envname) if ($opt_resetup_env);
1006 print "\n";
1008 teardown_env($_) foreach (keys %running_envs);
1010 my $failed = 0;
1012 # if there were any valgrind failures, show them
1013 foreach (<$prefix/valgrind.log*>) {
1014 next unless (-s $_);
1015 print "VALGRIND FAILURE\n";
1016 $failed++;
1017 system("cat $_");
1019 exit 0;