Skip to content

Commit

Permalink
Merge pull request #63 from clue-labs/ip-or-null
Browse files Browse the repository at this point in the history
Properly return null for unknown addresses
  • Loading branch information
clue authored Jan 25, 2017
2 parents 503874d + a967ef1 commit 761ffae
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public function handleClose()

public function getRemoteAddress()
{
return $this->parseAddress(stream_socket_get_name($this->stream, true));
return $this->parseAddress(@stream_socket_get_name($this->stream, true));
}

private function parseAddress($address)
{
if ($address === false) {
return null;
}

return trim(substr($address, 0, strrpos($address, ':')), '[]');
}
}
119 changes: 119 additions & 0 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use React\SocketClient\TcpConnector;
use React\Socket\Server;
use Clue\React\Block;
use React\Socket\ConnectionException;
use React\Socket\ConnectionInterface;

class FunctionalServerTest extends TestCase
{
Expand All @@ -26,6 +28,76 @@ public function testEmitsConnectionForNewConnection()
Block\sleep(0.1, $loop);
}

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

$server = new Server($loop);
$peer = null;
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$peer = $conn->getRemoteAddress();
});
$server->listen(0);
$port = $server->getPort();

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);

$promise->then($this->expectCallableOnce());

Block\sleep(0.1, $loop);

$this->assertEquals('127.0.0.1', $peer);
}

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

$server = new Server($loop);
$peer = null;
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$conn->on('close', function () use ($conn, &$peer) {
$peer = $conn->getRemoteAddress();
});
});
$server->listen(0);
$port = $server->getPort();

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);

$client = Block\await($promise, $loop, 0.1);
$client->end();

Block\sleep(0.1, $loop);

$this->assertEquals('127.0.0.1', $peer);
}

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

$server = new Server($loop);
$peer = null;
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$conn->close();
$peer = $conn->getRemoteAddress();
});
$server->listen(0);
$port = $server->getPort();

$connector = new TcpConnector($loop);
$promise = $connector->create('127.0.0.1', $port);

$promise->then($this->expectCallableOnce());

Block\sleep(0.1, $loop);

$this->assertNull($peer);
}

public function testEmitsConnectionEvenIfConnectionIsCancelled()
{
$loop = Factory::create();
Expand All @@ -43,4 +115,51 @@ public function testEmitsConnectionEvenIfConnectionIsCancelled()

Block\sleep(0.1, $loop);
}

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

$server = new Server($loop);
$server->on('connection', $this->expectCallableOnce());
try {
$server->listen(0, '::1');
} catch (ConnectionException $e) {
$this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)');
}
$port = $server->getPort();

$connector = new TcpConnector($loop);
$promise = $connector->create('::1', $port);

$promise->then($this->expectCallableOnce());

Block\sleep(0.1, $loop);
}

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

$server = new Server($loop);
$peer = null;
$server->on('connection', function (ConnectionInterface $conn) use (&$peer) {
$peer = $conn->getRemoteAddress();
});
try {
$server->listen(0, '::1');
} catch (ConnectionException $e) {
$this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)');
}
$port = $server->getPort();

$connector = new TcpConnector($loop);
$promise = $connector->create('::1', $port);

$promise->then($this->expectCallableOnce());

Block\sleep(0.1, $loop);

$this->assertEquals('::1', $peer);
}
}

0 comments on commit 761ffae

Please sign in to comment.