2012-05-06 08:01:00 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Mail::Header;
|
|
|
|
use Qpsmtpd::Address;
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
|
|
|
my @sample_headers = (
|
|
|
|
'No, score=-5.4 required=4.0 autolearn=ham',
|
|
|
|
'No, score=-8.2 required=4.0 autolearn=ham',
|
|
|
|
'No, score=-102.3 required=4.0 autolearn=disabled',
|
|
|
|
'No, score=-0.1 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RDNS_NONE autolearn=no version=3.3.2',
|
|
|
|
'No, score=4.4 required=5.0 autolearn=no',
|
|
|
|
'Yes, score=14.3 required=5.0 autolearn=no',
|
|
|
|
'Yes, score=18.3 required=5.0 autolearn=spam',
|
|
|
|
'Yes, score=26.6 required=4.0 autolearn=unavailable',
|
|
|
|
'No, score=-1.7 required=4.0 autolearn=unavailable version=3.3.2',
|
|
|
|
'No, hits=-1.0 required=4.0 autolearn=unavailable version=3.3.2',
|
|
|
|
);
|
|
|
|
|
|
|
|
sub register_tests {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-09-17 08:16:53 +02:00
|
|
|
$self->register_test('test_connect_to_spamd');
|
|
|
|
$self->register_test('test_parse_spam_header');
|
|
|
|
$self->register_test('test_get_spam_results');
|
|
|
|
$self->register_test('test_munge_subject');
|
|
|
|
$self->register_test('test_reject');
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub test_connect_to_spamd {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
$transaction->add_recipient( Qpsmtpd::Address->new( '<user@example.com>' ) );
|
|
|
|
my $username = $self->select_spamd_username( $transaction );
|
|
|
|
my $message = $self->test_message();
|
|
|
|
my $length = length $message;
|
|
|
|
|
|
|
|
# Try a unix socket
|
|
|
|
$self->{_args}{spamd_socket} = '/var/run/spamd/spamd.socket';
|
|
|
|
my $SPAMD = $self->connect_to_spamd();
|
|
|
|
if ( $SPAMD ) {
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( $SPAMD, "socket");
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->print_to_spamd( $SPAMD, $message, $length, $username );
|
|
|
|
shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done)
|
|
|
|
my $headers = $self->parse_spamd_response( $SPAMD );
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( $headers, "socket response\n");
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( 1 == 1, "socket connect FAILED");
|
|
|
|
ok( 1 == 1, "socket response FAILED");
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# Try a TCP/IP connection
|
|
|
|
$self->{_args}{spamd_socket} = '127.0.0.1:783';
|
|
|
|
$SPAMD = $self->connect_to_spamd();
|
|
|
|
if ( $SPAMD ) {
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( $SPAMD, "tcp/ip");
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->print_to_spamd( $SPAMD, $message, $length, $username );
|
|
|
|
shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done)
|
|
|
|
my $headers = $self->parse_spamd_response( $SPAMD );
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( $headers, "tcp/ip response\n");
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-06-23 00:29:28 +02:00
|
|
|
ok( 1 == 1, "tcp/ip connect FAILED");
|
|
|
|
ok( 1 == 1, "tcp/ip response FAILED");
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
sub test_reject {
|
2012-05-06 08:01:00 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
$self->setup_headers();
|
|
|
|
|
|
|
|
# message scored a 10, should pass
|
|
|
|
$self->{_args}{reject} = 12;
|
2012-05-11 07:50:08 +02:00
|
|
|
$transaction->notes('spamassassin', { is_spam => 'Yes', score => 10 } );
|
2012-06-23 00:29:28 +02:00
|
|
|
my $r = $self->reject($transaction);
|
|
|
|
cmp_ok( DECLINED, '==', $r, "r: $r");
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
# message scored a 15, should fail
|
|
|
|
$self->{_args}{reject} = 12;
|
2012-05-11 07:50:08 +02:00
|
|
|
$transaction->notes('spamassassin', { is_spam => 'Yes', score => 15 } );
|
2012-06-23 00:29:28 +02:00
|
|
|
($r) = $self->reject($transaction);
|
|
|
|
cmp_ok( DENY, '==', $r, "r: $r");
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
sub test_munge_subject {
|
2012-05-06 08:01:00 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
$self->setup_headers();
|
|
|
|
my $subject = 'DSPAM smells better than SpamAssassin';
|
|
|
|
|
|
|
|
$self->{_args}{munge_subject_threshold} = 5;
|
|
|
|
$transaction->notes('spamassassin', { score => 6 } );
|
|
|
|
$transaction->header->add('Subject', $subject);
|
2012-06-23 00:29:28 +02:00
|
|
|
$self->munge_subject($transaction);
|
2012-05-06 08:01:00 +02:00
|
|
|
my $r = $transaction->header->get('Subject'); chomp $r;
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok($r, 'eq', "*** SPAM *** $subject", "+");
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
$transaction->header->delete('Subject'); # cleanup
|
|
|
|
$self->{_args}{munge_subject_threshold} = 5;
|
|
|
|
$transaction->notes('spamassassin', { score => 3 } );
|
|
|
|
$transaction->header->add('Subject', $subject);
|
2012-06-23 00:29:28 +02:00
|
|
|
$self->munge_subject($transaction);
|
2012-05-06 08:01:00 +02:00
|
|
|
$r = $transaction->header->get('Subject'); chomp $r;
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok($r, 'eq', $subject, "-");
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
$transaction->header->delete('Subject'); # cleanup
|
|
|
|
$transaction->notes('spamassassin', { score => 3, required => 4 } );
|
|
|
|
$transaction->header->add('Subject', $subject);
|
2012-06-23 00:29:28 +02:00
|
|
|
$self->munge_subject($transaction);
|
2012-05-06 08:01:00 +02:00
|
|
|
$r = $transaction->header->get('Subject'); chomp $r;
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok($r, 'eq', $subject, "-");
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
$transaction->header->delete('Subject'); # cleanup
|
|
|
|
$transaction->notes('spamassassin', { score => 5, required => 4 } );
|
|
|
|
$transaction->header->add('Subject', $subject);
|
2012-06-23 00:29:28 +02:00
|
|
|
$self->munge_subject($transaction);
|
2012-05-06 08:01:00 +02:00
|
|
|
$r = $transaction->header->get('Subject'); chomp $r;
|
2014-05-15 16:45:33 +02:00
|
|
|
cmp_ok($r, 'eq', "$subject", "+");
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
sub test_get_spam_results {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
$self->setup_headers();
|
|
|
|
|
|
|
|
foreach my $h ( @sample_headers ) {
|
|
|
|
$transaction->notes('spamassassin', undef); # empty cache
|
|
|
|
$transaction->header->delete('X-Spam-Status'); # delete previous header
|
|
|
|
$transaction->header->add('X-Spam-Status', $h);
|
|
|
|
my $r_ref = $self->get_spam_results($transaction);
|
|
|
|
if ( $h =~ /hits=/ ) {
|
|
|
|
$r_ref->{hits} = delete $r_ref->{score}; # SA v2 compat
|
|
|
|
};
|
|
|
|
my $r2 = _reassemble_header($r_ref);
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok( $h, 'eq', $r2, $h );
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
# this time it should be cached
|
|
|
|
$r_ref = $self->get_spam_results($transaction);
|
2012-06-23 00:29:28 +02:00
|
|
|
if ( $h =~ /hits=/ ) {
|
|
|
|
ok( 1 );
|
|
|
|
next;
|
|
|
|
}; # caching is broken for SA v2 headers
|
2012-05-06 08:01:00 +02:00
|
|
|
$r2 = _reassemble_header($r_ref);
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok( $h, 'eq', $r2, $h );
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_parse_spam_header {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
foreach my $h ( @sample_headers ) {
|
|
|
|
my $r_ref = $self->parse_spam_header($h);
|
2012-06-23 00:29:28 +02:00
|
|
|
if ( $h =~ /hits=/ ) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$r_ref->{hits} = delete $r_ref->{score}; # SA v2 compat
|
|
|
|
};
|
|
|
|
my $r2 = _reassemble_header($r_ref);
|
2012-06-23 00:29:28 +02:00
|
|
|
cmp_ok( $h, 'eq', $r2, $h );
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
sub setup_headers {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
|
|
|
|
$transaction->header( $header );
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_message {
|
|
|
|
return <<'EO_MESSAGE'
|
|
|
|
To: Fictitious User <fict@example.com>
|
|
|
|
From: No Such <such@example.com>
|
2012-06-23 00:29:28 +02:00
|
|
|
Subject: jose can you see, by the dawns early light?
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
What so proudly we.
|
|
|
|
EO_MESSAGE
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
sub _reassemble_header {
|
|
|
|
my $info_ref = shift;
|
|
|
|
my $string = $info_ref->{'is_spam'};
|
|
|
|
$string .= ",";
|
|
|
|
foreach ( qw/ hits score required tests autolearn version / ) {
|
|
|
|
next if ! defined $info_ref->{$_};
|
|
|
|
$string .= " $_=$info_ref->{$_}";
|
|
|
|
};
|
|
|
|
return $string;
|
|
|
|
};
|
|
|
|
|