[New] Handling illegal key size in AesCryptoHelperTest

This commit is contained in:
Robert von Burg 2015-10-16 17:31:58 +02:00
parent 46c3db2913
commit 1de56d0043
1 changed files with 111 additions and 52 deletions

View File

@ -9,11 +9,16 @@ import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AesCryptoHelperTest {
private static final Logger logger = LoggerFactory.getLogger(AesCryptoHelperTest.class);
private static final char[] password = "A2589309-17AE-4819-B9E4-E577CFA7778F".toCharArray();
private static final byte[] salt;
@ -28,6 +33,7 @@ public class AesCryptoHelperTest {
@Test
public void shouldWrapStreams() throws Exception {
try {
byte[] clearTextBytes = "Some text".getBytes();
// encrypt data
@ -52,10 +58,18 @@ public class AesCryptoHelperTest {
byte[] decryptedBytes = decryptedOut.toByteArray();
assertArrayEquals(clearTextBytes, decryptedBytes);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
@Test
public void shouldEncryptBytes() {
try {
byte[] clearTextBytes = "Some text".getBytes();
@ -63,10 +77,19 @@ public class AesCryptoHelperTest {
byte[] decryptedBytes = AesCryptoHelper.decrypt(password, salt, encryptedBytes);
assertArrayEquals(clearTextBytes, decryptedBytes);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
@Test
public void shouldEncryptShortFile() {
try {
// file to be encrypted
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
// encrypted file
@ -75,10 +98,19 @@ public class AesCryptoHelperTest {
String decryptedFileS = "target/decrypted_short.txt";
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
@Test
public void shouldEncryptMiddleFile() {
try {
// file to be encrypted
String clearTextFileS = "src/test/resources/crypto_test_middle.txt";
@ -88,10 +120,19 @@ public class AesCryptoHelperTest {
String decryptedFileS = "target/decrypted_middle.txt";
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
@Test
public void shouldEncryptLongFile() {
try {
// file to be encrypted
String clearTextFileS = "src/test/resources/crypto_test_long.txt";
@ -101,11 +142,21 @@ public class AesCryptoHelperTest {
String decryptedFileS = "target/decrypted_long.txt";
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
@Test
public void shouldEncryptBinaryFile() {
try {
// file to be encrypted
String clearTextFileS = "src/test/resources/crypto_test_image.ico";
// encrypted file
@ -114,6 +165,14 @@ public class AesCryptoHelperTest {
String decryptedFileS = "target/decrypted_image.ico";
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidKeyException
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
else
throw e;
}
}
private static void testCrypto(String clearTextFileS, String encryptedFileS, String decryptedFileS) {