Skip to content

Commit

Permalink
Work around failing test case detecting EOF on TLS 1.3 socket streams
Browse files Browse the repository at this point in the history
This PR improves the test suite to avoid a possible race condition for
our TLS tests. It does not change anything about the actual behavior or
the expected output, but it helps making the expected output more
explicit and no longer subject to a possible race condition. This helps
avoiding possible false negatives if TLS 1.3 is supported and PHP
reports the EOF indicator before consuming all application data.

This builds on top of reactphp#185 and
reactphp#186
  • Loading branch information
clue committed May 26, 2019
1 parent 2cf8dfa commit 1f92698
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
17 changes: 0 additions & 17 deletions tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,21 +718,4 @@ private function createPromiseForEvent(EventEmitterInterface $emitter, $event, $
});
});
}

private function supportsTls13()
{
// TLS 1.3 is supported as of OpenSSL 1.1.1 (https://www.openssl.org/blog/blog/2018/09/11/release111/)
// The OpenSSL library version can only be obtained by parsing output from phpinfo().
// OPENSSL_VERSION_TEXT refers to header version which does not necessarily match actual library version
// see php -i | grep OpenSSL
// OpenSSL Library Version => OpenSSL 1.1.1 11 Sep 2018
ob_start();
phpinfo(INFO_MODULES);
$info = ob_get_clean();

if (preg_match('/OpenSSL Library Version => OpenSSL (\S+)/', $info, $match)) {
return version_compare($match[1], '1.1.1', '>=');
}
return false;
}
}
17 changes: 17 additions & 0 deletions tests/SecureIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public function testSendSmallDataToServerReceivesOneChunk()

public function testSendDataWithEndToServerReceivesAllData()
{
// PHP can report EOF on TLS 1.3 stream before consuming all data, so
// we explicitly use older TLS version instead. Selecting TLS version
// requires PHP 5.6+, so skip legacy versions if TLS 1.3 is supported.
// Continue if TLS 1.3 is not supported anyway.
if ($this->supportsTls13()) {
if (!defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$this->markTestSkipped('TLS 1.3 supported, but this legacy PHP version does not support explicit choice');
}

$this->connector = new SecureConnector(new TcpConnector($this->loop), $this->loop, array(
'verify_peer' => false,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
));
}

$disconnected = new Deferred();
$this->server->on('connection', function (ConnectionInterface $peer) use ($disconnected) {
$received = '';
Expand All @@ -113,6 +128,7 @@ public function testSendDataWithEndToServerReceivesAllData()
// await server to report connection "close" event
$received = Block\await($disconnected->promise(), $this->loop, self::TIMEOUT);

$this->assertEquals(strlen($data), strlen($received));
$this->assertEquals($data, $received);
}

Expand All @@ -136,6 +152,7 @@ public function testSendDataWithoutEndingToServerReceivesAllData()

$client->close();

$this->assertEquals(strlen($data), strlen($received));
$this->assertEquals($data, $received);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,21 @@ public function setExpectedException($exception, $exceptionMessage = '', $except
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}

protected function supportsTls13()
{
// TLS 1.3 is supported as of OpenSSL 1.1.1 (https://www.openssl.org/blog/blog/2018/09/11/release111/)
// The OpenSSL library version can only be obtained by parsing output from phpinfo().
// OPENSSL_VERSION_TEXT refers to header version which does not necessarily match actual library version
// see php -i | grep OpenSSL
// OpenSSL Library Version => OpenSSL 1.1.1 11 Sep 2018
ob_start();
phpinfo(INFO_MODULES);
$info = ob_get_clean();

if (preg_match('/OpenSSL Library Version => OpenSSL ([\d\.]+)/', $info, $match)) {
return version_compare($match[1], '1.1.1', '>=');
}
return false;
}
}

0 comments on commit 1f92698

Please sign in to comment.