Replace expected Junit 4 @Test field with assertThatThrownBy (#3814)

* Fix two operand stack tests

Signed-off-by: Diego López León <dieguitoll@gmail.com>

* Remove unreachable assert

Signed-off-by: Diego López León <dieguitoll@gmail.com>

* Replace expected annotation field with assert

Signed-off-by: Diego López León <dieguitoll@gmail.com>

* Extract variables to let only a single call for assert

Signed-off-by: Diego López León <dieguitoll@gmail.com>

Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net>
This commit is contained in:
Diego López León
2022-05-11 19:08:31 -03:00
committed by GitHub
parent 3820b584b5
commit 2cf97a0e67
39 changed files with 331 additions and 199 deletions

View File

@@ -15,6 +15,7 @@
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.bouncycastle.util.Pack;
import org.junit.Before;
@@ -84,31 +85,40 @@ public class Blake2bfMessageDigestTest {
assertThat(messageDigest.digest()).isEqualTo(BLAKE2F_ALL_ZERO_NEGATIVE_ROUNDS);
}
@Test(expected = IllegalStateException.class)
@Test
public void throwsIfBufferUpdatedWithLessThat213Bytes() {
for (int i = 0; i < 212; i++) {
messageDigest.update((byte) 0);
}
messageDigest.digest();
assertThatThrownBy(() -> messageDigest.digest()).isInstanceOf(IllegalStateException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void throwsIfBufferUpdatedWithMoreThat213Bytes() {
for (int i = 0; i < 214; i++) {
messageDigest.update((byte) 0);
}
assertThatThrownBy(
() -> {
for (int i = 0; i < 214; i++) {
messageDigest.update((byte) 0);
}
})
.isInstanceOf(IllegalArgumentException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void throwsIfBufferUpdatedLargeByteArray() {
final byte[] update = new byte[213];
messageDigest.update((byte) 0);
messageDigest.update(update, 0, 213);
assertThatThrownBy(() -> messageDigest.update(update, 0, 213))
.isInstanceOf(IllegalArgumentException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void throwsIfEmptyBufferUpdatedLargeByteArray() {
final byte[] update = new byte[214];
messageDigest.update(update, 0, 214);
assertThatThrownBy(
() -> {
final byte[] update = new byte[214];
messageDigest.update(update, 0, 214);
})
.isInstanceOf(IllegalArgumentException.class);
}
}

View File

@@ -16,6 +16,7 @@ package org.hyperledger.besu.crypto;
import static org.apache.tuweni.bytes.Bytes.fromHexString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.math.BigInteger;
import java.security.KeyPairGenerator;
@@ -52,14 +53,17 @@ public class KeyPairTest {
curve = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
}
@Test(expected = NullPointerException.class)
@Test
public void createKeyPair_PublicKeyNull() {
new KeyPair(null, SECPPublicKey.create(Bytes.wrap(new byte[64]), ALGORITHM));
final SECPPublicKey publicKey = SECPPublicKey.create(Bytes.wrap(new byte[64]), ALGORITHM);
assertThatThrownBy(() -> new KeyPair(null, publicKey)).isInstanceOf(NullPointerException.class);
}
@Test(expected = NullPointerException.class)
@Test
public void createKeyPair_PrivateKeyNull() {
new KeyPair(SECPPrivateKey.create(Bytes32.wrap(new byte[32]), ALGORITHM), null);
final SECPPrivateKey privateKey = SECPPrivateKey.create(Bytes32.wrap(new byte[32]), ALGORITHM);
assertThatThrownBy(() -> new KeyPair(privateKey, null))
.isInstanceOf(NullPointerException.class);
}
@Test

View File

@@ -15,6 +15,7 @@
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.File;
@@ -30,9 +31,10 @@ public class KeyPairUtilTest {
.isNotNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void shouldNotLoadInvalidKeyPair() throws Exception {
KeyPairUtil.loadKeyPair(
new File(this.getClass().getResource("/invalidPrivateKey.txt").toURI()));
final File keyFile = new File(this.getClass().getResource("/invalidPrivateKey.txt").toURI());
assertThatThrownBy(() -> KeyPairUtil.loadKeyPair(keyFile))
.isInstanceOf(IllegalArgumentException.class);
}
}

View File

@@ -16,6 +16,7 @@ package org.hyperledger.besu.crypto;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hyperledger.besu.crypto.Hash.keccak256;
import java.io.File;
@@ -115,19 +116,21 @@ public class SECP256K1Test {
assertThat(secp256K1.verify(data, signature, keyPair.getPublicKey(), Hash::keccak256)).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void invalidFileThrowsInvalidKeyPairException() throws Exception {
final File tempFile = Files.createTempFile(suiteName(), ".keypair").toFile();
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), "not valid".getBytes(UTF_8));
KeyPairUtil.load(tempFile);
assertThatThrownBy(() -> KeyPairUtil.load(tempFile))
.isInstanceOf(IllegalArgumentException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void invalidMultiLineFileThrowsInvalidIdException() throws Exception {
final File tempFile = Files.createTempFile(suiteName(), ".keypair").toFile();
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), "not\n\nvalid".getBytes(UTF_8));
KeyPairUtil.load(tempFile);
assertThatThrownBy(() -> KeyPairUtil.load(tempFile))
.isInstanceOf(IllegalArgumentException.class);
}
}

View File

@@ -16,6 +16,7 @@ package org.hyperledger.besu.crypto;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hyperledger.besu.crypto.Hash.keccak256;
import java.io.File;
@@ -163,20 +164,22 @@ public class SECP256R1Test {
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void invalidFileThrowsInvalidKeyPairException() throws Exception {
final File tempFile = Files.createTempFile(suiteName(), ".keypair").toFile();
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), "not valid".getBytes(UTF_8));
KeyPairUtil.load(tempFile);
assertThatThrownBy(() -> KeyPairUtil.load(tempFile))
.isInstanceOf(IllegalArgumentException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void invalidMultiLineFileThrowsInvalidIdException() throws Exception {
final File tempFile = Files.createTempFile(suiteName(), ".keypair").toFile();
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), "not\n\nvalid".getBytes(UTF_8));
KeyPairUtil.load(tempFile);
assertThatThrownBy(() -> KeyPairUtil.load(tempFile))
.isInstanceOf(IllegalArgumentException.class);
}
private static class SignTestVector {

View File

@@ -15,6 +15,7 @@
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.File;
import java.math.BigInteger;
@@ -54,9 +55,10 @@ public class SECPPrivateKeyTest {
suiteName = clazz.getSimpleName() + "-" + suiteStartTime;
}
@Test(expected = NullPointerException.class)
@Test
public void createPrivateKey_NullEncoding() {
SECPPrivateKey.create((Bytes32) null, ALGORITHM);
assertThatThrownBy(() -> SECPPrivateKey.create((Bytes32) null, ALGORITHM))
.isInstanceOf(NullPointerException.class);
}
@Test

View File

@@ -16,6 +16,7 @@ package org.hyperledger.besu.crypto;
import static org.apache.tuweni.bytes.Bytes.fromHexString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.math.BigInteger;
@@ -38,19 +39,24 @@ public class SECPPublicKeyTest {
curve = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
}
@Test(expected = NullPointerException.class)
@Test
public void createPublicKey_NullEncoding() {
SECPPublicKey.create((Bytes) null, ALGORITHM);
assertThatThrownBy(() -> SECPPublicKey.create((Bytes) null, ALGORITHM))
.isInstanceOf(NullPointerException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createPublicKey_EncodingTooShort() {
SECPPublicKey.create(Bytes.wrap(new byte[63]), ALGORITHM);
final Bytes publicKey = Bytes.wrap(new byte[63]);
assertThatThrownBy(() -> SECPPublicKey.create(publicKey, ALGORITHM))
.isInstanceOf(IllegalArgumentException.class);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createPublicKey_EncodingTooLong() {
SECPPublicKey.create(Bytes.wrap(new byte[65]), ALGORITHM);
final Bytes publicKey = Bytes.wrap(new byte[65]);
assertThatThrownBy(() -> SECPPublicKey.create(publicKey, ALGORITHM))
.isInstanceOf(IllegalArgumentException.class);
}
@Test

View File

@@ -15,6 +15,7 @@
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.math.BigInteger;
@@ -46,13 +47,15 @@ public class SECPSignatureTest {
assertThat(signature.getRecId()).isEqualTo((byte) 0);
}
@Test(expected = NullPointerException.class)
@Test
public void createSignature_NoR() {
SECPSignature.create(null, BigInteger.ZERO, (byte) 27, curveOrder);
assertThatThrownBy(() -> SECPSignature.create(null, BigInteger.ZERO, (byte) 27, curveOrder))
.isInstanceOf(NullPointerException.class);
}
@Test(expected = NullPointerException.class)
@Test
public void createSignature_NoS() {
SECPSignature.create(BigInteger.ZERO, null, (byte) 27, curveOrder);
assertThatThrownBy(() -> SECPSignature.create(BigInteger.ZERO, null, (byte) 27, curveOrder))
.isInstanceOf(NullPointerException.class);
}
}

View File

@@ -15,6 +15,7 @@
package org.hyperledger.besu.crypto;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import org.junit.Before;
import org.junit.Test;
@@ -42,11 +43,13 @@ public class SignatureAlgorithmFactoryTest {
.isEqualTo(SECP256K1.class.getSimpleName());
}
@Test(expected = RuntimeException.class)
@Test
public void shouldThrowExceptionWhenSetMoreThanOnce() {
SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1"));
assertThat(SignatureAlgorithmFactory.isInstanceSet()).isTrue();
SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1"));
assertThatThrownBy(
() -> SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1")))
.isInstanceOf(RuntimeException.class);
}
}