blib/lib/Mock/Net/Ping.pm

filter: Statementstmt 58.9% 23 / 39 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 45.4% 30 / 66 CC 9 SCAR 39.3
CC
9
Cov
41%
__ANON__
90.0
BEGIN
1.0
BEGIN
1.0
CRAP 51.1
↑ Summary
1 package Mock::Net::Ping;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Socket qw( inet_aton );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $VERSION = '0.09';
13  
14 # Override Net::Ping::ping
15 # Any private IP address, localhost and any IP from 127.0.0.0/8 will always pass.
16 # Other hosts and IPs will fail.
17 *Net::Ping::ping = sub
18 0 ▶{
Subroutine __ANON__: not called CC=9 SCAR=45.0
19 0 my ($self,
20 $host, # Name or IP number of host to ping
21 $timeout, # Seconds after which ping times out
22 ) = @_;
23 0 my ($ip, # Packed IP number of $host
24 $ret, # The return value
25 $ping_time, # When ping began
26 $address # address of $host
27 );
28  
29 0 ▶ croak("Usage: \$p->ping(\$host [, \$timeout])") unless @_ == 2 || @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 or @_ == 3 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
30 0 ▶ $timeout = $self->{"timeout"} unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
31 0 ▶ croak("Timeout must be greater than 0 seconds") if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
32  
33 0 ▶ return unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
34  
35 # Dispatch to the appropriate routine.
36 0 $ping_time = &Net::Ping::time();
37 0 ▶ if ( $host eq 'localhost' )
Branch: 2 branches
BranchTrueFalseTotal
if ($host eq 'localhost') { } 0 0 0
elsif ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
38 {
39 0 $address = '127.0.0.1';
40 0 $ret = 1;
41 }
42 elsif ( $host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ )
43 {
44 0 my $packed = inet_aton( $host );
45 # modified version of regex from http://www.perlmonks.org/?node_id=791164
46 # ret = 1 if local or private and 0 otherwise
47 0 ( $ret ) = 0 + ( $packed =~ m{^(?:\x0a|\x7F|\xAC[\x10-\x1F]|\xC0\xA8)} );
48 0 $address = $host;
49 }
50 else
51 {
52 0 $address = $host;
53 0 $ret = 0;
54 }
55  
56 0 ▶ return wantarray ? ($ret, &Net::Ping::time() - $ping_time, $address) : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
57 1 };
58  
59 1 1;
60 __END__
↑ Summary