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