lib/Net/DHCPv6/OptionList.pm

filter: Statementstmt 100% 87 / 87 Branchbran 87.5% 21 / 24 Conditioncond 87.5% 14 / 16 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 95.5% 151 / 158 CC 20 SCAR 16.7
CC
20
Cov
96%
try_from_bytes
11.0
options
4.0
get_option
3.0
CRAP 5.3
« lib/Net/DHCPv6/Option/AftrName.pm ↑ Summary lib/Net/DHCPv6/Option/DomainList.pm »
1 #!/bin/false
2 # ABSTRACT: Shared container for a collection of DHCPv6 options
3 # PODNAME: Net::DHCPv6::OptionList
4 81 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Net::DHCPv6::OptionList;
7 81 $Net::DHCPv6::OptionList::VERSION = '0.003';
8 81 ▶use Net::DHCPv6::Option::Generic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 81 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 81 ▶use Ref::Util qw( is_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 81 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 81 my $EMPTY = q();
14 81 my $OPT_HDR_SIZE = 4; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
15  
16 81 our %OPTION_CLASS;
17  
18 187 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 187 my $class = shift;
20 187 return bless {
21 options_by_code => {},
22 options_order => [],
23 }, $class;
24 }
25  
26 311 ▶sub add_option {
Subroutine add_option: called CC=3 SCAR=11.0
Pod documented
27 311 my ( $self, $option ) = @_;
28 311 my $code = $option->code;
29 307 push @{ $self->{options_order} }, $code
30 311 ▶ unless exists $self->{options_by_code}{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"options_by_code"}{$code} 307 4 311
31 311 ▶ $self->{options_by_code}{$code} //= [];
Condition100%2/2
$self->{"options_by_code"}{$code} //= []
l!l
4307
MC/DC100%1/1
$self->{"options_by_code"}{$code} //= []
$self->{"options_by_code"}{$code}
Truth table100%2/2
A
A= $self->{"options_by_code"}{$code}
Aresult
11
00
32 311 return push @{ $self->{options_by_code}{$code} }, $option;
33 }
34  
35 129 ▶sub get_option {
Subroutine get_option: called CC=3 SCAR=11.0
Pod documented
36 129 my ( $self, $code ) = @_;
37 129 my $list = $self->{options_by_code}{$code};
38 129 ▶ return unless $list && @{$list};
Branch: 1 branch
BranchTrueFalseTotal
unless $list and @{$list;} 4 125 129
Condition66%2/3
$list and @{$list;}
!ll&&!rl&&r
40125
MC/DC50%1/2
$list and @{$list;}
$list @{$list;}
Truth table66%2/3
A and B
A= $listB= @{$list;}
ABresult
0X0
100
111
39 125 return $list->[0];
40 }
41  
42 4 ▶sub remove_option {
Subroutine remove_option: called CC=1 SCAR=0.0
Pod documented
43 4 my ( $self, $code ) = @_;
44 4 delete $self->{options_by_code}{$code};
45 4 return @{ $self->{options_order} } = grep { $_ != $code } @{ $self->{options_order} };
46 }
47  
48 158 ▶sub options {
Subroutine options: called CC=4 SCAR=13.9
Pod documented
49 158 my $self = shift;
50 158 ▶ return [] unless @{ $self->{options_order} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"options_order"};} 104 54 158
51 54 my @opts;
52 54 for my $code ( @{ $self->{options_order} } ) {
53 68 my $list = $self->{options_by_code}{$code};
54 68 ▶ push @opts, @{$list} if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 68 0 68
55 }
56 54 return \@opts;
57 }
58  
59 147 ▶sub as_bytes {
Subroutine as_bytes: called CC=2 SCAR=7.0
Pod documented
60 147 my $self = shift;
61 147 ▶ my $opt_list = $self->options or return $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
my $opt_list = $self->options or return $EMPTY 147 0 147
62 147 return join( $EMPTY, map { $_->as_bytes } @{$opt_list} );
63 }
64  
65 164 ▶sub try_from_bytes {
Subroutine try_from_bytes: called CC=11 SCAR=24.0
Pod documented
66 164 my ( $class, $bytes ) = @_;
67 164 ▶ return ( $class->new, undef ) unless defined $bytes && CORE::length( $bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 55 109 164
Condition100%3/3
defined $bytes and length $bytes
!ll&&!rl&&r
154109
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
68  
69 109 my $list = $class->new;
70 109 my $offset = 0;
71 109 my $len = CORE::length( $bytes );
72 109 my $error;
73  
74 109 ▶ while ( $offset + $OPT_HDR_SIZE <= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset + $OPT_HDR_SIZE <= $len) 292 105 397
75 292 my $code = unpack( 'n', substr( $bytes, $offset, 2 ) );
76 292 my $optlen = unpack( 'n', substr( $bytes, $offset + 2, 2 ) );
77 292 $offset += $OPT_HDR_SIZE;
78 292 ▶ if ( $offset + $optlen > $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset + $optlen > $len) 3 289 292
79 3 $error = "Truncated option $code: need $optlen bytes, have " . ( $len - $offset );
80 3 last;
81 }
82 289 my $payload = substr( $bytes, $offset, $optlen );
83 289 $offset += $optlen;
84  
85 289 ▶ 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
2881
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
86 289 my $option;
87 289 ▶ if ( !eval { $option = $class_name->from_bytes_inner( $code, $payload ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $option = $class_name->from_bytes_inner($code, $payload); 1 } }) 5 284 289
88 5 ▶ if ( is_ref( $@ ) && $@->isa( 'Net::DHCPv6::X' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_ref($@) and $@->isa('Net::DHCPv6::X')) { } 4 1 5
Condition66%2/3
Ref::Util::XS::is_ref($@) and $@->isa("Net::DHCPv6::X")
!ll&&!rl&&r
104
MC/DC50%1/2
Ref::Util::XS::is_ref($@) and $@->isa("Net::DHCPv6::X")
Ref::Util::XS::is_ref($@) $@->isa("Net::DHCPv6::X")
Truth table66%2/3
A and B
A= Ref::Util::XS::is_ref($@)B= $@->isa("Net::DHCPv6::X")
ABresult
0X0
100
111
89 4 $option = Net::DHCPv6::Option::Generic->new( code => $code, data => $payload );
90 }
91 else {
92 1 $error = "Option $code parse error: $@";
93 1 last;
94 }
95 }
96 288 $list->add_option( $option );
97 }
98  
99 109 ▶ if ( !$error && $offset != $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $offset != $len) 2 107 109
Condition100%3/3
not $error and $offset != $len
!ll&&!rl&&r
41032
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
100 2 $error = 'Trailing garbage in option data';
101 }
102  
103 109 return ( $list, $error );
104 }
105  
106 153 ▶sub from_bytes {
Subroutine from_bytes: called CC=2 SCAR=7.0
Pod documented
107 153 my ( $class, $bytes ) = @_;
108 153 my ( $list, $error ) = $class->try_from_bytes( $bytes );
109 153 ▶ croak $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 153 153
110 153 return $list;
111 }
112  
113 81 1;
114  
115 __END__
« lib/Net/DHCPv6/Option/AftrName.pm ↑ Summary lib/Net/DHCPv6/Option/DomainList.pm »