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