Debian Codegraph @

pypy/module/binascii/test/test_binascii.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 class AppTestBinascii(object):
4 spaceconfig = dict(usemodules=['binascii'])
5
6 def setup_class(cls):
7 """Make binascii module available as self.binascii."""
8 cls.w_binascii = cls.space.getbuiltinmodule('binascii')
9
10 def test_a2b_uu(self):
11 # obscure case, for compability with CPython
12 assert self.binascii.a2b_uu(b"") == b"\x00" * 0x20
13 #
14 for input, expected in [
15 (b"!,_", b"3"),
16 (b" ", b""),
17 (b"`", b""),
18 (b"!", b"\x00"),
19 (b"!6", b"X"),
20 (b'"6', b"X\x00"),
21 (b'"W', b"\xdc\x00"),
22 (b'"WA', b"\xde\x10"),
23 (b'"WAX', b"\xde\x1e"),
24 (b'#WAX', b"\xde\x1e\x00"),
25 (b'#WAXR', b"\xde\x1e2"),
26 (b'$WAXR', b"\xde\x1e2\x00"),
27 (b'$WAXR6', b"\xde\x1e2X"),
28 (b'%WAXR6U', b"\xde\x1e2[P"),
29 (b'&WAXR6UB', b"\xde\x1e2[X\x80"),
30 (b"'WAXR6UBA3", b"\xde\x1e2[X\xa1L"),
31 (b'(WAXR6UBA3#', b"\xde\x1e2[X\xa1L0"),
32 (b')WAXR6UBA3#Q', b"\xde\x1e2[X\xa1L<@"),
33 (b'*WAXR6UBA3#Q!5', b"\xde\x1e2[X\xa1L<AT"),
34 (b'!,_', b'\x33'),
35 (b'$ $" P ', b'\x00\x01\x02\x03'),
36 (b'$``$"`P``', b'\x00\x01\x02\x03'),
37 ]:
38 assert self.binascii.a2b_uu(input) == expected
39 assert self.binascii.a2b_uu(input + b' ') == expected
40 assert self.binascii.a2b_uu(input + b' ') == expected
41 assert self.binascii.a2b_uu(input + b' ') == expected
42 assert self.binascii.a2b_uu(input + b' ') == expected
43 assert self.binascii.a2b_uu(input + b'`') == expected
44 assert self.binascii.a2b_uu(input + b'``') == expected
45 assert self.binascii.a2b_uu(input + b'```') == expected
46 assert self.binascii.a2b_uu(input + b'````') == expected
47 assert self.binascii.a2b_uu(input + b'\n') == expected
48 assert self.binascii.a2b_uu(input + b'\r\n') == expected
49 assert self.binascii.a2b_uu(input + b' \r\n') == expected
50 assert self.binascii.a2b_uu(input + b' \r\n') == expected
51 #
52 for bogus in [
53 b"!w",
54 b"! w",
55 b"! w",
56 b"! w",
57 b"! w",
58 b"! w",
59 b"! w",
60 b"#a",
61 b'"WAXR',
62 ]:
63 raises(self.binascii.Error, self.binascii.a2b_uu, bogus)
64 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b' ')
65 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b' ')
66 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b' ')
67 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b' ')
68 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b'\n')
69 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b'\r\n')
70 raises(self.binascii.Error, self.binascii.a2b_uu, bogus + b' \r\n')
71 #
72 assert self.binascii.a2b_uu(u"!6") == b"X"
73 raises(ValueError, self.binascii.a2b_uu, u"caf\xe9")
74
75 def test_b2a_uu(self):
76 for input, expected in [
77 (b"", b" "),
78 (b"\x00", b"! "),
79 (b"X", b"!6 "),
80 (b"X\x00", b'"6 '),
81 (b"\xdc\x00", b'"W '),
82 (b"\xde\x10", b'"WA '),
83 (b"\xde\x1e", b'"WAX '),
84 (b"\xde\x1e\x00", b'#WAX '),
85 (b"\xde\x1e2", b'#WAXR'),
86 (b"\xde\x1e2\x00", b'$WAXR '),
87 (b"\xde\x1e2X", b'$WAXR6 '),
88 (b"\xde\x1e2[P", b'%WAXR6U '),
89 (b"\xde\x1e2[X\x80", b'&WAXR6UB '),
90 (b"\xde\x1e2[X\xa1L", b"'WAXR6UBA3 "),
91 (b"\xde\x1e2[X\xa1L0", b'(WAXR6UBA3# '),
92 (b"\xde\x1e2[X\xa1L<@", b')WAXR6UBA3#Q '),
93 (b"\xde\x1e2[X\xa1L<AT", b'*WAXR6UBA3#Q!5 '),
94 ]:
95 for backtick in [None, True, False]:
96 if backtick is None:
97 assert self.binascii.b2a_uu(input) == expected + b'\n'
98 else:
99 really_expected = expected.replace(b' ', b'`') if backtick else expected
100 assert self.binascii.b2a_uu(input, backtick=backtick) == really_expected + b'\n'
101
102 def test_a2b_base64(self):
103 for input, expected in [
104 (b"", b""),
105 (b"\n", b""),
106 (b"Yg==\n", b"b"),
107 (b"Y g = \n = \r", b"b"), # random spaces
108 (b"Y\x80g\xff=\xc4=", b"b"), # random junk chars, >= 0x80
109 (b"abcd", b"i\xb7\x1d"),
110 (b"abcdef==", b"i\xb7\x1dy"),
111 (b"abcdefg=", b"i\xb7\x1dy\xf8"),
112 (b"abcdefgh", b"i\xb7\x1dy\xf8!"),
113 (b"abcdef==FINISHED", b"i\xb7\x1dy"),
114 (b"abcdef= \n =FINISHED", b"i\xb7\x1dy"),
115 (b"abcdefg=FINISHED", b"i\xb7\x1dy\xf8"),
116 (b"abcd=efgh", b"i\xb7\x1dy\xf8!"),
117 (b"abcde=fgh", b"i\xb7\x1dy\xf8!"),
118 (b"abcdef=gh", b"i\xb7\x1dy\xf8!"),
119 ]:
120 assert self.binascii.a2b_base64(input) == expected
121 #
122 for bogus in [
123 b"abcde",
124 b"abcde=",
125 b"abcde==",
126 b"abcde===",
127 b"abcdef",
128 b"abcdef=",
129 b"abcdefg",
130 ]:
131 raises(self.binascii.Error, self.binascii.a2b_base64, bogus)
132 #
133 assert self.binascii.a2b_base64(u"Yg==\n") == b"b"
134 raises(ValueError, self.binascii.a2b_base64, u"caf\xe9")
135
136 def test_b2a_base64(self):
137 for newline in (True, False, None):
138 for input, expected in [
139 (b"", b""),
140 (b"b", b"Yg=="),
141 (b"i\xb7\x1d", b"abcd"),
142 (b"i\xb7\x1dy", b"abcdeQ=="),
143 (b"i\xb7\x1dy\xf8", b"abcdefg="),
144 (b"i\xb7\x1dy\xf8!", b"abcdefgh"),
145 (b"i\xb7\x1d" * 345, b"abcd" * 345),
146 ]:
147 kwargs = {}
148 if isinstance(newline, bool):
149 kwargs['newline'] = newline
150 if newline is not False:
151 expected += b'\n'
152 assert self.binascii.b2a_base64(input, **kwargs) == expected
153
154 def test_a2b_qp(self):
155 for input, expected in [
156 # these are the tests from CPython 2.7
157 (b"= ", b"= "),
158 (b"==", b"="),
159 (b"=AX", b"=AX"),
160 (b"=00\r\n=00", b"\x00\r\n\x00"),
161 # more tests follow
162 (b"=", b""),
163 (b"abc=", b"abc"),
164 (b"ab=\ncd", b"abcd"),
165 (b"ab=\r\ncd", b"abcd"),
166 (''.join(["=%02x" % n for n in range(256)]).encode(),
167 bytes(range(256))),
168 (''.join(["=%02X" % n for n in range(256)]).encode(),
169 bytes(range(256))),
170 ]:
171 assert self.binascii.a2b_qp(input) == expected
172 #
173 for input, expected in [
174 (b"xyz", b"xyz"),
175 (b"__", b" "),
176 (b"a_b", b"a b"),
177 ]:
178 assert self.binascii.a2b_qp(input, header=True) == expected
179 #
180 assert self.binascii.a2b_qp(u"a_b", header=True) == b"a b"
181 raises(ValueError, self.binascii.a2b_qp, u"caf\xe9")
182
183 def test_b2a_qp(self):
184 for input, flags, expected in [
185 # these are the tests from CPython 2.7
186 (b"\xff\r\n\xff\n\xff", {}, b"=FF\r\n=FF\r\n=FF"),
187 (b"0"*75+b"\xff\r\n\xff\r\n\xff",{},b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF"),
188 (b'\0\n', {}, b'=00\n'),
189 (b'\0\n', {'quotetabs': True}, b'=00\n'),
190 (b'foo\tbar\t\n', {}, b'foo\tbar=09\n'),
191 (b'foo\tbar\t\n', {'quotetabs': True}, b'foo=09bar=09\n'),
192 (b'.', {}, b'=2E'),
193 (b'.\n', {}, b'=2E\n'),
194 (b'a.\n', {}, b'a.\n'),
195 # more tests follow
196 (b'_', {}, b'_'),
197 (b'_', {'header': True}, b'=5F'),
198 (b'.x', {}, b'.x'),
199 (b'.\r\nn', {}, b'=2E\r\nn'),
200 (b'\nn', {}, b'\nn'),
201 (b'\r\nn', {}, b'\r\nn'),
202 (b'\nn', {'istext': False}, b'=0An'),
203 (b'\r\nn', {'istext': False}, b'=0D=0An'),
204 (b' ', {}, b'=20'),
205 (b'\t', {}, b'=09'),
206 (b' x', {}, b' x'),
207 (b'\tx', {}, b'\tx'),
208 (b'\x16x', {}, b'=16x'),
209 (b' x', {'quotetabs': True}, b'=20x'),
210 (b'\tx', {'quotetabs': True}, b'=09x'),
211 (b' \nn', {}, b'=20\nn'),
212 (b'\t\nn', {}, b'=09\nn'),
213 (b'x\nn', {}, b'x\nn'),
214 (b' \r\nn', {}, b'=20\r\nn'),
215 (b'\t\r\nn', {}, b'=09\r\nn'),
216 (b'x\r\nn', {}, b'x\r\nn'),
217 (b'x\nn', {'istext': False}, b'x=0An'),
218 (b' ', {}, b' =20'),
219 (b' ', {'header': True}, b'__=20'),
220 (b' \nn', {}, b' =20\nn'),
221 (b' \nn', {'header': True}, b'___\nn'),
222 (b' ', {}, b' =20'),
223 (b'\t\t\t', {'header': True}, b'\t\t=09'),
224 (b'\t\t\t\nn', {}, b'\t\t=09\nn'),
225 (b'\t\t\t\nn', {'header': True}, b'\t\t=09\nn'),
226 ]:
227 assert self.binascii.b2a_qp(input, **flags) == expected
228
229 def test_a2b_hqx(self):
230 for input, expected, done in [
231 (b"", b"", 0),
232 (b"AAAA", b"]u\xd7", 0),
233 (b"A\nA\rAA", b"]u\xd7", 0),
234 (b":", b"", 1),
235 (b"A:", b"", 1),
236 (b"AA:", b"]", 1),
237 (b"AAA:", b"]u", 1),
238 (b"AAAA:", b"]u\xd7", 1),
239 (b"AAAA:foobarbaz", b"]u\xd7", 1),
240 (b"41-CZ:", b"D\xe3\x19", 1),
241 (b"41-CZl:", b"D\xe3\x19\xbb", 1),
242 (b"41-CZlm:", b"D\xe3\x19\xbb\xbf", 1),
243 (b"41-CZlm@:", b"D\xe3\x19\xbb\xbf\x16", 1),
244 ]:
245 assert self.binascii.a2b_hqx(input) == (expected, done)
246 #
247 for incomplete in [
248 b"A",
249 b"AA",
250 b"AAA",
251 b"12345",
252 b"123456",
253 b"1234560",
254 ]:
255 raises(self.binascii.Incomplete, self.binascii.a2b_hqx, incomplete)
256 #
257 for bogus in [
258 b"\x00",
259 b".",
260 b"AAA AAAAAA:",
261 ]:
262 raises(self.binascii.Error, self.binascii.a2b_hqx, bogus)
263 #
264 assert self.binascii.a2b_hqx("AAA:") == (b"]u", 1)
265 raises(ValueError, self.binascii.a2b_hqx, u"caf\xe9")
266
267 def test_b2a_hqx(self):
268 for input, expected in [
269 (b"", b""),
270 (b"A", b"33"),
271 (b"AB", b"38)"),
272 (b"ABC", b"38*$"),
273 (b"ABCD", b"38*$4!"),
274 (b"ABCDE", b"38*$4%8"),
275 (b"ABCDEF", b"38*$4%9'"),
276 (b"ABCDEFG", b"38*$4%9'4`"),
277 (b"]u\xd7", b"AAAA"),
278 ]:
279 assert self.binascii.b2a_hqx(input) == expected
280
281 def test_rledecode_hqx(self):
282 for input, expected in [
283 (b"", b""),
284 (b"hello world", b"hello world"),
285 (b"\x90\x00", b"\x90"),
286 (b"a\x90\x05", b"a" * 5),
287 (b"a\x90\xff", b"a" * 0xFF),
288 (b"abc\x90\x01def", b"abcdef"),
289 (b"abc\x90\x02def", b"abccdef"),
290 (b"abc\x90\x03def", b"abcccdef"),
291 (b"abc\x90\xa1def", b"ab" + b"c" * 0xA1 + b"def"),
292 (b"abc\x90\x03\x90\x02def", b"abccccdef"),
293 (b"abc\x90\x00\x90\x03def", b"abc\x90\x90\x90def"),
294 (b"abc\x90\x03\x90\x00def", b"abccc\x90def"),
295 ]:
296 assert self.binascii.rledecode_hqx(input) == expected
297 #
298 for input in [
299 b"\x90",
300 b"a\x90",
301 b"hello world\x90",
302 ]:
303 raises(self.binascii.Incomplete, self.binascii.rledecode_hqx,
304 input)
305 #
306 raises(self.binascii.Error, self.binascii.rledecode_hqx, b"\x90\x01")
307 raises(self.binascii.Error, self.binascii.rledecode_hqx, b"\x90\x02")
308 raises(self.binascii.Error, self.binascii.rledecode_hqx, b"\x90\xff")
309
310 def test_rlecode_hqx(self):
311 for input, expected in [
312 (b"", b""),
313 (b"hello world", b"hello world"),
314 (b"helllo world", b"helllo world"),
315 (b"hellllo world", b"hel\x90\x04o world"),
316 (b"helllllo world", b"hel\x90\x05o world"),
317 (b"aaa", b"aaa"),
318 (b"aaaa", b"a\x90\x04"),
319 (b"a" * 0xff, b"a\x90\xff"),
320 (b"a" * 0x100, b"a\x90\xffa"),
321 (b"a" * 0x101, b"a\x90\xffaa"),
322 (b"a" * 0x102, b"a\x90\xffaaa"), # see comments in the source
323 (b"a" * 0x103, b"a\x90\xffa\x90\x04"),
324 (b"a" * 0x1fe, b"a\x90\xffa\x90\xff"),
325 (b"a" * 0x1ff, b"a\x90\xffa\x90\xffa"),
326 (b"\x90", b"\x90\x00"),
327 (b"\x90" * 2, b"\x90\x00" * 2),
328 (b"\x90" * 3, b"\x90\x00" * 3), # see comments in the source
329 (b"\x90" * 345, b"\x90\x00" * 345),
330 ]:
331 assert self.binascii.rlecode_hqx(input) == expected
332
333 def test_crc_hqx(self):
334 for input, initial, expected in [
335 (b"", 0, 0),
336 (b'', 0x12345, 0x2345),
337 (b"", 123, 123),
338 (b"hello", 321, 28955),
339 (b"world", 65535, 12911),
340 (b"uh", 40102, 37544),
341 (b'a', 10000, 14338),
342 (b'b', 10000, 2145),
343 (b'c', 10000, 6208),
344 (b'd', 10000, 26791),
345 (b'e', 10000, 30854),
346 (b'f', 10000, 18661),
347 (b'g', 10000, 22724),
348 (b'h', 10000, 43307),
349 (b'i', 10000, 47370),
350 (b'j', 10000, 35177),
351 (b'k', 10000, 39240),
352 (b'l', 10000, 59823),
353 (b'm', 10000, 63886),
354 (b'n', 10000, 51693),
355 (b'o', 10000, 55756),
356 (b'p', 10000, 14866),
357 (b'q', 10000, 10803),
358 (b'r', 10000, 6736),
359 (b's', 10000, 2673),
360 (b't', 10000, 31382),
361 (b'u', 10000, 27319),
362 (b'v', 10000, 23252),
363 (b'w', 10000, 19189),
364 (b'x', 10000, 47898),
365 (b'y', 10000, 43835),
366 (b'z', 10000, 39768),
367 (b'', -1, 65535)
368 ]:
369 assert self.binascii.crc_hqx(input, initial) == expected
370
371 def test_crc_hqx_ovf_bug(self):
372 import sys
373 if sys.maxsize == 2 ** 32 - 1:
374 big = 2 ** 32
375 else:
376 big = 2 ** 63
377 assert self.binascii.crc_hqx(b'', big) == 0
378 assert self.binascii.crc_hqx(b'', -big) == 0
379
380 def test_crc32(self):
381 for input, initial, expected in [
382 (b"", 0, 0),
383 (b"", 123, 123),
384 (b"hello", 321, 3946819610),
385 (b"world", -2147483648, 32803080),
386 (b"world", 2147483647, 942244330),
387 (b'a', 10000, 4110462464),
388 (b'b', 10000, 1812594618),
389 (b'c', 10000, 453955372),
390 (b'd', 10000, 2238339727),
391 (b'e', 10000, 4067256857),
392 (b'f', 10000, 1801730979),
393 (b'g', 10000, 476252981),
394 (b'h', 10000, 2363233956),
395 (b'i', 10000, 4225443378),
396 (b'j', 10000, 1657960328),
397 (b'k', 10000, 366298910),
398 (b'l', 10000, 2343686845),
399 (b'm', 10000, 4239843883),
400 (b'n', 10000, 1707062161),
401 (b'o', 10000, 314082055),
402 (b'p', 10000, 2679148274),
403 (b'q', 10000, 3904355940),
404 (b'r', 10000, 1908338654),
405 (b's', 10000, 112844616),
406 (b't', 10000, 2564639467),
407 (b'u', 10000, 4024072829),
408 (b'v', 10000, 1993550791),
409 (b'w', 10000, 30677841),
410 (b'x', 10000, 2439710400),
411 (b'y', 10000, 3865851478),
412 (b'z', 10000, 2137352172),
413 (b'foo', 99999999999999999999999999, 2362262480),
414 (b'bar', -99999999999999999999999999, 2000545409),
415 ]:
416 assert self.binascii.crc32(input, initial) == expected
417
418 def test_hexlify(self):
419 for input, expected in [
420 (b"", b""),
421 (b"0", b"30"),
422 (b"1", b"31"),
423 (b"2", b"32"),
424 (b"8", b"38"),
425 (b"9", b"39"),
426 (b"A", b"41"),
427 (b"O", b"4f"),
428 (b"\xde", b"de"),
429 (b"ABC", b"414243"),
430 (b"\x00\x00\x00\xff\x00\x00", b"000000ff0000"),
431 (b"\x28\x9c\xc8\xc0\x3d\x8e", b"289cc8c03d8e"),
432 ]:
433 print(input, expected, self.binascii.hexlify(input))
434 assert self.binascii.hexlify(input) == expected
435 assert self.binascii.b2a_hex(input) == expected
436
437 def test_hexlify_sep(self):
438 res = self.binascii.hexlify(bytes([0x73,0x61,0x6e,0x74,0x61,0x20,0x63,0x6c,0x61,0x75,0x73]), '.')
439 assert res == b"73.61.6e.74.61.20.63.6c.61.75.73"
440 with raises(ValueError):
441 self.binascii.hexlify(bytes([1, 2, 3]), b"abc")
442 return
443 assert self.binascii.hexlify(bytes([0x73,0x61,0x6e,0x74,0x61,0x20,0x63,0x6c,0x61,0x75,0x73]), '?', 4) == \
444 b"73616e?74612063?6c617573"
445 assert self.binascii.hexlify(bytes([0x73,0x61,0x6e,0x74,0x61,0x20,0x63,0x6c,0x61,0x75,0x73]), '?', -4) == \
446 b"73616e74?6120636c?617573"
447 with raises(ValueError) as excinfo:
448 self.binascii.hexlify(bytes([1, 2, 3]), "ä")
449 assert "ASCII" in str(excinfo.value)
450 with raises(TypeError):
451 self.binascii.hexlify(bytes(), None, 1)
452
453 def test_unhexlify(self):
454 for input, expected in [
455 (b"", b""),
456 (b"30", b"0"),
457 (b"31", b"1"),
458 (b"32", b"2"),
459 (b"38", b"8"),
460 (b"39", b"9"),
461 (b"41", b"A"),
462 (b"4F", b"O"),
463 (b"4f", b"O"),
464 (b"DE", b"\xde"),
465 (b"De", b"\xde"),
466 (b"dE", b"\xde"),
467 (b"de", b"\xde"),
468 (b"414243", b"ABC"),
469 (b"000000FF0000", b"\x00\x00\x00\xff\x00\x00"),
470 (b"289cc8C03d8e", b"\x28\x9c\xc8\xc0\x3d\x8e"),
471 ]:
472 assert self.binascii.unhexlify(input) == expected
473 assert self.binascii.a2b_hex(input) == expected
474 assert self.binascii.unhexlify(input.decode('ascii')) == expected
475 assert self.binascii.a2b_hex(input.decode('ascii')) == expected
476 raises(ValueError, self.binascii.a2b_hex, u"caf\xe9")
477
478 def test_errors(self):
479 binascii = self.binascii
480 assert issubclass(binascii.Error, ValueError)
481 raises(binascii.Error, binascii.a2b_hex, b'u')
482 raises(binascii.Error, binascii.a2b_hex, b'bo')
483
484 def test_deprecated(self):
485 import warnings
486 with warnings.catch_warnings(record=True) as w:
487 warnings.simplefilter('always')
488 self.binascii.b2a_hqx(b'abc')
489 assert len(w) == 1
490