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