PKIXCertPathBuilderResultTest.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.NoSuchAlgorithmException;
25import java.security.PublicKey;
26import java.security.cert.CertPath;
27import java.security.cert.CertPathBuilderResult;
28import java.security.cert.PKIXCertPathBuilderResult;
29import java.security.cert.TrustAnchor;
30import java.security.spec.InvalidKeySpecException;
31
32import org.apache.harmony.security.tests.support.cert.MyCertPath;
33import org.apache.harmony.security.tests.support.cert.TestUtils;
34
35import junit.framework.TestCase;
36
37/**
38 * Tests for <code>PKIXCertPathBuilderResult</code>
39 *
40 */
41public class PKIXCertPathBuilderResultTest extends TestCase {
42    /**
43     * Cert path encoding stub
44     */
45    private static final byte[] testEncoding = new byte[] {
46            (byte)1, (byte)2, (byte)3, (byte)4, (byte)5
47    };
48
49    /**
50     * PublicKey stub
51     */
52    private static PublicKey testPublicKey = new PublicKey() {
53        public String getAlgorithm() {
54            return "NeverMind";
55        }
56        public String getFormat() {
57            return "NeverMind";
58        }
59        public byte[] getEncoded() {
60            return new byte[] {};
61        }
62    };
63
64
65    /**
66     * Constructor for PKIXCertPathBuilderResultTest.
67     * @param name
68     */
69    public PKIXCertPathBuilderResultTest(String name) {
70        super(name);
71    }
72
73    //
74    // Tests
75    //
76
77    /**
78     * Test #1 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
79     *   PolicyNode, PublicKey)</code> constructor<br>
80     * Assertion: Creates an instance of <code>PKIXCertPathBuilderResult</code>
81     * @throws NoSuchAlgorithmException
82     * @throws InvalidKeySpecException
83     */
84    public final void testPKIXCertPathBuilderResult01()
85        throws InvalidKeySpecException,
86               NoSuchAlgorithmException {
87        TrustAnchor ta = TestUtils.getTrustAnchor();
88        if (ta == null) {
89            fail(getName() + ": not performed (could not create test TrustAnchor)");
90        }
91        CertPathBuilderResult r =
92            new PKIXCertPathBuilderResult(
93                    new MyCertPath(testEncoding),
94                    ta,
95                    TestUtils.getPolicyTree(),
96                    testPublicKey);
97        assertTrue(r instanceof PKIXCertPathBuilderResult);
98    }
99
100    /**
101     * Test #2 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
102     *   PolicyNode, PublicKey)</code> constructor<br>
103     * Assertion: policy tree parameter may be <code>null</code>
104     * @throws NoSuchAlgorithmException
105     * @throws InvalidKeySpecException
106     */
107    public final void testPKIXCertPathBuilderResult02()
108        throws InvalidKeySpecException,
109               NoSuchAlgorithmException {
110        TrustAnchor ta = TestUtils.getTrustAnchor();
111        if (ta == null) {
112            fail(getName() + ": not performed (could not create test TrustAnchor)");
113        }
114        CertPathBuilderResult r =
115            new PKIXCertPathBuilderResult(
116                    new MyCertPath(testEncoding),
117                    ta,
118                    null,
119                    testPublicKey);
120        assertTrue(r instanceof PKIXCertPathBuilderResult);
121    }
122
123    /**
124     * Test #3 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
125     *   PolicyNode, PublicKey)</code> constructor<br>
126     * Assertion: <code>NullPointerException</code>
127     * if certPath is <code>null</code>
128     */
129    public final void testPKIXCertPathBuilderResult03() {
130        TrustAnchor ta = TestUtils.getTrustAnchor();
131        if (ta == null) {
132            fail(getName() + ": not performed (could not create test TrustAnchor)");
133        }
134
135        try {
136            // pass null
137            new PKIXCertPathBuilderResult(
138                    null,
139                    ta,
140                    TestUtils.getPolicyTree(),
141                    testPublicKey);
142            fail("NPE expected");
143        } catch (NullPointerException e) {
144        }
145    }
146
147    /**
148     * Test #4 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
149     *   PolicyNode, PublicKey)</code> constructor<br>
150     * Assertion: <code>NullPointerException</code>
151     * if trustAnchor is <code>null</code>
152     */
153    public final void testPKIXCertPathBuilderResult04() {
154        try {
155            // pass null
156            new PKIXCertPathBuilderResult(
157                    new MyCertPath(testEncoding),
158                    null,
159                    TestUtils.getPolicyTree(),
160                    testPublicKey);
161            fail("NPE expected");
162        } catch (NullPointerException e) {
163        }
164    }
165
166    /**
167     * Test #5 for <code>PKIXCertPathBuilderResult(CertPath, TrustAnchor,
168     *   PolicyNode, PublicKey)</code> constructor<br>
169     * Assertion: <code>NullPointerException</code>
170     * if publicKey is <code>null</code>
171     */
172    public final void testPKIXCertPathBuilderResult05() {
173        TrustAnchor ta = TestUtils.getTrustAnchor();
174        if (ta == null) {
175            fail(getName() + ": not performed (could not create test TrustAnchor)");
176        }
177
178        try {
179            // pass null
180            new PKIXCertPathBuilderResult(
181                    new MyCertPath(testEncoding),
182                    ta,
183                    TestUtils.getPolicyTree(),
184                    null);
185            fail("NPE expected");
186        } catch (NullPointerException e) {
187        }
188    }
189
190    public final void test_clone() {
191
192        // Regression for HARMONY-2786.
193        TrustAnchor ta = TestUtils.getTrustAnchor();
194        assertNotNull(getName()
195                + ": not performed (could not create test TrustAnchor)", ta);
196
197        PKIXCertPathBuilderResult init = new PKIXCertPathBuilderResult(
198                new MyCertPath(testEncoding), ta, TestUtils.getPolicyTree(),
199                testPublicKey);
200
201        PKIXCertPathBuilderResult clone = (PKIXCertPathBuilderResult) init
202                .clone();
203        assertSame(init.getCertPath(), clone.getCertPath());
204        assertSame(init.getPolicyTree(), clone.getPolicyTree());
205        assertSame(init.getPublicKey(), clone.getPublicKey());
206        assertSame(init.getTrustAnchor(), clone.getTrustAnchor());
207    }
208
209    /**
210     * Test for <code>getCertPath()</code> method<br>
211     * Assertion: the built and validated <code>CertPath</code>
212     * (never <code>null</code>)
213     * @throws NoSuchAlgorithmException
214     * @throws InvalidKeySpecException
215     */
216    public final void testGetCertPath() throws Exception {
217        TrustAnchor ta = TestUtils.getTrustAnchor();
218        if (ta == null) {
219            fail(getName() + ": not performed (could not create test TrustAnchor)");
220        }
221
222        CertPath cp = new MyCertPath(testEncoding);
223        CertPathBuilderResult r =
224            new PKIXCertPathBuilderResult(
225                    cp,
226                    ta,
227                    TestUtils.getPolicyTree(),
228                    testPublicKey);
229
230        // must return the same reference
231        // as passed to the constructor
232        assertSame(cp, r.getCertPath());
233    }
234
235    /**
236     * Test for <code>toString()</code> method<br>
237     * Assertion: the printable representation of this object
238     * @throws NoSuchAlgorithmException
239     * @throws InvalidKeySpecException
240     */
241    public final void testToString()
242        throws InvalidKeySpecException,
243               NoSuchAlgorithmException {
244        TrustAnchor ta = TestUtils.getTrustAnchor();
245        if (ta == null) {
246            fail(getName() + ": not performed (could not create test TrustAnchor)");
247        }
248        CertPathBuilderResult r =
249            new PKIXCertPathBuilderResult(
250                    new MyCertPath(testEncoding),
251                    ta,
252                    TestUtils.getPolicyTree(),
253                    testPublicKey);
254
255        assertNotNull(r.toString());
256    }
257
258}
259