TestUtils.java revision 5f96702f582050c1598136ed2a748f76b981c94e
1/*
2 * Copyright (C) 2015 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 android.security.net.config;
18
19import java.net.Socket;
20import java.net.URL;
21import javax.net.ssl.HttpsURLConnection;
22import javax.net.ssl.SSLContext;
23import javax.net.ssl.SSLHandshakeException;
24import javax.net.ssl.TrustManager;
25
26import junit.framework.Assert;
27
28public final class TestUtils extends Assert {
29
30    private TestUtils() {
31    }
32
33    public static void assertConnectionFails(SSLContext context, String host, int port)
34            throws Exception {
35        try {
36            Socket s = context.getSocketFactory().createSocket(host, port);
37            s.getInputStream();
38            fail("Expected connection to " + host + ":" + port + " to fail.");
39        } catch (SSLHandshakeException expected) {
40        }
41    }
42
43    public static void assertConnectionSucceeds(SSLContext context, String host, int port)
44            throws Exception {
45        Socket s = context.getSocketFactory().createSocket(host, port);
46        s.getInputStream();
47    }
48
49    public static void assertUrlConnectionFails(SSLContext context, String host, int port)
50            throws Exception {
51        URL url = new URL("https://" + host + ":" + port);
52        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
53        connection.setSSLSocketFactory(context.getSocketFactory());
54        try {
55            connection.getInputStream();
56            fail("Connection to " + host + ":" + port + " expected to fail");
57        } catch (SSLHandshakeException expected) {
58            // ignored.
59        }
60    }
61
62    public static void assertUrlConnectionSucceeds(SSLContext context, String host, int port)
63            throws Exception {
64        URL url = new URL("https://" + host + ":" + port);
65        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
66        connection.setSSLSocketFactory(context.getSocketFactory());
67        connection.getInputStream();
68    }
69
70    public static SSLContext getSSLContext(ConfigSource source) throws Exception {
71        ApplicationConfig config = new ApplicationConfig(source);
72        SSLContext context = SSLContext.getInstance("TLS");
73        context.init(null, new TrustManager[] {config.getTrustManager()}, null);
74        return context;
75    }
76}
77