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