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