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 javax.net.ssl.SSLSession;
20import javax.net.ssl.SSLSocket;
21import javax.net.ssl.SSLSocketFactory;
22
23/**
24 * TestSSLSessions is a convenience class for other tests that want
25 * precreated SSLSessions for testing. It contains a connected
26 * client/server pair of SSLSession as well as an invalid SSLSession.
27 */
28public final class TestSSLSessions {
29
30    /**
31     * An invalid session that is not connected
32     */
33    public final SSLSession invalid;
34
35    /**
36     * The server side of a connected session
37     */
38    public final SSLSession server;
39
40    /**
41     * The client side of a connected session
42     */
43    public final SSLSession client;
44
45    /**
46     * The associated SSLSocketTest.Helper that is the source of
47     * the client and server SSLSessions.
48     */
49    public final TestSSLSocketPair s;
50
51    private TestSSLSessions(SSLSession invalid,
52                            SSLSession server,
53                            SSLSession client,
54                            TestSSLSocketPair s) {
55        this.invalid = invalid;
56        this.server = server;
57        this.client = client;
58        this.s = s;
59    }
60
61    public void close() {
62        s.close();
63    }
64
65    public static final TestSSLSessions create() {
66        try {
67            SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
68            SSLSocket ssl = (SSLSocket) sf.createSocket();
69            SSLSession invalid = ssl.getSession();
70            TestSSLSocketPair s = TestSSLSocketPair.create();
71            return new TestSSLSessions(invalid, s.server.getSession(), s.client.getSession(), s);
72        } catch (Exception e) {
73            throw new RuntimeException(e);
74        }
75    }
76}
77