2012-05-13 05:27:49 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Data::Dumper;
|
2012-05-15 07:19:04 +02:00
|
|
|
use POSIX qw(strftime);
|
2012-05-13 05:27:49 +02:00
|
|
|
|
|
|
|
use Qpsmtpd::Address;
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
2012-05-21 23:30:11 +02:00
|
|
|
my $test_email = 'matt@example.com';
|
2012-05-15 07:19:04 +02:00
|
|
|
|
2012-05-13 05:27:49 +02:00
|
|
|
sub register_tests {
|
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-15 07:19:05 +02:00
|
|
|
$self->register_test("test_hook_data_post", 7);
|
2012-05-21 23:30:11 +02:00
|
|
|
$self->register_test('test_invalid_date_range', 7);
|
2012-05-13 05:27:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 23:30:11 +02:00
|
|
|
sub setup_test_headers {
|
2012-05-13 05:27:49 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $transaction = $self->qp->transaction;
|
|
|
|
my $address = Qpsmtpd::Address->new( "<$test_email>" );
|
|
|
|
my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
|
2012-05-15 07:19:04 +02:00
|
|
|
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime time;
|
2012-05-13 05:27:49 +02:00
|
|
|
|
|
|
|
$transaction->sender($address);
|
|
|
|
$transaction->header($header);
|
|
|
|
$transaction->header->add('From', "<$test_email>");
|
|
|
|
$transaction->header->add('Date', $now );
|
|
|
|
$transaction->body_write( "test message body " );
|
2012-05-21 23:30:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
sub test_invalid_date_range {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime time;
|
|
|
|
ok( ! $self->invalid_date_range($now), "valid +");
|
|
|
|
|
|
|
|
$self->{_args}{future} = 2;
|
|
|
|
|
|
|
|
my $future_6 = strftime "%a %b %e %H:%M:%S %Y", localtime time + 518400; #6d
|
|
|
|
my $r = $self->invalid_date_range( $future_6 );
|
|
|
|
ok( $r, "too new -" );
|
|
|
|
|
|
|
|
my $future_3 = strftime "%a %b %e %H:%M:%S %Y", localtime time + 259200; #3d
|
|
|
|
$r = $self->invalid_date_range( $future_3 );
|
|
|
|
ok( $r, "too new -" );
|
|
|
|
|
|
|
|
my $future_1 = strftime "%a %b %e %H:%M:%S %Y", localtime time + 86400; #1d
|
|
|
|
$r = $self->invalid_date_range( $future_1 );
|
|
|
|
ok( ! $r, "a little new, +" );
|
|
|
|
|
|
|
|
|
|
|
|
$self->{_args}{past} = 2;
|
|
|
|
|
|
|
|
my $past_6 = strftime "%a %b %e %H:%M:%S %Y", localtime time - 518400; #6d
|
|
|
|
$r = $self->invalid_date_range( $past_6 );
|
|
|
|
ok( $r, "too old -" );
|
|
|
|
|
|
|
|
my $past_3 = strftime "%a %b %e %H:%M:%S %Y", localtime time - 259200; #3d
|
|
|
|
$r = $self->invalid_date_range( $past_3 );
|
|
|
|
ok( $r, "too old -" );
|
|
|
|
|
|
|
|
my $past_1 = strftime "%a %b %e %H:%M:%S %Y", localtime time - 86400; #1d
|
|
|
|
$r = $self->invalid_date_range( $past_1 );
|
|
|
|
ok( ! $r, "a little old +" );
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_hook_data_post {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $reject = $self->{_args}{reject_type};
|
|
|
|
my $deny = $reject =~ /^temp|soft$/i ? DENYSOFT : DENY;
|
|
|
|
|
|
|
|
$self->setup_test_headers();
|
|
|
|
my $transaction = $self->qp->transaction;
|
2012-05-13 05:27:49 +02:00
|
|
|
|
|
|
|
my ($code, $mess) = $self->hook_data_post( $transaction );
|
2012-05-21 23:30:11 +02:00
|
|
|
cmp_ok( DECLINED, '==', $code, "okay +" );
|
2012-05-13 05:27:49 +02:00
|
|
|
|
|
|
|
$transaction->header->delete('Date');
|
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
2012-05-15 07:19:04 +02:00
|
|
|
cmp_ok( $deny, '==', $code, "missing date ( $mess )" );
|
2012-05-13 05:27:49 +02:00
|
|
|
|
2012-05-21 23:30:11 +02:00
|
|
|
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime time;
|
2012-05-13 05:27:49 +02:00
|
|
|
$transaction->header->add('Date', $now );
|
2012-05-15 07:19:04 +02:00
|
|
|
$transaction->header->delete('From');
|
2012-05-13 05:27:49 +02:00
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
2012-05-15 07:19:04 +02:00
|
|
|
cmp_ok( $deny, '==', $code, "missing from ( $mess )" );
|
|
|
|
$transaction->header->add('From', "<$test_email>");
|
2012-05-13 05:27:49 +02:00
|
|
|
|
2012-05-21 23:30:11 +02:00
|
|
|
$self->{_args}{future} = 5;
|
|
|
|
my $future = strftime "%a %b %e %H:%M:%S %Y", localtime time + 518400; #6d
|
2012-05-15 07:19:04 +02:00
|
|
|
$transaction->header->replace('Date', $future );
|
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
|
|
|
cmp_ok( $deny, '==', $code, "too new ( $mess )" );
|
2012-05-13 05:27:49 +02:00
|
|
|
|
2012-05-21 23:30:11 +02:00
|
|
|
$self->{_args}{past} = 5;
|
|
|
|
my $past = strftime "%a %b %e %H:%M:%S %Y", localtime time - 518400; #6d
|
2012-05-15 07:19:04 +02:00
|
|
|
$transaction->header->replace('Date', $past );
|
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
|
|
|
cmp_ok( $deny, '==', $code, "too old ( $mess )" );
|
2012-05-13 05:27:49 +02:00
|
|
|
|
|
|
|
$self->{_args}{reject_type} = 'temp';
|
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
|
|
|
cmp_ok( DENYSOFT, '==', $code, "defer, not deny ( $mess )" );
|
2012-05-15 07:19:04 +02:00
|
|
|
|
|
|
|
$self->{_args}{reject_type} = 'perm';
|
|
|
|
($code, $mess) = $self->hook_data_post( $transaction );
|
|
|
|
cmp_ok( DENY, '==', $code, "deny ( $mess )" );
|
2012-05-13 05:27:49 +02:00
|
|
|
};
|