Add tests for exclude files

This commit is contained in:
Jared Johnson 2014-11-06 17:02:27 -06:00
parent 7fb80f35ab
commit 2655e7b452
2 changed files with 54 additions and 4 deletions

View File

@ -253,20 +253,21 @@ sub load_exclude_file {
sub exclude_host { sub exclude_host {
my ( $self, $pattern ) = @_; my ( $self, $pattern ) = @_;
if ( $pattern =~ /^\/(.*)\/$/ ) { if ( $pattern =~ /^\/(.*)\/$/ ) {
push @{ $self->{_exclude_re} }, $1; push @{ $self->{_exclude_re} }, qr/$1/;
} }
elsif ( Qpsmtpd::Base->is_valid_ip($pattern) ) { elsif ( Qpsmtpd::Base->is_valid_ip($pattern) ) {
$self->{_exclude_ip}{$pattern} = undef; $self->{_exclude_ip}{$pattern} = 1;
} }
else { else {
$self->{_exclude_host}{$pattern} = undef; $self->{_exclude_hostname}{$pattern} = 1;
} }
} }
sub exclude_file_match { sub exclude_file_match {
my ( $self ) = @_; my ( $self ) = @_;
return 1 if $self->{_exclude_ip}{ $self->connection->remote_ip }; return 1 if $self->{_exclude_ip}{ $self->connection->remote_ip };
return 1 if $self->{_exclude_host}{ $self->connection->remote_host }; return 0 if ! $self->connection->remote_host;
return 1 if $self->{_exclude_hostname}{ $self->connection->remote_host };
for my $re ( @{ $self->{_exclude_re} || [] } ) { for my $re ( @{ $self->{_exclude_re} || [] } ) {
return 1 if $self->connection->remote_host =~ $re; return 1 if $self->connection->remote_host =~ $re;
} }

View File

@ -16,6 +16,7 @@ foreach ( @greydbs ) {
sub register_tests { sub register_tests {
my $self = shift; my $self = shift;
$self->register_test("test_load_exclude_files");
$self->register_test('test_hook_data'); $self->register_test('test_hook_data');
$self->register_test('test_get_db_key'); $self->register_test('test_get_db_key');
$self->register_test('test_get_db_location'); $self->register_test('test_get_db_location');
@ -25,6 +26,54 @@ sub register_tests {
$self->register_test("test_greylist_p0f_distance"); $self->register_test("test_greylist_p0f_distance");
$self->register_test("test_greylist_p0f_link"); $self->register_test("test_greylist_p0f_link");
$self->register_test("test_greylist_p0f_uptime"); $self->register_test("test_greylist_p0f_uptime");
$self->register_test('test_exclude_file_match');
}
sub test_load_exclude_files {
my ( $self ) = @_;
delete $self->{$_} for qw( _exclude_ip _exclude_hostname exclude_re );
$self->load_exclude_files();
ok( $self->{_exclude_ip}{'194.7.234.142'},
'Excluded IPs populated by load_exclude_files()' );
ok( $self->{_exclude_hostname}{'yahoo.com'},
'Excluded hostnames populated by load_exclude_files()' );
ok( ( grep { $_ eq qr/^mta[12].siol.net$/ } @{ $self->{_exclude_re} || [] } ),
'Excluded REs populated by load_exlude_files()' );
}
sub test_exclude_file_match {
my ( $self ) = @_;
my @test_data = (
{
ip => 192.168.1.1,
hostname => 'mta1234.siol.net',
expected => 0,
descr => 'miss',
},
{
ip => '194.7.234.142',
hostname => 'mta1234.siol.net',
expected => 1,
descr => 'IP match',
},
{
ip => 192.168.1.1,
hostname => 'postini.com',
expected => 1,
descr => 'Hostname match',
},
{
ip => 192.168.1.1,
hostname => 'mta2.siol.net',
expected => 1,
descr => 'Regex match',
},
);
for my $t ( @test_data ) {
$self->connection->remote_ip( $t->{ip} );
$self->connection->remote_host( $t->{hostname} );
is( $self->exclude_file_match(), $t->{expected}, "exclude_file_match(): $t->{descr}" );
}
} }
sub test_hook_data { sub test_hook_data {