summaryrefslogtreecommitdiff
path: root/ext/openssl/tests/bug77390.phpt
blob: 99ba43e50b68d55ebd0da47be77e9952d76b2509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--TEST--
Bug #76705: feof might hang on TLS streams in case of fragmented TLS records
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
?>
--FILE--
<?php
$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug77390.pem.tmp';
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug77390-ca.pem.tmp';

$peerName = 'bug77390';
$clientCode = <<<'CODE'
    $context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => '%s']]);

    phpt_wait('server');
    phpt_notify('proxy');

    phpt_wait('proxy');
    $fp = stream_socket_client("ssl://127.0.0.1:10012", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
    stream_set_blocking($fp, false);

    $read = [$fp];
    $buf = '';
    $emptyChunkPrinted = false;
    $warmedUp = false;
    while (stream_select($read, $write, $except, 1000)) {
        $chunk = stream_get_contents($fp, 4096);
        $buf .= $chunk;
        phpt_notify('proxy');
        if (!$warmedUp) {
            if ($buf !== 'warmup') {
                continue;
            }
            $warmedUp = true;
            $buf = '';
            phpt_notify('server');
            continue;
        }
        if ($chunk !== '' || !$emptyChunkPrinted) {
            $emptyChunkPrinted = true;
            var_dump($chunk);
        }
        if ($buf === 'hello, world') {
            break;
        }
    }

    phpt_notify('server');
    phpt_notify('proxy');
CODE;
$clientCode = sprintf($clientCode, $peerName);

$serverCode = <<<'CODE'
    $context = stream_context_create(['ssl' => ['local_cert' => '%s']]);

    $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
    $fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
    phpt_notify();

    $conn = stream_socket_accept($fp);
    fwrite($conn, 'warmup');
    phpt_wait();
    fwrite($conn, 'hello, world');

    phpt_wait();
    fclose($conn);
CODE;
$serverCode = sprintf($serverCode, $certFile);

$proxyCode = <<<'CODE'
    phpt_wait();

    $upstream = stream_socket_client("tcp://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT);
    stream_set_blocking($upstream, false);

    $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
    $server = stream_socket_server("tcp://127.0.0.1:10012", $errornum, $errorstr, $flags);
    phpt_notify();

    $conn = stream_socket_accept($server);
    stream_set_blocking($conn, false);

    $read = [$upstream, $conn];
    while (stream_select($read, $write, $except, 1)) {
        foreach ($read as $fp) {
            $data = stream_get_contents($fp);
            if ($fp === $conn) {
                fwrite($upstream, $data);
            } else {
                if ($data !== '' && $data[0] === chr(23)) {
                    $parts = str_split($data, (int) ceil(strlen($data) / 3));
                    foreach ($parts as $part) {
                        fwrite($conn, $part);
                        phpt_wait(null, 1);
                    }
                } else {
                    fwrite($conn, $data);
                }
            }
        }
        if (feof($upstream)) {
            break;
        }
        $read = [$upstream, $conn];
    }

    phpt_wait();
CODE;

include 'CertificateGenerator.inc';
$certificateGenerator = new CertificateGenerator();
$certificateGenerator->saveCaCert($cacertFile);
$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, [
    'server' => $serverCode,
    'proxy' => $proxyCode,
]);
?>
--CLEAN--
<?php
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug77390.pem.tmp');
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug77390-ca.pem.tmp');
?>
--EXPECT--
string(0) ""
string(12) "hello, world"