tests/krb5: Check buffer types in PAC with STRICT_CHECKING=1
[Samba.git] / selftest / selftest.pl
blob258a8437922ca333d70e3cab7e047372f5b654f4
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 Getopt::Long;
26 use POSIX;
27 use Cwd qw(abs_path);
28 use lib "$RealBin";
29 use Subunit;
30 use SocketWrapper;
31 use target::Samba;
32 use Time::HiRes qw(time);
34 eval {
35 require Time::HiRes;
36 Time::HiRes->import("time");
38 if ($@) {
39 print "You don't have Time::Hires installed !\n";
42 my $opt_help = 0;
43 my $opt_target = "samba";
44 my $opt_quick = 0;
45 my $opt_socket_wrapper = 0;
46 my $opt_socket_wrapper_pcap = undef;
47 my $opt_socket_wrapper_keep_pcap = undef;
48 my $opt_random_order = 0;
49 my $opt_one = 0;
50 my @opt_exclude = ();
51 my @opt_include = ();
52 my @opt_exclude_env = ();
53 my @opt_include_env = ();
54 my $opt_testenv = 0;
55 my $opt_list = 0;
56 my $opt_mitkrb5 = 0;
57 my $opt_resetup_env = undef;
58 my $opt_load_list = undef;
59 my $opt_libnss_wrapper_so_path = "";
60 my $opt_libresolv_wrapper_so_path = "";
61 my $opt_libsocket_wrapper_so_path = "";
62 my $opt_libuid_wrapper_so_path = "";
63 my $opt_libasan_so_path = "";
64 my $opt_use_dns_faking = 0;
65 my @testlists = ();
67 my $srcdir = ".";
68 my $bindir = "./bin";
69 my $prefix = "./st";
71 my @includes = ();
72 my @excludes = ();
74 sub find_in_list($$)
76 my ($list, $fullname) = @_;
78 foreach (@$list) {
79 if ($fullname =~ /$$_[0]/) {
80 return ($$_[1]) if ($$_[1]);
81 return "";
85 return undef;
88 sub skip
90 my ($name, $envname) = @_;
91 my ($env_basename, $env_localpart) = split(/:/, $envname);
93 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
94 return "environment $envname is disabled as this build does not include an AD DC";
97 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
98 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
99 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
100 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
103 return find_in_list(\@excludes, $name);
106 sub getlog_env($);
108 # expand strings from %ENV
109 sub expand_environment_strings($)
111 my $s = shift;
112 # we use a reverse sort so we do the longer ones first
113 foreach my $k (sort { $b cmp $a } keys %ENV) {
114 $s =~ s/\$$k/$ENV{$k}/g;
116 return $s;
119 my $target;
121 sub run_testsuite($$$$$)
123 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
124 my $pcap_file = $target->setup_pcap($name);
126 Subunit::start_testsuite($name);
127 Subunit::progress_push();
128 Subunit::report_time();
129 system($cmd);
130 Subunit::report_time();
131 Subunit::progress_pop();
133 if ($? == -1) {
134 print "command: $cmd\n";
135 printf "expanded command: %s\n", expand_environment_strings($cmd);
136 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
137 exit(1);
138 } elsif ($? & 127) {
139 print "command: $cmd\n";
140 printf "expanded command: %s\n", expand_environment_strings($cmd);
141 Subunit::end_testsuite($name, "error",
142 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
143 exit(1);
146 my $exitcode = $? >> 8;
148 my $envlog = getlog_env($envname);
149 if ($envlog ne "") {
150 print "envlog: $envlog\n";
153 print "command: $cmd\n";
154 printf "expanded command: %s\n", expand_environment_strings($cmd);
156 if ($exitcode == 0) {
157 Subunit::end_testsuite($name, "success");
158 } else {
159 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
162 $target->cleanup_pcap($pcap_file, $exitcode);
164 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
165 print "PCAP FILE: $pcap_file\n";
168 if ($exitcode != 0) {
169 exit(1) if ($opt_one);
172 return $exitcode;
175 sub ShowHelp()
177 print "Samba test runner
178 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
179 Copyright (C) Stefan Metzmacher <metze\@samba.org>
181 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
183 Generic options:
184 --help this help page
185 --target=samba[3]|win Samba version to target
186 --testlist=FILE file to read available tests from
187 --exclude=FILE Exclude tests listed in the file
188 --include=FILE Include tests listed in the file
189 --exclude-env=ENV Exclude tests for the specified environment
190 --include-env=ENV Include tests for the specified environment
192 Paths:
193 --prefix=DIR prefix to run tests in [st]
194 --srcdir=DIR source directory [.]
195 --bindir=DIR binaries directory [./bin]
197 Preload cwrap:
198 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
199 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
200 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
201 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
202 --asan_so_path=FILE the asan library to preload
204 DNS:
205 --use-dns-faking Fake DNS entries rather than talking to our
206 DNS implementation.
208 Target Specific:
209 --socket-wrapper-pcap save traffic to pcap directories
210 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
211 failed
212 --socket-wrapper enable socket wrapper
214 Behaviour:
215 --quick run quick overall test
216 --one abort when the first test fails
217 --testenv run a shell in the requested test environment
218 --list list available tests
220 exit(0);
223 my $result = GetOptions (
224 'help|h|?' => \$opt_help,
225 'target=s' => \$opt_target,
226 'prefix=s' => \$prefix,
227 'socket-wrapper' => \$opt_socket_wrapper,
228 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
229 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
230 'quick' => \$opt_quick,
231 'one' => \$opt_one,
232 'exclude=s' => \@opt_exclude,
233 'include=s' => \@opt_include,
234 'exclude-env=s' => \@opt_exclude_env,
235 'include-env=s' => \@opt_include_env,
236 'srcdir=s' => \$srcdir,
237 'bindir=s' => \$bindir,
238 'testenv' => \$opt_testenv,
239 'list' => \$opt_list,
240 'mitkrb5' => \$opt_mitkrb5,
241 'resetup-environment' => \$opt_resetup_env,
242 'testlist=s' => \@testlists,
243 'random-order' => \$opt_random_order,
244 'load-list=s' => \$opt_load_list,
245 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
246 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
247 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
248 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
249 'asan_so_path=s' => \$opt_libasan_so_path,
250 'use-dns-faking' => \$opt_use_dns_faking
253 exit(1) if (not $result);
255 ShowHelp() if ($opt_help);
257 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
259 # we want unbuffered output
260 $| = 1;
262 my @tests = @ARGV;
264 # quick hack to disable rpc validation when using valgrind - its way too slow
265 unless (defined($ENV{VALGRIND})) {
266 $ENV{VALIDATE} = "validate";
267 $ENV{MALLOC_CHECK_} = 3;
270 # make all our python scripts unbuffered
271 $ENV{PYTHONUNBUFFERED} = 1;
273 # do not depend on the users setup
274 # see also bootstrap/config.py
275 $ENV{TZ} = "UTC";
276 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
278 my $bindir_abs = abs_path($bindir);
280 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
282 $prefix =~ s+//+/+;
283 $prefix =~ s+/\./+/+;
284 $prefix =~ s+/$++;
286 die("using an empty prefix isn't allowed") unless $prefix ne "";
288 # Ensure we have the test prefix around.
290 # We need restrictive
291 # permissions on this as some subdirectories in this tree will have
292 # wider permissions (ie 0777) and this would allow other users on the
293 # host to subvert the test process.
294 umask 0077;
295 mkdir($prefix, 0700) unless -d $prefix;
296 chmod 0700, $prefix;
297 # We need to have no umask limitations for the tests.
298 umask 0000;
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{SAMBA_SELFTEST} = "1";
311 $ENV{PREFIX} = $prefix;
312 $ENV{PREFIX_ABS} = $prefix_abs;
313 $ENV{SRCDIR} = $srcdir;
314 $ENV{SRCDIR_ABS} = $srcdir_abs;
315 $ENV{BINDIR} = $bindir_abs;
317 my $tls_enabled = not $opt_quick;
318 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
320 sub prefix_pathvar($$)
322 my ($name, $newpath) = @_;
323 if (defined($ENV{$name})) {
324 $ENV{$name} = "$newpath:$ENV{$name}";
325 } else {
326 $ENV{$name} = $newpath;
329 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
330 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
332 if ($opt_socket_wrapper_keep_pcap) {
333 # Socket wrapper keep pcap implies socket wrapper pcap
334 $opt_socket_wrapper_pcap = 1;
337 if ($opt_socket_wrapper_pcap) {
338 # Socket wrapper pcap implies socket wrapper
339 $opt_socket_wrapper = 1;
342 my $ld_preload = $ENV{LD_PRELOAD};
344 if ($opt_libasan_so_path) {
345 if ($ld_preload) {
346 $ld_preload = "$ld_preload:$opt_libasan_so_path";
347 } else {
348 $ld_preload = "$opt_libasan_so_path";
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 if (defined($ENV{USE_NAMESPACES})) {
385 print "Using linux containerization for selftest testenv(s)...\n";
387 # Create a common bridge to connect up the testenv namespaces. We give
388 # it the client's IP address, as this is where the tests will run from
389 my $ipv4_addr = Samba::get_ipv4_addr("client");
390 my $ipv6_addr = Samba::get_ipv6_addr("client");
391 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
394 $ENV{LD_PRELOAD} = $ld_preload;
395 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
397 # Enable uid_wrapper globally
398 $ENV{UID_WRAPPER} = 1;
400 # We are already hitting the limit, so double it.
401 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
403 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
405 # This is needed in order to allow the ldb_*ldap module
406 # to work with a preloaded socket wrapper.
407 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
409 my $socket_wrapper_dir;
410 if ($opt_socket_wrapper) {
411 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
412 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
413 } elsif (not $opt_list) {
414 unless ($< == 0) {
415 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
419 if ($opt_use_dns_faking) {
420 print "DNS: Faking nameserver\n";
421 $ENV{SAMBA_DNS_FAKING} = 1;
424 my $testenv_default = "none";
426 if ($opt_mitkrb5 == 1) {
427 $ENV{MITKRB5} = $opt_mitkrb5;
428 $ENV{KRB5RCACHETYPE} = "none";
431 # After this many seconds, the server will self-terminate. All tests
432 # must terminate in this time, and testenv will only stay alive this
433 # long
435 my $server_maxtime;
436 if ($opt_testenv) {
437 # 1 year should be enough :-)
438 $server_maxtime = 365 * 24 * 60 * 60;
439 } else {
440 # make test should run under 5 hours
441 $server_maxtime = 5 * 60 * 60;
444 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
445 $server_maxtime = $ENV{SMBD_MAXTIME};
448 $target = new Samba($bindir, $srcdir, $server_maxtime,
449 $opt_socket_wrapper_pcap,
450 $opt_socket_wrapper_keep_pcap);
451 unless ($opt_list) {
452 if ($opt_target eq "samba") {
453 $testenv_default = "ad_dc";
454 } elsif ($opt_target eq "samba3") {
455 $testenv_default = "nt4_member";
459 sub read_test_regexes($)
461 my ($name) = @_;
462 my @ret = ();
463 open(LF, "<$name") or die("unable to read $name: $!");
464 while (<LF>) {
465 chomp;
466 next if (/^#/);
467 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
468 push (@ret, [$1, $4]);
469 } else {
470 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
471 push (@ret, [$_, undef]);
474 close(LF);
475 return @ret;
478 foreach (@opt_exclude) {
479 push (@excludes, read_test_regexes($_));
482 foreach (@opt_include) {
483 push (@includes, read_test_regexes($_));
486 # We give the selftest client 6 different IPv4 addresses to use. Most tests
487 # only use the first (.11) IP. Note that winsreplication.c is one test that
488 # uses the other IPs (search for iface_list_count()).
489 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
490 my $interfaces = Samba::get_interfaces_config("client", 6);
492 my $clientdir = "$prefix_abs/client";
494 my $conffile = "$clientdir/client.conf";
495 $ENV{SMB_CONF_PATH} = $conffile;
497 sub write_clientconf($$$)
499 my ($conffile, $clientdir, $vars) = @_;
501 mkdir("$clientdir", 0777) unless -d "$clientdir";
503 if ( -d "$clientdir/private" ) {
504 unlink <$clientdir/private/*>;
505 } else {
506 mkdir("$clientdir/private", 0777);
509 if ( -d "$clientdir/bind-dns" ) {
510 unlink <$clientdir/bind-dns/*>;
511 } else {
512 mkdir("$clientdir/bind-dns", 0777);
515 if ( -d "$clientdir/lockdir" ) {
516 unlink <$clientdir/lockdir/*>;
517 } else {
518 mkdir("$clientdir/lockdir", 0777);
521 if ( -d "$clientdir/statedir" ) {
522 unlink <$clientdir/statedir/*>;
523 } else {
524 mkdir("$clientdir/statedir", 0777);
527 if ( -d "$clientdir/cachedir" ) {
528 unlink <$clientdir/cachedir/*>;
529 } else {
530 mkdir("$clientdir/cachedir", 0777);
533 # this is ugly, but the ncalrpcdir needs exactly 0755
534 # otherwise tests fail.
535 my $mask = umask;
536 umask 0022;
537 if ( -d "$clientdir/ncalrpcdir/np" ) {
538 unlink <$clientdir/ncalrpcdir/np/*>;
539 rmdir "$clientdir/ncalrpcdir/np";
541 if ( -d "$clientdir/ncalrpcdir" ) {
542 unlink <$clientdir/ncalrpcdir/*>;
543 rmdir "$clientdir/ncalrpcdir";
545 mkdir("$clientdir/ncalrpcdir", 0755);
546 umask $mask;
548 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
549 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
550 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
551 my $ca_users_dir = "$cadir/Users";
553 if ( -d "$clientdir/pkinit" ) {
554 unlink <$clientdir/pkinit/*>;
555 } else {
556 mkdir("$clientdir/pkinit", 0700);
559 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
560 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
561 # We make a copy here and make the certificated easily
562 # accessable in the client environment.
563 $mask = umask;
564 umask 0077;
565 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
566 for my $d (readdir USERS) {
567 my $user_dir = "${ca_users_dir}/${d}";
568 next if ${d} =~ /^\./;
569 next if (! -d "${user_dir}");
570 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
571 for my $l (readdir USER) {
572 my $user_link = "${user_dir}/${l}";
573 next if ${l} =~ /^\./;
574 next if (! -l "${user_link}");
576 my $dest = "${clientdir}/pkinit/${l}";
577 Samba::copy_file_content(${user_link}, ${dest});
579 closedir USER;
581 closedir USERS;
582 umask $mask;
584 open(CF, ">$conffile");
585 print CF "[global]\n";
586 print CF "\tnetbios name = client\n";
587 if (defined($vars->{DOMAIN})) {
588 print CF "\tworkgroup = $vars->{DOMAIN}\n";
590 if (defined($vars->{REALM})) {
591 print CF "\trealm = $vars->{REALM}\n";
593 if ($opt_socket_wrapper) {
594 print CF "\tinterfaces = $interfaces\n";
596 print CF "
597 private dir = $clientdir/private
598 binddns dir = $clientdir/bind-dns
599 lock dir = $clientdir/lockdir
600 state directory = $clientdir/statedir
601 cache directory = $clientdir/cachedir
602 ncalrpc dir = $clientdir/ncalrpcdir
603 panic action = $RealBin/gdb_backtrace \%d
604 max xmit = 32K
605 notify:inotify = false
606 ldb:nosync = true
607 system:anonymous = true
608 client lanman auth = Yes
609 client min protocol = CORE
610 log level = 1
611 torture:basedir = $clientdir
612 #We don't want to pass our self-tests if the PAC code is wrong
613 gensec:require_pac = true
614 #We don't want to run 'speed' tests for very long
615 torture:timelimit = 1
616 winbind separator = /
617 tls cafile = ${cacert}
618 tls crlfile = ${cacrl_pem}
619 tls verify peer = no_check
620 include system krb5 conf = no
621 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
623 close(CF);
626 my @todo = ();
628 sub should_run_test($)
630 my $name = shift;
631 if ($#tests == -1) {
632 return 1;
634 for (my $i=0; $i <= $#tests; $i++) {
635 if ($name =~ /$tests[$i]/i) {
636 return 1;
639 return 0;
642 sub read_testlist($)
644 my ($filename) = @_;
646 my @ret = ();
647 open(IN, $filename) or die("Unable to open $filename: $!");
649 while (<IN>) {
650 if (/-- TEST(-LOADLIST|) --\n/) {
651 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
652 my $name = <IN>;
653 $name =~ s/\n//g;
654 my $env = <IN>;
655 $env =~ s/\n//g;
656 my $loadlist;
657 if ($supports_loadlist) {
658 $loadlist = <IN>;
659 $loadlist =~ s/\n//g;
661 my $cmdline = <IN>;
662 $cmdline =~ s/\n//g;
663 if (should_run_test($name) == 1) {
664 push (@ret, [$name, $env, $cmdline, $loadlist]);
666 } else {
667 print;
670 close(IN) or die("Error creating recipe from $filename");
671 return @ret;
674 if ($#testlists == -1) {
675 die("No testlists specified");
678 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
679 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
680 $ENV{TMPDIR} = "$tmpdir_abs";
681 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
682 if ($opt_quick) {
683 $ENV{SELFTEST_QUICK} = "1";
684 } else {
685 $ENV{SELFTEST_QUICK} = "";
687 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
689 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
690 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
692 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
693 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
695 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
696 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
698 my @available = ();
699 foreach my $fn (@testlists) {
700 foreach (read_testlist($fn)) {
701 my $name = $$_[0];
702 next if (@includes and not defined(find_in_list(\@includes, $name)));
703 push (@available, $_);
707 my $restricted = undef;
708 my $restricted_used = {};
710 if ($opt_load_list) {
711 $restricted = [];
712 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
713 while (<LOAD_LIST>) {
714 chomp;
715 push (@$restricted, $_);
717 close(LOAD_LIST);
720 my $individual_tests = undef;
721 $individual_tests = {};
723 foreach my $testsuite (@available) {
724 my $name = $$testsuite[0];
725 my $skipreason = skip(@$testsuite);
726 if (defined($restricted)) {
727 # Find the testsuite for this test
728 my $match = undef;
729 foreach my $r (@$restricted) {
730 if ($r eq $name) {
731 $individual_tests->{$name} = [];
732 $match = $r;
733 $restricted_used->{$r} = 1;
734 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
735 push(@{$individual_tests->{$name}}, $r);
736 $match = $r;
737 $restricted_used->{$r} = 1;
740 if ($match) {
741 if (defined($skipreason)) {
742 if (not $opt_list) {
743 Subunit::skip_testsuite($name, $skipreason);
745 } else {
746 push(@todo, $testsuite);
749 } elsif (defined($skipreason)) {
750 if (not $opt_list) {
751 Subunit::skip_testsuite($name, $skipreason);
753 } else {
754 push(@todo, $testsuite);
758 if (defined($restricted)) {
759 foreach (@$restricted) {
760 unless (defined($restricted_used->{$_})) {
761 print "No test or testsuite found matching $_\n";
764 } elsif ($#todo == -1) {
765 print STDERR "No tests to run\n";
766 exit(1);
769 my $suitestotal = $#todo + 1;
771 unless ($opt_list) {
772 Subunit::progress($suitestotal);
773 Subunit::report_time();
776 my $i = 0;
777 $| = 1;
779 my %running_envs = ();
781 sub get_running_env($)
783 my ($name) = @_;
785 my $envname = $name;
787 $envname =~ s/:.*//;
789 return $running_envs{$envname};
792 sub sighandler($)
794 my $signame = shift;
796 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
797 $SIG{PIPE} = 'IGNORE';
799 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
801 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
802 teardown_env($_) foreach(keys %running_envs);
803 system("pstree -p $$");
804 print "$0: PID[$$]: Exiting...\n";
805 exit(1);
808 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
810 sub setup_env($$)
812 my ($name, $prefix) = @_;
814 my $testenv_vars = undef;
816 my $envname = $name;
817 my $option = $name;
819 $envname =~ s/:.*//;
820 $option =~ s/^[^:]*//;
821 $option =~ s/^://;
823 $option = "client" if $option eq "";
825 # Initially clear out the environment for the provision, so previous envs'
826 # variables don't leak in. Provisioning steps must explicitly set their
827 # necessary variables when calling out to other executables
828 Samba::clear_exported_envvars();
829 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
830 delete $ENV{SMB_CONF_PATH};
832 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
833 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
834 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
836 if (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 (not defined($testenv_vars)) {
845 my $msg = "$opt_target can't start up known environment '$envname'";
846 if ($opt_one) {
847 die($msg);
849 warn $msg;
850 return;
852 if (ref $testenv_vars ne "HASH") {
853 return $testenv_vars;
855 if (defined($testenv_vars->{target})) {
856 $testenv_vars->{target} = $target;
860 return undef unless defined($testenv_vars);
862 $running_envs{$envname} = $testenv_vars;
864 if ($option eq "local") {
865 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
866 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
867 } elsif ($option eq "client") {
868 SocketWrapper::set_default_iface(11);
869 write_clientconf($conffile, $clientdir, $testenv_vars);
870 $ENV{SMB_CONF_PATH} = $conffile;
871 } else {
872 die("Unknown option[$option] for envname[$envname]");
875 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
876 Samba::export_envvars($testenv_vars);
878 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
879 unlink($krb5_ccache_path);
880 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
881 return $testenv_vars;
884 sub getlog_env($)
886 my ($envname) = @_;
887 return "" if ($envname eq "none");
888 my $env = get_running_env($envname);
889 return $env->{target}->getlog_env($env);
892 sub check_env($)
894 my ($envname) = @_;
895 my $env = get_running_env($envname);
896 return $env->{target}->check_env($env);
899 sub teardown_env($)
901 my ($envname) = @_;
902 return if ($envname eq "none");
903 print STDERR "teardown_env($envname)\n";
904 my $env = get_running_env($envname);
905 $env->{target}->teardown_env($env);
906 delete $running_envs{$envname};
909 # This 'global' file needs to be empty when we start
910 unlink("$prefix_abs/dns_host_file");
911 unlink("$prefix_abs/hosts");
913 if ($opt_random_order) {
914 require List::Util;
915 my @newtodo = List::Util::shuffle(@todo);
916 @todo = @newtodo;
919 if ($opt_testenv) {
920 my $testenv_name = $ENV{SELFTEST_TESTENV};
921 $testenv_name = $testenv_default unless defined($testenv_name);
923 my $testenv_vars = setup_env($testenv_name, $prefix);
925 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
926 die("Unable to setup environment $testenv_name");
929 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
930 $ENV{ENVNAME} = $testenv_name;
932 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
934 my @term_args = ("echo -e \"
935 Welcome to the Samba4 Test environment '$testenv_name'
937 This matches the client environment used in make test
938 server is pid `cat \$PIDDIR/samba.pid`
940 Some useful environment variables:
941 TORTURE_OPTIONS=\$TORTURE_OPTIONS
942 SMB_CONF_PATH=\$SMB_CONF_PATH
944 $envvarstr
945 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
946 my @term = ();
947 if ($ENV{TERMINAL}) {
948 @term = ($ENV{TERMINAL});
949 # override the default terminal args (if specified)
950 if (defined($ENV{TERMINAL_ARGS})) {
951 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
953 } else {
954 @term = ("xterm", "-e");
955 unshift(@term_args, ("bash", "-c"));
958 system(@term, @term_args);
960 teardown_env($testenv_name);
961 } elsif ($opt_list) {
962 foreach (@todo) {
963 my $name = $$_[0];
964 my $envname = $$_[1];
965 my $cmd = $$_[2];
966 my $listcmd = $$_[3];
968 unless (defined($listcmd)) {
969 warn("Unable to list tests in $name");
970 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
971 # a single "test".
972 print "$name\n";
973 next;
976 system($listcmd);
978 if ($? == -1) {
979 die("Unable to run $listcmd: $!");
980 } elsif ($? & 127) {
981 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
984 my $exitcode = $? >> 8;
985 if ($exitcode != 0) {
986 die("$cmd exited with exit code $exitcode");
989 } else {
990 foreach (@todo) {
991 $i++;
992 my $cmd = $$_[2];
993 my $name = $$_[0];
994 my $envname = $$_[1];
995 my $envvars = setup_env($envname, $prefix);
997 if (not defined($envvars)) {
998 Subunit::start_testsuite($name);
999 Subunit::end_testsuite($name, "error",
1000 "unable to set up environment $envname - exiting");
1001 next;
1002 } elsif ($envvars eq "UNKNOWN") {
1003 Subunit::start_testsuite($name);
1004 Subunit::end_testsuite($name, "error",
1005 "environment $envname is unknown - exiting");
1006 next;
1009 # Generate a file with the individual tests to run, if the
1010 # test runner for this test suite supports it.
1011 if ($individual_tests and $individual_tests->{$name}) {
1012 if ($$_[3]) {
1013 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1014 foreach my $test (@{$individual_tests->{$name}}) {
1015 print $fh substr($test, length($name)+1) . "\n";
1017 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1018 } else {
1019 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1023 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1025 teardown_env($envname) if ($opt_resetup_env);
1029 print "\n";
1031 teardown_env($_) foreach (keys %running_envs);
1033 my $failed = 0;
1035 # if there were any valgrind failures, show them
1036 foreach (<$prefix/valgrind.log*>) {
1037 next unless (-s $_);
1038 print "VALGRIND FAILURE\n";
1039 $failed++;
1040 system("cat $_");
1042 exit 0;