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

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