1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
# common functionality for tests.
# imported into main for ease of use.
package main;
use Cwd;
use Config;
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec;
BEGIN {
# No spamd test in Windows unless env override says user figured out a way
# If you want to know why these are vars and no constants, read this thread:
# <http://www.mail-archive.com/dev%40perl.apache.org/msg05466.html>
# -- mss, 2004-01-13
our $RUNNING_ON_WINDOWS = ($^O =~ /^(mswin|dos|os2)/oi);
our $SKIP_SPAMD_TESTS = $RUNNING_ON_WINDOWS;
our $NO_SPAMC_EXE;
our $SKIP_SPAMC_TESTS;
our $SSL_AVAILABLE;
}
# Set up for testing. Exports (as global vars):
# out: $home: $HOME env variable
# out: $cwd: here
# out: $scr: spamassassin script
#
sub sa_t_init {
my $tname = shift;
if ($config{PERL_PATH}) {
$perl_path = $config{PERL_PATH};
}
elsif ($^X =~ m|^/|) {
$perl_path = $^X;
}
else {
$perl_path = $Config{perlpath};
$perl_path =~ s|/[^/]*$|/$^X|;
}
$perl_cmd = $perl_path;
$perl_cmd .= " -T" if !defined($ENV{'TEST_PERL_TAINT'}) or $ENV{'TEST_PERL_TAINT'} ne 'no';
$perl_cmd .= " -w" if !defined($ENV{'TEST_PERL_WARN'}) or $ENV{'TEST_PERL_WARN'} ne 'no';
$scr = $ENV{'SCRIPT'};
$scr ||= "$perl_cmd ../spamassassin";
$spamd = "$perl_cmd ../spamd/spamd";
$spamc = $ENV{'SPAMC_SCRIPT'};
$spamc ||= "../spamc/spamc";
$salearn = $ENV{'SALEARN_SCRIPT'};
$salearn ||= "$perl_cmd ../sa-learn";
$spamdhost = $ENV{'SPAMD_HOST'};
$spamdhost ||= "localhost";
$spamdport = $ENV{'SPAMD_PORT'};
$spamdport ||= probably_unused_spamd_port();
$spamd_cf_args = "-C log/test_rules_copy";
$spamd_localrules_args = " --siteconfigpath log/localrules.tmp";
$scr_localrules_args = " --siteconfigpath log/localrules.tmp";
$salearn_localrules_args = " --siteconfigpath log/localrules.tmp";
$scr_cf_args = "-C log/test_rules_copy";
$scr_pref_args = "-p log/test_default.cf";
$salearn_cf_args = "-C log/test_rules_copy";
$salearn_pref_args = "-p log/test_default.cf";
$scr_test_args = "";
$salearn_test_args = "";
$set_test_prefs = 0;
$default_cf_lines = "
bayes_path ./log/user_state/bayes
auto_whitelist_path ./log/user_state/auto-whitelist
";
(-f "t/test_dir") && chdir("t"); # run from ..
read_config();
$NO_SPAMC_EXE = ($RUNNING_ON_WINDOWS &&
!$ENV{'SPAMC_SCRIPT'} &&
!(-e "../spamc/spamc.exe"));
$SKIP_SPAMC_TESTS = ($NO_SPAMC_EXE ||
($RUNNING_ON_WINDOWS && !$ENV{'SPAMD_HOST'}));
$SSL_AVAILABLE = ((!$SKIP_SPAMC_TESTS) && # no SSL test if no spamc
(!$SKIP_SPAMD_TESTS) && # or if no local spamd
(`$spamc -V` =~ /with SSL support/) &&
(`$spamd --version` =~ /with SSL support/));
# do not remove prior test results!
# rmtree ("log");
mkdir ("log", 0755);
rmtree ("log/user_state");
rmtree ("log/outputdir.tmp");
rmtree ("log/test_rules_copy");
mkdir ("log/test_rules_copy", 0755);
for $file (<../rules/*.cf>) {
$base = basename $file;
copy ($file, "log/test_rules_copy/$base")
or warn "cannot copy $file to log/test_rules_copy/$base";
}
copy ("data/01_test_rules.cf", "log/test_rules_copy/01_test_rules.cf")
or warn "cannot copy data/01_test_rules.cf to log/test_rules_copy/01_test_rules.cf";
rmtree ("log/localrules.tmp");
mkdir ("log/localrules.tmp", 0755);
for $file (<../rules/*.pre>) {
$base = basename $file;
copy ($file, "log/localrules.tmp/$base")
or warn "cannot copy $file to log/localrules.tmp/$base";
}
copy ("../rules/user_prefs.template", "log/test_rules_copy/99_test_default.cf")
or die "user prefs copy failed";
open (PREFS, ">>log/test_rules_copy/99_test_default.cf");
print PREFS $default_cf_lines;
close PREFS;
# create an empty .prefs file
open (PREFS, ">>log/test_default.cf"); close PREFS;
mkdir("log/user_state",0755);
$home = $ENV{'HOME'};
$home ||= $ENV{'WINDIR'} if (defined $ENV{'WINDIR'});
$cwd = getcwd;
$ENV{'TEST_DIR'} = $cwd;
$testname = $tname;
}
# a port number between 32768 and 65535; used to allow multiple test
# suite runs on the same machine simultaneously
sub probably_unused_spamd_port {
my $port;
my @nstat = ();
if (open(NSTAT, "netstat -a -n 2>&1 |")) {
@nstat = grep(/^\s*tcp/i, <NSTAT>);
close(NSTAT);
}
my $delta = ($$ % 32768) || int(rand(32768));
for (1..10) {
$port = 32768 + $delta;
last unless (getservbyport($port, "tcp") || grep(/[:.]$port\s/, @nstat));
$delta = int(rand(32768));
}
return $port;
}
sub locate_command {
my ($command) = @_;
my @path = File::Spec->path();
push(@path, '/usr/bin') if ! grep { m@/usr/bin/?$@ } @path;
for my $path (@path) {
$location = "$path/$command";
$location =~ s@//@/@g;
return $location if -x $location;
}
return 0;
}
sub sa_t_finish {
# no-op currently
}
sub tstfile {
my $file = shift;
open (OUT, ">log/mail.txt") or die;
print OUT $file; close OUT;
}
sub tstlocalrules {
my $lines = shift;
$set_local_rules = 1;
open (OUT, ">log/localrules.tmp/00test.cf") or die;
print OUT $lines; close OUT;
}
sub tstprefs {
my $lines = shift;
$set_test_prefs = 1;
# TODO: should we use -p, or modify the test_rules_copy/99_test_default.cf?
# for now, I'm taking the -p route, since we have to be able to test
# the operation of user-prefs in general, itself.
open (OUT, ">log/tst.cf") or die;
print OUT $lines; close OUT;
$scr_pref_args = "-p log/tst.cf";
}
# creates a .pre file in the localrules dir to be parsed alongside init.pre
# make it zz_* just to make sure it is parse last
sub tstpre {
my $lines = shift;
open (OUT, ">log/localrules.tmp/zz_tst.pre") or die;
print OUT $lines; close OUT;
}
# Run spamassassin. Calls back with the output.
# in $args: arguments to run with
# in $read_sub: callback for the output (should read from <IN>).
# This is called with no args.
#
# out: $sa_exitcode global: exitcode from sitescooper
# ret: undef if sitescooper fails, 1 for exit 0
#
sub sarun {
my $args = shift;
my $read_sub = shift;
my $post_redir = '';
$args =~ s/ 2\>\&1$// and $post_redir = ' 2>&1';
rmtree ("log/outputdir.tmp"); # some tests use this
mkdir ("log/outputdir.tmp", 0755);
%found = ();
%found_anti = ();
if (defined $ENV{'SA_ARGS'}) {
$args = $ENV{'SA_ARGS'} . " ". $args;
}
$args = "$scr_cf_args $scr_localrules_args $scr_pref_args $scr_test_args $args";
# added fix for Windows tests from Rudif
my $scrargs = "$scr $args";
$scrargs =~ s!/!\\!g if ($^O =~ /^MS(DOS|Win)/i);
print ("\t$scrargs\n");
system ("$scrargs > log/$testname.${Test::ntest} $post_redir");
$sa_exitcode = ($?>>8);
if ($sa_exitcode != 0) { return undef; }
&checkfile ("$testname.${Test::ntest}", $read_sub) if (defined $read_sub);
1;
}
# Run salearn. Calls back with the output.
# in $args: arguments to run with
# in $read_sub: callback for the output (should read from <IN>).
# This is called with no args.
#
# out: $salearn_exitcode global: exitcode from sitescooper
# ret: undef if sitescooper fails, 1 for exit 0
#
sub salearnrun {
my $args = shift;
my $read_sub = shift;
rmtree ("log/outputdir.tmp"); # some tests use this
mkdir ("log/outputdir.tmp", 0755);
%found = ();
%found_anti = ();
if (defined $ENV{'SA_ARGS'}) {
$args = $ENV{'SA_ARGS'} . " ". $args;
}
$args = "$salearn_cf_args $salearn_localrules_args $salearn_pref_args $salearn_test_args $args";
# added fix for Windows tests from Rudif
my $salearnargs = "$salearn $args";
$salearnargs =~ s!/!\\!g if ($^O =~ /^MS(DOS|Win)/i);
print ("\t$salearnargs\n");
system ("$salearnargs > log/$testname.${Test::ntest}");
$salearn_exitcode = ($?>>8);
if ($salearn_exitcode != 0) { return undef; }
&checkfile ("$testname.${Test::ntest}", $read_sub) if (defined $read_sub);
1;
}
sub scrun {
spamcrun (@_, 0);
}
sub scrunwithstderr {
spamcrun (@_, 1);
}
sub spamcrun {
my $args = shift;
my $read_sub = shift;
my $capture_stderr = shift;
if (defined $ENV{'SC_ARGS'}) {
$args = $ENV{'SC_ARGS'} . " ". $args;
}
my $spamcargs;
if($args !~ /\b(?:-p\s*[0-9]+|-F|-U)\b/)
{
$args = "-d $spamdhost -p $spamdport $args";
}
if ($args !~ /-F/) {
$spamcargs = "$spamc -F data/spamc_blank.cf $args";
}
else {
$spamcargs = "$spamc $args";
}
$spamcargs =~ s!/!\\!g if ($^O =~ /^MS(DOS|Win)/i);
print ("\t$spamcargs\n");
if ($capture_stderr) {
system ("$spamcargs > log/$testname.out 2>&1");
} else {
system ("$spamcargs > log/$testname.out");
}
$sa_exitcode = ($?>>8);
if ($sa_exitcode != 0) { stop_spamd(); return undef; }
%found = ();
%found_anti = ();
&checkfile ("$testname.out", $read_sub) if (defined $read_sub);
($sa_exitcode == 0);
}
sub spamcrun_background {
my $args = shift;
my $read_sub = shift;
if (defined $ENV{'SC_ARGS'}) {
$args = $ENV{'SC_ARGS'} . " ". $args;
}
my $spamcargs;
if($args !~ /\b(?:-p\s*[0-9]+|-o|-U)\b/)
{
$spamcargs = "$spamc -p $spamdport $args";
}
else
{
$spamcargs = "$spamc $args";
}
$spamcargs =~ s!/!\\!g if ($^O =~ /^MS(DOS|Win)/i);
print ("\t$spamcargs &\n");
system ("$spamcargs > log/$testname.bg &") and return 0;
1;
}
sub sdrun {
my $sdargs = shift;
my $args = shift;
my $read_sub = shift;
start_spamd ($sdargs);
spamcrun ($args, $read_sub);
stop_spamd ();
1;
}
sub start_spamd {
return if $SKIP_SPAMD_TESTS;
my $spamd_extra_args = shift;
return if (defined($spamd_pid) && $spamd_pid > 0);
rmtree ("log/outputdir.tmp"); # some tests use this
mkdir ("log/outputdir.tmp", 0755);
if (defined $ENV{'SD_ARGS'}) {
$spamd_extra_args = $ENV{'SD_ARGS'} . " ". $spamd_extra_args;
}
my @spamd_args = (
$spamd,
qq{-D},
qq{-x}
);
if (!$spamd_inhibit_log_to_err) {
push (@spamd_args,
qq{-s}, qq{stderr},
);
}
if ($spamd_extra_args !~ /(?:-C\s*[^-]\S+)/) {
push(@spamd_args,
$spamd_cf_args,
$spamd_localrules_args,
);
}
if ($spamd_extra_args !~ /(?:-p\s*[0-9]+|-o|--socketpath)/) {
push(@spamd_args,
qq{-p}, $spamdport,
);
}
if ($set_test_prefs) {
warn "oops! SATest.pm: a test prefs file was created, but spamd isn't reading it\n";
}
my $spamd_stdout = "log/$testname-spamd.out";
my $spamd_stderr = "log/$testname-spamd.err";
my $spamd_stdlog = "log/$testname-spamd.log";
my $spamd_forker = $ENV{'SPAMD_FORKER'} ?
$ENV{'SPAMD_FORKER'} :
$RUNNING_ON_WINDOWS ?
"start $perl_path" :
$perl_path;
my $spamd_cmd = join(' ',
$spamd_forker,
qq{SATest.pl},
qq{-Mredirect},
qq{-o${spamd_stdout}},
qq{-O${spamd_stderr}},
qq{--},
@spamd_args,
$spamd_extra_args,
qq{&},
);
unlink ($spamd_stdout, $spamd_stderr, $spamd_stdlog);
print ("\t${spamd_cmd}\n");
system ($spamd_cmd);
# now find the PID
$spamd_pid = 0;
# note that the wait period increases the longer it takes,
# 20 retries works out to a total of 60 seconds
my $retries = 20;
my $wait = 0;
while ($spamd_pid <= 0) {
my $spamdlog = '';
if (open (IN, "<${spamd_stderr}")) {
while (<IN>) {
# Yes, DO retry on this error. I'm getting test failures otherwise
# /Address already in use/ and $retries = 0;
/server pid: (\d+)/ and $spamd_pid = $1;
$spamdlog .= $_;
}
close IN;
last if ($spamd_pid);
}
sleep (int($wait++ / 4) + 1) if $retries > 0;
if ($retries-- <= 0) {
warn "spamd start failed: log: $spamdlog";
warn "\n\nMaybe you need to kill a running spamd process?\n\n";
return 0;
}
}
1;
}
sub stop_spamd {
return 0 if ( defined($spamd_already_killed) || $SKIP_SPAMD_TESTS);
$spamd_pid ||= 0;
if ( $spamd_pid <= 1) {
print ("Invalid spamd pid: $spamd_pid. Spamd not started/crashed?\n");
return 0;
} else {
my $killed = kill (15, $spamd_pid);
print ("Killed $killed spamd instances\n");
# wait for it to exit, before returning.
for my $waitfor (0 .. 5) {
my $killstat;
if (($killstat = kill (0, $spamd_pid)) == 0) { last; }
print ("Waiting for spamd at pid $spamd_pid to exit...\n");
sleep 1;
}
$spamd_pid = 0;
$spamd_already_killed = 1;
return $killed;
}
}
sub create_saobj {
my ($args) = shift; # lets you override/add arguments
# YUCK, these file/dir names should be some sort of variable, at
# least we keep their definition in the same file for the moment.
my %setup_args = ( rules_filename => 'log/test_rules_copy',
site_rules_filename => 'log/localrules.tmp',
userprefs_filename => 'log/test_default.cf',
userstate_dir => 'log/user_state',
local_tests_only => 1,
# debug => 'all',
);
# override default args
foreach my $arg (keys %$args) {
$setup_args{$arg} = $args->{$arg};
}
# We'll assume that the test has setup INC correctly
require Mail::SpamAssassin;
my $sa = Mail::SpamAssassin->new(\%setup_args);
return $sa;
}
# ---------------------------------------------------------------------------
sub checkfile {
my $filename = shift;
my $read_sub = shift;
# print "Checking $filename\n";
if (!open (IN, "< log/$filename")) {
warn "cannot open log/$filename"; return undef;
}
&$read_sub();
close IN;
}
# ---------------------------------------------------------------------------
sub pattern_to_re {
my $pat = shift;
$pat = quotemeta($pat);
# make whitespace irrelevant; match any amount as long as the
# non-whitespace chars are OK.
$pat =~ s/\\\s/\\s\*/gs;
$pat;
}
# ---------------------------------------------------------------------------
sub patterns_run_cb {
local ($_);
my $string = shift;
if (defined $string) {
$_ = $string;
} else {
$_ = join ('', <IN>);
}
$matched_output = $_;
# create default names == the pattern itself, if not specified
foreach my $pat (keys %patterns) {
if ($patterns{$pat} eq '') {
$patterns{$pat} = $pat;
}
}
foreach my $pat (sort keys %patterns) {
my $safe = pattern_to_re ($pat);
# print "JMD $patterns{$pat}\n";
if ($_ =~ /${safe}/s) {
$found{$patterns{$pat}}++;
}
}
foreach my $pat (sort keys %anti_patterns) {
my $safe = pattern_to_re ($pat);
# print "JMD $patterns{$pat}\n";
if ($_ =~ /${safe}/s) {
$found_anti{$anti_patterns{$pat}}++;
}
}
}
sub ok_all_patterns {
foreach my $pat (sort keys %patterns) {
my $type = $patterns{$pat};
print "\tChecking $type\n";
if (defined $found{$type}) {
ok ($found{$type} == 1) or warn "Found more than once: $type\n";
} else {
warn "\tNot found: $type = $pat\n";
ok (0); # keep the right # of tests
}
}
foreach my $pat (sort keys %anti_patterns) {
my $type = $anti_patterns{$pat};
print "\tChecking for anti-pattern $type\n";
if (defined $found_anti{$type}) {
warn "\tFound anti-pattern: $type = $pat\n";
ok (0);
}
else
{
ok (1);
}
}
}
sub skip_all_patterns {
my $skip = shift;
foreach my $pat (sort keys %patterns) {
my $type = $patterns{$pat};
print "\tChecking $type\n";
if (defined $found{$type}) {
skip ($skip, $found{$type} == 1) or warn "Found more than once: $type\n";
warn "\tThis test should have been skipped: $skip\n" if $skip;
} else {
if ($skip) {
warn "\tTest skipped: $skip\n";
} else {
warn "\tNot found: $type = $pat\n";
}
skip ($skip, 0); # keep the right # of tests
}
}
foreach my $pat (sort keys %anti_patterns) {
my $type = $anti_patterns{$pat};
print "\tChecking for anti-pattern $type\n";
if (defined $found_anti{$type}) {
warn "\tFound anti-pattern: $type = $pat\n";
skip ($skip, 0);
}
else
{
skip ($skip, 1);
}
}
}
sub clear_pattern_counters {
%found = ();
%found_anti = ();
}
sub read_config {
return if defined($already_read_config);
$already_read_config = 1;
# allow reading config from top-level dir, outside the test suite;
# this is so read_config() will work even when called from
# a "use constant" line at compile time.
my $prefix = '';
if (-f 't/test_dir') { $prefix = "t/"; }
if (!open (CF, "<${prefix}config")) {
if (!open (CF, "<${prefix}config.dist")) { # fall back to defaults
die "cannot open test suite configuration file 'config.dist'";
}
}
while (<CF>) {
s/#.*$//; s/^\s+//; s/\s+$//; next if /^$/;
/^([^=]+)=(.*)$/ or next;
$conf{$1} = $2;
}
close CF;
}
sub conf {
read_config();
return $conf{$_[0]};
}
sub conf_bool {
my $val = conf($_[0]);
return 0 unless defined($val);
return 1 if ($val =~ /^y/i); # y, YES, yes, etc.
return ($val+0) if ($val =~ /^\d/); # 1
return 0; # n or 0
}
sub mk_safe_tmpdir {
return $safe_tmpdir if defined($safe_tmpdir);
my $dir = File::Spec->tmpdir() || 'log';
# be a little paranoid, since we're using a public tmp dir and
# are exposed to race conditions
my $retries = 10;
my $tmp;
while (1) {
$tmp = "$dir/satest.$$.".rand(99999);
if (!-d $tmp && mkdir ($tmp, 0755)) {
if (-d $tmp && -o $tmp) { # check we own it
lstat($tmp);
if (-d _ && -o _) { # double-check, ignoring symlinks
last; # we got it safely
}
}
}
die "cannot get tmp dir, giving up" if ($retries-- < 0);
warn "failed to create tmp dir '$tmp' safely, retrying...";
sleep 1;
}
$safe_tmpdir = $tmp;
return $tmp;
}
sub cleanup_safe_tmpdir {
if ($safe_tmpdir) {
rmtree($safe_tmpdir) or warn "cannot rmtree $safe_tmpdir";
}
}
1;
|