blob: 60c40dbdc32050091f888ac6506775db6452f7e7 [file] [log] [blame]
Geremy Condracb4c5812012-09-18 13:35:29 -07001/*
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
17package android.net.http;
18
19import java.security.KeyStore;
20import java.security.cert.X509Certificate;
21
Geremy Condrafd5a80f2012-09-18 14:41:39 -070022import javax.net.ssl.TrustManager;
23import javax.net.ssl.TrustManagerFactory;
Geremy Condracb4c5812012-09-18 13:35:29 -070024import javax.net.ssl.X509TrustManager;
25
26import junit.framework.TestCase;
27
28import org.apache.harmony.xnet.provider.jsse.TrustManagerImpl;
29
30public 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);
Geremy Condrafd5a80f2012-09-18 14:41:39 -070047 fail();
48 } catch (IllegalArgumentException expected) {
Geremy Condracb4c5812012-09-18 13:35:29 -070049 }
Geremy Condracb4c5812012-09-18 13:35:29 -070050 }
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 }
Geremy Condrafd5a80f2012-09-18 14:41:39 -070057
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 }
Geremy Condracb4c5812012-09-18 13:35:29 -070072}