Skip to content

Commit

Permalink
Remove use of nulls with Optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed May 10, 2024
1 parent d0c2519 commit ac3c4f8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/test/java/tlschannel/NullEngineTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tlschannel;

import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -45,7 +47,7 @@ public Collection<DynamicTest> testHalfDuplexHeapBuffers() {
for (int size1 : sizes) {
DynamicTest test = DynamicTest.dynamicTest(String.format("Testing sizes: size1=%s", size1), () -> {
SocketGroups.SocketPair socketPair = factory.nioNio(
null,
Optional.of(NULL_CIPHER),
Optional.of(new ChunkSizeConfig(
new ChuckSizes(Optional.of(size1), Optional.empty()),
new ChuckSizes(Optional.of(size1), Optional.empty()))),
Expand All @@ -70,7 +72,7 @@ public Collection<DynamicTest> testHalfDuplexDirectBuffers() {
for (int size1 : sizes) {
DynamicTest test = DynamicTest.dynamicTest(String.format("Testing sizes: size1=%s", size1), () -> {
SocketGroups.SocketPair socketPair = factory.nioNio(
null,
Optional.of(NULL_CIPHER),
Optional.of(new ChunkSizeConfig(
new ChuckSizes(Optional.of(size1), Optional.empty()),
new ChuckSizes(Optional.of(size1), Optional.empty()))),
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/tlschannel/NullMultiNonBlockingTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tlschannel;

import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -25,8 +27,8 @@ public class NullMultiNonBlockingTest {

@Test
public void testRunTasksInNonBlockingLoop() {
List<SocketPair> pairs =
factory.nioNioN(null, totalConnections, Optional.empty(), true, false, Optional.empty());
List<SocketPair> pairs = factory.nioNioN(
Optional.of(NULL_CIPHER), totalConnections, Optional.empty(), true, false, Optional.empty());
NonBlockingLoops.Report report = NonBlockingLoops.loop(pairs, dataSize, false);
Assertions.assertEquals(0, report.asyncTasksRun);
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/tlschannel/async/AsyncCloseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -37,7 +39,7 @@ public class AsyncCloseTest implements AsyncTestBase {
public void testClosingWhileReading() throws IOException, InterruptedException {
for (int i = 0; i < repetitions; i++) {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
AsyncSocketPair socketPair = factory.async(Optional.of(NULL_CIPHER), channelGroup, true, false);

ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize);
Future<Integer> readFuture = socketPair.server.external.read(readBuffer);
Expand Down Expand Up @@ -75,7 +77,7 @@ public void testClosingWhileReading() throws IOException, InterruptedException {
public void testRawClosingWhileReading() throws IOException, InterruptedException {
for (int i = 0; i < repetitions; i++) {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
AsyncSocketPair socketPair = factory.async(Optional.of(NULL_CIPHER), channelGroup, true, false);

ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize);
Future<Integer> readFuture = socketPair.server.external.read(readBuffer);
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/tlschannel/async/AsyncQuickCloseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -36,7 +38,8 @@ public void testImmediateClose() throws IOException {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
for (int i = 1; i <= repetitions; i++) {
// create (and register) channels and close immediately
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair =
factory.async(Optional.of(NULL_CIPHER), channelGroup, true, false);
socketPair.server.external.close();
socketPair.client.external.close();

Expand All @@ -62,7 +65,8 @@ public void testRawImmediateClosing() throws IOException {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
for (int i = 1; i <= repetitions; i++) {
// create (and register) channels and close immediately
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair =
factory.async(Optional.of(NULL_CIPHER), channelGroup, true, false);
socketPair.server.plain.close();
socketPair.client.plain.close();

Expand Down
6 changes: 4 additions & 2 deletions src/test/java/tlschannel/async/AsyncShutdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand All @@ -29,7 +31,7 @@ public void testImmediateShutdown() throws InterruptedException {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
int socketPairCount = 50;
List<SocketGroups.AsyncSocketPair> socketPairs =
factory.asyncN(null, channelGroup, socketPairCount, true, false);
factory.asyncN(Optional.of(NULL_CIPHER), channelGroup, socketPairCount, true, false);
for (SocketGroups.AsyncSocketPair pair : socketPairs) {
ByteBuffer writeBuffer = ByteBuffer.allocate(bufferSize);
pair.client.external.write(writeBuffer);
Expand All @@ -56,7 +58,7 @@ public void testNonImmediateShutdown() throws InterruptedException, IOException
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
int socketPairCount = 50;
List<SocketGroups.AsyncSocketPair> socketPairs =
factory.asyncN(null, channelGroup, socketPairCount, true, false);
factory.asyncN(Optional.of(NULL_CIPHER), channelGroup, socketPairCount, true, false);
for (SocketGroups.AsyncSocketPair pair : socketPairs) {
ByteBuffer writeBuffer = ByteBuffer.allocate(bufferSize);
pair.client.external.write(writeBuffer);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/tlschannel/async/AsyncTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tlschannel.async;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -69,7 +70,8 @@ public void testNullEngine() throws Throwable {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
int dataSize = 12 * 1024 * 1024;
System.out.printf("data size: %d\n", dataSize);
List<AsyncSocketPair> socketPairs = factory.asyncN(null, channelGroup, socketPairCount, true, false);
List<AsyncSocketPair> socketPairs =
factory.asyncN(Optional.of(NULL_CIPHER), channelGroup, socketPairCount, true, false);
AsyncLoops.Report report = AsyncLoops.loop(socketPairs, dataSize);

shutdownChannelGroup(channelGroup);
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/tlschannel/async/AsyncTimeoutTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tlschannel.async;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static tlschannel.helpers.SocketPairFactory.NULL_CIPHER;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -39,7 +41,7 @@ public void testScheduledTimeout() throws IOException {
for (int i = 1; i <= repetitions; i++) {
int socketPairCount = 50;
List<SocketGroups.AsyncSocketPair> socketPairs =
factory.asyncN(null, channelGroup, socketPairCount, true, false);
factory.asyncN(Optional.of(NULL_CIPHER), channelGroup, socketPairCount, true, false);
CountDownLatch latch = new CountDownLatch(socketPairCount * 2);
for (SocketGroups.AsyncSocketPair pair : socketPairs) {
ByteBuffer writeBuffer = ByteBuffer.allocate(bufferSize);
Expand Down Expand Up @@ -120,7 +122,7 @@ public void testTriggeredTimeout() throws IOException {
for (int i = 1; i <= repetitions; i++) {
int socketPairCount = 50;
List<SocketGroups.AsyncSocketPair> socketPairs =
factory.asyncN(null, channelGroup, socketPairCount, true, false);
factory.asyncN(Optional.of(NULL_CIPHER), channelGroup, socketPairCount, true, false);

for (SocketGroups.AsyncSocketPair pair : socketPairs) {
ByteBuffer writeBuffer = ByteBuffer.allocate(bufferSize);
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/tlschannel/helpers/SocketPairFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class SocketPairFactory {

private static final Logger logger = Logger.getLogger(SocketPairFactory.class.getName());

public static final String NULL_CIPHER = "null-cipher";

private static final int maxAllowedKeyLength;

static {
Expand Down Expand Up @@ -247,7 +249,7 @@ public List<SocketPair> nioNioN(
}

SSLEngine clientEngine;
if (cipher == null) {
if (cipher.equals(Optional.of(NULL_CIPHER))) {
clientEngine = new NullSslEngine();
} else {
clientEngine = createClientSslEngine(cipher, chosenPort);
Expand All @@ -262,7 +264,7 @@ public List<SocketPair> nioNioN(
.build();

ServerTlsChannel.Builder serverChannelBuilder;
if (cipher == null) {
if (cipher.equals(Optional.of(NULL_CIPHER))) {
serverChannelBuilder = ServerTlsChannel.newBuilder(plainServer, new NullSslContext());
} else {
serverChannelBuilder = ServerTlsChannel.newBuilder(
Expand Down Expand Up @@ -363,7 +365,7 @@ public List<AsyncSocketPair> asyncN(
rawServer.configureBlocking(false);

SSLEngine clientEngine;
if (cipher == null) {
if (cipher.equals(Optional.of(NULL_CIPHER))) {
clientEngine = new NullSslEngine();
} else {
clientEngine = createClientSslEngine(cipher, chosenPort);
Expand All @@ -380,7 +382,7 @@ public List<AsyncSocketPair> asyncN(
.build();

ServerTlsChannel.Builder serverChannelBuilder;
if (cipher == null) {
if (cipher.equals(Optional.of(NULL_CIPHER))) {
serverChannelBuilder = ServerTlsChannel.newBuilder(
new RandomChunkingByteChannel(rawServer, SocketPairFactory::getChunkingSize),
new NullSslContext());
Expand Down

0 comments on commit ac3c4f8

Please sign in to comment.