diff --git a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java index ba9ba5da9..111d0efb9 100644 --- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java +++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java @@ -172,6 +172,36 @@ private void byteBufferTest(final String transformation, final byte[] key, final } } + private CryptoCipher getCipher(final String transformation) throws ClassNotFoundException { + return (CryptoCipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(cipherClass), props, + transformation); + } + + protected abstract void init(); + + SecretKeySpec newSecretKeySpec() { + return AES.newSecretKeySpec(KEY); + } + + private void resetCipher(final String transformation, final byte[] key, final byte[] iv) + throws ClassNotFoundException, InvalidKeyException, InvalidAlgorithmParameterException { + enc = getCipher(transformation); + dec = getCipher(transformation); + + enc.init(Cipher.ENCRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv)); + + dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv)); + } + + @BeforeEach + public void setup() { + init(); + assertNotNull(cipherClass, "cipherClass"); + assertNotNull(transformations, "transformations"); + props = new Properties(); + props.setProperty(CryptoCipherFactory.CLASSES_KEY, cipherClass); + } + @Test public void testCloseTestAfterInit() throws Exception { // This test deliberately does not use try with resources in order to control @@ -181,7 +211,7 @@ public void testCloseTestAfterInit() throws Exception { } } - @Test + @Test public void testCloseTestNoInit() throws Exception { // This test deliberately does not use try with resources in order to control // the sequence of operations exactly @@ -229,58 +259,6 @@ public void testCryptoTest() throws Exception { } } - private CryptoCipher getCipher(final String transformation) throws ClassNotFoundException { - return (CryptoCipher) ReflectionUtils.newInstance(ReflectionUtils.getClassByName(cipherClass), props, - transformation); - } - - protected abstract void init(); - - SecretKeySpec newSecretKeySpec() { - return AES.newSecretKeySpec(KEY); - } - - @Test - public void testReInitAfterClose() throws Exception { - // This test deliberately does not use try with resources in order to control - // the sequence of operations exactly - try (final CryptoCipher enc = getCipher(transformations[0])) { - enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); - enc.close(); - enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); - } - } - - @Test - public void testReInitTest() throws Exception { - // This test deliberately does not use try with resources in order to control - // the sequence of operations exactly - try (final CryptoCipher enc = getCipher(transformations[0])) { - enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); - enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); - enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); - } - } - - private void resetCipher(final String transformation, final byte[] key, final byte[] iv) - throws ClassNotFoundException, InvalidKeyException, InvalidAlgorithmParameterException { - enc = getCipher(transformation); - dec = getCipher(transformation); - - enc.init(Cipher.ENCRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv)); - - dec.init(Cipher.DECRYPT_MODE, AES.newSecretKeySpec(key), new IvParameterSpec(iv)); - } - - @BeforeEach - public void setup() { - init(); - assertNotNull(cipherClass, "cipherClass"); - assertNotNull(transformations, "transformations"); - props = new Properties(); - props.setProperty(CryptoCipherFactory.CLASSES_KEY, cipherClass); - } - @Test public void testInvalidIV() throws Exception { for (final String transform : transformations) { @@ -327,4 +305,26 @@ public void testNullTransform() { assertThrows(IllegalArgumentException.class, () -> getCipher(null).close()); } + + @Test + public void testReInitAfterClose() throws Exception { + // This test deliberately does not use try with resources in order to control + // the sequence of operations exactly + try (final CryptoCipher enc = getCipher(transformations[0])) { + enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); + enc.close(); + enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); + } + } + + @Test + public void testReInitTest() throws Exception { + // This test deliberately does not use try with resources in order to control + // the sequence of operations exactly + try (final CryptoCipher enc = getCipher(transformations[0])) { + enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); + enc.init(Cipher.DECRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); + enc.init(Cipher.ENCRYPT_MODE, newSecretKeySpec(), new IvParameterSpec(IV)); + } + } } diff --git a/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java b/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java index 5201c31e5..a9dc0246e 100644 --- a/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java +++ b/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java @@ -26,14 +26,14 @@ */ public class PositionedCryptoInputStreamJnaTest extends PositionedCryptoInputStreamTest { - @Test - public void testCipher() throws Exception { - testCipher(OpenSslJnaCipher.class.getName()); - } - @BeforeEach public void init() { assumeTrue(OpenSslJna.isEnabled()); } + @Test + public void testCipher() throws Exception { + testCipher(OpenSslJnaCipher.class.getName()); + } + } diff --git a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java index 9bc318f95..d661f29f5 100644 --- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java +++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java @@ -276,17 +276,6 @@ private void doSeekTests(final String cipherClass) throws Exception { testSeekFailed(cipherClass, -1, bufferSize); } - @Test - public void testJCE() throws Exception { - testCipher(AbstractCipherTest.JCE_CIPHER_CLASSNAME); - } - - @Test - public void testJNI() throws Exception { - assumeTrue(Crypto.isNativeCodeLoaded()); - testCipher(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME); - } - private CryptoCipher getCipher(final String cipherClass) throws IOException { try { return (CryptoCipher) ReflectionUtils.newInstance( @@ -336,6 +325,17 @@ protected void testCipher(final String cipherClass) throws Exception { doMultipleReadTest(); } + @Test + public void testJCE() throws Exception { + testCipher(AbstractCipherTest.JCE_CIPHER_CLASSNAME); + } + + @Test + public void testJNI() throws Exception { + assumeTrue(Crypto.isNativeCodeLoaded()); + testCipher(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME); + } + private void testPositionedReadLoop(final String cipherClass, int position, final int length, final int bufferSize, final int total) throws Exception { try (CryptoCipher cipher = getCipher(cipherClass);