001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap.ext;
019:
020: import java.io.IOException;
021: import java.net.InetAddress;
022: import java.net.ServerSocket;
023: import java.net.Socket;
024: import java.net.UnknownHostException;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import javax.net.ssl.HandshakeCompletedListener;
029: import javax.net.ssl.HostnameVerifier;
030: import javax.net.ssl.SSLSession;
031: import javax.net.ssl.SSLSocket;
032: import javax.net.ssl.SSLSocketFactory;
033:
034: import junit.framework.TestCase;
035:
036: public class StartTlsResponseImplTest extends TestCase {
037:
038: private final String HOST = "127.0.0.1";
039:
040: private final int PORT = 12345;
041:
042: public StartTlsResponseImplTest(String name) {
043: super (name);
044: }
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: protected void tearDown() throws Exception {
051: super .tearDown();
052: }
053:
054: /*
055: * After negotiation, a SSlSocket will be created and attach to the
056: * underlying Socket which is used before negotiation. This test case
057: * compare the local port, peer's host, peer' port before and after the
058: * negotiation. Test case fail If they are different.
059: */
060: public void test_negotiate() throws IOException {
061:
062: // This serverSocket is never used, just for creating a client socket.
063: ServerSocket server = new ServerSocket(PORT);
064:
065: Socket socket = new Socket(HOST, PORT);
066: MockSSLSocketFactory factory = new MockSSLSocketFactory();
067:
068: StartTlsResponseImpl tls = new StartTlsResponseImpl();
069:
070: tls.setSocket(socket);
071:
072: tls.setHostnameVerifier(new HostnameVerifier() {
073: public boolean verify(String hostname, SSLSession session) {
074: return true;
075: }
076: });
077:
078: tls.negotiate(factory);
079:
080: //Fail if SSLSocket is not created.
081: assertTrue(tls.getSSLSocket() instanceof SSLSocket);
082:
083: assertEquals(socket.getInetAddress().getHostName(), tls
084: .getSSLSocket().getInetAddress().getHostName());
085: assertEquals(socket.getPort(), tls.getSSLSocket().getPort());
086: assertEquals(socket.getLocalPort(), tls.getSSLSocket()
087: .getLocalPort());
088:
089: socket.close();
090: }
091:
092: /*
093: * This MockSSLSocketFactory is used for create and SSLSocket above an
094: * underlying simple socket. It won't do any connection.
095: */
096: public static class MockSSLSocketFactory extends SSLSocketFactory {
097:
098: /*
099: * Create a MockSSLSocket, and attach it to an underlying simple socket.
100: * Won't do any connection.
101: */
102: @Override
103: public Socket createSocket(Socket s, String host, int port,
104: boolean autoClose) throws IOException {
105: SSLSocket sslSocket = new MockSSLSocket(s, host, port);
106: return sslSocket;
107: }
108:
109: @Override
110: public String[] getDefaultCipherSuites() {
111: throw new Error("should not be here");
112: }
113:
114: @Override
115: public String[] getSupportedCipherSuites() {
116: throw new Error("should not be here");
117: }
118:
119: @Override
120: public Socket createSocket(String host, int port)
121: throws IOException, UnknownHostException {
122: throw new Error("should not be here");
123: }
124:
125: @Override
126: public Socket createSocket(String host, int port,
127: InetAddress localHost, int localPort)
128: throws IOException, UnknownHostException {
129: throw new Error("should not be here");
130: }
131:
132: @Override
133: public Socket createSocket(InetAddress host, int port)
134: throws IOException {
135: throw new Error("should not be here");
136: }
137:
138: @Override
139: public Socket createSocket(InetAddress address, int port,
140: InetAddress localAddress, int localPort)
141: throws IOException {
142: throw new Error("should not be here");
143: }
144:
145: }
146:
147: /*
148: * This MocketSSLSocket won't do any connection, nor any communication with
149: * a peer. It only stores peer host name, peer port, local host name and
150: * local port.
151: */
152: public static class MockSSLSocket extends SSLSocket {
153:
154: private int port; // peer's port
155:
156: private String localHost;
157:
158: private int localPort;
159:
160: private String[] suites;
161:
162: private List<HandshakeCompletedListener> listeners;
163:
164: private InetAddress address; // peer's address
165:
166: protected MockSSLSocket(Socket socket, String host, int port)
167: throws UnknownHostException {
168: this .port = port;
169: this .localHost = socket.getLocalAddress().getHostAddress();
170: this .localPort = socket.getLocalPort();
171: address = InetAddress.getByName(host);
172: listeners = new LinkedList<HandshakeCompletedListener>();
173: }
174:
175: /*
176: * Won't do any handshake with the peer, just notify registed listeners.
177: */
178: @Override
179: public void startHandshake() throws IOException {
180: for (HandshakeCompletedListener listener : listeners) {
181: if (this .listeners != null) {
182: listener.handshakeCompleted(null);
183: } else {
184: throw new Error(
185: "should not be here, at MockHandshake.startHandshake()");
186: }
187: }
188: }
189:
190: @Override
191: public void addHandshakeCompletedListener(
192: HandshakeCompletedListener listener) {
193: listeners.add(listener);
194: }
195:
196: @Override
197: public SSLSession getSession() {
198: return null;
199: }
200:
201: @Override
202: public void setEnabledCipherSuites(String[] suites) {
203: this .suites = suites;
204: }
205:
206: @Override
207: public String[] getEnabledCipherSuites() {
208: return this .suites;
209: }
210:
211: public InetAddress getInetAddress() {
212: return this .address;
213: }
214:
215: public int getPort() {
216: return this .port;
217: }
218:
219: public String getLocalHost() {
220: return this .localHost;
221: }
222:
223: public int getLocalPort() {
224: return this .localPort;
225: }
226:
227: @Override
228: public boolean getEnableSessionCreation() {
229: throw new Error("should not be here");
230: }
231:
232: @Override
233: public String[] getEnabledProtocols() {
234: throw new Error("should not be here");
235: }
236:
237: @Override
238: public boolean getNeedClientAuth() {
239: throw new Error("should not be here");
240: }
241:
242: @Override
243: public String[] getSupportedCipherSuites() {
244: throw new Error("should not be here");
245: }
246:
247: @Override
248: public String[] getSupportedProtocols() {
249: throw new Error("should not be here");
250: }
251:
252: @Override
253: public boolean getUseClientMode() {
254: throw new Error("should not be here");
255: }
256:
257: @Override
258: public boolean getWantClientAuth() {
259: throw new Error("should not be here");
260: }
261:
262: @Override
263: public void removeHandshakeCompletedListener(
264: HandshakeCompletedListener listener) {
265: throw new Error("should not be here");
266: }
267:
268: @Override
269: public void setEnableSessionCreation(boolean flag) {
270: throw new Error("should not be here");
271: }
272:
273: @Override
274: public void setEnabledProtocols(String[] protocols) {
275: throw new Error("should not be here");
276: }
277:
278: @Override
279: public void setNeedClientAuth(boolean need) {
280: throw new Error("should not be here");
281: }
282:
283: @Override
284: public void setUseClientMode(boolean mode) {
285: throw new Error("should not be here");
286: }
287:
288: @Override
289: public void setWantClientAuth(boolean want) {
290: throw new Error("should not be here");
291: }
292: }
293:
294: }
|