Skip to content

Commit ec60010

Browse files
ansanpermattsb42-aws
authored andcommitted
Migrate unit/test_deserialize.py from unittest from pytest (#117)
* Migrating unit/test_deserialize.py from unittest from pytest * Added yield and teardown function in apply_fixtures and removed unused import * Lintering issues, fixed with tox -re autoformat
1 parent 4362a50 commit ec60010

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

test/unit/test_deserialize.py

+41-26
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
"""Unit test suite for aws_encryption_sdk.deserialize"""
1414
import io
1515
import struct
16-
import unittest
1716

1817
import pytest
19-
import six
2018
from cryptography.exceptions import InvalidTag
2119
from mock import MagicMock, patch, sentinel
2220

@@ -56,8 +54,9 @@ def test_deserialize_tag():
5654
assert parsed_tag == tag
5755

5856

59-
class TestDeserialize(unittest.TestCase):
60-
def setUp(self):
57+
class TestDeserialize(object):
58+
@pytest.fixture(autouse=True)
59+
def apply_fixtures(self):
6160
self.mock_wrapping_algorithm = MagicMock()
6261
self.mock_wrapping_algorithm.algorithm.iv_len = VALUES["iv_len"]
6362

@@ -80,8 +79,8 @@ def setUp(self):
8079
# Set up mock verifier
8180
self.mock_verifier = MagicMock()
8281
self.mock_verifier.update.return_value = None
83-
84-
def tearDown(self):
82+
yield
83+
# Run tearDown
8584
self.mock_decrypt_patcher.stop()
8685
self.mock_deserialize_ec_patcher.stop()
8786

@@ -109,29 +108,32 @@ def test_validate_header_invalid(self):
109108
as expected for a valid header.
110109
"""
111110
self.mock_decrypt.side_effect = InvalidTag()
112-
with six.assertRaisesRegex(self, SerializationError, "Header authorization failed"):
111+
with pytest.raises(SerializationError) as excinfo:
113112
aws_encryption_sdk.internal.formatting.deserialize.validate_header(
114113
header=VALUES["deserialized_header_block"],
115114
header_auth=VALUES["deserialized_header_auth_block"],
116115
raw_header=VALUES["header"],
117116
data_key=VALUES["data_key_obj"],
118117
)
118+
excinfo.match("Header authorization failed")
119119

120120
def test_deserialize_header_unknown_object_type(self):
121121
"""Validate that the deserialize_header function behaves
122122
as expected for an unknown object type.
123123
"""
124-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported type *"):
124+
with pytest.raises(NotSupportedError) as excinfo:
125125
stream = io.BytesIO(VALUES["serialized_header_invalid_object_type"])
126126
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
127+
excinfo.match("Unsupported type *")
127128

128129
def test_deserialize_header_unknown_version(self):
129130
"""Validate that the deserialize_header function behaves
130131
as expected for an unknown message version.
131132
"""
132-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported version *"):
133+
with pytest.raises(NotSupportedError) as excinfo:
133134
stream = io.BytesIO(VALUES["serialized_header_invalid_version"])
134135
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
136+
excinfo.match("Unsupported version *")
135137

136138
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id")
137139
def test_deserialize_header_unsupported_data_encryption_algorithm(self, mock_algorithm_get):
@@ -141,65 +143,70 @@ def test_deserialize_header_unsupported_data_encryption_algorithm(self, mock_alg
141143
mock_unsupported_algorithm = MagicMock()
142144
mock_unsupported_algorithm.allowed = False
143145
mock_algorithm_get.return_value = mock_unsupported_algorithm
144-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported algorithm *"):
146+
with pytest.raises(NotSupportedError) as excinfo:
145147
stream = io.BytesIO(VALUES["serialized_header_disallowed_algorithm"])
146148
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
149+
excinfo.match("Unsupported algorithm *")
147150

148151
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id")
149152
def test_deserialize_header_unknown_data_encryption_algorithm(self, mock_algorithm_get):
150153
"""Validate that the deserialize_header function behaves
151154
as expected for an unknown algorithm.
152155
"""
153156
mock_algorithm_get.side_effect = KeyError()
154-
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown algorithm *"):
157+
with pytest.raises(UnknownIdentityError) as excinfo:
155158
stream = io.BytesIO(VALUES["serialized_header_invalid_algorithm"])
156159
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
160+
excinfo.match("Unknown algorithm *")
157161

158162
def test_deserialize_header_unknown_content_type(self):
159163
"""Validate that the deserialize_header function behaves
160164
as expected for an unknown content type.
161165
"""
162-
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown content type *"):
166+
with pytest.raises(UnknownIdentityError) as excinfo:
163167
stream = io.BytesIO(VALUES["serialized_header_unknown_content_type"])
164168
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
169+
excinfo.match("Unknown content type *")
165170

166171
def test_deserialize_header_invalid_reserved_space(self):
167172
"""Validate that the deserialize_header function behaves
168173
as expected for an invalid value in the reserved
169174
space (formerly content AAD).
170175
"""
171-
with six.assertRaisesRegex(
172-
self, SerializationError, "Content AAD length field is currently unused, its value must be always 0"
173-
):
176+
with pytest.raises(SerializationError) as excinfo:
174177
stream = io.BytesIO(VALUES["serialized_header_bad_reserved_space"])
175178
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
179+
excinfo.match("Content AAD length field is currently unused, its value must be always 0")
176180

177181
def test_deserialize_header_bad_iv_len(self):
178182
"""Validate that the deserialize_header function behaves
179183
as expected for bad IV length (incompatible with
180184
specified algorithm).
181185
"""
182-
with six.assertRaisesRegex(self, SerializationError, "Specified IV length *"):
186+
with pytest.raises(SerializationError) as excinfo:
183187
stream = io.BytesIO(VALUES["serialized_header_bad_iv_len"])
184188
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
189+
excinfo.match("Specified IV length *")
185190

186191
def test_deserialize_header_framed_bad_frame_length(self):
187192
"""Validate that the deserialize_header function behaves
188193
as expected for bad frame length values (greater than
189194
the default maximum).
190195
"""
191-
with six.assertRaisesRegex(self, SerializationError, "Specified frame length larger than allowed maximum: *"):
196+
with pytest.raises(SerializationError) as excinfo:
192197
stream = io.BytesIO(VALUES["serialized_header_bad_frame_len"])
193198
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
199+
excinfo.match("Specified frame length larger than allowed maximum: *")
194200

195201
def test_deserialize_header_non_framed_bad_frame_length(self):
196202
"""Validate that the deserialize_header function behaves
197203
as expected for bad frame length values for non-framed
198204
messages (non-zero).
199205
"""
200-
with six.assertRaisesRegex(self, SerializationError, "Non-zero frame length found for non-framed message"):
206+
with pytest.raises(SerializationError) as excinfo:
201207
stream = io.BytesIO(VALUES["serialized_non_framed_header_bad_frame_len"])
202208
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
209+
excinfo.match("Non-zero frame length found for non-framed message")
203210

204211
def test_deserialize_header_valid(self):
205212
"""Validate that the deserialize_header function behaves
@@ -247,10 +254,11 @@ def test_deserialize_body_frame_final_invalid_final_frame_length(self):
247254
behaves as expected for a valid final body frame.
248255
"""
249256
stream = io.BytesIO(VALUES["serialized_final_frame_bad_length"])
250-
with six.assertRaisesRegex(self, SerializationError, "Invalid final frame length: *"):
257+
with pytest.raises(SerializationError) as excinfo:
251258
aws_encryption_sdk.internal.formatting.deserialize.deserialize_frame(
252259
stream=stream, header=VALUES["deserialized_header_frame"]
253260
)
261+
excinfo.match("Invalid final frame length: *")
254262

255263
def test_deserialize_footer_no_verifier(self):
256264
"""Vaidate that the deserialize_footer function behaves
@@ -275,8 +283,9 @@ def test_deserialize_footer_verifier_no_footer(self):
275283
with no footer.
276284
"""
277285
stream = io.BytesIO(b"")
278-
with six.assertRaisesRegex(self, SerializationError, "No signature found in message"):
286+
with pytest.raises(SerializationError) as excinfo:
279287
aws_encryption_sdk.internal.formatting.deserialize.deserialize_footer(stream, self.mock_verifier)
288+
excinfo.match("No signature found in message")
280289

281290
@patch("aws_encryption_sdk.internal.formatting.deserialize.struct")
282291
def test_unpack_values(self, mock_struct):
@@ -328,59 +337,65 @@ def test_deserialize_wrapped_key_symmetric(self):
328337
)
329338

330339
def test_deserialize_wrapped_key_symmetric_wrapping_key_mismatch(self):
331-
with six.assertRaisesRegex(self, SerializationError, "Master Key mismatch for wrapped data key"):
340+
with pytest.raises(SerializationError) as excinfo:
332341
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
333342
wrapping_algorithm=self.mock_wrapping_algorithm,
334343
wrapping_key_id=b"asifuhasjaskldjfhlsakdfj",
335344
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"]["wrapped_encrypted_data_key_asymmetric"],
336345
)
346+
excinfo.match("Master Key mismatch for wrapped data key")
337347

338348
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_info(self):
339-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: key info missing data"):
349+
with pytest.raises(SerializationError) as excinfo:
340350
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
341351
wrapping_algorithm=self.mock_wrapping_algorithm,
342352
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
343353
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
344354
"wrapped_encrypted_data_key_symmetric_incomplete_info"
345355
],
346356
)
357+
excinfo.match("Malformed key info: key info missing data")
347358

348359
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_iv_len_mismatch(self):
349-
with six.assertRaisesRegex(self, SerializationError, "Wrapping AlgorithmSuite mismatch for wrapped data key"):
360+
with pytest.raises(SerializationError) as excinfo:
350361
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
351362
wrapping_algorithm=self.mock_wrapping_algorithm,
352363
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
353364
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
354365
"wrapped_encrypted_data_key_symmetric_bad_iv_len"
355366
],
356367
)
368+
excinfo.match("Wrapping AlgorithmSuite mismatch for wrapped data key")
357369

358370
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_iv(self):
359-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete iv"):
371+
with pytest.raises(SerializationError) as excinfo:
360372
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
361373
wrapping_algorithm=self.mock_wrapping_algorithm,
362374
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
363375
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
364376
"wrapped_encrypted_data_key_symmetric_incomplete_iv"
365377
],
366378
)
379+
excinfo.match("Malformed key info: incomplete iv")
367380

368381
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag(self):
369-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"):
382+
with pytest.raises(SerializationError) as excinfo:
370383
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
371384
wrapping_algorithm=self.mock_wrapping_algorithm,
372385
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
373386
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
374387
"wrapped_encrypted_data_key_symmetric_incomplete_tag"
375388
],
376389
)
390+
excinfo.match("Malformed key info: incomplete ciphertext or tag")
377391

378392
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag2(self):
379-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"):
393+
with pytest.raises(SerializationError) as excinfo:
380394
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
381395
wrapping_algorithm=self.mock_wrapping_algorithm,
382396
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
383397
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
384398
"wrapped_encrypted_data_key_symmetric_incomplete_tag2"
385399
],
386400
)
401+
excinfo.match("Malformed key info: incomplete ciphertext or tag")

0 commit comments

Comments
 (0)