Skip to content

Commit

Permalink
Changed HttpConnectorTest to use peer verification for SSL tests.
Browse files Browse the repository at this point in the history
Added try/finally blocks to HttpConnectorTest to faciliatate proper cleanup to prevent stalled builds and knock-on test failures.
  • Loading branch information
Paul committed Jun 1, 2017
1 parent 11b6c51 commit f7f2b9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
39 changes: 25 additions & 14 deletions test/Functional/Porter/Net/Http/HttpConnectorTest.php
Expand Up @@ -34,8 +34,12 @@ protected function setUp()
public function testConnectionToLocalWebserver()
{
$server = $this->startServer('feedback');
$response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')));
$this->stopServer($server);

try {
$response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')));
} finally {
$this->stopServer($server);
}

self::assertRegExp('[\AGET \Q' . self::HOST . self::URI . '\E HTTP/\d+\.\d+$]m', $response);
self::assertRegExp("[^$header$]m", $response);
Expand All @@ -47,12 +51,14 @@ public function testConnectionToLocalWebserver()
public function testSslConnectionToLocalWebserver()
{
$server = $this->startServer('feedback');
$this->startSsl();

$response = $this->fetchViaSsl(self::createUnverifiedSslConnector());

self::stopSsl();
$this->stopServer($server);
try {
$certificate = $this->startSsl();
$response = $this->fetchViaSsl(self::createSslConnector($certificate));
} finally {
self::stopSsl();
$this->stopServer($server);
}

self::assertRegExp('[\AGET \Q' . self::SSL_HOST . '\E/ HTTP/\d+\.\d+$]m', $response);
}
Expand Down Expand Up @@ -107,12 +113,12 @@ private function startSsl()
{
$accept = str_replace($filter = ['[', ']'], null, self::SSL_HOST);
$connect = str_replace($filter, null, self::HOST);
$certificate = tempnam(sys_get_temp_dir(), 'Porter');
$certificate = tempnam(sys_get_temp_dir(), null);

// Create SSL tunnel process.
(new Process(
// Generate self-signed SSL certificate in PEM format.
"openssl req -new -x509 -nodes -batch -keyout '$certificate' -out '$certificate'
"openssl req -new -x509 -nodes -subj /CN=::1 -keyout '$certificate' -out '$certificate'
{ stunnel4 -fd 0 || stunnel -fd 0; } <<.
# Disable PID to run as non-root user.
Expand All @@ -127,9 +133,11 @@ private function startSsl()
."
))->start();

self::waitForHttpServer(function () {
$this->fetchViaSsl(self::createUnverifiedSslConnector());
self::waitForHttpServer(function () use ($certificate) {
$this->fetchViaSsl(self::createSslConnector($certificate));
});

return $certificate;
}

private static function stopSsl()
Expand Down Expand Up @@ -173,14 +181,17 @@ function (\Exception $exception) {
}

/**
* @param string $certificate
*
* @return HttpConnector
*/
private static function createUnverifiedSslConnector()
private static function createSslConnector($certificate)
{
$connector = new HttpConnector($options = new HttpOptions);
$options->getSslOptions()
->setVerifyPeer(false)
->setVerifyPeerName(false)
->setCertificateAuthorityFilePath($certificate)
// IPv6 names don't work normally due to a bug/feature in PHP/OpenSSL.
->setPeerName('::1')
;

return $connector;
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/Porter/Net/Http/HttpOptionsTest.php
Expand Up @@ -62,7 +62,7 @@ public function testReplaceHeaders()

public function testProxy()
{
self::assertSame($host = 'https://example.com:80', (new HttpOptions)->setProxy($host)->getProxy());
self::assertSame($host = 'http://example.com', (new HttpOptions)->setProxy($host)->getProxy());
}

public function testUserAgent()
Expand Down

0 comments on commit f7f2b9d

Please sign in to comment.