| 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
| |||||||||||||||||||||||||||||
| 31 | 311 | ▶ | $self->{options_by_code}{$code} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"options_by_code"}{$code} //= []
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}
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$list and @{$list;}
MC/DC50%1/2
$list and @{$list;}
$list
@{$list;}
Truth table66%2/3
A and B
A= $listB= @{$list;}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $bytes and length $bytes
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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'
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}
| |||||||||||||||||||||||||||||
| 86 | 289 | my $option; | |||||||||||||||||||||||||||
| 87 | 289 | ▶ | if ( !eval { $option = $class_name->from_bytes_inner( $code, $payload ); 1 } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 5 | ▶ | if ( is_ref( $@ ) && $@->isa( 'Net::DHCPv6::X' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
Ref::Util::XS::is_ref($@) and $@->isa("Net::DHCPv6::X")
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")
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $error and $offset != $len
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 110 | 153 | return $list; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 81 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||