1 """Test the binascii C module."""
3 from test
.test_support
import verify
, verbose
, have_unicode
6 # Show module doc string
9 # Show module exceptions
11 print binascii
.Incomplete
13 # Check presence and display doc strings of all functions
15 for suffix
in "base64", "hqx", "uu":
16 prefixes
= ["a2b_", "b2a_"]
18 prefixes
.extend(["crc_", "rlecode_", "rledecode_"])
19 for prefix
in prefixes
:
20 name
= prefix
+ suffix
21 funcs
.append(getattr(binascii
, name
))
23 print "%-15s: %s" % (func
.__name
__, func
.__doc
__)
25 # Create binary test data
26 testdata
= "The quick brown fox jumps over the lazy dog.\r\n"
28 # Be slow so we don't depend on other modules
29 testdata
= testdata
+ chr(i
)
30 testdata
= testdata
+ "\r\nHello world.\n"
32 # Test base64 with valid data
36 for i
in range(0, len(testdata
), MAX_BASE64
):
37 b
= testdata
[i
:i
+MAX_BASE64
]
38 a
= binascii
.b2a_base64(b
)
43 b
= binascii
.a2b_base64(line
)
45 verify(res
== testdata
)
47 # Test base64 with random invalid characters sprinkled throughout
48 # (This requires a new version of binascii.)
50 valid
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
57 ratio
= len(line
) // len(noise
)
60 if len(line
) // len(noise
) > ratio
:
61 c
, line
= line
[0], line
[1:]
63 c
, noise
= noise
[0], noise
[1:]
65 return res
+ noise
+ line
67 for line
in map(addnoise
, lines
):
68 b
= binascii
.a2b_base64(line
)
70 verify(res
== testdata
)
72 # Test base64 with just invalid characters, which should return
73 # empty strings. TBD: shouldn't it raise an exception instead ?
74 verify(binascii
.a2b_base64(fillers
) == '')
80 for i
in range(0, len(testdata
), MAX_UU
):
81 b
= testdata
[i
:i
+MAX_UU
]
82 a
= binascii
.b2a_uu(b
)
87 b
= binascii
.a2b_uu(line
)
89 verify(res
== testdata
)
92 crc
= binascii
.crc32("Test the CRC-32 of")
93 crc
= binascii
.crc32(" this string.", crc
)
95 print "binascii.crc32() failed."
97 # The hqx test is in test_binhex.py
100 s
= '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000'
101 t
= binascii
.b2a_hex(s
)
102 u
= binascii
.a2b_hex(t
)
104 print 'binascii hexlification failed'
106 binascii
.a2b_hex(t
[:-1])
110 print 'expected TypeError not raised'
112 binascii
.a2b_hex(t
[:-1] + 'q')
116 print 'expected TypeError not raised'
118 # Verify the treatment of Unicode strings
120 verify(binascii
.hexlify(unicode('a', 'ascii')) == '61',
121 "hexlify failed for Unicode")
123 # A test for SF bug 534347 (segfaults without the proper fix)
125 binascii
.a2b_qp("", **{1:1})
129 raise TestFailed
, "binascii..a2b_qp(**{1:1}) didn't raise TypeError"