blib/lib/Linux/Event/WebSocket/_Parser.pm

filter: Statementstmt 82.9% 68 / 82 Branchbran 74.1% 46 / 62 Conditioncond 51.2% 20 / 39 MC/DCmcdc 36.3% 8 / 22 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 4 totaltotal 68.6% 151 / 220 CC 45 SCAR 31.2
CC
45
Cov
73%
_check_size
42.0
next_frame
28.2
new
16.1
CRAP 22.6
« blib/lib/Linux/Event/WebSocket/_Handshake.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Client.pm »
1 package Linux::Event::WebSocket::_Parser;
2 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Linux::Event::WebSocket::_Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 164 ▶sub new ($class, %option) {
Subroutine new: called CC=9 SCAR=27.8
Pod undocumented
12 164 my $endpoint_type = delete $option{endpoint_type};
13 164 ▶ croak 'new(): endpoint_type must be client or server'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $endpoint_type or $endpoint_type ne "client" and $endpoint_type ne "server" 0 164 164
Condition50%3/6
not defined $endpoint_type or $endpoint_type ne "client" and $endpoint_type ne "server"
l!l&&r!l&&!r
00164
$endpoint_type ne "client" and $endpoint_type ne "server"
!ll&&!rl&&r
271370
MC/DC0%0/3
not defined $endpoint_type or $endpoint_type ne "client" and $endpoint_type ne "server"
not defined $endpoint_type $endpoint_type ne "client" $endpoint_type ne "server"
Truth table50%2/4
not(A) or B and C
A= not defined $endpoint_typeB= $endpoint_type ne "client"C= $endpoint_type ne "server"
ABCresult
1XX1
0111
00X0
0100
14 if !defined($endpoint_type)
15 || ($endpoint_type ne 'client' && $endpoint_type ne 'server');
16  
17 164 my $max_frame_size = delete $option{max_frame_size};
18 164 ▶ croak 'new(): max_frame_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max_frame_size or ref $max_frame_size or not "$max_frame_size" =~ /\A[0-9]+\z/u or $max_frame_size < 1 0 164 164
Condition33%3/9
not defined $max_frame_size or ref $max_frame_size or not "$max_frame_size" =~ /\A[0-9]+\z/u or $max_frame_size < 1
l!l&&r!l&&!r
00164
not defined $max_frame_size or ref $max_frame_size or not "$max_frame_size" =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
00164
not defined $max_frame_size or ref $max_frame_size
l!l&&r!l&&!r
00164
MC/DC0%0/4
not defined $max_frame_size or ref $max_frame_size or not "$max_frame_size" =~ /\A[0-9]+\z/u or $max_frame_size < 1
not defined $max_frame_size ref $max_frame_size not "$max_frame_size" =~ /\A[0-9]+\z/u $max_frame_size < 1
Truth table20%1/5
not(A) or B or not(C) or D
A= not defined $max_frame_sizeB= ref $max_frame_sizeC= not "$max_frame_size" =~ /\A[0-9]+\z/uD= $max_frame_size < 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
19 if !defined($max_frame_size) || ref($max_frame_size)
20 || "$max_frame_size" !~ /\A[0-9]+\z/ || $max_frame_size < 1;
21 164 ▶ croak 'new(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 164 164
22 if %option;
23  
24 164 return bless {
25 endpoint_type => $endpoint_type,
26 max_frame_size => 0 + $max_frame_size,
27 input => '',
28 }, $class;
29 }
30  
31 298 ▶sub feed ($self, $bytes) {
Subroutine feed: called CC=5 SCAR=22.5
Pod undocumented
32 298 ▶ croak 'feed(): bytes must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or ref $bytes 0 298 298
Condition33%1/3
not defined $bytes or ref $bytes
l!l&&r!l&&!r
00298
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
ABresult
1X1
011
000
33 if !defined($bytes) || ref($bytes);
34  
35 298 ▶ if (utf8::is_utf8($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($bytes)) { } 0 298 298
36 0 my $copy = "$bytes";
37 0 ▶ croak 'feed(): input must contain bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 0 0
38 if !utf8::downgrade($copy, 1);
39 0 $self->{input} .= $copy;
40 } else {
41 298 $self->{input} .= $bytes;
42 }
43 298 return $self;
44 }
45  
46 0 ▶sub buffered_bytes ($self) {
Subroutine buffered_bytes: not called CC=1 SCAR=6.9
Pod undocumented
47 0 return length $self->{input};
48 }
49  
50 0 ▶sub _check_size ($self, $high, $low) {
Subroutine _check_size: not called CC=6 SCAR=37.4
51 0 my $max = $self->{max_frame_size};
52 0 ▶ if (!$high) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($high) 0 0 0
53 0 ▶ die "WebSocket frame payload exceeds configured limit\n"
Branch: 1 branch
BranchTrueFalseTotal
if $low > $max 0 0 0
54 if $low > $max;
55 0 return $low;
56 }
57  
58 0 my $max_high = int($max / 4_294_967_296);
59 0 my $max_low = $max % 4_294_967_296;
60 0 ▶ if ($high > $max_high || ($high == $max_high && $low > $max_low)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high > $max_high or $high == $max_high and $low > $max_low) 0 0 0
Condition0%0/6
$high > $max_high or $high == $max_high and $low > $max_low
l!l&&r!l&&!r
000
$high == $max_high and $low > $max_low
!ll&&!rl&&r
000
MC/DC0%0/3
$high > $max_high or $high == $max_high and $low > $max_low
$high > $max_high $high == $max_high $low > $max_low
Truth table0%0/4
A or B and C
A= $high > $max_highB= $high == $max_highC= $low > $max_low
ABCresult
1XX1
0111
00X0
0100
61 0 die "WebSocket frame payload exceeds configured limit\n";
62 }
63 0 return $high * 4_294_967_296 + $low;
64 }
65  
66 302 ▶sub next_frame ($self) {
Subroutine next_frame: called CC=28 SCAR=33.4
Pod undocumented
67 302 ▶ return undef if length($self->{input}) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"input"} < 2 4 298 302
68  
69 my ($first, $second) = unpack(
70 'CC',
71 298 substr($self->{input}, 0, 2),
72 );
73 298 ▶ my $fin = $first & 0x80 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$first & 128 ? : 297 1 298
74 298 my $rsv = $first & 0x70;
75 298 my $opcode = $first & 0x0f;
76 298 ▶ my $masked = $second & 0x80 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$second & 128 ? : 269 29 298
77 298 my $length_code = $second & 0x7f;
78  
79 298 ▶ die "WebSocket frame uses reserved bits without a negotiated extension\n"
Branch: 1 branch
BranchTrueFalseTotal
if $rsv 1 297 298
80 if $rsv;
81 297 my $type = Linux::Event::WebSocket::_Frame->type($opcode);
82 297 ▶ die "WebSocket frame uses an unknown opcode $opcode\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 296 297
83 if !defined $type;
84 die "WebSocket client frame is not masked\n"
85 296 ▶ if $self->{endpoint_type} eq 'server' && !$masked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"endpoint_type"} eq "server" and not $masked 1 295 296
Condition100%3/3
$self->{"endpoint_type"} eq "server" and not $masked
!ll&&!rl&&r
272681
MC/DC100%2/2
$self->{"endpoint_type"} eq "server" and not $masked
$self->{"endpoint_type"} eq "server" not $masked
Truth table100%3/3
A and not(B)
A= $self->{"endpoint_type"} eq "server"B= not $masked
ABresult
0X0
100
111
86 die "WebSocket server frame is masked\n"
87 295 ▶ if $self->{endpoint_type} eq 'client' && $masked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"endpoint_type"} eq "client" and $masked 1 294 295
Condition100%3/3
$self->{"endpoint_type"} eq "client" and $masked
!ll&&!rl&&r
268261
MC/DC100%2/2
$self->{"endpoint_type"} eq "client" and $masked
$self->{"endpoint_type"} eq "client" $masked
Truth table100%3/3
A and B
A= $self->{"endpoint_type"} eq "client"B= $masked
ABresult
0X0
100
111
88  
89 294 my $control = $opcode >= 8;
90 294 ▶ die "WebSocket control frame is fragmented\n" if $control && !$fin;
Branch: 1 branch
BranchTrueFalseTotal
if $control and not $fin 1 293 294
Condition100%3/3
$control and not $fin
!ll&&!rl&&r
275181
MC/DC100%2/2
$control and not $fin
$control not $fin
Truth table100%3/3
A and not(B)
A= $controlB= not $fin
ABresult
0X0
100
111
91 293 ▶ die "WebSocket control frame payload exceeds 125 bytes\n"
Branch: 1 branch
BranchTrueFalseTotal
if $control and $length_code > 125 1 292 293
Condition100%3/3
$control and $length_code > 125
!ll&&!rl&&r
275171
MC/DC100%2/2
$control and $length_code > 125
$control $length_code > 125
Truth table100%3/3
A and B
A= $controlB= $length_code > 125
ABresult
0X0
100
111
92 if $control && $length_code > 125;
93  
94 292 my $cursor = 2;
95 292 my $length;
96 292 ▶ if ($length_code < 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length_code < 126) { } 23 269 292
elsif ($length_code == 126) { } 267 2 269
97 23 $length = $length_code;
98 die "WebSocket frame payload exceeds configured limit\n"
99 23 ▶ if $length > $self->{max_frame_size};
Branch: 1 branch
BranchTrueFalseTotal
if $length > $self->{"max_frame_size"} 0 23 23
100 } elsif ($length_code == 126) {
101 267 ▶ return undef if length($self->{input}) < $cursor + 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"input"} < $cursor + 2 2 265 267
102 265 $length = unpack('n', substr($self->{input}, $cursor, 2));
103 265 $cursor += 2;
104 265 ▶ die "WebSocket frame uses a non-minimal 16-bit payload length\n"
Branch: 1 branch
BranchTrueFalseTotal
if $length < 126 1 264 265
105 if $length < 126;
106 die "WebSocket frame payload exceeds configured limit\n"
107 264 ▶ if $length > $self->{max_frame_size};
Branch: 1 branch
BranchTrueFalseTotal
if $length > $self->{"max_frame_size"} 1 263 264
108 } else {
109 2 ▶ return undef if length($self->{input}) < $cursor + 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"input"} < $cursor + 8 0 2 2
110 2 my ($high, $low) = unpack('NN', substr($self->{input}, $cursor, 8));
111 2 $cursor += 8;
112 2 ▶ die "WebSocket frame 64-bit payload length has its most significant bit set\n"
Branch: 1 branch
BranchTrueFalseTotal
if $high & 2147483648 1 1 2
113 if $high & 0x80000000;
114 1 ▶ die "WebSocket frame uses a non-minimal 64-bit payload length\n"
Branch: 1 branch
BranchTrueFalseTotal
if $high == 0 and $low < 65536 1 0 1
Condition33%1/3
$high == 0 and $low < 65536
!ll&&!rl&&r
001
MC/DC0%0/2
$high == 0 and $low < 65536
$high == 0 $low < 65536
Truth table33%1/3
A and B
A= $high == 0B= $low < 65536
ABresult
0X0
100
111
115 if $high == 0 && $low < 65_536;
116 0 $length = $self->_check_size($high, $low);
117 }
118  
119 286 my $mask = '';
120 286 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 266 20 286
121 266 ▶ return undef if length($self->{input}) < $cursor + 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"input"} < $cursor + 4 4 262 266
122 262 $mask = substr($self->{input}, $cursor, 4);
123 262 $cursor += 4;
124 }
125  
126 282 ▶ return undef if length($self->{input}) < $cursor + $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"input"} < $cursor + $length 126 156 282
127 156 my $payload = substr($self->{input}, $cursor, $length);
128 156 ▶ $payload = Linux::Event::WebSocket::_Frame->mask($payload, $mask)
Branch: 1 branch
BranchTrueFalseTotal
if $masked 136 20 156
129 if $masked;
130  
131 156 substr($self->{input}, 0, $cursor + $length, '');
132 return {
133 156 fin => $fin,
134 opcode => $opcode,
135 type => $type,
136 payload => $payload,
137 };
138 }
139  
140 4 1;
« blib/lib/Linux/Event/WebSocket/_Handshake.pm ↑ Summary blib/lib/Linux/Event/WebSocket/Client.pm »