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

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

Issue 17839: base64 module should use memoryview
Left Patch Set: Created 6 years, 5 months ago
Right Patch Set: Created 6 years, 5 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 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")
18 # Test for memoryview, issue17839
19 eq(base64.encodebytes(memoryview(b"abc")), b"YWJj\n")
20 eq(base64.encodebytes(b""), b"") 19 eq(base64.encodebytes(b""), b"")
21 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" 20 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz"
22 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 21 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23 b"0123456789!@#0^&*();:<>,. []{}"), 22 b"0123456789!@#0^&*();:<>,. []{}"),
24 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 23 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
25 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 24 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
26 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") 25 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
26 # Non-bytes
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')
27 self.assertRaises(TypeError, base64.encodebytes, "") 30 self.assertRaises(TypeError, base64.encodebytes, "")
31 self.assertRaises(TypeError, base64.encodebytes, [])
28 32
29 def test_decodebytes(self): 33 def test_decodebytes(self):
30 eq = self.assertEqual 34 eq = self.assertEqual
31 eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") 35 eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
32 eq(base64.decodebytes(b"YQ==\n"), b"a") 36 eq(base64.decodebytes(b"YQ==\n"), b"a")
33 eq(base64.decodebytes(b"YWI=\n"), b"ab") 37 eq(base64.decodebytes(b"YWI=\n"), b"ab")
34 eq(base64.decodebytes(b"YWJj\n"), b"abc") 38 eq(base64.decodebytes(b"YWJj\n"), b"abc")
35 eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 39 eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
36 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 40 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
37 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), 41 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"),
38 b"abcdefghijklmnopqrstuvwxyz" 42 b"abcdefghijklmnopqrstuvwxyz"
39 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 43 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 b"0123456789!@#0^&*();:<>,. []{}") 44 b"0123456789!@#0^&*();:<>,. []{}")
41 eq(base64.decodebytes(b''), b'') 45 eq(base64.decodebytes(b''), b'')
46 # Non-bytes
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')
42 self.assertRaises(TypeError, base64.decodebytes, "") 50 self.assertRaises(TypeError, base64.decodebytes, "")
51 self.assertRaises(TypeError, base64.decodebytes, [])
43 52
44 def test_encode(self): 53 def test_encode(self):
45 eq = self.assertEqual 54 eq = self.assertEqual
46 from io import BytesIO 55 from io import BytesIO, StringIO
47 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' 56 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz'
48 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 57 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
49 b'0123456789!@#0^&*();:<>,. []{}') 58 b'0123456789!@#0^&*();:<>,. []{}')
50 outfp = BytesIO() 59 outfp = BytesIO()
51 base64.encode(infp, outfp) 60 base64.encode(infp, outfp)
52 eq(outfp.getvalue(), 61 eq(outfp.getvalue(),
53 b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE' 62 b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE'
54 b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT' 63 b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT'
55 b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n') 64 b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n')
65 # Non-binary files
66 self.assertRaises(TypeError, base64.encode, StringIO('abc'), BytesIO())
67 self.assertRaises(TypeError, base64.encode, BytesIO(b'abc'), StringIO())
68 self.assertRaises(TypeError, base64.encode, StringIO('abc'), StringIO())
56 69
57 def test_decode(self): 70 def test_decode(self):
58 from io import BytesIO 71 from io import BytesIO, StringIO
59 infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=') 72 infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=')
60 outfp = BytesIO() 73 outfp = BytesIO()
61 base64.decode(infp, outfp) 74 base64.decode(infp, outfp)
62 self.assertEqual(outfp.getvalue(), b'www.python.org') 75 self.assertEqual(outfp.getvalue(), b'www.python.org')
76 # Non-binary files
77 self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), BytesIO( ))
78 self.assertRaises(TypeError, base64.encode, BytesIO(b'YWJj\n'), StringIO ())
79 self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), StringIO ())
63 80
64 81
65 class BaseXYTestCase(unittest.TestCase): 82 class BaseXYTestCase(unittest.TestCase):
66 def test_b64encode(self): 83 def test_b64encode(self):
67 eq = self.assertEqual 84 eq = self.assertEqual
68 # Test default alphabet 85 # Test default alphabet
69 eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") 86 eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=")
70 eq(base64.b64encode(b'\x00'), b'AA==') 87 eq(base64.b64encode(b'\x00'), b'AA==')
71 eq(base64.b64encode(b"a"), b"YQ==") 88 eq(base64.b64encode(b"a"), b"YQ==")
72 eq(base64.b64encode(b"ab"), b"YWI=") 89 eq(base64.b64encode(b"ab"), b"YWI=")
73 eq(base64.b64encode(b"abc"), b"YWJj") 90 eq(base64.b64encode(b"abc"), b"YWJj")
74 # Test for memoryview, issue17839
75 eq(base64.b64encode(memoryview(b"abc")), b"YWJj")
76 eq(base64.b64encode(b""), b"") 91 eq(base64.b64encode(b""), b"")
77 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" 92 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz"
78 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 93 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
79 b"0123456789!@#0^&*();:<>,. []{}"), 94 b"0123456789!@#0^&*();:<>,. []{}"),
80 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 95 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
81 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 96 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
82 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 97 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
83 # Test with arbitrary alternative characters 98 # Test with arbitrary alternative characters
84 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd') 99 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=b'*$'), b'01a*b$cd')
100 # Non-bytes
101 eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==')
102 eq(base64.b64encode(memoryview(b'abcd')), b'YWJjZA==')
103 eq(base64.b64encode(array('B', b'abcd')), b'YWJjZA==')
104 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
105 b'01a*b$cd')
106 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=memoryview(b'*$')),
107 b'01a*b$cd')
108 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=array('B', b'*$')),
109 b'01a*b$cd')
110 self.assertRaises(TypeError, base64.b64encode, [])
85 # Check if passing a str object raises an error 111 # Check if passing a str object raises an error
86 self.assertRaises(TypeError, base64.b64encode, "") 112 self.assertRaises(TypeError, base64.b64encode, "")
87 self.assertRaises(TypeError, base64.b64encode, b"", altchars="") 113 self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$")
88 # Test standard alphabet 114 # Test standard alphabet
89 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" ) 115 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" )
90 eq(base64.standard_b64encode(b"a"), b"YQ==") 116 eq(base64.standard_b64encode(b"a"), b"YQ==")
91 eq(base64.standard_b64encode(b"ab"), b"YWI=") 117 eq(base64.standard_b64encode(b"ab"), b"YWI=")
92 eq(base64.standard_b64encode(b"abc"), b"YWJj") 118 eq(base64.standard_b64encode(b"abc"), b"YWJj")
93 # Test for memoryview, issue17839
94 eq(base64.standard_b64encode(memoryview(b"abc")), b"YWJj")
95 eq(base64.standard_b64encode(b""), b"") 119 eq(base64.standard_b64encode(b""), b"")
96 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" 120 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz"
97 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 121 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
98 b"0123456789!@#0^&*();:<>,. []{}"), 122 b"0123456789!@#0^&*();:<>,. []{}"),
99 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 123 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
100 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 124 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
101 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 125 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
126 # Non-bytes
127 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==')
128 eq(base64.standard_b64encode(memoryview(b'abcd')), b'YWJjZA==')
129 eq(base64.standard_b64encode(array('B', b'abcd')), b'YWJjZA==')
130 self.assertRaises(TypeError, base64.standard_b64encode, [])
102 # Check if passing a str object raises an error 131 # Check if passing a str object raises an error
103 self.assertRaises(TypeError, base64.standard_b64encode, "") 132 self.assertRaises(TypeError, base64.standard_b64encode, "")
104 self.assertRaises(TypeError, base64.standard_b64encode, b"", altchars="" )
105 # Test with 'URL safe' alternative characters 133 # Test with 'URL safe' alternative characters
106 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')
135 # Non-bytes
136 eq(base64.urlsafe_b64encode(bytearray(b'\xd3V\xbeo\xf7\x1d')),
137 b'01a-b_cd')
138 eq(base64.urlsafe_b64encode(memoryview(b'\xd3V\xbeo\xf7\x1d')),
139 b'01a-b_cd')
140 eq(base64.urlsafe_b64encode(array('B', b'\xd3V\xbeo\xf7\x1d')),
141 b'01a-b_cd')
142 self.assertRaises(TypeError, base64.urlsafe_b64encode, [])
107 # Check if passing a str object raises an error 143 # Check if passing a str object raises an error
108 self.assertRaises(TypeError, base64.urlsafe_b64encode, "") 144 self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
109 145
110 def test_b64decode(self): 146 def test_b64decode(self):
111 eq = self.assertEqual 147 eq = self.assertEqual
112 148
113 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", 149 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org",
114 b'AA==': b'\x00', 150 b'AA==': b'\x00',
115 b"YQ==": b"a", 151 b"YQ==": b"a",
116 b"YWI=": b"ab", 152 b"YWI=": b"ab",
117 b"YWJj": b"abc", 153 b"YWJj": b"abc",
118 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 154 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
119 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 155 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
120 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==": 156 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==":
121 157
122 b"abcdefghijklmnopqrstuvwxyz" 158 b"abcdefghijklmnopqrstuvwxyz"
123 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 159 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
124 b"0123456789!@#0^&*();:<>,. []{}", 160 b"0123456789!@#0^&*();:<>,. []{}",
125 b'': b'', 161 b'': b'',
126 } 162 }
127 for data, res in tests.items(): 163 for data, res in tests.items():
128 eq(base64.b64decode(data), res) 164 eq(base64.b64decode(data), res)
129 eq(base64.b64decode(data.decode('ascii')), res) 165 eq(base64.b64decode(data.decode('ascii')), res)
166 # Non-bytes
167 eq(base64.b64decode(bytearray(b"YWJj")), b"abc")
168 eq(base64.b64decode(memoryview(b"YWJj")), b"abc")
169 eq(base64.b64decode(array('B', b"YWJj")), b"abc")
170 self.assertRaises(TypeError, base64.b64decode, [])
130 171
131 # Test with arbitrary alternative characters 172 # Test with arbitrary alternative characters
132 tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d', 173 tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d',
133 } 174 }
134 for (data, altchars), res in tests_altchars.items(): 175 for (data, altchars), res in tests_altchars.items():
135 data_str = data.decode('ascii') 176 data_str = data.decode('ascii')
136 altchars_str = altchars.decode('ascii') 177 altchars_str = altchars.decode('ascii')
137 178
138 eq(base64.b64decode(data, altchars=altchars), res) 179 eq(base64.b64decode(data, altchars=altchars), res)
139 eq(base64.b64decode(data_str, altchars=altchars), res) 180 eq(base64.b64decode(data_str, altchars=altchars), res)
140 eq(base64.b64decode(data, altchars=altchars_str), res) 181 eq(base64.b64decode(data, altchars=altchars_str), res)
141 eq(base64.b64decode(data_str, altchars=altchars_str), res) 182 eq(base64.b64decode(data_str, altchars=altchars_str), res)
142 183
143 # Test standard alphabet 184 # Test standard alphabet
144 for data, res in tests.items(): 185 for data, res in tests.items():
145 eq(base64.standard_b64decode(data), res) 186 eq(base64.standard_b64decode(data), res)
146 eq(base64.standard_b64decode(data.decode('ascii')), res) 187 eq(base64.standard_b64decode(data.decode('ascii')), res)
188 # Non-bytes
189 eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc")
190 eq(base64.standard_b64decode(memoryview(b"YWJj")), b"abc")
191 eq(base64.standard_b64decode(array('B', b"YWJj")), b"abc")
192 self.assertRaises(TypeError, base64.standard_b64decode, [])
147 193
148 # Test with 'URL safe' alternative characters 194 # Test with 'URL safe' alternative characters
149 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', 195 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
150 b'': b'', 196 b'': b'',
151 } 197 }
152 for data, res in tests_urlsafe.items(): 198 for data, res in tests_urlsafe.items():
153 eq(base64.urlsafe_b64decode(data), res) 199 eq(base64.urlsafe_b64decode(data), res)
154 eq(base64.urlsafe_b64decode(data.decode('ascii')), res) 200 eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
201 # Non-bytes
202 eq(base64.urlsafe_b64decode(bytearray(b'01a-b_cd')),
203 b'\xd3V\xbeo\xf7\x1d')
204 eq(base64.urlsafe_b64decode(memoryview(b'01a-b_cd')),
205 b'\xd3V\xbeo\xf7\x1d')
206 eq(base64.urlsafe_b64decode(array('B', b'01a-b_cd')),
207 b'\xd3V\xbeo\xf7\x1d')
208 self.assertRaises(TypeError, base64.urlsafe_b64decode, [])
155 209
156 def test_b64decode_padding_error(self): 210 def test_b64decode_padding_error(self):
157 self.assertRaises(binascii.Error, base64.b64decode, b'abc') 211 self.assertRaises(binascii.Error, base64.b64decode, b'abc')
158 self.assertRaises(binascii.Error, base64.b64decode, 'abc') 212 self.assertRaises(binascii.Error, base64.b64decode, 'abc')
159 213
160 def test_b64decode_invalid_chars(self): 214 def test_b64decode_invalid_chars(self):
161 # issue 1466065: Test some invalid characters. 215 # issue 1466065: Test some invalid characters.
162 tests = ((b'%3d==', b'\xdd'), 216 tests = ((b'%3d==', b'\xdd'),
163 (b'$3d==', b'\xdd'), 217 (b'$3d==', b'\xdd'),
164 (b'[==', b''), 218 (b'[==', b''),
(...skipping 13 matching lines...) Expand all
178 232
179 def test_b32encode(self): 233 def test_b32encode(self):
180 eq = self.assertEqual 234 eq = self.assertEqual
181 eq(base64.b32encode(b''), b'') 235 eq(base64.b32encode(b''), b'')
182 eq(base64.b32encode(b'\x00'), b'AA======') 236 eq(base64.b32encode(b'\x00'), b'AA======')
183 eq(base64.b32encode(b'a'), b'ME======') 237 eq(base64.b32encode(b'a'), b'ME======')
184 eq(base64.b32encode(b'ab'), b'MFRA====') 238 eq(base64.b32encode(b'ab'), b'MFRA====')
185 eq(base64.b32encode(b'abc'), b'MFRGG===') 239 eq(base64.b32encode(b'abc'), b'MFRGG===')
186 eq(base64.b32encode(b'abcd'), b'MFRGGZA=') 240 eq(base64.b32encode(b'abcd'), b'MFRGGZA=')
187 eq(base64.b32encode(b'abcde'), b'MFRGGZDF') 241 eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
242 # Non-bytes
243 eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=')
244 eq(base64.b32encode(memoryview(b'abcd')), b'MFRGGZA=')
245 eq(base64.b32encode(array('B', b'abcd')), b'MFRGGZA=')
188 self.assertRaises(TypeError, base64.b32encode, "") 246 self.assertRaises(TypeError, base64.b32encode, "")
189 # Test for memoryview, issue17839 247 self.assertRaises(TypeError, base64.b32encode, [])
190 eq(base64.b32encode(memoryview(b'abcde')), b'MFRGGZDF')
191 248
192 def test_b32decode(self): 249 def test_b32decode(self):
193 eq = self.assertEqual 250 eq = self.assertEqual
194 tests = {b'': b'', 251 tests = {b'': b'',
195 b'AA======': b'\x00', 252 b'AA======': b'\x00',
196 b'ME======': b'a', 253 b'ME======': b'a',
197 b'MFRA====': b'ab', 254 b'MFRA====': b'ab',
198 b'MFRGG===': b'abc', 255 b'MFRGG===': b'abc',
199 b'MFRGGZA=': b'abcd', 256 b'MFRGGZA=': b'abcd',
200 b'MFRGGZDF': b'abcde', 257 b'MFRGGZDF': b'abcde',
201 } 258 }
202 for data, res in tests.items(): 259 for data, res in tests.items():
203 eq(base64.b32decode(data), res) 260 eq(base64.b32decode(data), res)
204 eq(base64.b32decode(data.decode('ascii')), res) 261 eq(base64.b32decode(data.decode('ascii')), res)
262 # Non-bytes
263 eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc')
264 eq(base64.b32decode(memoryview(b'MFRGG===')), b'abc')
265 eq(base64.b32decode(array('B', b'MFRGG===')), b'abc')
266 self.assertRaises(TypeError, base64.b32decode, [])
205 267
206 def test_b32decode_casefold(self): 268 def test_b32decode_casefold(self):
207 eq = self.assertEqual 269 eq = self.assertEqual
208 tests = {b'': b'', 270 tests = {b'': b'',
209 b'ME======': b'a', 271 b'ME======': b'a',
210 b'MFRA====': b'ab', 272 b'MFRA====': b'ab',
211 b'MFRGG===': b'abc', 273 b'MFRGG===': b'abc',
212 b'MFRGGZA=': b'abcd', 274 b'MFRGGZA=': b'abcd',
213 b'MFRGGZDF': b'abcde', 275 b'MFRGGZDF': b'abcde',
214 # Lower cases 276 # Lower cases
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 for data in [b'abc', b'ABCDEF==']: 308 for data in [b'abc', b'ABCDEF==']:
247 with self.assertRaises(binascii.Error): 309 with self.assertRaises(binascii.Error):
248 base64.b32decode(data) 310 base64.b32decode(data)
249 with self.assertRaises(binascii.Error): 311 with self.assertRaises(binascii.Error):
250 base64.b32decode(data.decode('ascii')) 312 base64.b32decode(data.decode('ascii'))
251 313
252 def test_b16encode(self): 314 def test_b16encode(self):
253 eq = self.assertEqual 315 eq = self.assertEqual
254 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') 316 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
255 eq(base64.b16encode(b'\x00'), b'00') 317 eq(base64.b16encode(b'\x00'), b'00')
318 # Non-bytes
319 eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
320 eq(base64.b16encode(memoryview(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
321 eq(base64.b16encode(array('B', b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
256 self.assertRaises(TypeError, base64.b16encode, "") 322 self.assertRaises(TypeError, base64.b16encode, "")
257 # Test for memoryview, issue17839 323 self.assertRaises(TypeError, base64.b16encode, [])
258 eq(base64.b16encode(memoryview(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF')
259 324
260 def test_b16decode(self): 325 def test_b16decode(self):
261 eq = self.assertEqual 326 eq = self.assertEqual
262 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 327 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
263 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 328 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
264 eq(base64.b16decode(b'00'), b'\x00') 329 eq(base64.b16decode(b'00'), b'\x00')
265 eq(base64.b16decode('00'), b'\x00') 330 eq(base64.b16decode('00'), b'\x00')
266 # Lower case is not allowed without a flag 331 # Lower case is not allowed without a flag
267 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') 332 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef')
268 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') 333 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef')
269 # Case fold 334 # Case fold
270 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')
271 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') 336 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
337 # Non-bytes
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')
347 self.assertRaises(TypeError, base64.b16decode, [])
272 348
273 def test_decode_nonascii_str(self): 349 def test_decode_nonascii_str(self):
274 decode_funcs = (base64.b64decode, 350 decode_funcs = (base64.b64decode,
275 base64.standard_b64decode, 351 base64.standard_b64decode,
276 base64.urlsafe_b64decode, 352 base64.urlsafe_b64decode,
277 base64.b32decode, 353 base64.b32decode,
278 base64.b16decode) 354 base64.b16decode)
279 for f in decode_funcs: 355 for f in decode_funcs:
280 self.assertRaises(ValueError, f, 'with non-ascii \xcb') 356 self.assertRaises(ValueError, f, 'with non-ascii \xcb')
281 357
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 output = self.get_output('-d', support.TESTFN) 394 output = self.get_output('-d', support.TESTFN)
319 self.assertEqual(output.rstrip(), b'a\xffb') 395 self.assertEqual(output.rstrip(), b'a\xffb')
320 396
321 397
322 398
323 def test_main(): 399 def test_main():
324 support.run_unittest(__name__) 400 support.run_unittest(__name__)
325 401
326 if __name__ == '__main__': 402 if __name__ == '__main__':
327 test_main() 403 test_main()
LEFTRIGHT

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