diff --git a/tests/SSL/SSLTest.php b/tests/SSL/SSLTest.php index 727e6f9..efad60f 100644 --- a/tests/SSL/SSLTest.php +++ b/tests/SSL/SSLTest.php @@ -23,29 +23,33 @@ public function setUp() : void public static function setUpBeforeClass() : void { - $silence = '>/dev/null 2>&1 & echo $!'; + $testDir = dirname(__FILE__); - $commands = [ - sprintf("php -S %s", PHP_SERVER), - sprintf("stunnel3 -d %s -r %s -p %s -P '' -f", GOOD_STUNNEL_SERVER, PHP_SERVER, dirname(__FILE__) . "/" . "good.pem"), - sprintf("stunnel3 -d %s -r %s -p %s -P '' -f", SELF_SIGNED_STUNNEL_SERVER, PHP_SERVER, dirname(__FILE__) . "/" . "self.pem"), - sprintf("stunnel3 -d %s -r %s -p %s -P '' -f", BAD_HOSTNAME_STUNNEL_SERVER, PHP_SERVER, dirname(__FILE__) . "/" . "badhost.pem"), - ]; + // Use stunnel4 (or stunnel) with config files + $stunnelCmd = 'stunnel4'; + exec('which stunnel4 2>/dev/null', $output, $returnCode); + if ($returnCode !== 0) { + $stunnelCmd = 'stunnel'; + } - $pids = []; + // Start PHP server in background + $phpServerPid = trim(shell_exec(sprintf("php -S %s >/dev/null 2>&1 & echo \\$!", PHP_SERVER))); - foreach ($commands as $command) { - $output = []; - exec($command . $silence, $output); - $pid = (int) $output[0]; - array_push($pids, $pid); + // Start stunnel servers using config files (must run from test directory for relative cert paths) + $stunnelPids = []; + $configs = ['good', 'self', 'badhost']; + foreach ($configs as $name) { + $pid = trim(shell_exec(sprintf("cd %s && %s stunnel-%s.conf >/dev/null 2>&1 & echo \\$!", + $testDir, $stunnelCmd, $name))); + $stunnelPids[] = $pid; } // Allow processes to start sleep(1); - register_shutdown_function(function () use ($pids) { - foreach ($pids as $pid) { + register_shutdown_function(function () use ($phpServerPid, $stunnelPids) { + exec('kill ' . $phpServerPid); + foreach ($stunnelPids as $pid) { exec('kill ' . $pid); } }); diff --git a/tests/SSL/stunnel-badhost.conf b/tests/SSL/stunnel-badhost.conf new file mode 100644 index 0000000..4d8d5d6 --- /dev/null +++ b/tests/SSL/stunnel-badhost.conf @@ -0,0 +1,4 @@ +[badhost] +accept = 4445 +connect = localhost:4000 +cert = badhost.pem diff --git a/tests/SSL/stunnel-good.conf b/tests/SSL/stunnel-good.conf new file mode 100644 index 0000000..0ebdb29 --- /dev/null +++ b/tests/SSL/stunnel-good.conf @@ -0,0 +1,4 @@ +[good] +accept = 4443 +connect = localhost:4000 +cert = good.pem diff --git a/tests/SSL/stunnel-self.conf b/tests/SSL/stunnel-self.conf new file mode 100644 index 0000000..f4a5ff9 --- /dev/null +++ b/tests/SSL/stunnel-self.conf @@ -0,0 +1,4 @@ +[self] +accept = 4444 +connect = localhost:4000 +cert = self.pem