KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > pooled > 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.pooled.test;
23
24 import java.net.URL JavaDoc;
25 import java.security.cert.Certificate JavaDoc;
26 import java.security.cert.X509Certificate JavaDoc;
27 import java.security.Principal JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.net.ssl.HandshakeCompletedEvent;
31 import javax.net.ssl.HandshakeCompletedListener;
32 import javax.net.ssl.SSLPeerUnverifiedException;
33 import javax.net.ssl.SSLSession;
34
35 import junit.framework.Test;
36 import org.jboss.security.ssl.DomainSocketFactory;
37 import org.jboss.test.JBossTestCase;
38 import org.jboss.test.pooled.interfaces.StatelessSession;
39 import org.jboss.test.pooled.interfaces.StatelessSessionHome;
40
41 /**
42  * Test of using custom SSL socket factories with the PooledInvoker ejb
43  * container invoker.
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision: 40977 $
47  */

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

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

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