TestSSLSocketPair.java revision f7aab022dcbfcd8f27b409ab92b4bca4a84d0b8a
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.javax.net.ssl; 18 19import java.util.concurrent.Callable; 20import java.util.concurrent.ExecutorService; 21import java.util.concurrent.Executors; 22import java.util.concurrent.Future; 23import javax.net.ssl.SSLSocket; 24 25/** 26 * TestSSLSocketPair is a convenience class for other tests that want 27 * a pair of connected and handshaked client and server SSLSockets for 28 * testing. 29 */ 30public final class TestSSLSocketPair { 31 public final TestSSLContext c; 32 public final SSLSocket server; 33 public final SSLSocket client; 34 35 private TestSSLSocketPair (TestSSLContext c, 36 SSLSocket server, 37 SSLSocket client) { 38 this.c = c; 39 this.server = server; 40 this.client = client; 41 } 42 43 public void close() { 44 c.close(); 45 try { 46 server.close(); 47 client.close(); 48 } catch (Exception e) { 49 throw new RuntimeException(e); 50 } 51 } 52 53 /** 54 * based on test_SSLSocket_startHandshake 55 */ 56 public static TestSSLSocketPair create () { 57 TestSSLContext c = TestSSLContext.create(); 58 SSLSocket[] sockets = connect(c, null, null); 59 return new TestSSLSocketPair(c, sockets[0], sockets[1]); 60 } 61 62 /** 63 * Create a new connected server/client socket pair within a 64 * existing SSLContext. Optionally specify clientCipherSuites to 65 * allow forcing new SSLSession to test SSLSessionContext 66 * caching. Optionally specify serverCipherSuites for testing 67 * cipher suite negotiation. 68 */ 69 public static SSLSocket[] connect (final TestSSLContext context, 70 final String[] clientCipherSuites, 71 final String[] serverCipherSuites) { 72 try { 73 final SSLSocket client = (SSLSocket) 74 context.clientContext.getSocketFactory().createSocket(context.host, context.port); 75 final SSLSocket server = (SSLSocket) context.serverSocket.accept(); 76 77 ExecutorService executor = Executors.newFixedThreadPool(2); 78 Future s = executor.submit(new Callable<Void>() { 79 public Void call() throws Exception { 80 if (serverCipherSuites != null) { 81 server.setEnabledCipherSuites(serverCipherSuites); 82 } 83 server.startHandshake(); 84 return null; 85 } 86 }); 87 Future c = executor.submit(new Callable<Void>() { 88 public Void call() throws Exception { 89 if (clientCipherSuites != null) { 90 client.setEnabledCipherSuites(clientCipherSuites); 91 } 92 client.startHandshake(); 93 return null; 94 } 95 }); 96 executor.shutdown(); 97 98 s.get(); 99 c.get(); 100 return new SSLSocket[] { server, client }; 101 } catch (RuntimeException e) { 102 throw e; 103 } catch (Exception e) { 104 throw new RuntimeException(e); 105 } 106 } 107} 108 109