| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Data::Dumper; |
| 7 |
use Net::DNS; |
| 8 |
use Qpsmtpd::Address; |
| 9 |
use Qpsmtpd::Constants; |
| 10 |
|
| 11 |
my $res = new Net::DNS::Resolver(dnsrch => 0); |
| 12 |
my $test_email = '[email protected]'; |
| 13 |
|
| 14 |
sub register_tests { |
| 15 |
my $self = shift; |
| 16 |
|
| 17 |
my %args = ( ); |
| 18 |
$self->register( $self->qp, reject => 0 ); |
| 19 |
|
| 20 |
$self->register_test('test_populate_invalid_networks', 2); |
| 21 |
$self->register_test('test_mx_address_resolves', 2); |
| 22 |
$self->register_test('test_get_host_records', 2); |
| 23 |
$self->register_test('test_get_and_validate_mx', 2); |
| 24 |
$self->register_test('test_check_dns', 2); |
| 25 |
$self->register_test('test_hook_mail', 4); |
| 26 |
} |
| 27 |
|
| 28 |
sub test_hook_mail { |
| 29 |
my $self = shift; |
| 30 |
|
| 31 |
my $transaction = $self->qp->transaction; |
| 32 |
my $address = Qpsmtpd::Address->new('[email protected]'); |
| 33 |
$transaction->sender($address); |
| 34 |
|
| 35 |
my $sender = $transaction->sender; |
| 36 |
$sender->host('perl.com'); |
| 37 |
|
| 38 |
ok( $self->hook_mail( $transaction, $sender ) ); |
| 39 |
ok( $self->hook_mail( $transaction, $sender ) ); |
| 40 |
|
| 41 |
$sender->host(''); |
| 42 |
$self->{_args}{reject} = 1; |
| 43 |
$self->{_args}{reject_type} = 'soft'; |
| 44 |
my ($r) = $self->hook_mail( $transaction, $sender ); |
| 45 |
ok( $r == DENYSOFT, "($r)"); |
| 46 |
|
| 47 |
$self->{_args}{reject_type} = 'hard'; |
| 48 |
($r) = $self->hook_mail( $transaction, $sender ); |
| 49 |
ok( $r == DENY, "($r)"); |
| 50 |
}; |
| 51 |
|
| 52 |
sub test_check_dns { |
| 53 |
my $self = shift; |
| 54 |
|
| 55 |
my $transaction = $self->qp->transaction; |
| 56 |
ok( ! $self->check_dns( '', $transaction ) ); |
| 57 |
ok( $self->check_dns( 'perl.com', $transaction ) ); |
| 58 |
} |
| 59 |
|
| 60 |
sub test_get_and_validate_mx { |
| 61 |
my $self = shift; |
| 62 |
my $transaction = $self->qp->transaction; |
| 63 |
|
| 64 |
ok( scalar $self->get_and_validate_mx( $res, 'perl.com', $transaction ) ); |
| 65 |
|
| 66 |
ok( ! scalar $self->get_host_records( $res, 'fake-domain-name-for-test.com', $transaction ) ); |
| 67 |
}; |
| 68 |
|
| 69 |
sub test_get_host_records { |
| 70 |
my $self = shift; |
| 71 |
my $transaction = $self->qp->transaction; |
| 72 |
|
| 73 |
ok( scalar $self->get_host_records( $res, 'perl.com', $transaction ) ); |
| 74 |
ok( ! scalar $self->get_host_records( $res, 'fake-domain-name-for-test.com', $transaction ) ); |
| 75 |
}; |
| 76 |
|
| 77 |
sub test_mx_address_resolves { |
| 78 |
my $self = shift; |
| 79 |
|
| 80 |
my $fromhost = 'perl.com'; |
| 81 |
|
| 82 |
ok( $self->mx_address_resolves('mail.perl.com', $fromhost) ); |
| 83 |
ok( ! $self->mx_address_resolves('no-such-mx.perl.com', $fromhost) ); |
| 84 |
}; |
| 85 |
|
| 86 |
sub test_populate_invalid_networks { |
| 87 |
my $self = shift; |
| 88 |
|
| 89 |
my $ip = '10.9.8.7'; |
| 90 |
ok( $self->ip_is_valid($ip) ); |
| 91 |
|
| 92 |
$self->qp->config('invalid_resolvable_fromhost', $ip); |
| 93 |
$self->populate_invalid_networks(); |
| 94 |
ok( ! $self->ip_is_valid($ip) ); |
| 95 |
|
| 96 |
# clean up afterwards |
| 97 |
$self->qp->config('invalid_resolvable_fromhost', undef ); |
| 98 |
$self->{invalid} = (); |
| 99 |
}; |
| 100 |
|