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