-
Notifications
You must be signed in to change notification settings - Fork 86
Migrate unit/test_deserialize.py from unittest from pytest #117
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
"""Unit test suite for aws_encryption_sdk.deserialize""" | ||
import io | ||
import struct | ||
import unittest | ||
|
||
import pytest | ||
import six | ||
|
@@ -56,8 +55,9 @@ def test_deserialize_tag(): | |
assert parsed_tag == tag | ||
|
||
|
||
class TestDeserialize(unittest.TestCase): | ||
def setUp(self): | ||
class TestDeserialize(object): | ||
@pytest.fixture(autouse=True) | ||
def apply_fixtures(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #127 : need to run teardown actions. |
||
self.mock_wrapping_algorithm = MagicMock() | ||
self.mock_wrapping_algorithm.algorithm.iv_len = VALUES["iv_len"] | ||
|
||
|
@@ -109,29 +109,32 @@ def test_validate_header_invalid(self): | |
as expected for a valid header. | ||
""" | ||
self.mock_decrypt.side_effect = InvalidTag() | ||
with six.assertRaisesRegex(self, SerializationError, "Header authorization failed"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.validate_header( | ||
header=VALUES["deserialized_header_block"], | ||
header_auth=VALUES["deserialized_header_auth_block"], | ||
raw_header=VALUES["header"], | ||
data_key=VALUES["data_key_obj"], | ||
) | ||
excinfo.match("Header authorization failed") | ||
|
||
def test_deserialize_header_unknown_object_type(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for an unknown object type. | ||
""" | ||
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported type *"): | ||
with pytest.raises(NotSupportedError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_invalid_object_type"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Unsupported type *") | ||
|
||
def test_deserialize_header_unknown_version(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for an unknown message version. | ||
""" | ||
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported version *"): | ||
with pytest.raises(NotSupportedError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_invalid_version"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Unsupported version *") | ||
|
||
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id") | ||
def test_deserialize_header_unsupported_data_encryption_algorithm(self, mock_algorithm_get): | ||
|
@@ -141,65 +144,70 @@ def test_deserialize_header_unsupported_data_encryption_algorithm(self, mock_alg | |
mock_unsupported_algorithm = MagicMock() | ||
mock_unsupported_algorithm.allowed = False | ||
mock_algorithm_get.return_value = mock_unsupported_algorithm | ||
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported algorithm *"): | ||
with pytest.raises(NotSupportedError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_disallowed_algorithm"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Unsupported algorithm *") | ||
|
||
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id") | ||
def test_deserialize_header_unknown_data_encryption_algorithm(self, mock_algorithm_get): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for an unknown algorithm. | ||
""" | ||
mock_algorithm_get.side_effect = KeyError() | ||
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown algorithm *"): | ||
with pytest.raises(UnknownIdentityError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_invalid_algorithm"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Unknown algorithm *") | ||
|
||
def test_deserialize_header_unknown_content_type(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for an unknown content type. | ||
""" | ||
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown content type *"): | ||
with pytest.raises(UnknownIdentityError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_unknown_content_type"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Unknown content type *") | ||
|
||
def test_deserialize_header_invalid_reserved_space(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for an invalid value in the reserved | ||
space (formerly content AAD). | ||
""" | ||
with six.assertRaisesRegex( | ||
self, SerializationError, "Content AAD length field is currently unused, its value must be always 0" | ||
): | ||
with pytest.raises(SerializationError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_bad_reserved_space"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Content AAD length field is currently unused, its value must be always 0") | ||
|
||
def test_deserialize_header_bad_iv_len(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for bad IV length (incompatible with | ||
specified algorithm). | ||
""" | ||
with six.assertRaisesRegex(self, SerializationError, "Specified IV length *"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_bad_iv_len"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Specified IV length *") | ||
|
||
def test_deserialize_header_framed_bad_frame_length(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for bad frame length values (greater than | ||
the default maximum). | ||
""" | ||
with six.assertRaisesRegex(self, SerializationError, "Specified frame length larger than allowed maximum: *"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_header_bad_frame_len"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Specified frame length larger than allowed maximum: *") | ||
|
||
def test_deserialize_header_non_framed_bad_frame_length(self): | ||
"""Validate that the deserialize_header function behaves | ||
as expected for bad frame length values for non-framed | ||
messages (non-zero). | ||
""" | ||
with six.assertRaisesRegex(self, SerializationError, "Non-zero frame length found for non-framed message"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
stream = io.BytesIO(VALUES["serialized_non_framed_header_bad_frame_len"]) | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream) | ||
excinfo.match("Non-zero frame length found for non-framed message") | ||
|
||
def test_deserialize_header_valid(self): | ||
"""Validate that the deserialize_header function behaves | ||
|
@@ -247,10 +255,11 @@ def test_deserialize_body_frame_final_invalid_final_frame_length(self): | |
behaves as expected for a valid final body frame. | ||
""" | ||
stream = io.BytesIO(VALUES["serialized_final_frame_bad_length"]) | ||
with six.assertRaisesRegex(self, SerializationError, "Invalid final frame length: *"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_frame( | ||
stream=stream, header=VALUES["deserialized_header_frame"] | ||
) | ||
excinfo.match("Invalid final frame length: *") | ||
|
||
def test_deserialize_footer_no_verifier(self): | ||
"""Vaidate that the deserialize_footer function behaves | ||
|
@@ -275,8 +284,9 @@ def test_deserialize_footer_verifier_no_footer(self): | |
with no footer. | ||
""" | ||
stream = io.BytesIO(b"") | ||
with six.assertRaisesRegex(self, SerializationError, "No signature found in message"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_footer(stream, self.mock_verifier) | ||
excinfo.match("No signature found in message") | ||
|
||
@patch("aws_encryption_sdk.internal.formatting.deserialize.struct") | ||
def test_unpack_values(self, mock_struct): | ||
|
@@ -328,59 +338,65 @@ def test_deserialize_wrapped_key_symmetric(self): | |
) | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_key_mismatch(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Master Key mismatch for wrapped data key"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=b"asifuhasjaskldjfhlsakdfj", | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"]["wrapped_encrypted_data_key_asymmetric"], | ||
) | ||
excinfo.match("Master Key mismatch for wrapped data key") | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_info(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: key info missing data"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][ | ||
"wrapped_encrypted_data_key_symmetric_incomplete_info" | ||
], | ||
) | ||
excinfo.match("Malformed key info: key info missing data") | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_iv_len_mismatch(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Wrapping AlgorithmSuite mismatch for wrapped data key"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][ | ||
"wrapped_encrypted_data_key_symmetric_bad_iv_len" | ||
], | ||
) | ||
excinfo.match("Wrapping AlgorithmSuite mismatch for wrapped data key") | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_iv(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete iv"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][ | ||
"wrapped_encrypted_data_key_symmetric_incomplete_iv" | ||
], | ||
) | ||
excinfo.match("Malformed key info: incomplete iv") | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][ | ||
"wrapped_encrypted_data_key_symmetric_incomplete_tag" | ||
], | ||
) | ||
excinfo.match("Malformed key info: incomplete ciphertext or tag") | ||
|
||
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag2(self): | ||
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"): | ||
with pytest.raises(SerializationError) as excinfo: | ||
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( | ||
wrapping_algorithm=self.mock_wrapping_algorithm, | ||
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], | ||
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][ | ||
"wrapped_encrypted_data_key_symmetric_incomplete_tag2" | ||
], | ||
) | ||
excinfo.match("Malformed key info: incomplete ciphertext or tag") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to remove now-unused import.