s4:selftest: Only run auth_log tests with Heimdal
[Samba.git] / selftest / selftest.pl
blob32fc845bde55bb37d3e613fa7f9586130e53ec5a
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_testenv = 0;
51 my $opt_list = 0;
52 my $ldap = undef;
53 my $opt_resetup_env = undef;
54 my $opt_load_list = undef;
55 my $opt_libnss_wrapper_so_path = "";
56 my $opt_libresolv_wrapper_so_path = "";
57 my $opt_libsocket_wrapper_so_path = "";
58 my $opt_libuid_wrapper_so_path = "";
59 my $opt_use_dns_faking = 0;
60 my @testlists = ();
62 my $srcdir = ".";
63 my $bindir = "./bin";
64 my $prefix = "./st";
66 my @includes = ();
67 my @excludes = ();
69 sub find_in_list($$)
71 my ($list, $fullname) = @_;
73 foreach (@$list) {
74 if ($fullname =~ /$$_[0]/) {
75 return ($$_[1]) if ($$_[1]);
76 return "";
80 return undef;
83 sub skip($)
85 my ($name) = @_;
87 return find_in_list(\@excludes, $name);
90 sub getlog_env($);
92 sub setup_pcap($)
94 my ($name) = @_;
96 return unless ($opt_socket_wrapper_pcap);
97 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
99 my $fname = $name;
100 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
102 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
104 SocketWrapper::setup_pcap($pcap_file);
106 return $pcap_file;
109 sub cleanup_pcap($$)
111 my ($pcap_file, $exitcode) = @_;
113 return unless ($opt_socket_wrapper_pcap);
114 return if ($opt_socket_wrapper_keep_pcap);
115 return unless ($exitcode == 0);
116 return unless defined($pcap_file);
118 unlink($pcap_file);
121 # expand strings from %ENV
122 sub expand_environment_strings($)
124 my $s = shift;
125 # we use a reverse sort so we do the longer ones first
126 foreach my $k (sort { $b cmp $a } keys %ENV) {
127 $s =~ s/\$$k/$ENV{$k}/g;
129 return $s;
132 sub run_testsuite($$$$$)
134 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
135 my $pcap_file = setup_pcap($name);
137 Subunit::start_testsuite($name);
138 Subunit::progress_push();
139 Subunit::report_time(time());
140 system($cmd);
141 Subunit::report_time(time());
142 Subunit::progress_pop();
144 if ($? == -1) {
145 print "command: $cmd\n";
146 printf "expanded command: %s\n", expand_environment_strings($cmd);
147 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
148 exit(1);
149 } elsif ($? & 127) {
150 print "command: $cmd\n";
151 printf "expanded command: %s\n", expand_environment_strings($cmd);
152 Subunit::end_testsuite($name, "error",
153 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
154 exit(1);
157 my $exitcode = $? >> 8;
159 my $envlog = getlog_env($envname);
160 if ($envlog ne "") {
161 print "envlog: $envlog\n";
164 print "command: $cmd\n";
165 printf "expanded command: %s\n", expand_environment_strings($cmd);
167 if ($exitcode == 0) {
168 Subunit::end_testsuite($name, "success");
169 } else {
170 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
173 cleanup_pcap($pcap_file, $exitcode);
175 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
176 print "PCAP FILE: $pcap_file\n";
179 if ($exitcode != 0) {
180 exit(1) if ($opt_one);
183 return $exitcode;
186 sub ShowHelp()
188 print "Samba test runner
189 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
190 Copyright (C) Stefan Metzmacher <metze\@samba.org>
192 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
194 Generic options:
195 --help this help page
196 --target=samba[3]|win Samba version to target
197 --testlist=FILE file to read available tests from
198 --exclude=FILE Exclude tests listed in the file
199 --include=FILE Include tests listed in the file
201 Paths:
202 --prefix=DIR prefix to run tests in [st]
203 --srcdir=DIR source directory [.]
204 --bindir=DIR binaries directory [./bin]
206 Preload cwrap:
207 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
208 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
209 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
210 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
212 DNS:
213 --use-dns-faking Fake DNS entries rather than talking to our
214 DNS implementation.
216 Target Specific:
217 --socket-wrapper-pcap save traffic to pcap directories
218 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
219 failed
220 --socket-wrapper enable socket wrapper
222 Samba4 Specific:
223 --ldap=openldap|fedora-ds back samba onto specified ldap server
225 Behaviour:
226 --quick run quick overall test
227 --one abort when the first test fails
228 --testenv run a shell in the requested test environment
229 --list list available tests
231 exit(0);
234 my $result = GetOptions (
235 'help|h|?' => \$opt_help,
236 'target=s' => \$opt_target,
237 'prefix=s' => \$prefix,
238 'socket-wrapper' => \$opt_socket_wrapper,
239 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
240 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
241 'quick' => \$opt_quick,
242 'one' => \$opt_one,
243 'exclude=s' => \@opt_exclude,
244 'include=s' => \@opt_include,
245 'srcdir=s' => \$srcdir,
246 'bindir=s' => \$bindir,
247 'testenv' => \$opt_testenv,
248 'list' => \$opt_list,
249 'ldap:s' => \$ldap,
250 'resetup-environment' => \$opt_resetup_env,
251 'testlist=s' => \@testlists,
252 'random-order' => \$opt_random_order,
253 'load-list=s' => \$opt_load_list,
254 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
255 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
256 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
257 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
258 'use-dns-faking' => \$opt_use_dns_faking
261 exit(1) if (not $result);
263 ShowHelp() if ($opt_help);
265 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
267 # we want unbuffered output
268 $| = 1;
270 my @tests = @ARGV;
272 # quick hack to disable rpc validation when using valgrind - its way too slow
273 unless (defined($ENV{VALGRIND})) {
274 $ENV{VALIDATE} = "validate";
275 $ENV{MALLOC_CHECK_} = 3;
278 # make all our python scripts unbuffered
279 $ENV{PYTHONUNBUFFERED} = 1;
281 my $bindir_abs = abs_path($bindir);
283 # Backwards compatibility:
284 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
285 if (defined($ENV{FEDORA_DS_ROOT})) {
286 $ldap = "fedora-ds";
287 } else {
288 $ldap = "openldap";
292 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
293 if ($ldap) {
294 # LDAP is slow
295 $torture_maxtime *= 2;
298 $prefix =~ s+//+/+;
299 $prefix =~ s+/./+/+;
300 $prefix =~ s+/$++;
302 die("using an empty prefix isn't allowed") unless $prefix ne "";
304 # Ensure we have the test prefix around.
306 # We need restrictive
307 # permissions on this as some subdirectories in this tree will have
308 # wider permissions (ie 0777) and this would allow other users on the
309 # host to subvert the test process.
310 umask 0077;
311 mkdir($prefix, 0700) unless -d $prefix;
312 chmod 0700, $prefix;
313 # We need to have no umask limitations for the tests.
314 umask 0000;
316 my $prefix_abs = abs_path($prefix);
317 my $tmpdir_abs = abs_path("$prefix/tmp");
318 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
320 my $srcdir_abs = abs_path($srcdir);
322 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
323 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
325 $ENV{PREFIX} = $prefix;
326 $ENV{PREFIX_ABS} = $prefix_abs;
327 $ENV{SRCDIR} = $srcdir;
328 $ENV{SRCDIR_ABS} = $srcdir_abs;
329 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
330 $ENV{BINDIR} = $bindir_abs;
332 my $tls_enabled = not $opt_quick;
333 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
335 sub prefix_pathvar($$)
337 my ($name, $newpath) = @_;
338 if (defined($ENV{$name})) {
339 $ENV{$name} = "$newpath:$ENV{$name}";
340 } else {
341 $ENV{$name} = $newpath;
344 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
345 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
347 if ($opt_socket_wrapper_keep_pcap) {
348 # Socket wrapper keep pcap implies socket wrapper pcap
349 $opt_socket_wrapper_pcap = 1;
352 if ($opt_socket_wrapper_pcap) {
353 # Socket wrapper pcap implies socket wrapper
354 $opt_socket_wrapper = 1;
357 my $ld_preload = $ENV{LD_PRELOAD};
359 if ($opt_libnss_wrapper_so_path) {
360 if ($ld_preload) {
361 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
362 } else {
363 $ld_preload = "$opt_libnss_wrapper_so_path";
367 if ($opt_libresolv_wrapper_so_path) {
368 if ($ld_preload) {
369 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
370 } else {
371 $ld_preload = "$opt_libresolv_wrapper_so_path";
375 if ($opt_libsocket_wrapper_so_path) {
376 if ($ld_preload) {
377 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
378 } else {
379 $ld_preload = "$opt_libsocket_wrapper_so_path";
383 if ($opt_libuid_wrapper_so_path) {
384 if ($ld_preload) {
385 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
386 } else {
387 $ld_preload = "$opt_libuid_wrapper_so_path";
391 $ENV{LD_PRELOAD} = $ld_preload;
392 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
394 # Enable uid_wrapper globally
395 $ENV{UID_WRAPPER} = 1;
397 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
399 # This is needed in order to allow the ldb_*ldap module
400 # to work with a preloaded socket wrapper.
401 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
403 my $socket_wrapper_dir;
404 if ($opt_socket_wrapper) {
405 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
406 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
407 } elsif (not $opt_list) {
408 unless ($< == 0) {
409 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
413 if ($opt_use_dns_faking) {
414 print "DNS: Faking nameserver\n";
415 $ENV{SAMBA_DNS_FAKING} = 1;
418 my $target;
419 my $testenv_default = "none";
421 # After this many seconds, the server will self-terminate. All tests
422 # must terminate in this time, and testenv will only stay alive this
423 # long
425 my $server_maxtime;
426 if ($opt_testenv) {
427 # 1 year should be enough :-)
428 $server_maxtime = 365 * 24 * 60 * 60;
429 } else {
430 # make test should run under 4 hours
431 $server_maxtime = 4 * 60 * 60;
434 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
435 $server_maxtime = $ENV{SMBD_MAXTIME};
438 unless ($opt_list) {
439 if ($opt_target eq "samba") {
440 $testenv_default = "ad_dc_ntvfs";
441 require target::Samba;
442 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
443 } elsif ($opt_target eq "samba3") {
444 $testenv_default = "nt4_member";
445 require target::Samba3;
446 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
450 sub read_test_regexes($)
452 my ($name) = @_;
453 my @ret = ();
454 open(LF, "<$name") or die("unable to read $name: $!");
455 while (<LF>) {
456 chomp;
457 next if (/^#/);
458 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
459 push (@ret, [$1, $4]);
460 } else {
461 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
462 push (@ret, [$_, undef]);
465 close(LF);
466 return @ret;
469 foreach (@opt_exclude) {
470 push (@excludes, read_test_regexes($_));
473 foreach (@opt_include) {
474 push (@includes, read_test_regexes($_));
477 my $interfaces = join(',', ("127.0.0.11/8",
478 "127.0.0.12/8",
479 "127.0.0.13/8",
480 "127.0.0.14/8",
481 "127.0.0.15/8",
482 "127.0.0.16/8"));
484 my $clientdir = "$prefix_abs/client";
486 my $conffile = "$clientdir/client.conf";
487 $ENV{SMB_CONF_PATH} = $conffile;
489 sub write_clientconf($$$)
491 my ($conffile, $clientdir, $vars) = @_;
493 mkdir("$clientdir", 0777) unless -d "$clientdir";
495 if ( -d "$clientdir/private" ) {
496 unlink <$clientdir/private/*>;
497 } else {
498 mkdir("$clientdir/private", 0777);
501 if ( -d "$clientdir/lockdir" ) {
502 unlink <$clientdir/lockdir/*>;
503 } else {
504 mkdir("$clientdir/lockdir", 0777);
507 if ( -d "$clientdir/statedir" ) {
508 unlink <$clientdir/statedir/*>;
509 } else {
510 mkdir("$clientdir/statedir", 0777);
513 if ( -d "$clientdir/cachedir" ) {
514 unlink <$clientdir/cachedir/*>;
515 } else {
516 mkdir("$clientdir/cachedir", 0777);
519 # this is ugly, but the ncalrpcdir needs exactly 0755
520 # otherwise tests fail.
521 my $mask = umask;
522 umask 0022;
523 if ( -d "$clientdir/ncalrpcdir/np" ) {
524 unlink <$clientdir/ncalrpcdir/np/*>;
525 rmdir "$clientdir/ncalrpcdir/np";
527 if ( -d "$clientdir/ncalrpcdir" ) {
528 unlink <$clientdir/ncalrpcdir/*>;
529 rmdir "$clientdir/ncalrpcdir";
531 mkdir("$clientdir/ncalrpcdir", 0755);
532 umask $mask;
534 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
535 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
536 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
537 my $ca_users_dir = "$cadir/Users";
539 if ( -d "$clientdir/pkinit" ) {
540 unlink <$clientdir/pkinit/*>;
541 } else {
542 mkdir("$clientdir/pkinit", 0700);
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 lock dir = $clientdir/lockdir
585 state directory = $clientdir/statedir
586 cache directory = $clientdir/cachedir
587 ncalrpc dir = $clientdir/ncalrpcdir
588 panic action = $RealBin/gdb_backtrace \%d
589 max xmit = 32K
590 notify:inotify = false
591 ldb:nosync = true
592 system:anonymous = true
593 client lanman auth = Yes
594 log level = 1
595 torture:basedir = $clientdir
596 #We don't want to pass our self-tests if the PAC code is wrong
597 gensec:require_pac = true
598 #We don't want to run 'speed' tests for very long
599 torture:timelimit = 1
600 winbind separator = /
601 tls cafile = ${cacert}
602 tls crlfile = ${cacrl_pem}
603 tls verify peer = no_check
604 include system krb5 conf = no
606 close(CF);
609 my @todo = ();
611 sub should_run_test($)
613 my $name = shift;
614 if ($#tests == -1) {
615 return 1;
617 for (my $i=0; $i <= $#tests; $i++) {
618 if ($name =~ /$tests[$i]/i) {
619 return 1;
622 return 0;
625 sub read_testlist($)
627 my ($filename) = @_;
629 my @ret = ();
630 open(IN, $filename) or die("Unable to open $filename: $!");
632 while (<IN>) {
633 if (/-- TEST(-LOADLIST|) --\n/) {
634 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
635 my $name = <IN>;
636 $name =~ s/\n//g;
637 my $env = <IN>;
638 $env =~ s/\n//g;
639 my $loadlist;
640 if ($supports_loadlist) {
641 $loadlist = <IN>;
642 $loadlist =~ s/\n//g;
644 my $cmdline = <IN>;
645 $cmdline =~ s/\n//g;
646 if (should_run_test($name) == 1) {
647 push (@ret, [$name, $env, $cmdline, $loadlist]);
649 } else {
650 print;
653 close(IN) or die("Error creating recipe from $filename");
654 return @ret;
657 if ($#testlists == -1) {
658 die("No testlists specified");
661 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
662 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
663 $ENV{TMPDIR} = "$tmpdir_abs";
664 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
665 if ($opt_socket_wrapper) {
666 $ENV{SELFTEST_INTERFACES} = $interfaces;
667 } else {
668 $ENV{SELFTEST_INTERFACES} = "";
670 if ($opt_quick) {
671 $ENV{SELFTEST_QUICK} = "1";
672 } else {
673 $ENV{SELFTEST_QUICK} = "";
675 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
677 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
678 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
680 my @available = ();
681 foreach my $fn (@testlists) {
682 foreach (read_testlist($fn)) {
683 my $name = $$_[0];
684 next if (@includes and not defined(find_in_list(\@includes, $name)));
685 push (@available, $_);
689 my $restricted = undef;
690 my $restricted_used = {};
692 if ($opt_load_list) {
693 $restricted = [];
694 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
695 while (<LOAD_LIST>) {
696 chomp;
697 push (@$restricted, $_);
699 close(LOAD_LIST);
702 my $individual_tests = undef;
703 $individual_tests = {};
705 foreach my $testsuite (@available) {
706 my $name = $$testsuite[0];
707 my $skipreason = skip($name);
708 if (defined($restricted)) {
709 # Find the testsuite for this test
710 my $match = undef;
711 foreach my $r (@$restricted) {
712 if ($r eq $name) {
713 $individual_tests->{$name} = [];
714 $match = $r;
715 $restricted_used->{$r} = 1;
716 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
717 push(@{$individual_tests->{$name}}, $r);
718 $match = $r;
719 $restricted_used->{$r} = 1;
722 if ($match) {
723 if (defined($skipreason)) {
724 if (not $opt_list) {
725 Subunit::skip_testsuite($name, $skipreason);
727 } else {
728 push(@todo, $testsuite);
731 } elsif (defined($skipreason)) {
732 if (not $opt_list) {
733 Subunit::skip_testsuite($name, $skipreason);
735 } else {
736 push(@todo, $testsuite);
740 if (defined($restricted)) {
741 foreach (@$restricted) {
742 unless (defined($restricted_used->{$_})) {
743 print "No test or testsuite found matching $_\n";
746 } elsif ($#todo == -1) {
747 print STDERR "No tests to run\n";
748 exit(1);
751 my $suitestotal = $#todo + 1;
753 unless ($opt_list) {
754 Subunit::progress($suitestotal);
755 Subunit::report_time(time());
758 my $i = 0;
759 $| = 1;
761 my %running_envs = ();
763 sub get_running_env($)
765 my ($name) = @_;
767 my $envname = $name;
769 $envname =~ s/:.*//;
771 return $running_envs{$envname};
774 my @exported_envvars = (
775 # domain stuff
776 "DOMAIN",
777 "REALM",
779 # stuff related to a trusted domain
780 "TRUST_SERVER",
781 "TRUST_SERVER_IP",
782 "TRUST_SERVER_IPV6",
783 "TRUST_NETBIOSNAME",
784 "TRUST_USERNAME",
785 "TRUST_PASSWORD",
786 "TRUST_DOMAIN",
787 "TRUST_REALM",
789 # domain controller stuff
790 "DC_SERVER",
791 "DC_SERVER_IP",
792 "DC_SERVER_IPV6",
793 "DC_NETBIOSNAME",
794 "DC_NETBIOSALIAS",
796 # domain member
797 "MEMBER_SERVER",
798 "MEMBER_SERVER_IP",
799 "MEMBER_SERVER_IPV6",
800 "MEMBER_NETBIOSNAME",
801 "MEMBER_NETBIOSALIAS",
803 # rpc proxy controller stuff
804 "RPC_PROXY_SERVER",
805 "RPC_PROXY_SERVER_IP",
806 "RPC_PROXY_SERVER_IPV6",
807 "RPC_PROXY_NETBIOSNAME",
808 "RPC_PROXY_NETBIOSALIAS",
810 # domain controller stuff for Vampired DC
811 "VAMPIRE_DC_SERVER",
812 "VAMPIRE_DC_SERVER_IP",
813 "VAMPIRE_DC_SERVER_IPV6",
814 "VAMPIRE_DC_NETBIOSNAME",
815 "VAMPIRE_DC_NETBIOSALIAS",
817 # domain controller stuff for FL 2000 Vampired DC
818 "VAMPIRE_2000_DC_SERVER",
819 "VAMPIRE_2000_DC_SERVER_IP",
820 "VAMPIRE_2000_DC_SERVER_IPV6",
821 "VAMPIRE_2000_DC_NETBIOSNAME",
822 "VAMPIRE_2000_DC_NETBIOSALIAS",
824 "PROMOTED_DC_SERVER",
825 "PROMOTED_DC_SERVER_IP",
826 "PROMOTED_DC_SERVER_IPV6",
827 "PROMOTED_DC_NETBIOSNAME",
828 "PROMOTED_DC_NETBIOSALIAS",
830 # server stuff
831 "SERVER",
832 "SERVER_IP",
833 "SERVER_IPV6",
834 "NETBIOSNAME",
835 "NETBIOSALIAS",
837 # user stuff
838 "USERNAME",
839 "USERID",
840 "PASSWORD",
841 "DC_USERNAME",
842 "DC_PASSWORD",
844 # UID/GID for rfc2307 mapping tests
845 "UID_RFC2307TEST",
846 "GID_RFC2307TEST",
848 # misc stuff
849 "KRB5_CONFIG",
850 "KRB5CCNAME",
851 "SELFTEST_WINBINDD_SOCKET_DIR",
852 "NMBD_SOCKET_DIR",
853 "LOCAL_PATH",
854 "DNS_FORWARDER1",
855 "DNS_FORWARDER2",
856 "RESOLV_CONF",
857 "UNACCEPTABLE_PASSWORD",
859 # nss_wrapper
860 "NSS_WRAPPER_PASSWD",
861 "NSS_WRAPPER_GROUP",
862 "NSS_WRAPPER_HOSTS",
863 "NSS_WRAPPER_MODULE_SO_PATH",
864 "NSS_WRAPPER_MODULE_FN_PREFIX",
866 # resolv_wrapper
867 "RESOLV_WRAPPER_CONF",
868 "RESOLV_WRAPPER_HOSTS",
871 sub sighandler($)
873 my $signame = shift;
875 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
876 $SIG{PIPE} = 'IGNORE';
878 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
880 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
881 teardown_env($_) foreach(keys %running_envs);
882 system("pstree -p $$");
883 print "$0: PID[$$]: Exiting...\n";
884 exit(1);
887 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
889 sub setup_env($$)
891 my ($name, $prefix) = @_;
893 my $testenv_vars = undef;
895 my $envname = $name;
896 my $option = $name;
898 $envname =~ s/:.*//;
899 $option =~ s/^[^:]*//;
900 $option =~ s/^://;
902 $option = "client" if $option eq "";
904 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
906 if (defined(get_running_env($envname))) {
907 $testenv_vars = get_running_env($envname);
908 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
909 print $testenv_vars->{target}->getlog_env($testenv_vars);
910 $testenv_vars = undef;
912 } else {
913 $testenv_vars = $target->setup_env($envname, $prefix);
914 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
915 return $testenv_vars;
916 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
917 $testenv_vars->{target} = $target;
919 if (not defined($testenv_vars)) {
920 warn("$opt_target can't start up known environment '$envname'");
924 return undef unless defined($testenv_vars);
926 $running_envs{$envname} = $testenv_vars;
928 if ($option eq "local") {
929 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
930 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
931 } elsif ($option eq "client") {
932 SocketWrapper::set_default_iface(11);
933 write_clientconf($conffile, $clientdir, $testenv_vars);
934 $ENV{SMB_CONF_PATH} = $conffile;
935 } else {
936 die("Unknown option[$option] for envname[$envname]");
939 foreach (@exported_envvars) {
940 if (defined($testenv_vars->{$_})) {
941 $ENV{$_} = $testenv_vars->{$_};
942 } else {
943 delete $ENV{$_};
947 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
948 unlink($krb5_ccache_path);
949 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
950 return $testenv_vars;
953 sub exported_envvars_str($)
955 my ($testenv_vars) = @_;
956 my $out = "";
958 foreach (@exported_envvars) {
959 next unless defined($testenv_vars->{$_});
960 $out .= $_."=".$testenv_vars->{$_}."\n";
963 return $out;
966 sub getlog_env($)
968 my ($envname) = @_;
969 return "" if ($envname eq "none");
970 my $env = get_running_env($envname);
971 return $env->{target}->getlog_env($env);
974 sub check_env($)
976 my ($envname) = @_;
977 my $env = get_running_env($envname);
978 return $env->{target}->check_env($env);
981 sub teardown_env($)
983 my ($envname) = @_;
984 return if ($envname eq "none");
985 print STDERR "teardown_env($envname)\n";
986 my $env = get_running_env($envname);
987 $env->{target}->teardown_env($env);
988 delete $running_envs{$envname};
991 # This 'global' file needs to be empty when we start
992 unlink("$prefix_abs/dns_host_file");
993 unlink("$prefix_abs/hosts");
995 if ($opt_random_order) {
996 require List::Util;
997 my @newtodo = List::Util::shuffle(@todo);
998 @todo = @newtodo;
1001 if ($opt_testenv) {
1002 my $testenv_name = $ENV{SELFTEST_TESTENV};
1003 $testenv_name = $testenv_default unless defined($testenv_name);
1005 my $testenv_vars = setup_env($testenv_name, $prefix);
1007 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1008 die("Unable to setup environment $testenv_name");
1011 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1012 $ENV{ENVNAME} = $testenv_name;
1014 my $envvarstr = exported_envvars_str($testenv_vars);
1016 my @term_args = ("echo -e \"
1017 Welcome to the Samba4 Test environment '$testenv_name'
1019 This matches the client environment used in make test
1020 server is pid `cat \$PIDDIR/samba.pid`
1022 Some useful environment variables:
1023 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1024 SMB_CONF_PATH=\$SMB_CONF_PATH
1026 $envvarstr
1027 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1028 my @term = ();
1029 if ($ENV{TERMINAL}) {
1030 @term = ($ENV{TERMINAL});
1031 } else {
1032 @term = ("xterm", "-e");
1033 unshift(@term_args, ("bash", "-c"));
1036 system(@term, @term_args);
1038 teardown_env($testenv_name);
1039 } elsif ($opt_list) {
1040 foreach (@todo) {
1041 my $name = $$_[0];
1042 my $envname = $$_[1];
1043 my $cmd = $$_[2];
1044 my $listcmd = $$_[3];
1046 unless (defined($listcmd)) {
1047 warn("Unable to list tests in $name");
1048 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1049 # a single "test".
1050 print "$name\n";
1051 next;
1054 system($listcmd);
1056 if ($? == -1) {
1057 die("Unable to run $listcmd: $!");
1058 } elsif ($? & 127) {
1059 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1062 my $exitcode = $? >> 8;
1063 if ($exitcode != 0) {
1064 die("$cmd exited with exit code $exitcode");
1067 } else {
1068 foreach (@todo) {
1069 $i++;
1070 my $cmd = $$_[2];
1071 my $name = $$_[0];
1072 my $envname = $$_[1];
1074 my $envvars = setup_env($envname, $prefix);
1075 if (not defined($envvars)) {
1076 Subunit::start_testsuite($name);
1077 Subunit::end_testsuite($name, "error",
1078 "unable to set up environment $envname - exiting");
1079 next;
1080 } elsif ($envvars eq "UNKNOWN") {
1081 Subunit::start_testsuite($name);
1082 Subunit::end_testsuite($name, "skip",
1083 "environment $envname is unknown in this test backend - skipping");
1084 next;
1087 # Generate a file with the individual tests to run, if the
1088 # test runner for this test suite supports it.
1089 if ($individual_tests and $individual_tests->{$name}) {
1090 if ($$_[3]) {
1091 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1092 foreach my $test (@{$individual_tests->{$name}}) {
1093 print $fh substr($test, length($name)+1) . "\n";
1095 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1096 } else {
1097 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1101 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1103 teardown_env($envname) if ($opt_resetup_env);
1107 print "\n";
1109 teardown_env($_) foreach (keys %running_envs);
1111 my $failed = 0;
1113 # if there were any valgrind failures, show them
1114 foreach (<$prefix/valgrind.log*>) {
1115 next unless (-s $_);
1116 print "VALGRIND FAILURE\n";
1117 $failed++;
1118 system("cat $_");
1120 exit 0;