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