Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(33534)

Side by Side Diff: Lib/test/test_base64.py

Issue 17839: base64 module should use memoryview
Patch Set: Created 6 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/base64.py ('K') | « Lib/base64.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 import base64 3 import base64
4 import binascii 4 import binascii
5 import os 5 import os
6 import sys 6 import sys
7 import subprocess 7 import subprocess
8 from array import array
8 9
9 10
10 11
11 class LegacyBase64TestCase(unittest.TestCase): 12 class LegacyBase64TestCase(unittest.TestCase):
12 def test_encodebytes(self): 13 def test_encodebytes(self):
13 eq = self.assertEqual 14 eq = self.assertEqual
14 eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") 15 eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
15 eq(base64.encodebytes(b"a"), b"YQ==\n") 16 eq(base64.encodebytes(b"a"), b"YQ==\n")
16 eq(base64.encodebytes(b"ab"), b"YWI=\n") 17 eq(base64.encodebytes(b"ab"), b"YWI=\n")
17 eq(base64.encodebytes(b"abc"), b"YWJj\n") 18 eq(base64.encodebytes(b"abc"), b"YWJj\n")
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" 86 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz"
86 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 87 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
87 b"0123456789!@#0^&*();:<>,. []{}"), 88 b"0123456789!@#0^&*();:<>,. []{}"),
88 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 89 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
89 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 90 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
90 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 91 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
91 # Test with arbitrary alternative characters 92 # Test with arbitrary alternative characters
92 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd') 93 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd')
93 # Non-bytes 94 # Non-bytes
94 eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==') 95 eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==')
96 eq(base64.b64encode(memoryview(b'abcd')), b'YWJjZA==')
97 eq(base64.b64encode(array('B', b'abcd')), b'YWJjZA==')
95 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')), 98 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
96 b'01a*b$cd') 99 b'01a*b$cd')
100 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=memoryview(b'*$')),
101 b'01a*b$cd')
102 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=array('B', b'*$')),
103 b'01a*b$cd')
104 self.assertRaises(TypeError, base64.b64encode, [])
97 # Check if passing a str object raises an error 105 # Check if passing a str object raises an error
98 self.assertRaises(TypeError, base64.b64encode, "") 106 self.assertRaises(TypeError, base64.b64encode, "")
99 self.assertRaises(TypeError, base64.b64encode, b"", altchars="") 107 self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$")
100 # Test standard alphabet 108 # Test standard alphabet
101 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" ) 109 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" )
102 eq(base64.standard_b64encode(b"a"), b"YQ==") 110 eq(base64.standard_b64encode(b"a"), b"YQ==")
103 eq(base64.standard_b64encode(b"ab"), b"YWI=") 111 eq(base64.standard_b64encode(b"ab"), b"YWI=")
104 eq(base64.standard_b64encode(b"abc"), b"YWJj") 112 eq(base64.standard_b64encode(b"abc"), b"YWJj")
105 eq(base64.standard_b64encode(b""), b"") 113 eq(base64.standard_b64encode(b""), b"")
106 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" 114 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz"
107 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 115 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108 b"0123456789!@#0^&*();:<>,. []{}"), 116 b"0123456789!@#0^&*();:<>,. []{}"),
109 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 117 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
110 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 118 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
111 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 119 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
112 # Non-bytes 120 # Non-bytes
113 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==') 121 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
122 eq(base64.standard_b64encode(memoryview(b'abcd')), b'YWJjZA==')
123 eq(base64.standard_b64encode(array('B', b'abcd')), b'YWJjZA==')
124 self.assertRaises(TypeError, base64.standard_b64encode, [])
114 # Check if passing a str object raises an error 125 # Check if passing a str object raises an error
115 self.assertRaises(TypeError, base64.standard_b64encode, "") 126 self.assertRaises(TypeError, base64.standard_b64encode, "")
116 # Test with 'URL safe' alternative characters 127 # Test with 'URL safe' alternative characters
117 eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd') 128 eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd')
118 # Non-bytes 129 # Non-bytes
119 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_c d') 130 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_c d')
131 eq(base64.urlsafe_b64encode(memoryview(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_ cd')
132 eq(base64.urlsafe_b64encode(array('B', b'\xd3V\xbeo\xf7\x1d')), b'01a-b_ cd')
ezio.melotti 2013/05/07 09:38:03 These 3 lines are too long.
133 self.assertRaises(TypeError, base64.urlsafe_b64encode, [])
120 # Check if passing a str object raises an error 134 # Check if passing a str object raises an error
121 self.assertRaises(TypeError, base64.urlsafe_b64encode, "") 135 self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
122 136
123 def test_b64decode(self): 137 def test_b64decode(self):
124 eq = self.assertEqual 138 eq = self.assertEqual
125 139
126 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", 140 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org",
127 b'AA==': b'\x00', 141 b'AA==': b'\x00',
128 b"YQ==": b"a", 142 b"YQ==": b"a",
129 b"YWI=": b"ab", 143 b"YWI=": b"ab",
130 b"YWJj": b"abc", 144 b"YWJj": b"abc",
131 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 145 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
132 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 146 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
133 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==": 147 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==":
134 148
135 b"abcdefghijklmnopqrstuvwxyz" 149 b"abcdefghijklmnopqrstuvwxyz"
136 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 150 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
137 b"0123456789!@#0^&*();:<>,. []{}", 151 b"0123456789!@#0^&*();:<>,. []{}",
138 b'': b'', 152 b'': b'',
139 } 153 }
140 for data, res in tests.items(): 154 for data, res in tests.items():
141 eq(base64.b64decode(data), res) 155 eq(base64.b64decode(data), res)
142 eq(base64.b64decode(data.decode('ascii')), res) 156 eq(base64.b64decode(data.decode('ascii')), res)
143 # Non-bytes 157 # Non-bytes
144 eq(base64.b64decode(bytearray(b"YWJj")), b"abc") 158 eq(base64.b64decode(bytearray(b"YWJj")), b"abc")
159 eq(base64.b64decode(memoryview(b"YWJj")), b"abc")
160 eq(base64.b64decode(array('B', b"YWJj")), b"abc")
161 self.assertRaises(TypeError, base64.b64decode, [])
145 162
146 # Test with arbitrary alternative characters 163 # Test with arbitrary alternative characters
147 tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d', 164 tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d',
148 } 165 }
149 for (data, altchars), res in tests_altchars.items(): 166 for (data, altchars), res in tests_altchars.items():
150 data_str = data.decode('ascii') 167 data_str = data.decode('ascii')
151 altchars_str = altchars.decode('ascii') 168 altchars_str = altchars.decode('ascii')
152 169
153 eq(base64.b64decode(data, altchars=altchars), res) 170 eq(base64.b64decode(data, altchars=altchars), res)
154 eq(base64.b64decode(data_str, altchars=altchars), res) 171 eq(base64.b64decode(data_str, altchars=altchars), res)
155 eq(base64.b64decode(data, altchars=altchars_str), res) 172 eq(base64.b64decode(data, altchars=altchars_str), res)
156 eq(base64.b64decode(data_str, altchars=altchars_str), res) 173 eq(base64.b64decode(data_str, altchars=altchars_str), res)
157 174
158 # Test standard alphabet 175 # Test standard alphabet
159 for data, res in tests.items(): 176 for data, res in tests.items():
160 eq(base64.standard_b64decode(data), res) 177 eq(base64.standard_b64decode(data), res)
161 eq(base64.standard_b64decode(data.decode('ascii')), res) 178 eq(base64.standard_b64decode(data.decode('ascii')), res)
162 # Non-bytes 179 # Non-bytes
163 eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc") 180 eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc")
181 eq(base64.standard_b64decode(memoryview(b"YWJj")), b"abc")
182 eq(base64.standard_b64decode(array('B', b"YWJj")), b"abc")
183 self.assertRaises(TypeError, base64.standard_b64decode, [])
164 184
165 # Test with 'URL safe' alternative characters 185 # Test with 'URL safe' alternative characters
166 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', 186 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
167 b'': b'', 187 b'': b'',
168 } 188 }
169 for data, res in tests_urlsafe.items(): 189 for data, res in tests_urlsafe.items():
170 eq(base64.urlsafe_b64decode(data), res) 190 eq(base64.urlsafe_b64decode(data), res)
171 eq(base64.urlsafe_b64decode(data.decode('ascii')), res) 191 eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
172 # Non-bytes 192 # Non-bytes
173 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1 d') 193 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1 d')
194 eq(base64.urlsafe_b64decode(memoryview(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x 1d')
195 eq(base64.urlsafe_b64decode(array('B', b'01a-b_cd')), b'\xd3V\xbeo\xf7\x 1d')
ezio.melotti 2013/05/07 09:38:03 These 3 lines are too long.
196 self.assertRaises(TypeError, base64.urlsafe_b64decode, [])
174 197
175 def test_b64decode_padding_error(self): 198 def test_b64decode_padding_error(self):
176 self.assertRaises(binascii.Error, base64.b64decode, b'abc') 199 self.assertRaises(binascii.Error, base64.b64decode, b'abc')
177 self.assertRaises(binascii.Error, base64.b64decode, 'abc') 200 self.assertRaises(binascii.Error, base64.b64decode, 'abc')
178 201
179 def test_b64decode_invalid_chars(self): 202 def test_b64decode_invalid_chars(self):
180 # issue 1466065: Test some invalid characters. 203 # issue 1466065: Test some invalid characters.
181 tests = ((b'%3d==', b'\xdd'), 204 tests = ((b'%3d==', b'\xdd'),
182 (b'$3d==', b'\xdd'), 205 (b'$3d==', b'\xdd'),
183 (b'[==', b''), 206 (b'[==', b''),
(...skipping 15 matching lines...) Expand all
199 eq = self.assertEqual 222 eq = self.assertEqual
200 eq(base64.b32encode(b''), b'') 223 eq(base64.b32encode(b''), b'')
201 eq(base64.b32encode(b'\x00'), b'AA======') 224 eq(base64.b32encode(b'\x00'), b'AA======')
202 eq(base64.b32encode(b'a'), b'ME======') 225 eq(base64.b32encode(b'a'), b'ME======')
203 eq(base64.b32encode(b'ab'), b'MFRA====') 226 eq(base64.b32encode(b'ab'), b'MFRA====')
204 eq(base64.b32encode(b'abc'), b'MFRGG===') 227 eq(base64.b32encode(b'abc'), b'MFRGG===')
205 eq(base64.b32encode(b'abcd'), b'MFRGGZA=') 228 eq(base64.b32encode(b'abcd'), b'MFRGGZA=')
206 eq(base64.b32encode(b'abcde'), b'MFRGGZDF') 229 eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
207 # Non-bytes 230 # Non-bytes
208 eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=') 231 eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=')
232 eq(base64.b32encode(memoryview(b'abcd')), b'MFRGGZA=')
233 eq(base64.b32encode(array('B', b'abcd')), b'MFRGGZA=')
209 self.assertRaises(TypeError, base64.b32encode, "") 234 self.assertRaises(TypeError, base64.b32encode, "")
235 self.assertRaises(TypeError, base64.b32encode, [])
210 236
211 def test_b32decode(self): 237 def test_b32decode(self):
212 eq = self.assertEqual 238 eq = self.assertEqual
213 tests = {b'': b'', 239 tests = {b'': b'',
214 b'AA======': b'\x00', 240 b'AA======': b'\x00',
215 b'ME======': b'a', 241 b'ME======': b'a',
216 b'MFRA====': b'ab', 242 b'MFRA====': b'ab',
217 b'MFRGG===': b'abc', 243 b'MFRGG===': b'abc',
218 b'MFRGGZA=': b'abcd', 244 b'MFRGGZA=': b'abcd',
219 b'MFRGGZDF': b'abcde', 245 b'MFRGGZDF': b'abcde',
220 } 246 }
221 for data, res in tests.items(): 247 for data, res in tests.items():
222 eq(base64.b32decode(data), res) 248 eq(base64.b32decode(data), res)
223 eq(base64.b32decode(data.decode('ascii')), res) 249 eq(base64.b32decode(data.decode('ascii')), res)
224 # Non-bytes 250 # Non-bytes
225 eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc') 251 eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc')
252 eq(base64.b32decode(memoryview(b'MFRGG===')), b'abc')
253 eq(base64.b32decode(array('B', b'MFRGG===')), b'abc')
254 self.assertRaises(TypeError, base64.b32decode, [])
226 255
227 def test_b32decode_casefold(self): 256 def test_b32decode_casefold(self):
228 eq = self.assertEqual 257 eq = self.assertEqual
229 tests = {b'': b'', 258 tests = {b'': b'',
230 b'ME======': b'a', 259 b'ME======': b'a',
231 b'MFRA====': b'ab', 260 b'MFRA====': b'ab',
232 b'MFRGG===': b'abc', 261 b'MFRGG===': b'abc',
233 b'MFRGGZA=': b'abcd', 262 b'MFRGGZA=': b'abcd',
234 b'MFRGGZDF': b'abcde', 263 b'MFRGGZDF': b'abcde',
235 # Lower cases 264 # Lower cases
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 base64.b32decode(data) 298 base64.b32decode(data)
270 with self.assertRaises(binascii.Error): 299 with self.assertRaises(binascii.Error):
271 base64.b32decode(data.decode('ascii')) 300 base64.b32decode(data.decode('ascii'))
272 301
273 def test_b16encode(self): 302 def test_b16encode(self):
274 eq = self.assertEqual 303 eq = self.assertEqual
275 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') 304 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
276 eq(base64.b16encode(b'\x00'), b'00') 305 eq(base64.b16encode(b'\x00'), b'00')
277 # Non-bytes 306 # Non-bytes
278 eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF') 307 eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
308 eq(base64.b16encode(memoryview(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
309 eq(base64.b16encode(array('B', b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
279 self.assertRaises(TypeError, base64.b16encode, "") 310 self.assertRaises(TypeError, base64.b16encode, "")
311 self.assertRaises(TypeError, base64.b16encode, [])
280 312
281 def test_b16decode(self): 313 def test_b16decode(self):
282 eq = self.assertEqual 314 eq = self.assertEqual
283 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 315 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
284 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 316 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
285 eq(base64.b16decode(b'00'), b'\x00') 317 eq(base64.b16decode(b'00'), b'\x00')
286 eq(base64.b16decode('00'), b'\x00') 318 eq(base64.b16decode('00'), b'\x00')
287 # Lower case is not allowed without a flag 319 # Lower case is not allowed without a flag
288 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') 320 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef')
289 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') 321 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef')
290 # Case fold 322 # Case fold
291 eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') 323 eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef')
292 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') 324 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
293 # Non-bytes 325 # Non-bytes
294 eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef') 326 eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
327 self.assertRaises(TypeError, base64.b16decode, [])
295 328
296 def test_decode_nonascii_str(self): 329 def test_decode_nonascii_str(self):
297 decode_funcs = (base64.b64decode, 330 decode_funcs = (base64.b64decode,
298 base64.standard_b64decode, 331 base64.standard_b64decode,
299 base64.urlsafe_b64decode, 332 base64.urlsafe_b64decode,
300 base64.b32decode, 333 base64.b32decode,
301 base64.b16decode) 334 base64.b16decode)
302 for f in decode_funcs: 335 for f in decode_funcs:
303 self.assertRaises(ValueError, f, 'with non-ascii \xcb') 336 self.assertRaises(ValueError, f, 'with non-ascii \xcb')
304 337
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 output = self.get_output('-d', support.TESTFN) 374 output = self.get_output('-d', support.TESTFN)
342 self.assertEqual(output.rstrip(), b'a\xffb') 375 self.assertEqual(output.rstrip(), b'a\xffb')
343 376
344 377
345 378
346 def test_main(): 379 def test_main():
347 support.run_unittest(__name__) 380 support.run_unittest(__name__)
348 381
349 if __name__ == '__main__': 382 if __name__ == '__main__':
350 test_main() 383 test_main()
OLDNEW
« Lib/base64.py ('K') | « Lib/base64.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+