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.tests.provider.jsse;
19
20import java.security.InvalidAlgorithmParameterException;
21import java.security.KeyStore;
22import java.security.KeyStoreException;
23
24import javax.net.ssl.ManagerFactoryParameters;
25import javax.net.ssl.TrustManager;
26
27import org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl;
28import org.apache.harmony.xnet.provider.jsse.TrustManagerImpl;
29import junit.framework.TestCase;
30
31/**
32 * Tests for <code>TrustManagerFactoryImpl</code> constructor and methods
33 *
34 */
35public class TrustManagerFactoryImplTest extends TestCase {
36
37    /*
38     * Class under test for void engineInit(KeyStore)
39     */
40    public void testEngineInitKeyStore() throws Exception {
41        TrustManagerFactoryImpl tmf = new TrustManagerFactoryImpl();
42        tmf.engineInit((KeyStore) null);
43
44        String def_keystore = System.getProperty("javax.net.ssl.trustStore");
45        System.setProperty("javax.net.ssl.trustStore", "abc");
46        try {
47            tmf.engineInit((KeyStore) null);
48            fail("No expected KeyStoreException");
49        } catch (KeyStoreException e) {
50        } finally {
51            if (def_keystore == null) {
52                System.clearProperty("javax.net.ssl.trustStore");
53            } else {
54                System.setProperty("javax.net.ssl.trustStore", def_keystore);
55            }
56        }
57    }
58
59    /*
60     * Class under test for void engineInit(ManagerFactoryParameters)
61     */
62    public void testEngineInitManagerFactoryParameters() {
63        TrustManagerFactoryImpl tmf = new TrustManagerFactoryImpl();
64
65        try {
66            tmf.engineInit((ManagerFactoryParameters) null);
67            fail("No expected InvalidAlgorithmParameterException");
68        } catch (InvalidAlgorithmParameterException e) {
69        }
70    }
71
72    public void testEngineGetTrustManagers() throws Exception {
73        TrustManagerFactoryImpl tmf = new TrustManagerFactoryImpl();
74        try {
75            tmf.engineGetTrustManagers();
76            fail("No expected IllegalStateException");
77        } catch (IllegalStateException e) {
78            // expected
79        }
80        KeyStore ks;
81        ks = KeyStore.getInstance("BKS");
82        ks.load(null, null);
83        tmf.engineInit(ks);
84
85        TrustManager[] tma = tmf.engineGetTrustManagers();
86        assertEquals("Incorrect array length", 1, tma.length);
87        assertTrue("Incorrect KeyManager type",
88                tma[0] instanceof TrustManagerImpl);
89    }
90
91}