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)) |
Victor Stinner | 1bfe930 | 2015-10-14 15:02:35 +0200 | [diff] [blame] | 162 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 163 | b, _ = binascii.a2b_hqx(self.type2test(a)) |
| 164 | res = binascii.rledecode_hqx(b) |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 165 | self.assertEqual(res, self.rawdata) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 166 | |
Victor Stinner | 1bfe930 | 2015-10-14 15:02:35 +0200 | [diff] [blame] | 167 | def test_rle(self): |
| 168 | # test repetition with a repetition longer than the limit of 255 |
| 169 | data = (b'a' * 100 + b'b' + b'c' * 300) |
| 170 | |
| 171 | encoded = binascii.rlecode_hqx(data) |
| 172 | self.assertEqual(encoded, |
| 173 | (b'a\x90d' # 'a' * 100 |
| 174 | b'b' # 'b' |
| 175 | b'c\x90\xff' # 'c' * 255 |
| 176 | b'c\x90-')) # 'c' * 45 |
| 177 | |
| 178 | decoded = binascii.rledecode_hqx(encoded) |
| 179 | self.assertEqual(decoded, data) |
| 180 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 181 | def test_hex(self): |
| 182 | # test hexlification |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 183 | 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] | 184 | t = binascii.b2a_hex(self.type2test(s)) |
| 185 | u = binascii.a2b_hex(self.type2test(t)) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 186 | self.assertEqual(s, u) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 187 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
| 188 | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 189 | |
Zachary Ware | b176d40 | 2015-01-20 13:59:46 -0600 | [diff] [blame] | 190 | # Confirm that b2a_hex == hexlify and a2b_hex == unhexlify |
| 191 | self.assertEqual(binascii.hexlify(self.type2test(s)), t) |
| 192 | self.assertEqual(binascii.unhexlify(self.type2test(t)), u) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 193 | |
| 194 | def test_qp(self): |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 195 | binascii.a2b_qp(data=b"", header=False) # Keyword arguments allowed |
| 196 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 197 | # A test for SF bug 534347 (segfaults without the proper fix) |
| 198 | try: |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 199 | binascii.a2b_qp(b"", **{1:1}) |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 200 | except TypeError: |
| 201 | pass |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 202 | else: |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 203 | self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 205 | self.assertEqual(binascii.a2b_qp(b"= "), b"= ") |
| 206 | self.assertEqual(binascii.a2b_qp(b"=="), b"=") |
| 207 | self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 208 | self.assertRaises(TypeError, binascii.b2a_qp, foo="bar") |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 209 | 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] | 210 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 211 | binascii.b2a_qp(b"\xff\r\n\xff\n\xff"), |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 212 | b"=FF\r\n=FF\r\n=FF") |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 213 | self.assertEqual( |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 214 | 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] | 215 | 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] | 216 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 217 | self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n') |
| 218 | self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n') |
| 219 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n') |
| 220 | self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True), |
| 221 | b'foo=09bar=09\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 223 | self.assertEqual(binascii.b2a_qp(b'.'), b'=2E') |
| 224 | self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n') |
| 225 | self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 226 | |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 227 | def test_empty_string(self): |
| 228 | # A test for SF bug #1022953. Make sure SystemError is not raised. |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 229 | empty = self.type2test(b'') |
| 230 | for func in all_functions: |
| 231 | if func == 'crc_hqx': |
| 232 | # crc_hqx needs 2 arguments |
| 233 | binascii.crc_hqx(empty, 0) |
| 234 | continue |
| 235 | f = getattr(binascii, func) |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 236 | try: |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 237 | f(empty) |
| 238 | except Exception as err: |
| 239 | self.fail("{}({!r}) raises {!r}".format(func, empty, err)) |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 241 | def test_unicode_b2a(self): |
| 242 | # Unicode strings are not accepted by b2a_* functions. |
| 243 | for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}: |
Florent Xicluna | f1046ca | 2010-07-27 21:20:15 +0000 | [diff] [blame] | 244 | try: |
| 245 | self.assertRaises(TypeError, getattr(binascii, func), "test") |
| 246 | except Exception as err: |
| 247 | self.fail('{}("test") raises {!r}'.format(func, err)) |
| 248 | # crc_hqx needs 2 arguments |
| 249 | self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) |
Raymond Hettinger | 658717e | 2004-09-06 22:58:37 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 251 | def test_unicode_a2b(self): |
| 252 | # Unicode strings are accepted by a2b_* functions. |
| 253 | MAX_ALL = 45 |
| 254 | raw = self.rawdata[:MAX_ALL] |
| 255 | for fa, fb in zip(a2b_functions, b2a_functions): |
| 256 | if fa == 'rledecode_hqx': |
| 257 | # Takes non-ASCII data |
| 258 | continue |
| 259 | a2b = getattr(binascii, fa) |
| 260 | b2a = getattr(binascii, fb) |
| 261 | try: |
| 262 | a = b2a(self.type2test(raw)) |
| 263 | binary_res = a2b(a) |
| 264 | a = a.decode('ascii') |
| 265 | res = a2b(a) |
| 266 | except Exception as err: |
| 267 | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
| 268 | if fb == 'b2a_hqx': |
| 269 | # b2a_hqx returns a tuple |
| 270 | res, _ = res |
| 271 | binary_res, _ = binary_res |
| 272 | self.assertEqual(res, raw, "{}/{} conversion: " |
| 273 | "{!r} != {!r}".format(fb, fa, res, raw)) |
| 274 | self.assertEqual(res, binary_res) |
| 275 | self.assertIsInstance(res, bytes) |
| 276 | # non-ASCII string |
| 277 | self.assertRaises(ValueError, a2b, "\x80") |
| 278 | |
Victor Stinner | e84c976 | 2015-10-11 11:01:02 +0200 | [diff] [blame] | 279 | def test_b2a_base64_newline(self): |
| 280 | # Issue #25357: test newline parameter |
| 281 | b = self.type2test(b'hello') |
| 282 | self.assertEqual(binascii.b2a_base64(b), |
| 283 | b'aGVsbG8=\n') |
| 284 | self.assertEqual(binascii.b2a_base64(b, newline=True), |
| 285 | b'aGVsbG8=\n') |
| 286 | self.assertEqual(binascii.b2a_base64(b, newline=False), |
| 287 | b'aGVsbG8=') |
| 288 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 289 | |
| 290 | class ArrayBinASCIITest(BinASCIITest): |
| 291 | def type2test(self, s): |
| 292 | return array.array('B', list(s)) |
| 293 | |
| 294 | |
Antoine Pitrou | 747e8b3 | 2010-01-16 17:55:52 +0000 | [diff] [blame] | 295 | class BytearrayBinASCIITest(BinASCIITest): |
| 296 | type2test = bytearray |
| 297 | |
| 298 | |
Antoine Pitrou | 6d4b00c | 2010-01-14 16:33:34 +0000 | [diff] [blame] | 299 | class MemoryviewBinASCIITest(BinASCIITest): |
| 300 | type2test = memoryview |
| 301 | |
| 302 | |
Walter Dörwald | cd1e8a9 | 2004-03-15 12:07:38 +0000 | [diff] [blame] | 303 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 304 | unittest.main() |