Skip to content

Migrate unit/test_encryption_context.py from unittest to pytest #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions test/unit/test_encryption_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit test suite for aws_encryption_sdk.internal.formatting.encryption_context"""
import unittest

import pytest
import six

import aws_encryption_sdk.internal.defaults
import aws_encryption_sdk.internal.formatting.encryption_context
Expand All @@ -26,7 +23,7 @@
pytestmark = [pytest.mark.unit, pytest.mark.local]


class TestEncryptionContext(unittest.TestCase):
class TestEncryptionContext(object):
def test_assemble_content_aad(self):
"""Validate that the assemble_content_aad function
behaves as expected.
Expand All @@ -40,10 +37,11 @@ def test_assemble_content_aad(self):
assert test == VALUES["non_framed_aac"]

def test_assemble_content_aad_unknown_type(self):
with six.assertRaisesRegex(self, SerializationError, "Unknown aad_content_string"):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.assemble_content_aad(
message_id=VALUES["message_id"], aad_content_string=None, seq_num=1, length=VALUES["content_len"]
)
excinfo.match("Unknown aad_content_string")

def test_serialize_encryption_context_no_encryption_context(self):
"""Validate that the serialize_encryption_context
Expand All @@ -59,20 +57,22 @@ def test_serialize_encryption_context_too_many_elements(self):
with an encryption context with too many
elements.
"""
with six.assertRaisesRegex(self, SerializationError, "The encryption context contains too many elements."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
VALUES["encryption_context_too_many_elements"]
)
excinfo.match("The encryption context contains too many elements.")

def test_serialize_encryption_context_too_large(self):
"""Validate that the serialize_encryption_context
function behaves as expected when presented
with an encryption context which is too large.
"""
with six.assertRaisesRegex(self, SerializationError, "The serialized context is too large"):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
VALUES["encryption_context_too_large"]
)
excinfo.match("The serialized context is too large")

def test_serialize_encryption_context_unencodable(self):
"""Validate that the serialize_encryption_context
Expand All @@ -81,10 +81,11 @@ def test_serialize_encryption_context_unencodable(self):
unencodable elements.
"""
for encryption_context in [{"a": b"\xc4"}, {b"\xc4": "a"}, {b"\xc4": b"\xc4"}]:
with six.assertRaisesRegex(self, SerializationError, "Cannot encode dictionary key or value using *"):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.serialize_encryption_context(
encryption_context
)
excinfo.match("Cannot encode dictionary key or value using *")

def test_serialize_encryption_context_valid(self):
"""Validate that the serialize_encryption_context
Expand All @@ -100,8 +101,9 @@ def test_read_short_too_short(self):
"""Validate that the read_short function behaves
as expected when it encounters a struct error.
"""
with six.assertRaisesRegex(self, SerializationError, "Bad format of serialized context."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.read_short(b"d", 0)
excinfo.match("Bad format of serialized context.")

def test_read_short_valid(self):
"""Validate that the read_short function behaves
Expand All @@ -116,8 +118,9 @@ def test_read_string_encoding_error(self):
as expected when it encounters an encoding
error.
"""
with six.assertRaisesRegex(self, SerializationError, "Bad format of serialized context."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.read_string(b"\xc4", 0, 1)
excinfo.match("Bad format of serialized context.")

def test_read_string_valid(self):
"""Validate that the read_string function behaves
Expand All @@ -136,21 +139,23 @@ def test_deserialize_encryption_context_too_large(self):
data = ""
for i in range(aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE + 1):
data += str(i)
with six.assertRaisesRegex(self, SerializationError, "Serialized context is too long."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
serialized_encryption_context=data
)
excinfo.match("Serialized context is too long.")

def test_deserialize_encryption_context_duplicate_key(self):
"""Validate that the deserialize_encryption_context
function behaves as expected when it encounters
a serialized encryption context which contains
duplicate keys.
"""
with six.assertRaisesRegex(self, SerializationError, "Duplicate key in serialized context."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
serialized_encryption_context=VALUES["serialized_encryption_context_duplicate_key"]
)
excinfo.match("Duplicate key in serialized context.")

def test_deserialize_encryption_context_extra_data(self):
"""Validate that the deserialize_encryption_context
Expand All @@ -160,10 +165,11 @@ def test_deserialize_encryption_context_extra_data(self):
of pairs (formatting error).
"""
data = VALUES["serialized_encryption_context"] + b"jhofguijhsuskldfh"
with six.assertRaisesRegex(self, SerializationError, "Formatting error: Extra data in serialized context."):
with pytest.raises(SerializationError) as excinfo:
aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
serialized_encryption_context=data
)
excinfo.match("Formatting error: Extra data in serialized context.")

def test_deserialize_encryption_context_valid(self):
"""Validate that the deserialize_encryption_context
Expand Down