Skip to content

Commit

Permalink
Improve test suite to avoid some possible race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 1, 2019
1 parent 1218d5a commit 54fba44
Showing 1 changed file with 84 additions and 10 deletions.
94 changes: 84 additions & 10 deletions tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,67 @@ public function setUp()
}
}

public function testEmitsConnectionForNewConnection()
public function testClientCanConnectToServer()
{
$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
));
$promise = $connector->connect($server->getAddress());

Block\await($promise, $loop, self::TIMEOUT);
/* @var ConnectionInterface $client */
$client = Block\await($promise, $loop, self::TIMEOUT);

$this->assertInstanceOf('React\Socket\ConnectionInterface', $client);
$this->assertEquals($server->getAddress(), $client->getRemoteAddress());

$client->close();
$server->close();
}

public function testServerEmitsConnectionForClientConnection()
{
$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));

$peer = new Promise(function ($resolve, $reject) use ($server) {
$server->on('connection', $resolve);
$server->on('error', $reject);
});

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
));
$client = $connector->connect($server->getAddress());

// await both client and server side end of connection
/* @var ConnectionInterface[] $both */
$both = Block\awaitAll(array($peer, $client), $loop, self::TIMEOUT);

// both ends of the connection are represented by different instances of ConnectionInterface
$this->assertCount(2, $both);
$this->assertInstanceOf('React\Socket\ConnectionInterface', $both[0]);
$this->assertInstanceOf('React\Socket\ConnectionInterface', $both[1]);
$this->assertNotSame($both[0], $both[1]);

// server side end has local server address and client end has remote server address
$this->assertEquals($server->getAddress(), $both[0]->getLocalAddress());
$this->assertEquals($server->getAddress(), $both[1]->getRemoteAddress());

// clean up all connections and server again
$both[0]->close();
$both[1]->close();
$server->close();
}

public function testWritesDataToConnection()
Expand Down Expand Up @@ -275,7 +320,7 @@ public function testEmitsErrorForClientWithTlsVersionMismatch()
Block\await($promise, $loop, self::TIMEOUT);
}

public function testEmitsConnectionForNewConnectionWithEncryptedCertificate()
public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificate()
{
$loop = Factory::create();

Expand All @@ -284,26 +329,30 @@ public function testEmitsConnectionForNewConnectionWithEncryptedCertificate()
'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem',
'passphrase' => 'swordfish'
));
$server->on('connection', $this->expectCallableOnce());

$peer = new Promise(function ($resolve, $reject) use ($server) {
$server->on('connection', $resolve);
$server->on('error', $reject);
});

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
));
$promise = $connector->connect($server->getAddress());
$connector->connect($server->getAddress());

Block\await($promise, $loop, self::TIMEOUT);
$connection = Block\await($peer, $loop, self::TIMEOUT);

$this->assertInstanceOf('React\Socket\ConnectionInterface', $connection);
}

public function testEmitsErrorForServerWithInvalidCertificate()
public function testClientRejectsWithErrorForServerWithInvalidCertificate()
{
$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => 'invalid.pem'
));
$server->on('connection', $this->expectCallableNever());
$server->on('error', $this->expectCallableOnce());

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
Expand All @@ -314,6 +363,31 @@ public function testEmitsErrorForServerWithInvalidCertificate()
Block\await($promise, $loop, self::TIMEOUT);
}

public function testServerEmitsErrorForClientWithInvalidCertificate()
{
$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => 'invalid.pem'
));

$peer = new Promise(function ($resolve, $reject) use ($server) {
$server->on('connection', function () use ($reject) {
$reject(new \RuntimeException('Did not expect connection to succeed'));
});
$server->on('error', $reject);
});

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
));
$connector->connect($server->getAddress());

$this->setExpectedException('RuntimeException', 'handshake');
Block\await($peer, $loop, self::TIMEOUT);
}

public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase()
{
$loop = Factory::create();
Expand Down

0 comments on commit 54fba44

Please sign in to comment.