37
37
# Ideally, this logic would be based on mocking imports and testing logic,
38
38
# but doing that introduces errors that cause other tests to fail.
39
39
try :
40
- from aws_cryptographic_materialproviders .mpl .references import (
41
- IKeyring ,
42
- )
43
- HAS_MPL = True
44
-
45
40
from aws_encryption_sdk .materials_managers .mpl .cmm import (
46
41
CryptoMaterialsManagerFromMPL ,
47
42
)
43
+ HAS_MPL = True
44
+
48
45
except ImportError :
49
46
HAS_MPL = False
50
47
@@ -238,12 +235,14 @@ def test_read_header(self, mock_derive_datakey, mock_decrypt_materials_request,
238
235
@patch ("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest" )
239
236
@patch ("aws_encryption_sdk.streaming_client.derive_data_encryption_key" )
240
237
@patch ("aws_encryption_sdk.streaming_client.Verifier" )
238
+ # Given: no MPL
241
239
@pytest .mark .skipif (HAS_MPL , reason = "Test should only be executed without MPL in installation" )
242
240
def test_GIVEN_verification_key_AND_no_mpl_WHEN_read_header_THEN_calls_from_key_bytes (
243
241
self ,
244
242
mock_verifier ,
245
243
* _ ,
246
244
):
245
+ # Given: verification key
247
246
mock_verifier_instance = MagicMock ()
248
247
mock_verifier .from_key_bytes .return_value = mock_verifier_instance
249
248
ct_stream = io .BytesIO (VALUES ["data_128" ])
@@ -256,35 +255,42 @@ def test_GIVEN_verification_key_AND_no_mpl_WHEN_read_header_THEN_calls_from_key_
256
255
test_decryptor .source_stream = ct_stream
257
256
test_decryptor ._stream_length = len (VALUES ["data_128" ])
258
257
258
+ # When: read header
259
259
test_decryptor ._read_header ()
260
260
261
+ # Then: calls from_key_bytes
261
262
mock_verifier .from_key_bytes .assert_called_once_with (
262
263
algorithm = self .mock_header .algorithm , key_bytes = sentinel .verification_key
263
264
)
264
265
265
266
@patch ("aws_encryption_sdk.streaming_client.DecryptionMaterialsRequest" )
266
267
@patch ("aws_encryption_sdk.streaming_client.derive_data_encryption_key" )
267
268
@patch ("aws_encryption_sdk.streaming_client.Verifier" )
269
+ # Given: has MPL
268
270
@pytest .mark .skipif (not HAS_MPL , reason = "Test should only be executed with MPL in installation" )
269
271
def test_GIVEN_verification_key_AND_has_mpl_AND_not_MPLCMM_WHEN_read_header_THEN_calls_from_key_bytes (
270
272
self ,
271
273
mock_verifier ,
272
274
* _ ,
273
275
):
276
+ # Given: verification key
274
277
mock_verifier_instance = MagicMock ()
275
278
mock_verifier .from_key_bytes .return_value = mock_verifier_instance
276
279
ct_stream = io .BytesIO (VALUES ["data_128" ])
277
280
mock_commitment_policy = MagicMock (__class__ = CommitmentPolicy )
278
281
test_decryptor = StreamDecryptor (
282
+ # Given: native CMM
279
283
materials_manager = self .mock_materials_manager ,
280
284
source = ct_stream ,
281
285
commitment_policy = mock_commitment_policy ,
282
286
)
283
287
test_decryptor .source_stream = ct_stream
284
288
test_decryptor ._stream_length = len (VALUES ["data_128" ])
285
289
290
+ # When: read_header
286
291
test_decryptor ._read_header ()
287
292
293
+ # Then: calls from_key_bytess
288
294
mock_verifier .from_key_bytes .assert_called_once_with (
289
295
algorithm = self .mock_header .algorithm , key_bytes = sentinel .verification_key
290
296
)
@@ -293,56 +299,36 @@ def test_GIVEN_verification_key_AND_has_mpl_AND_not_MPLCMM_WHEN_read_header_THEN
293
299
@patch ("aws_encryption_sdk.streaming_client.derive_data_encryption_key" )
294
300
@patch ("aws_encryption_sdk.streaming_client.Verifier" )
295
301
@patch ("base64.b64encode" )
302
+ # Given: has MPL
296
303
@pytest .mark .skipif (not HAS_MPL , reason = "Test should only be executed with MPL in installation" )
297
304
def test_GIVEN_verification_key_AND_has_mpl_AND_has_MPLCMM_WHEN_read_header_THEN_calls_from_encoded_point (
298
305
self ,
299
306
mock_b64encoding ,
300
307
mock_verifier ,
301
308
* _ ,
302
309
):
310
+ # Given: Verification key
303
311
mock_verifier_instance = MagicMock ()
304
312
mock_verifier .from_key_bytes .return_value = mock_verifier_instance
305
313
ct_stream = io .BytesIO (VALUES ["data_128" ])
306
314
mock_commitment_policy = MagicMock (__class__ = CommitmentPolicy )
307
315
test_decryptor = StreamDecryptor (
316
+ # Given: MPL CMM
308
317
materials_manager = self .mock_mpl_materials_manager ,
309
318
source = ct_stream ,
310
319
commitment_policy = mock_commitment_policy ,
311
320
)
312
321
test_decryptor .source_stream = ct_stream
313
322
test_decryptor ._stream_length = len (VALUES ["data_128" ])
314
323
324
+ # When: read header
315
325
test_decryptor ._read_header ()
316
326
327
+ # Then: calls from_encoded_point
317
328
mock_verifier .from_encoded_point .assert_called_once_with (
318
329
algorithm = self .mock_header .algorithm , encoded_point = mock_b64encoding ()
319
330
)
320
331
321
- # @patch("aws_encryption_sdk.streaming_client.Verifier")
322
- # @pytest.mark.skipif(not HAS_MPL, reason="Test should only be executed with MPL in installation")
323
- # def test_GIVEN_verification_key_AND_has_mpl_AND_not_MPLCMM_WHEN_read_header_THEN_calls_from_key_bytes(
324
- # self,
325
- # mock_verifier,
326
- # ):
327
- # mock_verifier_instance = MagicMock()
328
- # mock_verifier.from_key_bytes.return_value = mock_verifier_instance
329
- # ct_stream = io.BytesIO(VALUES["data_128"])
330
- # mock_commitment_policy = MagicMock(__class__=CommitmentPolicy)
331
- # test_decryptor = StreamDecryptor(
332
- # materials_manager=self.mock_materials_manager,
333
- # source=ct_stream,
334
- # commitment_policy=mock_commitment_policy,
335
- # )
336
- # test_decryptor.source_stream = ct_stream
337
- # test_decryptor._stream_length = len(VALUES["data_128"])
338
-
339
- # test_decryptor._read_header()
340
-
341
- # mock_verifier.from_key_bytes.assert_called_once_with(
342
- # algorithm=self.mock_header.algorithm, key_bytes=sentinel.verification_key
343
- # )
344
-
345
-
346
332
@patch ("aws_encryption_sdk.streaming_client.derive_data_encryption_key" )
347
333
def test_read_header_frame_too_large (self , mock_derive_datakey ):
348
334
self .mock_header .content_type = ContentType .FRAMED_DATA
0 commit comments