Exemple #1
0
    def test_length_126_no_trailing(self):
        """
        Reading a header with a length of 126 that is missing the 2 trailing
        bytes should result in a WebSocketError.
        """
        data = StringIO('\x00\x7e')

        with self.assertRaises(exc.WebSocketError) as ctx:
            hybi.decode_header(data)

        self.assertEqual(
            'Unexpected EOF while decoding header',
            unicode(ctx.exception)
        )
Exemple #2
0
    def test_control_frame_size(self):
        """
        Page 37 of the spec specifies that control frames must not have a
        length of greater that 125.
        """
        byte = chr(hybi.FIN_MASK | hybi.OPCODE_CLOSE) + chr(0x7f)

        with self.assertRaises(exc.FrameTooLargeException) as ctx:
            hybi.decode_header(StringIO(byte))

        self.assertEqual(
            u"Control frame cannot be larger than 125 bytes: '\\x88\\x7f'",
            unicode(ctx.exception)
        )
Exemple #3
0
    def test_control_frame_fragmentation(self):
        """
        Page 36 of the spec specifies that control frames must not be
        fragmented
        """
        byte = chr(hybi.OPCODE_CLOSE)

        with self.assertRaises(exc.ProtocolError) as ctx:
            hybi.decode_header(StringIO(byte + 'a'))

        self.assertEqual(
            u"Received fragmented control frame: '\\x08a'",
            unicode(ctx.exception)
        )
Exemple #4
0
    def test_length_127_no_trailing(self):
        """
        A header with base length 127 should read another 8 bytes of trailing
        data.
        """
        for i in xrange(0, 8):
            data = StringIO('\x00\x7f' + ('\x00' * i))

            with self.assertRaises(exc.WebSocketError) as ctx:
                hybi.decode_header(data)

            self.assertEqual(
                'Unexpected EOF while decoding header',
                unicode(ctx.exception)
            )
Exemple #5
0
    def test_bad_length(self):
        """
        `exc.WebSocketError` must be raised if the number of bytes supplied < 2
        """
        for data in ('', '\x00'):
            # skip 2 bytes
            stream = StringIO(data)

            with self.assertRaises(exc.WebSocketError) as ctx:
                hybi.decode_header(stream)

            self.assertEqual(
                u'Unexpected EOF while decoding header',
                unicode(ctx.exception)
            )
Exemple #6
0
    def test_missing_mask(self):
        """
        Ensure that the 8 byte header is unsigned
        """
        # check the mask
        data = StringIO(
            chr(hybi.FIN_MASK | hybi.OPCODE_CLOSE) + chr(hybi.MASK_MASK) +
            'abc'  # this is the mask data
        )

        with self.assertRaises(exc.WebSocketError) as ctx:
            hybi.decode_header(data)

        self.assertEqual(
            'Unexpected EOF while decoding header',
            unicode(ctx.exception)
        )
Exemple #7
0
    def test_rsv_bits(self):
        """
        If the RSV bits are set then raise a `ProtocolError`
        """
        for rsv_mask in [0x40, 0x20, 0x10]:
            byte = chr(rsv_mask)

            header = hybi.decode_header(StringIO(byte + '\x00'))

            self.assertEqual(header.flags, rsv_mask)
Exemple #8
0
    def test_length_127_unsigned(self):
        """
        Ensure that the 8 byte header is unsigned
        """
        data = StringIO('\x00\x7f' + ('\xff' * 8))
        header = hybi.decode_header(data)

        self.assertFalse(header.fin)
        self.assertEqual(header.opcode, 0x00)
        self.assertEqual(header.mask, '')
        self.assertEqual(header.length, 0xffffffffffffffff)
        self.assertEqual(header.flags, 0)
Exemple #9
0
    def test_length_127_actual(self):
        """
        8 bytes should be decoded when reading a 127 base length header
        """
        data = StringIO('\x00\x7f' + ('\x00' * 8))

        header = hybi.decode_header(data)

        self.assertFalse(header.fin)
        self.assertEqual(header.opcode, 0x00)
        self.assertEqual(header.mask, '')
        self.assertEqual(header.length, 0)
        self.assertEqual(header.flags, 0)
Exemple #10
0
    def test_length_126_actual(self):
        """
        The correct length should be decoded when reading a 126 length header.
        """
        data = StringIO('\x00\x7e\x00\x00')

        header = hybi.decode_header(data)

        self.assertFalse(header.fin)
        self.assertEqual(header.opcode, 0x00)
        self.assertEqual(header.mask, '')
        self.assertEqual(header.length, 0)
        self.assertEqual(header.flags, 0)
Exemple #11
0
    def test_decode(self):
        """
        Basic sanity checks for decoding a header.
        """
        header = hybi.decode_header(StringIO(
            chr(hybi.FIN_MASK | hybi.OPCODE_CLOSE) + '\x00'
        ))

        # fin, opcode, mask, length
        self.assertTrue(header.fin)
        self.assertEqual(header.opcode, 0x08)
        self.assertEqual(header.mask, '')
        self.assertEqual(header.length, 0)
        self.assertEqual(header.flags, 0)

        # check the length
        header = hybi.decode_header(StringIO(
            chr(hybi.FIN_MASK | hybi.OPCODE_CLOSE) + '\x10'
        ))

        self.assertTrue(header.fin)
        self.assertEqual(header.opcode, 0x08)
        self.assertEqual(header.mask, '')
        # this is changed ..
        self.assertEqual(header.length, 16)
        self.assertEqual(header.flags, 0)

        # check the mask
        data = StringIO(
            chr(hybi.FIN_MASK | hybi.OPCODE_CLOSE) + chr(hybi.MASK_MASK) +
            'abcd'  # this is the mask data
        )
        header = hybi.decode_header(data)

        self.assertTrue(header.fin)
        self.assertEqual(header.opcode, 0x08)
        self.assertEqual(header.mask, 'abcd')
        self.assertEqual(header.length, 0)
        self.assertEqual(header.flags, 0)