blib/lib/Net/IP/Lite.pm

filter: Statementstmt 98.5% 347 / 352 Branchbran 97.1% 235 / 242 Conditioncond 94.3% 83 / 88 MC/DCmcdc 88.4% 46 / 52 Subroutinesub 96.1% 50 / 52 Podpod 95.2% 20 / 21 totaltotal 96.7% 781 / 807 CC 163 SCAR 27.2
CC
163
Cov
98%
ip2bin
42.0
_bin2ipv4
19.0
_ip_transform
16.0
CRAP 15.1
↑ Summary
1 package Net::IP::Lite;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 23 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 our @EXPORT = qw(
12 ip2bin ip_validate ip_is_ipv4 ip_is_ipv6 ip_is_ipv6ipv4
13 ip_transform ip_equal ip_equal_v4 ip_equal_v6 ip_in_range
14 );
15  
16 23 ▶use constant IPV6IPV4HEAD => '0' x 80 . '1' x 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 our $VERSION = '0.03';
19  
20 693 ▶sub _wrong_ip {
Subroutine _wrong_ip: called CC=2 SCAR=6.9
21 693 my $addr = shift;
22 693 ▶ $addr = 'UNDEFINED' unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 11 682 693
23 693 return "Wrong IP address: '$addr'";
24 }
25  
26 42 ▶sub _wrong_ipv6ipv4 {
Subroutine _wrong_ipv6ipv4: called CC=2 SCAR=7.1
27 42 my $addr = shift;
28 42 ▶ $addr = 'UNDEFINED' unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 0 42 42
29 42 return "Failed to convert IPv6 address '$addr' to IPv4 address";
30 }
31  
32 212 ▶sub _wrong_net {
Subroutine _wrong_net: called CC=2 SCAR=6.9
33 212 my $net = shift;
34 212 ▶ $net = 'UNDEFINED' unless defined $net;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $net 2 210 212
35 212 return "Wrong network definition: '$net'";
36 }
37  
38 10668 ▶sub ip2bin {
Subroutine ip2bin: called CC=42 SCAR=37.4
Pod documented
39 10668 my $addr = shift;
40 10668 ▶ return '' unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 17 10651 10668
41  
42 10651 my $bin = '';
43  
44 10651 ▶ if ($addr =~ /:/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($addr =~ /:/) { } 3087 7564 10651
elsif ($addr =~ /\./) { } 5205 2359 7564
elsif ($addr =~ /^0[0-7]+$/) { } 604 1755 2359
elsif ($addr =~ /^\d+$/) { } 1151 604 1755
elsif ($addr =~ /^0x[0-9a-f]+$/i) { } 544 60 604
45 # IPv6 address
46  
47 3087 ▶ return '0' x 128 if $addr eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if $addr eq "::" 487 2600 3087
48  
49 2600 ▶ return '' if $addr =~ s/^:// && $addr !~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ s/^:// and not $addr =~ /^:/ 230 2370 2600
Condition100%3/3
$addr =~ s/^:// and not $addr =~ /^:/
!ll&&!rl&&r
1737633230
MC/DC100%2/2
$addr =~ s/^:// and not $addr =~ /^:/
$addr =~ s/^:// not $addr =~ /^:/
Truth table100%3/3
A and not(B)
A= $addr =~ s/^://B= not $addr =~ /^:/
ABresult
0X0
100
111
50 2370 ▶ return '' if $addr =~ s/:$// && $addr !~ /:$/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ s/:$// and not $addr =~ /:$/ 141 2229 2370
Condition100%3/3
$addr =~ s/:$// and not $addr =~ /:$/
!ll&&!rl&&r
1947282141
MC/DC100%2/2
$addr =~ s/:$// and not $addr =~ /:$/
$addr =~ s/:$// not $addr =~ /:$/
Truth table100%3/3
A and not(B)
A= $addr =~ s/:$//B= not $addr =~ /:$/
ABresult
0X0
100
111
51  
52 2229 my @words = split(/:/, $addr, -1);
53 2229 my $words_amount = scalar @words;
54  
55 # IPv4 representation
56 2229 ▶ $words_amount++ if $addr =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\./ 458 1771 2229
57  
58 2229 my $reduct = 0;
59 2229 my $i = 0;
60 2229 for my $word (@words) {
61 11841 $i++;
62 11841 ▶ if ($word =~ /\./) {
Branch: 3 branches
BranchTrueFalseTotal
if ($word =~ /\./) { } 318 11523 11841
elsif (not length $word) { } 1336 10187 11523
elsif ($word =~ /^[0-9a-f]+$/i) { } 10110 77 10187
63 # IPv4 representation
64 318 ▶ return '' if $i != scalar @words || $bin ne IPV6IPV4HEAD;
Branch: 1 branch
BranchTrueFalseTotal
if $i != scalar @words or $bin ne "000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111" 100 218 318
Condition100%3/3
$i != scalar @words or $bin ne "000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111"
l!l&&r!l&&!r
4060218
MC/DC100%2/2
$i != scalar @words or $bin ne "000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111"
$i != scalar @words $bin ne "000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111"
Truth table100%3/3
A or B
A= $i != scalar @wordsB= $bin ne "000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111"
ABresult
1X1
011
000
65 218 my @octets = split(/\./, $word);
66 218 ▶ return '' if scalar @octets != 4;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @octets != 4 20 198 218
67 198 for my $octet (@octets) {
68 792 ▶ return '' if $octet !~ /^\d+$/ || $octet > 255;
Branch: 1 branch
BranchTrueFalseTotal
if not $octet =~ /^\d+$/ or $octet > 255 40 752 792
Condition100%3/3
not $octet =~ /^\d+$/ or $octet > 255
l!l&&r!l&&!r
2020752
MC/DC100%2/2
not $octet =~ /^\d+$/ or $octet > 255
not $octet =~ /^\d+$/ $octet > 255
Truth table100%3/3
not(A) or B
A= not $octet =~ /^\d+$/B= $octet > 255
ABresult
1X1
011
000
69 752 $bin .= unpack('B8', pack('C', $octet));
70 }
71 158 return $bin;
72 } elsif (!length $word) {
73 1336 ▶ return '' if $reduct;
Branch: 1 branch
BranchTrueFalseTotal
if $reduct 138 1198 1336
74 1198 $reduct = 1;
75 1198 my $len = (9 - $words_amount) << 4;
76 1198 ▶ return '' unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 25 1173 1198
77 1173 $bin .= '0' x ((9 - $words_amount) << 4);
78 } elsif ($word =~ /^[0-9a-f]+$/i) {
79 10110 $word =~ s/^0+//i;
80 10110 ▶ return '' if length($word) > 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $word > 4 183 9927 10110
81 9927 my $int = hex($word);
82 9927 $bin .= unpack('B16', pack('n', $int));
83 } else {
84 77 return '';
85 }
86 11100 ▶ return '' if length($bin) > 128;
Branch: 1 branch
BranchTrueFalseTotal
if length $bin > 128 0 11100 11100
87 }
88 1488 ▶ return '' if length($bin) < 128;
Branch: 1 branch
BranchTrueFalseTotal
if length $bin < 128 25 1463 1488
89 } elsif ($addr =~ /\./) {
90 # IPv4
91 5205 my @octets = split(/\./, $addr, -1);
92 5205 ▶ return '' if scalar @octets > 4;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @octets > 4 23 5182 5205
93 5182 my $i = 0;
94 5182 for my $octet (@octets) {
95 19805 $i++;
96 19805 my $dec;
97 19805 ▶ if ($octet =~ /^0[0-7]+$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($octet =~ /^0[0-7]+$/) { } 2297 17508 19805
elsif ($octet =~ /^\d+$/) { } 15736 1772 17508
elsif ($octet =~ /^0x[0-9a-f]+$/i) { } 1617 155 1772
98 # Octal octet
99 2297 $octet =~ s/^0+//;
100 2297 ▶ return '' if length($octet) > 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $octet > 3 0 2297 2297
101 2297 $dec = oct($octet);
102 } elsif ($octet =~ /^\d+$/) {
103 # Decimal octet
104 15736 ▶ return '' if length($octet) > 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $octet > 3 23 15713 15736
105 15713 $dec = $octet;
106 } elsif ($octet =~ /^0x[0-9a-f]+$/i) {
107 # Hexadecimal octet
108 1617 $octet =~ s/^0x0*//i;
109 1617 ▶ return '' if length($octet) > 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $octet > 2 23 1594 1617
110 1594 $dec = hex($octet);
111 } else {
112 155 return '';
113 }
114 19604 ▶ return '' if $dec > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $dec > 255 23 19581 19604
115 19581 ▶ if ($i == scalar @octets && $i < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == scalar @octets and $i < 4) 336 19245 19581
Condition100%3/3
$i == scalar @octets and $i < 4
!ll&&!rl&&r
146234622336
MC/DC100%2/2
$i == scalar @octets and $i < 4
$i == scalar @octets $i < 4
Truth table100%3/3
A and B
A= $i == scalar @octetsB= $i < 4
ABresult
0X0
100
111
116 # add missed octets
117 336 $bin .= '0' x ((4 - $i) << 3);
118 }
119 19581 $bin .= unpack('B8', pack('C', $dec));
120 }
121 } elsif ($addr =~ /^0[0-7]+$/) {
122 # Octal IPv4 address
123 604 $addr =~ s/^0+//i;
124 604 ▶ return '' if $addr > 37777777777;
Branch: 1 branch
BranchTrueFalseTotal
if $addr > '37777777777' 23 581 604
125 581 my $int = oct($addr);
126 581 $bin = unpack('B32', pack('N', $int));
127 } elsif ($addr =~ /^\d+$/) {
128 # Decimal IPv4 address
129 1151 ▶ return '' if $addr > 4294967295;
Branch: 1 branch
BranchTrueFalseTotal
if $addr > 4294967295 23 1128 1151
130 1128 $bin = unpack('B32', pack('N', $addr));
131 } elsif ($addr =~ /^0x[0-9a-f]+$/i) {
132 # Hexadecimal IPv4 addres
133 544 $addr =~ s/^0x0*//i;
134 544 ▶ return '' if length($addr) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $addr > 8 23 521 544
135 521 my $int = hex($addr);
136 521 $bin = unpack('B32', pack('N', $int));
137 }
138 8711 return $bin;
139 }
140  
141 12612 ▶sub _ip_validate {
Subroutine _ip_validate: called CC=1 SCAR=0.0
142 12612 return length(shift) > 0;
143 }
144  
145 87 ▶sub ip_validate {
Subroutine ip_validate: called CC=1 SCAR=0.0
Pod documented
146 87 return _ip_validate(ip2bin(shift)) > 0;
147 }
148  
149 5087 ▶sub _ip_is_ipv4 {
Subroutine _ip_is_ipv4: called CC=1 SCAR=0.0
150 5087 return length(shift) eq 32;
151 }
152  
153 87 ▶sub ip_is_ipv4 {
Subroutine ip_is_ipv4: called CC=1 SCAR=0.0
Pod documented
154 87 return _ip_is_ipv4(ip2bin(shift));
155 }
156  
157 4610 ▶sub _ip_type_equal {
Subroutine _ip_type_equal: called CC=1 SCAR=0.0
158 4610 return length(shift) == length(shift);
159 }
160  
161 647 ▶sub _ip_is_ipv6 {
Subroutine _ip_is_ipv6: called CC=1 SCAR=0.0
162 647 return length(shift) eq 128;
163 }
164  
165 87 ▶sub ip_is_ipv6 {
Subroutine ip_is_ipv6: called CC=1 SCAR=0.0
Pod documented
166 87 return _ip_is_ipv6(ip2bin(shift));
167 }
168  
169 536 ▶sub _ip_is_ipv6ipv4 {
Subroutine _ip_is_ipv6ipv4: called CC=1 SCAR=0.0
170 536 return substr(shift, 0, 96) eq IPV6IPV4HEAD;
171 }
172  
173 87 ▶sub ip_is_ipv6ipv4 {
Subroutine ip_is_ipv6ipv4: called CC=1 SCAR=0.0
Pod documented
174 87 return _ip_is_ipv6ipv4(ip2bin(shift));
175 }
176  
177 640 ▶sub _bin2ipv6 {
Subroutine _bin2ipv6: called CC=10 SCAR=23.0
178 640 my ($bin, $lead_zeros, $short) = @_;
179 640 my $result = '';
180 640 my @chunks = $bin =~ m/[0..1]{16}/g;
181  
182 640 my $short_len = 0;
183 640 ▶ if ($short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short) 200 440 640
184 200 my @zero_chunks;
185 200 my $i = 0;
186 200 for my $chunk (@chunks) {
187 1528 ▶ if ($chunk !~ /1/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $chunk =~ /1/) { } 1120 408 1528
188 1120 $zero_chunks[$i]++;
189 } else {
190 408 ▶ $i++ if defined $zero_chunks[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $zero_chunks[$i] 174 234 408
191 }
192 }
193 200 @zero_chunks = sort @zero_chunks;
194 200 ▶ $short_len = pop @zero_chunks if scalar @zero_chunks;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @zero_chunks 188 12 200
195 }
196 640 for my $chunk (@chunks) {
197 4960 my $word = unpack('H4', pack('B16', $chunk)) . ':';
198 4960 ▶ $word =~ s/^0{1,3}// unless $lead_zeros;
Branch: 1 branch
BranchTrueFalseTotal
unless $lead_zeros 4120 840 4960
199 4960 $result .= $word;
200 }
201  
202 640 ▶ $result =~ s/(^|:)(0{1,4}:){$short_len}/::/ if $short_len > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $short_len > 1 188 452 640
203 640 ▶ $result =~ s/:$// if $result !~ /::$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /::$/ 576 64 640
204 640 return $result;
205 }
206  
207 730 ▶sub _bin2ipv4 {
Subroutine _bin2ipv4: called CC=19 SCAR=29.4
208 730 my ($bin, $format, $lead_zeros, $short) = @_;
209  
210 730 ▶ $format ||= '';
Condition100%2/2
$format ||= ""
l!l
176554
MC/DC100%1/1
$format ||= ""
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
211 730 my @chunks = $bin =~ m/[0..1]{8}/g;
212  
213 730 ▶ if ($format =~ /^(D|O|X)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(D|O|X)$/) { } 72 658 730
214 72 my $result = 0;
215 72 my $i = 0;
216 72 for my $chunk (reverse @chunks) {
217 288 $result += unpack('C', pack('B8', $chunk)) << $i;
218 288 $i +=8;
219 }
220  
221 72 ▶ return sprintf("%#1o", $result) if $format eq 'O';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "O" 24 48 72
222 48 ▶ return sprintf("0x%.8x", $result) if $format eq 'X' && $lead_zeros;
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "X" and $lead_zeros 8 40 48
Condition100%3/3
$format eq "X" and $lead_zeros
!ll&&!rl&&r
20208
MC/DC100%2/2
$format eq "X" and $lead_zeros
$format eq "X" $lead_zeros
Truth table100%3/3
A and B
A= $format eq "X"B= $lead_zeros
ABresult
0X0
100
111
223 40 ▶ return sprintf("0x%x", $result) if $format eq 'X';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "X" 20 20 40
224 20 return $result;
225 } else {
226 658 my $result = '';
227 658 my $f = '';
228 658 ▶ if ($format eq 'o') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'o') { } 40 618 658
elsif ($format eq 'x') { } 48 570 618
229 40 $f = '%#.1o';
230 } elsif ($format eq 'x') {
231 48 ▶ $f = $lead_zeros ? '0x%.2x' : '0x%x' ;
Branch: 1 branch
BranchTrueFalseTotal
$lead_zeros ? : 16 32 48
232 }
233  
234 658 my $i = 4;
235 658 for my $chunk (reverse @chunks) {
236 2632 $i--;
237 2632 my $octet = unpack('C', pack('B8', $chunk));
238 2632 ▶ if ($short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short) 452 2180 2632
239 452 ▶ next if (!$octet && $i && $i < 3 );
Branch: 1 branch
BranchTrueFalseTotal
if not $octet and $i and $i < 3 210 242 452
Condition100%6/6
not $octet and $i and $i < 3
!ll&&!rl&&r
19250210
not $octet and $i
!ll&&!rl&&r
14250260
MC/DC100%3/3
not $octet and $i and $i < 3
not $octet $i $i < 3
Truth table100%4/4
not(A) and B and C
A= not $octetB= $iC= $i < 3
ABCresult
0XX0
10X0
1100
1111
240 242 ▶ $short = 0 if $i == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 2 10 232 242
241 }
242 2422 ▶ if ($format eq 'o') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'o') { } 128 2294 2422
elsif ($format eq 'x') { } 158 2136 2294
243 128 $octet = sprintf($f, $octet);
244 } elsif ($format eq 'x') {
245 158 $octet = sprintf($f, $octet);
246 }
247 2422 $result = "$octet.$result";
248 }
249 658 $result =~ s/\.$//;
250 658 return $result;
251 }
252 }
253  
254 352 ▶sub _reverse {
Subroutine _reverse: called CC=2 SCAR=6.9
255 352 my $bin = shift;
256 352 ▶ my $len = _ip_is_ipv4($bin) ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
_ip_is_ipv4($bin) ? : 210 142 352
257 352 my @chunks = $bin =~ m/[0..1]{$len}/g;
258 352 return join('', reverse @chunks);
259 }
260  
261 704 ▶sub _ip_transform {
Subroutine _ip_transform: called CC=16 SCAR=27.7
262 704 my ($bin, $opts) = @_;
263  
264 704 ▶ my $format = $opts->{format_ipv4} || '';
Condition100%2/2
$opts->{'format_ipv4'} || ''
l!l
256448
MC/DC100%1/1
$opts->{'format_ipv4'} || ''
$opts->{'format_ipv4'}
Truth table100%2/2
A
A= $opts->{'format_ipv4'}
Aresult
11
00
265 704 ▶ my $convert_to = $opts->{convert_to} || '';
Condition100%2/2
$opts->{'convert_to'} || ''
l!l
240464
MC/DC100%1/1
$opts->{'convert_to'} || ''
$opts->{'convert_to'}
Truth table100%2/2
A
A= $opts->{'convert_to'}
Aresult
11
00
266  
267 704 my $ipv6;
268 my $ipv4;
269  
270 704 ▶ if ($convert_to eq 'ipv4' && _ip_is_ipv6ipv4($bin)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_to eq "ipv4" and _ip_is_ipv6ipv4($bin)) 36 668 704
Condition100%3/3
$convert_to eq "ipv4" and _ip_is_ipv6ipv4($bin)
!ll&&!rl&&r
6442436
MC/DC100%2/2
$convert_to eq "ipv4" and _ip_is_ipv6ipv4($bin)
$convert_to eq "ipv4" _ip_is_ipv6ipv4($bin)
Truth table100%3/3
A and B
A= $convert_to eq "ipv4"B= _ip_is_ipv6ipv4($bin)
ABresult
0X0
100
111
271 # convert ipv6ipv4 to ipv4
272 36 $bin = substr($bin, 96, 32);
273 }
274  
275 704 ▶ if (($convert_to eq 'ipv6' || $convert_to eq 'ipv6ipv4') && _ip_is_ipv4($bin)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_to eq "ipv6" || $convert_to eq "ipv6ipv4" and _ip_is_ipv4($bin)) 136 568 704
Condition100%6/6
$convert_to eq "ipv6" || $convert_to eq "ipv6ipv4" and _ip_is_ipv4($bin)
!ll&&!rl&&r
52444136
$convert_to eq "ipv6" or $convert_to eq "ipv6ipv4"
l!l&&r!l&&!r
8892524
MC/DC100%3/3
$convert_to eq "ipv6" || $convert_to eq "ipv6ipv4" and _ip_is_ipv4($bin)
$convert_to eq "ipv6" $convert_to eq "ipv6ipv4" _ip_is_ipv4($bin)
Truth table100%5/5
A or B and C
A= $convert_to eq "ipv6"B= $convert_to eq "ipv6ipv4"C= _ip_is_ipv4($bin)
ABCresult
00X0
1X00
0100
1X11
0111
276 # convert ipv4 to ipv6
277 136 $bin = IPV6IPV4HEAD . $bin;
278 }
279  
280 704 ▶ if ($convert_to eq 'ipv6ipv4' && _ip_is_ipv6ipv4($bin)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_to eq 'ipv6ipv4' and _ip_is_ipv6ipv4($bin)) { } 80 624 704
Condition100%3/3
$convert_to eq "ipv6ipv4" and _ip_is_ipv6ipv4($bin)
!ll&&!rl&&r
6121280
MC/DC100%2/2
$convert_to eq "ipv6ipv4" and _ip_is_ipv6ipv4($bin)
$convert_to eq "ipv6ipv4" _ip_is_ipv6ipv4($bin)
Truth table100%3/3
A and B
A= $convert_to eq "ipv6ipv4"B= _ip_is_ipv6ipv4($bin)
ABresult
0X0
100
111
281 # convert ipv4 to ipv6ipv4
282 80 $ipv4 = substr($bin, 96, 32);
283 80 ▶ $ipv4 = _reverse($ipv4) if $opts->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"reverse"} 40 40 80
284 80 $bin = IPV6IPV4HEAD;
285 } else {
286 624 ▶ $bin = _reverse($bin) if $opts->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"reverse"} 312 312 624
287 }
288  
289 704 my $result = '';
290 704 ▶ if (length($bin) > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin > 32) 366 338 704
291 # IPv6
292 366 $result = _bin2ipv6($bin, $opts->{lead_zeros}, $opts->{short_ipv6});
293 366 ▶ if ($ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipv4) 80 286 366
294 80 $bin = $ipv4;
295 80 $ipv6 = $result;
296 }
297 }
298  
299 704 ▶ if (length($bin) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin == 32) 418 286 704
300 # IPv4
301 418 ▶ if ($ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipv6) { } 80 338 418
302 80 $result = "$ipv6:" . _bin2ipv4($bin);
303 } else {
304 338 $result = _bin2ipv4($bin, $format, $opts->{lead_zeros}, $opts->{short_ipv4});
305 }
306 }
307  
308 704 return $result;
309 }
310  
311 416 ▶sub ip_transform {
Subroutine ip_transform: called CC=4 SCAR=13.9
Pod documented
312 416 my ($addr, $opts) = @_;
313  
314 416 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
32888
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
315 416 ▶ croak 'Options must be a hash' unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 1 415 416
316  
317 415 my $bin = ip2bin($addr);
318 415 ▶ croak _wrong_ip($addr) unless _ip_validate($bin);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin) 63 352 415
319 352 return _ip_transform($bin, $opts);
320 }
321  
322 2951 ▶sub _ip_equal {
Subroutine _ip_equal: called CC=1 SCAR=0.0
323 2951 my ($bin1, $bin2) = @_;
324 2951 return $bin1 eq $bin2;
325 }
326  
327 157 ▶sub ip_equal {
Subroutine ip_equal: called CC=3 SCAR=11.0
Pod documented
328 157 my ($addr1, $addr2) = @_;
329 157 my $bin1 = ip2bin($addr1);
330 157 my $bin2 = ip2bin($addr2);
331  
332 157 ▶ croak _wrong_ip($addr1) unless _ip_validate($bin1);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin1) 63 94 157
333 94 ▶ croak _wrong_ip($addr2) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 63 31 94
334  
335 31 return $bin1 eq $bin2;
336 }
337  
338 93 ▶sub _ip_equal_v4 {
Subroutine _ip_equal_v4: called CC=3 SCAR=11.0
339 93 my ($bin1, $bin2) = @_;
340 93 ▶ $bin1 = substr($bin1, 96, 32) if _ip_is_ipv6ipv4($bin1);
Branch: 1 branch
BranchTrueFalseTotal
if _ip_is_ipv6ipv4($bin1) 21 72 93
341 93 ▶ $bin2 = substr($bin2, 96, 32) if _ip_is_ipv6ipv4($bin2);
Branch: 1 branch
BranchTrueFalseTotal
if _ip_is_ipv6ipv4($bin2) 24 69 93
342 93 return _ip_equal($bin1, $bin2);
343 }
344  
345 177 ▶sub ip_equal_v4 {
Subroutine ip_equal_v4: called CC=7 SCAR=19.5
Pod documented
346 177 my ($addr1, $addr2) = @_;
347 177 my $bin1 = ip2bin($addr1);
348 177 my $bin2 = ip2bin($addr2);
349  
350 177 ▶ croak _wrong_ip($addr1) unless _ip_validate($bin1);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin1) 0 177 177
351 177 ▶ croak _wrong_ip($addr2) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 126 51 177
352  
353 51 ▶ if ((_ip_is_ipv6($bin1) && ! _ip_is_ipv6ipv4($bin1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ip_is_ipv6($bin1) and not _ip_is_ipv6ipv4($bin1)) 11 40 51
Condition100%3/3
_ip_is_ipv6($bin1) and not _ip_is_ipv6ipv4($bin1)
!ll&&!rl&&r
33711
MC/DC100%2/2
_ip_is_ipv6($bin1) and not _ip_is_ipv6ipv4($bin1)
_ip_is_ipv6($bin1) not _ip_is_ipv6ipv4($bin1)
Truth table100%3/3
A and not(B)
A= _ip_is_ipv6($bin1)B= not _ip_is_ipv6ipv4($bin1)
ABresult
0X0
100
111
354 11 croak _wrong_ipv6ipv4($addr1);
355 }
356  
357 40 ▶ if ((_ip_is_ipv6($bin2) && ! _ip_is_ipv6ipv4($bin2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)) 9 31 40
Condition100%3/3
_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)
!ll&&!rl&&r
2389
MC/DC100%2/2
_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)
_ip_is_ipv6($bin2) not _ip_is_ipv6ipv4($bin2)
Truth table100%3/3
A and not(B)
A= _ip_is_ipv6($bin2)B= not _ip_is_ipv6ipv4($bin2)
ABresult
0X0
100
111
358 9 croak _wrong_ipv6ipv4($addr2);
359 }
360  
361 31 return _ip_equal_v4($bin1, $bin2);
362 }
363  
364 114 ▶sub _ip_equal_v6 {
Subroutine _ip_equal_v6: called CC=3 SCAR=11.0
365 114 my ($bin1, $bin2) = @_;
366 114 ▶ $bin1 = IPV6IPV4HEAD . $bin1 if _ip_is_ipv4($bin1);
Branch: 1 branch
BranchTrueFalseTotal
if _ip_is_ipv4($bin1) 72 42 114
367 114 ▶ $bin2 = IPV6IPV4HEAD . $bin2 if _ip_is_ipv4($bin2);
Branch: 1 branch
BranchTrueFalseTotal
if _ip_is_ipv4($bin2) 69 45 114
368 114 return _ip_equal($bin1, $bin2);
369 }
370  
371 164 ▶sub ip_equal_v6 {
Subroutine ip_equal_v6: called CC=3 SCAR=11.0
Pod documented
372 164 my ($addr1, $addr2) = @_;
373 164 my $bin1 = ip2bin($addr1);
374 164 my $bin2 = ip2bin($addr2);
375  
376 164 ▶ croak _wrong_ip($addr1) unless _ip_validate($bin1);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin1) 0 164 164
377 164 ▶ croak _wrong_ip($addr2) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 126 38 164
378  
379 38 return _ip_equal_v6($bin1, $bin2);
380 }
381  
382 2682 ▶sub __ip_in_range {
Subroutine __ip_in_range: called CC=2 SCAR=6.9
383 2682 my ($bin_addr, $bin_net, $bin_mask) = @_;
384  
385 2682 my @addr_bits = split(//, $bin_addr);
386 2682 my @mask_bits = split(//, $bin_mask);
387 2682 my $result = '';
388  
389 2682 my $i = 0;
390 2682 for my $bit (@addr_bits) {
391 130560 $result .= $bit & $mask_bits[$i];
392 130560 $i++;
393 }
394  
395 2682 return _ip_equal($bin_net, $result);
396 }
397  
398 2098 ▶sub _ip_in_range {
Subroutine _ip_in_range: called CC=11 SCAR=24.0
399 2098 my ($bin_addr, $net, $addr) = @_;
400  
401 2098 my $bin_net;
402 my $bin_mask;
403  
404 2098 ▶ if ($net =~ /^(.+)\/(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($net =~ m[^(.+)/(\d+)$]) { } 796 1302 2098
elsif ($net =~ /^(\S+)\s+(\S+)$/) { } 474 828 1302
405 796 my $mask = $2;
406 796 $bin_net = ip2bin($1);
407 796 ▶ croak _wrong_net($net) unless _ip_validate($bin_net);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin_net) 0 796 796
408  
409 796 ▶ my $mask_len = _ip_is_ipv4($bin_net) ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
_ip_is_ipv4($bin_net) ? : 606 190 796
410 796 ▶ croak _wrong_net($net) if $mask > $mask_len;
Branch: 1 branch
BranchTrueFalseTotal
if $mask > $mask_len 4 792 796
411  
412 792 $bin_mask = '1' x $mask . '0' x ($mask_len - $mask);
413 } elsif ($net =~ /^(\S+)\s+(\S+)$/) {
414 474 $bin_net = ip2bin($1);
415 474 $bin_mask = ip2bin($2);
416 } else {
417 828 $bin_net = ip2bin($net);
418 828 ▶ $bin_mask = '1' x (_ip_is_ipv4($bin_net) ? 32 : 128);
Branch: 1 branch
BranchTrueFalseTotal
_ip_is_ipv4($bin_net) ? : 554 274 828
419 }
420  
421 2094 ▶ unless (_ip_validate($bin_net) && _ip_validate($bin_mask) && _ip_type_equal($bin_net, $bin_mask)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_ip_validate($bin_net) and _ip_validate($bin_mask) and _ip_type_equal($bin_net, $bin_mask)) 206 1888 2094
Condition100%6/6
_ip_validate($bin_net) and _ip_validate($bin_mask) and _ip_type_equal($bin_net, $bin_mask)
!ll&&!rl&&r
20061888
_ip_validate($bin_net) and _ip_validate($bin_mask)
!ll&&!rl&&r
126741894
MC/DC100%3/3
_ip_validate($bin_net) and _ip_validate($bin_mask) and _ip_type_equal($bin_net, $bin_mask)
_ip_validate($bin_net) _ip_validate($bin_mask) _ip_type_equal($bin_net, $bin_mask)
Truth table100%4/4
A and B and C
A= _ip_validate($bin_net)B= _ip_validate($bin_mask)C= _ip_type_equal($bin_net, $bin_mask)
ABCresult
0XX0
10X0
1100
1111
422 206 croak _wrong_net($net);
423 }
424  
425 1888 ▶ return 0 unless _ip_type_equal($bin_addr, $bin_net);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_type_equal($bin_addr, $bin_net) 18 1870 1888
426  
427 1870 return __ip_in_range($bin_addr, $bin_net, $bin_mask);
428 }
429  
430 1403 ▶sub ip_in_range {
Subroutine ip_in_range: called CC=6 SCAR=17.9
Pod documented
431 1403 my ($addr, $range) = @_;
432  
433 1403 ▶ croak _wrong_net($range) unless defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 1 1402 1403
434  
435 1402 ▶ if (ref($range) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $range eq "ARRAY") 410 992 1402
436 410 for my $net (@$range) {
437 617 ▶ return 1 if ip_in_range($addr, $net);
Branch: 1 branch
BranchTrueFalseTotal
if ip_in_range($addr, $net) 303 314 617
438 }
439 107 return 0;
440 }
441  
442 992 my $bin_addr = ip2bin($addr);
443 992 ▶ croak _wrong_ip($addr) unless _ip_validate($bin_addr);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin_addr) 63 929 992
444  
445 929 return _ip_in_range($bin_addr, $range, $addr);
446 }
447  
448 4533 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
449 4533 my ($class, $addr) = @_;
450  
451 4533 my $bin = ip2bin($addr);
452 4533 ▶ return 0 unless _ip_validate($bin);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin) 162 4371 4533
453  
454 4371 my $self = {
455 bin => $bin,
456 addr => $addr
457 };
458  
459 4371 bless $self, $class;
460 4371 return $self;
461 }
462  
463 0 ▶sub _set_binary {
Subroutine _set_binary: not called CC=1 SCAR=6.9
464 0 my ($self, $bin) = @_;
465 0 $self->{bin} = $bin;
466 0 $self->{addr} = $self->transform();
467 }
468  
469 1182 ▶sub address {
Subroutine address: called CC=1 SCAR=0.0
Pod documented
470 1182 my $self = shift;
471 1182 return $self->{addr};
472 }
473  
474 8377 ▶sub binary {
Subroutine binary: called CC=1 SCAR=0.0
Pod documented
475 8377 my $self = shift;
476 8377 return $self->{bin};
477 }
478  
479 2616 ▶sub is_ipv4 {
Subroutine is_ipv4: called CC=1 SCAR=0.0
Pod documented
480 2616 my $self = shift;
481 2616 return _ip_is_ipv4($self->binary);
482 }
483  
484 398 ▶sub is_ipv6 {
Subroutine is_ipv6: called CC=1 SCAR=0.0
Pod documented
485 398 my $self = shift;
486 398 return _ip_is_ipv6($self->binary);
487 }
488  
489 51 ▶sub is_ipv6ipv4 {
Subroutine is_ipv6ipv4: called CC=1 SCAR=0.0
Pod documented
490 51 my $self = shift;
491 51 return _ip_is_ipv6ipv4($self->binary);
492 }
493  
494 353 ▶sub transform {
Subroutine transform: called CC=3 SCAR=11.0
Pod documented
495 353 my ($self, $opts) = @_;
496 353 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
32825
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
497 353 ▶ croak 'Options must be a hash' unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 1 352 353
498 352 return _ip_transform($self->binary, $opts);
499 }
500  
501 125 ▶sub equal {
Subroutine equal: called CC=4 SCAR=13.9
Pod documented
502 125 my ($self, $addr) = @_;
503  
504 125 ▶ if (blessed($addr) && $addr->isa('Net::IP::Lite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($addr) and $addr->isa("Net::IP::Lite")) 31 94 125
Condition66%2/3
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
!ll&&!rl&&r
94031
MC/DC50%1/2
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
builtin::blessed($addr) $addr->isa("Net::IP::Lite")
Truth table66%2/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Net::IP::Lite")
ABresult
0X0
100
111
505 31 return _ip_equal($self->binary, $addr->binary);
506 }
507  
508 94 my $bin2 = ip2bin($addr);
509 94 ▶ croak _wrong_ip($addr) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 63 31 94
510  
511 31 return _ip_equal($self->binary, $bin2);
512 }
513  
514 147 ▶sub equal_v4 {
Subroutine equal_v4: called CC=8 SCAR=20.8
Pod documented
515 147 my ($self, $addr) = @_;
516  
517 147 ▶ my $bin2 = (blessed($addr) && $addr->isa('Net::IP::Lite')) ? $addr->binary : ip2bin($addr);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($addr) && $addr->isa('Net::IP::Lite') ? : 31 116 147
Condition66%2/3
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
!ll&&!rl&&r
116031
MC/DC50%1/2
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
builtin::blessed($addr) $addr->isa("Net::IP::Lite")
Truth table66%2/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Net::IP::Lite")
ABresult
0X0
100
111
518 147 ▶ croak _wrong_ip($addr) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 63 84 147
519  
520 84 ▶ if (($self->is_ipv6() && ! $self->is_ipv6ipv4())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_ipv6 and not $self->is_ipv6ipv4) 13 71 84
Condition100%3/3
$self->is_ipv6 and not $self->is_ipv6ipv4
!ll&&!rl&&r
571413
MC/DC100%2/2
$self->is_ipv6 and not $self->is_ipv6ipv4
$self->is_ipv6 not $self->is_ipv6ipv4
Truth table100%3/3
A and not(B)
A= $self->is_ipv6B= not $self->is_ipv6ipv4
ABresult
0X0
100
111
521 13 croak _wrong_ipv6ipv4($self->address);
522 }
523  
524 71 ▶ if ((_ip_is_ipv6($bin2) && ! _ip_is_ipv6ipv4($bin2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)) 9 62 71
Condition100%3/3
_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)
!ll&&!rl&&r
46169
MC/DC100%2/2
_ip_is_ipv6($bin2) and not _ip_is_ipv6ipv4($bin2)
_ip_is_ipv6($bin2) not _ip_is_ipv6ipv4($bin2)
Truth table100%3/3
A and not(B)
A= _ip_is_ipv6($bin2)B= not _ip_is_ipv6ipv4($bin2)
ABresult
0X0
100
111
525 9 croak _wrong_ipv6ipv4($addr);
526 }
527  
528 62 return _ip_equal_v4($self->binary, $bin2);
529 }
530  
531 139 ▶sub equal_v6 {
Subroutine equal_v6: called CC=4 SCAR=13.9
Pod documented
532 139 my ($self, $addr) = @_;
533  
534 139 ▶ my $bin2 = (blessed($addr) && $addr->isa('Net::IP::Lite')) ? $addr->binary : ip2bin($addr);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($addr) && $addr->isa('Net::IP::Lite') ? : 38 101 139
Condition66%2/3
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
!ll&&!rl&&r
101038
MC/DC50%1/2
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
builtin::blessed($addr) $addr->isa("Net::IP::Lite")
Truth table66%2/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Net::IP::Lite")
ABresult
0X0
100
111
535  
536 139 ▶ croak _wrong_ip($addr) unless _ip_validate($bin2);
Branch: 1 branch
BranchTrueFalseTotal
unless _ip_validate($bin2) 63 76 139
537  
538 76 return _ip_equal_v6($self->binary, $bin2);
539 }
540  
541 2234 ▶sub in_range {
Subroutine in_range: called CC=7 SCAR=19.5
Pod documented
542 2234 my ($self, $range) = @_;
543  
544 2234 ▶ croak _wrong_net($range) unless defined $range;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 1 2233 2234
545  
546 2233 ▶ if (ref($range) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $range eq "ARRAY") 650 1583 2233
547 650 for my $net (@$range) {
548 1097 ▶ return 1 if $self->in_range($net);
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_range($net) 423 674 1097
549 }
550 227 return 0;
551 }
552  
553 1583 ▶ if (blessed($range) && $range->isa('Net::IP::Lite::Net')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($range) and $range->isa("Net::IP::Lite::Net")) 414 1169 1583
Condition66%2/3
builtin::blessed($range) and $range->isa("Net::IP::Lite::Net")
!ll&&!rl&&r
11690414
MC/DC50%1/2
builtin::blessed($range) and $range->isa("Net::IP::Lite::Net")
builtin::blessed($range) $range->isa("Net::IP::Lite::Net")
Truth table66%2/3
A and B
A= builtin::blessed($range)B= $range->isa("Net::IP::Lite::Net")
ABresult
0X0
100
111
554 414 return $range->contains($self);
555 }
556  
557 1169 return _ip_in_range($self->binary, $range, $self->address);
558 }
559  
560 package Net::IP::Lite::Net;
561 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
562 23 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
563  
564 23 ▶use base qw(Net::IP::Lite);
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 1104 ▶sub new {
Subroutine new: called CC=16 SCAR=27.7
567 1104 my ($class, $net) = @_;
568  
569 1104 ▶ return 0 unless defined $net;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $net 1 1103 1104
570  
571 1103 my $self = {};
572 1103 my $bin_mask;
573  
574 1103 ▶ if ($net =~ /^(.+)\/(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($net =~ m[^(.+)/(\d+)$]) { } 511 592 1103
elsif ($net =~ /^(\S+)\s+(\S+)$/) { } 454 138 592
575 511 my $mask = $2;
576 511 $self = $class->SUPER::new($1);
577  
578 511 ▶ my $mask_len = $self->is_ipv4() ? 32 : 128;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_ipv4 ? : 261 250 511
579 511 ▶ return 0 if $mask > $mask_len;
Branch: 1 branch
BranchTrueFalseTotal
if $mask > $mask_len 2 509 511
580  
581 509 $bin_mask = '1' x $mask . '0' x ($mask_len - $mask);
582 } elsif ($net =~ /^(\S+)\s+(\S+)$/) {
583 454 $self = $class->SUPER::new($1);
584 454 ▶ return 0 unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 1 453 454
585 453 $self->{mask} = Net::IP::Lite->new($2);
586 } else {
587 138 $self = $class->SUPER::new($net);
588 138 ▶ return 0 unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 61 77 138
589 77 ▶ $bin_mask = '1' x ($self->is_ipv4 ? 32 : 128);
Branch: 1 branch
BranchTrueFalseTotal
$self->is_ipv4 ? : 52 25 77
590 }
591  
592 1039 ▶ if ($bin_mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin_mask) 586 453 1039
593 586 ▶ if (length($bin_mask) == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin_mask == 32) { } 312 274 586
594 312 $self->{mask} = Net::IP::Lite->new(Net::IP::Lite::_bin2ipv4($bin_mask));
595 } else {
596 274 $self->{mask} = Net::IP::Lite->new(Net::IP::Lite::_bin2ipv6($bin_mask));
597 }
598 }
599  
600 1039 ▶ return 0 unless $self->{mask};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mask"} 37 1002 1039
601  
602 1002 my $ipv4_mask = $self->{mask}->is_ipv4;
603 1002 ▶ return 0 unless (($self->is_ipv4 && $ipv4_mask) || ($self->is_ipv6 && ! $ipv4_mask));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_ipv4 and $ipv4_mask or $self->is_ipv6 and not $ipv4_mask 3 999 1002
Condition100%9/9
$self->is_ipv4 and $ipv4_mask or $self->is_ipv6 and not $ipv4_mask
l!l&&r!l&&!r
7122873
$self->is_ipv4 and $ipv4_mask
!ll&&!rl&&r
2891712
$self->is_ipv6 and not $ipv4_mask
!ll&&!rl&&r
12287
MC/DC75%3/4
$self->is_ipv4 and $ipv4_mask or $self->is_ipv6 and not $ipv4_mask
$self->is_ipv4 $ipv4_mask $self->is_ipv6 not $ipv4_mask
Truth table57%4/7
A and B or C and not(D)
A= $self->is_ipv4B= $ipv4_maskC= $self->is_ipv6D= not $ipv4_mask
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
604  
605 999 $self->{net} = $net;
606 999 return $self;
607 }
608  
609 962 ▶sub mask {
Subroutine mask: called CC=1 SCAR=0.0
610 962 my ($self) = @_;
611 962 return $self->{mask};
612 }
613  
614 0 ▶sub network {
Subroutine network: not called CC=1 SCAR=6.9
615 0 my ($self) = @_;
616 0 return $self->{net};
617 }
618  
619 828 ▶sub contains {
Subroutine contains: called CC=6 SCAR=18.0
620 828 my ($self, $addr) = @_;
621  
622 828 ▶ if (blessed($addr) && $addr->isa('Net::IP::Lite')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($addr) and $addr->isa('Net::IP::Lite')) { } 414 414 828
Condition66%2/3
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
!ll&&!rl&&r
4140414
MC/DC50%1/2
builtin::blessed($addr) and $addr->isa("Net::IP::Lite")
builtin::blessed($addr) $addr->isa("Net::IP::Lite")
Truth table66%2/3
A and B
A= builtin::blessed($addr)B= $addr->isa("Net::IP::Lite")
ABresult
0X0
100
111
623 414 ▶ return 0 unless Net::IP::Lite::_ip_type_equal($self->binary, $addr->binary);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::IP::Lite::_ip_type_equal($self->binary, $addr->binary) 8 406 414
624 406 return Net::IP::Lite::__ip_in_range($addr->binary, $self->binary, $self->mask->binary);
625 } else {
626 414 my $bin_addr = Net::IP::Lite::ip2bin($addr);
627 414 ▶ croak Net::IP::Lite::_wrong_ip($addr) unless Net::IP::Lite::_ip_validate($bin_addr);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::IP::Lite::_ip_validate($bin_addr) 0 414 414
628 414 ▶ return 0 unless Net::IP::Lite::_ip_type_equal($self->binary, $bin_addr);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::IP::Lite::_ip_type_equal($self->binary, $bin_addr) 8 406 414
629 406 return Net::IP::Lite::__ip_in_range($bin_addr, $self->binary, $self->mask->binary);
630 }
631 }
632  
633  
634 23 1;
635  
636 __END__
↑ Summary