Skip to content

Commit bdcd293

Browse files
tests, cleanup
1 parent 68e954d commit bdcd293

File tree

6 files changed

+145
-1320
lines changed

6 files changed

+145
-1320
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@
149149
<artifactId>maven-compiler-plugin</artifactId>
150150
<version>3.10.1</version>
151151
<configuration>
152-
<source>8</source>
153-
<target>8</target>
152+
<source>1.8</source>
153+
<target>1.8</target>
154154
</configuration>
155155
</plugin>
156156

src/main/java/com/amazonaws/encryptionsdk/CMMHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public DecryptionMaterialsHandler decryptMaterials(
8484
// But custom CMMs' behavior was not updated.
8585
// However, there is no custom CMM before version 3.0 that could set an encryptionContext attribute.
8686
// The encryptionContext attribute was only introduced to decryptMaterials objects
87-
// in ESDK 3.0, so no CMM could have set this attribute before 3.0.
88-
// As a result, the ESDK assumes that any legacy native CMM
87+
// in ESDK 3.0, so no CMM could have configured this attribute before 3.0.
88+
// As a result, the ESDK assumes that any native CMM
8989
// that does not add encryptionContext to its decryptMaterials
90-
// SHOULD add encryptionContext to its decryptMaterials.
90+
// SHOULD add encryptionContext to its decryptMaterials,
9191
//
9292
// If a custom CMM implementation conflicts with this assumption.
9393
// that CMM implementation MUST move to the MPL.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.amazonaws.crypto.examples.keyrings.SetEncryptionAlgorithmKeyringExampleTest;
1414
import com.amazonaws.crypto.examples.v2.BasicEncryptionExampleTest;
1515
import com.amazonaws.crypto.examples.v2.BasicMultiRegionKeyEncryptionExampleTest;
16+
import com.amazonaws.crypto.examples.v2.CustomCMMExampleTest;
1617
import com.amazonaws.crypto.examples.v2.DiscoveryDecryptionExampleTest;
1718
import com.amazonaws.crypto.examples.v2.DiscoveryMultiRegionDecryptionExampleTest;
1819
import com.amazonaws.crypto.examples.v2.MultipleCmkEncryptExampleTest;
@@ -80,8 +81,6 @@
8081
AwsCryptoTest.class,
8182
CryptoInputStreamTest.class,
8283
CryptoOutputStreamTest.class,
83-
TestVectorRunner.class,
84-
TestVectorGenerator.class,
8584
XCompatDecryptTest.class,
8685
DefaultCryptoMaterialsManagerTest.class,
8786
NullCryptoMaterialsCacheTest.class,
@@ -102,6 +101,7 @@
102101
CommitmentKATRunner.class,
103102
BasicEncryptionExampleTest.class,
104103
BasicMultiRegionKeyEncryptionExampleTest.class,
104+
CustomCMMExampleTest.class,
105105
DiscoveryDecryptionExampleTest.class,
106106
DiscoveryMultiRegionDecryptionExampleTest.class,
107107
MultipleCmkEncryptExampleTest.class,
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.amazonaws.encryptionsdk;
5+
6+
import com.amazonaws.encryptionsdk.model.DecryptionMaterials;
7+
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsHandler;
8+
import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest;
9+
import com.amazonaws.encryptionsdk.model.KeyBlob;
10+
import org.junit.Test;
11+
12+
import java.security.PublicKey;
13+
import java.util.*;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.when;
19+
20+
public class CMMHandlerTest {
21+
22+
//
23+
private static final CryptoAlgorithm SOME_CRYPTO_ALGORITHM = CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384;
24+
private static final List<KeyBlob> SOME_EDK_LIST = new ArrayList<>(Collections.singletonList(new KeyBlob()));
25+
private static final CommitmentPolicy SOME_COMMITMENT_POLICY = CommitmentPolicy.RequireEncryptRequireDecrypt;
26+
private static final Map<String, String> SOME_NON_EMPTY_ENCRYPTION_CONTEXT = new HashMap<>();
27+
28+
static {{
29+
SOME_NON_EMPTY_ENCRYPTION_CONTEXT.put("SomeKey", "SomeValue");
30+
}}
31+
32+
private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC =
33+
DecryptionMaterialsRequest.newBuilder()
34+
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
35+
// Given: Request has some non-empty encryption context
36+
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT)
37+
.setReproducedEncryptionContext(new HashMap<>())
38+
.setEncryptedDataKeys(SOME_EDK_LIST)
39+
.build();
40+
41+
private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC =
42+
DecryptionMaterialsRequest.newBuilder()
43+
.setAlgorithm(SOME_CRYPTO_ALGORITHM)
44+
// Given: Request has empty encryption context
45+
.setEncryptionContext(new HashMap<>())
46+
.setReproducedEncryptionContext(new HashMap<>())
47+
.setEncryptedDataKeys(SOME_EDK_LIST)
48+
.build();
49+
50+
@Test
51+
public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
52+
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
53+
54+
// Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects
55+
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
56+
.setDataKey(mock(DataKey.class))
57+
.setTrailingSignatureKey(mock(PublicKey.class))
58+
.setEncryptionContext(new HashMap<>()).build();
59+
// Given: request with nonempty encryption context
60+
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC))
61+
.thenReturn(someDecryptionMaterialsWithoutEC);
62+
63+
// When: decryptMaterials
64+
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
65+
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC,
66+
SOME_COMMITMENT_POLICY);
67+
68+
// Then: output DecryptionMaterialsHandler has encryption context
69+
assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext());
70+
}
71+
72+
@Test
73+
public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
74+
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
75+
76+
// Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects
77+
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
78+
.setDataKey(mock(DataKey.class))
79+
.setTrailingSignatureKey(mock(PublicKey.class))
80+
.setEncryptionContext(new HashMap<>()).build();
81+
// Given: request with empty encryption context
82+
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC))
83+
.thenReturn(someDecryptionMaterialsWithoutEC);
84+
85+
// When: decryptMaterials
86+
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
87+
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC,
88+
SOME_COMMITMENT_POLICY);
89+
90+
// Then: output DecryptionMaterialsHandler has empty encryption context
91+
assertTrue(output.getEncryptionContext().isEmpty());
92+
}
93+
94+
@Test
95+
public void GIVEN_CMM_adds_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() {
96+
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
97+
98+
// Given: native CMM sets encryptionContext on returned DecryptionMaterials objects
99+
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
100+
.setDataKey(mock(DataKey.class))
101+
.setTrailingSignatureKey(mock(PublicKey.class))
102+
.setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT).build();
103+
// Given: request with nonempty encryption context
104+
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC))
105+
.thenReturn(someDecryptionMaterialsWithoutEC);
106+
107+
// When: decryptMaterials
108+
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
109+
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC,
110+
SOME_COMMITMENT_POLICY);
111+
112+
// Then: output DecryptionMaterialsHandler has nonempty encryption context
113+
assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext());
114+
}
115+
116+
@Test
117+
public void GIVEN_CMM_adds_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() {
118+
CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class);
119+
120+
// Given: native CMM sets encryptionContext on returned DecryptionMaterials objects
121+
DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder()
122+
.setDataKey(mock(DataKey.class))
123+
.setTrailingSignatureKey(mock(PublicKey.class))
124+
.setEncryptionContext(new HashMap<>()).build();
125+
// Given: request with empty encryption context
126+
when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC))
127+
.thenReturn(someDecryptionMaterialsWithoutEC);
128+
129+
// When: decryptMaterials
130+
CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM);
131+
DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC,
132+
SOME_COMMITMENT_POLICY);
133+
134+
// Then: output DecryptionMaterialsHandler has empty encryption context
135+
assertTrue(output.getEncryptionContext().isEmpty());
136+
}
137+
138+
}

0 commit comments

Comments
 (0)