"""Test the binascii C module."""
from test
import test_support
class BinASCIITest(unittest
.TestCase
):
# Create binary test data
data
= "The quick brown fox jumps over the lazy dog.\r\n"
# Be slow so we don't depend on other modules
data
+= "".join(map(chr, xrange(256)))
data
+= "\r\nHello world.\n"
def test_exceptions(self
):
# Check module exceptions
self
.assert_(issubclass(binascii
.Error
, Exception))
self
.assert_(issubclass(binascii
.Incomplete
, Exception))
def test_functions(self
):
# Check presence of all functions
for suffix
in "base64", "hqx", "uu", "hex":
prefixes
= ["a2b_", "b2a_"]
prefixes
.extend(["crc_", "rlecode_", "rledecode_"])
self
.assert_(callable(getattr(binascii
, name
)))
self
.assertRaises(TypeError, getattr(binascii
, name
))
for name
in ("hexlify", "unhexlify"):
self
.assert_(callable(getattr(binascii
, name
)))
self
.assertRaises(TypeError, getattr(binascii
, name
))
def test_base64valid(self
):
# Test base64 with valid data
for i
in range(0, len(self
.data
), MAX_BASE64
):
b
= self
.data
[i
:i
+MAX_BASE64
]
a
= binascii
.b2a_base64(b
)
b
= binascii
.a2b_base64(line
)
self
.assertEqual(res
, self
.data
)
def test_base64invalid(self
):
# Test base64 with random invalid characters sprinkled throughout
# (This requires a new version of binascii.)
for i
in range(0, len(self
.data
), MAX_BASE64
):
b
= self
.data
[i
:i
+MAX_BASE64
]
a
= binascii
.b2a_base64(b
)
valid
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
ratio
= len(line
) // len(noise
)
if len(line
) // len(noise
) > ratio
:
c
, line
= line
[0], line
[1:]
c
, noise
= noise
[0], noise
[1:]
return res
+ noise
+ line
for line
in map(addnoise
, lines
):
b
= binascii
.a2b_base64(line
)
self
.assertEqual(res
, self
.data
)
# Test base64 with just invalid characters, which should return
# empty strings. TBD: shouldn't it raise an exception instead ?
self
.assertEqual(binascii
.a2b_base64(fillers
), '')
for i
in range(0, len(self
.data
), MAX_UU
):
b
= self
.data
[i
:i
+MAX_UU
]
b
= binascii
.a2b_uu(line
)
self
.assertEqual(res
, self
.data
)
self
.assertEqual(binascii
.a2b_uu("\x7f"), "\x00"*31)
self
.assertEqual(binascii
.a2b_uu("\x80"), "\x00"*32)
self
.assertEqual(binascii
.a2b_uu("\xff"), "\x00"*31)
self
.assertRaises(binascii
.Error
, binascii
.a2b_uu
, "\xff\x00")
self
.assertRaises(binascii
.Error
, binascii
.a2b_uu
, "!!!!")
self
.assertRaises(binascii
.Error
, binascii
.b2a_uu
, 46*"!")
crc
= binascii
.crc32("Test the CRC-32 of")
crc
= binascii
.crc32(" this string.", crc
)
self
.assertEqual(crc
, 1571220330)
self
.assertRaises(TypeError, binascii
.crc32
)
# The hqx test is in test_binhex.py
s
= '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
self
.assertRaises(TypeError, binascii
.a2b_hex
, t
[:-1])
self
.assertRaises(TypeError, binascii
.a2b_hex
, t
[:-1] + 'q')
# Verify the treatment of Unicode strings
if test_support
.have_unicode
:
self
.assertEqual(binascii
.hexlify(unicode('a', 'ascii')), '61')
# A test for SF bug 534347 (segfaults without the proper fix)
binascii
.a2b_qp("", **{1:1})
self
.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
self
.assertEqual(binascii
.a2b_qp("= "), "")
self
.assertEqual(binascii
.a2b_qp("=="), "=")
self
.assertEqual(binascii
.a2b_qp("=AX"), "=AX")
self
.assertRaises(TypeError, binascii
.b2a_qp
, foo
="bar")
self
.assertEqual(binascii
.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
binascii
.b2a_qp("\xff\r\n\xff\n\xff"),
binascii
.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
def test_empty_string(self
):
# A test for SF bug #1022953. Make sure SystemError is not raised.
for n
in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp',
'b2a_hex', 'unhexlify', 'hexlify', 'crc32', 'b2a_hqx',
'a2b_hqx', 'a2b_base64', 'rlecode_hqx', 'b2a_uu',
test_support
.run_unittest(BinASCIITest
)
if __name__
== "__main__":