ยปCore Development>Code coverage>Lib/test/test_base64.py

Python code coverage for Lib/test/test_base64.py

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