Skip to content

Commit

Permalink
Add test for TLS without config
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Oct 3, 2018
1 parent c95926d commit efbe8fb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ServerSocket.php
Expand Up @@ -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."
Expand Down
5 changes: 4 additions & 1 deletion test/ClientTlsContextTest.php
Expand Up @@ -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(),
Expand All @@ -282,6 +285,6 @@ public function testStreamContextArray() {
"capture_peer_cert_chain" => $context->hasPeerCapturing(),
"SNI_enabled" => $context->hasSni(),
"capath" => $context->getCaPath(),
]], $context->toStreamContextArray());
]], $contextArray);
}
}
18 changes: 18 additions & 0 deletions test/SocketTest.php
Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit efbe8fb

Please sign in to comment.