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 org.apache.harmony.xnet.tests.javax.net.ssl;
     19 
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.KeyStore;
     22 import java.security.KeyStoreException;
     23 import java.security.NoSuchAlgorithmException;
     24 import java.security.NoSuchProviderException;
     25 import java.security.Provider;
     26 import java.security.Security;
     27 import java.security.UnrecoverableKeyException;
     28 
     29 import javax.net.ssl.ManagerFactoryParameters;
     30 import javax.net.ssl.KeyManager;
     31 import javax.net.ssl.KeyManagerFactory;
     32 import javax.net.ssl.KeyManagerFactorySpi;
     33 
     34 import org.apache.harmony.xnet.tests.support.SpiEngUtils;
     35 import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi;
     36 import junit.framework.TestCase;
     37 
     38 /**
     39  * Tests for <code>KeyManagerFactory</code> class constructors and methods.
     40  *
     41  */
     42 
     43 public class KeyManagerFactory1Test extends TestCase {
     44 
     45     private static final String srvKeyManagerFactory = "KeyManagerFactory";
     46 
     47     private static String defaultAlgorithm = null;
     48 
     49     private static String defaultProviderName = null;
     50 
     51     private static Provider defaultProvider = null;
     52 
     53     private static boolean DEFSupported = false;
     54 
     55     private static final String NotSupportedMsg = "There is no suitable provider for KeyManagerFactory";
     56 
     57     private static final String[] invalidValues = SpiEngUtils.invalidValues;
     58 
     59     private static String[] validValues = new String[3];
     60     static {
     61         defaultAlgorithm = Security
     62                 .getProperty("ssl.KeyManagerFactory.algorithm");
     63         if (defaultAlgorithm != null) {
     64             defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm,
     65                     srvKeyManagerFactory);
     66             DEFSupported = (defaultProvider != null);
     67             defaultProviderName = (DEFSupported ? defaultProvider.getName()
     68                     : null);
     69             validValues[0] = defaultAlgorithm;
     70             validValues[1] = defaultAlgorithm.toUpperCase();
     71             validValues[2] = defaultAlgorithm.toLowerCase();
     72         }
     73     }
     74 
     75     protected KeyManagerFactory[] createKMFac() {
     76         if (!DEFSupported) {
     77             fail(defaultAlgorithm + " algorithm is not supported");
     78             return null;
     79         }
     80         KeyManagerFactory[] kMF = new KeyManagerFactory[3];
     81         try {
     82             kMF[0] = KeyManagerFactory.getInstance(defaultAlgorithm);
     83             kMF[1] = KeyManagerFactory.getInstance(defaultAlgorithm,
     84                     defaultProvider);
     85             kMF[2] = KeyManagerFactory.getInstance(defaultAlgorithm,
     86                     defaultProviderName);
     87             return kMF;
     88         } catch (Exception e) {
     89             e.printStackTrace();
     90             return null;
     91         }
     92     }
     93 
     94     /**
     95      *  Test for <code>getDefaultAlgorithm()</code> method
     96      * Assertion: returns value which is specifoed in security property
     97      */
     98     public void testGetDefaultAlgorithm() {
     99         if (!DEFSupported) {
    100             fail(NotSupportedMsg);
    101             return;
    102         }
    103         String def = KeyManagerFactory.getDefaultAlgorithm();
    104         if (defaultAlgorithm == null) {
    105             assertNull("DefaultAlgorithm must be null", def);
    106         } else {
    107             assertEquals("Invalid default algorithm", def, defaultAlgorithm);
    108         }
    109         String defA = "Proba.keymanagerfactory.defaul.type";
    110         Security.setProperty("ssl.KeyManagerFactory.algorithm", defA);
    111         assertEquals("Incorrect defaultAlgorithm",
    112                 KeyManagerFactory.getDefaultAlgorithm(), defA);
    113         if (def == null) {
    114             def = "";
    115         }
    116         Security.setProperty("ssl.KeyManagerFactory.algorithm", def);
    117         assertEquals("Incorrect defaultAlgorithm",
    118                 KeyManagerFactory.getDefaultAlgorithm(), def);
    119     }
    120 
    121     /**
    122      * Test for <code>getInstance(String algorithm)</code> method
    123      * Assertions:
    124      * returns security property "ssl.KeyManagerFactory.algorithm";
    125      * returns instance of KeyManagerFactory
    126      */
    127     public void testKeyManagerFactory01() throws NoSuchAlgorithmException {
    128         if (!DEFSupported) {
    129             fail(NotSupportedMsg);
    130             return;
    131         }
    132         KeyManagerFactory keyMF;
    133         for (int i = 0; i < validValues.length; i++) {
    134             keyMF = KeyManagerFactory.getInstance(validValues[i]);
    135             assertTrue("Not KeyManagerFactory object",
    136                     keyMF instanceof KeyManagerFactory);
    137             assertEquals("Invalid algorithm", keyMF.getAlgorithm(),
    138                     validValues[i]);
    139         }
    140     }
    141 
    142     /**
    143      * Test for <code>getInstance(String algorithm)</code> method
    144      * Assertion:
    145      * throws NullPointerException when algorithm is null;
    146      * throws NoSuchAlgorithmException when algorithm is not correct;
    147      */
    148     public void testKeyManagerFactory02() {
    149         try {
    150             KeyManagerFactory.getInstance(null);
    151             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    152         } catch (NoSuchAlgorithmException e) {
    153         } catch (NullPointerException e) {
    154         }
    155         for (int i = 0; i < invalidValues.length; i++) {
    156             try {
    157                 KeyManagerFactory.getInstance(invalidValues[i]);
    158                 fail("NoSuchAlgorithmException was not thrown as expected for algorithm: "
    159                         .concat(invalidValues[i]));
    160             } catch (NoSuchAlgorithmException e) {
    161             }
    162         }
    163     }
    164 
    165     /**
    166      * Test for <code>getInstance(String algorithm, String provider)</code>
    167      * method
    168      * Assertion: throws IllegalArgumentException when provider is null or empty
    169      */
    170     public void testKeyManagerFactory03() throws NoSuchProviderException,
    171             NoSuchAlgorithmException {
    172         if (!DEFSupported) {
    173             fail(NotSupportedMsg);
    174             return;
    175         }
    176         String provider = null;
    177         for (int i = 0; i < validValues.length; i++) {
    178             try {
    179                 KeyManagerFactory.getInstance(validValues[i], provider);
    180                 fail("Expected IllegalArgumentException was not thrown for null provider");
    181             } catch (IllegalArgumentException e) {
    182             }
    183             try {
    184                 KeyManagerFactory.getInstance(validValues[i], "");
    185                 fail("Expected IllegalArgumentException was not thrown for empty provider");
    186             } catch (IllegalArgumentException e) {
    187             }
    188         }
    189     }
    190 
    191     /**
    192      * Test for <code>getInstance(String algorithm, String provider)</code>
    193      * method
    194      * Assertion:
    195      * throws NullPointerException when algorithm is null;
    196      * throws NoSuchAlgorithmException when algorithm is not correct;
    197      */
    198     public void testKeyManagerFactory04() throws NoSuchProviderException {
    199         if (!DEFSupported) {
    200             fail(NotSupportedMsg);
    201             return;
    202         }
    203         try {
    204             KeyManagerFactory.getInstance(null, defaultProviderName);
    205             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    206         } catch (NoSuchAlgorithmException e) {
    207         } catch (NullPointerException e) {
    208         }
    209         for (int i = 0; i < invalidValues.length; i++) {
    210             try {
    211                 KeyManagerFactory.getInstance(invalidValues[i],
    212                         defaultProviderName);
    213                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    214                         .concat(invalidValues[i]).concat(")"));
    215             } catch (NoSuchAlgorithmException e) {
    216             }
    217         }
    218     }
    219 
    220     /**
    221      * Test for <code>getInstance(String algorithm, String provider)</code>
    222      * method
    223      * Assertion: throws NoSuchProviderException when provider has
    224      * invalid value
    225      */
    226     public void testKeyManagerFactory05() throws NoSuchAlgorithmException {
    227         if (!DEFSupported) {
    228             fail(NotSupportedMsg);
    229             return;
    230         }
    231         for (int i = 0; i < validValues.length; i++) {
    232             for (int j = 1; j < invalidValues.length; j++) {
    233                 try {
    234                     KeyManagerFactory.getInstance(validValues[i],
    235                             invalidValues[j]);
    236                     fail("NuSuchProviderException must be thrown (algorithm: "
    237                             + validValues[i] + " provider: " + invalidValues[j]
    238                             + ")");
    239                 } catch (NoSuchProviderException e) {
    240                 }
    241             }
    242         }
    243     }
    244 
    245     /**
    246      * Test for <code>getInstance(String algorithm, String provider)</code>
    247      * method Assertion: returns instance of KeyManagerFactory
    248      */
    249     public void testKeyManagerFactory06() throws NoSuchProviderException,
    250             NoSuchAlgorithmException {
    251         if (!DEFSupported) {
    252             fail(NotSupportedMsg);
    253             return;
    254         }
    255         KeyManagerFactory kMF;
    256         for (int i = 0; i < validValues.length; i++) {
    257             kMF = KeyManagerFactory.getInstance(validValues[i],
    258                     defaultProviderName);
    259             assertTrue("Not KeyManagerFactory object",
    260                     kMF instanceof KeyManagerFactory);
    261             assertEquals("Incorrect algorithm", kMF.getAlgorithm(),
    262                     validValues[i]);
    263             assertEquals("Incorrect provider", kMF.getProvider().getName(),
    264                     defaultProviderName);
    265         }
    266     }
    267 
    268     /**
    269      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    270      * method
    271      * Assertion: throws IllegalArgumentException when provider is null
    272      */
    273     public void testKeyManagerFactory07() throws NoSuchAlgorithmException {
    274         if (!DEFSupported) {
    275             fail(NotSupportedMsg);
    276             return;
    277         }
    278         Provider provider = null;
    279         for (int i = 0; i < validValues.length; i++) {
    280             try {
    281                 KeyManagerFactory.getInstance(validValues[i], provider);
    282                 fail("Expected IllegalArgumentException was not thrown when provider is null");
    283             } catch (IllegalArgumentException e) {
    284             }
    285         }
    286     }
    287 
    288     /**
    289      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    290      * method
    291      * Assertion:
    292      * throws NullPointerException when algorithm is null;
    293      * throws NoSuchAlgorithmException when algorithm is not correct;
    294      */
    295     public void testKeyManagerFactory08() {
    296         if (!DEFSupported) {
    297             fail(NotSupportedMsg);
    298             return;
    299         }
    300         try {
    301             KeyManagerFactory.getInstance(null, defaultProvider);
    302             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    303         } catch (NoSuchAlgorithmException e) {
    304         } catch (NullPointerException e) {
    305         }
    306         for (int i = 0; i < invalidValues.length; i++) {
    307             try {
    308                 KeyManagerFactory
    309                         .getInstance(invalidValues[i], defaultProvider);
    310                 fail("Expected NuSuchAlgorithmException was not thrown");
    311             } catch (NoSuchAlgorithmException e) {
    312             }
    313         }
    314     }
    315 
    316     /**
    317      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    318      * method
    319      * Assertion: returns instance of KeyManagerFactory
    320      */
    321     public void testKeyManagerFactory09() throws NoSuchAlgorithmException,
    322             IllegalArgumentException {
    323         if (!DEFSupported) {
    324             fail(NotSupportedMsg);
    325             return;
    326         }
    327         KeyManagerFactory kMF;
    328         for (int i = 0; i < validValues.length; i++) {
    329             kMF = KeyManagerFactory
    330                     .getInstance(validValues[i], defaultProvider);
    331             assertTrue(kMF instanceof KeyManagerFactory);
    332             assertEquals(kMF.getAlgorithm(), validValues[i]);
    333             assertEquals(kMF.getProvider(), defaultProvider);
    334         }
    335     }
    336 
    337     /**
    338      * Test for <code>KeyManagerFactory</code> constructor
    339      * Assertion: returns KeyManagerFactory object
    340      */
    341     public void testKeyManagerFactory10() throws Exception {
    342         if (!DEFSupported) {
    343             fail(NotSupportedMsg);
    344             return;
    345         }
    346         KeyManagerFactorySpi spi = new MyKeyManagerFactorySpi();
    347         KeyManagerFactory keyMF = new myKeyManagerFactory(spi, defaultProvider,
    348                 defaultAlgorithm);
    349         assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory);
    350         assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
    351                 defaultAlgorithm);
    352         assertEquals("Incorrect provider", keyMF.getProvider(), defaultProvider);
    353         try {
    354             keyMF.init(null, new char[1]);
    355             fail("UnrecoverableKeyException must be thrown");
    356         } catch (UnrecoverableKeyException e) {
    357             // Expected
    358         }
    359 
    360         keyMF = new myKeyManagerFactory(null, null, null);
    361         assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory);
    362         assertNull("Aalgorithm must be null", keyMF.getAlgorithm());
    363         assertNull("Provider must be null", keyMF.getProvider());
    364         try {
    365             keyMF.getKeyManagers();
    366         } catch (NullPointerException e) {
    367         }
    368     }
    369 
    370     /**
    371      * Test for <code>init(KeyStore keyStore, char[] password)</code> and
    372      * <code>getKeyManagers()</code>
    373      * Assertion: returns not empty KeyManager array
    374      */
    375     public void testKeyManagerFactory11() throws Exception {
    376         if (!DEFSupported) {
    377             fail(NotSupportedMsg);
    378             return;
    379         }
    380         KeyManagerFactory[] keyMF = createKMFac();
    381         assertNotNull("KeyManagerFactory object were not created", keyMF);
    382         KeyStore ksNull = null;
    383         KeyManager[] km;
    384         for (int i = 0; i < keyMF.length; i++) {
    385             keyMF[i].init(ksNull, new char[10]);
    386             km = keyMF[i].getKeyManagers();
    387             assertNotNull("Result should not be null", km);
    388             assertTrue("Length of result KeyManager array should not be 0",
    389                     (km.length > 0));
    390         }
    391         KeyStore ks;
    392         ks = KeyStore.getInstance(KeyStore.getDefaultType());
    393         ks.load(null, null);
    394 
    395         for (int i = 0; i < keyMF.length; i++) {
    396             try {
    397                 keyMF[i].init(ks, new char[10]);
    398             } catch (KeyStoreException e) {
    399             }
    400             km = keyMF[i].getKeyManagers();
    401             assertNotNull("Result has not be null", km);
    402             assertTrue("Length of result KeyManager array should not be 0",
    403                     (km.length > 0));
    404         }
    405     }
    406 
    407     /**
    408      * Test for <code>init(ManagerFactoryParameters params)</code>
    409      * Assertion:
    410      * throws InvalidAlgorithmParameterException when params is null
    411      */
    412     public void testKeyManagerFactory12() throws NoSuchAlgorithmException {
    413         if (!DEFSupported) {
    414             fail(NotSupportedMsg);
    415             return;
    416         }
    417         ManagerFactoryParameters par = null;
    418         KeyManagerFactory[] keyMF = createKMFac();
    419         assertNotNull("KeyManagerFactory object were not created", keyMF);
    420         for (int i = 0; i < keyMF.length; i++) {
    421             try {
    422                 keyMF[i].init(par);
    423                 fail("InvalidAlgorithmParameterException must be thrown");
    424             } catch (InvalidAlgorithmParameterException e) {
    425             }
    426         }
    427     }
    428 
    429 }
    430 
    431 /**
    432  * Additional class for KeyManagerFactory constructor verification
    433  */
    434 class myKeyManagerFactory extends KeyManagerFactory {
    435     public myKeyManagerFactory(KeyManagerFactorySpi spi, Provider prov,
    436             String alg) {
    437         super(spi, prov, alg);
    438     }
    439 }
    440