KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > transports > https > JettySslClientSystemPropertiesConfigurerTest


1 package org.objectweb.celtix.bus.transports.https;
2
3 import java.net.URL JavaDoc;
4 import java.net.URLConnection JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import javax.net.ssl.SSLSocketFactory;
8
9 import junit.extensions.TestSetup;
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13
14 import org.easymock.classextension.EasyMock;
15 import org.objectweb.celtix.Bus;
16 import org.objectweb.celtix.BusException;
17 import org.objectweb.celtix.bus.configuration.security.SSLClientPolicy;
18 import org.objectweb.celtix.configuration.Configuration;
19
20 public class JettySslClientSystemPropertiesConfigurerTest extends TestCase {
21
22     
23     private static final String JavaDoc DROP_BACK_SRC_DIR =
24         "../../../../../../../../src/test/java/org/objectweb/celtix/bus/transports/https/";
25
26     Bus bus;
27
28     
29
30
31     public JettySslClientSystemPropertiesConfigurerTest(String JavaDoc arg0) {
32         super(arg0);
33     }
34
35     public static Test suite() throws Exception JavaDoc {
36         TestSuite suite = new TestSuite(JettySslClientSystemPropertiesConfigurerTest.class);
37         return new TestSetup(suite) {
38             protected void tearDown() throws Exception JavaDoc {
39                 super.tearDown();
40             }
41         };
42     }
43
44
45     public static void main(String JavaDoc[] args) {
46         junit.textui.TestRunner.run(JettySslClientSystemPropertiesConfigurerTest.class);
47     }
48
49     public void setUp() throws BusException {
50         bus = EasyMock.createMock(Bus.class);
51     }
52
53     public void tearDown() throws Exception JavaDoc {
54         EasyMock.reset(bus);
55         Properties JavaDoc props = System.getProperties();
56         props.remove("javax.net.ssl.trustStore");
57         props.remove("javax.net.ssl.keyStore");
58         props.remove("javax.net.ssl.keyPassword");
59         props.remove("javax.net.ssl.keyStorePassword");
60     }
61     
62     public void testSetAllDataSomeSystemProperties() {
63         
64         String JavaDoc keyStoreStr = getPath("resources/defaultkeystore");
65         SSLClientPolicy sslClientPolicy = new SSLClientPolicy();
66         System.setProperty("javax.net.ssl.keyStore", keyStoreStr);
67         sslClientPolicy.setKeystoreType("JKS");
68         
69         System.setProperty("javax.net.ssl.keyStorePassword", "defaultkeypass");
70         System.setProperty("javax.net.ssl.keyPassword", "defaultkeypass");
71         sslClientPolicy.setTrustStoreType("JKS");
72         sslClientPolicy.setTrustStoreAlgorithm("JKS");
73         sslClientPolicy.setSecureSocketProtocol("TLSv1");
74         sslClientPolicy.setSessionCacheKey("Anything");
75         sslClientPolicy.setSessionCaching(true);
76         sslClientPolicy.setMaxChainLength(new Long JavaDoc(2));
77         sslClientPolicy.setCertValidator("Anything");
78         sslClientPolicy.setProxyHost("Anything");
79         sslClientPolicy.setProxyPort(new Long JavaDoc(1234));
80         
81         String JavaDoc trustStoreStr = getPath("resources/defaulttruststore");
82         System.setProperty("javax.net.ssl.trustStore", trustStoreStr);
83         TestHandler handler = new TestHandler();
84         JettySslClientConfigurer jettySslClientConfigurer =
85                             createJettySslClientConfigurer(sslClientPolicy,
86                                                            "https://dummyurl",
87                                                            handler);
88
89         jettySslClientConfigurer.configure();
90         SSLSocketFactory sSLSocketFactory =
91                 jettySslClientConfigurer.getHttpsConnection().getSSLSocketFactory();
92         
93         assertTrue("sSLSocketFactory not correct, sSLSocketFactory = " + sSLSocketFactory,
94                    sSLSocketFactory instanceof SSLSocketFactoryWrapper);
95         assertTrue("Keystore loaded success message not present",
96                    handler.checkLogContainsString("Successfully loaded keystore"));
97         assertTrue("Trust store loaded success message not present",
98                    handler.checkLogContainsString("Successfully loaded trust store"));
99         assertTrue("Keystore type not being read",
100                    handler.checkLogContainsString("The key store type has been set in configuration to JKS"));
101         assertTrue("Keystore password not being read",
102                    handler.checkLogContainsString("The key store password was found to be set "
103                                                   + "as a system property and will be used."));
104         assertTrue("Key password not being read",
105                    handler.checkLogContainsString("The key password was found to be set as a "
106                                                   + "system property and will be used."));
107         assertTrue("Key manager factory is being being read from somewhere unknown",
108                    handler.checkLogContainsString("The keystore key manager factory "
109                                                   + "algorithm has not been set in configuration "
110                                                   + "so the default value SunX509 will be used."));
111         
112         assertTrue("Trust manager factory is being being read from somewhere unknown",
113                    handler.checkLogContainsString("The truststore key manager factory "
114                                                   + "algorithm has not been set in configuration "
115                                                   + "so the default value PKIX will be used."));
116         assertTrue("Trust store location not read successfully",
117                    handler.checkLogContainsString("The trust store location has been "
118                                                   + "via a system property to"));
119         
120         assertTrue("Ciphersuites is being being read from somewhere unknown",
121                    handler.checkLogContainsString("The cipher suite has not been set, default values "
122                                                   + "will be used."));
123         assertTrue("Truststore type not being read",
124                    handler.checkLogContainsString("The key store type has been set in "
125                                                   + "configuration to JKS"));
126
127         assertTrue("Secure socket protocol not being read",
128                    handler.checkLogContainsString("The secure socket protocol has been set to TLSv1."));
129         assertTrue("Session caching set but no warning about not supported",
130                    handler.checkLogContainsString("Unsupported SSLClientPolicy property : SessionCaching"));
131         assertTrue("SessionCacheKey caching set but no warning about not supported",
132                    handler.checkLogContainsString("Unsupported SSLClientPolicy property : SessionCacheKey"));
133         assertTrue("MaxChainLength caching set but no warning about not supported",
134                    handler.checkLogContainsString("Unsupported SSLClientPolicy property : MaxChainLength"));
135         assertTrue("CertValidator caching set but no warning about not supported",
136                    handler.checkLogContainsString("Unsupported SSLClientPolicy property : CertValidator"));
137     }
138     
139     private JettySslClientConfigurer createJettySslClientConfigurer(
140                                              SSLClientPolicy sslClientPolicy,
141                                              String JavaDoc urlStr,
142                                              TestHandler handler) {
143         try {
144             URL JavaDoc url = new URL JavaDoc(urlStr);
145             URLConnection JavaDoc connection = new DummyHttpsConnection(url);
146             Configuration configuration = EasyMock.createMock(Configuration.class);
147             JettySslClientConfigurer jettySslClientConfigurer =
148                 new JettySslClientConfigurer(sslClientPolicy,
149                                              connection,
150                                              configuration);
151             
152             jettySslClientConfigurer.addLogHandler(handler);
153             return jettySslClientConfigurer;
154             
155         } catch (Exception JavaDoc e) {
156             e.printStackTrace();
157         }
158         return null;
159     }
160     
161     private String JavaDoc getPath(String JavaDoc fileName) {
162         URL JavaDoc keystoreURL = JettySslClientSystemPropertiesConfigurerTest.class.getResource(".");
163         String JavaDoc str = keystoreURL.getFile();
164         str += DROP_BACK_SRC_DIR + fileName;
165         return str;
166     }
167 }
168
169
Popular Tags