1 | n/a | """Test the binascii C module.""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import binascii |
---|
5 | n/a | import array |
---|
6 | n/a | |
---|
7 | n/a | # Note: "*_hex" functions are aliases for "(un)hexlify" |
---|
8 | n/a | b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', |
---|
9 | n/a | 'hexlify', 'rlecode_hqx'] |
---|
10 | n/a | a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', |
---|
11 | n/a | 'unhexlify', 'rledecode_hqx'] |
---|
12 | n/a | all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class BinASCIITest(unittest.TestCase): |
---|
16 | n/a | |
---|
17 | n/a | type2test = bytes |
---|
18 | n/a | # Create binary test data |
---|
19 | n/a | rawdata = b"The quick brown fox jumps over the lazy dog.\r\n" |
---|
20 | n/a | # Be slow so we don't depend on other modules |
---|
21 | n/a | rawdata += bytes(range(256)) |
---|
22 | n/a | rawdata += b"\r\nHello world.\n" |
---|
23 | n/a | |
---|
24 | n/a | def setUp(self): |
---|
25 | n/a | self.data = self.type2test(self.rawdata) |
---|
26 | n/a | |
---|
27 | n/a | def test_exceptions(self): |
---|
28 | n/a | # Check module exceptions |
---|
29 | n/a | self.assertTrue(issubclass(binascii.Error, Exception)) |
---|
30 | n/a | self.assertTrue(issubclass(binascii.Incomplete, Exception)) |
---|
31 | n/a | |
---|
32 | n/a | def test_functions(self): |
---|
33 | n/a | # Check presence of all functions |
---|
34 | n/a | for name in all_functions: |
---|
35 | n/a | self.assertTrue(hasattr(getattr(binascii, name), '__call__')) |
---|
36 | n/a | self.assertRaises(TypeError, getattr(binascii, name)) |
---|
37 | n/a | |
---|
38 | n/a | def test_returned_value(self): |
---|
39 | n/a | # Limit to the minimum of all limits (b2a_uu) |
---|
40 | n/a | MAX_ALL = 45 |
---|
41 | n/a | raw = self.rawdata[:MAX_ALL] |
---|
42 | n/a | for fa, fb in zip(a2b_functions, b2a_functions): |
---|
43 | n/a | a2b = getattr(binascii, fa) |
---|
44 | n/a | b2a = getattr(binascii, fb) |
---|
45 | n/a | try: |
---|
46 | n/a | a = b2a(self.type2test(raw)) |
---|
47 | n/a | res = a2b(self.type2test(a)) |
---|
48 | n/a | except Exception as err: |
---|
49 | n/a | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
---|
50 | n/a | if fb == 'b2a_hqx': |
---|
51 | n/a | # b2a_hqx returns a tuple |
---|
52 | n/a | res, _ = res |
---|
53 | n/a | self.assertEqual(res, raw, "{}/{} conversion: " |
---|
54 | n/a | "{!r} != {!r}".format(fb, fa, res, raw)) |
---|
55 | n/a | self.assertIsInstance(res, bytes) |
---|
56 | n/a | self.assertIsInstance(a, bytes) |
---|
57 | n/a | self.assertLess(max(a), 128) |
---|
58 | n/a | self.assertIsInstance(binascii.crc_hqx(raw, 0), int) |
---|
59 | n/a | self.assertIsInstance(binascii.crc32(raw), int) |
---|
60 | n/a | |
---|
61 | n/a | def test_base64valid(self): |
---|
62 | n/a | # Test base64 with valid data |
---|
63 | n/a | MAX_BASE64 = 57 |
---|
64 | n/a | lines = [] |
---|
65 | n/a | for i in range(0, len(self.rawdata), MAX_BASE64): |
---|
66 | n/a | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
---|
67 | n/a | a = binascii.b2a_base64(b) |
---|
68 | n/a | lines.append(a) |
---|
69 | n/a | res = bytes() |
---|
70 | n/a | for line in lines: |
---|
71 | n/a | a = self.type2test(line) |
---|
72 | n/a | b = binascii.a2b_base64(a) |
---|
73 | n/a | res += b |
---|
74 | n/a | self.assertEqual(res, self.rawdata) |
---|
75 | n/a | |
---|
76 | n/a | def test_base64invalid(self): |
---|
77 | n/a | # Test base64 with random invalid characters sprinkled throughout |
---|
78 | n/a | # (This requires a new version of binascii.) |
---|
79 | n/a | MAX_BASE64 = 57 |
---|
80 | n/a | lines = [] |
---|
81 | n/a | for i in range(0, len(self.data), MAX_BASE64): |
---|
82 | n/a | b = self.type2test(self.rawdata[i:i+MAX_BASE64]) |
---|
83 | n/a | a = binascii.b2a_base64(b) |
---|
84 | n/a | lines.append(a) |
---|
85 | n/a | |
---|
86 | n/a | fillers = bytearray() |
---|
87 | n/a | valid = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
---|
88 | n/a | for i in range(256): |
---|
89 | n/a | if i not in valid: |
---|
90 | n/a | fillers.append(i) |
---|
91 | n/a | def addnoise(line): |
---|
92 | n/a | noise = fillers |
---|
93 | n/a | ratio = len(line) // len(noise) |
---|
94 | n/a | res = bytearray() |
---|
95 | n/a | while line and noise: |
---|
96 | n/a | if len(line) // len(noise) > ratio: |
---|
97 | n/a | c, line = line[0], line[1:] |
---|
98 | n/a | else: |
---|
99 | n/a | c, noise = noise[0], noise[1:] |
---|
100 | n/a | res.append(c) |
---|
101 | n/a | return res + noise + line |
---|
102 | n/a | res = bytearray() |
---|
103 | n/a | for line in map(addnoise, lines): |
---|
104 | n/a | a = self.type2test(line) |
---|
105 | n/a | b = binascii.a2b_base64(a) |
---|
106 | n/a | res += b |
---|
107 | n/a | self.assertEqual(res, self.rawdata) |
---|
108 | n/a | |
---|
109 | n/a | # Test base64 with just invalid characters, which should return |
---|
110 | n/a | # empty strings. TBD: shouldn't it raise an exception instead ? |
---|
111 | n/a | self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), b'') |
---|
112 | n/a | |
---|
113 | n/a | def test_uu(self): |
---|
114 | n/a | MAX_UU = 45 |
---|
115 | n/a | lines = [] |
---|
116 | n/a | for i in range(0, len(self.data), MAX_UU): |
---|
117 | n/a | b = self.type2test(self.rawdata[i:i+MAX_UU]) |
---|
118 | n/a | a = binascii.b2a_uu(b) |
---|
119 | n/a | lines.append(a) |
---|
120 | n/a | res = bytes() |
---|
121 | n/a | for line in lines: |
---|
122 | n/a | a = self.type2test(line) |
---|
123 | n/a | b = binascii.a2b_uu(a) |
---|
124 | n/a | res += b |
---|
125 | n/a | self.assertEqual(res, self.rawdata) |
---|
126 | n/a | |
---|
127 | n/a | self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) |
---|
128 | n/a | self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) |
---|
129 | n/a | self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) |
---|
130 | n/a | self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") |
---|
131 | n/a | self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") |
---|
132 | n/a | |
---|
133 | n/a | self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") |
---|
134 | n/a | |
---|
135 | n/a | # Issue #7701 (crash on a pydebug build) |
---|
136 | n/a | self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') |
---|
137 | n/a | |
---|
138 | n/a | def test_crc_hqx(self): |
---|
139 | n/a | crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0) |
---|
140 | n/a | crc = binascii.crc_hqx(self.type2test(b" this string."), crc) |
---|
141 | n/a | self.assertEqual(crc, 14290) |
---|
142 | n/a | |
---|
143 | n/a | self.assertRaises(TypeError, binascii.crc_hqx) |
---|
144 | n/a | self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b'')) |
---|
145 | n/a | |
---|
146 | n/a | for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1: |
---|
147 | n/a | self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc), |
---|
148 | n/a | crc & 0xffff) |
---|
149 | n/a | |
---|
150 | n/a | def test_crc32(self): |
---|
151 | n/a | crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) |
---|
152 | n/a | crc = binascii.crc32(self.type2test(b" this string."), crc) |
---|
153 | n/a | self.assertEqual(crc, 1571220330) |
---|
154 | n/a | |
---|
155 | n/a | self.assertRaises(TypeError, binascii.crc32) |
---|
156 | n/a | |
---|
157 | n/a | def test_hqx(self): |
---|
158 | n/a | # Perform binhex4 style RLE-compression |
---|
159 | n/a | # Then calculate the hexbin4 binary-to-ASCII translation |
---|
160 | n/a | rle = binascii.rlecode_hqx(self.data) |
---|
161 | n/a | a = binascii.b2a_hqx(self.type2test(rle)) |
---|
162 | n/a | |
---|
163 | n/a | b, _ = binascii.a2b_hqx(self.type2test(a)) |
---|
164 | n/a | res = binascii.rledecode_hqx(b) |
---|
165 | n/a | self.assertEqual(res, self.rawdata) |
---|
166 | n/a | |
---|
167 | n/a | def test_rle(self): |
---|
168 | n/a | # test repetition with a repetition longer than the limit of 255 |
---|
169 | n/a | data = (b'a' * 100 + b'b' + b'c' * 300) |
---|
170 | n/a | |
---|
171 | n/a | encoded = binascii.rlecode_hqx(data) |
---|
172 | n/a | self.assertEqual(encoded, |
---|
173 | n/a | (b'a\x90d' # 'a' * 100 |
---|
174 | n/a | b'b' # 'b' |
---|
175 | n/a | b'c\x90\xff' # 'c' * 255 |
---|
176 | n/a | b'c\x90-')) # 'c' * 45 |
---|
177 | n/a | |
---|
178 | n/a | decoded = binascii.rledecode_hqx(encoded) |
---|
179 | n/a | self.assertEqual(decoded, data) |
---|
180 | n/a | |
---|
181 | n/a | def test_hex(self): |
---|
182 | n/a | # test hexlification |
---|
183 | n/a | s = b'{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
---|
184 | n/a | t = binascii.b2a_hex(self.type2test(s)) |
---|
185 | n/a | u = binascii.a2b_hex(self.type2test(t)) |
---|
186 | n/a | self.assertEqual(s, u) |
---|
187 | n/a | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1]) |
---|
188 | n/a | self.assertRaises(binascii.Error, binascii.a2b_hex, t[:-1] + b'q') |
---|
189 | n/a | |
---|
190 | n/a | # Confirm that b2a_hex == hexlify and a2b_hex == unhexlify |
---|
191 | n/a | self.assertEqual(binascii.hexlify(self.type2test(s)), t) |
---|
192 | n/a | self.assertEqual(binascii.unhexlify(self.type2test(t)), u) |
---|
193 | n/a | |
---|
194 | n/a | def test_qp(self): |
---|
195 | n/a | type2test = self.type2test |
---|
196 | n/a | a2b_qp = binascii.a2b_qp |
---|
197 | n/a | b2a_qp = binascii.b2a_qp |
---|
198 | n/a | |
---|
199 | n/a | a2b_qp(data=b"", header=False) # Keyword arguments allowed |
---|
200 | n/a | |
---|
201 | n/a | # A test for SF bug 534347 (segfaults without the proper fix) |
---|
202 | n/a | try: |
---|
203 | n/a | a2b_qp(b"", **{1:1}) |
---|
204 | n/a | except TypeError: |
---|
205 | n/a | pass |
---|
206 | n/a | else: |
---|
207 | n/a | self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
---|
208 | n/a | |
---|
209 | n/a | self.assertEqual(a2b_qp(type2test(b"=")), b"") |
---|
210 | n/a | self.assertEqual(a2b_qp(type2test(b"= ")), b"= ") |
---|
211 | n/a | self.assertEqual(a2b_qp(type2test(b"==")), b"=") |
---|
212 | n/a | self.assertEqual(a2b_qp(type2test(b"=\nAB")), b"AB") |
---|
213 | n/a | self.assertEqual(a2b_qp(type2test(b"=\r\nAB")), b"AB") |
---|
214 | n/a | self.assertEqual(a2b_qp(type2test(b"=\rAB")), b"") # ? |
---|
215 | n/a | self.assertEqual(a2b_qp(type2test(b"=\rAB\nCD")), b"CD") # ? |
---|
216 | n/a | self.assertEqual(a2b_qp(type2test(b"=AB")), b"\xab") |
---|
217 | n/a | self.assertEqual(a2b_qp(type2test(b"=ab")), b"\xab") |
---|
218 | n/a | self.assertEqual(a2b_qp(type2test(b"=AX")), b"=AX") |
---|
219 | n/a | self.assertEqual(a2b_qp(type2test(b"=XA")), b"=XA") |
---|
220 | n/a | self.assertEqual(a2b_qp(type2test(b"=AB")[:-1]), b"=A") |
---|
221 | n/a | |
---|
222 | n/a | self.assertEqual(a2b_qp(type2test(b'_')), b'_') |
---|
223 | n/a | self.assertEqual(a2b_qp(type2test(b'_'), header=True), b' ') |
---|
224 | n/a | |
---|
225 | n/a | self.assertRaises(TypeError, b2a_qp, foo="bar") |
---|
226 | n/a | self.assertEqual(a2b_qp(type2test(b"=00\r\n=00")), b"\x00\r\n\x00") |
---|
227 | n/a | self.assertEqual(b2a_qp(type2test(b"\xff\r\n\xff\n\xff")), |
---|
228 | n/a | b"=FF\r\n=FF\r\n=FF") |
---|
229 | n/a | self.assertEqual(b2a_qp(type2test(b"0"*75+b"\xff\r\n\xff\r\n\xff")), |
---|
230 | n/a | b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF") |
---|
231 | n/a | |
---|
232 | n/a | self.assertEqual(b2a_qp(type2test(b'\x7f')), b'=7F') |
---|
233 | n/a | self.assertEqual(b2a_qp(type2test(b'=')), b'=3D') |
---|
234 | n/a | |
---|
235 | n/a | self.assertEqual(b2a_qp(type2test(b'_')), b'_') |
---|
236 | n/a | self.assertEqual(b2a_qp(type2test(b'_'), header=True), b'=5F') |
---|
237 | n/a | self.assertEqual(b2a_qp(type2test(b'x y'), header=True), b'x_y') |
---|
238 | n/a | self.assertEqual(b2a_qp(type2test(b'x '), header=True), b'x=20') |
---|
239 | n/a | self.assertEqual(b2a_qp(type2test(b'x y'), header=True, quotetabs=True), |
---|
240 | n/a | b'x=20y') |
---|
241 | n/a | self.assertEqual(b2a_qp(type2test(b'x\ty'), header=True), b'x\ty') |
---|
242 | n/a | |
---|
243 | n/a | self.assertEqual(b2a_qp(type2test(b' ')), b'=20') |
---|
244 | n/a | self.assertEqual(b2a_qp(type2test(b'\t')), b'=09') |
---|
245 | n/a | self.assertEqual(b2a_qp(type2test(b' x')), b' x') |
---|
246 | n/a | self.assertEqual(b2a_qp(type2test(b'\tx')), b'\tx') |
---|
247 | n/a | self.assertEqual(b2a_qp(type2test(b' x')[:-1]), b'=20') |
---|
248 | n/a | self.assertEqual(b2a_qp(type2test(b'\tx')[:-1]), b'=09') |
---|
249 | n/a | self.assertEqual(b2a_qp(type2test(b'\0')), b'=00') |
---|
250 | n/a | |
---|
251 | n/a | self.assertEqual(b2a_qp(type2test(b'\0\n')), b'=00\n') |
---|
252 | n/a | self.assertEqual(b2a_qp(type2test(b'\0\n'), quotetabs=True), b'=00\n') |
---|
253 | n/a | |
---|
254 | n/a | self.assertEqual(b2a_qp(type2test(b'x y\tz')), b'x y\tz') |
---|
255 | n/a | self.assertEqual(b2a_qp(type2test(b'x y\tz'), quotetabs=True), |
---|
256 | n/a | b'x=20y=09z') |
---|
257 | n/a | self.assertEqual(b2a_qp(type2test(b'x y\tz'), istext=False), |
---|
258 | n/a | b'x y\tz') |
---|
259 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n')), |
---|
260 | n/a | b'x=20\ny=09\n') |
---|
261 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), quotetabs=True), |
---|
262 | n/a | b'x=20\ny=09\n') |
---|
263 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ny\t\n'), istext=False), |
---|
264 | n/a | b'x =0Ay\t=0A') |
---|
265 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r')), |
---|
266 | n/a | b'x \ry\t\r') |
---|
267 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), quotetabs=True), |
---|
268 | n/a | b'x=20\ry=09\r') |
---|
269 | n/a | self.assertEqual(b2a_qp(type2test(b'x \ry\t\r'), istext=False), |
---|
270 | n/a | b'x =0Dy\t=0D') |
---|
271 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n')), |
---|
272 | n/a | b'x=20\r\ny=09\r\n') |
---|
273 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), quotetabs=True), |
---|
274 | n/a | b'x=20\r\ny=09\r\n') |
---|
275 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\ny\t\r\n'), istext=False), |
---|
276 | n/a | b'x =0D=0Ay\t=0D=0A') |
---|
277 | n/a | |
---|
278 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1]), b'x \r') |
---|
279 | n/a | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1]), b'x\t\r') |
---|
280 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], quotetabs=True), |
---|
281 | n/a | b'x=20\r') |
---|
282 | n/a | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], quotetabs=True), |
---|
283 | n/a | b'x=09\r') |
---|
284 | n/a | self.assertEqual(b2a_qp(type2test(b'x \r\n')[:-1], istext=False), |
---|
285 | n/a | b'x =0D') |
---|
286 | n/a | self.assertEqual(b2a_qp(type2test(b'x\t\r\n')[:-1], istext=False), |
---|
287 | n/a | b'x\t=0D') |
---|
288 | n/a | |
---|
289 | n/a | self.assertEqual(b2a_qp(type2test(b'.')), b'=2E') |
---|
290 | n/a | self.assertEqual(b2a_qp(type2test(b'.\n')), b'=2E\n') |
---|
291 | n/a | self.assertEqual(b2a_qp(type2test(b'.\r')), b'=2E\r') |
---|
292 | n/a | self.assertEqual(b2a_qp(type2test(b'.\0')), b'=2E=00') |
---|
293 | n/a | self.assertEqual(b2a_qp(type2test(b'a.\n')), b'a.\n') |
---|
294 | n/a | self.assertEqual(b2a_qp(type2test(b'.a')[:-1]), b'=2E') |
---|
295 | n/a | |
---|
296 | n/a | def test_empty_string(self): |
---|
297 | n/a | # A test for SF bug #1022953. Make sure SystemError is not raised. |
---|
298 | n/a | empty = self.type2test(b'') |
---|
299 | n/a | for func in all_functions: |
---|
300 | n/a | if func == 'crc_hqx': |
---|
301 | n/a | # crc_hqx needs 2 arguments |
---|
302 | n/a | binascii.crc_hqx(empty, 0) |
---|
303 | n/a | continue |
---|
304 | n/a | f = getattr(binascii, func) |
---|
305 | n/a | try: |
---|
306 | n/a | f(empty) |
---|
307 | n/a | except Exception as err: |
---|
308 | n/a | self.fail("{}({!r}) raises {!r}".format(func, empty, err)) |
---|
309 | n/a | |
---|
310 | n/a | def test_unicode_b2a(self): |
---|
311 | n/a | # Unicode strings are not accepted by b2a_* functions. |
---|
312 | n/a | for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}: |
---|
313 | n/a | try: |
---|
314 | n/a | self.assertRaises(TypeError, getattr(binascii, func), "test") |
---|
315 | n/a | except Exception as err: |
---|
316 | n/a | self.fail('{}("test") raises {!r}'.format(func, err)) |
---|
317 | n/a | # crc_hqx needs 2 arguments |
---|
318 | n/a | self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) |
---|
319 | n/a | |
---|
320 | n/a | def test_unicode_a2b(self): |
---|
321 | n/a | # Unicode strings are accepted by a2b_* functions. |
---|
322 | n/a | MAX_ALL = 45 |
---|
323 | n/a | raw = self.rawdata[:MAX_ALL] |
---|
324 | n/a | for fa, fb in zip(a2b_functions, b2a_functions): |
---|
325 | n/a | if fa == 'rledecode_hqx': |
---|
326 | n/a | # Takes non-ASCII data |
---|
327 | n/a | continue |
---|
328 | n/a | a2b = getattr(binascii, fa) |
---|
329 | n/a | b2a = getattr(binascii, fb) |
---|
330 | n/a | try: |
---|
331 | n/a | a = b2a(self.type2test(raw)) |
---|
332 | n/a | binary_res = a2b(a) |
---|
333 | n/a | a = a.decode('ascii') |
---|
334 | n/a | res = a2b(a) |
---|
335 | n/a | except Exception as err: |
---|
336 | n/a | self.fail("{}/{} conversion raises {!r}".format(fb, fa, err)) |
---|
337 | n/a | if fb == 'b2a_hqx': |
---|
338 | n/a | # b2a_hqx returns a tuple |
---|
339 | n/a | res, _ = res |
---|
340 | n/a | binary_res, _ = binary_res |
---|
341 | n/a | self.assertEqual(res, raw, "{}/{} conversion: " |
---|
342 | n/a | "{!r} != {!r}".format(fb, fa, res, raw)) |
---|
343 | n/a | self.assertEqual(res, binary_res) |
---|
344 | n/a | self.assertIsInstance(res, bytes) |
---|
345 | n/a | # non-ASCII string |
---|
346 | n/a | self.assertRaises(ValueError, a2b, "\x80") |
---|
347 | n/a | |
---|
348 | n/a | def test_b2a_base64_newline(self): |
---|
349 | n/a | # Issue #25357: test newline parameter |
---|
350 | n/a | b = self.type2test(b'hello') |
---|
351 | n/a | self.assertEqual(binascii.b2a_base64(b), |
---|
352 | n/a | b'aGVsbG8=\n') |
---|
353 | n/a | self.assertEqual(binascii.b2a_base64(b, newline=True), |
---|
354 | n/a | b'aGVsbG8=\n') |
---|
355 | n/a | self.assertEqual(binascii.b2a_base64(b, newline=False), |
---|
356 | n/a | b'aGVsbG8=') |
---|
357 | n/a | |
---|
358 | n/a | |
---|
359 | n/a | class ArrayBinASCIITest(BinASCIITest): |
---|
360 | n/a | def type2test(self, s): |
---|
361 | n/a | return array.array('B', list(s)) |
---|
362 | n/a | |
---|
363 | n/a | |
---|
364 | n/a | class BytearrayBinASCIITest(BinASCIITest): |
---|
365 | n/a | type2test = bytearray |
---|
366 | n/a | |
---|
367 | n/a | |
---|
368 | n/a | class MemoryviewBinASCIITest(BinASCIITest): |
---|
369 | n/a | type2test = memoryview |
---|
370 | n/a | |
---|
371 | n/a | |
---|
372 | n/a | if __name__ == "__main__": |
---|
373 | n/a | unittest.main() |
---|