PKIXCertPathValidatorResultTest.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.TestTargets;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetNew;
28import dalvik.annotation.TestTargetClass;
29
30import junit.framework.TestCase;
31
32import java.security.NoSuchAlgorithmException;
33import java.security.PublicKey;
34import java.security.cert.PKIXCertPathValidatorResult;
35import java.security.cert.PolicyNode;
36import java.security.cert.TrustAnchor;
37import java.security.spec.InvalidKeySpecException;
38
39import org.apache.harmony.security.tests.support.cert.TestUtils;
40
41/**
42 * Tests for <code>PKIXCertPathValidatorResult</code>
43 *
44 */
45@TestTargetClass(PKIXCertPathValidatorResult.class)
46public class PKIXCertPathValidatorResultTest extends TestCase {
47    /**
48     * PublicKey stub
49     */
50    private static PublicKey testPublicKey = new PublicKey() {
51        private static final long serialVersionUID = -737454523739489192L;
52        public String getAlgorithm() {
53            return "NeverMind";
54        }
55        public String getFormat() {
56            return "NeverMind";
57        }
58        public byte[] getEncoded() {
59            return new byte[] {};
60        }
61    };
62
63    //
64    // Tests
65    //
66
67    /**
68     * Test #1 for <code>PKIXCertPathValidatorResult(TrustAnchor,
69     * PolicyNode, PublicKey)</code> constructor<br>
70     * Assertion: creates an instance of
71     * <code>PKIXCertPathValidatorResult</code>
72     *
73     * @throws NoSuchAlgorithmException
74     * @throws InvalidKeySpecException
75     */
76    @TestTargetNew(
77        level = TestLevel.PARTIAL_COMPLETE,
78        notes = "Doesn't verify NullPointerException.",
79        method = "PKIXCertPathValidatorResult",
80        args = {java.security.cert.TrustAnchor.class, java.security.cert.PolicyNode.class, java.security.PublicKey.class}
81    )
82    public final void testPKIXCertPathValidatorResult01()
83        throws InvalidKeySpecException,
84               NoSuchAlgorithmException {
85        TrustAnchor ta = TestUtils.getTrustAnchor();
86        if (ta == null) {
87            fail(getName() + ": not performed (could not create test TrustAnchor)");
88        }
89        new PKIXCertPathValidatorResult(
90                ta,
91                TestUtils.getPolicyTree(),
92                testPublicKey);
93    }
94
95    /**
96     * Test #2 for <code>PKIXCertPathValidatorResult(TrustAnchor,
97     * PolicyNode, PublicKey)</code> constructor<br>
98     * Assertion: <code>NullPointerException</code> if
99     * <code>TrustAnchor</code> parameter is <code>null</code>
100     */
101    @TestTargetNew(
102        level = TestLevel.PARTIAL_COMPLETE,
103        notes = "Verifies NullPointerException.",
104        method = "PKIXCertPathValidatorResult",
105        args = {java.security.cert.TrustAnchor.class, java.security.cert.PolicyNode.class, java.security.PublicKey.class}
106    )
107    public final void testPKIXCertPathValidatorResult02() {
108        try {
109            // pass null
110            new PKIXCertPathValidatorResult(
111                    null,
112                    TestUtils.getPolicyTree(),
113                    testPublicKey);
114            fail("NPE expected");
115        } catch (NullPointerException e) {
116        }
117    }
118
119    /**
120     * Test #3 for <code>PKIXCertPathValidatorResult(TrustAnchor,
121     * PolicyNode, PublicKey)</code> constructor<br>
122     * Assertion: <code>NullPointerException</code> if
123     * <code>PublicKey</code> parameter is <code>null</code>
124     */
125    @TestTargetNew(
126        level = TestLevel.PARTIAL_COMPLETE,
127        notes = "Verifies NullPointerException.",
128        method = "PKIXCertPathValidatorResult",
129        args = {java.security.cert.TrustAnchor.class, java.security.cert.PolicyNode.class, java.security.PublicKey.class}
130    )
131    public final void testPKIXCertPathValidatorResult03() {
132        TrustAnchor ta = TestUtils.getTrustAnchor();
133        if (ta == null) {
134            fail(getName() + ": not performed (could not create test TrustAnchor)");
135        }
136        try {
137            // pass null
138            new PKIXCertPathValidatorResult(
139                    ta,
140                    TestUtils.getPolicyTree(),
141                    null);
142            fail("NPE expected");
143        } catch (NullPointerException e) {
144        }
145    }
146
147    /**
148     * Test #4 for <code>PKIXCertPathValidatorResult(TrustAnchor,
149     * PolicyNode, PublicKey)</code> constructor<br>
150     * Assertion: <code>PolicyNode</code>can be <code>null</code>
151     */
152    @TestTargetNew(
153        level = TestLevel.PARTIAL_COMPLETE,
154        notes = "Verifies null as a parameter.",
155        method = "PKIXCertPathValidatorResult",
156        args = {java.security.cert.TrustAnchor.class, java.security.cert.PolicyNode.class, java.security.PublicKey.class}
157    )
158    public final void testPKIXCertPathValidatorResult04() throws Exception {
159        TrustAnchor ta = TestUtils.getTrustAnchor();
160        if (ta == null) {
161            fail(getName() + ": not performed (could not create test TrustAnchor)");
162        }
163
164        new PKIXCertPathValidatorResult(
165                ta,
166                null,
167                testPublicKey);
168    }
169
170    /**
171     * Test for <code>getTrustAnchor()</code> method<br>
172     * Assertion: returns <code>TrustAnchor</code> (never <code>null</code>)
173     * @throws NoSuchAlgorithmException
174     * @throws InvalidKeySpecException
175     */
176    @TestTargetNew(
177        level = TestLevel.COMPLETE,
178        notes = "",
179        method = "getTrustAnchor",
180        args = {}
181    )
182    public final void testGetTrustAnchor() throws Exception {
183        TrustAnchor ta = TestUtils.getTrustAnchor();
184        if (ta == null) {
185            fail(getName() + ": not performed (could not create test TrustAnchor)");
186        }
187
188        PKIXCertPathValidatorResult vr =
189            new PKIXCertPathValidatorResult(
190                    ta,
191                    null,
192                    testPublicKey);
193
194        // must return the same reference passed
195        // as a parameter to the constructor
196        assertSame(ta, vr.getTrustAnchor());
197    }
198
199    /**
200     * Test for <code>getPublicKey()</code> method<br>
201     * Assertion: returns the subject's public key (never <code>null</code>)
202     * @throws NoSuchAlgorithmException
203     * @throws InvalidKeySpecException
204     */
205    @TestTargetNew(
206        level = TestLevel.COMPLETE,
207        notes = "",
208        method = "getPublicKey",
209        args = {}
210    )
211    public final void testGetPublicKey() throws Exception {
212        TrustAnchor ta = TestUtils.getTrustAnchor();
213        if (ta == null) {
214            fail(getName() + ": not performed (could not create test TrustAnchor)");
215        }
216
217        PublicKey pk = testPublicKey;
218        PKIXCertPathValidatorResult vr =
219            new PKIXCertPathValidatorResult(
220                    ta,
221                    null,
222                    pk);
223
224        // must return the same reference passed
225        // as a parameter to the constructor
226        assertSame(pk, vr.getPublicKey());
227    }
228
229    /**
230     * Test for <code>getPolicyTree()</code> method<br>
231     * Assertion: returns the root node of the valid
232     * policy tree or <code>null</code> if there are
233     * no valid policies
234     * @throws NoSuchAlgorithmException
235     * @throws InvalidKeySpecException
236     */
237    @TestTargetNew(
238        level = TestLevel.PARTIAL_COMPLETE,
239        notes = "Verifies that getPolicyTree method returns the root node of the valid policy tree.",
240        method = "getPolicyTree",
241        args = {}
242    )
243    public final void testGetPolicyTree01() throws Exception {
244        TrustAnchor ta = TestUtils.getTrustAnchor();
245        if (ta == null) {
246            fail(getName() + ": not performed (could not create test TrustAnchor)");
247        }
248
249        // valid policy tree case;
250        PolicyNode pn = TestUtils.getPolicyTree();
251        PKIXCertPathValidatorResult vr =
252            new PKIXCertPathValidatorResult(
253                    ta,
254                    pn,
255                    testPublicKey);
256
257        // must return the same reference passed
258        // as a parameter to the constructor
259        assertSame(pn, vr.getPolicyTree());
260    }
261
262    /**
263     * Test for <code>getPolicyTree()</code> method<br>
264     * Assertion: returns the root node of the valid
265     * policy tree or <code>null</code> if there are
266     * no valid policies
267     * @throws NoSuchAlgorithmException
268     * @throws InvalidKeySpecException
269     */
270    @TestTargetNew(
271        level = TestLevel.PARTIAL_COMPLETE,
272        notes = "Verifies that getPolicyTree method returns null if there are no valid policies.",
273        method = "getPolicyTree",
274        args = {}
275    )
276    public final void testGetPolicyTree02() throws Exception {
277        TrustAnchor ta = TestUtils.getTrustAnchor();
278        if (ta == null) {
279            fail(getName() + ": not performed (could not create test TrustAnchor)");
280        }
281
282        // no valid policy tree case (null)
283        PKIXCertPathValidatorResult vr =
284            new PKIXCertPathValidatorResult(
285                    ta,
286                    null,
287                    testPublicKey);
288
289        // must return the same reference passed
290        // as a parameter to the constructor
291        assertNull(vr.getPolicyTree());
292    }
293
294    /**
295     * Test for <code>clone()</code> method<br>
296     * Assertion: returns a copy of this object
297     * @throws NoSuchAlgorithmException
298     * @throws InvalidKeySpecException
299     */
300    @TestTargetNew(
301        level = TestLevel.COMPLETE,
302        notes = "",
303        method = "clone",
304        args = {}
305    )
306    public final void testClone() throws Exception {
307        TrustAnchor ta = TestUtils.getTrustAnchor();
308        if (ta == null) {
309            fail(getName() + ": not performed (could not create test TrustAnchor)");
310        }
311
312        PKIXCertPathValidatorResult vr1 =
313            new PKIXCertPathValidatorResult(
314                    ta,
315                    TestUtils.getPolicyTree(),
316                    testPublicKey);
317
318        PKIXCertPathValidatorResult vr2 =
319            (PKIXCertPathValidatorResult) vr1.clone();
320
321        // check that method makes shallow copy
322        assertNotSame("notSame", vr1, vr2);
323        assertSame("trustAncor", vr1.getTrustAnchor(), vr2.getTrustAnchor());
324        assertSame("policyTree", vr1.getPolicyTree(), vr2.getPolicyTree());
325        assertSame("publicKey", vr1.getPublicKey(), vr2.getPublicKey());
326
327        // Regression for HARMONY-2786.
328        byte[] encoding = { 0x01 };
329        MyPKIXCertPathBuilderResult my = new MyPKIXCertPathBuilderResult(ta,
330                TestUtils.getPolicyTree(), testPublicKey, encoding);
331        MyPKIXCertPathBuilderResult myClone = (MyPKIXCertPathBuilderResult) my
332                .clone();
333        assertSame(my.getPolicyTree(), myClone.getPolicyTree());
334        assertSame(my.getPublicKey(), myClone.getPublicKey());
335        assertSame(my.getTrustAnchor(), myClone.getTrustAnchor());
336        assertSame(my.enc, myClone.enc);
337    }
338
339    class MyPKIXCertPathBuilderResult extends PKIXCertPathValidatorResult {
340
341        public byte[] enc; // byte array is cloneable
342
343        public MyPKIXCertPathBuilderResult(TrustAnchor trustAnchor,
344                PolicyNode policyTree, PublicKey subjectPublicKey, byte[] enc) {
345            super(trustAnchor, policyTree, subjectPublicKey);
346
347            this.enc = enc;
348        }
349    }
350
351    /**
352     * Test #1 for <code>toString()</code> method<br>
353     * Assertion: Returns a formatted string describing this object
354     * @throws NoSuchAlgorithmException
355     * @throws InvalidKeySpecException
356     */
357    @TestTargetNew(
358        level = TestLevel.COMPLETE,
359        notes = "",
360        method = "toString",
361        args = {}
362    )
363    public final void testToString01() throws Exception {
364        TrustAnchor ta = TestUtils.getTrustAnchor();
365        if (ta == null) {
366            fail(getName() + ": not performed (could not create test TrustAnchor)");
367        }
368
369        PKIXCertPathValidatorResult vr =
370            new PKIXCertPathValidatorResult(
371                    ta,
372                    TestUtils.getPolicyTree(),
373                    testPublicKey);
374
375        assertNotNull(vr.toString());
376    }
377
378    /**
379     * Test #2 for <code>toString()</code> method<br>
380     * Assertion: Returns a formatted string describing this object
381     * @throws NoSuchAlgorithmException
382     * @throws InvalidKeySpecException
383     */
384    @TestTargetNew(
385        level = TestLevel.COMPLETE,
386        notes = "",
387        method = "toString",
388        args = {}
389    )
390    public final void testToString02() throws Exception {
391        TrustAnchor ta = TestUtils.getTrustAnchor();
392        if (ta == null) {
393            fail(getName() + ": not performed (could not create test TrustAnchor)");
394        }
395
396        PKIXCertPathValidatorResult vr =
397            new PKIXCertPathValidatorResult(
398                    ta,
399                    null,
400                    testPublicKey);
401
402        assertNotNull(vr.toString());
403    }
404
405}
406