+++ /dev/null
-"""Test the binascii C module."""\r
-\r
-from test import test_support\r
-import unittest\r
-import binascii\r
-import array\r
-\r
-# Note: "*_hex" functions are aliases for "(un)hexlify"\r
-b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu',\r
- 'hexlify', 'rlecode_hqx']\r
-a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu',\r
- 'unhexlify', 'rledecode_hqx']\r
-all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx']\r
-\r
-\r
-class BinASCIITest(unittest.TestCase):\r
-\r
- type2test = str\r
- # Create binary test data\r
- rawdata = "The quick brown fox jumps over the lazy dog.\r\n"\r
- # Be slow so we don't depend on other modules\r
- rawdata += "".join(map(chr, xrange(256)))\r
- rawdata += "\r\nHello world.\n"\r
-\r
- def setUp(self):\r
- self.data = self.type2test(self.rawdata)\r
-\r
- def test_exceptions(self):\r
- # Check module exceptions\r
- self.assertTrue(issubclass(binascii.Error, Exception))\r
- self.assertTrue(issubclass(binascii.Incomplete, Exception))\r
-\r
- def test_functions(self):\r
- # Check presence of all functions\r
- for name in all_functions:\r
- self.assertTrue(hasattr(getattr(binascii, name), '__call__'))\r
- self.assertRaises(TypeError, getattr(binascii, name))\r
-\r
- def test_returned_value(self):\r
- # Limit to the minimum of all limits (b2a_uu)\r
- MAX_ALL = 45\r
- raw = self.rawdata[:MAX_ALL]\r
- for fa, fb in zip(a2b_functions, b2a_functions):\r
- a2b = getattr(binascii, fa)\r
- b2a = getattr(binascii, fb)\r
- try:\r
- a = b2a(self.type2test(raw))\r
- res = a2b(self.type2test(a))\r
- except Exception, err:\r
- self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))\r
- if fb == 'b2a_hqx':\r
- # b2a_hqx returns a tuple\r
- res, _ = res\r
- self.assertEqual(res, raw, "{}/{} conversion: "\r
- "{!r} != {!r}".format(fb, fa, res, raw))\r
- self.assertIsInstance(res, str)\r
- self.assertIsInstance(a, str)\r
- self.assertLess(max(ord(c) for c in a), 128)\r
- self.assertIsInstance(binascii.crc_hqx(raw, 0), int)\r
- self.assertIsInstance(binascii.crc32(raw), int)\r
-\r
- def test_base64valid(self):\r
- # Test base64 with valid data\r
- MAX_BASE64 = 57\r
- lines = []\r
- for i in range(0, len(self.rawdata), MAX_BASE64):\r
- b = self.type2test(self.rawdata[i:i+MAX_BASE64])\r
- a = binascii.b2a_base64(b)\r
- lines.append(a)\r
- res = ""\r
- for line in lines:\r
- a = self.type2test(line)\r
- b = binascii.a2b_base64(a)\r
- res = res + b\r
- self.assertEqual(res, self.rawdata)\r
-\r
- def test_base64invalid(self):\r
- # Test base64 with random invalid characters sprinkled throughout\r
- # (This requires a new version of binascii.)\r
- MAX_BASE64 = 57\r
- lines = []\r
- for i in range(0, len(self.data), MAX_BASE64):\r
- b = self.type2test(self.rawdata[i:i+MAX_BASE64])\r
- a = binascii.b2a_base64(b)\r
- lines.append(a)\r
-\r
- fillers = ""\r
- valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"\r
- for i in xrange(256):\r
- c = chr(i)\r
- if c not in valid:\r
- fillers += c\r
- def addnoise(line):\r
- noise = fillers\r
- ratio = len(line) // len(noise)\r
- res = ""\r
- while line and noise:\r
- if len(line) // len(noise) > ratio:\r
- c, line = line[0], line[1:]\r
- else:\r
- c, noise = noise[0], noise[1:]\r
- res += c\r
- return res + noise + line\r
- res = ""\r
- for line in map(addnoise, lines):\r
- a = self.type2test(line)\r
- b = binascii.a2b_base64(a)\r
- res += b\r
- self.assertEqual(res, self.rawdata)\r
-\r
- # Test base64 with just invalid characters, which should return\r
- # empty strings. TBD: shouldn't it raise an exception instead ?\r
- self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), '')\r
-\r
- def test_uu(self):\r
- MAX_UU = 45\r
- lines = []\r
- for i in range(0, len(self.data), MAX_UU):\r
- b = self.type2test(self.rawdata[i:i+MAX_UU])\r
- a = binascii.b2a_uu(b)\r
- lines.append(a)\r
- res = ""\r
- for line in lines:\r
- a = self.type2test(line)\r
- b = binascii.a2b_uu(a)\r
- res += b\r
- self.assertEqual(res, self.rawdata)\r
-\r
- self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)\r
- self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32)\r
- self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31)\r
- self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00")\r
- self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!")\r
-\r
- self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!")\r
-\r
- # Issue #7701 (crash on a pydebug build)\r
- self.assertEqual(binascii.b2a_uu('x'), '!> \n')\r
-\r
- def test_crc32(self):\r
- crc = binascii.crc32(self.type2test("Test the CRC-32 of"))\r
- crc = binascii.crc32(self.type2test(" this string."), crc)\r
- self.assertEqual(crc, 1571220330)\r
-\r
- self.assertRaises(TypeError, binascii.crc32)\r
-\r
- def test_hqx(self):\r
- # Perform binhex4 style RLE-compression\r
- # Then calculate the hexbin4 binary-to-ASCII translation\r
- rle = binascii.rlecode_hqx(self.data)\r
- a = binascii.b2a_hqx(self.type2test(rle))\r
- b, _ = binascii.a2b_hqx(self.type2test(a))\r
- res = binascii.rledecode_hqx(b)\r
-\r
- self.assertEqual(res, self.rawdata)\r
-\r
- def test_hex(self):\r
- # test hexlification\r
- s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'\r
- t = binascii.b2a_hex(self.type2test(s))\r
- u = binascii.a2b_hex(self.type2test(t))\r
- self.assertEqual(s, u)\r
- self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])\r
- self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q')\r
-\r
- # Verify the treatment of Unicode strings\r
- if test_support.have_unicode:\r
- self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61')\r
-\r
- def test_qp(self):\r
- # A test for SF bug 534347 (segfaults without the proper fix)\r
- try:\r
- binascii.a2b_qp("", **{1:1})\r
- except TypeError:\r
- pass\r
- else:\r
- self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")\r
- self.assertEqual(binascii.a2b_qp("= "), "= ")\r
- self.assertEqual(binascii.a2b_qp("=="), "=")\r
- self.assertEqual(binascii.a2b_qp("=AX"), "=AX")\r
- self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")\r
- self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")\r
- self.assertEqual(\r
- binascii.b2a_qp("\xff\r\n\xff\n\xff"),\r
- "=FF\r\n=FF\r\n=FF"\r
- )\r
- self.assertEqual(\r
- binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),\r
- "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"\r
- )\r
-\r
- self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')\r
- self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')\r
- self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')\r
- self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')\r
-\r
- self.assertEqual(binascii.b2a_qp('.'), '=2E')\r
- self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')\r
- self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')\r
-\r
- def test_empty_string(self):\r
- # A test for SF bug #1022953. Make sure SystemError is not raised.\r
- empty = self.type2test('')\r
- for func in all_functions:\r
- if func == 'crc_hqx':\r
- # crc_hqx needs 2 arguments\r
- binascii.crc_hqx(empty, 0)\r
- continue\r
- f = getattr(binascii, func)\r
- try:\r
- f(empty)\r
- except Exception, err:\r
- self.fail("{}({!r}) raises {!r}".format(func, empty, err))\r
-\r
-\r
-class ArrayBinASCIITest(BinASCIITest):\r
- def type2test(self, s):\r
- return array.array('c', s)\r
-\r
-\r
-class BytearrayBinASCIITest(BinASCIITest):\r
- type2test = bytearray\r
-\r
-\r
-class MemoryviewBinASCIITest(BinASCIITest):\r
- type2test = memoryview\r
-\r
-\r
-def test_main():\r
- test_support.run_unittest(BinASCIITest,\r
- ArrayBinASCIITest,\r
- BytearrayBinASCIITest,\r
- MemoryviewBinASCIITest)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r