s3/utils: Fix use after free with popt 1.19
[Samba.git] / selftest / selftest.pl
blob11f8e92e84eb9232e54ff7da3d283690d37fccef
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;
20 use warnings;
22 use FindBin qw($RealBin $Script);
23 use File::Spec;
24 use File::Temp qw(tempfile);
25 use File::Path qw(remove_tree);
26 use Getopt::Long;
27 use POSIX;
28 use Cwd qw(abs_path);
29 use lib "$RealBin";
30 use Subunit;
31 use SocketWrapper;
32 use target::Samba;
33 use Time::HiRes qw(time);
35 eval {
36 require Time::HiRes;
37 Time::HiRes->import("time");
39 if ($@) {
40 print "You don't have Time::Hires installed !\n";
43 my $opt_help = 0;
44 my $opt_target = "samba";
45 my $opt_quick = 0;
46 my $opt_socket_wrapper = 0;
47 my $opt_socket_wrapper_pcap = undef;
48 my $opt_socket_wrapper_keep_pcap = undef;
49 my $opt_random_order = 0;
50 my $opt_one = 0;
51 my @opt_exclude = ();
52 my @opt_include = ();
53 my @opt_exclude_env = ();
54 my @opt_include_env = ();
55 my $opt_testenv = 0;
56 my $opt_list = 0;
57 my $opt_mitkrb5 = 0;
58 my $opt_resetup_env = undef;
59 my $opt_load_list = undef;
60 my $opt_libnss_wrapper_so_path = "";
61 my $opt_libresolv_wrapper_so_path = "";
62 my $opt_libsocket_wrapper_so_path = "";
63 my $opt_libuid_wrapper_so_path = "";
64 my $opt_libasan_so_path = "";
65 my $opt_libcrypt_so_path = "";
66 my $opt_use_dns_faking = 0;
67 my @testlists = ();
69 my $srcdir = ".";
70 my $bindir = "./bin";
71 my $prefix = "./st";
73 my @includes = ();
74 my @excludes = ();
76 sub find_in_list($$)
78 my ($list, $fullname) = @_;
80 foreach (@$list) {
81 if ($fullname =~ /$$_[0]/) {
82 return ($$_[1]) if ($$_[1]);
83 return "";
87 return undef;
90 sub skip
92 my ($name, $envname) = @_;
93 my ($env_basename, $env_localpart) = split(/:/, $envname);
95 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
96 return "environment $envname is disabled as this build does not include an AD DC";
99 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
100 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
101 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
102 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
105 return find_in_list(\@excludes, $name);
108 sub getlog_env($);
110 # expand strings from %ENV
111 sub expand_environment_strings($)
113 my $s = shift;
114 # we use a reverse sort so we do the longer ones first
115 foreach my $k (sort { $b cmp $a } keys %ENV) {
116 $s =~ s/\$$k/$ENV{$k}/g;
118 return $s;
121 my $target;
123 sub run_testsuite($$$$$)
125 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
126 my $pcap_file = $target->setup_pcap($name);
128 Subunit::start_testsuite($name);
129 Subunit::progress_push();
130 Subunit::report_time();
131 system($cmd);
132 Subunit::report_time();
133 Subunit::progress_pop();
135 if ($? == -1) {
136 print "command: $cmd\n";
137 printf "expanded command: %s\n", expand_environment_strings($cmd);
138 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
139 exit(1);
140 } elsif ($? & 127) {
141 print "command: $cmd\n";
142 printf "expanded command: %s\n", expand_environment_strings($cmd);
143 Subunit::end_testsuite($name, "error",
144 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
145 exit(1);
148 my $exitcode = $? >> 8;
150 my $envlog = getlog_env($envname);
151 if ($envlog ne "") {
152 print "envlog: $envlog\n";
155 print "command: $cmd\n";
156 printf "expanded command: %s\n", expand_environment_strings($cmd);
158 if ($exitcode == 0) {
159 Subunit::end_testsuite($name, "success");
160 } else {
161 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
164 $target->cleanup_pcap($pcap_file, $exitcode);
166 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
167 print "PCAP FILE: $pcap_file\n";
170 if ($exitcode != 0) {
171 exit(1) if ($opt_one);
174 return $exitcode;
177 sub ShowHelp()
179 print "Samba test runner
180 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
181 Copyright (C) Stefan Metzmacher <metze\@samba.org>
183 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
185 Generic options:
186 --help this help page
187 --target=samba[3]|win Samba version to target
188 --testlist=FILE file to read available tests from
189 --exclude=FILE Exclude tests listed in the file
190 --include=FILE Include tests listed in the file
191 --exclude-env=ENV Exclude tests for the specified environment
192 --include-env=ENV Include tests for the specified environment
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 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
202 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
203 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
204 --asan_so_path=FILE the asan library to preload
206 DNS:
207 --use-dns-faking Fake DNS entries rather than talking to our
208 DNS implementation.
210 Target Specific:
211 --socket-wrapper-pcap save traffic to pcap directories
212 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
213 failed
214 --socket-wrapper enable socket wrapper
216 Behaviour:
217 --quick run quick overall test
218 --one abort when the first test fails
219 --testenv run a shell in the requested test environment
220 --list list available tests
222 exit(0);
225 my $result = GetOptions (
226 'help|h|?' => \$opt_help,
227 'target=s' => \$opt_target,
228 'prefix=s' => \$prefix,
229 'socket-wrapper' => \$opt_socket_wrapper,
230 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
231 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
232 'quick' => \$opt_quick,
233 'one' => \$opt_one,
234 'exclude=s' => \@opt_exclude,
235 'include=s' => \@opt_include,
236 'exclude-env=s' => \@opt_exclude_env,
237 'include-env=s' => \@opt_include_env,
238 'srcdir=s' => \$srcdir,
239 'bindir=s' => \$bindir,
240 'testenv' => \$opt_testenv,
241 'list' => \$opt_list,
242 'mitkrb5' => \$opt_mitkrb5,
243 'resetup-environment' => \$opt_resetup_env,
244 'testlist=s' => \@testlists,
245 'random-order' => \$opt_random_order,
246 'load-list=s' => \$opt_load_list,
247 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
248 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
249 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
250 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
251 'asan_so_path=s' => \$opt_libasan_so_path,
252 'crypt_so_path=s' => \$opt_libcrypt_so_path,
253 'use-dns-faking' => \$opt_use_dns_faking
256 exit(1) if (not $result);
258 ShowHelp() if ($opt_help);
260 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
262 # we want unbuffered output
263 $| = 1;
265 my @tests = @ARGV;
267 # quick hack to disable rpc validation when using valgrind - its way too slow
268 unless (defined($ENV{VALGRIND})) {
269 $ENV{VALIDATE} = "validate";
270 $ENV{MALLOC_CHECK_} = 3;
273 # make all our python scripts unbuffered
274 $ENV{PYTHONUNBUFFERED} = 1;
276 $ENV{SAMBA_DEPRECATED_SUPPRESS} = 1;
278 # do not depend on the users setup
279 # see also bootstrap/config.py
280 $ENV{TZ} = "UTC";
281 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
283 my $bindir_abs = abs_path($bindir);
285 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
287 $prefix =~ s+//+/+;
288 $prefix =~ s+/\./+/+;
289 $prefix =~ s+/$++;
291 die("using an empty prefix isn't allowed") unless $prefix ne "";
293 # Ensure we have the test prefix around.
295 # We need restrictive
296 # permissions on this as some subdirectories in this tree will have
297 # wider permissions (ie 0777) and this would allow other users on the
298 # host to subvert the test process.
299 umask 0077;
300 mkdir($prefix, 0700) unless -d $prefix;
301 chmod 0700, $prefix;
302 # We need to have no umask limitations for the tests.
303 umask 0000;
305 my $prefix_abs = abs_path($prefix);
306 my $tmpdir_abs = abs_path("$prefix/tmp");
307 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
309 my $srcdir_abs = abs_path($srcdir);
311 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
312 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
314 $ENV{SAMBA_SELFTEST} = "1";
316 $ENV{PREFIX} = $prefix;
317 $ENV{PREFIX_ABS} = $prefix_abs;
318 $ENV{SRCDIR} = $srcdir;
319 $ENV{SRCDIR_ABS} = $srcdir_abs;
320 $ENV{BINDIR} = $bindir_abs;
322 my $tls_enabled = not $opt_quick;
323 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
325 sub prefix_pathvar($$)
327 my ($name, $newpath) = @_;
328 if (defined($ENV{$name})) {
329 $ENV{$name} = "$newpath:$ENV{$name}";
330 } else {
331 $ENV{$name} = $newpath;
334 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
335 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
337 if ($opt_socket_wrapper_keep_pcap) {
338 # Socket wrapper keep pcap implies socket wrapper pcap
339 $opt_socket_wrapper_pcap = 1;
342 if ($opt_socket_wrapper_pcap) {
343 # Socket wrapper pcap implies socket wrapper
344 $opt_socket_wrapper = 1;
347 my $ld_preload = $ENV{LD_PRELOAD};
349 if ($opt_libasan_so_path) {
350 if ($ld_preload) {
351 if ($opt_libcrypt_so_path) {
352 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path:$ld_preload";
353 } else {
354 $ld_preload = "$opt_libasan_so_path:$ld_preload";
356 } else {
357 if ($opt_libcrypt_so_path) {
358 $ld_preload = "$opt_libasan_so_path:$opt_libcrypt_so_path";
359 } else {
360 $ld_preload = "$opt_libasan_so_path";
365 if ($opt_libnss_wrapper_so_path) {
366 if ($ld_preload) {
367 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
368 } else {
369 $ld_preload = "$opt_libnss_wrapper_so_path";
373 if ($opt_libresolv_wrapper_so_path) {
374 if ($ld_preload) {
375 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
376 } else {
377 $ld_preload = "$opt_libresolv_wrapper_so_path";
381 if ($opt_libsocket_wrapper_so_path) {
382 if ($ld_preload) {
383 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
384 } else {
385 $ld_preload = "$opt_libsocket_wrapper_so_path";
389 if ($opt_libuid_wrapper_so_path) {
390 if ($ld_preload) {
391 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
392 } else {
393 $ld_preload = "$opt_libuid_wrapper_so_path";
397 if (defined($ENV{USE_NAMESPACES})) {
398 print "Using linux containerization for selftest testenv(s)...\n";
400 # Create a common bridge to connect up the testenv namespaces. We give
401 # it the client's IP address, as this is where the tests will run from
402 my $ipv4_addr = Samba::get_ipv4_addr("client");
403 my $ipv6_addr = Samba::get_ipv6_addr("client");
404 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
407 $ENV{LD_PRELOAD} = $ld_preload;
408 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
410 # Enable uid_wrapper globally
411 $ENV{UID_WRAPPER} = 1;
413 # We are already hitting the limit, so double it.
414 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
416 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
418 # This is needed in order to allow the ldb_*ldap module
419 # to work with a preloaded socket wrapper.
420 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
422 my $socket_wrapper_dir;
423 if ($opt_socket_wrapper) {
424 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
425 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
426 } elsif (not $opt_list) {
427 unless ($< == 0) {
428 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
432 if ($opt_use_dns_faking) {
433 print "DNS: Faking nameserver\n";
434 $ENV{SAMBA_DNS_FAKING} = 1;
437 my $testenv_default = "none";
439 if ($opt_mitkrb5 == 1) {
440 $ENV{MITKRB5} = $opt_mitkrb5;
441 $ENV{KRB5RCACHETYPE} = "none";
444 # After this many seconds, the server will self-terminate. All tests
445 # must terminate in this time, and testenv will only stay alive this
446 # long
448 my $server_maxtime;
449 if ($opt_testenv) {
450 # 1 year should be enough :-)
451 $server_maxtime = 365 * 24 * 60 * 60;
452 } else {
453 # make test should run under 5 hours
454 $server_maxtime = 5 * 60 * 60;
457 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
458 $server_maxtime = $ENV{SMBD_MAXTIME};
461 $target = new Samba($bindir, $srcdir, $server_maxtime,
462 $opt_socket_wrapper_pcap,
463 $opt_socket_wrapper_keep_pcap);
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 # We give the selftest client 6 different IPv4 addresses to use. Most tests
500 # only use the first (.11) IP. Note that winsreplication.c is one test that
501 # uses the other IPs (search for iface_list_count()).
502 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
503 my $interfaces = Samba::get_interfaces_config("client", 6);
505 my $clientdir = "$prefix_abs/client";
507 my $conffile = "$clientdir/client.conf";
508 $ENV{SMB_CONF_PATH} = $conffile;
510 sub write_clientconf($$$)
512 my ($conffile, $clientdir, $vars) = @_;
514 mkdir("$clientdir", 0777) unless -d "$clientdir";
516 my @subdirs = (
517 { name => "private", mask => 0777 },
518 { name => "bind-dns", mask => 0777 },
519 { name => "lockdir", mask => 0777 },
520 { name => "statedir", mask => 0777 },
521 { name => "cachedir", mask => 0777 },
522 { name => "pkinit", mask => 0700 },
523 { name => "pid", mask => 0777 },
524 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
525 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
528 foreach my $sub (@subdirs) {
529 my $dir = "$clientdir/$sub->{name}";
530 remove_tree($dir);
531 my $mask = umask;
532 if (defined($sub->{umask})) {
533 umask $sub->{umask};
535 mkdir($dir, $sub->{mask});
536 umask $mask;
539 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
540 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
541 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
542 my $ca_users_dir = "$cadir/Users";
543 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
545 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
546 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
547 # We make a copy here and make the certificated easily
548 # accessable in the client environment.
549 my $mask = umask;
550 umask 0077;
551 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
552 for my $d (readdir USERS) {
553 my $user_dir = "${ca_users_dir}/${d}";
554 next if ${d} =~ /^\./;
555 next if (! -d "${user_dir}");
556 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
557 for my $l (readdir USER) {
558 my $user_link = "${user_dir}/${l}";
559 next if ${l} =~ /^\./;
560 next if (! -l "${user_link}");
562 my $dest = "${clientdir}/pkinit/${l}";
563 Samba::copy_file_content(${user_link}, ${dest});
565 closedir USER;
567 closedir USERS;
568 umask $mask;
570 open(CF, ">$conffile");
571 print CF "[global]\n";
572 print CF "\tnetbios name = client\n";
573 if (defined($vars->{DOMAIN})) {
574 print CF "\tworkgroup = $vars->{DOMAIN}\n";
576 if (defined($vars->{REALM})) {
577 print CF "\trealm = $vars->{REALM}\n";
579 if ($opt_socket_wrapper) {
580 print CF "\tinterfaces = $interfaces\n";
582 print CF "
583 private dir = $clientdir/private
584 binddns dir = $clientdir/bind-dns
585 lock dir = $clientdir/lockdir
586 state directory = $clientdir/statedir
587 cache directory = $clientdir/cachedir
588 ncalrpc dir = $clientdir/ncalrpcdir
589 pid directory = $clientdir/pid
590 panic action = $RealBin/gdb_backtrace \%d
591 max xmit = 32K
592 notify:inotify = false
593 ldb:nosync = true
594 system:anonymous = true
595 client lanman auth = Yes
596 client min protocol = CORE
597 log level = $client_loglevel
598 torture:basedir = $clientdir
599 #We don't want to run 'speed' tests for very long
600 torture:timelimit = 1
601 winbind separator = /
602 tls cafile = ${cacert}
603 tls crlfile = ${cacrl_pem}
604 tls verify peer = no_check
605 include system krb5 conf = no
606 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
608 close(CF);
611 my @todo = ();
613 sub should_run_test($)
615 my $name = shift;
616 if ($#tests == -1) {
617 return 1;
619 for (my $i=0; $i <= $#tests; $i++) {
620 if ($name =~ /$tests[$i]/i) {
621 return 1;
624 return 0;
627 sub read_testlist($)
629 my ($filename) = @_;
631 my @ret = ();
632 open(IN, $filename) or die("Unable to open $filename: $!");
634 while (<IN>) {
635 if (/-- TEST(-LOADLIST|) --\n/) {
636 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
637 my $name = <IN>;
638 $name =~ s/\n//g;
639 my $env = <IN>;
640 $env =~ s/\n//g;
641 my $loadlist;
642 if ($supports_loadlist) {
643 $loadlist = <IN>;
644 $loadlist =~ s/\n//g;
646 my $cmdline = <IN>;
647 $cmdline =~ s/\n//g;
648 if (should_run_test($name) == 1) {
649 push (@ret, [$name, $env, $cmdline, $loadlist]);
651 } else {
652 print;
655 close(IN) or die("Error creating recipe from $filename");
656 return @ret;
659 if ($#testlists == -1) {
660 die("No testlists specified");
663 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
664 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
665 $ENV{TMPDIR} = "$tmpdir_abs";
666 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
667 if ($opt_quick) {
668 $ENV{SELFTEST_QUICK} = "1";
669 } else {
670 $ENV{SELFTEST_QUICK} = "";
672 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
674 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
675 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
677 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
678 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
680 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
681 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
683 my @available = ();
684 foreach my $fn (@testlists) {
685 foreach (read_testlist($fn)) {
686 my $name = $$_[0];
687 next if (@includes and not defined(find_in_list(\@includes, $name)));
688 push (@available, $_);
692 my $restricted = undef;
693 my $restricted_used = {};
695 if ($opt_load_list) {
696 $restricted = [];
697 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
698 while (<LOAD_LIST>) {
699 chomp;
700 push (@$restricted, $_);
702 close(LOAD_LIST);
705 my $individual_tests = undef;
706 $individual_tests = {};
708 foreach my $testsuite (@available) {
709 my $name = $$testsuite[0];
710 my $skipreason = skip(@$testsuite);
711 if (defined($restricted)) {
712 # Find the testsuite for this test
713 my $match = undef;
714 foreach my $r (@$restricted) {
715 if ($r eq $name) {
716 $individual_tests->{$name} = [];
717 $match = $r;
718 $restricted_used->{$r} = 1;
719 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
720 push(@{$individual_tests->{$name}}, $r);
721 $match = $r;
722 $restricted_used->{$r} = 1;
725 if ($match) {
726 if (defined($skipreason)) {
727 if (not $opt_list) {
728 Subunit::skip_testsuite($name, $skipreason);
730 } else {
731 push(@todo, $testsuite);
734 } elsif (defined($skipreason)) {
735 if (not $opt_list) {
736 Subunit::skip_testsuite($name, $skipreason);
738 } else {
739 push(@todo, $testsuite);
743 if (defined($restricted)) {
744 foreach (@$restricted) {
745 unless (defined($restricted_used->{$_})) {
746 print "No test or testsuite found matching $_\n";
749 } elsif ($#todo == -1) {
750 print STDERR "No tests to run\n";
751 exit(1);
754 my $suitestotal = $#todo + 1;
756 unless ($opt_list) {
757 Subunit::progress($suitestotal);
758 Subunit::report_time();
761 my $i = 0;
762 $| = 1;
764 my %running_envs = ();
766 sub get_running_env($)
768 my ($name) = @_;
770 my $envname = $name;
772 $envname =~ s/:.*//;
774 return $running_envs{$envname};
777 sub sighandler($)
779 my $signame = shift;
781 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
782 $SIG{PIPE} = 'IGNORE';
784 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
786 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
787 teardown_env($_) foreach(keys %running_envs);
788 system("pstree -p $$");
789 print "$0: PID[$$]: Exiting...\n";
790 exit(1);
793 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
795 sub setup_env($$)
797 my ($name, $prefix) = @_;
799 my $testenv_vars = undef;
801 my $envname = $name;
802 my $option = $name;
804 $envname =~ s/:.*//;
805 $option =~ s/^[^:]*//;
806 $option =~ s/^://;
808 $option = "client" if $option eq "";
810 # Initially clear out the environment for the provision, so previous envs'
811 # variables don't leak in. Provisioning steps must explicitly set their
812 # necessary variables when calling out to other executables
813 Samba::clear_exported_envvars();
814 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
815 delete $ENV{SMB_CONF_PATH};
817 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
818 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
819 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
821 if (defined(get_running_env($envname))) {
822 $testenv_vars = get_running_env($envname);
823 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
824 print $testenv_vars->{target}->getlog_env($testenv_vars);
825 $testenv_vars = undef;
827 } else {
828 $testenv_vars = $target->setup_env($envname, $prefix);
829 if (not defined($testenv_vars)) {
830 my $msg = "$opt_target can't start up known environment '$envname'";
831 if ($opt_one) {
832 die($msg);
834 warn $msg;
835 return;
837 if (ref $testenv_vars ne "HASH") {
838 return $testenv_vars;
840 if (defined($testenv_vars->{target})) {
841 $testenv_vars->{target} = $target;
845 return undef unless defined($testenv_vars);
847 $running_envs{$envname} = $testenv_vars;
849 if ($option eq "local") {
850 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
851 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
852 } elsif ($option eq "client") {
853 SocketWrapper::set_default_iface(11);
854 write_clientconf($conffile, $clientdir, $testenv_vars);
855 $ENV{SMB_CONF_PATH} = $conffile;
856 } else {
857 die("Unknown option[$option] for envname[$envname]");
860 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
861 Samba::export_envvars($testenv_vars);
863 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
864 unlink($krb5_ccache_path);
865 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
866 return $testenv_vars;
869 sub getlog_env($)
871 my ($envname) = @_;
872 return "" if ($envname eq "none");
873 my $env = get_running_env($envname);
874 return $env->{target}->getlog_env($env);
877 sub check_env($)
879 my ($envname) = @_;
880 my $env = get_running_env($envname);
881 return $env->{target}->check_env($env);
884 sub teardown_env($)
886 my ($envname) = @_;
887 return if ($envname eq "none");
888 print STDERR "teardown_env($envname)\n";
889 my $env = get_running_env($envname);
890 $env->{target}->teardown_env($env);
891 delete $running_envs{$envname};
894 # This 'global' file needs to be empty when we start
895 unlink("$prefix_abs/dns_host_file");
896 unlink("$prefix_abs/hosts");
898 if ($opt_random_order) {
899 require List::Util;
900 my @newtodo = List::Util::shuffle(@todo);
901 @todo = @newtodo;
904 if ($opt_testenv) {
905 my $testenv_name = $ENV{SELFTEST_TESTENV};
906 $testenv_name = $testenv_default unless defined($testenv_name);
908 my $testenv_vars = setup_env($testenv_name, $prefix);
910 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
911 die("Unable to setup environment $testenv_name");
914 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
915 $ENV{ENVNAME} = $testenv_name;
917 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
919 my @term_args = ("echo -e \"
920 Welcome to the Samba4 Test environment '$testenv_name'
922 This matches the client environment used in make test
923 server is pid `cat \$PIDDIR/samba.pid`
925 Some useful environment variables:
926 TORTURE_OPTIONS=\$TORTURE_OPTIONS
927 SMB_CONF_PATH=\$SMB_CONF_PATH
929 $envvarstr
930 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
931 my @term = ();
932 if ($ENV{TERMINAL}) {
933 @term = ($ENV{TERMINAL});
934 # override the default terminal args (if specified)
935 if (defined($ENV{TERMINAL_ARGS})) {
936 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
938 } else {
939 @term = ("xterm", "-e");
940 unshift(@term_args, ("bash", "-c"));
943 system(@term, @term_args);
945 teardown_env($testenv_name);
946 } elsif ($opt_list) {
947 foreach (@todo) {
948 my $name = $$_[0];
949 my $envname = $$_[1];
950 my $cmd = $$_[2];
951 my $listcmd = $$_[3];
953 unless (defined($listcmd)) {
954 warn("Unable to list tests in $name");
955 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
956 # a single "test".
957 print "$name\n";
958 next;
961 system($listcmd);
963 if ($? == -1) {
964 die("Unable to run $listcmd: $!");
965 } elsif ($? & 127) {
966 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
969 my $exitcode = $? >> 8;
970 if ($exitcode != 0) {
971 die("$cmd exited with exit code $exitcode");
974 } else {
975 foreach (@todo) {
976 $i++;
977 my $cmd = $$_[2];
978 my $name = $$_[0];
979 my $envname = $$_[1];
980 my $envvars = setup_env($envname, $prefix);
982 if (not defined($envvars)) {
983 Subunit::start_testsuite($name);
984 Subunit::end_testsuite($name, "error",
985 "unable to set up environment $envname - exiting");
986 next;
987 } elsif ($envvars eq "UNKNOWN") {
988 Subunit::start_testsuite($name);
989 Subunit::end_testsuite($name, "error",
990 "environment $envname is unknown - exiting");
991 next;
994 # Generate a file with the individual tests to run, if the
995 # test runner for this test suite supports it.
996 if ($individual_tests and $individual_tests->{$name}) {
997 if ($$_[3]) {
998 my ($fh, $listid_file) = tempfile(UNLINK => 0);
999 foreach my $test (@{$individual_tests->{$name}}) {
1000 print $fh substr($test, length($name)+1) . "\n";
1002 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1003 } else {
1004 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1008 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1010 teardown_env($envname) if ($opt_resetup_env);
1014 print "\n";
1016 teardown_env($_) foreach (keys %running_envs);
1018 my $failed = 0;
1020 # if there were any valgrind failures, show them
1021 foreach (<$prefix/valgrind.log*>) {
1022 next unless (-s $_);
1023 print "VALGRIND FAILURE\n";
1024 $failed++;
1025 system("cat $_");
1027 exit 0;