docs:smb.conf: fix typo in 'smb encrypt' text.
[Samba.git] / selftest / selftest.pl
blob779847f05a8df4779257867e234b32434fb09300
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_load_list = undef;
54 my $opt_libnss_wrapper_so_path = "";
55 my $opt_libresolv_wrapper_so_path = "";
56 my $opt_libsocket_wrapper_so_path = "";
57 my $opt_libuid_wrapper_so_path = "";
58 my $opt_use_dns_faking = 0;
59 my @testlists = ();
61 my $srcdir = ".";
62 my $bindir = "./bin";
63 my $prefix = "./st";
65 my @includes = ();
66 my @excludes = ();
68 sub find_in_list($$)
70 my ($list, $fullname) = @_;
72 foreach (@$list) {
73 if ($fullname =~ /$$_[0]/) {
74 return ($$_[1]) if ($$_[1]);
75 return "";
79 return undef;
82 sub skip($)
84 my ($name) = @_;
86 return find_in_list(\@excludes, $name);
89 sub getlog_env($);
91 sub setup_pcap($)
93 my ($name) = @_;
95 return unless ($opt_socket_wrapper_pcap);
96 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
98 my $fname = $name;
99 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
101 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
103 SocketWrapper::setup_pcap($pcap_file);
105 return $pcap_file;
108 sub cleanup_pcap($$)
110 my ($pcap_file, $exitcode) = @_;
112 return unless ($opt_socket_wrapper_pcap);
113 return if ($opt_socket_wrapper_keep_pcap);
114 return unless ($exitcode == 0);
115 return unless defined($pcap_file);
117 unlink($pcap_file);
120 # expand strings from %ENV
121 sub expand_environment_strings($)
123 my $s = shift;
124 # we use a reverse sort so we do the longer ones first
125 foreach my $k (sort { $b cmp $a } keys %ENV) {
126 $s =~ s/\$$k/$ENV{$k}/g;
128 return $s;
131 sub run_testsuite($$$$$)
133 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
134 my $pcap_file = setup_pcap($name);
136 Subunit::start_testsuite($name);
137 Subunit::progress_push();
138 Subunit::report_time(time());
139 system($cmd);
140 Subunit::report_time(time());
141 Subunit::progress_pop();
143 if ($? == -1) {
144 Subunit::progress_pop();
145 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
146 exit(1);
147 } elsif ($? & 127) {
148 Subunit::end_testsuite($name, "error",
149 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
150 exit(1);
153 my $exitcode = $? >> 8;
155 my $envlog = getlog_env($envname);
156 if ($envlog ne "") {
157 print "envlog: $envlog\n";
160 print "command: $cmd\n";
161 printf "expanded command: %s\n", expand_environment_strings($cmd);
163 if ($exitcode == 0) {
164 Subunit::end_testsuite($name, "success");
165 } else {
166 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
169 cleanup_pcap($pcap_file, $exitcode);
171 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
172 print "PCAP FILE: $pcap_file\n";
175 if ($exitcode != 0) {
176 exit(1) if ($opt_one);
179 return $exitcode;
182 sub ShowHelp()
184 print "Samba test runner
185 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
186 Copyright (C) Stefan Metzmacher <metze\@samba.org>
188 Usage: $Script [OPTIONS] TESTNAME-REGEX
190 Generic options:
191 --help this help page
192 --target=samba[3]|win Samba version to target
193 --testlist=FILE file to read available tests from
194 --exclude=FILE Exclude tests listed in the file
195 --include=FILE Include tests listed in the file
197 Paths:
198 --prefix=DIR prefix to run tests in [st]
199 --srcdir=DIR source directory [.]
200 --bindir=DIR binaries directory [./bin]
202 Preload cwrap:
203 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
204 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
205 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
206 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
208 DNS:
209 --use-dns-faking Fake DNS entries rather than talking to our
210 DNS implementation.
212 Target Specific:
213 --socket-wrapper-pcap save traffic to pcap directories
214 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
215 failed
216 --socket-wrapper enable socket wrapper
218 Samba4 Specific:
219 --ldap=openldap|fedora-ds back samba onto specified ldap server
221 Behaviour:
222 --quick run quick overall test
223 --one abort when the first test fails
224 --testenv run a shell in the requested test environment
225 --list list available tests
227 exit(0);
230 my $result = GetOptions (
231 'help|h|?' => \$opt_help,
232 'target=s' => \$opt_target,
233 'prefix=s' => \$prefix,
234 'socket-wrapper' => \$opt_socket_wrapper,
235 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
236 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
237 'quick' => \$opt_quick,
238 'one' => \$opt_one,
239 'exclude=s' => \@opt_exclude,
240 'include=s' => \@opt_include,
241 'srcdir=s' => \$srcdir,
242 'bindir=s' => \$bindir,
243 'testenv' => \$opt_testenv,
244 'list' => \$opt_list,
245 'ldap:s' => \$ldap,
246 'resetup-environment' => \$opt_resetup_env,
247 'testlist=s' => \@testlists,
248 'random-order' => \$opt_random_order,
249 'load-list=s' => \$opt_load_list,
250 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
251 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
252 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
253 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
254 'use-dns-faking' => \$opt_use_dns_faking
257 exit(1) if (not $result);
259 ShowHelp() if ($opt_help);
261 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
263 # we want unbuffered output
264 $| = 1;
266 my @tests = @ARGV;
268 # quick hack to disable rpc validation when using valgrind - its way too slow
269 unless (defined($ENV{VALGRIND})) {
270 $ENV{VALIDATE} = "validate";
271 $ENV{MALLOC_CHECK_} = 3;
274 # make all our python scripts unbuffered
275 $ENV{PYTHONUNBUFFERED} = 1;
277 my $bindir_abs = abs_path($bindir);
279 # Backwards compatibility:
280 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
281 if (defined($ENV{FEDORA_DS_ROOT})) {
282 $ldap = "fedora-ds";
283 } else {
284 $ldap = "openldap";
288 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
289 if ($ldap) {
290 # LDAP is slow
291 $torture_maxtime *= 2;
294 $prefix =~ s+//+/+;
295 $prefix =~ s+/./+/+;
296 $prefix =~ s+/$++;
298 die("using an empty prefix isn't allowed") unless $prefix ne "";
300 # Ensure we have the test prefix around.
302 # We need restrictive
303 # permissions on this as some subdirectories in this tree will have
304 # wider permissions (ie 0777) and this would allow other users on the
305 # host to subvert the test process.
306 mkdir($prefix, 0700) unless -d $prefix;
307 chmod 0700, $prefix;
309 my $prefix_abs = abs_path($prefix);
310 my $tmpdir_abs = abs_path("$prefix/tmp");
311 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
313 my $srcdir_abs = abs_path($srcdir);
315 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
316 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
318 $ENV{PREFIX} = $prefix;
319 $ENV{KRB5CCNAME} = "$prefix/krb5ticket";
320 $ENV{PREFIX_ABS} = $prefix_abs;
321 $ENV{SRCDIR} = $srcdir;
322 $ENV{SRCDIR_ABS} = $srcdir_abs;
323 $ENV{BINDIR} = $bindir_abs;
325 my $tls_enabled = not $opt_quick;
326 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
328 sub prefix_pathvar($$)
330 my ($name, $newpath) = @_;
331 if (defined($ENV{$name})) {
332 $ENV{$name} = "$newpath:$ENV{$name}";
333 } else {
334 $ENV{$name} = $newpath;
337 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
338 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
340 if ($opt_socket_wrapper_keep_pcap) {
341 # Socket wrapper keep pcap implies socket wrapper pcap
342 $opt_socket_wrapper_pcap = 1;
345 if ($opt_socket_wrapper_pcap) {
346 # Socket wrapper pcap implies socket wrapper
347 $opt_socket_wrapper = 1;
350 my $ld_preload = $ENV{LD_PRELOAD};
352 if ($opt_libnss_wrapper_so_path) {
353 if ($ld_preload) {
354 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
355 } else {
356 $ld_preload = "$opt_libnss_wrapper_so_path";
360 if ($opt_libresolv_wrapper_so_path) {
361 if ($ld_preload) {
362 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
363 } else {
364 $ld_preload = "$opt_libresolv_wrapper_so_path";
368 if ($opt_libsocket_wrapper_so_path) {
369 if ($ld_preload) {
370 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
371 } else {
372 $ld_preload = "$opt_libsocket_wrapper_so_path";
376 if ($opt_libuid_wrapper_so_path) {
377 if ($ld_preload) {
378 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
379 } else {
380 $ld_preload = "$opt_libuid_wrapper_so_path";
384 $ENV{LD_PRELOAD} = $ld_preload;
385 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
387 # Enable uid_wrapper globally
388 $ENV{UID_WRAPPER} = 1;
390 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
392 # This is needed in order to allow the ldb_*ldap module
393 # to work with a preloaded socket wrapper.
394 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
396 my $socket_wrapper_dir;
397 if ($opt_socket_wrapper) {
398 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
399 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
400 } elsif (not $opt_list) {
401 unless ($< == 0) {
402 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
406 if ($opt_use_dns_faking) {
407 print "DNS: Faking nameserver\n";
408 $ENV{SAMBA_DNS_FAKING} = 1;
411 my $target;
412 my $testenv_default = "none";
414 # After this many seconds, the server will self-terminate. All tests
415 # must terminate in this time, and testenv will only stay alive this
416 # long
418 my $server_maxtime = 10800;
419 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
420 $server_maxtime = $ENV{SMBD_MAXTIME};
423 unless ($opt_list) {
424 if ($opt_target eq "samba") {
425 $testenv_default = "ad_dc_ntvfs";
426 require target::Samba;
427 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
428 } elsif ($opt_target eq "samba3") {
429 $testenv_default = "nt4_member";
430 require target::Samba3;
431 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
435 sub read_test_regexes($)
437 my ($name) = @_;
438 my @ret = ();
439 open(LF, "<$name") or die("unable to read $name: $!");
440 while (<LF>) {
441 chomp;
442 next if (/^#/);
443 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
444 push (@ret, [$1, $4]);
445 } else {
446 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
447 push (@ret, [$_, undef]);
450 close(LF);
451 return @ret;
454 foreach (@opt_exclude) {
455 push (@excludes, read_test_regexes($_));
458 foreach (@opt_include) {
459 push (@includes, read_test_regexes($_));
462 my $interfaces = join(',', ("127.0.0.11/8",
463 "127.0.0.12/8",
464 "127.0.0.13/8",
465 "127.0.0.14/8",
466 "127.0.0.15/8",
467 "127.0.0.16/8"));
469 my $clientdir = "$prefix_abs/client";
471 my $conffile = "$clientdir/client.conf";
472 $ENV{SMB_CONF_PATH} = $conffile;
474 sub write_clientconf($$$)
476 my ($conffile, $clientdir, $vars) = @_;
478 mkdir("$clientdir", 0777) unless -d "$clientdir";
480 if ( -d "$clientdir/private" ) {
481 unlink <$clientdir/private/*>;
482 } else {
483 mkdir("$clientdir/private", 0777);
486 if ( -d "$clientdir/lockdir" ) {
487 unlink <$clientdir/lockdir/*>;
488 } else {
489 mkdir("$clientdir/lockdir", 0777);
492 if ( -d "$clientdir/statedir" ) {
493 unlink <$clientdir/statedir/*>;
494 } else {
495 mkdir("$clientdir/statedir", 0777);
498 if ( -d "$clientdir/cachedir" ) {
499 unlink <$clientdir/cachedir/*>;
500 } else {
501 mkdir("$clientdir/cachedir", 0777);
504 # this is ugly, but the ncalrpcdir needs exactly 0755
505 # otherwise tests fail.
506 my $mask = umask;
507 umask 0022;
508 if ( -d "$clientdir/ncalrpcdir/np" ) {
509 unlink <$clientdir/ncalrpcdir/np/*>;
510 rmdir "$clientdir/ncalrpcdir/np";
512 if ( -d "$clientdir/ncalrpcdir" ) {
513 unlink <$clientdir/ncalrpcdir/*>;
514 rmdir "$clientdir/ncalrpcdir";
516 mkdir("$clientdir/ncalrpcdir", 0755);
517 umask $mask;
519 open(CF, ">$conffile");
520 print CF "[global]\n";
521 print CF "\tnetbios name = client\n";
522 if (defined($vars->{DOMAIN})) {
523 print CF "\tworkgroup = $vars->{DOMAIN}\n";
525 if (defined($vars->{REALM})) {
526 print CF "\trealm = $vars->{REALM}\n";
528 if ($opt_socket_wrapper) {
529 print CF "\tinterfaces = $interfaces\n";
531 print CF "
532 private dir = $clientdir/private
533 lock dir = $clientdir/lockdir
534 state directory = $clientdir/statedir
535 cache directory = $clientdir/cachedir
536 ncalrpc dir = $clientdir/ncalrpcdir
537 panic action = $RealBin/gdb_backtrace \%d
538 max xmit = 32K
539 notify:inotify = false
540 ldb:nosync = true
541 system:anonymous = true
542 client lanman auth = Yes
543 log level = 1
544 torture:basedir = $clientdir
545 #We don't want to pass our self-tests if the PAC code is wrong
546 gensec:require_pac = true
547 #We don't want to run 'speed' tests for very long
548 torture:timelimit = 1
549 winbind separator = /
551 close(CF);
554 my @todo = ();
556 sub should_run_test($)
558 my $name = shift;
559 if ($#tests == -1) {
560 return 1;
562 for (my $i=0; $i <= $#tests; $i++) {
563 if ($name =~ /$tests[$i]/i) {
564 return 1;
567 return 0;
570 sub read_testlist($)
572 my ($filename) = @_;
574 my @ret = ();
575 open(IN, $filename) or die("Unable to open $filename: $!");
577 while (<IN>) {
578 if (/-- TEST(-LOADLIST|) --\n/) {
579 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
580 my $name = <IN>;
581 $name =~ s/\n//g;
582 my $env = <IN>;
583 $env =~ s/\n//g;
584 my $loadlist;
585 if ($supports_loadlist) {
586 $loadlist = <IN>;
587 $loadlist =~ s/\n//g;
589 my $cmdline = <IN>;
590 $cmdline =~ s/\n//g;
591 if (should_run_test($name) == 1) {
592 push (@ret, [$name, $env, $cmdline, $loadlist]);
594 } else {
595 print;
598 close(IN) or die("Error creating recipe from $filename");
599 return @ret;
602 if ($#testlists == -1) {
603 die("No testlists specified");
606 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
607 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
608 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
609 if ($opt_socket_wrapper) {
610 $ENV{SELFTEST_INTERFACES} = $interfaces;
611 } else {
612 $ENV{SELFTEST_INTERFACES} = "";
614 if ($opt_quick) {
615 $ENV{SELFTEST_QUICK} = "1";
616 } else {
617 $ENV{SELFTEST_QUICK} = "";
619 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
621 my @available = ();
622 foreach my $fn (@testlists) {
623 foreach (read_testlist($fn)) {
624 my $name = $$_[0];
625 next if (@includes and not defined(find_in_list(\@includes, $name)));
626 push (@available, $_);
630 my $restricted = undef;
631 my $restricted_used = {};
633 if ($opt_load_list) {
634 $restricted = [];
635 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
636 while (<LOAD_LIST>) {
637 chomp;
638 push (@$restricted, $_);
640 close(LOAD_LIST);
643 my $individual_tests = undef;
644 $individual_tests = {};
646 foreach my $testsuite (@available) {
647 my $name = $$testsuite[0];
648 my $skipreason = skip($name);
649 if (defined($restricted)) {
650 # Find the testsuite for this test
651 my $match = undef;
652 foreach my $r (@$restricted) {
653 if ($r eq $name) {
654 $individual_tests->{$name} = [];
655 $match = $r;
656 $restricted_used->{$r} = 1;
657 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
658 push(@{$individual_tests->{$name}}, $r);
659 $match = $r;
660 $restricted_used->{$r} = 1;
663 if ($match) {
664 if (defined($skipreason)) {
665 if (not $opt_list) {
666 Subunit::skip_testsuite($name, $skipreason);
668 } else {
669 push(@todo, $testsuite);
672 } elsif (defined($skipreason)) {
673 if (not $opt_list) {
674 Subunit::skip_testsuite($name, $skipreason);
676 } else {
677 push(@todo, $testsuite);
681 if (defined($restricted)) {
682 foreach (@$restricted) {
683 unless (defined($restricted_used->{$_})) {
684 print "No test or testsuite found matching $_\n";
687 } elsif ($#todo == -1) {
688 print STDERR "No tests to run\n";
689 exit(1);
692 my $suitestotal = $#todo + 1;
694 unless ($opt_list) {
695 Subunit::progress($suitestotal);
696 Subunit::report_time(time());
699 my $i = 0;
700 $| = 1;
702 my %running_envs = ();
704 sub get_running_env($)
706 my ($name) = @_;
708 my $envname = $name;
710 $envname =~ s/:.*//;
712 return $running_envs{$envname};
715 my @exported_envvars = (
716 # domain stuff
717 "DOMAIN",
718 "REALM",
720 # stuff related to a trusted domain
721 "TRUST_SERVER",
722 "TRUST_SERVER_IP",
723 "TRUST_SERVER_IPV6",
724 "TRUST_NETBIOSNAME",
725 "TRUST_USERNAME",
726 "TRUST_PASSWORD",
727 "TRUST_DOMAIN",
728 "TRUST_REALM",
730 # domain controller stuff
731 "DC_SERVER",
732 "DC_SERVER_IP",
733 "DC_SERVER_IPV6",
734 "DC_NETBIOSNAME",
735 "DC_NETBIOSALIAS",
737 # domain member
738 "MEMBER_SERVER",
739 "MEMBER_SERVER_IP",
740 "MEMBER_SERVER_IPV6",
741 "MEMBER_NETBIOSNAME",
742 "MEMBER_NETBIOSALIAS",
744 # rpc proxy controller stuff
745 "RPC_PROXY_SERVER",
746 "RPC_PROXY_SERVER_IP",
747 "RPC_PROXY_SERVER_IPV6",
748 "RPC_PROXY_NETBIOSNAME",
749 "RPC_PROXY_NETBIOSALIAS",
751 # domain controller stuff for Vampired DC
752 "VAMPIRE_DC_SERVER",
753 "VAMPIRE_DC_SERVER_IP",
754 "VAMPIRE_DC_SERVER_IPV6",
755 "VAMPIRE_DC_NETBIOSNAME",
756 "VAMPIRE_DC_NETBIOSALIAS",
758 "PROMOTED_DC_SERVER",
759 "PROMOTED_DC_SERVER_IP",
760 "PROMOTED_DC_SERVER_IPV6",
761 "PROMOTED_DC_NETBIOSNAME",
762 "PROMOTED_DC_NETBIOSALIAS",
764 # server stuff
765 "SERVER",
766 "SERVER_IP",
767 "SERVER_IPV6",
768 "NETBIOSNAME",
769 "NETBIOSALIAS",
771 # user stuff
772 "USERNAME",
773 "USERID",
774 "PASSWORD",
775 "DC_USERNAME",
776 "DC_PASSWORD",
778 # UID/GID for rfc2307 mapping tests
779 "UID_RFC2307TEST",
780 "GID_RFC2307TEST",
782 # misc stuff
783 "KRB5_CONFIG",
784 "SELFTEST_WINBINDD_SOCKET_DIR",
785 "WINBINDD_PRIV_PIPE_DIR",
786 "NMBD_SOCKET_DIR",
787 "LOCAL_PATH",
789 # nss_wrapper
790 "NSS_WRAPPER_PASSWD",
791 "NSS_WRAPPER_GROUP",
792 "NSS_WRAPPER_HOSTS",
793 "NSS_WRAPPER_MODULE_SO_PATH",
794 "NSS_WRAPPER_MODULE_FN_PREFIX",
796 # resolv_wrapper
797 "RESOLV_WRAPPER_CONF",
798 "RESOLV_WRAPPER_HOSTS",
801 sub sighandler($)
803 my $signame = shift;
805 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
806 $SIG{PIPE} = 'IGNORE';
808 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
810 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
811 teardown_env($_) foreach(keys %running_envs);
812 system("pstree -p $$");
813 print "$0: PID[$$]: Exiting...\n";
814 exit(1);
817 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
819 sub setup_env($$)
821 my ($name, $prefix) = @_;
823 my $testenv_vars = undef;
825 my $envname = $name;
826 my $option = $name;
828 $envname =~ s/:.*//;
829 $option =~ s/^[^:]*//;
830 $option =~ s/^://;
832 $option = "client" if $option eq "";
834 if ($envname eq "none") {
835 $testenv_vars = {};
836 } elsif (defined(get_running_env($envname))) {
837 $testenv_vars = get_running_env($envname);
838 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
839 print $testenv_vars->{target}->getlog_env($testenv_vars);
840 $testenv_vars = undef;
842 } else {
843 $testenv_vars = $target->setup_env($envname, $prefix);
844 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
845 return $testenv_vars;
846 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
847 $testenv_vars->{target} = $target;
849 if (not defined($testenv_vars)) {
850 warn("$opt_target can't start up known environment '$envname'");
854 return undef unless defined($testenv_vars);
856 $running_envs{$envname} = $testenv_vars;
858 if ($option eq "local") {
859 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
860 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
861 } elsif ($option eq "client") {
862 SocketWrapper::set_default_iface(11);
863 write_clientconf($conffile, $clientdir, $testenv_vars);
864 $ENV{SMB_CONF_PATH} = $conffile;
865 } else {
866 die("Unknown option[$option] for envname[$envname]");
869 foreach (@exported_envvars) {
870 if (defined($testenv_vars->{$_})) {
871 $ENV{$_} = $testenv_vars->{$_};
872 } else {
873 delete $ENV{$_};
877 return $testenv_vars;
880 sub exported_envvars_str($)
882 my ($testenv_vars) = @_;
883 my $out = "";
885 foreach (@exported_envvars) {
886 next unless defined($testenv_vars->{$_});
887 $out .= $_."=".$testenv_vars->{$_}."\n";
890 return $out;
893 sub getlog_env($)
895 my ($envname) = @_;
896 return "" if ($envname eq "none");
897 my $env = get_running_env($envname);
898 return $env->{target}->getlog_env($env);
901 sub check_env($)
903 my ($envname) = @_;
904 return 1 if ($envname eq "none");
905 my $env = get_running_env($envname);
906 return $env->{target}->check_env($env);
909 sub teardown_env($)
911 my ($envname) = @_;
912 return if ($envname eq "none");
913 print STDERR "teardown_env($envname)\n";
914 my $env = get_running_env($envname);
915 $env->{target}->teardown_env($env);
916 delete $running_envs{$envname};
919 # This 'global' file needs to be empty when we start
920 unlink("$prefix_abs/dns_host_file");
921 unlink("$prefix_abs/hosts");
923 if ($opt_random_order) {
924 require List::Util;
925 my @newtodo = List::Util::shuffle(@todo);
926 @todo = @newtodo;
929 if ($opt_testenv) {
930 my $testenv_name = $ENV{SELFTEST_TESTENV};
931 $testenv_name = $testenv_default unless defined($testenv_name);
933 my $testenv_vars = setup_env($testenv_name, $prefix);
935 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
936 die("Unable to setup environment $testenv_name");
939 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
940 $ENV{ENVNAME} = $testenv_name;
942 my $envvarstr = exported_envvars_str($testenv_vars);
944 my @term_args = ("echo -e \"
945 Welcome to the Samba4 Test environment '$testenv_name'
947 This matches the client environment used in make test
948 server is pid `cat \$PIDDIR/samba.pid`
950 Some useful environment variables:
951 TORTURE_OPTIONS=\$TORTURE_OPTIONS
952 SMB_CONF_PATH=\$SMB_CONF_PATH
954 $envvarstr
955 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
956 my @term = ();
957 if ($ENV{TERMINAL}) {
958 @term = ($ENV{TERMINAL});
959 } else {
960 @term = ("xterm", "-e");
961 unshift(@term_args, ("bash", "-c"));
964 system(@term, @term_args);
966 teardown_env($testenv_name);
967 } elsif ($opt_list) {
968 foreach (@todo) {
969 my $name = $$_[0];
970 my $envname = $$_[1];
971 my $cmd = $$_[2];
972 my $listcmd = $$_[3];
974 unless (defined($listcmd)) {
975 warn("Unable to list tests in $name");
976 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
977 # a single "test".
978 print "$name\n";
979 next;
982 system($listcmd);
984 if ($? == -1) {
985 die("Unable to run $listcmd: $!");
986 } elsif ($? & 127) {
987 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
990 my $exitcode = $? >> 8;
991 if ($exitcode != 0) {
992 die("$cmd exited with exit code $exitcode");
995 } else {
996 foreach (@todo) {
997 $i++;
998 my $cmd = $$_[2];
999 my $name = $$_[0];
1000 my $envname = $$_[1];
1002 my $envvars = setup_env($envname, $prefix);
1003 if (not defined($envvars)) {
1004 Subunit::start_testsuite($name);
1005 Subunit::end_testsuite($name, "error",
1006 "unable to set up environment $envname - exiting");
1007 next;
1008 } elsif ($envvars eq "UNKNOWN") {
1009 Subunit::start_testsuite($name);
1010 Subunit::end_testsuite($name, "skip",
1011 "environment $envname is unknown in this test backend - skipping");
1012 next;
1015 # Generate a file with the individual tests to run, if the
1016 # test runner for this test suite supports it.
1017 if ($individual_tests and $individual_tests->{$name}) {
1018 if ($$_[3]) {
1019 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1020 foreach my $test (@{$individual_tests->{$name}}) {
1021 print $fh substr($test, length($name)+1) . "\n";
1023 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1024 } else {
1025 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1029 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1031 teardown_env($envname) if ($opt_resetup_env);
1035 print "\n";
1037 teardown_env($_) foreach (keys %running_envs);
1039 my $failed = 0;
1041 # if there were any valgrind failures, show them
1042 foreach (<$prefix/valgrind.log*>) {
1043 next unless (-s $_);
1044 print "VALGRIND FAILURE\n";
1045 $failed++;
1046 system("cat $_");
1048 exit 0;