2012-05-21 05:04:40 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2010-05-11 07:31:52 +02:00
|
|
|
use Qpsmtpd::Address;
|
2012-05-21 05:04:40 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2010-05-11 07:31:52 +02:00
|
|
|
|
|
|
|
my $test_email = 'user@example.com';
|
|
|
|
|
|
|
|
my @greydbs = qw( denysoft_greylist.dbm denysoft_greylist.dbm.lock );
|
|
|
|
foreach ( @greydbs ) {
|
|
|
|
unlink $_ if -f $_;
|
|
|
|
};
|
|
|
|
|
|
|
|
sub register_tests {
|
|
|
|
my $self = shift;
|
2012-05-21 05:04:40 +02:00
|
|
|
|
2014-09-17 08:16:53 +02:00
|
|
|
$self->register_test('test_hook_data');
|
|
|
|
$self->register_test('test_get_db_key');
|
|
|
|
$self->register_test('test_get_db_location');
|
2014-10-28 20:38:15 +01:00
|
|
|
$self->register_test('test_exclude');
|
2014-09-17 08:16:53 +02:00
|
|
|
$self->register_test("test_greylist_geoip");
|
|
|
|
$self->register_test("test_greylist_p0f_genre");
|
|
|
|
$self->register_test("test_greylist_p0f_distance");
|
|
|
|
$self->register_test("test_greylist_p0f_link");
|
|
|
|
$self->register_test("test_greylist_p0f_uptime");
|
2010-05-11 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
sub test_hook_data {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
2012-05-21 05:04:40 +02:00
|
|
|
my $transaction = $self->qp->transaction;
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
my ($code, $mess) = $self->hook_data( $transaction );
|
|
|
|
cmp_ok( $code, '==', DECLINED, "no note" );
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$transaction->notes('greylist', 1);
|
|
|
|
|
|
|
|
($code, $mess) = $self->hook_data( $transaction );
|
|
|
|
cmp_ok( $code, '==', DECLINED, "no recipients");
|
|
|
|
|
|
|
|
my $address = Qpsmtpd::Address->new( "<$test_email>" );
|
|
|
|
$transaction->recipients( $address );
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$transaction->notes('whitelistrcpt', 2);
|
|
|
|
($code, $mess) = $self->hook_data( $transaction );
|
|
|
|
cmp_ok( $code, '==', DENYSOFT, "missing recipients");
|
|
|
|
|
|
|
|
$transaction->notes('whitelistrcpt', 1);
|
|
|
|
($code, $mess) = $self->hook_data( $transaction );
|
|
|
|
cmp_ok( $code, '==', DECLINED, "missing recipients");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_get_db_key {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{sender} = 0;
|
|
|
|
$self->{_args}{recipient} = 0;
|
|
|
|
$self->{_args}{remote_ip} = 0;
|
|
|
|
|
|
|
|
my $test_ip = '192.168.1.1';
|
|
|
|
|
|
|
|
my $address = Qpsmtpd::Address->new( "<$test_email>" );
|
2010-05-11 07:31:52 +02:00
|
|
|
$self->qp->transaction->sender( $address );
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->qp->transaction->add_recipient( $address );
|
|
|
|
$self->qp->connection->remote_ip($test_ip);
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
my $key = $self->get_db_key();
|
|
|
|
ok( ! $key, "db key empty: -");
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{remote_ip} = 1;
|
|
|
|
$key = $self->get_db_key( $address, $address );
|
|
|
|
cmp_ok( $key, 'eq', '3232235777', "db key: $key");
|
|
|
|
|
|
|
|
$self->{_args}{sender} = 1;
|
|
|
|
$key = $self->get_db_key( $address, $address );
|
|
|
|
cmp_ok( $key, 'eq', "3232235777:$test_email", "db key: $key");
|
|
|
|
|
|
|
|
$self->{_args}{recipient} = 1;
|
|
|
|
$key = $self->get_db_key( $address, $address );
|
|
|
|
cmp_ok( $key, 'eq', "3232235777:$test_email:$test_email", "db key: $key");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_get_db_location {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
my $db = $self->get_db_location();
|
|
|
|
ok( $db, "db location: $db");
|
|
|
|
};
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2014-10-28 20:38:15 +01:00
|
|
|
sub test_exclude {
|
|
|
|
my ( $self ) = @_;
|
|
|
|
|
|
|
|
$self->connection->relay_client(1);
|
|
|
|
ok( $self->exclude(), "Relay client results in exclude() hit" );
|
|
|
|
$self->connection->relay_client(0);
|
|
|
|
ok( ! $self->exclude(), "Non-relay client results in exclude() miss" );
|
|
|
|
};
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
sub test_greylist_geoip {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{_args}{'geoip'} = 'US,UK,HU';
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
my @valid = qw/ US us UK hu /;
|
|
|
|
my @invalid = qw/ PK RU ru /;
|
|
|
|
|
|
|
|
foreach my $cc ( @valid ) {
|
|
|
|
$self->connection->notes('geoip_country', $cc );
|
|
|
|
ok( $self->geoip_match(), "match + ($cc)");
|
2014-10-28 20:38:15 +01:00
|
|
|
ok( $self->exclude(), "match + ($cc) results in exclude() hit");
|
2012-05-21 05:04:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
foreach my $cc ( @invalid ) {
|
|
|
|
$self->connection->notes('geoip_country', $cc );
|
|
|
|
ok( ! $self->geoip_match(), "bad - ($cc)");
|
2014-10-28 20:38:15 +01:00
|
|
|
ok( ! $self->exclude(), "miss - ($cc) results in exclude() miss");
|
2012-05-21 05:04:40 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_greylist_p0f_genre {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'genre,Linux';
|
|
|
|
$self->connection->notes('p0f'=> { genre => 'windows', link => 'dsl' } );
|
|
|
|
ok( ! $self->p0f_match(), 'p0f genre miss');
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'genre,Windows';
|
|
|
|
$self->connection->notes('p0f'=> { genre => 'windows', link => 'dsl' } );
|
|
|
|
ok( $self->p0f_match(), 'p0f genre hit');
|
2010-05-11 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
sub test_greylist_p0f_distance {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'distance,8';
|
|
|
|
$self->connection->notes('p0f'=> { distance=>9 } );
|
|
|
|
ok( $self->p0f_match(), 'p0f distance hit');
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'distance,8';
|
|
|
|
$self->connection->notes('p0f'=> { distance=>7 } );
|
|
|
|
ok( ! $self->p0f_match(), 'p0f distance miss');
|
2010-05-11 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
sub test_greylist_p0f_link {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'link,dsl';
|
|
|
|
$self->connection->notes('p0f'=> { link=>'DSL' } );
|
|
|
|
ok( $self->p0f_match(), 'p0f link hit');
|
2014-10-28 20:38:15 +01:00
|
|
|
ok( ! $self->exclude(), 'p0f link hit results in exclude() miss' );
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'link,dsl';
|
|
|
|
$self->connection->notes('p0f'=> { link=>'Ethernet' } );
|
|
|
|
ok( ! $self->p0f_match(), 'p0f link miss');
|
2014-10-28 20:38:15 +01:00
|
|
|
ok( $self->exclude(), 'p0f link miss results in exclude() hit' );
|
2010-05-11 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
sub test_greylist_p0f_uptime {
|
2010-05-11 07:31:52 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'uptime,100';
|
|
|
|
$self->connection->notes('p0f'=> { uptime=> 99 } );
|
|
|
|
ok( $self->p0f_match(), 'p0f uptime hit');
|
2010-05-11 07:31:52 +02:00
|
|
|
|
2012-05-21 05:04:40 +02:00
|
|
|
$self->{_args}{'p0f'} = 'uptime,100';
|
|
|
|
$self->connection->notes('p0f'=> { uptime=>500 } );
|
|
|
|
ok( ! $self->p0f_match(), 'p0f uptime miss');
|
2010-05-11 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:04:41 +02:00
|
|
|
sub _reset_transaction {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->relay_client(0);
|
|
|
|
$self->qp->transaction->notes('whitelistsender',0);
|
2012-06-02 06:43:37 +02:00
|
|
|
$self->connection->notes('whitelisthost',0);
|
2012-05-21 05:04:41 +02:00
|
|
|
$self->qp->transaction->notes('tls_enabled',0);
|
|
|
|
$self->{_args}{p0f} = undef;
|
|
|
|
$self->{_args}{geoip} = undef;
|
|
|
|
};
|
2010-05-11 07:31:52 +02:00
|
|
|
|