|
13 | 13 | # language governing permissions and limitations under the License.
|
14 | 14 | """Test suite for aws_encryption_sdk.internal.utils"""
|
15 | 15 | import io
|
16 |
| -import unittest |
17 | 16 |
|
18 | 17 | import pytest
|
19 |
| -import six |
20 | 18 | from mock import MagicMock, patch, sentinel
|
21 | 19 |
|
22 | 20 | import aws_encryption_sdk.identifiers
|
@@ -44,8 +42,9 @@ def test_prep_stream_data_wrap(source):
|
44 | 42 | assert_prepped_stream_identity(test, io.BytesIO)
|
45 | 43 |
|
46 | 44 |
|
47 |
| -class TestUtils(unittest.TestCase): |
48 |
| - def setUp(self): |
| 45 | +class TestUtils(object): |
| 46 | + @pytest.fixture(autouse=True) |
| 47 | + def apply_fixtures(self): |
49 | 48 | # Set up mock key provider and keys
|
50 | 49 | self.mock_key_provider_1 = MasterKeyInfo(provider_id="adijoasijfoi", key_info=b"asoiwef8q34")
|
51 | 50 | self.mock_raw_data_key_1_bytes = b"asioufhaw9eruhtg"
|
@@ -139,49 +138,44 @@ def setUp(self):
|
139 | 138 | )
|
140 | 139 | self.mock_aws_encryption_sdk_instance.decrypt.return_value = VALUES["data_key"]
|
141 | 140 | self.mock_aws_encryption_sdk_instance.encrypt.return_value = VALUES["encrypted_data_key"]
|
142 |
| - |
143 |
| - def tearDown(self): |
| 141 | + yield |
| 142 | + # Run tearDown |
144 | 143 | self.mock_urandom_patcher.stop()
|
145 | 144 |
|
146 | 145 | def test_validate_frame_length_negative_frame_length(self):
|
147 | 146 | """Validate that the validate_frame_length function
|
148 | 147 | behaves as expected when supplied with a
|
149 | 148 | negative frame length.
|
150 | 149 | """
|
151 |
| - with six.assertRaisesRegex( |
152 |
| - self, |
153 |
| - SerializationError, |
154 |
| - "Frame size must be a non-negative multiple of the block size of the crypto algorithm: *", |
155 |
| - ): |
| 150 | + with pytest.raises(SerializationError) as excinfo: |
156 | 151 | aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=-1, algorithm=self.mock_algorithm)
|
| 152 | + excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *") |
157 | 153 |
|
158 | 154 | def test_validate_frame_length_invalid_frame_length(self):
|
159 | 155 | """Validate that the validate_frame_length function
|
160 | 156 | behaves as expected when supplied with an
|
161 | 157 | invalid frame length.
|
162 | 158 | """
|
163 |
| - with six.assertRaisesRegex( |
164 |
| - self, |
165 |
| - SerializationError, |
166 |
| - "Frame size must be a non-negative multiple of the block size of the crypto algorithm: *", |
167 |
| - ): |
| 159 | + with pytest.raises(SerializationError) as excinfo: |
168 | 160 | aws_encryption_sdk.internal.utils.validate_frame_length(frame_length=1, algorithm=self.mock_algorithm)
|
| 161 | + excinfo.match("Frame size must be a non-negative multiple of the block size of the crypto algorithm: *") |
169 | 162 |
|
170 | 163 | def test_validate_frame_length_too_large(self):
|
171 | 164 | """Validate that the validate_frame_length function
|
172 | 165 | behaves as expected when supplied with a
|
173 | 166 | frame length which is too large.
|
174 | 167 | """
|
175 |
| - with six.assertRaisesRegex(self, SerializationError, "Frame size too large: *"): |
| 168 | + with pytest.raises(SerializationError) as excinfo: |
176 | 169 | aws_encryption_sdk.internal.utils.validate_frame_length(
|
177 | 170 | frame_length=MAX_FRAME_SIZE + 1, algorithm=self.mock_algorithm
|
178 | 171 | )
|
| 172 | + excinfo.match("Frame size too large: *") |
179 | 173 |
|
180 | 174 | def test_message_id(self):
|
181 | 175 | """Validate that the message_id function behaves as expected."""
|
182 | 176 | test = aws_encryption_sdk.internal.utils.message_id()
|
183 | 177 | self.mock_urandom.assert_called_once_with(MESSAGE_ID_LENGTH)
|
184 |
| - self.assertEqual(test, sentinel.random) |
| 178 | + assert test == sentinel.random |
185 | 179 |
|
186 | 180 | def test_get_aad_content_string_no_framing(self):
|
187 | 181 | """Validate that the get_aad_content_string function behaves
|
@@ -214,8 +208,9 @@ def test_get_aad_content_string_framing_bad_type(self):
|
214 | 208 | """Validate that the get_aad_content_string function behaves as
|
215 | 209 | expected when called with an unknown content type.
|
216 | 210 | """
|
217 |
| - with six.assertRaisesRegex(self, UnknownIdentityError, "Unhandled content type"): |
| 211 | + with pytest.raises(UnknownIdentityError) as excinfo: |
218 | 212 | aws_encryption_sdk.internal.utils.get_aad_content_string(-1, False)
|
| 213 | + excinfo.match("Unhandled content type") |
219 | 214 |
|
220 | 215 | def test_prepare_data_keys(self):
|
221 | 216 | mock_encryption_dk = DataKey(
|
@@ -265,9 +260,8 @@ def test_source_data_key_length_check_invalid(self):
|
265 | 260 | mock_algorithm.kdf_input_len = 5
|
266 | 261 | mock_data_key = MagicMock()
|
267 | 262 | mock_data_key.data_key = "1234"
|
268 |
| - with six.assertRaisesRegex( |
269 |
| - self, InvalidDataKeyError, "Invalid Source Data Key length 4 for algorithm required: 5" |
270 |
| - ): |
| 263 | + with pytest.raises(InvalidDataKeyError) as excinfo: |
271 | 264 | aws_encryption_sdk.internal.utils.source_data_key_length_check(
|
272 | 265 | source_data_key=mock_data_key, algorithm=mock_algorithm
|
273 | 266 | )
|
| 267 | + excinfo.match("Invalid Source Data Key length 4 for algorithm required: 5") |
0 commit comments