1 /* 2 * Copyright (C) 2012 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.net.http; 18 19 import java.security.KeyStore; 20 import java.security.cert.X509Certificate; 21 22 import javax.net.ssl.TrustManager; 23 import javax.net.ssl.TrustManagerFactory; 24 import javax.net.ssl.X509TrustManager; 25 26 import junit.framework.TestCase; 27 28 import org.apache.harmony.xnet.provider.jsse.TrustManagerImpl; 29 30 public class X509TrustManagerExtensionsTest extends TestCase { 31 32 private class NotATrustManagerImpl implements X509TrustManager { 33 34 public void checkClientTrusted(X509Certificate[] chain, String authType) {} 35 36 public void checkServerTrusted(X509Certificate[] chain, String authType) {} 37 38 public X509Certificate[] getAcceptedIssuers() { 39 return new X509Certificate[0]; 40 } 41 } 42 43 public void testBadCast() throws Exception { 44 NotATrustManagerImpl ntmi = new NotATrustManagerImpl(); 45 try { 46 X509TrustManagerExtensions tme = new X509TrustManagerExtensions(ntmi); 47 fail(); 48 } catch (IllegalArgumentException expected) { 49 } 50 } 51 52 public void testGoodCast() throws Exception { 53 String defaultType = KeyStore.getDefaultType(); 54 TrustManagerImpl tmi = new TrustManagerImpl(KeyStore.getInstance(defaultType)); 55 X509TrustManagerExtensions tme = new X509TrustManagerExtensions(tmi); 56 } 57 58 public void testNormalUseCase() throws Exception { 59 String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 60 TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm); 61 String defaultKeystoreType = KeyStore.getDefaultType(); 62 tmf.init(KeyStore.getInstance(defaultKeystoreType)); 63 TrustManager[] tms = tmf.getTrustManagers(); 64 for (TrustManager tm : tms) { 65 if (tm instanceof X509TrustManager) { 66 new X509TrustManagerExtensions((X509TrustManager)tm); 67 return; 68 } 69 } 70 fail(); 71 } 72 } 73