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 |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import subprocess |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 8 | import struct |
| 9 | from array import array |
Raymond Hettinger | 2ae8753 | 2002-05-18 00:25:10 +0000 | [diff] [blame] | 10 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 12 | class LegacyBase64TestCase(unittest.TestCase): |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 13 | |
| 14 | # Legacy API is not as permissive as the modern API |
| 15 | def check_type_errors(self, f): |
| 16 | self.assertRaises(TypeError, f, "") |
| 17 | self.assertRaises(TypeError, f, []) |
| 18 | multidimensional = memoryview(b"1234").cast('B', (2, 2)) |
| 19 | self.assertRaises(TypeError, f, multidimensional) |
| 20 | int_data = memoryview(b"1234").cast('I') |
| 21 | self.assertRaises(TypeError, f, int_data) |
| 22 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 23 | def test_encodebytes(self): |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 24 | eq = self.assertEqual |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 25 | eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n") |
| 26 | eq(base64.encodebytes(b"a"), b"YQ==\n") |
| 27 | eq(base64.encodebytes(b"ab"), b"YWI=\n") |
| 28 | eq(base64.encodebytes(b"abc"), b"YWJj\n") |
| 29 | eq(base64.encodebytes(b""), b"") |
| 30 | eq(base64.encodebytes(b"abcdefghijklmnopqrstuvwxyz" |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 31 | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 32 | b"0123456789!@#0^&*();:<>,. []{}"), |
| 33 | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
| 34 | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
| 35 | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n") |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 36 | # Non-bytes |
| 37 | eq(base64.encodebytes(bytearray(b'abc')), b'YWJj\n') |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 38 | eq(base64.encodebytes(memoryview(b'abc')), b'YWJj\n') |
| 39 | eq(base64.encodebytes(array('B', b'abc')), b'YWJj\n') |
| 40 | self.check_type_errors(base64.encodebytes) |
Guido van Rossum | cb68258 | 2002-08-22 19:18:56 +0000 | [diff] [blame] | 41 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 42 | def test_decodebytes(self): |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 43 | eq = self.assertEqual |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 44 | eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org") |
| 45 | eq(base64.decodebytes(b"YQ==\n"), b"a") |
| 46 | eq(base64.decodebytes(b"YWI=\n"), b"ab") |
| 47 | eq(base64.decodebytes(b"YWJj\n"), b"abc") |
| 48 | eq(base64.decodebytes(b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 49 | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
| 50 | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n"), |
| 51 | b"abcdefghijklmnopqrstuvwxyz" |
| 52 | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 53 | b"0123456789!@#0^&*();:<>,. []{}") |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 54 | eq(base64.decodebytes(b''), b'') |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 55 | # Non-bytes |
| 56 | eq(base64.decodebytes(bytearray(b'YWJj\n')), b'abc') |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 57 | eq(base64.decodebytes(memoryview(b'YWJj\n')), b'abc') |
| 58 | eq(base64.decodebytes(array('B', b'YWJj\n')), b'abc') |
| 59 | self.check_type_errors(base64.decodebytes) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 60 | |
| 61 | def test_encode(self): |
| 62 | eq = self.assertEqual |
Serhiy Storchaka | abac0a7 | 2013-04-28 15:56:11 +0300 | [diff] [blame] | 63 | from io import BytesIO, StringIO |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 64 | infp = BytesIO(b'abcdefghijklmnopqrstuvwxyz' |
| 65 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 66 | b'0123456789!@#0^&*();:<>,. []{}') |
| 67 | outfp = BytesIO() |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 68 | base64.encode(infp, outfp) |
| 69 | eq(outfp.getvalue(), |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 70 | b'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE' |
| 71 | b'RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT' |
| 72 | b'Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==\n') |
Serhiy Storchaka | abac0a7 | 2013-04-28 15:56:11 +0300 | [diff] [blame] | 73 | # Non-binary files |
| 74 | self.assertRaises(TypeError, base64.encode, StringIO('abc'), BytesIO()) |
| 75 | self.assertRaises(TypeError, base64.encode, BytesIO(b'abc'), StringIO()) |
| 76 | self.assertRaises(TypeError, base64.encode, StringIO('abc'), StringIO()) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 77 | |
| 78 | def test_decode(self): |
Serhiy Storchaka | abac0a7 | 2013-04-28 15:56:11 +0300 | [diff] [blame] | 79 | from io import BytesIO, StringIO |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 80 | infp = BytesIO(b'd3d3LnB5dGhvbi5vcmc=') |
| 81 | outfp = BytesIO() |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 82 | base64.decode(infp, outfp) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 83 | self.assertEqual(outfp.getvalue(), b'www.python.org') |
Serhiy Storchaka | abac0a7 | 2013-04-28 15:56:11 +0300 | [diff] [blame] | 84 | # Non-binary files |
| 85 | self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), BytesIO()) |
| 86 | self.assertRaises(TypeError, base64.encode, BytesIO(b'YWJj\n'), StringIO()) |
| 87 | self.assertRaises(TypeError, base64.encode, StringIO('YWJj\n'), StringIO()) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 88 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 89 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 90 | class BaseXYTestCase(unittest.TestCase): |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 91 | |
| 92 | # Modern API completely ignores exported dimension and format data and |
| 93 | # treats any buffer as a stream of bytes |
| 94 | def check_encode_type_errors(self, f): |
| 95 | self.assertRaises(TypeError, f, "") |
| 96 | self.assertRaises(TypeError, f, []) |
| 97 | |
| 98 | def check_decode_type_errors(self, f): |
| 99 | self.assertRaises(TypeError, f, []) |
| 100 | |
| 101 | def check_other_types(self, f, bytes_data, expected): |
| 102 | eq = self.assertEqual |
| 103 | eq(f(bytearray(bytes_data)), expected) |
| 104 | eq(f(memoryview(bytes_data)), expected) |
| 105 | eq(f(array('B', bytes_data)), expected) |
| 106 | self.check_nonbyte_element_format(base64.b64encode, bytes_data) |
| 107 | self.check_multidimensional(base64.b64encode, bytes_data) |
| 108 | |
| 109 | def check_multidimensional(self, f, data): |
| 110 | padding = b"\x00" if len(data) % 2 else b"" |
| 111 | bytes_data = data + padding # Make sure cast works |
| 112 | shape = (len(bytes_data) // 2, 2) |
| 113 | multidimensional = memoryview(bytes_data).cast('B', shape) |
| 114 | self.assertEqual(f(multidimensional), f(bytes_data)) |
| 115 | |
| 116 | def check_nonbyte_element_format(self, f, data): |
| 117 | padding = b"\x00" * ((4 - len(data)) % 4) |
| 118 | bytes_data = data + padding # Make sure cast works |
| 119 | int_data = memoryview(bytes_data).cast('I') |
| 120 | self.assertEqual(f(int_data), f(bytes_data)) |
| 121 | |
| 122 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 123 | def test_b64encode(self): |
| 124 | eq = self.assertEqual |
| 125 | # Test default alphabet |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 126 | eq(base64.b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") |
| 127 | eq(base64.b64encode(b'\x00'), b'AA==') |
| 128 | eq(base64.b64encode(b"a"), b"YQ==") |
| 129 | eq(base64.b64encode(b"ab"), b"YWI=") |
| 130 | eq(base64.b64encode(b"abc"), b"YWJj") |
| 131 | eq(base64.b64encode(b""), b"") |
| 132 | eq(base64.b64encode(b"abcdefghijklmnopqrstuvwxyz" |
| 133 | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 134 | b"0123456789!@#0^&*();:<>,. []{}"), |
| 135 | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
| 136 | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" |
| 137 | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 138 | # Test with arbitrary alternative characters |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 139 | 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] | 140 | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=bytearray(b'*$')), |
| 141 | b'01a*b$cd') |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 142 | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=memoryview(b'*$')), |
| 143 | b'01a*b$cd') |
| 144 | eq(base64.b64encode(b'\xd3V\xbeo\xf7\x1d', altchars=array('B', b'*$')), |
| 145 | b'01a*b$cd') |
| 146 | # Non-bytes |
| 147 | self.check_other_types(base64.b64encode, b'abcd', b'YWJjZA==') |
| 148 | self.check_encode_type_errors(base64.b64encode) |
| 149 | self.assertRaises(TypeError, base64.b64encode, b"", altchars="*$") |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 150 | # Test standard alphabet |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 151 | eq(base64.standard_b64encode(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=") |
| 152 | eq(base64.standard_b64encode(b"a"), b"YQ==") |
| 153 | eq(base64.standard_b64encode(b"ab"), b"YWI=") |
| 154 | eq(base64.standard_b64encode(b"abc"), b"YWJj") |
| 155 | eq(base64.standard_b64encode(b""), b"") |
| 156 | eq(base64.standard_b64encode(b"abcdefghijklmnopqrstuvwxyz" |
| 157 | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 158 | b"0123456789!@#0^&*();:<>,. []{}"), |
| 159 | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
| 160 | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT" |
| 161 | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==") |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 162 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 163 | self.check_other_types(base64.standard_b64encode, |
| 164 | b'abcd', b'YWJjZA==') |
| 165 | self.check_encode_type_errors(base64.standard_b64encode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 166 | # Test with 'URL safe' alternative characters |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 167 | 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] | 168 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 169 | self.check_other_types(base64.urlsafe_b64encode, |
| 170 | b'\xd3V\xbeo\xf7\x1d', b'01a-b_cd') |
| 171 | self.check_encode_type_errors(base64.urlsafe_b64encode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 172 | |
| 173 | def test_b64decode(self): |
| 174 | eq = self.assertEqual |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 175 | |
| 176 | tests = {b"d3d3LnB5dGhvbi5vcmc=": b"www.python.org", |
| 177 | b'AA==': b'\x00', |
| 178 | b"YQ==": b"a", |
| 179 | b"YWI=": b"ab", |
| 180 | b"YWJj": b"abc", |
| 181 | b"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE" |
| 182 | b"RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT" |
| 183 | b"Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ==": |
| 184 | |
| 185 | b"abcdefghijklmnopqrstuvwxyz" |
| 186 | b"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 187 | b"0123456789!@#0^&*();:<>,. []{}", |
| 188 | b'': b'', |
| 189 | } |
| 190 | for data, res in tests.items(): |
| 191 | eq(base64.b64decode(data), res) |
| 192 | eq(base64.b64decode(data.decode('ascii')), res) |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 193 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 194 | self.check_other_types(base64.b64decode, b"YWJj", b"abc") |
| 195 | self.check_decode_type_errors(base64.b64decode) |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 196 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 197 | # Test with arbitrary alternative characters |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 198 | tests_altchars = {(b'01a*b$cd', b'*$'): b'\xd3V\xbeo\xf7\x1d', |
| 199 | } |
| 200 | for (data, altchars), res in tests_altchars.items(): |
| 201 | data_str = data.decode('ascii') |
| 202 | altchars_str = altchars.decode('ascii') |
| 203 | |
| 204 | eq(base64.b64decode(data, altchars=altchars), res) |
| 205 | eq(base64.b64decode(data_str, altchars=altchars), res) |
| 206 | eq(base64.b64decode(data, altchars=altchars_str), res) |
| 207 | eq(base64.b64decode(data_str, altchars=altchars_str), res) |
| 208 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 209 | # Test standard alphabet |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 210 | for data, res in tests.items(): |
| 211 | eq(base64.standard_b64decode(data), res) |
| 212 | eq(base64.standard_b64decode(data.decode('ascii')), res) |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 213 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 214 | self.check_other_types(base64.standard_b64decode, b"YWJj", b"abc") |
| 215 | self.check_decode_type_errors(base64.standard_b64decode) |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 216 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 217 | # Test with 'URL safe' alternative characters |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 218 | tests_urlsafe = {b'01a-b_cd': b'\xd3V\xbeo\xf7\x1d', |
| 219 | b'': b'', |
| 220 | } |
| 221 | for data, res in tests_urlsafe.items(): |
| 222 | eq(base64.urlsafe_b64decode(data), res) |
| 223 | eq(base64.urlsafe_b64decode(data.decode('ascii')), res) |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 224 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 225 | self.check_other_types(base64.urlsafe_b64decode, b'01a-b_cd', |
| 226 | b'\xd3V\xbeo\xf7\x1d') |
| 227 | self.check_decode_type_errors(base64.urlsafe_b64decode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 228 | |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 229 | def test_b64decode_padding_error(self): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 230 | self.assertRaises(binascii.Error, base64.b64decode, b'abc') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 231 | self.assertRaises(binascii.Error, base64.b64decode, 'abc') |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 232 | |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 233 | def test_b64decode_invalid_chars(self): |
| 234 | # issue 1466065: Test some invalid characters. |
| 235 | tests = ((b'%3d==', b'\xdd'), |
| 236 | (b'$3d==', b'\xdd'), |
| 237 | (b'[==', b''), |
| 238 | (b'YW]3=', b'am'), |
| 239 | (b'3{d==', b'\xdd'), |
| 240 | (b'3d}==', b'\xdd'), |
| 241 | (b'@@', b''), |
| 242 | (b'!', b''), |
| 243 | (b'YWJj\nYWI=', b'abcab')) |
| 244 | for bstr, res in tests: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 245 | self.assertEqual(base64.b64decode(bstr), res) |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 246 | self.assertEqual(base64.b64decode(bstr.decode('ascii')), res) |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 247 | with self.assertRaises(binascii.Error): |
| 248 | base64.b64decode(bstr, validate=True) |
Antoine Pitrou | dff46fa | 2012-02-20 19:46:26 +0100 | [diff] [blame] | 249 | with self.assertRaises(binascii.Error): |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 250 | base64.b64decode(bstr.decode('ascii'), validate=True) |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 251 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 252 | def test_b32encode(self): |
| 253 | eq = self.assertEqual |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 254 | eq(base64.b32encode(b''), b'') |
| 255 | eq(base64.b32encode(b'\x00'), b'AA======') |
| 256 | eq(base64.b32encode(b'a'), b'ME======') |
| 257 | eq(base64.b32encode(b'ab'), b'MFRA====') |
| 258 | eq(base64.b32encode(b'abc'), b'MFRGG===') |
| 259 | eq(base64.b32encode(b'abcd'), b'MFRGGZA=') |
| 260 | eq(base64.b32encode(b'abcde'), b'MFRGGZDF') |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 261 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 262 | self.check_other_types(base64.b32encode, b'abcd', b'MFRGGZA=') |
| 263 | self.check_encode_type_errors(base64.b32encode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 264 | |
| 265 | def test_b32decode(self): |
| 266 | eq = self.assertEqual |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 267 | tests = {b'': b'', |
| 268 | b'AA======': b'\x00', |
| 269 | b'ME======': b'a', |
| 270 | b'MFRA====': b'ab', |
| 271 | b'MFRGG===': b'abc', |
| 272 | b'MFRGGZA=': b'abcd', |
| 273 | b'MFRGGZDF': b'abcde', |
| 274 | } |
| 275 | for data, res in tests.items(): |
| 276 | eq(base64.b32decode(data), res) |
| 277 | eq(base64.b32decode(data.decode('ascii')), res) |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 278 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 279 | self.check_other_types(base64.b32decode, b'MFRGG===', b"abc") |
| 280 | self.check_decode_type_errors(base64.b32decode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 281 | |
| 282 | def test_b32decode_casefold(self): |
| 283 | eq = self.assertEqual |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 284 | tests = {b'': b'', |
| 285 | b'ME======': b'a', |
| 286 | b'MFRA====': b'ab', |
| 287 | b'MFRGG===': b'abc', |
| 288 | b'MFRGGZA=': b'abcd', |
| 289 | b'MFRGGZDF': b'abcde', |
| 290 | # Lower cases |
| 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 | |
| 298 | for data, res in tests.items(): |
| 299 | eq(base64.b32decode(data, True), res) |
| 300 | eq(base64.b32decode(data.decode('ascii'), True), res) |
| 301 | |
Serhiy Storchaka | ea2b490 | 2013-05-28 15:27:29 +0300 | [diff] [blame] | 302 | self.assertRaises(binascii.Error, base64.b32decode, b'me======') |
| 303 | self.assertRaises(binascii.Error, base64.b32decode, 'me======') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 304 | |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 305 | # Mapping zero and one |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 306 | eq(base64.b32decode(b'MLO23456'), b'b\xdd\xad\xf3\xbe') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 307 | eq(base64.b32decode('MLO23456'), b'b\xdd\xad\xf3\xbe') |
| 308 | |
| 309 | map_tests = {(b'M1023456', b'L'): b'b\xdd\xad\xf3\xbe', |
| 310 | (b'M1023456', b'I'): b'b\x1d\xad\xf3\xbe', |
| 311 | } |
| 312 | for (data, map01), res in map_tests.items(): |
| 313 | data_str = data.decode('ascii') |
| 314 | map01_str = map01.decode('ascii') |
| 315 | |
| 316 | eq(base64.b32decode(data, map01=map01), res) |
| 317 | eq(base64.b32decode(data_str, map01=map01), res) |
| 318 | eq(base64.b32decode(data, map01=map01_str), res) |
| 319 | eq(base64.b32decode(data_str, map01=map01_str), res) |
Serhiy Storchaka | ea2b490 | 2013-05-28 15:27:29 +0300 | [diff] [blame] | 320 | self.assertRaises(binascii.Error, base64.b32decode, data) |
| 321 | self.assertRaises(binascii.Error, base64.b32decode, data_str) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 322 | |
| 323 | def test_b32decode_error(self): |
Serhiy Storchaka | ea2b490 | 2013-05-28 15:27:29 +0300 | [diff] [blame] | 324 | for data in [b'abc', b'ABCDEF==', b'==ABCDEF']: |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 325 | with self.assertRaises(binascii.Error): |
| 326 | base64.b32decode(data) |
Antoine Pitrou | dff46fa | 2012-02-20 19:46:26 +0100 | [diff] [blame] | 327 | with self.assertRaises(binascii.Error): |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 328 | base64.b32decode(data.decode('ascii')) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 329 | |
| 330 | def test_b16encode(self): |
| 331 | eq = self.assertEqual |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 332 | eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') |
| 333 | eq(base64.b16encode(b'\x00'), b'00') |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 334 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 335 | self.check_other_types(base64.b16encode, b'\x01\x02\xab\xcd\xef', |
| 336 | b'0102ABCDEF') |
| 337 | self.check_encode_type_errors(base64.b16encode) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 338 | |
| 339 | def test_b16decode(self): |
| 340 | eq = self.assertEqual |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 341 | eq(base64.b16decode(b'0102ABCDEF'), b'\x01\x02\xab\xcd\xef') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 342 | eq(base64.b16decode('0102ABCDEF'), b'\x01\x02\xab\xcd\xef') |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 343 | eq(base64.b16decode(b'00'), b'\x00') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 344 | eq(base64.b16decode('00'), b'\x00') |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 345 | # Lower case is not allowed without a flag |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 346 | self.assertRaises(binascii.Error, base64.b16decode, b'0102abcdef') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 347 | self.assertRaises(binascii.Error, base64.b16decode, '0102abcdef') |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 348 | # Case fold |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 349 | eq(base64.b16decode(b'0102abcdef', True), b'\x01\x02\xab\xcd\xef') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 350 | eq(base64.b16decode('0102abcdef', True), b'\x01\x02\xab\xcd\xef') |
Serhiy Storchaka | 017523c | 2013-04-28 15:53:08 +0300 | [diff] [blame] | 351 | # Non-bytes |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 352 | self.check_other_types(base64.b16decode, b"0102ABCDEF", |
| 353 | b'\x01\x02\xab\xcd\xef') |
| 354 | self.check_decode_type_errors(base64.b16decode) |
| 355 | eq(base64.b16decode(bytearray(b"0102abcdef"), True), |
| 356 | b'\x01\x02\xab\xcd\xef') |
| 357 | eq(base64.b16decode(memoryview(b"0102abcdef"), True), |
| 358 | b'\x01\x02\xab\xcd\xef') |
| 359 | eq(base64.b16decode(array('B', b"0102abcdef"), True), |
| 360 | b'\x01\x02\xab\xcd\xef') |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 361 | |
| 362 | def test_decode_nonascii_str(self): |
| 363 | decode_funcs = (base64.b64decode, |
| 364 | base64.standard_b64decode, |
| 365 | base64.urlsafe_b64decode, |
| 366 | base64.b32decode, |
| 367 | base64.b16decode) |
| 368 | for f in decode_funcs: |
| 369 | self.assertRaises(ValueError, f, 'with non-ascii \xcb') |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 370 | |
| 371 | def test_ErrorHeritage(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 372 | self.assertTrue(issubclass(binascii.Error, ValueError)) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 373 | |
| 374 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 375 | |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 376 | class TestMain(unittest.TestCase): |
Vinay Sajip | f959618 | 2012-03-02 01:01:13 +0000 | [diff] [blame] | 377 | def tearDown(self): |
| 378 | if os.path.exists(support.TESTFN): |
| 379 | os.unlink(support.TESTFN) |
| 380 | |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 381 | def get_output(self, *args, **options): |
| 382 | args = (sys.executable, '-m', 'base64') + args |
| 383 | return subprocess.check_output(args, **options) |
| 384 | |
| 385 | def test_encode_decode(self): |
| 386 | output = self.get_output('-t') |
| 387 | self.assertSequenceEqual(output.splitlines(), ( |
| 388 | b"b'Aladdin:open sesame'", |
| 389 | br"b'QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n'", |
| 390 | b"b'Aladdin:open sesame'", |
| 391 | )) |
| 392 | |
| 393 | def test_encode_file(self): |
| 394 | with open(support.TESTFN, 'wb') as fp: |
| 395 | fp.write(b'a\xffb\n') |
| 396 | |
| 397 | output = self.get_output('-e', support.TESTFN) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 398 | self.assertEqual(output.rstrip(), b'Yf9iCg==') |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 399 | |
| 400 | with open(support.TESTFN, 'rb') as fp: |
| 401 | output = self.get_output('-e', stdin=fp) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 402 | self.assertEqual(output.rstrip(), b'Yf9iCg==') |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 403 | |
| 404 | def test_decode(self): |
| 405 | with open(support.TESTFN, 'wb') as fp: |
| 406 | fp.write(b'Yf9iCg==') |
| 407 | output = self.get_output('-d', support.TESTFN) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 408 | self.assertEqual(output.rstrip(), b'a\xffb') |
Victor Stinner | 479736b | 2010-05-25 21:12:34 +0000 | [diff] [blame] | 409 | |
| 410 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 411 | |
Raymond Hettinger | 2ae8753 | 2002-05-18 00:25:10 +0000 | [diff] [blame] | 412 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 413 | support.run_unittest(__name__) |
Barry Warsaw | 4f019d3 | 2004-01-04 01:13:02 +0000 | [diff] [blame] | 414 | |
| 415 | if __name__ == '__main__': |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 416 | test_main() |