Skip to content

Commit 9e94625

Browse files
committed
Added unit tests to check for decoding invalid socket id
1 parent 35a56be commit 9e94625

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/AblyBroadcaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function formatChannels($channels)
320320
*/
321321
protected function buildAblyMessage($event, $payload = [], $socketIdObject = null)
322322
{
323-
$message = tap(new AblyMessage, function ($message) use ($event, $payload, $socketIdObject) {
323+
$message = tap(new AblyMessage, function ($message) use ($event, $payload) {
324324
$message->name = $event;
325325
$message->data = $payload;
326326
});

src/Utils.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Ably\LaravelBroadcaster;
44

55
use Ably\Exceptions\AblyException;
6-
use Illuminate\Broadcasting\BroadcastException;
76

87
class Utils
98
{
@@ -73,7 +72,7 @@ public static function base64url_decode($data) {
7372
return base64_decode(strtr($data, '-_', '+/'), true);
7473
}
7574

76-
const SOCKET_ID_ERROR = "please make sure to send base64 encoded json with "
75+
const SOCKET_ID_ERROR = "please make sure to send base64 url encoded json with "
7776
."'connectionKey' and 'clientId' as keys. 'clientId' is null if connection is not identified";
7877

7978
/**
@@ -87,9 +86,9 @@ public static function decodeSocketId($socketId) {
8786
throw new AblyException("SocketId decoding failed, ".self::SOCKET_ID_ERROR);
8887
}
8988
if (!isset($socketIdObject->connectionKey)) {
90-
throw new AblyException("ConnectionKey is missing, ".self::SOCKET_ID_ERROR);
89+
throw new AblyException("ConnectionKey is not set, ".self::SOCKET_ID_ERROR);
9190
}
92-
if (!isset($socketIdObject->clientId)) {
91+
if (!property_exists($socketIdObject, 'clientId')) {
9392
throw new AblyException("ClientId is missing, ".self::SOCKET_ID_ERROR);
9493
}
9594
}

tests/UtilsTest.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ably\LaravelBroadcaster\Tests;
44

5+
use Ably\Exceptions\AblyException;
56
use Ably\LaravelBroadcaster\Utils;
67

78
class UtilsTest extends TestCase
@@ -27,12 +28,49 @@ public function testGenerateAndValidateToken()
2728
self::assertTrue($jwtIsValid);
2829
}
2930

30-
public function testValidateDecodingSocketId() {
31+
/**
32+
* @throws AblyException
33+
*/
34+
public function testDecodingSocketId() {
3135
$socketId = new \stdClass();
3236
$socketId->connectionKey = 'key';
33-
$socketId->clientId = 'clientId';
37+
$socketId->clientId = null;
3438
$socketIdObject = Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketId)));
3539
self::assertEquals('key', $socketIdObject->connectionKey);
36-
self::assertEquals('clientId', $socketIdObject->clientId);
40+
self::assertNull($socketIdObject->clientId);
41+
42+
$socketId = new \stdClass();
43+
$socketId->connectionKey = 'key';
44+
$socketId->clientId = 'id';
45+
$socketIdObject = Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketId)));
46+
self::assertEquals('key', $socketIdObject->connectionKey);
47+
self::assertEquals('id', $socketIdObject->clientId);
48+
}
49+
50+
public function testExceptionOnDecodingInvalidSocketId()
51+
{
52+
self::expectException(AblyException::class);
53+
self::expectExceptionMessage("SocketId decoding failed, ".Utils::SOCKET_ID_ERROR);
54+
Utils::decodeSocketId("invalid_socket_id");
55+
}
56+
57+
public function testExceptionOnMissingClientIdInSocketId()
58+
{
59+
$socketId = new \stdClass();
60+
$socketId->connectionKey = 'key';
61+
62+
self::expectException(AblyException::class);
63+
self::expectExceptionMessage("ClientId is missing, ".Utils::SOCKET_ID_ERROR);
64+
Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketId)));
65+
}
66+
67+
public function testExceptionOnMissingConnectionKeyInSocketId()
68+
{
69+
$socketId = new \stdClass();
70+
$socketId->clientId = 'id';
71+
72+
self::expectException(AblyException::class);
73+
self::expectExceptionMessage("ConnectionKey is not set, ".Utils::SOCKET_ID_ERROR);
74+
Utils::decodeSocketId(Utils::base64url_encode(json_encode($socketId)));
3775
}
3876
}

0 commit comments

Comments
 (0)