Skip to content

Commit

Permalink
Fix GH-16955: Use empheral ports for OpenSSL server client tests
Browse files Browse the repository at this point in the history
And refactor some client server tests.
  • Loading branch information
bukka committed Dec 31, 2024
1 parent 8a9d45b commit 89534a0
Show file tree
Hide file tree
Showing 44 changed files with 229 additions and 273 deletions.
65 changes: 50 additions & 15 deletions ext/openssl/tests/ServerClientTestCase.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ const WORKER_ARGV_VALUE = 'RUN_WORKER';

const WORKER_DEFAULT_NAME = 'server';

function phpt_notify($worker = WORKER_DEFAULT_NAME)
function phpt_notify(string $worker = WORKER_DEFAULT_NAME, string $message = ""): void
{
ServerClientTestCase::getInstance()->notify($worker);
ServerClientTestCase::getInstance()->notify($worker, $message);
}

function phpt_wait($worker = WORKER_DEFAULT_NAME, $timeout = null)
function phpt_wait($worker = WORKER_DEFAULT_NAME, $timeout = null): ?string
{
ServerClientTestCase::getInstance()->wait($worker, $timeout);
return ServerClientTestCase::getInstance()->wait($worker, $timeout);
}

function phpt_notify_server_start($server): void
{
ServerClientTestCase::getInstance()->notify_server_start($server);
}

function phpt_has_sslv3() {
Expand Down Expand Up @@ -119,43 +124,73 @@ class ServerClientTestCase
eval($code);
}

public function run($masterCode, $workerCode)
/**
* Run client and all workers
*
* @param string $clientCode The client PHP code
* @param string|array $workerCode
* @param bool $ephemeral Select whether automatic port selection and automatic awaiting is used
* @return void
* @throws Exception
*/
public function run(string $clientCode, string|array $workerCode, bool $ephemeral = true): void
{
if (!is_array($workerCode)) {
$workerCode = [WORKER_DEFAULT_NAME => $workerCode];
}
foreach ($workerCode as $worker => $code) {
reset($workerCode);
$code = current($workerCode);
$worker = key($workerCode);
while ($worker != null) {
$this->spawnWorkerProcess($worker, $this->stripPhpTagsFromCode($code));
$code = next($workerCode);
if ($ephemeral) {
$addr = trim($this->wait($worker));
if (empty($addr)) {
throw new \Exception("Failed server start");
}
if ($code === false) {
$clientCode = preg_replace('/{{\s*ADDR\s*}}/', $addr, $clientCode);
} else {
$code = preg_replace('/{{\s*ADDR\s*}}/', $addr, $code);
}
}
$worker = key($workerCode);
}
eval($this->stripPhpTagsFromCode($masterCode));

eval($this->stripPhpTagsFromCode($clientCode));
foreach ($workerCode as $worker => $code) {
$this->cleanupWorkerProcess($worker);
}
}

public function wait($worker, $timeout = null)
public function wait($worker, $timeout = null): ?string
{
$handle = $this->isWorker ? STDIN : $this->workerStdOut[$worker];
if ($timeout === null) {
fgets($handle);
return true;
return fgets($handle);
}

stream_set_blocking($handle, false);
$read = [$handle];
$result = stream_select($read, $write, $except, $timeout);
if (!$result) {
return false;
return null;
}

fgets($handle);
$result = fgets($handle);
stream_set_blocking($handle, true);
return true;
return $result;
}

public function notify(string $worker, string $message = ""): void
{
fwrite($this->isWorker ? STDOUT : $this->workerStdIn[$worker], "$message\n");
}

public function notify($worker)
public function notify_server_start($server): void
{
fwrite($this->isWorker ? STDOUT : $this->workerStdIn[$worker], "\n");
echo stream_socket_get_name($server, false) . "\n";
}
}

Expand Down
7 changes: 3 additions & 4 deletions ext/openssl/tests/bug46127.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@ if (!function_exists("proc_open")) die("skip no proc_open");
$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug46127.pem.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s',
]]);
$sock = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($sock);
$link = stream_socket_accept($sock);
fwrite($link, "Sending bug 46127\n");
CODE;
$serverCode = sprintf($serverCode, $certFile);

$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://{{ ADDR }}";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]]);
phpt_wait();
$sock = stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx);
echo fgets($sock);
Expand Down
7 changes: 3 additions & 4 deletions ext/openssl/tests/bug48182.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ $certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug48182.pem.tmp';
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug48182-ca.pem.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
$client = @stream_socket_accept($server, 1);
Expand All @@ -30,14 +30,13 @@ $serverCode = sprintf($serverCode, $certFile);

$peerName = 'bug48182';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://{{ ADDR }}";
$clientFlags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'cafile' => '%s',
'peer_name' => '%s'
]]);
phpt_wait();
$client = stream_socket_client($serverUri, $errno, $errstr, 10, $clientFlags, $clientCtx);
$data = "Sending data over to SSL server in async mode with contents like Hello World\n";
Expand Down
9 changes: 4 additions & 5 deletions ext/openssl/tests/bug54992.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ $certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug54992.pem.tmp';
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug54992-ca.pem.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
@stream_socket_accept($server, 1);
CODE;
Expand All @@ -28,15 +28,14 @@ $serverCode = sprintf($serverCode, $certFile);
$peerName = 'bug54992_actual_peer_name';
$wrongPeerName = 'bug54992_expected_peer_name';
$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://{{ ADDR }}";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => true,
'cafile' => '%s',
'peer_name' => '%s',
]]);
phpt_wait();
$client = stream_socket_client($serverUri, $errno, $errstr, 2, $clientFlags, $clientCtx);
var_dump($client);
Expand All @@ -61,5 +60,5 @@ Warning: stream_socket_client(): Peer certificate CN=`bug54992_actual_peer_name'

Warning: stream_socket_client(): Failed to enable crypto in %s on line %d

Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d
bool(false)
8 changes: 3 additions & 5 deletions ext/openssl/tests/bug62890.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ $serverCode = <<<'CODE'
'security_level' => 1,
]]);
$server = stream_socket_server('tls://127.0.0.1:64321', $errno, $errstr, $flags, $ctx);
phpt_notify();
$server = stream_socket_server('tls://127.0.0.1:0', $errno, $errstr, $flags, $ctx);
phpt_notify_server_start($server);
@stream_socket_accept($server, 3);
CODE;
$serverCode = sprintf($serverCode, $certFile);
Expand All @@ -33,9 +33,7 @@ $clientCode = <<<'CODE'
'security_level' => 1,
]]);
phpt_wait();
$client = stream_socket_client("tls://127.0.0.1:64321", $errno, $errstr, 3, $flags, $ctx);
$client = stream_socket_client("tls://{{ ADDR }}", $errno, $errstr, 3, $flags, $ctx);
var_dump($client);
CODE;

Expand Down
7 changes: 3 additions & 4 deletions ext/openssl/tests/bug65538_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ $certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001.pem.tmp';
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_001-ca.pem.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
$client = @stream_socket_accept($server);
if ($client) {
Expand All @@ -41,13 +41,12 @@ $serverCode = sprintf($serverCode, $certFile);

$peerName = 'bug65538_001';
$clientCode = <<<'CODE'
$serverUri = "https://127.0.0.1:64321/";
$serverUri = "https://{{ ADDR }}/";
$clientCtx = stream_context_create(['ssl' => [
'cafile' => 'file://%s',
'peer_name' => '%s',
]]);
phpt_wait();
$html = file_get_contents($serverUri, false, $clientCtx);
var_dump($html);
Expand Down
7 changes: 3 additions & 4 deletions ext/openssl/tests/bug65538_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ $cacertFile = 'bug65538_003-ca.pem';
$cacertPhar = __DIR__ . DIRECTORY_SEPARATOR . 'bug65538_003-ca.phar.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
$client = @stream_socket_accept($server);
if ($client) {
Expand All @@ -46,13 +46,12 @@ $serverCode = sprintf($serverCode, $certFile);

$peerName = 'bug65538_003';
$clientCode = <<<'CODE'
$serverUri = "https://127.0.0.1:64321/";
$serverUri = "https://{{ ADDR }}/";
$clientCtx = stream_context_create(['ssl' => [
'cafile' => 'phar://%s/%s',
'peer_name' => '%s',
]]);
phpt_wait();
$html = file_get_contents($serverUri, false, $clientCtx);
var_dump($html);
Expand Down
12 changes: 5 additions & 7 deletions ext/openssl/tests/bug65729.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ $certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65729.pem.tmp';
$cacertFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug65729-ca.pem.tmp';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s'
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
$expected_names = ['foo.test.com.sg', 'foo.test.com', 'FOO.TEST.COM', 'foo.bar.test.com'];
foreach ($expected_names as $name) {
Expand All @@ -29,11 +29,9 @@ CODE;
$serverCode = sprintf($serverCode, $certFile);

$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://{{ ADDR }}";
$clientFlags = STREAM_CLIENT_CONNECT;
phpt_wait();
$expected_names = ['foo.test.com.sg', 'foo.test.com', 'FOO.TEST.COM', 'foo.bar.test.com'];
foreach ($expected_names as $expected_name) {
$clientCtx = stream_context_create(['ssl' => [
Expand Down Expand Up @@ -65,7 +63,7 @@ Warning: stream_socket_client(): Peer certificate CN=`*.test.com' did not match

Warning: stream_socket_client(): Failed to enable crypto in %s on line %d

Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d
bool(false)
resource(%d) of type (stream)
resource(%d) of type (stream)
Expand All @@ -74,5 +72,5 @@ Warning: stream_socket_client(): Peer certificate CN=`*.test.com' did not match

Warning: stream_socket_client(): Failed to enable crypto in %s on line %d

Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:64321 (Unknown error) in %s on line %d
Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d
bool(false)
8 changes: 3 additions & 5 deletions ext/openssl/tests/bug68265.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@ $certFile = __DIR__ . DIRECTORY_SEPARATOR . 'bug68265.pem.tmp';
$san = 'DNS:debs.ak-online.be., DNS:debs.ak-online.net.';

$serverCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://127.0.0.1:0";
$serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
$serverCtx = stream_context_create(['ssl' => [
'local_cert' => '%s',
]]);
$server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx);
phpt_notify();
phpt_notify_server_start($server);
stream_socket_accept($server, 30);
CODE;
$serverCode = sprintf($serverCode, $certFile);

$clientCode = <<<'CODE'
$serverUri = "ssl://127.0.0.1:64321";
$serverUri = "ssl://{{ ADDR }}";
$clientFlags = STREAM_CLIENT_CONNECT;
$clientCtx = stream_context_create(['ssl' => [
'verify_peer' => false,
'verify_peer_name' => true,
'peer_name' => 'debs.ak-online.net',
]]);
phpt_wait();
var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx));
CODE;

Expand Down
Loading

0 comments on commit 89534a0

Please sign in to comment.