1 """Test the binascii C module."""
3 from test
import test_support
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']
16 class BinASCIITest(unittest
.TestCase
):
19 # Create binary test data
20 rawdata
= "The quick brown fox jumps over the lazy dog.\r\n"
21 # Be slow so we don't depend on other modules
22 rawdata
+= "".join(map(chr, xrange(256)))
23 rawdata
+= "\r\nHello world.\n"
26 self
.data
= self
.type2test(self
.rawdata
)
28 def test_exceptions(self
):
29 # Check module exceptions
30 self
.assertTrue(issubclass(binascii
.Error
, Exception))
31 self
.assertTrue(issubclass(binascii
.Incomplete
, Exception))
33 def test_functions(self
):
34 # Check presence of all functions
35 for name
in all_functions
:
36 self
.assertTrue(hasattr(getattr(binascii
, name
), '__call__'))
37 self
.assertRaises(TypeError, getattr(binascii
, name
))
39 def test_returned_value(self
):
40 # Limit to the minimum of all limits (b2a_uu)
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
)
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
))
52 # b2a_hqx returns a tuple
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)
62 def test_base64valid(self
):
63 # Test base64 with valid data
66 for i
in range(0, len(self
.rawdata
), MAX_BASE64
):
67 b
= self
.type2test(self
.rawdata
[i
:i
+MAX_BASE64
])
68 a
= binascii
.b2a_base64(b
)
72 a
= self
.type2test(line
)
73 b
= binascii
.a2b_base64(a
)
75 self
.assertEqual(res
, self
.rawdata
)
77 def test_base64invalid(self
):
78 # Test base64 with random invalid characters sprinkled throughout
79 # (This requires a new version of binascii.)
82 for i
in range(0, len(self
.data
), MAX_BASE64
):
83 b
= self
.type2test(self
.rawdata
[i
:i
+MAX_BASE64
])
84 a
= binascii
.b2a_base64(b
)
88 valid
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
95 ratio
= len(line
) // len(noise
)
98 if len(line
) // len(noise
) > ratio
:
99 c
, line
= line
[0], line
[1:]
101 c
, noise
= noise
[0], noise
[1:]
103 return res
+ noise
+ line
105 for line
in map(addnoise
, lines
):
106 a
= self
.type2test(line
)
107 b
= binascii
.a2b_base64(a
)
109 self
.assertEqual(res
, self
.rawdata
)
111 # Test base64 with just invalid characters, which should return
112 # empty strings. TBD: shouldn't it raise an exception instead ?
113 self
.assertEqual(binascii
.a2b_base64(self
.type2test(fillers
)), '')
118 for i
in range(0, len(self
.data
), MAX_UU
):
119 b
= self
.type2test(self
.rawdata
[i
:i
+MAX_UU
])
120 a
= binascii
.b2a_uu(b
)
124 a
= self
.type2test(line
)
125 b
= binascii
.a2b_uu(a
)
127 self
.assertEqual(res
, self
.rawdata
)
129 self
.assertEqual(binascii
.a2b_uu("\x7f"), "\x00"*31)
130 self
.assertEqual(binascii
.a2b_uu("\x80"), "\x00"*32)
131 self
.assertEqual(binascii
.a2b_uu("\xff"), "\x00"*31)
132 self
.assertRaises(binascii
.Error
, binascii
.a2b_uu
, "\xff\x00")
133 self
.assertRaises(binascii
.Error
, binascii
.a2b_uu
, "!!!!")
135 self
.assertRaises(binascii
.Error
, binascii
.b2a_uu
, 46*"!")
137 # Issue #7701 (crash on a pydebug build)
138 self
.assertEqual(binascii
.b2a_uu('x'), '!> \n')
140 def test_crc32(self
):
141 crc
= binascii
.crc32(self
.type2test("Test the CRC-32 of"))
142 crc
= binascii
.crc32(self
.type2test(" this string."), crc
)
143 self
.assertEqual(crc
, 1571220330)
145 self
.assertRaises(TypeError, binascii
.crc32
)
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
)
155 self
.assertEqual(res
, self
.rawdata
)
159 s
= '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
160 t
= binascii
.b2a_hex(self
.type2test(s
))
161 u
= binascii
.a2b_hex(self
.type2test(t
))
162 self
.assertEqual(s
, u
)
163 self
.assertRaises(TypeError, binascii
.a2b_hex
, t
[:-1])
164 self
.assertRaises(TypeError, binascii
.a2b_hex
, t
[:-1] + 'q')
166 # Verify the treatment of Unicode strings
167 if test_support
.have_unicode
:
168 self
.assertEqual(binascii
.hexlify(unicode('a', 'ascii')), '61')
171 # A test for SF bug 534347 (segfaults without the proper fix)
173 binascii
.a2b_qp("", **{1:1})
177 self
.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
178 self
.assertEqual(binascii
.a2b_qp("= "), "= ")
179 self
.assertEqual(binascii
.a2b_qp("=="), "=")
180 self
.assertEqual(binascii
.a2b_qp("=AX"), "=AX")
181 self
.assertRaises(TypeError, binascii
.b2a_qp
, foo
="bar")
182 self
.assertEqual(binascii
.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
184 binascii
.b2a_qp("\xff\r\n\xff\n\xff"),
188 binascii
.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
189 "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
192 self
.assertEqual(binascii
.b2a_qp('\0\n'), '=00\n')
193 self
.assertEqual(binascii
.b2a_qp('\0\n', quotetabs
=True), '=00\n')
194 self
.assertEqual(binascii
.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
195 self
.assertEqual(binascii
.b2a_qp('foo\tbar\t\n', quotetabs
=True), 'foo=09bar=09\n')
197 self
.assertEqual(binascii
.b2a_qp('.'), '=2E')
198 self
.assertEqual(binascii
.b2a_qp('.\n'), '=2E\n')
199 self
.assertEqual(binascii
.b2a_qp('a.\n'), 'a.\n')
201 def test_empty_string(self
):
202 # A test for SF bug #1022953. Make sure SystemError is not raised.
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)
209 f
= getattr(binascii
, func
)
212 except Exception, err
:
213 self
.fail("{}({!r}) raises {!r}".format(func
, empty
, err
))
216 class ArrayBinASCIITest(BinASCIITest
):
217 def type2test(self
, s
):
218 return array
.array('c', s
)
221 class BytearrayBinASCIITest(BinASCIITest
):
222 type2test
= bytearray
225 class MemoryviewBinASCIITest(BinASCIITest
):
226 type2test
= memoryview
230 test_support
.run_unittest(BinASCIITest
,
232 BytearrayBinASCIITest
,
233 MemoryviewBinASCIITest
)
235 if __name__
== "__main__":