More complete clamdcscan tests

* construct our own Qpsmtpd::Transaction object for testing, so we're sure
   to have a pristine state
 * Move some logic into a should_scan() sub, to help separate scan_all from
   is_not_multipart (since the multipart state of a message has nothing to
   do with any configuration parameters)
This commit is contained in:
Jonathan Hall 2014-09-16 13:48:19 -05:00
parent 8344bf8439
commit 44cafde7d4
2 changed files with 42 additions and 15 deletions

View File

@ -140,7 +140,7 @@ sub register {
if ( ! defined $self->{_args}{deny_viruses} ) { if ( ! defined $self->{_args}{deny_viruses} ) {
$self->{_args}{deny_viruses} = 'yes'; $self->{_args}{deny_viruses} = 'yes';
} }
if ( ! defined $self->{_args}{scan_all} ) { if ( ! $self->{_args}{scan_all} ) {
$self->{_args}{scan_all} = 1; $self->{_args}{scan_all} = 1;
} }
for my $setting (qw( deny_viruses defer_on_error scan_all )) { for my $setting (qw( deny_viruses defer_on_error scan_all )) {
@ -161,8 +161,7 @@ sub data_post_handler {
$self->log(LOGINFO, "skip, naughty"); $self->log(LOGINFO, "skip, naughty");
return (DECLINED); return (DECLINED);
} }
return (DECLINED) if $self->is_too_big($transaction); return DECLINED if ! $self->should_scan($transaction);
return (DECLINED) if $self->is_not_multipart($transaction);
my $clamd = $self->get_clamd() my $clamd = $self->get_clamd()
or return $self->err_and_return("Cannot instantiate ClamAV::Client"); or return $self->err_and_return("Cannot instantiate ClamAV::Client");
@ -328,8 +327,6 @@ sub is_not_multipart {
my $self = shift; my $self = shift;
my $transaction = shift || $self->qp->transaction; my $transaction = shift || $self->qp->transaction;
return if $self->{'_args'}{'scan_all'};
return 1 if !$transaction->header; return 1 if !$transaction->header;
# Ignore non-multipart emails # Ignore non-multipart emails
@ -340,5 +337,14 @@ sub is_not_multipart {
return 1; return 1;
} }
return; return 0;
}
sub should_scan {
my $self = shift;
my $tran = shift;
return 0 if $self->is_too_big($tran);
return 1 if $self->{_args}{scan_all};
return 0 if $self->is_not_multipart($tran);
return 1;
} }

View File

@ -4,6 +4,8 @@ use strict;
use warnings; use warnings;
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
use Qpsmtpd::Transaction;
use Mail::Header;
sub register_tests { sub register_tests {
my $self = shift; my $self = shift;
@ -20,6 +22,7 @@ sub register_tests {
$self->register_test('test_set_permission', 1); $self->register_test('test_set_permission', 1);
$self->register_test('test_is_too_big', 2); $self->register_test('test_is_too_big', 2);
$self->register_test('test_is_not_multipart', 2); $self->register_test('test_is_not_multipart', 2);
$self->register_test('test_should_scan',4);
} }
sub test_register { sub test_register {
@ -64,22 +67,40 @@ sub test_get_clamd {
sub test_is_too_big { sub test_is_too_big {
my $self = shift; my $self = shift;
my $tran = shift || $self->qp->transaction(); my $tran = Qpsmtpd::Transaction->new();
$self->{_args}{max_size} = 8; $self->{_args}{max_size} = 8;
$tran->{_body_size} = (7 * 1024 ); $tran->{_body_size} = 7 * 1024;
ok( ! $self->is_too_big( $tran ), "is_too_big"); ok( ! $self->is_too_big( $tran ), "is_too_big 1");
$tran->{_body_size} = (9 * 1024 ); $tran->{_body_size} = 9 * 1024;
ok( $self->is_too_big( $tran ), "is_too_big"); ok( $self->is_too_big( $tran ), "is_too_big 2");
} }
sub test_is_not_multipart { sub test_is_not_multipart {
my $self = shift; my $self = shift;
my $tran = $self->qp->transaction(); my $tran = Qpsmtpd::Transaction->new();
ok( $self->is_not_multipart(), "not_multipart" ); ok( $self->is_not_multipart($tran), "not_multipart 1" );
$tran->header->add('Content-Type', 'multipart/alternative; boundary="Jx3Wbb8BMHsO=_?:"'); $tran->header( Mail::Header->new( [
ok( ! $self->is_not_multipart(), "not_multipart" ); 'Content-Type: multipart/alternative; boundary="Jx3Wbb8BMHsO=_?:"'
] ) );
ok( ! $self->is_not_multipart($tran), "not_multipart 2" );
} }
sub test_should_scan {
my $self = shift;
my $trans = Qpsmtpd::Transaction->new();
$trans->{_body_size} = 1;
$self->{_args}{scan_all} = 1;
ok( $self->should_scan($trans), "Should scan small message, scan_all=1");
$self->{_args}{scan_all} = 0;
ok( ! $self->should_scan($trans), "Should not scan small message, scan_all=0");
$trans->{_body_size} = 99999999999;
ok( !$self->should_scan($trans), "Should not scan large message" );
$trans->{_body_size} = 1;
$trans->header( Mail::Header->new( [
'Content-Type: multipart/alternative; boundary="Jx3Wbb8BMHsO=_?:"'
] ) );
ok( $self->should_scan($trans), "Should not scan multi-part message" );
}