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.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             } catch (IllegalArgumentException expected) {
    299             }
    300         }
    301     }
    302 
    303     /**
    304      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    305      * method
    306      * Assertion:
    307      * throws NullPointerException when algorithm is null;
    308      * throws NoSuchAlgorithmException when algorithm is not correct;
    309      */
    310     public void test_getInstanceLjava_lang_StringLjava_security_Provider02() {
    311         try {
    312             TrustManagerFactory.getInstance(null, getDefaultProvider());
    313             fail("");
    314         } catch (NoSuchAlgorithmException expected) {
    315         } catch (NullPointerException expected) {
    316         }
    317         for (int i = 0; i < invalidValues.length; i++) {
    318             try {
    319                 TrustManagerFactory.getInstance(invalidValues[i],
    320                         getDefaultProvider());
    321                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    322                         .concat(invalidValues[i]).concat(")"));
    323             } catch (NoSuchAlgorithmException e) {
    324             }
    325         }
    326     }
    327 
    328     /**
    329      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    330      * method
    331      * Assertion: returns instance of TrustManagerFactory
    332      */
    333     public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws Exception {
    334         for (String validValue : getValidValues()) {
    335             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(validValue,
    336                                                                           getDefaultProvider());
    337             assertTrue("Not TrustManagerFactory object",
    338                        trustMF instanceof TrustManagerFactory);
    339             assertEquals("Invalid algorithm", trustMF.getAlgorithm(), validValue);
    340             assertEquals("Invalid provider", trustMF.getProvider(), getDefaultProvider());
    341         }
    342     }
    343 
    344     /**
    345      * Test for <code>getProvider()</code>
    346      * @throws NoSuchAlgorithmException
    347      * @throws NoSuchProviderException
    348      */
    349     public void test_getProvider()
    350         throws NoSuchAlgorithmException, NoSuchProviderException {
    351         assertEquals("Incorrect provider",
    352                 getDefaultProvider(),
    353                 TrustManagerFactory
    354                 .getInstance(getDefaultAlgorithm()).getProvider());
    355         assertEquals("Incorrect provider",
    356                 getDefaultProvider(),
    357                 TrustManagerFactory
    358                 .getInstance(getDefaultAlgorithm(), getDefaultProviderName())
    359                 .getProvider());
    360         assertEquals("Incorrect provider",
    361                 getDefaultProvider(),
    362                 TrustManagerFactory.getInstance(getDefaultAlgorithm(), getDefaultProvider())
    363                 .getProvider());
    364     }
    365 
    366     /**
    367      * Test for <code>geTrustManagers()</code>
    368      * @throws KeyStoreException
    369      * @throws IOException
    370      * @throws CertificateException
    371      * @throws NoSuchAlgorithmException
    372      */
    373     public void test_getTrustManagers() {
    374         try {
    375             TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm());
    376             KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    377             ks.load(null, null);
    378             trustMF.init(ks);
    379             TrustManager[] tm = trustMF.getTrustManagers();
    380             assertNotNull("Result has not be null", tm);
    381             assertTrue("Length of result TrustManager array should not be 0",
    382                     (tm.length > 0));
    383         } catch (Exception ex) {
    384             fail("Unexpected exception " + ex.toString());
    385         }
    386     }
    387 
    388     /**
    389      * Test for <code>init(KeyStore keyStore)</code>
    390      * Assertion: call method with null parameter
    391      */
    392     public void test_initLjava_security_KeyStore_01() throws Exception {
    393         KeyStore ksNull = null;
    394         TrustManagerFactory[] trustMF = createTMFac();
    395         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    396         // null parameter
    397         try {
    398             trustMF[0].init(ksNull);
    399         } catch (Exception ex) {
    400             fail(ex + " unexpected exception was thrown for null parameter");
    401         }
    402     }
    403 
    404     /**
    405      * Test for <code>init(KeyStore keyStore)</code>
    406      * Assertion: call method with not null parameter
    407      */
    408     public void test_initLjava_security_KeyStore_02() throws Exception {
    409         KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    410         TrustManagerFactory[] trustMF = createTMFac();
    411         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    412 
    413         // not null parameter
    414         trustMF[0].init(ks);
    415     }
    416 
    417     /**
    418      * Test for <code>init(ManagerFactoryParameters params)</code>
    419      * Assertion:
    420      * throws InvalidAlgorithmParameterException when params is null
    421      */
    422     @KnownFailure("ManagerFactoryParameters object is not supported "
    423                   + "and InvalidAlgorithmParameterException was thrown.")
    424     public void test_initLjavax_net_ssl_ManagerFactoryParameters() throws Exception {
    425         ManagerFactoryParameters par = null;
    426         TrustManagerFactory[] trustMF = createTMFac();
    427         assertNotNull("TrustManagerFactory objects were not created", trustMF);
    428         for (int i = 0; i < trustMF.length; i++) {
    429             try {
    430                 trustMF[i].init(par);
    431                 fail("InvalidAlgorithmParameterException must be thrown");
    432             } catch (InvalidAlgorithmParameterException e) {
    433             }
    434         }
    435 
    436         String keyAlg = "DSA";
    437         String validCaNameRfc2253 = ("CN=Test CA,"
    438                                      + "OU=Testing Division,"
    439                                      + "O=Test It All,"
    440                                      + "L=Test Town,"
    441                                      + "ST=Testifornia,"
    442                                      + "C=Testland");
    443 
    444         try {
    445             KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType());
    446             kStore.load(null, null);
    447             PublicKey pk = new TestKeyPair(keyAlg).getPublic();
    448             TrustAnchor ta = new TrustAnchor(validCaNameRfc2253, pk, getFullEncoding());
    449             Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
    450             trustAnchors.add(ta);
    451             X509CertSelector xcs = new X509CertSelector();
    452             PKIXBuilderParameters pkixBP = new PKIXBuilderParameters(trustAnchors, xcs);
    453             CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pkixBP);
    454             TrustManagerFactory tmf = TrustManagerFactory.getInstance(getDefaultAlgorithm());
    455             try {
    456                 tmf.init(cptmp);
    457             } catch (Exception ex) {
    458                 fail(ex + " was thrown for init(ManagerFactoryParameters spec)");
    459             }
    460         } catch (Exception e) {
    461             fail("Unexpected exception for configuration: " + e);
    462         }
    463 
    464     }
    465 
    466     private static final byte[] getFullEncoding() {
    467         // DO NOT MODIFY!
    468         return new byte[] {
    469                 (byte)0x30,(byte)0x81,(byte)0x8c,(byte)0xa0,
    470                 (byte)0x44,(byte)0x30,(byte)0x16,(byte)0x86,
    471                 (byte)0x0e,(byte)0x66,(byte)0x69,(byte)0x6c,
    472                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    473                 (byte)0x66,(byte)0x6f,(byte)0x6f,(byte)0x2e,
    474                 (byte)0x63,(byte)0x6f,(byte)0x6d,(byte)0x80,
    475                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    476                 (byte)0x01,(byte)0x30,(byte)0x16,(byte)0x86,
    477                 (byte)0x0e,(byte)0x66,(byte)0x69,(byte)0x6c,
    478                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    479                 (byte)0x62,(byte)0x61,(byte)0x72,(byte)0x2e,
    480                 (byte)0x63,(byte)0x6f,(byte)0x6d,(byte)0x80,
    481                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    482                 (byte)0x01,(byte)0x30,(byte)0x12,(byte)0x86,
    483                 (byte)0x0a,(byte)0x66,(byte)0x69,(byte)0x6c,
    484                 (byte)0x65,(byte)0x3a,(byte)0x2f,(byte)0x2f,
    485                 (byte)0x6d,(byte)0x75,(byte)0x75,(byte)0x80,
    486                 (byte)0x01,(byte)0x00,(byte)0x81,(byte)0x01,
    487                 (byte)0x01,(byte)0xa1,(byte)0x44,(byte)0x30,
    488                 (byte)0x16,(byte)0x86,(byte)0x0e,(byte)0x68,
    489                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    490                 (byte)0x2f,(byte)0x2f,(byte)0x66,(byte)0x6f,
    491                 (byte)0x6f,(byte)0x2e,(byte)0x63,(byte)0x6f,
    492                 (byte)0x6d,(byte)0x80,(byte)0x01,(byte)0x00,
    493                 (byte)0x81,(byte)0x01,(byte)0x01,(byte)0x30,
    494                 (byte)0x16,(byte)0x86,(byte)0x0e,(byte)0x68,
    495                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    496                 (byte)0x2f,(byte)0x2f,(byte)0x62,(byte)0x61,
    497                 (byte)0x72,(byte)0x2e,(byte)0x63,(byte)0x6f,
    498                 (byte)0x6d,(byte)0x80,(byte)0x01,(byte)0x00,
    499                 (byte)0x81,(byte)0x01,(byte)0x01,(byte)0x30,
    500                 (byte)0x12,(byte)0x86,(byte)0x0a,(byte)0x68,
    501                 (byte)0x74,(byte)0x74,(byte)0x70,(byte)0x3a,
    502                 (byte)0x2f,(byte)0x2f,(byte)0x6d,(byte)0x75,
    503                 (byte)0x75,(byte)0x80,(byte)0x01,(byte)0x00,
    504                 (byte)0x81,(byte)0x01,(byte)0x01
    505         };
    506     }
    507 }
    508 
    509 /**
    510  * Addifional class to verify TrustManagerFactory constructor
    511  */
    512 
    513 class myTrustManagerFactory extends TrustManagerFactory {
    514     public myTrustManagerFactory(TrustManagerFactorySpi spi, Provider prov,
    515             String alg) {
    516         super(spi, prov, alg);
    517     }
    518 }
    519 
    520