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