blib/lib/Net/DHCPv6/OptionList.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 138 CC 11 SCAR 27.3
CC
11
Cov
0%
try_from_bytes
42.0
options
12.0
add_option
6.0
CRAP 15.4
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 $Net::DHCPv6::OptionList::VERSION = '0.001';
6 use strictures 2;
7 use Carp qw(croak);
8 use Ref::Util qw(is_ref);
9 use namespace::clean;
10  
11 our %OPTION_CLASS;
12  
13 sub new {
14 my $class = shift;
15 return bless {
16 options_by_code => {},
17 options_order => [],
18 }, $class;
19 }
20  
21 sub add_option {
22 my ( $self, $option ) = @_;
23 my $code = $option->code;
24 push @{ $self->{options_order} }, $code
25 unless exists $self->{options_by_code}{$code};
26 $self->{options_by_code}{$code} //= [];
27 push @{ $self->{options_by_code}{$code} }, $option;
28 }
29  
30 sub get_option {
31 my ( $self, $code ) = @_;
32 my $list = $self->{options_by_code}{$code};
33 return unless $list && @$list;
34 return $list->[0];
35 }
36  
37 sub remove_option {
38 my ( $self, $code ) = @_;
39 delete $self->{options_by_code}{$code};
40 @{ $self->{options_order} } = grep { $_ != $code } @{ $self->{options_order} };
41 }
42  
43 sub options {
44 my $self = shift;
45 return [] unless @{ $self->{options_order} };
46 my @opts;
47 for my $code ( @{ $self->{options_order} } ) {
48 my $list = $self->{options_by_code}{$code};
49 push @opts, @$list if $list;
50 }
51 return \@opts;
52 }
53  
54 sub as_bytes {
55 my $self = shift;
56 my $opts = $self->options or return '';
57 return join( '', map { $_->as_bytes } @$opts );
58 }
59  
60 sub try_from_bytes {
61 my ( $class, $bytes ) = @_;
62 return ( $class->new, undef ) unless defined $bytes && CORE::length( $bytes );
63  
64 my $list = $class->new;
65 my $offset = 0;
66 my $len = CORE::length( $bytes );
67 my $error;
68  
69 while ( $offset + 4 <= $len ) {
70 my $code = unpack( 'n', substr( $bytes, $offset, 2 ) );
71 my $optlen = unpack( 'n', substr( $bytes, $offset + 2, 2 ) );
72 $offset += 4;
73 if ( $offset + $optlen > $len ) {
74 $error = "Truncated option $code: need $optlen bytes, have " . ( $len - $offset );
75 last;
76 }
77 my $data = substr( $bytes, $offset, $optlen );
78 $offset += $optlen;
79  
80 my $class_name = $OPTION_CLASS{$code} || 'Net::DHCPv6::Option::Generic';
81 my $option;
82 eval { $option = $class_name->from_bytes_inner( $code, $data ); };
83 if ( my $err = $@ ) {
84 is_ref( $err ) && $err->isa( 'Net::DHCPv6::X' )
85 ? do {
86 $option = Net::DHCPv6::Option::Generic->new( code => $code, data => $data );
87 }
88 : do {
89 $error = "Option $code parse error: $err";
90 last;
91 };
92 }
93 $list->add_option( $option );
94 }
95  
96 if ( !$error && $offset != $len ) {
97 $error = 'Trailing garbage in option data';
98 }
99  
100 return ( $list, $error );
101 }
102  
103 sub from_bytes {
104 my ( $class, $bytes ) = @_;
105 my ( $list, $error ) = $class->try_from_bytes( $bytes );
106 croak $error if $error;
107 return $list;
108 }
109  
110 1;
111  
112 __END__