From 3e3e70158ba3a755ea66e29a696fd74546db5e25 Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 18:33:24 -0600 Subject: [PATCH 01/10] Migrating unit/test_deserialize.py from unittest from pytest --- test/unit/test_deserialize.py | 62 ++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/test/unit/test_deserialize.py b/test/unit/test_deserialize.py index b591159e0..386652c28 100644 --- a/test/unit/test_deserialize.py +++ b/test/unit/test_deserialize.py @@ -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): 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,9 +144,10 @@ 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): @@ -151,55 +155,59 @@ def test_deserialize_header_unknown_data_encryption_algorithm(self, mock_algorit 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,15 +338,16 @@ 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"], @@ -344,9 +355,10 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_info(se "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"], @@ -354,9 +366,10 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_iv_len_mismatch(se "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"], @@ -364,9 +377,10 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_iv(self "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"], @@ -374,9 +388,10 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag(sel "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"], @@ -384,3 +399,4 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag2(se "wrapped_encrypted_data_key_symmetric_incomplete_tag2" ], ) + excinfo.match("Malformed key info: incomplete ciphertext or tag") From a070cb6536565943db3960da4dbf2782f0f2144c Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 18:49:08 -0600 Subject: [PATCH 02/10] Migrate unit/test_provides_kms_master_key.py from unittest to pytest --- test/unit/test_providers_kms_master_key.py | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/test/unit/test_providers_kms_master_key.py b/test/unit/test_providers_kms_master_key.py index ec0cef167..406fd134e 100644 --- a/test/unit/test_providers_kms_master_key.py +++ b/test/unit/test_providers_kms_master_key.py @@ -11,11 +11,9 @@ # 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.key_providers.kms.KMSMasterKey""" -import unittest - import botocore.client import pytest -import six + from botocore.exceptions import ClientError from mock import MagicMock, patch, sentinel @@ -30,8 +28,9 @@ pytestmark = [pytest.mark.unit, pytest.mark.local] -class TestKMSMasterKey(unittest.TestCase): - def setUp(self): +class TestKMSMasterKey(object): + @pytest.fixture(autouse=True) + def apply_fixture(self): self.mock_client = MagicMock() self.mock_client.__class__ = botocore.client.BaseClient self.mock_client.generate_data_key.return_value = { @@ -111,14 +110,16 @@ def test_generate_data_key_with_grant_tokens(self): def test_generate_data_key_unsuccessful_clienterror(self): self.mock_client.generate_data_key.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, GenerateKeyError, "Master Key .* unable to generate data key"): + with pytest.raises(GenerateKeyError) as excinfo: test._generate_data_key(self.mock_algorithm) + excinfo.match("Master Key .* unable to generate data key") def test_generate_data_key_unsuccessful_keyerror(self): self.mock_client.generate_data_key.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, GenerateKeyError, "Master Key .* unable to generate data key"): + with pytest.raises(GenerateKeyError) as excinfo: test._generate_data_key(self.mock_algorithm) + excinfo.match("Master Key .* unable to generate data key") def test_encrypt_data_key(self): test = KMSMasterKey(config=self.mock_kms_mkc_3) @@ -146,14 +147,16 @@ def test_encrypt_data_key_with_grant_tokens(self): def test_encrypt_data_key_unsuccessful_clienterror(self): self.mock_client.encrypt.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, EncryptKeyError, "Master Key .* unable to encrypt data key"): + with pytest.raises(EncryptKeyError) as excinfo: test._encrypt_data_key(self.mock_data_key, self.mock_algorithm) + excinfo.match("Master Key .* unable to encrypt data key") def test_encrypt_data_key_unsuccessful_keyerror(self): self.mock_client.encrypt.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, EncryptKeyError, "Master Key .* unable to encrypt data key"): + with pytest.raises(EncryptKeyError) as excinfo: test._encrypt_data_key(self.mock_data_key, self.mock_algorithm) + excinfo.match("Master Key .* unable to encrypt data key") def test_decrypt_data_key(self): test = KMSMasterKey(config=self.mock_kms_mkc_1) @@ -186,11 +189,13 @@ def test_decrypt_data_key_with_grant_tokens(self): def test_decrypt_data_key_unsuccessful_clienterror(self): self.mock_client.decrypt.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, DecryptKeyError, "Master Key .* unable to decrypt data key"): + with pytest.raises(DecryptKeyError) as excinfo: test._decrypt_data_key(encrypted_data_key=self.mock_encrypted_data_key, algorithm=sentinel.algorithm) + excinfo.match("Master Key .* unable to decrypt data key") def test_decrypt_data_key_unsuccessful_keyerror(self): self.mock_client.decrypt.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with six.assertRaisesRegex(self, DecryptKeyError, "Master Key .* unable to decrypt data key"): + with pytest.raises(DecryptKeyError) as excinfo: test._decrypt_data_key(encrypted_data_key=self.mock_encrypted_data_key, algorithm=sentinel.algorithm) + excinfo.match("Master Key .* unable to decrypt data key") From cba699599500eca0a260aa51356c34c9c057dbb1 Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:31:27 -0600 Subject: [PATCH 03/10] Migrated unit/test/_providers_kms_master_key_provider.py from unittest to pytest --- test/unit/test_providers_kms_master_key_provider.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/unit/test_providers_kms_master_key_provider.py b/test/unit/test_providers_kms_master_key_provider.py index aacc062de..fc66bde97 100644 --- a/test/unit/test_providers_kms_master_key_provider.py +++ b/test/unit/test_providers_kms_master_key_provider.py @@ -11,8 +11,6 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Unit test suite from aws_encryption_sdk.key_providers.kms.KMSMasterKeyProvider""" -import unittest - import botocore.client import pytest import six @@ -32,8 +30,9 @@ def test_init_with_regionless_key_ids_and_region_names(): assert provider.master_key("alias/key_1").config.client.meta.region_name == region_names[0] -class TestKMSMasterKeyProvider(unittest.TestCase): - def setUp(self): +class TestKMSMasterKeyProvider(object): + @pytest.fixture(autouse=True) + def apply_fixtures(self): self.mock_botocore_session_patcher = patch("aws_encryption_sdk.key_providers.kms.botocore.session.Session") self.mock_botocore_session = self.mock_botocore_session_patcher.start() self.mock_boto3_session_patcher = patch("aws_encryption_sdk.key_providers.kms.boto3.session.Session") @@ -130,10 +129,9 @@ def test_client_no_region_name_with_default(self, mock_add_client): def test_client_no_region_name_without_default(self): test = KMSMasterKeyProvider() - with six.assertRaisesRegex( - self, UnknownRegionError, "No default region found and no region determinable from key id: *" - ): + with pytest.raises(UnknownRegionError) as excinfo: test._client("") + excinfo.match("No default region found and no region determinable from key id: *") @patch("aws_encryption_sdk.key_providers.kms.KMSMasterKeyProvider._client") def test_new_master_key(self, mock_client): From fdb3b5adc30647c83fb96685c95aabc122bc910c Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:35:17 -0600 Subject: [PATCH 04/10] unit/test_providers_raw_master_key.py --- test/unit/test_providers_raw_master_key.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/test_providers_raw_master_key.py b/test/unit/test_providers_raw_master_key.py index d117ee16f..9abcd14c6 100644 --- a/test/unit/test_providers_raw_master_key.py +++ b/test/unit/test_providers_raw_master_key.py @@ -11,8 +11,6 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Test suite for aws_encryption_sdk.key_providers.raw.RawMasterKey""" -import unittest - import pytest from mock import MagicMock, patch, sentinel @@ -27,8 +25,9 @@ pytestmark = [pytest.mark.unit, pytest.mark.local] -class TestRawMasterKey(unittest.TestCase): - def setUp(self): +class TestRawMasterKey(object): + @pytest.fixture(autouse=True) + def apply_fixtures(self): self.mock_algorithm = MagicMock() self.mock_algorithm.__class__ = Algorithm self.mock_algorithm.data_key_len = sentinel.data_key_len From 1d2b654f2463766bc0c14b69f2132eeffa27dd5b Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:37:47 -0600 Subject: [PATCH 05/10] Migrate unit/test_providers_raw_master_key_provider.py from unittest from pytest --- test/unit/test_providers_raw_master_key_provider.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/test_providers_raw_master_key_provider.py b/test/unit/test_providers_raw_master_key_provider.py index 568955bcd..ad3684d6a 100644 --- a/test/unit/test_providers_raw_master_key_provider.py +++ b/test/unit/test_providers_raw_master_key_provider.py @@ -11,8 +11,6 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Test suite for aws_encryption_sdk.key_providers.raw.RawMasterKeyProvider""" -import unittest - import attr import pytest import six @@ -42,7 +40,7 @@ def _get_raw_key(self, key_id): return self.config.mock_wrapping_key -class TestRawMasterKeyProvider(unittest.TestCase): +class TestRawMasterKeyProvider(object): def test_parent(self): assert issubclass(RawMasterKeyProvider, MasterKeyProvider) @@ -50,8 +48,9 @@ def test_get_raw_key_enforcement(self): class TestProvider(RawMasterKeyProvider): pass - with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestProvider *"): + with pytest.raises(TypeError) as excinfo: TestProvider() + excinfo.match("Can't instantiate abstract class TestProvider *") @patch( "aws_encryption_sdk.key_providers.raw.RawMasterKeyConfig", return_value=sentinel.raw_master_key_config_instance From aa3df87af56a862772ca29ff6c68f2ed8e27cefc Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:42:06 -0600 Subject: [PATCH 06/10] Migrate unit/test_serialize.py from unittest to pytest --- test/unit/test_serialize.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/test_serialize.py b/test/unit/test_serialize.py index 98068c2ca..aa952ccec 100644 --- a/test/unit/test_serialize.py +++ b/test/unit/test_serialize.py @@ -11,8 +11,6 @@ # 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.serialize""" -import unittest - import pytest from mock import MagicMock, patch, sentinel @@ -51,8 +49,9 @@ def test_serialize_frame_invalid_sequence_number(sequence_number, error_message) excinfo.match(error_message) -class TestSerialize(unittest.TestCase): - def setUp(self): +class TestSerialize(object): + @pytest.fixture(autouse=True) + def apply_fixtures(self): self.mock_algorithm = MagicMock() self.mock_algorithm.encryption_algorithm.block_size = VALUES["block_size"] self.mock_algorithm.algorithm_id = VALUES["algorithm_id"] From 437c391a3f1382b7c4aec6a6b8ee3c7ae0289e47 Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:50:12 -0600 Subject: [PATCH 07/10] Migrate unit/test_streaming_client_stream_decryptor.py from unittest to pytest --- .../test_streaming_client_stream_decryptor.py | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/test/unit/test_streaming_client_stream_decryptor.py b/test/unit/test_streaming_client_stream_decryptor.py index c59ae4beb..a122e5ad1 100644 --- a/test/unit/test_streaming_client_stream_decryptor.py +++ b/test/unit/test_streaming_client_stream_decryptor.py @@ -12,7 +12,6 @@ # language governing permissions and limitations under the License. """Unit test suite for aws_encryption_sdk.streaming_client.StreamDecryptor""" import io -import unittest import pytest import six @@ -29,8 +28,9 @@ pytestmark = [pytest.mark.unit, pytest.mark.local] -class TestStreamDecryptor(unittest.TestCase): - def setUp(self): +class TestStreamDecryptor(object): + @pytest.fixture(autouse=True) + def apply_fixtures(self): self.mock_key_provider = MagicMock(__class__=MasterKeyProvider) self.mock_materials_manager = MagicMock(__class__=CryptoMaterialsManager) self.mock_materials_manager.decrypt_materials.return_value = MagicMock( @@ -186,12 +186,9 @@ def test_read_header_frame_too_large(self, mock_derive_datakey): test_decryptor.key_provider = self.mock_key_provider test_decryptor.source_stream = ct_stream test_decryptor._stream_length = len(VALUES["data_128"]) - with six.assertRaisesRegex( - self, - CustomMaximumValueExceeded, - "Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10), - ): + with pytest.raises(CustomMaximumValueExceeded) as excinfo: test_decryptor._read_header() + excinfo.match("Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10)) @patch("aws_encryption_sdk.streaming_client.Verifier") @patch("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest") @@ -220,14 +217,10 @@ def test_prep_non_framed_content_length_too_large(self): mock_data_key = MagicMock() test_decryptor.data_key = mock_data_key - with six.assertRaisesRegex( - self, - CustomMaximumValueExceeded, - "Non-framed message content length found larger than custom value: {found} > {custom}".format( - found=len(VALUES["data_128"]), custom=len(VALUES["data_128"]) // 2 - ), - ): + with pytest.raises(CustomMaximumValueExceeded) as excinfo: test_decryptor._prep_non_framed() + excinfo.match("Non-framed message content length found larger than custom value: {found} > {custom}".format( + found=len(VALUES["data_128"]), custom=len(VALUES["data_128"]) // 2)) def test_prep_non_framed(self): test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=self.mock_input_stream) @@ -288,10 +281,9 @@ def test_read_bytes_from_non_framed_message_body_too_small(self): test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=ct_stream) test_decryptor.body_length = len(VALUES["data_128"] * 2) test_decryptor._header = self.mock_header - with six.assertRaisesRegex( - self, SerializationError, "Total message body contents less than specified in body description" - ): + with pytest.raises(SerializationError) as excinfo: test_decryptor._read_bytes_from_non_framed_body(1) + excinfo.match("Total message body contents less than specified in body description") def test_read_bytes_from_non_framed_no_verifier(self): ct_stream = io.BytesIO(VALUES["data_128"]) @@ -497,8 +489,9 @@ def test_read_bytes_from_framed_body_bad_sequence_number(self): frame_data.final_frame = False frame_data.ciphertext = b"asdfzxcv" self.mock_deserialize_frame.return_value = (frame_data, False) - with six.assertRaisesRegex(self, SerializationError, "Malformed message: frames out of order"): + with pytest.raises(SerializationError) as excinfo: test_decryptor._read_bytes_from_framed_body(4) + excinfo.match("Malformed message: frames out of order") @patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_non_framed_body") @patch("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_framed_body") @@ -549,8 +542,9 @@ def test_read_bytes_unknown(self, mock_read_frame, mock_read_block): test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=ct_stream) test_decryptor._header = MagicMock() test_decryptor._header.content_type = None - with six.assertRaisesRegex(self, NotSupportedError, "Unsupported content type"): + with pytest.raises(NotSupportedError) as excinfo: test_decryptor._read_bytes(5) + excinfo.match("Unsupported content type") @patch("aws_encryption_sdk.streaming_client._EncryptionStream.close") def test_close(self, mock_close): @@ -565,5 +559,6 @@ def test_close(self, mock_close): def test_close_no_footer(self, mock_close): self.mock_header.content_type = ContentType.FRAMED_DATA test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=self.mock_input_stream) - with six.assertRaisesRegex(self, SerializationError, "Footer not read"): + with pytest.raises(SerializationError) as excinfo: test_decryptor.close() + excinfo.match("Footer not read") From 5c0acfb43c14553bcd4c78a7c89cf4117a2f36a4 Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Wed, 12 Dec 2018 19:51:49 -0600 Subject: [PATCH 08/10] Autoformat --- test/unit/test_streaming_client_stream_decryptor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/unit/test_streaming_client_stream_decryptor.py b/test/unit/test_streaming_client_stream_decryptor.py index a122e5ad1..9cf73c821 100644 --- a/test/unit/test_streaming_client_stream_decryptor.py +++ b/test/unit/test_streaming_client_stream_decryptor.py @@ -188,7 +188,9 @@ def test_read_header_frame_too_large(self, mock_derive_datakey): test_decryptor._stream_length = len(VALUES["data_128"]) with pytest.raises(CustomMaximumValueExceeded) as excinfo: test_decryptor._read_header() - excinfo.match("Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10)) + excinfo.match( + "Frame Size in header found larger than custom value: {found} > {custom}".format(found=1024, custom=10) + ) @patch("aws_encryption_sdk.streaming_client.Verifier") @patch("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest") @@ -219,8 +221,11 @@ def test_prep_non_framed_content_length_too_large(self): with pytest.raises(CustomMaximumValueExceeded) as excinfo: test_decryptor._prep_non_framed() - excinfo.match("Non-framed message content length found larger than custom value: {found} > {custom}".format( - found=len(VALUES["data_128"]), custom=len(VALUES["data_128"]) // 2)) + excinfo.match( + "Non-framed message content length found larger than custom value: {found} > {custom}".format( + found=len(VALUES["data_128"]), custom=len(VALUES["data_128"]) // 2 + ) + ) def test_prep_non_framed(self): test_decryptor = StreamDecryptor(key_provider=self.mock_key_provider, source=self.mock_input_stream) From 8dc8804bb0b9dab3a295f74d7c7812fce46aeab9 Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Thu, 13 Dec 2018 12:27:45 -0600 Subject: [PATCH 09/10] Removed unit tests not corresponding to this branch --- test/unit/test_deserialize.py | 62 +++++++------------ test/unit/test_providers_kms_master_key.py | 27 ++++---- .../test_providers_kms_master_key_provider.py | 12 ++-- test/unit/test_providers_raw_master_key.py | 7 ++- .../test_providers_raw_master_key_provider.py | 7 ++- test/unit/test_serialize.py | 7 ++- 6 files changed, 53 insertions(+), 69 deletions(-) diff --git a/test/unit/test_deserialize.py b/test/unit/test_deserialize.py index 386652c28..b591159e0 100644 --- a/test/unit/test_deserialize.py +++ b/test/unit/test_deserialize.py @@ -13,6 +13,7 @@ """Unit test suite for aws_encryption_sdk.deserialize""" import io import struct +import unittest import pytest import six @@ -55,9 +56,8 @@ def test_deserialize_tag(): assert parsed_tag == tag -class TestDeserialize(object): - @pytest.fixture(autouse=True) - def apply_fixtures(self): +class TestDeserialize(unittest.TestCase): + def setUp(self): self.mock_wrapping_algorithm = MagicMock() self.mock_wrapping_algorithm.algorithm.iv_len = VALUES["iv_len"] @@ -109,32 +109,29 @@ def test_validate_header_invalid(self): as expected for a valid header. """ self.mock_decrypt.side_effect = InvalidTag() - with pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Header authorization failed"): 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 pytest.raises(NotSupportedError) as excinfo: + with six.assertRaisesRegex(self, NotSupportedError, "Unsupported type *"): 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 pytest.raises(NotSupportedError) as excinfo: + with six.assertRaisesRegex(self, NotSupportedError, "Unsupported version *"): 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): @@ -144,10 +141,9 @@ 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 pytest.raises(NotSupportedError) as excinfo: + with six.assertRaisesRegex(self, NotSupportedError, "Unsupported algorithm *"): 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): @@ -155,59 +151,55 @@ def test_deserialize_header_unknown_data_encryption_algorithm(self, mock_algorit as expected for an unknown algorithm. """ mock_algorithm_get.side_effect = KeyError() - with pytest.raises(UnknownIdentityError) as excinfo: + with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown algorithm *"): 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 pytest.raises(UnknownIdentityError) as excinfo: + with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown content type *"): 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex( + self, SerializationError, "Content AAD length field is currently unused, its value must be always 0" + ): 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Specified IV length *"): 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Specified frame length larger than allowed maximum: *"): 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Non-zero frame length found for non-framed message"): 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 @@ -255,11 +247,10 @@ 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Invalid final frame length: *"): 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 @@ -284,9 +275,8 @@ def test_deserialize_footer_verifier_no_footer(self): with no footer. """ stream = io.BytesIO(b"") - with pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "No signature found in message"): 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): @@ -338,16 +328,15 @@ def test_deserialize_wrapped_key_symmetric(self): ) def test_deserialize_wrapped_key_symmetric_wrapping_key_mismatch(self): - with pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Master Key mismatch for wrapped data key"): 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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Malformed key info: key info missing data"): aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( wrapping_algorithm=self.mock_wrapping_algorithm, wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], @@ -355,10 +344,9 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_info(se "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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Wrapping AlgorithmSuite mismatch for wrapped data key"): aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( wrapping_algorithm=self.mock_wrapping_algorithm, wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], @@ -366,10 +354,9 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_iv_len_mismatch(se "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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete iv"): aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( wrapping_algorithm=self.mock_wrapping_algorithm, wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], @@ -377,10 +364,9 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_iv(self "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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"): aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( wrapping_algorithm=self.mock_wrapping_algorithm, wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], @@ -388,10 +374,9 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag(sel "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 pytest.raises(SerializationError) as excinfo: + with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"): aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key( wrapping_algorithm=self.mock_wrapping_algorithm, wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"], @@ -399,4 +384,3 @@ def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag2(se "wrapped_encrypted_data_key_symmetric_incomplete_tag2" ], ) - excinfo.match("Malformed key info: incomplete ciphertext or tag") diff --git a/test/unit/test_providers_kms_master_key.py b/test/unit/test_providers_kms_master_key.py index 406fd134e..ec0cef167 100644 --- a/test/unit/test_providers_kms_master_key.py +++ b/test/unit/test_providers_kms_master_key.py @@ -11,9 +11,11 @@ # 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.key_providers.kms.KMSMasterKey""" +import unittest + import botocore.client import pytest - +import six from botocore.exceptions import ClientError from mock import MagicMock, patch, sentinel @@ -28,9 +30,8 @@ pytestmark = [pytest.mark.unit, pytest.mark.local] -class TestKMSMasterKey(object): - @pytest.fixture(autouse=True) - def apply_fixture(self): +class TestKMSMasterKey(unittest.TestCase): + def setUp(self): self.mock_client = MagicMock() self.mock_client.__class__ = botocore.client.BaseClient self.mock_client.generate_data_key.return_value = { @@ -110,16 +111,14 @@ def test_generate_data_key_with_grant_tokens(self): def test_generate_data_key_unsuccessful_clienterror(self): self.mock_client.generate_data_key.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(GenerateKeyError) as excinfo: + with six.assertRaisesRegex(self, GenerateKeyError, "Master Key .* unable to generate data key"): test._generate_data_key(self.mock_algorithm) - excinfo.match("Master Key .* unable to generate data key") def test_generate_data_key_unsuccessful_keyerror(self): self.mock_client.generate_data_key.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(GenerateKeyError) as excinfo: + with six.assertRaisesRegex(self, GenerateKeyError, "Master Key .* unable to generate data key"): test._generate_data_key(self.mock_algorithm) - excinfo.match("Master Key .* unable to generate data key") def test_encrypt_data_key(self): test = KMSMasterKey(config=self.mock_kms_mkc_3) @@ -147,16 +146,14 @@ def test_encrypt_data_key_with_grant_tokens(self): def test_encrypt_data_key_unsuccessful_clienterror(self): self.mock_client.encrypt.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(EncryptKeyError) as excinfo: + with six.assertRaisesRegex(self, EncryptKeyError, "Master Key .* unable to encrypt data key"): test._encrypt_data_key(self.mock_data_key, self.mock_algorithm) - excinfo.match("Master Key .* unable to encrypt data key") def test_encrypt_data_key_unsuccessful_keyerror(self): self.mock_client.encrypt.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(EncryptKeyError) as excinfo: + with six.assertRaisesRegex(self, EncryptKeyError, "Master Key .* unable to encrypt data key"): test._encrypt_data_key(self.mock_data_key, self.mock_algorithm) - excinfo.match("Master Key .* unable to encrypt data key") def test_decrypt_data_key(self): test = KMSMasterKey(config=self.mock_kms_mkc_1) @@ -189,13 +186,11 @@ def test_decrypt_data_key_with_grant_tokens(self): def test_decrypt_data_key_unsuccessful_clienterror(self): self.mock_client.decrypt.side_effect = ClientError({"Error": {}}, "This is an error!") test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(DecryptKeyError) as excinfo: + with six.assertRaisesRegex(self, DecryptKeyError, "Master Key .* unable to decrypt data key"): test._decrypt_data_key(encrypted_data_key=self.mock_encrypted_data_key, algorithm=sentinel.algorithm) - excinfo.match("Master Key .* unable to decrypt data key") def test_decrypt_data_key_unsuccessful_keyerror(self): self.mock_client.decrypt.side_effect = KeyError test = KMSMasterKey(config=self.mock_kms_mkc_3) - with pytest.raises(DecryptKeyError) as excinfo: + with six.assertRaisesRegex(self, DecryptKeyError, "Master Key .* unable to decrypt data key"): test._decrypt_data_key(encrypted_data_key=self.mock_encrypted_data_key, algorithm=sentinel.algorithm) - excinfo.match("Master Key .* unable to decrypt data key") diff --git a/test/unit/test_providers_kms_master_key_provider.py b/test/unit/test_providers_kms_master_key_provider.py index fc66bde97..aacc062de 100644 --- a/test/unit/test_providers_kms_master_key_provider.py +++ b/test/unit/test_providers_kms_master_key_provider.py @@ -11,6 +11,8 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Unit test suite from aws_encryption_sdk.key_providers.kms.KMSMasterKeyProvider""" +import unittest + import botocore.client import pytest import six @@ -30,9 +32,8 @@ def test_init_with_regionless_key_ids_and_region_names(): assert provider.master_key("alias/key_1").config.client.meta.region_name == region_names[0] -class TestKMSMasterKeyProvider(object): - @pytest.fixture(autouse=True) - def apply_fixtures(self): +class TestKMSMasterKeyProvider(unittest.TestCase): + def setUp(self): self.mock_botocore_session_patcher = patch("aws_encryption_sdk.key_providers.kms.botocore.session.Session") self.mock_botocore_session = self.mock_botocore_session_patcher.start() self.mock_boto3_session_patcher = patch("aws_encryption_sdk.key_providers.kms.boto3.session.Session") @@ -129,9 +130,10 @@ def test_client_no_region_name_with_default(self, mock_add_client): def test_client_no_region_name_without_default(self): test = KMSMasterKeyProvider() - with pytest.raises(UnknownRegionError) as excinfo: + with six.assertRaisesRegex( + self, UnknownRegionError, "No default region found and no region determinable from key id: *" + ): test._client("") - excinfo.match("No default region found and no region determinable from key id: *") @patch("aws_encryption_sdk.key_providers.kms.KMSMasterKeyProvider._client") def test_new_master_key(self, mock_client): diff --git a/test/unit/test_providers_raw_master_key.py b/test/unit/test_providers_raw_master_key.py index 9abcd14c6..d117ee16f 100644 --- a/test/unit/test_providers_raw_master_key.py +++ b/test/unit/test_providers_raw_master_key.py @@ -11,6 +11,8 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Test suite for aws_encryption_sdk.key_providers.raw.RawMasterKey""" +import unittest + import pytest from mock import MagicMock, patch, sentinel @@ -25,9 +27,8 @@ pytestmark = [pytest.mark.unit, pytest.mark.local] -class TestRawMasterKey(object): - @pytest.fixture(autouse=True) - def apply_fixtures(self): +class TestRawMasterKey(unittest.TestCase): + def setUp(self): self.mock_algorithm = MagicMock() self.mock_algorithm.__class__ = Algorithm self.mock_algorithm.data_key_len = sentinel.data_key_len diff --git a/test/unit/test_providers_raw_master_key_provider.py b/test/unit/test_providers_raw_master_key_provider.py index ad3684d6a..568955bcd 100644 --- a/test/unit/test_providers_raw_master_key_provider.py +++ b/test/unit/test_providers_raw_master_key_provider.py @@ -11,6 +11,8 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. """Test suite for aws_encryption_sdk.key_providers.raw.RawMasterKeyProvider""" +import unittest + import attr import pytest import six @@ -40,7 +42,7 @@ def _get_raw_key(self, key_id): return self.config.mock_wrapping_key -class TestRawMasterKeyProvider(object): +class TestRawMasterKeyProvider(unittest.TestCase): def test_parent(self): assert issubclass(RawMasterKeyProvider, MasterKeyProvider) @@ -48,9 +50,8 @@ def test_get_raw_key_enforcement(self): class TestProvider(RawMasterKeyProvider): pass - with pytest.raises(TypeError) as excinfo: + with six.assertRaisesRegex(self, TypeError, "Can't instantiate abstract class TestProvider *"): TestProvider() - excinfo.match("Can't instantiate abstract class TestProvider *") @patch( "aws_encryption_sdk.key_providers.raw.RawMasterKeyConfig", return_value=sentinel.raw_master_key_config_instance diff --git a/test/unit/test_serialize.py b/test/unit/test_serialize.py index aa952ccec..98068c2ca 100644 --- a/test/unit/test_serialize.py +++ b/test/unit/test_serialize.py @@ -11,6 +11,8 @@ # 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.serialize""" +import unittest + import pytest from mock import MagicMock, patch, sentinel @@ -49,9 +51,8 @@ def test_serialize_frame_invalid_sequence_number(sequence_number, error_message) excinfo.match(error_message) -class TestSerialize(object): - @pytest.fixture(autouse=True) - def apply_fixtures(self): +class TestSerialize(unittest.TestCase): + def setUp(self): self.mock_algorithm = MagicMock() self.mock_algorithm.encryption_algorithm.block_size = VALUES["block_size"] self.mock_algorithm.algorithm_id = VALUES["algorithm_id"] From 2e8110be027a5c0efc7bca3bcf2a6d218a479c0b Mon Sep 17 00:00:00 2001 From: Andres Sanchez Date: Mon, 17 Dec 2018 16:57:58 -0600 Subject: [PATCH 10/10] Removed unused import and added yield and tearDown to apply_fixture function --- test/unit/test_streaming_client_stream_decryptor.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit/test_streaming_client_stream_decryptor.py b/test/unit/test_streaming_client_stream_decryptor.py index 9cf73c821..69cd72c73 100644 --- a/test/unit/test_streaming_client_stream_decryptor.py +++ b/test/unit/test_streaming_client_stream_decryptor.py @@ -14,7 +14,6 @@ import io import pytest -import six from mock import MagicMock, call, patch, sentinel from aws_encryption_sdk.exceptions import CustomMaximumValueExceeded, NotSupportedError, SerializationError @@ -92,8 +91,8 @@ def apply_fixtures(self): # Set up decrypt patch self.mock_decrypt_patcher = patch("aws_encryption_sdk.streaming_client.decrypt") self.mock_decrypt = self.mock_decrypt_patcher.start() - - def tearDown(self): + yield + # Run tearDown self.mock_deserialize_header_patcher.stop() self.mock_deserialize_header_auth_patcher.stop() self.mock_validate_header_patcher.stop()