Skip to content

Commit

Permalink
Refactor the encryptiondecryptionutilstests
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <jiallian@amazon.com>
  • Loading branch information
RyanL1997 committed Aug 22, 2023
1 parent 1c1bae6 commit 878a107
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.junit.Test;
import java.util.Base64;

public class EncryptionDecryptionUtilTest {
public class EncryptionDecryptionUtilsTest {

@Test
public void testEncryptDecrypt() {
Expand All @@ -36,25 +36,22 @@ public void testDecryptingWithWrongKey() {

String encryptedString = EncryptionDecryptionUtil.encrypt(secret1, data);

try {
EncryptionDecryptionUtil.decrypt(secret2, encryptedString);
Assert.fail("Should have thrown an exception when decrypting with a wrong key");
} catch (RuntimeException e) {
// Expected exception
}
RuntimeException ex = Assert.assertThrows(RuntimeException.class, () -> EncryptionDecryptionUtil.decrypt(secret2, encryptedString));

Assert.assertEquals("The cipher was unable to perform pass over data", ex.getMessage());
}

@Test
public void testDecryptingCorruptedData() {
String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
String corruptedEncryptedString = "corruptedData";

try {
EncryptionDecryptionUtil.decrypt(secret, corruptedEncryptedString);
Assert.fail("Should have thrown an exception when trying to decrypt corrupted data");
} catch (RuntimeException e) {
// Expected exception
}
RuntimeException ex = Assert.assertThrows(
RuntimeException.class,
() -> EncryptionDecryptionUtil.decrypt(secret, corruptedEncryptedString)
);

Assert.assertEquals("Last unit does not have enough valid bits", ex.getMessage());
}

@Test
Expand All @@ -69,7 +66,7 @@ public void testEncryptDecryptEmptyString() {
}

@Test(expected = NullPointerException.class)
public void testEncryptDecryptNullValue() {
public void testEncryptNullValue() {
String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
String data = null;

Expand Down

0 comments on commit 878a107

Please sign in to comment.