blob: 8fa57cdf1b0be36b6651b625aa215ebb9b8aca2d [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
Xiang Zhang13f1f422017-05-03 11:16:21 +0800115 for backtick in (True, False):
116 lines = []
117 for i in range(0, len(self.data), MAX_UU):
118 b = self.type2test(self.rawdata[i:i+MAX_UU])
119 a = binascii.b2a_uu(b, backtick=backtick)
120 lines.append(a)
121 res = bytes()
122 for line in lines:
123 a = self.type2test(line)
124 b = binascii.a2b_uu(a)
125 res += b
126 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000127
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000128 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"!!!!")
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
Xiang Zhang13f1f422017-05-03 11:16:21 +0800138 self.assertEqual(binascii.b2a_uu(b''), b' \n')
139 self.assertEqual(binascii.b2a_uu(b'', backtick=True), b'`\n')
140 self.assertEqual(binascii.a2b_uu(b' \n'), b'')
141 self.assertEqual(binascii.a2b_uu(b'`\n'), b'')
142 self.assertEqual(binascii.b2a_uu(b'\x00Cat'), b'$ $-A= \n')
143 self.assertEqual(binascii.b2a_uu(b'\x00Cat', backtick=True),
144 b'$`$-A=```\n')
145 self.assertEqual(binascii.a2b_uu(b'$`$-A=```\n'),
146 binascii.a2b_uu(b'$ $-A= \n'))
147 with self.assertRaises(TypeError):
148 binascii.b2a_uu(b"", True)
149
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300150 def test_crc_hqx(self):
151 crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0)
152 crc = binascii.crc_hqx(self.type2test(b" this string."), crc)
153 self.assertEqual(crc, 14290)
154
155 self.assertRaises(TypeError, binascii.crc_hqx)
156 self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b''))
157
158 for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1:
159 self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc),
160 crc & 0xffff)
161
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000162 def test_crc32(self):
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000163 crc = binascii.crc32(self.type2test(b"Test the CRC-32 of"))
164 crc = binascii.crc32(self.type2test(b" this string."), crc)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000165 self.assertEqual(crc, 1571220330)
166
167 self.assertRaises(TypeError, binascii.crc32)
168
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000169 def test_hqx(self):
170 # Perform binhex4 style RLE-compression
171 # Then calculate the hexbin4 binary-to-ASCII translation
172 rle = binascii.rlecode_hqx(self.data)
173 a = binascii.b2a_hqx(self.type2test(rle))
Victor Stinner1bfe9302015-10-14 15:02:35 +0200174
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000175 b, _ = binascii.a2b_hqx(self.type2test(a))
176 res = binascii.rledecode_hqx(b)
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000177 self.assertEqual(res, self.rawdata)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000178
Victor Stinner1bfe9302015-10-14 15:02:35 +0200179 def test_rle(self):
180 # test repetition with a repetition longer than the limit of 255
181 data = (b'a' * 100 + b'b' + b'c' * 300)
182
183 encoded = binascii.rlecode_hqx(data)
184 self.assertEqual(encoded,
185 (b'a\x90d' # 'a' * 100
186 b'b' # 'b'
187 b'c\x90\xff' # 'c' * 255
188 b'c\x90-')) # 'c' * 45
189
190 decoded = binascii.rledecode_hqx(encoded)
191 self.assertEqual(decoded, data)
192
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000193 def test_hex(self):
194 # test hexlification
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000195 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 +0000196 t = binascii.b2a_hex(self.type2test(s))
197 u = binascii.a2b_hex(self.type2test(t))
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000198 self.assertEqual(s, u)
Guido van Rossum4581ae52007-05-22 21:56:47 +0000199 self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1])
200 self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q')
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000201
Zachary Wareb176d402015-01-20 13:59:46 -0600202 # Confirm that b2a_hex == hexlify and a2b_hex == unhexlify
203 self.assertEqual(binascii.hexlify(self.type2test(s)), t)
204 self.assertEqual(binascii.unhexlify(self.type2test(t)), u)
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000205
206 def test_qp(self):
Serhiy Storchakae6265e92016-09-14 16:34:37 +0300207 type2test = self.type2test
208 a2b_qp = binascii.a2b_qp
209 b2a_qp = binascii.b2a_qp
210
211 a2b_qp(data=b"", header=False) # Keyword arguments allowed
Martin Panterbf19d162015-09-09 01:01:13 +0000212
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000213 # A test for SF bug 534347 (segfaults without the proper fix)
214 try:
Serhiy Storchakae6265e92016-09-14 16:34:37 +0300215 a2b_qp(b"", **{1:1})
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000216 except TypeError:
217 pass
Guido van Rossumfdecda01999-10-19 19:08:13 +0000218 else:
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000219 self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
Martin Panterbf19d162015-09-09 01:01:13 +0000220
Serhiy Storchakae6265e92016-09-14 16:34:37 +0300221 self.assertEqual(a2b_qp(type2test(b"=")), b"")
222 self.assertEqual(a2b_qp(type2test(b"= ")), b"= ")
223 self.assertEqual(a2b_qp(type2test(b"==")), b"=")
224 self.assertEqual(a2b_qp(type2test(b"=\nAB")), b"AB")
225 self.assertEqual(a2b_qp(type2test(b"=\r\nAB")), b"AB")
226 self.assertEqual(a2b_qp(type2test(b"=\rAB")), b"") # ?
227 self.assertEqual(a2b_qp(type2test(b"=\rAB\nCD")), b"CD") # ?
228 self.assertEqual(a2b_qp(type2test(b"=AB")), b"\xab")
229 self.assertEqual(a2b_qp(type2test(b"=ab")), b"\xab")
230 self.assertEqual(a2b_qp(type2test(b"=AX")), b"=AX")
231 self.assertEqual(a2b_qp(type2test(b"=XA")), b"=XA")
232 self.assertEqual(a2b_qp(type2test(b"=AB")[:-1]), b"=A")
Roger E. Masse2a1c8341997-01-16 16:44:09 +0000233
Serhiy Storchakae6265e92016-09-14 16:34:37 +0300234 self.assertEqual(a2b_qp(type2test(b'_')), b'_')
235 self.assertEqual(a2b_qp(type2test(b'_'), header=True), b' ')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000236
Serhiy Storchakae6265e92016-09-14 16:34:37 +0300237 self.assertRaises(TypeError, b2a_qp, foo="bar")
238 self.assertEqual(a2b_qp(type2test(b"=00\r\n=00")), b"\x00\r\n\x00")
239 self.assertEqual(b2a_qp(type2test(b"\xff\r\n\xff\n\xff")),
240 b"=FF\r\n=FF\r\n=FF")
241 self.assertEqual(b2a_qp(type2test(b"0"*75+b"\xff\r\n\xff\r\n\xff")),
242 b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")
243
244 self.assertEqual(b2a_qp(type2test(b'\x7f')), b'=7F')
245 self.assertEqual(b2a_qp(type2test(b'=')), b'=3D')
246
247 self.assertEqual(b2a_qp(type2test(b'_')), b'_')
248 self.assertEqual(b2a_qp(type2test(b'_'), header=True), b'=5F')
249 self.assertEqual(b2a_qp(type2test(b'x y'), header=True), b'x_y')
250 self.assertEqual(b2a_qp(type2test(b'x '), header=True), b'x=20')
251 self.assertEqual(b2a_qp(type2test(b'x y'), header=True, quotetabs=True),
252 b'x=20y')
253 self.assertEqual(b2a_qp(type2test(b'x\ty'), header=True), b'x\ty')
254
255 self.assertEqual(b2a_qp(type2test(b' ')), b'=20')
256 self.assertEqual(b2a_qp(type2test(b'\t')), b'=09')
257 self.assertEqual(b2a_qp(type2test(b' x')), b' x')
258 self.assertEqual(b2a_qp(type2test(b'\tx')), b'\tx')
259 self.assertEqual(b2a_qp(type2test(b' x')[:-1]), b'=20')
260 self.assertEqual(b2a_qp(type2test(b'\tx')[:-1]), b'=09')
261 self.assertEqual(b2a_qp(type2test(b'\0')), b'=00')
262
263 self.assertEqual(b2a_qp(type2test(b'\0\n')), b'=00\n')
264 self.assertEqual(b2a_qp(type2test(b'\0\n'), quotetabs=True), b'=00\n')
265
266 self.assertEqual(b2a_qp(type2test(b'x y\tz')), b'x y\tz')
267 self.assertEqual(b2a_qp(type2test(b'x y\tz'), quotetabs=True),
268 b'x=20y=09z')
269 self.assertEqual(b2a_qp(type2test(b'x y\tz'), istext=False),
270 b'x y\tz')
271 self.assertEqual(b2a_qp(type2test(b'x \ny\t\n')),
272 b'x=20\ny=09\n')
273 self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), quotetabs=True),
274 b'x=20\ny=09\n')
275 self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), istext=False),
276 b'x =0Ay\t=0A')
277 self.assertEqual(b2a_qp(type2test(b'x \ry\t\r')),
278 b'x \ry\t\r')
279 self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), quotetabs=True),
280 b'x=20\ry=09\r')
281 self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), istext=False),
282 b'x =0Dy\t=0D')
283 self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n')),
284 b'x=20\r\ny=09\r\n')
285 self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), quotetabs=True),
286 b'x=20\r\ny=09\r\n')
287 self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), istext=False),
288 b'x =0D=0Ay\t=0D=0A')
289
290 self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1]), b'x \r')
291 self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1]), b'x\t\r')
292 self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], quotetabs=True),
293 b'x=20\r')
294 self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], quotetabs=True),
295 b'x=09\r')
296 self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], istext=False),
297 b'x =0D')
298 self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], istext=False),
299 b'x\t=0D')
300
301 self.assertEqual(b2a_qp(type2test(b'.')), b'=2E')
302 self.assertEqual(b2a_qp(type2test(b'.\n')), b'=2E\n')
303 self.assertEqual(b2a_qp(type2test(b'.\r')), b'=2E\r')
304 self.assertEqual(b2a_qp(type2test(b'.\0')), b'=2E=00')
305 self.assertEqual(b2a_qp(type2test(b'a.\n')), b'a.\n')
306 self.assertEqual(b2a_qp(type2test(b'.a')[:-1]), b'=2E')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000307
Raymond Hettinger658717e2004-09-06 22:58:37 +0000308 def test_empty_string(self):
309 # A test for SF bug #1022953. Make sure SystemError is not raised.
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000310 empty = self.type2test(b'')
311 for func in all_functions:
312 if func == 'crc_hqx':
313 # crc_hqx needs 2 arguments
314 binascii.crc_hqx(empty, 0)
315 continue
316 f = getattr(binascii, func)
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000317 try:
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000318 f(empty)
319 except Exception as err:
320 self.fail("{}({!r}) raises {!r}".format(func, empty, err))
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000321
Antoine Pitrou08316762011-12-20 13:58:41 +0100322 def test_unicode_b2a(self):
323 # Unicode strings are not accepted by b2a_* functions.
324 for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}:
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000325 try:
326 self.assertRaises(TypeError, getattr(binascii, func), "test")
327 except Exception as err:
328 self.fail('{}("test") raises {!r}'.format(func, err))
329 # crc_hqx needs 2 arguments
330 self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
Raymond Hettinger658717e2004-09-06 22:58:37 +0000331
Antoine Pitrou08316762011-12-20 13:58:41 +0100332 def test_unicode_a2b(self):
333 # Unicode strings are accepted by a2b_* functions.
334 MAX_ALL = 45
335 raw = self.rawdata[:MAX_ALL]
336 for fa, fb in zip(a2b_functions, b2a_functions):
337 if fa == 'rledecode_hqx':
338 # Takes non-ASCII data
339 continue
340 a2b = getattr(binascii, fa)
341 b2a = getattr(binascii, fb)
342 try:
343 a = b2a(self.type2test(raw))
344 binary_res = a2b(a)
345 a = a.decode('ascii')
346 res = a2b(a)
347 except Exception as err:
348 self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
349 if fb == 'b2a_hqx':
350 # b2a_hqx returns a tuple
351 res, _ = res
352 binary_res, _ = binary_res
353 self.assertEqual(res, raw, "{}/{} conversion: "
354 "{!r} != {!r}".format(fb, fa, res, raw))
355 self.assertEqual(res, binary_res)
356 self.assertIsInstance(res, bytes)
357 # non-ASCII string
358 self.assertRaises(ValueError, a2b, "\x80")
359
Victor Stinnere84c9762015-10-11 11:01:02 +0200360 def test_b2a_base64_newline(self):
361 # Issue #25357: test newline parameter
362 b = self.type2test(b'hello')
363 self.assertEqual(binascii.b2a_base64(b),
364 b'aGVsbG8=\n')
365 self.assertEqual(binascii.b2a_base64(b, newline=True),
366 b'aGVsbG8=\n')
367 self.assertEqual(binascii.b2a_base64(b, newline=False),
368 b'aGVsbG8=')
369
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +0000370
371class ArrayBinASCIITest(BinASCIITest):
372 def type2test(self, s):
373 return array.array('B', list(s))
374
375
Antoine Pitrou747e8b32010-01-16 17:55:52 +0000376class BytearrayBinASCIITest(BinASCIITest):
377 type2test = bytearray
378
379
Antoine Pitrou6d4b00c2010-01-14 16:33:34 +0000380class MemoryviewBinASCIITest(BinASCIITest):
381 type2test = memoryview
382
383
Walter Dörwaldcd1e8a92004-03-15 12:07:38 +0000384if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500385 unittest.main()