2012-05-22 22:40:39 +02:00
|
|
|
#!perl -w
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2014-11-04 23:43:54 +01:00
|
|
|
use lib 'lib';
|
2012-05-22 22:40:39 +02:00
|
|
|
use Qpsmtpd::Constants;
|
|
|
|
|
|
|
|
sub register_tests {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-05 09:23:31 +01:00
|
|
|
eval 'use GeoIP2::Database::Reader';
|
|
|
|
if ( !$@ ) {
|
|
|
|
warn "using GeoIP2\n";
|
|
|
|
$self->register_test('test_geoip2_lookup');
|
|
|
|
}
|
|
|
|
|
2012-05-22 22:40:39 +02:00
|
|
|
eval 'use Geo::IP';
|
2014-11-05 22:50:19 +01:00
|
|
|
if ( !$@ ) {
|
|
|
|
warn "loaded Geo::IP\n";
|
|
|
|
|
|
|
|
$self->register_test('test_geoip_lookup');
|
|
|
|
$self->register_test('test_geoip_load_db');
|
|
|
|
$self->register_test('test_geoip_init_cc');
|
|
|
|
$self->register_test('test_set_country_code');
|
|
|
|
$self->register_test('test_set_country_name');
|
|
|
|
$self->register_test('test_set_continent');
|
|
|
|
$self->register_test('test_set_distance');
|
|
|
|
$self->register_test('test_set_asn');
|
2014-11-13 20:52:49 +01:00
|
|
|
$self->register_test('test_add_headers');
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-05 09:23:31 +01:00
|
|
|
|
|
|
|
sub test_geoip2_lookup {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
2014-11-05 22:50:19 +01:00
|
|
|
cmp_ok( $self->geoip2_lookup(), '==', DECLINED, "exit code DECLINED");
|
2014-11-05 09:23:31 +01:00
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
if (!$self->load_geoip2()) {
|
|
|
|
warn "failed to load GeoIP2\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp_ok( $self->connection->notes('geoip_country'), 'eq', 'US', "24.24.24.24 is in country US");
|
|
|
|
cmp_ok( $self->connection->notes('geoip_country_name'), 'eq', 'United States', "24.24.24.24 is in country United States");
|
|
|
|
cmp_ok( $self->connection->notes('geoip_continent'), 'eq', 'NA', "24.24.24.24 is in continent NA");
|
|
|
|
cmp_ok( $self->connection->notes('geoip_city'), 'eq', 'Deer Park', "24.24.24.24 is in city of Deer Park");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-05-22 22:40:39 +02:00
|
|
|
|
2014-11-13 20:52:49 +01:00
|
|
|
sub test_add_headers {
|
|
|
|
my ( $self ) = @_;
|
|
|
|
my @notes = qw( geoip_country geoip_continent geoip_city geoip_asn );
|
|
|
|
$self->connection->notes( $_ => "test $_" ) for @notes;
|
|
|
|
my $header = $self->transaction->header( Mail::Header->new );
|
|
|
|
my @tags = (qw( X-GeoIP-Country X-GeoIP-Continent X-GeoIP-City X-GeoIP-ASN ));
|
|
|
|
$header->add( $_ => 'DELETETHIS' ) for @tags;
|
|
|
|
$self->add_headers($self->transaction);
|
|
|
|
is( $self->all_headers('X-GeoIP-Country'), 'test geoip_country',
|
|
|
|
'X-GeoIP-Country header added' );
|
|
|
|
is( $self->all_headers('X-GeoIP-Continent'), 'test geoip_continent',
|
|
|
|
'X-GeoIP-Continent header added' );
|
|
|
|
is( $self->all_headers('X-GeoIP-City'), 'test geoip_city',
|
|
|
|
'X-GeoIP-City header added' );
|
|
|
|
is( $self->all_headers('X-GeoIP-ASN'), 'test geoip_asn',
|
|
|
|
'X-GeoIP-ASN header added' );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub all_headers {
|
|
|
|
# Return all instances of a given message header
|
|
|
|
my ( $self, $tag ) = @_;
|
|
|
|
return join " | ", map { chomp $_; $_ } $self->transaction->header->get($tag);
|
|
|
|
}
|
|
|
|
|
2012-05-22 22:40:39 +02:00
|
|
|
sub test_geoip_lookup {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
2014-11-04 23:43:54 +01:00
|
|
|
cmp_ok( $self->geoip_lookup(), '==', DECLINED, "exit code");
|
2012-05-22 22:40:39 +02:00
|
|
|
|
2014-11-05 09:23:31 +01:00
|
|
|
cmp_ok( $self->connection->notes('geoip_country'), 'eq', 'US', "24.24.24.24 is in the US");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-05-22 22:40:39 +02:00
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
sub test_geoip_load_db {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->open_geoip_db();
|
|
|
|
|
|
|
|
if ( $self->{_geoip_city} ) {
|
|
|
|
ok( ref $self->{_geoip_city}, "loaded GeoIP city db" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok( "no GeoIP city db" );
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
if ( $self->{_geoip} ) {
|
|
|
|
ok( ref $self->{_geoip}, "loaded GeoIP db" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok( "no GeoIP db" );
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub test_geoip_init_cc {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{_my_country_code} = undef;
|
|
|
|
ok( ! $self->{_my_country_code}, "undefined");
|
|
|
|
|
|
|
|
my $test_ip = '208.175.177.10';
|
|
|
|
$self->{_args}{distance} = $test_ip;
|
|
|
|
$self->init_my_country_code( $test_ip );
|
|
|
|
cmp_ok( $self->{_my_country_code}, 'eq', 'US', "country set and matches");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub test_set_country_code {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('');
|
|
|
|
my $cc = $self->set_country_code();
|
|
|
|
ok( ! $cc, "undef");
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
|
|
|
$cc = $self->set_country_code();
|
2014-11-05 09:23:31 +01:00
|
|
|
cmp_ok( $cc, 'eq', 'US', "set_country_code result is $cc");
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
my $note = $self->connection->notes('geoip_country');
|
2014-11-05 09:23:31 +01:00
|
|
|
cmp_ok( $note, 'eq', 'US', "set_country_code set note to $cc");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub test_set_country_name {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{_geoip_record} = undef;
|
|
|
|
$self->qp->connection->remote_ip('');
|
|
|
|
$self->set_country_code();
|
|
|
|
my $cn = $self->set_country_name();
|
|
|
|
ok( ! $cn, "undef") or warn "$cn\n";
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
|
|
|
$self->set_country_code();
|
|
|
|
$cn = $self->set_country_name();
|
|
|
|
cmp_ok( $cn, 'eq', 'United States', "$cn");
|
|
|
|
|
|
|
|
my $note = $self->connection->notes('geoip_country_name');
|
|
|
|
cmp_ok( $note, 'eq', 'United States', "note has: $cn");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub test_set_continent {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{_geoip_record} = undef;
|
|
|
|
$self->qp->connection->remote_ip('');
|
|
|
|
$self->set_country_code();
|
|
|
|
my $cn = $self->set_continent();
|
|
|
|
ok( ! $cn, "undef") or warn "$cn\n";
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
|
|
|
$self->set_country_code();
|
|
|
|
$cn = $self->set_continent() || '';
|
|
|
|
my $note = $self->connection->notes('geoip_continent');
|
|
|
|
if ( $cn ) {
|
|
|
|
cmp_ok( $cn, 'eq', 'NA', "$cn");
|
|
|
|
cmp_ok( $note, 'eq', 'NA', "note has: $cn");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok(1, "no continent data" );
|
|
|
|
ok(1, "no continent data" );
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub test_set_distance {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{_geoip_record} = undef;
|
|
|
|
$self->qp->connection->remote_ip('');
|
|
|
|
$self->set_country_code();
|
|
|
|
my $cn = $self->set_distance_gc();
|
|
|
|
ok( ! $cn, "undef") or warn "$cn\n";
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
|
|
|
$self->set_country_code();
|
|
|
|
$cn = $self->set_distance_gc();
|
|
|
|
if ( $cn ) {
|
|
|
|
ok( $cn, "$cn km");
|
|
|
|
|
|
|
|
my $note = $self->connection->notes('geoip_distance');
|
|
|
|
ok( $note, "note has: $cn");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ok( 1, "no distance data");
|
|
|
|
ok( 1, "no distance data");
|
|
|
|
}
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
sub test_set_asn {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-11 08:02:21 +01:00
|
|
|
return if !$self->{GeoIPASNum};
|
|
|
|
|
2014-11-05 22:50:19 +01:00
|
|
|
$self->qp->connection->remote_ip('');
|
|
|
|
$self->set_asn();
|
|
|
|
my $asn = $self->set_asn();
|
|
|
|
ok( ! $asn, "undef") or warn "$asn\n";
|
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('24.24.24.24');
|
|
|
|
$asn = $self->set_asn();
|
2014-11-05 22:52:21 +01:00
|
|
|
ok( $self->connection->notes('geoip_asn') =~ /^11351/, "note has: $asn");
|
2014-11-05 22:50:19 +01:00
|
|
|
|
|
|
|
$self->qp->connection->remote_ip('66.128.51.163');
|
|
|
|
$asn = $self->set_asn();
|
|
|
|
|
2014-11-05 22:52:21 +01:00
|
|
|
ok( $self->connection->notes('geoip_asn') =~ /^7819/, "note has: $asn");
|
2014-11-11 23:59:40 +01:00
|
|
|
}
|