12
12
# language governing permissions and limitations under the License.
13
13
"""Test suite for aws_encryption_sdk.materials_managers"""
14
14
import pytest
15
- from mock import MagicMock
15
+ from mock import MagicMock , sentinel
16
16
from pytest_mock import mocker # noqa pylint: disable=unused-import
17
17
18
- from aws_encryption_sdk .identifiers import Algorithm
18
+ from aws_encryption_sdk .identifiers import KeyRingTraceFlag
19
+ from aws_encryption_sdk .internal .defaults import ALGORITHM
19
20
from aws_encryption_sdk .internal .utils .streams import ROStream
20
21
from aws_encryption_sdk .materials_managers import (
22
+ CryptographicMaterials ,
21
23
DecryptionMaterials ,
22
24
DecryptionMaterialsRequest ,
23
25
EncryptionMaterials ,
24
26
EncryptionMaterialsRequest ,
25
27
)
26
- from aws_encryption_sdk .structures import DataKey
28
+ from aws_encryption_sdk .structures import DataKey , KeyRingTrace , MasterKeyInfo
27
29
28
30
pytestmark = [pytest .mark .unit , pytest .mark .local ]
29
31
32
+ _DATA_KEY = DataKey (
33
+ key_provider = MasterKeyInfo (provider_id = "Provider" , key_info = b"Info" ),
34
+ data_key = b"1234567890123456789012" ,
35
+ encrypted_data_key = b"asdf" ,
36
+ )
30
37
31
38
_VALID_KWARGS = {
39
+ "CryptographicMaterials" : dict (
40
+ algorithm = ALGORITHM ,
41
+ encryption_context = {"additional" : "data" },
42
+ data_encryption_key = _DATA_KEY ,
43
+ encrypted_data_keys = [],
44
+ keyring_trace = [
45
+ KeyRingTrace (
46
+ wrapping_key = MasterKeyInfo (provider_id = "Provider" , key_info = b"Info" ),
47
+ flags = {KeyRingTraceFlag .WRAPPING_KEY_GENERATED_DATA_KEY },
48
+ )
49
+ ],
50
+ ),
32
51
"EncryptionMaterialsRequest" : dict (
33
52
encryption_context = {},
34
53
plaintext_rostream = MagicMock (__class__ = ROStream ),
35
54
frame_length = 5 ,
36
- algorithm = MagicMock ( __class__ = Algorithm ) ,
55
+ algorithm = ALGORITHM ,
37
56
plaintext_length = 5 ,
38
57
),
39
58
"EncryptionMaterials" : dict (
40
- algorithm = MagicMock ( __class__ = Algorithm ) ,
41
- data_encryption_key = MagicMock ( __class__ = DataKey ) ,
59
+ algorithm = ALGORITHM ,
60
+ data_encryption_key = _DATA_KEY ,
42
61
encrypted_data_keys = set ([]),
43
62
encryption_context = {},
44
63
signing_key = b"" ,
45
64
),
46
- "DecryptionMaterialsRequest" : dict (
47
- algorithm = MagicMock (__class__ = Algorithm ), encrypted_data_keys = set ([]), encryption_context = {}
65
+ "DecryptionMaterialsRequest" : dict (algorithm = ALGORITHM , encrypted_data_keys = set ([]), encryption_context = {}),
66
+ "DecryptionMaterials" : dict (
67
+ data_key = _DATA_KEY , verification_key = b"ex_verification_key" , algorithm = ALGORITHM , encryption_context = {}
48
68
),
49
- "DecryptionMaterials" : dict (data_key = MagicMock (__class__ = DataKey ), verification_key = b"ex_verification_key" ),
50
69
}
70
+ _REMOVE = object ()
51
71
52
72
53
73
@pytest .mark .parametrize (
54
74
"attr_class, invalid_kwargs" ,
55
75
(
76
+ (CryptographicMaterials , dict (algorithm = 1234 )),
77
+ (CryptographicMaterials , dict (encryption_context = 1234 )),
78
+ (CryptographicMaterials , dict (data_encryption_key = 1234 )),
79
+ (CryptographicMaterials , dict (encrypted_data_keys = 1234 )),
80
+ (CryptographicMaterials , dict (keyring_trace = 1234 )),
56
81
(EncryptionMaterialsRequest , dict (encryption_context = None )),
57
82
(EncryptionMaterialsRequest , dict (frame_length = "not an int" )),
58
83
(EncryptionMaterialsRequest , dict (algorithm = "not an Algorithm or None" )),
59
84
(EncryptionMaterialsRequest , dict (plaintext_length = "not an int or None" )),
60
85
(EncryptionMaterials , dict (algorithm = None )),
61
- (EncryptionMaterials , dict (data_encryption_key = None )),
62
- (EncryptionMaterials , dict (encrypted_data_keys = None )),
63
86
(EncryptionMaterials , dict (encryption_context = None )),
64
87
(EncryptionMaterials , dict (signing_key = u"not bytes or None" )),
65
88
(DecryptionMaterialsRequest , dict (algorithm = None )),
66
89
(DecryptionMaterialsRequest , dict (encrypted_data_keys = None )),
67
90
(DecryptionMaterialsRequest , dict (encryption_context = None )),
68
- (DecryptionMaterials , dict (data_key = None )),
69
91
(DecryptionMaterials , dict (verification_key = 5555 )),
92
+ (DecryptionMaterials , dict (data_key = _DATA_KEY , data_encryption_key = _DATA_KEY )),
93
+ (DecryptionMaterials , dict (data_key = _REMOVE , data_encryption_key = _REMOVE )),
70
94
),
71
95
)
72
96
def test_attributes_fails (attr_class , invalid_kwargs ):
73
97
kwargs = _VALID_KWARGS [attr_class .__name__ ].copy ()
74
98
kwargs .update (invalid_kwargs )
99
+ purge_keys = [key for key , val in kwargs .items () if val is _REMOVE ]
100
+ for key in purge_keys :
101
+ del kwargs [key ]
75
102
with pytest .raises (TypeError ):
76
103
attr_class (** kwargs )
77
104
@@ -85,14 +112,29 @@ def test_encryption_materials_request_attributes_defaults():
85
112
86
113
def test_encryption_materials_defaults ():
87
114
test = EncryptionMaterials (
88
- algorithm = MagicMock (__class__ = Algorithm ),
89
- data_encryption_key = MagicMock (__class__ = DataKey ),
90
- encrypted_data_keys = set ([]),
91
- encryption_context = {},
115
+ algorithm = ALGORITHM , data_encryption_key = _DATA_KEY , encrypted_data_keys = set ([]), encryption_context = {}
92
116
)
93
117
assert test .signing_key is None
94
118
95
119
96
120
def test_decryption_materials_defaults ():
97
- test = DecryptionMaterials (data_key = MagicMock ( __class__ = DataKey ) )
121
+ test = DecryptionMaterials (data_key = _DATA_KEY )
98
122
assert test .verification_key is None
123
+ assert test .algorithm is None
124
+ assert test .encryption_context is None
125
+
126
+
127
+ def test_decryption_materials_legacy_data_key_get ():
128
+ test = DecryptionMaterials (data_encryption_key = _DATA_KEY )
129
+
130
+ assert test .data_encryption_key is _DATA_KEY
131
+ assert test .data_key is _DATA_KEY
132
+
133
+
134
+ def test_decryption_materials_legacy_data_key_set ():
135
+ test = DecryptionMaterials (data_encryption_key = _DATA_KEY )
136
+
137
+ test .data_key = sentinel .data_key
138
+
139
+ assert test .data_encryption_key is sentinel .data_key
140
+ assert test .data_key is sentinel .data_key
0 commit comments