KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > SslTransportTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.transport.tcp;
20
21 import java.io.IOException JavaDoc;
22 import java.security.cert.X509Certificate JavaDoc;
23
24 import javax.management.remote.JMXPrincipal JavaDoc;
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 /**
34  * Unit tests for the SslTransport class.
35  *
36  */

37 public class SslTransportTest extends TestCase {
38     
39     SSLSocket sslSocket;
40     SslTransport transport;
41     StubTransportListener stubListener;
42     
43     String JavaDoc username;
44     String JavaDoc password;
45     String JavaDoc certDistinguishedName;
46     
47     protected void setUp() throws Exception JavaDoc {
48         certDistinguishedName = "ThisNameIsDistinguished";
49         username = "SomeUserName";
50         password = "SomePassword";
51     }
52
53     protected void tearDown() throws Exception JavaDoc {
54         super.tearDown();
55     }
56     
57     private void createTransportAndConsume( boolean wantAuth, boolean needAuth ) throws IOException JavaDoc {
58         JMXPrincipal JavaDoc principal = new JMXPrincipal JavaDoc( certDistinguishedName );
59         X509Certificate JavaDoc 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 JavaDoc {
83         createTransportAndConsume(true, true);
84         
85         final ConnectionInfo receivedInfo =
86             (ConnectionInfo) stubListener.getCommands().remove();
87         
88         X509Certificate JavaDoc receivedCert;
89         
90         try {
91             receivedCert = ((X509Certificate JavaDoc[])receivedInfo.getTransportContext())[0];
92         } catch (Exception JavaDoc 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