From 567bef3b413ba0202d5aacaa8accd7ceb367f520 Mon Sep 17 00:00:00 2001 From: holt666 Date: Sun, 14 May 2023 23:42:40 +0800 Subject: [PATCH 1/2] remove BouncyCastle provider --- src/test/java/io/xdag/consensus/SyncTest.java | 4 ---- src/test/java/io/xdag/core/BlockchainTest.java | 4 ---- src/test/java/io/xdag/crypto/Bip32Test.java | 6 +----- src/test/java/io/xdag/crypto/Libp2pCryptoTest.java | 4 ---- src/test/java/io/xdag/wallet/WalletUtilsTest.java | 4 ---- 5 files changed, 1 insertion(+), 21 deletions(-) diff --git a/src/test/java/io/xdag/consensus/SyncTest.java b/src/test/java/io/xdag/consensus/SyncTest.java index b366245a..51d6b683 100644 --- a/src/test/java/io/xdag/consensus/SyncTest.java +++ b/src/test/java/io/xdag/consensus/SyncTest.java @@ -55,12 +55,10 @@ import io.xdag.utils.XdagTime; import io.xdag.Wallet; import java.math.BigInteger; -import java.security.Security; import java.util.Collections; import java.util.List; import org.apache.tuweni.bytes.Bytes32; import org.apache.tuweni.bytes.MutableBytes32; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SECPPrivateKey; import org.junit.Before; @@ -70,8 +68,6 @@ public class SyncTest { - static { Security.addProvider(new BouncyCastleProvider()); } - @Rule public TemporaryFolder root1 = new TemporaryFolder(); @Rule diff --git a/src/test/java/io/xdag/core/BlockchainTest.java b/src/test/java/io/xdag/core/BlockchainTest.java index 8f9edda6..40373a6a 100644 --- a/src/test/java/io/xdag/core/BlockchainTest.java +++ b/src/test/java/io/xdag/core/BlockchainTest.java @@ -68,12 +68,10 @@ import io.xdag.Wallet; import java.io.IOException; import java.math.BigInteger; -import java.security.Security; import java.util.Collections; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.tuweni.bytes.Bytes32; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SECPPrivateKey; import org.junit.After; @@ -85,8 +83,6 @@ @Slf4j public class BlockchainTest { - static { Security.addProvider(new BouncyCastleProvider()); } - @Rule public TemporaryFolder root = new TemporaryFolder(); diff --git a/src/test/java/io/xdag/crypto/Bip32Test.java b/src/test/java/io/xdag/crypto/Bip32Test.java index 3ba6d4f4..6d091fce 100644 --- a/src/test/java/io/xdag/crypto/Bip32Test.java +++ b/src/test/java/io/xdag/crypto/Bip32Test.java @@ -31,23 +31,19 @@ import io.xdag.utils.Numeric; import java.nio.ByteBuffer; -import java.security.Security; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes32; import org.apache.tuweni.io.Base58; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.junit.Test; /** * BIP-32 implementation test. * *

Test vectors taken from BIP-32 definition - * https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki + * bip32 */ public class Bip32Test { - static { Security.addProvider(new BouncyCastleProvider()); } - public static Bytes addChecksum(byte[] input) { int inputLength = input.length; byte[] checksummed = new byte[inputLength + 4]; diff --git a/src/test/java/io/xdag/crypto/Libp2pCryptoTest.java b/src/test/java/io/xdag/crypto/Libp2pCryptoTest.java index f0709c82..f9f49ab3 100644 --- a/src/test/java/io/xdag/crypto/Libp2pCryptoTest.java +++ b/src/test/java/io/xdag/crypto/Libp2pCryptoTest.java @@ -30,16 +30,12 @@ import io.libp2p.core.crypto.PubKey; import io.libp2p.crypto.keys.Secp256k1Kt; import io.xdag.utils.Numeric; -import java.security.Security; import org.apache.tuweni.bytes.Bytes; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.junit.Before; import org.junit.Test; public class Libp2pCryptoTest { - static { Security.addProvider(new BouncyCastleProvider()); } - private PrivKey libp2pPrivKey; private PubKey libp2pPubKey; diff --git a/src/test/java/io/xdag/wallet/WalletUtilsTest.java b/src/test/java/io/xdag/wallet/WalletUtilsTest.java index e968718a..96a989bf 100644 --- a/src/test/java/io/xdag/wallet/WalletUtilsTest.java +++ b/src/test/java/io/xdag/wallet/WalletUtilsTest.java @@ -41,10 +41,8 @@ import io.xdag.utils.WalletUtils; import java.io.IOException; -import java.security.Security; import java.util.Collections; import org.apache.tuweni.io.Base58; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.hyperledger.besu.crypto.KeyPair; import org.junit.After; import org.junit.Before; @@ -54,8 +52,6 @@ public class WalletUtilsTest { - static { Security.addProvider(new BouncyCastleProvider()); } - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); private String pwd; From 90bc01574a2ad2ab779b8e7cda91b7067c24ffef Mon Sep 17 00:00:00 2001 From: holt666 Date: Sun, 14 May 2023 23:43:13 +0800 Subject: [PATCH 2/2] use system-lambda replace system-rules --- pom.xml | 6 +-- src/test/java/io/xdag/cli/XdagCliTest.java | 50 ++++++++++------------ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 9ce089c4..2cf1c1d1 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ 3.23.0 4.13.2 5.2.0 - 1.19.0 + 1.2.1 2.20.0 **/*RandomXSyncTest.java,**/*SyncTest.java,**/*SnapshotJTest.java @@ -579,8 +579,8 @@ com.github.stefanbirkner - system-rules - ${system-rules.version} + system-lambda + ${system-lambda.version} test diff --git a/src/test/java/io/xdag/cli/XdagCliTest.java b/src/test/java/io/xdag/cli/XdagCliTest.java index ac7e9aef..cd813094 100644 --- a/src/test/java/io/xdag/cli/XdagCliTest.java +++ b/src/test/java/io/xdag/cli/XdagCliTest.java @@ -24,7 +24,11 @@ package io.xdag.cli; +import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit; +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut; import static io.xdag.utils.WalletUtils.WALLET_PASSWORD_PROMPT; +import static java.lang.System.setErr; +import static java.lang.System.setOut; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -48,40 +52,25 @@ import io.xdag.utils.BytesUtils; import io.xdag.Wallet; -import java.security.Security; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.hyperledger.besu.crypto.KeyPair; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.ExpectedSystemExit; -import org.junit.contrib.java.lang.system.SystemErrRule; -import org.junit.contrib.java.lang.system.SystemOutRule; import org.mockito.Mockito; import com.google.common.collect.Lists; public class XdagCliTest { - - static { Security.addProvider(new BouncyCastleProvider()); } - - @Rule - public final ExpectedSystemExit exit = ExpectedSystemExit.none(); - @Rule - public final SystemOutRule outRule = new SystemOutRule(); - @Rule - public final SystemErrRule errRule = new SystemErrRule(); private Config config; @Before public void setUp() throws Exception { config = new DevnetConfig(); - outRule.mute(); - errRule.mute(); } @Test @@ -94,10 +83,11 @@ public void testMain() throws Exception { @Test public void testHelp() throws Exception { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setOut(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.start(new String[]{"--help"}); - outRule.enableLog(); - xdagCLI.printHelp(); + String helpStr = """ usage: ./xdag.sh [options] --account init|create|list @@ -112,16 +102,16 @@ public void testHelp() throws Exception { --password wallet password --version show version """; - assertEquals(helpStr, outRule.getLog()); + assertEquals(helpStr, tapSystemOut(xdagCLI::printHelp)); } @Test public void testVersion() throws Exception { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setOut(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.start(new String[]{"--version"}); - outRule.enableLog(); - xdagCLI.printVersion(); - assertEquals(Constants.CLIENT_VERSION + "\n", outRule.getLog()); + assertEquals(Constants.CLIENT_VERSION + "\n", tapSystemOut(xdagCLI::printVersion)); } @Test @@ -258,6 +248,8 @@ public void testStartKernelWithEmptyWallet() throws Exception { @Test public void testStartKernelWithEmptyWalletInvalidNewPassword() throws Exception { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setErr(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.setConfig(config); // mock wallet @@ -271,12 +263,8 @@ public void testStartKernelWithEmptyWalletInvalidNewPassword() throws Exception // mock password doReturn("a").doReturn("b").when(xdagCLI).readPassword(any()); - // mock exits - doNothing().when(xdagCLI).exit(anyInt()); - - exit.expectSystemExitWithStatus(-1); // execution - xdagCLI.start(); + assertEquals(-1, catchSystemExit(xdagCLI::start)); } @Test @@ -308,6 +296,8 @@ public void testAccountList() throws Exception { @Test public void testCreateAccount() throws Exception { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setOut(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.setConfig(config); // mock wallet @@ -364,6 +354,8 @@ public void testListAccounts() throws Exception { @Test public void testChangePasswordIncorrectConfirmation() { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setErr(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.setConfig(config); @@ -487,6 +479,8 @@ public void testImportPrivateKeyFailedToFlushWalletFile() throws Exception { @Test public void testImportPrivateKey() throws Exception { + ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream(); + setOut(new PrintStream(captureOutputStream)); XdagCli xdagCLI = spy(new XdagCli()); xdagCLI.setConfig(config);