blib/lib/Punk/Test/WS.pm

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