Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ private function sanitizeAddress($address)
{
// doc comment suggests IPv6 address is not enclosed in square brackets?

$pos = strrpos(':', $address);
$pos = strrpos($address, ':');
// this is an IPv6 address which includes colons but no square brackets
if ($pos !== false && substr($address, 0, 1) !== '[') {
if (strpos(':', $address) < $pos) {
if (strpos($address, ':') < $pos) {
$port = substr($address, $pos + 1);
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,46 @@ public function testCreatePair()

$this->loop->run();
}

public function provideSanitizeAddress()
{
return array(
array(
'127.0.0.1:1337',
),
array(
'[::1]:1337',
),
);
}

/**
* @dataProvider provideSanitizeAddress
*/
public function testSanitizeAddress($address)
{
$promise = $this->factory->createServer($address);
$server = $this->getValueFromResolvedPromise($promise);

$promise = $this->factory->createClient($server->getLocalAddress());
$client = $this->getValueFromResolvedPromise($promise);

$that = $this;
$server->on('message', function ($message, $remote, $server) use ($that) {
// once the server receives a message, send it pack to client and stop server
$server->send('response:' . $message, $remote);
$server->end();
});

$client->on('message', function ($message, $remote, $client) use ($that, $address) {
$that->assertEquals($address, $remote);

// once the client receives a message, stop client
$client->end();
});

$client->send('test');

$this->loop->run();
}
}