Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 1 | """Test the binascii C module.""" |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 2 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 3 | from test_support import verify, verbose |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 4 | import binascii |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 6 | # Show module doc string |
| 7 | print binascii.__doc__ |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 9 | # Show module exceptions |
| 10 | print binascii.Error |
| 11 | print binascii.Incomplete |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 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__) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 25 | # Create binary test data |
| 26 | testdata = "The quick brown fox jumps over the lazy dog.\r\n" |
| 27 | for i in range(256): |
| 28 | # Be slow so we don't depend on other modules |
| 29 | testdata = testdata + chr(i) |
| 30 | testdata = testdata + "\r\nHello world.\n" |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 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 |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 45 | verify(res == testdata) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 57 | ratio = len(line) // len(noise) |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 58 | res = "" |
| 59 | while line and noise: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 60 | if len(line) // len(noise) > ratio: |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 61 | c, line = line[0], line[1:] |
| 62 | 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 |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 70 | verify(res == testdata) |
Roger E. Masse | 2a1c834 | 1997-01-16 16:44:09 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 72 | # Test uu |
| 73 | print "uu test" |
| 74 | MAX_UU = 45 |
| 75 | lines = [] |
| 76 | for i in range(0, len(testdata), MAX_UU): |
| 77 | b = testdata[i:i+MAX_UU] |
| 78 | a = binascii.b2a_uu(b) |
| 79 | lines.append(a) |
| 80 | print a, |
| 81 | res = "" |
| 82 | for line in lines: |
| 83 | b = binascii.a2b_uu(line) |
| 84 | res = res + b |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 85 | verify(res == testdata) |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | cba0436 | 2000-02-16 21:13:06 +0000 | [diff] [blame] | 87 | # Test crc32() |
| 88 | crc = binascii.crc32("Test the CRC-32 of") |
| 89 | crc = binascii.crc32(" this string.", crc) |
| 90 | if crc != 1571220330: |
| 91 | print "binascii.crc32() failed." |
| 92 | |
Guido van Rossum | fdecda0 | 1999-10-19 19:08:13 +0000 | [diff] [blame] | 93 | # The hqx test is in test_binhex.py |
Barry Warsaw | 97ca66f | 2000-08-15 06:08:31 +0000 | [diff] [blame] | 94 | |
| 95 | # test hexlification |
| 96 | s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' |
| 97 | t = binascii.b2a_hex(s) |
| 98 | u = binascii.a2b_hex(t) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 99 | if s != u: |
Barry Warsaw | 97ca66f | 2000-08-15 06:08:31 +0000 | [diff] [blame] | 100 | print 'binascii hexlification failed' |
| 101 | try: |
| 102 | binascii.a2b_hex(t[:-1]) |
| 103 | except TypeError: |
| 104 | pass |
| 105 | else: |
| 106 | print 'expected TypeError not raised' |
| 107 | try: |
| 108 | binascii.a2b_hex(t[:-1] + 'q') |
| 109 | except TypeError: |
| 110 | pass |
| 111 | else: |
| 112 | print 'expected TypeError not raised' |
Jeremy Hylton | de7c192 | 2001-10-11 14:09:03 +0000 | [diff] [blame^] | 113 | |
| 114 | # Verify the treatment of Unicode strings |
| 115 | verify(binascii.hexlify(u'a') == '61', "hexlify failed for Unicode") |
| 116 | |