Skip to content

Commit

Permalink
Merge pull request #281 from mikesoule/feature/rfc-message-roles
Browse files Browse the repository at this point in the history
Ensured valid roles in Welcome Message per the RFC spec.
  • Loading branch information
mbonneau committed Jan 22, 2018
2 parents 400c8ea + 3e49d6a commit d576664
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Realm.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,24 @@ private function processAbort(Session $session, Message $msg)
*/
private function processSendWelcome(Session $session, WelcomeMessage $msg)
{
$details = $session->getHelloMessage()->getDetails();
$helloDetails = $session->getHelloMessage()->getDetails();

if (is_object($details) && isset($details->roles) && is_object($details->roles)) {
$session->setRoleFeatures($details->roles);
if (is_object($helloDetails) && isset($helloDetails->roles) && is_object($helloDetails->roles)) {
$session->setRoleFeatures($helloDetails->roles);
}

$session->setState(Session::STATE_UP); // this should probably be after authentication

$details = $msg->getDetails();

if (is_object($details) && isset($details->roles) && is_object($details->roles)) {
$roles = array_filter((array) $details->roles, function($key) {
return in_array($key, ['broker', 'dealer']);
}, ARRAY_FILTER_USE_KEY);

$details->roles = (object) $roles;
$msg->setDetails($details);
}
}


Expand Down
92 changes: 92 additions & 0 deletions tests/Unit/RealmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,98 @@ public function testImmediateAbort()
$realm->handleAbortMessage(new \Thruway\Event\MessageEvent($session, $abortMessage));
}

/**
* Ensure the roles in welcome messages obey the RFC spec.
*
* @see https://github.com/wamp-proto/wamp-proto/blob/master/rfc/draft-oberstet-hybi-crossbar-wamp.txt#L1685
*/
public function testWelcomeMessageRfcRoles()
{
$realm = new \Thruway\Realm("realm1");

/** @var \Thruway\Session $session */
$session = $this->getMockBuilder('\Thruway\Session')
->disableOriginalConstructor()
->setMethods(null)
->getMock();

$session->dispatcher = new \Thruway\Event\EventDispatcher();

$expected = (object) [
'broker' => (object) [
'features' => (object) [
'subscriber_blackwhite_listing' => true,
'publisher_exclusion' => true,
'subscriber_metaevents' => true,
]
],
'dealer' => (object) [
'features' => (object) [
'caller_identification' => true,
'progressive_call_results' => true,
]
]
];

$helloMessage = new \Thruway\Message\HelloMessage('realm1', (object) [
'roles' => (object) [
'subscriber' => (object) [
'features' => (object) [
'publisher_identification' => true,
'pattern_based_subscription' => true,
'subscription_revocation' => true,
'payload_transparency' => true,
'payload_encryption_cryptobox' => true,
]
],
'publisher' => (object) [
'features' => (object) [
'publisher_identification' => true,
'subscriber_blackwhite_listing' => true,
'publisher_exclusion' => true,
'payload_transparency' => true,
'x_acknowledged_event_delivery' => true,
'payload_encryption_cryptobox' => true,
]
],
'caller' => (object) [
'features' => (object) [
'caller_identification' => true,
'progressive_call_results' => true,
'payload_transparency' => true,
'payload_encryption_cryptobox' => true,
]
],
'callee' => (object) [
'features' => (object) [
'caller_identification' => true,
'pattern_based_registration' => true,
'shared_registration' => true,
'progressive_call_results' => true,
'registration_revocation' => true,
'payload_transparency' => true,
'payload_encryption_cryptobox' => true,
]
]
]
]);

$session->setHelloMessage($helloMessage);

$welcomeMessage = new \Thruway\Message\WelcomeMessage(
$session->getSessionId(),
$helloMessage->getDetails()
);

$welcomeMessage->addFeatures('broker', $expected->broker->features);
$welcomeMessage->addFeatures('dealer', $expected->dealer->features);

$realm->handleSendWelcomeMessage(new \Thruway\Event\MessageEvent($session, $welcomeMessage));

$this->assertNotEmpty($welcomeMessage->getDetails()->roles);
$this->assertEquals($expected, $welcomeMessage->getDetails()->roles);
}

// This should be irrelevant when dispatcher is complete
// because the dealer shouldn't even be attached yet
// public function testCallBeforeWelcome() {
Expand Down

0 comments on commit d576664

Please sign in to comment.