KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jrmp > test > SSLSocketsUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jrmp.test;
23
24 import java.net.URL JavaDoc;
25 import java.security.cert.Certificate JavaDoc;
26 import java.security.cert.X509Certificate JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.net.ssl.HandshakeCompletedEvent;
29 import javax.net.ssl.HandshakeCompletedListener;
30 import javax.net.ssl.SSLPeerUnverifiedException;
31 import javax.net.ssl.SSLSession;
32
33 import junit.framework.Test;
34 import org.jboss.security.ssl.RMISSLClientSocketFactory;
35 import org.jboss.test.JBossTestCase;
36 import org.jboss.test.jrmp.interfaces.StatelessSession;
37 import org.jboss.test.jrmp.interfaces.StatelessSessionHome;
38
39 /**
40  * Test of using custom RMI socket factories with the JRMP ejb container
41  * invoker.
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 44735 $
45  */

46 public class SSLSocketsUnitTestCase extends JBossTestCase
47    implements HandshakeCompletedListener
48 {
49    private String JavaDoc cipherSuite;
50    private Certificate JavaDoc[] localCerts;
51    private Certificate JavaDoc[] peerCerts;
52
53    /**
54     * Constructor for the CustomSocketsUnitTestCase object
55     *
56     * @param name Description of Parameter
57     */

58    public SSLSocketsUnitTestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    /**
64     * Test basic ejb access over the ssl socket requiring a client cert
65     *
66     * @exception Exception Description of Exception
67     */

68    public void testClientCertSSLAccess() throws Exception JavaDoc
69    {
70       log.info("+++ testClientCertSSLAccess");
71       String JavaDoc res = super.getResourceURL("test-configs/tomcat-ssl/conf/client.keystore");
72       log.info("client.keystore: "+res);
73       URL JavaDoc clientURL = new URL JavaDoc(res);
74       System.setProperty("javax.net.ssl.trustStore", clientURL.getFile());
75       System.setProperty("javax.net.ssl.trustStorePassword", "unit-tests-client");
76       System.setProperty("javax.net.ssl.keyStore", clientURL.getFile());
77       System.setProperty("javax.net.ssl.keyStorePassword", "unit-tests-client");
78       System.getProperties().put(RMISSLClientSocketFactory.HANDSHAKE_COMPLETE_LISTENER, this);
79
80       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
81       log.debug("Lookup StatelessSessionWithSSL");
82       Object JavaDoc obj = jndiContext.lookup("StatelessSessionWithSSL");
83       StatelessSessionHome home = (StatelessSessionHome)obj;
84       log.debug("Found StatelessSessionWithSSL Home");
85       StatelessSession bean = home.create();
86       log.debug("Created StatelessSessionWithSSL");
87       // Test that the Entity bean sees username as its principal
88
String JavaDoc echo = bean.echo("jrmp");
89       log.debug("bean.echo(jrmp) = " + echo);
90       bean.remove();
91
92       //Allow the handshakecompleted listener to populate the cipherSuite
93
sleep(2000);
94       assertNotNull("cipherSuite != null",cipherSuite);
95
96       // Validate the expected ssl session
97
assertTrue("CipherSuite = TLS_DHE_DSS_WITH_AES_128_CBC_SHA",
98          cipherSuite.equals("TLS_DHE_DSS_WITH_AES_128_CBC_SHA"));
99       X509Certificate JavaDoc localCert = (X509Certificate JavaDoc) localCerts[0];
100       assertTrue("LocalCert.SubjectDN = CN=unit-tests-client, OU=JBoss Inc., O=JBoss Inc., ST=Washington, C=US",
101          localCert.getSubjectDN().getName().equals("CN=unit-tests-client, OU=JBoss Inc., O=JBoss Inc., ST=Washington, C=US"));
102    }
103
104    public void handshakeCompleted(HandshakeCompletedEvent event)
105    {
106       log.info("handshakeCompleted, event="+event);
107       try
108       {
109          cipherSuite = event.getCipherSuite();
110          log.info("CipherSuite: "+cipherSuite);
111          localCerts = event.getLocalCertificates();
112          log.info("LocalCertificates:");
113          for(int n = 0; n < localCerts.length; n ++)
114          {
115             Certificate JavaDoc cert = localCerts[n];
116             log.info(cert);
117          }
118          log.info("PeerCertificates:");
119          peerCerts = event.getPeerCertificates();
120          for(int n = 0; n < peerCerts.length; n ++)
121          {
122             Certificate JavaDoc cert = peerCerts[n];
123             log.info(cert);
124          }
125
126          SSLSession session = event.getSession();
127          String JavaDoc[] names = session.getValueNames();
128          for(int n = 0; n < names.length; n ++)
129          {
130             String JavaDoc name = names[n];
131             log.info(name+"="+session.getValue(name));
132          }
133       }
134       catch (SSLPeerUnverifiedException e)
135       {
136          log.error("Failed to get peer cert", e);
137       }
138    }
139
140    public static Test suite() throws Exception JavaDoc
141    {
142       return getDeploySetup(SSLSocketsUnitTestCase.class, "jrmp-comp.jar");
143    }
144
145 }
146
Popular Tags