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