Home | History | Annotate | Download | only in ssl
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.apache.harmony.tests.javax.net.ssl;
     17 
     18 import java.security.AccessController;
     19 import java.security.InvalidAlgorithmParameterException;
     20 import java.security.KeyStore;
     21 import java.security.KeyStoreException;
     22 import java.security.NoSuchAlgorithmException;
     23 import java.security.Provider;
     24 
     25 import javax.net.ssl.ManagerFactoryParameters;
     26 import javax.net.ssl.TrustManager;
     27 import javax.net.ssl.TrustManagerFactory;
     28 import javax.net.ssl.TrustManagerFactorySpi;
     29 
     30 import junit.framework.TestCase;
     31 import org.apache.harmony.xnet.tests.support.TrustManagerFactorySpiImpl;
     32 import org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi.Parameters;
     33 
     34 public class TrustManagerFactorySpiTest extends TestCase {
     35 
     36     private TrustManagerFactorySpiImpl factory = new TrustManagerFactorySpiImpl();
     37     /**
     38      * javax.net.ssl.TrustManagerFactorySpi#TrustManagerFactorySpi()
     39      */
     40     public void test_Constructor() {
     41         try {
     42             TrustManagerFactorySpiImpl tmf = new TrustManagerFactorySpiImpl();
     43         } catch (Exception e) {
     44             fail("Unexpected exception " + e.toString());
     45         }
     46     }
     47 
     48     /**
     49      * @throws NoSuchAlgorithmException
     50      * @throws KeyStoreException
     51      * javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore ks)
     52      */
     53     public void test_engineInit_01() throws NoSuchAlgorithmException,
     54             KeyStoreException {
     55         factory.reset();
     56         Provider provider = new MyProvider();
     57         TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF",
     58                 provider);
     59         KeyStore ks = null;
     60         try {
     61             ks = KeyStore.getInstance(KeyStore.getDefaultType());
     62             ks.load(null, null);
     63             tmf.init(ks);
     64         } catch (Exception e) {
     65             fail("Unexpected exception " + e.toString());
     66         }
     67         assertTrue(factory.isEngineInitCalled());
     68         assertEquals(ks, factory.getKs());
     69         factory.reset();
     70         tmf.init((KeyStore) null);
     71         assertTrue(factory.isEngineInitCalled());
     72         assertNull(factory.getKs());
     73     }
     74 
     75     /**
     76      * @throws InvalidAlgorithmParameterException
     77      * @throws NoSuchAlgorithmException
     78      * javax.net.ssl.TrustManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
     79      */
     80     public void test_engineInit_02() throws InvalidAlgorithmParameterException,
     81             NoSuchAlgorithmException {
     82         factory.reset();
     83         Provider provider = new MyProvider();
     84         TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF",
     85                 provider);
     86         Parameters pr = null;
     87         try {
     88             KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
     89             ks.load(null, null);
     90             pr = new Parameters(ks);
     91             tmf.init(pr);
     92         } catch (Exception e) {
     93             fail("Unexpected exception " + e.toString());
     94         }
     95         assertTrue(factory.isEngineInitCalled());
     96         assertEquals(pr, factory.getSpec());
     97         factory.reset();
     98         tmf.init((ManagerFactoryParameters) null);
     99         assertTrue(factory.isEngineInitCalled());
    100         assertNull(factory.getSpec());
    101     }
    102 
    103     /**
    104      * @throws NoSuchAlgorithmException
    105      * javax.net.ssl.TrustManagerFactorySpi#engineGetTrustManagers()
    106      */
    107     public void test_engineGetTrustManagers() throws NoSuchAlgorithmException {
    108         factory.reset();
    109         Provider provider = new MyProvider();
    110         TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF",
    111                 provider);
    112         TrustManager[] tm = tmf.getTrustManagers();
    113         assertTrue(factory.isEngineGetTrustManagersCalled());
    114         factory.reset();
    115         try {
    116             KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    117             ks.load(null, null);
    118             tmf.init(ks);
    119             tm = tmf.getTrustManagers();
    120             assertTrue(factory.isEngineGetTrustManagersCalled());
    121         } catch (Exception e) {
    122             fail("Unexpected exception " + e.toString());
    123         }
    124     }
    125 }
    126 
    127 class MyProvider extends Provider {
    128 
    129     public MyProvider() {
    130         super("MyProvider", 1.0, "My Test Provider");
    131         AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
    132             public Void run() {
    133                 put("TrustManagerFactory.MyTMF",
    134                         "org.apache.harmony.xnet.tests.support.TrustManagerFactorySpiImpl");
    135                 return null;
    136             }
    137         });
    138     }
    139 }
    140