|
18 | 18 | import com.rabbitmq.client.Connection;
|
19 | 19 | import com.rabbitmq.client.ConnectionFactory;
|
20 | 20 | import com.rabbitmq.client.impl.TlsUtils;
|
| 21 | +import com.rabbitmq.client.impl.nio.NioParams; |
21 | 22 | import com.rabbitmq.client.test.TestUtils;
|
22 | 23 | import org.assertj.core.api.Assertions;
|
23 | 24 | import org.junit.Test;
|
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.junit.runners.Parameterized; |
24 | 27 |
|
25 | 28 | import javax.net.ssl.*;
|
26 | 29 | import java.security.cert.X509Certificate;
|
27 | 30 | import java.util.concurrent.atomic.AtomicReference;
|
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.function.Supplier; |
28 | 33 |
|
29 | 34 | import static org.junit.Assert.assertNotNull;
|
30 | 35 |
|
| 36 | +@RunWith(Parameterized.class) |
31 | 37 | public class TlsConnectionLogging {
|
32 | 38 |
|
| 39 | + @Parameterized.Parameter |
| 40 | + public Function<ConnectionFactory, Supplier<SSLSession>> configurer; |
| 41 | + |
| 42 | + @Parameterized.Parameters |
| 43 | + public static Object[] data() { |
| 44 | + return new Object[]{blockingIo(), nio()}; |
| 45 | + } |
| 46 | + |
| 47 | + public static Function<ConnectionFactory, Supplier<SSLSession>> blockingIo() { |
| 48 | + return connectionFactory -> { |
| 49 | + connectionFactory.useBlockingIo(); |
| 50 | + AtomicReference<SSLSocket> socketCaptor = new AtomicReference<>(); |
| 51 | + connectionFactory.setSocketConfigurator(socket -> socketCaptor.set((SSLSocket) socket)); |
| 52 | + return () -> socketCaptor.get().getSession(); |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + public static Function<ConnectionFactory, Supplier<SSLSession>> nio() { |
| 57 | + return connectionFactory -> { |
| 58 | + connectionFactory.useNio(); |
| 59 | + AtomicReference<SSLEngine> sslEngineCaptor = new AtomicReference<>(); |
| 60 | + connectionFactory.setNioParams(new NioParams() |
| 61 | + .setSslEngineConfigurator(sslEngine -> sslEngineCaptor.set(sslEngine))); |
| 62 | + return () -> sslEngineCaptor.get().getSession(); |
| 63 | + }; |
| 64 | + } |
| 65 | + |
33 | 66 | @Test
|
34 | 67 | public void certificateInfoAreProperlyExtracted() throws Exception {
|
35 | 68 | SSLContext sslContext = TestUtils.getSSLContext();
|
36 | 69 | sslContext.init(null, new TrustManager[]{new AlwaysTrustTrustManager()}, null);
|
37 | 70 | ConnectionFactory connectionFactory = TestUtils.connectionFactory();
|
38 | 71 | connectionFactory.useSslProtocol(sslContext);
|
39 |
| - connectionFactory.useBlockingIo(); |
40 |
| - AtomicReference<SSLSocket> socketCaptor = new AtomicReference<>(); |
41 |
| - connectionFactory.setSocketConfigurator(socket -> socketCaptor.set((SSLSocket) socket)); |
| 72 | + Supplier<SSLSession> sslSessionSupplier = configurer.apply(connectionFactory); |
42 | 73 | try (Connection ignored = connectionFactory.newConnection()) {
|
43 |
| - SSLSession session = socketCaptor.get().getSession(); |
| 74 | + SSLSession session = sslSessionSupplier.get(); |
44 | 75 | assertNotNull(session);
|
45 | 76 | String info = TlsUtils.peerCertificateInfo(session.getPeerCertificates()[0], "some prefix");
|
46 | 77 | Assertions.assertThat(info).contains("some prefix")
|
|
0 commit comments