blib/lib/Punk/Test/WS.pm

filter: Statementstmt 100% 108 / 108 Branchbran 100% 55 / 70 (15 uncoverable) Conditioncond 100% 8 / 14 (6 uncoverable) MC/DCmcdc 100% 2 / 8 (6 uncoverable) Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 100% 191 / 218 (27 uncoverable) CC 45 SCAR 23.7
CC
45
Cov
100%
decode_ref
18.0
handshake_request
11.0
encode_client
9.0
CRAP 10.7
« blib/lib/Punk/DevError.pm ↑ Summary blib/lib/Punk/Test.pm »
1 package Punk::Test::WS;
2  
3 70 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 70 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 70 our $VERSION = '0.70';
9  
10 70 our @EXPORT_OK = qw(
11 encode_client encode_server decode_ref accept_key handshake_request
12 upgrade_env
13 );
14  
15 70 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 70 my $GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
19  
20 1671 ▶sub encode_client {
Subroutine encode_client: called CC=9 SCAR=22.0
Pod documented
21 1671 my (%a) = @_;
22 # uncoverable branch false
23 1671 ▶ my $payload = defined $a{payload} ? $a{payload} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $a{'payload'} ? : 1671 0 1671
24 # uncoverable branch false
25 1671 ▶ my $opcode = defined $a{opcode} ? $a{opcode} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $a{'opcode'} ? : 1671 0 1671
26 1671 ▶ my $fin = exists $a{fin} ? $a{fin} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $a{'fin'} ? : 14 1657 1671
27 # uncoverable condition left
28 # uncoverable condition false
29 1671 ▶ my $mask = $a{mask} || pack 'C4', map { int rand 256 } 1 .. 4;
Condition100%1/3
$a{'mask'} || pack('C4', map({int rand 256;} 1..4))
l!l&&r!l&&!r
016710
MC/DC100%0/2
$a{'mask'} || pack('C4', map({int rand 256;} 1..4))
$a{'mask'} pack('C4', map({int rand 256;} 1..4))
Truth table100%1/3
A || B
A= $a{'mask'}B= pack('C4', map({int rand 256;} 1..4))
ABresult
1X1
011
000
30  
31 1671 my $len = length $payload;
32 1671 ▶ my $out = pack 'C', ($fin ? 0x80 : 0) | ($opcode & 0x0F);
Branch: 1 branch
BranchTrueFalseTotal
$fin ? : 1664 7 1671
33 1671 ▶ if ($len < 126) { $out .= pack 'C', 0x80 | $len }
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 126) { } 162 1509 1671
elsif ($len <= 65535) { } 1507 2 1509
34 1507 elsif ($len <= 0xFFFF) { $out .= pack 'C', 0x80 | 126; $out .= pack 'n', $len }
35 2 else { $out .= pack 'C', 0x80 | 127; $out .= pack 'Q>', $len }
36 1671 $out .= $mask;
37  
38 1671 my @m = unpack 'C4', $mask;
39 1671 my @p = unpack 'C*', $payload;
40 1671 $p[$_] ^= $m[$_ % 4] for 0 .. $#p;
41 1671 return $out . pack 'C*', @p;
42 }
43  
44 # A server frame: never masked.
45 18 ▶sub encode_server {
Subroutine encode_server: called CC=7 SCAR=19.5
Pod documented
46 18 my (%a) = @_;
47 # uncoverable branch false
48 18 ▶ my $payload = defined $a{payload} ? $a{payload} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $a{'payload'} ? : 18 0 18
49 # uncoverable branch false
50 18 ▶ my $opcode = defined $a{opcode} ? $a{opcode} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $a{'opcode'} ? : 18 0 18
51 18 ▶ my $fin = exists $a{fin} ? $a{fin} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $a{'fin'} ? : 15 3 18
52  
53 18 my $len = length $payload;
54 18 ▶ my $out = pack 'C', ($fin ? 0x80 : 0) | ($opcode & 0x0F);
Branch: 1 branch
BranchTrueFalseTotal
$fin ? : 17 1 18
55 18 ▶ if ($len < 126) { $out .= pack 'C', $len }
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 126) { } 12 6 18
elsif ($len <= 65535) { } 4 2 6
56 4 elsif ($len <= 0xFFFF) { $out .= pack 'C', 126; $out .= pack 'n', $len }
57 2 else { $out .= pack 'C', 127; $out .= pack 'Q>', $len }
58 18 return $out . $payload;
59 }
60  
61 # Decode one frame. Returns a hashref (consumed, fin, opcode, payload,
62 # masked), or undef when more bytes are needed, or a string error name.
63 14500 ▶sub decode_ref {
Subroutine decode_ref: called CC=18 SCAR=28.9
Pod documented
64 14500 my ($buf) = @_;
65 14500 ▶ return undef if length $buf < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 2 603 13897 14500
66 13897 my ($b0, $b1) = unpack 'C2', $buf;
67 # uncoverable branch false class:ignore_covered_err
68 13897 ▶ my $fin = ($b0 & 0x80) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b0 & 128 ? : 13897 0 13897
69 13897 my $rsv = ($b0 >> 4) & 0x07;
70 13897 my $opcode = $b0 & 0x0F;
71 13897 ▶ my $masked = ($b1 & 0x80) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b1 & 128 ? : 5 13892 13897
72 13897 my $len = $b1 & 0x7F;
73 13897 my $off = 2;
74  
75 # uncoverable branch true class:ignore_covered_err
76 13897 ▶ return 'rsv' if $rsv;
Branch: 1 branch
BranchTrueFalseTotal
if $rsv 0 13897 13897
77 13897 ▶ if ($opcode & 0x08) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode & 8) 481 13416 13897
78 # uncoverable branch true
79 481 ▶ return 'control-fragmented' unless $fin;
Branch: 1 branch
BranchTrueFalseTotal
unless $fin 0 481 481
80 # uncoverable branch true
81 481 ▶ return 'control-too-long' if $len > 125;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 125 0 481 481
82 }
83  
84 13897 ▶ if ($len == 126) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len == 126) { } 13220 677 13897
elsif ($len == 127) { } 81 596 677
85 # uncoverable branch true
86 13220 ▶ return undef if length $buf < 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 4 0 13220 13220
87 13220 $len = unpack 'n', substr $buf, 2, 2;
88 13220 $off = 4;
89 }
90 elsif ($len == 127) {
91 # uncoverable branch true
92 81 ▶ return undef if length $buf < 10;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 10 0 81 81
93 81 $len = unpack 'Q>', substr $buf, 2, 8;
94 # uncoverable branch true
95 81 ▶ return 'length-msb' if $len & (1 << 63);
Branch: 1 branch
BranchTrueFalseTotal
if $len & '9223372036854775808' 0 81 81
96 81 $off = 10;
97 }
98  
99 13897 my $mask;
100 13897 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 5 13892 13897
101 # uncoverable branch true
102 5 ▶ return undef if length $buf < $off + 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < $off + 4 0 5 5
103 5 $mask = substr $buf, $off, 4;
104 5 $off += 4;
105 }
106 13897 ▶ return undef if length $buf < $off + $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < $off + $len 11759 2138 13897
107  
108 2138 my $payload = substr $buf, $off, $len;
109 2138 ▶ if ($masked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($masked) 5 2133 2138
110 5 my @m = unpack 'C4', $mask;
111 5 my @p = unpack 'C*', $payload;
112 5 $p[$_] ^= $m[$_ % 4] for 0 .. $#p;
113 5 $payload = pack 'C*', @p;
114 }
115 return {
116 2138 consumed => $off + $len, fin => $fin, opcode => $opcode,
117 payload => $payload, masked => $masked,
118 };
119 }
120  
121 39 ▶sub accept_key {
Subroutine accept_key: called CC=1 SCAR=0.0
Pod documented
122 39 my ($key) = @_;
123 39 return MIME::Base64::encode_base64(Digest::SHA::sha1($key . $GUID), '');
124 }
125  
126 # A complete client handshake request for a live server test.
127 75 ▶sub handshake_request {
Subroutine handshake_request: called CC=11 SCAR=24.0
Pod documented
128 75 my (%a) = @_;
129 # uncoverable condition right
130 75 ▶ my $path = $a{path} || '/';
Condition100%1/2
$a{'path'} || '/'
l!l
750
MC/DC100%0/1
$a{'path'} || '/'
$a{'path'}
Truth table100%1/2
A
A= $a{'path'}
Aresult
11
00
131 # exists, not truth: a test asking for an empty key means "send no
132 # key", which is exactly the case the server must reject
133 my $key = exists $a{key} ? $a{key}
134 : MIME::Base64::encode_base64(
135 75 ▶ pack('C16', map { int rand 256 } 1 .. 16), '');
Branch: 1 branch
BranchTrueFalseTotal
exists $a{'key'} ? : 2 73 75
136 75 ▶ my $ver = exists $a{version} ? $a{version} : 13;
Branch: 1 branch
BranchTrueFalseTotal
exists $a{'version'} ? : 2 73 75
137 75 my $req = "GET $path HTTP/1.1\r\n"
138 . "Host: $a{host}\r\n"
139 . "Upgrade: websocket\r\n"
140 . "Connection: Upgrade\r\n";
141 # uncoverable condition left
142 75 ▶ $req .= "Sec-WebSocket-Key: $key\r\n" if defined $key && length $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and length $key 73 2 75
Condition100%2/3
defined $key and length $key
!ll&&!rl&&r
0273
MC/DC100%1/2
defined $key and length $key
defined $key length $key
Truth table100%2/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
143 # uncoverable branch false
144 75 ▶ $req .= "Sec-WebSocket-Version: $ver\r\n" if defined $ver;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ver 75 0 75
145 75 ▶ $req .= "Sec-WebSocket-Protocol: $a{protocol}\r\n" if $a{protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"protocol"} 5 70 75
146 75 ▶ $req .= "$_\r\n" for @{ $a{extra} || [] };
Condition100%2/2
$a{'extra'} || []
l!l
1065
MC/DC100%1/1
$a{'extra'} || []
$a{'extra'}
Truth table100%2/2
A
A= $a{'extra'}
Aresult
11
00
147 75 $req .= "\r\n";
148 # uncoverable branch false
149 75 ▶ return wantarray ? ($req, $key) : $req;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 75 0 75
150 }
151  
152 # The PSGI env of an upgrade request, for driving an app in-process: the C
153 # handshake in punk_wshandshake.h validates env headers, not raw bytes.
154 # Returns ($env, $key); the caller adds psgix.io and any cookies.
155 57 ▶sub upgrade_env {
Subroutine upgrade_env: called CC=4 SCAR=13.9
Pod documented
156 57 my (%a) = @_;
157 my $key = MIME::Base64::encode_base64(
158 57 pack('C16', map { int rand 256 } 1 .. 16), '');
159 my $env = {
160 REQUEST_METHOD => 'GET',
161 PATH_INFO => $a{path} || '/',
162 # uncoverable condition right count:1
163 # uncoverable condition left count:2
164 57 ▶ QUERY_STRING => $a{query} // '',
Condition100%2/4
$a{'path'} || '/'
l!l
570
$a{"query"} // ""
l!l
057
MC/DC100%0/1
$a{'path'} || '/'
$a{'path'}
MC/DC100%0/1
$a{"query"} // ""
$a{"query"}
Truth table100%1/2
A
A= $a{'path'}
Aresult
11
00
Truth table100%1/2
A
A= $a{"query"}
Aresult
11
00
165 SERVER_NAME => 'localhost', SERVER_PORT => 80,
166 HTTP_HOST => 'localhost',
167 'psgi.url_scheme' => 'http',
168 HTTP_UPGRADE => 'websocket',
169 HTTP_CONNECTION => 'Upgrade',
170 HTTP_SEC_WEBSOCKET_KEY => $key,
171 HTTP_SEC_WEBSOCKET_VERSION => 13,
172 };
173 # uncoverable branch true
174 57 ▶ $env->{HTTP_SEC_WEBSOCKET_PROTOCOL} = $a{protocol} if $a{protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"protocol"} 0 57 57
175 57 return ($env, $key);
176 }
177  
178 70 1;
179  
180 __END__
« blib/lib/Punk/DevError.pm ↑ Summary blib/lib/Punk/Test.pm »