Skip to content

Commit 98fbe39

Browse files
?
1 parent abfc8d9 commit 98fbe39

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

src/test/java/com/amazonaws/crypto/examples/v2/V2DefaultCryptoMaterialsManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.amazonaws.crypto.examples.v2;
22

3+
import static com.amazonaws.encryptionsdk.internal.Utils.assertNonNull;
4+
35
import com.amazonaws.encryptionsdk.CommitmentPolicy;
46
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
57
import com.amazonaws.encryptionsdk.CryptoMaterialsManager;
@@ -16,7 +18,6 @@
1618
import com.amazonaws.encryptionsdk.model.EncryptionMaterials;
1719
import com.amazonaws.encryptionsdk.model.EncryptionMaterialsRequest;
1820
import com.amazonaws.encryptionsdk.model.KeyBlob;
19-
2021
import java.security.GeneralSecurityException;
2122
import java.security.KeyPair;
2223
import java.security.PublicKey;
@@ -25,8 +26,6 @@
2526
import java.util.List;
2627
import java.util.Map;
2728

28-
import static com.amazonaws.encryptionsdk.internal.Utils.assertNonNull;
29-
3029
/*
3130
This is a copy-paste of the DefaultCryptoMaterialsManager implementation
3231
from the final commit of the V2 ESDK: 1870a082358d59e32c60d74116d6f43c0efa466b

src/test/java/com/amazonaws/encryptionsdk/CMMHandlerTest.java

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,119 +22,139 @@
2222

2323
public class CMMHandlerTest {
2424

25-
private static final CryptoAlgorithm SOME_CRYPTO_ALGORITHM = CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
26-
private static final List<KeyBlob> SOME_EDK_LIST = new ArrayList<>(Collections.singletonList(new KeyBlob()));
27-
private static final CommitmentPolicy SOME_COMMITMENT_POLICY = CommitmentPolicy.RequireEncryptRequireDecrypt;
25+
private static final CryptoAlgorithm SOME_CRYPTO_ALGORITHM =
26+
CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
27+
private static final List<KeyBlob> SOME_EDK_LIST =
28+
new ArrayList<>(Collections.singletonList(new KeyBlob()));
29+
private static final CommitmentPolicy SOME_COMMITMENT_POLICY =
30+
CommitmentPolicy.RequireEncryptRequireDecrypt;
2831
private static final Map<String, String> SOME_NON_EMPTY_ENCRYPTION_CONTEXT = new HashMap<>();
2932

30-
static {{
31-
SOME_NON_EMPTY_ENCRYPTION_CONTEXT.put("SomeKey", "SomeValue");
32-
}}
33+
static {
34+
{
35+
SOME_NON_EMPTY_ENCRYPTION_CONTEXT.put("SomeKey", "SomeValue");
36+
}
37+
}
3338

3439
private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC =
35-
DecryptionMaterialsRequest.newBuilder()
36-
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
37-
// Given: Request has some non-empty encryption context
38-
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT)
39-
.setReproducedEncryptionContext(new HashMap<>())
40-
.setEncryptedDataKeys(SOME_EDK_LIST)
41-
.build();
40+
DecryptionMaterialsRequest.newBuilder()
41+
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
42+
// Given: Request has some non-empty encryption context
43+
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT)
44+
.setReproducedEncryptionContext(new HashMap<>())
45+
.setEncryptedDataKeys(SOME_EDK_LIST)
46+
.build();
4247

4348
private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC =
44-
DecryptionMaterialsRequest.newBuilder()
45-
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
46-
// Given: Request has empty encryption context
47-
.setEncryptionContext(new HashMap<>())
48-
.setReproducedEncryptionContext(new HashMap<>())
49-
.setEncryptedDataKeys(SOME_EDK_LIST)
50-
.build();
49+
DecryptionMaterialsRequest.newBuilder()
50+
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
51+
// Given: Request has empty encryption context
52+
.setEncryptionContext(new HashMap<>())
53+
.setReproducedEncryptionContext(new HashMap<>())
54+
.setEncryptedDataKeys(SOME_EDK_LIST)
55+
.build();
5156

5257
@Test
53-
public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
58+
public void
59+
GIVEN_CMM_does_not_add_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
5460
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
5561

5662
// Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects
57-
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
58-
.setDataKey(mock(DataKey.class))
59-
.setTrailingSignatureKey(mock(PublicKey.class))
60-
.setEncryptionContext(new HashMap<>()).build();
63+
DecryptionMaterials someDecryptionMaterialsWithoutEC =
64+
DecryptionMaterials.newBuilder()
65+
.setDataKey(mock(DataKey.class))
66+
.setTrailingSignatureKey(mock(PublicKey.class))
67+
.setEncryptionContext(new HashMap<>())
68+
.build();
6169
// Given: request with nonempty encryption context
6270
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC))
63-
.thenReturn(someDecryptionMaterialsWithoutEC);
71+
.thenReturn(someDecryptionMaterialsWithoutEC);
6472

6573
// When: decryptMaterials
6674
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
67-
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC,
68-
SOME_COMMITMENT_POLICY);
75+
DecryptionMaterialsHandler output =
76+
handlerUnderTest.decryptMaterials(
77+
SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC, SOME_COMMITMENT_POLICY);
6978

7079
// Then: output DecryptionMaterialsHandler has encryption context
7180
assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext());
7281
}
7382

7483
@Test
75-
public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
84+
public void
85+
GIVEN_CMM_does_not_add_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
7686
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
7787

7888
// Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects
79-
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
89+
DecryptionMaterials someDecryptionMaterialsWithoutEC =
90+
DecryptionMaterials.newBuilder()
8091
.setDataKey(mock(DataKey.class))
8192
.setTrailingSignatureKey(mock(PublicKey.class))
82-
.setEncryptionContext(new HashMap<>()).build();
93+
.setEncryptionContext(new HashMap<>())
94+
.build();
8395
// Given: request with empty encryption context
8496
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC))
85-
.thenReturn(someDecryptionMaterialsWithoutEC);
97+
.thenReturn(someDecryptionMaterialsWithoutEC);
8698

8799
// When: decryptMaterials
88100
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
89-
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC,
90-
SOME_COMMITMENT_POLICY);
101+
DecryptionMaterialsHandler output =
102+
handlerUnderTest.decryptMaterials(
103+
SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC, SOME_COMMITMENT_POLICY);
91104

92105
// Then: output DecryptionMaterialsHandler has empty encryption context
93106
assertTrue(output.getEncryptionContext().isEmpty());
94107
}
95108

96109
@Test
97-
public void GIVEN_CMM_adds_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
110+
public void
111+
GIVEN_CMM_adds_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
98112
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
99113

100114
// Given: native CMM sets encryptionContext on returned DecryptionMaterials objects
101-
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
115+
DecryptionMaterials someDecryptionMaterialsWithoutEC =
116+
DecryptionMaterials.newBuilder()
102117
.setDataKey(mock(DataKey.class))
103118
.setTrailingSignatureKey(mock(PublicKey.class))
104-
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT).build();
119+
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT)
120+
.build();
105121
// Given: request with nonempty encryption context
106122
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC))
107-
.thenReturn(someDecryptionMaterialsWithoutEC);
123+
.thenReturn(someDecryptionMaterialsWithoutEC);
108124

109125
// When: decryptMaterials
110126
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
111-
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC,
112-
SOME_COMMITMENT_POLICY);
127+
DecryptionMaterialsHandler output =
128+
handlerUnderTest.decryptMaterials(
129+
SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC, SOME_COMMITMENT_POLICY);
113130

114131
// Then: output DecryptionMaterialsHandler has nonempty encryption context
115132
assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext());
116133
}
117134

118135
@Test
119-
public void GIVEN_CMM_adds_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
136+
public void
137+
GIVEN_CMM_adds_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
120138
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
121139

122140
// Given: native CMM sets encryptionContext on returned DecryptionMaterials objects
123-
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
141+
DecryptionMaterials someDecryptionMaterialsWithoutEC =
142+
DecryptionMaterials.newBuilder()
124143
.setDataKey(mock(DataKey.class))
125144
.setTrailingSignatureKey(mock(PublicKey.class))
126-
.setEncryptionContext(new HashMap<>()).build();
145+
.setEncryptionContext(new HashMap<>())
146+
.build();
127147
// Given: request with empty encryption context
128148
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC))
129-
.thenReturn(someDecryptionMaterialsWithoutEC);
149+
.thenReturn(someDecryptionMaterialsWithoutEC);
130150

131151
// When: decryptMaterials
132152
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
133-
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC,
134-
SOME_COMMITMENT_POLICY);
153+
DecryptionMaterialsHandler output =
154+
handlerUnderTest.decryptMaterials(
155+
SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC, SOME_COMMITMENT_POLICY);
135156

136157
// Then: output DecryptionMaterialsHandler has empty encryption context
137158
assertTrue(output.getEncryptionContext().isEmpty());
138159
}
139-
140160
}

0 commit comments

Comments
 (0)