TestSSLSocketPair.java revision 6882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5
1bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom/*
2bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Copyright (C) 2010 The Android Open Source Project
3bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
4bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * you may not use this file except in compliance with the License.
6bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * You may obtain a copy of the License at
7bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
8bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom *
10bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * See the License for the specific language governing permissions and
14bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * limitations under the License.
15bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom */
16bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
17bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrompackage javax.net.ssl;
18bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
196882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstromimport java.util.concurrent.Callable;
206882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstromimport java.util.concurrent.ExecutorService;
216882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstromimport java.util.concurrent.Executors;
226882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstromimport java.util.concurrent.Future;
236882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom
24bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom/**
25bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * TestSSLSocketPair is a convenience class for other tests that want
26bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * a pair of connected and handshaked client and server SSLSockets for
27bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom * testing.
28bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom */
29bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrompublic final class TestSSLSocketPair {
30bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final TestSSLContext c;
31bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final SSLSocket server;
32bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    public final SSLSocket client;
33bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
34bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    private TestSSLSocketPair (TestSSLContext c,
35bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                               SSLSocket server,
36bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                               SSLSocket client) {
37bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.c = c;
38bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.server = server;
39bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        this.client = client;
40bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
41bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
42bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    /**
4317c744222e249ed5f7ab36e49ed11f9bb062a302Brian Carlstrom     * based on test_SSLSocket_startHandshake
44bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     */
4517c744222e249ed5f7ab36e49ed11f9bb062a302Brian Carlstrom    public static TestSSLSocketPair create () {
46bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        TestSSLContext c = TestSSLContext.create();
47204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom        SSLSocket[] sockets = connect(c, null, null);
48bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        return new TestSSLSocketPair(c, sockets[0], sockets[1]);
49bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
50bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
51bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    /**
52bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     * Create a new connected server/client socket pair within a
53204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom     * existing SSLContext. Optionally specify clientCipherSuites to
54204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom     * allow forcing new SSLSession to test SSLSessionContext
55204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom     * caching. Optionally specify serverCipherSuites for testing
56204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom     * cipher suite negotiation.
57bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom     */
586882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom    public static SSLSocket[] connect (final TestSSLContext context,
59204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                                       final String[] clientCipherSuites,
60204cab3c22b4d75c866c95e2d2eec42e14cbd924Brian Carlstrom                                       final String[] serverCipherSuites) {
61bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        try {
626882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            final SSLSocket client = (SSLSocket)
636882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                context.clientContext.getSocketFactory().createSocket(context.host, context.port);
646882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            final SSLSocket server = (SSLSocket) context.serverSocket.accept();
656882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom
666882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            ExecutorService executor = Executors.newFixedThreadPool(2);
676882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            Future s = executor.submit(new Callable<Void>() {
686882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                    public Void call() throws Exception {
696882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        if (serverCipherSuites != null) {
706882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                            server.setEnabledCipherSuites(serverCipherSuites);
716882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        }
726882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        server.startHandshake();
736882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        return null;
746882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                    }
756882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                });
766882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            Future c = executor.submit(new Callable<Void>() {
776882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                    public Void call() throws Exception {
786882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        if (clientCipherSuites != null) {
796882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                            client.setEnabledCipherSuites(clientCipherSuites);
80bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                        }
816882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        client.startHandshake();
826882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom                        return null;
83bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                    }
84bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom                });
856882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            executor.shutdown();
866882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom
876882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            s.get();
886882e31b7ce2d04ebbc91c7a55d7840e8fdce8a5Brian Carlstrom            c.get();
8917c744222e249ed5f7ab36e49ed11f9bb062a302Brian Carlstrom            return new SSLSocket[] { server, client };
90bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        } catch (RuntimeException e) {
91bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom            throw e;
92bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        } catch (Exception e) {
93bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom            throw new RuntimeException(e);
94bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom        }
95bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom    }
96bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom}
97bcfb325d5b1f9529b439cc0805a1c140521510f7Brian Carlstrom
98