diff options
Diffstat (limited to 'Lib/test/test_binascii.py')
| -rw-r--r--[-rwxr-xr-x] | Lib/test/test_binascii.py | 160 |
1 files changed, 108 insertions, 52 deletions
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index a35ec05..1e9e888 100755..100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -3,14 +3,27 @@ from test import support import unittest import binascii +import array + +# Note: "*_hex" functions are aliases for "(un)hexlify" +b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', + 'hexlify', 'rlecode_hqx'] +a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', + 'unhexlify', 'rledecode_hqx'] +all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] + class BinASCIITest(unittest.TestCase): + type2test = bytes # Create binary test data - data = b"The quick brown fox jumps over the lazy dog.\r\n" + rawdata = b"The quick brown fox jumps over the lazy dog.\r\n" # Be slow so we don't depend on other modules - data += bytes(range(256)) - data += b"\r\nHello world.\n" + rawdata += bytes(range(256)) + rawdata += b"\r\nHello world.\n" + + def setUp(self): + self.data = self.type2test(self.rawdata) def test_exceptions(self): # Check module exceptions @@ -19,32 +32,47 @@ class BinASCIITest(unittest.TestCase): def test_functions(self): # Check presence of all functions - funcs = [] - for suffix in "base64", "hqx", "uu", "hex": - prefixes = ["a2b_", "b2a_"] - if suffix == "hqx": - prefixes.extend(["crc_", "rlecode_", "rledecode_"]) - for prefix in prefixes: - name = prefix + suffix - self.assertTrue(hasattr(getattr(binascii, name), '__call__')) - self.assertRaises(TypeError, getattr(binascii, name)) - for name in ("hexlify", "unhexlify"): + for name in all_functions: self.assertTrue(hasattr(getattr(binascii, name), '__call__')) self.assertRaises(TypeError, getattr(binascii, name)) + def test_returned_value(self): + # Limit to the minimum of all limits (b2a_uu) + MAX_ALL = 45 + raw = self.rawdata[:MAX_ALL] + for fa, fb in zip(a2b_functions, b2a_functions): + a2b = getattr(binascii, fa) + b2a = getattr(binascii, fb) + try: + a = b2a(self.type2test(raw)) + res = a2b(self.type2test(a)) + except Exception as err: + self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) + if fb == 'b2a_hqx': + # b2a_hqx returns a tuple + res, _ = res + self.assertEqual(res, raw, "{}/{} conversion: " + "{!r} != {!r}".format(fb, fa, res, raw)) + self.assertIsInstance(res, bytes) + self.assertIsInstance(a, bytes) + self.assertLess(max(a), 128) + self.assertIsInstance(binascii.crc_hqx(raw, 0), int) + self.assertIsInstance(binascii.crc32(raw), int) + def test_base64valid(self): # Test base64 with valid data MAX_BASE64 = 57 lines = [] - for i in range(0, len(self.data), MAX_BASE64): - b = self.data[i:i+MAX_BASE64] + for i in range(0, len(self.rawdata), MAX_BASE64): + b = self.type2test(self.rawdata[i:i+MAX_BASE64]) a = binascii.b2a_base64(b) lines.append(a) res = bytes() for line in lines: - b = binascii.a2b_base64(line) + a = self.type2test(line) + b = binascii.a2b_base64(a) res += b - self.assertEqual(res, self.data) + self.assertEqual(res, self.rawdata) def test_base64invalid(self): # Test base64 with random invalid characters sprinkled throughout @@ -52,7 +80,7 @@ class BinASCIITest(unittest.TestCase): MAX_BASE64 = 57 lines = [] for i in range(0, len(self.data), MAX_BASE64): - b = self.data[i:i+MAX_BASE64] + b = self.type2test(self.rawdata[i:i+MAX_BASE64]) a = binascii.b2a_base64(b) lines.append(a) @@ -74,26 +102,28 @@ class BinASCIITest(unittest.TestCase): return res + noise + line res = bytearray() for line in map(addnoise, lines): - b = binascii.a2b_base64(line) + a = self.type2test(line) + b = binascii.a2b_base64(a) res += b - self.assertEqual(res, self.data) + self.assertEqual(res, self.rawdata) # Test base64 with just invalid characters, which should return # empty strings. TBD: shouldn't it raise an exception instead ? - self.assertEqual(binascii.a2b_base64(fillers), b'') + self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'') def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): - b = self.data[i:i+MAX_UU] + b = self.type2test(self.rawdata[i:i+MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: - b = binascii.a2b_uu(line) + a = self.type2test(line) + b = binascii.a2b_uu(a) res += b - self.assertEqual(res, self.data) + self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) @@ -107,19 +137,27 @@ class BinASCIITest(unittest.TestCase): self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') def test_crc32(self): - crc = binascii.crc32(b"Test the CRC-32 of") - crc = binascii.crc32(b" this string.", crc) + crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) + crc = binascii.crc32(self.type2test(b" this string."), crc) self.assertEqual(crc, 1571220330) self.assertRaises(TypeError, binascii.crc32) - # The hqx test is in test_binhex.py + def test_hqx(self): + # Perform binhex4 style RLE-compression + # Then calculate the hexbin4 binary-to-ASCII translation + rle = binascii.rlecode_hqx(self.data) + a = binascii.b2a_hqx(self.type2test(rle)) + b, _ = binascii.a2b_hqx(self.type2test(a)) + res = binascii.rledecode_hqx(b) + + self.assertEqual(res, self.rawdata) def test_hex(self): # test hexlification s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' - t = binascii.b2a_hex(s) - u = binascii.a2b_hex(t) + t = binascii.b2a_hex(self.type2test(s)) + u = binascii.a2b_hex(self.type2test(t)) self.assertEqual(s, u) self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') @@ -129,7 +167,7 @@ class BinASCIITest(unittest.TestCase): def test_qp(self): # A test for SF bug 534347 (segfaults without the proper fix) try: - binascii.a2b_qp("", **{1:1}) + binascii.a2b_qp(b"", **{1:1}) except TypeError: pass else: @@ -141,12 +179,10 @@ class BinASCIITest(unittest.TestCase): self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00") self.assertEqual( binascii.b2a_qp(b"\xff\r\n\xff\n\xff"), - b"=FF\r\n=FF\r\n=FF" - ) + b"=FF\r\n=FF\r\n=FF") self.assertEqual( binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"), - b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF" - ) + b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF") self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n') self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n') @@ -160,27 +196,47 @@ class BinASCIITest(unittest.TestCase): def test_empty_string(self): # A test for SF bug #1022953. Make sure SystemError is not raised. - for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', - 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', - 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', - 'rledecode_hqx']: - f = getattr(binascii, n) + empty = self.type2test(b'') + for func in all_functions: + if func == 'crc_hqx': + # crc_hqx needs 2 arguments + binascii.crc_hqx(empty, 0) + continue + f = getattr(binascii, func) + try: + f(empty) + except Exception as err: + self.fail("{}({!r}) raises {!r}".format(func, empty, err)) + + def test_unicode_strings(self): + # Unicode strings are not accepted. + for func in all_functions: try: - f(b'') - except SystemError as err: - self.fail("%s(b'') raises SystemError: %s" % (n, err)) - binascii.crc_hqx(b'', 0) - - def test_no_binary_strings(self): - # b2a_ must not accept strings - for f in (binascii.b2a_uu, binascii.b2a_base64, - binascii.b2a_hqx, binascii.b2a_qp, - binascii.hexlify, binascii.rlecode_hqx, - binascii.crc_hqx, binascii.crc32): - self.assertRaises(TypeError, f, "test") + self.assertRaises(TypeError, getattr(binascii, func), "test") + except Exception as err: + self.fail('{}("test") raises {!r}'.format(func, err)) + # crc_hqx needs 2 arguments + self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) + + +class ArrayBinASCIITest(BinASCIITest): + def type2test(self, s): + return array.array('B', list(s)) + + +class BytearrayBinASCIITest(BinASCIITest): + type2test = bytearray + + +class MemoryviewBinASCIITest(BinASCIITest): + type2test = memoryview + def test_main(): - support.run_unittest(BinASCIITest) + support.run_unittest(BinASCIITest, + ArrayBinASCIITest, + BytearrayBinASCIITest, + MemoryviewBinASCIITest) if __name__ == "__main__": test_main() |
