xref: /php-src/ext/openssl/tests/bug72333.phpt (revision 74859783)
1--TEST--
2Bug #72333: fwrite() on non-blocking SSL sockets doesn't work
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!function_exists("proc_open")) die("skip no proc_open");
8?>
9--FILE--
10<?php
11$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug72333.pem.tmp';
12
13$serverCode = <<<'CODE'
14    $context = stream_context_create(['ssl' => ['local_cert' => '%s']]);
15
16    $flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
17    $fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
18    phpt_notify();
19    $conn = stream_socket_accept($fp);
20    $total = 100000;
21    $result = fread($conn, $total);
22    stream_set_blocking($conn, false);
23    usleep(50000);
24    $read = [$conn];
25    while (stream_select($read, $write, $except, 1)) {
26        $result = fread($conn, 100000);
27        if (!$result) {
28            break;
29        }
30        $total += strlen($result);
31        if ($total >= 4000000) {
32            break;
33        }
34    }
35    phpt_wait();
36CODE;
37$serverCode = sprintf($serverCode, $certFile);
38
39$peerName = 'bug72333';
40$clientCode = <<<'CODE'
41    $context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => '%s']]);
42
43    phpt_wait();
44    $fp = stream_socket_client("ssl://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
45    stream_set_blocking($fp, false);
46
47    function blocking_fwrite($fp, $buf) {
48        $write = [$fp];
49        $total = 0;
50        while (stream_select($read, $write, $except, 1)) {
51            $result = fwrite($fp, $buf);
52            if (!$result) {
53                break;
54            }
55            $total += $result;
56            if ($total >= strlen($buf)) {
57                return $total;
58            }
59            $buf = substr($buf, $total);
60        }
61    }
62    $str1 = str_repeat("a", 4000000);
63    blocking_fwrite($fp, $str1);
64    phpt_notify();
65    echo "done";
66CODE;
67$clientCode = sprintf($clientCode, $peerName);
68
69include 'CertificateGenerator.inc';
70$certificateGenerator = new CertificateGenerator();
71$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
72
73include 'ServerClientTestCase.inc';
74ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
75?>
76--CLEAN--
77<?php
78@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug72333.pem.tmp');
79?>
80--EXPECT--
81done
82