s4:librpc: ask for GENSEC_FEATURE_SIGN_PKT_HEADER after the gensec_update() dance
[Samba.git] / selftest / selftest.pl
blobf05fc5cde494657cb7c8fcf963849b91b31186e8
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 $opt_mitkrb5 = 0;
53 my $ldap = undef;
54 my $opt_resetup_env = undef;
55 my $opt_load_list = undef;
56 my $opt_libnss_wrapper_so_path = "";
57 my $opt_libresolv_wrapper_so_path = "";
58 my $opt_libsocket_wrapper_so_path = "";
59 my $opt_libuid_wrapper_so_path = "";
60 my $opt_use_dns_faking = 0;
61 my @testlists = ();
63 my $srcdir = ".";
64 my $bindir = "./bin";
65 my $prefix = "./st";
67 my @includes = ();
68 my @excludes = ();
70 sub find_in_list($$)
72 my ($list, $fullname) = @_;
74 foreach (@$list) {
75 if ($fullname =~ /$$_[0]/) {
76 return ($$_[1]) if ($$_[1]);
77 return "";
81 return undef;
84 sub skip($)
86 my ($name) = @_;
88 return find_in_list(\@excludes, $name);
91 sub getlog_env($);
93 sub setup_pcap($)
95 my ($name) = @_;
97 return unless ($opt_socket_wrapper_pcap);
98 return unless defined($ENV{SOCKET_WRAPPER_PCAP_DIR});
100 my $fname = $name;
101 $fname =~ s%[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\-]%_%g;
103 my $pcap_file = "$ENV{SOCKET_WRAPPER_PCAP_DIR}/$fname.pcap";
105 SocketWrapper::setup_pcap($pcap_file);
107 return $pcap_file;
110 sub cleanup_pcap($$)
112 my ($pcap_file, $exitcode) = @_;
114 return unless ($opt_socket_wrapper_pcap);
115 return if ($opt_socket_wrapper_keep_pcap);
116 return unless ($exitcode == 0);
117 return unless defined($pcap_file);
119 unlink($pcap_file);
122 # expand strings from %ENV
123 sub expand_environment_strings($)
125 my $s = shift;
126 # we use a reverse sort so we do the longer ones first
127 foreach my $k (sort { $b cmp $a } keys %ENV) {
128 $s =~ s/\$$k/$ENV{$k}/g;
130 return $s;
133 sub run_testsuite($$$$$)
135 my ($envname, $name, $cmd, $i, $totalsuites) = @_;
136 my $pcap_file = setup_pcap($name);
138 Subunit::start_testsuite($name);
139 Subunit::progress_push();
140 Subunit::report_time(time());
141 system($cmd);
142 Subunit::report_time(time());
143 Subunit::progress_pop();
145 if ($? == -1) {
146 print "command: $cmd\n";
147 printf "expanded command: %s\n", expand_environment_strings($cmd);
148 Subunit::end_testsuite($name, "error", "Unable to run $cmd: $!");
149 exit(1);
150 } elsif ($? & 127) {
151 print "command: $cmd\n";
152 printf "expanded command: %s\n", expand_environment_strings($cmd);
153 Subunit::end_testsuite($name, "error",
154 sprintf("%s died with signal %d, %s coredump\n", $cmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
155 exit(1);
158 my $exitcode = $? >> 8;
160 my $envlog = getlog_env($envname);
161 if ($envlog ne "") {
162 print "envlog: $envlog\n";
165 print "command: $cmd\n";
166 printf "expanded command: %s\n", expand_environment_strings($cmd);
168 if ($exitcode == 0) {
169 Subunit::end_testsuite($name, "success");
170 } else {
171 Subunit::end_testsuite($name, "failure", "Exit code was $exitcode");
174 cleanup_pcap($pcap_file, $exitcode);
176 if (not $opt_socket_wrapper_keep_pcap and defined($pcap_file)) {
177 print "PCAP FILE: $pcap_file\n";
180 if ($exitcode != 0) {
181 exit(1) if ($opt_one);
184 return $exitcode;
187 sub ShowHelp()
189 print "Samba test runner
190 Copyright (C) Jelmer Vernooij <jelmer\@samba.org>
191 Copyright (C) Stefan Metzmacher <metze\@samba.org>
193 Usage: $Script [OPTIONS] TESTNAME-REGEX [TESTNAME-REGEX...]
195 Generic options:
196 --help this help page
197 --target=samba[3]|win Samba version to target
198 --testlist=FILE file to read available tests from
199 --exclude=FILE Exclude tests listed in the file
200 --include=FILE Include tests listed in the file
202 Paths:
203 --prefix=DIR prefix to run tests in [st]
204 --srcdir=DIR source directory [.]
205 --bindir=DIR binaries directory [./bin]
207 Preload cwrap:
208 --nss_wrapper_so_path=FILE the nss_wrapper library to preload
209 --resolv_wrapper_so_path=FILE the resolv_wrapper library to preload
210 --socket_wrapper_so_path=FILE the socket_wrapper library to preload
211 --uid_wrapper_so_path=FILE the uid_wrapper library to preload
213 DNS:
214 --use-dns-faking Fake DNS entries rather than talking to our
215 DNS implementation.
217 Target Specific:
218 --socket-wrapper-pcap save traffic to pcap directories
219 --socket-wrapper-keep-pcap keep all pcap files, not just those for tests that
220 failed
221 --socket-wrapper enable socket wrapper
223 Samba4 Specific:
224 --ldap=openldap|fedora-ds back samba onto specified ldap server
226 Behaviour:
227 --quick run quick overall test
228 --one abort when the first test fails
229 --testenv run a shell in the requested test environment
230 --list list available tests
232 exit(0);
235 my $result = GetOptions (
236 'help|h|?' => \$opt_help,
237 'target=s' => \$opt_target,
238 'prefix=s' => \$prefix,
239 'socket-wrapper' => \$opt_socket_wrapper,
240 'socket-wrapper-pcap' => \$opt_socket_wrapper_pcap,
241 'socket-wrapper-keep-pcap' => \$opt_socket_wrapper_keep_pcap,
242 'quick' => \$opt_quick,
243 'one' => \$opt_one,
244 'exclude=s' => \@opt_exclude,
245 'include=s' => \@opt_include,
246 'srcdir=s' => \$srcdir,
247 'bindir=s' => \$bindir,
248 'testenv' => \$opt_testenv,
249 'list' => \$opt_list,
250 'mitkrb5' => \$opt_mitkrb5,
251 'ldap:s' => \$ldap,
252 'resetup-environment' => \$opt_resetup_env,
253 'testlist=s' => \@testlists,
254 'random-order' => \$opt_random_order,
255 'load-list=s' => \$opt_load_list,
256 'nss_wrapper_so_path=s' => \$opt_libnss_wrapper_so_path,
257 'resolv_wrapper_so_path=s' => \$opt_libresolv_wrapper_so_path,
258 'socket_wrapper_so_path=s' => \$opt_libsocket_wrapper_so_path,
259 'uid_wrapper_so_path=s' => \$opt_libuid_wrapper_so_path,
260 'use-dns-faking' => \$opt_use_dns_faking
263 exit(1) if (not $result);
265 ShowHelp() if ($opt_help);
267 die("--list and --testenv are mutually exclusive") if ($opt_list and $opt_testenv);
269 # we want unbuffered output
270 $| = 1;
272 my @tests = @ARGV;
274 # quick hack to disable rpc validation when using valgrind - its way too slow
275 unless (defined($ENV{VALGRIND})) {
276 $ENV{VALIDATE} = "validate";
277 $ENV{MALLOC_CHECK_} = 3;
280 # make all our python scripts unbuffered
281 $ENV{PYTHONUNBUFFERED} = 1;
283 my $bindir_abs = abs_path($bindir);
285 # Backwards compatibility:
286 if (defined($ENV{TEST_LDAP}) and $ENV{TEST_LDAP} eq "yes") {
287 if (defined($ENV{FEDORA_DS_ROOT})) {
288 $ldap = "fedora-ds";
289 } else {
290 $ldap = "openldap";
294 my $torture_maxtime = ($ENV{TORTURE_MAXTIME} or 1200);
295 if ($ldap) {
296 # LDAP is slow
297 $torture_maxtime *= 2;
300 $prefix =~ s+//+/+;
301 $prefix =~ s+/./+/+;
302 $prefix =~ s+/$++;
304 die("using an empty prefix isn't allowed") unless $prefix ne "";
306 # Ensure we have the test prefix around.
308 # We need restrictive
309 # permissions on this as some subdirectories in this tree will have
310 # wider permissions (ie 0777) and this would allow other users on the
311 # host to subvert the test process.
312 umask 0077;
313 mkdir($prefix, 0700) unless -d $prefix;
314 chmod 0700, $prefix;
315 # We need to have no umask limitations for the tests.
316 umask 0000;
318 my $prefix_abs = abs_path($prefix);
319 my $tmpdir_abs = abs_path("$prefix/tmp");
320 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
322 my $srcdir_abs = abs_path($srcdir);
324 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
325 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
327 $ENV{SAMBA_SELFTEST} = "1";
329 $ENV{PREFIX} = $prefix;
330 $ENV{PREFIX_ABS} = $prefix_abs;
331 $ENV{SRCDIR} = $srcdir;
332 $ENV{SRCDIR_ABS} = $srcdir_abs;
333 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
334 $ENV{BINDIR} = $bindir_abs;
336 my $tls_enabled = not $opt_quick;
337 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
339 sub prefix_pathvar($$)
341 my ($name, $newpath) = @_;
342 if (defined($ENV{$name})) {
343 $ENV{$name} = "$newpath:$ENV{$name}";
344 } else {
345 $ENV{$name} = $newpath;
348 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
349 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
351 if ($opt_socket_wrapper_keep_pcap) {
352 # Socket wrapper keep pcap implies socket wrapper pcap
353 $opt_socket_wrapper_pcap = 1;
356 if ($opt_socket_wrapper_pcap) {
357 # Socket wrapper pcap implies socket wrapper
358 $opt_socket_wrapper = 1;
361 my $ld_preload = $ENV{LD_PRELOAD};
363 if ($opt_libnss_wrapper_so_path) {
364 if ($ld_preload) {
365 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
366 } else {
367 $ld_preload = "$opt_libnss_wrapper_so_path";
371 if ($opt_libresolv_wrapper_so_path) {
372 if ($ld_preload) {
373 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
374 } else {
375 $ld_preload = "$opt_libresolv_wrapper_so_path";
379 if ($opt_libsocket_wrapper_so_path) {
380 if ($ld_preload) {
381 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
382 } else {
383 $ld_preload = "$opt_libsocket_wrapper_so_path";
387 if ($opt_libuid_wrapper_so_path) {
388 if ($ld_preload) {
389 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
390 } else {
391 $ld_preload = "$opt_libuid_wrapper_so_path";
395 $ENV{LD_PRELOAD} = $ld_preload;
396 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
398 # Enable uid_wrapper globally
399 $ENV{UID_WRAPPER} = 1;
401 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
403 # This is needed in order to allow the ldb_*ldap module
404 # to work with a preloaded socket wrapper.
405 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
407 my $socket_wrapper_dir;
408 if ($opt_socket_wrapper) {
409 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
410 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
411 } elsif (not $opt_list) {
412 unless ($< == 0) {
413 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
417 if ($opt_use_dns_faking) {
418 print "DNS: Faking nameserver\n";
419 $ENV{SAMBA_DNS_FAKING} = 1;
422 my $target;
423 my $testenv_default = "none";
425 if ($opt_mitkrb5 == 1) {
426 $ENV{MITKRB5} = $opt_mitkrb5;
429 # After this many seconds, the server will self-terminate. All tests
430 # must terminate in this time, and testenv will only stay alive this
431 # long
433 my $server_maxtime;
434 if ($opt_testenv) {
435 # 1 year should be enough :-)
436 $server_maxtime = 365 * 24 * 60 * 60;
437 } else {
438 # make test should run under 4 hours
439 $server_maxtime = 4 * 60 * 60;
442 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
443 $server_maxtime = $ENV{SMBD_MAXTIME};
446 unless ($opt_list) {
447 if ($opt_target eq "samba") {
448 $testenv_default = "ad_dc_ntvfs";
449 require target::Samba;
450 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
451 } elsif ($opt_target eq "samba3") {
452 $testenv_default = "nt4_member";
453 require target::Samba3;
454 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
458 sub read_test_regexes($)
460 my ($name) = @_;
461 my @ret = ();
462 open(LF, "<$name") or die("unable to read $name: $!");
463 while (<LF>) {
464 chomp;
465 next if (/^#/);
466 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
467 push (@ret, [$1, $4]);
468 } else {
469 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
470 push (@ret, [$_, undef]);
473 close(LF);
474 return @ret;
477 foreach (@opt_exclude) {
478 push (@excludes, read_test_regexes($_));
481 foreach (@opt_include) {
482 push (@includes, read_test_regexes($_));
485 my $interfaces = join(',', ("127.0.0.11/8",
486 "127.0.0.12/8",
487 "127.0.0.13/8",
488 "127.0.0.14/8",
489 "127.0.0.15/8",
490 "127.0.0.16/8"));
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/lockdir" ) {
510 unlink <$clientdir/lockdir/*>;
511 } else {
512 mkdir("$clientdir/lockdir", 0777);
515 if ( -d "$clientdir/statedir" ) {
516 unlink <$clientdir/statedir/*>;
517 } else {
518 mkdir("$clientdir/statedir", 0777);
521 if ( -d "$clientdir/cachedir" ) {
522 unlink <$clientdir/cachedir/*>;
523 } else {
524 mkdir("$clientdir/cachedir", 0777);
527 # this is ugly, but the ncalrpcdir needs exactly 0755
528 # otherwise tests fail.
529 my $mask = umask;
530 umask 0022;
531 if ( -d "$clientdir/ncalrpcdir/np" ) {
532 unlink <$clientdir/ncalrpcdir/np/*>;
533 rmdir "$clientdir/ncalrpcdir/np";
535 if ( -d "$clientdir/ncalrpcdir" ) {
536 unlink <$clientdir/ncalrpcdir/*>;
537 rmdir "$clientdir/ncalrpcdir";
539 mkdir("$clientdir/ncalrpcdir", 0755);
540 umask $mask;
542 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
543 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
544 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
545 my $ca_users_dir = "$cadir/Users";
547 if ( -d "$clientdir/pkinit" ) {
548 unlink <$clientdir/pkinit/*>;
549 } else {
550 mkdir("$clientdir/pkinit", 0700);
553 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
554 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
555 # We make a copy here and make the certificated easily
556 # accessable in the client environment.
557 my $mask = umask;
558 umask 0077;
559 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
560 for my $d (readdir USERS) {
561 my $user_dir = "${ca_users_dir}/${d}";
562 next if ${d} =~ /^\./;
563 next if (! -d "${user_dir}");
564 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
565 for my $l (readdir USER) {
566 my $user_link = "${user_dir}/${l}";
567 next if ${l} =~ /^\./;
568 next if (! -l "${user_link}");
570 my $dest = "${clientdir}/pkinit/${l}";
571 Samba::copy_file_content(${user_link}, ${dest});
573 closedir USER;
575 closedir USERS;
576 umask $mask;
578 open(CF, ">$conffile");
579 print CF "[global]\n";
580 print CF "\tnetbios name = client\n";
581 if (defined($vars->{DOMAIN})) {
582 print CF "\tworkgroup = $vars->{DOMAIN}\n";
584 if (defined($vars->{REALM})) {
585 print CF "\trealm = $vars->{REALM}\n";
587 if ($opt_socket_wrapper) {
588 print CF "\tinterfaces = $interfaces\n";
590 print CF "
591 private dir = $clientdir/private
592 lock dir = $clientdir/lockdir
593 state directory = $clientdir/statedir
594 cache directory = $clientdir/cachedir
595 ncalrpc dir = $clientdir/ncalrpcdir
596 panic action = $RealBin/gdb_backtrace \%d
597 max xmit = 32K
598 notify:inotify = false
599 ldb:nosync = true
600 system:anonymous = true
601 client lanman auth = Yes
602 log level = 1
603 torture:basedir = $clientdir
604 #We don't want to pass our self-tests if the PAC code is wrong
605 gensec:require_pac = true
606 #We don't want to run 'speed' tests for very long
607 torture:timelimit = 1
608 winbind separator = /
609 tls cafile = ${cacert}
610 tls crlfile = ${cacrl_pem}
611 tls verify peer = no_check
612 include system krb5 conf = no
614 close(CF);
617 my @todo = ();
619 sub should_run_test($)
621 my $name = shift;
622 if ($#tests == -1) {
623 return 1;
625 for (my $i=0; $i <= $#tests; $i++) {
626 if ($name =~ /$tests[$i]/i) {
627 return 1;
630 return 0;
633 sub read_testlist($)
635 my ($filename) = @_;
637 my @ret = ();
638 open(IN, $filename) or die("Unable to open $filename: $!");
640 while (<IN>) {
641 if (/-- TEST(-LOADLIST|) --\n/) {
642 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
643 my $name = <IN>;
644 $name =~ s/\n//g;
645 my $env = <IN>;
646 $env =~ s/\n//g;
647 my $loadlist;
648 if ($supports_loadlist) {
649 $loadlist = <IN>;
650 $loadlist =~ s/\n//g;
652 my $cmdline = <IN>;
653 $cmdline =~ s/\n//g;
654 if (should_run_test($name) == 1) {
655 push (@ret, [$name, $env, $cmdline, $loadlist]);
657 } else {
658 print;
661 close(IN) or die("Error creating recipe from $filename");
662 return @ret;
665 if ($#testlists == -1) {
666 die("No testlists specified");
669 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
670 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
671 $ENV{TMPDIR} = "$tmpdir_abs";
672 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
673 if ($opt_socket_wrapper) {
674 $ENV{SELFTEST_INTERFACES} = $interfaces;
675 } else {
676 $ENV{SELFTEST_INTERFACES} = "";
678 if ($opt_quick) {
679 $ENV{SELFTEST_QUICK} = "1";
680 } else {
681 $ENV{SELFTEST_QUICK} = "";
683 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
685 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
686 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
688 my @available = ();
689 foreach my $fn (@testlists) {
690 foreach (read_testlist($fn)) {
691 my $name = $$_[0];
692 next if (@includes and not defined(find_in_list(\@includes, $name)));
693 push (@available, $_);
697 my $restricted = undef;
698 my $restricted_used = {};
700 if ($opt_load_list) {
701 $restricted = [];
702 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
703 while (<LOAD_LIST>) {
704 chomp;
705 push (@$restricted, $_);
707 close(LOAD_LIST);
710 my $individual_tests = undef;
711 $individual_tests = {};
713 foreach my $testsuite (@available) {
714 my $name = $$testsuite[0];
715 my $skipreason = skip($name);
716 if (defined($restricted)) {
717 # Find the testsuite for this test
718 my $match = undef;
719 foreach my $r (@$restricted) {
720 if ($r eq $name) {
721 $individual_tests->{$name} = [];
722 $match = $r;
723 $restricted_used->{$r} = 1;
724 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
725 push(@{$individual_tests->{$name}}, $r);
726 $match = $r;
727 $restricted_used->{$r} = 1;
730 if ($match) {
731 if (defined($skipreason)) {
732 if (not $opt_list) {
733 Subunit::skip_testsuite($name, $skipreason);
735 } else {
736 push(@todo, $testsuite);
739 } elsif (defined($skipreason)) {
740 if (not $opt_list) {
741 Subunit::skip_testsuite($name, $skipreason);
743 } else {
744 push(@todo, $testsuite);
748 if (defined($restricted)) {
749 foreach (@$restricted) {
750 unless (defined($restricted_used->{$_})) {
751 print "No test or testsuite found matching $_\n";
754 } elsif ($#todo == -1) {
755 print STDERR "No tests to run\n";
756 exit(1);
759 my $suitestotal = $#todo + 1;
761 unless ($opt_list) {
762 Subunit::progress($suitestotal);
763 Subunit::report_time(time());
766 my $i = 0;
767 $| = 1;
769 my %running_envs = ();
771 sub get_running_env($)
773 my ($name) = @_;
775 my $envname = $name;
777 $envname =~ s/:.*//;
779 return $running_envs{$envname};
782 my @exported_envvars = (
783 # domain stuff
784 "DOMAIN",
785 "REALM",
787 # stuff related to a trusted domain
788 "TRUST_SERVER",
789 "TRUST_SERVER_IP",
790 "TRUST_SERVER_IPV6",
791 "TRUST_NETBIOSNAME",
792 "TRUST_USERNAME",
793 "TRUST_PASSWORD",
794 "TRUST_DOMAIN",
795 "TRUST_REALM",
797 # domain controller stuff
798 "DC_SERVER",
799 "DC_SERVER_IP",
800 "DC_SERVER_IPV6",
801 "DC_NETBIOSNAME",
802 "DC_NETBIOSALIAS",
804 # domain member
805 "MEMBER_SERVER",
806 "MEMBER_SERVER_IP",
807 "MEMBER_SERVER_IPV6",
808 "MEMBER_NETBIOSNAME",
809 "MEMBER_NETBIOSALIAS",
811 # rpc proxy controller stuff
812 "RPC_PROXY_SERVER",
813 "RPC_PROXY_SERVER_IP",
814 "RPC_PROXY_SERVER_IPV6",
815 "RPC_PROXY_NETBIOSNAME",
816 "RPC_PROXY_NETBIOSALIAS",
818 # domain controller stuff for Vampired DC
819 "VAMPIRE_DC_SERVER",
820 "VAMPIRE_DC_SERVER_IP",
821 "VAMPIRE_DC_SERVER_IPV6",
822 "VAMPIRE_DC_NETBIOSNAME",
823 "VAMPIRE_DC_NETBIOSALIAS",
825 # domain controller stuff for FL 2000 Vampired DC
826 "VAMPIRE_2000_DC_SERVER",
827 "VAMPIRE_2000_DC_SERVER_IP",
828 "VAMPIRE_2000_DC_SERVER_IPV6",
829 "VAMPIRE_2000_DC_NETBIOSNAME",
830 "VAMPIRE_2000_DC_NETBIOSALIAS",
832 "PROMOTED_DC_SERVER",
833 "PROMOTED_DC_SERVER_IP",
834 "PROMOTED_DC_SERVER_IPV6",
835 "PROMOTED_DC_NETBIOSNAME",
836 "PROMOTED_DC_NETBIOSALIAS",
838 # server stuff
839 "SERVER",
840 "SERVER_IP",
841 "SERVER_IPV6",
842 "NETBIOSNAME",
843 "NETBIOSALIAS",
845 # user stuff
846 "USERNAME",
847 "USERID",
848 "PASSWORD",
849 "DC_USERNAME",
850 "DC_PASSWORD",
852 # UID/GID for rfc2307 mapping tests
853 "UID_RFC2307TEST",
854 "GID_RFC2307TEST",
856 # misc stuff
857 "KRB5_CONFIG",
858 "KRB5CCNAME",
859 "SELFTEST_WINBINDD_SOCKET_DIR",
860 "NMBD_SOCKET_DIR",
861 "LOCAL_PATH",
862 "DNS_FORWARDER1",
863 "DNS_FORWARDER2",
864 "RESOLV_CONF",
865 "UNACCEPTABLE_PASSWORD",
866 "LOCK_DIR",
868 # nss_wrapper
869 "NSS_WRAPPER_PASSWD",
870 "NSS_WRAPPER_GROUP",
871 "NSS_WRAPPER_HOSTS",
872 "NSS_WRAPPER_MODULE_SO_PATH",
873 "NSS_WRAPPER_MODULE_FN_PREFIX",
875 # resolv_wrapper
876 "RESOLV_WRAPPER_CONF",
877 "RESOLV_WRAPPER_HOSTS",
880 sub sighandler($)
882 my $signame = shift;
884 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
885 $SIG{PIPE} = 'IGNORE';
887 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
889 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
890 teardown_env($_) foreach(keys %running_envs);
891 system("pstree -p $$");
892 print "$0: PID[$$]: Exiting...\n";
893 exit(1);
896 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
898 sub setup_env($$)
900 my ($name, $prefix) = @_;
902 my $testenv_vars = undef;
904 my $envname = $name;
905 my $option = $name;
907 $envname =~ s/:.*//;
908 $option =~ s/^[^:]*//;
909 $option =~ s/^://;
911 $option = "client" if $option eq "";
913 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
915 if (defined(get_running_env($envname))) {
916 $testenv_vars = get_running_env($envname);
917 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
918 print $testenv_vars->{target}->getlog_env($testenv_vars);
919 $testenv_vars = undef;
921 } else {
922 $testenv_vars = $target->setup_env($envname, $prefix);
923 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
924 return $testenv_vars;
925 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
926 $testenv_vars->{target} = $target;
928 if (not defined($testenv_vars)) {
929 warn("$opt_target can't start up known environment '$envname'");
933 return undef unless defined($testenv_vars);
935 $running_envs{$envname} = $testenv_vars;
937 if ($option eq "local") {
938 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
939 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
940 } elsif ($option eq "client") {
941 SocketWrapper::set_default_iface(11);
942 write_clientconf($conffile, $clientdir, $testenv_vars);
943 $ENV{SMB_CONF_PATH} = $conffile;
944 } else {
945 die("Unknown option[$option] for envname[$envname]");
948 foreach (@exported_envvars) {
949 if (defined($testenv_vars->{$_})) {
950 $ENV{$_} = $testenv_vars->{$_};
951 } else {
952 delete $ENV{$_};
956 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
957 unlink($krb5_ccache_path);
958 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
959 return $testenv_vars;
962 sub exported_envvars_str($)
964 my ($testenv_vars) = @_;
965 my $out = "";
967 foreach (@exported_envvars) {
968 next unless defined($testenv_vars->{$_});
969 $out .= $_."=".$testenv_vars->{$_}."\n";
972 return $out;
975 sub getlog_env($)
977 my ($envname) = @_;
978 return "" if ($envname eq "none");
979 my $env = get_running_env($envname);
980 return $env->{target}->getlog_env($env);
983 sub check_env($)
985 my ($envname) = @_;
986 my $env = get_running_env($envname);
987 return $env->{target}->check_env($env);
990 sub teardown_env($)
992 my ($envname) = @_;
993 return if ($envname eq "none");
994 print STDERR "teardown_env($envname)\n";
995 my $env = get_running_env($envname);
996 $env->{target}->teardown_env($env);
997 delete $running_envs{$envname};
1000 # This 'global' file needs to be empty when we start
1001 unlink("$prefix_abs/dns_host_file");
1002 unlink("$prefix_abs/hosts");
1004 if ($opt_random_order) {
1005 require List::Util;
1006 my @newtodo = List::Util::shuffle(@todo);
1007 @todo = @newtodo;
1010 if ($opt_testenv) {
1011 my $testenv_name = $ENV{SELFTEST_TESTENV};
1012 $testenv_name = $testenv_default unless defined($testenv_name);
1014 my $testenv_vars = setup_env($testenv_name, $prefix);
1016 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1017 die("Unable to setup environment $testenv_name");
1020 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1021 $ENV{ENVNAME} = $testenv_name;
1023 my $envvarstr = exported_envvars_str($testenv_vars);
1025 my @term_args = ("echo -e \"
1026 Welcome to the Samba4 Test environment '$testenv_name'
1028 This matches the client environment used in make test
1029 server is pid `cat \$PIDDIR/samba.pid`
1031 Some useful environment variables:
1032 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1033 SMB_CONF_PATH=\$SMB_CONF_PATH
1035 $envvarstr
1036 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1037 my @term = ();
1038 if ($ENV{TERMINAL}) {
1039 @term = ($ENV{TERMINAL});
1040 } else {
1041 @term = ("xterm", "-e");
1042 unshift(@term_args, ("bash", "-c"));
1045 system(@term, @term_args);
1047 teardown_env($testenv_name);
1048 } elsif ($opt_list) {
1049 foreach (@todo) {
1050 my $name = $$_[0];
1051 my $envname = $$_[1];
1052 my $cmd = $$_[2];
1053 my $listcmd = $$_[3];
1055 unless (defined($listcmd)) {
1056 warn("Unable to list tests in $name");
1057 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1058 # a single "test".
1059 print "$name\n";
1060 next;
1063 system($listcmd);
1065 if ($? == -1) {
1066 die("Unable to run $listcmd: $!");
1067 } elsif ($? & 127) {
1068 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1071 my $exitcode = $? >> 8;
1072 if ($exitcode != 0) {
1073 die("$cmd exited with exit code $exitcode");
1076 } else {
1077 foreach (@todo) {
1078 $i++;
1079 my $cmd = $$_[2];
1080 my $name = $$_[0];
1081 my $envname = $$_[1];
1083 my $envvars = setup_env($envname, $prefix);
1084 if (not defined($envvars)) {
1085 Subunit::start_testsuite($name);
1086 Subunit::end_testsuite($name, "error",
1087 "unable to set up environment $envname - exiting");
1088 next;
1089 } elsif ($envvars eq "UNKNOWN") {
1090 Subunit::start_testsuite($name);
1091 Subunit::end_testsuite($name, "skip",
1092 "environment $envname is unknown in this test backend - skipping");
1093 next;
1096 # Generate a file with the individual tests to run, if the
1097 # test runner for this test suite supports it.
1098 if ($individual_tests and $individual_tests->{$name}) {
1099 if ($$_[3]) {
1100 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1101 foreach my $test (@{$individual_tests->{$name}}) {
1102 print $fh substr($test, length($name)+1) . "\n";
1104 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1105 } else {
1106 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1110 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1112 teardown_env($envname) if ($opt_resetup_env);
1116 print "\n";
1118 teardown_env($_) foreach (keys %running_envs);
1120 my $failed = 0;
1122 # if there were any valgrind failures, show them
1123 foreach (<$prefix/valgrind.log*>) {
1124 next unless (-s $_);
1125 print "VALGRIND FAILURE\n";
1126 $failed++;
1127 system("cat $_");
1129 exit 0;