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

Delta Between Two Patch Sets: Lib/test/test_base64.py

Issue 17839: base64 module should use memoryview
Left Patch Set: Created 6 years, 4 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/base64.py ('k') | Lib/test/test_codecs.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 from array import array
9 9
10 10
11 11
12 class LegacyBase64TestCase(unittest.TestCase): 12 class LegacyBase64TestCase(unittest.TestCase):
13 def test_encodebytes(self): 13 def test_encodebytes(self):
14 eq = self.assertEqual 14 eq = self.assertEqual
15 eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") 15 eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
16 eq(base64.encodebytes(b"a"), b"YQ==\n") 16 eq(base64.encodebytes(b"a"), b"YQ==\n")
17 eq(base64.encodebytes(b"ab"), b"YWI=\n") 17 eq(base64.encodebytes(b"ab"), b"YWI=\n")
18 eq(base64.encodebytes(b"abc"), b"YWJj\n") 18 eq(base64.encodebytes(b"abc"), b"YWJj\n")
19 eq(base64.encodebytes(b""), b"") 19 eq(base64.encodebytes(b""), b"")
20 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" 20 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz"
21 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 21 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22 b"0123456789!@#0^&*();:<>,. []{}"), 22 b"0123456789!@#0^&*();:<>,. []{}"),
23 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 23 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
24 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 24 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
25 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") 25 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
26 # Non-bytes 26 # Non-bytes
27 eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n') 27 eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n')
28 eq(base64.encodebytes(memoryview(b'abc')), b'YWJj\n')
29 eq(base64.encodebytes(array('B', b'abc')), b'YWJj\n')
28 self.assertRaises(TypeError, base64.encodebytes, "") 30 self.assertRaises(TypeError, base64.encodebytes, "")
31 self.assertRaises(TypeError, base64.encodebytes, [])
29 32
30 def test_decodebytes(self): 33 def test_decodebytes(self):
31 eq = self.assertEqual 34 eq = self.assertEqual
32 eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") 35 eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
33 eq(base64.decodebytes(b"YQ==\n"), b"a") 36 eq(base64.decodebytes(b"YQ==\n"), b"a")
34 eq(base64.decodebytes(b"YWI=\n"), b"ab") 37 eq(base64.decodebytes(b"YWI=\n"), b"ab")
35 eq(base64.decodebytes(b"YWJj\n"), b"abc") 38 eq(base64.decodebytes(b"YWJj\n"), b"abc")
36 eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 39 eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
37 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 40 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
38 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), 41 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
39 b"abcdefghijklmnopqrstuvwxyz" 42 b"abcdefghijklmnopqrstuvwxyz"
40 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 43 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 b"0123456789!@#0^&*();:<>,. []{}") 44 b"0123456789!@#0^&*();:<>,. []{}")
42 eq(base64.decodebytes(b''), b'') 45 eq(base64.decodebytes(b''), b'')
43 # Non-bytes 46 # Non-bytes
44 eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc') 47 eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc')
48 eq(base64.decodebytes(memoryview(b'YWJj\n')), b'abc')
49 eq(base64.decodebytes(array('B', b'YWJj\n')), b'abc')
45 self.assertRaises(TypeError, base64.decodebytes, "") 50 self.assertRaises(TypeError, base64.decodebytes, "")
51 self.assertRaises(TypeError, base64.decodebytes, [])
46 52
47 def test_encode(self): 53 def test_encode(self):
48 eq = self.assertEqual 54 eq = self.assertEqual
49 from io import BytesIO, StringIO 55 from io import BytesIO, StringIO
50 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' 56 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz'
51 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 57 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
52 b'0123456789!@#0^&*();:<>,. []{}') 58 b'0123456789!@#0^&*();:<>,. []{}')
53 outfp = BytesIO() 59 outfp = BytesIO()
54 base64.encode(infp, outfp) 60 base64.encode(infp, outfp)
55 eq(outfp.getvalue(), 61 eq(outfp.getvalue(),
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 # Non-bytes 126 # Non-bytes
121 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==') 127 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
122 eq(base64.standard_b64encode(memoryview(b'abcd')), b'YWJjZA==') 128 eq(base64.standard_b64encode(memoryview(b'abcd')), b'YWJjZA==')
123 eq(base64.standard_b64encode(array('B', b'abcd')), b'YWJjZA==') 129 eq(base64.standard_b64encode(array('B', b'abcd')), b'YWJjZA==')
124 self.assertRaises(TypeError, base64.standard_b64encode, []) 130 self.assertRaises(TypeError, base64.standard_b64encode, [])
125 # Check if passing a str object raises an error 131 # Check if passing a str object raises an error
126 self.assertRaises(TypeError, base64.standard_b64encode, "") 132 self.assertRaises(TypeError, base64.standard_b64encode, "")
127 # Test with 'URL safe' alternative characters 133 # Test with 'URL safe' alternative characters
128 eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd') 134 eq(base64.urlsafe_b64encode(b'\xd3V\xbeo\xf7\x1d'), b'01a-b_cd')
129 # Non-bytes 135 # Non-bytes
130 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_c d') 136 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')),
131 eq(base64.urlsafe_b64encode(memoryview(b'\xd3V\xbeo\xf7\x1d')), b'01a-b_ cd') 137 b'01a-b_cd')
132 eq(base64.urlsafe_b64encode(array('B', b'\xd3V\xbeo\xf7\x1d')), b'01a-b_ cd') 138 eq(base64.urlsafe_b64encode(memoryview(b'\xd3V\xbeo\xf7\x1d')),
ezio.melotti 2013/05/07 09:38:03 These 3 lines are too long.
139 b'01a-b_cd')
140 eq(base64.urlsafe_b64encode(array('B', b'\xd3V\xbeo\xf7\x1d')),
141 b'01a-b_cd')
133 self.assertRaises(TypeError, base64.urlsafe_b64encode, []) 142 self.assertRaises(TypeError, base64.urlsafe_b64encode, [])
134 # Check if passing a str object raises an error 143 # Check if passing a str object raises an error
135 self.assertRaises(TypeError, base64.urlsafe_b64encode, "") 144 self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
136 145
137 def test_b64decode(self): 146 def test_b64decode(self):
138 eq = self.assertEqual 147 eq = self.assertEqual
139 148
140 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", 149 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org",
141 b'AA==': b'\x00', 150 b'AA==': b'\x00',
142 b"YQ==": b"a", 151 b"YQ==": b"a",
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 self.assertRaises(TypeError, base64.standard_b64decode, []) 192 self.assertRaises(TypeError, base64.standard_b64decode, [])
184 193
185 # Test with 'URL safe' alternative characters 194 # Test with 'URL safe' alternative characters
186 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', 195 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
187 b'': b'', 196 b'': b'',
188 } 197 }
189 for data, res in tests_urlsafe.items(): 198 for data, res in tests_urlsafe.items():
190 eq(base64.urlsafe_b64decode(data), res) 199 eq(base64.urlsafe_b64decode(data), res)
191 eq(base64.urlsafe_b64decode(data.decode('ascii')), res) 200 eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
192 # Non-bytes 201 # Non-bytes
193 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x1 d') 202 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')),
194 eq(base64.urlsafe_b64decode(memoryview(b'01a-b_cd')), b'\xd3V\xbeo\xf7\x 1d') 203 b'\xd3V\xbeo\xf7\x1d')
195 eq(base64.urlsafe_b64decode(array('B', b'01a-b_cd')), b'\xd3V\xbeo\xf7\x 1d') 204 eq(base64.urlsafe_b64decode(memoryview(b'01a-b_cd')),
ezio.melotti 2013/05/07 09:38:03 These 3 lines are too long.
205 b'\xd3V\xbeo\xf7\x1d')
206 eq(base64.urlsafe_b64decode(array('B', b'01a-b_cd')),
207 b'\xd3V\xbeo\xf7\x1d')
196 self.assertRaises(TypeError, base64.urlsafe_b64decode, []) 208 self.assertRaises(TypeError, base64.urlsafe_b64decode, [])
197 209
198 def test_b64decode_padding_error(self): 210 def test_b64decode_padding_error(self):
199 self.assertRaises(binascii.Error, base64.b64decode, b'abc') 211 self.assertRaises(binascii.Error, base64.b64decode, b'abc')
200 self.assertRaises(binascii.Error, base64.b64decode, 'abc') 212 self.assertRaises(binascii.Error, base64.b64decode, 'abc')
201 213
202 def test_b64decode_invalid_chars(self): 214 def test_b64decode_invalid_chars(self):
203 # issue 1466065: Test some invalid characters. 215 # issue 1466065: Test some invalid characters.
204 tests = ((b'%3d==', b'\xdd'), 216 tests = ((b'%3d==', b'\xdd'),
205 (b'$3d==', b'\xdd'), 217 (b'$3d==', b'\xdd'),
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 eq(base64.b16decode(b'00'), b'\x00') 329 eq(base64.b16decode(b'00'), b'\x00')
318 eq(base64.b16decode('00'), b'\x00') 330 eq(base64.b16decode('00'), b'\x00')
319 # Lower case is not allowed without a flag 331 # Lower case is not allowed without a flag
320 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') 332 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef')
321 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') 333 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef')
322 # Case fold 334 # Case fold
323 eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') 335 eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef')
324 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') 336 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
325 # Non-bytes 337 # Non-bytes
326 eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef') 338 eq(base64.b16decode(bytearray(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
339 eq(base64.b16decode(memoryview(b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
340 eq(base64.b16decode(array('B', b"0102ABCDEF")), b'\x01\x02\xab\xcd\xef')
341 eq(base64.b16decode(bytearray(b"0102abcdef"), True),
342 b'\x01\x02\xab\xcd\xef')
343 eq(base64.b16decode(memoryview(b"0102abcdef"), True),
344 b'\x01\x02\xab\xcd\xef')
345 eq(base64.b16decode(array('B', b"0102abcdef"), True),
346 b'\x01\x02\xab\xcd\xef')
327 self.assertRaises(TypeError, base64.b16decode, []) 347 self.assertRaises(TypeError, base64.b16decode, [])
328 348
329 def test_decode_nonascii_str(self): 349 def test_decode_nonascii_str(self):
330 decode_funcs = (base64.b64decode, 350 decode_funcs = (base64.b64decode,
331 base64.standard_b64decode, 351 base64.standard_b64decode,
332 base64.urlsafe_b64decode, 352 base64.urlsafe_b64decode,
333 base64.b32decode, 353 base64.b32decode,
334 base64.b16decode) 354 base64.b16decode)
335 for f in decode_funcs: 355 for f in decode_funcs:
336 self.assertRaises(ValueError, f, 'with non-ascii \xcb') 356 self.assertRaises(ValueError, f, 'with non-ascii \xcb')
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 output = self.get_output('-d', support.TESTFN) 394 output = self.get_output('-d', support.TESTFN)
375 self.assertEqual(output.rstrip(), b'a\xffb') 395 self.assertEqual(output.rstrip(), b'a\xffb')
376 396
377 397
378 398
379 def test_main(): 399 def test_main():
380 support.run_unittest(__name__) 400 support.run_unittest(__name__)
381 401
382 if __name__ == '__main__': 402 if __name__ == '__main__':
383 test_main() 403 test_main()
LEFTRIGHT

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