| 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
| |||||||||||||||||||||||||||||
| 24 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 25 | 1671 | ▶ | my $opcode = defined $a{opcode} ? $a{opcode} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1671 | ▶ | my $fin = exists $a{fin} ? $a{fin} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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))
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))
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1671 | my $len = length $payload; | |||||||||||||||||||||||||||
| 32 | 1671 | ▶ | my $out = pack 'C', ($fin ? 0x80 : 0) | ($opcode & 0x0F); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1671 | ▶ | if ($len < 126) { $out .= pack 'C', 0x80 | $len } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 49 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 50 | 18 | ▶ | my $opcode = defined $a{opcode} ? $a{opcode} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 18 | ▶ | my $fin = exists $a{fin} ? $a{fin} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 18 | my $len = length $payload; | |||||||||||||||||||||||||||
| 54 | 18 | ▶ | my $out = pack 'C', ($fin ? 0x80 : 0) | ($opcode & 0x0F); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 18 | ▶ | if ($len < 126) { $out .= pack 'C', $len } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 13897 | my $rsv = ($b0 >> 4) & 0x07; | |||||||||||||||||||||||||||
| 70 | 13897 | my $opcode = $b0 & 0x0F; | |||||||||||||||||||||||||||
| 71 | 13897 | ▶ | my $masked = ($b1 & 0x80) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | 13897 | ▶ | if ($opcode & 0x08) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 79 | 481 | ▶ | return 'control-fragmented' unless $fin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 81 | 481 | ▶ | return 'control-too-long' if $len > 125; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 13897 | ▶ | if ($len == 126) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 86 | 13220 | ▶ | return undef if length $buf < 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | 81 | $off = 10; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 13897 | my $mask; | |||||||||||||||||||||||||||
| 100 | 13897 | ▶ | if ($masked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 102 | 5 | ▶ | return undef if length $buf < $off + 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 5 | $mask = substr $buf, $off, 4; | |||||||||||||||||||||||||||
| 104 | 5 | $off += 4; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 13897 | ▶ | return undef if length $buf < $off + $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2138 | my $payload = substr $buf, $off, $len; | |||||||||||||||||||||||||||
| 109 | 2138 | ▶ | if ($masked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || '/'
MC/DC100%0/1
$a{'path'} || '/'
$a{'path'}
Truth table100%1/2
A
A= $a{'path'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 75 | ▶ | my $ver = exists $a{version} ? $a{version} : 13; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%2/3
defined $key and length $key
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
| |||||||||||||||||||||||||||||
| 143 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 144 | 75 | ▶ | $req .= "Sec-WebSocket-Version: $ver\r\n" if defined $ver; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 75 | ▶ | $req .= "Sec-WebSocket-Protocol: $a{protocol}\r\n" if $a{protocol}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 75 | ▶ | $req .= "$_\r\n" for @{ $a{extra} || [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$a{'extra'} || []
MC/DC100%1/1
$a{'extra'} || []
$a{'extra'}
Truth table100%2/2
A
A= $a{'extra'}
| |||||||||||||||||||||||||||||
| 147 | 75 | $req .= "\r\n"; | |||||||||||||||||||||||||||
| 148 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 149 | 75 | ▶ | return wantarray ? ($req, $key) : $req; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || '/'
$a{"query"} // ""
MC/DC100%0/1
$a{'path'} || '/'
$a{'path'}
MC/DC100%0/1
$a{"query"} // ""
$a{"query"}
Truth table100%1/2
A
A= $a{'path'}
Truth table100%1/2
A
A= $a{"query"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 175 | 57 | return ($env, $key); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 70 | 1; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||