ldap_server: Move code into authenticate_ldap_simple_bind()
[Samba.git] / selftest / selftest.pl
blob45e0ae0451f1851530602c4abbeb906add5085fb
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 mkdir($prefix, 0700) unless -d $prefix;
311 chmod 0700, $prefix;
313 my $prefix_abs = abs_path($prefix);
314 my $tmpdir_abs = abs_path("$prefix/tmp");
315 mkdir($tmpdir_abs, 0777) unless -d $tmpdir_abs;
317 my $srcdir_abs = abs_path($srcdir);
319 die("using an empty absolute prefix isn't allowed") unless $prefix_abs ne "";
320 die("using '/' as absolute prefix isn't allowed") unless $prefix_abs ne "/";
322 $ENV{PREFIX} = $prefix;
323 $ENV{PREFIX_ABS} = $prefix_abs;
324 $ENV{SRCDIR} = $srcdir;
325 $ENV{SRCDIR_ABS} = $srcdir_abs;
326 $ENV{GNUPGHOME} = "$srcdir_abs/selftest/gnupg";
327 $ENV{BINDIR} = $bindir_abs;
329 my $tls_enabled = not $opt_quick;
330 $ENV{TLS_ENABLED} = ($tls_enabled?"yes":"no");
332 sub prefix_pathvar($$)
334 my ($name, $newpath) = @_;
335 if (defined($ENV{$name})) {
336 $ENV{$name} = "$newpath:$ENV{$name}";
337 } else {
338 $ENV{$name} = $newpath;
341 prefix_pathvar("PKG_CONFIG_PATH", "$bindir_abs/pkgconfig");
342 prefix_pathvar("PYTHONPATH", "$bindir_abs/python");
344 if ($opt_socket_wrapper_keep_pcap) {
345 # Socket wrapper keep pcap implies socket wrapper pcap
346 $opt_socket_wrapper_pcap = 1;
349 if ($opt_socket_wrapper_pcap) {
350 # Socket wrapper pcap implies socket wrapper
351 $opt_socket_wrapper = 1;
354 my $ld_preload = $ENV{LD_PRELOAD};
356 if ($opt_libnss_wrapper_so_path) {
357 if ($ld_preload) {
358 $ld_preload = "$ld_preload:$opt_libnss_wrapper_so_path";
359 } else {
360 $ld_preload = "$opt_libnss_wrapper_so_path";
364 if ($opt_libresolv_wrapper_so_path) {
365 if ($ld_preload) {
366 $ld_preload = "$ld_preload:$opt_libresolv_wrapper_so_path";
367 } else {
368 $ld_preload = "$opt_libresolv_wrapper_so_path";
372 if ($opt_libsocket_wrapper_so_path) {
373 if ($ld_preload) {
374 $ld_preload = "$ld_preload:$opt_libsocket_wrapper_so_path";
375 } else {
376 $ld_preload = "$opt_libsocket_wrapper_so_path";
380 if ($opt_libuid_wrapper_so_path) {
381 if ($ld_preload) {
382 $ld_preload = "$ld_preload:$opt_libuid_wrapper_so_path";
383 } else {
384 $ld_preload = "$opt_libuid_wrapper_so_path";
388 $ENV{LD_PRELOAD} = $ld_preload;
389 print "LD_PRELOAD=$ENV{LD_PRELOAD}\n";
391 # Enable uid_wrapper globally
392 $ENV{UID_WRAPPER} = 1;
394 # Disable RTLD_DEEPBIND hack for Samba bind dlz module
396 # This is needed in order to allow the ldb_*ldap module
397 # to work with a preloaded socket wrapper.
398 $ENV{LDB_MODULES_DISABLE_DEEPBIND} = 1;
400 my $socket_wrapper_dir;
401 if ($opt_socket_wrapper) {
402 $socket_wrapper_dir = SocketWrapper::setup_dir("$prefix_abs/w", $opt_socket_wrapper_pcap);
403 print "SOCKET_WRAPPER_DIR=$socket_wrapper_dir\n";
404 } elsif (not $opt_list) {
405 unless ($< == 0) {
406 warn("not using socket wrapper, but also not running as root. Will not be able to listen on proper ports");
410 if ($opt_use_dns_faking) {
411 print "DNS: Faking nameserver\n";
412 $ENV{SAMBA_DNS_FAKING} = 1;
415 my $target;
416 my $testenv_default = "none";
418 # After this many seconds, the server will self-terminate. All tests
419 # must terminate in this time, and testenv will only stay alive this
420 # long
422 my $server_maxtime;
423 if ($opt_testenv) {
424 # 1 year should be enough :-)
425 $server_maxtime = 365 * 24 * 60 * 60;
426 } else {
427 # make test should run under 4 hours
428 $server_maxtime = 4 * 60 * 60;
431 if (defined($ENV{SMBD_MAXTIME}) and $ENV{SMBD_MAXTIME} ne "") {
432 $server_maxtime = $ENV{SMBD_MAXTIME};
435 unless ($opt_list) {
436 if ($opt_target eq "samba") {
437 $testenv_default = "ad_dc_ntvfs";
438 require target::Samba;
439 $target = new Samba($bindir, $ldap, $srcdir, $server_maxtime);
440 } elsif ($opt_target eq "samba3") {
441 $testenv_default = "nt4_member";
442 require target::Samba3;
443 $target = new Samba3($bindir, $srcdir_abs, $server_maxtime);
447 sub read_test_regexes($)
449 my ($name) = @_;
450 my @ret = ();
451 open(LF, "<$name") or die("unable to read $name: $!");
452 while (<LF>) {
453 chomp;
454 next if (/^#/);
455 if (/^(.*?)([ \t]+)\#([\t ]*)(.*?)$/) {
456 push (@ret, [$1, $4]);
457 } else {
458 s/^(.*?)([ \t]+)\#([\t ]*)(.*?)$//;
459 push (@ret, [$_, undef]);
462 close(LF);
463 return @ret;
466 foreach (@opt_exclude) {
467 push (@excludes, read_test_regexes($_));
470 foreach (@opt_include) {
471 push (@includes, read_test_regexes($_));
474 my $interfaces = join(',', ("127.0.0.11/8",
475 "127.0.0.12/8",
476 "127.0.0.13/8",
477 "127.0.0.14/8",
478 "127.0.0.15/8",
479 "127.0.0.16/8"));
481 my $clientdir = "$prefix_abs/client";
483 my $conffile = "$clientdir/client.conf";
484 $ENV{SMB_CONF_PATH} = $conffile;
486 sub write_clientconf($$$)
488 my ($conffile, $clientdir, $vars) = @_;
490 mkdir("$clientdir", 0777) unless -d "$clientdir";
492 if ( -d "$clientdir/private" ) {
493 unlink <$clientdir/private/*>;
494 } else {
495 mkdir("$clientdir/private", 0777);
498 if ( -d "$clientdir/lockdir" ) {
499 unlink <$clientdir/lockdir/*>;
500 } else {
501 mkdir("$clientdir/lockdir", 0777);
504 if ( -d "$clientdir/statedir" ) {
505 unlink <$clientdir/statedir/*>;
506 } else {
507 mkdir("$clientdir/statedir", 0777);
510 if ( -d "$clientdir/cachedir" ) {
511 unlink <$clientdir/cachedir/*>;
512 } else {
513 mkdir("$clientdir/cachedir", 0777);
516 # this is ugly, but the ncalrpcdir needs exactly 0755
517 # otherwise tests fail.
518 my $mask = umask;
519 umask 0022;
520 if ( -d "$clientdir/ncalrpcdir/np" ) {
521 unlink <$clientdir/ncalrpcdir/np/*>;
522 rmdir "$clientdir/ncalrpcdir/np";
524 if ( -d "$clientdir/ncalrpcdir" ) {
525 unlink <$clientdir/ncalrpcdir/*>;
526 rmdir "$clientdir/ncalrpcdir";
528 mkdir("$clientdir/ncalrpcdir", 0755);
529 umask $mask;
531 my $cadir = "$ENV{SRCDIR_ABS}/selftest/manage-ca/CA-samba.example.com";
532 my $cacert = "$cadir/Public/CA-samba.example.com-cert.pem";
533 my $cacrl_pem = "$cadir/Public/CA-samba.example.com-crl.pem";
534 my $ca_users_dir = "$cadir/Users";
536 if ( -d "$clientdir/pkinit" ) {
537 unlink <$clientdir/pkinit/*>;
538 } else {
539 mkdir("$clientdir/pkinit", 0700);
542 # each user has a USER-${USER_PRINCIPAL_NAME}-cert.pem and
543 # USER-${USER_PRINCIPAL_NAME}-private-key.pem symlink
544 # We make a copy here and make the certificated easily
545 # accessable in the client environment.
546 my $mask = umask;
547 umask 0077;
548 opendir USERS, "${ca_users_dir}" or die "Could not open dir '${ca_users_dir}': $!";
549 for my $d (readdir USERS) {
550 my $user_dir = "${ca_users_dir}/${d}";
551 next if ${d} =~ /^\./;
552 next if (! -d "${user_dir}");
553 opendir USER, "${user_dir}" or die "Could not open dir '${user_dir}': $!";
554 for my $l (readdir USER) {
555 my $user_link = "${user_dir}/${l}";
556 next if ${l} =~ /^\./;
557 next if (! -l "${user_link}");
559 my $dest = "${clientdir}/pkinit/${l}";
560 Samba::copy_file_content(${user_link}, ${dest});
562 closedir USER;
564 closedir USERS;
565 umask $mask;
567 open(CF, ">$conffile");
568 print CF "[global]\n";
569 print CF "\tnetbios name = client\n";
570 if (defined($vars->{DOMAIN})) {
571 print CF "\tworkgroup = $vars->{DOMAIN}\n";
573 if (defined($vars->{REALM})) {
574 print CF "\trealm = $vars->{REALM}\n";
576 if ($opt_socket_wrapper) {
577 print CF "\tinterfaces = $interfaces\n";
579 print CF "
580 private dir = $clientdir/private
581 lock dir = $clientdir/lockdir
582 state directory = $clientdir/statedir
583 cache directory = $clientdir/cachedir
584 ncalrpc dir = $clientdir/ncalrpcdir
585 panic action = $RealBin/gdb_backtrace \%d
586 max xmit = 32K
587 notify:inotify = false
588 ldb:nosync = true
589 system:anonymous = true
590 client lanman auth = Yes
591 log level = 1
592 torture:basedir = $clientdir
593 #We don't want to pass our self-tests if the PAC code is wrong
594 gensec:require_pac = true
595 #We don't want to run 'speed' tests for very long
596 torture:timelimit = 1
597 winbind separator = /
598 tls cafile = ${cacert}
599 tls crlfile = ${cacrl_pem}
600 tls verify peer = no_check
601 include system krb5 conf = no
603 close(CF);
606 my @todo = ();
608 sub should_run_test($)
610 my $name = shift;
611 if ($#tests == -1) {
612 return 1;
614 for (my $i=0; $i <= $#tests; $i++) {
615 if ($name =~ /$tests[$i]/i) {
616 return 1;
619 return 0;
622 sub read_testlist($)
624 my ($filename) = @_;
626 my @ret = ();
627 open(IN, $filename) or die("Unable to open $filename: $!");
629 while (<IN>) {
630 if (/-- TEST(-LOADLIST|) --\n/) {
631 my $supports_loadlist = (defined($1) and $1 eq "-LOADLIST");
632 my $name = <IN>;
633 $name =~ s/\n//g;
634 my $env = <IN>;
635 $env =~ s/\n//g;
636 my $loadlist;
637 if ($supports_loadlist) {
638 $loadlist = <IN>;
639 $loadlist =~ s/\n//g;
641 my $cmdline = <IN>;
642 $cmdline =~ s/\n//g;
643 if (should_run_test($name) == 1) {
644 push (@ret, [$name, $env, $cmdline, $loadlist]);
646 } else {
647 print;
650 close(IN) or die("Error creating recipe from $filename");
651 return @ret;
654 if ($#testlists == -1) {
655 die("No testlists specified");
658 $ENV{SELFTEST_PREFIX} = "$prefix_abs";
659 $ENV{SELFTEST_TMPDIR} = "$tmpdir_abs";
660 $ENV{TMPDIR} = "$tmpdir_abs";
661 $ENV{TEST_DATA_PREFIX} = "$tmpdir_abs";
662 if ($opt_socket_wrapper) {
663 $ENV{SELFTEST_INTERFACES} = $interfaces;
664 } else {
665 $ENV{SELFTEST_INTERFACES} = "";
667 if ($opt_quick) {
668 $ENV{SELFTEST_QUICK} = "1";
669 } else {
670 $ENV{SELFTEST_QUICK} = "";
672 $ENV{SELFTEST_MAXTIME} = $torture_maxtime;
674 my $selftest_krbt_ccache_path = "$tmpdir_abs/selftest.krb5_ccache";
675 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.global";
677 my @available = ();
678 foreach my $fn (@testlists) {
679 foreach (read_testlist($fn)) {
680 my $name = $$_[0];
681 next if (@includes and not defined(find_in_list(\@includes, $name)));
682 push (@available, $_);
686 my $restricted = undef;
687 my $restricted_used = {};
689 if ($opt_load_list) {
690 $restricted = [];
691 open(LOAD_LIST, "<$opt_load_list") or die("Unable to open $opt_load_list");
692 while (<LOAD_LIST>) {
693 chomp;
694 push (@$restricted, $_);
696 close(LOAD_LIST);
699 my $individual_tests = undef;
700 $individual_tests = {};
702 foreach my $testsuite (@available) {
703 my $name = $$testsuite[0];
704 my $skipreason = skip($name);
705 if (defined($restricted)) {
706 # Find the testsuite for this test
707 my $match = undef;
708 foreach my $r (@$restricted) {
709 if ($r eq $name) {
710 $individual_tests->{$name} = [];
711 $match = $r;
712 $restricted_used->{$r} = 1;
713 } elsif (substr($r, 0, length($name)+1) eq "$name.") {
714 push(@{$individual_tests->{$name}}, $r);
715 $match = $r;
716 $restricted_used->{$r} = 1;
719 if ($match) {
720 if (defined($skipreason)) {
721 if (not $opt_list) {
722 Subunit::skip_testsuite($name, $skipreason);
724 } else {
725 push(@todo, $testsuite);
728 } elsif (defined($skipreason)) {
729 if (not $opt_list) {
730 Subunit::skip_testsuite($name, $skipreason);
732 } else {
733 push(@todo, $testsuite);
737 if (defined($restricted)) {
738 foreach (@$restricted) {
739 unless (defined($restricted_used->{$_})) {
740 print "No test or testsuite found matching $_\n";
743 } elsif ($#todo == -1) {
744 print STDERR "No tests to run\n";
745 exit(1);
748 my $suitestotal = $#todo + 1;
750 unless ($opt_list) {
751 Subunit::progress($suitestotal);
752 Subunit::report_time(time());
755 my $i = 0;
756 $| = 1;
758 my %running_envs = ();
760 sub get_running_env($)
762 my ($name) = @_;
764 my $envname = $name;
766 $envname =~ s/:.*//;
768 return $running_envs{$envname};
771 my @exported_envvars = (
772 # domain stuff
773 "DOMAIN",
774 "REALM",
776 # stuff related to a trusted domain
777 "TRUST_SERVER",
778 "TRUST_SERVER_IP",
779 "TRUST_SERVER_IPV6",
780 "TRUST_NETBIOSNAME",
781 "TRUST_USERNAME",
782 "TRUST_PASSWORD",
783 "TRUST_DOMAIN",
784 "TRUST_REALM",
786 # domain controller stuff
787 "DC_SERVER",
788 "DC_SERVER_IP",
789 "DC_SERVER_IPV6",
790 "DC_NETBIOSNAME",
791 "DC_NETBIOSALIAS",
793 # domain member
794 "MEMBER_SERVER",
795 "MEMBER_SERVER_IP",
796 "MEMBER_SERVER_IPV6",
797 "MEMBER_NETBIOSNAME",
798 "MEMBER_NETBIOSALIAS",
800 # rpc proxy controller stuff
801 "RPC_PROXY_SERVER",
802 "RPC_PROXY_SERVER_IP",
803 "RPC_PROXY_SERVER_IPV6",
804 "RPC_PROXY_NETBIOSNAME",
805 "RPC_PROXY_NETBIOSALIAS",
807 # domain controller stuff for Vampired DC
808 "VAMPIRE_DC_SERVER",
809 "VAMPIRE_DC_SERVER_IP",
810 "VAMPIRE_DC_SERVER_IPV6",
811 "VAMPIRE_DC_NETBIOSNAME",
812 "VAMPIRE_DC_NETBIOSALIAS",
814 # domain controller stuff for FL 2000 Vampired DC
815 "VAMPIRE_2000_DC_SERVER",
816 "VAMPIRE_2000_DC_SERVER_IP",
817 "VAMPIRE_2000_DC_SERVER_IPV6",
818 "VAMPIRE_2000_DC_NETBIOSNAME",
819 "VAMPIRE_2000_DC_NETBIOSALIAS",
821 "PROMOTED_DC_SERVER",
822 "PROMOTED_DC_SERVER_IP",
823 "PROMOTED_DC_SERVER_IPV6",
824 "PROMOTED_DC_NETBIOSNAME",
825 "PROMOTED_DC_NETBIOSALIAS",
827 # server stuff
828 "SERVER",
829 "SERVER_IP",
830 "SERVER_IPV6",
831 "NETBIOSNAME",
832 "NETBIOSALIAS",
834 # user stuff
835 "USERNAME",
836 "USERID",
837 "PASSWORD",
838 "DC_USERNAME",
839 "DC_PASSWORD",
841 # UID/GID for rfc2307 mapping tests
842 "UID_RFC2307TEST",
843 "GID_RFC2307TEST",
845 # misc stuff
846 "KRB5_CONFIG",
847 "KRB5CCNAME",
848 "SELFTEST_WINBINDD_SOCKET_DIR",
849 "NMBD_SOCKET_DIR",
850 "LOCAL_PATH",
851 "DNS_FORWARDER1",
852 "DNS_FORWARDER2",
853 "RESOLV_CONF",
854 "UNACCEPTABLE_PASSWORD",
856 # nss_wrapper
857 "NSS_WRAPPER_PASSWD",
858 "NSS_WRAPPER_GROUP",
859 "NSS_WRAPPER_HOSTS",
860 "NSS_WRAPPER_MODULE_SO_PATH",
861 "NSS_WRAPPER_MODULE_FN_PREFIX",
863 # resolv_wrapper
864 "RESOLV_WRAPPER_CONF",
865 "RESOLV_WRAPPER_HOSTS",
868 sub sighandler($)
870 my $signame = shift;
872 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'DEFAULT';
873 $SIG{PIPE} = 'IGNORE';
875 open(STDOUT, ">&STDERR") or die "can't dup STDOUT to STDERR: $!";
877 print "$0: PID[$$]: Got SIG${signame} teardown environments.\n";
878 teardown_env($_) foreach(keys %running_envs);
879 system("pstree -p $$");
880 print "$0: PID[$$]: Exiting...\n";
881 exit(1);
884 $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = $SIG{PIPE} = \&sighandler;
886 sub setup_env($$)
888 my ($name, $prefix) = @_;
890 my $testenv_vars = undef;
892 my $envname = $name;
893 my $option = $name;
895 $envname =~ s/:.*//;
896 $option =~ s/^[^:]*//;
897 $option =~ s/^://;
899 $option = "client" if $option eq "";
901 $ENV{KRB5CCNAME} = "FILE:${selftest_krbt_ccache_path}.${envname}/ignore";
903 if (defined(get_running_env($envname))) {
904 $testenv_vars = get_running_env($envname);
905 if (not $testenv_vars->{target}->check_env($testenv_vars)) {
906 print $testenv_vars->{target}->getlog_env($testenv_vars);
907 $testenv_vars = undef;
909 } else {
910 $testenv_vars = $target->setup_env($envname, $prefix);
911 if (defined($testenv_vars) and $testenv_vars eq "UNKNOWN") {
912 return $testenv_vars;
913 } elsif (defined($testenv_vars) && not defined($testenv_vars->{target})) {
914 $testenv_vars->{target} = $target;
916 if (not defined($testenv_vars)) {
917 warn("$opt_target can't start up known environment '$envname'");
921 return undef unless defined($testenv_vars);
923 $running_envs{$envname} = $testenv_vars;
925 if ($option eq "local") {
926 SocketWrapper::set_default_iface($testenv_vars->{SOCKET_WRAPPER_DEFAULT_IFACE});
927 $ENV{SMB_CONF_PATH} = $testenv_vars->{SERVERCONFFILE};
928 } elsif ($option eq "client") {
929 SocketWrapper::set_default_iface(11);
930 write_clientconf($conffile, $clientdir, $testenv_vars);
931 $ENV{SMB_CONF_PATH} = $conffile;
932 } else {
933 die("Unknown option[$option] for envname[$envname]");
936 foreach (@exported_envvars) {
937 if (defined($testenv_vars->{$_})) {
938 $ENV{$_} = $testenv_vars->{$_};
939 } else {
940 delete $ENV{$_};
944 my $krb5_ccache_path = "${selftest_krbt_ccache_path}.${envname}.${option}";
945 unlink($krb5_ccache_path);
946 $ENV{KRB5CCNAME} = "FILE:${krb5_ccache_path}";
947 return $testenv_vars;
950 sub exported_envvars_str($)
952 my ($testenv_vars) = @_;
953 my $out = "";
955 foreach (@exported_envvars) {
956 next unless defined($testenv_vars->{$_});
957 $out .= $_."=".$testenv_vars->{$_}."\n";
960 return $out;
963 sub getlog_env($)
965 my ($envname) = @_;
966 return "" if ($envname eq "none");
967 my $env = get_running_env($envname);
968 return $env->{target}->getlog_env($env);
971 sub check_env($)
973 my ($envname) = @_;
974 my $env = get_running_env($envname);
975 return $env->{target}->check_env($env);
978 sub teardown_env($)
980 my ($envname) = @_;
981 return if ($envname eq "none");
982 print STDERR "teardown_env($envname)\n";
983 my $env = get_running_env($envname);
984 $env->{target}->teardown_env($env);
985 delete $running_envs{$envname};
988 # This 'global' file needs to be empty when we start
989 unlink("$prefix_abs/dns_host_file");
990 unlink("$prefix_abs/hosts");
992 if ($opt_random_order) {
993 require List::Util;
994 my @newtodo = List::Util::shuffle(@todo);
995 @todo = @newtodo;
998 if ($opt_testenv) {
999 my $testenv_name = $ENV{SELFTEST_TESTENV};
1000 $testenv_name = $testenv_default unless defined($testenv_name);
1002 my $testenv_vars = setup_env($testenv_name, $prefix);
1004 if (not $testenv_vars or $testenv_vars eq "UNKNOWN") {
1005 die("Unable to setup environment $testenv_name");
1008 $ENV{PIDDIR} = $testenv_vars->{PIDDIR};
1009 $ENV{ENVNAME} = $testenv_name;
1011 my $envvarstr = exported_envvars_str($testenv_vars);
1013 my @term_args = ("echo -e \"
1014 Welcome to the Samba4 Test environment '$testenv_name'
1016 This matches the client environment used in make test
1017 server is pid `cat \$PIDDIR/samba.pid`
1019 Some useful environment variables:
1020 TORTURE_OPTIONS=\$TORTURE_OPTIONS
1021 SMB_CONF_PATH=\$SMB_CONF_PATH
1023 $envvarstr
1024 \" && LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH} bash");
1025 my @term = ();
1026 if ($ENV{TERMINAL}) {
1027 @term = ($ENV{TERMINAL});
1028 } else {
1029 @term = ("xterm", "-e");
1030 unshift(@term_args, ("bash", "-c"));
1033 system(@term, @term_args);
1035 teardown_env($testenv_name);
1036 } elsif ($opt_list) {
1037 foreach (@todo) {
1038 my $name = $$_[0];
1039 my $envname = $$_[1];
1040 my $cmd = $$_[2];
1041 my $listcmd = $$_[3];
1043 unless (defined($listcmd)) {
1044 warn("Unable to list tests in $name");
1045 # Rather than ignoring this testsuite altogether, just pretend the entire testsuite is
1046 # a single "test".
1047 print "$name\n";
1048 next;
1051 system($listcmd);
1053 if ($? == -1) {
1054 die("Unable to run $listcmd: $!");
1055 } elsif ($? & 127) {
1056 die(sprintf("%s died with signal %d, %s coredump\n", $listcmd, ($? & 127), ($? & 128) ? 'with' : 'without'));
1059 my $exitcode = $? >> 8;
1060 if ($exitcode != 0) {
1061 die("$cmd exited with exit code $exitcode");
1064 } else {
1065 foreach (@todo) {
1066 $i++;
1067 my $cmd = $$_[2];
1068 my $name = $$_[0];
1069 my $envname = $$_[1];
1071 my $envvars = setup_env($envname, $prefix);
1072 if (not defined($envvars)) {
1073 Subunit::start_testsuite($name);
1074 Subunit::end_testsuite($name, "error",
1075 "unable to set up environment $envname - exiting");
1076 next;
1077 } elsif ($envvars eq "UNKNOWN") {
1078 Subunit::start_testsuite($name);
1079 Subunit::end_testsuite($name, "skip",
1080 "environment $envname is unknown in this test backend - skipping");
1081 next;
1084 # Generate a file with the individual tests to run, if the
1085 # test runner for this test suite supports it.
1086 if ($individual_tests and $individual_tests->{$name}) {
1087 if ($$_[3]) {
1088 my ($fh, $listid_file) = tempfile(UNLINK => 0);
1089 foreach my $test (@{$individual_tests->{$name}}) {
1090 print $fh substr($test, length($name)+1) . "\n";
1092 $cmd =~ s/\$LOADLIST/--load-list=$listid_file/g;
1093 } else {
1094 warn("Unable to run individual tests in $name, it does not support --loadlist.");
1098 run_testsuite($envname, $name, $cmd, $i, $suitestotal);
1100 teardown_env($envname) if ($opt_resetup_env);
1104 print "\n";
1106 teardown_env($_) foreach (keys %running_envs);
1108 my $failed = 0;
1110 # if there were any valgrind failures, show them
1111 foreach (<$prefix/valgrind.log*>) {
1112 next unless (-s $_);
1113 print "VALGRIND FAILURE\n";
1114 $failed++;
1115 system("cat $_");
1117 exit 0;