Switch to using shell-less proc_open() in various server tests

This commit is contained in:
Nikita Popov 2019-07-01 13:07:30 +02:00
parent 6285bb52fa
commit 17f7fb7605
9 changed files with 51 additions and 92 deletions

View File

@ -12,30 +12,13 @@ function curl_cli_server_start() {
$php_executable = getenv('TEST_PHP_EXECUTABLE'); $php_executable = getenv('TEST_PHP_EXECUTABLE');
$doc_root = __DIR__; $doc_root = __DIR__;
$router = "responder/get.inc"; $router = "responder/get.inc";
$cmd = [$php_executable, '-t', $doc_root, '-n', '-S', PHP_CURL_SERVER_ADDRESS, $router];
if (substr(PHP_OS, 0, 3) == 'WIN') { $descriptorspec = array(
$descriptorspec = array( 0 => STDIN,
0 => STDIN, 1 => STDOUT,
1 => STDOUT, 2 => array("null"),
2 => array("pipe", "w"), );
); $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
$cmd .= " {$router}";
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
} else {
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR,
);
$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
$cmd .= " {$router}";
$cmd .= " 2>/dev/null";
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
}
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.' // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
// it might not be listening yet...need to wait until fsockopen() call returns // it might not be listening yet...need to wait until fsockopen() call returns

View File

@ -7,25 +7,14 @@ function php_cli_server_start($ini = "") {
$php_executable = getenv('TEST_PHP_EXECUTABLE'); $php_executable = getenv('TEST_PHP_EXECUTABLE');
$doc_root = __DIR__; $doc_root = __DIR__;
if (substr(PHP_OS, 0, 3) == 'WIN') { $ini_array = preg_split('/\s+/', trim($ini));
$descriptorspec = array( $cmd = [$php_executable, '-t', $doc_root, '-n', ...$ini_array, '-S', PHP_CLI_SERVER_ADDRESS];
0 => STDIN, $descriptorspec = array(
1 => STDOUT, 0 => STDIN,
2 => array("pipe", "w"), 1 => STDOUT,
); 2 => array("null"),
);
$cmd = "{$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS; $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
} else {
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR,
);
$cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
}
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.' // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
// it might not be listening yet...need to wait until fsockopen() call returns // it might not be listening yet...need to wait until fsockopen() call returns

View File

@ -63,7 +63,8 @@ function get_data($max)
} }
$router = "bug73037_server.php"; $router = "bug73037_server.php";
$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : ""; $args = substr(PHP_OS, 0, 3) == 'WIN'
? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
$code = <<<'PHP' $code = <<<'PHP'
$s = new SoapServer(NULL, array('uri' => 'http://here')); $s = new SoapServer(NULL, array('uri' => 'http://here'));
$s->setObject(new stdclass()); $s->setObject(new stdclass());

View File

@ -15,13 +15,14 @@ server
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc"; include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";
$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : ""; $args = substr(PHP_OS, 0, 3) == 'WIN'
? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
$code = <<<'PHP' $code = <<<'PHP'
/* Receive */ /* Receive */
$content = trim(file_get_contents("php://input")) . PHP_EOL; $content = trim(file_get_contents("php://input")) . PHP_EOL;
PHP; PHP;
php_cli_server_start($code, false, $args); php_cli_server_start($code, null, $args);
$client = new soapclient(NULL, [ $client = new soapclient(NULL, [
'location' => 'http://' . PHP_CLI_SERVER_ADDRESS, 'location' => 'http://' . PHP_CLI_SERVER_ADDRESS,

View File

@ -3,7 +3,11 @@ define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
define ("PHP_CLI_SERVER_PORT", 8964); define ("PHP_CLI_SERVER_PORT", 8964);
define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT); define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.php', $cmd_args = null) { function php_cli_server_start(
?string $code = 'echo "Hello world";',
?string $router = 'index.php',
array $cmd_args = []
) {
$php_executable = getenv('TEST_PHP_EXECUTABLE'); $php_executable = getenv('TEST_PHP_EXECUTABLE');
$doc_root = __DIR__; $doc_root = __DIR__;
$error = null; $error = null;
@ -12,35 +16,18 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>'); file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>');
} }
if (substr(PHP_OS, 0, 3) == 'WIN') { $cmd = [$php_executable, '-t', $doc_root, '-n', ...$cmd_args, '-S', PHP_CLI_SERVER_ADDRESS];
$descriptorspec = array( if (!is_null($router)) {
0 => STDIN, $cmd[] = $router;
1 => STDOUT,
2 => array("pipe", "w"),
);
$cmd = "{$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
if (!is_null($router)) {
$cmd .= " {$router}";
}
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
} else {
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR,
);
$cmd = "exec {$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
if (!is_null($router)) {
$cmd .= " {$router}";
}
$cmd .= " 2>/dev/null";
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
} }
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => array("null"),
);
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
// note: here we check the process is running // note: here we check the process is running
for ($i=0; $i < 120; $i++) { for ($i=0; $i < 120; $i++) {
$status = proc_get_status($handle); $status = proc_get_status($handle);

View File

@ -37,8 +37,8 @@ echo "Test\n";
include "php_cli_server.inc"; include "php_cli_server.inc";
php_cli_server_start("var_dump(\$_FILES);", false, php_cli_server_start("var_dump(\$_FILES);", null,
"-d post_max_size=3G -d upload_max_filesize=3G"); ["-d", "post_max_size=3G", "-d", "upload_max_filesize=3G"]);
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS); list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)?:80; $port = intval($port)?:80;

View File

@ -27,7 +27,7 @@ EOT;
$prefix = __DIR__; $prefix = __DIR__;
$tester = new FPM\Tester($cfg); $tester = new FPM\Tester($cfg);
$tester->start('--prefix ' . $prefix); $tester->start(['--prefix', $prefix]);
$tester->expectLogStartNotices(); $tester->expectLogStartNotices();
$tester->expectFile(FPM\Tester::FILE_EXT_LOG_ACC, $prefix); $tester->expectFile(FPM\Tester::FILE_EXT_LOG_ACC, $prefix);
$tester->expectFile(FPM\Tester::FILE_EXT_LOG_ERR, $prefix); $tester->expectFile(FPM\Tester::FILE_EXT_LOG_ERR, $prefix);

View File

@ -320,25 +320,21 @@ class Tester
/** /**
* Start PHP-FPM master process * Start PHP-FPM master process
* *
* @param string $extraArgs * @param array $extraArgs
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function start(string $extraArgs = '') public function start(array $extraArgs = [])
{ {
$configFile = $this->createConfig(); $configFile = $this->createConfig();
$desc = $this->outDesc ? [] : [1 => array('pipe', 'w')]; $desc = $this->outDesc ? [] : [1 => array('pipe', 'w'), 2 => array('redirect', 1)];
$asRoot = getenv('TEST_FPM_RUN_AS_ROOT') ? '--allow-to-run-as-root' : ''; $cmd = [self::findExecutable(), '-F', '-O', '-y', $configFile];
$cmd = self::findExecutable() . " $asRoot -F -O -y $configFile $extraArgs"; if (getenv('TEST_FPM_RUN_AS_ROOT')) {
/* Since it's not possible to spawn a process under linux without using a $cmd[] = '--allow-to-run-as-root';
* shell in php (why?!?) we need a little shell trickery, so that we can }
* actually kill php-fpm */ $cmd = array_merge($cmd, $extraArgs);
$this->masterProcess = proc_open(
"killit () { kill \$child 2> /dev/null; }; " . $this->masterProcess = proc_open($cmd, $desc, $pipes);
"trap killit TERM; $cmd 2>&1 & child=\$!; wait",
$desc,
$pipes
);
register_shutdown_function( register_shutdown_function(
function($masterProcess) use($configFile) { function($masterProcess) use($configFile) {
@unlink($configFile); @unlink($configFile);

View File

@ -35,7 +35,9 @@ $opts = array('http' =>
$context = stream_context_create($opts); $context = stream_context_create($opts);
php_cli_server_start("exit(file_get_contents('php://input'));", false, "-d enable_post_data_reading=Off"); php_cli_server_start(
"exit(file_get_contents('php://input'));", null,
["-d", "enable_post_data_reading=Off"]);
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context)); var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context)); var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));