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