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