lib/Net/DHCPv6/DUID.pm

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