@@ -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 ;
0 commit comments