| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Mail::Header; |
| 7 |
use Qpsmtpd::Constants; |
| 8 |
|
| 9 |
my $r; |
| 10 |
|
| 11 |
sub register_tests { |
| 12 |
my $self = shift; |
| 13 |
|
| 14 |
$self->register_test('test_get_dspam_results', 6); |
| 15 |
$self->register_test('test_log_and_return', 6); |
| 16 |
$self->register_test('test_reject_type', 3); |
| 17 |
} |
| 18 |
|
| 19 |
sub test_log_and_return { |
| 20 |
my $self = shift; |
| 21 |
|
| 22 |
my $transaction = $self->qp->transaction; |
| 23 |
|
| 24 |
# reject not set |
| 25 |
$self->{_args}{reject} = undef; |
| 26 |
$transaction->notes('dspam', { class=> 'Spam', probability => .99, confidence=>1 } ); |
| 27 |
($r) = $self->log_and_return( $transaction ); |
| 28 |
cmp_ok( $r, '==', DECLINED, "($r)"); |
| 29 |
|
| 30 |
# reject exceeded |
| 31 |
$self->{_args}{reject} = .95; |
| 32 |
$transaction->notes('dspam', { class=> 'Spam', probability => .99, confidence=>1 } ); |
| 33 |
($r) = $self->log_and_return( $transaction ); |
| 34 |
cmp_ok( $r, '==', DENY, "($r)"); |
| 35 |
|
| 36 |
# below reject threshold |
| 37 |
$transaction->notes('dspam', { class=> 'Spam', probability => .94, confidence=>1 } ); |
| 38 |
($r) = $self->log_and_return( $transaction ); |
| 39 |
cmp_ok( $r, '==', DECLINED, "($r)"); |
| 40 |
|
| 41 |
# requires agreement |
| 42 |
$self->{_args}{reject} = 'agree'; |
| 43 |
$transaction->notes('spamassassin', { is_spam => 'Yes', score => 25 } ); |
| 44 |
$transaction->notes('dspam', { class=> 'Spam', probability => .90, confidence=>1 } ); |
| 45 |
($r) = $self->log_and_return( $transaction ); |
| 46 |
cmp_ok( $r, '==', DENY, "($r)"); |
| 47 |
|
| 48 |
# requires agreement |
| 49 |
$transaction->notes('spamassassin', { is_spam => 'No', score => 15 } ); |
| 50 |
$transaction->notes('dspam', { class=> 'Spam', probability => .96, confidence=>1 } ); |
| 51 |
($r) = $self->log_and_return( $transaction ); |
| 52 |
cmp_ok( $r, '==', DECLINED, "($r)"); |
| 53 |
|
| 54 |
# requires agreement |
| 55 |
$transaction->notes('spamassassin', { is_spam => 'Yes', score => 10 } ); |
| 56 |
$transaction->notes('dspam', { class=> 'Innocent', probability => .96, confidence=>1 } ); |
| 57 |
($r) = $self->log_and_return( $transaction ); |
| 58 |
cmp_ok( $r, '==', DECLINED, "($r)"); |
| 59 |
}; |
| 60 |
|
| 61 |
sub test_get_dspam_results { |
| 62 |
my $self = shift; |
| 63 |
|
| 64 |
my $transaction = $self->qp->transaction; |
| 65 |
my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE"); |
| 66 |
$transaction->header( $header ); |
| 67 |
|
| 68 |
my @dspam_sample_headers = ( |
| 69 |
'Innocent, probability=0.0000, confidence=0.69', |
| 70 |
'Innocent, probability=0.0000, confidence=0.85', |
| 71 |
'Innocent, probability=0.0023, confidence=1.00', |
| 72 |
'Spam, probability=1.0000, confidence=0.87', |
| 73 |
'Spam, probability=1.0000, confidence=0.99', |
| 74 |
'Whitelisted', |
| 75 |
); |
| 76 |
|
| 77 |
foreach my $header ( @dspam_sample_headers ) { |
| 78 |
$transaction->header->delete('X-DSPAM-Result'); |
| 79 |
$transaction->header->add('X-DSPAM-Result', $header); |
| 80 |
my $r = $self->get_dspam_results($transaction); |
| 81 |
ok( ref $r, "r: ($header)" ); |
| 82 |
#warn Data::Dumper::Dumper($r); |
| 83 |
}; |
| 84 |
}; |
| 85 |
|
| 86 |
sub test_reject_type { |
| 87 |
my $self = shift; |
| 88 |
|
| 89 |
$self->{_args}{reject_type} = undef; |
| 90 |
cmp_ok( $self->get_reject_type(), '==', DENY, "default"); |
| 91 |
|
| 92 |
$self->{_args}{reject_type} = 'temp'; |
| 93 |
cmp_ok( $self->get_reject_type(), '==', DENYSOFT, "defer"); |
| 94 |
|
| 95 |
$self->{_args}{reject_type} = 'disconnect'; |
| 96 |
cmp_ok( $self->get_reject_type(), '==', DENY_DISCONNECT, "disconnect"); |
| 97 |
}; |