def test_payload_without_padding(self): """Use a payload which isn't padded.""" self.assertEqual(decode(b'\x00\x00\x00\x04ABCD'), b'ABCD') self.assertEqual( decode( b'\x00\x00\x01\x00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ), b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' )
def test_payload_with_padding(self): """Use a payload which is padded.""" self.assertEqual( decode(b'\x00\x00\x00\x04ABCDPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'), b'ABCD') self.assertEqual( decode( b'\x00\x00\x00\x05AB\x00CDPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP' ), b'AB\x00CD') self.assertEqual(decode(b'\x00\x00\x00\x01PP'), b'P')
def test_rountrip_with_padding(self): """Encode each test case with padding.""" for data in all_test_cases(): # Round up to the nearest 100 multiplier = 100 target_length = (int((len(data) + 4) / multiplier) + 1) * multiplier enc = encode(data, target_length) dec = decode(enc) self.assertEqual(data, dec)
def test_file_too_small(self): """Use a file shorter than the length header.""" with self.assertRaises(ValueError): decode(b'\x00\x00\x00\x01') with self.assertRaises(ValueError): decode(b'\x00\x00\x00\x04ABC') with self.assertRaises(ValueError): decode(b'\x00\x00\x00\x08ABCDPPP')
def test_invalid_length(self): """Use an invalid length header.""" with self.assertRaises(ValueError): decode(b'') with self.assertRaises(ValueError): decode(b'\xFF') with self.assertRaises(ValueError): decode(b'\x00\x00\x00')
def test_payload_with_weird_padding(self): """Use a payload which is padded with non-standard characters.""" self.assertEqual(decode(b'\x00\x00\x00\x04ABCDABCD'), b'ABCD') self.assertEqual(decode(b'\x00\x00\x00\x01P\x00\x00\x00'), b'P')
def test_empty_payload(self): """Use an empty file.""" self.assertEqual(decode(b'\x00\x00\x00\x00'), b'')
def test_roundtrip_without_padding(self): """Encode each test case without padding.""" for data in all_test_cases(): enc = encode(data, len(data) + 4) dec = decode(enc) self.assertEqual(data, dec)