Skip to content

Commit 97ff17a

Browse files
author
Anirav Kareddy
committed
Added test cases for testing if material description was placed in object's metadata correctly
1 parent 541132e commit 97ff17a

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/test/java/software/amazon/encryption/s3/materials/MaterialsDescriptionTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import org.junit.jupiter.api.BeforeAll;
44
import org.junit.jupiter.api.Test;
55

6+
import software.amazon.awssdk.core.ResponseBytes;
7+
import software.amazon.awssdk.core.sync.RequestBody;
8+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
9+
import software.amazon.encryption.s3.S3EncryptionClient;
10+
611
import javax.crypto.KeyGenerator;
712
import javax.crypto.SecretKey;
813
import java.security.KeyPair;
914
import java.security.KeyPairGenerator;
1015
import java.security.NoSuchAlgorithmException;
16+
import java.security.SecureRandom;
1117
import java.util.HashMap;
1218
import java.util.Map;
1319

@@ -16,6 +22,8 @@
1622
import static org.junit.jupiter.api.Assertions.assertNull;
1723
import static org.junit.jupiter.api.Assertions.assertTrue;
1824
import static org.junit.jupiter.api.Assertions.fail;
25+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET;
26+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject;
1927

2028
public class MaterialsDescriptionTest {
2129
private static SecretKey AES_KEY;
@@ -98,5 +106,69 @@ public void testMaterialsDescriptionRsaKeyring() {
98106
assertEquals(2, rsaKeyring.getMaterialsDescription().getMaterialsDescription().size());
99107

100108
}
109+
@Test
110+
public void testAesMaterialsDescriptionInObjectMetadata() {
111+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
112+
.put("version", "1.0")
113+
.build();
114+
AesKeyring aesKeyring = AesKeyring.builder()
115+
.wrappingKey(AES_KEY)
116+
.reEncryptInstructionFile(true)
117+
.secureRandom(new SecureRandom())
118+
.materialsDescription(materialsDescription)
119+
.build();
120+
S3EncryptionClient client = S3EncryptionClient.builder()
121+
.keyring(aesKeyring)
122+
.build();
123+
final String input = "Testing Materials Description in Object Metadata!";
124+
final String objectKey = "test-aes-materials-description-in-object-metadata";
125+
126+
client.putObject(builder -> builder
127+
.bucket(BUCKET)
128+
.key(objectKey)
129+
.build(), RequestBody.fromString(input)
130+
);
131+
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder
132+
.bucket(BUCKET)
133+
.key(objectKey)
134+
.build());
135+
assertEquals(input, responseBytes.asUtf8String());
136+
assertEquals("{\"version\":\"1.0\"}", responseBytes.response().metadata().get("x-amz-matdesc"));
137+
138+
deleteObject(BUCKET, objectKey, client);
139+
140+
}
141+
@Test
142+
public void testRsaMaterialsDescriptionInObjectMetadata() {
143+
PartialRsaKeyPair keyPair = new PartialRsaKeyPair(RSA_KEY_PAIR.getPrivate(), RSA_KEY_PAIR.getPublic());
144+
MaterialsDescription materialsDescription = MaterialsDescription.builder()
145+
.put("version", "1.0")
146+
.put("admin", "yes")
147+
.build();
148+
RsaKeyring rsaKeyring = RsaKeyring.builder()
149+
.wrappingKeyPair(keyPair)
150+
.reEncryptInstructionFile(true)
151+
.materialsDescription(materialsDescription)
152+
.build();
153+
S3EncryptionClient client = S3EncryptionClient.builder()
154+
.keyring(rsaKeyring)
155+
.build();
156+
final String input = "Testing Materials Description in Object Metadata!";
157+
final String objectKey = "test-rsa-materials-description-in-object-metadata";
158+
159+
client.putObject(builder -> builder
160+
.bucket(BUCKET)
161+
.key(objectKey)
162+
.build(), RequestBody.fromString(input)
163+
);
164+
ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(builder -> builder
165+
.bucket(BUCKET)
166+
.key(objectKey)
167+
.build());
168+
assertEquals(input, responseBytes.asUtf8String());
169+
assertEquals("{\"admin\":\"yes\",\"version\":\"1.0\"}", responseBytes.response().metadata().get("x-amz-matdesc"));
170+
171+
}
172+
101173

102174
}

0 commit comments

Comments
 (0)