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