'Test that the transport disconnects with an error when it receives
bad packets.'

def test_badPackets(self):

    def testBad(packet, error=transport.DISCONNECT_PROTOCOL_ERROR):
        self.packets = []
        self.proto.buf = packet
        self.assertIsNone(self.proto.getPacket())
        self.assertEqual(len(self.packets), 1)
        self.assertEqual(self.packets[0][0], transport.MSG_DISCONNECT)
        self.assertEqual(self.packets[0][1][3:4], chr(error))
    testBad(('\xff' * 8))
    testBad('\x00\x00\x00\x05\x00BCDE')
    oldEncryptions = self.proto.currentEncryptions
    self.proto.currentEncryptions = MockCipher()
    testBad('\x00\x00\x00\x08\x06AB123456', transport.DISCONNECT_MAC_ERROR)
    self.proto.currentEncryptions.decrypt = (lambda x: x[:(-1)])
    testBad('\x00\x00\x00\x08\x06BCDEFGHIJK')
    self.proto.currentEncryptions = oldEncryptions
    self.proto.incomingCompression = MockCompression()
    def stubDecompress(payload):
        raise Exception('bad    compression')
    self.proto.incomingCompression.decompress = stubDecompress
    testBad('\x00\x00\x00\x04\x00BCDE', transport.DISCONNECT_COMPRESSION_ERROR)
    self.flushLoggedErrors()
