|
1 | 1 | """Test the binascii C module.""" |
2 | 2 |
|
3 | | -from test.test_support import verify, verbose, have_unicode |
| 3 | +from test import test_support |
| 4 | +import unittest |
4 | 5 | import binascii |
5 | 6 |
|
6 | | -# Show module doc string |
7 | | -print binascii.__doc__ |
8 | | - |
9 | | -# Show module exceptions |
10 | | -print binascii.Error |
11 | | -print binascii.Incomplete |
12 | | - |
13 | | -# Check presence and display doc strings of all functions |
14 | | -funcs = [] |
15 | | -for suffix in "base64", "hqx", "uu": |
16 | | - prefixes = ["a2b_", "b2a_"] |
17 | | - if suffix == "hqx": |
18 | | - prefixes.extend(["crc_", "rlecode_", "rledecode_"]) |
19 | | - for prefix in prefixes: |
20 | | - name = prefix + suffix |
21 | | - funcs.append(getattr(binascii, name)) |
22 | | -for func in funcs: |
23 | | - print "%-15s: %s" % (func.__name__, func.__doc__) |
24 | | - |
25 | | -# Create binary test data |
26 | | -testdata = "The quick brown fox jumps over the lazy dog.\r\n" |
27 | | -for i in range(256): |
| 7 | +class BinASCIITest(unittest.TestCase): |
| 8 | + |
| 9 | + # Create binary test data |
| 10 | + data = "The quick brown fox jumps over the lazy dog.\r\n" |
28 | 11 | # Be slow so we don't depend on other modules |
29 | | - testdata = testdata + chr(i) |
30 | | -testdata = testdata + "\r\nHello world.\n" |
31 | | - |
32 | | -# Test base64 with valid data |
33 | | -print "base64 test" |
34 | | -MAX_BASE64 = 57 |
35 | | -lines = [] |
36 | | -for i in range(0, len(testdata), MAX_BASE64): |
37 | | - b = testdata[i:i+MAX_BASE64] |
38 | | - a = binascii.b2a_base64(b) |
39 | | - lines.append(a) |
40 | | - print a, |
41 | | -res = "" |
42 | | -for line in lines: |
43 | | - b = binascii.a2b_base64(line) |
44 | | - res = res + b |
45 | | -verify(res == testdata) |
46 | | - |
47 | | -# Test base64 with random invalid characters sprinkled throughout |
48 | | -# (This requires a new version of binascii.) |
49 | | -fillers = "" |
50 | | -valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
51 | | -for i in range(256): |
52 | | - c = chr(i) |
53 | | - if c not in valid: |
54 | | - fillers = fillers + c |
55 | | -def addnoise(line): |
56 | | - noise = fillers |
57 | | - ratio = len(line) // len(noise) |
58 | | - res = "" |
59 | | - while line and noise: |
60 | | - if len(line) // len(noise) > ratio: |
61 | | - c, line = line[0], line[1:] |
| 12 | + data += "".join(map(chr, xrange(256))) |
| 13 | + data += "\r\nHello world.\n" |
| 14 | + |
| 15 | + def test_exceptions(self): |
| 16 | + # Check module exceptions |
| 17 | + self.assert_(issubclass(binascii.Error, Exception)) |
| 18 | + self.assert_(issubclass(binascii.Incomplete, Exception)) |
| 19 | + |
| 20 | + def test_functions(self): |
| 21 | + # Check presence of all functions |
| 22 | + funcs = [] |
| 23 | + for suffix in "base64", "hqx", "uu", "hex": |
| 24 | + prefixes = ["a2b_", "b2a_"] |
| 25 | + if suffix == "hqx": |
| 26 | + prefixes.extend(["crc_", "rlecode_", "rledecode_"]) |
| 27 | + for prefix in prefixes: |
| 28 | + name = prefix + suffix |
| 29 | + self.assert_(callable(getattr(binascii, name))) |
| 30 | + self.assertRaises(TypeError, getattr(binascii, name)) |
| 31 | + for name in ("hexlify", "unhexlify"): |
| 32 | + self.assert_(callable(getattr(binascii, name))) |
| 33 | + self.assertRaises(TypeError, getattr(binascii, name)) |
| 34 | + |
| 35 | + def test_base64valid(self): |
| 36 | + # Test base64 with valid data |
| 37 | + MAX_BASE64 = 57 |
| 38 | + lines = [] |
| 39 | + for i in range(0, len(self.data), MAX_BASE64): |
| 40 | + b = self.data[i:i+MAX_BASE64] |
| 41 | + a = binascii.b2a_base64(b) |
| 42 | + lines.append(a) |
| 43 | + res = "" |
| 44 | + for line in lines: |
| 45 | + b = binascii.a2b_base64(line) |
| 46 | + res = res + b |
| 47 | + self.assertEqual(res, self.data) |
| 48 | + |
| 49 | + def test_base64invalid(self): |
| 50 | + # Test base64 with random invalid characters sprinkled throughout |
| 51 | + # (This requires a new version of binascii.) |
| 52 | + MAX_BASE64 = 57 |
| 53 | + lines = [] |
| 54 | + for i in range(0, len(self.data), MAX_BASE64): |
| 55 | + b = self.data[i:i+MAX_BASE64] |
| 56 | + a = binascii.b2a_base64(b) |
| 57 | + lines.append(a) |
| 58 | + |
| 59 | + fillers = "" |
| 60 | + valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/" |
| 61 | + for i in xrange(256): |
| 62 | + c = chr(i) |
| 63 | + if c not in valid: |
| 64 | + fillers += c |
| 65 | + def addnoise(line): |
| 66 | + noise = fillers |
| 67 | + ratio = len(line) // len(noise) |
| 68 | + res = "" |
| 69 | + while line and noise: |
| 70 | + if len(line) // len(noise) > ratio: |
| 71 | + c, line = line[0], line[1:] |
| 72 | + else: |
| 73 | + c, noise = noise[0], noise[1:] |
| 74 | + res += c |
| 75 | + return res + noise + line |
| 76 | + res = "" |
| 77 | + for line in map(addnoise, lines): |
| 78 | + b = binascii.a2b_base64(line) |
| 79 | + res += b |
| 80 | + self.assertEqual(res, self.data) |
| 81 | + |
| 82 | + # Test base64 with just invalid characters, which should return |
| 83 | + # empty strings. TBD: shouldn't it raise an exception instead ? |
| 84 | + self.assertEqual(binascii.a2b_base64(fillers), '') |
| 85 | + |
| 86 | + def test_uu(self): |
| 87 | + MAX_UU = 45 |
| 88 | + lines = [] |
| 89 | + for i in range(0, len(self.data), MAX_UU): |
| 90 | + b = self.data[i:i+MAX_UU] |
| 91 | + a = binascii.b2a_uu(b) |
| 92 | + lines.append(a) |
| 93 | + res = "" |
| 94 | + for line in lines: |
| 95 | + b = binascii.a2b_uu(line) |
| 96 | + res += b |
| 97 | + self.assertEqual(res, self.data) |
| 98 | + |
| 99 | + self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31) |
| 100 | + self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32) |
| 101 | + self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31) |
| 102 | + self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") |
| 103 | + self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") |
| 104 | + |
| 105 | + self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!") |
| 106 | + |
| 107 | + def test_crc32(self): |
| 108 | + crc = binascii.crc32("Test the CRC-32 of") |
| 109 | + crc = binascii.crc32(" this string.", crc) |
| 110 | + self.assertEqual(crc, 1571220330) |
| 111 | + |
| 112 | + self.assertRaises(TypeError, binascii.crc32) |
| 113 | + |
| 114 | + # The hqx test is in test_binhex.py |
| 115 | + |
| 116 | + def test_hex(self): |
| 117 | + # test hexlification |
| 118 | + s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
| 119 | + t = binascii.b2a_hex(s) |
| 120 | + u = binascii.a2b_hex(t) |
| 121 | + self.assertEqual(s, u) |
| 122 | + self.assertRaises(TypeError, binascii.a2b_hex, t[:-1]) |
| 123 | + self.assertRaises(TypeError, binascii.a2b_hex, t[:-1] + 'q') |
| 124 | + |
| 125 | + # Verify the treatment of Unicode strings |
| 126 | + if test_support.have_unicode: |
| 127 | + self.assertEqual(binascii.hexlify(unicode('a', 'ascii')), '61') |
| 128 | + |
| 129 | + def test_qp(self): |
| 130 | + # A test for SF bug 534347 (segfaults without the proper fix) |
| 131 | + try: |
| 132 | + binascii.a2b_qp("", **{1:1}) |
| 133 | + except TypeError: |
| 134 | + pass |
62 | 135 | else: |
63 | | - c, noise = noise[0], noise[1:] |
64 | | - res = res + c |
65 | | - return res + noise + line |
66 | | -res = "" |
67 | | -for line in map(addnoise, lines): |
68 | | - b = binascii.a2b_base64(line) |
69 | | - res = res + b |
70 | | -verify(res == testdata) |
71 | | - |
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) == '') |
75 | | - |
76 | | -# Test uu |
77 | | -print "uu test" |
78 | | -MAX_UU = 45 |
79 | | -lines = [] |
80 | | -for i in range(0, len(testdata), MAX_UU): |
81 | | - b = testdata[i:i+MAX_UU] |
82 | | - a = binascii.b2a_uu(b) |
83 | | - lines.append(a) |
84 | | - print a, |
85 | | -res = "" |
86 | | -for line in lines: |
87 | | - b = binascii.a2b_uu(line) |
88 | | - res = res + b |
89 | | -verify(res == testdata) |
90 | | - |
91 | | -# Test crc32() |
92 | | -crc = binascii.crc32("Test the CRC-32 of") |
93 | | -crc = binascii.crc32(" this string.", crc) |
94 | | -if crc != 1571220330: |
95 | | - print "binascii.crc32() failed." |
96 | | - |
97 | | -# The hqx test is in test_binhex.py |
98 | | - |
99 | | -# test hexlification |
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) |
103 | | -if s != u: |
104 | | - print 'binascii hexlification failed' |
105 | | -try: |
106 | | - binascii.a2b_hex(t[:-1]) |
107 | | -except TypeError: |
108 | | - pass |
109 | | -else: |
110 | | - print 'expected TypeError not raised' |
111 | | -try: |
112 | | - binascii.a2b_hex(t[:-1] + 'q') |
113 | | -except TypeError: |
114 | | - pass |
115 | | -else: |
116 | | - print 'expected TypeError not raised' |
117 | | - |
118 | | -# Verify the treatment of Unicode strings |
119 | | -if have_unicode: |
120 | | - verify(binascii.hexlify(unicode('a', 'ascii')) == '61', |
121 | | - "hexlify failed for Unicode") |
122 | | - |
123 | | -# A test for SF bug 534347 (segfaults without the proper fix) |
124 | | -try: |
125 | | - binascii.a2b_qp("", **{1:1}) |
126 | | -except TypeError: |
127 | | - pass |
128 | | -else: |
129 | | - raise TestFailed, "binascii..a2b_qp(**{1:1}) didn't raise TypeError" |
| 136 | + self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError") |
| 137 | + self.assertEqual(binascii.a2b_qp("= "), "") |
| 138 | + self.assertEqual(binascii.a2b_qp("=="), "=") |
| 139 | + self.assertEqual(binascii.a2b_qp("=AX"), "=AX") |
| 140 | + self.assertRaises(TypeError, binascii.b2a_qp, foo="bar") |
| 141 | + self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00") |
| 142 | + self.assertEqual( |
| 143 | + binascii.b2a_qp("\xff\r\n\xff\n\xff"), |
| 144 | + "=FF\r\n=FF\r\n=FF" |
| 145 | + ) |
| 146 | + self.assertEqual( |
| 147 | + binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"), |
| 148 | + "0"*75+"=\r\n=FF\r\n=FF\r\n=FF" |
| 149 | + ) |
| 150 | + |
| 151 | +def test_main(): |
| 152 | + test_support.run_unittest(BinASCIITest) |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + test_main() |
0 commit comments