1cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra/*
2cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * Copyright (C) 2012 The Android Open Source Project
3cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra *
4cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * Licensed under the Apache License, Version 2.0 (the "License");
5cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * you may not use this file except in compliance with the License.
6cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * You may obtain a copy of the License at
7cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra *
8cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra *      http://www.apache.org/licenses/LICENSE-2.0
9cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra *
10cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * Unless required by applicable law or agreed to in writing, software
11cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * distributed under the License is distributed on an "AS IS" BASIS,
12cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * See the License for the specific language governing permissions and
14cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra * limitations under the License.
15cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra */
16cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
17cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condrapackage android.net.http;
18cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
19cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condraimport java.security.KeyStore;
20cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condraimport java.security.cert.X509Certificate;
21cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
22fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condraimport javax.net.ssl.TrustManager;
23fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condraimport javax.net.ssl.TrustManagerFactory;
24cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condraimport javax.net.ssl.X509TrustManager;
25cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
26cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condraimport junit.framework.TestCase;
27cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
28cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condraimport org.apache.harmony.xnet.provider.jsse.TrustManagerImpl;
29cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
30cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condrapublic class X509TrustManagerExtensionsTest extends TestCase {
31cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
32cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    private class NotATrustManagerImpl implements X509TrustManager {
33cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
34cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
35cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
36cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
37cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
38cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        public X509Certificate[] getAcceptedIssuers() {
39cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra            return new X509Certificate[0];
40cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        }
41cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    }
42cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
43cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    public void testBadCast() throws Exception {
44cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        NotATrustManagerImpl ntmi = new NotATrustManagerImpl();
45cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        try {
46cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra            X509TrustManagerExtensions tme = new X509TrustManagerExtensions(ntmi);
47fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra            fail();
48fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        } catch (IllegalArgumentException expected) {
49cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        }
50cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    }
51cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra
52cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    public void testGoodCast() throws Exception {
53cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        String defaultType = KeyStore.getDefaultType();
54cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        TrustManagerImpl tmi = new TrustManagerImpl(KeyStore.getInstance(defaultType));
55cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra        X509TrustManagerExtensions tme = new X509TrustManagerExtensions(tmi);
56cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra    }
57fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra
58fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra    public void testNormalUseCase() throws Exception {
59fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
60fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm);
61fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        String defaultKeystoreType = KeyStore.getDefaultType();
62fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        tmf.init(KeyStore.getInstance(defaultKeystoreType));
63fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        TrustManager[] tms = tmf.getTrustManagers();
64fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        for (TrustManager tm : tms) {
65fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra            if (tm instanceof X509TrustManager) {
66fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra                new X509TrustManagerExtensions((X509TrustManager)tm);
67fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra                return;
68fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra            }
69fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        }
70fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra        fail();
71fd5a80f6e98124d908643a14d33b8d2fa5238da6Geremy Condra    }
72cb4c5819758a7e2951bd4818383f7c5e10a5f016Geremy Condra}
73