diff --git a/src/Factory.php b/src/Factory.php index 17dce82..58bbcd7 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -111,6 +111,12 @@ public function awaitLogin(Client $client, $user, $pass) return; } + // reject if core rejects initialization + if ($type === 'ClientInitReject') { + $reject(new \RuntimeException('Connection rejected by Quassel core: ' . $data['Error'])); + return $client->close(); + } + // reject promise if login is rejected if ($type === 'ClientLoginReject') { $reject(new \RuntimeException('Unable to log in: ' . $data['Error'])); @@ -125,6 +131,10 @@ public function awaitLogin(Client $client, $user, $pass) return; } + + // otherwise reject if we receive an unexpected message + $reject(new \RuntimeException('Received unexpected "' . $type . '" message during login')); + $client->close(); }); // reject promise if client emits error diff --git a/tests/FactoryIntegrationTest.php b/tests/FactoryIntegrationTest.php index d27ebb4..7ad4bc2 100644 --- a/tests/FactoryIntegrationTest.php +++ b/tests/FactoryIntegrationTest.php @@ -154,6 +154,64 @@ public function testCreateClientWithAuthRejectsIfServerClosesAfterClientInit() Block\await($promise, $loop, 10.0); } + /** + * @expectedException RuntimeException + */ + public function testCreateClientWithAuthRejectsIfServerSendsClientInitRejectAfterClientInit() + { + $loop = LoopFactory::create(); + $server = new Server(0, $loop); + + $server->on('connection', function (ConnectionInterface $conn) { + $conn->once('data', function () use ($conn) { + $conn->write("\x00\x00\x00\x02"); + + $conn->on('data', function () use ($conn) { + // respond with rejection + $conn->write(FactoryIntegrationTest::encode(array( + 'MsgType' => 'ClientInitReject', + 'Error' => 'Too old' + ))); + }); + }); + }); + + $uri = str_replace('tcp://', '', $server->getAddress()); + $factory = new Factory($loop); + $promise = $factory->createClient('user:pass@' . $uri); + + Block\await($promise, $loop, 10.0); + } + + /** + * @expectedException RuntimeException + */ + public function testCreateClientWithAuthRejectsIfServerSendsUnknownMessageAfterClientInit() + { + $loop = LoopFactory::create(); + $server = new Server(0, $loop); + + $server->on('connection', function (ConnectionInterface $conn) { + $conn->once('data', function () use ($conn) { + $conn->write("\x00\x00\x00\x02"); + + $conn->on('data', function () use ($conn) { + // respond with unknown message + $conn->write(FactoryIntegrationTest::encode(array( + 'MsgType' => 'Unknown', + 'Error' => 'Ignored' + ))); + }); + }); + }); + + $uri = str_replace('tcp://', '', $server->getAddress()); + $factory = new Factory($loop); + $promise = $factory->createClient('user:pass@' . $uri); + + Block\await($promise, $loop, 10.0); + } + /** * @expectedException RuntimeException */