1 package org.objectweb.celtix.bus.transports.https; 2 3 import java.net.URL ; 4 import java.util.Properties ; 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 JettySslListenerSystemPropertiesConfigurerTest extends TestCase { 19 20 21 private static final String DROP_BACK_SRC_DIR = 22 "../../../../../../../../src/test/java/org/objectweb/celtix/bus/transports/https/"; 23 24 Bus bus; 25 26 27 28 29 public JettySslListenerSystemPropertiesConfigurerTest(String arg0) { 30 super(arg0); 31 } 32 33 public static Test suite() throws Exception { 34 TestSuite suite = new TestSuite(JettySslListenerSystemPropertiesConfigurerTest.class); 35 return new TestSetup(suite) { 36 protected void tearDown() throws Exception { 37 super.tearDown(); 38 } 39 }; 40 } 41 42 43 public static void main(String [] args) { 44 junit.textui.TestRunner.run(JettySslListenerSystemPropertiesConfigurerTest.class); 45 } 46 47 public void setUp() throws BusException { 48 bus = EasyMock.createMock(Bus.class); 49 } 50 51 public void tearDown() throws Exception { 52 EasyMock.reset(bus); 53 Properties props = System.getProperties(); 54 props.remove("javax.net.ssl.trustStore"); 55 props.remove("javax.net.ssl.keyStore"); 56 props.remove("javax.net.ssl.keyPassword"); 57 props.remove("javax.net.ssl.keyStorePassword"); 58 } 59 60 public void testSetAllData() { 61 62 String keyStoreStr = getPath("resources/defaultkeystore"); 63 SSLServerPolicy sslServerPolicy = new SSLServerPolicy(); 64 System.setProperty("javax.net.ssl.keyStore", keyStoreStr); 65 System.setProperty("javax.net.ssl.keyStorePassword", "defaultkeypass"); 66 sslServerPolicy.setTrustStoreType("JKS"); 67 sslServerPolicy.setTrustStoreAlgorithm("JKS"); 68 sslServerPolicy.setSecureSocketProtocol("TLSv1"); 69 sslServerPolicy.setKeystoreAlgorithm("Anything"); 70 sslServerPolicy.setSessionCacheKey("Anything"); 71 sslServerPolicy.setSessionCaching(true); 72 sslServerPolicy.setMaxChainLength(new Long (2)); 73 sslServerPolicy.setCertValidator("Anything"); 74 75 76 String trustStoreStr = getPath("resources/defaulttruststore"); 77 System.setProperty("javax.net.ssl.trustStore", trustStoreStr); 78 TestHandler handler = new TestHandler(); 79 JettySslListenerConfigurer jettySslListenerConfigurer = 80 createJettySslListenerConfigurer(sslServerPolicy, 81 "https://dummyurl", 82 handler); 83 84 jettySslListenerConfigurer.configure(); 85 SslListener sslListener = 86 jettySslListenerConfigurer.getSslListener(); 87 88 assertTrue("Keystore not set properly", 89 sslListener.getKeystore().contains("resources/defaultkeystore")); 90 String trustStr = System.getProperty("javax.net.ssl.trustStore"); 91 assertTrue("Trust store loaded success message not present", 92 trustStr.contains("resources/defaulttruststore")); 93 assertTrue("Keystore type not being read", 94 sslListener.getKeystoreType().equals("PKCS12")); 95 assertTrue("Couldn't deal with case when SSLServerPolicy keystore password is null", 96 sslServerPolicy.getKeystorePassword() == null); 97 assertTrue("Couldn't deal with case when SSLServerPolicy key password is null", 98 sslServerPolicy.getKeyPassword() == null); 99 100 assertTrue("Ciphersuites is being being read from somewhere unknown", 101 sslListener.getCipherSuites() == null); 102 assertTrue("Truststore type not being read", 103 handler.checkLogContainsString("Unsupported SSLServerPolicy property : " 104 + "TrustStoreType")); 105 106 assertTrue("Secure socket protocol not being read", 107 handler.checkLogContainsString("The secure socket protocol has been set to TLSv1.")); 108 assertTrue("Session caching set but no warning about not supported", 109 handler.checkLogContainsString("Unsupported SSLServerPolicy property : " 110 + "SessionCaching")); 111 assertTrue("SessionCacheKey caching set but no warning about not supported", 112 handler.checkLogContainsString("Unsupported SSLServerPolicy property : " 113 + "SessionCacheKey")); 114 assertTrue("MaxChainLength caching set but no warning about not supported", 115 handler.checkLogContainsString("Unsupported SSLServerPolicy property : " 116 + "MaxChainLength")); 117 assertTrue("CertValidator caching set but no warning about not supported", 118 handler.checkLogContainsString("Unsupported SSLServerPolicy property : " 119 + "CertValidator")); 120 } 121 122 123 private JettySslListenerConfigurer createJettySslListenerConfigurer( 124 SSLServerPolicy sslServerPolicy, 125 String urlStr, 126 TestHandler handler) { 127 try { 128 Configuration configuration = EasyMock.createMock(Configuration.class); 129 130 131 SslListener sslListener = new SslListener(); 132 JettySslListenerConfigurer jettySslListenerConfigurer = 133 new JettySslListenerConfigurer(configuration, 134 sslServerPolicy, 135 sslListener); 136 137 jettySslListenerConfigurer.addLogHandler(handler); 138 return jettySslListenerConfigurer; 139 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 return null; 144 } 145 146 protected static String getPath(String fileName) { 147 URL keystoreURL = JettySslListenerSystemPropertiesConfigurerTest.class.getResource("."); 148 String str = keystoreURL.getFile(); 149 str += DROP_BACK_SRC_DIR + fileName; 150 return str; 151 } 152 } 153 154 155 | Popular Tags |