Set/IPv6.pm

filter: Statementstmt 86.9% 73 / 84 Branchbran 63.3% 38 / 60 Conditioncond 60.0% 12 / 20 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 4 totaltotal 71.0% 140 / 197 CC 41 SCAR 26.1
CC
41
Cov
75%
_pack_ipv4
30.0
_pack
25.9
_encode
9.2
CRAP 13.6
↑ Summary Set/IPv4.pm »
1 package Net::CIDR::Set::IPv6;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Encode / decode IPv6 addresses
10  
11 9 our $VERSION = '0.23';
12  
13 42 ▶sub new { bless \my $x, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14  
15 9 my $DEC = "(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})";
16  
17 0 ▶sub _pack_ipv4 {
Subroutine _pack_ipv4: not called CC=5 SCAR=34.0
18 0 my @nums = split /[.]/, shift, -1;
19 0 ▶ return unless @nums == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums == 4 0 0 0
20 0 for ( @nums ) {
21 0 ▶ return unless /\A${DEC}\z/ and !/\A0[1-9]+/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A$DEC\z/ and not /\A0[1-9]+/ 0 0 0
Condition0%0/3
/\A$DEC\z/ and not /\A0[1-9]+/
!ll&&!rl&&r
000
MC/DC0%0/2
/\A$DEC\z/ and not /\A0[1-9]+/
/\A$DEC\z/ not /\A0[1-9]+/
Truth table0%0/3
A and not(B)
A= /\A$DEC\z/B= not /\A0[1-9]+/
ABresult
0X0
100
111
22 }
23 0 return pack "CC*", 0, @nums;
24 }
25  
26 0 ▶sub _426 {
Subroutine _426: not called CC=2 SCAR=17.9
27 0 my @nums = split /[.]/, shift, -1;
28 0 ▶ return if grep $_ > 255, @nums;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ > 255), @nums) 0 0 0
29 0 return join( ":", unpack( 'H*', pack 'C*', @nums ) =~ /..../g );
30 }
31  
32 112 ▶sub _pack {
Subroutine _pack: called CC=17 SCAR=32.6
33 112 my $ip = shift;
34 112 ▶ return pack( 'H*', '0' x 33 ) if $ip eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if $ip eq "::" 3 109 112
35 109 ▶ return if $ip =~ /\A:/ and $ip !~ s/\A::/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /\A:/ and not $ip =~ s/\A::/:/ 0 109 109
Condition66%2/3
$ip =~ /\A:/ and not $ip =~ s/\A::/:/
!ll&&!rl&&r
10720
MC/DC0%0/2
$ip =~ /\A:/ and not $ip =~ s/\A::/:/
$ip =~ /\A:/ not $ip =~ s/\A::/:/
Truth table66%2/3
A and not(B)
A= $ip =~ /\A:/B= not $ip =~ s/\A::/:/
ABresult
0X0
100
111
36 109 ▶ return if $ip =~ /:\z/ and $ip !~ s/::\z/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /:\z/ and not $ip =~ s/::\z/:/ 0 109 109
Condition66%2/3
$ip =~ /:\z/ and not $ip =~ s/::\z/:/
!ll&&!rl&&r
12970
MC/DC0%0/2
$ip =~ /:\z/ and not $ip =~ s/::\z/:/
$ip =~ /:\z/ not $ip =~ s/::\z/:/
Truth table66%2/3
A and not(B)
A= $ip =~ /:\z/B= not $ip =~ s/::\z/:/
ABresult
0X0
100
111
37 109 my @nums = split /:/, $ip, -1;
38 109 ▶ return unless @nums <= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums <= 8 0 109 109
39 109 my ( $empty, $ipv4, $str ) = ( 0, '', '' );
40 109 for ( @nums ) {
41 469 ▶ return if $ipv4;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 0 469 469
42 469 ▶ $str .= "0" x ( 4 - length ) . $_, next if /\A[A-Fa-f0-9]{1,4}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A[A-Fa-f0-9]{1,4}\z/ 370 99 469
43 99 ▶ do { return if $empty++ }, $str .= "X", next if $_ eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $_ eq "" 99 0 99
if $empty++ 0 99 99
44 0 ▶ next if $ipv4 = _pack_ipv4( $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 = _pack_ipv4($_) 0 0 0
45 0 return;
46 }
47 109 ▶ return if $ipv4 and @nums > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 and @nums > 6 0 109 109
Condition33%1/3
$ipv4 and @nums > 6
!ll&&!rl&&r
10900
MC/DC0%0/2
$ipv4 and @nums > 6
$ipv4 @nums > 6
Truth table33%1/3
A and B
A= $ipv4B= @nums > 6
ABresult
0X0
100
111
48 109 ▶ $str =~ s/X/"0" x (($ipv4 ? 25 : 33)-length($str))/e if $empty;
Branch: 2 branches
BranchTrueFalseTotal
if $empty 99 10 109
$ipv4 ? : 0 99 99
49 109 return pack( "H*", "00" . $str ) . $ipv4;
50 }
51  
52 45 ▶sub _unpack {
Subroutine _unpack: called CC=1 SCAR=0.0
53 45 return _compress_ipv6(
54 join( ":", unpack( "xH*", shift ) =~ /..../g ) );
55 }
56  
57 # Replace longest run of null blocks with a double colon
58 45 ▶sub _compress_ipv6 {
Subroutine _compress_ipv6: called CC=4 SCAR=13.9
59 45 my $ip = shift;
60 45 ▶ if ( my @runs = $ip =~ /((?:(?:\A|:)(?:0000))+:?)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@runs) = $ip =~ /((?:(?:\A|:)(?:0000))+:?)/g) 38 7 45
61 38 my $max = $runs[0];
62 38 for ( @runs[ 1 .. $#runs ] ) {
63 2 ▶ $max = $_ if length( $max ) < length;
Branch: 1 branch
BranchTrueFalseTotal
if length $max < length $_ 0 2 2
64 }
65 38 $ip =~ s/$max/::/;
66 }
67 45 $ip =~ s/:0{1,3}/:/g;
68 45 return $ip;
69 }
70  
71 60 ▶sub _width2bits {
Subroutine _width2bits: called CC=1 SCAR=0.0
72 60 my ( $width, $size ) = @_;
73 60 return pack 'B*',
74 ( '1' x ( $width + 8 ) ) . ( '0' x ( $size - $width ) );
75 }
76  
77 33 ▶sub _is_cidr {
Subroutine _is_cidr: called CC=3 SCAR=11.0
78 33 my ( $lo, $hi ) = @_;
79 33 my $mask = ~( $lo ^ $hi );
80 33 my $bits = unpack 'B*', $mask;
81 33 ▶ return unless $hi eq ($lo | $hi);
Branch: 1 branch
BranchTrueFalseTotal
unless $hi eq ($lo | $hi) 6 27 33
82 27 ▶ return unless $bits =~ /\A(1*)0*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $bits =~ /\A(1*)0*\z/ 4 23 27
83 23 return length( $1 ) - 8;
84 }
85  
86 97 ▶sub _encode {
Subroutine _encode: called CC=9 SCAR=22.2
87 97 my ( $self, $ip ) = @_;
88 97 ▶ if ( $ip =~ m{\A([0-9A-Fa-f:]+)/(0|[1-9][0-9]*)\z} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ip =~ m[\A([0-9A-Fa-f:]+)/(0|[1-9][0-9]*)\z]) { } 61 36 97
elsif ($ip =~ /\A([0-9A-Fa-f:]+)-([0-9A-Fa-f:]+)\z/) { } 26 10 36
elsif ($ip =~ /\A[0-9A-Fa-f:]+\z/) { } 2 8 10
89 61 my $mask = $2;
90 61 ▶ return if $mask > 128;
Branch: 1 branch
BranchTrueFalseTotal
if $mask > 128 1 60 61
91 60 ▶ return unless my $addr = _pack( $1 );
Branch: 1 branch
BranchTrueFalseTotal
unless my $addr = _pack($1) 0 60 60
92 60 ▶ return unless my $bits = _width2bits( $mask, 128 );
Branch: 1 branch
BranchTrueFalseTotal
unless my $bits = _width2bits($mask, 128) 0 60 60
93 60 return ( $addr & $bits, Net::CIDR::Set::_inc( $addr | ~$bits ) );
94 }
95 elsif ( $ip =~ m{\A([0-9A-Fa-f:]+)-([0-9A-Fa-f:]+)\z} ) {
96 26 my ( $from, $to ) = ( $1, $2 );
97 26 ▶ return unless my $lo = _pack( $from );
Branch: 1 branch
BranchTrueFalseTotal
unless my $lo = _pack($from) 0 26 26
98 26 ▶ return unless my $hi = _pack( $to );
Branch: 1 branch
BranchTrueFalseTotal
unless my $hi = _pack($to) 0 26 26
99 26 return ( $lo, Net::CIDR::Set::_inc( $hi ) );
100 }
101 elsif ( $ip =~ m{\A[0-9A-Fa-f:]+\z} ) {
102 2 return $self->_encode( "$ip/128" );
103 }
104 else {
105 8 return;
106 }
107 }
108  
109 95 ▶sub encode {
Subroutine encode: called CC=2 SCAR=6.9
Pod undocumented
110 95 my ( $self, $ip ) = @_;
111 95 ▶ my @r = $self->_encode( $ip )
Branch: 1 branch
BranchTrueFalseTotal
my(@r) = $self->_encode($ip) or croak("Can't decode $ip as an IPv6 address") 86 9 95
112 or croak "Can't decode $ip as an IPv6 address";
113 86 return @r;
114 }
115  
116 34 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.1
Pod undocumented
117 34 my $self = shift;
118 34 my $lo = shift;
119 34 my $hi = Net::CIDR::Set::_dec( shift );
120 34 ▶ my $generic = shift || 0;
Condition100%2/2
shift() || 0
l!l
1024
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
121 34 ▶ if ( $generic < 1 && $lo eq $hi ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($generic < 1 and $lo eq $hi) { } 0 34 34
elsif ($generic < 2 and defined(my $w = _is_cidr($lo, $hi))) { } 23 11 34
Condition66%2/3
$generic < 1 and $lo eq $hi
!ll&&!rl&&r
10240
MC/DC0%0/2
$generic < 1 and $lo eq $hi
$generic < 1 $lo eq $hi
Truth table66%2/3
A and B
A= $generic < 1B= $lo eq $hi
ABresult
0X0
100
111
122 # Single address
123 0 return _unpack( $lo );
124 }
125 ▶ elsif ( $generic < 2 && defined( my $w = _is_cidr( $lo, $hi ) ) ) {
Condition100%3/3
$generic < 2 and defined(my $w = _is_cidr($lo, $hi))
!ll&&!rl&&r
11023
MC/DC100%2/2
$generic < 2 and defined(my $w = _is_cidr($lo, $hi))
$generic < 2 defined(my $w = _is_cidr($lo, $hi))
Truth table100%3/3
A and B
A= $generic < 2B= defined(my $w = _is_cidr($lo, $hi))
ABresult
0X0
100
111
126 # Valid CIDR range
127 23 return join '/', _unpack( $lo ), $w;
128 }
129 else {
130 # General range
131 11 return join '-', _unpack( $lo ), _unpack( $hi );
132 }
133 }
134  
135 1 ▶sub nbits { 128 }
Subroutine nbits: called CC=1 SCAR=0.0
Pod undocumented
136  
137 9 1;
138  
139 __END__
↑ Summary Set/IPv4.pm »