blob: fbc933e4e67724772a59078c9bae702c877da8ee [file] [log] [blame]
Guido van Rossumfdecda01999-10-19 19:08:13 +00001"""Test the binascii C module."""
Roger E. Masse2a1c8341997-01-16 16:44:09 +00002
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +00003import unittest
Guido van Rossumfdecda01999-10-19 19:08:13 +00004import binascii
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +00005import array
Roger E. Masse2a1c8341997-01-16 16:44:09 +00006
Antoine Pitrou747e8b32010-01-16 17:55:52 +00007# Note: "*_hex" functions are aliases for "(un)hexlify"
8b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu',
9 'hexlify', 'rlecode_hqx']
10a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu',
11 'unhexlify', 'rledecode_hqx']
12all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx']
13
14
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000015class BinASCIITest(unittest.TestCase):
Roger E. Masse2a1c8341997-01-16 16:44:09 +000016
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +000017 type2test = bytes
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000018 # Create binary test data
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +000019 rawdata = b"The quick brown fox jumps over the lazy dog.\r\n"
Guido van Rossumfdecda01999-10-19 19:08:13 +000020 # Be slow so we don't depend on other modules
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +000021 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. Masse2a1c8341997-01-16 16:44:09 +000026
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000027 def test_exceptions(self):
28 # Check module exceptions
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000029 self.assertTrue(issubclass(binascii.Error, Exception))
30 self.assertTrue(issubclass(binascii.Incomplete, Exception))
Roger E. Masse2a1c8341997-01-16 16:44:09 +000031
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000032 def test_functions(self):
33 # Check presence of all functions
Antoine Pitrou747e8b32010-01-16 17:55:52 +000034 for name in all_functions:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000035 self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000036 self.assertRaises(TypeError, getattr(binascii, name))
37
Antoine Pitrou747e8b32010-01-16 17:55:52 +000038 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 Xiclunaf1046ca2010-07-27 21:20:15 +000057 self.assertLess(max(a), 128)
Antoine Pitrou747e8b32010-01-16 17:55:52 +000058 self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
59 self.assertIsInstance(binascii.crc32(raw), int)
60
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000061 def test_base64valid(self):
62 # Test base64 with valid data
63 MAX_BASE64 = 57
64 lines = []
Antoine Pitrou747e8b32010-01-16 17:55:52 +000065 for i in range(0, len(self.rawdata), MAX_BASE64):
66 b = self.type2test(self.rawdata[i:i+MAX_BASE64])
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000067 a = binascii.b2a_base64(b)
68 lines.append(a)
Guido van Rossum0e225aa2007-05-22 20:24:57 +000069 res = bytes()
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000070 for line in lines:
Antoine Pitrou747e8b32010-01-16 17:55:52 +000071 a = self.type2test(line)
72 b = binascii.a2b_base64(a)
Guido van Rossum0e225aa2007-05-22 20:24:57 +000073 res += b
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +000074 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000075
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 Pitrou747e8b32010-01-16 17:55:52 +000082 b = self.type2test(self.rawdata[i:i+MAX_BASE64])
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000083 a = binascii.b2a_base64(b)
84 lines.append(a)
85
Guido van Rossum254348e2007-11-21 19:29:53 +000086 fillers = bytearray()
Guido van Rossum0e225aa2007-05-22 20:24:57 +000087 valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
Guido van Rossum805365e2007-05-07 22:24:25 +000088 for i in range(256):
Guido van Rossum0e225aa2007-05-22 20:24:57 +000089 if i not in valid:
90 fillers.append(i)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000091 def addnoise(line):
92 noise = fillers
93 ratio = len(line) // len(noise)
Guido van Rossum254348e2007-11-21 19:29:53 +000094 res = bytearray()
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +000095 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 Rossum0e225aa2007-05-22 20:24:57 +0000100 res.append(c)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000101 return res + noise + line
Guido van Rossum254348e2007-11-21 19:29:53 +0000102 res = bytearray()
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000103 for line in map(addnoise, lines):
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000104 a = self.type2test(line)
105 b = binascii.a2b_base64(a)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000106 res += b
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +0000107 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000108
109 # Test base64 with just invalid characters, which should return
110 # empty strings. TBD: shouldn't it raise an exception instead ?
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000111 self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'')
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000112
113 def test_uu(self):
114 MAX_UU = 45
115 lines = []
116 for i in range(0, len(self.data), MAX_UU):
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000117 b = self.type2test(self.rawdata[i:i+MAX_UU])
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000118 a = binascii.b2a_uu(b)
119 lines.append(a)
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000120 res = bytes()
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000121 for line in lines:
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000122 a = self.type2test(line)
123 b = binascii.a2b_uu(a)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000124 res += b
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +0000125 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000126
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000127 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 Peters27f88362004-07-08 04:22:35 +0000132
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000133 self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000134
Antoine Pitrou2843aa82010-01-15 00:27:43 +0000135 # Issue #7701 (crash on a pydebug build)
136 self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
137
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300138 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örwaldcd1e8a92004-03-15 12:07:38 +0000150 def test_crc32(self):
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000151 crc = binascii.crc32(self.type2test(b"Test the CRC-32 of"))
152 crc = binascii.crc32(self.type2test(b" this string."), crc)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000153 self.assertEqual(crc, 1571220330)
154
155 self.assertRaises(TypeError, binascii.crc32)
156
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000157 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 Stinner1bfe9302015-10-14 15:02:35 +0200162
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000163 b, _ = binascii.a2b_hqx(self.type2test(a))
164 res = binascii.rledecode_hqx(b)
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000165 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000166
Victor Stinner1bfe9302015-10-14 15:02:35 +0200167 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örwaldcd1e8a92004-03-15 12:07:38 +0000181 def test_hex(self):
182 # test hexlification
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000183 s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000184 t = binascii.b2a_hex(self.type2test(s))
185 u = binascii.a2b_hex(self.type2test(t))
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000186 self.assertEqual(s, u)
Guido van Rossum4581ae52007-05-22 21:56:47 +0000187 self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
188 self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000189
Zachary Wareb176d402015-01-20 13:59:46 -0600190 # 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örwaldcd1e8a92004-03-15 12:07:38 +0000193
194 def test_qp(self):
Martin Panterbf19d162015-09-09 01:01:13 +0000195 binascii.a2b_qp(data=b"", header=False) # Keyword arguments allowed
196
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000197 # A test for SF bug 534347 (segfaults without the proper fix)
198 try:
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000199 binascii.a2b_qp(b"", **{1:1})
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000200 except TypeError:
201 pass
Guido van Rossumfdecda01999-10-19 19:08:13 +0000202 else:
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000203 self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
Martin Panterbf19d162015-09-09 01:01:13 +0000204
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000205 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örwaldcd1e8a92004-03-15 12:07:38 +0000208 self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000209 self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000210 self.assertEqual(
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000211 binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000212 b"=FF\r\n=FF\r\n=FF")
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000213 self.assertEqual(
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000214 binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000215 b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")
Roger E. Masse2a1c8341997-01-16 16:44:09 +0000216
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000217 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 Rossumd8faa362007-04-27 19:54:29 +0000222
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000223 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 Rossumd8faa362007-04-27 19:54:29 +0000226
Raymond Hettinger658717e2004-09-06 22:58:37 +0000227 def test_empty_string(self):
228 # A test for SF bug #1022953. Make sure SystemError is not raised.
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000229 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 Rossum0e225aa2007-05-22 20:24:57 +0000236 try:
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000237 f(empty)
238 except Exception as err:
239 self.fail("{}({!r}) raises {!r}".format(func, empty, err))
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000240
Antoine Pitrou08316762011-12-20 13:58:41 +0100241 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 Xiclunaf1046ca2010-07-27 21:20:15 +0000244 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 Hettinger658717e2004-09-06 22:58:37 +0000250
Antoine Pitrou08316762011-12-20 13:58:41 +0100251 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 Stinnere84c9762015-10-11 11:01:02 +0200279 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 Pitrou6d4b00c2010-01-14 16:33:34 +0000289
290class ArrayBinASCIITest(BinASCIITest):
291 def type2test(self, s):
292 return array.array('B', list(s))
293
294
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000295class BytearrayBinASCIITest(BinASCIITest):
296 type2test = bytearray
297
298
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +0000299class MemoryviewBinASCIITest(BinASCIITest):
300 type2test = memoryview
301
302
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000303if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500304 unittest.main()