001: package org.objectweb.celtix.bus.transports.https;
002:
003: import java.net.URL;
004: import java.net.URLConnection;
005: import java.util.Properties;
006:
007: import javax.net.ssl.SSLSocketFactory;
008:
009: import junit.extensions.TestSetup;
010: import junit.framework.Test;
011: import junit.framework.TestCase;
012: import junit.framework.TestSuite;
013:
014: import org.easymock.classextension.EasyMock;
015: import org.objectweb.celtix.Bus;
016: import org.objectweb.celtix.BusException;
017: import org.objectweb.celtix.bus.configuration.security.SSLClientPolicy;
018: import org.objectweb.celtix.configuration.Configuration;
019:
020: public class JettySslClientSystemPropertiesConfigurerTest extends
021: TestCase {
022:
023: private static final String DROP_BACK_SRC_DIR = "../../../../../../../../src/test/java/org/objectweb/celtix/bus/transports/https/";
024:
025: Bus bus;
026:
027: public JettySslClientSystemPropertiesConfigurerTest(String arg0) {
028: super (arg0);
029: }
030:
031: public static Test suite() throws Exception {
032: TestSuite suite = new TestSuite(
033: JettySslClientSystemPropertiesConfigurerTest.class);
034: return new TestSetup(suite) {
035: protected void tearDown() throws Exception {
036: super .tearDown();
037: }
038: };
039: }
040:
041: public static void main(String[] args) {
042: junit.textui.TestRunner
043: .run(JettySslClientSystemPropertiesConfigurerTest.class);
044: }
045:
046: public void setUp() throws BusException {
047: bus = EasyMock.createMock(Bus.class);
048: }
049:
050: public void tearDown() throws Exception {
051: EasyMock.reset(bus);
052: Properties props = System.getProperties();
053: props.remove("javax.net.ssl.trustStore");
054: props.remove("javax.net.ssl.keyStore");
055: props.remove("javax.net.ssl.keyPassword");
056: props.remove("javax.net.ssl.keyStorePassword");
057: }
058:
059: public void testSetAllDataSomeSystemProperties() {
060:
061: String keyStoreStr = getPath("resources/defaultkeystore");
062: SSLClientPolicy sslClientPolicy = new SSLClientPolicy();
063: System.setProperty("javax.net.ssl.keyStore", keyStoreStr);
064: sslClientPolicy.setKeystoreType("JKS");
065:
066: System.setProperty("javax.net.ssl.keyStorePassword",
067: "defaultkeypass");
068: System.setProperty("javax.net.ssl.keyPassword",
069: "defaultkeypass");
070: sslClientPolicy.setTrustStoreType("JKS");
071: sslClientPolicy.setTrustStoreAlgorithm("JKS");
072: sslClientPolicy.setSecureSocketProtocol("TLSv1");
073: sslClientPolicy.setSessionCacheKey("Anything");
074: sslClientPolicy.setSessionCaching(true);
075: sslClientPolicy.setMaxChainLength(new Long(2));
076: sslClientPolicy.setCertValidator("Anything");
077: sslClientPolicy.setProxyHost("Anything");
078: sslClientPolicy.setProxyPort(new Long(1234));
079:
080: String trustStoreStr = getPath("resources/defaulttruststore");
081: System.setProperty("javax.net.ssl.trustStore", trustStoreStr);
082: TestHandler handler = new TestHandler();
083: JettySslClientConfigurer jettySslClientConfigurer = createJettySslClientConfigurer(
084: sslClientPolicy, "https://dummyurl", handler);
085:
086: jettySslClientConfigurer.configure();
087: SSLSocketFactory sSLSocketFactory = jettySslClientConfigurer
088: .getHttpsConnection().getSSLSocketFactory();
089:
090: assertTrue("sSLSocketFactory not correct, sSLSocketFactory = "
091: + sSLSocketFactory,
092: sSLSocketFactory instanceof SSLSocketFactoryWrapper);
093: assertTrue(
094: "Keystore loaded success message not present",
095: handler
096: .checkLogContainsString("Successfully loaded keystore"));
097: assertTrue(
098: "Trust store loaded success message not present",
099: handler
100: .checkLogContainsString("Successfully loaded trust store"));
101: assertTrue(
102: "Keystore type not being read",
103: handler
104: .checkLogContainsString("The key store type has been set in configuration to JKS"));
105: assertTrue(
106: "Keystore password not being read",
107: handler
108: .checkLogContainsString("The key store password was found to be set "
109: + "as a system property and will be used."));
110: assertTrue(
111: "Key password not being read",
112: handler
113: .checkLogContainsString("The key password was found to be set as a "
114: + "system property and will be used."));
115: assertTrue(
116: "Key manager factory is being being read from somewhere unknown",
117: handler
118: .checkLogContainsString("The keystore key manager factory "
119: + "algorithm has not been set in configuration "
120: + "so the default value SunX509 will be used."));
121:
122: assertTrue(
123: "Trust manager factory is being being read from somewhere unknown",
124: handler
125: .checkLogContainsString("The truststore key manager factory "
126: + "algorithm has not been set in configuration "
127: + "so the default value PKIX will be used."));
128: assertTrue(
129: "Trust store location not read successfully",
130: handler
131: .checkLogContainsString("The trust store location has been "
132: + "via a system property to"));
133:
134: assertTrue(
135: "Ciphersuites is being being read from somewhere unknown",
136: handler
137: .checkLogContainsString("The cipher suite has not been set, default values "
138: + "will be used."));
139: assertTrue(
140: "Truststore type not being read",
141: handler
142: .checkLogContainsString("The key store type has been set in "
143: + "configuration to JKS"));
144:
145: assertTrue(
146: "Secure socket protocol not being read",
147: handler
148: .checkLogContainsString("The secure socket protocol has been set to TLSv1."));
149: assertTrue(
150: "Session caching set but no warning about not supported",
151: handler
152: .checkLogContainsString("Unsupported SSLClientPolicy property : SessionCaching"));
153: assertTrue(
154: "SessionCacheKey caching set but no warning about not supported",
155: handler
156: .checkLogContainsString("Unsupported SSLClientPolicy property : SessionCacheKey"));
157: assertTrue(
158: "MaxChainLength caching set but no warning about not supported",
159: handler
160: .checkLogContainsString("Unsupported SSLClientPolicy property : MaxChainLength"));
161: assertTrue(
162: "CertValidator caching set but no warning about not supported",
163: handler
164: .checkLogContainsString("Unsupported SSLClientPolicy property : CertValidator"));
165: }
166:
167: private JettySslClientConfigurer createJettySslClientConfigurer(
168: SSLClientPolicy sslClientPolicy, String urlStr,
169: TestHandler handler) {
170: try {
171: URL url = new URL(urlStr);
172: URLConnection connection = new DummyHttpsConnection(url);
173: Configuration configuration = EasyMock
174: .createMock(Configuration.class);
175: JettySslClientConfigurer jettySslClientConfigurer = new JettySslClientConfigurer(
176: sslClientPolicy, connection, configuration);
177:
178: jettySslClientConfigurer.addLogHandler(handler);
179: return jettySslClientConfigurer;
180:
181: } catch (Exception e) {
182: e.printStackTrace();
183: }
184: return null;
185: }
186:
187: private String getPath(String fileName) {
188: URL keystoreURL = JettySslClientSystemPropertiesConfigurerTest.class
189: .getResource(".");
190: String str = keystoreURL.getFile();
191: str += DROP_BACK_SRC_DIR + fileName;
192: return str;
193: }
194: }
|