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 17 package android.security.net.config; 18 19 import android.content.pm.ApplicationInfo; 20 import android.os.Build; 21 import java.net.Socket; 22 import java.net.URL; 23 import javax.net.ssl.HttpsURLConnection; 24 import javax.net.ssl.SSLContext; 25 import javax.net.ssl.SSLHandshakeException; 26 import javax.net.ssl.TrustManager; 27 import javax.net.ssl.TrustManagerFactory; 28 29 import junit.framework.Assert; 30 31 public final class TestUtils extends Assert { 32 TestUtils()33 private TestUtils() { 34 } 35 assertConnectionFails(SSLContext context, String host, int port)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 assertConnectionSucceeds(SSLContext context, String host, int port)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 assertUrlConnectionFails(SSLContext context, String host, int port)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 assertUrlConnectionSucceeds(SSLContext context, String host, int port)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 getSSLContext(ConfigSource source)73 public static SSLContext getSSLContext(ConfigSource source) throws Exception { 74 ApplicationConfig config = new ApplicationConfig(source); 75 TrustManagerFactory tmf = 76 TrustManagerFactory.getInstance("PKIX", new NetworkSecurityConfigProvider()); 77 tmf.init(new RootTrustManagerFactorySpi.ApplicationConfigParameters(config)); 78 SSLContext context = SSLContext.getInstance("TLS"); 79 context.init(null, tmf.getTrustManagers(), null); 80 return context; 81 } 82 makeApplicationInfo()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 makeApplicationInfo(int targetSdkVersion)90 public static ApplicationInfo makeApplicationInfo(int targetSdkVersion) { 91 ApplicationInfo info = makeApplicationInfo(); 92 info.targetSdkVersion = targetSdkVersion; 93 return info; 94 } 95 } 96