diff --git a/.travis.yml b/.travis.yml index 507576a..6409f1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,6 +24,11 @@ install: - wget https://github.com/php-coveralls/php-coveralls/releases/download/v1.0.2/coveralls.phar - chmod +x coveralls.phar +before_script: + - pushd test/tls + - ./regenerate.sh + - popd + script: # PHPDBG segfaults on versions other than 7.0 currently - if [ "$TRAVIS_PHP_VERSION" = "7.0" ]; then diff --git a/src/ServerSocket.php b/src/ServerSocket.php index 829cd53..522c270 100644 --- a/src/ServerSocket.php +++ b/src/ServerSocket.php @@ -15,7 +15,7 @@ public function enableCrypto(): Promise { $ctx = \stream_context_get_options($resource); if (empty($ctx['ssl'])) { - return new Failure(new SocketException( + return new Failure(new CryptoException( "Can't enable TLS without configuration. " . "If you used Amp\\Socket\\listen(), be sure to pass a ServerTlsContext as third argument, " . "otherwise set the 'ssl' context option to the PHP stream resource." diff --git a/test/ClientTlsContextTest.php b/test/ClientTlsContextTest.php index 260fb55..49d309d 100644 --- a/test/ClientTlsContextTest.php +++ b/test/ClientTlsContextTest.php @@ -271,6 +271,9 @@ public function testStreamContextArray() { $context = (new ClientTlsContext) ->withCaPath("/var/foobar"); + $contextArray = $context->toStreamContextArray(); + unset($contextArray['ssl']['security_level']); // present depending on OpenSSL version + $this->assertSame(["ssl" => [ "crypto_method" => $context->toStreamCryptoMethod(), "peer_name" => $context->getPeerName(), @@ -282,6 +285,6 @@ public function testStreamContextArray() { "capture_peer_cert_chain" => $context->hasPeerCapturing(), "SNI_enabled" => $context->hasSni(), "capath" => $context->getCaPath(), - ]], $context->toStreamContextArray()); + ]], $contextArray); } } diff --git a/test/SocketTest.php b/test/SocketTest.php index 9cb03ec..2b04387 100644 --- a/test/SocketTest.php +++ b/test/SocketTest.php @@ -5,6 +5,8 @@ use Amp\Loop; use Amp\Socket; use PHPUnit\Framework\TestCase; +use function Amp\asyncCall; +use function Amp\Promise\wait; class SocketTest extends TestCase { public function testReadAndClose() { @@ -39,4 +41,20 @@ public function testSocketAddress() { $this->assertSame($serverSocket->getRemoteAddress(), $serverSocket->getLocalAddress()); $this->assertSame($serverSocket->getRemoteAddress(), $clientSocket->getLocalAddress()); } + + public function testEnableCryptoWithoutTlsContext() { + $server = Socket\listen('127.0.0.1:0'); + + asyncCall(function () use ($server) { + yield Socket\connect($server->getAddress()); + }); + + /** @var Socket\ServerSocket $client */ + $client = wait($server->accept()); + + $this->expectException(Socket\CryptoException::class); + $this->expectExceptionMessage("Can't enable TLS without configuration."); + + wait($client->enableCrypto()); + } }