• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import test_support
3import base64
4
5
6
7class 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")
21        # Non-bytes
22        eq(base64.encodestring(bytearray('abc')), 'YWJj\n')
23
24    def test_decodestring(self):
25        eq = self.assertEqual
26        eq(base64.decodestring("d3d3LnB5dGhvbi5vcmc=\n"), "www.python.org")
27        eq(base64.decodestring("YQ==\n"), "a")
28        eq(base64.decodestring("YWI=\n"), "ab")
29        eq(base64.decodestring("YWJj\n"), "abc")
30        eq(base64.decodestring("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
31                               "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
32                               "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
33           "abcdefghijklmnopqrstuvwxyz"
34           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35           "0123456789!@#0^&*();:<>,. []{}")
36        eq(base64.decodestring(''), '')
37        # Non-bytes
38        eq(base64.decodestring(bytearray("YWJj\n")), "abc")
39
40    def test_encode(self):
41        eq = self.assertEqual
42        from cStringIO import StringIO
43        infp = StringIO('abcdefghijklmnopqrstuvwxyz'
44                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
45                        '0123456789!@#0^&*();:<>,. []{}')
46        outfp = StringIO()
47        base64.encode(infp, outfp)
48        eq(outfp.getvalue(),
49           'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
50           'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
51           'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
52
53    def test_decode(self):
54        from cStringIO import StringIO
55        infp = StringIO('d3d3LnB5dGhvbi5vcmc=')
56        outfp = StringIO()
57        base64.decode(infp, outfp)
58        self.assertEqual(outfp.getvalue(), 'www.python.org')
59
60
61
62class BaseXYTestCase(unittest.TestCase):
63    def test_b64encode(self):
64        eq = self.assertEqual
65        # Test default alphabet
66        eq(base64.b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
67        eq(base64.b64encode('\x00'), 'AA==')
68        eq(base64.b64encode("a"), "YQ==")
69        eq(base64.b64encode("ab"), "YWI=")
70        eq(base64.b64encode("abc"), "YWJj")
71        eq(base64.b64encode(""), "")
72        eq(base64.b64encode("abcdefghijklmnopqrstuvwxyz"
73                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
74                            "0123456789!@#0^&*();:<>,. []{}"),
75           "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
76           "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
77           "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
78        # Test with arbitrary alternative characters
79        eq(base64.b64encode('\xd3V\xbeo\xf7\x1d', altchars='*$'), '01a*b$cd')
80        # Non-bytes
81        eq(base64.b64encode(bytearray('abcd')), 'YWJjZA==')
82        self.assertRaises(TypeError, base64.b64encode,
83                          '\xd3V\xbeo\xf7\x1d', altchars=bytearray('*$'))
84        # Test standard alphabet
85        eq(base64.standard_b64encode("www.python.org"), "d3d3LnB5dGhvbi5vcmc=")
86        eq(base64.standard_b64encode("a"), "YQ==")
87        eq(base64.standard_b64encode("ab"), "YWI=")
88        eq(base64.standard_b64encode("abc"), "YWJj")
89        eq(base64.standard_b64encode(""), "")
90        eq(base64.standard_b64encode("abcdefghijklmnopqrstuvwxyz"
91                                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
92                                     "0123456789!@#0^&*();:<>,. []{}"),
93           "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
94           "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
95           "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
96        # Non-bytes
97        eq(base64.standard_b64encode(bytearray('abcd')), 'YWJjZA==')
98        # Test with 'URL safe' alternative characters
99        eq(base64.urlsafe_b64encode('\xd3V\xbeo\xf7\x1d'), '01a-b_cd')
100        # Non-bytes
101        eq(base64.urlsafe_b64encode(bytearray('\xd3V\xbeo\xf7\x1d')), '01a-b_cd')
102
103    def test_b64decode(self):
104        eq = self.assertEqual
105        eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
106        eq(base64.b64decode('AA=='), '\x00')
107        eq(base64.b64decode("YQ=="), "a")
108        eq(base64.b64decode("YWI="), "ab")
109        eq(base64.b64decode("YWJj"), "abc")
110        eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
111                            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
112                            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
113           "abcdefghijklmnopqrstuvwxyz"
114           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
115           "0123456789!@#0^&*();:<>,. []{}")
116        eq(base64.b64decode(''), '')
117        # Test with arbitrary alternative characters
118        eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
119        # Non-bytes
120        eq(base64.b64decode(bytearray("YWJj")), "abc")
121        # Test standard alphabet
122        eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
123        eq(base64.standard_b64decode("YQ=="), "a")
124        eq(base64.standard_b64decode("YWI="), "ab")
125        eq(base64.standard_b64decode("YWJj"), "abc")
126        eq(base64.standard_b64decode(""), "")
127        eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
128                                     "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
129                                     "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
130           "abcdefghijklmnopqrstuvwxyz"
131           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
132           "0123456789!@#0^&*();:<>,. []{}")
133        # Non-bytes
134        eq(base64.standard_b64decode(bytearray("YWJj")), "abc")
135        # Test with 'URL safe' alternative characters
136        eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
137        # Non-bytes
138        eq(base64.urlsafe_b64decode(bytearray('01a-b_cd')), '\xd3V\xbeo\xf7\x1d')
139
140    def test_b64decode_padding_error(self):
141        self.assertRaises(TypeError, base64.b64decode, 'abc')
142
143    def test_b64decode_invalid_chars(self):
144        # issue 1466065: Test some invalid characters.
145        tests = ((b'%3d==', b'\xdd'),
146                 (b'$3d==', b'\xdd'),
147                 (b'[==', b''),
148                 (b'YW]3=', b'am'),
149                 (b'3{d==', b'\xdd'),
150                 (b'3d}==', b'\xdd'),
151                 (b'@@', b''),
152                 (b'!', b''),
153                 (b'YWJj\nYWI=', b'abcab'))
154        for bstr, res in tests:
155            self.assertEqual(base64.b64decode(bstr), res)
156            self.assertEqual(base64.standard_b64decode(bstr), res)
157            self.assertEqual(base64.urlsafe_b64decode(bstr), res)
158
159        # Normal alphabet characters not discarded when alternative given
160        res = b'\xFB\xEF\xBE\xFF\xFF\xFF'
161        self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
162        self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res)
163
164    def test_b32encode(self):
165        eq = self.assertEqual
166        eq(base64.b32encode(''), '')
167        eq(base64.b32encode('\x00'), 'AA======')
168        eq(base64.b32encode('a'), 'ME======')
169        eq(base64.b32encode('ab'), 'MFRA====')
170        eq(base64.b32encode('abc'), 'MFRGG===')
171        eq(base64.b32encode('abcd'), 'MFRGGZA=')
172        eq(base64.b32encode('abcde'), 'MFRGGZDF')
173        # Non-bytes
174        eq(base64.b32encode(bytearray('abcd')), 'MFRGGZA=')
175
176    def test_b32decode(self):
177        eq = self.assertEqual
178        eq(base64.b32decode(''), '')
179        eq(base64.b32decode('AA======'), '\x00')
180        eq(base64.b32decode('ME======'), 'a')
181        eq(base64.b32decode('MFRA===='), 'ab')
182        eq(base64.b32decode('MFRGG==='), 'abc')
183        eq(base64.b32decode('MFRGGZA='), 'abcd')
184        eq(base64.b32decode('MFRGGZDF'), 'abcde')
185        # Non-bytes
186        self.assertRaises(TypeError, base64.b32decode, bytearray('MFRGG==='))
187
188    def test_b32decode_casefold(self):
189        eq = self.assertEqual
190        eq(base64.b32decode('', True), '')
191        eq(base64.b32decode('ME======', True), 'a')
192        eq(base64.b32decode('MFRA====', True), 'ab')
193        eq(base64.b32decode('MFRGG===', True), 'abc')
194        eq(base64.b32decode('MFRGGZA=', True), 'abcd')
195        eq(base64.b32decode('MFRGGZDF', True), 'abcde')
196        # Lower cases
197        eq(base64.b32decode('me======', True), 'a')
198        eq(base64.b32decode('mfra====', True), 'ab')
199        eq(base64.b32decode('mfrgg===', True), 'abc')
200        eq(base64.b32decode('mfrggza=', True), 'abcd')
201        eq(base64.b32decode('mfrggzdf', True), 'abcde')
202        # Expected exceptions
203        self.assertRaises(TypeError, base64.b32decode, 'me======')
204        # Mapping zero and one
205        eq(base64.b32decode('MLO23456'), 'b\xdd\xad\xf3\xbe')
206        eq(base64.b32decode('M1023456', map01='L'), 'b\xdd\xad\xf3\xbe')
207        eq(base64.b32decode('M1023456', map01='I'), 'b\x1d\xad\xf3\xbe')
208
209    def test_b32decode_error(self):
210        self.assertRaises(TypeError, base64.b32decode, 'abc')
211        self.assertRaises(TypeError, base64.b32decode, 'ABCDEF==')
212
213    def test_b16encode(self):
214        eq = self.assertEqual
215        eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF')
216        eq(base64.b16encode('\x00'), '00')
217        # Non-bytes
218        eq(base64.b16encode(bytearray('\x01\x02\xab\xcd\xef')), '0102ABCDEF')
219
220    def test_b16decode(self):
221        eq = self.assertEqual
222        eq(base64.b16decode('0102ABCDEF'), '\x01\x02\xab\xcd\xef')
223        eq(base64.b16decode('00'), '\x00')
224        # Lower case is not allowed without a flag
225        self.assertRaises(TypeError, base64.b16decode, '0102abcdef')
226        # Case fold
227        eq(base64.b16decode('0102abcdef', True), '\x01\x02\xab\xcd\xef')
228        # Non-bytes
229        eq(base64.b16decode(bytearray("0102ABCDEF")), '\x01\x02\xab\xcd\xef')
230        # Non-alphabet characters
231        self.assertRaises(TypeError, base64.b16decode, '0102AG')
232        # Incorrect "padding"
233        self.assertRaises(TypeError, base64.b16decode, '010')
234
235
236
237def test_main():
238    test_support.run_unittest(__name__)
239
240if __name__ == '__main__':
241    test_main()
242