CVE-2023-0614 s4:dsdb/extended_dn_in: Don't modify a search tree we don't own
[Samba.git] / selftest / selftest.pl
blob75763ef3838a84a1b51c26ccfb8fa358f596f5a2
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_use_dns_faking = 0;
66 my @testlists = ();
68 my $srcdir = ".";
69 my $bindir = "./bin";
70 my $prefix = "./st";
72 my @includes = ();
73 my @excludes = ();
75 sub find_in_list($$)
77 my ($list, $fullname) = @_;
79 foreach (@$list) {
80 if ($fullname =~ /$$_[0]/) {
81 return ($$_[1]) if ($$_[1]);
82 return "";
86 return undef;
89 sub skip
91 my ($name, $envname) = @_;
92 my ($env_basename, $env_localpart) = split(/:/, $envname);
94 if ($opt_target eq "samba3" && $Samba::ENV_NEEDS_AD_DC{$env_basename}) {
95 return "environment $envname is disabled as this build does not include an AD DC";
98 if (@opt_include_env && !(grep {$_ eq $env_basename} @opt_include_env)) {
99 return "environment $envname is disabled (via --include-env command line option) in this test run - skipping";
100 } elsif (@opt_exclude_env && grep {$_ eq $env_basename} @opt_exclude_env) {
101 return "environment $envname is disabled (via --exclude-env command line option) in this test run - skipping";
104 return find_in_list(\@excludes, $name);
107 sub getlog_env($);
109 # expand strings from %ENV
110 sub expand_environment_strings($)
112 my $s = shift;
113 # we use a reverse sort so we do the longer ones first
114 foreach my $k (sort { $b cmp $a } keys %ENV) {
115 $s =~ s/\$$k/$ENV{$k}/g;
117 return $s;
120 my $target;
122 sub run_testsuite($$$$$)
124 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
125 my $pcap_file = $target->setup_pcap($name);
127 Subunit::start_testsuite($name);
128 Subunit::progress_push();
129 Subunit::report_time();
130 system($cmd);
131 Subunit::report_time();
132 Subunit::progress_pop();
134 if ($? == -1) {
135 print "command: $cmd\n";
136 printf "expanded command: %s\n", expand_environment_strings($cmd);
137 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
138 exit(1);
139 } elsif ($? & 127) {
140 print "command: $cmd\n";
141 printf "expanded command: %s\n", expand_environment_strings($cmd);
142 Subunit::end_testsuite($name, "error",
143 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
144 exit(1);
147 my $exitcode = $? >> 8;
149 my $envlog = getlog_env($envname);
150 if ($envlog ne "") {
151 print "envlog: $envlog\n";
154 print "command: $cmd\n";
155 printf "expanded command: %s\n", expand_environment_strings($cmd);
157 if ($exitcode == 0) {
158 Subunit::end_testsuite($name, "success");
159 } else {
160 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
163 $target->cleanup_pcap($pcap_file, $exitcode);
165 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
166 print "PCAP FILE: $pcap_file\n";
169 if ($exitcode != 0) {
170 exit(1) if ($opt_one);
173 return $exitcode;
176 sub ShowHelp()
178 print "Samba test runner
179 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
180 Copyright (C) Stefan Metzmacher <metze\@samba.org>
182 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
184 Generic options:
185 --help this help page
186 --target=samba[3]|win Samba version to target
187 --testlist=FILE file to read available tests from
188 --exclude=FILE Exclude tests listed in the file
189 --include=FILE Include tests listed in the file
190 --exclude-env=ENV Exclude tests for the specified environment
191 --include-env=ENV Include tests for the specified environment
193 Paths:
194 --prefix=DIR prefix to run tests in [st]
195 --srcdir=DIR source directory [.]
196 --bindir=DIR binaries directory [./bin]
198 Preload cwrap:
199 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
200 --resolv_wrapper_so_path=FILE the resolv_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
203 --asan_so_path=FILE the asan library to preload
205 DNS:
206 --use-dns-faking Fake DNS entries rather than talking to our
207 DNS implementation.
209 Target Specific:
210 --socket-wrapper-pcap save traffic to pcap directories
211 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
212 failed
213 --socket-wrapper enable socket wrapper
215 Behaviour:
216 --quick run quick overall test
217 --one abort when the first test fails
218 --testenv run a shell in the requested test environment
219 --list list available tests
221 exit(0);
224 my $result = GetOptions (
225 'help|h|?' => \$opt_help,
226 'target=s' => \$opt_target,
227 'prefix=s' => \$prefix,
228 'socket-wrapper' => \$opt_socket_wrapper,
229 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
230 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
231 'quick' => \$opt_quick,
232 'one' => \$opt_one,
233 'exclude=s' => \@opt_exclude,
234 'include=s' => \@opt_include,
235 'exclude-env=s' => \@opt_exclude_env,
236 'include-env=s' => \@opt_include_env,
237 'srcdir=s' => \$srcdir,
238 'bindir=s' => \$bindir,
239 'testenv' => \$opt_testenv,
240 'list' => \$opt_list,
241 'mitkrb5' => \$opt_mitkrb5,
242 'resetup-environment' => \$opt_resetup_env,
243 'testlist=s' => \@testlists,
244 'random-order' => \$opt_random_order,
245 'load-list=s' => \$opt_load_list,
246 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
247 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
248 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
249 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
250 'asan_so_path=s' => \$opt_libasan_so_path,
251 'use-dns-faking' => \$opt_use_dns_faking
254 exit(1) if (not $result);
256 ShowHelp() if ($opt_help);
258 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
260 # we want unbuffered output
261 $| = 1;
263 my @tests = @ARGV;
265 # quick hack to disable rpc validation when using valgrind - its way too slow
266 unless (defined($ENV{VALGRIND})) {
267 $ENV{VALIDATE} = "validate";
268 $ENV{MALLOC_CHECK_} = 3;
271 # make all our python scripts unbuffered
272 $ENV{PYTHONUNBUFFERED} = 1;
274 $ENV{SAMBA_DEPRECATED_SUPPRESS} = 1;
276 # do not depend on the users setup
277 # see also bootstrap/config.py
278 $ENV{TZ} = "UTC";
279 $ENV{LC_ALL} = $ENV{LANG} = "en_US.utf8";
281 my $bindir_abs = abs_path($bindir);
283 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
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 umask 0077;
298 mkdir($prefix, 0700) unless -d $prefix;
299 chmod 0700, $prefix;
300 # We need to have no umask limitations for the tests.
301 umask 0000;
303 my $prefix_abs = abs_path($prefix);
304 my $tmpdir_abs = abs_path("$prefix/tmp");
305 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
307 my $srcdir_abs = abs_path($srcdir);
309 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
310 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
312 $ENV{SAMBA_SELFTEST} = "1";
314 $ENV{PREFIX} = $prefix;
315 $ENV{PREFIX_ABS} = $prefix_abs;
316 $ENV{SRCDIR} = $srcdir;
317 $ENV{SRCDIR_ABS} = $srcdir_abs;
318 $ENV{BINDIR} = $bindir_abs;
320 my $tls_enabled = not $opt_quick;
321 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
323 sub prefix_pathvar($$)
325 my ($name, $newpath) = @_;
326 if (defined($ENV{$name})) {
327 $ENV{$name} = "$newpath:$ENV{$name}";
328 } else {
329 $ENV{$name} = $newpath;
332 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
333 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
335 if ($opt_socket_wrapper_keep_pcap) {
336 # Socket wrapper keep pcap implies socket wrapper pcap
337 $opt_socket_wrapper_pcap = 1;
340 if ($opt_socket_wrapper_pcap) {
341 # Socket wrapper pcap implies socket wrapper
342 $opt_socket_wrapper = 1;
345 my $ld_preload = $ENV{LD_PRELOAD};
347 if ($opt_libasan_so_path) {
348 if ($ld_preload) {
349 $ld_preload = "$opt_libasan_so_path:$ld_preload";
350 } else {
351 $ld_preload = "$opt_libasan_so_path";
355 if ($opt_libnss_wrapper_so_path) {
356 if ($ld_preload) {
357 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
358 } else {
359 $ld_preload = "$opt_libnss_wrapper_so_path";
363 if ($opt_libresolv_wrapper_so_path) {
364 if ($ld_preload) {
365 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
366 } else {
367 $ld_preload = "$opt_libresolv_wrapper_so_path";
371 if ($opt_libsocket_wrapper_so_path) {
372 if ($ld_preload) {
373 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
374 } else {
375 $ld_preload = "$opt_libsocket_wrapper_so_path";
379 if ($opt_libuid_wrapper_so_path) {
380 if ($ld_preload) {
381 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
382 } else {
383 $ld_preload = "$opt_libuid_wrapper_so_path";
387 if (defined($ENV{USE_NAMESPACES})) {
388 print "Using linux containerization for selftest testenv(s)...\n";
390 # Create a common bridge to connect up the testenv namespaces. We give
391 # it the client's IP address, as this is where the tests will run from
392 my $ipv4_addr = Samba::get_ipv4_addr("client");
393 my $ipv6_addr = Samba::get_ipv6_addr("client");
394 system "$ENV{SRCDIR_ABS}/selftest/ns/create_bridge.sh selftest0 $ipv4_addr $ipv6_addr";
397 $ENV{LD_PRELOAD} = $ld_preload;
398 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
400 # Enable uid_wrapper globally
401 $ENV{UID_WRAPPER} = 1;
403 # We are already hitting the limit, so double it.
404 $ENV{NSS_WRAPPER_MAX_HOSTENTS} = 200;
406 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
408 # This is needed in order to allow the ldb_*ldap module
409 # to work with a preloaded socket wrapper.
410 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
412 my $socket_wrapper_dir;
413 if ($opt_socket_wrapper) {
414 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
415 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
416 } elsif (not $opt_list) {
417 unless ($< == 0) {
418 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
422 if ($opt_use_dns_faking) {
423 print "DNS: Faking nameserver\n";
424 $ENV{SAMBA_DNS_FAKING} = 1;
427 my $testenv_default = "none";
429 if ($opt_mitkrb5 == 1) {
430 $ENV{MITKRB5} = $opt_mitkrb5;
431 $ENV{KRB5RCACHETYPE} = "none";
434 # After this many seconds, the server will self-terminate. All tests
435 # must terminate in this time, and testenv will only stay alive this
436 # long
438 my $server_maxtime;
439 if ($opt_testenv) {
440 # 1 year should be enough :-)
441 $server_maxtime = 365 * 24 * 60 * 60;
442 } else {
443 # make test should run under 5 hours
444 $server_maxtime = 5 * 60 * 60;
447 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
448 $server_maxtime = $ENV{SMBD_MAXTIME};
451 $target = new Samba($bindir, $srcdir, $server_maxtime,
452 $opt_socket_wrapper_pcap,
453 $opt_socket_wrapper_keep_pcap);
454 unless ($opt_list) {
455 if ($opt_target eq "samba") {
456 $testenv_default = "ad_dc";
457 } elsif ($opt_target eq "samba3") {
458 $testenv_default = "nt4_member";
462 sub read_test_regexes($)
464 my ($name) = @_;
465 my @ret = ();
466 open(LF, "<$name") or die("unable to read $name: $!");
467 while (<LF>) {
468 chomp;
469 next if (/^#/);
470 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
471 push (@ret, [$1, $4]);
472 } else {
473 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
474 push (@ret, [$_, undef]);
477 close(LF);
478 return @ret;
481 foreach (@opt_exclude) {
482 push (@excludes, read_test_regexes($_));
485 foreach (@opt_include) {
486 push (@includes, read_test_regexes($_));
489 # We give the selftest client 6 different IPv4 addresses to use. Most tests
490 # only use the first (.11) IP. Note that winsreplication.c is one test that
491 # uses the other IPs (search for iface_list_count()).
492 $ENV{SOCKET_WRAPPER_IPV4_NETWORK} = "10.53.57.0";
493 my $interfaces = Samba::get_interfaces_config("client", 6);
495 my $clientdir = "$prefix_abs/client";
497 my $conffile = "$clientdir/client.conf";
498 $ENV{SMB_CONF_PATH} = $conffile;
500 sub write_clientconf($$$)
502 my ($conffile, $clientdir, $vars) = @_;
504 mkdir("$clientdir", 0777) unless -d "$clientdir";
506 my @subdirs = (
507 { name => "private", mask => 0777 },
508 { name => "bind-dns", mask => 0777 },
509 { name => "lockdir", mask => 0777 },
510 { name => "statedir", mask => 0777 },
511 { name => "cachedir", mask => 0777 },
512 { name => "pkinit", mask => 0700 },
513 { name => "pid", mask => 0777 },
514 # the ncalrpcdir needs exactly 0755 otherwise tests fail.
515 { name => "ncalrpcdir", mask => 0755, umask => 0022 },
518 foreach my $sub (@subdirs) {
519 my $dir = "$clientdir/$sub->{name}";
520 remove_tree($dir);
521 my $mask = umask;
522 if (defined($sub->{umask})) {
523 umask $sub->{umask};
525 mkdir($dir, $sub->{mask});
526 umask $mask;
529 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
530 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
531 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
532 my $ca_users_dir = "$cadir/Users";
533 my $client_loglevel = $ENV{CLIENT_LOG_LEVEL} || 1;
535 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
536 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
537 # We make a copy here and make the certificated easily
538 # accessable in the client environment.
539 my $mask = umask;
540 umask 0077;
541 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
542 for my $d (readdir USERS) {
543 my $user_dir = "${ca_users_dir}/${d}";
544 next if ${d} =~ /^\./;
545 next if (! -d "${user_dir}");
546 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
547 for my $l (readdir USER) {
548 my $user_link = "${user_dir}/${l}";
549 next if ${l} =~ /^\./;
550 next if (! -l "${user_link}");
552 my $dest = "${clientdir}/pkinit/${l}";
553 Samba::copy_file_content(${user_link}, ${dest});
555 closedir USER;
557 closedir USERS;
558 umask $mask;
560 open(CF, ">$conffile");
561 print CF "[global]\n";
562 print CF "\tnetbios name = client\n";
563 if (defined($vars->{DOMAIN})) {
564 print CF "\tworkgroup = $vars->{DOMAIN}\n";
566 if (defined($vars->{REALM})) {
567 print CF "\trealm = $vars->{REALM}\n";
569 if ($opt_socket_wrapper) {
570 print CF "\tinterfaces = $interfaces\n";
572 print CF "
573 private dir = $clientdir/private
574 binddns dir = $clientdir/bind-dns
575 lock dir = $clientdir/lockdir
576 state directory = $clientdir/statedir
577 cache directory = $clientdir/cachedir
578 ncalrpc dir = $clientdir/ncalrpcdir
579 pid directory = $clientdir/pid
580 panic action = $RealBin/gdb_backtrace \%d
581 max xmit = 32K
582 notify:inotify = false
583 ldb:nosync = true
584 system:anonymous = true
585 client lanman auth = Yes
586 client min protocol = CORE
587 log level = $client_loglevel
588 torture:basedir = $clientdir
589 #We don't want to run 'speed' tests for very long
590 torture:timelimit = 1
591 winbind separator = /
592 tls cafile = ${cacert}
593 tls crlfile = ${cacrl_pem}
594 tls verify peer = no_check
595 include system krb5 conf = no
596 elasticsearch:mappings = $srcdir_abs/source3/rpc_server/mdssvc/elasticsearch_mappings.json
598 close(CF);
601 my @todo = ();
603 sub should_run_test($)
605 my $name = shift;
606 if ($#tests == -1) {
607 return 1;
609 for (my $i=0; $i <= $#tests; $i++) {
610 if ($name =~ /$tests[$i]/i) {
611 return 1;
614 return 0;
617 sub read_testlist($)
619 my ($filename) = @_;
621 my @ret = ();
622 open(IN, $filename) or die("Unable to open $filename: $!");
624 while (<IN>) {
625 if (/-- TEST(-LOADLIST|) --\n/) {
626 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
627 my $name = <IN>;
628 $name =~ s/\n//g;
629 my $env = <IN>;
630 $env =~ s/\n//g;
631 my $loadlist;
632 if ($supports_loadlist) {
633 $loadlist = <IN>;
634 $loadlist =~ s/\n//g;
636 my $cmdline = <IN>;
637 $cmdline =~ s/\n//g;
638 if (should_run_test($name) == 1) {
639 push (@ret, [$name, $env, $cmdline, $loadlist]);
641 } else {
642 print;
645 close(IN) or die("Error creating recipe from $filename");
646 return @ret;
649 if ($#testlists == -1) {
650 die("No testlists specified");
653 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
654 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
655 $ENV{TMPDIR} = "$tmpdir_abs";
656 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
657 if ($opt_quick) {
658 $ENV{SELFTEST_QUICK} = "1";
659 } else {
660 $ENV{SELFTEST_QUICK} = "";
662 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
664 my $selftest_resolv_conf_path = "$tmpdir_abs/selftest.resolv.conf";
665 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.global";
667 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
668 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
670 my $selftest_gnupghome_path = "$tmpdir_abs/selftest.no.gnupg";
671 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.global";
673 my @available = ();
674 foreach my $fn (@testlists) {
675 foreach (read_testlist($fn)) {
676 my $name = $$_[0];
677 next if (@includes and not defined(find_in_list(\@includes, $name)));
678 push (@available, $_);
682 my $restricted = undef;
683 my $restricted_used = {};
685 if ($opt_load_list) {
686 $restricted = [];
687 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
688 while (<LOAD_LIST>) {
689 chomp;
690 push (@$restricted, $_);
692 close(LOAD_LIST);
695 my $individual_tests = undef;
696 $individual_tests = {};
698 foreach my $testsuite (@available) {
699 my $name = $$testsuite[0];
700 my $skipreason = skip(@$testsuite);
701 if (defined($restricted)) {
702 # Find the testsuite for this test
703 my $match = undef;
704 foreach my $r (@$restricted) {
705 if ($r eq $name) {
706 $individual_tests->{$name} = [];
707 $match = $r;
708 $restricted_used->{$r} = 1;
709 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
710 push(@{$individual_tests->{$name}}, $r);
711 $match = $r;
712 $restricted_used->{$r} = 1;
715 if ($match) {
716 if (defined($skipreason)) {
717 if (not $opt_list) {
718 Subunit::skip_testsuite($name, $skipreason);
720 } else {
721 push(@todo, $testsuite);
724 } elsif (defined($skipreason)) {
725 if (not $opt_list) {
726 Subunit::skip_testsuite($name, $skipreason);
728 } else {
729 push(@todo, $testsuite);
733 if (defined($restricted)) {
734 foreach (@$restricted) {
735 unless (defined($restricted_used->{$_})) {
736 print "No test or testsuite found matching $_\n";
739 } elsif ($#todo == -1) {
740 print STDERR "No tests to run\n";
741 exit(1);
744 my $suitestotal = $#todo + 1;
746 unless ($opt_list) {
747 Subunit::progress($suitestotal);
748 Subunit::report_time();
751 my $i = 0;
752 $| = 1;
754 my %running_envs = ();
756 sub get_running_env($)
758 my ($name) = @_;
760 my $envname = $name;
762 $envname =~ s/:.*//;
764 return $running_envs{$envname};
767 sub sighandler($)
769 my $signame = shift;
771 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
772 $SIG{PIPE} = 'IGNORE';
774 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
776 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
777 teardown_env($_) foreach(keys %running_envs);
778 system("pstree -p $$");
779 print "$0: PID[$$]: Exiting...\n";
780 exit(1);
783 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
785 sub setup_env($$)
787 my ($name, $prefix) = @_;
789 my $testenv_vars = undef;
791 my $envname = $name;
792 my $option = $name;
794 $envname =~ s/:.*//;
795 $option =~ s/^[^:]*//;
796 $option =~ s/^://;
798 $option = "client" if $option eq "";
800 # Initially clear out the environment for the provision, so previous envs'
801 # variables don't leak in. Provisioning steps must explicitly set their
802 # necessary variables when calling out to other executables
803 Samba::clear_exported_envvars();
804 delete $ENV{SOCKET_WRAPPER_DEFAULT_IFACE};
805 delete $ENV{SMB_CONF_PATH};
807 $ENV{RESOLV_CONF} = "${selftest_resolv_conf_path}.${envname}/ignore";
808 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
809 $ENV{GNUPGHOME} = "${selftest_gnupghome_path}.${envname}/ignore";
811 if (defined(get_running_env($envname))) {
812 $testenv_vars = get_running_env($envname);
813 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
814 print $testenv_vars->{target}->getlog_env($testenv_vars);
815 $testenv_vars = undef;
817 } else {
818 $testenv_vars = $target->setup_env($envname, $prefix);
819 if (not defined($testenv_vars)) {
820 my $msg = "$opt_target can't start up known environment '$envname'";
821 if ($opt_one) {
822 die($msg);
824 warn $msg;
825 return;
827 if (ref $testenv_vars ne "HASH") {
828 return $testenv_vars;
830 if (defined($testenv_vars->{target})) {
831 $testenv_vars->{target} = $target;
835 return undef unless defined($testenv_vars);
837 $running_envs{$envname} = $testenv_vars;
839 if ($option eq "local") {
840 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
841 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
842 } elsif ($option eq "client") {
843 SocketWrapper::set_default_iface(11);
844 write_clientconf($conffile, $clientdir, $testenv_vars);
845 $ENV{SMB_CONF_PATH} = $conffile;
846 } else {
847 die("Unknown option[$option] for envname[$envname]");
850 # export the environment variables for the testenv (SERVER, SERVER_IP, etc)
851 Samba::export_envvars($testenv_vars);
853 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
854 unlink($krb5_ccache_path);
855 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
856 return $testenv_vars;
859 sub getlog_env($)
861 my ($envname) = @_;
862 return "" if ($envname eq "none");
863 my $env = get_running_env($envname);
864 return $env->{target}->getlog_env($env);
867 sub check_env($)
869 my ($envname) = @_;
870 my $env = get_running_env($envname);
871 return $env->{target}->check_env($env);
874 sub teardown_env($)
876 my ($envname) = @_;
877 return if ($envname eq "none");
878 print STDERR "teardown_env($envname)\n";
879 my $env = get_running_env($envname);
880 $env->{target}->teardown_env($env);
881 delete $running_envs{$envname};
884 # This 'global' file needs to be empty when we start
885 unlink("$prefix_abs/dns_host_file");
886 unlink("$prefix_abs/hosts");
888 if ($opt_random_order) {
889 require List::Util;
890 my @newtodo = List::Util::shuffle(@todo);
891 @todo = @newtodo;
894 if ($opt_testenv) {
895 my $testenv_name = $ENV{SELFTEST_TESTENV};
896 $testenv_name = $testenv_default unless defined($testenv_name);
898 my $testenv_vars = setup_env($testenv_name, $prefix);
900 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
901 die("Unable to setup environment $testenv_name");
904 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
905 $ENV{ENVNAME} = $testenv_name;
907 my $envvarstr = Samba::exported_envvars_str($testenv_vars);
909 my @term_args = ("echo -e \"
910 Welcome to the Samba4 Test environment '$testenv_name'
912 This matches the client environment used in make test
913 server is pid `cat \$PIDDIR/samba.pid`
915 Some useful environment variables:
916 TORTURE_OPTIONS=\$TORTURE_OPTIONS
917 SMB_CONF_PATH=\$SMB_CONF_PATH
919 $envvarstr
920 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
921 my @term = ();
922 if ($ENV{TERMINAL}) {
923 @term = ($ENV{TERMINAL});
924 # override the default terminal args (if specified)
925 if (defined($ENV{TERMINAL_ARGS})) {
926 @term_args = split(/ /, $ENV{TERMINAL_ARGS});
928 } else {
929 @term = ("xterm", "-e");
930 unshift(@term_args, ("bash", "-c"));
933 system(@term, @term_args);
935 teardown_env($testenv_name);
936 } elsif ($opt_list) {
937 foreach (@todo) {
938 my $name = $$_[0];
939 my $envname = $$_[1];
940 my $cmd = $$_[2];
941 my $listcmd = $$_[3];
943 unless (defined($listcmd)) {
944 warn("Unable to list tests in $name");
945 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
946 # a single "test".
947 print "$name\n";
948 next;
951 system($listcmd);
953 if ($? == -1) {
954 die("Unable to run $listcmd: $!");
955 } elsif ($? & 127) {
956 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
959 my $exitcode = $? >> 8;
960 if ($exitcode != 0) {
961 die("$cmd exited with exit code $exitcode");
964 } else {
965 foreach (@todo) {
966 $i++;
967 my $cmd = $$_[2];
968 my $name = $$_[0];
969 my $envname = $$_[1];
970 my $envvars = setup_env($envname, $prefix);
972 if (not defined($envvars)) {
973 Subunit::start_testsuite($name);
974 Subunit::end_testsuite($name, "error",
975 "unable to set up environment $envname - exiting");
976 next;
977 } elsif ($envvars eq "UNKNOWN") {
978 Subunit::start_testsuite($name);
979 Subunit::end_testsuite($name, "error",
980 "environment $envname is unknown - exiting");
981 next;
984 # Generate a file with the individual tests to run, if the
985 # test runner for this test suite supports it.
986 if ($individual_tests and $individual_tests->{$name}) {
987 if ($$_[3]) {
988 my ($fh, $listid_file) = tempfile(UNLINK => 0);
989 foreach my $test (@{$individual_tests->{$name}}) {
990 print $fh substr($test, length($name)+1) . "\n";
992 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
993 } else {
994 warn("Unable to run individual tests in $name, it does not support --loadlist.");
998 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1000 teardown_env($envname) if ($opt_resetup_env);
1004 print "\n";
1006 teardown_env($_) foreach (keys %running_envs);
1008 my $failed = 0;
1010 # if there were any valgrind failures, show them
1011 foreach (<$prefix/valgrind.log*>) {
1012 next unless (-s $_);
1013 print "VALGRIND FAILURE\n";
1014 $failed++;
1015 system("cat $_");
1017 exit 0;