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.cts; 18 19 import android.net.http.X509TrustManagerExtensions; 20 21 import java.security.KeyStore; 22 import java.security.cert.Certificate; 23 import java.security.cert.X509Certificate; 24 25 import javax.net.ssl.TrustManager; 26 import javax.net.ssl.TrustManagerFactory; 27 import javax.net.ssl.X509TrustManager; 28 29 import junit.framework.TestCase; 30 31 public class X509TrustManagerExtensionsTest extends TestCase { 32 33 private static X509TrustManager getFirstX509TrustManager(TrustManagerFactory tmf) 34 throws Exception { 35 for (TrustManager trustManager : tmf.getTrustManagers()) { 36 if (trustManager instanceof X509TrustManager) { 37 return (X509TrustManager) trustManager; 38 } 39 } 40 fail("Unable to find X509TrustManager"); 41 return null; 42 } 43 44 public void testIsUserAddedCertificateDefaults() throws Exception { 45 final TrustManagerFactory tmf = 46 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 47 tmf.init((KeyStore) null); 48 X509TrustManager tm = getFirstX509TrustManager(tmf); 49 X509TrustManagerExtensions xtm = new X509TrustManagerExtensions(tm); 50 // Verify that all the default system provided CAs are not marked as user added. 51 for (Certificate cert : tm.getAcceptedIssuers()) { 52 assertFalse(xtm.isUserAddedCertificate((X509Certificate) cert)); 53 } 54 } 55 } 56