Skip to content

Commit d6fb7b8

Browse files
committed
Update openssl tests with new server/client test harness
1 parent 480e4f8 commit d6fb7b8

26 files changed

+960
-953
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
const WORKER_ARGV_VALUE = 'RUN_WORKER';
4+
5+
function phpt_notify()
6+
{
7+
ServerClientTestCase::getInstance()->notify();
8+
}
9+
10+
function phpt_wait()
11+
{
12+
ServerClientTestCase::getInstance()->wait();
13+
}
14+
15+
/**
16+
* This is a singleton to let the wait/notify functions work
17+
* I know it's horrible, but it's a means to an end
18+
*/
19+
class ServerClientTestCase
20+
{
21+
private $isWorker = false;
22+
23+
private $workerHandle;
24+
25+
private $workerStdIn;
26+
27+
private $workerStdOut;
28+
29+
private static $instance;
30+
31+
public static function getInstance($isWorker = false)
32+
{
33+
if (!isset(self::$instance)) {
34+
self::$instance = new self($isWorker);
35+
}
36+
37+
return self::$instance;
38+
}
39+
40+
public function __construct($isWorker = false)
41+
{
42+
if (!isset(self::$instance)) {
43+
self::$instance = $this;
44+
}
45+
46+
$this->isWorker = $isWorker;
47+
}
48+
49+
private function spawnWorkerProcess($code)
50+
{
51+
$cmd = sprintf('%s "%s" %s', PHP_BINARY, __FILE__, WORKER_ARGV_VALUE);
52+
53+
$this->workerHandle = proc_open($cmd, [['pipe', 'r'], ['pipe', 'w'], STDERR], $pipes);
54+
$this->workerStdIn = $pipes[0];
55+
$this->workerStdOut = $pipes[1];
56+
57+
fwrite($this->workerStdIn, $code . "\n---\n");
58+
}
59+
60+
private function cleanupWorkerProcess()
61+
{
62+
fclose($this->workerStdIn);
63+
fclose($this->workerStdOut);
64+
proc_close($this->workerHandle);
65+
}
66+
67+
private function stripPhpTagsFromCode($code)
68+
{
69+
return preg_replace('/^\s*<\?(?:php)?|\?>\s*$/i', '', $code);
70+
}
71+
72+
public function runWorker()
73+
{
74+
$code = '';
75+
76+
while (1) {
77+
$line = fgets(STDIN);
78+
79+
if (trim($line) === "---") {
80+
break;
81+
}
82+
83+
$code .= $line;
84+
}
85+
86+
eval($code);
87+
}
88+
89+
public function run($proc1Code, $proc2Code)
90+
{
91+
$this->spawnWorkerProcess($this->stripPhpTagsFromCode($proc2Code));
92+
eval($this->stripPhpTagsFromCode($proc1Code));
93+
$this->cleanupWorkerProcess();
94+
}
95+
96+
public function wait()
97+
{
98+
fgets($this->isWorker ? STDIN : $this->workerStdOut);
99+
}
100+
101+
public function notify()
102+
{
103+
fwrite($this->isWorker ? STDOUT : $this->workerStdIn, "\n");
104+
}
105+
}
106+
107+
if (isset($argv[1]) && $argv[1] === WORKER_ARGV_VALUE) {
108+
ServerClientTestCase::getInstance(true)->runWorker();
109+
}

ext/openssl/tests/bug46127.phpt

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,41 @@
22
#46127, openssl_sign/verify: accept different algos
33
--SKIPIF--
44
<?php
5-
if (!extension_loaded("openssl")) die("skip, openssl required");
6-
if (!extension_loaded("pcntl")) die("skip, pcntl required");
7-
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
8-
?>
5+
if (!extension_loaded("openssl")) die("skip openssl not loaded");
6+
if (!function_exists("proc_open")) die("skip no proc_open");
7+
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip openssl version too low");
98
--FILE--
109
<?php
11-
12-
function ssl_server($port) {
13-
$pem = dirname(__FILE__) . '/bug46127.pem';
14-
$ssl = array(
15-
'verify_peer' => false,
16-
'verify_host' => false,
17-
'allow_self_signed' => true,
18-
'local_cert' => $pem,
19-
// 'passphrase' => '',
20-
);
21-
$context = stream_context_create(array('ssl' => $ssl));
22-
$sock = stream_socket_server('ssl://127.0.0.1:'.$port, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);
23-
if (!$sock) return false;
24-
25-
$link = stream_socket_accept($sock);
26-
if (!$link) return false; // bad link?
27-
28-
fputs($link, "Sending bug 46127\n");
29-
30-
// close stuff
31-
fclose($link);
32-
fclose($sock);
33-
34-
exit;
35-
}
36-
37-
echo "Running bug46127\n";
38-
39-
$port = rand(15000, 32000);
40-
41-
$pid = pcntl_fork();
42-
if ($pid == 0) { // child
43-
ssl_server($port);
44-
exit;
45-
}
46-
47-
// client or failed
48-
sleep(1);
49-
$ctx = stream_context_create(['ssl' => [
50-
'verify_peer' => false,
51-
'verify_host' => false
52-
]]);
53-
$sock = stream_socket_client("ssl://127.0.0.1:{$port}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
54-
if (!$sock) exit;
55-
56-
echo fgets($sock);
57-
58-
pcntl_waitpid($pid, $status);
59-
60-
?>
61-
--EXPECTF--
62-
Running bug46127
10+
$serverCode = <<<'CODE'
11+
$serverUri = "ssl://127.0.0.1:64321";
12+
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
13+
$serverCtx = stream_context_create(['ssl' => [
14+
'local_cert' => __DIR__ . '/bug46127.pem',
15+
]]);
16+
17+
$sock = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
18+
phpt_notify();
19+
20+
$link = stream_socket_accept($sock);
21+
fwrite($link, "Sending bug 46127\n");
22+
CODE;
23+
24+
$clientCode = <<<'CODE'
25+
$serverUri = "ssl://127.0.0.1:64321";
26+
$clientFlags = STREAM_CLIENT_CONNECT;
27+
28+
$clientCtx = stream_context_create(['ssl' => [
29+
'verify_peer' => false,
30+
'verify_host' => false
31+
]]);
32+
33+
phpt_wait();
34+
$sock = stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx);
35+
36+
echo fgets($sock);
37+
CODE;
38+
39+
include 'ServerClientTestCase.inc';
40+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
41+
--EXPECT--
6342
Sending bug 46127

ext/openssl/tests/bug48182.phpt

Lines changed: 31 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,49 @@
11
--TEST--
2-
#48182,ssl handshake fails during asynchronous socket connection
2+
Bug #48182: ssl handshake fails during asynchronous socket connection
33
--SKIPIF--
44
<?php
5-
if (!extension_loaded("openssl")) die("skip, openssl required");
6-
if (!extension_loaded("pcntl")) die("skip, pcntl required");
7-
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
8-
?>
5+
if (!extension_loaded("openssl")) die("skip openssl not loaded");
6+
if (!function_exists("proc_open")) die("skip no proc_open");
7+
if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip openssl version too low");
98
--FILE--
109
<?php
10+
$serverCode = <<<'CODE'
11+
$serverUri = "ssl://127.0.0.1:64321";
12+
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
13+
$serverCtx = stream_context_create(['ssl' => [
14+
'local_cert' => __DIR__ . '/bug54992.pem'
15+
]]);
1116

12-
function ssl_server($port) {
13-
$host = 'ssl://127.0.0.1'.':'.$port;
14-
$flags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
15-
$data = "Sending bug48182\n";
16-
$pem = dirname(__FILE__) . '/bug54992.pem';
17-
$ssl_params = array( 'verify_peer' => false, 'allow_self_signed' => true, 'local_cert' => $pem);
18-
$ssl = array('ssl' => $ssl_params);
17+
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
18+
phpt_notify();
1919

20-
$context = stream_context_create($ssl);
21-
$sock = stream_socket_server($host, $errno, $errstr, $flags, $context);
22-
if (!$sock) return false;
20+
$client = @stream_socket_accept($server, 1);
2321

24-
$link = stream_socket_accept($sock);
25-
if (!$link) return false; // bad link?
22+
$data = "Sending bug48182\n" . fread($client, 8192);
23+
fwrite($client, $data);
24+
CODE;
2625

27-
$r = array($link);
28-
$w = array();
29-
$e = array();
30-
if (stream_select($r, $w, $e, 1, 0) != 0)
31-
$data .= fread($link, 8192);
26+
$clientCode = <<<'CODE'
27+
$serverUri = "ssl://127.0.0.1:64321";
28+
$clientFlags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
29+
$clientCtx = stream_context_create(['ssl' => [
30+
'cafile' => __DIR__ . '/bug54992-ca.pem',
31+
'CN_match' => 'bug54992.local'
32+
]]);
3233
33-
$r = array();
34-
$w = array($link);
35-
if (stream_select($r, $w, $e, 1, 0) != 0)
36-
$wrote = fwrite($link, $data, strlen($data));
34+
phpt_wait();
35+
$client = stream_socket_client($serverUri, $errno, $errstr, 10, $clientFlags, $clientCtx);
3736
38-
// close stuff
39-
fclose($link);
40-
fclose($sock);
37+
$data = "Sending data over to SSL server in async mode with contents like Hello World\n";
4138
42-
exit;
43-
}
44-
45-
function ssl_async_client($port) {
46-
$host = 'ssl://127.0.0.1'.':'.$port;
47-
$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
48-
$data = "Sending data over to SSL server in async mode with contents like Hello World\n";
49-
$context = stream_context_create(array('ssl' => array(
50-
'cafile' => dirname(__FILE__) . '/bug54992-ca.pem',
51-
'CN_match' => 'bug54992.local'
52-
)));
53-
$socket = stream_socket_client($host, $errno, $errstr, 10, $flags, $context);
54-
stream_set_blocking($socket, 0);
55-
56-
while ($socket && $data) {
57-
$wrote = fwrite($socket, $data, strlen($data));
58-
$data = substr($data, $wrote);
59-
}
60-
61-
$r = array($socket);
62-
$w = array();
63-
$e = array();
64-
if (stream_select($r, $w, $e, 1, 0) != 0)
65-
{
66-
$data .= fread($socket, 1024);
67-
}
68-
69-
echo "$data";
70-
71-
fclose($socket);
72-
}
39+
fwrite($client, $data);
40+
echo fread($client, 1024);
41+
CODE;
7342

7443
echo "Running bug48182\n";
7544

76-
$port = rand(15000, 32000);
77-
78-
$pid = pcntl_fork();
79-
if ($pid == 0) { // child
80-
ssl_server($port);
81-
exit;
82-
}
83-
84-
// client or failed
85-
sleep(1);
86-
ssl_async_client($port);
87-
88-
pcntl_waitpid($pid, $status);
89-
90-
?>
45+
include 'ServerClientTestCase.inc';
46+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
9147
--EXPECTF--
9248
Running bug48182
9349
Sending bug48182

0 commit comments

Comments
 (0)