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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Vladimir N. Molotkov
23 package tests.security.cert;
25 import dalvik.annotation.TestTargets;
26 import dalvik.annotation.TestLevel;
27 import dalvik.annotation.TestTargetNew;
28 import dalvik.annotation.TestTargetClass;
30 import junit.framework.TestCase;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.PublicKey;
34 import java.security.cert.PKIXCertPathValidatorResult;
35 import java.security.cert.PolicyNode;
36 import java.security.cert.TrustAnchor;
37 import java.security.spec.InvalidKeySpecException;
39 import org.apache.harmony.security.tests.support.cert.TestUtils;
42 * Tests for <code>PKIXCertPathValidatorResult</code>
45 @TestTargetClass(PKIXCertPathValidatorResult.class)
46 public class PKIXCertPathValidatorResultTest extends TestCase {
50 private static PublicKey testPublicKey = new PublicKey() {
51 private static final long serialVersionUID = -737454523739489192L;
52 public String getAlgorithm() {
55 public String getFormat() {
58 public byte[] getEncoded() {
68 * Test #1 for <code>PKIXCertPathValidatorResult(TrustAnchor,
69 * PolicyNode, PublicKey)</code> constructor<br>
70 * Assertion: creates an instance of
71 * <code>PKIXCertPathValidatorResult</code>
73 * @throws NoSuchAlgorithmException
74 * @throws InvalidKeySpecException
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}
82 public final void testPKIXCertPathValidatorResult01()
83 throws InvalidKeySpecException,
84 NoSuchAlgorithmException {
85 TrustAnchor ta = TestUtils.getTrustAnchor();
87 fail(getName() + ": not performed (could not create test TrustAnchor)");
89 new PKIXCertPathValidatorResult(
91 TestUtils.getPolicyTree(),
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>
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}
107 public final void testPKIXCertPathValidatorResult02() {
110 new PKIXCertPathValidatorResult(
112 TestUtils.getPolicyTree(),
114 fail("NPE expected");
115 } catch (NullPointerException e) {
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>
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}
131 public final void testPKIXCertPathValidatorResult03() {
132 TrustAnchor ta = TestUtils.getTrustAnchor();
134 fail(getName() + ": not performed (could not create test TrustAnchor)");
138 new PKIXCertPathValidatorResult(
140 TestUtils.getPolicyTree(),
142 fail("NPE expected");
143 } catch (NullPointerException e) {
148 * Test #4 for <code>PKIXCertPathValidatorResult(TrustAnchor,
149 * PolicyNode, PublicKey)</code> constructor<br>
150 * Assertion: <code>PolicyNode</code>can be <code>null</code>
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}
158 public final void testPKIXCertPathValidatorResult04() throws Exception {
159 TrustAnchor ta = TestUtils.getTrustAnchor();
161 fail(getName() + ": not performed (could not create test TrustAnchor)");
164 new PKIXCertPathValidatorResult(
171 * Test for <code>getTrustAnchor()</code> method<br>
172 * Assertion: returns <code>TrustAnchor</code> (never <code>null</code>)
173 * @throws NoSuchAlgorithmException
174 * @throws InvalidKeySpecException
177 level = TestLevel.COMPLETE,
179 method = "getTrustAnchor",
182 public final void testGetTrustAnchor() throws Exception {
183 TrustAnchor ta = TestUtils.getTrustAnchor();
185 fail(getName() + ": not performed (could not create test TrustAnchor)");
188 PKIXCertPathValidatorResult vr =
189 new PKIXCertPathValidatorResult(
194 // must return the same reference passed
195 // as a parameter to the constructor
196 assertSame(ta, vr.getTrustAnchor());
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
206 level = TestLevel.COMPLETE,
208 method = "getPublicKey",
211 public final void testGetPublicKey() throws Exception {
212 TrustAnchor ta = TestUtils.getTrustAnchor();
214 fail(getName() + ": not performed (could not create test TrustAnchor)");
217 PublicKey pk = testPublicKey;
218 PKIXCertPathValidatorResult vr =
219 new PKIXCertPathValidatorResult(
224 // must return the same reference passed
225 // as a parameter to the constructor
226 assertSame(pk, vr.getPublicKey());
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
234 * @throws NoSuchAlgorithmException
235 * @throws InvalidKeySpecException
238 level = TestLevel.PARTIAL_COMPLETE,
239 notes = "Verifies that getPolicyTree method returns the root node of the valid policy tree.",
240 method = "getPolicyTree",
243 public final void testGetPolicyTree01() throws Exception {
244 TrustAnchor ta = TestUtils.getTrustAnchor();
246 fail(getName() + ": not performed (could not create test TrustAnchor)");
249 // valid policy tree case;
250 PolicyNode pn = TestUtils.getPolicyTree();
251 PKIXCertPathValidatorResult vr =
252 new PKIXCertPathValidatorResult(
257 // must return the same reference passed
258 // as a parameter to the constructor
259 assertSame(pn, vr.getPolicyTree());
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
267 * @throws NoSuchAlgorithmException
268 * @throws InvalidKeySpecException
271 level = TestLevel.PARTIAL_COMPLETE,
272 notes = "Verifies that getPolicyTree method returns null if there are no valid policies.",
273 method = "getPolicyTree",
276 public final void testGetPolicyTree02() throws Exception {
277 TrustAnchor ta = TestUtils.getTrustAnchor();
279 fail(getName() + ": not performed (could not create test TrustAnchor)");
282 // no valid policy tree case (null)
283 PKIXCertPathValidatorResult vr =
284 new PKIXCertPathValidatorResult(
289 // must return the same reference passed
290 // as a parameter to the constructor
291 assertNull(vr.getPolicyTree());
295 * Test for <code>clone()</code> method<br>
296 * Assertion: returns a copy of this object
297 * @throws NoSuchAlgorithmException
298 * @throws InvalidKeySpecException
301 level = TestLevel.COMPLETE,
306 public final void testClone() throws Exception {
307 TrustAnchor ta = TestUtils.getTrustAnchor();
309 fail(getName() + ": not performed (could not create test TrustAnchor)");
312 PKIXCertPathValidatorResult vr1 =
313 new PKIXCertPathValidatorResult(
315 TestUtils.getPolicyTree(),
318 PKIXCertPathValidatorResult vr2 =
319 (PKIXCertPathValidatorResult) vr1.clone();
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());
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
333 assertSame(my.getPolicyTree(), myClone.getPolicyTree());
334 assertSame(my.getPublicKey(), myClone.getPublicKey());
335 assertSame(my.getTrustAnchor(), myClone.getTrustAnchor());
336 assertSame(my.enc, myClone.enc);
339 class MyPKIXCertPathBuilderResult extends PKIXCertPathValidatorResult {
341 public byte[] enc; // byte array is cloneable
343 public MyPKIXCertPathBuilderResult(TrustAnchor trustAnchor,
344 PolicyNode policyTree, PublicKey subjectPublicKey, byte[] enc) {
345 super(trustAnchor, policyTree, subjectPublicKey);
352 * Test #1 for <code>toString()</code> method<br>
353 * Assertion: Returns a formatted string describing this object
354 * @throws NoSuchAlgorithmException
355 * @throws InvalidKeySpecException
358 level = TestLevel.COMPLETE,
363 public final void testToString01() throws Exception {
364 TrustAnchor ta = TestUtils.getTrustAnchor();
366 fail(getName() + ": not performed (could not create test TrustAnchor)");
369 PKIXCertPathValidatorResult vr =
370 new PKIXCertPathValidatorResult(
372 TestUtils.getPolicyTree(),
375 assertNotNull(vr.toString());
379 * Test #2 for <code>toString()</code> method<br>
380 * Assertion: Returns a formatted string describing this object
381 * @throws NoSuchAlgorithmException
382 * @throws InvalidKeySpecException
385 level = TestLevel.COMPLETE,
390 public final void testToString02() throws Exception {
391 TrustAnchor ta = TestUtils.getTrustAnchor();
393 fail(getName() + ": not performed (could not create test TrustAnchor)");
396 PKIXCertPathValidatorResult vr =
397 new PKIXCertPathValidatorResult(
402 assertNotNull(vr.toString());