Home | History | Annotate | Download | only in ssl
      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 tests.api.javax.net.ssl;
     19 
     20 import dalvik.annotation.TestTargetClass;
     21 import dalvik.annotation.TestTargets;
     22 import dalvik.annotation.TestLevel;
     23 import dalvik.annotation.TestTargetNew;
     24 
     25 import java.security.InvalidAlgorithmParameterException;
     26 import java.security.KeyStore;
     27 import java.security.KeyStoreException;
     28 import java.security.NoSuchAlgorithmException;
     29 import java.security.NoSuchProviderException;
     30 import java.security.Provider;
     31 import java.security.Security;
     32 import java.security.UnrecoverableKeyException;
     33 
     34 import javax.net.ssl.KeyManagerFactory;
     35 import javax.net.ssl.ManagerFactoryParameters;
     36 
     37 import org.apache.harmony.security.tests.support.SpiEngUtils;
     38 import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi;
     39 import junit.framework.TestCase;
     40 
     41 /**
     42  * Tests for KeyManagerFactory class constructors and methods
     43  *
     44  */
     45 @TestTargetClass(KeyManagerFactory.class)
     46 public class KeyManagerFactory2Test extends TestCase {
     47     private static final String srvKeyManagerFactory = "KeyManagerFactory";
     48 
     49     private static final String defaultAlg = "KeyMF";
     50 
     51     private static final String KeyManagerFactoryProviderClass = "org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi";
     52 
     53     private static final String[] invalidValues = SpiEngUtils.invalidValues;
     54 
     55     private static final String[] validValues;
     56 
     57     static {
     58         validValues = new String[4];
     59         validValues[0] = defaultAlg;
     60         validValues[1] = defaultAlg.toLowerCase();
     61         validValues[2] = "Keymf";
     62         validValues[3] = "kEYMF";
     63     }
     64 
     65     Provider mProv;
     66 
     67     protected void setUp() throws Exception {
     68         super.setUp();
     69         mProv = (new SpiEngUtils()).new MyProvider("MyKMFProvider",
     70                 "Provider for testing", srvKeyManagerFactory.concat(".")
     71                         .concat(defaultAlg), KeyManagerFactoryProviderClass);
     72         Security.insertProviderAt(mProv, 2);
     73     }
     74 
     75     /*
     76      * @see TestCase#tearDown()
     77      */
     78     protected void tearDown() throws Exception {
     79         super.tearDown();
     80         Security.removeProvider(mProv.getName());
     81     }
     82 
     83     private void checkResult(KeyManagerFactory keyMF)
     84         throws Exception {
     85         KeyStore kStore = null;
     86         ManagerFactoryParameters mfp = null;
     87 
     88         char[] pass = { 'a', 'b', 'c' };
     89 
     90         try {
     91             keyMF.init(kStore, null);
     92             fail("KeyStoreException must be thrown");
     93         } catch (KeyStoreException e) {
     94         }
     95         try {
     96             keyMF.init(kStore, pass);
     97             fail("UnrecoverableKeyException must be thrown");
     98         } catch (UnrecoverableKeyException e) {
     99         }
    100         try {
    101             keyMF.init(mfp);
    102             fail("InvalidAlgorithmParameterException must be thrown");
    103         } catch (InvalidAlgorithmParameterException e) {
    104         }
    105         assertNull("getKeyManagers() should return null object", keyMF
    106                 .getKeyManagers());
    107 
    108         try {
    109             kStore = KeyStore.getInstance(KeyStore.getDefaultType());
    110             kStore.load(null, null);
    111         } catch (KeyStoreException e) {
    112             fail("default keystore is not supported");
    113             return;
    114         }
    115         keyMF.init(kStore, pass);
    116 
    117         mfp = new MyKeyManagerFactorySpi.Parameters(kStore, null);
    118         try {
    119             keyMF.init(mfp);
    120             fail("InvalidAlgorithmParameterException must be thrown");
    121         } catch (InvalidAlgorithmParameterException e) {
    122         }
    123         mfp = new MyKeyManagerFactorySpi.Parameters(kStore, pass);
    124         keyMF.init(mfp);
    125     }
    126     /**
    127      * Test for <code>getInstance(String algorithm)</code> method
    128      * Assertions:
    129      * throws NullPointerException when algorithm is null;
    130      * throws NoSuchAlgorithmException when algorithm is not correct;
    131      * returns KeyManagerFactory object
    132      */
    133     @TestTargetNew(
    134         level = TestLevel.COMPLETE,
    135         notes = "",
    136         method = "getInstance",
    137         args = {java.lang.String.class}
    138     )
    139     public void test_getInstanceLjava_lang_String() throws Exception {
    140         try {
    141             KeyManagerFactory.getInstance(null);
    142             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    143         } catch (NoSuchAlgorithmException e) {
    144         } catch (NullPointerException e) {
    145         }
    146         for (int i = 0; i < invalidValues.length; i++) {
    147             try {
    148                 KeyManagerFactory.getInstance(invalidValues[i]);
    149                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    150                         .concat(invalidValues[i]).concat(")"));
    151             } catch (NoSuchAlgorithmException e) {
    152             }
    153         }
    154         KeyManagerFactory keyMF;
    155         for (int i = 0; i < validValues.length; i++) {
    156             keyMF = KeyManagerFactory.getInstance(validValues[i]);
    157             assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
    158                     validValues[i]);
    159             assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
    160             checkResult(keyMF);
    161         }
    162     }
    163 
    164     /**
    165      * Test for <code>getInstance(String algorithm, String provider)</code>
    166      * method
    167      * Assertions:
    168      * throws NullPointerException when algorithm is null;
    169      * throws NoSuchAlgorithmException when algorithm is not correct;
    170      * throws IllegalArgumentException when provider is null or empty;
    171      * throws NoSuchProviderException when provider is available;
    172      * returns KeyManagerFactory object
    173      */
    174     @TestTargetNew(
    175         level = TestLevel.COMPLETE,
    176         notes = "",
    177         method = "getInstance",
    178         args = {java.lang.String.class, java.lang.String.class}
    179     )
    180     public void test_getInstanceLjava_lang_StringLjava_lang_String()
    181         throws Exception
    182     {
    183         try {
    184             KeyManagerFactory.getInstance(null, mProv.getName());
    185             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    186         } catch (NoSuchAlgorithmException e) {
    187         } catch (NullPointerException e) {
    188         }
    189         for (int i = 0; i < invalidValues.length; i++) {
    190             try {
    191                 KeyManagerFactory
    192                         .getInstance(invalidValues[i], mProv.getName());
    193                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    194                         .concat(invalidValues[i]).concat(")"));
    195             } catch (NoSuchAlgorithmException e) {
    196             }
    197         }
    198         String prov = null;
    199         for (int i = 0; i < validValues.length; i++) {
    200             try {
    201                 KeyManagerFactory.getInstance(validValues[i], prov);
    202                 fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
    203                         .concat(invalidValues[i]).concat(")"));
    204             } catch (IllegalArgumentException e) {
    205             }
    206             try {
    207                 KeyManagerFactory.getInstance(validValues[i], "");
    208                 fail("IllegalArgumentException must be thrown when provider is empty (algorithm: "
    209                         .concat(invalidValues[i]).concat(")"));
    210             } catch (IllegalArgumentException e) {
    211             }
    212         }
    213         for (int i = 0; i < validValues.length; i++) {
    214             for (int j = 1; j < invalidValues.length; j++) {
    215                 try {
    216                     KeyManagerFactory.getInstance(validValues[i],
    217                             invalidValues[j]);
    218                     fail("NoSuchProviderException must be thrown (algorithm: "
    219                             .concat(invalidValues[i]).concat(" provider: ")
    220                             .concat(invalidValues[j]).concat(")"));
    221                 } catch (NoSuchProviderException e) {
    222                 }
    223             }
    224         }
    225         KeyManagerFactory keyMF;
    226         for (int i = 0; i < validValues.length; i++) {
    227             keyMF = KeyManagerFactory.getInstance(validValues[i], mProv
    228                     .getName());
    229             assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
    230                     validValues[i]);
    231             assertEquals("Incorrect provider", keyMF.getProvider().getName(),
    232                     mProv.getName());
    233             checkResult(keyMF);
    234         }
    235     }
    236 
    237     /**
    238      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    239      * method
    240      * Assertions:
    241      * throws NullPointerException when algorithm is null;
    242      * throws NoSuchAlgorithmException when algorithm is not correct;
    243      * throws IllegalArgumentException when provider is null;
    244      * returns KeyManagerFactory object
    245      */
    246     @TestTargetNew(
    247         level = TestLevel.COMPLETE,
    248         notes = "",
    249         method = "getInstance",
    250         args = {java.lang.String.class, java.security.Provider.class}
    251     )
    252     public void test_getInstanceLjava_lang_StringLjava_security_Provider()
    253         throws Exception
    254     {
    255         try {
    256             KeyManagerFactory.getInstance(null, mProv);
    257             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    258         } catch (NoSuchAlgorithmException e) {
    259         } catch (NullPointerException e) {
    260         }
    261         for (int i = 0; i < invalidValues.length; i++) {
    262             try {
    263                 KeyManagerFactory.getInstance(invalidValues[i], mProv);
    264                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    265                         .concat(invalidValues[i]).concat(")"));
    266             } catch (NoSuchAlgorithmException e) {
    267             }
    268         }
    269         Provider prov = null;
    270         for (int i = 0; i < validValues.length; i++) {
    271             try {
    272                 KeyManagerFactory.getInstance(validValues[i], prov);
    273                 fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
    274                         .concat(invalidValues[i]).concat(")"));
    275             } catch (IllegalArgumentException e) {
    276             }
    277         }
    278         KeyManagerFactory keyMF;
    279         for (int i = 0; i < validValues.length; i++) {
    280             keyMF = KeyManagerFactory.getInstance(validValues[i], mProv);
    281             assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
    282                     validValues[i]);
    283             assertEquals("Incorrect provider", keyMF.getProvider(), mProv);
    284             checkResult(keyMF);
    285        }
    286     }
    287 }