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 |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 6 | import array |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 8 | # Note: "*_hex" functions are aliases for "(un)hexlify" |
| 9 | b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', |
| 10 | 'hexlify', 'rlecode_hqx'] |
| 11 | a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', |
| 12 | 'unhexlify', 'rledecode_hqx'] |
| 13 | all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] |
| 14 | |
| 15 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 16 | class BinASCIITest(unittest.TestCase): |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 18 | type2test = bytes |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 19 | # Create binary test data |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 20 | rawdata = 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] | 21 | # Be slow so we don't depend on other modules |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 22 | rawdata += bytes(range(256)) |
| 23 | rawdata += b"\r\nHello world.\n" |
| 24 | |
| 25 | def setUp(self): |
| 26 | self.data = self.type2test(self.rawdata) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 27 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 28 | def test_exceptions(self): |
| 29 | # Check module exceptions |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 30 | self.assertTrue(issubclass(binascii.Error, Exception)) |
| 31 | self.assertTrue(issubclass(binascii.Incomplete, Exception)) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 32 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 33 | def test_functions(self): |
| 34 | # Check presence of all functions |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 35 | for name in all_functions: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 36 | self.assertTrue(hasattr(getattr(binascii, name), '__call__')) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 37 | self.assertRaises(TypeError, getattr(binascii, name)) |
| 38 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 39 | def test_returned_value(self): |
| 40 | # Limit to the minimum of all limits (b2a_uu) |
| 41 | MAX_ALL = 45 |
| 42 | raw = self.rawdata[:MAX_ALL] |
| 43 | for fa, fb in zip(a2b_functions, b2a_functions): |
| 44 | a2b = getattr(binascii, fa) |
| 45 | b2a = getattr(binascii, fb) |
| 46 | try: |
| 47 | a = b2a(self.type2test(raw)) |
| 48 | res = a2b(self.type2test(a)) |
| 49 | except Exception as err: |
| 50 | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 51 | if fb == 'b2a_hqx': |
| 52 | # b2a_hqx returns a tuple |
| 53 | res, _ = res |
| 54 | self.assertEqual(res, raw, "{}/{} conversion: " |
| 55 | "{!r} != {!r}".format(fb, fa, res, raw)) |
| 56 | self.assertIsInstance(res, bytes) |
| 57 | self.assertIsInstance(a, bytes) |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 58 | self.assertLess(max(a), 128) |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 59 | self.assertIsInstance(binascii.crc_hqx(raw, 0), int) |
| 60 | self.assertIsInstance(binascii.crc32(raw), int) |
| 61 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 62 | def test_base64valid(self): |
| 63 | # Test base64 with valid data |
| 64 | MAX_BASE64 = 57 |
| 65 | lines = [] |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 66 | for i in range(0, len(self.rawdata), MAX_BASE64): |
| 67 | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 68 | a = binascii.b2a_base64(b) |
| 69 | lines.append(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 70 | res = bytes() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 71 | for line in lines: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 72 | a = self.type2test(line) |
| 73 | b = binascii.a2b_base64(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 74 | res += b |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 75 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 76 | |
| 77 | def test_base64invalid(self): |
| 78 | # Test base64 with random invalid characters sprinkled throughout |
| 79 | # (This requires a new version of binascii.) |
| 80 | MAX_BASE64 = 57 |
| 81 | lines = [] |
| 82 | for i in range(0, len(self.data), MAX_BASE64): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 83 | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 84 | a = binascii.b2a_base64(b) |
| 85 | lines.append(a) |
| 86 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 87 | fillers = bytearray() |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 88 | valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 89 | for i in range(256): |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 90 | if i not in valid: |
| 91 | fillers.append(i) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 92 | def addnoise(line): |
| 93 | noise = fillers |
| 94 | ratio = len(line) // len(noise) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 95 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 96 | while line and noise: |
| 97 | if len(line) // len(noise) > ratio: |
| 98 | c, line = line[0], line[1:] |
| 99 | else: |
| 100 | c, noise = noise[0], noise[1:] |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 101 | res.append(c) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 102 | return res + noise + line |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 103 | res = bytearray() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 104 | for line in map(addnoise, lines): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 105 | a = self.type2test(line) |
| 106 | b = binascii.a2b_base64(a) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 107 | res += b |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 108 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 109 | |
| 110 | # Test base64 with just invalid characters, which should return |
| 111 | # empty strings. TBD: shouldn't it raise an exception instead ? |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 112 | self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 113 | |
| 114 | def test_uu(self): |
| 115 | MAX_UU = 45 |
| 116 | lines = [] |
| 117 | for i in range(0, len(self.data), MAX_UU): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 118 | b = self.type2test(self.rawdata[i:i+MAX_UU]) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 119 | a = binascii.b2a_uu(b) |
| 120 | lines.append(a) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 121 | res = bytes() |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 122 | for line in lines: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 123 | a = self.type2test(line) |
| 124 | b = binascii.a2b_uu(a) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 125 | res += b |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 126 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 128 | self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) |
| 129 | self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) |
| 130 | self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) |
| 131 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") |
| 132 | self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") |
Tim Peters | 27f8836 | 2004-07-08 04:22:35 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 134 | self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | 2843aa8 | 2010-01-15 00:27:43 +0000 | [diff] [blame] | 136 | # Issue #7701 (crash on a pydebug build) |
| 137 | self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') |
| 138 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 139 | def test_crc32(self): |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 140 | crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) |
| 141 | crc = binascii.crc32(self.type2test(b" this string."), crc) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 142 | self.assertEqual(crc, 1571220330) |
| 143 | |
| 144 | self.assertRaises(TypeError, binascii.crc32) |
| 145 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 146 | def test_hqx(self): |
| 147 | # Perform binhex4 style RLE-compression |
| 148 | # Then calculate the hexbin4 binary-to-ASCII translation |
| 149 | rle = binascii.rlecode_hqx(self.data) |
| 150 | a = binascii.b2a_hqx(self.type2test(rle)) |
| 151 | b, _ = binascii.a2b_hqx(self.type2test(a)) |
| 152 | res = binascii.rledecode_hqx(b) |
| 153 | |
| 154 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 155 | |
| 156 | def test_hex(self): |
| 157 | # test hexlification |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 158 | s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 159 | t = binascii.b2a_hex(self.type2test(s)) |
| 160 | u = binascii.a2b_hex(self.type2test(t)) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 161 | self.assertEqual(s, u) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 162 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
| 163 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 164 | |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 165 | self.assertEqual(binascii.hexlify(b'a'), b'61') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 166 | |
| 167 | def test_qp(self): |
| 168 | # A test for SF bug 534347 (segfaults without the proper fix) |
| 169 | try: |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 170 | binascii.a2b_qp(b"", **{1:1}) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 171 | except TypeError: |
| 172 | pass |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 173 | else: |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 174 | self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 175 | self.assertEqual(binascii.a2b_qp(b"= "), b"= ") |
| 176 | self.assertEqual(binascii.a2b_qp(b"=="), b"=") |
| 177 | self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 178 | self.assertRaises(TypeError, binascii.b2a_qp, foo="bar") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 179 | 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] | 180 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 181 | binascii.b2a_qp(b"\xff\r\n\xff\n\xff"), |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 182 | b"=FF\r\n=FF\r\n=FF") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 183 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 184 | binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"), |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 185 | b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF") |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 187 | self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n') |
| 188 | self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n') |
| 189 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n') |
| 190 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True), |
| 191 | b'foo=09bar=09\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 193 | self.assertEqual(binascii.b2a_qp(b'.'), b'=2E') |
| 194 | self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n') |
| 195 | self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 196 | |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 197 | def test_empty_string(self): |
| 198 | # A test for SF bug #1022953. Make sure SystemError is not raised. |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 199 | empty = self.type2test(b'') |
| 200 | for func in all_functions: |
| 201 | if func == 'crc_hqx': |
| 202 | # crc_hqx needs 2 arguments |
| 203 | binascii.crc_hqx(empty, 0) |
| 204 | continue |
| 205 | f = getattr(binascii, func) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 206 | try: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 207 | f(empty) |
| 208 | except Exception as err: |
| 209 | self.fail("{}({!r}) raises {!r}".format(func, empty, err)) |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 210 | |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 211 | def test_unicode_strings(self): |
| 212 | # Unicode strings are not accepted. |
| 213 | for func in all_functions: |
| 214 | try: |
| 215 | self.assertRaises(TypeError, getattr(binascii, func), "test") |
| 216 | except Exception as err: |
| 217 | self.fail('{}("test") raises {!r}'.format(func, err)) |
| 218 | # crc_hqx needs 2 arguments |
| 219 | self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 221 | |
| 222 | class ArrayBinASCIITest(BinASCIITest): |
| 223 | def type2test(self, s): |
| 224 | return array.array('B', list(s)) |
| 225 | |
| 226 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 227 | class BytearrayBinASCIITest(BinASCIITest): |
| 228 | type2test = bytearray |
| 229 | |
| 230 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 231 | class MemoryviewBinASCIITest(BinASCIITest): |
| 232 | type2test = memoryview |
| 233 | |
| 234 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 235 | def test_main(): |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 236 | support.run_unittest(BinASCIITest, |
| 237 | ArrayBinASCIITest, |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 238 | BytearrayBinASCIITest, |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 239 | MemoryviewBinASCIITest) |
Thomas Wouters | 9e1c192 | 2003-03-17 11:24:29 +0000 | [diff] [blame] | 240 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 241 | if __name__ == "__main__": |
| 242 | test_main() |