| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Qpsmtpd::Address; |
| 7 |
use Qpsmtpd::Constants; |
| 8 |
|
| 9 |
my $test_email = '[email protected]'; |
| 10 |
|
| 11 |
my @greydbs = qw( denysoft_greylist.dbm denysoft_greylist.dbm.lock ); |
| 12 |
foreach ( @greydbs ) { |
| 13 |
unlink $_ if -f $_; |
| 14 |
}; |
| 15 |
|
| 16 |
sub register_tests { |
| 17 |
my $self = shift; |
| 18 |
|
| 19 |
$self->register_test('test_hook_data', 4); |
| 20 |
$self->register_test('test_get_db_key', 4); |
| 21 |
$self->register_test('test_get_db_location', 1); |
| 22 |
$self->register_test("test_greylist_geoip", 7); |
| 23 |
$self->register_test("test_greylist_p0f_genre", 2); |
| 24 |
$self->register_test("test_greylist_p0f_distance", 2); |
| 25 |
$self->register_test("test_greylist_p0f_link", 2); |
| 26 |
$self->register_test("test_greylist_p0f_uptime", 2); |
| 27 |
} |
| 28 |
|
| 29 |
sub test_hook_data { |
| 30 |
my $self = shift; |
| 31 |
my $transaction = $self->qp->transaction; |
| 32 |
|
| 33 |
my ($code, $mess) = $self->hook_data( $transaction ); |
| 34 |
cmp_ok( $code, '==', DECLINED, "no note" ); |
| 35 |
|
| 36 |
$transaction->notes('greylist', 1); |
| 37 |
|
| 38 |
($code, $mess) = $self->hook_data( $transaction ); |
| 39 |
cmp_ok( $code, '==', DECLINED, "no recipients"); |
| 40 |
|
| 41 |
my $address = Qpsmtpd::Address->new( "<$test_email>" ); |
| 42 |
$transaction->recipients( $address ); |
| 43 |
|
| 44 |
$transaction->notes('whitelistrcpt', 2); |
| 45 |
($code, $mess) = $self->hook_data( $transaction ); |
| 46 |
cmp_ok( $code, '==', DENYSOFT, "missing recipients"); |
| 47 |
|
| 48 |
$transaction->notes('whitelistrcpt', 1); |
| 49 |
($code, $mess) = $self->hook_data( $transaction ); |
| 50 |
cmp_ok( $code, '==', DECLINED, "missing recipients"); |
| 51 |
}; |
| 52 |
|
| 53 |
sub test_get_db_key { |
| 54 |
my $self = shift; |
| 55 |
|
| 56 |
$self->{_args}{sender} = 0; |
| 57 |
$self->{_args}{recipient} = 0; |
| 58 |
$self->{_args}{remote_ip} = 0; |
| 59 |
|
| 60 |
my $test_ip = '192.168.1.1'; |
| 61 |
|
| 62 |
my $address = Qpsmtpd::Address->new( "<$test_email>" ); |
| 63 |
$self->qp->transaction->sender( $address ); |
| 64 |
$self->qp->transaction->add_recipient( $address ); |
| 65 |
$self->qp->connection->remote_ip($test_ip); |
| 66 |
|
| 67 |
my $key = $self->get_db_key(); |
| 68 |
ok( ! $key, "db key empty: -"); |
| 69 |
|
| 70 |
$self->{_args}{remote_ip} = 1; |
| 71 |
$key = $self->get_db_key( $address, $address ); |
| 72 |
cmp_ok( $key, 'eq', '3232235777', "db key: $key"); |
| 73 |
|
| 74 |
$self->{_args}{sender} = 1; |
| 75 |
$key = $self->get_db_key( $address, $address ); |
| 76 |
cmp_ok( $key, 'eq', "3232235777:$test_email", "db key: $key"); |
| 77 |
|
| 78 |
$self->{_args}{recipient} = 1; |
| 79 |
$key = $self->get_db_key( $address, $address ); |
| 80 |
cmp_ok( $key, 'eq', "3232235777:$test_email:$test_email", "db key: $key"); |
| 81 |
}; |
| 82 |
|
| 83 |
sub test_get_db_location { |
| 84 |
my $self = shift; |
| 85 |
|
| 86 |
my $db = $self->get_db_location(); |
| 87 |
ok( $db, "db location: $db"); |
| 88 |
}; |
| 89 |
|
| 90 |
sub test_greylist_geoip { |
| 91 |
my $self = shift; |
| 92 |
|
| 93 |
$self->{_args}{'geoip'} = 'US,UK,HU'; |
| 94 |
|
| 95 |
my @valid = qw/ US us UK hu /; |
| 96 |
my @invalid = qw/ PK RU ru /; |
| 97 |
|
| 98 |
foreach my $cc ( @valid ) { |
| 99 |
$self->connection->notes('geoip_country', $cc ); |
| 100 |
ok( $self->geoip_match(), "match + ($cc)"); |
| 101 |
}; |
| 102 |
|
| 103 |
foreach my $cc ( @invalid ) { |
| 104 |
$self->connection->notes('geoip_country', $cc ); |
| 105 |
ok( ! $self->geoip_match(), "bad - ($cc)"); |
| 106 |
}; |
| 107 |
}; |
| 108 |
|
| 109 |
sub test_greylist_p0f_genre { |
| 110 |
my $self = shift; |
| 111 |
|
| 112 |
$self->{_args}{'p0f'} = 'genre,Linux'; |
| 113 |
$self->connection->notes('p0f'=> { genre => 'windows', link => 'dsl' } ); |
| 114 |
ok( ! $self->p0f_match(), 'p0f genre miss'); |
| 115 |
|
| 116 |
$self->{_args}{'p0f'} = 'genre,Windows'; |
| 117 |
$self->connection->notes('p0f'=> { genre => 'windows', link => 'dsl' } ); |
| 118 |
ok( $self->p0f_match(), 'p0f genre hit'); |
| 119 |
} |
| 120 |
|
| 121 |
sub test_greylist_p0f_distance { |
| 122 |
my $self = shift; |
| 123 |
|
| 124 |
$self->{_args}{'p0f'} = 'distance,8'; |
| 125 |
$self->connection->notes('p0f'=> { distance=>9 } ); |
| 126 |
ok( $self->p0f_match(), 'p0f distance hit'); |
| 127 |
|
| 128 |
$self->{_args}{'p0f'} = 'distance,8'; |
| 129 |
$self->connection->notes('p0f'=> { distance=>7 } ); |
| 130 |
ok( ! $self->p0f_match(), 'p0f distance miss'); |
| 131 |
} |
| 132 |
|
| 133 |
sub test_greylist_p0f_link { |
| 134 |
my $self = shift; |
| 135 |
|
| 136 |
$self->{_args}{'p0f'} = 'link,dsl'; |
| 137 |
$self->connection->notes('p0f'=> { link=>'DSL' } ); |
| 138 |
ok( $self->p0f_match(), 'p0f link hit'); |
| 139 |
|
| 140 |
$self->{_args}{'p0f'} = 'link,dsl'; |
| 141 |
$self->connection->notes('p0f'=> { link=>'Ethernet' } ); |
| 142 |
ok( ! $self->p0f_match(), 'p0f link miss'); |
| 143 |
} |
| 144 |
|
| 145 |
sub test_greylist_p0f_uptime { |
| 146 |
my $self = shift; |
| 147 |
|
| 148 |
$self->{_args}{'p0f'} = 'uptime,100'; |
| 149 |
$self->connection->notes('p0f'=> { uptime=> 99 } ); |
| 150 |
ok( $self->p0f_match(), 'p0f uptime hit'); |
| 151 |
|
| 152 |
$self->{_args}{'p0f'} = 'uptime,100'; |
| 153 |
$self->connection->notes('p0f'=> { uptime=>500 } ); |
| 154 |
ok( ! $self->p0f_match(), 'p0f uptime miss'); |
| 155 |
} |
| 156 |
|
| 157 |
sub _reset_transaction { |
| 158 |
my $self = shift; |
| 159 |
|
| 160 |
$self->qp->connection->relay_client(0); |
| 161 |
$self->qp->transaction->notes('whitelistsender',0); |
| 162 |
$self->connection->notes('whitelisthost',0); |
| 163 |
$self->qp->transaction->notes('tls_enabled',0); |
| 164 |
$self->{_args}{p0f} = undef; |
| 165 |
$self->{_args}{geoip} = undef; |
| 166 |
}; |
| 167 |
|