Skip to content

Commit 0cd3f8b

Browse files
authored
Add Unit Test Classes, Add Mockito dependency (#14)
* Add Unit Test Classes, Add Mockito dependency.
1 parent 136b025 commit 0cd3f8b

File tree

10 files changed

+406
-2
lines changed

10 files changed

+406
-2
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@
7575
</dependency>
7676

7777
<!-- Test Dependencies -->
78+
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
79+
<dependency>
80+
<groupId>org.mockito</groupId>
81+
<artifactId>mockito-core</artifactId>
82+
<version>4.6.1</version>
83+
<scope>test</scope>
84+
</dependency>
85+
7886
<dependency>
7987
<groupId>org.junit.jupiter</groupId>
8088
<artifactId>junit-jupiter</artifactId>

src/test/java/software/amazon/encryption/s3/S3EncryptionClientTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package software.amazon.encryption.s3;
22

3+
import org.junit.jupiter.api.BeforeAll;
34
import org.junit.jupiter.api.Test;
45
import software.amazon.awssdk.core.ResponseBytes;
56
import software.amazon.awssdk.core.sync.RequestBody;
@@ -14,8 +15,7 @@
1415
import java.util.HashMap;
1516
import java.util.Map;
1617

17-
import static org.junit.jupiter.api.Assertions.assertEquals;
18-
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.*;
1919
import static software.amazon.encryption.s3.S3EncryptionClient.withAdditionalEncryptionContext;
2020

2121
/**
@@ -29,6 +29,41 @@ public class S3EncryptionClientTest {
2929
// This alias must point to the same key as KMS_KEY_ID
3030
private static final String KMS_KEY_ALIAS = System.getenv("AWS_S3EC_TEST_KMS_KEY_ALIAS");
3131

32+
private static SecretKey AES_KEY;
33+
private static KeyPair RSA_KEY_PAIR;
34+
35+
@BeforeAll
36+
public static void setUp() throws NoSuchAlgorithmException {
37+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
38+
keyGen.init(256);
39+
AES_KEY = keyGen.generateKey();
40+
41+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
42+
keyPairGen.initialize(2048);
43+
RSA_KEY_PAIR = keyPairGen.generateKeyPair();
44+
}
45+
46+
@Test
47+
public void s3EncryptionClientWithMultipleKeyringsFails() {
48+
assertThrows(S3EncryptionClientException.class, () -> S3EncryptionClient.builder()
49+
.aesKey(AES_KEY)
50+
.rsaKeyPair(RSA_KEY_PAIR)
51+
.build());
52+
}
53+
54+
@Test
55+
public void s3EncryptionClientWithNoKeyringsFails() {
56+
assertThrows(S3EncryptionClientException.class, () -> S3EncryptionClient.builder()
57+
.build());
58+
}
59+
60+
@Test
61+
public void s3EncryptionClientWithNoLegacyKeyringsFails() {
62+
assertThrows(S3EncryptionClientException.class, () -> S3EncryptionClient.builder()
63+
.enableLegacyModes(true)
64+
.build());
65+
}
66+
3267
@Test
3368
public void KmsWithAliasARN() {
3469
S3Client v3Client = S3EncryptionClient.builder()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package software.amazon.encryption.s3.internal;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class ApiNameVersionTest {
8+
9+
private final static String EXPECTED_API_NAME = "AwsS3Encrypt";
10+
private final static String EXPECTED_API_VERSION = "unknown";
11+
12+
@Test
13+
public void testApiNameWithVersion() {
14+
assertEquals(EXPECTED_API_NAME, ApiNameVersion.apiNameWithVersion().name());
15+
assertEquals(EXPECTED_API_VERSION, ApiNameVersion.apiNameWithVersion().version());
16+
}
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package software.amazon.encryption.s3.internal;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import software.amazon.awssdk.services.s3.S3Client;
6+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
7+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
8+
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
9+
10+
import java.util.Arrays;
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.mockito.Mockito.mock;
16+
17+
public class ContentMetadataStrategyTest {
18+
19+
private S3Client mockS3client;
20+
private Map<String, String> metadata = new HashMap<>();
21+
private GetObjectResponse getObjectResponse;
22+
private ContentMetadata expectedContentMetadata;
23+
private GetObjectRequest getObjectRequest;
24+
25+
@BeforeEach
26+
public void setUp() {
27+
mockS3client = mock(S3Client.class);
28+
metadata.put("x-amz-tag-len" , "128");
29+
metadata.put("x-amz-wrap-alg" , "AES/GCM");
30+
metadata.put("x-amz-cek-alg" , "AES/GCM/NoPadding");
31+
metadata.put("x-amz-key-v2" , "dYHaV24t2HuNACA50fWTh2xpMDk+kpnfhHBcaEonAR3kte6WTmV9uOUxFgyVpz+2dAcJQDj6AKrxKElf");
32+
metadata.put("x-amz-iv" , "j3GWQ0HQVDOkPDf+");
33+
metadata.put("x-amz-matdesc" , "{}");
34+
getObjectRequest = GetObjectRequest.builder()
35+
.bucket("TestBucket")
36+
.key("TestKey")
37+
.build();
38+
}
39+
40+
@Test
41+
public void decodeWithObjectMetadata() {
42+
getObjectResponse = GetObjectResponse.builder()
43+
.metadata(metadata)
44+
.build();
45+
byte[] bytes = {-113, 113, -106, 67, 65, -48, 84, 51, -92, 60, 55, -2};
46+
expectedContentMetadata = ContentMetadata.builder()
47+
.algorithmSuite(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)
48+
.encryptedDataKeyAlgorithm(null)
49+
.encryptedDataKeyContext(new HashMap())
50+
.contentNonce(bytes)
51+
.build();
52+
53+
ContentMetadata contentMetadata = ContentMetadataStrategy.decode(mockS3client, getObjectRequest, getObjectResponse);
54+
assertEquals(expectedContentMetadata.algorithmSuite(), contentMetadata.algorithmSuite());
55+
String actualContentNonce = Arrays.toString(contentMetadata.contentNonce());
56+
String expectedContentNonce = Arrays.toString(expectedContentMetadata.contentNonce());
57+
assertEquals(expectedContentNonce, actualContentNonce);
58+
}
59+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package software.amazon.encryption.s3.internal;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
6+
import software.amazon.encryption.s3.materials.EncryptedDataKey;
7+
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
14+
import static org.mockito.Mockito.mock;
15+
16+
public class ContentMetadataTest {
17+
18+
private EncryptedDataKey encryptedDataKey;
19+
private ContentMetadata actualContentMetadata;
20+
private String encryptedDataKeyAlgorithm;
21+
private final Map<String, String> encryptedDataKeyContext = new HashMap<>();
22+
private byte[] contentNonce;
23+
24+
@BeforeEach
25+
public void setUp() {
26+
encryptedDataKey = mock(EncryptedDataKey.class);
27+
contentNonce = "Test String".getBytes();
28+
encryptedDataKeyAlgorithm = "Test Algorithm";
29+
encryptedDataKeyContext.put("testKey", "testValue");
30+
31+
actualContentMetadata = ContentMetadata.builder()
32+
.algorithmSuite(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)
33+
.encryptedDataKey(encryptedDataKey)
34+
.contentNonce(contentNonce)
35+
.encryptedDataKeyAlgorithm(encryptedDataKeyAlgorithm)
36+
.encryptedDataKeyContext(encryptedDataKeyContext)
37+
.build();
38+
}
39+
40+
@Test
41+
public void testAlgorithmSuite() {
42+
assertEquals(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF, actualContentMetadata.algorithmSuite());
43+
assertNotEquals(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF, actualContentMetadata.algorithmSuite());
44+
}
45+
46+
@Test
47+
public void testEncryptedDataKey() {
48+
assertEquals(encryptedDataKey, actualContentMetadata.encryptedDataKey());
49+
}
50+
51+
@Test
52+
public void testEncryptedDataKeyAlgorithm() {
53+
assertEquals(encryptedDataKeyAlgorithm, actualContentMetadata.encryptedDataKeyAlgorithm());
54+
}
55+
56+
@Test
57+
public void testEncryptedDataKeyContext() {
58+
assertEquals(encryptedDataKeyContext, actualContentMetadata.encryptedDataKeyContext());
59+
}
60+
61+
@Test
62+
public void testContentNonce() {
63+
assertEquals(Arrays.toString(contentNonce),Arrays.toString(actualContentMetadata.contentNonce()));
64+
}
65+
}
66+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package software.amazon.encryption.s3.materials;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
6+
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
13+
14+
public class DecryptionMaterialsTest {
15+
16+
private DecryptionMaterials actualDecryptionMaterials;
17+
private GetObjectRequest s3Request;
18+
private Map<String, String> encryptionContext = new HashMap<>();
19+
20+
@BeforeEach
21+
public void setUp() {
22+
s3Request = GetObjectRequest.builder().bucket("testBucket").key("testKey").build();
23+
encryptionContext.put("testKey", "testValue");
24+
25+
actualDecryptionMaterials = DecryptionMaterials.builder()
26+
.algorithmSuite(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF)
27+
.s3Request(s3Request)
28+
.encryptionContext(encryptionContext)
29+
.build();
30+
}
31+
32+
@Test
33+
public void testS3Request() {
34+
assertEquals(s3Request, actualDecryptionMaterials.s3Request());
35+
}
36+
37+
@Test
38+
public void testAlgorithmSuite() {
39+
assertEquals(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF, actualDecryptionMaterials.algorithmSuite());
40+
assertNotEquals(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF, actualDecryptionMaterials.algorithmSuite());
41+
}
42+
43+
@Test
44+
public void testEncryptionContext() {
45+
assertEquals(encryptionContext, actualDecryptionMaterials.encryptionContext());
46+
}
47+
}
48+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package software.amazon.encryption.s3.materials;
2+
3+
import org.junit.jupiter.api.Test;
4+
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
5+
6+
import javax.crypto.SecretKey;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class DefaultDataKeyGeneratorTest {
11+
12+
private final DataKeyGenerator dataKeyGenerator = new DefaultDataKeyGenerator();
13+
14+
@Test
15+
public void testGenerateDataKey() {
16+
SecretKey actualSecretKey = dataKeyGenerator.generateDataKey(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF);
17+
assertEquals("AES", actualSecretKey.getAlgorithm());
18+
actualSecretKey = dataKeyGenerator.generateDataKey(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF);
19+
assertEquals("AES", actualSecretKey.getAlgorithm());
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package software.amazon.encryption.s3.materials;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Arrays;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class EncryptedDataKeyTest {
11+
12+
private EncryptedDataKey actualEncryptedDataKey;
13+
private byte[] ciphertext;
14+
private String keyProviderId;
15+
private byte[] keyProviderInfo;
16+
17+
@BeforeEach
18+
public void setUp() {
19+
keyProviderId = "testKeyProviderId";
20+
keyProviderInfo = new byte[]{20, 10, 30, 5};
21+
ciphertext = new byte[]{20, 10, 30, 5};
22+
23+
actualEncryptedDataKey = EncryptedDataKey.builder()
24+
.keyProviderId(keyProviderId)
25+
.keyProviderInfo(keyProviderInfo)
26+
.ciphertext(ciphertext)
27+
.build();
28+
}
29+
30+
@Test
31+
public void keyProviderId() {
32+
assertEquals(keyProviderId, actualEncryptedDataKey.keyProviderId());
33+
}
34+
35+
@Test
36+
public void keyProviderInfo() {
37+
assertEquals(Arrays.toString(keyProviderInfo), Arrays.toString(actualEncryptedDataKey.keyProviderInfo()));
38+
}
39+
40+
@Test
41+
public void ciphertext() {
42+
assertEquals(Arrays.toString(ciphertext), Arrays.toString(actualEncryptedDataKey.ciphertext()));
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package software.amazon.encryption.s3.materials;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class EncryptionMaterialsRequestTest {
13+
14+
private PutObjectRequest request;
15+
private EncryptionMaterialsRequest actualRequestBuilder;
16+
private Map<String, String> encryptionContext = new HashMap<>();
17+
18+
@BeforeEach
19+
public void setUp() {
20+
request = PutObjectRequest.builder().bucket("testBucket").key("testKey").build();
21+
encryptionContext.put("Key","Value");
22+
23+
actualRequestBuilder = EncryptionMaterialsRequest.builder()
24+
.s3Request(request).encryptionContext(encryptionContext).build();
25+
}
26+
27+
@Test
28+
public void testS3Request() {
29+
assertEquals(request, actualRequestBuilder.s3Request());
30+
}
31+
32+
@Test
33+
public void testEncryptionContext() {
34+
assertEquals(encryptionContext, actualRequestBuilder.encryptionContext());
35+
}
36+
}

0 commit comments

Comments
 (0)