|
20 | 20 | import java.io.ByteArrayOutputStream; |
21 | 21 | import java.io.FileInputStream; |
22 | 22 | import java.io.IOException; |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.net.Socket; |
23 | 25 | import java.security.KeyStore; |
24 | 26 | import java.security.SecureRandom; |
25 | 27 | import java.security.UnrecoverableKeyException; |
26 | 28 |
|
27 | 29 | import javax.net.ssl.KeyManager; |
28 | 30 | import javax.net.ssl.KeyManagerFactory; |
| 31 | +import javax.net.ssl.SSLSocket; |
29 | 32 | import javax.net.ssl.TrustManager; |
30 | 33 | import javax.net.ssl.TrustManagerFactory; |
31 | 34 |
|
32 | 35 | import org.apache.activemq.broker.BrokerService; |
33 | 36 | import org.apache.activemq.broker.SslBrokerService; |
| 37 | +import org.apache.activemq.transport.Transport; |
| 38 | +import org.apache.activemq.transport.TransportFilter; |
| 39 | +import org.apache.activemq.transport.tcp.SslTransport; |
| 40 | +import org.apache.activemq.transport.tcp.TcpTransport; |
34 | 41 | import org.apache.commons.logging.Log; |
35 | 42 | import org.apache.commons.logging.LogFactory; |
36 | 43 |
|
| 44 | +import static org.junit.Assert.assertArrayEquals; |
| 45 | + |
37 | 46 | public class ActiveMQSslConnectionFactoryTest extends CombinationTestSupport { |
38 | 47 | private static final Log LOG = LogFactory.getLog(ActiveMQSslConnectionFactoryTest.class); |
39 | 48 |
|
@@ -72,6 +81,26 @@ public void testCreateTcpConnectionUsingKnownPort() throws Exception { |
72 | 81 | brokerStop(); |
73 | 82 | } |
74 | 83 |
|
| 84 | + public void testCreateTcpConnectionWithSocketParameters() throws Exception { |
| 85 | + // Control case: check that the factory can create an ordinary (non-ssl) connection. |
| 86 | + String tcpUri = "tcp://localhost:61610?socket.OOBInline=true&socket.keepAlive=true&tcpNoDelay=true"; |
| 87 | + broker = createBroker(tcpUri); |
| 88 | + |
| 89 | + // This should create the connection. |
| 90 | + ActiveMQSslConnectionFactory cf = getFactory(tcpUri); |
| 91 | + connection = (ActiveMQConnection)cf.createConnection(); |
| 92 | + assertNotNull(connection); |
| 93 | + |
| 94 | + Socket socket = getSocketFromConnection(connection); |
| 95 | + assertTrue(socket.getOOBInline()); |
| 96 | + assertTrue(socket.getKeepAlive()); |
| 97 | + assertTrue(socket.getTcpNoDelay()); |
| 98 | + |
| 99 | + connection.start(); |
| 100 | + connection.stop(); |
| 101 | + brokerStop(); |
| 102 | + } |
| 103 | + |
75 | 104 | public void testCreateFailoverTcpConnectionUsingKnownPort() throws Exception { |
76 | 105 | // Control case: check that the factory can create an ordinary (non-ssl) connection. |
77 | 106 | broker = createBroker("tcp://localhost:61610?wireFormat.tcpNoDelayEnabled=true"); |
@@ -103,6 +132,30 @@ public void testCreateSslConnection() throws Exception { |
103 | 132 | brokerStop(); |
104 | 133 | } |
105 | 134 |
|
| 135 | + public void testCreateSslConnectionWithSocketParameters() throws Exception { |
| 136 | + // Create SSL/TLS connection with trusted cert from truststore. |
| 137 | + String sslUri = "ssl://localhost:61611?socket.enabledProtocols=TLSv1.3&socket.enableSessionCreation=true&socket.needClientAuth=true"; |
| 138 | + broker = createSslBroker(sslUri); |
| 139 | + assertNotNull(broker); |
| 140 | + |
| 141 | + // This should create the connection. |
| 142 | + ActiveMQSslConnectionFactory cf = getFactory(sslUri); |
| 143 | + cf.setTrustStore("server.keystore"); |
| 144 | + cf.setTrustStorePassword("password"); |
| 145 | + connection = (ActiveMQConnection)cf.createConnection(); |
| 146 | + assertNotNull(connection); |
| 147 | + |
| 148 | + SSLSocket socket = (SSLSocket) getSocketFromConnection(connection); |
| 149 | + String[] expectedProtocols = {"TLSv1.3"}; |
| 150 | + assertArrayEquals(expectedProtocols, socket.getEnabledProtocols()); |
| 151 | + assertTrue(socket.getEnableSessionCreation()); |
| 152 | + assertTrue(socket.getNeedClientAuth()); |
| 153 | + |
| 154 | + connection.start(); |
| 155 | + connection.stop(); |
| 156 | + brokerStop(); |
| 157 | + } |
| 158 | + |
106 | 159 | public void testCreateSslConnectionKeyStore() throws Exception { |
107 | 160 | // Create SSL/TLS connection with trusted cert from truststore. |
108 | 161 | String sslUri = "ssl://localhost:61611"; |
@@ -371,4 +424,18 @@ private static Throwable getRootCause(Throwable throwable) { |
371 | 424 | return rootCause; |
372 | 425 | } |
373 | 426 |
|
| 427 | + private Socket getSocketFromConnection(ActiveMQConnection connection) throws Exception { |
| 428 | + Transport transport = connection.getTransport(); |
| 429 | + while(!(transport instanceof TcpTransport)) { |
| 430 | + transport = ((TransportFilter) transport).getNext(); |
| 431 | + } |
| 432 | + Class<?> transportClass = transport.getClass(); |
| 433 | + if (transport instanceof SslTransport) { |
| 434 | + transportClass = transportClass.getSuperclass(); |
| 435 | + } |
| 436 | + Field socket = transportClass.getDeclaredField("socket"); |
| 437 | + socket.setAccessible(true); |
| 438 | + return (Socket) socket.get(transport); |
| 439 | + } |
| 440 | + |
374 | 441 | } |
0 commit comments