Rewritten -- this now tests the binascii *except* for the binhex

module, which is tested by test_binhex.py.
This commit is contained in:
Guido van Rossum 1999-10-19 19:08:13 +00:00
parent a0e85b241d
commit fdecda0123

View file

@ -1,46 +1,87 @@
#! /usr/bin/env python """Test the binascii C module."""
"""Test script for the binascii C module
Uses the mechanism of the python binhex module
Roger E. Masse
"""
import binhex
import tempfile
from test_support import verbose from test_support import verbose
import binascii
def test(): # Show module doc string
print binascii.__doc__
try: # Show module exceptions
fname1 = tempfile.mktemp() print binascii.Error
fname2 = tempfile.mktemp() print binascii.Incomplete
f = open(fname1, 'w')
except:
raise ImportError, "Cannot test binascii without a temp file"
start = 'Jack is my hero' # Check presence and display doc strings of all functions
f.write(start) funcs = []
f.close() for suffix in "base64", "hqx", "uu":
prefixes = ["a2b_", "b2a_"]
binhex.binhex(fname1, fname2) if suffix == "hqx":
if verbose: prefixes.extend(["crc_", "rlecode_", "rledecode_"])
print 'binhex' for prefix in prefixes:
name = prefix + suffix
funcs.append(getattr(binascii, name))
for func in funcs:
print "%-15s: %s" % (func.__name__, func.__doc__)
binhex.hexbin(fname2, fname1) # Create binary test data
if verbose: testdata = "The quick brown fox jumps over the lazy dog.\r\n"
print 'hexbin' for i in range(256):
# Be slow so we don't depend on other modules
testdata = testdata + chr(i)
testdata = testdata + "\r\nHello world.\n"
f = open(fname1, 'r') # Test base64 with valid data
finish = f.readline() 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
assert res == testdata
if start <> finish: # Test base64 with random invalid characters sprinkled throughout
print 'Error: binhex <> hexbin' # (This requires a new version of binascii.)
elif verbose: fillers = ""
print 'binhex == hexbin' 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:]
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
assert res == testdata
try: # Test uu
import os print "uu test"
os.unlink(fname1) MAX_UU = 45
os.unlink(fname2) lines = []
except: for i in range(0, len(testdata), MAX_UU):
pass b = testdata[i:i+MAX_UU]
test() a = binascii.b2a_uu(b)
lines.append(a)
print a,
res = ""
for line in lines:
b = binascii.a2b_uu(line)
res = res + b
assert res == testdata
# The hqx test is in test_binhex.py