1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.io.ByteArrayInputStream;
21import java.io.File;
22import java.io.FileInputStream;
23import java.security.KeyStore;
24import java.security.cert.CertificateException;
25import java.security.cert.CertificateFactory;
26import java.security.cert.X509Certificate;
27
28import junit.framework.TestCase;
29
30/**
31 * Tests for <code>TrustManagerImpl</code> constructor and methods
32 */
33public class TrustManagerImplTest extends TestCase {
34
35    // Cert. encoding.was generated by using of classes
36    // from org.apache.harmony.security.asn1 package and encoded
37    // by org.apache.harmony.misc.Base64 class.
38    // Source:
39    // org.apache.harmony.security.tests.support.provider.cert.CertFactoryTestData
40    private static String base64certEncoding =
41            "-----BEGIN CERTIFICATE-----\n" +
42                    "MIIC+jCCAragAwIBAgICAiswDAYHKoZIzjgEAwEBADAdMRswGQYDVQQKExJDZXJ0a" +
43                    "WZpY2F0ZSBJc3N1ZXIwIhgPMTk3MDAxMTIxMzQ2NDBaGA8xOTcwMDEyNDAzMzMyMF" +
44                    "owHzEdMBsGA1UEChMUU3ViamVjdCBPcmdhbml6YXRpb24wGTAMBgcqhkjOOAQDAQE" +
45                    "AAwkAAQIDBAUGBwiBAgCqggIAVaOCAhQwggIQMA8GA1UdDwEB/wQFAwMBqoAwEgYD" +
46                    "VR0TAQH/BAgwBgEB/wIBBTAUBgNVHSABAf8ECjAIMAYGBFUdIAAwZwYDVR0RAQH/B" +
47                    "F0wW4EMcmZjQDgyMi5OYW1lggdkTlNOYW1lpBcxFTATBgNVBAoTDE9yZ2FuaXphdG" +
48                    "lvboYaaHR0cDovL3VuaWZvcm0uUmVzb3VyY2UuSWSHBP///wCIByoDolyDsgMwDAY" +
49                    "DVR0eAQH/BAIwADAMBgNVHSQBAf8EAjAAMIGZBgNVHSUBAf8EgY4wgYsGBFUdJQAG" +
50                    "CCsGAQUFBwMBBggrBgEFBQcDAQYIKwYBBQUHAwIGCCsGAQUFBwMDBggrBgEFBQcDB" +
51                    "AYIKwYBBQUHAwUGCCsGAQUFBwMGBggrBgEFBQcDBwYIKwYBBQUHAwgGCCsGAQUFBw" +
52                    "MJBggrBgEFBQgCAgYKKwYBBAGCNwoDAwYJYIZIAYb4QgQBMA0GA1UdNgEB/wQDAgE" +
53                    "BMA4GBCpNhgkBAf8EAwEBATBkBgNVHRIEXTBbgQxyZmNAODIyLk5hbWWCB2ROU05h" +
54                    "bWWkFzEVMBMGA1UEChMMT3JnYW5pemF0aW9uhhpodHRwOi8vdW5pZm9ybS5SZXNvd" +
55                    "XJjZS5JZIcE////AIgHKgOiXIOyAzAJBgNVHR8EAjAAMAoGA1UdIwQDAQEBMAoGA1" +
56                    "UdDgQDAQEBMAoGA1UdIQQDAQEBMAwGByqGSM44BAMBAQADMAAwLQIUAL4QvoazNWP" +
57                    "7jrj84/GZlhm09DsCFQCBKGKCGbrP64VtUt4JPmLjW1VxQA==\n" +
58                    "-----END CERTIFICATE-----\n";
59
60    private X509Certificate[] untrustedChain;
61
62    @Override
63    protected void setUp() throws Exception {
64        super.setUp();
65        CertificateFactory certFactory = CertificateFactory.getInstance("X509");
66        ByteArrayInputStream bais = new ByteArrayInputStream(base64certEncoding
67                .getBytes("UTF-8"));
68        X509Certificate cert = (X509Certificate) certFactory
69                .generateCertificate(bais);
70        untrustedChain = new X509Certificate[] { cert };
71    }
72
73    public void testTrustManagerImpl_1() throws Exception {
74        KeyStore ks = KeyStore.getInstance("BKS");
75        ks.load(null, null);
76
77        TrustManagerImpl tm = new TrustManagerImpl(ks);
78        assertEquals(0, tm.getAcceptedIssuers().length);
79        checkTrustManager(tm);
80    }
81
82    public void testTrustManagerImpl_2() throws Exception {
83        KeyStore ks = JSSETestData.getKeyStore();
84
85        TrustManagerImpl tm = new TrustManagerImpl(ks);
86        assertEquals(1, tm.getAcceptedIssuers().length);
87        checkTrustManager(tm);
88    }
89
90    private void checkTrustManager(TrustManagerImpl tm) throws Exception {
91        try {
92            tm.checkClientTrusted(null, "RSA");
93            fail("No expected IllegalArgumentException ");
94        } catch (IllegalArgumentException e) {
95        }
96
97        try {
98            tm.checkClientTrusted(new X509Certificate[0], "RSA");
99            fail("No expected IllegalArgumentException ");
100        } catch (IllegalArgumentException e) {
101        }
102
103        try {
104            tm.checkClientTrusted(untrustedChain, "RSA");
105            fail("No expected CertificateException ");
106        } catch (CertificateException e) {
107        }
108
109        try {
110            tm.checkServerTrusted(null, "RSA");
111            fail("No expected IllegalArgumentException ");
112        } catch (IllegalArgumentException e) {
113        }
114
115        try {
116            tm.checkServerTrusted(new X509Certificate[0], "RSA");
117            fail("No expected IllegalArgumentException ");
118        } catch (IllegalArgumentException e) {
119        }
120
121        try {
122            tm.checkServerTrusted(untrustedChain, "RSA");
123            fail("No expected CertificateException ");
124        } catch (CertificateException e) {
125        }
126    }
127}