1 18 19 package org.apache.activemq.transport.tcp; 20 21 import java.io.IOException ; 22 import java.security.cert.X509Certificate ; 23 24 import javax.management.remote.JMXPrincipal ; 25 import javax.net.ssl.SSLSocket; 26 27 import junit.framework.TestCase; 28 29 import org.apache.activemq.command.ConnectionInfo; 30 import org.apache.activemq.transport.StubTransportListener; 31 import org.apache.activemq.wireformat.ObjectStreamWireFormat; 32 33 37 public class SslTransportTest extends TestCase { 38 39 SSLSocket sslSocket; 40 SslTransport transport; 41 StubTransportListener stubListener; 42 43 String username; 44 String password; 45 String certDistinguishedName; 46 47 protected void setUp() throws Exception { 48 certDistinguishedName = "ThisNameIsDistinguished"; 49 username = "SomeUserName"; 50 password = "SomePassword"; 51 } 52 53 protected void tearDown() throws Exception { 54 super.tearDown(); 55 } 56 57 private void createTransportAndConsume( boolean wantAuth, boolean needAuth ) throws IOException { 58 JMXPrincipal principal = new JMXPrincipal ( certDistinguishedName ); 59 X509Certificate cert = new StubX509Certificate( principal ); 60 StubSSLSession sslSession = 61 new StubSSLSession( cert ); 62 63 sslSocket = new StubSSLSocket( sslSession ); 64 sslSocket.setWantClientAuth(wantAuth); 65 sslSocket.setNeedClientAuth(needAuth); 66 67 SslTransport transport = new SslTransport( 68 new ObjectStreamWireFormat(), sslSocket ); 69 70 stubListener = new StubTransportListener(); 71 72 transport.setTransportListener( stubListener ); 73 74 ConnectionInfo sentInfo = new ConnectionInfo(); 75 76 sentInfo.setUserName(username); 77 sentInfo.setPassword(password); 78 79 transport.doConsume(sentInfo); 80 } 81 82 public void testKeepClientUserName() throws IOException { 83 createTransportAndConsume(true, true); 84 85 final ConnectionInfo receivedInfo = 86 (ConnectionInfo) stubListener.getCommands().remove(); 87 88 X509Certificate receivedCert; 89 90 try { 91 receivedCert = ((X509Certificate [])receivedInfo.getTransportContext())[0]; 92 } catch (Exception e) { 93 receivedCert = null; 94 } 95 96 if ( receivedCert == null ) { 97 fail("Transmitted certificate chain was not attached to ConnectionInfo."); 98 } 99 100 assertEquals("Received certificate distinguished name did not match the one transmitted.", 101 certDistinguishedName, receivedCert.getSubjectDN().getName()); 102 103 } 104 } 105 106 107 | Popular Tags |