From 7c6e351a3764cb9834a8095d40d9fa49624484ca Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 2 Jan 2025 14:45:21 +0000 Subject: [PATCH] Remove reflection from tests that's not allowed by Java 17. --- .../crypto/NeitherSecureNorRandom.java | 45 ++++++++++++++++++ .../bramble/crypto/PseudoRandom.java | 34 -------------- .../bramble/crypto/PseudoSecureRandom.java | 46 ------------------- .../KeyAgreementTransportTest.java | 14 ++---- .../bramble/plugin/PollerImplTest.java | 15 ++---- 5 files changed, 55 insertions(+), 99 deletions(-) create mode 100644 bramble-core/src/test/java/org/briarproject/bramble/crypto/NeitherSecureNorRandom.java delete mode 100644 bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoRandom.java delete mode 100644 bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoSecureRandom.java diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/NeitherSecureNorRandom.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/NeitherSecureNorRandom.java new file mode 100644 index 000000000..3868efca8 --- /dev/null +++ b/bramble-core/src/test/java/org/briarproject/bramble/crypto/NeitherSecureNorRandom.java @@ -0,0 +1,45 @@ +package org.briarproject.bramble.crypto; + +import java.security.Provider; +import java.security.SecureRandom; +import java.security.SecureRandomSpi; + +import static java.util.Arrays.fill; + +/** + * A fake SecureRandom implementation for testing, which returns all zeroes. + */ +public class NeitherSecureNorRandom extends SecureRandom { + + private static final Provider PROVIDER = + new NeitherSecureNorRandomProvider(); + + public NeitherSecureNorRandom() { + super(new NeitherSecureNorRandomSpi(), PROVIDER); + } + + private static class NeitherSecureNorRandomSpi extends SecureRandomSpi { + + @Override + protected byte[] engineGenerateSeed(int length) { + return new byte[length]; + } + + @Override + protected void engineNextBytes(byte[] b) { + fill(b, (byte) 0); + } + + @Override + protected void engineSetSeed(byte[] seed) { + // Thank you for your input + } + } + + private static class NeitherSecureNorRandomProvider extends Provider { + + private NeitherSecureNorRandomProvider() { + super("NeitherSecureNorRandom", 1.0, "Only for testing"); + } + } +} diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoRandom.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoRandom.java deleted file mode 100644 index 235aa94a5..000000000 --- a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoRandom.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.briarproject.bramble.crypto; - -import org.bouncycastle.crypto.Digest; -import org.bouncycastle.crypto.digests.Blake2bDigest; -import org.bouncycastle.crypto.engines.Salsa20Engine; -import org.bouncycastle.crypto.params.KeyParameter; -import org.bouncycastle.crypto.params.ParametersWithIV; -import org.briarproject.nullsafety.NotNullByDefault; - -import javax.annotation.concurrent.NotThreadSafe; - -@NotThreadSafe -@NotNullByDefault -class PseudoRandom { - - private final Salsa20Engine cipher = new Salsa20Engine(); - - PseudoRandom(byte[] seed) { - // Hash the seed to produce a 32-byte key - byte[] key = new byte[32]; - Digest digest = new Blake2bDigest(256); - digest.update(seed, 0, seed.length); - digest.doFinal(key, 0); - // Initialise the stream cipher with an all-zero nonce - byte[] nonce = new byte[8]; - cipher.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); - } - - byte[] nextBytes(int length) { - byte[] in = new byte[length], out = new byte[length]; - cipher.processBytes(in, 0, length, out, 0); - return out; - } -} diff --git a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoSecureRandom.java b/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoSecureRandom.java deleted file mode 100644 index 5e75bf20e..000000000 --- a/bramble-core/src/test/java/org/briarproject/bramble/crypto/PseudoSecureRandom.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.briarproject.bramble.crypto; - -import java.security.Provider; -import java.security.SecureRandom; -import java.security.SecureRandomSpi; - -class PseudoSecureRandom extends SecureRandom { - - private static final Provider PROVIDER = new PseudoSecureRandomProvider(); - - PseudoSecureRandom(byte[] seed) { - super(new PseudoSecureRandomSpi(seed), PROVIDER); - } - - private static class PseudoSecureRandomSpi extends SecureRandomSpi { - - private final PseudoRandom pseudoRandom; - - private PseudoSecureRandomSpi(byte[] seed) { - pseudoRandom = new PseudoRandom(seed); - } - - @Override - protected byte[] engineGenerateSeed(int length) { - return pseudoRandom.nextBytes(length); - } - - @Override - protected void engineNextBytes(byte[] b) { - byte[] random = pseudoRandom.nextBytes(b.length); - System.arraycopy(random, 0, b, 0, b.length); - } - - @Override - protected void engineSetSeed(byte[] seed) { - // Thank you for your input - } - } - - private static class PseudoSecureRandomProvider extends Provider { - - private PseudoSecureRandomProvider() { - super("PseudoSecureRandom", 1.0, "Only for testing"); - } - } -} diff --git a/bramble-core/src/test/java/org/briarproject/bramble/keyagreement/KeyAgreementTransportTest.java b/bramble-core/src/test/java/org/briarproject/bramble/keyagreement/KeyAgreementTransportTest.java index b1708e859..d94049c6f 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/keyagreement/KeyAgreementTransportTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/keyagreement/KeyAgreementTransportTest.java @@ -15,9 +15,10 @@ import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.CaptureArgumentAction; import org.briarproject.bramble.test.PredicateMatcher; import org.jmock.Expectations; -import org.jmock.imposters.ByteBuddyClassImposteriser; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.concurrent.atomic.AtomicReference; @@ -51,17 +52,12 @@ public class KeyAgreementTransportTest extends BrambleMockTestCase { private final KeyAgreementConnection keyAgreementConnection = new KeyAgreementConnection(duplexTransportConnection, transportId); - private final InputStream inputStream; - private final OutputStream outputStream; + private final InputStream inputStream = + new ByteArrayInputStream(new byte[0]); + private final OutputStream outputStream = new ByteArrayOutputStream(); private KeyAgreementTransport kat; - public KeyAgreementTransportTest() { - context.setImposteriser(ByteBuddyClassImposteriser.INSTANCE); - inputStream = context.mock(InputStream.class); - outputStream = context.mock(OutputStream.class); - } - @Test public void testSendKey() throws Exception { byte[] key = getRandomBytes(123); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java index 2e4930b21..397d1df43 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java @@ -21,11 +21,11 @@ import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportPropertyManager; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.crypto.NeitherSecureNorRandom; import org.briarproject.bramble.test.BrambleMockTestCase; import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.bramble.test.RunAction; import org.jmock.Expectations; -import org.jmock.imposters.ByteBuddyClassImposteriser; import org.junit.Test; import java.security.SecureRandom; @@ -55,7 +55,6 @@ public class PollerImplTest extends BrambleMockTestCase { context.mock(TransportPropertyManager.class); private final Clock clock = context.mock(Clock.class); private final Cancellable cancellable = context.mock(Cancellable.class); - private final SecureRandom random; private final Executor ioExecutor = new ImmediateExecutor(); private final TransportId transportId = getTransportId(); @@ -67,8 +66,8 @@ public class PollerImplTest extends BrambleMockTestCase { private final PollerImpl poller; public PollerImplTest() { - context.setImposteriser(ByteBuddyClassImposteriser.INSTANCE); - random = context.mock(SecureRandom.class); + // Use a fake SecureRandom that returns all zeroes + SecureRandom random = new NeitherSecureNorRandom(); Executor wakefulIoExecutor = new ImmediateExecutor(); poller = new PollerImpl(ioExecutor, wakefulIoExecutor, scheduler, connectionManager, connectionRegistry, pluginManager, @@ -352,12 +351,10 @@ public class PollerImplTest extends BrambleMockTestCase { // Running the polling task schedules the next polling task oneOf(plugin).getPollingInterval(); will(returnValue(pollingInterval)); - oneOf(random).nextDouble(); - will(returnValue(0.5)); oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) (pollingInterval * 0.5)), + with(ioExecutor), with(0L), with(MILLISECONDS)); will(returnValue(cancellable)); // Get the transport properties and connected contacts @@ -396,12 +393,10 @@ public class PollerImplTest extends BrambleMockTestCase { // Running the polling task schedules the next polling task oneOf(plugin).getPollingInterval(); will(returnValue(pollingInterval)); - oneOf(random).nextDouble(); - will(returnValue(0.5)); oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) (pollingInterval * 0.5)), + with(ioExecutor), with(0L), with(MILLISECONDS)); will(returnValue(cancellable)); // Get the transport properties and connected contacts