0001 """Test the binascii C module.""" 0002 0003 from test import test_support 0004 import unittest 0005 import binascii 0006 0007 class BinASCIITest(unittest.TestCase): 0008 0009 # Create binary test data 0010 data = "The quick brown fox jumps over the lazy dog.\r\n" 0011 # Be slow so we don't depend on other modules 0012 data += "".join(map(chr, xrange(256))) 0013 data += "\r\nHello world.\n" 0014 0015 def test_exceptions(self): 0016 # Check module exceptions 0017 self.assert_(issubclass(binascii.Error, Exception)) 0018 self.assert_(issubclass(binascii.Incomplete, Exception)) 0019 0020 def test_functions(self): 0021 # Check presence of all functions 0022 funcs = [] 0023 for suffix in "base64", "hqx", "uu", "hex": 0024 prefixes = ["a2b_", "b2a_"] 0025 if suffix == "hqx": 0026 prefixes.extend(["crc_", "rlecode_", "rledecode_"]) 0027 for prefix in prefixes: 0028 name = prefix + suffix 0029 self.assert_(callable(getattr(binascii, name))) 0030 self.assertRaises(TypeError, getattr(binascii, name)) 0031 for name in ("hexlify", "unhexlify"): 0032 self.assert_(callable(getattr(binascii, name))) 0033 self.assertRaises(TypeError, getattr(binascii, name)) 0034 0035 def test_base64valid(self): 0036 # Test base64 with valid data 0037 MAX_BASE64 = 57 0038 lines = [] 0039 for i in range(0, len(self.data), MAX_BASE64): 0040 b = self.data[i:i+MAX_BASE64] 0041 a = binascii.b2a_base64(b) 0042 lines.append(a) 0043 res = "" 0044 for line in lines: 0045 b = binascii.a2b_base64(line) 0046 res = res + b 0047 self.assertEqual(res, self.data) 0048 0049 def test_base64invalid(self): 0050 # Test base64 with random invalid characters sprinkled throughout 0051 # (This requires a new version of binascii.) 0052 MAX_BASE64 = 57 0053 lines = [] 0054 for i in range(0, len(self.data), MAX_BASE64): 0055 b = self.data[i:i+MAX_BASE64] 0056 a = binascii.b2a_base64(b) 0057 lines.append(a) 0058 0059 fillers = "" 0060 valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" 0061 for i in xrange(256): 0062 c = chr(i) 0063 if c not in valid: 0064 fillers += c 0065 def addnoise(line): 0066 noise = fillers 0067 ratio = len(line) // len(noise) 0068 res = "" 0069 while line and noise: 0070 if len(line) // len(noise) > ratio: 0071 c, line = line[0], line[1:] 0072 else: 0073 c, noise = noise[0], noise[1:] 0074 res += c 0075 return res + noise + line 0076 res = "" 0077 for line in map(addnoise, lines): 0078 b = binascii.a2b_base64(line) 0079 res += b 0080 self.assertEqual(res, self.data) 0081 0082 # Test base64 with just invalid characters, which should return 0083 # empty strings. TBD: shouldn't it raise an exception instead ? 0084 self.assertEqual(binascii.a2b_base64(fillers), '') 0085 0086 def test_uu(self): 0087 MAX_UU = 45 0088 lines = [] 0089 for i in range(0, len(self.data), MAX_UU): 0090 b = self.data[i:i+MAX_UU] 0091 a = binascii.b2a_uu(b) 0092 lines.append(a) 0093 res = "" 0094 for line in lines: 0095 b = binascii.a2b_uu(line) 0096 res += b 0097 self.assertEqual(res, self.data) 0098 0099 self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31) 0100 self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32) 0101 self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31) 0102 self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") 0103 self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") 0104 0105 self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!") 0106 0107 def test_crc32(self): 0108 crc = binascii.crc32("Test the CRC-32 of") 0109 crc = binascii.crc32(" this string.", crc) 0110 self.assertEqual(crc, 1571220330) 0111 0112 self.assertRaises(TypeError, binascii.crc32) 0113 0114 # The hqx test is in test_binhex.py 0115 0116 def test_hex(self): 0117 # test hexlification 0118 s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' 0119 t = binascii.b2a_hex(s) 0120 u = binascii.a2b_hex(t) 0121 self.assertEqual(s, u) 0122 self.assertRaises(TypeError, binascii.a2b_hex, t[:-1]) 0123 self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q') 0124 0125 # Verify the treatment of Unicode strings 0126 if test_support.have_unicode: 0127 self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61') 0128 0129 def test_qp(self): 0130 # A test for SF bug 534347 (segfaults without the proper fix) 0131 try: 0132 binascii.a2b_qp("", **{1:1}) 0133 except TypeError: 0134 pass 0135 else: 0136 self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") 0137 self.assertEqual(binascii.a2b_qp("= "), "") 0138 self.assertEqual(binascii.a2b_qp("=="), "=") 0139 self.assertEqual(binascii.a2b_qp("=AX"), "=AX") 0140 self.assertRaises(TypeError, binascii.b2a_qp, foo="bar") 0141 self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00") 0142 self.assertEqual( 0143 binascii.b2a_qp("\xff\r\n\xff\n\xff"), 0144 "=FF\r\n=FF\r\n=FF" 0145 ) 0146 self.assertEqual( 0147 binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"), 0148 "0"*75+"=\r\n=FF\r\n=FF\r\n=FF" 0149 ) 0150 0151 def test_empty_string(self): 0152 # A test for SF bug #1022953. Make sure SystemError is not raised. 0153 for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp', 0154 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx', 0155 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu', 0156 'rledecode_hqx']: 0157 f = getattr(binascii, n) 0158 f('') 0159 binascii.crc_hqx('', 0) 0160 0161 def test_main(): 0162 test_support.run_unittest(BinASCIITest) 0163 0164 if __name__ == "__main__": 0165 test_main() 0166
Generated by PyXR 0.9.4