3 $php = getenv('TEST_PHP_EXECUTABLE');
5 define('PHP_BIN', $php);
6 } else if (defined('PHP_BINARY')) {
7 define('PHP_BIN', PHP_BINARY);
10 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
14 /* stream_socket_server() automatically sets SO_REUSEADDR,
15 * which is, well, bad if the tests are run in parallel
17 $offset = rand(0,2000);
18 foreach (range(8000+$offset, 9000+$offset) as $port) {
19 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
20 fprintf(STDERR, "%s\n", $port);
22 $R = array($server); $W = array(); $E = array();
23 $select = stream_select($R, $E, $E, 0, 10000);
24 if ($select && ($client = stream_socket_accept($server, 1))) {
25 if (getenv("PHP_HTTP_TEST_SSL")) {
26 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
29 while (!feof($client)) {
32 } catch (Exception $ex) {
33 /* ignore disconnect */
34 if ($ex->getMessage() !== "Empty message received from stream") {
35 fprintf(STDERR, "%s\n", $ex);
40 } while ($select !== false);
46 function server($handler, $cb) {
47 $args = explode(' ', getenv('TEST_PHP_ARGS'));
48 $args[] = __DIR__."/$handler";
49 foreach ($args as $k => $v) {
50 if (!$v) unset($args[$k]);
52 proc(PHP_BIN, $args, $cb);
55 function nghttpd($cb) {
56 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
57 $offset = rand(0,2000);
58 foreach (range(8000+$offset, 9000+$offset) as $port) {
59 $comm = "exec nghttpd -d html $port http2.key http2.crt";
60 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
66 $status = proc_get_status($proc);
68 if (!$status["running"]) {
73 $cb($port, $stdin, $stdout, $stderr);
74 } catch (Exception $e) {
78 proc_terminate($proc);
88 function proc($bin, $args, $cb) {
89 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
90 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
91 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
97 $port = trim(fgets($stderr));
98 $R = array($stderr); $W = array(); $E = array();
99 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
101 if (is_numeric($port)) {
103 $cb($port, $stdin, $stdout, $stderr);
104 } catch (Exception $e) {
109 proc_terminate($proc);