Works/Util.pm

filter: Statementstmt 100% 43 / 43 Branchbran 95.0% 19 / 20 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 93.3% 84 / 90 CC 14 SCAR 13.0
CC
14
Cov
96%
_validate_ip_string
7.0
_string_address_to_integer
4.0
_integer_address_to_binary
3.0
CRAP 3.7
« Works/Network.pm ↑ Summary Works/Address.pm »
1 package Net::Works::Util;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.22';
7  
8 4 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Math::Int128 qw( net_to_uint128 uint128_to_net );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Socket qw( AF_INET AF_INET6 inet_pton inet_ntop );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our @EXPORT_OK = qw(
16 _string_address_to_integer
17 _integer_address_to_binary
18 _binary_address_to_string
19 _integer_address_to_string
20 _validate_ip_string
21 );
22  
23 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
24 900 ▶sub _string_address_to_integer {
Subroutine _string_address_to_integer: called CC=4 SCAR=13.9
25 900 my $string = shift;
26 900 my $version = shift;
27  
28 900 ▶ my $binary = inet_pton( $version == 4 ? AF_INET : AF_INET6, $string )
Branch: 2 branches
BranchTrueFalseTotal
my $binary = inet_pton($version == 4 ? 2 : 10, $string) or return 882 18 900
$version == 4 ? : 189 711 900
29 or return;
30  
31 882 ▶ return $version == 4
Branch: 1 branch
BranchTrueFalseTotal
$version == 4 ? : 181 701 882
32 ? unpack( N => $binary )
33 : net_to_uint128($binary);
34 }
35  
36 2736 ▶sub _integer_address_to_binary {
Subroutine _integer_address_to_binary: called CC=3 SCAR=11.0
37 2736 my $integer = shift;
38  
39 2736 ▶ if ( ref $integer && blessed $integer) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $integer and builtin::blessed($integer)) { } 2259 477 2736
Condition66%2/3
ref $integer and builtin::blessed($integer)
!ll&&!rl&&r
47702259
MC/DC50%1/2
ref $integer and builtin::blessed($integer)
ref $integer builtin::blessed($integer)
Truth table66%2/3
A and B
A= ref $integerB= builtin::blessed($integer)
ABresult
0X0
100
111
40 2259 return uint128_to_net($integer);
41 }
42 else {
43 477 return pack( N => $integer );
44 }
45 }
46  
47 2736 ▶sub _binary_address_to_string {
Subroutine _binary_address_to_string: called CC=3 SCAR=11.0
48 2736 my $binary = shift;
49  
50 2736 ▶ my $family = length($binary) == 4 ? AF_INET : AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
length $binary == 4 ? : 477 2259 2736
51  
52 2736 my $string = inet_ntop( $family, $binary );
53 2736 ▶ return $string eq '::' ? '::0' : $string;
Branch: 1 branch
BranchTrueFalseTotal
$string eq '::' ? : 6 2730 2736
54 }
55  
56 2736 ▶sub _integer_address_to_string {
Subroutine _integer_address_to_string: called CC=1 SCAR=0.0
57 2736 _binary_address_to_string( _integer_address_to_binary( $_[0] ) );
58 }
59  
60 84 ▶sub _validate_ip_string {
Subroutine _validate_ip_string: called CC=7 SCAR=19.5
61 84 my $str = shift;
62 84 my $version = shift;
63  
64 84 ▶ my $str_val = defined $str ? $str : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 82 2 84
65 84 ▶ if ( $version == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 4) { } 7 77 84
66 7 ▶ confess("$str_val is not a valid IPv4 address")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and defined inet_pton(2, $str) 7 0 7
Condition66%2/3
defined $str and defined inet_pton(2, $str)
!ll&&!rl&&r
160
MC/DC0%0/2
defined $str and defined inet_pton(2, $str)
defined $str defined inet_pton(2, $str)
Truth table66%2/3
A and B
A= defined $strB= defined inet_pton(2, $str)
ABresult
0X0
100
111
67 unless defined $str && defined inet_pton( AF_INET, $str );
68 }
69 else {
70 77 ▶ confess("$str_val is not a valid IPv6 address")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and defined inet_pton(10, $str) 10 67 77
Condition100%3/3
defined $str and defined inet_pton(10, $str)
!ll&&!rl&&r
1967
MC/DC100%2/2
defined $str and defined inet_pton(10, $str)
defined $str defined inet_pton(10, $str)
Truth table100%3/3
A and B
A= defined $strB= defined inet_pton(10, $str)
ABresult
0X0
100
111
71 unless defined $str && defined inet_pton( AF_INET6, $str );
72 }
73 }
74  
75 4 1;
76  
77 # ABSTRACT: Utility subroutines for Net-Works
78  
79 __END__
« Works/Network.pm ↑ Summary Works/Address.pm »