]> granicus.if.org Git - php/commitdiff
Use ephemeral port in ftp tests
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 22 Oct 2020 10:53:57 +0000 (12:53 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 23 Oct 2020 08:48:21 +0000 (10:48 +0200)
And enable them to run in parallel.

ext/ftp/tests/CONFLICTS [deleted file]
ext/ftp/tests/server.inc

diff --git a/ext/ftp/tests/CONFLICTS b/ext/ftp/tests/CONFLICTS
deleted file mode 100644 (file)
index 254defd..0000000
+++ /dev/null
@@ -1 +0,0 @@
-server
index 0d0fea63c4c36a8329a9848654a1452de6a07424..33e32ac1bb059830de50123f3766624925739857 100644 (file)
@@ -4,21 +4,15 @@ $socket = null;
 $errno = 0;
 $context = stream_context_create(array('ssl' => array('local_cert' => dirname(__FILE__).'/cert.pem')));
 
-for ($i=0; $i<10 && !$socket; ++$i) {
-    $port = rand(50000, 65535);
-
-    $socket = @stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
-}
-//set anther random port that is not the same as $port
-do{
-    $pasv_port = rand(50000, 65535);
-}while($pasv_port == $port);
-
+$socket = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
 if (!$socket) {
     echo "$errstr ($errno)\n";
     die("could not start/bind the ftp server\n");
 }
 
+$socket_name = stream_socket_get_name($socket, false);
+$port = (int) substr($socket_name, strrpos($socket_name, ':') + 1);
+
 $pid = pcntl_fork();
 
 if ($pid) {
@@ -402,25 +396,20 @@ if ($pid) {
             $i=0;
 
             if (empty($bug73457)) {
-                do {
-                    if (!empty($ssl)) {
-                        $soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
-                    } else {
-                        $soc = @stream_socket_server("tcp://127.0.0.1:$pasv_port");
-                    }
-                    /* Could bind port, Try another port */
-                    if (!$soc) {
-                        $pasv_port = rand(50000, 65535);
-                    }
-                    $i++;
-                } while ($i<10 && !$soc);
-
+                if (!empty($ssl)) {
+                    $soc = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
+                } else {
+                    $soc = stream_socket_server("tcp://127.0.0.1:0");
+                }
                 if (!$soc) {
                     echo "$errstr ($errno)\n";
                     die("could not bind passive port\n");
                 }
+
+                $soc_name = stream_socket_get_name($soc, false);
+                $pasv_port = (int) substr($socket_name, strrpos($soc_name, ':') + 1);
             } else {
-                $pasv_port=1234;
+                $pasv_port = 1234;
             }
 
             $p2 = $pasv_port % ((int) 1 << 8);