From a0cb252cb54094137435ba843cf9064f19e69403 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 4 May 2021 17:28:44 +0200 Subject: [PATCH 1/2] 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. --- ext/pdo_firebird/tests/payload_server.inc | 86 ++++++++++++++++++++++ ext/pdo_firebird/tests/payload_server.php | 20 +++++ ext/pdo_firebird/tests/payload_test.data | Bin 0 -> 424 bytes ext/pdo_firebird/tests/payload_test.phpt | 20 +++++ 4 files changed, 126 insertions(+) create mode 100644 ext/pdo_firebird/tests/payload_server.inc create mode 100644 ext/pdo_firebird/tests/payload_server.php create mode 100644 ext/pdo_firebird/tests/payload_test.data create mode 100644 ext/pdo_firebird/tests/payload_test.phpt diff --git a/ext/pdo_firebird/tests/payload_server.inc b/ext/pdo_firebird/tests/payload_server.inc new file mode 100644 index 000000000000..03de54a06406 --- /dev/null +++ b/ext/pdo_firebird/tests/payload_server.inc @@ -0,0 +1,86 @@ + 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; +} diff --git a/ext/pdo_firebird/tests/payload_server.php b/ext/pdo_firebird/tests/payload_server.php new file mode 100644 index 000000000000..a2d2df4b7b2d --- /dev/null +++ b/ext/pdo_firebird/tests/payload_server.php @@ -0,0 +1,20 @@ +kvS4_>TgdBaUub{p_+4aN%WTvrqI<|I54CNRu(T`wOW?~m7Y-A>$} z(odJ~$sRr-KwBk2)W35MoMy^o#T;tL(bDWTnJt-9X|mvT(>4Xd2X&&Vz2&kpoNdg;5 mZTUDyX~faeRY2~&wOY83mv_%^uc!Rt;kWy1zC8cIzw=K&yiBYB literal 0 HcmV?d00001 diff --git a/ext/pdo_firebird/tests/payload_test.phpt b/ext/pdo_firebird/tests/payload_test.phpt new file mode 100644 index 000000000000..2c368394b5e4 --- /dev/null +++ b/ext/pdo_firebird/tests/payload_test.phpt @@ -0,0 +1,20 @@ +--TEST-- +FB payload server satisfies connection attempt +--SKIPIF-- + +--FILE-- + PDO::ERRMODE_EXCEPTION]); +?> +--EXPECT-- From 98aeabcd18224de70d6167d6f554923e3ed51eb5 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Wed, 5 May 2021 15:44:53 +0200 Subject: [PATCH 2/2] Add missing skipif clause We also fix a typo in a comment. --- ext/pdo_firebird/tests/payload_test.phpt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/pdo_firebird/tests/payload_test.phpt b/ext/pdo_firebird/tests/payload_test.phpt index 2c368394b5e4..6f9ef730f453 100644 --- a/ext/pdo_firebird/tests/payload_test.phpt +++ b/ext/pdo_firebird/tests/payload_test.phpt @@ -2,6 +2,7 @@ FB payload server satisfies connection attempt --SKIPIF-- --FILE-- @@ -10,7 +11,7 @@ require_once "payload_server.inc"; $address = run_server(__DIR__ . "/payload_test.data"); -// no need to change the credentials; we're running against a falke server +// no need to change the credentials; we're running against a fake server $dsn = "firebird:dbname=inet://$address/test"; $username = 'SYSDBA'; $password = 'masterkey';