Skip to content

Commit f7f2b9d

Browse files
author
Paul
committed
Changed HttpConnectorTest to use peer verification for SSL tests.
Added try/finally blocks to HttpConnectorTest to faciliatate proper cleanup to prevent stalled builds and knock-on test failures.
1 parent 11b6c51 commit f7f2b9d

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

test/Functional/Porter/Net/Http/HttpConnectorTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ protected function setUp()
3434
public function testConnectionToLocalWebserver()
3535
{
3636
$server = $this->startServer('feedback');
37-
$response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')));
38-
$this->stopServer($server);
37+
38+
try {
39+
$response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')));
40+
} finally {
41+
$this->stopServer($server);
42+
}
3943

4044
self::assertRegExp('[\AGET \Q' . self::HOST . self::URI . '\E HTTP/\d+\.\d+$]m', $response);
4145
self::assertRegExp("[^$header$]m", $response);
@@ -47,12 +51,14 @@ public function testConnectionToLocalWebserver()
4751
public function testSslConnectionToLocalWebserver()
4852
{
4953
$server = $this->startServer('feedback');
50-
$this->startSsl();
51-
52-
$response = $this->fetchViaSsl(self::createUnverifiedSslConnector());
5354

54-
self::stopSsl();
55-
$this->stopServer($server);
55+
try {
56+
$certificate = $this->startSsl();
57+
$response = $this->fetchViaSsl(self::createSslConnector($certificate));
58+
} finally {
59+
self::stopSsl();
60+
$this->stopServer($server);
61+
}
5662

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

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

130-
self::waitForHttpServer(function () {
131-
$this->fetchViaSsl(self::createUnverifiedSslConnector());
136+
self::waitForHttpServer(function () use ($certificate) {
137+
$this->fetchViaSsl(self::createSslConnector($certificate));
132138
});
139+
140+
return $certificate;
133141
}
134142

135143
private static function stopSsl()
@@ -173,14 +181,17 @@ function (\Exception $exception) {
173181
}
174182

175183
/**
184+
* @param string $certificate
185+
*
176186
* @return HttpConnector
177187
*/
178-
private static function createUnverifiedSslConnector()
188+
private static function createSslConnector($certificate)
179189
{
180190
$connector = new HttpConnector($options = new HttpOptions);
181191
$options->getSslOptions()
182-
->setVerifyPeer(false)
183-
->setVerifyPeerName(false)
192+
->setCertificateAuthorityFilePath($certificate)
193+
// IPv6 names don't work normally due to a bug/feature in PHP/OpenSSL.
194+
->setPeerName('::1')
184195
;
185196

186197
return $connector;

test/Unit/Porter/Net/Http/HttpOptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testReplaceHeaders()
6262

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

6868
public function testUserAgent()

0 commit comments

Comments
 (0)