com php-src: Make the ftp and stream tests more reliable.: ext/ftp/tests/server.inc

[email protected] (Nikita Popov)
Newsgroups php.cvs
Message-ID <[email protected]>
Commit:    3b91ed112ff06d6b38a6db9e28649f2b79b40cad
Author:    Mitch Hagstrand <[email protected]>         Thu, 16 Feb 2017 01:19:47 -0800
Committer: Nikita Popov <[email protected]>      Thu, 16 Feb 2017 12:46:55 +0100
Parents:   5432d6f982e36afd6505ff0d9b7ffb7ffc98b7f1
Branches:  PHP-7.0 PHP-7.1 master

Link:       http://git.php.net/?p=php-src.git;a=commitdiff;h=3b91ed112ff06d6b38a6db9e28649f2b79b40cad

Log:
Make the ftp and stream tests more reliable.

The tests can sometimes fail because it chooses a passive port for ftp that
is already in use. This makes the test attempt multiple times to find a free
port.

Changed paths:
  M  ext/ftp/tests/server.inc


Diff:
diff --git a/ext/ftp/tests/server.inc b/ext/ftp/tests/server.inc
index ffef025..06c0d0d 100644
--- a/ext/ftp/tests/server.inc
+++ b/ext/ftp/tests/server.inc
@@ -6,8 +6,8 @@ $context = stream_context_create(array('ssl' => array('local_cert' => dirname(__
 
 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);
+
+	$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{
@@ -402,20 +402,34 @@ if ($pid) {
 
 		}elseif (preg_match('/^PASV/', $buf, $matches)) {
 			$pasv=true;
-			$p2 = $pasv_port % ((int) 1 << 8);
-			$p1 = ($pasv_port-$p2)/((int) 1 << 8);
 			$host = "127.0.0.1";
-			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");
+			$i=0;
+
+			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 (!$soc) {
+				echo "$errstr ($errno)\n";
+				die("could not bind passive port\n");
 			}
 
+			$p2 = $pasv_port % ((int) 1 << 8);
+			$p1 = ($pasv_port-$p2)/((int) 1 << 8);
 			fputs($s, "227 Entering Passive Mode. (127,0,0,1,{$p1},{$p2})\r\n");
 
 			$pasvs = stream_socket_accept($soc,10);
 
-		}elseif (preg_match('/^EPSV/', $buf, $matches)) {
+		} elseif (preg_match('/^EPSV/', $buf, $matches)) {
 			fputs($s, "550 Extended passsive mode not supported.\r\n");
 		} elseif (preg_match('/^SITE EXEC/', $buf, $matches)) {
 			fputs($s, "200 OK\r\n");
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.