KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix.bus.transports.https;
2
3 import java.net.URL JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import junit.extensions.TestSetup;
7 import junit.framework.Test;
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10
11 import org.easymock.classextension.EasyMock;
12 import org.mortbay.http.SslListener;
13 import org.objectweb.celtix.Bus;
14 import org.objectweb.celtix.BusException;
15 import org.objectweb.celtix.bus.configuration.security.SSLServerPolicy;
16 import org.objectweb.celtix.configuration.Configuration;
17
18 public class JettySslListenerConfigurerTest extends TestCase {
19
20     
21     private static final String JavaDoc DROP_BACK_SRC_DIR =
22         "../../../../../../../../src/test/java/org/objectweb/celtix/bus/transports/https/";
23
24     Bus bus;
25     private Configuration configuration;
26
27     
28
29
30     public JettySslListenerConfigurerTest(String JavaDoc arg0) {
31         super(arg0);
32     }
33
34     public static Test suite() throws Exception JavaDoc {
35         TestSuite suite = new TestSuite(JettySslListenerConfigurerTest.class);
36         return new TestSetup(suite) {
37             protected void tearDown() throws Exception JavaDoc {
38                 super.tearDown();
39             }
40         };
41     }
42
43
44     public static void main(String JavaDoc[] args) {
45         junit.textui.TestRunner.run(JettySslListenerConfigurerTest.class);
46     }
47
48     public void setUp() throws BusException {
49         bus = EasyMock.createMock(Bus.class);
50         configuration = EasyMock.createMock(Configuration.class);
51     }
52
53     public void tearDown() throws Exception JavaDoc {
54         EasyMock.reset(bus);
55         EasyMock.reset(configuration);
56         Properties JavaDoc props = System.getProperties();
57         props.remove("javax.net.ssl.trustStore");
58         props.remove("javax.net.ssl.keyStore");
59     }
60     
61     public void testSecurityConfigurer() {
62         try {
63             System.setProperty("celtix.security.configurer.celtix.null",
64                                "org.objectweb.celtix.bus.transports.https.SetAllDataSecurityDataProvider");
65             SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
66             TestHandler handler = new TestHandler();
67             JettySslListenerConfigurer jettySslListenerConfigurer =
68                                 createJettySslListenerConfigurer(sslServerPolicy,
69                                                                "https://dummyurl",
70                                                                handler);
71     
72             jettySslListenerConfigurer.configure();
73             SslListener sslListener = jettySslListenerConfigurer.getSslListener();
74             assertTrue("Keystore not set properly",
75                        sslListener.getKeystore().contains("resources/defaultkeystore"));
76             String JavaDoc trustStr = System.getProperty("javax.net.ssl.trustStore");
77             assertTrue("Trust store loaded success message not present",
78                        trustStr.contains("resources/defaulttruststore"));
79             assertTrue("Keystore type not being read",
80                        sslListener.getKeystoreType().equals("JKS"));
81             assertTrue("Keystore password not being read",
82                        sslServerPolicy.getKeystorePassword().equals("defaultkeypass"));
83             assertTrue("Key password not being read",
84                        sslServerPolicy.getKeyPassword().equals("defaultkeypass"));
85             
86             assertTrue("Ciphersuites is not being read from the config provider",
87                        sslListener.getCipherSuites()[0].equals("MyCipher"));
88             assertTrue("Truststore type not being read",
89                        handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
90                                                       + "TrustStoreType"));
91     
92             assertTrue("Secure socket protocol not being read",
93                        handler.checkLogContainsString("The secure socket protocol has been set to TLSv1."));
94             assertTrue("Session caching set but no warning about not supported",
95                        handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
96                                                       + "SessionCaching"));
97             assertTrue("SessionCacheKey caching set but no warning about not supported",
98                        handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
99                                                       + "SessionCacheKey"));
100             assertTrue("MaxChainLength caching set but no warning about not supported",
101                        handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
102                                                       + "MaxChainLength"));
103             assertTrue("CertValidator caching set but no warning about not supported",
104                        handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
105                                                       + "CertValidator"));
106             
107             System.setProperty("celtix.security.configurer.celtix.null",
108                 "org.objectweb.celtix.bus.transports.https.DoesNotExistSetAllDataSecurityDataProvider");
109             SSLServerPolicy sslServerPolicy2 = new SSLServerPolicy();
110             TestHandler handler2 = new TestHandler();
111             EasyMock.reset(configuration);
112             configuration = EasyMock.createMock(Configuration.class);
113             JettySslListenerConfigurer jettySslListenerConfigurer2 =
114                 createJettySslListenerConfigurer(sslServerPolicy2,
115                                             "https://dummyurl",
116                                             handler2);
117             sslServerPolicy2.setKeyPassword("test");
118             sslServerPolicy2.setKeystorePassword("test1");
119             jettySslListenerConfigurer2.configure();
120             
121             assertTrue("Keystore not set properly",
122                        handler2.checkLogContainsString("Failure invoking on custom security configurer "
123                                  + "org.objectweb.celtix.bus.transports.https."
124                                  + "DoesNotExistSetAllDataSecurityDataProvider, "));
125         } finally {
126             System.setProperty("celtix.security.configurer.celtix.null", "");
127         }
128     }
129     
130     public void testSetAllData() {
131         
132         String JavaDoc keyStoreStr = getPath("resources/defaultkeystore");
133         SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
134         sslServerPolicy.setKeystore(keyStoreStr);
135         sslServerPolicy.setKeystoreType("JKS");
136         
137         sslServerPolicy.setKeyPassword("defaultkeypass");
138         sslServerPolicy.setKeystorePassword("defaultkeypass");
139         sslServerPolicy.setTrustStoreType("JKS");
140         sslServerPolicy.setTrustStoreAlgorithm("JKS");
141         sslServerPolicy.setSecureSocketProtocol("TLSv1");
142         sslServerPolicy.setSessionCacheKey("Anything");
143         sslServerPolicy.setSessionCaching(true);
144         sslServerPolicy.setMaxChainLength(new Long JavaDoc(2));
145         sslServerPolicy.setCertValidator("Anything");
146
147         
148         String JavaDoc trustStoreStr = getPath("resources/defaulttruststore");
149         sslServerPolicy.setTrustStore(trustStoreStr);
150         TestHandler handler = new TestHandler();
151         JettySslListenerConfigurer jettySslListenerConfigurer =
152                             createJettySslListenerConfigurer(sslServerPolicy,
153                                                            "https://dummyurl",
154                                                            handler);
155
156         jettySslListenerConfigurer.configure();
157         SslListener sslListener =
158                 jettySslListenerConfigurer.getSslListener();
159         
160         assertTrue("Keystore not set properly",
161                    sslListener.getKeystore().contains("resources/defaultkeystore"));
162         String JavaDoc trustStr = System.getProperty("javax.net.ssl.trustStore");
163         assertTrue("Trust store loaded success message not present",
164                    trustStr.contains("resources/defaulttruststore"));
165         assertTrue("Keystore type not being read",
166                    sslListener.getKeystoreType().equals("JKS"));
167         assertTrue("Keystore password not being read",
168                    sslServerPolicy.getKeystorePassword().equals("defaultkeypass"));
169         assertTrue("Key password not being read",
170                    sslServerPolicy.getKeyPassword().equals("defaultkeypass"));
171         
172         assertTrue("Ciphersuites is being being read from somewhere unknown",
173                    sslListener.getCipherSuites() == null);
174         assertTrue("Truststore type not being read",
175                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
176                                                   + "TrustStoreType"));
177
178         assertTrue("Secure socket protocol not being read",
179                    handler.checkLogContainsString("The secure socket protocol has been set to TLSv1."));
180         assertTrue("Session caching set but no warning about not supported",
181                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
182                                                   + "SessionCaching"));
183         assertTrue("SessionCacheKey caching set but no warning about not supported",
184                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
185                                                   + "SessionCacheKey"));
186         assertTrue("MaxChainLength caching set but no warning about not supported",
187                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
188                                                   + "MaxChainLength"));
189         assertTrue("CertValidator caching set but no warning about not supported",
190                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
191                                                   + "CertValidator"));
192     }
193     
194     public void testSetAllDataExceptKeystoreAndTrustStore() {
195         
196         SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
197         sslServerPolicy.setKeystore(null);
198         sslServerPolicy.setKeystoreType("JKS");
199         
200         sslServerPolicy.setKeyPassword("defaultkeypass");
201         sslServerPolicy.setKeystorePassword("defaultkeypass");
202         sslServerPolicy.setTrustStoreType("JKS");
203         sslServerPolicy.setTrustStoreAlgorithm("JKS");
204         sslServerPolicy.setSecureSocketProtocol("TLSv1");
205         sslServerPolicy.setSessionCacheKey("Anything");
206         sslServerPolicy.setSessionCaching(true);
207         sslServerPolicy.setMaxChainLength(new Long JavaDoc(2));
208         sslServerPolicy.setCertValidator("Anything");
209
210         
211         
212         sslServerPolicy.setTrustStore(null);
213         TestHandler handler = new TestHandler();
214         JettySslListenerConfigurer jettySslListenerConfigurer =
215                             createJettySslListenerConfigurer(sslServerPolicy,
216                                                            "https://dummyurl",
217                                                            handler);
218
219         jettySslListenerConfigurer.configure();
220         SslListener sslListener =
221                 jettySslListenerConfigurer.getSslListener();
222         
223         assertTrue("Keystore not set properly, sslListener.getKeystore() = " + sslListener.getKeystore(),
224                    sslListener.getKeystore().contains(".keystore"));
225         String JavaDoc trustStr = System.getProperty("javax.net.ssl.trustStore");
226         assertTrue("Trust store loaded success message not present",
227                    trustStr.contains("cacerts"));
228         assertTrue("Keystore type not being read",
229                    sslListener.getKeystoreType().equals("JKS"));
230         assertTrue("Keystore password not being read",
231                    sslServerPolicy.getKeystorePassword().equals("defaultkeypass"));
232         assertTrue("Key password not being read",
233                    sslServerPolicy.getKeyPassword().equals("defaultkeypass"));
234         
235         assertTrue("Ciphersuites is being being read from somewhere unknown",
236                    sslListener.getCipherSuites() == null);
237         assertTrue("Truststore type not being read",
238                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
239                                                   + "TrustStoreType"));
240
241         assertTrue("Secure socket protocol not being read",
242                    handler.checkLogContainsString("The secure socket protocol has been set to TLSv1."));
243         assertTrue("Session caching set but no warning about not supported",
244                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
245                                                   + "SessionCaching"));
246         assertTrue("SessionCacheKey caching set but no warning about not supported",
247                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
248                                                   + "SessionCacheKey"));
249         assertTrue("MaxChainLength caching set but no warning about not supported",
250                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
251                                                   + "MaxChainLength"));
252         assertTrue("CertValidator caching set but no warning about not supported",
253                    handler.checkLogContainsString("Unsupported SSLServerPolicy property : "
254                                                   + "CertValidator"));
255     }
256
257     public void testAllValidDataJKS() {
258         
259         String JavaDoc keyStoreStr = getPath("resources/defaultkeystore");
260         SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
261         sslServerPolicy.setKeystore(keyStoreStr);
262         sslServerPolicy.setKeyPassword("defaultkeypass");
263         sslServerPolicy.setKeystorePassword("defaultkeypass");
264         
265         sslServerPolicy.setKeystoreType("JKS");
266         String JavaDoc trustStoreStr = getPath("resources/defaulttruststore");
267         sslServerPolicy.setTrustStore(trustStoreStr);
268         TestHandler handler = new TestHandler();
269         JettySslListenerConfigurer jettySslListenerConfigurer =
270                             createJettySslListenerConfigurer(sslServerPolicy,
271                                                            "https://dummyurl",
272                                                            handler);
273
274         jettySslListenerConfigurer.configure();
275
276         
277     }
278     
279     public void testAllValidDataPKCS12() {
280         
281         String JavaDoc keyStoreStr = getPath("resources/celtix.p12");
282         SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
283         sslServerPolicy.setKeystore(keyStoreStr);
284         sslServerPolicy.setKeyPassword("celtixpass");
285         sslServerPolicy.setKeystorePassword("celtixpass");
286         
287         sslServerPolicy.setKeystoreType("PKCS12");
288         String JavaDoc trustStoreStr = getPath("resources/abigcompany_ca.pem");
289         sslServerPolicy.setTrustStore(trustStoreStr);
290         TestHandler handler = new TestHandler();
291         JettySslListenerConfigurer jettySslListenerConfigurer =
292                             createJettySslListenerConfigurer(sslServerPolicy,
293                                                            "https://dummyurl",
294                                                            handler);
295
296         jettySslListenerConfigurer.configure();
297
298         
299     }
300
301     
302         
303     
304     public void testAllElementsHaveSetupMethod() {
305         SSLServerPolicy sslServerPolicy = new SSLServerPolicy();
306         TestHandler handler = new TestHandler();
307         JettySslListenerConfigurer jettySslListenerConfigurer =
308             createJettySslListenerConfigurer(sslServerPolicy,
309                                            "https://dummyurl",
310                                            handler);
311         assertTrue("A new element has been "
312                    + "added to SSLServerPolicy without a corresponding "
313                    + "setup method in the configurer.",
314                    jettySslListenerConfigurer.testAllDataHasSetupMethod());
315     }
316     
317     
318     private JettySslListenerConfigurer createJettySslListenerConfigurer(
319                                              SSLServerPolicy sslServerPolicy,
320                                              String JavaDoc urlStr,
321                                              TestHandler handler) {
322         try {
323             
324             
325             
326             SslListener sslListener = new SslListener();
327             JettySslListenerConfigurer jettySslListenerConfigurer =
328                 new JettySslListenerConfigurer(configuration,
329                                                sslServerPolicy,
330                                                sslListener);
331             
332             jettySslListenerConfigurer.addLogHandler(handler);
333             return jettySslListenerConfigurer;
334             
335         } catch (Exception JavaDoc e) {
336             e.printStackTrace();
337         }
338         return null;
339     }
340     
341     protected static String JavaDoc getPath(String JavaDoc fileName) {
342         URL JavaDoc keystoreURL = JettySslListenerConfigurerTest.class.getResource(".");
343         String JavaDoc str = keystoreURL.getFile();
344         str += DROP_BACK_SRC_DIR + fileName;
345         return str;
346     }
347 }
348
349
350
Popular Tags