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 
     28 import javax.net.ssl.ManagerFactoryParameters;
     29 import javax.net.ssl.TrustManagerFactory;
     30 
     31 import org.apache.harmony.xnet.tests.support.SpiEngUtils;
     32 import org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi;
     33 import junit.framework.TestCase;
     34 
     35 /**
     36  * Tests for TrustManagerFactory class constructors and methods
     37  *
     38  */
     39 
     40 public class TrustManagerFactory2Test extends TestCase {
     41     private static final String srvTrustManagerFactory = "TrustManagerFactory";
     42     private static final String defaultAlg = "TMF";
     43     private static final String TrustManagerFactoryProviderClass = "org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi";
     44 
     45     private static final String[] invalidValues = SpiEngUtils.invalidValues;
     46 
     47     private static final String[] validValues;
     48 
     49     static {
     50         validValues = new String[4];
     51         validValues[0] = defaultAlg;
     52         validValues[1] = defaultAlg.toLowerCase();
     53         validValues[2] = "Tmf";
     54         validValues[3] = "tMF";
     55     }
     56 
     57     Provider mProv;
     58 
     59     @Override
     60     protected void setUp() throws Exception {
     61         super.setUp();
     62         mProv = (new SpiEngUtils()).new MyProvider("MyTMFProvider",
     63                 "Provider for testing", srvTrustManagerFactory.concat(".")
     64                         .concat(defaultAlg), TrustManagerFactoryProviderClass);
     65         Security.insertProviderAt(mProv, 1);
     66     }
     67 
     68     /*
     69      * @see TestCase#tearDown()
     70      */
     71     @Override
     72     protected void tearDown() throws Exception {
     73         super.tearDown();
     74         Security.removeProvider(mProv.getName());
     75     }
     76 
     77     private void checkResult(TrustManagerFactory tmf) throws Exception {
     78         KeyStore kStore = null;
     79         ManagerFactoryParameters mfp = null;
     80 
     81         try {
     82             tmf.init(kStore);
     83             fail("KeyStoreException must be thrown");
     84         } catch (KeyStoreException e) {
     85         }
     86         try {
     87             tmf.init(mfp);
     88             fail("InvalidAlgorithmParameterException must be thrown");
     89         } catch (InvalidAlgorithmParameterException e) {
     90         }
     91         assertNull("getTrustManagers() should return null object", tmf
     92                 .getTrustManagers());
     93 
     94         try {
     95             kStore = KeyStore.getInstance(KeyStore.getDefaultType());
     96             kStore.load(null, null);
     97         } catch (KeyStoreException e) {
     98             fail("default keystore is not supported");
     99             return;
    100         }
    101         tmf.init(kStore);
    102         mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(null);
    103         try {
    104             tmf.init(mfp);
    105             fail("RuntimeException must be thrown");
    106         } catch (RuntimeException e) {
    107             assertTrue("Incorrect exception", e.getCause() instanceof KeyStoreException);
    108         }
    109         mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(kStore);
    110         tmf.init(mfp);
    111     }
    112 
    113     /**
    114      * Test for <code>getInstance(String algorithm)</code> method
    115      * Assertions:
    116      * throws NullPointerException when algorithm is null;
    117      * throws NoSuchAlgorithmException when algorithm is not correct;
    118      * returns TrustManagerFactory object
    119      */
    120     public void testGetInstance01() throws Exception {
    121         try {
    122             TrustManagerFactory.getInstance(null);
    123             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    124         } catch (NoSuchAlgorithmException e) {
    125         } catch (NullPointerException e) {
    126         }
    127         for (int i = 0; i < invalidValues.length; i++) {
    128             try {
    129                 TrustManagerFactory.getInstance(invalidValues[i]);
    130                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    131                         .concat(invalidValues[i]).concat(")"));
    132             } catch (NoSuchAlgorithmException e) {
    133             }
    134         }
    135         TrustManagerFactory tmf;
    136         for (int i = 0; i < validValues.length; i++) {
    137             tmf = TrustManagerFactory.getInstance(validValues[i]);
    138             assertTrue("Not instanceof TrustManagerFactory object",
    139                     tmf instanceof TrustManagerFactory);
    140             assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
    141                     validValues[i]);
    142             assertEquals("Incorrect provider", tmf.getProvider(), mProv);
    143             checkResult(tmf);
    144         }
    145     }
    146 
    147     /**
    148      * Test for <code>getInstance(String algorithm, String provider)</code>
    149      * method
    150      * Assertions:
    151      * throws NullPointerException when algorithm is null;
    152      * throws NoSuchAlgorithmException when algorithm is not correct;
    153      * throws IllegalArgumentException when provider is null or empty;
    154      * throws NoSuchProviderException when provider is available;
    155      * returns TrustManagerFactory object
    156      */
    157     public void testGetInstance02() throws Exception {
    158         try {
    159             TrustManagerFactory.getInstance(null, mProv.getName());
    160             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    161         } catch (NoSuchAlgorithmException e) {
    162         } catch (NullPointerException e) {
    163         }
    164         for (int i = 0; i < invalidValues.length; i++) {
    165             try {
    166                 TrustManagerFactory.getInstance(invalidValues[i], mProv
    167                         .getName());
    168                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    169                         .concat(invalidValues[i]).concat(")"));
    170             } catch (NoSuchAlgorithmException e) {
    171             }
    172         }
    173         String prov = null;
    174         for (int i = 0; i < validValues.length; i++) {
    175             try {
    176                 TrustManagerFactory.getInstance(validValues[i], prov);
    177                 fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
    178                         .concat(invalidValues[i]).concat(")"));
    179             } catch (IllegalArgumentException e) {
    180             }
    181             try {
    182                 TrustManagerFactory.getInstance(validValues[i], "");
    183                 fail("IllegalArgumentException must be thrown when provider is empty (algorithm: "
    184                         .concat(invalidValues[i]).concat(")"));
    185             } catch (IllegalArgumentException e) {
    186             }
    187         }
    188         for (int i = 0; i < validValues.length; i++) {
    189             for (int j = 1; j < invalidValues.length; j++) {
    190                 try {
    191                     TrustManagerFactory.getInstance(validValues[i],
    192                             invalidValues[j]);
    193                     fail("NoSuchProviderException must be thrown (algorithm: "
    194                             .concat(invalidValues[i]).concat(" provider: ")
    195                             .concat(invalidValues[j]).concat(")"));
    196                 } catch (NoSuchProviderException e) {
    197                 }
    198             }
    199         }
    200         TrustManagerFactory tmf;
    201         for (int i = 0; i < validValues.length; i++) {
    202             tmf = TrustManagerFactory.getInstance(validValues[i], mProv
    203                     .getName());
    204             assertTrue("Not instanceof TrustManagerFactory object",
    205                     tmf instanceof TrustManagerFactory);
    206             assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
    207                     validValues[i]);
    208             assertEquals("Incorrect provider", tmf.getProvider().getName(),
    209                     mProv.getName());
    210             checkResult(tmf);
    211         }
    212     }
    213 
    214     /**
    215      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    216      * method
    217      * Assertions:
    218      * throws NullPointerException when algorithm is null;
    219      * throws NoSuchAlgorithmException when algorithm is not correct;
    220      * throws IllegalArgumentException when provider is null;
    221      * returns TrustManagerFactory object
    222      */
    223     public void testGetInstance03() throws Exception {
    224         try {
    225             TrustManagerFactory.getInstance(null, mProv);
    226             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    227         } catch (NoSuchAlgorithmException e) {
    228         } catch (NullPointerException e) {
    229         }
    230         for (int i = 0; i < invalidValues.length; i++) {
    231             try {
    232                 TrustManagerFactory.getInstance(invalidValues[i], mProv);
    233                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    234                         .concat(invalidValues[i]).concat(")"));
    235             } catch (NoSuchAlgorithmException e) {
    236             }
    237         }
    238         Provider prov = null;
    239         for (int i = 0; i < validValues.length; i++) {
    240             try {
    241                 TrustManagerFactory.getInstance(validValues[i], prov);
    242                 fail("IllegalArgumentException must be thrown when provider is null (algorithm: "
    243                         .concat(invalidValues[i]).concat(")"));
    244             } catch (IllegalArgumentException e) {
    245             }
    246         }
    247         TrustManagerFactory tmf;
    248         for (int i = 0; i < validValues.length; i++) {
    249             tmf = TrustManagerFactory.getInstance(validValues[i], mProv);
    250             assertTrue("Not instanceof TrustManagerFactory object",
    251                     tmf instanceof TrustManagerFactory);
    252             assertEquals("Incorrect algorithm", tmf.getAlgorithm(),
    253                     validValues[i]);
    254             assertEquals("Incorrect provider", tmf.getProvider(), mProv);
    255             checkResult(tmf);
    256         }
    257     }
    258 }