12
12
# language governing permissions and limitations under the License.
13
13
"""Unit test suite for aws_encryption_sdk.streaming_client.StreamDecryptor"""
14
14
import io
15
- import unittest
16
15
17
16
import pytest
18
- import six
19
17
from mock import MagicMock , call , patch , sentinel
20
18
21
19
from aws_encryption_sdk .exceptions import CustomMaximumValueExceeded , NotSupportedError , SerializationError
29
27
pytestmark = [pytest .mark .unit , pytest .mark .local ]
30
28
31
29
32
- class TestStreamDecryptor (unittest .TestCase ):
33
- def setUp (self ):
30
+ class TestStreamDecryptor (object ):
31
+ @pytest .fixture (autouse = True )
32
+ def apply_fixtures (self ):
34
33
self .mock_key_provider = MagicMock (__class__ = MasterKeyProvider )
35
34
self .mock_materials_manager = MagicMock (__class__ = CryptoMaterialsManager )
36
35
self .mock_materials_manager .decrypt_materials .return_value = MagicMock (
@@ -92,8 +91,8 @@ def setUp(self):
92
91
# Set up decrypt patch
93
92
self .mock_decrypt_patcher = patch ("aws_encryption_sdk.streaming_client.decrypt" )
94
93
self .mock_decrypt = self .mock_decrypt_patcher .start ()
95
-
96
- def tearDown ( self ):
94
+ yield
95
+ # Run tearDown
97
96
self .mock_deserialize_header_patcher .stop ()
98
97
self .mock_deserialize_header_auth_patcher .stop ()
99
98
self .mock_validate_header_patcher .stop ()
@@ -186,12 +185,11 @@ def test_read_header_frame_too_large(self, mock_derive_datakey):
186
185
test_decryptor .key_provider = self .mock_key_provider
187
186
test_decryptor .source_stream = ct_stream
188
187
test_decryptor ._stream_length = len (VALUES ["data_128" ])
189
- with six .assertRaisesRegex (
190
- self ,
191
- CustomMaximumValueExceeded ,
192
- "Frame Size in header found larger than custom value: {found} > {custom}" .format (found = 1024 , custom = 10 ),
193
- ):
188
+ with pytest .raises (CustomMaximumValueExceeded ) as excinfo :
194
189
test_decryptor ._read_header ()
190
+ excinfo .match (
191
+ "Frame Size in header found larger than custom value: {found} > {custom}" .format (found = 1024 , custom = 10 )
192
+ )
195
193
196
194
@patch ("aws_encryption_sdk.streaming_client.Verifier" )
197
195
@patch ("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest" )
@@ -220,14 +218,13 @@ def test_prep_non_framed_content_length_too_large(self):
220
218
mock_data_key = MagicMock ()
221
219
test_decryptor .data_key = mock_data_key
222
220
223
- with six . assertRaisesRegex (
224
- self ,
225
- CustomMaximumValueExceeded ,
221
+ with pytest . raises ( CustomMaximumValueExceeded ) as excinfo :
222
+ test_decryptor . _prep_non_framed ()
223
+ excinfo . match (
226
224
"Non-framed message content length found larger than custom value: {found} > {custom}" .format (
227
225
found = len (VALUES ["data_128" ]), custom = len (VALUES ["data_128" ]) // 2
228
- ),
229
- ):
230
- test_decryptor ._prep_non_framed ()
226
+ )
227
+ )
231
228
232
229
def test_prep_non_framed (self ):
233
230
test_decryptor = StreamDecryptor (key_provider = self .mock_key_provider , source = self .mock_input_stream )
@@ -287,10 +284,9 @@ def test_read_bytes_from_non_framed_message_body_too_small(self):
287
284
test_decryptor = StreamDecryptor (key_provider = self .mock_key_provider , source = ct_stream )
288
285
test_decryptor .body_length = len (VALUES ["data_128" ] * 2 )
289
286
test_decryptor ._header = self .mock_header
290
- with six .assertRaisesRegex (
291
- self , SerializationError , "Total message body contents less than specified in body description"
292
- ):
287
+ with pytest .raises (SerializationError ) as excinfo :
293
288
test_decryptor ._read_bytes_from_non_framed_body (1 )
289
+ excinfo .match ("Total message body contents less than specified in body description" )
294
290
295
291
def test_read_bytes_from_non_framed_no_verifier (self ):
296
292
ct_stream = io .BytesIO (VALUES ["data_128" ])
@@ -493,8 +489,9 @@ def test_read_bytes_from_framed_body_bad_sequence_number(self):
493
489
frame_data .final_frame = False
494
490
frame_data .ciphertext = b"asdfzxcv"
495
491
self .mock_deserialize_frame .return_value = (frame_data , False )
496
- with six . assertRaisesRegex ( self , SerializationError , "Malformed message: frames out of order" ) :
492
+ with pytest . raises ( SerializationError ) as excinfo :
497
493
test_decryptor ._read_bytes_from_framed_body (4 )
494
+ excinfo .match ("Malformed message: frames out of order" )
498
495
499
496
@patch ("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_non_framed_body" )
500
497
@patch ("aws_encryption_sdk.streaming_client.StreamDecryptor._read_bytes_from_framed_body" )
@@ -545,8 +542,9 @@ def test_read_bytes_unknown(self, mock_read_frame, mock_read_block):
545
542
test_decryptor = StreamDecryptor (key_provider = self .mock_key_provider , source = ct_stream )
546
543
test_decryptor ._header = MagicMock ()
547
544
test_decryptor ._header .content_type = None
548
- with six . assertRaisesRegex ( self , NotSupportedError , "Unsupported content type" ) :
545
+ with pytest . raises ( NotSupportedError ) as excinfo :
549
546
test_decryptor ._read_bytes (5 )
547
+ excinfo .match ("Unsupported content type" )
550
548
551
549
@patch ("aws_encryption_sdk.streaming_client._EncryptionStream.close" )
552
550
def test_close (self , mock_close ):
@@ -561,5 +559,6 @@ def test_close(self, mock_close):
561
559
def test_close_no_footer (self , mock_close ):
562
560
self .mock_header .content_type = ContentType .FRAMED_DATA
563
561
test_decryptor = StreamDecryptor (key_provider = self .mock_key_provider , source = self .mock_input_stream )
564
- with six . assertRaisesRegex ( self , SerializationError , "Footer not read" ) :
562
+ with pytest . raises ( SerializationError ) as excinfo :
565
563
test_decryptor .close ()
564
+ excinfo .match ("Footer not read" )
0 commit comments