PKIXBuilderParametersTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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*/
21
22package org.apache.harmony.security.tests.java.security.cert;
23
24import java.security.InvalidAlgorithmParameterException;
25import java.security.KeyStore;
26import java.security.cert.PKIXBuilderParameters;
27import java.security.cert.PKIXParameters;
28import java.security.cert.X509CertSelector;
29import java.util.HashSet;
30import java.util.Set;
31
32import org.apache.harmony.security.tests.support.cert.TestUtils;
33
34import junit.framework.TestCase;
35
36/**
37 * Tests for <code>PKIXBuilderParameters</code> fields and methods
38 *
39 */
40public class PKIXBuilderParametersTest extends TestCase {
41    private static final int DEFAULT_MAX_PATH_LEN = 5;
42
43    /**
44     * Constructor for PKIXBuilderParametersTest.
45     * @param name
46     */
47    public PKIXBuilderParametersTest(String name) {
48        super(name);
49    }
50
51    /**
52     * Test #1 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
53     * constructor<br>
54     * Assertion: creates an instance of <code>PKIXBuilderParameters</code>
55     * @throws InvalidAlgorithmParameterException
56     */
57    public final void testPKIXBuilderParametersSetCertSelector01()
58        throws InvalidAlgorithmParameterException {
59        Set taSet = TestUtils.getTrustAnchorSet();
60        if (taSet == null) {
61            fail(getName() + ": not performed (could not create test TrustAnchor set)");
62        }
63        // both parameters are valid and non-null
64        PKIXParameters p =
65            new PKIXBuilderParameters(taSet, new X509CertSelector());
66        assertTrue("instanceOf", p instanceof PKIXBuilderParameters);
67        assertNotNull("certSelector", p.getTargetCertConstraints());
68    }
69
70    /**
71     * Test #2 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
72     * constructor<br>
73     * Assertion: creates an instance of <code>PKIXBuilderParameters</code>
74     * @throws InvalidAlgorithmParameterException
75     */
76    public final void testPKIXBuilderParametersSetCertSelector02()
77        throws InvalidAlgorithmParameterException {
78        Set taSet = TestUtils.getTrustAnchorSet();
79        if (taSet == null) {
80            fail(getName() + ": not performed (could not create test TrustAnchor set)");
81        }
82        // both parameters are valid but CertSelector is null
83        PKIXParameters p = new PKIXBuilderParameters(taSet, null);
84        assertTrue("instanceOf", p instanceof PKIXBuilderParameters);
85        assertNull("certSelector", p.getTargetCertConstraints());
86    }
87
88    /**
89     * Test #3 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
90     * constructor<br>
91     * Assertion: ... the <code>Set</code> is copied to protect against
92     * subsequent modifications
93     * @throws InvalidAlgorithmParameterException
94     */
95    public final void testPKIXBuilderParametersSetCertSelector03()
96        throws InvalidAlgorithmParameterException {
97        Set taSet = TestUtils.getTrustAnchorSet();
98        if (taSet == null) {
99            fail(getName() + ": not performed (could not create test TrustAnchor set)");
100        }
101        HashSet originalSet = (HashSet)taSet;
102        HashSet originalSetCopy = (HashSet)originalSet.clone();
103        // create test object using originalSet
104        PKIXBuilderParameters pp =
105            new PKIXBuilderParameters(originalSetCopy, null);
106        // modify originalSet
107        originalSetCopy.clear();
108        // check that test object's internal state
109        // has not been affected by the above modification
110        Set returnedSet = pp.getTrustAnchors();
111        assertEquals(originalSet, returnedSet);
112    }
113
114    /**
115     * Test #4 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
116     * constructor<br>
117     * Assertion: <code>NullPointerException</code> -
118     * if the specified <code>Set</code> is null
119     */
120    public final void testPKIXBuilderParametersSetCertSelector04() throws Exception {
121        try {
122            // pass null
123            new PKIXBuilderParameters((Set)null, null);
124            fail("NPE expected");
125        } catch (NullPointerException e) {
126        }
127    }
128
129    /**
130     * Test #5 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
131     * constructor<br>
132     * Assertion: <code>InvalidAlgorithmParameterException</code> -
133     * if the specified <code>Set</code> is empty
134     * (<code>trustAnchors.isEmpty() == true</code>)
135     */
136    public final void testPKIXBuilderParametersSetCertSelector05() {
137        try {
138            // use empty set
139            new PKIXBuilderParameters(new HashSet(), null);
140            fail("InvalidAlgorithmParameterException expected");
141        } catch (InvalidAlgorithmParameterException e) {
142        }
143    }
144
145    /**
146     * Test #6 for <code>PKIXBuilderParameters(Set, CertSelector)</code>
147     * constructor<br>
148     * Assertion: <code>ClassCastException</code> -
149     * if any of the elements in the <code>Set</code> are not of type
150     * <code>java.security.cert.TrustAnchor</code>
151     */
152    public final void testPKIXBuilderParametersSetCertSelector06() throws Exception {
153        Set taSet = TestUtils.getTrustAnchorSet();
154        if (taSet == null) {
155            fail(getName() + ": not performed (could not create test TrustAnchor set)");
156        }
157
158        // add wrong object to valid set
159        assertTrue(taSet.add(new Object()));
160
161        try {
162            new PKIXBuilderParameters(taSet, null);
163            fail("ClassCastException expected");
164        } catch (ClassCastException e) {
165        }
166    }
167
168    /**
169     * Test #4 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
170     * constructor<br>
171     * Assertion: <code>NullPointerException</code> -
172     * if the <code>keystore</code> is <code>null</code>
173     */
174    public final void testPKIXBuilderParametersKeyStoreCertSelector04() throws Exception {
175        try {
176            // pass null
177            new PKIXBuilderParameters((KeyStore)null, null);
178            fail("NPE expected");
179        } catch (NullPointerException e) {
180        }
181    }
182
183}
184