Mailing List Archive

svn commit: r1901956 - in /spamassassin/trunk: lib/Mail/SpamAssassin/Plugin/Pyzor.pm lib/Mail/SpamAssassin/Plugin/Razor2.pm lib/Mail/SpamAssassin/Util.pm t/SATest.pm t/pyzor.t
Author: sidney
Date: Wed Jun 15 23:10:44 2022
New Revision: 1901956

URL: http://svn.apache.org/viewvc?rev=1901956&view=rev
Log:
bug 8011 - Fix Pyzor and Razor tests and various code that supports them for use in Windows

Modified:
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Pyzor.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm
spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm
spamassassin/trunk/t/SATest.pm
spamassassin/trunk/t/pyzor.t

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Pyzor.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Pyzor.pm?rev=1901956&r1=1901955&r2=1901956&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Pyzor.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Pyzor.pm Wed Jun 15 23:10:44 2022
@@ -37,7 +37,7 @@ package Mail::SpamAssassin::Plugin::Pyzo
use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::Logger;
use Mail::SpamAssassin::Timeout;
-use Mail::SpamAssassin::Util qw(untaint_var untaint_file_path
+use Mail::SpamAssassin::Util qw(untaint_var untaint_file_path am_running_on_windows
proc_status_ok exit_status_str force_die);
use strict;
use warnings;
@@ -324,8 +324,7 @@ sub check_pyzor {

if (!$self->{main}->{conf}->{pyzor_fork}) {
my @results = $self->pyzor_lookup($pms);
- $self->_check_result($pms, \@results);
- return 0; # _check_result calls got_hit()
+ return $self->_check_result($pms, \@results);
}

## forking method
@@ -357,7 +356,7 @@ sub check_pyzor {
$SIG{$_} = sub {
eval { dbg("pyzor: child process $$ caught signal $_[0]"); };
force_die(6); # avoid END and destructor processing
- } foreach qw(INT HUP TERM TSTP QUIT USR1 USR2);
+ } foreach am_running_on_windows()?qw(INT HUP TERM QUIT):qw(INT HUP TERM TSTP QUIT USR1 USR2);
dbg("pyzor: child process $$ forked");
$pms->{pyzor_backchannel}->setup_backchannel_child_post_fork();
my @results = $self->pyzor_lookup($pms);
@@ -513,7 +512,7 @@ sub _check_forked_result {
dbg("pyzor: child process $kid_pid finished, reading results");

my $backmsg;
- my $ret = sysread($pms->{pyzor_backchannel}->{latest_kid_fh}, $backmsg, PIPE_BUF);
+ my $ret = sysread($pms->{pyzor_backchannel}->{latest_kid_fh}, $backmsg, am_running_on_windows()?512:PIPE_BUF);
if (!defined $ret || $ret == 0) {
dbg("pyzor: could not read result from child: ".($ret == 0 ? 0 : $!));
delete $pms->{pyzor_backchannel};
@@ -591,10 +590,10 @@ sub _check_result {
if ($conf->{pyzor_fork}) {
# forked needs to run got_hit()
$pms->got_hit($pms->{pyzor_rulename}, "", ruletype => 'eval');
+ return 0;
}
return 1;
}
-
return 0;
}


Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm?rev=1901956&r1=1901955&r2=1901956&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/Razor2.pm Wed Jun 15 23:10:44 2022
@@ -44,7 +44,7 @@ use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::Logger;
use Mail::SpamAssassin::Timeout;
use Mail::SpamAssassin::SubProcBackChannel;
-use Mail::SpamAssassin::Util qw(force_die);
+use Mail::SpamAssassin::Util qw(force_die am_running_on_windows);
use strict;
use warnings;
# use bytes;
@@ -470,7 +470,7 @@ sub check_razor2 {
$SIG{$_} = sub {
eval { dbg("razor2: child process $$ caught signal $_[0]"); };
force_die(6); # avoid END and destructor processing
- } foreach qw(INT HUP TERM TSTP QUIT USR1 USR2);
+ } foreach am_running_on_windows()?qw(INT HUP TERM QUIT):qw(INT HUP TERM TSTP QUIT USR1 USR2);
dbg("razor2: child process $$ forked");
$pms->{razor2_backchannel}->setup_backchannel_child_post_fork();
(undef, my @results) =
@@ -554,7 +554,7 @@ sub _check_forked_result {
dbg("razor2: child process $kid_pid finished, reading results");

my $backmsg;
- my $ret = sysread($pms->{razor2_backchannel}->{latest_kid_fh}, $backmsg, PIPE_BUF);
+ my $ret = sysread($pms->{razor2_backchannel}->{latest_kid_fh}, $backmsg, am_running_on_windows()?512:PIPE_BUF);
if (!defined $ret || $ret == 0) {
dbg("razor2: could not read result from child: ".($ret == 0 ? 0 : $!));
delete $pms->{razor2_backchannel};

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm?rev=1901956&r1=1901955&r2=1901956&view=diff
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Util.pm Wed Jun 15 23:10:44 2022
@@ -146,24 +146,32 @@ $have_libidn||$have_libidn2
sub find_executable_in_env_path {
my ($filename) = @_;

- if (RUNNING_ON_WINDOWS) {
- # Look for .exe on Windows
- $filename .= '.exe' unless $filename =~ /\.exe$/i;
- }
-
clean_path_in_taint_mode();
if ( !$displayed_path++ ) {
dbg("util: current PATH is: ".join($Config{'path_sep'},File::Spec->path()));
}
+
+ my @pathext = ('');
+ if (RUNNING_ON_WINDOWS) {
+ if ( $ENV{PATHEXT} ) {
+ push @pathext, split($Config{'path_sep'}, $ENV{PATHEXT});
+ } else {
+ push @pathext, qw{.exe .com .bat};
+ }
+ }
+
foreach my $path (File::Spec->path()) {
- my $fname = File::Spec->catfile ($path, $filename);
- if ( -f $fname ) {
- if (-x $fname) {
- dbg("util: executable for $filename was found at $fname");
- return $fname;
- }
- else {
- dbg("util: $filename was found at $fname, but isn't executable");
+ my $base = File::Spec->catfile ($path, $filename);
+ for my $ext ( @pathext ) {
+ my $fname = $base.$ext;
+ if ( -f $fname ) {
+ if (-x $fname) {
+ dbg("util: executable for $filename was found at $fname");
+ return $fname;
+ }
+ else {
+ dbg("util: $filename was found at $fname, but isn't executable");
+ }
}
}
}
@@ -185,12 +193,13 @@ $have_libidn||$have_libidn2
dbg("util: taint mode: deleting unsafe environment variables, resetting PATH");

if (RUNNING_ON_WINDOWS) {
- dbg("util: running on Win32, skipping PATH cleaning");
- return;
+ if ( $ENV{'PATHEXT'} ) { # clean and untaint
+ $ENV{'PATHEXT'} = join($Config{'path_sep'}, grep ($_, map( {$_ =~ m/^(\.[a-zA-Z]{1,10})$/; $1; } split($Config{'path_sep'}, $ENV{'PATHEXT'}))));
+ }
+ } else {
+ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
}

- delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
-
# Go through and clean the PATH out
my @path;
my @stat;
@@ -215,8 +224,8 @@ $have_libidn||$have_libidn2
dbg("util: PATH included '$dir', which isn't a directory, dropping");
next;
}
- elsif (($stat[2]&2) != 0) {
- # World-Writable directories are considered insecure.
+ elsif (!RUNNING_ON_WINDOWS && (($stat[2]&2) != 0)) {
+ # World-Writable directories are considered insecure, but unavoidable on Windows
# We could be more paranoid and check all of the parent directories as well,
# but it's good for now.
dbg("util: PATH included '$dir', which is world writable, dropping");
@@ -1929,6 +1938,7 @@ sub helper_app_pipe_open_windows {
my ($fh, $stdinfile, $duperr2out, @cmdline) = @_;

# use a traditional open(FOO, "cmd |")
+ $cmdline[0] = '"'.$cmdline[0].'"' if ($cmdline[0] !~ /^\".*\"$/);
my $cmd = join(' ', @cmdline);
if ($stdinfile) { $cmd .= qq/ < "$stdinfile"/; }
if ($duperr2out) {

Modified: spamassassin/trunk/t/SATest.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/SATest.pm?rev=1901956&r1=1901955&r2=1901956&view=diff
==============================================================================
--- spamassassin/trunk/t/SATest.pm (original)
+++ spamassassin/trunk/t/SATest.pm Wed Jun 15 23:10:44 2022
@@ -74,13 +74,14 @@ BEGIN {
my @pathdirs = split(';', $ENV{'PATH'});
$ENV{'PATH'} =
join(';', # filter for only dirs that are canonical absolute paths that exist
- map {
- my $pathdir = $_;
- File::Spec->canonpath(Cwd::realpath($pathdir)) =~ /^(.*)\z/s;
- my $abspathdir = $1; # untaint it
- ((lc $pathdir eq lc $abspathdir) and (-d $abspathdir))?($abspathdir):()
- }
- @pathdirs);
+ map {
+ my $pathdir = $_;
+ $pathdir =~ s/\\*\z//;
+ File::Spec->canonpath(Cwd::realpath($pathdir)) =~ /^(.*)\z/s;
+ my $abspathdir = $1; # untaint it
+ ((lc $pathdir eq lc $abspathdir) and (-d $abspathdir))?($abspathdir):()
+ }
+ @pathdirs);
}

# Fix INC to point to absolute path of built SA
@@ -135,6 +136,9 @@ sub sa_t_init {
$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';

+ # To work in Windows, the perl scripts have to be launched by $perl_cmd and
+ # the ones that are exe files have to be directly called in the command lines
+
$scr = $ENV{'SPAMASSASSIN_SCRIPT'};
$scr ||= "$perl_cmd ../spamassassin.raw";

@@ -148,10 +152,10 @@ sub sa_t_init {
$salearn ||= "$perl_cmd ../sa-learn.raw";

$saawl = $ENV{'SAAWL_SCRIPT'};
- $saawl ||= "../sa-awl";
+ $saawl ||= "$perl_cmd ../sa-awl";

$sacheckspamd = $ENV{'SACHECKSPAMD_SCRIPT'};
- $sacheckspamd ||= "../sa-check_spamd";
+ $sacheckspamd ||= "$perl_cmd ../sa-check_spamd";

$spamdlocalhost = $ENV{'SPAMD_LOCALHOST'};
if (!$spamdlocalhost) {

Modified: spamassassin/trunk/t/pyzor.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/pyzor.t?rev=1901956&r1=1901955&r2=1901956&view=diff
==============================================================================
--- spamassassin/trunk/t/pyzor.t (original)
+++ spamassassin/trunk/t/pyzor.t Wed Jun 15 23:10:44 2022
@@ -3,7 +3,8 @@
use lib '.'; use lib 't';
use SATest; sa_t_init("pyzor");

-use constant HAS_PYZOR => eval { $_ = untaint_cmd("which pyzor"); chomp; -x };
+use Mail::SpamAssassin::Util;
+use constant HAS_PYZOR => Mail::SpamAssassin::Util::find_executable_in_env_path('pyzor');

use Test::More;
plan skip_all => "Net tests disabled" unless conf_bool('run_net_tests');
@@ -30,10 +31,11 @@ tstprefs ("
q{ 3.3 PYZOR_CHECK }, 'spam',
);

-sarun ("-t < data/spam/pyzor", \&patterns_run_cb);
+# Windows cmd doesn't recognize ' character
+sarun ("--cf=\"pyzor_fork 0\" -t < data/spam/pyzor", \&patterns_run_cb);
ok_all_patterns();
# Same with fork
-sarun ("--cf='pyzor_fork 1' -t < data/spam/pyzor", \&patterns_run_cb);
+sarun ("--cf=\"pyzor_fork 1\" -t < data/spam/pyzor", \&patterns_run_cb);
ok_all_patterns();

#TESTING FOR HAM
@@ -45,9 +47,9 @@ ok_all_patterns();
q{ 3.3 PYZOR_CHECK }, 'nonspam',
);

-sarun ("-D pyzor -t < data/nice/001 2>&1", \&patterns_run_cb);
+sarun ("-D pyzor --cf=\"pyzor_fork 0\" -t < data/nice/001 2>&1", \&patterns_run_cb);
ok_all_patterns();
# same with fork
-sarun ("-D pyzor --cf='pyzor_fork 1' -t < data/nice/001 2>&1", \&patterns_run_cb);
+sarun ("-D pyzor --cf=\"pyzor_fork 1\" -t < data/nice/001 2>&1", \&patterns_run_cb);
ok_all_patterns();