blib/lib/Unblock/WebSocket/_Frame.pm

filter: Statementstmt 93.5% 131 / 140 Branchbran 77.7% 98 / 126 Conditioncond 43.4% 20 / 46 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 5 totaltotal 74.3% 266 / 358 CC 81 SCAR 33.1
CC
81
Cov
80%
encode
38.8
next_frame
36.6
new_parser
21.2
CRAP 27.3
« blib/lib/Unblock/WebSocket/Handshake.pm ↑ Summary blib/lib/Unblock/WebSocket/_Engine.pm »
1 package Unblock::WebSocket::_Frame;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Unblock::WebSocket::_Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 my %TYPE_TO_OPCODE = (
11 continuation => 0x0,
12 text => 0x1,
13 binary => 0x2,
14 close => 0x8,
15 ping => 0x9,
16 pong => 0xA,
17 );
18  
19 19 my %OPCODE_TO_TYPE = reverse %TYPE_TO_OPCODE;
20  
21 0 ▶sub opcode {
Subroutine opcode: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my ($class, $type) = @_;
23 0 return $TYPE_TO_OPCODE{$type};
24 }
25  
26 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ($class, $opcode) = @_;
28 0 return $OPCODE_TO_TYPE{$opcode};
29 }
30  
31 665 ▶sub _byte_string {
Subroutine _byte_string: called CC=4 SCAR=15.2
32 665 my ($where, $value) = @_;
33 665 ▶ croak "$where requires a defined scalar"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 665 665
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
00665
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
34 if !defined($value) || ref($value);
35 665 my $bytes = "$value";
36 665 ▶ croak "$where requires a byte string"
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 665 665
37 unless utf8::downgrade($bytes, 1);
38 665 return $bytes;
39 }
40  
41 239 ▶sub mask {
Subroutine mask: called CC=4 SCAR=14.0
Pod undocumented
42 239 my ($class, $payload, $key, $offset) = @_;
43 239 $payload = _byte_string('mask()', $payload);
44 239 $key = _byte_string('mask()', $key);
45 239 ▶ $offset ||= 0;
Condition50%1/2
$offset ||= 0
l!l
0239
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
46 239 ▶ croak 'mask(): key must contain exactly four bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless length $key == 4 0 239 239
47 unless length($key) == 4;
48  
49 239 my $out = $payload;
50 239 for my $i (0 .. length($out) - 1) {
51 17694 substr($out, $i, 1) = chr(
52 ord(substr($out, $i, 1))
53 ^ ord(substr($key, ($offset + $i) & 3, 1))
54 );
55 }
56 239 return $out;
57 }
58  
59 108 ▶sub encode {
Subroutine encode: called CC=27 SCAR=36.6
Pod undocumented
60 108 my ($class, %option) = @_;
61 108 my $opcode = delete $option{opcode};
62 108 ▶ my $payload = exists($option{payload}) ? delete($option{payload}) : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'payload'} ? : 108 0 108
63 108 ▶ my $fin = exists($option{fin}) ? delete($option{fin}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'fin'} ? : 15 93 108
64 108 ▶ my $masked = delete($option{masked}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'masked'} ? : 79 29 108
65 108 my $mask_key = delete $option{mask_key};
66 108 ▶ my $rsv1 = delete($option{rsv1}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'rsv1'} ? : 16 92 108
67 108 ▶ my $rsv2 = delete($option{rsv2}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'rsv2'} ? : 3 105 108
68 108 ▶ my $rsv3 = delete($option{rsv3}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'rsv3'} ? : 0 108 108
69  
70 108 ▶ croak 'encode(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 108 108
71 if %option;
72 108 ▶ croak 'encode(): opcode must be an integer between 0 and 15'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/ and $opcode <= 15 0 108 108
Condition33%3/9
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/ and $opcode <= 15
!ll&&!rl&&r
00108
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/
!ll&&!rl&&r
00108
defined $opcode and not ref $opcode
!ll&&!rl&&r
00108
MC/DC0%0/4
defined $opcode and not ref $opcode and $opcode =~ /\A[0-9]+\z/ and $opcode <= 15
defined $opcode not ref $opcode $opcode =~ /\A[0-9]+\z/ $opcode <= 15
Truth table20%1/5
A and not(B) and C and D
A= defined $opcodeB= not ref $opcodeC= $opcode =~ /\A[0-9]+\z/D= $opcode <= 15
ABCDresult
0XXX0
10XX0
110X0
11100
11111
73 unless defined($opcode) && !ref($opcode)
74 && $opcode =~ /\A[0-9]+\z/ && $opcode <= 15;
75 108 ▶ croak 'encode(): fin must be zero or one' unless "$fin" =~ /\A[01]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless "$fin" =~ /\A[01]\z/ 0 108 108
76  
77 108 $payload = _byte_string('encode(): payload', $payload);
78 108 my $length = length($payload);
79  
80 108 ▶ if ($opcode >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode >= 8) 45 63 108
81 45 ▶ croak 'encode(): control frames must not be fragmented' unless $fin;
Branch: 1 branch
BranchTrueFalseTotal
unless $fin 0 45 45
82 45 ▶ croak 'encode(): control frame payload exceeds 125 bytes'
Branch: 1 branch
BranchTrueFalseTotal
if $length > 125 1 44 45
83 if $length > 125;
84 }
85  
86 107 ▶ my $first = ($fin ? 0x80 : 0)
Branch: 4 branches
BranchTrueFalseTotal
$fin ? : 97 10 107
$rsv1 ? : 16 91 107
$rsv2 ? : 3 104 107
$rsv3 ? : 0 107 107
87 | ($rsv1 ? 0x40 : 0)
88 | ($rsv2 ? 0x20 : 0)
89 | ($rsv3 ? 0x10 : 0)
90 | $opcode;
91 107 ▶ my $mask_bit = $masked ? 0x80 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$masked ? : 79 28 107
92 107 my $header;
93  
94 107 ▶ if ($length < 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length < 126) { } 105 2 107
elsif ($length <= 65535) { } 2 0 2
95 105 $header = pack('CC', $first, $mask_bit | $length);
96 }
97 elsif ($length <= 0xffff) {
98 2 $header = pack('CCn', $first, $mask_bit | 126, $length);
99 }
100 else {
101 0 my $high = int($length / 4_294_967_296);
102 0 my $low = $length % 4_294_967_296;
103 0 ▶ croak 'encode(): payload length exceeds RFC 6455 63-bit limit'
Branch: 1 branch
BranchTrueFalseTotal
if $high >= 2147483648 0 0 0
104 if $high >= 0x80000000;
105 0 $header = pack('CCNN', $first, $mask_bit | 127, $high, $low);
106 }
107  
108 107 ▶ return $header . $payload unless $masked;
Branch: 1 branch
BranchTrueFalseTotal
unless $masked 28 79 107
109  
110 79 ▶ $mask_key = Unblock::WebSocket::_Random->mask_key
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mask_key 0 79 79
111 unless defined $mask_key;
112 79 $mask_key = _byte_string('encode(): mask_key', $mask_key);
113 79 ▶ croak 'encode(): mask_key must contain exactly four bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless length $mask_key == 4 0 79 79
114 unless length($mask_key) == 4;
115  
116 79 return $header . $mask_key . $class->mask($payload, $mask_key);
117 }
118  
119 210 ▶sub new_parser {
Subroutine new_parser: called CC=12 SCAR=30.5
Pod undocumented
120 210 my ($class, %option) = @_;
121 210 my $expect_masked = delete $option{expect_masked};
122 my $max_frame_size = exists($option{max_frame_size})
123 210 ▶ ? delete($option{max_frame_size}) : 16 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'max_frame_size'} ? : 210 0 210
124 210 ▶ my $allow_rsv1 = delete($option{allow_rsv1}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'allow_rsv1'} ? : 11 199 210
125 210 ▶ croak 'new_parser(): expect_masked must be zero or one'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expect_masked and not ref $expect_masked and "$expect_masked" =~ /\A[01]\z/ 0 210 210
Condition33%2/6
defined $expect_masked and not ref $expect_masked and "$expect_masked" =~ /\A[01]\z/
!ll&&!rl&&r
00210
defined $expect_masked and not ref $expect_masked
!ll&&!rl&&r
00210
MC/DC0%0/3
defined $expect_masked and not ref $expect_masked and "$expect_masked" =~ /\A[01]\z/
defined $expect_masked not ref $expect_masked "$expect_masked" =~ /\A[01]\z/
Truth table25%1/4
A and not(B) and C
A= defined $expect_maskedB= not ref $expect_maskedC= "$expect_masked" =~ /\A[01]\z/
ABCresult
0XX0
10X0
1100
1111
126 unless defined($expect_masked) && !ref($expect_masked)
127 && "$expect_masked" =~ /\A[01]\z/;
128 210 ▶ croak 'new_parser(): max_frame_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/ and $max_frame_size > 0 0 210 210
Condition33%3/9
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/ and $max_frame_size > 0
!ll&&!rl&&r
00210
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/
!ll&&!rl&&r
00210
defined $max_frame_size and not ref $max_frame_size
!ll&&!rl&&r
00210
MC/DC0%0/4
defined $max_frame_size and not ref $max_frame_size and $max_frame_size =~ /\A[0-9]+\z/ and $max_frame_size > 0
defined $max_frame_size not ref $max_frame_size $max_frame_size =~ /\A[0-9]+\z/ $max_frame_size > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $max_frame_sizeB= not ref $max_frame_sizeC= $max_frame_size =~ /\A[0-9]+\z/D= $max_frame_size > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
129 unless defined($max_frame_size) && !ref($max_frame_size)
130 && $max_frame_size =~ /\A[0-9]+\z/ && $max_frame_size > 0;
131 210 ▶ croak 'new_parser(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 210 210
132 if %option;
133  
134 210 ▶ return bless {
Branch: 1 branch
BranchTrueFalseTotal
$expect_masked ? : 162 48 210
135 expect_masked => $expect_masked ? 1 : 0,
136 max_frame_size => 0 + $max_frame_size,
137 allow_rsv1 => $allow_rsv1,
138 buffer => '',
139 }, 'Unblock::WebSocket::_Frame::Parser';
140 }
141  
142 package Unblock::WebSocket::_Frame::Parser;
143  
144 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 0 ▶sub buffered_bytes { length($_[0]{buffer}) }
Subroutine buffered_bytes: not called CC=1 SCAR=6.9
149  
150 347 ▶sub input {
Subroutine input: called CC=4 SCAR=14.3
151 347 my ($self, $bytes) = @_;
152 347 ▶ croak 'input(): bytes must be a scalar' if ref($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if ref $bytes 0 347 347
153 347 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 347 347
154 347 my $copy = "$bytes";
155 347 ▶ croak 'input(): bytes must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 347 347
156 unless utf8::downgrade($copy, 1);
157 347 $self->{buffer} .= $copy;
158 347 return length($copy);
159 }
160  
161 396 ▶sub next_frame {
Subroutine next_frame: called CC=35 SCAR=36.0
162 396 my ($self) = @_;
163 396 my $buffer = $self->{buffer};
164 396 ▶ return unless length($buffer) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer >= 2 39 357 396
165  
166 357 my ($first, $second) = unpack('CC', substr($buffer, 0, 2));
167 357 ▶ my $fin = ($first & 0x80) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first & 128 ? : 354 3 357
168 357 ▶ my $rsv1 = ($first & 0x40) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first & 64 ? : 9 348 357
169 357 ▶ my $rsv2 = ($first & 0x20) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first & 32 ? : 1 356 357
170 357 ▶ my $rsv3 = ($first & 0x10) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first & 16 ? : 0 357 357
171 357 my $opcode = $first & 0x0f;
172 357 ▶ my $masked = ($second & 0x80) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$second & 128 ? : 296 61 357
173 357 my $length7 = $second & 0x7f;
174  
175 357 ▶ croak 'WebSocket frame uses reserved RSV bits'
Branch: 1 branch
BranchTrueFalseTotal
if $rsv2 or $rsv3 1 356 357
Condition66%2/3
$rsv2 or $rsv3
l!l&&r!l&&!r
10356
MC/DC50%1/2
$rsv2 or $rsv3
$rsv2 $rsv3
Truth table66%2/3
A or B
A= $rsv2B= $rsv3
ABresult
1X1
011
000
176 if $rsv2 || $rsv3;
177 croak 'WebSocket frame uses reserved RSV bits (RSV1 not negotiated)'
178 356 ▶ if $rsv1 && !$self->{allow_rsv1};
Branch: 1 branch
BranchTrueFalseTotal
if $rsv1 and not $self->{"allow_rsv1"} 2 354 356
Condition100%3/3
$rsv1 and not $self->{"allow_rsv1"}
!ll&&!rl&&r
34772
MC/DC100%2/2
$rsv1 and not $self->{"allow_rsv1"}
$rsv1 not $self->{"allow_rsv1"}
Truth table100%3/3
A and not(B)
A= $rsv1B= not $self->{"allow_rsv1"}
ABresult
0X0
100
111
179 croak 'WebSocket frame has reserved opcode'
180 354 ▶ unless exists $OPCODE_TO_TYPE{$opcode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $OPCODE_TO_TYPE{$opcode} 1 353 354
181 353 ▶ croak 'WebSocket continuation frame must not set RSV1'
Branch: 1 branch
BranchTrueFalseTotal
if $rsv1 and $opcode == 0 0 353 353
Condition66%2/3
$rsv1 and $opcode == 0
!ll&&!rl&&r
34670
MC/DC0%0/2
$rsv1 and $opcode == 0
$rsv1 $opcode == 0
Truth table66%2/3
A and B
A= $rsv1B= $opcode == 0
ABresult
0X0
100
111
182 if $rsv1 && $opcode == 0;
183 353 ▶ croak 'WebSocket control frame must not set RSV1'
Branch: 1 branch
BranchTrueFalseTotal
if $rsv1 and $opcode >= 8 0 353 353
Condition66%2/3
$rsv1 and $opcode >= 8
!ll&&!rl&&r
34670
MC/DC0%0/2
$rsv1 and $opcode >= 8
$rsv1 $opcode >= 8
Truth table66%2/3
A and B
A= $rsv1B= $opcode >= 8
ABresult
0X0
100
111
184 if $rsv1 && $opcode >= 8;
185 croak 'WebSocket peer used incorrect masking direction'
186 353 ▶ if $masked != $self->{expect_masked};
Branch: 1 branch
BranchTrueFalseTotal
if $masked != $self->{"expect_masked"} 2 351 353
187  
188 351 my $offset = 2;
189 351 my $length;
190 351 ▶ if ($length7 < 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length7 < 126) { } 80 271 351
elsif ($length7 == 126) { } 269 2 271
191 80 $length = $length7;
192 }
193 elsif ($length7 == 126) {
194 269 ▶ return unless length($buffer) >= $offset + 2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer >= $offset + 2 2 267 269
195 267 $length = unpack('n', substr($buffer, $offset, 2));
196 267 $offset += 2;
197 267 ▶ croak 'WebSocket frame uses non-minimal 16-bit length'
Branch: 1 branch
BranchTrueFalseTotal
if $length < 126 1 266 267
198 if $length < 126;
199 }
200 else {
201 2 ▶ return unless length($buffer) >= $offset + 8;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer >= $offset + 8 0 2 2
202 2 my ($high, $low) = unpack('NN', substr($buffer, $offset, 8));
203 2 ▶ croak 'WebSocket frame length has reserved high bit set'
Branch: 1 branch
BranchTrueFalseTotal
if $high & 2147483648 1 1 2
204 if $high & 0x80000000;
205 croak 'WebSocket frame length is too large for this Perl build'
206 1 ▶ if $high && ($Config::Config{uvsize} || 4) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $high and ($Config::Config{"uvsize"} || 4) < 8 0 1 1
Condition20%1/5
$high and ($Config::Config{"uvsize"} || 4) < 8
!ll&&!rl&&r
100
$Config::Config{'uvsize'} || 4
l!l
00
MC/DC0%0/2
$high and ($Config::Config{"uvsize"} || 4) < 8
$high $Config::Config{'uvsize'}
Truth table33%1/3
A and B
A= $highB= $Config::Config{'uvsize'}
ABresult
0X0
100
111
207 1 $length = $high * 4_294_967_296 + $low;
208 1 $offset += 8;
209 1 ▶ croak 'WebSocket frame uses non-minimal 64-bit length'
Branch: 1 branch
BranchTrueFalseTotal
if $length <= 65535 1 0 1
210 if $length <= 0xffff;
211 }
212  
213 346 ▶ if ($opcode >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode >= 8) 51 295 346
214 51 ▶ croak 'WebSocket control frame is fragmented' unless $fin;
Branch: 1 branch
BranchTrueFalseTotal
unless $fin 1 50 51
215 50 ▶ croak 'WebSocket control frame payload exceeds 125 bytes'
Branch: 1 branch
BranchTrueFalseTotal
if $length > 125 1 49 50
216 if $length > 125;
217 }
218  
219 croak 'WebSocket frame exceeds configured limit'
220 344 ▶ if $length > $self->{max_frame_size};
Branch: 1 branch
BranchTrueFalseTotal
if $length > $self->{"max_frame_size"} 1 343 344
221  
222 343 my $mask_key;
223 343 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 291 52 343
224 291 ▶ return unless length($buffer) >= $offset + 4;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer >= $offset + 4 5 286 291
225 286 $mask_key = substr($buffer, $offset, 4);
226 286 $offset += 4;
227 }
228  
229 338 ▶ return unless length($buffer) >= $offset + $length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer >= $offset + $length 126 212 338
230 212 my $payload = substr($buffer, $offset, $length);
231 212 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 160 52 212
232 160 $payload = Unblock::WebSocket::_Frame->mask($payload, $mask_key);
233 }
234  
235 212 substr($self->{buffer}, 0, $offset + $length, '');
236  
237 return {
238 fin => $fin,
239 rsv1 => $rsv1,
240 rsv2 => $rsv2,
241 rsv3 => $rsv3,
242 opcode => $opcode,
243 212 type => $OPCODE_TO_TYPE{$opcode},
244 masked => $masked,
245 payload => $payload,
246 };
247 }
248  
249 19 1;
« blib/lib/Unblock/WebSocket/Handshake.pm ↑ Summary blib/lib/Unblock/WebSocket/_Engine.pm »