/pypy/module/binascii/test/test_binascii.py

https://bitbucket.org/dac_io/pypy · Python · 420 lines · 386 code · 17 blank · 17 comment · 22 complexity · 48ee827698ad4144eb01ef8dedf8d492 MD5 · raw file

  1. from pypy.conftest import gettestobjspace
  2. class AppTestBinascii(object):
  3. def setup_class(cls):
  4. """
  5. Create a space with the binascii module and import it for use by the
  6. tests.
  7. """
  8. cls.space = gettestobjspace(usemodules=['binascii'])
  9. cls.w_binascii = cls.space.appexec([], """():
  10. import binascii
  11. return binascii
  12. """)
  13. def test_a2b_uu(self):
  14. # obscure case, for compability with CPython
  15. assert self.binascii.a2b_uu("") == "\x00" * 0x20
  16. #
  17. for input, expected in [
  18. (" ", ""),
  19. ("!", "\x00"),
  20. ("!6", "X"),
  21. ('"6', "X\x00"),
  22. ('"W', "\xdc\x00"),
  23. ('"WA', "\xde\x10"),
  24. ('"WAX', "\xde\x1e"),
  25. ('#WAX', "\xde\x1e\x00"),
  26. ('#WAXR', "\xde\x1e2"),
  27. ('$WAXR', "\xde\x1e2\x00"),
  28. ('$WAXR6', "\xde\x1e2X"),
  29. ('%WAXR6U', "\xde\x1e2[P"),
  30. ('&WAXR6UB', "\xde\x1e2[X\x80"),
  31. ("'WAXR6UBA3", "\xde\x1e2[X\xa1L"),
  32. ('(WAXR6UBA3#', "\xde\x1e2[X\xa1L0"),
  33. (')WAXR6UBA3#Q', "\xde\x1e2[X\xa1L<@"),
  34. ('*WAXR6UBA3#Q!5', "\xde\x1e2[X\xa1L<AT"),
  35. ]:
  36. assert self.binascii.a2b_uu(input) == expected
  37. assert self.binascii.a2b_uu(input + ' ') == expected
  38. assert self.binascii.a2b_uu(input + ' ') == expected
  39. assert self.binascii.a2b_uu(input + ' ') == expected
  40. assert self.binascii.a2b_uu(input + ' ') == expected
  41. assert self.binascii.a2b_uu(input + '\n') == expected
  42. assert self.binascii.a2b_uu(input + '\r\n') == expected
  43. assert self.binascii.a2b_uu(input + ' \r\n') == expected
  44. assert self.binascii.a2b_uu(input + ' \r\n') == expected
  45. #
  46. for bogus in [
  47. "!w",
  48. "! w",
  49. "! w",
  50. "! w",
  51. "! w",
  52. "! w",
  53. "! w",
  54. "#a",
  55. '"WAXR',
  56. ]:
  57. raises(self.binascii.Error, self.binascii.a2b_uu, bogus)
  58. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' ')
  59. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' ')
  60. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' ')
  61. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' ')
  62. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '\n')
  63. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + '\r\n')
  64. raises(self.binascii.Error, self.binascii.a2b_uu, bogus + ' \r\n')
  65. def test_b2a_uu(self):
  66. for input, expected in [
  67. ("", " "),
  68. ("\x00", "! "),
  69. ("X", "!6 "),
  70. ("X\x00", '"6 '),
  71. ("\xdc\x00", '"W '),
  72. ("\xde\x10", '"WA '),
  73. ("\xde\x1e", '"WAX '),
  74. ("\xde\x1e\x00", '#WAX '),
  75. ("\xde\x1e2", '#WAXR'),
  76. ("\xde\x1e2\x00", '$WAXR '),
  77. ("\xde\x1e2X", '$WAXR6 '),
  78. ("\xde\x1e2[P", '%WAXR6U '),
  79. ("\xde\x1e2[X\x80", '&WAXR6UB '),
  80. ("\xde\x1e2[X\xa1L", "'WAXR6UBA3 "),
  81. ("\xde\x1e2[X\xa1L0", '(WAXR6UBA3# '),
  82. ("\xde\x1e2[X\xa1L<@", ')WAXR6UBA3#Q '),
  83. ("\xde\x1e2[X\xa1L<AT", '*WAXR6UBA3#Q!5 '),
  84. ]:
  85. assert self.binascii.b2a_uu(input) == expected + '\n'
  86. def test_a2b_base64(self):
  87. for input, expected in [
  88. ("", ""),
  89. ("\n", ""),
  90. ("Yg==\n", "b"),
  91. ("Y g = \n = \r", "b"), # random spaces
  92. ("Y\x80g\xff=\xc4=", "b"), # random junk chars, >= 0x80
  93. ("abcd", "i\xb7\x1d"),
  94. ("abcdef==", "i\xb7\x1dy"),
  95. ("abcdefg=", "i\xb7\x1dy\xf8"),
  96. ("abcdefgh", "i\xb7\x1dy\xf8!"),
  97. ("abcdef==FINISHED", "i\xb7\x1dy"),
  98. ("abcdef= \n =FINISHED", "i\xb7\x1dy"),
  99. ("abcdefg=FINISHED", "i\xb7\x1dy\xf8"),
  100. ("abcd=efgh", "i\xb7\x1dy\xf8!"),
  101. ("abcde=fgh", "i\xb7\x1dy\xf8!"),
  102. ("abcdef=gh", "i\xb7\x1dy\xf8!"),
  103. ]:
  104. assert self.binascii.a2b_base64(input) == expected
  105. #
  106. for bogus in [
  107. "abcde",
  108. "abcde=",
  109. "abcde==",
  110. "abcde===",
  111. "abcdef",
  112. "abcdef=",
  113. "abcdefg",
  114. ]:
  115. raises(self.binascii.Error, self.binascii.a2b_base64, bogus)
  116. def test_b2a_base64(self):
  117. for input, expected in [
  118. ("", ""),
  119. ("b", "Yg=="),
  120. ("i\xb7\x1d", "abcd"),
  121. ("i\xb7\x1dy", "abcdeQ=="),
  122. ("i\xb7\x1dy\xf8", "abcdefg="),
  123. ("i\xb7\x1dy\xf8!", "abcdefgh"),
  124. ("i\xb7\x1d" * 345, "abcd" * 345),
  125. ]:
  126. assert self.binascii.b2a_base64(input) == expected + '\n'
  127. def test_a2b_qp(self):
  128. for input, expected in [
  129. # these are the tests from CPython 2.7
  130. ("= ", "= "),
  131. ("==", "="),
  132. ("=AX", "=AX"),
  133. ("=00\r\n=00", "\x00\r\n\x00"),
  134. # more tests follow
  135. ("=", ""),
  136. ("abc=", "abc"),
  137. ("ab=\ncd", "abcd"),
  138. ("ab=\r\ncd", "abcd"),
  139. (''.join(["=%02x" % n for n in range(256)]),
  140. ''.join(map(chr, range(256)))),
  141. (''.join(["=%02X" % n for n in range(256)]),
  142. ''.join(map(chr, range(256)))),
  143. ]:
  144. assert self.binascii.a2b_qp(input) == expected
  145. #
  146. for input, expected in [
  147. ("xyz", "xyz"),
  148. ("__", " "),
  149. ("a_b", "a b"),
  150. ]:
  151. assert self.binascii.a2b_qp(input, header=True) == expected
  152. def test_b2a_qp(self):
  153. for input, flags, expected in [
  154. # these are the tests from CPython 2.7
  155. ("\xff\r\n\xff\n\xff", {}, "=FF\r\n=FF\r\n=FF"),
  156. ("0"*75+"\xff\r\n\xff\r\n\xff",{},"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"),
  157. ('\0\n', {}, '=00\n'),
  158. ('\0\n', {'quotetabs': True}, '=00\n'),
  159. ('foo\tbar\t\n', {}, 'foo\tbar=09\n'),
  160. ('foo\tbar\t\n', {'quotetabs': True}, 'foo=09bar=09\n'),
  161. ('.', {}, '=2E'),
  162. ('.\n', {}, '=2E\n'),
  163. ('a.\n', {}, 'a.\n'),
  164. # more tests follow
  165. ('_', {}, '_'),
  166. ('_', {'header': True}, '=5F'),
  167. ('.x', {}, '.x'),
  168. ('.\r\nn', {}, '=2E\r\nn'),
  169. ('\nn', {}, '\nn'),
  170. ('\r\nn', {}, '\r\nn'),
  171. ('\nn', {'istext': False}, '=0An'),
  172. ('\r\nn', {'istext': False}, '=0D=0An'),
  173. (' ', {}, '=20'),
  174. ('\t', {}, '=09'),
  175. (' x', {}, ' x'),
  176. ('\tx', {}, '\tx'),
  177. ('\x16x', {}, '=16x'),
  178. (' x', {'quotetabs': True}, '=20x'),
  179. ('\tx', {'quotetabs': True}, '=09x'),
  180. (' \nn', {}, '=20\nn'),
  181. ('\t\nn', {}, '=09\nn'),
  182. ('x\nn', {}, 'x\nn'),
  183. (' \r\nn', {}, '=20\r\nn'),
  184. ('\t\r\nn', {}, '=09\r\nn'),
  185. ('x\r\nn', {}, 'x\r\nn'),
  186. ('x\nn', {'istext': False}, 'x=0An'),
  187. (' ', {}, ' =20'),
  188. (' ', {'header': True}, '__=20'),
  189. (' \nn', {}, ' =20\nn'),
  190. (' \nn', {'header': True}, '___\nn'),
  191. (' ', {}, ' =20'),
  192. ('\t\t\t', {'header': True}, '\t\t=09'),
  193. ('\t\t\t\nn', {}, '\t\t=09\nn'),
  194. ('\t\t\t\nn', {'header': True}, '\t\t=09\nn'),
  195. ]:
  196. assert self.binascii.b2a_qp(input, **flags) == expected
  197. def test_a2b_hqx(self):
  198. for input, expected, done in [
  199. ("", "", 0),
  200. ("AAAA", "]u\xd7", 0),
  201. ("A\nA\rAA", "]u\xd7", 0),
  202. (":", "", 1),
  203. ("A:", "", 1),
  204. ("AA:", "]", 1),
  205. ("AAA:", "]u", 1),
  206. ("AAAA:", "]u\xd7", 1),
  207. ("AAAA:foobarbaz", "]u\xd7", 1),
  208. ("41-CZ:", "D\xe3\x19", 1),
  209. ("41-CZl:", "D\xe3\x19\xbb", 1),
  210. ("41-CZlm:", "D\xe3\x19\xbb\xbf", 1),
  211. ("41-CZlm@:", "D\xe3\x19\xbb\xbf\x16", 1),
  212. ]:
  213. assert self.binascii.a2b_hqx(input) == (expected, done)
  214. #
  215. for incomplete in [
  216. "A",
  217. "AA",
  218. "AAA",
  219. "12345",
  220. "123456",
  221. "1234560",
  222. ]:
  223. raises(self.binascii.Incomplete, self.binascii.a2b_hqx, incomplete)
  224. #
  225. for bogus in [
  226. "\x00",
  227. ".",
  228. "AAA AAAAAA:",
  229. ]:
  230. raises(self.binascii.Error, self.binascii.a2b_hqx, bogus)
  231. def test_b2a_hqx(self):
  232. for input, expected in [
  233. ("", ""),
  234. ("A", "33"),
  235. ("AB", "38)"),
  236. ("ABC", "38*$"),
  237. ("ABCD", "38*$4!"),
  238. ("ABCDE", "38*$4%8"),
  239. ("ABCDEF", "38*$4%9'"),
  240. ("ABCDEFG", "38*$4%9'4`"),
  241. ("]u\xd7", "AAAA"),
  242. ]:
  243. assert self.binascii.b2a_hqx(input) == expected
  244. def test_rledecode_hqx(self):
  245. for input, expected in [
  246. ("", ""),
  247. ("hello world", "hello world"),
  248. ("\x90\x00", "\x90"),
  249. ("a\x90\x05", "a" * 5),
  250. ("a\x90\xff", "a" * 0xFF),
  251. ("abc\x90\x01def", "abcdef"),
  252. ("abc\x90\x02def", "abccdef"),
  253. ("abc\x90\x03def", "abcccdef"),
  254. ("abc\x90\xa1def", "ab" + "c" * 0xA1 + "def"),
  255. ("abc\x90\x03\x90\x02def", "abccccdef"),
  256. ("abc\x90\x00\x90\x03def", "abc\x90\x90\x90def"),
  257. ("abc\x90\x03\x90\x00def", "abccc\x90def"),
  258. ]:
  259. assert self.binascii.rledecode_hqx(input) == expected
  260. #
  261. for input in [
  262. "\x90",
  263. "a\x90",
  264. "hello world\x90",
  265. ]:
  266. raises(self.binascii.Incomplete, self.binascii.rledecode_hqx,
  267. input)
  268. #
  269. raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\x01")
  270. raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\x02")
  271. raises(self.binascii.Error, self.binascii.rledecode_hqx, "\x90\xff")
  272. def test_rlecode_hqx(self):
  273. for input, expected in [
  274. ("", ""),
  275. ("hello world", "hello world"),
  276. ("helllo world", "helllo world"),
  277. ("hellllo world", "hel\x90\x04o world"),
  278. ("helllllo world", "hel\x90\x05o world"),
  279. ("aaa", "aaa"),
  280. ("aaaa", "a\x90\x04"),
  281. ("a" * 0xff, "a\x90\xff"),
  282. ("a" * 0x100, "a\x90\xffa"),
  283. ("a" * 0x101, "a\x90\xffaa"),
  284. ("a" * 0x102, "a\x90\xffaaa"), # see comments in the source
  285. ("a" * 0x103, "a\x90\xffa\x90\x04"),
  286. ("a" * 0x1fe, "a\x90\xffa\x90\xff"),
  287. ("a" * 0x1ff, "a\x90\xffa\x90\xffa"),
  288. ("\x90", "\x90\x00"),
  289. ("\x90" * 2, "\x90\x00" * 2),
  290. ("\x90" * 3, "\x90\x00" * 3), # see comments in the source
  291. ("\x90" * 345, "\x90\x00" * 345),
  292. ]:
  293. assert self.binascii.rlecode_hqx(input) == expected
  294. def test_crc_hqx(self):
  295. for input, initial, expected in [
  296. ("", 0, 0),
  297. ("", 123, 123),
  298. ("hello", 321, 28955),
  299. ("world", 65535, 12911),
  300. ("uh", 40102, 37544),
  301. ('a', 10000, 14338),
  302. ('b', 10000, 2145),
  303. ('c', 10000, 6208),
  304. ('d', 10000, 26791),
  305. ('e', 10000, 30854),
  306. ('f', 10000, 18661),
  307. ('g', 10000, 22724),
  308. ('h', 10000, 43307),
  309. ('i', 10000, 47370),
  310. ('j', 10000, 35177),
  311. ('k', 10000, 39240),
  312. ('l', 10000, 59823),
  313. ('m', 10000, 63886),
  314. ('n', 10000, 51693),
  315. ('o', 10000, 55756),
  316. ('p', 10000, 14866),
  317. ('q', 10000, 10803),
  318. ('r', 10000, 6736),
  319. ('s', 10000, 2673),
  320. ('t', 10000, 31382),
  321. ('u', 10000, 27319),
  322. ('v', 10000, 23252),
  323. ('w', 10000, 19189),
  324. ('x', 10000, 47898),
  325. ('y', 10000, 43835),
  326. ('z', 10000, 39768),
  327. ]:
  328. assert self.binascii.crc_hqx(input, initial) == expected
  329. def test_crc32(self):
  330. for input, initial, expected in [
  331. ("", 0, 0),
  332. ("", 123, 123),
  333. ("hello", 321, -348147686),
  334. ("world", -2147483648, 32803080),
  335. ("world", 2147483647, 942244330),
  336. ('a', 10000, -184504832),
  337. ('b', 10000, 1812594618),
  338. ('c', 10000, 453955372),
  339. ('d', 10000, -2056627569),
  340. ('e', 10000, -227710439),
  341. ('f', 10000, 1801730979),
  342. ('g', 10000, 476252981),
  343. ('h', 10000, -1931733340),
  344. ('i', 10000, -69523918),
  345. ('j', 10000, 1657960328),
  346. ('k', 10000, 366298910),
  347. ('l', 10000, -1951280451),
  348. ('m', 10000, -55123413),
  349. ('n', 10000, 1707062161),
  350. ('o', 10000, 314082055),
  351. ('p', 10000, -1615819022),
  352. ('q', 10000, -390611356),
  353. ('r', 10000, 1908338654),
  354. ('s', 10000, 112844616),
  355. ('t', 10000, -1730327829),
  356. ('u', 10000, -270894467),
  357. ('v', 10000, 1993550791),
  358. ('w', 10000, 30677841),
  359. ('x', 10000, -1855256896),
  360. ('y', 10000, -429115818),
  361. ('z', 10000, 2137352172),
  362. ('foo', 99999999999999999999999999, -1932704816),
  363. ('bar', -99999999999999999999999999, 2000545409),
  364. ]:
  365. assert self.binascii.crc32(input, initial) == expected
  366. def test_hexlify(self):
  367. for input, expected in [
  368. ("", ""),
  369. ("0", "30"),
  370. ("1", "31"),
  371. ("2", "32"),
  372. ("8", "38"),
  373. ("9", "39"),
  374. ("A", "41"),
  375. ("O", "4f"),
  376. ("\xde", "de"),
  377. ("ABC", "414243"),
  378. ("\x00\x00\x00\xff\x00\x00", "000000ff0000"),
  379. ("\x28\x9c\xc8\xc0\x3d\x8e", "289cc8c03d8e"),
  380. ]:
  381. assert self.binascii.hexlify(input) == expected
  382. assert self.binascii.b2a_hex(input) == expected
  383. def test_unhexlify(self):
  384. for input, expected in [
  385. ("", ""),
  386. ("30", "0"),
  387. ("31", "1"),
  388. ("32", "2"),
  389. ("38", "8"),
  390. ("39", "9"),
  391. ("41", "A"),
  392. ("4F", "O"),
  393. ("4f", "O"),
  394. ("DE", "\xde"),
  395. ("De", "\xde"),
  396. ("dE", "\xde"),
  397. ("de", "\xde"),
  398. ("414243", "ABC"),
  399. ("000000FF0000", "\x00\x00\x00\xff\x00\x00"),
  400. ("289cc8C03d8e", "\x28\x9c\xc8\xc0\x3d\x8e"),
  401. ]:
  402. assert self.binascii.unhexlify(input) == expected
  403. assert self.binascii.a2b_hex(input) == expected