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.KnownFailure; 26import dalvik.annotation.TestLevel; 27import dalvik.annotation.TestTargetClass; 28import dalvik.annotation.TestTargetNew; 29import dalvik.annotation.TestTargets; 30 31import junit.framework.TestCase; 32 33import org.apache.harmony.security.tests.support.cert.MyCertPath; 34import org.apache.harmony.security.tests.support.cert.MyFailingCertPath; 35import org.apache.harmony.security.tests.support.cert.TestUtils; 36import org.apache.harmony.testframework.serialization.SerializationTest; 37import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 38 39import java.io.ObjectStreamException; 40import java.io.Serializable; 41import java.security.cert.CertPath; 42import java.security.cert.CertificateEncodingException; 43import java.util.Arrays; 44 45/** 46 * Tests for <code>CertPath</code> fields and methods 47 * 48 */ 49@TestTargetClass(CertPath.class) 50public class CertPathTest extends TestCase { 51 /** 52 * Meaningless cert path encoding just for testing purposes 53 */ 54 private static final byte[] testEncoding = new byte[] { 55 (byte)1, (byte)2, (byte)3, (byte)4, (byte)5 56 }; 57 58 private static final byte[] testEncoding1 = new byte[] { 59 (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6 60 }; 61 62 // 63 // Tests 64 // 65 66 /** 67 * Test for <code>CertPath(String type)</code> method<br> 68 * Assertion: returns hash of the <code>Certificate</code> instance 69 */ 70 @TestTargetNew( 71 level = TestLevel.COMPLETE, 72 notes = "", 73 method = "CertPath", 74 args = {java.lang.String.class} 75 ) 76 public final void testCertPath() { 77 try { 78 CertPath cp1 = new MyCertPath(testEncoding); 79 assertEquals("MyEncoding", cp1.getType()); 80 assertTrue(Arrays.equals(testEncoding, cp1.getEncoded())); 81 } catch (CertificateEncodingException e) { 82 fail("Unexpected CertificateEncodingException " + e.getMessage()); 83 } 84 85 try { 86 CertPath cp1 = new MyCertPath(null); 87 } catch (Exception e) { 88 fail("Unexpected exception " + e.getMessage()); 89 } 90 } 91 92 /** 93 * Test for <code>hashCode()</code> method<br> 94 * Assertion: returns hash of the <code>Certificate</code> instance 95 */ 96 @TestTargetNew( 97 level = TestLevel.PARTIAL_COMPLETE, 98 notes = "", 99 method = "hashCode", 100 args = {} 101 ) 102 public final void testHashCode() { 103 CertPath cp1 = new MyCertPath(testEncoding); 104 CertPath cp2 = new MyCertPath(testEncoding); 105 CertPath cp3 = new MyCertPath(testEncoding1); 106 107 assertTrue(cp1.hashCode() == cp2.hashCode()); 108 assertTrue(cp1.hashCode() != cp3.hashCode()); 109 } 110 111 /** 112 * Test for <code>hashCode()</code> method<br> 113 * Assertion: hash code of equal objects should be the same 114 */ 115 @TestTargetNew( 116 level = TestLevel.PARTIAL_COMPLETE, 117 notes = "", 118 method = "hashCode", 119 args = {} 120 ) 121 public final void testHashCodeEqualsObject() { 122 CertPath cp1 = new MyCertPath(testEncoding); 123 CertPath cp2 = new MyCertPath(testEncoding); 124 assertTrue((cp1.hashCode() == cp2.hashCode()) && cp1.equals(cp2)); 125 } 126 127 /** 128 * Test for <code>getType()</code> method<br> 129 * Assertion: returns cert path type 130 */ 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 notes = "", 134 method = "getType", 135 args = {} 136 ) 137 public final void testGetType() { 138 assertEquals("MyEncoding", new MyCertPath(testEncoding).getType()); 139 } 140 141 /** 142 * Test #1 for <code>equals(Object)</code> method<br> 143 * Assertion: object equals to itself 144 */ 145 @TestTargetNew( 146 level = TestLevel.PARTIAL_COMPLETE, 147 notes = "Verifies that object equals to itself.", 148 method = "equals", 149 args = {java.lang.Object.class} 150 ) 151 public final void testEqualsObject01() { 152 CertPath cp1 = new MyCertPath(testEncoding); 153 assertTrue(cp1.equals(cp1)); 154 } 155 156 /** 157 * Test for <code>equals(Object)</code> method<br> 158 * Assertion: object equals to other <code>CertPath</code> 159 * instance with the same state 160 */ 161 @TestTargetNew( 162 level = TestLevel.PARTIAL_COMPLETE, 163 notes = "Verifies that CertPath object equals to other CertPath with the same state.", 164 method = "equals", 165 args = {java.lang.Object.class} 166 ) 167 public final void testEqualsObject02() { 168 CertPath cp1 = new MyCertPath(testEncoding); 169 CertPath cp2 = new MyCertPath(testEncoding); 170 assertTrue(cp1.equals(cp2) && cp2.equals(cp1)); 171 } 172 173 /** 174 * Test for <code>equals(Object)</code> method<br> 175 * Assertion: object not equals to <code>null</code> 176 */ 177 @TestTargetNew( 178 level = TestLevel.PARTIAL_COMPLETE, 179 notes = "Verifies null as a parameter.", 180 method = "equals", 181 args = {java.lang.Object.class} 182 ) 183 public final void testEqualsObject03() { 184 CertPath cp1 = new MyCertPath(testEncoding); 185 assertFalse(cp1.equals(null)); 186 } 187 188 /** 189 * Test for <code>equals(Object)</code> method<br> 190 * Assertion: object not equals to other which is not 191 * instance of <code>CertPath</code> 192 */ 193 @TestTargetNew( 194 level = TestLevel.PARTIAL_COMPLETE, 195 notes = "Verifies non equal objects.", 196 method = "equals", 197 args = {java.lang.Object.class} 198 ) 199 public final void testEqualsObject04() { 200 CertPath cp1 = new MyCertPath(testEncoding); 201 assertFalse(cp1.equals("MyEncoding")); 202 } 203 204 /** 205 * Test for <code>toString()</code> method<br> 206 * Assertion: returns string representation of 207 * <code>CertPath</code> object 208 */ 209 @TestTargetNew( 210 level = TestLevel.COMPLETE, 211 notes = "", 212 method = "toString", 213 args = {} 214 ) 215 public final void testToString() { 216 CertPath cp1 = new MyCertPath(testEncoding); 217 assertNotNull(cp1.toString()); 218 } 219 220 // 221 // the following tests just call methods 222 // that are abstract in <code>CertPath</code> 223 // (So they just like signature tests) 224 // 225 226 /** 227 * This test just calls <code>getCertificates()</code> method<br> 228 */ 229 @TestTargetNew( 230 level = TestLevel.COMPLETE, 231 notes = "Abstract method.", 232 method = "getCertificates", 233 args = {} 234 ) 235 public final void testGetCertificates() { 236 CertPath cp1 = new MyCertPath(testEncoding); 237 cp1.getCertificates(); 238 } 239 240 /** 241 * This test just calls <code>getEncoded()</code> method<br> 242 * 243 * @throws CertificateEncodingException 244 */ 245 @TestTargetNew( 246 level = TestLevel.COMPLETE, 247 notes = "Abstract method.", 248 method = "getEncoded", 249 args = {} 250 ) 251 public final void testGetEncoded() throws CertificateEncodingException { 252 CertPath cp1 = new MyCertPath(testEncoding); 253 cp1.getEncoded(); 254 } 255 256 /** 257 * This test just calls <code>getEncoded(String)</code> method<br> 258 * 259 * @throws CertificateEncodingException 260 */ 261 @TestTargetNew( 262 level = TestLevel.COMPLETE, 263 notes = "Abstract method.", 264 method = "getEncoded", 265 args = {java.lang.String.class} 266 ) 267 public final void testGetEncodedString() throws CertificateEncodingException { 268 CertPath cp1 = new MyCertPath(testEncoding); 269 cp1.getEncoded("MyEncoding"); 270 } 271 272 /** 273 * This test just calls <code>getEncodings()</code> method<br> 274 */ 275 @TestTargetNew( 276 level = TestLevel.COMPLETE, 277 notes = "Abstract method.", 278 method = "getEncodings", 279 args = {} 280 ) 281 public final void testGetEncodings() { 282 CertPath cp1 = new MyCertPath(testEncoding); 283 cp1.getEncodings(); 284 } 285 286 /** 287 * This test just calls <code>writeReplace()</code> method<br> 288 */ 289 @TestTargetNew( 290 level = TestLevel.PARTIAL_COMPLETE, 291 notes = "Doesn't verify ObjectStreamException.", 292 method = "writeReplace", 293 args = {} 294 ) 295 public final void testWriteReplace() { 296 try { 297 MyCertPath cp1 = new MyCertPath(testEncoding); 298 Object obj = cp1.writeReplace(); 299 assertTrue(obj.toString().contains( 300 "java.security.cert.CertPath$CertPathRep")); 301 } catch (ObjectStreamException e) { 302 fail("Unexpected ObjectStreamException " + e.getMessage()); 303 } 304 } 305 306 @TestTargetNew( 307 level = TestLevel.PARTIAL_COMPLETE, 308 notes = "verifies ObjectStreamException.", 309 method = "writeReplace", 310 args = {} 311 ) 312 public final void testWriteReplace_ObjectStreamException() { 313 try { 314 MyFailingCertPath cp = new MyFailingCertPath(testEncoding); 315 Object obj = cp.writeReplace(); 316 fail("expected ObjectStreamException"); 317 } catch (ObjectStreamException e) { 318 // ok 319 } 320 } 321 322 /** 323 * @tests serialization/deserialization compatibility. 324 */ 325 @TestTargets({ 326 @TestTargetNew( 327 level = TestLevel.COMPLETE, 328 notes = "Verifies serialization/deserialization compatibility. And tests default constructor", 329 method = "!SerializationSelf", 330 args = {} 331 ), 332 @TestTargetNew( 333 level = TestLevel.PARTIAL_COMPLETE, 334 notes = "", 335 method = "writeReplace", 336 args = {} 337 ), 338 @TestTargetNew( 339 level = TestLevel.PARTIAL_COMPLETE, 340 notes = "", 341 method = "CertPath.CertPathRep.readResolve", 342 args = {} 343 ) 344 }) 345 // Test passed on RI 346 @KnownFailure(value="expired certificate bug 2322662") 347 public void testSerializationSelf() throws Exception { 348 TestUtils.initCertPathSSCertChain(); 349 CertPath certPath = TestUtils.buildCertPathSSCertChain(); 350 351 SerializationTest.verifySelf(certPath); 352 } 353 354 /** 355 * @tests serialization/deserialization compatibility with RI. 356 */ 357 @TestTargets({ 358 @TestTargetNew( 359 level = TestLevel.COMPLETE, 360 notes = "Verifies serialization/deserialization compatibility.", 361 method = "!SerializationGolden", 362 args = {} 363 ), 364 @TestTargetNew( 365 level = TestLevel.PARTIAL_COMPLETE, 366 notes = "", 367 method = "writeReplace", 368 args = {} 369 ), 370 @TestTargetNew( 371 level = TestLevel.PARTIAL_COMPLETE, 372 notes = "", 373 method = "CertPath.CertPathRep.readResolve", 374 args = {} 375 ) 376 }) 377 // Test passed on RI 378 @KnownFailure(value="expired certificate bug 2322662") 379 public void testSerializationCompatibility() throws Exception { 380 TestUtils.initCertPathSSCertChain(); 381 CertPath certPath = TestUtils.buildCertPathSSCertChain(); 382 383 SerializationTest.verifyGolden(this, certPath); 384 } 385} 386