Skip to content

Commit

Permalink
Test TLS 1.3 only if available
Browse files Browse the repository at this point in the history
TLS 1.3 has not been backported to all Java version (e.g. on 9 and 10),
so this commit checks if the protocol is available before
running the test.

References #715

(cherry picked from commit 23961d5)
  • Loading branch information
acogoluegnes committed Nov 8, 2021
1 parent 525cb08 commit 830bdc6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import com.rabbitmq.client.impl.nio.NioParams;
import com.rabbitmq.client.test.BrokerTestCase;
import com.rabbitmq.client.test.TestUtils;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.junit.Test;
Expand Down Expand Up @@ -84,7 +87,10 @@ public void connectionGetConsume() throws Exception {

@Test
public void connectionGetConsumeProtocols() throws Exception {
String [] protocols = new String[] {"TLSv1.2", "TLSv1.3"};
Collection<String> availableProtocols = TlsTestUtils.availableTlsProtocols();
Collection<String> protocols = Stream.of("TLSv1.2", "TLSv1.3")
.filter(p -> availableProtocols.contains(p))
.collect(Collectors.toList());
for (String protocol : protocols) {
SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(null, new TrustManager[] {new TrustEverythingTrustManager()}, null);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/rabbitmq/client/test/ssl/TlsTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
Expand Down Expand Up @@ -107,6 +109,16 @@ public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
throw new NoSuchAlgorithmException();
}

static Collection<String> availableTlsProtocols() {
try {
String[] protocols = SSLContext.getDefault().getSupportedSSLParameters().getProtocols();
return Arrays.stream(protocols).filter(p -> p.toLowerCase().startsWith("tls")).collect(
Collectors.toList());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@FunctionalInterface
interface CallableSupplier <T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.impl.nio.NioParams;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;

Expand Down Expand Up @@ -69,7 +72,10 @@ public void openConnection()

@Test
public void connectionGetConsumeProtocols() throws Exception {
String [] protocols = new String[] {"TLSv1.2", "TLSv1.3"};
Collection<String> availableProtocols = TlsTestUtils.availableTlsProtocols();
Collection<String> protocols = Stream.of("TLSv1.2", "TLSv1.3")
.filter(p -> availableProtocols.contains(p))
.collect(Collectors.toList());
for (String protocol : protocols) {
SSLContext sslContext = SSLContext.getInstance(protocol);
ConnectionFactory cf = TestUtils.connectionFactory();
Expand Down

0 comments on commit 830bdc6

Please sign in to comment.