dnsserver: Tighten DNS name checking
[Samba.git] / selftest / selftest.pl
blobb3ef65828d7116dd3ed92c13be54a1ef1d258fa1
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;
30 use target::Samba;
32 eval {
33 require Time::HiRes;
34 Time::HiRes->import("time");
36 if ($@) {
37 print "You don't have Time::Hires installed !\n";
40 my $opt_help = 0;
41 my $opt_target = "samba";
42 my $opt_quick = 0;
43 my $opt_socket_wrapper = 0;
44 my $opt_socket_wrapper_pcap = undef;
45 my $opt_socket_wrapper_keep_pcap = undef;
46 my $opt_random_order = 0;
47 my $opt_one = 0;
48 my @opt_exclude = ();
49 my @opt_include = ();
50 my @opt_exclude_env = ();
51 my @opt_include_env = ();
52 my $opt_testenv = 0;
53 my $opt_list = 0;
54 my $opt_mitkrb5 = 0;
55 my $ldap = undef;
56 my $opt_resetup_env = undef;
57 my $opt_load_list = undef;
58 my $opt_libnss_wrapper_so_path = "";
59 my $opt_libresolv_wrapper_so_path = "";
60 my $opt_libsocket_wrapper_so_path = "";
61 my $opt_libuid_wrapper_so_path = "";
62 my $opt_use_dns_faking = 0;
63 my @testlists = ();
65 my $srcdir = ".";
66 my $bindir = "./bin";
67 my $prefix = "./st";
69 my @includes = ();
70 my @excludes = ();
72 sub find_in_list($$)
74 my ($list, $fullname) = @_;
76 foreach (@$list) {
77 if ($fullname =~ /$$_[0]/) {
78 return ($$_[1]) if ($$_[1]);
79 return "";
83 return undef;
86 sub skip($)
88 my ($name) = @_;
90 return find_in_list(\@excludes, $name);
93 sub getlog_env($);
95 sub setup_pcap($)
97 my ($name) = @_;
99 return unless ($opt_socket_wrapper_pcap);
100 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
102 my $fname = $name;
103 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
105 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
107 SocketWrapper::setup_pcap($pcap_file);
109 return $pcap_file;
112 sub cleanup_pcap($$)
114 my ($pcap_file, $exitcode) = @_;
116 return unless ($opt_socket_wrapper_pcap);
117 return if ($opt_socket_wrapper_keep_pcap);
118 return unless ($exitcode == 0);
119 return unless defined($pcap_file);
121 unlink($pcap_file);
124 # expand strings from %ENV
125 sub expand_environment_strings($)
127 my $s = shift;
128 # we use a reverse sort so we do the longer ones first
129 foreach my $k (sort { $b cmp $a } keys %ENV) {
130 $s =~ s/\$$k/$ENV{$k}/g;
132 return $s;
135 sub run_testsuite($$$$$)
137 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
138 my $pcap_file = setup_pcap($name);
140 Subunit::start_testsuite($name);
141 Subunit::progress_push();
142 Subunit::report_time(time());
143 system($cmd);
144 Subunit::report_time(time());
145 Subunit::progress_pop();
147 if ($? == -1) {
148 print "command: $cmd\n";
149 printf "expanded command: %s\n", expand_environment_strings($cmd);
150 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
151 exit(1);
152 } elsif ($? & 127) {
153 print "command: $cmd\n";
154 printf "expanded command: %s\n", expand_environment_strings($cmd);
155 Subunit::end_testsuite($name, "error",
156 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
157 exit(1);
160 my $exitcode = $? >> 8;
162 my $envlog = getlog_env($envname);
163 if ($envlog ne "") {
164 print "envlog: $envlog\n";
167 print "command: $cmd\n";
168 printf "expanded command: %s\n", expand_environment_strings($cmd);
170 if ($exitcode == 0) {
171 Subunit::end_testsuite($name, "success");
172 } else {
173 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
176 cleanup_pcap($pcap_file, $exitcode);
178 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
179 print "PCAP FILE: $pcap_file\n";
182 if ($exitcode != 0) {
183 exit(1) if ($opt_one);
186 return $exitcode;
189 sub ShowHelp()
191 print "Samba test runner
192 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
193 Copyright (C) Stefan Metzmacher <metze\@samba.org>
195 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
197 Generic options:
198 --help this help page
199 --target=samba[3]|win Samba version to target
200 --testlist=FILE file to read available tests from
201 --exclude=FILE Exclude tests listed in the file
202 --include=FILE Include tests listed in the file
203 --exclude-env=ENV Exclude tests for the specified environment
204 --include-env=ENV Include tests for the specified environment
206 Paths:
207 --prefix=DIR prefix to run tests in [st]
208 --srcdir=DIR source directory [.]
209 --bindir=DIR binaries directory [./bin]
211 Preload cwrap:
212 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
213 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
214 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
215 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
217 DNS:
218 --use-dns-faking Fake DNS entries rather than talking to our
219 DNS implementation.
221 Target Specific:
222 --socket-wrapper-pcap save traffic to pcap directories
223 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
224 failed
225 --socket-wrapper enable socket wrapper
227 Samba4 Specific:
228 --ldap=openldap|fedora-ds back samba onto specified ldap server
230 Behaviour:
231 --quick run quick overall test
232 --one abort when the first test fails
233 --testenv run a shell in the requested test environment
234 --list list available tests
236 exit(0);
239 my $result = GetOptions (
240 'help|h|?' => \$opt_help,
241 'target=s' => \$opt_target,
242 'prefix=s' => \$prefix,
243 'socket-wrapper' => \$opt_socket_wrapper,
244 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
245 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
246 'quick' => \$opt_quick,
247 'one' => \$opt_one,
248 'exclude=s' => \@opt_exclude,
249 'include=s' => \@opt_include,
250 'exclude-env=s' => \@opt_exclude_env,
251 'include-env=s' => \@opt_include_env,
252 'srcdir=s' => \$srcdir,
253 'bindir=s' => \$bindir,
254 'testenv' => \$opt_testenv,
255 'list' => \$opt_list,
256 'mitkrb5' => \$opt_mitkrb5,
257 'ldap:s' => \$ldap,
258 'resetup-environment' => \$opt_resetup_env,
259 'testlist=s' => \@testlists,
260 'random-order' => \$opt_random_order,
261 'load-list=s' => \$opt_load_list,
262 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
263 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
264 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
265 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
266 'use-dns-faking' => \$opt_use_dns_faking
269 exit(1) if (not $result);
271 ShowHelp() if ($opt_help);
273 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
275 # we want unbuffered output
276 $| = 1;
278 my @tests = @ARGV;
280 # quick hack to disable rpc validation when using valgrind - its way too slow
281 unless (defined($ENV{VALGRIND})) {
282 $ENV{VALIDATE} = "validate";
283 $ENV{MALLOC_CHECK_} = 3;
286 # make all our python scripts unbuffered
287 $ENV{PYTHONUNBUFFERED} = 1;
289 my $bindir_abs = abs_path($bindir);
291 # Backwards compatibility:
292 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
293 if (defined($ENV{FEDORA_DS_ROOT})) {
294 $ldap = "fedora-ds";
295 } else {
296 $ldap = "openldap";
300 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
301 if ($ldap) {
302 # LDAP is slow
303 $torture_maxtime *= 2;
306 $prefix =~ s+//+/+;
307 $prefix =~ s+/./+/+;
308 $prefix =~ s+/$++;
310 die("using an empty prefix isn't allowed") unless $prefix ne "";
312 # Ensure we have the test prefix around.
314 # We need restrictive
315 # permissions on this as some subdirectories in this tree will have
316 # wider permissions (ie 0777) and this would allow other users on the
317 # host to subvert the test process.
318 umask 0077;
319 mkdir($prefix, 0700) unless -d $prefix;
320 chmod 0700, $prefix;
321 # We need to have no umask limitations for the tests.
322 umask 0000;
324 my $prefix_abs = abs_path($prefix);
325 my $tmpdir_abs = abs_path("$prefix/tmp");
326 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
328 my $srcdir_abs = abs_path($srcdir);
330 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
331 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
333 $ENV{SAMBA_SELFTEST} = "1";
335 $ENV{PREFIX} = $prefix;
336 $ENV{PREFIX_ABS} = $prefix_abs;
337 $ENV{SRCDIR} = $srcdir;
338 $ENV{SRCDIR_ABS} = $srcdir_abs;
339 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
340 $ENV{BINDIR} = $bindir_abs;
342 my $tls_enabled = not $opt_quick;
343 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
345 sub prefix_pathvar($$)
347 my ($name, $newpath) = @_;
348 if (defined($ENV{$name})) {
349 $ENV{$name} = "$newpath:$ENV{$name}";
350 } else {
351 $ENV{$name} = $newpath;
354 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
355 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
357 if ($opt_socket_wrapper_keep_pcap) {
358 # Socket wrapper keep pcap implies socket wrapper pcap
359 $opt_socket_wrapper_pcap = 1;
362 if ($opt_socket_wrapper_pcap) {
363 # Socket wrapper pcap implies socket wrapper
364 $opt_socket_wrapper = 1;
367 my $ld_preload = $ENV{LD_PRELOAD};
369 if ($opt_libnss_wrapper_so_path) {
370 if ($ld_preload) {
371 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
372 } else {
373 $ld_preload = "$opt_libnss_wrapper_so_path";
377 if ($opt_libresolv_wrapper_so_path) {
378 if ($ld_preload) {
379 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
380 } else {
381 $ld_preload = "$opt_libresolv_wrapper_so_path";
385 if ($opt_libsocket_wrapper_so_path) {
386 if ($ld_preload) {
387 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
388 } else {
389 $ld_preload = "$opt_libsocket_wrapper_so_path";
393 if ($opt_libuid_wrapper_so_path) {
394 if ($ld_preload) {
395 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
396 } else {
397 $ld_preload = "$opt_libuid_wrapper_so_path";
401 $ENV{LD_PRELOAD} = $ld_preload;
402 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
404 # Enable uid_wrapper globally
405 $ENV{UID_WRAPPER} = 1;
407 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
409 # This is needed in order to allow the ldb_*ldap module
410 # to work with a preloaded socket wrapper.
411 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
413 my $socket_wrapper_dir;
414 if ($opt_socket_wrapper) {
415 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
416 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
417 } elsif (not $opt_list) {
418 unless ($< == 0) {
419 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
423 if ($opt_use_dns_faking) {
424 print "DNS: Faking nameserver\n";
425 $ENV{SAMBA_DNS_FAKING} = 1;
428 my $target;
429 my $testenv_default = "none";
431 if ($opt_mitkrb5 == 1) {
432 $ENV{MITKRB5} = $opt_mitkrb5;
435 # After this many seconds, the server will self-terminate. All tests
436 # must terminate in this time, and testenv will only stay alive this
437 # long
439 my $server_maxtime;
440 if ($opt_testenv) {
441 # 1 year should be enough :-)
442 $server_maxtime = 365 * 24 * 60 * 60;
443 } else {
444 # make test should run under 4 hours
445 $server_maxtime = 4 * 60 * 60;
448 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
449 $server_maxtime = $ENV{SMBD_MAXTIME};
452 unless ($opt_list) {
453 if ($opt_target eq "samba") {
454 $testenv_default = "ad_dc";
455 require target::Samba;
456 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
457 } elsif ($opt_target eq "samba3") {
458 $testenv_default = "nt4_member";
459 require target::Samba3;
460 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
464 sub read_test_regexes($)
466 my ($name) = @_;
467 my @ret = ();
468 open(LF, "<$name") or die("unable to read $name: $!");
469 while (<LF>) {
470 chomp;
471 next if (/^#/);
472 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
473 push (@ret, [$1, $4]);
474 } else {
475 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
476 push (@ret, [$_, undef]);
479 close(LF);
480 return @ret;
483 foreach (@opt_exclude) {
484 push (@excludes, read_test_regexes($_));
487 foreach (@opt_include) {
488 push (@includes, read_test_regexes($_));
491 my $interfaces = join(',', ("127.0.0.11/8",
492 "127.0.0.12/8",
493 "127.0.0.13/8",
494 "127.0.0.14/8",
495 "127.0.0.15/8",
496 "127.0.0.16/8"));
498 my $clientdir = "$prefix_abs/client";
500 my $conffile = "$clientdir/client.conf";
501 $ENV{SMB_CONF_PATH} = $conffile;
503 sub write_clientconf($$$)
505 my ($conffile, $clientdir, $vars) = @_;
507 mkdir("$clientdir", 0777) unless -d "$clientdir";
509 if ( -d "$clientdir/private" ) {
510 unlink <$clientdir/private/*>;
511 } else {
512 mkdir("$clientdir/private", 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 my $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 lock dir = $clientdir/lockdir
599 state directory = $clientdir/statedir
600 cache directory = $clientdir/cachedir
601 ncalrpc dir = $clientdir/ncalrpcdir
602 panic action = $RealBin/gdb_backtrace \%d
603 max xmit = 32K
604 notify:inotify = false
605 ldb:nosync = true
606 system:anonymous = true
607 client lanman auth = Yes
608 log level = 1
609 torture:basedir = $clientdir
610 #We don't want to pass our self-tests if the PAC code is wrong
611 gensec:require_pac = true
612 #We don't want to run 'speed' tests for very long
613 torture:timelimit = 1
614 winbind separator = /
615 tls cafile = ${cacert}
616 tls crlfile = ${cacrl_pem}
617 tls verify peer = no_check
618 include system krb5 conf = no
620 close(CF);
623 my @todo = ();
625 sub should_run_test($)
627 my $name = shift;
628 if ($#tests == -1) {
629 return 1;
631 for (my $i=0; $i <= $#tests; $i++) {
632 if ($name =~ /$tests[$i]/i) {
633 return 1;
636 return 0;
639 sub read_testlist($)
641 my ($filename) = @_;
643 my @ret = ();
644 open(IN, $filename) or die("Unable to open $filename: $!");
646 while (<IN>) {
647 if (/-- TEST(-LOADLIST|) --\n/) {
648 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
649 my $name = <IN>;
650 $name =~ s/\n//g;
651 my $env = <IN>;
652 $env =~ s/\n//g;
653 my $loadlist;
654 if ($supports_loadlist) {
655 $loadlist = <IN>;
656 $loadlist =~ s/\n//g;
658 my $cmdline = <IN>;
659 $cmdline =~ s/\n//g;
660 if (should_run_test($name) == 1) {
661 push (@ret, [$name, $env, $cmdline, $loadlist]);
663 } else {
664 print;
667 close(IN) or die("Error creating recipe from $filename");
668 return @ret;
671 if ($#testlists == -1) {
672 die("No testlists specified");
675 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
676 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
677 $ENV{TMPDIR} = "$tmpdir_abs";
678 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
679 if ($opt_socket_wrapper) {
680 $ENV{SELFTEST_INTERFACES} = $interfaces;
681 } else {
682 $ENV{SELFTEST_INTERFACES} = "";
684 if ($opt_quick) {
685 $ENV{SELFTEST_QUICK} = "1";
686 } else {
687 $ENV{SELFTEST_QUICK} = "";
689 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
691 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
692 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
694 my @available = ();
695 foreach my $fn (@testlists) {
696 foreach (read_testlist($fn)) {
697 my $name = $$_[0];
698 next if (@includes and not defined(find_in_list(\@includes, $name)));
699 push (@available, $_);
703 my $restricted = undef;
704 my $restricted_used = {};
706 if ($opt_load_list) {
707 $restricted = [];
708 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
709 while (<LOAD_LIST>) {
710 chomp;
711 push (@$restricted, $_);
713 close(LOAD_LIST);
716 my $individual_tests = undef;
717 $individual_tests = {};
719 foreach my $testsuite (@available) {
720 my $name = $$testsuite[0];
721 my $skipreason = skip($name);
722 if (defined($restricted)) {
723 # Find the testsuite for this test
724 my $match = undef;
725 foreach my $r (@$restricted) {
726 if ($r eq $name) {
727 $individual_tests->{$name} = [];
728 $match = $r;
729 $restricted_used->{$r} = 1;
730 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
731 push(@{$individual_tests->{$name}}, $r);
732 $match = $r;
733 $restricted_used->{$r} = 1;
736 if ($match) {
737 if (defined($skipreason)) {
738 if (not $opt_list) {
739 Subunit::skip_testsuite($name, $skipreason);
741 } else {
742 push(@todo, $testsuite);
745 } elsif (defined($skipreason)) {
746 if (not $opt_list) {
747 Subunit::skip_testsuite($name, $skipreason);
749 } else {
750 push(@todo, $testsuite);
754 if (defined($restricted)) {
755 foreach (@$restricted) {
756 unless (defined($restricted_used->{$_})) {
757 print "No test or testsuite found matching $_\n";
760 } elsif ($#todo == -1) {
761 print STDERR "No tests to run\n";
762 exit(1);
765 my $suitestotal = $#todo + 1;
767 unless ($opt_list) {
768 Subunit::progress($suitestotal);
769 Subunit::report_time(time());
772 my $i = 0;
773 $| = 1;
775 my %running_envs = ();
777 sub get_running_env($)
779 my ($name) = @_;
781 my $envname = $name;
783 $envname =~ s/:.*//;
785 return $running_envs{$envname};
788 my @exported_envvars = (
789 # domain stuff
790 "DOMAIN",
791 "REALM",
793 # stuff related to a trusted domain
794 "TRUST_SERVER",
795 "TRUST_SERVER_IP",
796 "TRUST_SERVER_IPV6",
797 "TRUST_NETBIOSNAME",
798 "TRUST_USERNAME",
799 "TRUST_PASSWORD",
800 "TRUST_DOMAIN",
801 "TRUST_REALM",
803 # domain controller stuff
804 "DC_SERVER",
805 "DC_SERVER_IP",
806 "DC_SERVER_IPV6",
807 "DC_NETBIOSNAME",
808 "DC_NETBIOSALIAS",
810 # domain member
811 "MEMBER_SERVER",
812 "MEMBER_SERVER_IP",
813 "MEMBER_SERVER_IPV6",
814 "MEMBER_NETBIOSNAME",
815 "MEMBER_NETBIOSALIAS",
817 # rpc proxy controller stuff
818 "RPC_PROXY_SERVER",
819 "RPC_PROXY_SERVER_IP",
820 "RPC_PROXY_SERVER_IPV6",
821 "RPC_PROXY_NETBIOSNAME",
822 "RPC_PROXY_NETBIOSALIAS",
824 # domain controller stuff for Vampired DC
825 "VAMPIRE_DC_SERVER",
826 "VAMPIRE_DC_SERVER_IP",
827 "VAMPIRE_DC_SERVER_IPV6",
828 "VAMPIRE_DC_NETBIOSNAME",
829 "VAMPIRE_DC_NETBIOSALIAS",
831 # domain controller stuff for FL 2000 Vampired DC
832 "VAMPIRE_2000_DC_SERVER",
833 "VAMPIRE_2000_DC_SERVER_IP",
834 "VAMPIRE_2000_DC_SERVER_IPV6",
835 "VAMPIRE_2000_DC_NETBIOSNAME",
836 "VAMPIRE_2000_DC_NETBIOSALIAS",
838 "PROMOTED_DC_SERVER",
839 "PROMOTED_DC_SERVER_IP",
840 "PROMOTED_DC_SERVER_IPV6",
841 "PROMOTED_DC_NETBIOSNAME",
842 "PROMOTED_DC_NETBIOSALIAS",
844 # server stuff
845 "SERVER",
846 "SERVER_IP",
847 "SERVER_IPV6",
848 "NETBIOSNAME",
849 "NETBIOSALIAS",
851 # user stuff
852 "USERNAME",
853 "USERID",
854 "PASSWORD",
855 "DC_USERNAME",
856 "DC_PASSWORD",
858 # UID/GID for rfc2307 mapping tests
859 "UID_RFC2307TEST",
860 "GID_RFC2307TEST",
862 # misc stuff
863 "KRB5_CONFIG",
864 "KRB5CCNAME",
865 "SELFTEST_WINBINDD_SOCKET_DIR",
866 "NMBD_SOCKET_DIR",
867 "LOCAL_PATH",
868 "DNS_FORWARDER1",
869 "DNS_FORWARDER2",
870 "RESOLV_CONF",
871 "UNACCEPTABLE_PASSWORD",
872 "LOCK_DIR",
874 # nss_wrapper
875 "NSS_WRAPPER_PASSWD",
876 "NSS_WRAPPER_GROUP",
877 "NSS_WRAPPER_HOSTS",
878 "NSS_WRAPPER_MODULE_SO_PATH",
879 "NSS_WRAPPER_MODULE_FN_PREFIX",
881 # resolv_wrapper
882 "RESOLV_WRAPPER_CONF",
883 "RESOLV_WRAPPER_HOSTS",
886 sub sighandler($)
888 my $signame = shift;
890 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
891 $SIG{PIPE} = 'IGNORE';
893 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
895 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
896 teardown_env($_) foreach(keys %running_envs);
897 system("pstree -p $$");
898 print "$0: PID[$$]: Exiting...\n";
899 exit(1);
902 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
904 sub setup_env($$)
906 my ($name, $prefix) = @_;
908 my $testenv_vars = undef;
910 my $envname = $name;
911 my $option = $name;
913 $envname =~ s/:.*//;
914 $option =~ s/^[^:]*//;
915 $option =~ s/^://;
917 $option = "client" if $option eq "";
919 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
921 if (defined(get_running_env($envname))) {
922 $testenv_vars = get_running_env($envname);
923 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
924 print $testenv_vars->{target}->getlog_env($testenv_vars);
925 $testenv_vars = undef;
927 } else {
928 $testenv_vars = $target->setup_env($envname, $prefix);
929 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
930 return $testenv_vars;
931 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
932 $testenv_vars->{target} = $target;
934 if (not defined($testenv_vars)) {
935 warn("$opt_target can't start up known environment '$envname'");
939 return undef unless defined($testenv_vars);
941 $running_envs{$envname} = $testenv_vars;
943 if ($option eq "local") {
944 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
945 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
946 } elsif ($option eq "client") {
947 SocketWrapper::set_default_iface(11);
948 write_clientconf($conffile, $clientdir, $testenv_vars);
949 $ENV{SMB_CONF_PATH} = $conffile;
950 } else {
951 die("Unknown option[$option] for envname[$envname]");
954 foreach (@exported_envvars) {
955 if (defined($testenv_vars->{$_})) {
956 $ENV{$_} = $testenv_vars->{$_};
957 } else {
958 delete $ENV{$_};
962 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
963 unlink($krb5_ccache_path);
964 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
965 return $testenv_vars;
968 sub exported_envvars_str($)
970 my ($testenv_vars) = @_;
971 my $out = "";
973 foreach (@exported_envvars) {
974 next unless defined($testenv_vars->{$_});
975 $out .= $_."=".$testenv_vars->{$_}."\n";
978 return $out;
981 sub getlog_env($)
983 my ($envname) = @_;
984 return "" if ($envname eq "none");
985 my $env = get_running_env($envname);
986 return $env->{target}->getlog_env($env);
989 sub check_env($)
991 my ($envname) = @_;
992 my $env = get_running_env($envname);
993 return $env->{target}->check_env($env);
996 sub teardown_env($)
998 my ($envname) = @_;
999 return if ($envname eq "none");
1000 print STDERR "teardown_env($envname)\n";
1001 my $env = get_running_env($envname);
1002 $env->{target}->teardown_env($env);
1003 delete $running_envs{$envname};
1006 # This 'global' file needs to be empty when we start
1007 unlink("$prefix_abs/dns_host_file");
1008 unlink("$prefix_abs/hosts");
1010 if ($opt_random_order) {
1011 require List::Util;
1012 my @newtodo = List::Util::shuffle(@todo);
1013 @todo = @newtodo;
1016 if ($opt_testenv) {
1017 my $testenv_name = $ENV{SELFTEST_TESTENV};
1018 $testenv_name = $testenv_default unless defined($testenv_name);
1020 my $testenv_vars = setup_env($testenv_name, $prefix);
1022 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1023 die("Unable to setup environment $testenv_name");
1026 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1027 $ENV{ENVNAME} = $testenv_name;
1029 my $envvarstr = exported_envvars_str($testenv_vars);
1031 my @term_args = ("echo -e \"
1032 Welcome to the Samba4 Test environment '$testenv_name'
1034 This matches the client environment used in make test
1035 server is pid `cat \$PIDDIR/samba.pid`
1037 Some useful environment variables:
1038 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1039 SMB_CONF_PATH=\$SMB_CONF_PATH
1041 $envvarstr
1042 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1043 my @term = ();
1044 if ($ENV{TERMINAL}) {
1045 @term = ($ENV{TERMINAL});
1046 } else {
1047 @term = ("xterm", "-e");
1048 unshift(@term_args, ("bash", "-c"));
1051 system(@term, @term_args);
1053 teardown_env($testenv_name);
1054 } elsif ($opt_list) {
1055 foreach (@todo) {
1056 my $name = $$_[0];
1057 my $envname = $$_[1];
1058 my $cmd = $$_[2];
1059 my $listcmd = $$_[3];
1061 unless (defined($listcmd)) {
1062 warn("Unable to list tests in $name");
1063 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1064 # a single "test".
1065 print "$name\n";
1066 next;
1069 system($listcmd);
1071 if ($? == -1) {
1072 die("Unable to run $listcmd: $!");
1073 } elsif ($? & 127) {
1074 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1077 my $exitcode = $? >> 8;
1078 if ($exitcode != 0) {
1079 die("$cmd exited with exit code $exitcode");
1082 } else {
1083 foreach (@todo) {
1084 $i++;
1085 my $cmd = $$_[2];
1086 my $name = $$_[0];
1087 my $envname = $$_[1];
1088 my ($env_basename, $env_localpart) = split(/:/, $envname);
1089 my $envvars = "SKIP";
1091 if (@opt_include_env) {
1092 foreach my $env (@opt_include_env) {
1093 if ($env_basename eq $env) {
1094 $envvars = setup_env($envname, $prefix);
1097 } elsif (@opt_exclude_env) {
1098 my $excluded = 0;
1099 foreach my $env (@opt_exclude_env) {
1100 if ($env_basename eq $env) {
1101 $excluded = 1;
1104 if ($excluded == 0) {
1105 $envvars = setup_env($envname, $prefix);
1107 } else {
1108 $envvars = setup_env($envname, $prefix);
1111 if (not defined($envvars)) {
1112 Subunit::start_testsuite($name);
1113 Subunit::end_testsuite($name, "error",
1114 "unable to set up environment $envname - exiting");
1115 next;
1116 } elsif ($envvars eq "SKIP") {
1117 Subunit::start_testsuite($name);
1118 Subunit::end_testsuite($name, "skip",
1119 "environment $envname is disabled (via --exclude-env / --include-env command line options) in this test run - skipping");
1120 next;
1121 } elsif ($envvars eq "UNKNOWN") {
1122 Subunit::start_testsuite($name);
1123 Subunit::end_testsuite($name, "skip",
1124 "environment $envname is unknown in this test backend - skipping");
1125 next;
1128 # Generate a file with the individual tests to run, if the
1129 # test runner for this test suite supports it.
1130 if ($individual_tests and $individual_tests->{$name}) {
1131 if ($$_[3]) {
1132 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1133 foreach my $test (@{$individual_tests->{$name}}) {
1134 print $fh substr($test, length($name)+1) . "\n";
1136 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1137 } else {
1138 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1142 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1144 teardown_env($envname) if ($opt_resetup_env);
1148 print "\n";
1150 teardown_env($_) foreach (keys %running_envs);
1152 my $failed = 0;
1154 # if there were any valgrind failures, show them
1155 foreach (<$prefix/valgrind.log*>) {
1156 next unless (-s $_);
1157 print "VALGRIND FAILURE\n";
1158 $failed++;
1159 system("cat $_");
1161 exit 0;