blib/lib/Unblock/WebSocket/_Deflate.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 68.9% 40 / 58 Conditioncond 48.2% 27 / 56 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 64.8% 155 / 239 CC 50 SCAR 30.4
CC
50
Cov
73%
new
36.6
decompress
15.7
compress
12.3
CRAP 20.8
« blib/lib/Unblock/WebSocket/_Engine.pm ↑ Summary blib/lib/Unblock/WebSocket/_Random.pm »
1 package Unblock::WebSocket::_Deflate;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Compress::Raw::Zlib 2.017 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 Z_BUF_ERROR
8 Z_HUFFMAN_ONLY
9 Z_OK
10 Z_SYNC_FLUSH
11 15 );
12  
13 15 my $TAIL = "\x00\x00\xff\xff";
14  
15 31 ▶sub new {
Subroutine new: called CC=24 SCAR=36.0
Pod undocumented
16 31 my ($class, %option) = @_;
17  
18 31 my $role = delete $option{role};
19 31 ▶ croak 'new(): role must be client or server'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $role and $role eq "client" || $role eq "server" 0 31 31
Condition50%3/6
defined $role and $role eq "client" || $role eq "server"
!ll&&!rl&&r
0031
$role eq "client" or $role eq "server"
l!l&&r!l&&!r
17140
MC/DC0%0/3
defined $role and $role eq "client" || $role eq "server"
defined $role $role eq "client" $role eq "server"
Truth table50%2/4
A and B or C
A= defined $roleB= $role eq "client"C= $role eq "server"
ABCresult
0XX0
1000
11X1
1011
20 unless defined($role) && ($role eq 'client' || $role eq 'server');
21  
22 31 my $config = delete $option{config};
23 31 ▶ croak 'new(): config must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 31 31
24 unless ref($config) eq 'HASH';
25  
26 31 my $max_message_size = delete $option{max_message_size};
27 31 ▶ croak 'new(): max_message_size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0 0 31 31
Condition33%3/9
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0
!ll&&!rl&&r
0031
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/
!ll&&!rl&&r
0031
defined $max_message_size and not ref $max_message_size
!ll&&!rl&&r
0031
MC/DC0%0/4
defined $max_message_size and not ref $max_message_size and $max_message_size =~ /\A[0-9]+\z/ and $max_message_size > 0
defined $max_message_size not ref $max_message_size $max_message_size =~ /\A[0-9]+\z/ $max_message_size > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $max_message_sizeB= not ref $max_message_sizeC= $max_message_size =~ /\A[0-9]+\z/D= $max_message_size > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
28 unless defined($max_message_size) && !ref($max_message_size)
29 && $max_message_size =~ /\A[0-9]+\z/ && $max_message_size > 0;
30  
31 31 ▶ croak 'new(): unknown option(s): ' . join(', ', sort keys %option)
Branch: 1 branch
BranchTrueFalseTotal
if %option 0 31 31
32 if %option;
33  
34 my $out_bits = $role eq 'client'
35 ? ($config->{client_max_window_bits} || 15)
36 31 ▶ : ($config->{server_max_window_bits} || 15);
Branch: 1 branch
BranchTrueFalseTotal
$role eq 'client' ? : 17 14 31
Condition100%4/4
$config->{"client_max_window_bits"} or 15
l!l
116
$config->{"server_max_window_bits"} or 15
l!l
113
MC/DC100%1/1
$config->{"client_max_window_bits"} or 15
$config->{"client_max_window_bits"}
MC/DC100%1/1
$config->{"server_max_window_bits"} or 15
$config->{"server_max_window_bits"}
Truth table100%2/2
A
A= $config->{"client_max_window_bits"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"server_max_window_bits"}
Aresult
11
00
37 31 ▶ croak 'permessage-deflate compressor window must be between 8 and 15'
Branch: 1 branch
BranchTrueFalseTotal
if $out_bits < 8 or $out_bits > 15 0 31 31
Condition33%1/3
$out_bits < 8 or $out_bits > 15
l!l&&r!l&&!r
0031
MC/DC0%0/2
$out_bits < 8 or $out_bits > 15
$out_bits < 8 $out_bits > 15
Truth table33%1/3
A or B
A= $out_bits < 8B= $out_bits > 15
ABresult
1X1
011
000
38 if $out_bits < 8 || $out_bits > 15;
39  
40 my $in_bits = $role eq 'client'
41 ? ($config->{server_max_window_bits} || 15)
42 31 ▶ : ($config->{client_max_window_bits} || 15);
Branch: 1 branch
BranchTrueFalseTotal
$role eq 'client' ? : 17 14 31
Condition100%4/4
$config->{"server_max_window_bits"} or 15
l!l
116
$config->{"client_max_window_bits"} or 15
l!l
113
MC/DC100%1/1
$config->{"server_max_window_bits"} or 15
$config->{"server_max_window_bits"}
MC/DC100%1/1
$config->{"client_max_window_bits"} or 15
$config->{"client_max_window_bits"}
Truth table100%2/2
A
A= $config->{"server_max_window_bits"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"client_max_window_bits"}
Aresult
11
00
43 31 ▶ croak 'permessage-deflate inflater window must be between 8 and 15'
Branch: 1 branch
BranchTrueFalseTotal
if $in_bits < 8 or $in_bits > 15 0 31 31
Condition33%1/3
$in_bits < 8 or $in_bits > 15
l!l&&r!l&&!r
0031
MC/DC0%0/2
$in_bits < 8 or $in_bits > 15
$in_bits < 8 $in_bits > 15
Truth table33%1/3
A or B
A= $in_bits < 8B= $in_bits > 15
ABresult
1X1
011
000
44 if $in_bits < 8 || $in_bits > 15;
45  
46 my $out_no_context = $role eq 'client'
47 ? $config->{client_no_context_takeover}
48 31 ▶ : $config->{server_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
$role eq 'client' ? : 17 14 31
49 my $in_no_context = $role eq 'client'
50 ? $config->{server_no_context_takeover}
51 31 ▶ : $config->{client_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
$role eq 'client' ? : 17 14 31
52  
53 31 ▶ return bless {
Branch: 2 branches
BranchTrueFalseTotal
$out_no_context ? : 5 26 31
$in_no_context ? : 4 27 31
54 role => $role,
55 config => { %$config },
56 max_message_size => 0 + $max_message_size,
57 out_bits => 0 + $out_bits,
58 in_bits => 0 + $in_bits,
59 out_no_context => $out_no_context ? 1 : 0,
60 in_no_context => $in_no_context ? 1 : 0,
61 deflater => undef,
62 inflater => undef,
63 }, $class;
64 }
65  
66 20 ▶sub _new_deflater {
Subroutine _new_deflater: called CC=4 SCAR=14.2
67 20 my ($self) = @_;
68 my %option = (
69 -WindowBits => -$self->{out_bits},
70 20 -AppendOutput => 1,
71 );
72  
73 # zlib promotes an 8-bit deflate window to 9 internally. For an RFC 7692
74 # window of 8, use a 9-bit raw stream with Huffman-only compression. That
75 # emits no LZ77 distance references, so the stream is valid for a peer
76 # constrained to a 256-byte window.
77 20 ▶ if ($self->{out_bits} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out_bits"} == 8) 2 18 20
78 2 $option{-WindowBits} = -9;
79 2 $option{-Strategy} = Z_HUFFMAN_ONLY;
80 }
81  
82 20 my ($z, $status) = Compress::Raw::Zlib::Deflate->new(%option);
83 20 ▶ croak "permessage-deflate deflater initialization failed: $status"
Branch: 1 branch
BranchTrueFalseTotal
unless $z and $status == Z_OK() 0 20 20
Condition33%1/3
$z and $status == Z_OK()
!ll&&!rl&&r
0020
MC/DC0%0/2
$z and $status == Z_OK()
$z $status == Z_OK()
Truth table33%1/3
A and B
A= $zB= $status == Z_OK()
ABresult
0X0
100
111
84 unless $z && $status == Z_OK;
85 20 return $z;
86 }
87  
88 15 ▶sub _new_inflater {
Subroutine _new_inflater: called CC=4 SCAR=14.3
89 15 my ($self) = @_;
90 15 my $bufsize = $self->{max_message_size} + 1;
91 15 ▶ $bufsize = 64 * 1024 if $bufsize > 64 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $bufsize > 65536 11 4 15
92  
93 my ($z, $status) = Compress::Raw::Zlib::Inflate->new(
94 -WindowBits => -$self->{in_bits},
95 15 -LimitOutput => 1,
96 -Bufsize => $bufsize,
97 -AppendOutput => 0,
98 );
99 15 ▶ croak "permessage-deflate inflater initialization failed: $status"
Branch: 1 branch
BranchTrueFalseTotal
unless $z and $status == Z_OK() 0 15 15
Condition33%1/3
$z and $status == Z_OK()
!ll&&!rl&&r
0015
MC/DC0%0/2
$z and $status == Z_OK()
$z $status == Z_OK()
Truth table33%1/3
A and B
A= $zB= $status == Z_OK()
ABresult
0X0
100
111
100 unless $z && $status == Z_OK;
101 15 return $z;
102 }
103  
104 22 ▶sub compress {
Subroutine compress: called CC=10 SCAR=25.1
Pod undocumented
105 22 my ($self, $payload) = @_;
106 22 ▶ croak 'compress(): payload must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $payload or ref $payload 0 22 22
Condition33%1/3
not defined $payload or ref $payload
l!l&&r!l&&!r
0022
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
107 if !defined($payload) || ref($payload);
108 22 my $bytes = "$payload";
109 22 ▶ croak 'compress(): payload must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 22 22
110 unless utf8::downgrade($bytes, 1);
111  
112 22 ▶ my $z = $self->{deflater} ||= $self->_new_deflater;
Condition66%2/3
$self->{"deflater"} ||= $self->_new_deflater
l!l&&r!l&&!r
2200
MC/DC0%0/2
$self->{"deflater"} ||= $self->_new_deflater
$self->{"deflater"} $self->_new_deflater
Truth table66%2/3
A ||= B
A= $self->{"deflater"}B= $self->_new_deflater
ABresult
1X1
011
000
113 22 my $output = '';
114  
115 22 my $status = $z->deflate($bytes, $output);
116 22 ▶ croak "permessage-deflate compression failed: $status"
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Z_OK() 0 22 22
117 unless $status == Z_OK;
118  
119 22 $status = $z->flush($output, Z_SYNC_FLUSH);
120 22 ▶ croak "permessage-deflate sync flush failed: $status"
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Z_OK() 0 22 22
121 unless $status == Z_OK;
122  
123 22 ▶ croak 'permessage-deflate compressor produced an invalid sync-flush tail'
Branch: 1 branch
BranchTrueFalseTotal
if length $output < 4 or substr($output, -4) ne $TAIL 0 22 22
Condition33%1/3
length $output < 4 or substr($output, -4) ne $TAIL
l!l&&r!l&&!r
0022
MC/DC0%0/2
length $output < 4 or substr($output, -4) ne $TAIL
length $output < 4 substr($output, -4) ne $TAIL
Truth table33%1/3
A or B
A= length $output < 4B= substr($output, -4) ne $TAIL
ABresult
1X1
011
000
124 if length($output) < 4 || substr($output, -4) ne $TAIL;
125 22 substr($output, -4, 4, '');
126  
127 22 ▶ $self->{deflater} = undef if $self->{out_no_context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"out_no_context"} 4 18 22
128 22 return $output;
129 }
130  
131 16 ▶sub decompress {
Subroutine decompress: called CC=12 SCAR=27.5
Pod undocumented
132 16 my ($self, $payload) = @_;
133 16 ▶ croak 'decompress(): payload must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $payload or ref $payload 0 16 16
Condition33%1/3
not defined $payload or ref $payload
l!l&&r!l&&!r
0016
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
134 if !defined($payload) || ref($payload);
135 16 my $input = "$payload";
136 16 ▶ croak 'decompress(): payload must be a byte string'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($input, 1) 0 16 16
137 unless utf8::downgrade($input, 1);
138 16 $input .= $TAIL;
139  
140 16 ▶ my $z = $self->{inflater} ||= $self->_new_inflater;
Condition66%2/3
$self->{"inflater"} ||= $self->_new_inflater
l!l&&r!l&&!r
1150
MC/DC0%0/2
$self->{"inflater"} ||= $self->_new_inflater
$self->{"inflater"} $self->_new_inflater
Truth table66%2/3
A ||= B
A= $self->{"inflater"}B= $self->_new_inflater
ABresult
1X1
011
000
141 16 my $output = '';
142  
143 16 while (1) {
144 16 my $chunk = '';
145 16 my $before = length($input);
146 16 my $status = $z->inflate($input, $chunk);
147  
148 16 ▶ croak "permessage-deflate decompression failed: $status"
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Z_OK() or $status == Z_BUF_ERROR() 1 15 16
Condition66%2/3
$status == Z_OK() or $status == Z_BUF_ERROR()
l!l&&r!l&&!r
1501
MC/DC50%1/2
$status == Z_OK() or $status == Z_BUF_ERROR()
$status == Z_OK() $status == Z_BUF_ERROR()
Truth table66%2/3
A or B
A= $status == Z_OK()B= $status == Z_BUF_ERROR()
ABresult
1X1
011
000
149 unless $status == Z_OK || $status == Z_BUF_ERROR;
150  
151 15 $output .= $chunk;
152 croak 'WebSocket message exceeds configured limit'
153 15 ▶ if length($output) > $self->{max_message_size};
Branch: 1 branch
BranchTrueFalseTotal
if length $output > $self->{"max_message_size"} 2 13 15
154  
155 13 ▶ last unless length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 13 0 13
156 0 ▶ croak 'permessage-deflate decompressor made no progress'
Branch: 1 branch
BranchTrueFalseTotal
if length $input == $before and not length $chunk 0 0 0
Condition0%0/3
length $input == $before and not length $chunk
!ll&&!rl&&r
000
MC/DC0%0/2
length $input == $before and not length $chunk
length $input == $before not length $chunk
Truth table0%0/3
A and not(B)
A= length $input == $beforeB= not length $chunk
ABresult
0X0
100
111
157 if length($input) == $before && !length($chunk);
158 }
159  
160 13 ▶ $self->{inflater} = undef if $self->{in_no_context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_no_context"} 2 11 13
161 13 return $output;
162 }
163  
164 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
165 0 my ($self) = @_;
166 0 return { %{ $self->{config} } };
167 }
168  
169 15 1;
« blib/lib/Unblock/WebSocket/_Engine.pm ↑ Summary blib/lib/Unblock/WebSocket/_Random.pm »