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