Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 1 | """Test the binascii C module.""" |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 4 | import unittest |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 5 | import binascii |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 7 | class BinASCIITest(unittest.TestCase): |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 9 | # Create binary test data |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 10 | data = b"The quick brown fox jumps over the lazy dog.\r\n" |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 11 | # Be slow so we don't depend on other modules |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 12 | data += bytes(range(256)) |
| 13 | data += b"\r\nHello world.\n" |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 14 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 15 | def test_exceptions(self): |
| 16 | # Check module exceptions |
| 17 | self.assert_(issubclass(binascii.Error, Exception)) |
| 18 | self.assert_(issubclass(binascii.Incomplete, Exception)) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 19 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 20 | def test_functions(self): |
| 21 | # Check presence of all functions |
| 22 | funcs = [] |
| 23 | for suffix in "base64", "hqx", "uu", "hex": |
| 24 | prefixes = ["a2b_", "b2a_"] |
| 25 | if suffix == "hqx": |
| 26 | prefixes.extend(["crc_", "rlecode_", "rledecode_"]) |
| 27 | for prefix in prefixes: |
| 28 | name = prefix + suffix |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 29 | self.assert_(hasattr(getattr(binascii, name), '__call__')) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 30 | self.assertRaises(TypeError, getattr(binascii, name)) |
| 31 | for name in ("hexlify", "unhexlify"): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 32 | self.assert_(hasattr(getattr(binascii, name), '__call__')) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 33 | self.assertRaises(TypeError, getattr(binascii, name)) |
| 34 | |
| 35 | def test_base64valid(self): |
| 36 | # Test base64 with valid data |
| 37 | MAX_BASE64 = 57 |
| 38 | lines = [] |
| 39 | for i in range(0, len(self.data), MAX_BASE64): |
| 40 | b = self.data[i:i+MAX_BASE64] |
| 41 | a = binascii.b2a_base64(b) |
| 42 | lines.append(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 43 | res = bytes() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 44 | for line in lines: |
| 45 | b = binascii.a2b_base64(line) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 46 | res += b |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 47 | self.assertEqual(res, self.data) |
| 48 | |
| 49 | def test_base64invalid(self): |
| 50 | # Test base64 with random invalid characters sprinkled throughout |
| 51 | # (This requires a new version of binascii.) |
| 52 | MAX_BASE64 = 57 |
| 53 | lines = [] |
| 54 | for i in range(0, len(self.data), MAX_BASE64): |
| 55 | b = self.data[i:i+MAX_BASE64] |
| 56 | a = binascii.b2a_base64(b) |
| 57 | lines.append(a) |
| 58 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 59 | fillers = bytearray() |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 60 | valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 61 | for i in range(256): |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 62 | if i not in valid: |
| 63 | fillers.append(i) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 64 | def addnoise(line): |
| 65 | noise = fillers |
| 66 | ratio = len(line) // len(noise) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 67 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 68 | while line and noise: |
| 69 | if len(line) // len(noise) > ratio: |
| 70 | c, line = line[0], line[1:] |
| 71 | else: |
| 72 | c, noise = noise[0], noise[1:] |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 73 | res.append(c) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 74 | return res + noise + line |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 75 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 76 | for line in map(addnoise, lines): |
| 77 | b = binascii.a2b_base64(line) |
| 78 | res += b |
| 79 | self.assertEqual(res, self.data) |
| 80 | |
| 81 | # Test base64 with just invalid characters, which should return |
| 82 | # empty strings. TBD: shouldn't it raise an exception instead ? |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 83 | self.assertEqual(binascii.a2b_base64(fillers), b'') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 84 | |
| 85 | def test_uu(self): |
| 86 | MAX_UU = 45 |
| 87 | lines = [] |
| 88 | for i in range(0, len(self.data), MAX_UU): |
| 89 | b = self.data[i:i+MAX_UU] |
| 90 | a = binascii.b2a_uu(b) |
| 91 | lines.append(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 92 | res = bytes() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 93 | for line in lines: |
| 94 | b = binascii.a2b_uu(line) |
| 95 | res += b |
| 96 | self.assertEqual(res, self.data) |
| 97 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 98 | self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) |
| 99 | self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) |
| 100 | self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) |
| 101 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") |
| 102 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 104 | self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 105 | |
| 106 | def test_crc32(self): |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 107 | crc = binascii.crc32(b"Test the CRC-32 of") |
| 108 | crc = binascii.crc32(b" this string.", crc) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 109 | self.assertEqual(crc, 1571220330) |
| 110 | |
| 111 | self.assertRaises(TypeError, binascii.crc32) |
| 112 | |
| 113 | # The hqx test is in test_binhex.py |
| 114 | |
| 115 | def test_hex(self): |
| 116 | # test hexlification |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 117 | s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 118 | t = binascii.b2a_hex(s) |
| 119 | u = binascii.a2b_hex(t) |
| 120 | self.assertEqual(s, u) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 121 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
| 122 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 123 | |
Walter Dörwald | 9b77553 | 2007-06-08 14:30:53 +0000 | [diff] [blame] | 124 | self.assertEqual(binascii.hexlify('a'), b'61') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 125 | |
| 126 | def test_qp(self): |
| 127 | # A test for SF bug 534347 (segfaults without the proper fix) |
| 128 | try: |
| 129 | binascii.a2b_qp("", **{1:1}) |
| 130 | except TypeError: |
| 131 | pass |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 132 | else: |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 133 | self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 134 | self.assertEqual(binascii.a2b_qp(b"= "), b"= ") |
| 135 | self.assertEqual(binascii.a2b_qp(b"=="), b"=") |
| 136 | self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, binascii.b2a_qp, foo="bar") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 138 | self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 139 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 140 | binascii.b2a_qp(b"\xff\r\n\xff\n\xff"), |
| 141 | b"=FF\r\n=FF\r\n=FF" |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 142 | ) |
| 143 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 144 | binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"), |
| 145 | b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF" |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 146 | ) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 148 | self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n') |
| 149 | self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n') |
| 150 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n') |
| 151 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True), |
| 152 | b'foo=09bar=09\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 154 | self.assertEqual(binascii.b2a_qp(b'.'), b'=2E') |
| 155 | self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n') |
| 156 | self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 157 | |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 158 | def test_empty_string(self): |
| 159 | # A test for SF bug #1022953. Make sure SystemError is not raised. |
| 160 | for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', |
| 161 | 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', |
| 162 | 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', |
| 163 | 'rledecode_hqx']: |
| 164 | f = getattr(binascii, n) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 165 | try: |
| 166 | f(b'') |
| 167 | except SystemError as err: |
| 168 | self.fail("%s(b'') raises SystemError: %s" % (n, err)) |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 169 | binascii.crc_hqx('', 0) |
| 170 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 171 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 172 | support.run_unittest(BinASCIITest) |
Thomas Wouters | 9e1c192 | 2003-03-17 11:24:29 +0000 | [diff] [blame] | 173 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 174 | if __name__ == "__main__": |
| 175 | test_main() |