Changeset 391 for python/trunk/Lib/test/test_binascii.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_binascii.py
r2 r391 4 4 import unittest 5 5 import binascii 6 import array 7 8 # Note: "*_hex" functions are aliases for "(un)hexlify" 9 b2a_functions = ['b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp', 'b2a_uu', 10 'hexlify', 'rlecode_hqx'] 11 a2b_functions = ['a2b_base64', 'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', 12 'unhexlify', 'rledecode_hqx'] 13 all_functions = a2b_functions + b2a_functions + ['crc32', 'crc_hqx'] 14 6 15 7 16 class BinASCIITest(unittest.TestCase): 8 17 18 type2test = str 9 19 # 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" 11 21 # 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) 14 27 15 28 def test_exceptions(self): 16 29 # 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)) 19 32 20 33 def test_functions(self): 21 34 # 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__')) 33 37 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) 34 61 35 62 def test_base64valid(self): … … 37 64 MAX_BASE64 = 57 38 65 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]) 41 68 a = binascii.b2a_base64(b) 42 69 lines.append(a) 43 70 res = "" 44 71 for line in lines: 45 b = binascii.a2b_base64(line) 72 a = self.type2test(line) 73 b = binascii.a2b_base64(a) 46 74 res = res + b 47 self.assertEqual(res, self. data)75 self.assertEqual(res, self.rawdata) 48 76 49 77 def test_base64invalid(self): … … 53 81 lines = [] 54 82 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]) 56 84 a = binascii.b2a_base64(b) 57 85 lines.append(a) … … 76 104 res = "" 77 105 for line in map(addnoise, lines): 78 b = binascii.a2b_base64(line) 106 a = self.type2test(line) 107 b = binascii.a2b_base64(a) 79 108 res += b 80 self.assertEqual(res, self. data)109 self.assertEqual(res, self.rawdata) 81 110 82 111 # Test base64 with just invalid characters, which should return 83 112 # 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)), '') 85 114 86 115 def test_uu(self): … … 88 117 lines = [] 89 118 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]) 91 120 a = binascii.b2a_uu(b) 92 121 lines.append(a) 93 122 res = "" 94 123 for line in lines: 95 b = binascii.a2b_uu(line) 124 a = self.type2test(line) 125 b = binascii.a2b_uu(a) 96 126 res += b 97 self.assertEqual(res, self. data)127 self.assertEqual(res, self.rawdata) 98 128 99 129 self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31) … … 109 139 110 140 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) 113 143 self.assertEqual(crc, 1571220330) 114 144 115 145 self.assertRaises(TypeError, binascii.crc32) 116 146 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) 118 156 119 157 def test_hex(self): 120 158 # test hexlification 121 159 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)) 124 162 self.assertEqual(s, u) 125 163 self.assertRaises(TypeError, binascii.a2b_hex, t[:-1]) … … 163 201 def test_empty_string(self): 164 202 # 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 216 class ArrayBinASCIITest(BinASCIITest): 217 def type2test(self, s): 218 return array.array('c', s) 219 220 221 class BytearrayBinASCIITest(BinASCIITest): 222 type2test = bytearray 223 224 225 class MemoryviewBinASCIITest(BinASCIITest): 226 type2test = memoryview 227 172 228 173 229 def test_main(): 174 test_support.run_unittest(BinASCIITest) 230 test_support.run_unittest(BinASCIITest, 231 ArrayBinASCIITest, 232 BytearrayBinASCIITest, 233 MemoryviewBinASCIITest) 175 234 176 235 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.