001: package org.objectweb.celtix.bus.transports.https;
002:
003: import java.net.InetAddress;
004: import java.net.Socket;
005: import java.net.URL;
006: import java.util.Properties;
007:
008: import javax.net.ssl.SSLSocket;
009: import javax.net.ssl.SSLSocketFactory;
010:
011: import junit.extensions.TestSetup;
012: import junit.framework.Test;
013: import junit.framework.TestCase;
014: import junit.framework.TestSuite;
015:
016: import org.easymock.classextension.EasyMock;
017: import org.objectweb.celtix.Bus;
018: import org.objectweb.celtix.BusException;
019:
020: public class SSLSocketFactoryWrapperTest extends TestCase {
021:
022: private static final String DROP_BACK_SRC_DIR = "../../../../../../../../src/test/java/org/objectweb/celtix/bus/transports/https/";
023:
024: Bus bus;
025:
026: private SSLSocketFactory sslSocketFactory;
027: private SSLSocket mockSocket;
028:
029: public SSLSocketFactoryWrapperTest(String arg0) {
030: super (arg0);
031: }
032:
033: public static Test suite() throws Exception {
034: TestSuite suite = new TestSuite(
035: SSLSocketFactoryWrapperTest.class);
036: return new TestSetup(suite) {
037: protected void tearDown() throws Exception {
038: super .tearDown();
039: }
040: };
041: }
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(SSLSocketFactoryWrapperTest.class);
045: }
046:
047: public void setUp() throws BusException {
048: bus = EasyMock.createMock(Bus.class);
049: sslSocketFactory = EasyMock.createMock(SSLSocketFactory.class);
050: mockSocket = EasyMock.createMock(SSLSocket.class);
051:
052: }
053:
054: public void tearDown() throws Exception {
055: EasyMock.reset(bus);
056: EasyMock.reset(sslSocketFactory);
057: EasyMock.reset(mockSocket);
058: Properties props = System.getProperties();
059: props.remove("javax.net.ssl.trustStore");
060: props.remove("javax.net.ssl.keyStore");
061: props.remove("javax.net.ssl.keyPassword");
062: props.remove("javax.net.ssl.keyStorePassword");
063: }
064:
065: public void testConstructor1() {
066: TestHandler handler = new TestHandler();
067:
068: try {
069: String[] ciphs = new String[] { "a", "b", "c" };
070: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
071: ciphs, handler);
072: EasyMock.expect(
073: sslSocketFactory.createSocket((String) EasyMock
074: .anyObject(), EasyMock.anyInt()))
075: .andReturn(mockSocket);
076: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
077: .andReturn(ciphs);
078: EasyMock
079: .expect(sslSocketFactory.getSupportedCipherSuites())
080: .andReturn(ciphs);
081: EasyMock.replay(sslSocketFactory);
082: SSLSocket socket = (SSLSocket) sslSocketFactoryWrapper
083: .createSocket("localhost", 9001);
084: assertTrue("Expected socket != null", socket != null);
085: String[] defCiphers = sslSocketFactoryWrapper
086: .getDefaultCipherSuites();
087: assertTrue("Default Cipher suite not as expected",
088: defCiphers[1].equals("b"));
089: String[] suppCiphers = sslSocketFactoryWrapper
090: .getSupportedCipherSuites();
091: assertTrue("Supported Cipher suite not as expected",
092: suppCiphers[1].equals("b"));
093:
094: EasyMock.reset(sslSocketFactory);
095: EasyMock.reset(mockSocket);
096: EasyMock.expect(
097: sslSocketFactory.createSocket((String) EasyMock
098: .anyObject(), EasyMock.anyInt()))
099: .andReturn(null);
100: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
101: .andReturn(null);
102: EasyMock
103: .expect(sslSocketFactory.getSupportedCipherSuites())
104: .andReturn(null);
105: EasyMock.replay(sslSocketFactory);
106: socket = (SSLSocket) sslSocketFactoryWrapper.createSocket(
107: "localhost", 9001);
108: assertTrue("Expected socket == null", socket == null);
109:
110: } catch (Exception e) {
111: assertTrue("Caught an unexpected exception e = " + e, false);
112: }
113: }
114:
115: public void testConstructor2() {
116: TestHandler handler = new TestHandler();
117:
118: try {
119: String[] ciphs = new String[] {};
120: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
121: ciphs, handler);
122: EasyMock.expect(
123: sslSocketFactory.createSocket((Socket) EasyMock
124: .anyObject(),
125: (String) EasyMock.anyObject(), EasyMock
126: .anyInt(), EasyMock.anyBoolean()))
127: .andReturn(mockSocket);
128: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
129: .andReturn(ciphs);
130: EasyMock
131: .expect(sslSocketFactory.getSupportedCipherSuites())
132: .andReturn(ciphs);
133: EasyMock.replay(sslSocketFactory);
134: Socket socket = sslSocketFactoryWrapper.createSocket(null,
135: "localhost", 9001, true);
136: assertTrue("Expected socket != null", socket != null);
137:
138: EasyMock.reset(sslSocketFactory);
139: EasyMock.reset(mockSocket);
140: EasyMock.expect(
141: sslSocketFactory.createSocket((Socket) EasyMock
142: .anyObject(),
143: (String) EasyMock.anyObject(), EasyMock
144: .anyInt(), EasyMock.anyBoolean()))
145: .andReturn(null);
146: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
147: .andReturn(null);
148: EasyMock
149: .expect(sslSocketFactory.getSupportedCipherSuites())
150: .andReturn(null);
151: EasyMock.replay(sslSocketFactory);
152: socket = sslSocketFactoryWrapper.createSocket(null,
153: "localhost", 9001, true);
154: assertTrue("Expected socket == null", socket == null);
155: } catch (Exception e) {
156: assertTrue("Caught an unexpected exception e = " + e, false);
157: }
158: }
159:
160: public void testConstructor3() {
161: TestHandler handler = new TestHandler();
162: try {
163: String[] ciphs = new String[] { "a", "b", "c" };
164: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
165: ciphs, handler);
166: EasyMock.expect(
167: sslSocketFactory.createSocket((String) EasyMock
168: .anyObject(), EasyMock.anyInt(),
169: (InetAddress) EasyMock.anyObject(),
170: EasyMock.anyInt())).andReturn(mockSocket);
171: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
172: .andReturn(ciphs);
173: EasyMock
174: .expect(sslSocketFactory.getSupportedCipherSuites())
175: .andReturn(ciphs);
176: EasyMock.replay(sslSocketFactory);
177: Socket socket = sslSocketFactoryWrapper.createSocket(
178: "localhost", 9001, null, 9001);
179: assertTrue("Expected socket != null", socket != null);
180:
181: EasyMock.reset(sslSocketFactory);
182: EasyMock.reset(mockSocket);
183: EasyMock.expect(
184: sslSocketFactory.createSocket((String) EasyMock
185: .anyObject(), EasyMock.anyInt(),
186: (InetAddress) EasyMock.anyObject(),
187: EasyMock.anyInt())).andReturn(null);
188: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
189: .andReturn(null);
190: EasyMock
191: .expect(sslSocketFactory.getSupportedCipherSuites())
192: .andReturn(null);
193: EasyMock.replay(sslSocketFactory);
194: socket = sslSocketFactoryWrapper.createSocket("localhost",
195: 9001, null, 9001);
196: assertTrue("Expected socket == null", socket == null);
197:
198: } catch (Exception e) {
199: assertTrue("Caught an unexpected exception e = " + e, false);
200: }
201: }
202:
203: public void testConstructor4() {
204: TestHandler handler = new TestHandler();
205:
206: try {
207: String[] ciphs = new String[] { "a", "b", "c" };
208: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
209: ciphs, handler);
210: EasyMock.expect(
211: sslSocketFactory.createSocket(
212: (InetAddress) EasyMock.anyObject(),
213: EasyMock.anyInt())).andReturn(mockSocket);
214: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
215: .andReturn(ciphs);
216: EasyMock
217: .expect(sslSocketFactory.getSupportedCipherSuites())
218: .andReturn(ciphs);
219: EasyMock.replay(sslSocketFactory);
220: InetAddress addr = InetAddress.getLocalHost();
221: Socket socket = sslSocketFactoryWrapper.createSocket(addr,
222: 9001);
223: assertTrue("Expected socket != null", socket != null);
224:
225: EasyMock.reset(sslSocketFactory);
226: EasyMock.reset(mockSocket);
227: EasyMock.expect(
228: sslSocketFactory.createSocket(
229: (InetAddress) EasyMock.anyObject(),
230: EasyMock.anyInt())).andReturn(null);
231: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
232: .andReturn(null);
233: EasyMock
234: .expect(sslSocketFactory.getSupportedCipherSuites())
235: .andReturn(null);
236: EasyMock.replay(sslSocketFactory);
237:
238: socket = sslSocketFactoryWrapper.createSocket(addr, 9001);
239: assertTrue("Expected socket == null", socket == null);
240:
241: } catch (Exception e) {
242: assertTrue("Caught an unexpected exception e = " + e, false);
243: }
244: }
245:
246: public void testConstructor5() {
247: TestHandler handler = new TestHandler();
248:
249: try {
250: String[] ciphs = new String[] { "a", "b", "c" };
251: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
252: ciphs, handler);
253: EasyMock.expect(
254: sslSocketFactory.createSocket(
255: (InetAddress) EasyMock.anyObject(),
256: EasyMock.anyInt(), (InetAddress) EasyMock
257: .anyObject(), EasyMock.anyInt()))
258: .andReturn(mockSocket);
259: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
260: .andReturn(ciphs);
261: EasyMock
262: .expect(sslSocketFactory.getSupportedCipherSuites())
263: .andReturn(ciphs);
264: EasyMock.replay(sslSocketFactory);
265: InetAddress addr = InetAddress.getLocalHost();
266: Socket socket = sslSocketFactoryWrapper.createSocket(addr,
267: 9001, addr, 9001);
268: assertTrue("Expected socket != null", socket != null);
269:
270: } catch (Exception e) {
271: assertTrue("Caught an unexpected exception e = " + e, false);
272: }
273: }
274:
275: public void testConstructor6() {
276: TestHandler handler = new TestHandler();
277:
278: try {
279: String[] ciphs = null;
280: SSLSocketFactoryWrapper sslSocketFactoryWrapper = createSSLSocketFactoryWrapper(
281: ciphs, handler);
282: EasyMock.expect(
283: sslSocketFactory.createSocket(
284: (InetAddress) EasyMock.anyObject(),
285: EasyMock.anyInt(), (InetAddress) EasyMock
286: .anyObject(), EasyMock.anyInt()))
287: .andReturn(mockSocket);
288: EasyMock.expect(sslSocketFactory.getDefaultCipherSuites())
289: .andReturn(ciphs);
290: EasyMock
291: .expect(sslSocketFactory.getSupportedCipherSuites())
292: .andReturn(ciphs);
293: EasyMock.replay(sslSocketFactory);
294: InetAddress addr = InetAddress.getLocalHost();
295: Socket socket = sslSocketFactoryWrapper.createSocket(addr,
296: 9001, addr, 9001);
297: assertTrue("Expected socket != null", socket != null);
298:
299: } catch (Exception e) {
300: assertTrue("Caught an unexpected exception e = " + e, false);
301: }
302: }
303:
304: private SSLSocketFactoryWrapper createSSLSocketFactoryWrapper(
305: String[] cipherSuite, TestHandler handler) {
306: try {
307:
308: SSLSocketFactoryWrapper sslSocketFactoryWrapper = new SSLSocketFactoryWrapper(
309: sslSocketFactory, cipherSuite);
310:
311: sslSocketFactoryWrapper.addLogHandler(handler);
312: return sslSocketFactoryWrapper;
313:
314: } catch (Exception e) {
315: e.printStackTrace();
316: }
317: return null;
318: }
319:
320: protected static String getPath(String fileName) {
321: URL keystoreURL = SSLSocketFactoryWrapperTest.class
322: .getResource(".");
323: String str = keystoreURL.getFile();
324: str += DROP_BACK_SRC_DIR + fileName;
325: return str;
326: }
327: }
|