diff --git a/src/test/scala/tlschannel/CloseTest.java b/src/test/scala/tlschannel/CloseTest.java index d4c2a952..4081fb3d 100644 --- a/src/test/scala/tlschannel/CloseTest.java +++ b/src/test/scala/tlschannel/CloseTest.java @@ -45,13 +45,13 @@ void testTcpImmediateClose() throws InterruptedException, IOException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { clientGroup.plain.close(); assertFalse(clientGroup.tls.shutdownSent()); assertFalse(clientGroup.tls.shutdownReceived()); Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data))); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(-1, server.read(buffer)); assertFalse(serverGroup.tls.shutdownReceived()); @@ -84,14 +84,14 @@ void testTcpClose() throws InterruptedException, IOException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); clientGroup.plain.close(); assertFalse(clientGroup.tls.shutdownSent()); assertFalse(clientGroup.tls.shutdownReceived()); Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data))); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -128,14 +128,14 @@ void testClose() throws InterruptedException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); client.close(); assertTrue(clientGroup.tls.shutdownSent()); assertFalse(clientGroup.tls.shutdownReceived()); Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data))); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -172,14 +172,14 @@ void testCloseAndWait() throws InterruptedException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); client.close(); assertTrue(clientGroup.tls.shutdownReceived()); assertTrue(clientGroup.tls.shutdownSent()); Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data))); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -216,11 +216,11 @@ void testCloseAndWaitForever() throws IOException, InterruptedException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); client.close(); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -258,14 +258,14 @@ void testShutdownAndForget() throws InterruptedException, IOException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); assertFalse(clientGroup.tls.shutdown()); assertFalse(clientGroup.tls.shutdownReceived()); assertTrue(clientGroup.tls.shutdownSent()); Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data))); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -300,7 +300,7 @@ void testShutdownAndWait() throws IOException, InterruptedException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); // send first close_notify assertFalse(clientGroup.tls.shutdown()); @@ -312,7 +312,7 @@ void testShutdownAndWait() throws IOException, InterruptedException { assertTrue(clientGroup.tls.shutdownReceived()); assertTrue(clientGroup.tls.shutdownSent()); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); @@ -352,7 +352,7 @@ void testShutdownAndWaitForever() throws InterruptedException, IOException { SocketGroups.SocketGroup serverGroup = socketPair.server; ByteChannel client = clientGroup.external; ByteChannel server = serverGroup.external; - Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable clientFn = TestUtil.cannotFailRunnable(() -> { client.write(ByteBuffer.wrap(data)); // send first close_notify assertFalse(clientGroup.tls.shutdown()); @@ -362,7 +362,7 @@ void testShutdownAndWaitForever() throws InterruptedException, IOException { // wait for second close_notify Assertions.assertThrows(AsynchronousCloseException.class, () -> clientGroup.tls.shutdown()); }); - Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> { + Runnable serverFn = TestUtil.cannotFailRunnable(() -> { ByteBuffer buffer = ByteBuffer.allocate(1); assertEquals(1, server.read(buffer)); buffer.flip(); diff --git a/src/test/scala/tlschannel/ConcurrentTest.java b/src/test/scala/tlschannel/ConcurrentTest.java index 060164bf..50c8603b 100644 --- a/src/test/scala/tlschannel/ConcurrentTest.java +++ b/src/test/scala/tlschannel/ConcurrentTest.java @@ -70,7 +70,7 @@ public void testReadSide() throws IOException { } private void writerLoop(int size, char ch, SocketGroup socketGroup) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { logger.fine(() -> String.format("Starting writer loop, size: %s", size)); int bytesRemaining = size; byte[] bufferArray = new byte[bufferSize]; @@ -89,7 +89,7 @@ private void writerLoop(int size, char ch, SocketGroup socketGroup) { } private void readerLoop(int size, SocketGroup socketGroup) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { logger.fine(() -> String.format("Starting reader loop, size: %s", size)); byte[] readArray = new byte[bufferSize]; int bytesRemaining = size; @@ -105,7 +105,7 @@ private void readerLoop(int size, SocketGroup socketGroup) { } private void readerLoopUntilEof(SocketGroup socketGroup, AtomicLong accumulator) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { logger.fine("Starting reader loop"); byte[] readArray = new byte[bufferSize]; while (true) { diff --git a/src/test/scala/tlschannel/FailTest.java b/src/test/scala/tlschannel/FailTest.java index 55f10359..c8886a35 100644 --- a/src/test/scala/tlschannel/FailTest.java +++ b/src/test/scala/tlschannel/FailTest.java @@ -15,7 +15,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import tlschannel.helpers.SocketPairFactory; import tlschannel.helpers.SslContextFactory; -import tlschannel.helpers.TestJavaUtil; +import tlschannel.helpers.TestUtil; @TestInstance(Lifecycle.PER_CLASS) public class FailTest { @@ -41,7 +41,7 @@ public void testPlanToTls() throws IOException, InterruptedException { ServerTlsChannel serverChannel = serverChannelBuilder.build(); Runnable serverFn = () -> { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { ByteBuffer buffer = ByteBuffer.allocate(10000); assertThrows(SSLException.class, () -> serverChannel.read(buffer)); try { diff --git a/src/test/scala/tlschannel/InteroperabilityTest.java b/src/test/scala/tlschannel/InteroperabilityTest.java index 0437fd0c..8fda867b 100644 --- a/src/test/scala/tlschannel/InteroperabilityTest.java +++ b/src/test/scala/tlschannel/InteroperabilityTest.java @@ -35,7 +35,7 @@ public class InteroperabilityTest { private final int margin = random.nextInt(100); private void writerLoop(Writer writer, boolean renegotiate) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { int remaining = dataSize; while (remaining > 0) { if (renegotiate) writer.renegotiate(); @@ -47,7 +47,7 @@ private void writerLoop(Writer writer, boolean renegotiate) { } private void readerLoop(Reader reader) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { byte[] receivedData = new byte[dataSize + margin]; int remaining = dataSize; while (remaining > 0) { diff --git a/src/test/scala/tlschannel/helpers/Loops.java b/src/test/scala/tlschannel/helpers/Loops.java index 4aa6f7a3..24326cf9 100644 --- a/src/test/scala/tlschannel/helpers/Loops.java +++ b/src/test/scala/tlschannel/helpers/Loops.java @@ -102,7 +102,7 @@ public static void writerLoop( boolean scattering, boolean shutdown, boolean close) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { logger.fine(() -> String.format( "Starting writer loop, size: %s, scattering: %s, renegotiate: %s", size, scattering, renegotiate)); SplittableRandom random = new SplittableRandom(seed); @@ -138,7 +138,7 @@ public static void writerLoop( public static void readerLoop( int size, SocketGroup socketGroup, boolean gathering, boolean readEof, boolean close) { - TestJavaUtil.cannotFail(() -> { + TestUtil.cannotFail(() -> { logger.fine(() -> String.format("Starting reader loop. Size: $size, gathering: %s", gathering)); byte[] readArray = new byte[bufferSize]; int bytesRemaining = size; @@ -183,7 +183,7 @@ private static byte[] hash(int size) { } } - public static final Function expectedBytesHash = new TestJavaUtil.Memo<>(Loops::hash)::apply; + public static final Function expectedBytesHash = new TestUtil.Memo<>(Loops::hash)::apply; private static ByteBuffer[] multiWrap(ByteBuffer buffer) { return new ByteBuffer[] {ByteBuffer.allocate(0), buffer, ByteBuffer.allocate(0)}; diff --git a/src/test/scala/tlschannel/helpers/NonBlockingLoops.java b/src/test/scala/tlschannel/helpers/NonBlockingLoops.java index 6b55a133..b618de04 100644 --- a/src/test/scala/tlschannel/helpers/NonBlockingLoops.java +++ b/src/test/scala/tlschannel/helpers/NonBlockingLoops.java @@ -181,8 +181,7 @@ public static Report loop(List socketPairs, int dataSize, boolean re selector.select(); // block for (Endpoint endpoint : Stream.concat( - getSelectedEndpoints(selector), - TestJavaUtil.removeAndCollect(readyTaskSockets.iterator())) + getSelectedEndpoints(selector), TestUtil.removeAndCollect(readyTaskSockets.iterator())) .collect(Collectors.toList())) { try { if (endpoint instanceof WriterEndpoint) { diff --git a/src/test/scala/tlschannel/helpers/TestJavaUtil.java b/src/test/scala/tlschannel/helpers/TestUtil.java similarity index 74% rename from src/test/scala/tlschannel/helpers/TestJavaUtil.java rename to src/test/scala/tlschannel/helpers/TestUtil.java index 8fd53a37..8157b2c2 100644 --- a/src/test/scala/tlschannel/helpers/TestJavaUtil.java +++ b/src/test/scala/tlschannel/helpers/TestUtil.java @@ -3,13 +3,14 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.SplittableRandom; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; -public class TestJavaUtil { +public class TestUtil { public static Stream removeAndCollect(Iterator iterator) { List builder = new ArrayList<>(); @@ -25,7 +26,7 @@ public interface ExceptionalRunnable { void run() throws Exception; } - private static final Logger logger = Logger.getLogger(TestJavaUtil.class.getName()); + private static final Logger logger = Logger.getLogger(TestUtil.class.getName()); public static void cannotFail(ExceptionalRunnable exceptionalRunnable) { cannotFailRunnable(exceptionalRunnable).run(); @@ -64,4 +65,22 @@ public O apply(I i) { return cache.computeIfAbsent(i, f); } } + + public static void nextBytes(SplittableRandom random, byte[] bytes) { + nextBytes(random, bytes, bytes.length); + } + + public static void nextBytes(SplittableRandom random, byte[] bytes, int len) { + int i = 0; + while (i < len) { + int rnd = random.nextInt(); + int n = Math.min(len - i, Integer.SIZE / java.lang.Byte.SIZE); + while (n > 0) { + bytes[i] = (byte) rnd; + rnd >>= java.lang.Byte.SIZE; + n -= 1; + i += 1; + } + } + } } diff --git a/src/test/scala/tlschannel/helpers/TestUtil.scala b/src/test/scala/tlschannel/helpers/TestUtil.scala deleted file mode 100644 index 211c3b10..00000000 --- a/src/test/scala/tlschannel/helpers/TestUtil.scala +++ /dev/null @@ -1,27 +0,0 @@ -package tlschannel.helpers - -import java.util.SplittableRandom -import java.util.logging.Logger - -object TestUtil { - - val logger = Logger.getLogger(TestUtil.getClass.getName) - - def nextBytes(random: SplittableRandom, bytes: Array[Byte]): Unit = { - nextBytes(random, bytes, bytes.length) - } - - def nextBytes(random: SplittableRandom, bytes: Array[Byte], len: Int): Unit = { - var i = 0 - while (i < len) { - var rnd = random.nextInt() - var n = Math.min(len - i, Integer.SIZE / java.lang.Byte.SIZE) - while (n > 0) { - bytes(i) = rnd.toByte - rnd >>= java.lang.Byte.SIZE - n -= 1 - i += 1 - } - } - } -}