Skip to content

Commit

Permalink
Migrate to Java: CipherTest
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Mar 31, 2024
1 parent fd8c4e1 commit 05ff380
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 71 deletions.
97 changes: 97 additions & 0 deletions src/test/scala/tlschannel/CipherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tlschannel;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import scala.Option;
import scala.Some;
import scala.jdk.CollectionConverters;
import tlschannel.helpers.Loops;
import tlschannel.helpers.SocketPair;
import tlschannel.helpers.SocketPairFactory;
import tlschannel.helpers.SslContextFactory;

@TestInstance(Lifecycle.PER_CLASS)
public class CipherTest {

private final List<String> protocols;
private final int dataSize = 200 * 1000;

public CipherTest() {
try {
String[] allProtocols =
SSLContext.getDefault().getSupportedSSLParameters().getProtocols();
protocols = Arrays.stream(allProtocols)
.filter(x -> !x.equals("SSLv2Hello"))
.collect(Collectors.toList());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException();
}
}

// Test a half-duplex interaction, with renegotiation before reversing the direction of the flow (as in HTTP)
@TestFactory
public Collection<DynamicTest> testHalfDuplexWithRenegotiation() {
System.out.println("testHalfDuplexWithRenegotiation():");
List<DynamicTest> tests = new ArrayList<>();
for (String protocol : protocols) {
SslContextFactory ctxFactory = new SslContextFactory(protocol);
for (String cipher :
CollectionConverters.SeqHasAsJava(ctxFactory.allCiphers()).asJava()) {
tests.add(DynamicTest.dynamicTest(
String.format("testHalfDuplexWithRenegotiation() - protocol: %s, cipher: %s", protocol, cipher),
() -> {
SocketPairFactory socketFactory = new SocketPairFactory(ctxFactory.defaultContext());
SocketPair socketPair = socketFactory.nioNio(
Some.apply(cipher), Option.apply(null), true, false, Option.apply(null));
Loops.halfDuplex(socketPair, dataSize, protocol.compareTo("TLSv1.2") < 0, false);
String actualProtocol = socketPair
.client()
.tls()
.getSslEngine()
.getSession()
.getProtocol();
String p = String.format("%s (%s)", protocol, actualProtocol);
System.out.printf("%-18s %-50s\n", p, cipher);
}));
}
}
return tests;
}

// Test a full-duplex interaction, without any renegotiation
@TestFactory
public Collection<DynamicTest> testFullDuplex() {
List<DynamicTest> tests = new ArrayList<>();
for (String protocol : protocols) {
SslContextFactory ctxFactory = new SslContextFactory(protocol);
for (String cipher :
CollectionConverters.SeqHasAsJava(ctxFactory.allCiphers()).asJava()) {
tests.add(DynamicTest.dynamicTest(
String.format("testFullDuplex() - protocol: %s, cipher: %s", protocol, cipher), () -> {
SocketPairFactory socketFactory = new SocketPairFactory(ctxFactory.defaultContext());
SocketPair socketPair = socketFactory.nioNio(
Some.apply(cipher), Option.apply(null), true, false, Option.apply(null));
Loops.fullDuplex(socketPair, dataSize);
String actualProtocol = socketPair
.client()
.tls()
.getSslEngine()
.getSession()
.getProtocol();
String p = String.format("%s (%s)", protocol, actualProtocol);
System.out.printf("%-18s %-50s\n", p, cipher);
}));
}
}
return tests;
}
}
71 changes: 0 additions & 71 deletions src/test/scala/tlschannel/CipherTest.scala

This file was deleted.

0 comments on commit 05ff380

Please sign in to comment.