Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_binascii.py

    r2 r391  
    44import unittest
    55import binascii
     6import array
     7
     8# Note: "*_hex" functions are aliases for "(un)hexlify"
     9b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu',
     10                 'hexlify', 'rlecode_hqx']
     11a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu',
     12                 'unhexlify', 'rledecode_hqx']
     13all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx']
     14
    615
    716class BinASCIITest(unittest.TestCase):
    817
     18    type2test = str
    919    # Create binary test data
    10     data = "The quick brown fox jumps over the lazy dog.\r\n"
     20    rawdata = "The quick brown fox jumps over the lazy dog.\r\n"
    1121    # Be slow so we don't depend on other modules
    12     data += "".join(map(chr, xrange(256)))
    13     data += "\r\nHello world.\n"
     22    rawdata += "".join(map(chr, xrange(256)))
     23    rawdata += "\r\nHello world.\n"
     24
     25    def setUp(self):
     26        self.data = self.type2test(self.rawdata)
    1427
    1528    def test_exceptions(self):
    1629        # Check module exceptions
    17         self.assert_(issubclass(binascii.Error, Exception))
    18         self.assert_(issubclass(binascii.Incomplete, Exception))
     30        self.assertTrue(issubclass(binascii.Error, Exception))
     31        self.assertTrue(issubclass(binascii.Incomplete, Exception))
    1932
    2033    def test_functions(self):
    2134        # Check presence of all functions
    22         funcs = []
    23         for suffix in "base64", "hqx", "uu", "hex":
    24             prefixes = ["a2b_", "b2a_"]
    25             if suffix == "hqx":
    26                 prefixes.extend(["crc_", "rlecode_", "rledecode_"])
    27             for prefix in prefixes:
    28                 name = prefix + suffix
    29                 self.assert_(callable(getattr(binascii, name)))
    30                 self.assertRaises(TypeError, getattr(binascii, name))
    31         for name in ("hexlify", "unhexlify"):
    32             self.assert_(callable(getattr(binascii, name)))
     35        for name in all_functions:
     36            self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
    3337            self.assertRaises(TypeError, getattr(binascii, name))
     38
     39    def test_returned_value(self):
     40        # Limit to the minimum of all limits (b2a_uu)
     41        MAX_ALL = 45
     42        raw = self.rawdata[:MAX_ALL]
     43        for fa, fb in zip(a2b_functions, b2a_functions):
     44            a2b = getattr(binascii, fa)
     45            b2a = getattr(binascii, fb)
     46            try:
     47                a = b2a(self.type2test(raw))
     48                res = a2b(self.type2test(a))
     49            except Exception, err:
     50                self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
     51            if fb == 'b2a_hqx':
     52                # b2a_hqx returns a tuple
     53                res, _ = res
     54            self.assertEqual(res, raw, "{}/{} conversion: "
     55                             "{!r} != {!r}".format(fb, fa, res, raw))
     56            self.assertIsInstance(res, str)
     57            self.assertIsInstance(a, str)
     58            self.assertLess(max(ord(c) for c in a), 128)
     59        self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
     60        self.assertIsInstance(binascii.crc32(raw), int)
    3461
    3562    def test_base64valid(self):
     
    3764        MAX_BASE64 = 57
    3865        lines = []
    39         for i in range(0, len(self.data), MAX_BASE64):
    40             b = self.data[i:i+MAX_BASE64]
     66        for i in range(0, len(self.rawdata), MAX_BASE64):
     67            b = self.type2test(self.rawdata[i:i+MAX_BASE64])
    4168            a = binascii.b2a_base64(b)
    4269            lines.append(a)
    4370        res = ""
    4471        for line in lines:
    45             b = binascii.a2b_base64(line)
     72            a = self.type2test(line)
     73            b = binascii.a2b_base64(a)
    4674            res = res + b
    47         self.assertEqual(res, self.data)
     75        self.assertEqual(res, self.rawdata)
    4876
    4977    def test_base64invalid(self):
     
    5381        lines = []
    5482        for i in range(0, len(self.data), MAX_BASE64):
    55             b = self.data[i:i+MAX_BASE64]
     83            b = self.type2test(self.rawdata[i:i+MAX_BASE64])
    5684            a = binascii.b2a_base64(b)
    5785            lines.append(a)
     
    76104        res = ""
    77105        for line in map(addnoise, lines):
    78             b = binascii.a2b_base64(line)
     106            a = self.type2test(line)
     107            b = binascii.a2b_base64(a)
    79108            res += b
    80         self.assertEqual(res, self.data)
     109        self.assertEqual(res, self.rawdata)
    81110
    82111        # Test base64 with just invalid characters, which should return
    83112        # empty strings. TBD: shouldn't it raise an exception instead ?
    84         self.assertEqual(binascii.a2b_base64(fillers), '')
     113        self.assertEqual(binascii.a2b_base64(self.type2test(fillers)), '')
    85114
    86115    def test_uu(self):
     
    88117        lines = []
    89118        for i in range(0, len(self.data), MAX_UU):
    90             b = self.data[i:i+MAX_UU]
     119            b = self.type2test(self.rawdata[i:i+MAX_UU])
    91120            a = binascii.b2a_uu(b)
    92121            lines.append(a)
    93122        res = ""
    94123        for line in lines:
    95             b = binascii.a2b_uu(line)
     124            a = self.type2test(line)
     125            b = binascii.a2b_uu(a)
    96126            res += b
    97         self.assertEqual(res, self.data)
     127        self.assertEqual(res, self.rawdata)
    98128
    99129        self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)
     
    109139
    110140    def test_crc32(self):
    111         crc = binascii.crc32("Test the CRC-32 of")
    112         crc = binascii.crc32(" this string.", crc)
     141        crc = binascii.crc32(self.type2test("Test the CRC-32 of"))
     142        crc = binascii.crc32(self.type2test(" this string."), crc)
    113143        self.assertEqual(crc, 1571220330)
    114144
    115145        self.assertRaises(TypeError, binascii.crc32)
    116146
    117     # The hqx test is in test_binhex.py
     147    def test_hqx(self):
     148        # Perform binhex4 style RLE-compression
     149        # Then calculate the hexbin4 binary-to-ASCII translation
     150        rle = binascii.rlecode_hqx(self.data)
     151        a = binascii.b2a_hqx(self.type2test(rle))
     152        b, _ = binascii.a2b_hqx(self.type2test(a))
     153        res = binascii.rledecode_hqx(b)
     154
     155        self.assertEqual(res, self.rawdata)
    118156
    119157    def test_hex(self):
    120158        # test hexlification
    121159        s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
    122         t = binascii.b2a_hex(s)
    123         u = binascii.a2b_hex(t)
     160        t = binascii.b2a_hex(self.type2test(s))
     161        u = binascii.a2b_hex(self.type2test(t))
    124162        self.assertEqual(s, u)
    125163        self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])
     
    163201    def test_empty_string(self):
    164202        # A test for SF bug #1022953.  Make sure SystemError is not raised.
    165         for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp',
    166                   'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx',
    167                   'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu',
    168                   'rledecode_hqx']:
    169             f = getattr(binascii, n)
    170             f('')
    171         binascii.crc_hqx('', 0)
     203        empty = self.type2test('')
     204        for func in all_functions:
     205            if func == 'crc_hqx':
     206                # crc_hqx needs 2 arguments
     207                binascii.crc_hqx(empty, 0)
     208                continue
     209            f = getattr(binascii, func)
     210            try:
     211                f(empty)
     212            except Exception, err:
     213                self.fail("{}({!r}) raises {!r}".format(func, empty, err))
     214
     215
     216class ArrayBinASCIITest(BinASCIITest):
     217    def type2test(self, s):
     218        return array.array('c', s)
     219
     220
     221class BytearrayBinASCIITest(BinASCIITest):
     222    type2test = bytearray
     223
     224
     225class MemoryviewBinASCIITest(BinASCIITest):
     226    type2test = memoryview
     227
    172228
    173229def test_main():
    174     test_support.run_unittest(BinASCIITest)
     230    test_support.run_unittest(BinASCIITest,
     231                              ArrayBinASCIITest,
     232                              BytearrayBinASCIITest,
     233                              MemoryviewBinASCIITest)
    175234
    176235if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.