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