PY3: Ensure normalize_before_encryption encodes b64payload

In Python 3, str and bytes are separate types, whereas in
Python 2 they were the same type.

Change-Id: Iba84b329f9eae5e9851aa6effb96ff24cb7eb1bc
Story: 2004170
Task: 27645
This commit is contained in:
Corey Bryant 2018-10-25 09:07:16 -04:00 committed by Douglas Mendizábal
parent af35f4e475
commit be4f35dcc8
2 changed files with 26 additions and 1 deletions

View File

@ -60,7 +60,10 @@ def normalize_before_encryption(unencrypted, content_type, content_encoding,
if not content_encoding:
b64payload = base64.encode_as_bytes(unencrypted)
elif content_encoding.lower() == 'base64':
b64payload = unencrypted
if not isinstance(unencrypted, six.binary_type):
b64payload = unencrypted.encode('utf-8')
else:
b64payload = unencrypted
elif enforce_text_only:
# For text-based protocols (such as the one-step secret POST),
# only 'base64' encoding is possible/supported.

View File

@ -174,6 +174,28 @@ class WhenNormalizingBeforeEncryption(utils.BaseTestCase):
self.assertEqual(base64.encode_as_bytes('bam'), unencrypted)
self.assertEqual('application/octet-stream', content_type)
def test_can_normalize_base64_str(self):
unencrypted, content_type = self.normalize(
unencrypted=base64.encode_as_bytes('stuff').decode('utf-8'),
content_type='application/octet-stream',
content_encoding='base64',
secret_type=s.SecretType.OPAQUE
)
self.assertEqual(base64.encode_as_bytes('stuff'), unencrypted)
self.assertEqual('application/octet-stream', content_type)
def test_can_normalize_base64_bytes(self):
unencrypted, content_type = self.normalize(
unencrypted=base64.encode_as_bytes('stuff'),
content_type='application/octet-stream',
content_encoding='base64',
secret_type=s.SecretType.OPAQUE
)
self.assertEqual(base64.encode_as_bytes('stuff'), unencrypted)
self.assertEqual('application/octet-stream', content_type)
@utils.parameterized_dataset(dataset_for_raised_exceptions)
def test_normalize_raising_exceptions_with(self, exception, **kwargs):
self.assertRaises(exception, self.normalize, **kwargs)