blib/lib/Mail/Exim/ACL/Geolocation.pm

filter: Statementstmt 72.5% 45 / 62 Branchbran 37.5% 6 / 16 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 61.7% 63 / 102 CC 14 SCAR 27.0
CC
14
Cov
62%
_find_newest_database
42.0
asn_lookup
6.4
country_code
4.3
CRAP 14.9
↑ Summary
1 package Mail::Exim::ACL::Geolocation;
2  
3 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4  
5 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 1.006;
10  
11 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IP::Geolocation::MMDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT_OK = qw(country_code asn_lookup);
15  
16 1 my @directories = qw(
17 /var/lib/GeoIP
18 /usr/local/share/GeoIP
19 /usr/share/GeoIP
20 /opt/share/GeoIP
21 /var/db/GeoIP
22 );
23  
24 1 my @country_databases = qw(
25 dbip-country.mmdb
26 dbip-country-lite.mmdb
27 GeoIP2-Country.mmdb
28 GeoLite2-Country.mmdb
29 );
30  
31 1 my @city_databases = qw(
32 dbip-city.mmdb
33 dbip-city-lite.mmdb
34 GeoIP2-City.mmdb
35 GeoLite2-City.mmdb
36 );
37  
38 1 my @asn_databases = qw(
39 dbip-asn.mmdb
40 dbip-asn-lite.mmdb
41 GeoIP2-ASN.mmdb
42 GeoLite2-ASN.mmdb
43 );
44  
45 1 my $country_reader;
46 my $asn_reader;
47  
48 2 ▶sub country_code {
Subroutine country_code: called CC=4 SCAR=14.6
Pod documented
49 2 my $ip_address = shift;
50  
51 2 ▶ if (!defined $country_reader) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $country_reader) 1 1 2
52 1 $country_reader = eval {
53 my $path
54 = $ENV{COUNTRY_DB}
55 1 ▶ || _find_newest_database(\@directories, \@country_databases)
Condition33%2/6
$ENV{'COUNTRY_DB'} || _find_newest_database(\@directories, \@country_databases) || _find_newest_database(\@directories, \@city_databases)
l!l&&r!l&&!r
100
$ENV{"COUNTRY_DB"} or _find_newest_database(\@directories, \@country_databases)
l!l&&r!l&&!r
100
MC/DC0%0/3
$ENV{'COUNTRY_DB'} || _find_newest_database(\@directories, \@country_databases) || _find_newest_database(\@directories, \@city_databases)
$ENV{"COUNTRY_DB"} _find_newest_database(\@directories, \@country_databases) _find_newest_database(\@directories, \@city_databases)
Truth table25%1/4
A or B || C
A= $ENV{"COUNTRY_DB"}B= _find_newest_database(\@directories, \@country_databases)C= _find_newest_database(\@directories, \@city_databases)
ABCresult
1XX1
01X1
0011
0000
56 || _find_newest_database(\@directories, \@city_databases);
57 1 IP::Geolocation::MMDB->new(file => $path);
58 };
59 }
60  
61 2 return eval { $country_reader->getcc($ip_address) };
62 }
63  
64 1 ▶sub asn_lookup {
Subroutine asn_lookup: called CC=6 SCAR=18.6
Pod documented
65 1 my $ip_address = shift;
66  
67 1 ▶ if (!defined $asn_reader) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $asn_reader) 1 0 1
68 1 $asn_reader = eval {
69 my $path = $ENV{ASN_DB}
70 1 ▶ || _find_newest_database(\@directories, \@asn_databases);
Condition33%1/3
$ENV{'ASN_DB'} || _find_newest_database(\@directories, \@asn_databases)
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'ASN_DB'} || _find_newest_database(\@directories, \@asn_databases)
$ENV{'ASN_DB'} _find_newest_database(\@directories, \@asn_databases)
Truth table33%1/3
A || B
A= $ENV{'ASN_DB'}B= _find_newest_database(\@directories, \@asn_databases)
ABresult
1X1
011
000
71 1 IP::Geolocation::MMDB->new(file => $path);
72 };
73 }
74  
75 1 my $asn_tag;
76 1 my $asn = eval { $asn_reader->record_for_address($ip_address) };
77 1 ▶ if (defined $asn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $asn) 1 0 1
78 1 my $number = $asn->{autonomous_system_number};
79 1 ▶ if (defined $number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number) 1 0 1
80 1 $asn_tag = $number;
81 1 my $organization = $asn->{autonomous_system_organization};
82 1 ▶ if (defined $organization) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $organization) 1 0 1
83 1 $asn_tag .= q{ } . $organization;
84 }
85 }
86 }
87 1 return $asn_tag;
88 }
89  
90 0 ▶sub _find_newest_database {
Subroutine _find_newest_database: not called CC=6 SCAR=37.4
91 0 my ($directories, $databases) = @_;
92  
93 0 my %path_for;
94 0 for my $dir (@{$directories}) {
95 0 for my $db (@{$databases}) {
96 0 my $path = $dir . q{/} . $db;
97 0 ▶ if (-r $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $path) 0 0 0
98 0 my @status_info = stat $path;
99 0 ▶ if (@status_info > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@status_info > 0) 0 0 0
100 0 my $time = $status_info[9];
101 0 $path_for{$time} = $path;
102 }
103 }
104 }
105 }
106 0 my @times = sort { $b <=> $a } keys %path_for;
107 0 ▶ if (@times > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@times > 0) 0 0 0
108 0 return $path_for{$times[0]};
109 }
110 0 return;
111 }
112  
113 1 1;
114 __END__
↑ Summary