KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > test > ssl > SSLUnitTestCase


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.web.test.ssl;
23
24 import java.net.HttpURLConnection JavaDoc;
25
26 import junit.framework.Test;
27 import org.apache.commons.httpclient.Header;
28 import org.apache.commons.httpclient.HttpClient;
29 import org.apache.commons.httpclient.methods.GetMethod;
30 import org.jboss.test.JBossTestCase;
31
32 /** Tests of ssl and CLIENT-CERT auth
33  *
34  * @author Scott.Stark@jboss.org
35  * @author Anil.Saldhana@jboss.org
36  * @version $Revision: 55386 $
37  */

38 public class SSLUnitTestCase extends JBossTestCase
39 {
40    private String JavaDoc baseHttpNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/";
41    private String JavaDoc baseHttpsNoAuth = "https://" + getServerHost() + ":" + Integer.getInteger("secureweb.port", 8443) + "/";
42
43    public SSLUnitTestCase(String JavaDoc name)
44    {
45       super(name);
46    }
47
48    /** Test that access of the transport constrained redirects to the ssl connector
49     *
50     * @throws Exception
51     */

52    public void testHttpRedirect() throws Exception JavaDoc
53    {
54       log.info("+++ testHttpRedirect");
55       doHttpRedirect(baseHttpNoAuth);
56    }
57    /** Test that access of the transport constrained redirects to the ssl connector
58     * when using the SecurityDomain based connector config.
59     *
60     * @throws Exception
61     */

62    public void testHttpRedirectSecurityDomain() throws Exception JavaDoc
63    {
64       log.info("+++ testHttpRedirectSecurityDomain");
65       int port = Integer.getInteger("web.port", 8080).intValue();
66       port += 1000;
67       String JavaDoc httpNoAuth = "http://" + getServerHost() + ":" + port + "/";
68       doHttpRedirect(httpNoAuth);
69    }
70
71    /** Test that access of the transport constrained
72     *
73     * @throws Exception
74     */

75    public void testHttps() throws Exception JavaDoc
76    {
77       log.info("+++ testHttps");
78       doHttps(baseHttpsNoAuth);
79    }
80    
81    public void testHttpsSecurityDomain() throws Exception JavaDoc
82    {
83       log.info("+++ testHttps");
84       int port = Integer.getInteger("secureweb.port", 8443).intValue();
85       port += 1000;
86       String JavaDoc httpsNoAuth = "https://" + getServerHost() + ":" + port + "/";
87       doHttps(httpsNoAuth);
88    }
89    
90    /**
91     * Test masking of Keystore password via encryption
92     * @throws Exception
93     */

94    public void testEncryptPassword() throws Exception JavaDoc
95    {
96       log.info("+++ testHttps");
97       int port = Integer.getInteger("secureweb.port", 8443).intValue();
98       port += 1500;
99       String JavaDoc httpsNoAuth = "https://" + getServerHost() + ":" + port + "/";
100       doHttps(httpsNoAuth);
101    }
102    
103
104    private void doHttpRedirect(String JavaDoc httpNoAuth) throws Exception JavaDoc
105    {
106       log.info("+++ testHttpRedirect, httpNoAuth="+httpNoAuth);
107       // Start by accessing the secured index.html of war1
108
HttpClient httpConn = new HttpClient();
109       String JavaDoc url = httpNoAuth+"clientcert-auth/unrestricted/SecureServlet";
110       log.info("Accessing: "+url);
111       GetMethod get = new GetMethod(url);
112       int responseCode = httpConn.executeMethod(get);
113       String JavaDoc status = get.getStatusText();
114       log.debug(status);
115       assertTrue("Get HTTP_MOVED_TEMP("+responseCode+")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);
116
117       Header hdr = get.getResponseHeader("Location");
118       url = hdr.getValue();
119       get = new GetMethod(url);
120       responseCode = httpConn.executeMethod(get);
121       status = get.getStatusText();
122       log.debug(status);
123       assertTrue("Get OK("+responseCode+")", responseCode == HttpURLConnection.HTTP_OK);
124    }
125    public void doHttps(String JavaDoc httpsNoAuth) throws Exception JavaDoc
126    {
127       log.info("+++ doHttps, httpsNoAuth="+httpsNoAuth);
128       // Start by accessing the secured index.html of war1
129
HttpClient httpConn = new HttpClient();
130       String JavaDoc url = httpsNoAuth+"clientcert-auth/unrestricted/SecureServlet";
131       log.info("Accessing: "+url);
132       GetMethod get = new GetMethod(url);
133       int responseCode = httpConn.executeMethod(get);
134       String JavaDoc status = get.getStatusText();
135       log.debug(status);
136       assertTrue("Get OK("+responseCode+")", responseCode == HttpURLConnection.HTTP_OK);
137    }
138    
139    /** One time setup for all SingleSignOnUnitTestCase unit tests
140     */

141    public static Test suite() throws Exception JavaDoc
142    {
143       Test suite = getDeploySetup(SSLUnitTestCase.class, "clientcert-auth.war");
144       return suite;
145    }
146 }
147
Popular Tags