|
| 1 | +--TEST-- |
| 2 | +Timeout for supplemental read at end of a blocking stream in SSL stream wrapper |
| 3 | +--EXTENSIONS-- |
| 4 | +openssl |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (!function_exists("proc_open")) die("skip no proc_open"); |
| 8 | +?> |
| 9 | +--FILE-- |
| 10 | +<?php |
| 11 | +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'crypto_supplemental_read_timeout.pem.tmp'; |
| 12 | +$peerName = 'crypto-supplemental-read-timeout'; |
| 13 | + |
| 14 | +$serverCode = <<<'CODE' |
| 15 | + $ctx = stream_context_create(['ssl' => ['local_cert' => '%s']]); |
| 16 | + $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN; |
| 17 | + $server = stream_socket_server("tls://127.0.0.1:0", $errno, $errstr, $flags, $ctx); |
| 18 | + phpt_notify_server_start($server); |
| 19 | +
|
| 20 | + $conn = stream_socket_accept($server, 30); |
| 21 | +
|
| 22 | + fwrite($conn, "hello\n"); |
| 23 | +
|
| 24 | + phpt_wait(); |
| 25 | + fclose($conn); |
| 26 | +CODE; |
| 27 | +$serverCode = sprintf($serverCode, $certFile); |
| 28 | + |
| 29 | +$clientCode = <<<'CODE' |
| 30 | + $ctx = stream_context_create(['ssl' => [ |
| 31 | + 'verify_peer' => false, |
| 32 | + 'verify_peer_name' => false, |
| 33 | + 'peer_name' => '%s', |
| 34 | + ]]); |
| 35 | +
|
| 36 | + $client = stream_socket_client("tls://{{ ADDR }}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx); |
| 37 | + stream_set_blocking($client, true); |
| 38 | + stream_set_timeout($client, 5); |
| 39 | + $start = hrtime(true); |
| 40 | +
|
| 41 | + $buf = ''; |
| 42 | + $read = [$client]; |
| 43 | + $write = $except = null; |
| 44 | + while (true) { |
| 45 | + if (!stream_select($read, $write, $except, 5)) { |
| 46 | + break; |
| 47 | + } |
| 48 | +
|
| 49 | + // Initially, read only the first char, then request more than is stored |
| 50 | + // in the buffer, triggering a supplemental read. |
| 51 | + $chunk = fread($client, strlen($buf) === 0 ? 1 : 10); |
| 52 | + if ($chunk === '' || $chunk === false) { |
| 53 | + /* A non-application record (e.g. a TLS 1.3 session ticket) may arrive first. */ |
| 54 | + if (feof($client)) { |
| 55 | + break; |
| 56 | + } |
| 57 | + } else { |
| 58 | + $buf .= $chunk; |
| 59 | + if (strlen($buf) >= 6) { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + $read = [$client]; |
| 64 | + $write = $except = null; |
| 65 | + } |
| 66 | +
|
| 67 | + echo trim($buf), "\n"; |
| 68 | +
|
| 69 | + $diff = (hrtime(true) - $start) / 1e9; |
| 70 | + var_dump($diff < 4.0); |
| 71 | +
|
| 72 | + phpt_notify(); |
| 73 | + fclose($client); |
| 74 | +CODE; |
| 75 | +$clientCode = sprintf($clientCode, $peerName); |
| 76 | + |
| 77 | +include 'CertificateGenerator.inc'; |
| 78 | +$certificateGenerator = new CertificateGenerator(); |
| 79 | +$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile); |
| 80 | + |
| 81 | +include 'ServerClientTestCase.inc'; |
| 82 | +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); |
| 83 | +?> |
| 84 | +--CLEAN-- |
| 85 | +<?php |
| 86 | +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'crypto_supplemental_read_timeout.pem.tmp'); |
| 87 | +?> |
| 88 | +--EXPECT-- |
| 89 | +hello |
| 90 | +bool(true) |
0 commit comments