15
15
16
16
import pytest
17
17
import six
18
+ from mock import patch
18
19
19
20
from aws_encryption_sdk import CommitmentPolicy
20
21
from aws_encryption_sdk .internal .defaults import ALGORITHM , FRAME_LENGTH , LINE_LENGTH
28
29
pytestmark = [pytest .mark .unit , pytest .mark .local ]
29
30
30
31
32
+ # Check if MPL is installed, and skip tests based on its installation status
33
+ # Ideally, this logic would be based on mocking imports and testing logic,
34
+ # but doing that introduces errors that cause other tests to fail.
35
+ try :
36
+ from aws_cryptographic_materialproviders .mpl .references import (
37
+ IKeyring ,
38
+ )
39
+ HAS_MPL = True
40
+
41
+ from aws_encryption_sdk .materials_managers .mpl .cmm import (
42
+ CryptoMaterialsManagerFromMPL ,
43
+ )
44
+ except ImportError :
45
+ HAS_MPL = False
46
+
47
+
31
48
class FakeCryptoMaterialsManager (CryptoMaterialsManager ):
32
49
def get_encryption_materials (self , request ):
33
50
return
@@ -42,6 +59,14 @@ class FakeMasterKeyProvider(MasterKeyProvider):
42
59
43
60
def _new_master_key (self , key_id ):
44
61
return
62
+
63
+ if HAS_MPL :
64
+ class FakeKeyring (IKeyring ):
65
+ def on_encrypt (self , param ):
66
+ return
67
+
68
+ def on_decrypt (self , param ):
69
+ return
45
70
46
71
47
72
BASE_KWARGS = dict (
@@ -126,6 +151,18 @@ def test_client_config_defaults():
126
151
assert test .max_encrypted_data_keys is None
127
152
128
153
154
+ @pytest .mark .skipif (not HAS_MPL , reason = "Test should only be executed with MPL in installation" )
155
+ def test_client_config_with_mpl_attr ():
156
+ test = _ClientConfig (** BASE_KWARGS )
157
+ assert hasattr (test , "keyring" )
158
+
159
+
160
+ @pytest .mark .skipif (HAS_MPL , reason = "Test should only be executed without MPL in installation" )
161
+ def test_client_config_no_mpl ():
162
+ test = _ClientConfig (** BASE_KWARGS )
163
+ assert not hasattr (test , "keyring" )
164
+
165
+
129
166
def test_encryptor_config_defaults ():
130
167
test = EncryptorConfig (** BASE_KWARGS )
131
168
assert test .encryption_context == {}
@@ -154,3 +191,62 @@ def test_client_config_converts(kwargs, stream_type):
154
191
assert isinstance (test .source , stream_type )
155
192
if test .key_provider is not None :
156
193
assert isinstance (test .materials_manager , DefaultCryptoMaterialsManager )
194
+
195
+
196
+ @pytest .mark .skipif (HAS_MPL , reason = "Test should only be executed without MPL in installation" )
197
+ @patch .object (_ClientConfig , "_no_mpl_attrs_post_init" )
198
+ def test_GIVEN_no_mpl_WHEN_attrs_post_init_THEN_calls_no_mpl_method (
199
+ mock_no_mpl_attrs_post_init ,
200
+ ):
201
+ _ClientConfig (** BASE_KWARGS )
202
+ mock_no_mpl_attrs_post_init .assert_called_once_with ()
203
+
204
+
205
+ @pytest .mark .skipif (not HAS_MPL , reason = "Test should only be executed with MPL in installation" )
206
+ @patch .object (_ClientConfig , "_has_mpl_attrs_post_init" )
207
+ def test_GIVEN_has_mpl_WHEN_attrs_post_init_THEN_calls_no_mpl_method (
208
+ _has_mpl_attrs_post_init ,
209
+ ):
210
+ _ClientConfig (** BASE_KWARGS )
211
+ _has_mpl_attrs_post_init .assert_called_once_with ()
212
+
213
+
214
+ @pytest .mark .parametrize (
215
+ "kwargs, stream_type" ,
216
+ (
217
+ (dict (source = b"" , materials_manager = FakeCryptoMaterialsManager ()), io .BytesIO ),
218
+ (dict (source = b"" , key_provider = FakeMasterKeyProvider ()), io .BytesIO ),
219
+ (dict (source = "" , materials_manager = FakeCryptoMaterialsManager ()), io .BytesIO ),
220
+ (dict (source = io .BytesIO (), materials_manager = FakeCryptoMaterialsManager ()), io .BytesIO ),
221
+ (dict (source = six .StringIO (), materials_manager = FakeCryptoMaterialsManager ()), six .StringIO ),
222
+ (dict (source = b"" , keyring = FakeKeyring ()), io .BytesIO ),
223
+ ),
224
+ )
225
+ @pytest .mark .skipif (not HAS_MPL , reason = "Test should only be executed with MPL in installation" )
226
+ def test_client_configs_with_mpl (
227
+ kwargs ,
228
+ stream_type
229
+ ):
230
+ kwargs ["commitment_policy" ] = CommitmentPolicy .REQUIRE_ENCRYPT_REQUIRE_DECRYPT
231
+
232
+ test = _ClientConfig (** kwargs )
233
+
234
+ # In all cases, config should have a materials manager
235
+ assert test .materials_manager is not None
236
+
237
+ # If materials manager was provided, it should be directly used
238
+ if hasattr (kwargs , "materials_manager" ):
239
+ assert kwargs ["materials_manager" ] == test .materials_manager
240
+
241
+ # If MPL keyring was provided, it should be wrapped in MPL materials manager
242
+ if hasattr (kwargs , "keyring" ):
243
+ assert test .keyring is not None
244
+ assert test .keyring == kwargs ["keyring" ]
245
+ assert isinstance (test .keyring , IKeyring )
246
+ assert isinstance (test .materials_manager , CryptoMaterialsManagerFromMPL )
247
+
248
+ # If native key_provider was provided, it should be wrapped in native materials manager
249
+ if hasattr (kwargs , "key_provider" ):
250
+ assert test .key_provider is not None
251
+ assert test .key_provider == kwargs ["key_provider" ]
252
+ assert isinstance (test .materials_manager , DefaultCryptoMaterialsManager )
0 commit comments