diff options
| author | Filipe LaĆns <lains@archlinux.org> | 2020-08-10 15:48:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-10 07:48:20 -0700 |
| commit | 4ce6faa6c9591de6079347eccc9e61ae4e8d9e31 (patch) | |
| tree | 72e6015f5de2f32e283864275cc48b72de8bb969 /Lib/test/test_base64.py | |
| parent | 39042e00ab01d6521548c1b7cc6554c09f4389ff (diff) | |
| download | cpython-git-4ce6faa6c9591de6079347eccc9e61ae4e8d9e31.tar.gz | |
bpo-16995: add support for base32 extended hex (base32hex) (GH-20441)
cc @pganssle
Automerge-Triggered-By: @pganssle
Diffstat (limited to 'Lib/test/test_base64.py')
| -rw-r--r-- | Lib/test/test_base64.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 1f67e46cd2..4f62c4115f 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -351,6 +351,76 @@ class BaseXYTestCase(unittest.TestCase): with self.assertRaises(binascii.Error): base64.b32decode(data.decode('ascii')) + def test_b32hexencode(self): + test_cases = [ + # to_encode, expected + (b'', b''), + (b'\x00', b'00======'), + (b'a', b'C4======'), + (b'ab', b'C5H0===='), + (b'abc', b'C5H66==='), + (b'abcd', b'C5H66P0='), + (b'abcde', b'C5H66P35'), + ] + for to_encode, expected in test_cases: + with self.subTest(to_decode=to_encode): + self.assertEqual(base64.b32hexencode(to_encode), expected) + + def test_b32hexencode_other_types(self): + self.check_other_types(base64.b32hexencode, b'abcd', b'C5H66P0=') + self.check_encode_type_errors(base64.b32hexencode) + + def test_b32hexdecode(self): + test_cases = [ + # to_decode, expected, casefold + (b'', b'', False), + (b'00======', b'\x00', False), + (b'C4======', b'a', False), + (b'C5H0====', b'ab', False), + (b'C5H66===', b'abc', False), + (b'C5H66P0=', b'abcd', False), + (b'C5H66P35', b'abcde', False), + (b'', b'', True), + (b'00======', b'\x00', True), + (b'C4======', b'a', True), + (b'C5H0====', b'ab', True), + (b'C5H66===', b'abc', True), + (b'C5H66P0=', b'abcd', True), + (b'C5H66P35', b'abcde', True), + (b'c4======', b'a', True), + (b'c5h0====', b'ab', True), + (b'c5h66===', b'abc', True), + (b'c5h66p0=', b'abcd', True), + (b'c5h66p35', b'abcde', True), + ] + for to_decode, expected, casefold in test_cases: + with self.subTest(to_decode=to_decode, casefold=casefold): + self.assertEqual(base64.b32hexdecode(to_decode, casefold), + expected) + self.assertEqual(base64.b32hexdecode(to_decode.decode('ascii'), + casefold), expected) + + def test_b32hexdecode_other_types(self): + self.check_other_types(base64.b32hexdecode, b'C5H66===', b'abc') + self.check_decode_type_errors(base64.b32hexdecode) + + def test_b32hexdecode_error(self): + tests = [b'abc', b'ABCDEF==', b'==ABCDEF', b'c4======'] + prefixes = [b'M', b'ME', b'MFRA', b'MFRGG', b'MFRGGZA', b'MFRGGZDF'] + for i in range(0, 17): + if i: + tests.append(b'='*i) + for prefix in prefixes: + if len(prefix) + i != 8: + tests.append(prefix + b'='*i) + for data in tests: + with self.subTest(to_decode=data): + with self.assertRaises(binascii.Error): + base64.b32hexdecode(data) + with self.assertRaises(binascii.Error): + base64.b32hexdecode(data.decode('ascii')) + + def test_b16encode(self): eq = self.assertEqual eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF') |
