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.InvalidAlgorithmParameterException;
     19 import java.security.KeyStore;
     20 import java.security.KeyStoreException;
     21 import java.security.NoSuchAlgorithmException;
     22 import java.security.UnrecoverableKeyException;
     23 import javax.net.ssl.KeyManager;
     24 import javax.net.ssl.KeyManagerFactorySpi;
     25 import javax.net.ssl.ManagerFactoryParameters;
     26 
     27 import junit.framework.TestCase;
     28 
     29 import org.apache.harmony.xnet.tests.support.KeyManagerFactorySpiImpl;
     30 
     31 public class KeyManagerFactorySpiTest extends TestCase {
     32 
     33     /**
     34      * javax.net.ssl.KeyManagerFactorySpi#KeyManagerFactorySpi()
     35      */
     36     public void test_Constructor() {
     37         try {
     38             KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
     39             assertTrue(kmf instanceof KeyManagerFactorySpi);
     40         } catch (Exception e) {
     41             fail("Unexpected Exception " + e.toString());
     42         }
     43     }
     44 
     45     /**
     46      * javax.net.ssl.KeyManagerFactorySpi#KengineInit(KeyStore ks, char[] password)
     47      */
     48     public void test_engineInit_01() {
     49         KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
     50         KeyStore ks;
     51         char[] psw = "password".toCharArray();
     52 
     53         try {
     54             kmf.engineInit(null, null);
     55             fail("NoSuchAlgorithmException wasn't thrown");
     56         } catch (NoSuchAlgorithmException kse) {
     57             //expected
     58         } catch (Exception e) {
     59             fail(e + " was thrown instead of NoSuchAlgorithmException");
     60         }
     61 
     62         try {
     63             kmf.engineInit(null, psw);
     64             fail("KeyStoreException wasn't thrown");
     65         } catch (KeyStoreException uke) {
     66             //expected
     67         } catch (Exception e) {
     68             fail(e + " was thrown instead of KeyStoreException");
     69         }
     70 
     71         try {
     72             ks = KeyStore.getInstance(KeyStore.getDefaultType());
     73             kmf.engineInit(ks, null);
     74             fail("UnrecoverableKeyException wasn't thrown");
     75         } catch (UnrecoverableKeyException uke) {
     76             //expected
     77         } catch (Exception e) {
     78             fail(e + " was thrown instead of UnrecoverableKeyException");
     79         }
     80 
     81         try {
     82             KeyStore kst = KeyStore.getInstance(KeyStore.getDefaultType());
     83             kst.load(null, null);
     84             kmf.engineInit(kst, psw);
     85         } catch (Exception e) {
     86             fail("Unexpected exception " + e);
     87         }
     88     }
     89 
     90     /**
     91      * javax.net.ssl.KeyManagerFactorySpi#KengineInit(ManagerFactoryParameters spec)
     92      */
     93     public void test_engineInit_02() {
     94         KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
     95 
     96         try {
     97             kmf.engineInit(null);
     98             fail("InvalidAlgorithmParameterException wasn't thrown");
     99         } catch (InvalidAlgorithmParameterException iape) {
    100             //expected
    101         } catch (Exception e) {
    102             fail(e + " was thrown instead of InvalidAlgorithmParameterException");
    103         }
    104 
    105         try {
    106             char[] psw = "password".toCharArray();
    107             Parameters pr = new Parameters(psw);
    108             kmf.engineInit(pr);
    109         } catch (Exception e) {
    110             fail(e + " unexpected exception was thrown");
    111         }
    112     }
    113 
    114     /**
    115      * javax.net.ssl.KeyManagerFactorySpi#engineGetKeyManagers()
    116      */
    117     public void test_engineGetKeyManagers() {
    118         KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
    119 
    120         try {
    121             KeyManager[] km = kmf.engineGetKeyManagers();
    122             fail("IllegalStateException wasn't thrown");
    123         } catch (IllegalStateException ise) {
    124             //expected
    125         } catch (Exception e) {
    126             fail(e + " was thrown instead of IllegalStateException");
    127         }
    128 
    129         try {
    130             char[] psw = "password".toCharArray();
    131             Parameters pr = new Parameters(psw);
    132             kmf.engineInit(pr);
    133             KeyManager[] km = kmf.engineGetKeyManagers();
    134             assertNull("Object is not NULL", km);
    135         } catch (Exception e) {
    136             fail(e + " unexpected exception was thrown");
    137         }
    138     }
    139 
    140     public class Parameters implements ManagerFactoryParameters {
    141         private char[] passWD;
    142 
    143         public Parameters (char[] pass) {
    144             this.passWD = pass;
    145         }
    146         public char[] getPassword() {
    147             return passWD;
    148         }
    149     }
    150 
    151 }
    152