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

filter: Statementstmt 79.4% 85 / 107 Branchbran 51.3% 38 / 74 Conditioncond 38.4% 15 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 9 totaltotal 56.3% 152 / 270 CC 52 SCAR 29.0
CC
52
Cov
63%
encode
34.7
encode_data
30.0
close_payload
13.7
CRAP 18.2
« blib/lib/Linux/Event/WebSocket/Client.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_Random.pm »
1 package Linux::Event::WebSocket::_Frame;
2 10 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Linux::Event::WebSocket::_Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Linux::Event::WebSocket::_UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 my %OPCODE = (
13 continuation => 0,
14 text => 1,
15 binary => 2,
16 close => 8,
17 ping => 9,
18 pong => 10,
19 );
20 10 my %TYPE = reverse %OPCODE;
21  
22 10 my %CLOSE_CODE = (
23 SUCCESS => 1000,
24 ENDPOINT_UNAVAILABLE => 1001,
25 PROTOCOL_ERROR => 1002,
26 INVALID_DATA_TYPE => 1003,
27 INVALID_PAYLOAD => 1007,
28 POLICY_VIOLATION => 1008,
29 MESSAGE_TOO_BIG => 1009,
30 UNSUPPORTED_EXTENSIONS => 1010,
31 INTERNAL_ERROR => 1011,
32 SERVER_ERROR => 1011,
33 SERVICE_RESTART => 1012,
34 TRY_AGAIN_LATER => 1013,
35 BAD_GATEWAY => 1014,
36 );
37  
38 297 ▶sub type ($class, $opcode) {
Subroutine type: called CC=1 SCAR=0.0
Pod undocumented
39 297 return $TYPE{$opcode};
40 }
41  
42 0 ▶sub opcode ($class, $type) {
Subroutine opcode: not called CC=1 SCAR=6.9
Pod undocumented
43 0 return $OPCODE{$type};
44 }
45  
46 172 ▶sub mask ($class, $payload, $key) {
Subroutine mask: called CC=5 SCAR=18.9
Pod undocumented
47 172 ▶ croak 'mask(): key must contain exactly four bytes'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or ref $key or length $key != 4 0 172 172
Condition33%2/6
not defined $key or ref $key or length $key != 4
l!l&&r!l&&!r
00172
not defined $key or ref $key
l!l&&r!l&&!r
00172
MC/DC0%0/3
not defined $key or ref $key or length $key != 4
not defined $key ref $key length $key != 4
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= ref $keyC= length $key != 4
ABCresult
1XX1
01X1
0011
0000
48 if !defined($key) || ref($key) || length($key) != 4;
49 172 ▶ return '' if !length $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless length $payload 0 172 172
50 172 my $mask = $key x (int(length($payload) / 4) + 1);
51 172 substr($mask, length($payload)) = '';
52 172 return $payload ^. $mask;
53 }
54  
55 0 ▶sub _mask_data ($payload, $key) {
Subroutine _mask_data: not called CC=2 SCAR=17.9
56 0 ▶ return '' if !length $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless length $payload 0 0 0
57 0 my $mask = $key x (int(length($payload) / 4) + 1);
58 0 substr($mask, length($payload)) = '';
59 0 return $payload ^. $mask;
60 }
61  
62 0 ▶sub encode_data ($class, $opcode, $bytes, $masked) {
Subroutine encode_data: not called CC=5 SCAR=34.0
Pod undocumented
63 0 my $length = length $bytes;
64 0 ▶ my $mask_bit = $masked ? 0x80 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$masked ? : 0 0 0
65 0 my $header;
66  
67 0 ▶ if ($length < 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length < 126) { } 0 0 0
elsif ($length < 65536) { } 0 0 0
68 0 $header = pack('CC', 0x80 | $opcode, $mask_bit | $length);
69 } elsif ($length < 65_536) {
70 0 $header = pack('CCn', 0x80 | $opcode, $mask_bit | 126, $length);
71 } else {
72 0 my $high = int($length / 4_294_967_296);
73 0 my $low = $length % 4_294_967_296;
74 0 $header = pack(
75 'CCNN',
76 0x80 | $opcode,
77 $mask_bit | 127,
78 $high,
79 $low,
80 );
81 }
82  
83 0 ▶ return $header . $bytes if !$masked;
Branch: 1 branch
BranchTrueFalseTotal
unless $masked 0 0 0
84  
85 0 my $mask = Linux::Event::WebSocket::_Random->mask_key;
86 0 return $header . $mask . _mask_data($bytes, $mask);
87 }
88  
89 42 ▶sub encode ($class, $type, $payload, %option) {
Subroutine encode: called CC=23 SCAR=35.5
Pod undocumented
90 croak "encode(): unknown frame type '$type'"
91 42 ▶ if !defined($type) || ref($type) || !exists $OPCODE{$type};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type or ref $type or not exists $OPCODE{$type} 0 42 42
Condition33%2/6
not defined $type or ref $type or not exists $OPCODE{$type}
l!l&&r!l&&!r
0042
not defined $type or ref $type
l!l&&r!l&&!r
0042
MC/DC0%0/3
not defined $type or ref $type or not exists $OPCODE{$type}
not defined $type ref $type not exists $OPCODE{$type}
Truth table25%1/4
not(A) or B or not(C)
A= not defined $typeB= ref $typeC= not exists $OPCODE{$type}
ABCresult
1XX1
01X1
0011
0000
92 42 ▶ croak 'encode(): payload must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $payload or ref $payload 0 42 42
Condition33%1/3
not defined $payload or ref $payload
l!l&&r!l&&!r
0042
MC/DC0%0/2
not defined $payload or ref $payload
not defined $payload ref $payload
Truth table33%1/3
not(A) or B
A= not defined $payloadB= ref $payload
ABresult
1X1
011
000
93 if !defined($payload) || ref($payload);
94  
95 42 my $bytes = "$payload";
96 42 ▶ croak 'encode(): payload must contain bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 42 42
97 if !utf8::downgrade($bytes, 1);
98  
99 42 ▶ my $fin = exists($option{fin}) ? delete($option{fin}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'fin'} ? : 3 39 42
100 42 ▶ my $masked = delete($option{masked}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $option{'masked'} ? : 36 6 42
101 42 my $mask_key = delete $option{mask_key};
102 42 ▶ croak 'encode(): mask_key requires masked output'
Branch: 1 branch
BranchTrueFalseTotal
if defined $mask_key and not $masked 0 42 42
Condition66%2/3
defined $mask_key and not $masked
!ll&&!rl&&r
6360
MC/DC0%0/2
defined $mask_key and not $masked
defined $mask_key not $masked
Truth table66%2/3
A and not(B)
A= defined $mask_keyB= not $masked
ABresult
0X0
100
111
103 if defined($mask_key) && !$masked;
104 42 ▶ croak 'encode(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 42 42
105 if %option;
106  
107 42 my $opcode = $OPCODE{$type};
108 42 my $control = $opcode >= 8;
109 42 ▶ croak 'encode(): control frames must be final' if $control && !$fin;
Branch: 1 branch
BranchTrueFalseTotal
if $control and not $fin 0 42 42
Condition66%2/3
$control and not $fin
!ll&&!rl&&r
31110
MC/DC0%0/2
$control and not $fin
$control not $fin
Truth table66%2/3
A and not(B)
A= $controlB= not $fin
ABresult
0X0
100
111
110 42 ▶ croak 'encode(): control frame payload exceeds 125 bytes'
Branch: 1 branch
BranchTrueFalseTotal
if $control and length $bytes > 125 0 42 42
Condition66%2/3
$control and length $bytes > 125
!ll&&!rl&&r
31110
MC/DC0%0/2
$control and length $bytes > 125
$control length $bytes > 125
Truth table66%2/3
A and B
A= $controlB= length $bytes > 125
ABresult
0X0
100
111
111 if $control && length($bytes) > 125;
112  
113 42 my $length = length $bytes;
114 42 my ($length_code, $extended);
115 42 ▶ if ($length < 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length < 126) { } 41 1 42
elsif ($length < 65536) { } 1 0 1
116 41 $length_code = $length;
117 41 $extended = '';
118 } elsif ($length < 65_536) {
119 1 $length_code = 126;
120 1 $extended = pack('n', $length);
121 } else {
122 0 $length_code = 127;
123 0 my $high = int($length / 4_294_967_296);
124 0 my $low = $length % 4_294_967_296;
125 0 $extended = pack('NN', $high, $low);
126 }
127  
128 42 ▶ my $first = $opcode | ($fin ? 0x80 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$fin ? : 39 3 42
129 42 ▶ my $second = $length_code | ($masked ? 0x80 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$masked ? : 36 6 42
130 42 my $mask = '';
131 42 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 36 6 42
132 36 ▶ $mask = defined($mask_key)
Branch: 1 branch
BranchTrueFalseTotal
defined $mask_key ? : 36 0 36
133 ? "$mask_key"
134 : Linux::Event::WebSocket::_Random->bytes(4);
135 36 ▶ croak 'encode(): mask_key must contain exactly four bytes'
Branch: 1 branch
BranchTrueFalseTotal
if length $mask != 4 0 36 36
136 if length($mask) != 4;
137 36 $bytes = $class->mask($bytes, $mask);
138 }
139  
140 42 return chr($first) . chr($second) . $extended . $mask . $bytes;
141 }
142  
143 34 ▶sub valid_close_code ($class, $code) {
Subroutine valid_close_code: called CC=3 SCAR=16.5
Pod undocumented
144 34 ▶ return 1 if grep { $_ == $code } values %CLOSE_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $code;} values %CLOSE_CODE 34 0 34
145 0 ▶ return $code >= 3000 && $code < 5000;
Condition0%0/3
$code >= 3000 && $code < 5000
!ll&&!rl&&r
000
MC/DC0%0/2
$code >= 3000 && $code < 5000
$code >= 3000 $code < 5000
Truth table0%0/3
A && B
A= $code >= 3000B= $code < 5000
ABresult
0X0
100
111
146 }
147  
148 8 ▶sub close_code ($class, $value) {
Subroutine close_code: called CC=6 SCAR=22.0
Pod undocumented
149 8 ▶ croak 'close code is required' if !defined($value) || ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 8 8
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
008
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
150 8 ▶ return $CLOSE_CODE{$value} if exists $CLOSE_CODE{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $CLOSE_CODE{$value} 0 8 8
151 8 ▶ croak "invalid WebSocket close code '$value'"
Branch: 1 branch
BranchTrueFalseTotal
unless "$value" =~ /\A[0-9]+\z/u and $class->valid_close_code(0 + $value) 0 8 8
Condition33%1/3
"$value" =~ /\A[0-9]+\z/u and $class->valid_close_code(0 + $value)
!ll&&!rl&&r
008
MC/DC0%0/2
"$value" =~ /\A[0-9]+\z/u and $class->valid_close_code(0 + $value)
"$value" =~ /\A[0-9]+\z/u $class->valid_close_code(0 + $value)
Truth table33%1/3
A and B
A= "$value" =~ /\A[0-9]+\z/uB= $class->valid_close_code(0 + $value)
ABresult
0X0
100
111
152 if "$value" !~ /\A[0-9]+\z/ || !$class->valid_close_code(0 + $value);
153 8 return 0 + $value;
154 }
155  
156 2 ▶sub close_payload ($class, $code, $reason = '') {
Subroutine close_payload: called CC=10 SCAR=26.2
Pod undocumented
157 2 ▶ croak 'close reason must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $reason or ref $reason 0 2 2
Condition33%1/3
not defined $reason or ref $reason
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $reason or ref $reason
not defined $reason ref $reason
Truth table33%1/3
not(A) or B
A= not defined $reasonB= ref $reason
ABresult
1X1
011
000
158 if !defined($reason) || ref($reason);
159 2 ▶ croak 'close reason requires a close code'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $code and length $reason 0 2 2
Condition33%1/3
not defined $code and length $reason
!ll&&!rl&&r
200
MC/DC0%0/2
not defined $code and length $reason
not defined $code length $reason
Truth table33%1/3
not(A) and B
A= not defined $codeB= length $reason
ABresult
0X0
100
111
160 if !defined($code) && length($reason);
161 2 ▶ return '' if !defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 2 2
162  
163 2 my $number = $class->close_code($code);
164 2 my $bytes = "$reason";
165 2 ▶ croak 'close reason must contain UTF-8 bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 2 2
166 if !utf8::downgrade($bytes, 1);
167 2 ▶ croak 'close reason exceeds 123 bytes' if length($bytes) > 123;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > 123 0 2 2
168 2 ▶ eval { Linux::Event::WebSocket::_UTF8->validate_bytes($bytes); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "Linux::Event::WebSocket::_UTF8"->validate_bytes($bytes); 1 } } or croak("close reason contains invalid UTF-8") 2 0 2
169 or croak 'close reason contains invalid UTF-8';
170 2 return pack('n', $number) . $bytes;
171 }
172  
173 26 ▶sub parse_close_payload ($class, $payload) {
Subroutine parse_close_payload: called CC=5 SCAR=16.7
Pod undocumented
174 26 my $length = length $payload;
175 26 ▶ die "WebSocket close frame has a one-byte payload\n" if $length == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $length == 1 0 26 26
176 26 ▶ return (undef, '') if !$length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 26 26
177  
178 26 my ($code, $reason) = unpack('na*', $payload);
179 26 ▶ die "WebSocket close frame contains invalid status code $code\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->valid_close_code($code) 0 26 26
180 if !$class->valid_close_code($code);
181 26 ▶ eval { Linux::Event::WebSocket::_UTF8->validate_bytes($reason); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "Linux::Event::WebSocket::_UTF8"->validate_bytes($reason); 1 } } or die "WebSocket close frame contains invalid UTF-8 reason\n" 26 0 26
182 or die "WebSocket close frame contains invalid UTF-8 reason\n";
183 26 return ($code, $reason);
184 }
185  
186 10 1;
« blib/lib/Linux/Event/WebSocket/Client.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_Random.pm »