1 """Test the binascii C module."""
3 from test
import test_support
8 class BinASCIITest(unittest
.TestCase
):
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"
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
28 for suffix
in "base64", "hqx", "uu", "hex":
29 prefixes
= ["a2b_", "b2a_"]
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
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
)
50 b
= binascii
.a2b_base64(line
)
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.)
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
)
65 valid
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
72 ratio
= len(line
) // len(noise
)
75 if len(line
) // len(noise
) > ratio
:
76 c
, line
= line
[0], line
[1:]
78 c
, noise
= noise
[0], noise
[1:]
80 return res
+ noise
+ line
82 for line
in map(addnoise
, lines
):
83 b
= binascii
.a2b_base64(line
)
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
), '')
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
)
100 b
= binascii
.a2b_uu(line
)
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
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')
135 # A test for SF bug 534347 (segfaults without the proper fix)
137 binascii
.a2b_qp("", **{1:1})
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")
148 binascii
.b2a_qp("\xff\r\n\xff\n\xff"),
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',
171 f
= getattr(binascii
, n
)
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
186 test_support
.run_unittest(BinASCIITest
,
188 MemoryviewBinASCIITest
)
190 if __name__
== "__main__":