xref: /PHP-7.3/ext/openssl/tests/bug48182.phpt (revision 1fab01be)
1--TEST--
2Bug #48182: ssl handshake fails during asynchronous socket connection
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 . 'bug48182.pem.tmp';
11$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug48182-ca.pem.tmp';
12
13$serverCode = <<<'CODE'
14    $serverUri = "ssl://127.0.0.1:64321";
15    $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
16    $serverCtx = stream_context_create(['ssl' => [
17        'local_cert' => '%s'
18    ]]);
19
20    $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
21    phpt_notify();
22
23    $client = @stream_socket_accept($server, 1);
24
25    $data = "Sending bug48182\n" . fread($client, 8192);
26    fwrite($client, $data);
27CODE;
28$serverCode = sprintf($serverCode, $certFile);
29
30$peerName = 'bug48182';
31$clientCode = <<<'CODE'
32    $serverUri = "ssl://127.0.0.1:64321";
33    $clientFlags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
34    $clientCtx = stream_context_create(['ssl' => [
35        'cafile' => '%s',
36        'peer_name' => '%s'
37    ]]);
38
39    phpt_wait();
40    $client = stream_socket_client($serverUri, $errno, $errstr, 10, $clientFlags, $clientCtx);
41
42    $data = "Sending data over to SSL server in async mode with contents like Hello World\n";
43
44    fwrite($client, $data);
45    echo fread($client, 1024);
46CODE;
47$clientCode = sprintf($clientCode, $cacertFile, $peerName);
48
49echo "Running bug48182\n";
50
51include 'CertificateGenerator.inc';
52$certificateGenerator = new CertificateGenerator();
53$certificateGenerator->saveCaCert($cacertFile);
54$certificateGenerator->saveNewCertAsFileWithKey($peerName, $certFile);
55
56include 'ServerClientTestCase.inc';
57ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
58?>
59--CLEAN--
60<?php
61@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug48182.pem.tmp');
62@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'bug48182-ca.pem.tmp');
63?>
64--EXPECT--
65Running bug48182
66Sending bug48182
67Sending data over to SSL server in async mode with contents like Hello World
68