1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | import base64 |
---|
4 | n/a | import binascii |
---|
5 | n/a | import os |
---|
6 | n/a | from array import array |
---|
7 | n/a | from test.support import script_helper |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class LegacyBase64TestCase(unittest.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | # Legacy API is not as permissive as the modern API |
---|
13 | n/a | def check_type_errors(self, f): |
---|
14 | n/a | self.assertRaises(TypeError, f, "") |
---|
15 | n/a | self.assertRaises(TypeError, f, []) |
---|
16 | n/a | multidimensional = memoryview(b"1234").cast('B', (2, 2)) |
---|
17 | n/a | self.assertRaises(TypeError, f, multidimensional) |
---|
18 | n/a | int_data = memoryview(b"1234").cast('I') |
---|
19 | n/a | self.assertRaises(TypeError, f, int_data) |
---|
20 | n/a | |
---|
21 | n/a | def test_encodebytes(self): |
---|
22 | n/a | eq = self.assertEqual |
---|
23 | n/a | eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") |
---|
24 | n/a | eq(base64.encodebytes(b"a"), b"YQ==\n") |
---|
25 | n/a | eq(base64.encodebytes(b"ab"), b"YWI=\n") |
---|
26 | n/a | eq(base64.encodebytes(b"abc"), b"YWJj\n") |
---|
27 | n/a | eq(base64.encodebytes(b""), b"") |
---|
28 | n/a | eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" |
---|
29 | n/a | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
30 | n/a | b"0123456789!@#0^&*();:<>,. []{}"), |
---|
31 | n/a | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
---|
32 | n/a | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
---|
33 | n/a | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") |
---|
34 | n/a | # Non-bytes |
---|
35 | n/a | eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n') |
---|
36 | n/a | eq(base64.encodebytes(memoryview(b'abc')), b'YWJj\n') |
---|
37 | n/a | eq(base64.encodebytes(array('B', b'abc')), b'YWJj\n') |
---|
38 | n/a | self.check_type_errors(base64.encodebytes) |
---|
39 | n/a | |
---|
40 | n/a | def test_decodebytes(self): |
---|
41 | n/a | eq = self.assertEqual |
---|
42 | n/a | eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") |
---|
43 | n/a | eq(base64.decodebytes(b"YQ==\n"), b"a") |
---|
44 | n/a | eq(base64.decodebytes(b"YWI=\n"), b"ab") |
---|
45 | n/a | eq(base64.decodebytes(b"YWJj\n"), b"abc") |
---|
46 | n/a | eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
---|
47 | n/a | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
---|
48 | n/a | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), |
---|
49 | n/a | b"abcdefghijklmnopqrstuvwxyz" |
---|
50 | n/a | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
51 | n/a | b"0123456789!@#0^&*();:<>,. []{}") |
---|
52 | n/a | eq(base64.decodebytes(b''), b'') |
---|
53 | n/a | # Non-bytes |
---|
54 | n/a | eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc') |
---|
55 | n/a | eq(base64.decodebytes(memoryview(b'YWJj\n')), b'abc') |
---|
56 | n/a | eq(base64.decodebytes(array('B', b'YWJj\n')), b'abc') |
---|
57 | n/a | self.check_type_errors(base64.decodebytes) |
---|
58 | n/a | |
---|
59 | n/a | def test_encode(self): |
---|
60 | n/a | eq = self.assertEqual |
---|
61 | n/a | from io import BytesIO, StringIO |
---|
62 | n/a | infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' |
---|
63 | n/a | b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
---|
64 | n/a | b'0123456789!@#0^&*();:<>,. []{}') |
---|
65 | n/a | outfp = BytesIO() |
---|
66 | n/a | base64.encode(infp, outfp) |
---|
67 | n/a | eq(outfp.getvalue(), |
---|
68 | n/a | b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE' |
---|
69 | n/a | b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT' |
---|
70 | n/a | b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n') |
---|
71 | n/a | # Non-binary files |
---|
72 | n/a | self.assertRaises(TypeError, base64.encode, StringIO('abc'), BytesIO()) |
---|
73 | n/a | self.assertRaises(TypeError, base64.encode, BytesIO(b'abc'), StringIO()) |
---|
74 | n/a | self.assertRaises(TypeError, base64.encode, StringIO('abc'), StringIO()) |
---|
75 | n/a | |
---|
76 | n/a | def test_decode(self): |
---|
77 | n/a | from io import BytesIO, StringIO |
---|
78 | n/a | infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=') |
---|
79 | n/a | outfp = BytesIO() |
---|
80 | n/a | base64.decode(infp, outfp) |
---|
81 | n/a | self.assertEqual(outfp.getvalue(), b'www.python.org') |
---|
82 | n/a | # Non-binary files |
---|
83 | n/a | self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), BytesIO()) |
---|
84 | n/a | self.assertRaises(TypeError, base64.encode, BytesIO(b'YWJj\n'), StringIO()) |
---|
85 | n/a | self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), StringIO()) |
---|
86 | n/a | |
---|
87 | n/a | |
---|
88 | n/a | class BaseXYTestCase(unittest.TestCase): |
---|
89 | n/a | |
---|
90 | n/a | # Modern API completely ignores exported dimension and format data and |
---|
91 | n/a | # treats any buffer as a stream of bytes |
---|
92 | n/a | def check_encode_type_errors(self, f): |
---|
93 | n/a | self.assertRaises(TypeError, f, "") |
---|
94 | n/a | self.assertRaises(TypeError, f, []) |
---|
95 | n/a | |
---|
96 | n/a | def check_decode_type_errors(self, f): |
---|
97 | n/a | self.assertRaises(TypeError, f, []) |
---|
98 | n/a | |
---|
99 | n/a | def check_other_types(self, f, bytes_data, expected): |
---|
100 | n/a | eq = self.assertEqual |
---|
101 | n/a | b = bytearray(bytes_data) |
---|
102 | n/a | eq(f(b), expected) |
---|
103 | n/a | # The bytearray wasn't mutated |
---|
104 | n/a | eq(b, bytes_data) |
---|
105 | n/a | eq(f(memoryview(bytes_data)), expected) |
---|
106 | n/a | eq(f(array('B', bytes_data)), expected) |
---|
107 | n/a | # XXX why is b64encode hardcoded here? |
---|
108 | n/a | self.check_nonbyte_element_format(base64.b64encode, bytes_data) |
---|
109 | n/a | self.check_multidimensional(base64.b64encode, bytes_data) |
---|
110 | n/a | |
---|
111 | n/a | def check_multidimensional(self, f, data): |
---|
112 | n/a | padding = b"\x00" if len(data) % 2 else b"" |
---|
113 | n/a | bytes_data = data + padding # Make sure cast works |
---|
114 | n/a | shape = (len(bytes_data) // 2, 2) |
---|
115 | n/a | multidimensional = memoryview(bytes_data).cast('B', shape) |
---|
116 | n/a | self.assertEqual(f(multidimensional), f(bytes_data)) |
---|
117 | n/a | |
---|
118 | n/a | def check_nonbyte_element_format(self, f, data): |
---|
119 | n/a | padding = b"\x00" * ((4 - len(data)) % 4) |
---|
120 | n/a | bytes_data = data + padding # Make sure cast works |
---|
121 | n/a | int_data = memoryview(bytes_data).cast('I') |
---|
122 | n/a | self.assertEqual(f(int_data), f(bytes_data)) |
---|
123 | n/a | |
---|
124 | n/a | |
---|
125 | n/a | def test_b64encode(self): |
---|
126 | n/a | eq = self.assertEqual |
---|
127 | n/a | # Test default alphabet |
---|
128 | n/a | eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") |
---|
129 | n/a | eq(base64.b64encode(b'\x00'), b'AA==') |
---|
130 | n/a | eq(base64.b64encode(b"a"), b"YQ==") |
---|
131 | n/a | eq(base64.b64encode(b"ab"), b"YWI=") |
---|
132 | n/a | eq(base64.b64encode(b"abc"), b"YWJj") |
---|
133 | n/a | eq(base64.b64encode(b""), b"") |
---|
134 | n/a | eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" |
---|
135 | n/a | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
136 | n/a | b"0123456789!@#0^&*();:<>,. []{}"), |
---|
137 | n/a | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
---|
138 | n/a | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" |
---|
139 | n/a | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") |
---|
140 | n/a | # Test with arbitrary alternative characters |
---|
141 | n/a | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd') |
---|
142 | n/a | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')), |
---|
143 | n/a | b'01a*b$cd') |
---|
144 | n/a | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=memoryview(b'*$')), |
---|
145 | n/a | b'01a*b$cd') |
---|
146 | n/a | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=array('B', b'*$')), |
---|
147 | n/a | b'01a*b$cd') |
---|
148 | n/a | # Non-bytes |
---|
149 | n/a | self.check_other_types(base64.b64encode, b'abcd', b'YWJjZA==') |
---|
150 | n/a | self.check_encode_type_errors(base64.b64encode) |
---|
151 | n/a | self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$") |
---|
152 | n/a | # Test standard alphabet |
---|
153 | n/a | eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") |
---|
154 | n/a | eq(base64.standard_b64encode(b"a"), b"YQ==") |
---|
155 | n/a | eq(base64.standard_b64encode(b"ab"), b"YWI=") |
---|
156 | n/a | eq(base64.standard_b64encode(b"abc"), b"YWJj") |
---|
157 | n/a | eq(base64.standard_b64encode(b""), b"") |
---|
158 | n/a | eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" |
---|
159 | n/a | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
160 | n/a | b"0123456789!@#0^&*();:<>,. []{}"), |
---|
161 | n/a | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
---|
162 | n/a | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" |
---|
163 | n/a | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") |
---|
164 | n/a | # Non-bytes |
---|
165 | n/a | self.check_other_types(base64.standard_b64encode, |
---|
166 | n/a | b'abcd', b'YWJjZA==') |
---|
167 | n/a | self.check_encode_type_errors(base64.standard_b64encode) |
---|
168 | n/a | # Test with 'URL safe' alternative characters |
---|
169 | n/a | eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd') |
---|
170 | n/a | # Non-bytes |
---|
171 | n/a | self.check_other_types(base64.urlsafe_b64encode, |
---|
172 | n/a | b'\xd3V\xbeo\xf7\x1d', b'01a-b_cd') |
---|
173 | n/a | self.check_encode_type_errors(base64.urlsafe_b64encode) |
---|
174 | n/a | |
---|
175 | n/a | def test_b64decode(self): |
---|
176 | n/a | eq = self.assertEqual |
---|
177 | n/a | |
---|
178 | n/a | tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", |
---|
179 | n/a | b'AA==': b'\x00', |
---|
180 | n/a | b"YQ==": b"a", |
---|
181 | n/a | b"YWI=": b"ab", |
---|
182 | n/a | b"YWJj": b"abc", |
---|
183 | n/a | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
---|
184 | n/a | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
---|
185 | n/a | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==": |
---|
186 | n/a | |
---|
187 | n/a | b"abcdefghijklmnopqrstuvwxyz" |
---|
188 | n/a | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
189 | n/a | b"0123456789!@#0^&*();:<>,. []{}", |
---|
190 | n/a | b'': b'', |
---|
191 | n/a | } |
---|
192 | n/a | for data, res in tests.items(): |
---|
193 | n/a | eq(base64.b64decode(data), res) |
---|
194 | n/a | eq(base64.b64decode(data.decode('ascii')), res) |
---|
195 | n/a | # Non-bytes |
---|
196 | n/a | self.check_other_types(base64.b64decode, b"YWJj", b"abc") |
---|
197 | n/a | self.check_decode_type_errors(base64.b64decode) |
---|
198 | n/a | |
---|
199 | n/a | # Test with arbitrary alternative characters |
---|
200 | n/a | tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d', |
---|
201 | n/a | } |
---|
202 | n/a | for (data, altchars), res in tests_altchars.items(): |
---|
203 | n/a | data_str = data.decode('ascii') |
---|
204 | n/a | altchars_str = altchars.decode('ascii') |
---|
205 | n/a | |
---|
206 | n/a | eq(base64.b64decode(data, altchars=altchars), res) |
---|
207 | n/a | eq(base64.b64decode(data_str, altchars=altchars), res) |
---|
208 | n/a | eq(base64.b64decode(data, altchars=altchars_str), res) |
---|
209 | n/a | eq(base64.b64decode(data_str, altchars=altchars_str), res) |
---|
210 | n/a | |
---|
211 | n/a | # Test standard alphabet |
---|
212 | n/a | for data, res in tests.items(): |
---|
213 | n/a | eq(base64.standard_b64decode(data), res) |
---|
214 | n/a | eq(base64.standard_b64decode(data.decode('ascii')), res) |
---|
215 | n/a | # Non-bytes |
---|
216 | n/a | self.check_other_types(base64.standard_b64decode, b"YWJj", b"abc") |
---|
217 | n/a | self.check_decode_type_errors(base64.standard_b64decode) |
---|
218 | n/a | |
---|
219 | n/a | # Test with 'URL safe' alternative characters |
---|
220 | n/a | tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', |
---|
221 | n/a | b'': b'', |
---|
222 | n/a | } |
---|
223 | n/a | for data, res in tests_urlsafe.items(): |
---|
224 | n/a | eq(base64.urlsafe_b64decode(data), res) |
---|
225 | n/a | eq(base64.urlsafe_b64decode(data.decode('ascii')), res) |
---|
226 | n/a | # Non-bytes |
---|
227 | n/a | self.check_other_types(base64.urlsafe_b64decode, b'01a-b_cd', |
---|
228 | n/a | b'\xd3V\xbeo\xf7\x1d') |
---|
229 | n/a | self.check_decode_type_errors(base64.urlsafe_b64decode) |
---|
230 | n/a | |
---|
231 | n/a | def test_b64decode_padding_error(self): |
---|
232 | n/a | self.assertRaises(binascii.Error, base64.b64decode, b'abc') |
---|
233 | n/a | self.assertRaises(binascii.Error, base64.b64decode, 'abc') |
---|
234 | n/a | |
---|
235 | n/a | def test_b64decode_invalid_chars(self): |
---|
236 | n/a | # issue 1466065: Test some invalid characters. |
---|
237 | n/a | tests = ((b'%3d==', b'\xdd'), |
---|
238 | n/a | (b'$3d==', b'\xdd'), |
---|
239 | n/a | (b'[==', b''), |
---|
240 | n/a | (b'YW]3=', b'am'), |
---|
241 | n/a | (b'3{d==', b'\xdd'), |
---|
242 | n/a | (b'3d}==', b'\xdd'), |
---|
243 | n/a | (b'@@', b''), |
---|
244 | n/a | (b'!', b''), |
---|
245 | n/a | (b'YWJj\nYWI=', b'abcab')) |
---|
246 | n/a | funcs = ( |
---|
247 | n/a | base64.b64decode, |
---|
248 | n/a | base64.standard_b64decode, |
---|
249 | n/a | base64.urlsafe_b64decode, |
---|
250 | n/a | ) |
---|
251 | n/a | for bstr, res in tests: |
---|
252 | n/a | for func in funcs: |
---|
253 | n/a | with self.subTest(bstr=bstr, func=func): |
---|
254 | n/a | self.assertEqual(func(bstr), res) |
---|
255 | n/a | self.assertEqual(func(bstr.decode('ascii')), res) |
---|
256 | n/a | with self.assertRaises(binascii.Error): |
---|
257 | n/a | base64.b64decode(bstr, validate=True) |
---|
258 | n/a | with self.assertRaises(binascii.Error): |
---|
259 | n/a | base64.b64decode(bstr.decode('ascii'), validate=True) |
---|
260 | n/a | |
---|
261 | n/a | # Normal alphabet characters not discarded when alternative given |
---|
262 | n/a | res = b'\xFB\xEF\xBE\xFF\xFF\xFF' |
---|
263 | n/a | self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res) |
---|
264 | n/a | self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res) |
---|
265 | n/a | |
---|
266 | n/a | def test_b32encode(self): |
---|
267 | n/a | eq = self.assertEqual |
---|
268 | n/a | eq(base64.b32encode(b''), b'') |
---|
269 | n/a | eq(base64.b32encode(b'\x00'), b'AA======') |
---|
270 | n/a | eq(base64.b32encode(b'a'), b'ME======') |
---|
271 | n/a | eq(base64.b32encode(b'ab'), b'MFRA====') |
---|
272 | n/a | eq(base64.b32encode(b'abc'), b'MFRGG===') |
---|
273 | n/a | eq(base64.b32encode(b'abcd'), b'MFRGGZA=') |
---|
274 | n/a | eq(base64.b32encode(b'abcde'), b'MFRGGZDF') |
---|
275 | n/a | # Non-bytes |
---|
276 | n/a | self.check_other_types(base64.b32encode, b'abcd', b'MFRGGZA=') |
---|
277 | n/a | self.check_encode_type_errors(base64.b32encode) |
---|
278 | n/a | |
---|
279 | n/a | def test_b32decode(self): |
---|
280 | n/a | eq = self.assertEqual |
---|
281 | n/a | tests = {b'': b'', |
---|
282 | n/a | b'AA======': b'\x00', |
---|
283 | n/a | b'ME======': b'a', |
---|
284 | n/a | b'MFRA====': b'ab', |
---|
285 | n/a | b'MFRGG===': b'abc', |
---|
286 | n/a | b'MFRGGZA=': b'abcd', |
---|
287 | n/a | b'MFRGGZDF': b'abcde', |
---|
288 | n/a | } |
---|
289 | n/a | for data, res in tests.items(): |
---|
290 | n/a | eq(base64.b32decode(data), res) |
---|
291 | n/a | eq(base64.b32decode(data.decode('ascii')), res) |
---|
292 | n/a | # Non-bytes |
---|
293 | n/a | self.check_other_types(base64.b32decode, b'MFRGG===', b"abc") |
---|
294 | n/a | self.check_decode_type_errors(base64.b32decode) |
---|
295 | n/a | |
---|
296 | n/a | def test_b32decode_casefold(self): |
---|
297 | n/a | eq = self.assertEqual |
---|
298 | n/a | tests = {b'': b'', |
---|
299 | n/a | b'ME======': b'a', |
---|
300 | n/a | b'MFRA====': b'ab', |
---|
301 | n/a | b'MFRGG===': b'abc', |
---|
302 | n/a | b'MFRGGZA=': b'abcd', |
---|
303 | n/a | b'MFRGGZDF': b'abcde', |
---|
304 | n/a | # Lower cases |
---|
305 | n/a | b'me======': b'a', |
---|
306 | n/a | b'mfra====': b'ab', |
---|
307 | n/a | b'mfrgg===': b'abc', |
---|
308 | n/a | b'mfrggza=': b'abcd', |
---|
309 | n/a | b'mfrggzdf': b'abcde', |
---|
310 | n/a | } |
---|
311 | n/a | |
---|
312 | n/a | for data, res in tests.items(): |
---|
313 | n/a | eq(base64.b32decode(data, True), res) |
---|
314 | n/a | eq(base64.b32decode(data.decode('ascii'), True), res) |
---|
315 | n/a | |
---|
316 | n/a | self.assertRaises(binascii.Error, base64.b32decode, b'me======') |
---|
317 | n/a | self.assertRaises(binascii.Error, base64.b32decode, 'me======') |
---|
318 | n/a | |
---|
319 | n/a | # Mapping zero and one |
---|
320 | n/a | eq(base64.b32decode(b'MLO23456'), b'b\xdd\xad\xf3\xbe') |
---|
321 | n/a | eq(base64.b32decode('MLO23456'), b'b\xdd\xad\xf3\xbe') |
---|
322 | n/a | |
---|
323 | n/a | map_tests = {(b'M1023456', b'L'): b'b\xdd\xad\xf3\xbe', |
---|
324 | n/a | (b'M1023456', b'I'): b'b\x1d\xad\xf3\xbe', |
---|
325 | n/a | } |
---|
326 | n/a | for (data, map01), res in map_tests.items(): |
---|
327 | n/a | data_str = data.decode('ascii') |
---|
328 | n/a | map01_str = map01.decode('ascii') |
---|
329 | n/a | |
---|
330 | n/a | eq(base64.b32decode(data, map01=map01), res) |
---|
331 | n/a | eq(base64.b32decode(data_str, map01=map01), res) |
---|
332 | n/a | eq(base64.b32decode(data, map01=map01_str), res) |
---|
333 | n/a | eq(base64.b32decode(data_str, map01=map01_str), res) |
---|
334 | n/a | self.assertRaises(binascii.Error, base64.b32decode, data) |
---|
335 | n/a | self.assertRaises(binascii.Error, base64.b32decode, data_str) |
---|
336 | n/a | |
---|
337 | n/a | def test_b32decode_error(self): |
---|
338 | n/a | for data in [b'abc', b'ABCDEF==', b'==ABCDEF']: |
---|
339 | n/a | with self.assertRaises(binascii.Error): |
---|
340 | n/a | base64.b32decode(data) |
---|
341 | n/a | with self.assertRaises(binascii.Error): |
---|
342 | n/a | base64.b32decode(data.decode('ascii')) |
---|
343 | n/a | |
---|
344 | n/a | def test_b16encode(self): |
---|
345 | n/a | eq = self.assertEqual |
---|
346 | n/a | eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') |
---|
347 | n/a | eq(base64.b16encode(b'\x00'), b'00') |
---|
348 | n/a | # Non-bytes |
---|
349 | n/a | self.check_other_types(base64.b16encode, b'\x01\x02\xab\xcd\xef', |
---|
350 | n/a | b'0102ABCDEF') |
---|
351 | n/a | self.check_encode_type_errors(base64.b16encode) |
---|
352 | n/a | |
---|
353 | n/a | def test_b16decode(self): |
---|
354 | n/a | eq = self.assertEqual |
---|
355 | n/a | eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') |
---|
356 | n/a | eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef') |
---|
357 | n/a | eq(base64.b16decode(b'00'), b'\x00') |
---|
358 | n/a | eq(base64.b16decode('00'), b'\x00') |
---|
359 | n/a | # Lower case is not allowed without a flag |
---|
360 | n/a | self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') |
---|
361 | n/a | self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') |
---|
362 | n/a | # Case fold |
---|
363 | n/a | eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') |
---|
364 | n/a | eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') |
---|
365 | n/a | # Non-bytes |
---|
366 | n/a | self.check_other_types(base64.b16decode, b"0102ABCDEF", |
---|
367 | n/a | b'\x01\x02\xab\xcd\xef') |
---|
368 | n/a | self.check_decode_type_errors(base64.b16decode) |
---|
369 | n/a | eq(base64.b16decode(bytearray(b"0102abcdef"), True), |
---|
370 | n/a | b'\x01\x02\xab\xcd\xef') |
---|
371 | n/a | eq(base64.b16decode(memoryview(b"0102abcdef"), True), |
---|
372 | n/a | b'\x01\x02\xab\xcd\xef') |
---|
373 | n/a | eq(base64.b16decode(array('B', b"0102abcdef"), True), |
---|
374 | n/a | b'\x01\x02\xab\xcd\xef') |
---|
375 | n/a | # Non-alphabet characters |
---|
376 | n/a | self.assertRaises(binascii.Error, base64.b16decode, '0102AG') |
---|
377 | n/a | # Incorrect "padding" |
---|
378 | n/a | self.assertRaises(binascii.Error, base64.b16decode, '010') |
---|
379 | n/a | |
---|
380 | n/a | def test_a85encode(self): |
---|
381 | n/a | eq = self.assertEqual |
---|
382 | n/a | |
---|
383 | n/a | tests = { |
---|
384 | n/a | b'': b'', |
---|
385 | n/a | b"www.python.org": b'GB\\6`E-ZP=Df.1GEb>', |
---|
386 | n/a | bytes(range(255)): b"""!!*-'"9eu7#RLhG$k3[W&.oNg'GVB"(`=52*$$""" |
---|
387 | n/a | b"""(B+<_pR,UFcb-n-Vr/1iJ-0JP==1c70M3&s#]4?Ykm5X@_(6q'R884cE""" |
---|
388 | n/a | b"""H9MJ8X:f1+h<)lt#=BSg3>[:ZC?t!MSA7]@cBPD3sCi+'.E,fo>FEMbN""" |
---|
389 | n/a | b"""G^4U^I!pHnJ:W<)KS>/9Ll%"IN/`jYOHG]iPa.Q$R$jD4S=Q7DTV8*TU""" |
---|
390 | n/a | b"""nsrdW2ZetXKAY/Yd(L?['d?O\\@K2_]Y2%o^qmn*`5Ta:aN;TJbg"GZd""" |
---|
391 | n/a | b"""*^:jeCE.%f\\,!5gtgiEi8N\\UjQ5OekiqBum-X60nF?)@o_%qPq"ad`""" |
---|
392 | n/a | b"""r;HT""", |
---|
393 | n/a | b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
394 | n/a | b"0123456789!@#0^&*();:<>,. []{}": |
---|
395 | n/a | b'@:E_WAS,RgBkhF"D/O92EH6,BF`qtRH$VbC6UX@47n?3D92&&T' |
---|
396 | n/a | b":Jand;cHat='/U/0JP==1c70M3&r-I,;<FN.OZ`-3]oSW/g+A(H[P", |
---|
397 | n/a | b"no padding..": b'DJpY:@:Wn_DJ(RS', |
---|
398 | n/a | b"zero compression\0\0\0\0": b'H=_,8+Cf>,E,oN2F(oQ1z', |
---|
399 | n/a | b"zero compression\0\0\0": b'H=_,8+Cf>,E,oN2F(oQ1!!!!', |
---|
400 | n/a | b"Boundary:\0\0\0\0": b'6>q!aA79M(3WK-[!!', |
---|
401 | n/a | b"Space compr: ": b';fH/TAKYK$D/aMV+<VdL', |
---|
402 | n/a | b'\xff': b'rr', |
---|
403 | n/a | b'\xff'*2: b's8N', |
---|
404 | n/a | b'\xff'*3: b's8W*', |
---|
405 | n/a | b'\xff'*4: b's8W-!', |
---|
406 | n/a | } |
---|
407 | n/a | |
---|
408 | n/a | for data, res in tests.items(): |
---|
409 | n/a | eq(base64.a85encode(data), res, data) |
---|
410 | n/a | eq(base64.a85encode(data, adobe=False), res, data) |
---|
411 | n/a | eq(base64.a85encode(data, adobe=True), b'<~' + res + b'~>', data) |
---|
412 | n/a | |
---|
413 | n/a | self.check_other_types(base64.a85encode, b"www.python.org", |
---|
414 | n/a | b'GB\\6`E-ZP=Df.1GEb>') |
---|
415 | n/a | |
---|
416 | n/a | self.assertRaises(TypeError, base64.a85encode, "") |
---|
417 | n/a | |
---|
418 | n/a | eq(base64.a85encode(b"www.python.org", wrapcol=7, adobe=False), |
---|
419 | n/a | b'GB\\6`E-\nZP=Df.1\nGEb>') |
---|
420 | n/a | eq(base64.a85encode(b"\0\0\0\0www.python.org", wrapcol=7, adobe=False), |
---|
421 | n/a | b'zGB\\6`E\n-ZP=Df.\n1GEb>') |
---|
422 | n/a | eq(base64.a85encode(b"www.python.org", wrapcol=7, adobe=True), |
---|
423 | n/a | b'<~GB\\6`\nE-ZP=Df\n.1GEb>\n~>') |
---|
424 | n/a | |
---|
425 | n/a | eq(base64.a85encode(b' '*8, foldspaces=True, adobe=False), b'yy') |
---|
426 | n/a | eq(base64.a85encode(b' '*7, foldspaces=True, adobe=False), b'y+<Vd') |
---|
427 | n/a | eq(base64.a85encode(b' '*6, foldspaces=True, adobe=False), b'y+<U') |
---|
428 | n/a | eq(base64.a85encode(b' '*5, foldspaces=True, adobe=False), b'y+9') |
---|
429 | n/a | |
---|
430 | n/a | def test_b85encode(self): |
---|
431 | n/a | eq = self.assertEqual |
---|
432 | n/a | |
---|
433 | n/a | tests = { |
---|
434 | n/a | b'': b'', |
---|
435 | n/a | b'www.python.org': b'cXxL#aCvlSZ*DGca%T', |
---|
436 | n/a | bytes(range(255)): b"""009C61O)~M2nh-c3=Iws5D^j+6crX17#SKH9337X""" |
---|
437 | n/a | b"""AR!_nBqb&%C@Cr{EG;fCFflSSG&MFiI5|2yJUu=?KtV!7L`6nNNJ&ad""" |
---|
438 | n/a | b"""OifNtP*GA-R8>}2SXo+ITwPvYU}0ioWMyV&XlZI|Y;A6DaB*^Tbai%j""" |
---|
439 | n/a | b"""czJqze0_d@fPsR8goTEOh>41ejE#<ukdcy;l$Dm3n3<ZJoSmMZprN9p""" |
---|
440 | n/a | b"""q@|{(sHv)}tgWuEu(7hUw6(UkxVgH!yuH4^z`?@9#Kp$P$jQpf%+1cv""" |
---|
441 | n/a | b"""(9zP<)YaD4*xB0K+}+;a;Njxq<mKk)=;`X~?CtLF@bU8V^!4`l`1$(#""" |
---|
442 | n/a | b"""{Qdp""", |
---|
443 | n/a | b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ""" |
---|
444 | n/a | b"""0123456789!@#0^&*();:<>,. []{}""": |
---|
445 | n/a | b"""VPa!sWoBn+X=-b1ZEkOHadLBXb#`}nd3r%YLqtVJM@UIZOH55pPf$@(""" |
---|
446 | n/a | b"""Q&d$}S6EqEFflSSG&MFiI5{CeBQRbjDkv#CIy^osE+AW7dwl""", |
---|
447 | n/a | b'no padding..': b'Zf_uPVPs@!Zf7no', |
---|
448 | n/a | b'zero compression\x00\x00\x00\x00': b'dS!BNAY*TBaB^jHb7^mG00000', |
---|
449 | n/a | b'zero compression\x00\x00\x00': b'dS!BNAY*TBaB^jHb7^mG0000', |
---|
450 | n/a | b"""Boundary:\x00\x00\x00\x00""": b"""LT`0$WMOi7IsgCw00""", |
---|
451 | n/a | b'Space compr: ': b'Q*dEpWgug3ZE$irARr(h', |
---|
452 | n/a | b'\xff': b'{{', |
---|
453 | n/a | b'\xff'*2: b'|Nj', |
---|
454 | n/a | b'\xff'*3: b'|Ns9', |
---|
455 | n/a | b'\xff'*4: b'|NsC0', |
---|
456 | n/a | } |
---|
457 | n/a | |
---|
458 | n/a | for data, res in tests.items(): |
---|
459 | n/a | eq(base64.b85encode(data), res) |
---|
460 | n/a | |
---|
461 | n/a | self.check_other_types(base64.b85encode, b"www.python.org", |
---|
462 | n/a | b'cXxL#aCvlSZ*DGca%T') |
---|
463 | n/a | |
---|
464 | n/a | def test_a85decode(self): |
---|
465 | n/a | eq = self.assertEqual |
---|
466 | n/a | |
---|
467 | n/a | tests = { |
---|
468 | n/a | b'': b'', |
---|
469 | n/a | b'GB\\6`E-ZP=Df.1GEb>': b'www.python.org', |
---|
470 | n/a | b"""! ! * -'"\n\t\t9eu\r\n7# RL\vhG$k3[W&.oNg'GVB"(`=52*$$""" |
---|
471 | n/a | b"""(B+<_pR,UFcb-n-Vr/1iJ-0JP==1c70M3&s#]4?Ykm5X@_(6q'R884cE""" |
---|
472 | n/a | b"""H9MJ8X:f1+h<)lt#=BSg3>[:ZC?t!MSA7]@cBPD3sCi+'.E,fo>FEMbN""" |
---|
473 | n/a | b"""G^4U^I!pHnJ:W<)KS>/9Ll%"IN/`jYOHG]iPa.Q$R$jD4S=Q7DTV8*TU""" |
---|
474 | n/a | b"""nsrdW2ZetXKAY/Yd(L?['d?O\\@K2_]Y2%o^qmn*`5Ta:aN;TJbg"GZd""" |
---|
475 | n/a | b"""*^:jeCE.%f\\,!5gtgiEi8N\\UjQ5OekiqBum-X60nF?)@o_%qPq"ad`""" |
---|
476 | n/a | b"""r;HT""": bytes(range(255)), |
---|
477 | n/a | b"""@:E_WAS,RgBkhF"D/O92EH6,BF`qtRH$VbC6UX@47n?3D92&&T:Jand;c""" |
---|
478 | n/a | b"""Hat='/U/0JP==1c70M3&r-I,;<FN.OZ`-3]oSW/g+A(H[P""": |
---|
479 | n/a | b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234' |
---|
480 | n/a | b'56789!@#0^&*();:<>,. []{}', |
---|
481 | n/a | b'DJpY:@:Wn_DJ(RS': b'no padding..', |
---|
482 | n/a | b'H=_,8+Cf>,E,oN2F(oQ1z': b'zero compression\x00\x00\x00\x00', |
---|
483 | n/a | b'H=_,8+Cf>,E,oN2F(oQ1!!!!': b'zero compression\x00\x00\x00', |
---|
484 | n/a | b'6>q!aA79M(3WK-[!!': b"Boundary:\x00\x00\x00\x00", |
---|
485 | n/a | b';fH/TAKYK$D/aMV+<VdL': b'Space compr: ', |
---|
486 | n/a | b'rr': b'\xff', |
---|
487 | n/a | b's8N': b'\xff'*2, |
---|
488 | n/a | b's8W*': b'\xff'*3, |
---|
489 | n/a | b's8W-!': b'\xff'*4, |
---|
490 | n/a | } |
---|
491 | n/a | |
---|
492 | n/a | for data, res in tests.items(): |
---|
493 | n/a | eq(base64.a85decode(data), res, data) |
---|
494 | n/a | eq(base64.a85decode(data, adobe=False), res, data) |
---|
495 | n/a | eq(base64.a85decode(data.decode("ascii"), adobe=False), res, data) |
---|
496 | n/a | eq(base64.a85decode(b'<~' + data + b'~>', adobe=True), res, data) |
---|
497 | n/a | eq(base64.a85decode(data + b'~>', adobe=True), res, data) |
---|
498 | n/a | eq(base64.a85decode('<~%s~>' % data.decode("ascii"), adobe=True), |
---|
499 | n/a | res, data) |
---|
500 | n/a | |
---|
501 | n/a | eq(base64.a85decode(b'yy', foldspaces=True, adobe=False), b' '*8) |
---|
502 | n/a | eq(base64.a85decode(b'y+<Vd', foldspaces=True, adobe=False), b' '*7) |
---|
503 | n/a | eq(base64.a85decode(b'y+<U', foldspaces=True, adobe=False), b' '*6) |
---|
504 | n/a | eq(base64.a85decode(b'y+9', foldspaces=True, adobe=False), b' '*5) |
---|
505 | n/a | |
---|
506 | n/a | self.check_other_types(base64.a85decode, b'GB\\6`E-ZP=Df.1GEb>', |
---|
507 | n/a | b"www.python.org") |
---|
508 | n/a | |
---|
509 | n/a | def test_b85decode(self): |
---|
510 | n/a | eq = self.assertEqual |
---|
511 | n/a | |
---|
512 | n/a | tests = { |
---|
513 | n/a | b'': b'', |
---|
514 | n/a | b'cXxL#aCvlSZ*DGca%T': b'www.python.org', |
---|
515 | n/a | b"""009C61O)~M2nh-c3=Iws5D^j+6crX17#SKH9337X""" |
---|
516 | n/a | b"""AR!_nBqb&%C@Cr{EG;fCFflSSG&MFiI5|2yJUu=?KtV!7L`6nNNJ&ad""" |
---|
517 | n/a | b"""OifNtP*GA-R8>}2SXo+ITwPvYU}0ioWMyV&XlZI|Y;A6DaB*^Tbai%j""" |
---|
518 | n/a | b"""czJqze0_d@fPsR8goTEOh>41ejE#<ukdcy;l$Dm3n3<ZJoSmMZprN9p""" |
---|
519 | n/a | b"""q@|{(sHv)}tgWuEu(7hUw6(UkxVgH!yuH4^z`?@9#Kp$P$jQpf%+1cv""" |
---|
520 | n/a | b"""(9zP<)YaD4*xB0K+}+;a;Njxq<mKk)=;`X~?CtLF@bU8V^!4`l`1$(#""" |
---|
521 | n/a | b"""{Qdp""": bytes(range(255)), |
---|
522 | n/a | b"""VPa!sWoBn+X=-b1ZEkOHadLBXb#`}nd3r%YLqtVJM@UIZOH55pPf$@(""" |
---|
523 | n/a | b"""Q&d$}S6EqEFflSSG&MFiI5{CeBQRbjDkv#CIy^osE+AW7dwl""": |
---|
524 | n/a | b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ""" |
---|
525 | n/a | b"""0123456789!@#0^&*();:<>,. []{}""", |
---|
526 | n/a | b'Zf_uPVPs@!Zf7no': b'no padding..', |
---|
527 | n/a | b'dS!BNAY*TBaB^jHb7^mG00000': b'zero compression\x00\x00\x00\x00', |
---|
528 | n/a | b'dS!BNAY*TBaB^jHb7^mG0000': b'zero compression\x00\x00\x00', |
---|
529 | n/a | b"""LT`0$WMOi7IsgCw00""": b"""Boundary:\x00\x00\x00\x00""", |
---|
530 | n/a | b'Q*dEpWgug3ZE$irARr(h': b'Space compr: ', |
---|
531 | n/a | b'{{': b'\xff', |
---|
532 | n/a | b'|Nj': b'\xff'*2, |
---|
533 | n/a | b'|Ns9': b'\xff'*3, |
---|
534 | n/a | b'|NsC0': b'\xff'*4, |
---|
535 | n/a | } |
---|
536 | n/a | |
---|
537 | n/a | for data, res in tests.items(): |
---|
538 | n/a | eq(base64.b85decode(data), res) |
---|
539 | n/a | eq(base64.b85decode(data.decode("ascii")), res) |
---|
540 | n/a | |
---|
541 | n/a | self.check_other_types(base64.b85decode, b'cXxL#aCvlSZ*DGca%T', |
---|
542 | n/a | b"www.python.org") |
---|
543 | n/a | |
---|
544 | n/a | def test_a85_padding(self): |
---|
545 | n/a | eq = self.assertEqual |
---|
546 | n/a | |
---|
547 | n/a | eq(base64.a85encode(b"x", pad=True), b'GQ7^D') |
---|
548 | n/a | eq(base64.a85encode(b"xx", pad=True), b"G^'2g") |
---|
549 | n/a | eq(base64.a85encode(b"xxx", pad=True), b'G^+H5') |
---|
550 | n/a | eq(base64.a85encode(b"xxxx", pad=True), b'G^+IX') |
---|
551 | n/a | eq(base64.a85encode(b"xxxxx", pad=True), b'G^+IXGQ7^D') |
---|
552 | n/a | |
---|
553 | n/a | eq(base64.a85decode(b'GQ7^D'), b"x\x00\x00\x00") |
---|
554 | n/a | eq(base64.a85decode(b"G^'2g"), b"xx\x00\x00") |
---|
555 | n/a | eq(base64.a85decode(b'G^+H5'), b"xxx\x00") |
---|
556 | n/a | eq(base64.a85decode(b'G^+IX'), b"xxxx") |
---|
557 | n/a | eq(base64.a85decode(b'G^+IXGQ7^D'), b"xxxxx\x00\x00\x00") |
---|
558 | n/a | |
---|
559 | n/a | def test_b85_padding(self): |
---|
560 | n/a | eq = self.assertEqual |
---|
561 | n/a | |
---|
562 | n/a | eq(base64.b85encode(b"x", pad=True), b'cmMzZ') |
---|
563 | n/a | eq(base64.b85encode(b"xx", pad=True), b'cz6H+') |
---|
564 | n/a | eq(base64.b85encode(b"xxx", pad=True), b'czAdK') |
---|
565 | n/a | eq(base64.b85encode(b"xxxx", pad=True), b'czAet') |
---|
566 | n/a | eq(base64.b85encode(b"xxxxx", pad=True), b'czAetcmMzZ') |
---|
567 | n/a | |
---|
568 | n/a | eq(base64.b85decode(b'cmMzZ'), b"x\x00\x00\x00") |
---|
569 | n/a | eq(base64.b85decode(b'cz6H+'), b"xx\x00\x00") |
---|
570 | n/a | eq(base64.b85decode(b'czAdK'), b"xxx\x00") |
---|
571 | n/a | eq(base64.b85decode(b'czAet'), b"xxxx") |
---|
572 | n/a | eq(base64.b85decode(b'czAetcmMzZ'), b"xxxxx\x00\x00\x00") |
---|
573 | n/a | |
---|
574 | n/a | def test_a85decode_errors(self): |
---|
575 | n/a | illegal = (set(range(32)) | set(range(118, 256))) - set(b' \t\n\r\v') |
---|
576 | n/a | for c in illegal: |
---|
577 | n/a | with self.assertRaises(ValueError, msg=bytes([c])): |
---|
578 | n/a | base64.a85decode(b'!!!!' + bytes([c])) |
---|
579 | n/a | with self.assertRaises(ValueError, msg=bytes([c])): |
---|
580 | n/a | base64.a85decode(b'!!!!' + bytes([c]), adobe=False) |
---|
581 | n/a | with self.assertRaises(ValueError, msg=bytes([c])): |
---|
582 | n/a | base64.a85decode(b'<~!!!!' + bytes([c]) + b'~>', adobe=True) |
---|
583 | n/a | |
---|
584 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
585 | n/a | b"malformed", adobe=True) |
---|
586 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
587 | n/a | b"<~still malformed", adobe=True) |
---|
588 | n/a | |
---|
589 | n/a | # With adobe=False (the default), Adobe framing markers are disallowed |
---|
590 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
591 | n/a | b"<~~>") |
---|
592 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
593 | n/a | b"<~~>", adobe=False) |
---|
594 | n/a | base64.a85decode(b"<~~>", adobe=True) # sanity check |
---|
595 | n/a | |
---|
596 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
597 | n/a | b"abcx", adobe=False) |
---|
598 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
599 | n/a | b"abcdey", adobe=False) |
---|
600 | n/a | self.assertRaises(ValueError, base64.a85decode, |
---|
601 | n/a | b"a b\nc", adobe=False, ignorechars=b"") |
---|
602 | n/a | |
---|
603 | n/a | self.assertRaises(ValueError, base64.a85decode, b's', adobe=False) |
---|
604 | n/a | self.assertRaises(ValueError, base64.a85decode, b's8', adobe=False) |
---|
605 | n/a | self.assertRaises(ValueError, base64.a85decode, b's8W', adobe=False) |
---|
606 | n/a | self.assertRaises(ValueError, base64.a85decode, b's8W-', adobe=False) |
---|
607 | n/a | self.assertRaises(ValueError, base64.a85decode, b's8W-"', adobe=False) |
---|
608 | n/a | |
---|
609 | n/a | def test_b85decode_errors(self): |
---|
610 | n/a | illegal = list(range(33)) + \ |
---|
611 | n/a | list(b'"\',./:[\\]') + \ |
---|
612 | n/a | list(range(128, 256)) |
---|
613 | n/a | for c in illegal: |
---|
614 | n/a | with self.assertRaises(ValueError, msg=bytes([c])): |
---|
615 | n/a | base64.b85decode(b'0000' + bytes([c])) |
---|
616 | n/a | |
---|
617 | n/a | self.assertRaises(ValueError, base64.b85decode, b'|') |
---|
618 | n/a | self.assertRaises(ValueError, base64.b85decode, b'|N') |
---|
619 | n/a | self.assertRaises(ValueError, base64.b85decode, b'|Ns') |
---|
620 | n/a | self.assertRaises(ValueError, base64.b85decode, b'|NsC') |
---|
621 | n/a | self.assertRaises(ValueError, base64.b85decode, b'|NsC1') |
---|
622 | n/a | |
---|
623 | n/a | def test_decode_nonascii_str(self): |
---|
624 | n/a | decode_funcs = (base64.b64decode, |
---|
625 | n/a | base64.standard_b64decode, |
---|
626 | n/a | base64.urlsafe_b64decode, |
---|
627 | n/a | base64.b32decode, |
---|
628 | n/a | base64.b16decode, |
---|
629 | n/a | base64.b85decode, |
---|
630 | n/a | base64.a85decode) |
---|
631 | n/a | for f in decode_funcs: |
---|
632 | n/a | self.assertRaises(ValueError, f, 'with non-ascii \xcb') |
---|
633 | n/a | |
---|
634 | n/a | def test_ErrorHeritage(self): |
---|
635 | n/a | self.assertTrue(issubclass(binascii.Error, ValueError)) |
---|
636 | n/a | |
---|
637 | n/a | |
---|
638 | n/a | class TestMain(unittest.TestCase): |
---|
639 | n/a | def tearDown(self): |
---|
640 | n/a | if os.path.exists(support.TESTFN): |
---|
641 | n/a | os.unlink(support.TESTFN) |
---|
642 | n/a | |
---|
643 | n/a | def get_output(self, *args): |
---|
644 | n/a | return script_helper.assert_python_ok('-m', 'base64', *args).out |
---|
645 | n/a | |
---|
646 | n/a | def test_encode_decode(self): |
---|
647 | n/a | output = self.get_output('-t') |
---|
648 | n/a | self.assertSequenceEqual(output.splitlines(), ( |
---|
649 | n/a | b"b'Aladdin:open sesame'", |
---|
650 | n/a | br"b'QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n'", |
---|
651 | n/a | b"b'Aladdin:open sesame'", |
---|
652 | n/a | )) |
---|
653 | n/a | |
---|
654 | n/a | def test_encode_file(self): |
---|
655 | n/a | with open(support.TESTFN, 'wb') as fp: |
---|
656 | n/a | fp.write(b'a\xffb\n') |
---|
657 | n/a | output = self.get_output('-e', support.TESTFN) |
---|
658 | n/a | self.assertEqual(output.rstrip(), b'Yf9iCg==') |
---|
659 | n/a | |
---|
660 | n/a | def test_encode_from_stdin(self): |
---|
661 | n/a | with script_helper.spawn_python('-m', 'base64', '-e') as proc: |
---|
662 | n/a | out, err = proc.communicate(b'a\xffb\n') |
---|
663 | n/a | self.assertEqual(out.rstrip(), b'Yf9iCg==') |
---|
664 | n/a | self.assertIsNone(err) |
---|
665 | n/a | |
---|
666 | n/a | def test_decode(self): |
---|
667 | n/a | with open(support.TESTFN, 'wb') as fp: |
---|
668 | n/a | fp.write(b'Yf9iCg==') |
---|
669 | n/a | output = self.get_output('-d', support.TESTFN) |
---|
670 | n/a | self.assertEqual(output.rstrip(), b'a\xffb') |
---|
671 | n/a | |
---|
672 | n/a | if __name__ == '__main__': |
---|
673 | n/a | unittest.main() |
---|