| 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 |
eval 'use Geo::IP'; |
| 12 |
if ( $@ ) { |
| 13 |
warn "could not load Geo::IP\n"; |
| 14 |
return; |
| 15 |
}; |
| 16 |
|
| 17 |
$self->register_test('test_geoip_lookup', 2); |
| 18 |
$self->register_test('test_geoip_load_db', 2); |
| 19 |
$self->register_test('test_geoip_init_cc', 2); |
| 20 |
$self->register_test('test_set_country_code', 3); |
| 21 |
$self->register_test('test_set_country_name', 3); |
| 22 |
$self->register_test('test_set_continent', 3); |
| 23 |
$self->register_test('test_set_distance', 3); |
| 24 |
}; |
| 25 |
|
| 26 |
sub test_geoip_lookup { |
| 27 |
my $self = shift; |
| 28 |
|
| 29 |
$self->qp->connection->remote_ip('24.24.24.24'); |
| 30 |
cmp_ok( $self->connect_handler(), '==', DECLINED, "exit code"); |
| 31 |
|
| 32 |
cmp_ok( $self->connection->notes('geoip_country'), 'eq', 'US', "note"); |
| 33 |
}; |
| 34 |
|
| 35 |
sub test_geoip_load_db { |
| 36 |
my $self = shift; |
| 37 |
|
| 38 |
$self->open_geoip_db(); |
| 39 |
|
| 40 |
if ( $self->{_geoip_city} ) { |
| 41 |
ok( ref $self->{_geoip_city}, "loaded GeoIP city db" ); |
| 42 |
} |
| 43 |
else { |
| 44 |
ok( "no GeoIP city db" ); |
| 45 |
}; |
| 46 |
|
| 47 |
if ( $self->{_geoip} ) { |
| 48 |
ok( ref $self->{_geoip}, "loaded GeoIP db" ); |
| 49 |
} |
| 50 |
else { |
| 51 |
ok( "no GeoIP db" ); |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
sub test_geoip_init_cc { |
| 56 |
my $self = shift; |
| 57 |
|
| 58 |
$self->{_my_country_code} = undef; |
| 59 |
ok( ! $self->{_my_country_code}, "undefined"); |
| 60 |
|
| 61 |
my $test_ip = '208.175.177.10'; |
| 62 |
$self->{_args}{distance} = $test_ip; |
| 63 |
$self->init_my_country_code( $test_ip ); |
| 64 |
cmp_ok( $self->{_my_country_code}, 'eq', 'US', "country set and matches"); |
| 65 |
}; |
| 66 |
|
| 67 |
sub test_set_country_code { |
| 68 |
my $self = shift; |
| 69 |
|
| 70 |
$self->qp->connection->remote_ip(''); |
| 71 |
my $cc = $self->set_country_code(); |
| 72 |
ok( ! $cc, "undef"); |
| 73 |
|
| 74 |
$self->qp->connection->remote_ip('24.24.24.24'); |
| 75 |
$cc = $self->set_country_code(); |
| 76 |
cmp_ok( $cc, 'eq', 'US', "$cc"); |
| 77 |
|
| 78 |
my $note = $self->connection->notes('geoip_country'); |
| 79 |
cmp_ok( $note, 'eq', 'US', "note has: $cc"); |
| 80 |
}; |
| 81 |
|
| 82 |
sub test_set_country_name { |
| 83 |
my $self = shift; |
| 84 |
|
| 85 |
$self->{_geoip_record} = undef; |
| 86 |
$self->qp->connection->remote_ip(''); |
| 87 |
$self->set_country_code(); |
| 88 |
my $cn = $self->set_country_name(); |
| 89 |
ok( ! $cn, "undef") or warn "$cn\n"; |
| 90 |
|
| 91 |
$self->qp->connection->remote_ip('24.24.24.24'); |
| 92 |
$self->set_country_code(); |
| 93 |
$cn = $self->set_country_name(); |
| 94 |
cmp_ok( $cn, 'eq', 'United States', "$cn"); |
| 95 |
|
| 96 |
my $note = $self->connection->notes('geoip_country_name'); |
| 97 |
cmp_ok( $note, 'eq', 'United States', "note has: $cn"); |
| 98 |
}; |
| 99 |
|
| 100 |
sub test_set_continent { |
| 101 |
my $self = shift; |
| 102 |
|
| 103 |
$self->{_geoip_record} = undef; |
| 104 |
$self->qp->connection->remote_ip(''); |
| 105 |
$self->set_country_code(); |
| 106 |
my $cn = $self->set_continent(); |
| 107 |
ok( ! $cn, "undef") or warn "$cn\n"; |
| 108 |
|
| 109 |
$self->qp->connection->remote_ip('24.24.24.24'); |
| 110 |
$self->set_country_code(); |
| 111 |
$cn = $self->set_continent() || ''; |
| 112 |
my $note = $self->connection->notes('geoip_continent'); |
| 113 |
if ( $cn ) { |
| 114 |
cmp_ok( $cn, 'eq', 'NA', "$cn"); |
| 115 |
cmp_ok( $note, 'eq', 'NA', "note has: $cn"); |
| 116 |
} |
| 117 |
else { |
| 118 |
ok(1, "no continent data" ); |
| 119 |
ok(1, "no continent data" ); |
| 120 |
}; |
| 121 |
}; |
| 122 |
|
| 123 |
sub test_set_distance { |
| 124 |
my $self = shift; |
| 125 |
|
| 126 |
$self->{_geoip_record} = undef; |
| 127 |
$self->qp->connection->remote_ip(''); |
| 128 |
$self->set_country_code(); |
| 129 |
my $cn = $self->set_distance_gc(); |
| 130 |
ok( ! $cn, "undef") or warn "$cn\n"; |
| 131 |
|
| 132 |
$self->qp->connection->remote_ip('24.24.24.24'); |
| 133 |
$self->set_country_code(); |
| 134 |
$cn = $self->set_distance_gc(); |
| 135 |
if ( $cn ) { |
| 136 |
ok( $cn, "$cn km"); |
| 137 |
|
| 138 |
my $note = $self->connection->notes('geoip_distance'); |
| 139 |
ok( $note, "note has: $cn"); |
| 140 |
} |
| 141 |
else { |
| 142 |
ok( 1, "no distance data"); |
| 143 |
ok( 1, "no distance data"); |
| 144 |
} |
| 145 |
}; |
| 146 |
|