lib/Net/DHCPv6/DUID.pm

filter: Statementstmt 99.3% 150 / 151 Branchbran 92.0% 46 / 50 Conditioncond 53.5% 15 / 28 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 100% 28 / 28 Podpod 100% 15 / 15 totaltotal 88.9% 257 / 289 CC 36 SCAR 13.0
CC
36
Cov
92%
new
7.0
try_from_bytes
7.0
as_bytes
6.0
CRAP 3.7
« lib/Net/DHCPv6/Helpers.pm ↑ Summary lib/Net/DHCPv6/Packet/Relay.pm »
1 #!/bin/false
2 # ABSTRACT: DUID parse/emit and helper constructors
3 # PODNAME: Net::DHCPv6::DUID
4 21 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Net::DHCPv6::DUID;
7 21 $Net::DHCPv6::DUID::VERSION = '0.003';
8 21 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Net::DHCPv6::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 $DUID_EN $DUID_LL $DUID_LLT $DUID_UUID $IPV6_ADDR_LEN
11 21 );
12 21 ▶use Net::DHCPv6::X::BadDUID ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 my $EMPTY = q();
15 21 my $N_LEN = 4; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
16 21 my $LLT_HDR_SIZE = 6; ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
17  
18 15 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
19 15 my ( $class, %args ) = @_;
20 15 ▶ my $type = $args{duid_type} // croak 'Net::DHCPv6::DUID->new: duid_type is required';
Condition100%2/2
$args{"duid_type"} // croak("Net::DHCPv6::DUID->new: duid_type is required")
l!l
141
MC/DC100%1/1
$args{"duid_type"} // croak("Net::DHCPv6::DUID->new: duid_type is required")
$args{"duid_type"}
Truth table100%2/2
A
A= $args{"duid_type"}
Aresult
11
00
21 14 ▶ my $self = { duid_type => $type, identifier => $args{identifier} // $EMPTY };
Condition66%2/3
$args{"identifier"} // $EMPTY
l!l&&r!l&&!r
1310
MC/DC0%0/2
$args{"identifier"} // $EMPTY
$args{"identifier"} $EMPTY
Truth table66%2/3
A // B
A= $args{"identifier"}B= $EMPTY
ABresult
1X1
011
000
22 14 ▶ if ( $type == $DUID_LLT ) { _new_llt( $self, \%args ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LLT) 9 5 14
23 13 ▶ if ( $type == $DUID_EN ) { _new_en( $self, \%args ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_EN) 1 12 13
24 13 ▶ if ( $type == $DUID_LL ) { _new_ll( $self, \%args ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LL) 1 12 13
25 13 ▶ if ( $type == $DUID_UUID ) { _new_uuid( $self, \%args ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_UUID) 2 11 13
26 12 return bless $self, $class;
27 }
28  
29 9 ▶sub _new_llt {
Subroutine _new_llt: called CC=4 SCAR=14.0
30 9 my ( $self, $arg ) = @_;
31 croak 'DUID-LLT requires link_layer_type, time, and identifier'
32 unless defined $arg->{link_layer_type}
33 && defined $arg->{time}
34 9 ▶ && defined $arg->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"link_layer_type"} and defined $arg->{"time"} and defined $arg->{"identifier"} 1 8 9
Condition66%4/6
defined $arg->{"link_layer_type"} and defined $arg->{"time"} and defined $arg->{"identifier"}
!ll&&!rl&&r
108
defined $arg->{"link_layer_type"} and defined $arg->{"time"}
!ll&&!rl&&r
108
MC/DC33%1/3
defined $arg->{"link_layer_type"} and defined $arg->{"time"} and defined $arg->{"identifier"}
defined $arg->{"link_layer_type"} defined $arg->{"time"} defined $arg->{"identifier"}
Truth table50%2/4
A and B and C
A= defined $arg->{"link_layer_type"}B= defined $arg->{"time"}C= defined $arg->{"identifier"}
ABCresult
0XX0
10X0
1100
1111
35 8 $self->{link_layer_type} = $arg->{link_layer_type};
36 8 $self->{time} = $arg->{time};
37 8 $self->{identifier} = $arg->{identifier};
38 8 return;
39 }
40  
41 1 ▶sub _new_en {
Subroutine _new_en: called CC=3 SCAR=11.8
42 1 my ( $self, $arg ) = @_;
43 croak 'DUID-EN requires enterprise_number and identifier'
44 unless defined $arg->{enterprise_number}
45 1 ▶ && defined $arg->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"enterprise_number"} and defined $arg->{"identifier"} 0 1 1
Condition33%1/3
defined $arg->{"enterprise_number"} and defined $arg->{"identifier"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $arg->{"enterprise_number"} and defined $arg->{"identifier"}
defined $arg->{"enterprise_number"} defined $arg->{"identifier"}
Truth table33%1/3
A and B
A= defined $arg->{"enterprise_number"}B= defined $arg->{"identifier"}
ABresult
0X0
100
111
46 1 $self->{enterprise_number} = $arg->{enterprise_number};
47 1 $self->{identifier} = $arg->{identifier};
48 1 return;
49 }
50  
51 1 ▶sub _new_ll {
Subroutine _new_ll: called CC=3 SCAR=11.8
52 1 my ( $self, $arg ) = @_;
53 croak 'DUID-LL requires link_layer_type and identifier'
54 unless defined $arg->{link_layer_type}
55 1 ▶ && defined $arg->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"link_layer_type"} and defined $arg->{"identifier"} 0 1 1
Condition33%1/3
defined $arg->{"link_layer_type"} and defined $arg->{"identifier"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $arg->{"link_layer_type"} and defined $arg->{"identifier"}
defined $arg->{"link_layer_type"} defined $arg->{"identifier"}
Truth table33%1/3
A and B
A= defined $arg->{"link_layer_type"}B= defined $arg->{"identifier"}
ABresult
0X0
100
111
56 1 $self->{link_layer_type} = $arg->{link_layer_type};
57 1 $self->{identifier} = $arg->{identifier};
58 1 return;
59 }
60  
61 2 ▶sub _new_uuid {
Subroutine _new_uuid: called CC=3 SCAR=11.0
62 2 my ( $self, $arg ) = @_;
63 croak 'DUID-UUID requires identifier (16 bytes)'
64 2 ▶ unless defined $arg->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{"identifier"} 0 2 2
65 croak 'DUID-UUID identifier must be 16 bytes'
66 2 ▶ unless CORE::length( $arg->{identifier} ) == $IPV6_ADDR_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $arg->{"identifier"} == $Net::DHCPv6::DUID::IPV6_ADDR_LEN 1 1 2
67 1 $self->{identifier} = $arg->{identifier};
68 1 return;
69 }
70  
71 26 ▶sub duid_type { return shift->{duid_type} }
Subroutine duid_type: called CC=1 SCAR=0.0
Pod documented
72 12 ▶sub link_layer_type { return shift->{link_layer_type} }
Subroutine link_layer_type: called CC=1 SCAR=0.0
Pod documented
73 16 ▶sub time { return shift->{time} } ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine time: called CC=1 SCAR=0.0
Pod documented
74 3 ▶sub enterprise_number { return shift->{enterprise_number} }
Subroutine enterprise_number: called CC=1 SCAR=0.0
Pod documented
75 16 ▶sub identifier { return shift->{identifier} }
Subroutine identifier: called CC=1 SCAR=0.0
Pod documented
76  
77 21 my %DUID_LENGTH_BASE = (
78 $DUID_LLT => 2 + 2 + $N_LEN,
79 $DUID_EN => 2 + $N_LEN,
80 $DUID_LL => 2 + 2,
81 $DUID_UUID => 2,
82 );
83  
84 5 ▶sub length { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine length: called CC=3 SCAR=11.3
Pod documented
85 5 my $self = shift;
86 5 my $type = $self->{duid_type};
87 5 ▶ my $id = $self->{identifier} // $EMPTY;
Condition33%1/3
$self->{"identifier"} // $EMPTY
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->{"identifier"} // $EMPTY
$self->{"identifier"} $EMPTY
Truth table33%1/3
A // B
A= $self->{"identifier"}B= $EMPTY
ABresult
1X1
011
000
88 5 ▶ return ( $DUID_LENGTH_BASE{$type} // 2 ) + CORE::length( $id );
Condition100%2/2
$DUID_LENGTH_BASE{$type} // 2
l!l
41
MC/DC100%1/1
$DUID_LENGTH_BASE{$type} // 2
$DUID_LENGTH_BASE{$type}
Truth table100%2/2
A
A= $DUID_LENGTH_BASE{$type}
Aresult
11
00
89 }
90  
91 125 ▶sub as_bytes {
Subroutine as_bytes: called CC=6 SCAR=17.9
Pod documented
92 125 my $self = shift;
93 125 my $type = $self->{duid_type};
94 125 ▶ my $id = $self->{identifier} // $EMPTY;
Condition33%1/3
$self->{"identifier"} // $EMPTY
l!l&&r!l&&!r
12500
MC/DC0%0/2
$self->{"identifier"} // $EMPTY
$self->{"identifier"} $EMPTY
Truth table33%1/3
A // B
A= $self->{"identifier"}B= $EMPTY
ABresult
1X1
011
000
95 125 my $buf = pack( 'n', $type );
96  
97 125 ▶ if ( $type == $DUID_LLT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LLT) 89 36 125
98 89 $buf .= pack( 'n', $self->{link_layer_type} );
99 89 $buf .= pack( 'N', $self->{time} );
100 89 $buf .= $id;
101 89 return $buf;
102 }
103 36 ▶ if ( $type == $DUID_EN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_EN) 9 27 36
104 9 $buf .= pack( 'N', $self->{enterprise_number} );
105 9 $buf .= $id;
106 9 return $buf;
107 }
108 27 ▶ if ( $type == $DUID_LL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LL) 25 2 27
109 25 $buf .= pack( 'n', $self->{link_layer_type} );
110 25 $buf .= $id;
111 25 return $buf;
112 }
113 2 ▶ if ( $type == $DUID_UUID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_UUID) 1 1 2
114 1 $buf .= $id;
115 1 return $buf;
116 }
117 1 $buf .= $id;
118 1 return $buf;
119 }
120  
121 1 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.4
Pod documented
122 1 my $self = shift;
123 1 my $bytes = $self->as_bytes;
124 my $tname = $Net::DHCPv6::Constants::REV_DUID_TYPE{ $self->{duid_type} }
125 1 ▶ || sprintf( 'TYPE%d', $self->{duid_type} );
Condition33%1/3
$Net::DHCPv6::Constants::REV_DUID_TYPE{$self->{'duid_type'}} || sprintf('TYPE%d', $self->{'duid_type'})
l!l&&r!l&&!r
100
MC/DC0%0/2
$Net::DHCPv6::Constants::REV_DUID_TYPE{$self->{'duid_type'}} || sprintf('TYPE%d', $self->{'duid_type'})
$Net::DHCPv6::Constants::REV_DUID_TYPE{$self->{'duid_type'}} sprintf('TYPE%d', $self->{'duid_type'})
Truth table33%1/3
A || B
A= $Net::DHCPv6::Constants::REV_DUID_TYPE{$self->{'duid_type'}}B= sprintf('TYPE%d', $self->{'duid_type'})
ABresult
1X1
011
000
126 1 return sprintf( '%s:%s', $tname, unpack( 'H*', $bytes ) );
127 }
128  
129 89 ▶sub _try_llt {
Subroutine _try_llt: called CC=3 SCAR=11.0
130 89 my ( $class, $rest, $rest_len ) = @_;
131 89 my $partial = bless( { duid_type => $DUID_LLT }, $class );
132 89 my $error;
133 89 ▶ if ( $rest_len >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest_len >= 2) { } 87 2 89
134 87 $partial->{link_layer_type} = unpack( 'n', $rest );
135 87 ▶ if ( $rest_len >= $LLT_HDR_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest_len >= $LLT_HDR_SIZE) { } 84 3 87
136 84 $partial->{time} = unpack( 'x2 N', $rest );
137 84 $partial->{identifier} = substr( $rest, $LLT_HDR_SIZE );
138 }
139 else {
140 3 $error = "Need $LLT_HDR_SIZE bytes for DUID-LLT hwtype+time, got $rest_len";
141 }
142 }
143 else {
144 2 $error = "Need at least 2 bytes for DUID-LLT hwtype, got $rest_len";
145 }
146 89 return ( $partial, $error );
147 }
148  
149 10 ▶sub _try_en {
Subroutine _try_en: called CC=2 SCAR=6.9
150 10 my ( $class, $rest, $rest_len ) = @_;
151 10 my $partial = bless( { duid_type => $DUID_EN }, $class );
152 10 my $error;
153 10 ▶ if ( $rest_len >= $N_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest_len >= $N_LEN) { } 9 1 10
154 9 $partial->{enterprise_number} = unpack( 'N', $rest );
155 9 $partial->{identifier} = substr( $rest, $N_LEN );
156 }
157 else {
158 1 $error = "Need $N_LEN bytes for DUID-EN enterprise_number, got $rest_len";
159 }
160 10 return ( $partial, $error );
161 }
162  
163 25 ▶sub _try_ll {
Subroutine _try_ll: called CC=2 SCAR=7.1
164 25 my ( $class, $rest, $rest_len ) = @_;
165 25 my $partial = bless( { duid_type => $DUID_LL }, $class );
166 25 my $error;
167 25 ▶ if ( $rest_len >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest_len >= 2) { } 25 0 25
168 25 $partial->{link_layer_type} = unpack( 'n', $rest );
169 25 $partial->{identifier} = substr( $rest, 2 );
170 }
171 else {
172 0 $error = "Need at least 2 bytes for DUID-LL hwtype, got $rest_len";
173 }
174 25 return ( $partial, $error );
175 }
176  
177 2 ▶sub _try_uuid {
Subroutine _try_uuid: called CC=2 SCAR=6.9
178 2 my ( $class, $rest, $rest_len ) = @_;
179 2 my $partial = bless( { duid_type => $DUID_UUID }, $class );
180 2 my $error;
181 2 ▶ if ( $rest_len >= $IPV6_ADDR_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest_len >= $Net::DHCPv6::DUID::IPV6_ADDR_LEN) { } 1 1 2
182 1 $partial->{identifier} = substr( $rest, 0, $IPV6_ADDR_LEN );
183 }
184 else {
185 1 $error = "Need $IPV6_ADDR_LEN bytes for DUID-UUID, got $rest_len";
186 }
187 2 return ( $partial, $error );
188 }
189  
190 131 ▶sub try_from_bytes {
Subroutine try_from_bytes: called CC=7 SCAR=19.5
Pod documented
191 131 my ( $class, $bytes ) = @_;
192 131 ▶ return ( undef, 'No data provided' ) unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1 130 131
193  
194 130 my $len = CORE::length( $bytes );
195 130 ▶ return ( undef, 'Need at least 2 bytes for DUID type' ) if $len < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 2 3 127 130
196  
197 127 my $type = unpack( 'n', substr( $bytes, 0, 2 ) );
198 127 my $rest = substr( $bytes, 2 );
199 127 my $rest_len = CORE::length( $rest );
200  
201 127 ▶ if ( $type == $DUID_LLT ) { return _try_llt( $class, $rest, $rest_len ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LLT) 89 38 127
202 38 ▶ if ( $type == $DUID_EN ) { return _try_en( $class, $rest, $rest_len ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_EN) 10 28 38
203 28 ▶ if ( $type == $DUID_LL ) { return _try_ll( $class, $rest, $rest_len ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_LL) 25 3 28
204 3 ▶ if ( $type == $DUID_UUID ) { return _try_uuid( $class, $rest, $rest_len ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type == $Net::DHCPv6::DUID::DUID_UUID) 2 1 3
205  
206 return (
207 1 bless(
208 {
209 duid_type => $type,
210 identifier => $rest,
211 },
212 $class
213 ),
214 undef
215 );
216 }
217  
218 119 ▶sub from_bytes {
Subroutine from_bytes: called CC=2 SCAR=6.9
Pod documented
219 119 my ( $class, $bytes ) = @_;
220 119 my ( $duid, $error ) = $class->try_from_bytes( $bytes );
221 119 ▶ Net::DHCPv6::X::BadDUID->throw( message => $error ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 2 117 119
222 117 return $duid;
223 }
224  
225 8 ▶sub new_llt {
Subroutine new_llt: called CC=1 SCAR=0.0
Pod documented
226 8 my ( $class, $link_layer_type, $time, $mac_bytes ) = @_;
227 8 return $class->new(
228 duid_type => $DUID_LLT,
229 link_layer_type => $link_layer_type,
230 time => $time,
231 identifier => $mac_bytes,
232 );
233 }
234  
235 1 ▶sub new_en {
Subroutine new_en: called CC=1 SCAR=0.0
Pod documented
236 1 my ( $class, $enterprise_number, $identifier ) = @_;
237 1 return $class->new(
238 duid_type => $DUID_EN,
239 enterprise_number => $enterprise_number,
240 identifier => $identifier,
241 );
242 }
243  
244 1 ▶sub new_ll {
Subroutine new_ll: called CC=1 SCAR=0.0
Pod documented
245 1 my ( $class, $link_layer_type, $mac_bytes ) = @_;
246 1 return $class->new(
247 duid_type => $DUID_LL,
248 link_layer_type => $link_layer_type,
249 identifier => $mac_bytes,
250 );
251 }
252  
253 2 ▶sub new_uuid {
Subroutine new_uuid: called CC=1 SCAR=0.0
Pod documented
254 2 my ( $class, $uuid_bytes ) = @_;
255 2 return $class->new(
256 duid_type => $DUID_UUID,
257 identifier => $uuid_bytes,
258 );
259 }
260  
261 21 1;
262  
263 __END__
« lib/Net/DHCPv6/Helpers.pm ↑ Summary lib/Net/DHCPv6/Packet/Relay.pm »