blob: 9dec21be7f37f664066d87e2667a542b81b865ff [file] [log] [blame]
Chad Brubaker5f967022015-11-04 23:55:29 -08001/*
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
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070019import android.content.pm.ApplicationInfo;
20import android.os.Build;
Chad Brubaker5f967022015-11-04 23:55:29 -080021import java.net.Socket;
22import java.net.URL;
23import javax.net.ssl.HttpsURLConnection;
24import javax.net.ssl.SSLContext;
25import javax.net.ssl.SSLHandshakeException;
26import javax.net.ssl.TrustManager;
Chad Brubaker5a1078f2015-11-10 12:26:18 -080027import javax.net.ssl.TrustManagerFactory;
Chad Brubaker5f967022015-11-04 23:55:29 -080028
29import junit.framework.Assert;
30
31public final class TestUtils extends Assert {
32
33 private TestUtils() {
34 }
35
36 public static void assertConnectionFails(SSLContext context, String host, int port)
37 throws Exception {
38 try {
39 Socket s = context.getSocketFactory().createSocket(host, port);
40 s.getInputStream();
41 fail("Expected connection to " + host + ":" + port + " to fail.");
42 } catch (SSLHandshakeException expected) {
43 }
44 }
45
46 public static void assertConnectionSucceeds(SSLContext context, String host, int port)
47 throws Exception {
48 Socket s = context.getSocketFactory().createSocket(host, port);
49 s.getInputStream();
50 }
51
52 public static void assertUrlConnectionFails(SSLContext context, String host, int port)
53 throws Exception {
54 URL url = new URL("https://" + host + ":" + port);
55 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
56 connection.setSSLSocketFactory(context.getSocketFactory());
57 try {
58 connection.getInputStream();
59 fail("Connection to " + host + ":" + port + " expected to fail");
60 } catch (SSLHandshakeException expected) {
61 // ignored.
62 }
63 }
64
65 public static void assertUrlConnectionSucceeds(SSLContext context, String host, int port)
66 throws Exception {
67 URL url = new URL("https://" + host + ":" + port);
68 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
69 connection.setSSLSocketFactory(context.getSocketFactory());
70 connection.getInputStream();
71 }
72
73 public static SSLContext getSSLContext(ConfigSource source) throws Exception {
74 ApplicationConfig config = new ApplicationConfig(source);
Chad Brubaker5a1078f2015-11-10 12:26:18 -080075 TrustManagerFactory tmf =
76 TrustManagerFactory.getInstance("PKIX", new NetworkSecurityConfigProvider());
77 tmf.init(new RootTrustManagerFactorySpi.ApplicationConfigParameters(config));
Chad Brubaker5f967022015-11-04 23:55:29 -080078 SSLContext context = SSLContext.getInstance("TLS");
Chad Brubaker5a1078f2015-11-10 12:26:18 -080079 context.init(null, tmf.getTrustManagers(), null);
Chad Brubaker5f967022015-11-04 23:55:29 -080080 return context;
81 }
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070082
83 public static ApplicationInfo makeApplicationInfo() {
84 ApplicationInfo info = new ApplicationInfo();
85 info.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
86 info.targetSandboxVersion = 1;
87 return info;
88 }
89
90 public static ApplicationInfo makeApplicationInfo(int targetSdkVersion) {
91 ApplicationInfo info = makeApplicationInfo();
92 info.targetSdkVersion = targetSdkVersion;
93 return info;
94 }
Chad Brubaker5f967022015-11-04 23:55:29 -080095}