Net/EmptyPort.pm

filter: Statementstmt 98.8% 89 / 90 Branchbran 80.3% 45 / 56 Conditioncond 79.4% 31 / 39 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 87.4% 202 / 231 CC 43 SCAR 21.6
CC
43
Cov
89%
empty_port
16.2
wait_port
11.1
_check_port_udp
7.2
CRAP 8.7
↑ Summary Test/TCP.pm »
1 package Net::EmptyPort;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Errno qw/ECONNREFUSED/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 our @EXPORT = qw/ can_bind empty_port check_port wait_port /;
11 19 our @EXPORT_OK = qw/ listen_socket /;
12  
13 8 ▶sub can_bind {
Subroutine can_bind: called CC=1 SCAR=0.0
Pod documented
14 8 my ($host, $port, $proto) = @_;
15 # The following must be split across two statements, due to
16 # https://rt.perl.org/Public/Bug/Display.html?id=124248
17 8 my $s = _listen_socket($host, $port, $proto);
18 8 return defined $s;
19 }
20  
21 30 ▶sub _listen_socket {
Subroutine _listen_socket: called CC=5 SCAR=16.1
22 30 my ($host, $port, $proto) = @_;
23 30 ▶ $port ||= 0;
Condition100%2/2
$port ||= 0
l!l
426
MC/DC100%1/1
$port ||= 0
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
24 30 ▶ $proto ||= 'tcp';
Condition100%2/2
$proto ||= "tcp"
l!l
255
MC/DC100%1/1
$proto ||= "tcp"
$proto
Truth table100%2/2
A
A= $proto
Aresult
11
00
25 30 ▶ IO::Socket::IP->new(
Branch: 2 branches
BranchTrueFalseTotal
$proto eq 'udp' ? : 2 28 30
$^O eq 'MSWin32' ? : 0 30 30
26 (($proto eq 'udp') ? () : (Listen => 5)),
27 LocalAddr => $host,
28 LocalPort => $port,
29 Proto => $proto,
30 V6Only => 1,
31 (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)),
32 );
33 }
34  
35 1 ▶sub listen_socket {
Subroutine listen_socket: called CC=2 SCAR=7.0
Pod documented
36 1 my ($host, $proto) = @{$_[0]}{qw(host proto)};
37 1 ▶ $host = '127.0.0.1' unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 1 1
38 1 return _listen_socket($host, undef, $proto);
39 }
40  
41 # get a empty port on 49152 .. 65535
42 # http://www.iana.org/assignments/port-numbers
43 25 ▶sub empty_port {
Subroutine empty_port: called CC=15 SCAR=27.8
Pod documented
44 25 ▶ my ($host, $port, $proto) = @_ && ref $_[0] eq 'HASH' ? ($_[0]->{host}, $_[0]->{port}, $_[0]->{proto}) : (undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 17 8 25
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
4417
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
45 25 ▶ $host = '127.0.0.1'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 9 16 25
46 unless defined $host;
47 25 ▶ $proto = $proto ? lc($proto) : 'tcp';
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 5 20 25
48  
49 25 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) { } 4 21 25
50 # to ensure lower bound, check one by one in order
51 4 ▶ $port = 49152 unless $port =~ /^[0-9]+$/ && $port < 49152;
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /^[0-9]+$/ and $port < 49152 3 1 4
Condition100%3/3
$port =~ /^[0-9]+$/ and $port < 49152
!ll&&!rl&&r
211
MC/DC100%2/2
$port =~ /^[0-9]+$/ and $port < 49152
$port =~ /^[0-9]+$/ $port < 49152
Truth table100%3/3
A and B
A= $port =~ /^[0-9]+$/B= $port < 49152
ABresult
0X0
100
111
52 4 ▶ while ( $port++ < 65000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port++ < 65000) 4 0 4
53 # Remote checks don't work on UDP, and Local checks would be redundant here...
54 4 ▶ next if ($proto eq 'tcp' && check_port({ host => $host, port => $port }));
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "tcp" and check_port({"host", $host, "port", $port}) 0 4 4
Condition66%2/3
$proto eq "tcp" and check_port({"host", $host, "port", $port})
!ll&&!rl&&r
130
MC/DC0%0/2
$proto eq "tcp" and check_port({"host", $host, "port", $port})
$proto eq "tcp" check_port({"host", $host, "port", $port})
Truth table66%2/3
A and B
A= $proto eq "tcp"B= check_port({"host", $host, "port", $port})
ABresult
0X0
100
111
55 4 ▶ return $port if can_bind($host, $port, $proto);
Branch: 1 branch
BranchTrueFalseTotal
if can_bind($host, $port, $proto) 4 0 4
56 }
57 } else {
58 # kernel will select an unused port
59 21 ▶ while ( my $sock = _listen_socket($host, undef, $proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sock = _listen_socket($host, undef, $proto)) 21 0 21
60 21 $port = $sock->sockport;
61 21 $sock->close;
62 21 ▶ next if ($proto eq 'tcp' && check_port({ host => $host, port => $port }));
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "tcp" and check_port({"host", $host, "port", $port}) 0 21 21
Condition66%2/3
$proto eq "tcp" and check_port({"host", $host, "port", $port})
!ll&&!rl&&r
1200
MC/DC0%0/2
$proto eq "tcp" and check_port({"host", $host, "port", $port})
$proto eq "tcp" check_port({"host", $host, "port", $port})
Truth table66%2/3
A and B
A= $proto eq "tcp"B= check_port({"host", $host, "port", $port})
ABresult
0X0
100
111
63 21 return $port;
64 }
65 }
66 0 die "empty port not found";
67 }
68  
69 96 ▶sub check_port {
Subroutine check_port: called CC=7 SCAR=19.5
Pod documented
70 96 ▶ my ($host, $port, $proto) = @_ && ref $_[0] eq 'HASH' ? ($_[0]->{host}, $_[0]->{port}, $_[0]->{proto}) : (undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 92 4 96
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
1392
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
71 96 ▶ $host = '127.0.0.1'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 4 92 96
72 unless defined $host;
73  
74 96 ▶ return _check_port_udp($host, $port)
Branch: 1 branch
BranchTrueFalseTotal
if $proto and lc $proto eq "udp" 2 94 96
Condition100%3/3
$proto and lc $proto eq "udp"
!ll&&!rl&&r
24702
MC/DC100%2/2
$proto and lc $proto eq "udp"
$proto lc $proto eq "udp"
Truth table100%3/3
A and B
A= $protoB= lc $proto eq "udp"
ABresult
0X0
100
111
75 if $proto && lc($proto) eq 'udp';
76  
77 # TCP, check if possible to connect
78 94 my $sock = IO::Socket::IP->new(
79 Proto => 'tcp',
80 PeerAddr => $host,
81 PeerPort => $port,
82 V6Only => 1,
83 );
84  
85 92 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) { } 14 78 92
86 14 close $sock;
87 14 return 1; # The port is used.
88 }
89 else {
90 78 return 0; # The port is not used.
91 }
92  
93 }
94  
95 2 ▶sub _check_port_udp {
Subroutine _check_port_udp: called CC=6 SCAR=19.7
96 2 my ($host, $port) = @_;
97  
98 # send some UDP data and see if ICMP error is being sent back (i.e. ECONNREFUSED)
99 2 ▶ my $sock = IO::Socket::IP->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::IP"->new("Proto", "udp", "PeerAddr", $host, "PeerPort", $port, "V6Only", 1, "Blocking", 0) or die "failed to create bound UDP socket:$!" 2 0 2
100 Proto => 'udp',
101 PeerAddr => $host,
102 PeerPort => $port,
103 V6Only => 1,
104 Blocking => 0,
105 ) or die "failed to create bound UDP socket:$!";
106  
107 2 ▶ $sock->send("0", 0)
Branch: 1 branch
BranchTrueFalseTotal
$sock->send(0, 0) or die "failed to send a UDP packet:$!" 2 0 2
108 or die "failed to send a UDP packet:$!";
109  
110 2 my ($rfds, $efds) = ('', '');
111 2 vec($rfds, fileno($sock), 1) = 1;
112 2 vec($efds, fileno($sock), 1) = 1;
113 2 select $rfds, undef, $efds, 0.1;
114  
115 # after 0.1 second of silence, we assume that the server is up
116 2 ▶ my $up = defined($sock->recv(my $data, 1000)) || (
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 2 2
Condition33%2/6
defined $sock->recv(my $data, 1000) || ($^O eq 'MSWin32' ? $^E != Errno::WSAECONNRESET() && $^E != Errno::WSAECONNREFUSED() : $! != 111)
l!l&&r!l&&!r
011
$^E != Errno::WSAECONNRESET() and $^E != Errno::WSAECONNREFUSED()
!ll&&!rl&&r
000
MC/DC50%1/2
defined $sock->recv(my $data, 1000) || ($^O eq 'MSWin32' ? $^E != Errno::WSAECONNRESET() && $^E != Errno::WSAECONNREFUSED() : $! != 111)
defined $sock->recv(my $data, 1000) ($^O eq 'MSWin32' ? $^E != Errno::WSAECONNRESET() && $^E != Errno::WSAECONNREFUSED() : $! != 111)
MC/DC0%0/2
$^E != Errno::WSAECONNRESET() and $^E != Errno::WSAECONNREFUSED()
$^E != Errno::WSAECONNRESET() $^E != Errno::WSAECONNREFUSED()
Truth table66%2/3
A || B
A= defined $sock->recv(my $data, 1000)B= ($^O eq 'MSWin32' ? $^E != Errno::WSAECONNRESET() && $^E != Errno::WSAECONNREFUSED() : $! != 111)
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= $^E != Errno::WSAECONNRESET()B= $^E != Errno::WSAECONNREFUSED()
ABresult
0X0
100
111
117 ($^O eq 'MSWin32')
118 ? ($^E != Errno::WSAECONNRESET() && $^E != Errno::WSAECONNREFUSED())
119 : ($! != ECONNREFUSED)
120 );
121 2 close $sock;
122 2 $up;
123 }
124  
125  
126 19 ▶sub _make_waiter {
Subroutine _make_waiter: called CC=1 SCAR=0.0
127 19 my $max_wait = shift;
128 19 my $waited = 0;
129 19 my $sleep = 0.001;
130  
131 73 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
132 73 ▶ return 0 if $max_wait >= 0 && $waited > $max_wait;
Branch: 1 branch
BranchTrueFalseTotal
if $max_wait >= 0 and $waited > $max_wait 4 69 73
Condition100%3/3
$max_wait >= 0 and $waited > $max_wait
!ll&&!rl&&r
8614
MC/DC100%2/2
$max_wait >= 0 and $waited > $max_wait
$max_wait >= 0 $waited > $max_wait
Truth table100%3/3
A and B
A= $max_wait >= 0B= $waited > $max_wait
ABresult
0X0
100
111
133  
134 69 Time::HiRes::sleep($sleep);
135 69 $waited += $sleep;
136 69 $sleep *= 2;
137  
138 69 return 1;
139 19 };
140 }
141  
142 19 ▶sub wait_port {
Subroutine wait_port: called CC=11 SCAR=24.0
Pod documented
143 19 my ($host, $port, $max_wait, $proto);
144 19 ▶ if (@_ && ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and ref $_[0] eq 'HASH') { } 15 4 19
elsif (@_ == 4) { } 1 3 4
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
1315
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
145 15 ($host, $port, $max_wait, $proto) = ($_[0]->{host}, $_[0]->{port}, $_[0]->{max_wait}, $_[0]->{proto});
146 } elsif (@_==4) {
147 # backward compat.
148 1 ($port, (my $sleep), (my $retry), $proto) = @_;
149 1 $max_wait = $sleep * $retry;
150 } else {
151 3 ($port, $max_wait, $proto) = @_;
152 }
153 19 ▶ $host = '127.0.0.1' unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 4 15 19
154 19 ▶ $max_wait ||= 10;
Condition100%2/2
$max_wait ||= 10
l!l
172
MC/DC100%1/1
$max_wait ||= 10
$max_wait
Truth table100%2/2
A
A= $max_wait
Aresult
11
00
155 19 ▶ $proto = $proto ? lc($proto) : 'tcp';
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 2 17 19
156 19 my $waiter = _make_waiter($max_wait);
157  
158 19 ▶ while ( $waiter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$waiter()) 69 4 73
159 69 ▶ if ($^O eq 'MSWin32' && defined($port) ? `$^X -MTest::TCP::CheckPort -echeck_port $host $port $proto` : check_port({ host => $host, port => $port, proto => $proto })) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq "MSWin32" && defined $port ? `$^X -MTest::TCP::CheckPort -echeck_port $host $port $proto` : check_port({"host", $host, "port", $port, "proto", $proto})) 14 54 68
$^O eq 'MSWin32' && defined $port ? : 0 69 69
Condition33%1/3
$^O eq "MSWin32" and defined $port
!ll&&!rl&&r
6900
MC/DC0%0/2
$^O eq "MSWin32" and defined $port
$^O eq "MSWin32" defined $port
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= defined $port
ABresult
0X0
100
111
160 14 return 1;
161 }
162 }
163 4 return 0;
164 }
165  
166 19 1;
167  
168 __END__
↑ Summary Test/TCP.pm »