Skip to content

Commit

Permalink
Add simple Firebird payload fake server to test suite
Browse files Browse the repository at this point in the history
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
cmb69 committed May 6, 2021
1 parent 178bbe3 commit f95f8a3
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
86 changes: 86 additions & 0 deletions ext/pdo_firebird/tests/payload_server.inc
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;
}
20 changes: 20 additions & 0 deletions ext/pdo_firebird/tests/payload_server.php
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 added ext/pdo_firebird/tests/payload_test.data
Binary file not shown.
21 changes: 21 additions & 0 deletions ext/pdo_firebird/tests/payload_test.phpt
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--

0 comments on commit f95f8a3

Please sign in to comment.