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