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.tests.javax.net.ssl;
     19 
     20 import dalvik.annotation.KnownFailure;
     21 import java.io.IOException;
     22 import java.security.InvalidAlgorithmParameterException;
     23 import java.security.KeyStore;
     24 import java.security.KeyStoreException;
     25 import java.security.NoSuchAlgorithmException;
     26 import java.security.NoSuchProviderException;
     27 import java.security.Provider;
     28 import java.security.PublicKey;
     29 import java.security.Security;
     30 import java.security.cert.CertificateException;
     31 import java.security.cert.PKIXBuilderParameters;
     32 import java.security.cert.TrustAnchor;
     33 import java.security.cert.X509CertSelector;
     34 import java.util.HashSet;
     35 import java.util.Set;
     36 import javax.net.ssl.CertPathTrustManagerParameters;
     37 import javax.net.ssl.ManagerFactoryParameters;
     38 import javax.net.ssl.TrustManager;
     39 import javax.net.ssl.TrustManagerFactory;
     40 import javax.net.ssl.TrustManagerFactorySpi;
     41 import junit.framework.TestCase;
     42 import org.apache.harmony.security.tests.support.SpiEngUtils;
     43 import org.apache.harmony.security.tests.support.TestKeyPair;
     44 import org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi;
     45 
     46 /**
     47  * Tests for <code>TrustManagerFactory</code> class constructors and methods.
     48  *
     49  */
     50 public class TrustManagerFactory1Test extends TestCase {
     51 
     52     private static final String srvTrustManagerFactory = "TrustManagerFactory";
     53 
     54     private static final String[] invalidValues = SpiEngUtils.invalidValues;
     55 
     56     private static String DEFAULT_ALGORITHM;
     57     private static String DEFAULT_PROVIDER_NAME;
     58     private static Provider DEFAULT_PROVIDER;
     59     private static String[] VALID_VALUES;
     60 
     61     private static String getDefaultAlgorithm() {
     62         init();
     63         return DEFAULT_ALGORITHM;
     64     }
     65     private static String getDefaultProviderName() {
     66         init();
     67         return DEFAULT_PROVIDER_NAME;
     68     }
     69     private static Provider getDefaultProvider() {
     70         init();
     71         return DEFAULT_PROVIDER;
     72     }
     73     private static String[] getValidValues() {
     74         init();
     75         return VALID_VALUES;
     76     }
     77 
     78     private static synchronized void init() {
     79         if (DEFAULT_ALGORITHM != null) {
     80             return;
     81         }
     82         DEFAULT_ALGORITHM = Security.getProperty("ssl.TrustManagerFactory.algorithm");
     83         assertNotNull(DEFAULT_ALGORITHM);
     84         DEFAULT_PROVIDER = SpiEngUtils.isSupport(DEFAULT_ALGORITHM, srvTrustManagerFactory);
     85         DEFAULT_PROVIDER_NAME = DEFAULT_PROVIDER.getName();
     86         VALID_VALUES = new String[] { DEFAULT_ALGORITHM,
     87                                       DEFAULT_ALGORITHM.toUpperCase(),
     88                                       DEFAULT_ALGORITHM.toLowerCase() };
     89     }
     90 
     91     private static TrustManagerFactory[] createTMFac() throws Exception {
     92         return new TrustManagerFactory[] {
     93             TrustManagerFactory.getInstance(getDefaultAlgorithm()),
     94             TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider()),
     95             TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProviderName())
     96         };
     97     }
     98 
     99     public void test_ConstructorLjavax_net_ssl_TrustManagerFactorySpiLjava_security_ProviderLjava_lang_String()
    100             throws NoSuchAlgorithmException {
    101         TrustManagerFactorySpi spi = new MyTrustManagerFactorySpi();
    102         TrustManagerFactory tmF = new myTrustManagerFactory(spi, getDefaultProvider(),
    103                 getDefaultAlgorithm());
    104         assertTrue("Not CertStore object", tmF instanceof TrustManagerFactory);
    105         assertEquals("Incorrect algorithm", tmF.getAlgorithm(),
    106                 getDefaultAlgorithm());
    107         assertEquals("Incorrect provider", tmF.getProvider(), getDefaultProvider());
    108         assertNull("Incorrect result", tmF.getTrustManagers());
    109 
    110         tmF = new myTrustManagerFactory(null, null, null);
    111         assertTrue("Not CertStore object", tmF instanceof TrustManagerFactory);
    112         assertNull("Provider must be null", tmF.getProvider());
    113         assertNull("Algorithm must be null", tmF.getAlgorithm());
    114         try {
    115             tmF.getTrustManagers();
    116             fail("NullPointerException must be thrown");
    117         } catch (NullPointerException e) {
    118         }
    119     }
    120 
    121     /**
    122      *  Test for <code>getAlgorithm()</code> method
    123      * Assertion: returns the algorithm name of this object
    124      * @throws NoSuchAlgorithmException
    125      * @throws NoSuchProviderException
    126      */
    127     public void test_getAlgorithm()
    128         throws NoSuchAlgorithmException, NoSuchProviderException {
    129         assertEquals("Incorrect algorithm",
    130                 getDefaultAlgorithm(),
    131                 TrustManagerFactory
    132                 .getInstance(getDefaultAlgorithm()).getAlgorithm());
    133         assertEquals("Incorrect algorithm",
    134                 getDefaultAlgorithm(),
    135                 TrustManagerFactory
    136                 .getInstance(getDefaultAlgorithm(), getDefaultProviderName())
    137                 .getAlgorithm());
    138         assertEquals("Incorrect algorithm",
    139                 getDefaultAlgorithm(),
    140                 TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider())
    141                 .getAlgorithm());
    142     }
    143 
    144     /**
    145      *  Test for <code>getDefaultAlgorithm()</code> method
    146      * Assertion: returns value which is specifoed in security property
    147      */
    148     public void test_getDefaultAlgorithm() {
    149         String def = TrustManagerFactory.getDefaultAlgorithm();
    150         if (getDefaultAlgorithm() == null) {
    151             assertNull("DefaultAlgorithm must be null", def);
    152         } else {
    153             assertEquals("Invalid default algorithm", def, getDefaultAlgorithm());
    154         }
    155         String defA = "Proba.trustmanagerfactory.defaul.type";
    156         Security.setProperty("ssl.TrustManagerFactory.algorithm", defA);
    157         assertEquals("Incorrect getDefaultAlgorithm()",
    158                 TrustManagerFactory.getDefaultAlgorithm(), defA);
    159         if (def == null) {
    160             def = "";
    161         }
    162         Security.setProperty("ssl.TrustManagerFactory.algorithm", def);
    163         assertEquals("Incorrect getDefaultAlgorithm()",
    164                 TrustManagerFactory.getDefaultAlgorithm(), def);
    165     }
    166 
    167     /**
    168      * Test for <code>getInstance(String algorithm)</code> method
    169      * Assertions: returns security property "ssl.TrustManagerFactory.algorithm";
    170      * returns instance of TrustManagerFactory
    171      */
    172     public void test_getInstanceLjava_lang_String01() throws NoSuchAlgorithmException {
    173         for (String validValue : getValidValues()) {
    174             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue);
    175             assertTrue("Not TrustManagerFactory object",
    176                        trustMF instanceof TrustManagerFactory);
    177             assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue);
    178         }
    179     }
    180 
    181     /**
    182      * Test for <code>getInstance(String algorithm)</code> method
    183      * Assertion:
    184      * throws NullPointerException when algorithm is null;
    185      * throws NoSuchAlgorithmException when algorithm is not correct;
    186      */
    187     public void test_getInstanceLjava_lang_String02() {
    188         try {
    189             TrustManagerFactory.getInstance(null);
    190             fail();
    191         } catch (NoSuchAlgorithmException expected) {
    192         } catch (NullPointerException expected) {
    193         }
    194         for (int i = 0; i < invalidValues.length; i++) {
    195             try {
    196                 TrustManagerFactory.getInstance(invalidValues[i]);
    197                 fail("NoSuchAlgorithmException was not thrown as expected for algorithm: "
    198                         .concat(invalidValues[i]));
    199             } catch (NoSuchAlgorithmException e) {
    200             }
    201         }
    202     }
    203 
    204     /**
    205      * Test for <code>getInstance(String algorithm, String provider)</code>
    206      * method
    207      * Assertion: throws IllegalArgumentException when provider is null
    208      * or empty
    209      */
    210     public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws Exception {
    211         for (String validValue : getValidValues()) {
    212             try {
    213                 TrustManagerFactory.getInstance(validValue, (String) null);
    214                 fail();
    215             } catch (IllegalArgumentException expected) {
    216             }
    217             try {
    218                 TrustManagerFactory.getInstance(validValue, "");
    219                 fail();
    220             } catch (IllegalArgumentException expected) {
    221             }
    222         }
    223     }
    224 
    225     /**
    226      * Test for <code>getInstance(String algorithm, String provider)</code>
    227      * method
    228      * Assertion:
    229      * throws NullPointerException when algorithm is null;
    230      * throws NoSuchAlgorithmException when algorithm is not correct;
    231      */
    232     public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws Exception {
    233         try {
    234             TrustManagerFactory.getInstance(null, getDefaultProviderName());
    235             fail();
    236         } catch (NoSuchAlgorithmException expected) {
    237         } catch (NullPointerException expected) {
    238         }
    239         for (int i = 0; i < invalidValues.length; i++) {
    240             try {
    241                 TrustManagerFactory.getInstance(invalidValues[i],
    242                         getDefaultProviderName());
    243                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    244                         .concat(invalidValues[i]).concat(")"));
    245             } catch (NoSuchAlgorithmException e) {
    246             }
    247         }
    248     }
    249 
    250     /**
    251      * Test for <code>getInstance(String algorithm, String provider)</code>
    252      * method
    253      * Assertion: throws NoSuchProviderException when provider has
    254      * invalid value
    255      */
    256     public void test_getInstanceLjava_lang_StringLjava_lang_String03() throws Exception {
    257         for (String invalidValue : invalidValues) {
    258             for (String validValue : getValidValues()) {
    259                 try {
    260                     TrustManagerFactory.getInstance(validValue, invalidValue);
    261                     fail("NoSuchProviderException must be thrown (algorithm: "
    262                             .concat(validValue).concat(" provider: ")
    263                             .concat(invalidValue).concat(")"));
    264                 } catch (NoSuchProviderException expected) {
    265                     assertFalse("".equals(invalidValue));
    266                 } catch (IllegalArgumentException expected) {
    267                     assertEquals("", invalidValue);
    268                 }
    269             }
    270         }
    271     }
    272 
    273     /**
    274      * Test for <code>getInstance(String algorithm, String provider)</code>
    275      * method
    276      * Assertion: returns instance of TrustManagerFactory
    277      */
    278     public void test_getInstanceLjava_lang_StringLjava_lang_String04() throws Exception {
    279         for (String validValue : getValidValues()) {
    280             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue,
    281                                                                           getDefaultProviderName());
    282             assertTrue("Not TrustManagerFactory object",
    283                        trustMF instanceof TrustManagerFactory);
    284             assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue);
    285             assertEquals("Invalid provider", trustMF.getProvider(), getDefaultProvider());
    286         }
    287     }
    288 
    289     /**
    290      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    291      * method
    292      * Assertion: throws IllegalArgumentException when provider is null
    293      */
    294     public void test_getInstanceLjava_lang_StringLjava_security_Provider01() throws Exception {
    295         for (String validValue : getValidValues()) {
    296             try {
    297                 TrustManagerFactory.getInstance(validValue, (Provider) null);
    298                 fail();
    299             } catch (IllegalArgumentException expected) {
    300             }
    301         }
    302     }
    303 
    304     /**
    305      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    306      * method
    307      * Assertion:
    308      * throws NullPointerException when algorithm is null;
    309      * throws NoSuchAlgorithmException when algorithm is not correct;
    310      */
    311     public void test_getInstanceLjava_lang_StringLjava_security_Provider02() {
    312         try {
    313             TrustManagerFactory.getInstance(null, getDefaultProvider());
    314             fail("");
    315         } catch (NoSuchAlgorithmException expected) {
    316         } catch (NullPointerException expected) {
    317         }
    318         for (int i = 0; i < invalidValues.length; i++) {
    319             try {
    320                 TrustManagerFactory.getInstance(invalidValues[i],
    321                         getDefaultProvider());
    322                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    323                         .concat(invalidValues[i]).concat(")"));
    324             } catch (NoSuchAlgorithmException e) {
    325             }
    326         }
    327     }
    328 
    329     /**
    330      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    331      * method
    332      * Assertion: returns instance of TrustManagerFactory
    333      */
    334     public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws Exception {
    335         for (String validValue : getValidValues()) {
    336             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue,
    337                                                                           getDefaultProvider());
    338             assertTrue("Not TrustManagerFactory object",
    339                        trustMF instanceof TrustManagerFactory);
    340             assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue);
    341             assertEquals("Invalid provider", trustMF.getProvider(), getDefaultProvider());
    342         }
    343     }
    344 
    345     /**
    346      * Test for <code>getProvider()</code>
    347      * @throws NoSuchAlgorithmException
    348      * @throws NoSuchProviderException
    349      */
    350     public void test_getProvider()
    351         throws NoSuchAlgorithmException, NoSuchProviderException {
    352         assertEquals("Incorrect provider",
    353                 getDefaultProvider(),
    354                 TrustManagerFactory
    355                 .getInstance(getDefaultAlgorithm()).getProvider());
    356         assertEquals("Incorrect provider",
    357                 getDefaultProvider(),
    358                 TrustManagerFactory
    359                 .getInstance(getDefaultAlgorithm(), getDefaultProviderName())
    360                 .getProvider());
    361         assertEquals("Incorrect provider",
    362                 getDefaultProvider(),
    363                 TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider())
    364                 .getProvider());
    365     }
    366 
    367     /**
    368      * Test for <code>geTrustManagers()</code>
    369      * @throws KeyStoreException
    370      * @throws IOException
    371      * @throws CertificateException
    372      * @throws NoSuchAlgorithmException
    373      */
    374     public void test_getTrustManagers() {
    375         try {
    376             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm());
    377             KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    378             ks.load(null, null);
    379             trustMF.init(ks);
    380             TrustManager[] tm = trustMF.getTrustManagers();
    381             assertNotNull("Result has not be null", tm);
    382             assertTrue("Length of result TrustManager array should not be 0",
    383                     (tm.length > 0));
    384         } catch (Exception ex) {
    385             fail("Unexpected exception " + ex.toString());
    386         }
    387     }
    388 
    389     /**
    390      * Test for <code>init(KeyStore keyStore)</code>
    391      * Assertion: call method with null parameter
    392      */
    393     public void test_initLjava_security_KeyStore_01() throws Exception {
    394         KeyStore ksNull = null;
    395         TrustManagerFactory[] trustMF = createTMFac();
    396         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    397         // null parameter
    398         try {
    399             trustMF[0].init(ksNull);
    400         } catch (Exception ex) {
    401             fail(ex + " unexpected exception was thrown for null parameter");
    402         }
    403     }
    404 
    405     /**
    406      * Test for <code>init(KeyStore keyStore)</code>
    407      * Assertion: call method with not null parameter
    408      */
    409     public void test_initLjava_security_KeyStore_02() throws Exception {
    410         KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    411         TrustManagerFactory[] trustMF = createTMFac();
    412         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    413 
    414         // not null parameter
    415         trustMF[0].init(ks);
    416     }
    417 
    418     /**
    419      * Test for <code>init(ManagerFactoryParameters params)</code>
    420      * Assertion:
    421      * throws InvalidAlgorithmParameterException when params is null
    422      */
    423     @KnownFailure("ManagerFactoryParameters object is not supported "
    424                   + "and InvalidAlgorithmParameterException was thrown.")
    425     public void test_initLjavax_net_ssl_ManagerFactoryParameters() throws Exception {
    426         ManagerFactoryParameters par = null;
    427         TrustManagerFactory[] trustMF = createTMFac();
    428         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    429         for (int i = 0; i < trustMF.length; i++) {
    430             try {
    431                 trustMF[i].init(par);
    432                 fail("InvalidAlgorithmParameterException must be thrown");
    433             } catch (InvalidAlgorithmParameterException e) {
    434             }
    435         }
    436 
    437         String keyAlg = "DSA";
    438         String validCaNameRfc2253 = ("CN=Test CA,"
    439                                      + "OU=Testing Division,"
    440                                      + "O=Test It All,"
    441                                      + "L=Test Town,"
    442                                      + "ST=Testifornia,"
    443                                      + "C=Testland");
    444 
    445         try {
    446             KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType());
    447             kStore.load(null, null);
    448             PublicKey pk = new TestKeyPair(keyAlg).getPublic();
    449             TrustAnchor ta = new TrustAnchor(validCaNameRfc2253, pk, getFullEncoding());
    450             Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
    451             trustAnchors.add(ta);
    452             X509CertSelector xcs = new X509CertSelector();
    453             PKIXBuilderParameters pkixBP = new PKIXBuilderParameters(trustAnchors, xcs);
    454             CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pkixBP);
    455             TrustManagerFactory tmf = TrustManagerFactory.getInstance(getDefaultAlgorithm());
    456             try {
    457                 tmf.init(cptmp);
    458             } catch (Exception ex) {
    459                 fail(ex + " was thrown for init(ManagerFactoryParameters spec)");
    460             }
    461         } catch (Exception e) {
    462             fail("Unexpected exception for configuration: " + e);
    463         }
    464 
    465     }
    466 
    467     private static final byte[] getFullEncoding() {
    468         // DO NOT MODIFY!
    469         return new byte[] {
    470                 (byte)0x30,(byte)0x81,(byte)0x8c,(byte)0xa0,
    471                 (byte)0x44,(byte)0x30,(byte)0x16,(byte)0x86,
    472                 (byte)0x0e,(byte)0x66,(byte)0x69,(byte)0x6c,
    473                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    474                 (byte)0x66,(byte)0x6f,(byte)0x6f,(byte)0x2e,
    475                 (byte)0x63,(byte)0x6f,(byte)0x6d,(byte)0x80,
    476                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    477                 (byte)0x01,(byte)0x30,(byte)0x16,(byte)0x86,
    478                 (byte)0x0e,(byte)0x66,(byte)0x69,(byte)0x6c,
    479                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    480                 (byte)0x62,(byte)0x61,(byte)0x72,(byte)0x2e,
    481                 (byte)0x63,(byte)0x6f,(byte)0x6d,(byte)0x80,
    482                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    483                 (byte)0x01,(byte)0x30,(byte)0x12,(byte)0x86,
    484                 (byte)0x0a,(byte)0x66,(byte)0x69,(byte)0x6c,
    485                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    486                 (byte)0x6d,(byte)0x75,(byte)0x75,(byte)0x80,
    487                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    488                 (byte)0x01,(byte)0xa1,(byte)0x44,(byte)0x30,
    489                 (byte)0x16,(byte)0x86,(byte)0x0e,(byte)0x68,
    490                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    491                 (byte)0x2f,(byte)0x2f,(byte)0x66,(byte)0x6f,
    492                 (byte)0x6f,(byte)0x2e,(byte)0x63,(byte)0x6f,
    493                 (byte)0x6d,(byte)0x80,(byte)0x01,(byte)0x00,
    494                 (byte)0x81,(byte)0x01,(byte)0x01,(byte)0x30,
    495                 (byte)0x16,(byte)0x86,(byte)0x0e,(byte)0x68,
    496                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    497                 (byte)0x2f,(byte)0x2f,(byte)0x62,(byte)0x61,
    498                 (byte)0x72,(byte)0x2e,(byte)0x63,(byte)0x6f,
    499                 (byte)0x6d,(byte)0x80,(byte)0x01,(byte)0x00,
    500                 (byte)0x81,(byte)0x01,(byte)0x01,(byte)0x30,
    501                 (byte)0x12,(byte)0x86,(byte)0x0a,(byte)0x68,
    502                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    503                 (byte)0x2f,(byte)0x2f,(byte)0x6d,(byte)0x75,
    504                 (byte)0x75,(byte)0x80,(byte)0x01,(byte)0x00,
    505                 (byte)0x81,(byte)0x01,(byte)0x01
    506         };
    507     }
    508 }
    509 
    510 /**
    511  * Addifional class to verify TrustManagerFactory constructor
    512  */
    513 
    514 class myTrustManagerFactory extends TrustManagerFactory {
    515     public myTrustManagerFactory(TrustManagerFactorySpi spi, Provider prov,
    516             String alg) {
    517         super(spi, prov, alg);
    518     }
    519 }
    520 
    521