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