The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_binascii.py
blobc8fad5809b4c8f1f07eedbf81678c2e841d9e60a
1 """Test the binascii C module."""
3 from test import test_support
4 import unittest
5 import binascii
6 import array
8 class BinASCIITest(unittest.TestCase):
10 type2test = str
11 # Create binary test data
12 rawdata = "The quick brown fox jumps over the lazy dog.\r\n"
13 # Be slow so we don't depend on other modules
14 rawdata += "".join(map(chr, xrange(256)))
15 rawdata += "\r\nHello world.\n"
17 def setUp(self):
18 self.data = self.type2test(self.rawdata)
20 def test_exceptions(self):
21 # Check module exceptions
22 self.assertTrue(issubclass(binascii.Error, Exception))
23 self.assertTrue(issubclass(binascii.Incomplete, Exception))
25 def test_functions(self):
26 # Check presence of all functions
27 funcs = []
28 for suffix in "base64", "hqx", "uu", "hex":
29 prefixes = ["a2b_", "b2a_"]
30 if suffix == "hqx":
31 prefixes.extend(["crc_", "rlecode_", "rledecode_"])
32 for prefix in prefixes:
33 name = prefix + suffix
34 self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
35 self.assertRaises(TypeError, getattr(binascii, name))
36 for name in ("hexlify", "unhexlify"):
37 self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
38 self.assertRaises(TypeError, getattr(binascii, name))
40 def test_base64valid(self):
41 # Test base64 with valid data
42 MAX_BASE64 = 57
43 lines = []
44 for i in range(0, len(self.data), MAX_BASE64):
45 b = self.data[i:i+MAX_BASE64]
46 a = binascii.b2a_base64(b)
47 lines.append(a)
48 res = ""
49 for line in lines:
50 b = binascii.a2b_base64(line)
51 res = res + b
52 self.assertEqual(res, self.rawdata)
54 def test_base64invalid(self):
55 # Test base64 with random invalid characters sprinkled throughout
56 # (This requires a new version of binascii.)
57 MAX_BASE64 = 57
58 lines = []
59 for i in range(0, len(self.data), MAX_BASE64):
60 b = self.data[i:i+MAX_BASE64]
61 a = binascii.b2a_base64(b)
62 lines.append(a)
64 fillers = ""
65 valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
66 for i in xrange(256):
67 c = chr(i)
68 if c not in valid:
69 fillers += c
70 def addnoise(line):
71 noise = fillers
72 ratio = len(line) // len(noise)
73 res = ""
74 while line and noise:
75 if len(line) // len(noise) > ratio:
76 c, line = line[0], line[1:]
77 else:
78 c, noise = noise[0], noise[1:]
79 res += c
80 return res + noise + line
81 res = ""
82 for line in map(addnoise, lines):
83 b = binascii.a2b_base64(line)
84 res += b
85 self.assertEqual(res, self.rawdata)
87 # Test base64 with just invalid characters, which should return
88 # empty strings. TBD: shouldn't it raise an exception instead ?
89 self.assertEqual(binascii.a2b_base64(fillers), '')
91 def test_uu(self):
92 MAX_UU = 45
93 lines = []
94 for i in range(0, len(self.data), MAX_UU):
95 b = self.data[i:i+MAX_UU]
96 a = binascii.b2a_uu(b)
97 lines.append(a)
98 res = ""
99 for line in lines:
100 b = binascii.a2b_uu(line)
101 res += b
102 self.assertEqual(res, self.rawdata)
104 self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)
105 self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32)
106 self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31)
107 self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00")
108 self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!")
110 self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!")
112 def test_crc32(self):
113 crc = binascii.crc32("Test the CRC-32 of")
114 crc = binascii.crc32(" this string.", crc)
115 self.assertEqual(crc, 1571220330)
117 self.assertRaises(TypeError, binascii.crc32)
119 # The hqx test is in test_binhex.py
121 def test_hex(self):
122 # test hexlification
123 s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
124 t = binascii.b2a_hex(s)
125 u = binascii.a2b_hex(t)
126 self.assertEqual(s, u)
127 self.assertRaises(TypeError, binascii.a2b_hex, t[:-1])
128 self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q')
130 # Verify the treatment of Unicode strings
131 if test_support.have_unicode:
132 self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61')
134 def test_qp(self):
135 # A test for SF bug 534347 (segfaults without the proper fix)
136 try:
137 binascii.a2b_qp("", **{1:1})
138 except TypeError:
139 pass
140 else:
141 self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
142 self.assertEqual(binascii.a2b_qp("= "), "= ")
143 self.assertEqual(binascii.a2b_qp("=="), "=")
144 self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
145 self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
146 self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
147 self.assertEqual(
148 binascii.b2a_qp("\xff\r\n\xff\n\xff"),
149 "=FF\r\n=FF\r\n=FF"
151 self.assertEqual(
152 binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
153 "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
156 self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
157 self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
158 self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
159 self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')
161 self.assertEqual(binascii.b2a_qp('.'), '=2E')
162 self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
163 self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
165 def test_empty_string(self):
166 # A test for SF bug #1022953. Make sure SystemError is not raised.
167 for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp',
168 'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx',
169 'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu',
170 'rledecode_hqx']:
171 f = getattr(binascii, n)
172 f('')
173 binascii.crc_hqx('', 0)
176 class ArrayBinASCIITest(BinASCIITest):
177 def type2test(self, s):
178 return array.array('c', s)
181 class MemoryviewBinASCIITest(BinASCIITest):
182 type2test = memoryview
185 def test_main():
186 test_support.run_unittest(BinASCIITest,
187 ArrayBinASCIITest,
188 MemoryviewBinASCIITest)
190 if __name__ == "__main__":
191 test_main()