qpsmtpd/t/plugin_tests/check_basicheaders
2012-05-20 23:40:39 -07:00

67 lines
2.2 KiB
Perl

#!perl -w
use strict;
use Data::Dumper;
use POSIX qw(strftime);
use Qpsmtpd::Address;
use Qpsmtpd::Constants;
sub register_tests {
my $self = shift;
$self->register_test("test_hook_data_post", 7);
}
sub test_hook_data_post {
my $self = shift;
my $reject = $self->{_args}{reject_type};
my $deny = $reject =~ /^temp|soft$/i ? DENYSOFT : DENY;
my $transaction = $self->qp->transaction;
my $test_email = 'matt@example.com';
my $address = Qpsmtpd::Address->new( "<$test_email>" );
my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime time;
my $future = strftime "%a %b %e %H:%M:%S %Y", localtime time + 518400; #6d
my $past = strftime "%a %b %e %H:%M:%S %Y", localtime time - 518400; #6d
$self->{_args}{days} = 5;
$transaction->sender($address);
$transaction->header($header);
$transaction->header->add('From', "<$test_email>");
$transaction->header->add('Date', $now );
$transaction->body_write( "test message body " );
my ($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( DECLINED, '==', $code, "okay" );
$transaction->header->delete('Date');
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( $deny, '==', $code, "missing date ( $mess )" );
$transaction->header->add('Date', $now );
$transaction->header->delete('From');
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( $deny, '==', $code, "missing from ( $mess )" );
$transaction->header->add('From', "<$test_email>");
$transaction->header->replace('Date', $future );
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( $deny, '==', $code, "too new ( $mess )" );
$transaction->header->replace('Date', $past );
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( $deny, '==', $code, "too old ( $mess )" );
$self->{_args}{reject_type} = 'temp';
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( DENYSOFT, '==', $code, "defer, not deny ( $mess )" );
$self->{_args}{reject_type} = 'perm';
($code, $mess) = $self->hook_data_post( $transaction );
cmp_ok( DENY, '==', $code, "deny ( $mess )" );
};