-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple Firebird payload fake server to test suite
This is meant to test against certain fixed responses of Firebird servers. For now we add just a most basic test which verifies a connection attempt. Closes GH-6940.
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
function run_server(string $payloadFile): string { | ||
$cmd = [getenv("TEST_PHP_EXECUTABLE"), "-n", __DIR__ . "/payload_server.php", $payloadFile]; | ||
$descriptorspec = array( | ||
0 => STDIN, | ||
1 => STDOUT, | ||
2 => ['pipe', 'w'], | ||
); | ||
$proc = proc_open($cmd, $descriptorspec, $pipes); | ||
|
||
// First, wait for the payload server to declare itself ready. | ||
$bound = null; | ||
stream_set_blocking($pipes[2], false); | ||
for ($i = 0; $i < 60; $i++) { | ||
usleep(50000); // 50ms per try | ||
$status = proc_get_status($proc); | ||
if (empty($status['running'])) { | ||
echo "Server is not running\n"; | ||
proc_terminate($proc); | ||
exit(1); | ||
} | ||
while (($line = fgets($pipes[2])) !== false) { | ||
if (preg_match('/FB payload server listening on (.+)/', $line, $matches)) { | ||
$bound = $matches[1]; | ||
// Now that we've identified the listen address, close STDERR. | ||
// Otherwise the pipe may clog up with unread log messages. | ||
fclose($pipes[2]); | ||
break 2; | ||
} | ||
} | ||
} | ||
|
||
if ($bound === null) { | ||
echo "Server did not output startup message"; | ||
proc_terminate($proc); | ||
exit(1); | ||
} | ||
|
||
// Now wait for a connection to succeed. | ||
// note: even when server prints 'FB payload server listening on localhost:12345' | ||
// it might not be listening yet...need to wait until fsockopen() call returns | ||
$error = "Unable to connect to server\n"; | ||
for ($i=0; $i < 60; $i++) { | ||
usleep(50000); // 50ms per try | ||
$status = proc_get_status($proc); | ||
$fp = fsockopen("tcp://$bound"); | ||
// Failure, the server is no longer running | ||
if (!($status && $status['running'])) { | ||
$error = "Server is not running\n"; | ||
break; | ||
} | ||
// Success, Connected to servers | ||
if ($fp) { | ||
$error = ''; | ||
break; | ||
} | ||
} | ||
|
||
if ($fp) { | ||
fclose($fp); | ||
} | ||
|
||
if ($error) { | ||
echo $error; | ||
proc_terminate($proc); | ||
exit(1); | ||
} | ||
|
||
register_shutdown_function( | ||
function($proc) { | ||
proc_terminate($proc); | ||
/* Wait for server to shutdown */ | ||
for ($i = 0; $i < 60; $i++) { | ||
$status = proc_get_status($proc); | ||
if (!($status && $status['running'])) { | ||
break; | ||
} | ||
usleep(50000); | ||
} | ||
}, | ||
$proc | ||
); | ||
|
||
return $bound; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$socket = stream_socket_server("tcp://localhost:0", $errno, $errstr); | ||
if (!$socket) { | ||
echo "Can't start server: $errstr ($errno)\n"; | ||
exit(1); | ||
} | ||
|
||
$filename = $argv[1]; | ||
$payload = file_get_contents($filename); | ||
if ($payload === false) { | ||
echo "Can't read $filename\n"; | ||
exit(1); | ||
} | ||
|
||
fputs(STDERR, "FB payload server listening on " . stream_socket_get_name($socket, false) . "\n"); | ||
|
||
while ($conn = stream_socket_accept($socket)) { | ||
fwrite($conn, $payload); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
FB payload server satisfies connection attempt | ||
--SKIPIF-- | ||
<?php | ||
if (!extension_loaded('pdo_firebird')) die("skip pdo_firebird extension not available"); | ||
if (!extension_loaded('sockets')) die("skip sockets extension not available"); | ||
?> | ||
--FILE-- | ||
<?php | ||
require_once "payload_server.inc"; | ||
|
||
$address = run_server(__DIR__ . "/payload_test.data"); | ||
|
||
// no need to change the credentials; we're running against a fake server | ||
$dsn = "firebird:dbname=inet://$address/test"; | ||
$username = 'SYSDBA'; | ||
$password = 'masterkey'; | ||
|
||
new PDO($dsn, $username, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); | ||
?> | ||
--EXPECT-- |