Exemple #1
0
    def test_no_header_from_too_short_stream(self):
        """Fails to read FrameHeader from a too short byte stream"""
        # Arrange
        frame_id = b'PRIV'
        size = b'\x00\x00\x00\xFF'

        stream = BytesIO(frame_id + size)

        # Act
        header = FrameHeader.read(stream)

        # Assert
        self.assertFalse(bool(header))  # TODO: fix this with proper None
Exemple #2
0
    def test_reads_no_header_if_size_is_zero(self):
        """Fails to read FrameHeader if size is zero"""
        # Arrange
        frame_id = b'PRIV'
        size = b'\x00\x00\x00\x00'
        flags = b'\x00\x00'

        stream = BytesIO(frame_id + size + flags)

        # Act
        header = FrameHeader.read(stream)

        # Assert
        self.assertFalse(header)
Exemple #3
0
    def test_read_synchsafe_size(self):
        """Reads FrameHeader from a bytes stream"""
        # Arrange
        frame_id = b'PRIV'
        size = b'\x00\x00\x02\x01'  # would be 513 in plain binary
        flags = b'\x00\x00'

        expected_size = 257  # ... but is 257 in synchsafe world

        stream = BytesIO(frame_id + size + flags)

        # Act
        header = FrameHeader.read(stream, synchsafe_size=True)

        # Assert
        self.assertEqual(header.frame_size, expected_size)
Exemple #4
0
    def test_reads_header_from_stream(self):
        """Reads FrameHeader from a bytes stream"""
        # Arrange
        frame_id = b'PRIV'
        size = b'\x00\x00\x00\xFF'
        flags = b'\x00\x00'

        stream = BytesIO(frame_id + size + flags)

        # Act
        header = FrameHeader.read(stream)

        # Assert
        self.assertEqual(header.frame_size, 255)
        self.assertEqual(header.flags, FrameHeader.Flags(0))
        self.assertEqual(header.identifier, "PRIV")
Exemple #5
0
    def test_reads_header_if_size_bigger_than_zero(self):
        """Reads FrameHeader as long as size is present"""
        # Arrange
        frame_id = b'\x00\x00\x00\x00'
        frame_size = b'\x00\x00\x00\x01'
        flags = b'\x00\x00'

        stream = BytesIO(frame_id + frame_size + flags)

        # Act
        header = FrameHeader.read(stream)

        # Assert
        self.assertEqual(header.frame_size, 1)
        self.assertEqual(header.identifier, frame_id.decode("latin1"))
        self.assertEqual(header.flags, FrameHeader.Flags(0))
Exemple #6
0
    def test_reads_some_flags(self):
        """Reads some flags correctly"""
        # Arrange
        frame_id = b'PRIV'
        size = b'\x00\x00\x00\xFF'
        flags = 0b0000000011100000.to_bytes(2, "big")

        stream = BytesIO(frame_id + size + flags)

        # Act
        header = FrameHeader.read(stream)

        # Assert
        self.assertIn(FrameHeader.Flags.Compression, header.flags)
        self.assertIn(FrameHeader.Flags.Encryption, header.flags)
        self.assertIn(FrameHeader.Flags.GroupingIdentity, header.flags)