annotate Lib/test/test_base64.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 06145fbc7ab9
children 6e57d097ae56 eb45f85c4c79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24954
diff changeset
1 import unittest
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24954
diff changeset
2 from test import test_support
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 24954
diff changeset
3 import base64
24954
05da95bcd724 Made it more readable.
Guido van Rossum <guido@python.org>
parents: 24851
diff changeset
4
23472
be0bc55ecf4a Added Mitchell Surface's regression tests for base64. Closes patch #550002.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
6
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
7 class LegacyBase64TestCase(unittest.TestCase):
24954
05da95bcd724 Made it more readable.
Guido van Rossum <guido@python.org>
parents: 24851
diff changeset
8 def test_encodestring(self):
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
9 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
10 eq(base64.encodestring("www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
11 eq(base64.encodestring("a"), "YQ==\n")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
12 eq(base64.encodestring("ab"), "YWI=\n")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
13 eq(base64.encodestring("abc"), "YWJj\n")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
14 eq(base64.encodestring(""), "")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
15 eq(base64.encodestring("abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
17 "0123456789!@#0^&*();:<>,. []{}"),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
18 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
19 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
20 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
23472
be0bc55ecf4a Added Mitchell Surface's regression tests for base64. Closes patch #550002.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21
24954
05da95bcd724 Made it more readable.
Guido van Rossum <guido@python.org>
parents: 24851
diff changeset
22 def test_decodestring(self):
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
23 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
24 eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
25 eq(base64.decodestring("YQ==\n"), "a")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
26 eq(base64.decodestring("YWI=\n"), "ab")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
27 eq(base64.decodestring("YWJj\n"), "abc")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
28 eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
29 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
30 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
31 "abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
33 "0123456789!@#0^&*();:<>,. []{}")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
34 eq(base64.decodestring(''), '')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
35
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
36 def test_encode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
37 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
38 from cStringIO import StringIO
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
39 infp = StringIO('abcdefghijklmnopqrstuvwxyz'
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
40 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
41 '0123456789!@#0^&*();:<>,. []{}')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
42 outfp = StringIO()
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
43 base64.encode(infp, outfp)
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
44 eq(outfp.getvalue(),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
45 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
46 'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
47 'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
48
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
49 def test_decode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
50 from cStringIO import StringIO
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
51 infp = StringIO('d3d3LnB5dGhvbi5vcmc=')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
52 outfp = StringIO()
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
53 base64.decode(infp, outfp)
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
54 self.assertEqual(outfp.getvalue(), 'www.python.org')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
55
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
56
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
57
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
58 class BaseXYTestCase(unittest.TestCase):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
59 def test_b64encode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
60 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
61 # Test default alphabet
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
62 eq(base64.b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
63 eq(base64.b64encode('\x00'), 'AA==')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
64 eq(base64.b64encode("a"), "YQ==")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
65 eq(base64.b64encode("ab"), "YWI=")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
66 eq(base64.b64encode("abc"), "YWJj")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
67 eq(base64.b64encode(""), "")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
68 eq(base64.b64encode("abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
69 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
70 "0123456789!@#0^&*();:<>,. []{}"),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
71 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
72 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
73 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
74 # Test with arbitrary alternative characters
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
75 eq(base64.b64encode('\xd3V\xbeo\xf7\x1d', altchars='*$'), '01a*b$cd')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
76 # Test standard alphabet
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
77 eq(base64.standard_b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
78 eq(base64.standard_b64encode("a"), "YQ==")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
79 eq(base64.standard_b64encode("ab"), "YWI=")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
80 eq(base64.standard_b64encode("abc"), "YWJj")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
81 eq(base64.standard_b64encode(""), "")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
82 eq(base64.standard_b64encode("abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
83 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
84 "0123456789!@#0^&*();:<>,. []{}"),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
85 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
86 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
87 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
88 # Test with 'URL safe' alternative characters
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
89 eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
90
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
91 def test_b64decode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
92 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
93 eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
94 eq(base64.b64decode('AA=='), '\x00')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
95 eq(base64.b64decode("YQ=="), "a")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
96 eq(base64.b64decode("YWI="), "ab")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
97 eq(base64.b64decode("YWJj"), "abc")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
98 eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
99 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
100 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
101 "abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
103 "0123456789!@#0^&*();:<>,. []{}")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
104 eq(base64.b64decode(''), '')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
105 # Test with arbitrary alternative characters
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
106 eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
107 # Test standard alphabet
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
108 eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
109 eq(base64.standard_b64decode("YQ=="), "a")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
110 eq(base64.standard_b64decode("YWI="), "ab")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
111 eq(base64.standard_b64decode("YWJj"), "abc")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
112 eq(base64.standard_b64decode(""), "")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
113 eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
114 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
115 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
116 "abcdefghijklmnopqrstuvwxyz"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
118 "0123456789!@#0^&*();:<>,. []{}")
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
119 # Test with 'URL safe' alternative characters
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
120 eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
121
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
122 def test_b64decode_error(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
123 self.assertRaises(TypeError, base64.b64decode, 'abc')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
124
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
125 def test_b32encode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
126 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
127 eq(base64.b32encode(''), '')
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
128 eq(base64.b32encode('\x00'), 'AA======')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
129 eq(base64.b32encode('a'), 'ME======')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
130 eq(base64.b32encode('ab'), 'MFRA====')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
131 eq(base64.b32encode('abc'), 'MFRGG===')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
132 eq(base64.b32encode('abcd'), 'MFRGGZA=')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
133 eq(base64.b32encode('abcde'), 'MFRGGZDF')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
134
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
135 def test_b32decode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
136 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
137 eq(base64.b32decode(''), '')
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
138 eq(base64.b32decode('AA======'), '\x00')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
139 eq(base64.b32decode('ME======'), 'a')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
140 eq(base64.b32decode('MFRA===='), 'ab')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
141 eq(base64.b32decode('MFRGG==='), 'abc')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
142 eq(base64.b32decode('MFRGGZA='), 'abcd')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
143 eq(base64.b32decode('MFRGGZDF'), 'abcde')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
144
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
145 def test_b32decode_casefold(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
146 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
147 eq(base64.b32decode('', True), '')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
148 eq(base64.b32decode('ME======', True), 'a')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
149 eq(base64.b32decode('MFRA====', True), 'ab')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
150 eq(base64.b32decode('MFRGG===', True), 'abc')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
151 eq(base64.b32decode('MFRGGZA=', True), 'abcd')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
152 eq(base64.b32decode('MFRGGZDF', True), 'abcde')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
153 # Lower cases
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
154 eq(base64.b32decode('me======', True), 'a')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
155 eq(base64.b32decode('mfra====', True), 'ab')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
156 eq(base64.b32decode('mfrgg===', True), 'abc')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
157 eq(base64.b32decode('mfrggza=', True), 'abcd')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
158 eq(base64.b32decode('mfrggzdf', True), 'abcde')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
159 # Expected exceptions
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
160 self.assertRaises(TypeError, base64.b32decode, 'me======')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
161 # Mapping zero and one
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
162 eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
163 eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
164 eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
165
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
166 def test_b32decode_error(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
167 self.assertRaises(TypeError, base64.b32decode, 'abc')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
168 self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
169
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
170 def test_b16encode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
171 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
172 eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF')
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
173 eq(base64.b16encode('\x00'), '00')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
174
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
175 def test_b16decode(self):
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
176 eq = self.assertEqual
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
177 eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef')
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31327
diff changeset
178 eq(base64.b16decode('00'), '\x00')
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
179 # Lower case is not allowed without a flag
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
180 self.assertRaises(TypeError, base64.b16decode, '0102abcdef')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
181 # Case fold
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
182 eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef')
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
183
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
184
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
185
23472
be0bc55ecf4a Added Mitchell Surface's regression tests for base64. Closes patch #550002.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 def test_main():
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 35047
diff changeset
187 test_support.run_unittest(__name__)
23472
be0bc55ecf4a Added Mitchell Surface's regression tests for base64. Closes patch #550002.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188
31327
8c7090908cec More complete code coverage, including testing the new RFC 3548 support.
Barry Warsaw <barry@python.org>
parents: 28917
diff changeset
189 if __name__ == '__main__':
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 35047
diff changeset
190 test_main()