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

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
« no previous file with comments | « Lib/base64.py ('k') | Lib/test/test_codecs.py » ('j') | 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")
18 eq(base64.encodebytes(b""), b"") 19 eq(base64.encodebytes(b""), b"")
19 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" 20 eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz"
20 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 21 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21 b"0123456789!@#0^&*();:<>,. []{}"), 22 b"0123456789!@#0^&*();:<>,. []{}"),
22 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 23 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
23 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 24 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
24 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") 25 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n")
25 # Non-bytes 26 # Non-bytes
26 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')
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'')
42 # Non-bytes 46 # Non-bytes
43 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')
44 self.assertRaises(TypeError, base64.decodebytes, "") 50 self.assertRaises(TypeError, base64.decodebytes, "")
51 self.assertRaises(TypeError, base64.decodebytes, [])
45 52
46 def test_encode(self): 53 def test_encode(self):
47 eq = self.assertEqual 54 eq = self.assertEqual
48 from io import BytesIO, StringIO 55 from io import BytesIO, StringIO
49 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' 56 infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz'
50 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 57 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
51 b'0123456789!@#0^&*();:<>,. []{}') 58 b'0123456789!@#0^&*();:<>,. []{}')
52 outfp = BytesIO() 59 outfp = BytesIO()
53 base64.encode(infp, outfp) 60 base64.encode(infp, outfp)
54 eq(outfp.getvalue(), 61 eq(outfp.getvalue(),
(...skipping 30 matching lines...) Expand all
85 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" 92 eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz"
86 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 93 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
87 b"0123456789!@#0^&*();:<>,. []{}"), 94 b"0123456789!@#0^&*();:<>,. []{}"),
88 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 95 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
89 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 96 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
90 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 97 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
91 # Test with arbitrary alternative characters 98 # Test with arbitrary alternative characters
92 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')
93 # Non-bytes 100 # Non-bytes
94 eq(base64.b64encode(bytearray(b'abcd')), b'YWJjZA==') 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==')
95 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')), 104 eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')),
96 b'01a*b$cd') 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, [])
97 # Check if passing a str object raises an error 111 # Check if passing a str object raises an error
98 self.assertRaises(TypeError, base64.b64encode, "") 112 self.assertRaises(TypeError, base64.b64encode, "")
99 self.assertRaises(TypeError, base64.b64encode, b"", altchars="") 113 self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$")
100 # Test standard alphabet 114 # Test standard alphabet
101 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" ) 115 eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=" )
102 eq(base64.standard_b64encode(b"a"), b"YQ==") 116 eq(base64.standard_b64encode(b"a"), b"YQ==")
103 eq(base64.standard_b64encode(b"ab"), b"YWI=") 117 eq(base64.standard_b64encode(b"ab"), b"YWI=")
104 eq(base64.standard_b64encode(b"abc"), b"YWJj") 118 eq(base64.standard_b64encode(b"abc"), b"YWJj")
105 eq(base64.standard_b64encode(b""), b"") 119 eq(base64.standard_b64encode(b""), b"")
106 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" 120 eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz"
107 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 121 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108 b"0123456789!@#0^&*();:<>,. []{}"), 122 b"0123456789!@#0^&*();:<>,. []{}"),
109 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 123 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
110 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" 124 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
111 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") 125 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==")
112 # Non-bytes 126 # Non-bytes
113 eq(base64.standard_b64encode(bytearray(b'abcd')), b'YWJjZA==') 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, [])
114 # Check if passing a str object raises an error 131 # Check if passing a str object raises an error
115 self.assertRaises(TypeError, base64.standard_b64encode, "") 132 self.assertRaises(TypeError, base64.standard_b64encode, "")
116 # Test with 'URL safe' alternative characters 133 # Test with 'URL safe' alternative characters
117 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')
118 # Non-bytes 135 # Non-bytes
119 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')),
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, [])
120 # Check if passing a str object raises an error 143 # Check if passing a str object raises an error
121 self.assertRaises(TypeError, base64.urlsafe_b64encode, "") 144 self.assertRaises(TypeError, base64.urlsafe_b64encode, "")
122 145
123 def test_b64decode(self): 146 def test_b64decode(self):
124 eq = self.assertEqual 147 eq = self.assertEqual
125 148
126 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", 149 tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org",
127 b'AA==': b'\x00', 150 b'AA==': b'\x00',
128 b"YQ==": b"a", 151 b"YQ==": b"a",
129 b"YWI=": b"ab", 152 b"YWI=": b"ab",
130 b"YWJj": b"abc", 153 b"YWJj": b"abc",
131 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" 154 b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
132 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" 155 b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
133 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==": 156 b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==":
134 157
135 b"abcdefghijklmnopqrstuvwxyz" 158 b"abcdefghijklmnopqrstuvwxyz"
136 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 159 b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
137 b"0123456789!@#0^&*();:<>,. []{}", 160 b"0123456789!@#0^&*();:<>,. []{}",
138 b'': b'', 161 b'': b'',
139 } 162 }
140 for data, res in tests.items(): 163 for data, res in tests.items():
141 eq(base64.b64decode(data), res) 164 eq(base64.b64decode(data), res)
142 eq(base64.b64decode(data.decode('ascii')), res) 165 eq(base64.b64decode(data.decode('ascii')), res)
143 # Non-bytes 166 # Non-bytes
144 eq(base64.b64decode(bytearray(b"YWJj")), b"abc") 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, [])
145 171
146 # Test with arbitrary alternative characters 172 # Test with arbitrary alternative characters
147 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',
148 } 174 }
149 for (data, altchars), res in tests_altchars.items(): 175 for (data, altchars), res in tests_altchars.items():
150 data_str = data.decode('ascii') 176 data_str = data.decode('ascii')
151 altchars_str = altchars.decode('ascii') 177 altchars_str = altchars.decode('ascii')
152 178
153 eq(base64.b64decode(data, altchars=altchars), res) 179 eq(base64.b64decode(data, altchars=altchars), res)
154 eq(base64.b64decode(data_str, altchars=altchars), res) 180 eq(base64.b64decode(data_str, altchars=altchars), res)
155 eq(base64.b64decode(data, altchars=altchars_str), res) 181 eq(base64.b64decode(data, altchars=altchars_str), res)
156 eq(base64.b64decode(data_str, altchars=altchars_str), res) 182 eq(base64.b64decode(data_str, altchars=altchars_str), res)
157 183
158 # Test standard alphabet 184 # Test standard alphabet
159 for data, res in tests.items(): 185 for data, res in tests.items():
160 eq(base64.standard_b64decode(data), res) 186 eq(base64.standard_b64decode(data), res)
161 eq(base64.standard_b64decode(data.decode('ascii')), res) 187 eq(base64.standard_b64decode(data.decode('ascii')), res)
162 # Non-bytes 188 # Non-bytes
163 eq(base64.standard_b64decode(bytearray(b"YWJj")), b"abc") 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, [])
164 193
165 # Test with 'URL safe' alternative characters 194 # Test with 'URL safe' alternative characters
166 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', 195 tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d',
167 b'': b'', 196 b'': b'',
168 } 197 }
169 for data, res in tests_urlsafe.items(): 198 for data, res in tests_urlsafe.items():
170 eq(base64.urlsafe_b64decode(data), res) 199 eq(base64.urlsafe_b64decode(data), res)
171 eq(base64.urlsafe_b64decode(data.decode('ascii')), res) 200 eq(base64.urlsafe_b64decode(data.decode('ascii')), res)
172 # Non-bytes 201 # Non-bytes
173 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')),
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, [])
174 209
175 def test_b64decode_padding_error(self): 210 def test_b64decode_padding_error(self):
176 self.assertRaises(binascii.Error, base64.b64decode, b'abc') 211 self.assertRaises(binascii.Error, base64.b64decode, b'abc')
177 self.assertRaises(binascii.Error, base64.b64decode, 'abc') 212 self.assertRaises(binascii.Error, base64.b64decode, 'abc')
178 213
179 def test_b64decode_invalid_chars(self): 214 def test_b64decode_invalid_chars(self):
180 # issue 1466065: Test some invalid characters. 215 # issue 1466065: Test some invalid characters.
181 tests = ((b'%3d==', b'\xdd'), 216 tests = ((b'%3d==', b'\xdd'),
182 (b'$3d==', b'\xdd'), 217 (b'$3d==', b'\xdd'),
183 (b'[==', b''), 218 (b'[==', b''),
(...skipping 15 matching lines...) Expand all
199 eq = self.assertEqual 234 eq = self.assertEqual
200 eq(base64.b32encode(b''), b'') 235 eq(base64.b32encode(b''), b'')
201 eq(base64.b32encode(b'\x00'), b'AA======') 236 eq(base64.b32encode(b'\x00'), b'AA======')
202 eq(base64.b32encode(b'a'), b'ME======') 237 eq(base64.b32encode(b'a'), b'ME======')
203 eq(base64.b32encode(b'ab'), b'MFRA====') 238 eq(base64.b32encode(b'ab'), b'MFRA====')
204 eq(base64.b32encode(b'abc'), b'MFRGG===') 239 eq(base64.b32encode(b'abc'), b'MFRGG===')
205 eq(base64.b32encode(b'abcd'), b'MFRGGZA=') 240 eq(base64.b32encode(b'abcd'), b'MFRGGZA=')
206 eq(base64.b32encode(b'abcde'), b'MFRGGZDF') 241 eq(base64.b32encode(b'abcde'), b'MFRGGZDF')
207 # Non-bytes 242 # Non-bytes
208 eq(base64.b32encode(bytearray(b'abcd')), b'MFRGGZA=') 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=')
209 self.assertRaises(TypeError, base64.b32encode, "") 246 self.assertRaises(TypeError, base64.b32encode, "")
247 self.assertRaises(TypeError, base64.b32encode, [])
210 248
211 def test_b32decode(self): 249 def test_b32decode(self):
212 eq = self.assertEqual 250 eq = self.assertEqual
213 tests = {b'': b'', 251 tests = {b'': b'',
214 b'AA======': b'\x00', 252 b'AA======': b'\x00',
215 b'ME======': b'a', 253 b'ME======': b'a',
216 b'MFRA====': b'ab', 254 b'MFRA====': b'ab',
217 b'MFRGG===': b'abc', 255 b'MFRGG===': b'abc',
218 b'MFRGGZA=': b'abcd', 256 b'MFRGGZA=': b'abcd',
219 b'MFRGGZDF': b'abcde', 257 b'MFRGGZDF': b'abcde',
220 } 258 }
221 for data, res in tests.items(): 259 for data, res in tests.items():
222 eq(base64.b32decode(data), res) 260 eq(base64.b32decode(data), res)
223 eq(base64.b32decode(data.decode('ascii')), res) 261 eq(base64.b32decode(data.decode('ascii')), res)
224 # Non-bytes 262 # Non-bytes
225 eq(base64.b32decode(bytearray(b'MFRGG===')), b'abc') 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, [])
226 267
227 def test_b32decode_casefold(self): 268 def test_b32decode_casefold(self):
228 eq = self.assertEqual 269 eq = self.assertEqual
229 tests = {b'': b'', 270 tests = {b'': b'',
230 b'ME======': b'a', 271 b'ME======': b'a',
231 b'MFRA====': b'ab', 272 b'MFRA====': b'ab',
232 b'MFRGG===': b'abc', 273 b'MFRGG===': b'abc',
233 b'MFRGGZA=': b'abcd', 274 b'MFRGGZA=': b'abcd',
234 b'MFRGGZDF': b'abcde', 275 b'MFRGGZDF': b'abcde',
235 # Lower cases 276 # Lower cases
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 base64.b32decode(data) 310 base64.b32decode(data)
270 with self.assertRaises(binascii.Error): 311 with self.assertRaises(binascii.Error):
271 base64.b32decode(data.decode('ascii')) 312 base64.b32decode(data.decode('ascii'))
272 313
273 def test_b16encode(self): 314 def test_b16encode(self):
274 eq = self.assertEqual 315 eq = self.assertEqual
275 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') 316 eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
276 eq(base64.b16encode(b'\x00'), b'00') 317 eq(base64.b16encode(b'\x00'), b'00')
277 # Non-bytes 318 # Non-bytes
278 eq(base64.b16encode(bytearray(b'\x01\x02\xab\xcd\xef')), b'0102ABCDEF') 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')
279 self.assertRaises(TypeError, base64.b16encode, "") 322 self.assertRaises(TypeError, base64.b16encode, "")
323 self.assertRaises(TypeError, base64.b16encode, [])
280 324
281 def test_b16decode(self): 325 def test_b16decode(self):
282 eq = self.assertEqual 326 eq = self.assertEqual
283 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 327 eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
284 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef') 328 eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef')
285 eq(base64.b16decode(b'00'), b'\x00') 329 eq(base64.b16decode(b'00'), b'\x00')
286 eq(base64.b16decode('00'), b'\x00') 330 eq(base64.b16decode('00'), b'\x00')
287 # Lower case is not allowed without a flag 331 # Lower case is not allowed without a flag
288 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') 332 self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef')
289 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') 333 self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef')
290 # Case fold 334 # Case fold
291 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')
292 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') 336 eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef')
293 # Non-bytes 337 # Non-bytes
294 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')
347 self.assertRaises(TypeError, base64.b16decode, [])
295 348
296 def test_decode_nonascii_str(self): 349 def test_decode_nonascii_str(self):
297 decode_funcs = (base64.b64decode, 350 decode_funcs = (base64.b64decode,
298 base64.standard_b64decode, 351 base64.standard_b64decode,
299 base64.urlsafe_b64decode, 352 base64.urlsafe_b64decode,
300 base64.b32decode, 353 base64.b32decode,
301 base64.b16decode) 354 base64.b16decode)
302 for f in decode_funcs: 355 for f in decode_funcs:
303 self.assertRaises(ValueError, f, 'with non-ascii \xcb') 356 self.assertRaises(ValueError, f, 'with non-ascii \xcb')
304 357
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 output = self.get_output('-d', support.TESTFN) 394 output = self.get_output('-d', support.TESTFN)
342 self.assertEqual(output.rstrip(), b'a\xffb') 395 self.assertEqual(output.rstrip(), b'a\xffb')
343 396
344 397
345 398
346 def test_main(): 399 def test_main():
347 support.run_unittest(__name__) 400 support.run_unittest(__name__)
348 401
349 if __name__ == '__main__': 402 if __name__ == '__main__':
350 test_main() 403 test_main()
OLDNEW
« no previous file with comments | « Lib/base64.py ('k') | Lib/test/test_codecs.py » ('j') | no next file with comments »

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