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