Port test_binascii.py to PyUnit and enhance tests.
Code coverage for binascii.c is at 92%. From SF patch #736962.
This commit is contained in:
parent
d4ff741e78
commit
cd1e8a9485
|
@ -1,29 +0,0 @@
|
|||
test_binascii
|
||||
Conversion between binary data and ASCII
|
||||
binascii.Error
|
||||
binascii.Incomplete
|
||||
a2b_base64 : (ascii) -> bin. Decode a line of base64 data
|
||||
b2a_base64 : (bin) -> ascii. Base64-code line of data
|
||||
a2b_hqx : ascii -> bin, done. Decode .hqx coding
|
||||
b2a_hqx : Encode .hqx data
|
||||
crc_hqx : (data, oldcrc) -> newcrc. Compute hqx CRC incrementally
|
||||
rlecode_hqx : Binhex RLE-code binary data
|
||||
rledecode_hqx : Decode hexbin RLE-coded string
|
||||
a2b_uu : (ascii) -> bin. Decode a line of uuencoded data
|
||||
b2a_uu : (bin) -> ascii. Uuencode line of data
|
||||
base64 test
|
||||
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4NCgABAgMEBQYHCAkK
|
||||
CwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJD
|
||||
REVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8
|
||||
fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1
|
||||
tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u
|
||||
7/Dx8vP09fb3+Pn6+/z9/v8NCkhlbGxvIHdvcmxkLgo=
|
||||
uu test
|
||||
M5&AE('%U:6-K(&)R;W=N(&9O>"!J=6UP<R!O=F5R('1H92!L87IY(&1O9RX-
|
||||
M"@ ! @,$!08'" D*"PP-#@\0$1(3%!46%Q@9&AL<'1X?("$B(R0E)B<H*2HK
|
||||
M+"TN+S Q,C,T-38W.#DZ.SP]/C] 04)#1$5&1TA)2DM,34Y/4%%24U155E=8
|
||||
M65I;7%U>7V!A8F-D969G:&EJ:VQM;F]P<7)S='5V=WAY>GM\?7Y_@(&"@X2%
|
||||
MAH>(B8J+C(V.CY"1DI.4E9:7F)F:FYR=GI^@H:*CI*6FIZBIJJNLK:ZOL+&R
|
||||
ML[2UMK>XN;J[O+V^O\#!PL/$Q<;'R,G*R\S-SL_0T=+3U-76U]C9VMO<W=[?
|
||||
MX.'BX^3EYN?HZ>KK[.WN[_#Q\O/T]?;W^/GZ^_S]_O\-"DAE;&QO('=O<FQD
|
||||
"+@H
|
|
@ -1,129 +1,155 @@
|
|||
"""Test the binascii C module."""
|
||||
|
||||
from test.test_support import verify, verbose, have_unicode
|
||||
from test import test_support
|
||||
import unittest
|
||||
import binascii
|
||||
|
||||
# Show module doc string
|
||||
print binascii.__doc__
|
||||
class BinASCIITest(unittest.TestCase):
|
||||
|
||||
# Show module exceptions
|
||||
print binascii.Error
|
||||
print binascii.Incomplete
|
||||
|
||||
# Check presence and display doc strings of all functions
|
||||
funcs = []
|
||||
for suffix in "base64", "hqx", "uu":
|
||||
prefixes = ["a2b_", "b2a_"]
|
||||
if suffix == "hqx":
|
||||
prefixes.extend(["crc_", "rlecode_", "rledecode_"])
|
||||
for prefix in prefixes:
|
||||
name = prefix + suffix
|
||||
funcs.append(getattr(binascii, name))
|
||||
for func in funcs:
|
||||
print "%-15s: %s" % (func.__name__, func.__doc__)
|
||||
|
||||
# Create binary test data
|
||||
testdata = "The quick brown fox jumps over the lazy dog.\r\n"
|
||||
for i in range(256):
|
||||
# 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
|
||||
testdata = testdata + chr(i)
|
||||
testdata = testdata + "\r\nHello world.\n"
|
||||
data += "".join(map(chr, xrange(256)))
|
||||
data += "\r\nHello world.\n"
|
||||
|
||||
# Test base64 with valid data
|
||||
print "base64 test"
|
||||
MAX_BASE64 = 57
|
||||
lines = []
|
||||
for i in range(0, len(testdata), MAX_BASE64):
|
||||
b = testdata[i:i+MAX_BASE64]
|
||||
a = binascii.b2a_base64(b)
|
||||
lines.append(a)
|
||||
print a,
|
||||
res = ""
|
||||
for line in lines:
|
||||
b = binascii.a2b_base64(line)
|
||||
res = res + b
|
||||
verify(res == testdata)
|
||||
def test_exceptions(self):
|
||||
# Check module exceptions
|
||||
self.assert_(issubclass(binascii.Error, Exception))
|
||||
self.assert_(issubclass(binascii.Incomplete, Exception))
|
||||
|
||||
# Test base64 with random invalid characters sprinkled throughout
|
||||
# (This requires a new version of binascii.)
|
||||
fillers = ""
|
||||
valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
|
||||
for i in range(256):
|
||||
c = chr(i)
|
||||
if c not in valid:
|
||||
fillers = fillers + c
|
||||
def addnoise(line):
|
||||
noise = fillers
|
||||
ratio = len(line) // len(noise)
|
||||
res = ""
|
||||
while line and noise:
|
||||
if len(line) // len(noise) > ratio:
|
||||
c, line = line[0], line[1:]
|
||||
def test_functions(self):
|
||||
# Check presence of all functions
|
||||
funcs = []
|
||||
for suffix in "base64", "hqx", "uu", "hex":
|
||||
prefixes = ["a2b_", "b2a_"]
|
||||
if suffix == "hqx":
|
||||
prefixes.extend(["crc_", "rlecode_", "rledecode_"])
|
||||
for prefix in prefixes:
|
||||
name = prefix + suffix
|
||||
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
|
||||
MAX_BASE64 = 57
|
||||
lines = []
|
||||
for i in range(0, len(self.data), MAX_BASE64):
|
||||
b = self.data[i:i+MAX_BASE64]
|
||||
a = binascii.b2a_base64(b)
|
||||
lines.append(a)
|
||||
res = ""
|
||||
for line in lines:
|
||||
b = binascii.a2b_base64(line)
|
||||
res = res + b
|
||||
self.assertEqual(res, self.data)
|
||||
|
||||
def test_base64invalid(self):
|
||||
# Test base64 with random invalid characters sprinkled throughout
|
||||
# (This requires a new version of binascii.)
|
||||
MAX_BASE64 = 57
|
||||
lines = []
|
||||
for i in range(0, len(self.data), MAX_BASE64):
|
||||
b = self.data[i:i+MAX_BASE64]
|
||||
a = binascii.b2a_base64(b)
|
||||
lines.append(a)
|
||||
|
||||
fillers = ""
|
||||
valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
|
||||
for i in xrange(256):
|
||||
c = chr(i)
|
||||
if c not in valid:
|
||||
fillers += c
|
||||
def addnoise(line):
|
||||
noise = fillers
|
||||
ratio = len(line) // len(noise)
|
||||
res = ""
|
||||
while line and noise:
|
||||
if len(line) // len(noise) > ratio:
|
||||
c, line = line[0], line[1:]
|
||||
else:
|
||||
c, noise = noise[0], noise[1:]
|
||||
res += c
|
||||
return res + noise + line
|
||||
res = ""
|
||||
for line in map(addnoise, lines):
|
||||
b = binascii.a2b_base64(line)
|
||||
res += b
|
||||
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), '')
|
||||
|
||||
def test_uu(self):
|
||||
MAX_UU = 45
|
||||
lines = []
|
||||
for i in range(0, len(self.data), MAX_UU):
|
||||
b = self.data[i:i+MAX_UU]
|
||||
a = binascii.b2a_uu(b)
|
||||
lines.append(a)
|
||||
res = ""
|
||||
for line in lines:
|
||||
b = binascii.a2b_uu(line)
|
||||
res += b
|
||||
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*"!")
|
||||
|
||||
def test_crc32(self):
|
||||
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
|
||||
|
||||
def test_hex(self):
|
||||
# test hexlification
|
||||
s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
|
||||
t = binascii.b2a_hex(s)
|
||||
u = binascii.a2b_hex(t)
|
||||
self.assertEqual(s, u)
|
||||
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')
|
||||
|
||||
def test_qp(self):
|
||||
# A test for SF bug 534347 (segfaults without the proper fix)
|
||||
try:
|
||||
binascii.a2b_qp("", **{1:1})
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
c, noise = noise[0], noise[1:]
|
||||
res = res + c
|
||||
return res + noise + line
|
||||
res = ""
|
||||
for line in map(addnoise, lines):
|
||||
b = binascii.a2b_base64(line)
|
||||
res = res + b
|
||||
verify(res == testdata)
|
||||
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")
|
||||
self.assertEqual(
|
||||
binascii.b2a_qp("\xff\r\n\xff\n\xff"),
|
||||
"=FF\r\n=FF\r\n=FF"
|
||||
)
|
||||
self.assertEqual(
|
||||
binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
|
||||
"0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
|
||||
)
|
||||
|
||||
# Test base64 with just invalid characters, which should return
|
||||
# empty strings. TBD: shouldn't it raise an exception instead ?
|
||||
verify(binascii.a2b_base64(fillers) == '')
|
||||
def test_main():
|
||||
test_support.run_unittest(BinASCIITest)
|
||||
|
||||
# Test uu
|
||||
print "uu test"
|
||||
MAX_UU = 45
|
||||
lines = []
|
||||
for i in range(0, len(testdata), MAX_UU):
|
||||
b = testdata[i:i+MAX_UU]
|
||||
a = binascii.b2a_uu(b)
|
||||
lines.append(a)
|
||||
print a,
|
||||
res = ""
|
||||
for line in lines:
|
||||
b = binascii.a2b_uu(line)
|
||||
res = res + b
|
||||
verify(res == testdata)
|
||||
|
||||
# Test crc32()
|
||||
crc = binascii.crc32("Test the CRC-32 of")
|
||||
crc = binascii.crc32(" this string.", crc)
|
||||
if crc != 1571220330:
|
||||
print "binascii.crc32() failed."
|
||||
|
||||
# The hqx test is in test_binhex.py
|
||||
|
||||
# test hexlification
|
||||
s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
|
||||
t = binascii.b2a_hex(s)
|
||||
u = binascii.a2b_hex(t)
|
||||
if s != u:
|
||||
print 'binascii hexlification failed'
|
||||
try:
|
||||
binascii.a2b_hex(t[:-1])
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
print 'expected TypeError not raised'
|
||||
try:
|
||||
binascii.a2b_hex(t[:-1] + 'q')
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
print 'expected TypeError not raised'
|
||||
|
||||
# Verify the treatment of Unicode strings
|
||||
if have_unicode:
|
||||
verify(binascii.hexlify(unicode('a', 'ascii')) == '61',
|
||||
"hexlify failed for Unicode")
|
||||
|
||||
# A test for SF bug 534347 (segfaults without the proper fix)
|
||||
try:
|
||||
binascii.a2b_qp("", **{1:1})
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
raise TestFailed, "binascii..a2b_qp(**{1:1}) didn't raise TypeError"
|
||||
if __name__ == "__main__":
|
||||
test_main()
|
||||
|
|
Loading…
Reference in New Issue