| 1 |
#!perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Qpsmtpd::Constants; |
| 7 |
|
| 8 |
sub register_tests { |
| 9 |
my $self = shift; |
| 10 |
|
| 11 |
$self->register_test('test_init_resolver', 2); |
| 12 |
$self->register_test('test_is_in_badhelo', 2); |
| 13 |
$self->register_test('test_is_regex_match', 3); |
| 14 |
$self->register_test('test_invalid_localhost', 4); |
| 15 |
$self->register_test('test_is_plain_ip', 3); |
| 16 |
$self->register_test('test_is_address_literal', 3); |
| 17 |
$self->register_test('test_no_forward_dns', 2); |
| 18 |
$self->register_test('test_no_reverse_dns', 3); |
| 19 |
$self->register_test('test_no_matching_dns', 2); |
| 20 |
$self->register_test('test_helo_handler', 1); |
| 21 |
$self->register_test('test_check_ip_match', 3); |
| 22 |
$self->register_test('test_check_name_match', 3); |
| 23 |
} |
| 24 |
|
| 25 |
sub test_helo_handler { |
| 26 |
my $self = shift; |
| 27 |
|
| 28 |
cmp_ok( $self->helo_handler(undef, undef), '==', DECLINED, "empty host"); |
| 29 |
}; |
| 30 |
|
| 31 |
sub test_init_resolver { |
| 32 |
my $self = shift; |
| 33 |
my $net_dns = $self->init_resolver(); |
| 34 |
ok( $net_dns, "net::dns" ); |
| 35 |
cmp_ok( ref $net_dns, 'eq', 'Net::DNS::Resolver', "ref ok"); |
| 36 |
}; |
| 37 |
|
| 38 |
sub test_is_in_badhelo { |
| 39 |
my $self = shift; |
| 40 |
|
| 41 |
my ($err, $why) = $self->is_in_badhelo('yahoo.com'); |
| 42 |
ok( $err, "yahoo.com, $why"); |
| 43 |
|
| 44 |
($err, $why) = $self->is_in_badhelo('example.com'); |
| 45 |
ok( ! $err, "example.com"); |
| 46 |
}; |
| 47 |
|
| 48 |
sub test_is_regex_match { |
| 49 |
my $self = shift; |
| 50 |
|
| 51 |
my ($err, $why) = $self->is_regex_match('yahoo.com', 'ya.oo\.com$' ); |
| 52 |
ok( $err, "yahoo.com, $why"); |
| 53 |
|
| 54 |
($err, $why) = $self->is_regex_match('yoda.com', 'ya.oo\.com$' ); |
| 55 |
ok( ! $err, "yahoo.com"); |
| 56 |
|
| 57 |
($err, $why) = $self->is_regex_match('host-only', '!\.' ); |
| 58 |
ok( $err, "negated pattern, $why"); |
| 59 |
}; |
| 60 |
|
| 61 |
sub test_invalid_localhost { |
| 62 |
my $self = shift; |
| 63 |
|
| 64 |
$self->qp->connection->remote_ip(undef); |
| 65 |
my ($err, $why) = $self->invalid_localhost('localhost' ); |
| 66 |
ok( $err, "localhost, undefined remote IP: $why"); |
| 67 |
|
| 68 |
$self->qp->connection->remote_ip(''); |
| 69 |
($err, $why) = $self->invalid_localhost('localhost' ); |
| 70 |
ok( $err, "localhost, empty remote IP: $why"); |
| 71 |
|
| 72 |
$self->qp->connection->remote_ip('192.0.99.5'); |
| 73 |
($err, $why) = $self->invalid_localhost('localhost'); |
| 74 |
ok( $err, "localhost, invalid remote IP: $why"); |
| 75 |
|
| 76 |
$self->qp->connection->remote_ip('127.0.0.1'); |
| 77 |
($err, $why) = $self->invalid_localhost('localhost'); |
| 78 |
ok( ! $err, "localhost, correct remote IP"); |
| 79 |
}; |
| 80 |
|
| 81 |
sub test_is_plain_ip { |
| 82 |
my $self = shift; |
| 83 |
|
| 84 |
my ($err, $why) = $self->is_plain_ip('0.0.0.0'); |
| 85 |
ok( $err, "plain IP, $why"); |
| 86 |
|
| 87 |
($err, $why) = $self->is_plain_ip('255.255.255.255'); |
| 88 |
ok( $err, "plain IP, $why"); |
| 89 |
|
| 90 |
($err, $why) = $self->is_plain_ip('[255.255.255.255]'); |
| 91 |
ok( ! $err, "address literal"); |
| 92 |
}; |
| 93 |
|
| 94 |
sub test_is_address_literal { |
| 95 |
my $self = shift; |
| 96 |
|
| 97 |
my ($err, $why) = $self->is_address_literal('[0.0.0.0]'); |
| 98 |
ok( $err, "plain IP, $why"); |
| 99 |
|
| 100 |
($err, $why) = $self->is_address_literal('[255.255.255.255]'); |
| 101 |
ok( $err, "plain IP, $why"); |
| 102 |
|
| 103 |
($err, $why) = $self->is_address_literal('255.255.255.255'); |
| 104 |
ok( ! $err, "address literal"); |
| 105 |
}; |
| 106 |
|
| 107 |
sub test_no_forward_dns { |
| 108 |
my $self = shift; |
| 109 |
|
| 110 |
my ($err, $why) = $self->no_forward_dns('perl.org'); |
| 111 |
ok( ! $err, "perl.org"); |
| 112 |
|
| 113 |
# reserved .test TLD: http://tools.ietf.org/html/rfc2606 |
| 114 |
($err, $why) = $self->no_forward_dns('perl.org.test'); |
| 115 |
ok( $err, "test.perl.org.test"); |
| 116 |
}; |
| 117 |
|
| 118 |
sub test_no_reverse_dns { |
| 119 |
my $self = shift; |
| 120 |
|
| 121 |
my ($err, $why) = $self->no_reverse_dns('test-host', '192.0.2.0'); |
| 122 |
ok( $err, "192.0.2.0, $why"); |
| 123 |
|
| 124 |
($err, $why) = $self->no_reverse_dns('test-host', '192.0.2.1'); |
| 125 |
ok( $err, "192.0.2.1, $why"); |
| 126 |
|
| 127 |
($err, $why) = $self->no_reverse_dns('mail.theartfarm.com', '208.75.177.101'); |
| 128 |
ok( ! $err, "208.75.177.101"); |
| 129 |
}; |
| 130 |
|
| 131 |
sub test_no_matching_dns { |
| 132 |
my $self = shift; |
| 133 |
|
| 134 |
$self->qp->connection->notes('helo_forward_match', undef); |
| 135 |
$self->qp->connection->notes('helo_reverse_match', undef); |
| 136 |
|
| 137 |
my ($err, $why) = $self->no_matching_dns('matt.test'); |
| 138 |
ok( $err, "fail, $why"); |
| 139 |
|
| 140 |
$self->qp->connection->notes('helo_forward_match', 1); |
| 141 |
($err, $why) = $self->no_matching_dns('matt.test'); |
| 142 |
ok( ! $err, "pass"); |
| 143 |
}; |
| 144 |
|
| 145 |
sub test_check_ip_match { |
| 146 |
my $self = shift; |
| 147 |
|
| 148 |
$self->qp->connection->remote_ip('192.0.2.1'); |
| 149 |
|
| 150 |
$self->connection->notes('helo_forward_match', 0); |
| 151 |
$self->check_ip_match('192.0.2.1'); |
| 152 |
ok( $self->connection->notes('helo_forward_match'), "exact"); |
| 153 |
|
| 154 |
$self->connection->notes('helo_forward_match', 0); |
| 155 |
$self->check_ip_match('192.0.2.2'); |
| 156 |
ok( $self->connection->notes('helo_forward_match'), "network"); |
| 157 |
|
| 158 |
$self->connection->notes('helo_forward_match', 0); |
| 159 |
$self->check_ip_match('192.0.1.1'); |
| 160 |
ok( ! $self->connection->notes('helo_forward_match'), "miss"); |
| 161 |
}; |
| 162 |
|
| 163 |
sub test_check_name_match { |
| 164 |
my $self = shift; |
| 165 |
|
| 166 |
$self->connection->notes('helo_reverse_match', 0); |
| 167 |
$self->check_name_match('mx0.example.com', 'mx0.example.com'); |
| 168 |
ok( $self->connection->notes('helo_reverse_match'), "exact"); |
| 169 |
|
| 170 |
$self->connection->notes('helo_reverse_match', 0); |
| 171 |
$self->check_name_match('mx0.example.com', 'mx1.example.com'); |
| 172 |
ok( $self->connection->notes('helo_reverse_match'), "domain"); |
| 173 |
|
| 174 |
$self->connection->notes('helo_reverse_match', 0); |
| 175 |
$self->check_name_match('mx0.example.com', 'mx0.example.net'); |
| 176 |
ok( ! $self->connection->notes('helo_reverse_match'), "domain"); |
| 177 |
}; |
| 178 |
|