Skip to content

Refactor DynamoDBEncryptorTests to use hamcrest #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package com.amazonaws.services.dynamodbv2.datamodeling.encryption;

import static com.amazonaws.services.dynamodbv2.datamodeling.encryption.utils.EncryptionContextOperators.overrideEncryptionContextTableName;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -69,7 +70,8 @@ public class DynamoDBEncryptorTest {
private DynamoDBEncryptor encryptor;
private Map<String, AttributeValue> attribs;
private EncryptionContext context;

private static final String OVERRIDDEN_TABLE_NAME = "TheBestTableName";

@BeforeClass
public static void setUpClass() throws Exception {
KeyGenerator aesGen = KeyGenerator.getInstance("AES");
Expand Down Expand Up @@ -316,9 +318,9 @@ public void testNullEncryptionContextOperator() throws GeneralSecurityException
@Test
public void testTableNameOverriddenEncryptionContextOperator() throws GeneralSecurityException {
// Ensure that the table name is different from what we override the table to.
assertNotEquals(context.getTableName(), "TheBestTableName");
assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME)));
DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov);
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName"));
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME));
Map<String, AttributeValue> encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList());
Map<String, AttributeValue> decryptedItems = encryptor.decryptAllFieldsExcept(encryptedItems, context, Collections.emptyList());
assertThat(decryptedItems, AttrMatcher.match(attribs));
Expand All @@ -332,10 +334,10 @@ public void testTableNameOverriddenEncryptionContextOperator() throws GeneralSec
@Test
public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptor() throws GeneralSecurityException {
// Ensure that the table name is different from what we override the table to.
assertNotEquals(context.getTableName(), "TheBestTableName");
assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME)));
DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov);
DynamoDBEncryptor encryptorWithoutOverride = DynamoDBEncryptor.getInstance(prov);
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName"));
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME));
Map<String, AttributeValue> encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList());

EncryptionContext expectedOverriddenContext = new EncryptionContext.Builder(context).withTableName("TheBestTableName").build();
Expand All @@ -351,10 +353,10 @@ public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptor(
@Test(expected = SignatureException.class)
public void testTableNameOverriddenEncryptionContextOperatorWithSecondEncryptorButTheOriginalEncryptionContext() throws GeneralSecurityException {
// Ensure that the table name is different from what we override the table to.
assertNotEquals(context.getTableName(), "TheBestTableName");
assertThat(context.getTableName(), not(equalTo(OVERRIDDEN_TABLE_NAME)));
DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(prov);
DynamoDBEncryptor encryptorWithoutOverride = DynamoDBEncryptor.getInstance(prov);
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), "TheBestTableName"));
encryptor.setEncryptionContextOverrideOperator(overrideEncryptionContextTableName(context.getTableName(), OVERRIDDEN_TABLE_NAME));
Map<String, AttributeValue> encryptedItems = encryptor.encryptAllFieldsExcept(attribs, context, Collections.emptyList());

// Use the original encryption context, and expect a signature failure
Expand Down