You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/12/27 17:11:30 UTC
[commons-crypto] branch master updated: CRYPTO-151 - Migrate to
Junit 5
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
The following commit(s) were added to refs/heads/master by this push:
new 2ed4f9b CRYPTO-151 - Migrate to Junit 5
new 8f45c6a Merge pull request #114 from arturobernalg/feature/CRYPTO-151
2ed4f9b is described below
commit 2ed4f9b3f4276274b6e5a2f8e13ec3398fe3b5f9
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Sun Nov 22 14:20:22 2020 +0100
CRYPTO-151 - Migrate to Junit 5
---
pom.xml | 27 ++-
.../apache/commons/crypto/AbstractBenchmark.java | 7 +-
.../java/org/apache/commons/crypto/CryptoTest.java | 19 +-
.../commons/crypto/NativeCodeLoaderTest.java | 22 ++-
.../commons/crypto/cipher/AbstractCipherTest.java | 69 ++++---
.../crypto/cipher/CryptoCipherFactoryTest.java | 39 ++--
.../commons/crypto/cipher/GcmCipherTest.java | 96 +++++-----
.../commons/crypto/cipher/JceCipherTest.java | 22 ++-
.../commons/crypto/cipher/OpenSslCipherTest.java | 137 +++++++-------
.../crypto/jna/AbstractCipherJnaStreamTest.java | 20 +-
.../jna/CbcNoPaddingCipherJnaStreamTest.java | 3 +-
.../jna/CbcPkcs5PaddingCipherJnaStreamTest.java | 3 +-
.../commons/crypto/jna/CtrCryptoJnaStreamTest.java | 3 +-
.../jna/CtrNoPaddingCipherJnaStreamTest.java | 3 +-
.../commons/crypto/jna/OpenSslJnaCipherTest.java | 5 +-
.../crypto/jna/OpenSslJnaCryptoRandomTest.java | 15 +-
.../commons/crypto/jna/OpenSslNativeJnaTest.java | 2 +-
.../jna/PositionedCryptoInputStreamJnaTest.java | 11 +-
.../commons/crypto/random/AbstractRandomTest.java | 10 +-
.../crypto/random/CryptoRandomFactoryTest.java | 85 ++++-----
.../apache/commons/crypto/random/DummyRandom.java | 3 +-
.../commons/crypto/random/FailingRandom.java | 3 +-
.../crypto/random/JavaCryptoRandomTest.java | 8 +-
.../crypto/random/OpenSslCryptoRandomTest.java | 11 +-
.../commons/crypto/random/OsCryptoRandomTest.java | 41 ++--
.../crypto/stream/AbstractCipherStreamTest.java | 209 +++++++++------------
.../stream/CbcNoPaddingCipherStreamTest.java | 4 +-
.../stream/CbcPkcs5PaddingCipherStreamTest.java | 4 +-
.../commons/crypto/stream/CtrCryptoStreamTest.java | 81 ++++----
.../stream/CtrNoPaddingCipherStreamTest.java | 4 +-
.../stream/PositionedCryptoInputStreamTest.java | 47 ++---
.../crypto/stream/input/ChannelInputTest.java | 6 +-
.../crypto/stream/output/StreamOutputTest.java | 6 +-
.../org/apache/commons/crypto/utils/EnumTest.java | 2 +-
.../org/apache/commons/crypto/utils/UtilsTest.java | 18 +-
35 files changed, 501 insertions(+), 544 deletions(-)
diff --git a/pom.xml b/pom.xml
index 68407c0..b898251 100644
--- a/pom.xml
+++ b/pom.xml
@@ -162,7 +162,7 @@ The following provides more details on the included cryptographic software:
User is able to specify the platform by maven profiles.-->
<target.name>all</target.name>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
- <junit.version>4.13</junit.version>
+ <junit.version>5.7.0</junit.version>
<!-- Override default buildNumber timestamp format, needed for coveralls plugin -->
<maven.buildNumber.timestampFormat>{0,date,yyyy-MM-dd HH:mm:ssZ}</maven.buildNumber.timestampFormat>
@@ -708,17 +708,28 @@ The following provides more details on the included cryptographic software:
</plugin>
</plugins>
</reporting>
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit</groupId>
+ <artifactId>junit-bom</artifactId>
+ <version>${junit.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
<dependencies>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>${jna.version}</version>
- </dependency>
+ </dependency>
+ <!-- testing -->
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
index 175d85f..5a8874c 100644
--- a/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
+++ b/src/test/java/org/apache/commons/crypto/AbstractBenchmark.java
@@ -28,7 +28,8 @@ import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;
-import org.junit.Assert;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
public abstract class AbstractBenchmark {
@@ -67,7 +68,7 @@ public abstract class AbstractBenchmark {
Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, className);
final CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props);
- Assert.assertEquals(className, cryptoRandom.getClass().getCanonicalName());
+ assertEquals(className, cryptoRandom.getClass().getCanonicalName());
return cryptoRandom;
}
@@ -75,7 +76,7 @@ public abstract class AbstractBenchmark {
Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, className);
CryptoCipher cipher = CryptoCipherFactory.getCryptoCipher("AES/CBC/PKCS5Padding", properties);
- Assert.assertEquals(className, cipher.getClass().getCanonicalName());
+ assertEquals(className, cipher.getClass().getCanonicalName());
return cipher;
}
diff --git a/src/test/java/org/apache/commons/crypto/CryptoTest.java b/src/test/java/org/apache/commons/crypto/CryptoTest.java
index 7c6762f..af60902 100644
--- a/src/test/java/org/apache/commons/crypto/CryptoTest.java
+++ b/src/test/java/org/apache/commons/crypto/CryptoTest.java
@@ -17,9 +17,12 @@
*/
package org.apache.commons.crypto;
-import org.junit.Assert;
-import org.junit.Ignore;
-import org.junit.Test;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class CryptoTest {
@@ -30,8 +33,8 @@ public class CryptoTest {
@Test
public void testGetComponentName() {
final String version = Crypto.getComponentName();
- Assert.assertNotNull("Should not be null", version);
- Assert.assertTrue(version, version.matches("^Apache Commons Crypto.*"));
+ assertNotNull("Should not be null", version);
+ assertTrue(version.matches("^Apache Commons Crypto.*"), version);
}
/**
@@ -41,12 +44,12 @@ public class CryptoTest {
@Test
public void testGetComponentVersion() {
final String version = Crypto.getComponentVersion();
- Assert.assertNotNull("Should not be null", version);
- Assert.assertTrue(version, version.matches("^\\d+\\.\\d+.*"));
+ assertNotNull("Should not be null", version);
+ assertTrue(version.matches("^\\d+\\.\\d+.*"), version);
}
@Test
- @Ignore("Mac64 failure with OpenSSL 1.1.1g")
+ @Disabled("Mac64 failure with OpenSSL 1.1.1g")
public void testMain() throws Throwable {
try {
Crypto.main(new String[0]);
diff --git a/src/test/java/org/apache/commons/crypto/NativeCodeLoaderTest.java b/src/test/java/org/apache/commons/crypto/NativeCodeLoaderTest.java
index 3982345..1c08b06 100644
--- a/src/test/java/org/apache/commons/crypto/NativeCodeLoaderTest.java
+++ b/src/test/java/org/apache/commons/crypto/NativeCodeLoaderTest.java
@@ -18,15 +18,17 @@
package org.apache.commons.crypto;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
import java.io.File;
-import org.junit.Assume;
-import org.junit.Ignore;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
public class NativeCodeLoaderTest {
@@ -43,25 +45,25 @@ public class NativeCodeLoaderTest {
@Test
public void testNativePresent() {
- Assume.assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
+ assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
assertNull(NativeCodeLoader.getLoadingError());
}
@Test
public void testNativeNotPresent() {
- Assume.assumeTrue(!NativeCodeLoader.isNativeCodeLoaded());
+ assumeTrue(!NativeCodeLoader.isNativeCodeLoaded());
assertNotNull(NativeCodeLoader.getLoadingError());
}
@Test
public void testCanLoadIfPresent() {
- Assume.assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
+ assumeTrue(NativeCodeLoader.isNativeCodeLoaded());
// This will try to reload the library, so should work
assertNull(NativeCodeLoader.loadLibrary());
}
@Test
- @Ignore("Seems to cause issues with other tests on Linux; disable for now")
+ @Disabled("Seems to cause issues with other tests on Linux; disable for now")
public void testUnSuccessfulLoad() throws Exception {
final String nameKey = System.getProperty(Crypto.LIB_NAME_KEY);
final String pathKey = System.getProperty(Crypto.LIB_PATH_KEY);
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 da36947..4351d6c 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java
@@ -17,7 +17,6 @@
*/
package org.apache.commons.crypto.cipher;
-import static org.junit.Assert.assertNotNull;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
@@ -33,9 +32,14 @@ import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.crypto.utils.ReflectionUtils;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
public abstract class AbstractCipherTest {
@@ -58,11 +62,11 @@ public abstract class AbstractCipherTest {
0x08 };
private CryptoCipher enc, dec;
- @Before
+ @BeforeEach
public void setup() {
init();
- assertNotNull("cipherClass", cipherClass);
- assertNotNull("transformations", transformations);
+ assertNotNull(cipherClass, "cipherClass");
+ assertNotNull(transformations, "transformations");
props = new Properties();
props.setProperty(CryptoCipherFactory.CLASSES_KEY, cipherClass);
}
@@ -123,7 +127,7 @@ public abstract class AbstractCipherTest {
for (final String tran : transformations) {
/** uses the small data set in {@link TestData} */
cipherTests = TestData.getTestData(tran);
- assertNotNull(tran, cipherTests);
+ assertNotNull(cipherTests, tran);
for (int i = 0; i != cipherTests.length; i += 5) {
final byte[] key = DatatypeConverter.parseHexBinary(cipherTests[i + 1]);
final byte[] iv = DatatypeConverter.parseHexBinary(cipherTests[i + 2]);
@@ -147,27 +151,27 @@ public abstract class AbstractCipherTest {
}
}
- @Test(expected = IllegalArgumentException.class)
- public void testNullTransform() throws Exception {
- getCipher(null).close();
+ @Test
+ public void testNullTransform() {
+ assertThrows(IllegalArgumentException.class,
+ () -> getCipher(null).close());
}
- @Test(expected = IllegalArgumentException.class)
- public void testInvalidTransform() throws Exception {
- getCipher("AES/CBR/NoPadding/garbage/garbage").close();
+ @Test
+ public void testInvalidTransform() {
+ assertThrows(IllegalArgumentException.class,
+ () -> getCipher("AES/CBR/NoPadding/garbage/garbage").close());
}
@Test
public void testInvalidKey() throws Exception {
for (final String transform : transformations) {
try (final CryptoCipher cipher = getCipher(transform)) {
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(invalidKey, "AES"), new IvParameterSpec(IV));
- Assert.fail("Expected InvalidKeyException");
- } catch (final InvalidKeyException ike) {
+ assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(invalidKey, "AES"), new IvParameterSpec(IV)));
}
}
}
@@ -176,13 +180,10 @@ public abstract class AbstractCipherTest {
public void testInvalidIV() throws Exception {
for (final String transform : transformations) {
try (final CryptoCipher cipher = getCipher(transform)) {
- Assert.assertNotNull(cipher);
-
+ assertNotNull(cipher);
final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0x0e, 0x0f, 0x11 };
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(invalidIV));
- Assert.fail("Expected InvalidAlgorithmParameterException");
- } catch (final InvalidAlgorithmParameterException iape) {
+ assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new IvParameterSpec(invalidIV)));
}
}
}
@@ -191,11 +192,8 @@ public abstract class AbstractCipherTest {
public void testInvalidIVClass() throws Exception {
for (final String transform : transformations) {
try (final CryptoCipher cipher = getCipher(transform)) {
- Assert.assertNotNull(cipher);
-
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new GCMParameterSpec(IV.length, IV));
- Assert.fail("Should have caught an InvalidAlgorithmParameterException");
- } catch (final InvalidAlgorithmParameterException iape) {
+ assertNotNull(cipher);
+ assertThrows(InvalidAlgorithmParameterException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"), new GCMParameterSpec(IV.length, IV)));
}
}
}
@@ -221,8 +219,8 @@ public abstract class AbstractCipherTest {
output.get(b);
final byte[] c = new byte[encResult.remaining()];
encResult.get(c);
- Assert.fail("AES failed encryption - expected " + new String(DatatypeConverter.printHexBinary(b))
- + " got " + new String(DatatypeConverter.printHexBinary(c)));
+ fail("AES failed encryption - expected " + DatatypeConverter.printHexBinary(b)
+ + " got " + DatatypeConverter.printHexBinary(c));
}
//
@@ -236,7 +234,7 @@ public abstract class AbstractCipherTest {
final byte[] decResultArray = new byte[decResult.remaining()];
input.get(inArray);
decResult.get(decResultArray);
- Assert.fail();
+ fail();
}
}
}
@@ -251,13 +249,13 @@ public abstract class AbstractCipherTest {
final int n = enc.doFinal(input, 0, input.length, temp, 0);
final byte[] cipherText = new byte[n];
System.arraycopy(temp, 0, cipherText, 0, n);
- Assert.assertArrayEquals("byte array encryption error.", output, cipherText);
+ assertArrayEquals(output, cipherText, "byte array encryption error.");
temp = new byte[cipherText.length + blockSize];
final int m = dec.doFinal(cipherText, 0, cipherText.length, temp, 0);
final byte[] plainText = new byte[m];
System.arraycopy(temp, 0, plainText, 0, m);
- Assert.assertArrayEquals("byte array decryption error.", input, plainText);
+ assertArrayEquals(input, plainText, "byte array decryption error.");
}
/** test byte array whose data is randomly generated */
@@ -300,12 +298,11 @@ public abstract class AbstractCipherTest {
plainPos += dec.doFinal(cipherText, offset, cipherPos % bufferLen, realPlainText, plainPos);
// verify
- Assert.assertEquals("random byte array length changes after transformation", dataLen, plainPos);
+ assertEquals(dataLen, plainPos, "random byte array length changes after transformation");
final byte[] shrinkPlainText = new byte[plainPos];
System.arraycopy(realPlainText, 0, shrinkPlainText, 0, plainPos);
- Assert.assertArrayEquals("random byte array contents changes after transformation", plainText,
- shrinkPlainText);
+ assertArrayEquals(plainText, shrinkPlainText, "random byte array contents changes after transformation");
}
}
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
index 41a79e9..1161546 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/CryptoCipherFactoryTest.java
@@ -17,11 +17,14 @@
*/
package org.apache.commons.crypto.cipher;
+import org.junit.jupiter.api.Test;
+
import java.security.GeneralSecurityException;
import java.util.Properties;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
public class CryptoCipherFactoryTest {
@Test
@@ -30,9 +33,9 @@ public class CryptoCipherFactoryTest {
.getCryptoCipher("AES/CBC/NoPadding");
final String name = defaultCipher.getClass().getName();
if (OpenSsl.getLoadingFailureReason() == null) {
- Assert.assertEquals(OpenSslCipher.class.getName(), name);
+ assertEquals(OpenSslCipher.class.getName(), name);
} else {
- Assert.assertEquals(JceCipher.class.getName(), name);
+ assertEquals(JceCipher.class.getName(), name);
}
}
@@ -44,33 +47,39 @@ public class CryptoCipherFactoryTest {
"AES/CBC/NoPadding", properties);
final String name = defaultCipher.getClass().getName();
if (OpenSsl.getLoadingFailureReason() == null) {
- Assert.assertEquals(OpenSslCipher.class.getName(), name);
+ assertEquals(OpenSslCipher.class.getName(), name);
} else {
- Assert.assertEquals(JceCipher.class.getName(), name);
+ assertEquals(JceCipher.class.getName(), name);
}
}
- @Test(expected = GeneralSecurityException.class)
- public void testInvalidCipher() throws GeneralSecurityException {
+ @Test
+ public void testInvalidCipher() {
final Properties properties = new Properties();
properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
"InvalidCipherName");
- CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties);
+ assertThrows(GeneralSecurityException.class,
+ () -> CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties));
+
}
- @Test(expected = GeneralSecurityException.class)
- public void testInvalidTransformation() throws GeneralSecurityException {
+ @Test
+ public void testInvalidTransformation() {
final Properties properties = new Properties();
- CryptoCipherFactory.getCryptoCipher("AES/Invalid/NoPadding", properties);
+ assertThrows(GeneralSecurityException.class,
+ () -> CryptoCipherFactory.getCryptoCipher("AES/Invalid/NoPadding", properties));
+
}
- @Test(expected = IllegalArgumentException.class)
- public void testNoCipher() throws Exception {
+ @Test
+ public void testNoCipher() {
final Properties properties = new Properties();
// An empty string currently means use the default
// However the splitter drops empty fields
properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ",");
- CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties);
+ assertThrows(IllegalArgumentException.class,
+ () -> CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties));
+
}
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
index b12ee3a..8b29a0a 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/GcmCipherTest.java
@@ -30,9 +30,13 @@ import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.crypto.utils.Utils;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
public class GcmCipherTest {
@@ -47,7 +51,7 @@ public class GcmCipherTest {
private String[] cHex;
private String[] tHex;
- @Before
+ @BeforeEach
public void setup() {
//init
cipherClass = OpenSslCipher.class.getName();
@@ -193,7 +197,7 @@ public class GcmCipherTest {
}
}
- @Test(expected = AEADBadTagException.class)
+ @Test
public void testGcmTamperedData() throws Exception {
final Random r = new Random();
@@ -230,20 +234,16 @@ public class GcmCipherTest {
// Tamper the encrypted data.
encOutput[0] = (byte)(encOutput[0] + 1);
- try {
- final CryptoCipher c = Utils.getCipherInstance(transformation, props);
- final Key key = new SecretKeySpec(keyBytes, "AES");
+ final CryptoCipher c = Utils.getCipherInstance(transformation, props);
+ final Key key = new SecretKeySpec(keyBytes, "AES");
+
+ final GCMParameterSpec iv = new GCMParameterSpec(tagLength, ivBytes);
+ c.init(Cipher.DECRYPT_MODE, key, iv);
+ c.updateAAD(aadBytes);
+ Exception ex = assertThrows(AEADBadTagException.class, () -> c.doFinal(encOutput, 0, encOutput.length, decOutput, 0));
+ assertEquals(ex.getMessage(),"Tag mismatch!");
+ c.close();
- final GCMParameterSpec iv = new GCMParameterSpec(tagLength, ivBytes);
- c.init(Cipher.DECRYPT_MODE, key, iv);
- c.updateAAD(aadBytes);
- c.doFinal(encOutput, 0, encOutput.length, decOutput, 0);
- c.close();
- }
- catch (final AEADBadTagException ex) {
- Assert.assertTrue("Tag mismatch!".equals(ex.getMessage()));
- throw ex;
- }
}
@Test
@@ -287,7 +287,7 @@ public class GcmCipherTest {
}
// tag should be the same as JDK's cipher
- Assert.assertArrayEquals(tag_orig, tag);
+ assertArrayEquals(tag_orig, tag);
// like JDK's decrypt mode. The plaintext+tag is the input for decrypt mode
// let's verify the add & tag now
@@ -302,7 +302,8 @@ public class GcmCipherTest {
}
}
- @Test(expected = AEADBadTagException.class)
+ @Test
+ //(expected = AEADBadTagException.class)
public void testGMacTamperedData() throws Exception {
final Random r = new Random();
final byte[] keyBytes = new byte[32];
@@ -328,25 +329,22 @@ public class GcmCipherTest {
c.close();
}
- try {
- // like JDK's decrypt mode. The plaintext+tag is the input for decrypt mode
- final CryptoCipher c = Utils.getCipherInstance(transformation, props);
- final Key key = new SecretKeySpec(keyBytes, "AES");
- final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
- c.init(Cipher.DECRYPT_MODE, key, iv);
+ // like JDK's decrypt mode. The plaintext+tag is the input for decrypt mode
+ final CryptoCipher c = Utils.getCipherInstance(transformation, props);
+ final Key key = new SecretKeySpec(keyBytes, "AES");
+ final GCMParameterSpec iv = new GCMParameterSpec(128, ivBytes);
+ c.init(Cipher.DECRYPT_MODE, key, iv);
+
+ // if the origin data is tampered
+ aad[0] = (byte) (aad[0] + 1);
+ c.updateAAD(aad);
+
+ Exception ex = assertThrows(AEADBadTagException.class,
+ () -> c.doFinal(tag, 0, tag.length, input, 0));
+ assertEquals(ex.getMessage(), "Tag mismatch!");
- // if the origin data is tampered
- aad[0] = (byte) (aad[0] + 1);
- c.updateAAD(aad);
- c.doFinal(tag, 0, tag.length, input, 0);
- c.close();
- }
- catch (final AEADBadTagException ex) {
- Assert.assertTrue("Tag mismatch!".equals(ex.getMessage()));
- throw ex;
- }
}
private void testGcmEncryption(final String kHex, final String pHex, final String ivHex, final String aadHex,
@@ -370,7 +368,7 @@ public class GcmCipherTest {
c.doFinal(input, 0, input.length, output, 0);
- Assert.assertArrayEquals(expectedOutput, output);
+ assertArrayEquals(expectedOutput, output);
c.close();
}
@@ -402,11 +400,11 @@ public class GcmCipherTest {
int partLen = r.nextInt(input.length);
int len = enc.update(input, 0, partLen, encOutput, 0);
- Assert.assertTrue(len == partLen);
+ assertEquals(partLen, len);
len = enc.doFinal(input, partLen, input.length - partLen, encOutput, partLen);
- Assert.assertTrue(len == (input.length + (iv.getTLen() >> 3) - partLen));
+ assertEquals((input.length + (iv.getTLen() >> 3) - partLen), len);
- Assert.assertArrayEquals(expectedOutput, encOutput);
+ assertArrayEquals(expectedOutput, encOutput);
enc.close();
// Decryption
@@ -423,11 +421,11 @@ public class GcmCipherTest {
final byte[] decInput = encOutput;
partLen = r.nextInt(input.length);
len = dec.update(decInput, 0, partLen, decOutput, 0);
- Assert.assertTrue(len == 0);
+ assertEquals(len, 0);
len = dec.doFinal(decInput, partLen, decInput.length - partLen, decOutput, 0);
- Assert.assertTrue(len == input.length);
+ assertEquals(input.length, len);
- Assert.assertArrayEquals(input, decOutput);
+ assertArrayEquals(input, decOutput);
dec.close();
}
@@ -453,7 +451,7 @@ public class GcmCipherTest {
c.updateAAD(aad);
c.doFinal(input, 0, input.length, output, 0);
- Assert.assertArrayEquals(plainBytes, output);
+ assertArrayEquals(plainBytes, output);
c.close();
}
@@ -480,11 +478,11 @@ public class GcmCipherTest {
//only return recovered data after tag is successfully verified
int len = c.update(input, 0, input.length, output, 0);
- Assert.assertTrue(len == 0);
+ assertEquals(len, 0);
len += c.doFinal(input, input.length, 0, output, 0);
- Assert.assertTrue(len == plainBytes.length);
+ assertEquals(plainBytes.length, len);
- Assert.assertArrayEquals(plainBytes, output);
+ assertArrayEquals(plainBytes, output);
c.close();
}
@@ -525,7 +523,7 @@ public class GcmCipherTest {
bfCipherText.flip();
bfCipherText.get(encOutput);
- Assert.assertArrayEquals(cipherText, encOutput);
+ assertArrayEquals(cipherText, encOutput);
c.close();
// Decryption -------------------
@@ -541,7 +539,7 @@ public class GcmCipherTest {
dec.doFinal(bfCipherText, bfPlainText);
bfPlainText.flip();
bfPlainText.get(decOutput);
- Assert.assertArrayEquals(plainText, decOutput);
+ assertArrayEquals(plainText, decOutput);
dec.close();
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
index 84a3895..e094937 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/JceCipherTest.java
@@ -18,12 +18,14 @@
package org.apache.commons.crypto.cipher;
+import org.junit.jupiter.api.BeforeAll;
+
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
-import org.junit.Assert;
-import org.junit.BeforeClass;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
public class JceCipherTest extends AbstractCipherTest {
@@ -38,15 +40,15 @@ public class JceCipherTest extends AbstractCipherTest {
cipherClass = JCE_CIPHER_CLASSNAME;
}
- @BeforeClass
+ @BeforeAll
public static void checkJceUnlimitedStrength() throws NoSuchAlgorithmException {
final int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
- Assert.assertTrue(String.format(
- "Testing requires support for an AES key length of %d, but " +
- "the detected maximum key length is %d. This may indicate " +
- "that the test environment is missing the JCE Unlimited " +
- "Strength Jurisdiction Policy Files.",
- MAX_KEY_LEN_LOWER_BOUND, maxKeyLen),
- maxKeyLen >= MAX_KEY_LEN_LOWER_BOUND);
+ assertTrue(maxKeyLen >= MAX_KEY_LEN_LOWER_BOUND,
+ String.format(
+ "Testing requires support for an AES key length of %d, but " +
+ "the detected maximum key length is %d. This may indicate " +
+ "that the test environment is missing the JCE Unlimited " +
+ "Strength Jurisdiction Policy Files.",
+ MAX_KEY_LEN_LOWER_BOUND, maxKeyLen));
}
}
diff --git a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
index 35187d5..bc11b72 100644
--- a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java
@@ -18,11 +18,15 @@
package org.apache.commons.crypto.cipher;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
+import java.util.concurrent.TimeUnit;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
@@ -30,15 +34,17 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.GCMParameterSpec;
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
public class OpenSslCipherTest extends AbstractCipherTest {
@Override
public void init() {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
transformations = new String[] {
"AES/CBC/NoPadding",
"AES/CBC/PKCS5Padding",
@@ -46,24 +52,29 @@ public class OpenSslCipherTest extends AbstractCipherTest {
cipherClass = OPENSSL_CIPHER_CLASSNAME;
}
- @Test(expected = NoSuchPaddingException.class, timeout = 120000)
- public void testInvalidPadding() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
- OpenSsl.getInstance("AES/CTR/NoPadding2");
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
+ public void testInvalidPadding() {
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assertThrows(NoSuchPaddingException.class,
+ () -> OpenSsl.getInstance("AES/CTR/NoPadding2"));
}
- @Test(expected = NoSuchAlgorithmException.class, timeout = 120000)
- public void testInvalidMode() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
- OpenSsl.getInstance("AES/CTR2/NoPadding");
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
+ public void testInvalidMode() {
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assertThrows(NoSuchAlgorithmException.class,
+ () -> OpenSsl.getInstance("AES/CTR2/NoPadding"));
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testUpdateArguments() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
.getInstance("AES/CTR/NoPadding");
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(IV));
@@ -71,32 +82,28 @@ public class OpenSslCipherTest extends AbstractCipherTest {
ByteBuffer input = ByteBuffer.allocate(1024);
ByteBuffer output = ByteBuffer.allocate(1024);
- try {
- cipher.update(input, output);
- Assert.fail("Should have failed to accept non-direct buffers.");
- } catch (final IllegalArgumentException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Direct buffers are required"));
- }
+ final ByteBuffer finalInput = input;
+ final ByteBuffer finalOutput = output;
+ Exception ex = assertThrows(IllegalArgumentException.class, () -> cipher.update(finalInput, finalOutput));
+ assertTrue(ex.getMessage().contains("Direct buffers are required"));
// Output buffer length should be sufficient to store output data
input = ByteBuffer.allocateDirect(1024);
output = ByteBuffer.allocateDirect(1000);
- try {
- cipher.update(input, output);
- Assert.fail("Failed to check for output buffer size.");
- } catch (final ShortBufferException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Output buffer is not sufficient"));
- }
+ final ByteBuffer finalInput1 = input;
+ final ByteBuffer finalOutput1 = output;
+ ex = assertThrows(ShortBufferException.class, () -> cipher.update(finalInput1, finalOutput1));
+ assertTrue(ex.getMessage().contains("Output buffer is not sufficient"));
+
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testDoFinalArguments() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
.getInstance("AES/CTR/NoPadding");
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(IV));
@@ -104,88 +111,74 @@ public class OpenSslCipherTest extends AbstractCipherTest {
final ByteBuffer input = ByteBuffer.allocate(1024);
final ByteBuffer output = ByteBuffer.allocate(1024);
- try {
- cipher.doFinal(input, output);
- Assert.fail("Should have failed to accept non-direct buffers.");
- } catch (final IllegalArgumentException e) {
- Assert.assertTrue(e.getMessage().contains(
- "Direct buffer is required"));
- }
+ Exception ex = assertThrows(IllegalArgumentException.class, () -> cipher.doFinal(input, output));
+ assertTrue(ex.getMessage().contains("Direct buffer is required"));
}
@Override
- @Test(expected = InvalidKeyException.class, timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testInvalidKey() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
.getInstance("AES/CTR/NoPadding");
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
- cipher.init(OpenSsl.ENCRYPT_MODE, invalidKey, new IvParameterSpec(IV));
+
+ assertThrows(InvalidKeyException.class,
+ () -> cipher.init(OpenSsl.ENCRYPT_MODE, invalidKey, new IvParameterSpec(IV)));
}
@Override
- @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testInvalidIV() throws Exception {
- Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null);
+ assumeTrue(OpenSsl.getLoadingFailureReason() == null);
final OpenSsl cipher = OpenSsl
.getInstance("AES/CTR/NoPadding");
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 };
- cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(invalidIV));
+
+ assertThrows(InvalidAlgorithmParameterException.class,
+ () -> cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new IvParameterSpec(invalidIV)));
}
@Override
- @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testInvalidIVClass() throws Exception {
final OpenSsl cipher = OpenSsl.getInstance("AES/CTR/NoPadding");
- Assert.assertNotNull(cipher);
+ assertNotNull(cipher);
- cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new GCMParameterSpec(IV.length, IV));
+
+ assertThrows(InvalidAlgorithmParameterException.class,
+ () -> cipher.init(OpenSsl.ENCRYPT_MODE, KEY, new GCMParameterSpec(IV.length, IV)));
}
@Test
public void testCipherLifecycle() throws Exception {
try (OpenSslCipher cipher = new OpenSslCipher(new Properties(), "AES/CTR/NoPadding")) {
- try {
- cipher.update(dummyBuffer(), dummyBuffer());
- Assert.fail("Should have thrown exception.");
- } catch (final IllegalStateException ise) {
- // expected;
- }
+ assertThrows(IllegalStateException.class, () -> cipher.update(dummyBuffer(), dummyBuffer()));
cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
cipher.update(dummyBuffer(), dummyBuffer());
- try {
- cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(new byte[1], "AES"),
- new IvParameterSpec(IV));
- Assert.fail("Should have thrown exception.");
- } catch (final InvalidKeyException ike) {
- // expected;
- }
-
+ assertThrows(InvalidKeyException.class, () -> cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(new byte[1], "AES"),
+ new IvParameterSpec(IV)));
// Should keep working with previous init parameters.
cipher.update(dummyBuffer(), dummyBuffer());
cipher.doFinal(dummyBuffer(), dummyBuffer());
cipher.close();
- try {
- cipher.update(dummyBuffer(), dummyBuffer());
- Assert.fail("Should have thrown exception.");
- } catch (final IllegalStateException ise) {
- // expected;
- }
-
+ assertThrows(IllegalStateException.class, () -> cipher.update(dummyBuffer(), dummyBuffer()));
cipher.init(OpenSsl.ENCRYPT_MODE, new SecretKeySpec(KEY, "AES"),
new IvParameterSpec(IV));
cipher.update(dummyBuffer(), dummyBuffer());
- cipher.close();
}
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/AbstractCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/AbstractCipherJnaStreamTest.java
index 88a3e25..a027473 100644
--- a/src/test/java/org/apache/commons/crypto/jna/AbstractCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/AbstractCipherJnaStreamTest.java
@@ -18,25 +18,29 @@
package org.apache.commons.crypto.jna;
import java.io.ByteArrayOutputStream;
+import java.util.concurrent.TimeUnit;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.stream.AbstractCipherStreamTest;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
public abstract class AbstractCipherJnaStreamTest extends AbstractCipherStreamTest {
private static final String CIPHER_OPENSSL_JNA = OpenSslJna.getCipherClass().getName();
- @Before
+ @BeforeEach
public void init() {
- Assume.assumeTrue(OpenSslJna.isEnabled());
+ assumeTrue(OpenSslJna.isEnabled());
}
/** Test skip. */
@Override
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testSkip() throws Exception {
doSkipTest(CIPHER_OPENSSL_JNA, false);
@@ -45,7 +49,7 @@ public abstract class AbstractCipherJnaStreamTest extends AbstractCipherStreamTe
/** Test byte buffer read with different buffer size. */
@Override
- @Test(timeout = 120000)
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testByteBufferRead() throws Exception {
doByteBufferRead(CIPHER_OPENSSL_JNA, false);
@@ -54,7 +58,7 @@ public abstract class AbstractCipherJnaStreamTest extends AbstractCipherStreamTe
/** Test byte buffer write. */
@Override
- @Test(timeout = 120000)
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testByteBufferWrite() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
doByteBufferWrite(CIPHER_OPENSSL_JNA, baos, false);
diff --git a/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
index a8116f4..deba4b8 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CbcNoPaddingCipherJnaStreamTest.java
@@ -17,12 +17,11 @@
*/
package org.apache.commons.crypto.jna;
-import java.io.IOException;
public class CbcNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CBC/NoPadding";
}
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
index 798f582..144dcb5 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CbcPkcs5PaddingCipherJnaStreamTest.java
@@ -17,12 +17,11 @@
*/
package org.apache.commons.crypto.jna;
-import java.io.IOException;
public class CbcPkcs5PaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CBC/PKCS5Padding";
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
index 6eaa69e..137afb4 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CtrCryptoJnaStreamTest.java
@@ -17,12 +17,11 @@
*/
package org.apache.commons.crypto.jna;
-import java.io.IOException;
public class CtrCryptoJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CTR/NoPadding";
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java b/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
index e3d49cd..863fc1b 100644
--- a/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/CtrNoPaddingCipherJnaStreamTest.java
@@ -17,12 +17,11 @@
*/
package org.apache.commons.crypto.jna;
-import java.io.IOException;
public class CtrNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CTR/NoPadding";
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
index dd18bdf..dd995e7 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java
@@ -19,13 +19,14 @@
package org.apache.commons.crypto.jna;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
-import org.junit.Assume;
+
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class OpenSslJnaCipherTest extends AbstractCipherTest {
@Override
public void init() {
- Assume.assumeTrue(OpenSslJna.isEnabled());
+ assumeTrue(OpenSslJna.isEnabled());
transformations = new String[] {
"AES/CBC/NoPadding",
"AES/CBC/PKCS5Padding",
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java
index 5b7ba6d..3880fc1 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java
@@ -17,7 +17,6 @@
*/
package org.apache.commons.crypto.jna;
-import static org.junit.Assert.assertTrue;
import java.security.GeneralSecurityException;
import java.util.Properties;
@@ -25,14 +24,16 @@ import java.util.Properties;
import org.apache.commons.crypto.random.AbstractRandomTest;
import org.apache.commons.crypto.random.CryptoRandom;
import org.apache.commons.crypto.random.CryptoRandomFactory;
-import org.junit.Assume;
-import org.junit.Before;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.BeforeEach;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class OpenSslJnaCryptoRandomTest extends AbstractRandomTest {
- @Before
+ @BeforeEach
public void init() {
- Assume.assumeTrue(OpenSslJna.isEnabled());
+ Assumptions.assumeTrue(OpenSslJna.isEnabled());
}
@Override
@@ -43,8 +44,8 @@ public class OpenSslJnaCryptoRandomTest extends AbstractRandomTest {
OpenSslJnaCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
assertTrue(
- "The CryptoRandom should be: " + OpenSslJnaCryptoRandom.class.getName(),
- random instanceof OpenSslJnaCryptoRandom);
+ random instanceof OpenSslJnaCryptoRandom,
+ "The CryptoRandom should be: " + OpenSslJnaCryptoRandom.class.getName());
return random;
}
diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslNativeJnaTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslNativeJnaTest.java
index 26437a0..8ae0949 100644
--- a/src/test/java/org/apache/commons/crypto/jna/OpenSslNativeJnaTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslNativeJnaTest.java
@@ -18,7 +18,7 @@
package org.apache.commons.crypto.jna;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
public class OpenSslNativeJnaTest {
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 2b2d4b4..325a30d 100644
--- a/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java
+++ b/src/test/java/org/apache/commons/crypto/jna/PositionedCryptoInputStreamJnaTest.java
@@ -18,18 +18,19 @@
package org.apache.commons.crypto.jna;
import org.apache.commons.crypto.stream.PositionedCryptoInputStreamTest;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
*
*/
public class PositionedCryptoInputStreamJnaTest extends PositionedCryptoInputStreamTest {
- @Before
+ @BeforeEach
public void init() {
- Assume.assumeTrue(OpenSslJna.isEnabled());
+ assumeTrue(OpenSslJna.isEnabled());
}
@Test
diff --git a/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java b/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
index f5d8edc..ee3e6d3 100644
--- a/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/AbstractRandomTest.java
@@ -22,15 +22,18 @@ import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.TimeUnit;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
public abstract class AbstractRandomTest {
public abstract CryptoRandom getCryptoRandom()
throws GeneralSecurityException;
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testRandomBytes() throws Exception {
try (CryptoRandom random = getCryptoRandom()) {
// len = 16
@@ -44,7 +47,8 @@ public abstract class AbstractRandomTest {
}
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testRandomBytesMultiThreaded() throws Exception {
final int threadCount = 100;
try (final CryptoRandom random = getCryptoRandom()) {
diff --git a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
index 4f51de5..07f7bdb 100644
--- a/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/CryptoRandomFactoryTest.java
@@ -17,19 +17,24 @@
*/
package org.apache.commons.crypto.random;
+import org.junit.jupiter.api.Test;
+
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.Properties;
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
public class CryptoRandomFactoryTest {
- @Test(expected=NullPointerException.class)
- public void testNull() throws Exception {
- CryptoRandomFactory.getCryptoRandom(null);
+ @Test
+ public void testNull() {
+ assertThrows(NullPointerException.class,
+ () -> CryptoRandomFactory.getCryptoRandom(null));
}
@Test
@@ -46,23 +51,23 @@ public class CryptoRandomFactoryTest {
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
final String name = random.getClass().getName();
if (OpenSslCryptoRandom.isNativeCodeEnabled()) {
- Assert.assertEquals(OpenSslCryptoRandom.class.getName(), name);
+ assertEquals(OpenSslCryptoRandom.class.getName(), name);
} else {
- Assert.assertEquals(JavaCryptoRandom.class.getName(), name);
+ assertEquals(JavaCryptoRandom.class.getName(), name);
}
}
@Test
public void testGetOSRandom() throws GeneralSecurityException {
// Windows does not have a /dev/random device
- Assume.assumeTrue(!System.getProperty("os.name").contains("Windows"));
+ assumeTrue(!System.getProperty("os.name").contains("Windows"));
final Properties props = new Properties();
props.setProperty(
CryptoRandomFactory.CLASSES_KEY,
CryptoRandomFactory.RandomProvider.OS.getClassName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
- Assert.assertEquals(OsCryptoRandom.class.getName(), random.getClass()
+ assertEquals(OsCryptoRandom.class.getName(), random.getClass()
.getName());
}
@@ -74,17 +79,19 @@ public class CryptoRandomFactoryTest {
JavaCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
- Assert.assertEquals(JavaCryptoRandom.class.getName(), random.getClass()
+ assertEquals(JavaCryptoRandom.class.getName(), random.getClass()
.getName());
}
- @Test(expected = GeneralSecurityException.class)
- public void testInvalidRandom() throws GeneralSecurityException {
+ @Test
+ public void testInvalidRandom() {
final Properties properties = new Properties();
properties.setProperty(
CryptoRandomFactory.CLASSES_KEY,
"InvalidCipherName");
- CryptoRandomFactory.getCryptoRandom(properties);
+
+ assertThrows(GeneralSecurityException.class,
+ () -> CryptoRandomFactory.getCryptoRandom(properties));
}
@Test
@@ -94,66 +101,54 @@ public class CryptoRandomFactoryTest {
"org.apache.commons.crypto.cipher",
"OpenSsl");
final CryptoRandom rand = CryptoRandomFactory.getCryptoRandom(properties);
- Assert.assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName());
+ assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName());
}
@Test
public void testDefaultRandomClass() throws GeneralSecurityException {
final CryptoRandom rand = CryptoRandomFactory.getCryptoRandom();
- Assert.assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName());
+ assertEquals(OpenSslCryptoRandom.class.getName(), rand.getClass().getName());
}
@Test
public void testAbstractRandom() {
final Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, AbstractRandom.class.getName());
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- Assert.fail("Expected GeneralSecurityException");
- } catch (final GeneralSecurityException e) {
- final String message = e.getMessage();
- Assert.assertTrue(message, message.contains("InstantiationException"));
- }
-
+ Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props));
+ final String message = ex.getMessage();
+ assertTrue(message.contains("InstantiationException"), message);
}
@Test
public void testDummmyRandom() {
final Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, DummyRandom.class.getName());
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- Assert.fail("Expected GeneralSecurityException");
- } catch (final GeneralSecurityException e) {
- final String message = e.getMessage();
- Assert.assertTrue(message, message.contains("NoSuchMethodException"));
- }
+ Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props));
+ final String message = ex.getMessage();
+ assertTrue(message.contains("NoSuchMethodException"), message);
}
- @Test(expected=IllegalArgumentException.class)
- public void testNoClasses() throws Exception {
+ @Test
+ public void testNoClasses() {
final Properties props = new Properties();
// An empty string currently means use the default
// However the splitter drops empty fields
props.setProperty(CryptoRandomFactory.CLASSES_KEY, ",");
- CryptoRandomFactory.getCryptoRandom(props);
+ assertThrows(IllegalArgumentException.class, () -> CryptoRandomFactory.getCryptoRandom(props));
}
@Test
public void testFailingRandom() {
final Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, FailingRandom.class.getName());
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- Assert.fail("Expected GeneralSecurityException");
- } catch (final GeneralSecurityException e) {
- Throwable cause = e.getCause();
- Assert.assertEquals(IllegalArgumentException.class, cause.getClass());
- cause = cause.getCause();
- Assert.assertEquals(InvocationTargetException.class, cause.getClass());
- cause = cause.getCause();
- Assert.assertEquals(UnsatisfiedLinkError.class, cause.getClass());
- }
+ Exception ex = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props));
+
+ Throwable cause = ex.getCause();
+ assertEquals(IllegalArgumentException.class, cause.getClass());
+ cause = cause.getCause();
+ assertEquals(InvocationTargetException.class, cause.getClass());
+ cause = cause.getCause();
+ assertEquals(UnsatisfiedLinkError.class, cause.getClass());
}
}
diff --git a/src/test/java/org/apache/commons/crypto/random/DummyRandom.java b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
index 399b03b..4d1b4ac 100644
--- a/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
+++ b/src/test/java/org/apache/commons/crypto/random/DummyRandom.java
@@ -17,7 +17,6 @@
*/
package org.apache.commons.crypto.random;
-import java.io.IOException;
class DummyRandom implements CryptoRandom {
@@ -27,7 +26,7 @@ class DummyRandom implements CryptoRandom {
}
@Override
- public void close() throws IOException {
+ public void close() {
}
@Override
diff --git a/src/test/java/org/apache/commons/crypto/random/FailingRandom.java b/src/test/java/org/apache/commons/crypto/random/FailingRandom.java
index e362076..6600bc7 100644
--- a/src/test/java/org/apache/commons/crypto/random/FailingRandom.java
+++ b/src/test/java/org/apache/commons/crypto/random/FailingRandom.java
@@ -17,7 +17,6 @@
*/
package org.apache.commons.crypto.random;
-import java.io.IOException;
import java.util.Properties;
class FailingRandom implements CryptoRandom {
@@ -28,7 +27,7 @@ class FailingRandom implements CryptoRandom {
}
@Override
- public void close() throws IOException {
+ public void close() {
}
@Override
diff --git a/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java
index 8121b0c..cd6aaca 100644
--- a/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/JavaCryptoRandomTest.java
@@ -17,11 +17,11 @@
*/
package org.apache.commons.crypto.random;
-import static org.junit.Assert.assertTrue;
-
import java.security.GeneralSecurityException;
import java.util.Properties;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
public class JavaCryptoRandomTest extends AbstractRandomTest {
@Override
@@ -32,8 +32,8 @@ public class JavaCryptoRandomTest extends AbstractRandomTest {
JavaCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
assertTrue(
- "The CryptoRandom should be: " + JavaCryptoRandom.class.getName(),
- random instanceof JavaCryptoRandom);
+ random instanceof JavaCryptoRandom,
+ "The CryptoRandom should be: " + JavaCryptoRandom.class.getName());
return random;
}
diff --git a/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java
index cc543e3..add73f2 100644
--- a/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/OpenSslCryptoRandomTest.java
@@ -17,27 +17,28 @@
*/
package org.apache.commons.crypto.random;
-import static org.junit.Assert.assertTrue;
import java.security.GeneralSecurityException;
import java.util.Properties;
import org.apache.commons.crypto.Crypto;
-import org.junit.Assume;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class OpenSslCryptoRandomTest extends AbstractRandomTest {
@Override
public CryptoRandom getCryptoRandom() throws GeneralSecurityException {
- Assume.assumeTrue(Crypto.isNativeCodeLoaded());
+ assumeTrue(Crypto.isNativeCodeLoaded());
final Properties props = new Properties();
props.setProperty(
CryptoRandomFactory.CLASSES_KEY,
OpenSslCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
assertTrue(
- "The CryptoRandom should be: " + OpenSslCryptoRandom.class.getName(),
- random instanceof OpenSslCryptoRandom);
+ random instanceof OpenSslCryptoRandom,
+ "The CryptoRandom should be: " + OpenSslCryptoRandom.class.getName());
return random;
}
diff --git a/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java
index 28e1fe5..3f8b80d 100644
--- a/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java
+++ b/src/test/java/org/apache/commons/crypto/random/OsCryptoRandomTest.java
@@ -17,28 +17,30 @@
*/
package org.apache.commons.crypto.random;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import org.junit.jupiter.api.Test;
+
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.Properties;
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
public class OsCryptoRandomTest extends AbstractRandomTest {
@Override
public CryptoRandom getCryptoRandom() throws GeneralSecurityException {
// Windows does not have a /dev/random device
- Assume.assumeTrue(!System.getProperty("os.name").contains("Windows"));
+ assumeTrue(!System.getProperty("os.name").contains("Windows"));
final Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName());
final CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);
- assertTrue("The CryptoRandom should be: " + OsCryptoRandom.class.getName(), random instanceof OsCryptoRandom);
+ assertTrue(random instanceof OsCryptoRandom, "The CryptoRandom should be: " + OsCryptoRandom.class.getName());
return random;
}
@@ -48,19 +50,16 @@ public class OsCryptoRandomTest extends AbstractRandomTest {
props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName());
// Invalid device
props.setProperty(CryptoRandomFactory.DEVICE_FILE_PATH_KEY, "");
- try {
- CryptoRandomFactory.getCryptoRandom(props);
- fail("Expected GeneralSecurityException");
- } catch (final GeneralSecurityException e) {
- Throwable cause;
- cause = e.getCause();
- Assert.assertEquals(IllegalArgumentException.class, cause.getClass());
- cause = cause.getCause();
- Assert.assertEquals(InvocationTargetException.class, cause.getClass());
- cause = cause.getCause();
- Assert.assertEquals(IllegalArgumentException.class, cause.getClass());
- cause = cause.getCause();
- Assert.assertEquals(FileNotFoundException.class, cause.getClass());
- }
+ Exception e = assertThrows(GeneralSecurityException.class, () -> CryptoRandomFactory.getCryptoRandom(props));
+ Throwable cause;
+ cause = e.getCause();
+ assertEquals(IllegalArgumentException.class, cause.getClass());
+ cause = cause.getCause();
+ assertEquals(InvocationTargetException.class, cause.getClass());
+ cause = cause.getCause();
+ assertEquals(IllegalArgumentException.class, cause.getClass());
+ cause = cause.getCause();
+ assertEquals(FileNotFoundException.class, cause.getClass());
+
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
index 2266f91..5950fad 100644
--- a/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/AbstractCipherStreamTest.java
@@ -17,8 +17,6 @@
*/
package org.apache.commons.crypto.stream;
-import static org.junit.Assert.assertEquals;
-
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -34,6 +32,7 @@ import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Properties;
import java.util.Random;
+import java.util.concurrent.TimeUnit;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
@@ -43,9 +42,17 @@ import org.apache.commons.crypto.Crypto;
import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.utils.ReflectionUtils;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
public abstract class AbstractCipherStreamTest {
@@ -63,7 +70,7 @@ public abstract class AbstractCipherStreamTest {
public abstract void setUp() throws IOException;
- @Before
+ @BeforeEach
public void before() throws Exception {
final Random random = new SecureRandom();
random.nextBytes(data);
@@ -74,7 +81,8 @@ public abstract class AbstractCipherStreamTest {
}
/** Test skip. */
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testSkip() throws Exception {
doSkipTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
doSkipTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, false);
@@ -84,7 +92,8 @@ public abstract class AbstractCipherStreamTest {
}
/** Test byte buffer read with different buffer size. */
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testByteBufferRead() throws Exception {
doByteBufferRead(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
doByteBufferRead(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, false);
@@ -94,7 +103,8 @@ public abstract class AbstractCipherStreamTest {
}
/** Test byte buffer write. */
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testByteBufferWrite() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
doByteBufferWrite(AbstractCipherTest.JCE_CIPHER_CLASSNAME, baos, false);
@@ -104,7 +114,8 @@ public abstract class AbstractCipherStreamTest {
doByteBufferWrite(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, baos, true);
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testExceptions() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
doExceptionTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, baos, false);
@@ -114,7 +125,8 @@ public abstract class AbstractCipherStreamTest {
doExceptionTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, baos, true);
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testFieldGetters() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
doFieldGetterTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, baos, false);
@@ -138,28 +150,24 @@ public abstract class AbstractCipherStreamTest {
final byte[] result = new byte[dataLen];
final int n1 = readAll(in, result, 0, dataLen / 5);
- Assert.assertEquals(in.skip(0), 0);
+ assertEquals(in.skip(0), 0);
long skipped = in.skip(dataLen / 5);
final int n2 = readAll(in, result, 0, dataLen);
- Assert.assertEquals(dataLen, n1 + skipped + n2);
+ assertEquals(dataLen, n1 + skipped + n2);
final byte[] readData = new byte[n2];
System.arraycopy(result, 0, readData, 0, n2);
final byte[] expectedData = new byte[n2];
System.arraycopy(data, dataLen - n2, expectedData, 0, n2);
- Assert.assertArrayEquals(readData, expectedData);
+ assertArrayEquals(readData, expectedData);
- try {
- skipped = in.skip(-3);
- Assert.fail("Skip Negative length should fail.");
- } catch (final IllegalArgumentException e) {
- Assert.assertTrue(e.getMessage().contains("Negative skip length"));
- }
+ Exception e = assertThrows(IllegalArgumentException.class, () -> in.skip(-3));
+ assertTrue(e.getMessage().contains("Negative skip length"));
// Skip after EOF
skipped = in.skip(3);
- Assert.assertEquals(skipped, 0);
+ assertEquals(skipped, 0);
}
}
@@ -331,12 +339,12 @@ public abstract class AbstractCipherStreamTest {
new IvParameterSpec(iv), withChannel);
doByteBufferWrite(out, withChannel);
out.write(1);
- Assert.assertTrue(out.isOpen());
+ assertTrue(out.isOpen());
out = newCryptoOutputStream(transformation, props, baos, key,
new IvParameterSpec(iv), withChannel);
out.close();
- Assert.assertTrue(!out.isOpen());
+ assertFalse(out.isOpen());
}
protected void doExceptionTest(final String cipherClass, final ByteArrayOutputStream baos,
@@ -347,102 +355,68 @@ public abstract class AbstractCipherStreamTest {
}
}
- InputStream in = null;
- OutputStream out = null;
-
// Test InvalidAlgorithmParameters
- try {
- in = newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData),
- new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), withChannel);
- Assert.fail("Expected IOException.");
- } catch (final IOException ex) {
- Assert.assertEquals(ex.getMessage(),"Illegal parameters");
- }
-
+ Exception ex = assertThrows(IOException.class, () -> newCryptoInputStream(transformation, props, new ByteArrayInputStream(encData),
+ new SecretKeySpec(key, "AES"), new GCMParameterSpec(0, new byte[0]), withChannel));
+ assertEquals(ex.getMessage(),"Illegal parameters");
// Test InvalidAlgorithmParameters
- try {
- out = newCryptoOutputStream(transformation, props, baos,
- new SecretKeySpec(key, "AES"), new GCMParameterSpec(0,
- new byte[0]), withChannel);
- Assert.fail("Expected IOException.");
- } catch (final IOException ex) {
- Assert.assertEquals(ex.getMessage(),"Illegal parameters");
- }
+ ex = assertThrows(IOException.class, () -> newCryptoOutputStream(transformation, props, baos,
+ new SecretKeySpec(key, "AES"), new GCMParameterSpec(0,
+ new byte[0]), withChannel));
+ assertEquals(ex.getMessage(),"Illegal parameters");
// Test Invalid Key
- try {
- in = newCryptoInputStream(transformation,props, new ByteArrayInputStream(encData),
- new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel);
- Assert.fail("Expected IOException for Invalid Key");
- } catch (final IOException ex) {
- Assert.assertNotNull(ex);
- }
-
+ assertThrows(IOException.class, () -> newCryptoInputStream(transformation,props, new ByteArrayInputStream(encData),
+ new SecretKeySpec(new byte[10], "AES"), new IvParameterSpec(iv), withChannel));
// Test Invalid Key
- try {
- out = newCryptoOutputStream(transformation, props, baos, new byte[10],
- new IvParameterSpec(iv), withChannel);
- Assert.fail("Expected IOException for Invalid Key");
- } catch (final IOException ex) {
- Assert.assertNotNull(ex);
- }
+ assertThrows(IOException.class, () -> newCryptoOutputStream(transformation, props, baos, new byte[10],
+ new IvParameterSpec(iv), withChannel));
// Test reading a closed stream.
- try {
- in = newCryptoInputStream(new ByteArrayInputStream(encData),
- getCipher(cipherClass), defaultBufferSize, iv, withChannel);
- in.close();
- in.read(); // Throw exception.
- } catch (final IOException ex) {
- Assert.assertTrue(ex.getMessage().equals("Stream closed"));
- }
+ final InputStream in = newCryptoInputStream(new ByteArrayInputStream(encData),
+ getCipher(cipherClass), defaultBufferSize, iv, withChannel);
+ in.close();
+ // Throw exception.
+ ex = assertThrows(IOException.class, in::read);
+ assertEquals(ex.getMessage(), "Stream closed");
// Test closing a closed stream.
try {
in.close(); // Don't throw exception on double-close.
- } catch (final IOException ex) {
- Assert.fail("Should not throw exception closing a closed stream.");
+ } catch (final IOException ioEx) {
+ fail("Should not throw exception closing a closed stream.");
}
// Test checking a closed stream.
- try {
- out = newCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv),
- withChannel);
- out.close();
- ((CryptoOutputStream)out).checkStream(); // Throw exception.
- } catch (final IOException ex) {
- Assert.assertTrue(ex.getMessage().equals("Stream closed"));
- }
+ final OutputStream out = newCryptoOutputStream(transformation, props, baos, key, new IvParameterSpec(iv),
+ withChannel);
+ out.close();
+ // Throw exception.
+ assertThrows(IOException.class, ((CryptoOutputStream) out)::checkStream);
// Test closing a closed stream.
try {
out.close(); // Don't throw exception.
- } catch (final IOException ex) {
- Assert.fail("Should not throw exception closing a closed stream.");
+ } catch (final IOException ioEx) {
+ fail("Should not throw exception closing a closed stream.");
}
// Test checkStreamCipher
try {
CryptoInputStream.checkStreamCipher(getCipher(cipherClass));
- } catch (final IOException ex) {
- Assert.assertTrue(ex.getMessage().equals("AES/CTR/NoPadding is required"));
+ } catch (final IOException ioEx) {
+ assertEquals(ioEx.getMessage(), "AES/CTR/NoPadding is required");
} finally {
in.close();
}
// Test unsupported operation handling.
- try {
- in = newCryptoInputStream(new ByteArrayInputStream(encData),
- getCipher(cipherClass), defaultBufferSize, iv, false);
- in.mark(0);
- assertEquals(false, in.markSupported());
- in.reset();
- Assert.fail("Expected IOException.");
- } catch (final IOException ex) {
- Assert.assertTrue(ex.getMessage().equals("mark/reset not supported"));
- } finally {
- in.close();
- }
+ final InputStream inNewCrytptoStr = newCryptoInputStream(new ByteArrayInputStream(encData),
+ getCipher(cipherClass), defaultBufferSize, iv, false);
+ in.mark(0);
+ assertFalse(in.markSupported());
+ ex = assertThrows(IOException.class, inNewCrytptoStr::reset);
+ assertEquals(ex.getMessage(), "mark/reset not supported");
}
protected void doFieldGetterTest(final String cipherClass, final ByteArrayOutputStream baos,
@@ -463,40 +437,35 @@ public abstract class AbstractCipherStreamTest {
final String bufferSize = Integer.toString(defaultBufferSize / 2);
props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
- Assert.assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
- Assert.assertEquals(in.getBufferSize(), defaultBufferSize);
- Assert.assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
- Assert.assertEquals(in.getKey().getAlgorithm(), "AES");
- Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
- Assert.assertNotNull(in.getInput());
+ assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
+ assertEquals(in.getBufferSize(), defaultBufferSize);
+ assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
+ assertEquals(in.getKey().getAlgorithm(), "AES");
+ assertEquals(in.getParams().getClass(), IvParameterSpec.class);
+ assertNotNull(in.getInput());
final CryptoOutputStream out = newCryptoOutputStream(baos, getCipher(cipherClass),
defaultBufferSize, iv, withChannel);
- Assert.assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + cipher.getBlockSize());
- Assert.assertEquals(out.getInBuffer().capacity(), defaultBufferSize);
- Assert.assertEquals(out.getBufferSize(), defaultBufferSize);
+ assertEquals(out.getOutBuffer().capacity(), defaultBufferSize + cipher.getBlockSize());
+ assertEquals(out.getInBuffer().capacity(), defaultBufferSize);
+ assertEquals(out.getBufferSize(), defaultBufferSize);
}
private void byteBufferReadCheck(final InputStream in, final ByteBuffer buf, final int bufPos)
throws Exception {
buf.position(bufPos);
final int n = ((ReadableByteChannel) in).read(buf);
- Assert.assertEquals(bufPos + n, buf.position());
+ assertEquals(bufPos + n, buf.position());
final byte[] readData = new byte[n];
buf.rewind();
buf.position(bufPos);
buf.get(readData);
final byte[] expectedData = new byte[n];
System.arraycopy(data, 0, expectedData, 0, n);
- Assert.assertArrayEquals(readData, expectedData);
+ assertArrayEquals(readData, expectedData);
- try {
- in.read(readData, -1, 0);
- Assert.fail("Expected IndexOutOfBoundsException.");
- } catch (final IndexOutOfBoundsException ex) {
- Assert.assertNotNull(ex);
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> in.read(readData, -1, 0));
}
private void byteBufferFinalReadCheck(final InputStream in, final ByteBuffer buf, final int bufPos)
@@ -513,7 +482,7 @@ public abstract class AbstractCipherStreamTest {
buf.get(readData);
final byte[] expectedData = new byte[len + 1];
System.arraycopy(data, 0, expectedData, 0, len + 1);
- Assert.assertArrayEquals(readData, expectedData);
+ assertArrayEquals(readData, expectedData);
}
private void prepareData() throws IOException {
@@ -554,15 +523,9 @@ public abstract class AbstractCipherStreamTest {
out.write(1);
- Assert.assertEquals(dataLen, n1 + n2 + n3 + 1);
-
- try {
- out.write(data, 0, data.length + 1);
- Assert.fail("Expected IndexOutOfBoundsException.");
- } catch (final IndexOutOfBoundsException ex) {
- Assert.assertNotNull(ex);
- }
+ assertEquals(dataLen, n1 + n2 + n3 + 1);
+ assertThrows(IndexOutOfBoundsException.class, () -> out.write(data, 0, data.length + 1));
out.flush();
try (InputStream in = newCryptoInputStream(
@@ -732,8 +695,7 @@ public abstract class AbstractCipherStreamTest {
}
}
- Assert.assertArrayEquals("originalData and decryptedData not equal",
- originalData, decryptedData);
+ assertArrayEquals(originalData, decryptedData,"originalData and decryptedData not equal");
// Decrypt data byte-at-a-time
in = newCryptoInputStream(
@@ -746,8 +708,7 @@ public abstract class AbstractCipherStreamTest {
int expected;
do {
expected = originalIn.read();
- Assert.assertEquals("Decrypted stream read by byte does not match",
- expected, in.read());
+ assertEquals(expected, in.read(),"Decrypted stream read by byte does not match");
} while (expected != -1);
// Completed checking records;
@@ -800,8 +761,7 @@ public abstract class AbstractCipherStreamTest {
}
}
- Assert.assertArrayEquals("originalData and decryptedData not equal",
- originalData, decryptedData);
+ assertArrayEquals(originalData, decryptedData);
// Decrypt data byte-at-a-time
in = newCryptoInputStream(
@@ -814,8 +774,7 @@ public abstract class AbstractCipherStreamTest {
int expected;
do {
expected = originalIn.read();
- Assert.assertEquals("Decrypted stream read by byte does not match",
- expected, in.read());
+ assertEquals(expected, in.read());
} while (expected != -1);
// Completed checking records
diff --git a/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
index c6c92cf..1dc9976 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CbcNoPaddingCipherStreamTest.java
@@ -17,12 +17,10 @@
*/
package org.apache.commons.crypto.stream;
-import java.io.IOException;
-
public class CbcNoPaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CBC/NoPadding";
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
index f425b84..ea90283 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CbcPkcs5PaddingCipherStreamTest.java
@@ -17,12 +17,10 @@
*/
package org.apache.commons.crypto.stream;
-import java.io.IOException;
-
public class CbcPkcs5PaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CBC/PKCS5Padding";
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
index c640d72..cc8f584 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrCryptoStreamTest.java
@@ -24,6 +24,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Properties;
+import java.util.concurrent.TimeUnit;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
@@ -33,13 +34,18 @@ import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.stream.input.ChannelInput;
import org.apache.commons.crypto.stream.input.StreamInput;
import org.apache.commons.crypto.stream.output.ChannelOutput;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CTR/NoPadding";
}
@@ -97,34 +103,21 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
}
final StreamInput streamInput = new StreamInput(new ByteArrayInputStream(encData), 0);
- try {
- streamInput.seek(0);
- Assert.fail("Expected UnsupportedOperationException.");
- } catch (final UnsupportedOperationException ex) {
- Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
- }
- try {
- streamInput.read(0, new byte[0], 0, 0);
- Assert.fail("Expected UnsupportedOperationException.");
- } catch (final UnsupportedOperationException ex) {
- Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
- }
- Assert.assertEquals(streamInput.available(), encData.length);
+ Exception ex = assertThrows(UnsupportedOperationException.class, () -> streamInput.seek(0));
+ assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
+
+ ex = assertThrows(UnsupportedOperationException.class, () -> streamInput.read(0, new byte[0], 0, 0));
+ assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
+
+ assertEquals(streamInput.available(), encData.length);
final ChannelInput channelInput = new ChannelInput(Channels.newChannel(new ByteArrayInputStream(encData)));
- try {
- channelInput.seek(0);
- Assert.fail("Expected UnsupportedOperationException.");
- } catch (final UnsupportedOperationException ex) {
- Assert.assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
- }
- try {
- channelInput.read(0, new byte[0], 0, 0);
- Assert.fail("Expected UnsupportedOperationException.");
- } catch (final UnsupportedOperationException ex) {
- Assert.assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
- }
- Assert.assertEquals(channelInput.available(), 0);
+ ex = assertThrows(UnsupportedOperationException.class, () -> channelInput.seek(0));
+ assertEquals(ex.getMessage(), "Seek is not supported by this implementation");
+
+ ex = assertThrows(UnsupportedOperationException.class, () -> channelInput.read(0, new byte[0], 0, 0));
+ assertEquals(ex.getMessage(), "Positioned read is not supported by this implementation");
+ assertEquals(channelInput.available(), 0);
final CtrCryptoInputStream in = new CtrCryptoInputStream(channelInput, getCipher(cipherClass),
defaultBufferSize, key, iv);
@@ -134,13 +127,13 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
props.put(CryptoInputStream.STREAM_BUFFER_SIZE_KEY, bufferSize);
in.setStreamOffset(smallBufferSize);
- Assert.assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
- Assert.assertEquals(smallBufferSize, in.getStreamOffset());
- Assert.assertEquals(in.getBufferSize(), 8192);
- Assert.assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
- Assert.assertEquals(in.getKey().getAlgorithm(), "AES");
- Assert.assertEquals(in.getParams().getClass(), IvParameterSpec.class);
- Assert.assertNotNull(in.getInput());
+ assertEquals(CryptoInputStream.getBufferSize(props), Integer.parseInt(bufferSize));
+ assertEquals(smallBufferSize, in.getStreamOffset());
+ assertEquals(in.getBufferSize(), 8192);
+ assertEquals(in.getCipher().getClass(), Class.forName(cipherClass));
+ assertEquals(in.getKey().getAlgorithm(), "AES");
+ assertEquals(in.getParams().getClass(), IvParameterSpec.class);
+ assertNotNull(in.getInput());
in.close();
@@ -148,12 +141,13 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
Channels.newChannel(baos)), getCipher(cipherClass),
Integer.parseInt(bufferSize), key, iv);
out.setStreamOffset(smallBufferSize);
- Assert.assertEquals(out.getStreamOffset(), smallBufferSize);
+ assertEquals(out.getStreamOffset(), smallBufferSize);
out.close();
}
- @Test(timeout = 120000)
+ @Test
+ @Timeout(value = 120000, unit = TimeUnit.MILLISECONDS)
public void testDecrypt() throws Exception {
doDecryptTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
doDecryptTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, false);
@@ -176,14 +170,9 @@ public class CtrCryptoStreamTest extends AbstractCipherStreamTest {
final byte[] expectedData = new byte[dataLen];
buf.get(readData);
System.arraycopy(data, 0, expectedData, 0, dataLen);
- Assert.assertArrayEquals(readData, expectedData);
-
- try {
- in.decryptBuffer(buf);
- Assert.fail("Expected IOException.");
- } catch (final IOException ex) {
- Assert.assertEquals(ex.getCause().getClass(), ShortBufferException.class);
- }
+ assertArrayEquals(readData, expectedData);
+ Exception ex = assertThrows(IOException.class, () -> in.decryptBuffer(buf));
+ assertEquals(ex.getCause().getClass(), ShortBufferException.class);
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java b/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
index 878de93..e7358ac 100644
--- a/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/CtrNoPaddingCipherStreamTest.java
@@ -17,12 +17,10 @@
*/
package org.apache.commons.crypto.stream;
-import java.io.IOException;
-
public class CtrNoPaddingCipherStreamTest extends AbstractCipherStreamTest {
@Override
- public void setUp() throws IOException {
+ public void setUp() {
transformation = "AES/CTR/NoPadding";
}
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 5fa09fb..318d482 100644
--- a/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/PositionedCryptoInputStreamTest.java
@@ -36,10 +36,13 @@ import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.cipher.CryptoCipher;
import org.apache.commons.crypto.stream.input.Input;
import org.apache.commons.crypto.utils.ReflectionUtils;
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class PositionedCryptoInputStreamTest {
@@ -58,7 +61,7 @@ public class PositionedCryptoInputStreamTest {
private final String transformation = "AES/CTR/NoPadding";
- @Before
+ @BeforeEach
public void before() throws IOException {
final Random random = new SecureRandom();
random.nextBytes(testData);
@@ -68,7 +71,7 @@ public class PositionedCryptoInputStreamTest {
}
private void prepareData() throws IOException {
- CryptoCipher cipher = null;
+ final CryptoCipher cipher;
try {
cipher = (CryptoCipher) ReflectionUtils.newInstance(
ReflectionUtils.getClassByName(AbstractCipherTest.JCE_CIPHER_CLASSNAME), props,
@@ -107,7 +110,7 @@ public class PositionedCryptoInputStreamTest {
@Test
public void doTestJNI() throws Exception {
- Assume.assumeTrue(Crypto.isNativeCodeLoaded());
+ assumeTrue(Crypto.isNativeCodeLoaded());
testCipher(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME);
}
@@ -245,11 +248,7 @@ public class PositionedCryptoInputStreamTest {
throws Exception {
final PositionedCryptoInputStream in = getCryptoInputStream(
getCipher(cipherClass), bufferSize);
- try {
- in.seek(position);
- Assert.fail("Excepted exception for cannot seek to negative offset.");
- } catch (final IllegalArgumentException iae) {
- }
+ assertThrows(IllegalArgumentException.class, () -> in.seek(position));
in.close();
}
@@ -276,7 +275,7 @@ public class PositionedCryptoInputStreamTest {
try (PositionedCryptoInputStream in = getCryptoInputStream(getCipher(cipherClass), bufferSize)) {
final byte[] bytes = new byte[length];
final int n = in.read(position, bytes, 0, length);
- Assert.assertEquals(n, -1);
+ assertEquals(n, -1);
}
}
@@ -302,12 +301,7 @@ public class PositionedCryptoInputStreamTest {
final PositionedCryptoInputStream in = getCryptoInputStream(
getCipher(cipherClass), bufferSize);
final byte[] bytes = new byte[length];
- try {
- in.readFully(position, bytes, 0, length);
- Assert.fail("Expected IOException.");
- } catch (final IOException ioe) {
- // excepted exception
- }
+ assertThrows(IOException.class, () -> in.readFully(position, bytes, 0, length));
in.close();
in.close(); // Don't throw exception.
}
@@ -321,7 +315,7 @@ public class PositionedCryptoInputStreamTest {
System.arraycopy(data1, pos, expectedData, 0, length);
// get the real data
System.arraycopy(data2, 0, realData, 0, length);
- Assert.assertArrayEquals(expectedData, realData);
+ assertArrayEquals(expectedData, realData);
}
private CryptoCipher getCipher(final String cipherClass) throws IOException {
@@ -334,7 +328,7 @@ public class PositionedCryptoInputStreamTest {
}
}
- class PositionedInputForTest implements Input {
+ static class PositionedInputForTest implements Input {
final byte[] data;
long pos;
@@ -347,7 +341,7 @@ public class PositionedCryptoInputStreamTest {
}
@Override
- public int read(final ByteBuffer dst) throws IOException {
+ public int read(final ByteBuffer dst) {
final int remaining = (int) (count - pos);
if (remaining <= 0) {
return -1;
@@ -360,7 +354,7 @@ public class PositionedCryptoInputStreamTest {
}
@Override
- public long skip(long n) throws IOException {
+ public long skip(long n) {
if (n <= 0) {
return 0;
}
@@ -375,8 +369,7 @@ public class PositionedCryptoInputStreamTest {
}
@Override
- public int read(final long position, final byte[] buffer, final int offset, int length)
- throws IOException {
+ public int read(final long position, final byte[] buffer, final int offset, int length) {
Objects.requireNonNull(buffer, "buffer");
if (offset < 0 || length < 0
|| length > buffer.length - offset) {
@@ -411,11 +404,11 @@ public class PositionedCryptoInputStreamTest {
}
@Override
- public void close() throws IOException {
+ public void close() {
}
@Override
- public int available() throws IOException {
+ public int available() {
return (int) (count - pos);
}
}
diff --git a/src/test/java/org/apache/commons/crypto/stream/input/ChannelInputTest.java b/src/test/java/org/apache/commons/crypto/stream/input/ChannelInputTest.java
index 059ffcd..cb00e26 100644
--- a/src/test/java/org/apache/commons/crypto/stream/input/ChannelInputTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/input/ChannelInputTest.java
@@ -18,13 +18,15 @@
package org.apache.commons.crypto.stream.input;
-import static org.junit.Assert.assertEquals;
+import org.junit.jupiter.api.Test;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.channels.Channels;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
/**
* Tests {@link ChannelInput}.
diff --git a/src/test/java/org/apache/commons/crypto/stream/output/StreamOutputTest.java b/src/test/java/org/apache/commons/crypto/stream/output/StreamOutputTest.java
index a214719..3f6fb59 100644
--- a/src/test/java/org/apache/commons/crypto/stream/output/StreamOutputTest.java
+++ b/src/test/java/org/apache/commons/crypto/stream/output/StreamOutputTest.java
@@ -18,12 +18,14 @@
package org.apache.commons.crypto.stream.output;
-import static org.junit.Assert.assertEquals;
+import org.junit.jupiter.api.Test;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
/**
* Tests {@link StreamOutput}.
diff --git a/src/test/java/org/apache/commons/crypto/utils/EnumTest.java b/src/test/java/org/apache/commons/crypto/utils/EnumTest.java
index 9500075..392ba5e 100644
--- a/src/test/java/org/apache/commons/crypto/utils/EnumTest.java
+++ b/src/test/java/org/apache/commons/crypto/utils/EnumTest.java
@@ -21,7 +21,7 @@ import org.apache.commons.crypto.cipher.CryptoCipherFactory;
import org.apache.commons.crypto.cipher.CryptoCipherFactory.CipherProvider;
import org.apache.commons.crypto.random.CryptoRandomFactory;
import org.apache.commons.crypto.random.CryptoRandomFactory.RandomProvider;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Test the enums used to define the internal implementation classes
diff --git a/src/test/java/org/apache/commons/crypto/utils/UtilsTest.java b/src/test/java/org/apache/commons/crypto/utils/UtilsTest.java
index d17faf2..9c2a8ff 100644
--- a/src/test/java/org/apache/commons/crypto/utils/UtilsTest.java
+++ b/src/test/java/org/apache/commons/crypto/utils/UtilsTest.java
@@ -17,31 +17,33 @@
*/
package org.apache.commons.crypto.utils;
+import org.junit.jupiter.api.Test;
+
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
public class UtilsTest {
@Test
public void testSplitOmitEmptyLine() {
List<String> clazzNames = Utils.splitClassNames("", ",");
- Assert.assertEquals(Collections.<String> emptyList(), clazzNames);
+ assertEquals(Collections.<String> emptyList(), clazzNames);
clazzNames = Utils.splitClassNames("a,b", ",");
- Assert.assertEquals(Arrays.asList("a", "b"), clazzNames);
+ assertEquals(Arrays.asList("a", "b"), clazzNames);
clazzNames = Utils.splitClassNames("a,b,", ",");
- Assert.assertEquals(Arrays.asList("a", "b"), clazzNames);
+ assertEquals(Arrays.asList("a", "b"), clazzNames);
clazzNames = Utils.splitClassNames("a, b,", ",");
- Assert.assertEquals(Arrays.asList("a", "b"), clazzNames);
+ assertEquals(Arrays.asList("a", "b"), clazzNames);
}
@Test
public void testSplitNull() {
- Assert.assertEquals(Collections.<String> emptyList(), Utils.splitClassNames(null, ","));
+ assertEquals(Collections.<String> emptyList(), Utils.splitClassNames(null, ","));
}
@Test
@@ -51,6 +53,6 @@ public class UtilsTest {
"garbage.in",
"out");
final Properties allprops = Utils.getProperties(props);
- Assert.assertEquals(allprops.getProperty("garbage.in"), "out");
+ assertEquals(allprops.getProperty("garbage.in"), "out");
}
}