[New] Handling illegal key size in AesCryptoHelperTest
This commit is contained in:
parent
1de56d0043
commit
d0691e4d35
|
@ -95,4 +95,20 @@ public class ExceptionHelper {
|
|||
String root = formatExceptionMessage(t.getCause());
|
||||
return getExceptionMessage(t) + "\ncause:\n" + root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root cause for the given {@link Throwable}
|
||||
*
|
||||
* @param t
|
||||
* the {@link Throwable} for which to get the root cause
|
||||
*
|
||||
* @return the root cause of the given {@link Throwable}
|
||||
*/
|
||||
public static Throwable getRootCause(Throwable t) {
|
||||
while (t.getCause() != null) {
|
||||
t = t.getCause();
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ 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;
|
||||
|
@ -59,8 +58,7 @@ public class AesCryptoHelperTest {
|
|||
|
||||
assertArrayEquals(clearTextBytes, decryptedBytes);
|
||||
} catch (RuntimeException e) {
|
||||
if (e.getCause() instanceof InvalidKeyException
|
||||
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||
if (ExceptionHelper.getRootCause(e).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;
|
||||
|
@ -78,8 +76,7 @@ public class AesCryptoHelperTest {
|
|||
|
||||
assertArrayEquals(clearTextBytes, decryptedBytes);
|
||||
} catch (RuntimeException e) {
|
||||
if (e.getCause() instanceof InvalidKeyException
|
||||
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||
if (ExceptionHelper.getRootCause(e).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;
|
||||
|
@ -88,8 +85,6 @@ public class AesCryptoHelperTest {
|
|||
|
||||
@Test
|
||||
public void shouldEncryptShortFile() {
|
||||
try {
|
||||
|
||||
// file to be encrypted
|
||||
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
|
||||
// encrypted file
|
||||
|
@ -98,20 +93,10 @@ 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";
|
||||
// encrypted file
|
||||
|
@ -120,20 +105,10 @@ 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";
|
||||
// encrypted file
|
||||
|
@ -142,21 +117,11 @@ 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
|
||||
|
@ -166,21 +131,23 @@ public class AesCryptoHelperTest {
|
|||
|
||||
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) {
|
||||
try {
|
||||
|
||||
AesCryptoHelper.encrypt(password, salt, clearTextFileS, encryptedFileS);
|
||||
AesCryptoHelper.decrypt(password, salt, encryptedFileS, decryptedFileS);
|
||||
|
||||
String inputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(clearTextFileS)));
|
||||
String doutputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(decryptedFileS)));
|
||||
assertEquals(inputSha256, doutputSha256);
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
if (ExceptionHelper.getRootCause(e).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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue