diff --git a/src/test/scala/tlschannel/CipherTest.java b/src/test/scala/tlschannel/CipherTest.java new file mode 100644 index 00000000..3a696206 --- /dev/null +++ b/src/test/scala/tlschannel/CipherTest.java @@ -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 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 testHalfDuplexWithRenegotiation() { + System.out.println("testHalfDuplexWithRenegotiation():"); + List 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 testFullDuplex() { + List 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; + } +} diff --git a/src/test/scala/tlschannel/CipherTest.scala b/src/test/scala/tlschannel/CipherTest.scala deleted file mode 100644 index c7d4cd78..00000000 --- a/src/test/scala/tlschannel/CipherTest.scala +++ /dev/null @@ -1,71 +0,0 @@ -package tlschannel - -import org.junit.jupiter.api.{DynamicTest, Test, TestFactory, TestInstance} -import org.junit.jupiter.api.TestInstance.Lifecycle - -import javax.net.ssl.SSLContext -import tlschannel.helpers.SslContextFactory -import tlschannel.helpers.SocketPairFactory -import tlschannel.helpers.Loops - -import java.util -import scala.jdk.CollectionConverters._ - -@TestInstance(Lifecycle.PER_CLASS) -class CipherTest { - - val protocols = { - val allProtocols = SSLContext.getDefault.getSupportedSSLParameters.getProtocols.toSeq - allProtocols.filterNot(_ == "SSLv2Hello") - } - - val dataSize = 200 * 1000 - - // Test a half-duplex interaction, with renegotiation before reversing the direction of the flow (as in HTTP) - @TestFactory - def testHalfDuplexWithRenegotiation(): util.Collection[DynamicTest] = { - println("testHalfDuplexWithRenegotiation():") - val tests = for { - protocol <- protocols - ctxFactory = new SslContextFactory(protocol) - cipher <- ctxFactory.allCiphers - } yield { - DynamicTest.dynamicTest( - s"testHalfDuplexWithRenegotiation() - protocol: $protocol, cipher: $cipher", - () => { - val socketFactory = new SocketPairFactory(ctxFactory.defaultContext) - val socketPair = socketFactory.nioNio(Some(cipher)) - Loops.halfDuplex(socketPair, dataSize, renegotiation = protocol <= "TLSv1.2") - val actualProtocol = socketPair.client.tls.getSslEngine.getSession.getProtocol - val p = s"$protocol ($actualProtocol)" - println(f"$p%-18s $cipher%-50s") - } - ) - } - tests.asJava - } - - // Test a full-duplex interaction, without any renegotiation - @TestFactory - def testFullDuplex(): util.Collection[DynamicTest] = { - val tests = for { - protocol <- protocols - ctxFactory = new SslContextFactory(protocol) - cipher <- ctxFactory.allCiphers - } yield { - DynamicTest.dynamicTest( - s"testFullDuplex() - protocol: $protocol, cipher: $cipher", - () => { - val socketFactory = new SocketPairFactory(ctxFactory.defaultContext) - val socketPair = socketFactory.nioNio(Some(cipher)) - Loops.fullDuplex(socketPair, dataSize) - val actualProtocol = socketPair.client.tls.getSslEngine.getSession.getProtocol - val p = s"$protocol ($actualProtocol)" - println(f"$p%-18s $cipher%-50s") - } - ) - } - tests.asJava - } - -}