Home | History | Annotate | Download | only in cert
      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 /**
     19  * @author Vladimir N. Molotkov
     20  * @version $Revision$
     21  */
     22 
     23 package libcore.java.security.cert;
     24 
     25 import java.io.ByteArrayInputStream;
     26 import java.security.InvalidAlgorithmParameterException;
     27 import java.security.KeyStore;
     28 import java.security.KeyStoreException;
     29 import java.security.cert.CertificateFactory;
     30 import java.security.cert.PKIXParameters;
     31 import java.security.cert.TrustAnchor;
     32 import java.util.Calendar;
     33 import java.util.Collections;
     34 import java.util.Set;
     35 import junit.framework.TestCase;
     36 import org.apache.harmony.security.tests.support.cert.TestUtils;
     37 import tests.targets.security.KeyStoreTestPKCS12;
     38 
     39 /**
     40  * Tests for <code>PKIXParameters</code> fields and methods
     41  */
     42 public class OldPKIXParametersTest extends TestCase {
     43 
     44     public final void testClone() throws InvalidAlgorithmParameterException {
     45         Set<TrustAnchor> taSet = TestUtils.getTrustAnchorSet();
     46         if (taSet == null) {
     47             fail(getName()
     48                     + ": not performed (could not create test TrustAnchor set)");
     49         }
     50 
     51         PKIXParameters cpp = new PKIXParameters(taSet);
     52         PKIXParameters cppc = (PKIXParameters) cpp.clone();
     53 
     54         assertEquals(cpp.getPolicyQualifiersRejected(), cppc
     55                 .getPolicyQualifiersRejected());
     56         assertEquals(cpp.getCertPathCheckers(), cppc.getCertPathCheckers());
     57         assertEquals(cpp.getCertStores(), cppc.getCertStores());
     58         assertEquals(cpp.getDate(), cppc.getDate());
     59         assertEquals(cpp.getInitialPolicies(), cppc.getInitialPolicies());
     60         assertEquals(cpp.getSigProvider(), cppc.getSigProvider());
     61         assertEquals(cpp.getTargetCertConstraints(), cppc
     62                 .getTargetCertConstraints());
     63         assertEquals(cpp.getTrustAnchors(), cppc.getTrustAnchors());
     64 
     65         assertEquals(cpp.isAnyPolicyInhibited(), cppc.isAnyPolicyInhibited());
     66         assertEquals(cpp.isExplicitPolicyRequired(), cppc
     67                 .isExplicitPolicyRequired());
     68         assertEquals(cpp.isPolicyMappingInhibited(), cppc
     69                 .isPolicyMappingInhibited());
     70         assertEquals(cpp.isRevocationEnabled(), cppc.isRevocationEnabled());
     71 
     72         cpp.setDate(Calendar.getInstance().getTime());
     73         cpp.setPolicyQualifiersRejected(!cppc.getPolicyQualifiersRejected());
     74         assertFalse(cpp.getDate().equals(cppc.getDate()));
     75         assertFalse(cpp.getPolicyQualifiersRejected() == cppc
     76                 .getPolicyQualifiersRejected());
     77 
     78         cppc.setExplicitPolicyRequired(!cpp.isExplicitPolicyRequired());
     79         cppc.setRevocationEnabled(!cpp.isRevocationEnabled());
     80 
     81         assertFalse(cpp.isExplicitPolicyRequired() == cppc
     82                 .isExplicitPolicyRequired());
     83         assertFalse(cpp.isRevocationEnabled() == cppc.isRevocationEnabled());
     84 
     85         PKIXParameters cpp1 = null;
     86         try {
     87             cpp1.clone();
     88         } catch (NullPointerException e) {
     89             // expected
     90         }
     91     }
     92 
     93     /**
     94      * Test for <code>isPolicyMappingInhibited()</code> method<br>
     95      * Assertion: returns true if policy mapping is inhibited, false otherwise
     96      * Assertion: by default, policy mapping is not inhibited (the flag is
     97      * false)
     98      *
     99      * @throws InvalidAlgorithmParameterException
    100      */
    101     public final void testIsPolicyMappingInhibited() throws Exception {
    102         Set<TrustAnchor> taSet = TestUtils.getTrustAnchorSet();
    103         if (taSet == null) {
    104             fail(getName() + ": not performed (could not create test TrustAnchor set)");
    105         }
    106 
    107         PKIXParameters p = new PKIXParameters(taSet);
    108         assertFalse(p.isPolicyMappingInhibited());
    109 
    110         CertificateFactory cf = CertificateFactory.getInstance("X.509");
    111         TestUtils.initCertPathSSCertChain();
    112         Set<TrustAnchor> taSet2 = Collections.singleton(new TrustAnchor(
    113                TestUtils.rootCertificateSS, null));
    114         p = new PKIXParameters(taSet2);
    115 
    116         assertFalse(p.isPolicyMappingInhibited());
    117         p.setPolicyMappingInhibited(true);
    118         assertTrue(p.isRevocationEnabled());
    119     }
    120 
    121     /**
    122      * Test for <code>isPolicyMappingInhibited()</code> method<br>
    123      * Assertion: returns the current value of the RevocationEnabled flag
    124      * Assertion: when a <code>PKIXParameters</code> object is created, this
    125      * flag is set to true
    126      *
    127      * @throws InvalidAlgorithmParameterException
    128      */
    129     public final void testIsRevocationEnabled() throws Exception {
    130         Set<TrustAnchor> taSet = TestUtils.getTrustAnchorSet();
    131         if (taSet == null) {
    132             fail(getName()
    133                     + ": not performed (could not create test TrustAnchor set)");
    134         }
    135 
    136         PKIXParameters p = new PKIXParameters(taSet);
    137         assertTrue(p.isRevocationEnabled());
    138 
    139         CertificateFactory cf = CertificateFactory.getInstance("X.509");
    140        TestUtils.initCertPathSSCertChain();
    141        Set<TrustAnchor> taSet2 = Collections.singleton(new TrustAnchor(
    142               TestUtils.rootCertificateSS, null));
    143        p = new PKIXParameters(taSet2);
    144 
    145        assertTrue(p.isRevocationEnabled());
    146        p.setRevocationEnabled(false);
    147        assertFalse(p.isRevocationEnabled());
    148     }
    149 
    150     /**
    151      * Test for <code>toString</code> method<br>
    152      */
    153     public final void testToString() throws Exception {
    154         Set<TrustAnchor> taSet = TestUtils.getTrustAnchorSet();
    155         if (taSet == null) {
    156             fail(getName()
    157                     + ": not performed (could not create test TrustAnchor set)");
    158         }
    159 
    160         PKIXParameters p = new PKIXParameters(taSet);
    161         assertNotNull(p.toString());
    162 
    163         PKIXParameters p1 = null;
    164         try {
    165             p1.toString();
    166             fail("NullPointerException expected");
    167         } catch (NullPointerException e) {
    168             // expected
    169         }
    170     }
    171 
    172     /**
    173      * Test #4 for <code>PKIXParameters(KeyStore)</code> constructor<br>
    174      *
    175      * @throws InvalidAlgorithmParameterException
    176      * @throws KeyStoreException
    177      */
    178     // Broken Test: Fails in CTS environment, but passes in CoreTestRunner
    179     public final void testPKIXParametersKeyStore04() throws Exception {
    180 
    181 
    182         KeyStore store = KeyStore.getInstance("PKCS12");
    183         KeyStoreTestPKCS12 k = new KeyStoreTestPKCS12();
    184         ByteArrayInputStream stream = new ByteArrayInputStream(k.keyStoreData);
    185 
    186         try {
    187             PKIXParameters p = new PKIXParameters(store);
    188         } catch (KeyStoreException e) {
    189             // ok
    190         }
    191 
    192         store = KeyStore.getInstance("PKCS12");
    193         store.load(stream, new String(KeyStoreTestPKCS12.keyStorePassword)
    194                 .toCharArray());
    195         stream.close();
    196 
    197         try {
    198               PKIXParameters p = new PKIXParameters(store);
    199         } catch (InvalidAlgorithmParameterException e) {
    200             // ok
    201         }
    202 
    203 
    204         KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    205         keystore.load(null,null);
    206         keystore.setCertificateEntry("test", TestUtils.rootCertificateSS);
    207 
    208 
    209         PKIXParameters p = new PKIXParameters(keystore);
    210     }
    211 }
    212