2012-06-12 04:13:50 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
|
|
|
sub register_tests {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->register_test('test_init_resolver', 2);
|
|
|
|
$self->register_test('test_is_in_badhelo', 2);
|
|
|
|
$self->register_test('test_is_regex_match', 3);
|
2014-09-11 00:12:52 +02:00
|
|
|
$self->register_test('test_invalid_localhost', 8);
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->register_test('test_is_plain_ip', 3);
|
|
|
|
$self->register_test('test_is_address_literal', 3);
|
|
|
|
$self->register_test('test_no_forward_dns', 2);
|
2012-06-23 01:24:16 +02:00
|
|
|
$self->register_test('test_no_reverse_dns', 3);
|
|
|
|
$self->register_test('test_no_matching_dns', 2);
|
2012-06-12 04:13:50 +02:00
|
|
|
$self->register_test('test_helo_handler', 1);
|
2012-06-23 01:24:16 +02:00
|
|
|
$self->register_test('test_check_ip_match', 3);
|
|
|
|
$self->register_test('test_check_name_match', 3);
|
2012-06-12 04:13:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub test_helo_handler {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
cmp_ok( $self->helo_handler(undef, undef), '==', DECLINED, "empty host");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_init_resolver {
|
|
|
|
my $self = shift;
|
|
|
|
my $net_dns = $self->init_resolver();
|
|
|
|
ok( $net_dns, "net::dns" );
|
|
|
|
cmp_ok( ref $net_dns, 'eq', 'Net::DNS::Resolver', "ref ok");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_is_in_badhelo {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->is_in_badhelo('yahoo.com');
|
|
|
|
ok( $err, "yahoo.com, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_in_badhelo('example.com');
|
|
|
|
ok( ! $err, "example.com");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_is_regex_match {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->is_regex_match('yahoo.com', 'ya.oo\.com$' );
|
|
|
|
ok( $err, "yahoo.com, $why");
|
2014-09-11 00:12:52 +02:00
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
($err, $why) = $self->is_regex_match('yoda.com', 'ya.oo\.com$' );
|
|
|
|
ok( ! $err, "yahoo.com");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_regex_match('host-only', '!\.' );
|
|
|
|
ok( $err, "negated pattern, $why");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_invalid_localhost {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-09-11 00:12:52 +02:00
|
|
|
my ($err, $why);
|
|
|
|
foreach my $ip ( undef, '', '192.0.99.5' ) {
|
|
|
|
$self->qp->connection->remote_ip(undef);
|
|
|
|
($err, $why) = $self->invalid_localhost('localhost' );
|
|
|
|
ok(!$err, "host: localhost, remote ip: $ip");
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2014-09-11 00:12:52 +02:00
|
|
|
$self->qp->connection->remote_ip(undef);
|
|
|
|
($err, $why) = $self->invalid_localhost('not-localhost');
|
|
|
|
ok($err, "host: not-localhost, remote ip: $ip");
|
|
|
|
};
|
2012-06-12 04:13:50 +02:00
|
|
|
|
2014-09-09 23:44:50 +02:00
|
|
|
foreach my $ip (qw/ ::1 127.0.0.1 / ) {
|
|
|
|
$self->qp->connection->remote_ip($ip);
|
2014-09-11 00:12:52 +02:00
|
|
|
($err, $why) = $self->invalid_localhost('not-localhost');
|
2014-09-09 23:44:50 +02:00
|
|
|
ok( ! $err, "localhost, correct remote IP ($ip)");
|
|
|
|
}
|
2012-06-12 04:13:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
sub test_is_plain_ip {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->is_plain_ip('0.0.0.0');
|
|
|
|
ok( $err, "plain IP, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_plain_ip('255.255.255.255');
|
|
|
|
ok( $err, "plain IP, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_plain_ip('[255.255.255.255]');
|
|
|
|
ok( ! $err, "address literal");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_is_address_literal {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->is_address_literal('[0.0.0.0]');
|
|
|
|
ok( $err, "plain IP, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_address_literal('[255.255.255.255]');
|
|
|
|
ok( $err, "plain IP, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->is_address_literal('255.255.255.255');
|
|
|
|
ok( ! $err, "address literal");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_no_forward_dns {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->no_forward_dns('perl.org');
|
|
|
|
ok( ! $err, "perl.org");
|
2014-09-11 00:12:52 +02:00
|
|
|
|
2012-06-12 04:13:50 +02:00
|
|
|
# reserved .test TLD: http://tools.ietf.org/html/rfc2606
|
|
|
|
($err, $why) = $self->no_forward_dns('perl.org.test');
|
|
|
|
ok( $err, "test.perl.org.test");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_no_reverse_dns {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my ($err, $why) = $self->no_reverse_dns('test-host', '192.0.2.0');
|
|
|
|
ok( $err, "192.0.2.0, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->no_reverse_dns('test-host', '192.0.2.1');
|
|
|
|
ok( $err, "192.0.2.1, $why");
|
|
|
|
|
|
|
|
($err, $why) = $self->no_reverse_dns('mail.theartfarm.com', '208.75.177.101');
|
|
|
|
ok( ! $err, "208.75.177.101");
|
|
|
|
};
|
|
|
|
|
|
|
|
sub test_no_matching_dns {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->notes('helo_forward_match', undef);
|
|
|
|
$self->qp->connection->notes('helo_reverse_match', undef);
|
|
|
|
|
|
|
|
my ($err, $why) = $self->no_matching_dns('matt.test');
|
|
|
|
ok( $err, "fail, $why");
|
|
|
|
|
|
|
|
$self->qp->connection->notes('helo_forward_match', 1);
|
|
|
|
($err, $why) = $self->no_matching_dns('matt.test');
|
|
|
|
ok( ! $err, "pass");
|
|
|
|
};
|
|
|
|
|
2012-06-13 23:49:25 +02:00
|
|
|
sub test_check_ip_match {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('192.0.2.1');
|
|
|
|
|
|
|
|
$self->connection->notes('helo_forward_match', 0);
|
|
|
|
$self->check_ip_match('192.0.2.1');
|
2012-06-23 01:24:16 +02:00
|
|
|
ok( $self->connection->notes('helo_forward_match'), "exact");
|
2012-06-13 23:49:25 +02:00
|
|
|
|
|
|
|
$self->connection->notes('helo_forward_match', 0);
|
|
|
|
$self->check_ip_match('192.0.2.2');
|
2012-06-23 01:24:16 +02:00
|
|
|
ok( $self->connection->notes('helo_forward_match'), "network");
|
2012-06-13 23:49:25 +02:00
|
|
|
|
|
|
|
$self->connection->notes('helo_forward_match', 0);
|
|
|
|
$self->check_ip_match('192.0.1.1');
|
2012-06-23 01:24:16 +02:00
|
|
|
ok( ! $self->connection->notes('helo_forward_match'), "miss");
|
2012-06-13 23:49:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
sub test_check_name_match {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->connection->notes('helo_reverse_match', 0);
|
|
|
|
$self->check_name_match('mx0.example.com', 'mx0.example.com');
|
|
|
|
ok( $self->connection->notes('helo_reverse_match'), "exact");
|
|
|
|
|
|
|
|
$self->connection->notes('helo_reverse_match', 0);
|
|
|
|
$self->check_name_match('mx0.example.com', 'mx1.example.com');
|
|
|
|
ok( $self->connection->notes('helo_reverse_match'), "domain");
|
|
|
|
|
|
|
|
$self->connection->notes('helo_reverse_match', 0);
|
|
|
|
$self->check_name_match('mx0.example.com', 'mx0.example.net');
|
|
|
|
ok( ! $self->connection->notes('helo_reverse_match'), "domain");
|
|
|
|
};
|
|
|
|
|