From c15e275584cf9d8b5a3f93bef5ce7d0495ed9f3f Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 11 Feb 2016 18:33:50 -0500 Subject: [PATCH] Deleted deprecated tests from PSR-7 refactor GuzzleTest -> no longer using Guzzle HTTP interfaces WebSocket tests have been moved to RFC library --- tests/integration/GuzzleTest.php | 53 -- tests/unit/WebSocket/Version/Hixie76Test.php | 103 ---- tests/unit/WebSocket/Version/HyBi10Test.php | 67 --- .../WebSocket/Version/RFC6455/FrameTest.php | 543 ------------------ .../Version/RFC6455/HandshakeVerifierTest.php | 170 ------ .../WebSocket/Version/RFC6455/MessageTest.php | 63 -- tests/unit/WebSocket/Version/RFC6455Test.php | 151 ----- tests/unit/WebSocket/VersionManagerTest.php | 91 --- tests/unit/WebSocket/WsServerTest.php | 51 -- 9 files changed, 1292 deletions(-) delete mode 100644 tests/integration/GuzzleTest.php delete mode 100644 tests/unit/WebSocket/Version/Hixie76Test.php delete mode 100644 tests/unit/WebSocket/Version/HyBi10Test.php delete mode 100644 tests/unit/WebSocket/Version/RFC6455/FrameTest.php delete mode 100644 tests/unit/WebSocket/Version/RFC6455/HandshakeVerifierTest.php delete mode 100644 tests/unit/WebSocket/Version/RFC6455/MessageTest.php delete mode 100644 tests/unit/WebSocket/Version/RFC6455Test.php delete mode 100644 tests/unit/WebSocket/VersionManagerTest.php delete mode 100644 tests/unit/WebSocket/WsServerTest.php diff --git a/tests/integration/GuzzleTest.php b/tests/integration/GuzzleTest.php deleted file mode 100644 index 5e4d8aa..0000000 --- a/tests/integration/GuzzleTest.php +++ /dev/null @@ -1,53 +0,0 @@ - 'websocket' - , 'Connection' => 'Upgrade' - , 'Host' => 'localhost:8080' - , 'Origin' => 'chrome://newtab' - , 'Sec-WebSocket-Protocol' => 'one, two, three' - , 'Sec-WebSocket-Key' => '9bnXNp3ae6FbFFRtPdiPXA==' - , 'Sec-WebSocket-Version' => '13' - ); - - public function setUp() { - $this->_request = new Request('GET', 'http://localhost', $this->_headers); - } - - public function testGetHeaderString() { - $this->assertEquals('Upgrade', (string)$this->_request->getHeader('connection')); - $this->assertEquals('9bnXNp3ae6FbFFRtPdiPXA==', (string)$this->_request->getHeader('Sec-Websocket-Key')); - } - - public function testGetHeaderInteger() { - $this->assertSame('13', (string)$this->_request->getHeader('Sec-Websocket-Version')); - $this->assertSame(13, (int)(string)$this->_request->getHeader('Sec-WebSocket-Version')); - } - - public function testGetHeaderObject() { - $this->assertInstanceOf('Guzzle\Http\Message\Header', $this->_request->getHeader('Origin')); - $this->assertNull($this->_request->getHeader('Non-existant-header')); - } - - public function testHeaderObjectNormalizeValues() { - $expected = 1 + substr_count($this->_headers['Sec-WebSocket-Protocol'], ','); - $protocols = $this->_request->getHeader('Sec-WebSocket-Protocol')->normalize(); - $count = 0; - - foreach ($protocols as $protocol) { - $count++; - } - - $this->assertEquals($expected, $count); - $this->assertEquals($expected, count($protocols)); - } - - public function testRequestFactoryCreateSignature() { - $ref = new \ReflectionMethod('Guzzle\Http\Message\RequestFactory', 'create'); - $this->assertEquals(2, $ref->getNumberOfRequiredParameters()); - } -} diff --git a/tests/unit/WebSocket/Version/Hixie76Test.php b/tests/unit/WebSocket/Version/Hixie76Test.php deleted file mode 100644 index 75998aa..0000000 --- a/tests/unit/WebSocket/Version/Hixie76Test.php +++ /dev/null @@ -1,103 +0,0 @@ -_version = new Hixie76; - } - - public function testClassImplementsVersionInterface() { - $constraint = $this->isInstanceOf('\\Ratchet\\WebSocket\\Version\\VersionInterface'); - $this->assertThat($this->_version, $constraint); - } - - /** - * @dataProvider keyProvider - */ - public function testKeySigningForHandshake($accept, $key) { - $this->assertEquals($accept, $this->_version->generateKeyNumber($key)); - } - - public static function keyProvider() { - return array( - array(179922739, '17 9 G`ZD9 2 2b 7X 3 /r90') - , array(906585445, '3e6b263 4 17 80') - , array(0, '3e6b26341780') - ); - } - - public function headerProvider() { - $key1 = base64_decode('QTN+ICszNiA2IDJvICBWOG4gNyAgc08yODhZ'); - $key2 = base64_decode('TzEyICAgeVsgIFFSNDUgM1IgLiAyOFggNC00dn4z'); - - $headers = "GET / HTTP/1.1"; - $headers .= "Upgrade: WebSocket{$this->_crlf}"; - $headers .= "Connection: Upgrade{$this->_crlf}"; - $headers .= "Host: socketo.me{$this->_crlf}"; - $headers .= "Origin: http://fiddle.jshell.net{$this->_crlf}"; - $headers .= "Sec-WebSocket-Key1:17 Z4< F94 N3 7P41 7{$this->_crlf}"; - $headers .= "Sec-WebSocket-Key2:1 23C3:,2% 1-29 4 f0{$this->_crlf}"; - $headers .= "(Key3):70:00:EE:6E:33:20:90:69{$this->_crlf}"; - $headers .= $this->_crlf; - - return $headers; - } - - public function testNoUpgradeBeforeBody() { - $headers = $this->headerProvider(); - - $mockConn = $this->getMock('\Ratchet\ConnectionInterface'); - $mockApp = $this->getMock('\Ratchet\MessageComponentInterface'); - - $server = new HttpServer(new WsServer($mockApp)); - $server->onOpen($mockConn); - $mockApp->expects($this->exactly(0))->method('onOpen'); - $server->onMessage($mockConn, $headers); - } - - public function testTcpFragmentedUpgrade() { - $headers = $this->headerProvider(); - $body = base64_decode($this->_body); - - $mockConn = $this->getMock('\Ratchet\ConnectionInterface'); - $mockApp = $this->getMock('\Ratchet\MessageComponentInterface'); - - $server = new HttpServer(new WsServer($mockApp)); - $server->onOpen($mockConn); - $server->onMessage($mockConn, $headers); - - $mockApp->expects($this->once())->method('onOpen'); - $server->onMessage($mockConn, $body . $this->_crlf . $this->_crlf); - } - - public function testTcpFragmentedBodyUpgrade() { - $headers = $this->headerProvider(); - $body = base64_decode($this->_body); - $body1 = substr($body, 0, 4); - $body2 = substr($body, 4); - - $mockConn = $this->getMock('\Ratchet\ConnectionInterface'); - $mockApp = $this->getMock('\Ratchet\MessageComponentInterface'); - - $server = new HttpServer(new WsServer($mockApp)); - $server->onOpen($mockConn); - $server->onMessage($mockConn, $headers); - - $mockApp->expects($this->once())->method('onOpen'); - - $server->onMessage($mockConn, $body1); - $server->onMessage($mockConn, $body2); - $server->onMessage($mockConn, $this->_crlf . $this->_crlf); - } -} diff --git a/tests/unit/WebSocket/Version/HyBi10Test.php b/tests/unit/WebSocket/Version/HyBi10Test.php deleted file mode 100644 index 1d9e8a9..0000000 --- a/tests/unit/WebSocket/Version/HyBi10Test.php +++ /dev/null @@ -1,67 +0,0 @@ -_version = new HyBi10(); - } - - /** - * Is this useful? - */ - public function testClassImplementsVersionInterface() { - $constraint = $this->isInstanceOf('\\Ratchet\\WebSocket\\Version\\VersionInterface'); - $this->assertThat($this->_version, $constraint); - } - - /** - * @dataProvider HandshakeProvider - */ - public function testKeySigningForHandshake($key, $accept) { - $this->assertEquals($accept, $this->_version->sign($key)); - } - - public static function HandshakeProvider() { - return array( - array('x3JJHMbDL1EzLkh9GBhXDw==', 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=') - , array('dGhlIHNhbXBsZSBub25jZQ==', 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=') - ); - } - - /** - * @dataProvider UnframeMessageProvider - */ - public function testUnframeMessage($message, $framed) { -// $decoded = $this->_version->unframe(base64_decode($framed)); - $frame = new Frame; - $frame->addBuffer(base64_decode($framed)); - - $this->assertEquals($message, $frame->getPayload()); - } - - public static function UnframeMessageProvider() { - return array( - array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7') - , array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg') - , array('ಠ_ಠ', 'gYfnSpu5B/g75gf4Ow==') - , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') - ); - } - - public function testUnframeMatchesPreFraming() { - $string = 'Hello World!'; - $framed = $this->_version->newFrame($string)->getContents(); - - $frame = new Frame; - $frame->addBuffer($framed); - - $this->assertEquals($string, $frame->getPayload()); - } -} diff --git a/tests/unit/WebSocket/Version/RFC6455/FrameTest.php b/tests/unit/WebSocket/Version/RFC6455/FrameTest.php deleted file mode 100644 index eff9513..0000000 --- a/tests/unit/WebSocket/Version/RFC6455/FrameTest.php +++ /dev/null @@ -1,543 +0,0 @@ -_frame = new Frame; - } - - /** - * Encode the fake binary string to send over the wire - * @param string of 1's and 0's - * @return string - */ - public static function encode($in) { - if (strlen($in) > 8) { - $out = ''; - - while (strlen($in) >= 8) { - $out .= static::encode(substr($in, 0, 8)); - $in = substr($in, 8); - } - - return $out; - } - - return chr(bindec($in)); - } - - /** - * This is a data provider - * @param string The UTF8 message - * @param string The WebSocket framed message, then base64_encoded - */ - public static function UnframeMessageProvider() { - return array( - array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7') - , array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg') - , array('ಠ_ಠ', 'gYfnSpu5B/g75gf4Ow==') - , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') - ); - } - - public static function underflowProvider() { - return array( - array('isFinal', '') - , array('getRsv1', '') - , array('getRsv2', '') - , array('getRsv3', '') - , array('getOpcode', '') - , array('isMasked', '10000001') - , array('getPayloadLength', '10000001') - , array('getPayloadLength', '1000000111111110') - , array('getMaskingKey', '1000000110000111') - , array('getPayload', '100000011000000100011100101010101001100111110100') - ); - } - - /** - * @dataProvider underflowProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isFinal - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv1 - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv2 - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv3 - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getOpcode - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isMasked - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getMaskingKey - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayload - */ - public function testUnderflowExceptionFromAllTheMethodsMimickingBuffering($method, $bin) { - $this->setExpectedException('\UnderflowException'); - - if (!empty($bin)) { - $this->_frame->addBuffer(static::encode($bin)); - } - - call_user_func(array($this->_frame, $method)); - } - - /** - * A data provider for testing the first byte of a WebSocket frame - * @param bool Given, is the byte indicate this is the final frame - * @param int Given, what is the expected opcode - * @param string of 0|1 Each character represents a bit in the byte - */ - public static function firstByteProvider() { - return array( - array(false, false, false, true, 8, '00011000') - , array(true, false, true, false, 10, '10101010') - , array(false, false, false, false, 15, '00001111') - , array(true, false, false, false, 1, '10000001') - , array(true, true, true, true, 15, '11111111') - , array(true, true, false, false, 7, '11000111') - ); - } - - /** - * @dataProvider firstByteProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isFinal - */ - public function testFinCodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { - $this->_frame->addBuffer(static::encode($bin)); - $this->assertEquals($fin, $this->_frame->isFinal()); - } - - /** - * @dataProvider firstByteProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv1 - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv2 - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv3 - */ - public function testGetRsvFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { - $this->_frame->addBuffer(static::encode($bin)); - - $this->assertEquals($rsv1, $this->_frame->getRsv1()); - $this->assertEquals($rsv2, $this->_frame->getRsv2()); - $this->assertEquals($rsv3, $this->_frame->getRsv3()); - } - - /** - * @dataProvider firstByteProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getOpcode - */ - public function testOpcodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) { - $this->_frame->addBuffer(static::encode($bin)); - $this->assertEquals($opcode, $this->_frame->getOpcode()); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isFinal - */ - public function testFinCodeFromFullMessage($msg, $encoded) { - $this->_frame->addBuffer(base64_decode($encoded)); - $this->assertTrue($this->_frame->isFinal()); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getOpcode - */ - public function testOpcodeFromFullMessage($msg, $encoded) { - $this->_frame->addBuffer(base64_decode($encoded)); - $this->assertEquals(1, $this->_frame->getOpcode()); - } - - public static function payloadLengthDescriptionProvider() { - return array( - array(7, '01110101') - , array(7, '01111101') - , array(23, '01111110') - , array(71, '01111111') - , array(7, '00000000') // Should this throw an exception? Can a payload be empty? - , array(7, '00000001') - ); - } - - /** - * @dataProvider payloadLengthDescriptionProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::addBuffer - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getFirstPayloadVal - */ - public function testFirstPayloadDesignationValue($bits, $bin) { - $this->_frame->addBuffer(static::encode($this->_firstByteFinText)); - $this->_frame->addBuffer(static::encode($bin)); - - $ref = new \ReflectionClass($this->_frame); - $cb = $ref->getMethod('getFirstPayloadVal'); - $cb->setAccessible(true); - - $this->assertEquals(bindec($bin), $cb->invoke($this->_frame)); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getFirstPayloadVal - */ - public function testFirstPayloadValUnderflow() { - $ref = new \ReflectionClass($this->_frame); - $cb = $ref->getMethod('getFirstPayloadVal'); - $cb->setAccessible(true); - - $this->setExpectedException('UnderflowException'); - $cb->invoke($this->_frame); - } - - /** - * @dataProvider payloadLengthDescriptionProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getNumPayloadBits - */ - public function testDetermineHowManyBitsAreUsedToDescribePayload($expected_bits, $bin) { - $this->_frame->addBuffer(static::encode($this->_firstByteFinText)); - $this->_frame->addBuffer(static::encode($bin)); - - $ref = new \ReflectionClass($this->_frame); - $cb = $ref->getMethod('getNumPayloadBits'); - $cb->setAccessible(true); - - $this->assertEquals($expected_bits, $cb->invoke($this->_frame)); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getNumPayloadBits - */ - public function testgetNumPayloadBitsUnderflow() { - $ref = new \ReflectionClass($this->_frame); - $cb = $ref->getMethod('getNumPayloadBits'); - $cb->setAccessible(true); - - $this->setExpectedException('UnderflowException'); - $cb->invoke($this->_frame); - } - - public function secondByteProvider() { - return array( - array(true, 1, '10000001') - , array(false, 1, '00000001') - , array(true, 125, $this->_secondByteMaskedSPL) - ); - } - - /** - * @dataProvider secondByteProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isMasked - */ - public function testIsMaskedReturnsExpectedValue($masked, $payload_length, $bin) { - $this->_frame->addBuffer(static::encode($this->_firstByteFinText)); - $this->_frame->addBuffer(static::encode($bin)); - - $this->assertEquals($masked, $this->_frame->isMasked()); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::isMasked - */ - public function testIsMaskedFromFullMessage($msg, $encoded) { - $this->_frame->addBuffer(base64_decode($encoded)); - $this->assertTrue($this->_frame->isMasked()); - } - - /** - * @dataProvider secondByteProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - */ - public function testGetPayloadLengthWhenOnlyFirstFrameIsUsed($masked, $payload_length, $bin) { - $this->_frame->addBuffer(static::encode($this->_firstByteFinText)); - $this->_frame->addBuffer(static::encode($bin)); - - $this->assertEquals($payload_length, $this->_frame->getPayloadLength()); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - * @todo Not yet testing when second additional payload length descriptor - */ - public function testGetPayloadLengthFromFullMessage($msg, $encoded) { - $this->_frame->addBuffer(base64_decode($encoded)); - $this->assertEquals(strlen($msg), $this->_frame->getPayloadLength()); - } - - public function maskingKeyProvider() { - $frame = new Frame; - - return array( - array($frame->generateMaskingKey()) - , array($frame->generateMaskingKey()) - , array($frame->generateMaskingKey()) - ); - } - - /** - * @dataProvider maskingKeyProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getMaskingKey - * @todo I I wrote the dataProvider incorrectly, skipping for now - */ - public function testGetMaskingKey($mask) { - $this->_frame->addBuffer(static::encode($this->_firstByteFinText)); - $this->_frame->addBuffer(static::encode($this->_secondByteMaskedSPL)); - $this->_frame->addBuffer($mask); - - $this->assertEquals($mask, $this->_frame->getMaskingKey()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getMaskingKey - */ - public function testGetMaskingKeyOnUnmaskedPayload() { - $frame = new Frame('Hello World!'); - - $this->assertEquals('', $frame->getMaskingKey()); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayload - * @todo Move this test to bottom as it requires all methods of the class - */ - public function testUnframeFullMessage($unframed, $base_framed) { - $this->_frame->addBuffer(base64_decode($base_framed)); - $this->assertEquals($unframed, $this->_frame->getPayload()); - } - - public static function messageFragmentProvider() { - return array( - array(false, '', '', '', '', '') - ); - } - - /** - * @dataProvider UnframeMessageProvider - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayload - */ - public function testCheckPiecingTogetherMessage($msg, $encoded) { - $framed = base64_decode($encoded); - for ($i = 0, $len = strlen($framed);$i < $len; $i++) { - $this->_frame->addBuffer(substr($framed, $i, 1)); - } - - $this->assertEquals($msg, $this->_frame->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::__construct - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayload - */ - public function testLongCreate() { - $len = 65525; - $pl = $this->generateRandomString($len); - - $frame = new Frame($pl, true, Frame::OP_PING); - - $this->assertTrue($frame->isFinal()); - $this->assertEquals(Frame::OP_PING, $frame->getOpcode()); - $this->assertFalse($frame->isMasked()); - $this->assertEquals($len, $frame->getPayloadLength()); - $this->assertEquals($pl, $frame->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::__construct - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - */ - public function testReallyLongCreate() { - $len = 65575; - - $frame = new Frame($this->generateRandomString($len)); - - $this->assertEquals($len, $frame->getPayloadLength()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::__construct - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::extractOverflow - */ - public function testExtractOverflow() { - $string1 = $this->generateRandomString(); - $frame1 = new Frame($string1); - - $string2 = $this->generateRandomString(); - $frame2 = new Frame($string2); - - $cat = new Frame; - $cat->addBuffer($frame1->getContents() . $frame2->getContents()); - - $this->assertEquals($frame1->getContents(), $cat->getContents()); - $this->assertEquals($string1, $cat->getPayload()); - - $uncat = new Frame; - $uncat->addBuffer($cat->extractOverflow()); - - $this->assertEquals($string1, $cat->getPayload()); - $this->assertEquals($string2, $uncat->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::extractOverflow - */ - public function testEmptyExtractOverflow() { - $string = $this->generateRandomString(); - $frame = new Frame($string); - - $this->assertEquals($string, $frame->getPayload()); - $this->assertEquals('', $frame->extractOverflow()); - $this->assertEquals($string, $frame->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getContents - */ - public function testGetContents() { - $msg = 'The quick brown fox jumps over the lazy dog.'; - - $frame1 = new Frame($msg); - $frame2 = new Frame($msg); - $frame2->maskPayload(); - - $this->assertNotEquals($frame1->getContents(), $frame2->getContents()); - $this->assertEquals(strlen($frame1->getContents()) + 4, strlen($frame2->getContents())); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::maskPayload - */ - public function testMasking() { - $msg = 'The quick brown fox jumps over the lazy dog.'; - $frame = new Frame($msg); - $frame->maskPayload(); - - $this->assertTrue($frame->isMasked()); - $this->assertEquals($msg, $frame->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::unMaskPayload - */ - public function testUnMaskPayload() { - $string = $this->generateRandomString(); - $frame = new Frame($string); - $frame->maskPayload()->unMaskPayload(); - - $this->assertFalse($frame->isMasked()); - $this->assertEquals($string, $frame->getPayload()); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::generateMaskingKey - */ - public function testGenerateMaskingKey() { - $dupe = false; - $done = array(); - - for ($i = 0; $i < 10; $i++) { - $new = $this->_frame->generateMaskingKey(); - - if (in_array($new, $done)) { - $dupe = true; - } - - $done[] = $new; - } - - $this->assertEquals(4, strlen($new)); - $this->assertFalse($dupe); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::maskPayload - */ - public function testGivenMaskIsValid() { - $this->setExpectedException('InvalidArgumentException'); - $this->_frame->maskPayload('hello world'); - } - - /** - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::maskPayload - */ - public function testGivenMaskIsValidAscii() { - if (!extension_loaded('mbstring')) { - return $this->markTestSkipped("mbstring required for this test"); - } - - $this->setExpectedException('OutOfBoundsException'); - $this->_frame->maskPayload('x✖'); - } - - protected function generateRandomString($length = 10, $addSpaces = true, $addNumbers = true) { - $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$%&/()=[]{}'; // ยง - - $useChars = array(); - for($i = 0; $i < $length; $i++) { - $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)]; - } - - if($addSpaces === true) { - array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' '); - } - - if($addNumbers === true) { - array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9)); - } - - shuffle($useChars); - - $randomString = trim(implode('', $useChars)); - $randomString = substr($randomString, 0, $length); - - return $randomString; - } - - /** - * There was a frame boundary issue when the first 3 bytes of a frame with a payload greater than - * 126 was added to the frame buffer and then Frame::getPayloadLength was called. It would cause the frame - * to set the payload length to 126 and then not recalculate it once the full length information was available. - * - * This is fixed by setting the defPayLen back to -1 before the underflow exception is thrown. - * - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength - * @covers Ratchet\WebSocket\Version\RFC6455\Frame::extractOverflow - */ - public function testFrameDeliveredOneByteAtATime() { - $startHeader = "\x01\x7e\x01\x00"; // header for a text frame of 256 - non-final - $framePayload = str_repeat("*", 256); - $rawOverflow = "xyz"; - $rawFrame = $startHeader . $framePayload . $rawOverflow; - - $frame = new Frame(); - $payloadLen = 256; - - for ($i = 0; $i < strlen($rawFrame); $i++) { - $frame->addBuffer($rawFrame[$i]); - - try { - // payloadLen will - $payloadLen = $frame->getPayloadLength(); - } catch (\UnderflowException $e) { - if ($i > 2) { // we should get an underflow on 0,1,2 - $this->fail("Underflow exception when the frame length should be available"); - } - } - - if ($payloadLen !== 256) { - $this->fail("Payload length of " . $payloadLen . " should have been 256."); - } - } - - // make sure the overflow is good - $this->assertEquals($rawOverflow, $frame->extractOverflow()); - } -} diff --git a/tests/unit/WebSocket/Version/RFC6455/HandshakeVerifierTest.php b/tests/unit/WebSocket/Version/RFC6455/HandshakeVerifierTest.php deleted file mode 100644 index 6761c32..0000000 --- a/tests/unit/WebSocket/Version/RFC6455/HandshakeVerifierTest.php +++ /dev/null @@ -1,170 +0,0 @@ -_v = new HandshakeVerifier; - } - - public static function methodProvider() { - return array( - array(true, 'GET') - , array(true, 'get') - , array(true, 'Get') - , array(false, 'POST') - , array(false, 'DELETE') - , array(false, 'PUT') - , array(false, 'PATCH') - ); - } - - /** - * @dataProvider methodProvider - */ - public function testMethodMustBeGet($result, $in) { - $this->assertEquals($result, $this->_v->verifyMethod($in)); - } - - public static function httpVersionProvider() { - return array( - array(true, 1.1) - , array(true, '1.1') - , array(true, 1.2) - , array(true, '1.2') - , array(true, 2) - , array(true, '2') - , array(true, '2.0') - , array(false, '1.0') - , array(false, 1) - , array(false, '0.9') - , array(false, '') - , array(false, 'hello') - ); - } - - /** - * @dataProvider httpVersionProvider - */ - public function testHttpVersionIsAtLeast1Point1($expected, $in) { - $this->assertEquals($expected, $this->_v->verifyHTTPVersion($in)); - } - - public static function uRIProvider() { - return array( - array(true, '/chat') - , array(true, '/hello/world?key=val') - , array(false, '/chat#bad') - , array(false, 'nope') - , array(false, '/ ಠ_ಠ ') - , array(false, '/✖') - ); - } - - /** - * @dataProvider URIProvider - */ - public function testRequestUri($expected, $in) { - $this->assertEquals($expected, $this->_v->verifyRequestURI($in)); - } - - public static function hostProvider() { - return array( - array(true, 'server.example.com') - , array(false, null) - ); - } - - /** - * @dataProvider HostProvider - */ - public function testVerifyHostIsSet($expected, $in) { - $this->assertEquals($expected, $this->_v->verifyHost($in)); - } - - public static function upgradeProvider() { - return array( - array(true, 'websocket') - , array(true, 'Websocket') - , array(true, 'webSocket') - , array(false, null) - , array(false, '') - ); - } - - /** - * @dataProvider upgradeProvider - */ - public function testVerifyUpgradeIsWebSocket($expected, $val) { - $this->assertEquals($expected, $this->_v->verifyUpgradeRequest($val)); - } - - public static function connectionProvider() { - return array( - array(true, 'Upgrade') - , array(true, 'upgrade') - , array(true, 'keep-alive, Upgrade') - , array(true, 'Upgrade, keep-alive') - , array(true, 'keep-alive, Upgrade, something') - , array(false, '') - , array(false, null) - ); - } - - /** - * @dataProvider connectionProvider - */ - public function testConnectionHeaderVerification($expected, $val) { - $this->assertEquals($expected, $this->_v->verifyConnection($val)); - } - - public static function keyProvider() { - return array( - array(true, 'hkfa1L7uwN6DCo4IS3iWAw==') - , array(true, '765vVoQpKSGJwPzJIMM2GA==') - , array(true, 'AQIDBAUGBwgJCgsMDQ4PEC==') - , array(true, 'axa2B/Yz2CdpfQAY2Q5P7w==') - , array(false, 0) - , array(false, 'Hello World') - , array(false, '1234567890123456') - , array(false, '123456789012345678901234') - , array(true, base64_encode('UTF8allthngs+✓')) - , array(true, 'dGhlIHNhbXBsZSBub25jZQ==') - ); - } - - /** - * @dataProvider keyProvider - */ - public function testKeyIsBase64Encoded16BitNonce($expected, $val) { - $this->assertEquals($expected, $this->_v->verifyKey($val)); - } - - public static function versionProvider() { - return array( - array(true, 13) - , array(true, '13') - , array(false, 12) - , array(false, 14) - , array(false, '14') - , array(false, 'hi') - , array(false, '') - , array(false, null) - ); - } - - /** - * @dataProvider versionProvider - */ - public function testVersionEquals13($expected, $in) { - $this->assertEquals($expected, $this->_v->verifyVersion($in)); - } -} diff --git a/tests/unit/WebSocket/Version/RFC6455/MessageTest.php b/tests/unit/WebSocket/Version/RFC6455/MessageTest.php deleted file mode 100644 index b2d21d2..0000000 --- a/tests/unit/WebSocket/Version/RFC6455/MessageTest.php +++ /dev/null @@ -1,63 +0,0 @@ -message = new Message; - } - - public function testNoFrames() { - $this->assertFalse($this->message->isCoalesced()); - } - - public function testNoFramesOpCode() { - $this->setExpectedException('UnderflowException'); - $this->message->getOpCode(); - } - - public function testFragmentationPayload() { - $a = 'Hello '; - $b = 'World!'; - - $f1 = new Frame($a, false); - $f2 = new Frame($b, true, Frame::OP_CONTINUE); - - $this->message->addFrame($f1)->addFrame($f2); - - $this->assertEquals(strlen($a . $b), $this->message->getPayloadLength()); - $this->assertEquals($a . $b, $this->message->getPayload()); - } - - public function testUnbufferedFragment() { - $this->message->addFrame(new Frame('The quick brow', false)); - - $this->setExpectedException('UnderflowException'); - $this->message->getPayload(); - } - - public function testGetOpCode() { - $this->message - ->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT)) - ->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE)) - ->addFrame(new Frame('er the lazy dog', true, Frame::OP_CONTINUE)) - ; - - $this->assertEquals(Frame::OP_TEXT, $this->message->getOpCode()); - } - - public function testGetUnBufferedPayloadLength() { - $this->message - ->addFrame(new Frame('The quick brow', false, Frame::OP_TEXT)) - ->addFrame(new Frame('n fox jumps ov', false, Frame::OP_CONTINUE)) - ; - - $this->assertEquals(28, $this->message->getPayloadLength()); - } -} diff --git a/tests/unit/WebSocket/Version/RFC6455Test.php b/tests/unit/WebSocket/Version/RFC6455Test.php deleted file mode 100644 index 86e5631..0000000 --- a/tests/unit/WebSocket/Version/RFC6455Test.php +++ /dev/null @@ -1,151 +0,0 @@ -version = new RFC6455; - } - - /** - * @dataProvider handshakeProvider - */ - public function testKeySigningForHandshake($key, $accept) { - $this->assertEquals($accept, $this->version->sign($key)); - } - - public static function handshakeProvider() { - return array( - array('x3JJHMbDL1EzLkh9GBhXDw==', 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=') - , array('dGhlIHNhbXBsZSBub25jZQ==', 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=') - ); - } - - /** - * @dataProvider UnframeMessageProvider - */ - public function testUnframeMessage($message, $framed) { - $frame = new Frame; - $frame->addBuffer(base64_decode($framed)); - - $this->assertEquals($message, $frame->getPayload()); - } - - public static function UnframeMessageProvider() { - return array( - array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7') - , array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg') - , array('ಠ_ಠ', 'gYfnSpu5B/g75gf4Ow==') - , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') - ); - } - - public function testUnframeMatchesPreFraming() { - $string = 'Hello World!'; - $framed = $this->version->newFrame($string)->getContents(); - - $frame = new Frame; - $frame->addBuffer($framed); - - $this->assertEquals($string, $frame->getPayload()); - } - - public static $good_rest = 'GET /chat HTTP/1.1'; - - public static $good_header = array( - 'Host' => 'server.example.com' - , 'Upgrade' => 'websocket' - , 'Connection' => 'Upgrade' - , 'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==' - , 'Origin' => 'http://example.com' - , 'Sec-WebSocket-Protocol' => 'chat, superchat' - , 'Sec-WebSocket-Version' => 13 - ); - - public function caseVariantProvider() { - return array( - array('Sec-Websocket-Version') - , array('sec-websocket-version') - , array('SEC-WEBSOCKET-VERSION') - , array('sEC-wEBsOCKET-vERSION') - ); - } - - /** - * @dataProvider caseVariantProvider - */ - public function testIsProtocolWithCaseInsensitivity($headerName) { - $header = static::$good_header; - unset($header['Sec-WebSocket-Version']); - $header[$headerName] = 13; - - $this->assertTrue($this->version->isProtocol(new EntityEnclosingRequest('get', '/', $header))); - } - - /** - * A helper function to try and quickly put together a valid WebSocket HTTP handshake - * but optionally replace a piece to an invalid value for failure testing - */ - public static function getAndSpliceHeader($key = null, $val = null) { - $headers = static::$good_header; - - if (null !== $key && null !== $val) { - $headers[$key] = $val; - } - - $header = ''; - foreach ($headers as $key => $val) { - if (!empty($key)) { - $header .= "{$key}: "; - } - - $header .= "{$val}\r\n"; - } - $header .= "\r\n"; - - return $header; - } - - public static function headerHandshakeProvider() { - return array( - array(false, "GET /test HTTP/1.0\r\n" . static::getAndSpliceHeader()) - , array(true, static::$good_rest . "\r\n" . static::getAndSpliceHeader()) - , array(false, "POST / HTTP:/1.1\r\n" . static::getAndSpliceHeader()) - , array(false, static::$good_rest . "\r\n" . static::getAndSpliceHeader('Upgrade', 'useless')) - , array(false, "GET /ಠ_ಠ HTTP/1.1\r\n" . static::getAndSpliceHeader()) - , array(true, static::$good_rest . "\r\n" . static::getAndSpliceHeader('Connection', 'Herp, Upgrade, Derp')) - ); - } - - /** - * @dataProvider headerHandshakeProvider - */ - public function testVariousHeadersToCheckHandshakeTolerance($pass, $header) { - $request = RequestFactory::getInstance()->fromMessage($header); - $response = $this->version->handshake($request); - - $this->assertInstanceOf('\\Guzzle\\Http\\Message\\Response', $response); - - if ($pass) { - $this->assertEquals(101, $response->getStatusCode()); - } else { - $this->assertGreaterThanOrEqual(400, $response->getStatusCode()); - } - } - - public function testNewMessage() { - $this->assertInstanceOf('\\Ratchet\\WebSocket\\Version\\RFC6455\\Message', $this->version->newMessage()); - } - - public function testNewFrame() { - $this->assertInstanceOf('\\Ratchet\\WebSocket\\Version\\RFC6455\\Frame', $this->version->newFrame()); - } -} diff --git a/tests/unit/WebSocket/VersionManagerTest.php b/tests/unit/WebSocket/VersionManagerTest.php deleted file mode 100644 index d9c55fe..0000000 --- a/tests/unit/WebSocket/VersionManagerTest.php +++ /dev/null @@ -1,91 +0,0 @@ -vm = new VersionManager; - } - - public function testFluentInterface() { - $rfc = new RFC6455; - - $this->assertSame($this->vm, $this->vm->enableVersion($rfc)); - $this->assertSame($this->vm, $this->vm->disableVersion(13)); - } - - public function testGetVersion() { - $rfc = new RFC6455; - $this->vm->enableVersion($rfc); - - $req = new EntityEnclosingRequest('get', '/', array( - 'Host' => 'socketo.me' - , 'Sec-WebSocket-Version' => 13 - )); - - $this->assertSame($rfc, $this->vm->getVersion($req)); - } - - public function testGetNopeVersionAndDisable() { - $req = new EntityEnclosingRequest('get', '/', array( - 'Host' => 'socketo.me' - , 'Sec-WebSocket-Version' => 13 - )); - - $this->setExpectedException('InvalidArgumentException'); - - $this->vm->getVersion($req); - } - - public function testYesIsVersionEnabled() { - $this->vm->enableVersion(new RFC6455); - - $this->assertTrue($this->vm->isVersionEnabled(new EntityEnclosingRequest('get', '/', array( - 'Host' => 'socketo.me' - , 'Sec-WebSocket-Version' => 13 - )))); - } - - public function testNoIsVersionEnabled() { - $this->assertFalse($this->vm->isVersionEnabled(new EntityEnclosingRequest('get', '/', array( - 'Host' => 'socketo.me' - , 'Sec-WebSocket-Version' => 9000 - )))); - } - - public function testGetSupportedVersionString() { - $v1 = new RFC6455; - $v2 = new HyBi10; - - $this->vm->enableVersion($v1); - $this->vm->enableVersion($v2); - - $string = $this->vm->getSupportedVersionString(); - $values = explode(',', $string); - - $this->assertContains($v1->getVersionNumber(), $values); - $this->assertContains($v2->getVersionNumber(), $values); - } - - public function testGetSupportedVersionAfterRemoval() { - $this->vm->enableVersion(new RFC6455); - $this->vm->enableVersion(new HyBi10); - $this->vm->enableVersion(new Hixie76); - - $this->vm->disableVersion(0); - - $values = explode(',', $this->vm->getSupportedVersionString()); - - $this->assertEquals(2, count($values)); - $this->assertFalse(array_search(0, $values)); - } -} diff --git a/tests/unit/WebSocket/WsServerTest.php b/tests/unit/WebSocket/WsServerTest.php deleted file mode 100644 index 3f4aa43..0000000 --- a/tests/unit/WebSocket/WsServerTest.php +++ /dev/null @@ -1,51 +0,0 @@ -comp = new MockComponent; - $this->serv = new WsServer($this->comp); - } - - public function testIsSubProtocolSupported() { - $this->comp->protocols = array('hello', 'world'); - - $this->assertTrue($this->serv->isSubProtocolSupported('hello')); - $this->assertFalse($this->serv->isSubProtocolSupported('nope')); - } - - public function protocolProvider() { - return array( - array('hello', array('hello', 'world'), array('hello', 'world')) - , array('', array('hello', 'world'), array('wamp')) - , array('', array(), null) - , array('wamp', array('hello', 'wamp', 'world'), array('herp', 'derp', 'wamp')) - , array('wamp', array('wamp'), array('wamp')) - ); - } - - /** - * @dataProvider protocolProvider - */ - public function testGetSubProtocolString($expected, $supported, $requested) { - $this->comp->protocols = $supported; - $req = (null === $requested ? $requested : new \ArrayIterator($requested)); - - $class = new \ReflectionClass('Ratchet\\WebSocket\\WsServer'); - $method = $class->getMethod('getSubProtocolString'); - $method->setAccessible(true); - - $this->assertSame($expected, $method->invokeArgs($this->serv, array($req))); - } -}