summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-02-23 22:30:50 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-02-23 22:30:50 (GMT)
commitee3074e1f473c2ebdc89e2b6889747fc586002e9 (patch)
tree92767bec7863d3a7c7e62ff507d88482f43cc623 /Lib/test
parente1d4e58777f28bb000fe95768186f70ff1de45a3 (diff)
downloadcpython-ee3074e1f473c2ebdc89e2b6889747fc586002e9.zip
cpython-ee3074e1f473c2ebdc89e2b6889747fc586002e9.tar.gz
cpython-ee3074e1f473c2ebdc89e2b6889747fc586002e9.tar.bz2
Issue #22088: Clarify base-64 alphabets and which characters are discarded
* There are only two base-64 alphabets defined by the RFCs, not three * Due to the internal translation, plus (+) and slash (/) are never discarded * standard_ and urlsafe_b64decode() discard characters as well Also update the doc strings to clarify data types, based on revision 92760d2edc9e, correct the exception raised by b16decode(), and correct the parameter name for the base-85 functions.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_base64.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py
index a0f548d..9b853a8 100644
--- a/Lib/test/test_base64.py
+++ b/Lib/test/test_base64.py
@@ -243,14 +243,26 @@ class BaseXYTestCase(unittest.TestCase):
(b'@@', b''),
(b'!', b''),
(b'YWJj\nYWI=', b'abcab'))
+ funcs = (
+ base64.b64decode,
+ base64.standard_b64decode,
+ base64.urlsafe_b64decode,
+ )
for bstr, res in tests:
- self.assertEqual(base64.b64decode(bstr), res)
- self.assertEqual(base64.b64decode(bstr.decode('ascii')), res)
+ for func in funcs:
+ with self.subTest(bstr=bstr, func=func):
+ self.assertEqual(func(bstr), res)
+ self.assertEqual(func(bstr.decode('ascii')), res)
with self.assertRaises(binascii.Error):
base64.b64decode(bstr, validate=True)
with self.assertRaises(binascii.Error):
base64.b64decode(bstr.decode('ascii'), validate=True)
+ # Normal alphabet characters not discarded when alternative given
+ res = b'\xFB\xEF\xBE\xFF\xFF\xFF'
+ self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
+ self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res)
+
def test_b32encode(self):
eq = self.assertEqual
eq(base64.b32encode(b''), b'')
@@ -360,6 +372,10 @@ class BaseXYTestCase(unittest.TestCase):
b'\x01\x02\xab\xcd\xef')
eq(base64.b16decode(array('B', b"0102abcdef"), True),
b'\x01\x02\xab\xcd\xef')
+ # Non-alphabet characters
+ self.assertRaises(binascii.Error, base64.b16decode, '0102AG')
+ # Incorrect "padding"
+ self.assertRaises(binascii.Error, base64.b16decode, '010')
def test_a85encode(self):
eq = self.assertEqual