move sections
[python/dscho.git] / Lib / test / test_base64.py
blobff2c3700d3b10998b95c19bc2654733499a3c7b7
1 import unittest
2 from test import test_support
3 import base64
6 \f
7 class LegacyBase64TestCase(unittest.TestCase):
8 def test_encodestring(self):
9 eq = self.assertEqual
10 eq(base64.encodestring("www.python.org"), "d3d3LnB5dGhvbi5vcmc=\n")
11 eq(base64.encodestring("a"), "YQ==\n")
12 eq(base64.encodestring("ab"), "YWI=\n")
13 eq(base64.encodestring("abc"), "YWJj\n")
14 eq(base64.encodestring(""), "")
15 eq(base64.encodestring("abcdefghijklmnopqrstuvwxyz"
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17 "0123456789!@#0^&*();:<>,. []{}"),
18 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
19 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
20 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
22 def test_decodestring(self):
23 eq = self.assertEqual
24 eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
25 eq(base64.decodestring("YQ==\n"), "a")
26 eq(base64.decodestring("YWI=\n"), "ab")
27 eq(base64.decodestring("YWJj\n"), "abc")
28 eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
29 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
30 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
31 "abcdefghijklmnopqrstuvwxyz"
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33 "0123456789!@#0^&*();:<>,. []{}")
34 eq(base64.decodestring(''), '')
36 def test_encode(self):
37 eq = self.assertEqual
38 from cStringIO import StringIO
39 infp = StringIO('abcdefghijklmnopqrstuvwxyz'
40 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
41 '0123456789!@#0^&*();:<>,. []{}')
42 outfp = StringIO()
43 base64.encode(infp, outfp)
44 eq(outfp.getvalue(),
45 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
46 'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
47 'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
49 def test_decode(self):
50 from cStringIO import StringIO
51 infp = StringIO('d3d3LnB5dGhvbi5vcmc=')
52 outfp = StringIO()
53 base64.decode(infp, outfp)
54 self.assertEqual(outfp.getvalue(), 'www.python.org')
58 class BaseXYTestCase(unittest.TestCase):
59 def test_b64encode(self):
60 eq = self.assertEqual
61 # Test default alphabet
62 eq(base64.b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
63 eq(base64.b64encode('\x00'), 'AA==')
64 eq(base64.b64encode("a"), "YQ==")
65 eq(base64.b64encode("ab"), "YWI=")
66 eq(base64.b64encode("abc"), "YWJj")
67 eq(base64.b64encode(""), "")
68 eq(base64.b64encode("abcdefghijklmnopqrstuvwxyz"
69 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
70 "0123456789!@#0^&*();:<>,. []{}"),
71 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
72 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
73 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
74 # Test with arbitrary alternative characters
75 eq(base64.b64encode('\xd3V\xbeo\xf7\x1d', altchars='*$'), '01a*b$cd')
76 # Test standard alphabet
77 eq(base64.standard_b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
78 eq(base64.standard_b64encode("a"), "YQ==")
79 eq(base64.standard_b64encode("ab"), "YWI=")
80 eq(base64.standard_b64encode("abc"), "YWJj")
81 eq(base64.standard_b64encode(""), "")
82 eq(base64.standard_b64encode("abcdefghijklmnopqrstuvwxyz"
83 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
84 "0123456789!@#0^&*();:<>,. []{}"),
85 "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
86 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
87 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
88 # Test with 'URL safe' alternative characters
89 eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd')
91 def test_b64decode(self):
92 eq = self.assertEqual
93 eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
94 eq(base64.b64decode('AA=='), '\x00')
95 eq(base64.b64decode("YQ=="), "a")
96 eq(base64.b64decode("YWI="), "ab")
97 eq(base64.b64decode("YWJj"), "abc")
98 eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
99 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
100 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
101 "abcdefghijklmnopqrstuvwxyz"
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
103 "0123456789!@#0^&*();:<>,. []{}")
104 eq(base64.b64decode(''), '')
105 # Test with arbitrary alternative characters
106 eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
107 # Test standard alphabet
108 eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
109 eq(base64.standard_b64decode("YQ=="), "a")
110 eq(base64.standard_b64decode("YWI="), "ab")
111 eq(base64.standard_b64decode("YWJj"), "abc")
112 eq(base64.standard_b64decode(""), "")
113 eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
114 "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
115 "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
116 "abcdefghijklmnopqrstuvwxyz"
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118 "0123456789!@#0^&*();:<>,. []{}")
119 # Test with 'URL safe' alternative characters
120 eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
122 def test_b64decode_error(self):
123 self.assertRaises(TypeError, base64.b64decode, 'abc')
125 def test_b32encode(self):
126 eq = self.assertEqual
127 eq(base64.b32encode(''), '')
128 eq(base64.b32encode('\x00'), 'AA======')
129 eq(base64.b32encode('a'), 'ME======')
130 eq(base64.b32encode('ab'), 'MFRA====')
131 eq(base64.b32encode('abc'), 'MFRGG===')
132 eq(base64.b32encode('abcd'), 'MFRGGZA=')
133 eq(base64.b32encode('abcde'), 'MFRGGZDF')
135 def test_b32decode(self):
136 eq = self.assertEqual
137 eq(base64.b32decode(''), '')
138 eq(base64.b32decode('AA======'), '\x00')
139 eq(base64.b32decode('ME======'), 'a')
140 eq(base64.b32decode('MFRA===='), 'ab')
141 eq(base64.b32decode('MFRGG==='), 'abc')
142 eq(base64.b32decode('MFRGGZA='), 'abcd')
143 eq(base64.b32decode('MFRGGZDF'), 'abcde')
145 def test_b32decode_casefold(self):
146 eq = self.assertEqual
147 eq(base64.b32decode('', True), '')
148 eq(base64.b32decode('ME======', True), 'a')
149 eq(base64.b32decode('MFRA====', True), 'ab')
150 eq(base64.b32decode('MFRGG===', True), 'abc')
151 eq(base64.b32decode('MFRGGZA=', True), 'abcd')
152 eq(base64.b32decode('MFRGGZDF', True), 'abcde')
153 # Lower cases
154 eq(base64.b32decode('me======', True), 'a')
155 eq(base64.b32decode('mfra====', True), 'ab')
156 eq(base64.b32decode('mfrgg===', True), 'abc')
157 eq(base64.b32decode('mfrggza=', True), 'abcd')
158 eq(base64.b32decode('mfrggzdf', True), 'abcde')
159 # Expected exceptions
160 self.assertRaises(TypeError, base64.b32decode, 'me======')
161 # Mapping zero and one
162 eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe')
163 eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe')
164 eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe')
166 def test_b32decode_error(self):
167 self.assertRaises(TypeError, base64.b32decode, 'abc')
168 self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==')
170 def test_b16encode(self):
171 eq = self.assertEqual
172 eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF')
173 eq(base64.b16encode('\x00'), '00')
175 def test_b16decode(self):
176 eq = self.assertEqual
177 eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef')
178 eq(base64.b16decode('00'), '\x00')
179 # Lower case is not allowed without a flag
180 self.assertRaises(TypeError, base64.b16decode, '0102abcdef')
181 # Case fold
182 eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef')
186 def test_main():
187 test_support.run_unittest(__name__)
189 if __name__ == '__main__':
190 test_main()