Skip to content

Commit 3e3e701

Browse files
author
Andres Sanchez
committed
Migrating unit/test_deserialize.py from unittest from pytest
1 parent 1942f59 commit 3e3e701

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

test/unit/test_deserialize.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"""Unit test suite for aws_encryption_sdk.deserialize"""
1414
import io
1515
import struct
16-
import unittest
1716

1817
import pytest
1918
import six
@@ -56,8 +55,9 @@ def test_deserialize_tag():
5655
assert parsed_tag == tag
5756

5857

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

@@ -109,29 +109,32 @@ def test_validate_header_invalid(self):
109109
as expected for a valid header.
110110
"""
111111
self.mock_decrypt.side_effect = InvalidTag()
112-
with six.assertRaisesRegex(self, SerializationError, "Header authorization failed"):
112+
with pytest.raises(SerializationError) as excinfo:
113113
aws_encryption_sdk.internal.formatting.deserialize.validate_header(
114114
header=VALUES["deserialized_header_block"],
115115
header_auth=VALUES["deserialized_header_auth_block"],
116116
raw_header=VALUES["header"],
117117
data_key=VALUES["data_key_obj"],
118118
)
119+
excinfo.match("Header authorization failed")
119120

120121
def test_deserialize_header_unknown_object_type(self):
121122
"""Validate that the deserialize_header function behaves
122123
as expected for an unknown object type.
123124
"""
124-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported type *"):
125+
with pytest.raises(NotSupportedError) as excinfo:
125126
stream = io.BytesIO(VALUES["serialized_header_invalid_object_type"])
126127
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
128+
excinfo.match("Unsupported type *")
127129

128130
def test_deserialize_header_unknown_version(self):
129131
"""Validate that the deserialize_header function behaves
130132
as expected for an unknown message version.
131133
"""
132-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported version *"):
134+
with pytest.raises(NotSupportedError) as excinfo:
133135
stream = io.BytesIO(VALUES["serialized_header_invalid_version"])
134136
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
137+
excinfo.match("Unsupported version *")
135138

136139
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id")
137140
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
141144
mock_unsupported_algorithm = MagicMock()
142145
mock_unsupported_algorithm.allowed = False
143146
mock_algorithm_get.return_value = mock_unsupported_algorithm
144-
with six.assertRaisesRegex(self, NotSupportedError, "Unsupported algorithm *"):
147+
with pytest.raises(NotSupportedError) as excinfo:
145148
stream = io.BytesIO(VALUES["serialized_header_disallowed_algorithm"])
146149
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
150+
excinfo.match("Unsupported algorithm *")
147151

148152
@patch("aws_encryption_sdk.internal.formatting.deserialize.AlgorithmSuite.get_by_id")
149153
def test_deserialize_header_unknown_data_encryption_algorithm(self, mock_algorithm_get):
150154
"""Validate that the deserialize_header function behaves
151155
as expected for an unknown algorithm.
152156
"""
153157
mock_algorithm_get.side_effect = KeyError()
154-
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown algorithm *"):
158+
with pytest.raises(UnknownIdentityError) as excinfo:
155159
stream = io.BytesIO(VALUES["serialized_header_invalid_algorithm"])
156160
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
161+
excinfo.match("Unknown algorithm *")
157162

158163
def test_deserialize_header_unknown_content_type(self):
159164
"""Validate that the deserialize_header function behaves
160165
as expected for an unknown content type.
161166
"""
162-
with six.assertRaisesRegex(self, UnknownIdentityError, "Unknown content type *"):
167+
with pytest.raises(UnknownIdentityError) as excinfo:
163168
stream = io.BytesIO(VALUES["serialized_header_unknown_content_type"])
164169
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
170+
excinfo.match("Unknown content type *")
165171

166172
def test_deserialize_header_invalid_reserved_space(self):
167173
"""Validate that the deserialize_header function behaves
168174
as expected for an invalid value in the reserved
169175
space (formerly content AAD).
170176
"""
171-
with six.assertRaisesRegex(
172-
self, SerializationError, "Content AAD length field is currently unused, its value must be always 0"
173-
):
177+
with pytest.raises(SerializationError) as excinfo:
174178
stream = io.BytesIO(VALUES["serialized_header_bad_reserved_space"])
175179
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
180+
excinfo.match("Content AAD length field is currently unused, its value must be always 0")
176181

177182
def test_deserialize_header_bad_iv_len(self):
178183
"""Validate that the deserialize_header function behaves
179184
as expected for bad IV length (incompatible with
180185
specified algorithm).
181186
"""
182-
with six.assertRaisesRegex(self, SerializationError, "Specified IV length *"):
187+
with pytest.raises(SerializationError) as excinfo:
183188
stream = io.BytesIO(VALUES["serialized_header_bad_iv_len"])
184189
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
190+
excinfo.match("Specified IV length *")
185191

186192
def test_deserialize_header_framed_bad_frame_length(self):
187193
"""Validate that the deserialize_header function behaves
188194
as expected for bad frame length values (greater than
189195
the default maximum).
190196
"""
191-
with six.assertRaisesRegex(self, SerializationError, "Specified frame length larger than allowed maximum: *"):
197+
with pytest.raises(SerializationError) as excinfo:
192198
stream = io.BytesIO(VALUES["serialized_header_bad_frame_len"])
193199
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
200+
excinfo.match("Specified frame length larger than allowed maximum: *")
194201

195202
def test_deserialize_header_non_framed_bad_frame_length(self):
196203
"""Validate that the deserialize_header function behaves
197204
as expected for bad frame length values for non-framed
198205
messages (non-zero).
199206
"""
200-
with six.assertRaisesRegex(self, SerializationError, "Non-zero frame length found for non-framed message"):
207+
with pytest.raises(SerializationError) as excinfo:
201208
stream = io.BytesIO(VALUES["serialized_non_framed_header_bad_frame_len"])
202209
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
210+
excinfo.match("Non-zero frame length found for non-framed message")
203211

204212
def test_deserialize_header_valid(self):
205213
"""Validate that the deserialize_header function behaves
@@ -247,10 +255,11 @@ def test_deserialize_body_frame_final_invalid_final_frame_length(self):
247255
behaves as expected for a valid final body frame.
248256
"""
249257
stream = io.BytesIO(VALUES["serialized_final_frame_bad_length"])
250-
with six.assertRaisesRegex(self, SerializationError, "Invalid final frame length: *"):
258+
with pytest.raises(SerializationError) as excinfo:
251259
aws_encryption_sdk.internal.formatting.deserialize.deserialize_frame(
252260
stream=stream, header=VALUES["deserialized_header_frame"]
253261
)
262+
excinfo.match("Invalid final frame length: *")
254263

255264
def test_deserialize_footer_no_verifier(self):
256265
"""Vaidate that the deserialize_footer function behaves
@@ -275,8 +284,9 @@ def test_deserialize_footer_verifier_no_footer(self):
275284
with no footer.
276285
"""
277286
stream = io.BytesIO(b"")
278-
with six.assertRaisesRegex(self, SerializationError, "No signature found in message"):
287+
with pytest.raises(SerializationError) as excinfo:
279288
aws_encryption_sdk.internal.formatting.deserialize.deserialize_footer(stream, self.mock_verifier)
289+
excinfo.match("No signature found in message")
280290

281291
@patch("aws_encryption_sdk.internal.formatting.deserialize.struct")
282292
def test_unpack_values(self, mock_struct):
@@ -328,59 +338,65 @@ def test_deserialize_wrapped_key_symmetric(self):
328338
)
329339

330340
def test_deserialize_wrapped_key_symmetric_wrapping_key_mismatch(self):
331-
with six.assertRaisesRegex(self, SerializationError, "Master Key mismatch for wrapped data key"):
341+
with pytest.raises(SerializationError) as excinfo:
332342
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
333343
wrapping_algorithm=self.mock_wrapping_algorithm,
334344
wrapping_key_id=b"asifuhasjaskldjfhlsakdfj",
335345
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"]["wrapped_encrypted_data_key_asymmetric"],
336346
)
347+
excinfo.match("Master Key mismatch for wrapped data key")
337348

338349
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_info(self):
339-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: key info missing data"):
350+
with pytest.raises(SerializationError) as excinfo:
340351
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
341352
wrapping_algorithm=self.mock_wrapping_algorithm,
342353
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
343354
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
344355
"wrapped_encrypted_data_key_symmetric_incomplete_info"
345356
],
346357
)
358+
excinfo.match("Malformed key info: key info missing data")
347359

348360
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"):
361+
with pytest.raises(SerializationError) as excinfo:
350362
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
351363
wrapping_algorithm=self.mock_wrapping_algorithm,
352364
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
353365
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
354366
"wrapped_encrypted_data_key_symmetric_bad_iv_len"
355367
],
356368
)
369+
excinfo.match("Wrapping AlgorithmSuite mismatch for wrapped data key")
357370

358371
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_iv(self):
359-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete iv"):
372+
with pytest.raises(SerializationError) as excinfo:
360373
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
361374
wrapping_algorithm=self.mock_wrapping_algorithm,
362375
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
363376
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
364377
"wrapped_encrypted_data_key_symmetric_incomplete_iv"
365378
],
366379
)
380+
excinfo.match("Malformed key info: incomplete iv")
367381

368382
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag(self):
369-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"):
383+
with pytest.raises(SerializationError) as excinfo:
370384
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
371385
wrapping_algorithm=self.mock_wrapping_algorithm,
372386
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
373387
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
374388
"wrapped_encrypted_data_key_symmetric_incomplete_tag"
375389
],
376390
)
391+
excinfo.match("Malformed key info: incomplete ciphertext or tag")
377392

378393
def test_deserialize_wrapped_key_symmetric_wrapping_algorithm_incomplete_tag2(self):
379-
with six.assertRaisesRegex(self, SerializationError, "Malformed key info: incomplete ciphertext or tag"):
394+
with pytest.raises(SerializationError) as excinfo:
380395
aws_encryption_sdk.internal.formatting.deserialize.deserialize_wrapped_key(
381396
wrapping_algorithm=self.mock_wrapping_algorithm,
382397
wrapping_key_id=VALUES["wrapped_keys"]["raw"]["key_info"],
383398
wrapped_encrypted_key=VALUES["wrapped_keys"]["structures"][
384399
"wrapped_encrypted_data_key_symmetric_incomplete_tag2"
385400
],
386401
)
402+
excinfo.match("Malformed key info: incomplete ciphertext or tag")

0 commit comments

Comments
 (0)