Skip to content

Commit

Permalink
Sort test members
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 3, 2023
1 parent c0a9499 commit 2bd9a51
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
106 changes: 53 additions & 53 deletions src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2bd9a51

Please sign in to comment.