lib/Net/DHCPv6/OptionList.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 79.1% 19 / 24 Conditioncond 75.0% 12 / 16 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 8 / 8 totaltotal 89.9% 134 / 149 CC 20 SCAR 17.1
CC
20
Cov
91%
try_from_bytes
11.1
options
4.0
add_option
3.1
CRAP 5.5
« blib/lib/Net/DHCPv6/Option/PdExclude.pm ↑ Summary blib/lib/Net/DHCPv6/Option/ClientLinkLayerAddr.pm »
1 #!/usr/bin/false
2 # ABSTRACT: Shared container for a collection of DHCPv6 options
3 # PODNAME: Net::DHCPv6::OptionList
4 package Net::DHCPv6::OptionList;
5 81 $Net::DHCPv6::OptionList::VERSION = '0.001';
6 81 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 81 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 81 ▶use Ref::Util qw(is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 81 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 81 our %OPTION_CLASS;
12  
13 sub new {
14 185 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 185 return bless {
16 options_by_code => {},
17 options_order => [],
18 }, $class;
19 }
20  
21 sub add_option {
22 303 ▶ my ( $self, $option ) = @_;
Subroutine add_option: called CC=3 SCAR=11.2
Pod documented
23 303 my $code = $option->code;
24 303 push @{ $self->{options_order} }, $code
25 303 ▶ unless exists $self->{options_by_code}{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"options_by_code"}{$code} 303 0 303
26 303 ▶ $self->{options_by_code}{$code} //= [];
Condition50%1/2
$self->{"options_by_code"}{$code} //= []
l!l
0303
MC/DC0%0/1
$self->{"options_by_code"}{$code} //= []
$self->{"options_by_code"}{$code}
Truth table50%1/2
A
A= $self->{"options_by_code"}{$code}
Aresult
11
00
27 303 push @{ $self->{options_by_code}{$code} }, $option;
28 }
29  
30 sub get_option {
31 128 ▶ my ( $self, $code ) = @_;
Subroutine get_option: called CC=3 SCAR=11.0
Pod documented
32 128 my $list = $self->{options_by_code}{$code};
33 128 ▶ return unless $list && @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list and @$list 4 124 128
Condition66%2/3
$list and @$list
!ll&&!rl&&r
40124
MC/DC50%1/2
$list and @$list
$list @$list
Truth table66%2/3
A and B
A= $listB= @$list
ABresult
0X0
100
111
34 124 return $list->[0];
35 }
36  
37 sub remove_option {
38 4 ▶ my ( $self, $code ) = @_;
Subroutine remove_option: called CC=1 SCAR=0.0
Pod documented
39 4 delete $self->{options_by_code}{$code};
40 4 @{ $self->{options_order} } = grep { $_ != $code } @{ $self->{options_order} };
41 }
42  
43 sub options {
44 156 ▶ my $self = shift;
Subroutine options: called CC=4 SCAR=13.9
Pod documented
45 156 ▶ return [] unless @{ $self->{options_order} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"options_order"};} 104 52 156
46 52 my @opts;
47 52 for my $code ( @{ $self->{options_order} } ) {
48 62 my $list = $self->{options_by_code}{$code};
49 62 ▶ push @opts, @$list if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 62 0 62
50 }
51 52 return \@opts;
52 }
53  
54 sub as_bytes {
55 146 ▶ my $self = shift;
Subroutine as_bytes: called CC=2 SCAR=7.0
Pod documented
56 146 ▶ my $opts = $self->options or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $opts = $self->options or return "" 0 146 146
57 146 return join( '', map { $_->as_bytes } @$opts );
58 }
59  
60 sub try_from_bytes {
61 162 ▶ my ( $class, $bytes ) = @_;
Subroutine try_from_bytes: called CC=11 SCAR=24.1
Pod documented
62 162 ▶ return ( $class->new, undef ) unless defined $bytes && CORE::length( $bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 55 107 162
Condition100%3/3
defined $bytes and length $bytes
!ll&&!rl&&r
154107
MC/DC100%2/2
defined $bytes and length $bytes
defined $bytes length $bytes
Truth table100%3/3
A and B
A= defined $bytesB= length $bytes
ABresult
0X0
100
111
63  
64 107 my $list = $class->new;
65 107 my $offset = 0;
66 107 my $len = CORE::length( $bytes );
67 107 my $error;
68  
69 107 ▶ while ( $offset + 4 <= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset + 4 <= $len) 285 104 389
70 285 my $code = unpack( 'n', substr( $bytes, $offset, 2 ) );
71 285 my $optlen = unpack( 'n', substr( $bytes, $offset + 2, 2 ) );
72 285 $offset += 4;
73 285 ▶ if ( $offset + $optlen > $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset + $optlen > $len) 3 282 285
74 3 $error = "Truncated option $code: need $optlen bytes, have " . ( $len - $offset );
75 3 last;
76 }
77 282 my $data = substr( $bytes, $offset, $optlen );
78 282 $offset += $optlen;
79  
80 282 ▶ my $class_name = $OPTION_CLASS{$code} || 'Net::DHCPv6::Option::Generic';
Condition100%2/2
$Net::DHCPv6::OptionList::OPTION_CLASS{$code} || 'Net::DHCPv6::Option::Generic'
l!l
2811
MC/DC100%1/1
$Net::DHCPv6::OptionList::OPTION_CLASS{$code} || 'Net::DHCPv6::Option::Generic'
$Net::DHCPv6::OptionList::OPTION_CLASS{$code}
Truth table100%2/2
A
A= $Net::DHCPv6::OptionList::OPTION_CLASS{$code}
Aresult
11
00
81 282 my $option;
82 282 eval { $option = $class_name->from_bytes_inner( $code, $data ); };
83 282 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 4 278 282
84 is_ref( $err ) && $err->isa( 'Net::DHCPv6::X' )
85 ? do {
86 4 $option = Net::DHCPv6::Option::Generic->new( code => $code, data => $data );
87 }
88 4 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_ref($err) && $err->isa('Net::DHCPv6::X') ? : 4 0 4
Condition33%1/3
Ref::Util::XS::is_ref($err) and $err->isa("Net::DHCPv6::X")
!ll&&!rl&&r
004
MC/DC0%0/2
Ref::Util::XS::is_ref($err) and $err->isa("Net::DHCPv6::X")
Ref::Util::XS::is_ref($err) $err->isa("Net::DHCPv6::X")
Truth table33%1/3
A and B
A= Ref::Util::XS::is_ref($err)B= $err->isa("Net::DHCPv6::X")
ABresult
0X0
100
111
89 0 $error = "Option $code parse error: $err";
90 0 last;
91 };
92 }
93 282 $list->add_option( $option );
94 }
95  
96 107 ▶ if ( !$error && $offset != $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $offset != $len) 1 106 107
Condition100%3/3
not $error and $offset != $len
!ll&&!rl&&r
31031
MC/DC100%2/2
not $error and $offset != $len
not $error $offset != $len
Truth table100%3/3
not(A) and B
A= not $errorB= $offset != $len
ABresult
0X0
100
111
97 1 $error = 'Trailing garbage in option data';
98 }
99  
100 107 return ( $list, $error );
101 }
102  
103 sub from_bytes {
104 153 ▶ my ( $class, $bytes ) = @_;
Subroutine from_bytes: called CC=2 SCAR=7.0
Pod documented
105 153 my ( $list, $error ) = $class->try_from_bytes( $bytes );
106 153 ▶ croak $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 153 153
107 153 return $list;
108 }
109  
110 81 1;
111  
112 __END__
« blib/lib/Net/DHCPv6/Option/PdExclude.pm ↑ Summary blib/lib/Net/DHCPv6/Option/ClientLinkLayerAddr.pm »