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 Alexander Y. Kleymenov 20* @version $Revision$ 21*/ 22 23package tests.security.cert; 24 25import dalvik.annotation.AndroidOnly; 26import dalvik.annotation.TestTargets; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetNew; 29import dalvik.annotation.TestTargetClass; 30 31import junit.framework.Test; 32import junit.framework.TestCase; 33import junit.framework.TestSuite; 34 35import java.io.ByteArrayInputStream; 36import java.math.BigInteger; 37import java.security.InvalidKeyException; 38import java.security.NoSuchAlgorithmException; 39import java.security.NoSuchProviderException; 40import java.security.Principal; 41import java.security.PublicKey; 42import java.security.SignatureException; 43import java.security.cert.CRLException; 44import java.security.cert.Certificate; 45import java.security.cert.CertificateFactory; 46import java.security.cert.X509CRL; 47import java.security.cert.X509CRLEntry; 48import java.security.cert.X509Certificate; 49import java.util.Date; 50import java.util.Set; 51 52import javax.security.auth.x500.X500Principal; 53 54import org.apache.harmony.security.tests.support.cert.TestUtils; 55 56import tests.security.cert.X509CRL2Test.MyX509CRL; 57/** 58 */ 59@TestTargetClass(X509CRL.class) 60public class X509CRLTest extends TestCase { 61 62 private X509CRL tbt_crl; 63 64 String certificate = "-----BEGIN CERTIFICATE-----\n" 65 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" 66 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" 67 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" 68 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" 69 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" 70 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" 71 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" 72 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" 73 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" 74 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" 75 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" 76 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" 77 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" 78 + "-----END CERTIFICATE-----\n"; 79 80 ByteArrayInputStream certArray = new ByteArrayInputStream(certificate 81 .getBytes()); 82 83 /** 84 * The stub class used for testing of non abstract methods. 85 */ 86 private class TBTCRL extends X509CRL { 87 public String toString() { 88 return null; 89 } 90 91 public boolean isRevoked(Certificate cert) { 92 return true; 93 } 94 95 public Set<String> getNonCriticalExtensionOIDs() { 96 return null; 97 } 98 99 public Set<String> getCriticalExtensionOIDs() { 100 return null; 101 } 102 103 public byte[] getExtensionValue(String oid) { 104 return null; 105 } 106 107 public boolean hasUnsupportedCriticalExtension() { 108 return false; 109 } 110 111 public byte[] getEncoded() { 112 return null; 113 } 114 115 public void verify(PublicKey key) 116 throws CRLException, NoSuchAlgorithmException, 117 InvalidKeyException, NoSuchProviderException, 118 SignatureException 119 { 120 } 121 122 public void verify(PublicKey key, String sigProvider) 123 throws CRLException, NoSuchAlgorithmException, 124 InvalidKeyException, NoSuchProviderException, 125 SignatureException 126 { 127 } 128 129 public int getVersion() { 130 return 2; 131 } 132 133 public Principal getIssuerDN() { 134 return null; 135 } 136 137 public Date getThisUpdate() { 138 return null; 139 } 140 141 public Date getNextUpdate() { 142 return null; 143 } 144 145 public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) { 146 return null; 147 } 148 149 public Set<X509CRLEntry> getRevokedCertificates() { 150 return null; 151 } 152 153 public byte[] getTBSCertList() { 154 return null; 155 } 156 157 public byte[] getSignature() { 158 return null; 159 } 160 161 public String getSigAlgName() { 162 return null; 163 } 164 165 public String getSigAlgOID() { 166 return null; 167 } 168 169 public byte[] getSigAlgParams() { 170 return null; 171 } 172 } 173 174 175 public X509CRLTest() { 176 177 } 178 179 public void setUp() { 180 tbt_crl = new TBTCRL() { 181 public byte[] getEncoded() { 182 return new byte[] {1, 2, 3}; 183 } 184 }; 185 } 186 187 /** 188 * getType() method testing. Tests that getType() method returns 189 * the value "X.509" 190 */ 191 @TestTargetNew( 192 level = TestLevel.COMPLETE, 193 notes = "", 194 method = "getType", 195 args = {} 196 ) 197 public void testGetType() { 198 assertEquals("The type of X509CRL should be X.509", 199 tbt_crl.getType(), "X.509"); 200 } 201 202 /** 203 * equals(Object other) method testing. Tests the correctness of equal 204 * operation: it should be reflexive, symmetric, transitive, consistent 205 * and should be false on null object. 206 */ 207 @TestTargetNew( 208 level = TestLevel.COMPLETE, 209 notes = "", 210 method = "equals", 211 args = {java.lang.Object.class} 212 ) 213 public void testEquals() { 214 TBTCRL tbt_crl_1 = new TBTCRL() { 215 public byte[] getEncoded() { 216 return new byte[] {1, 2, 3}; 217 } 218 }; 219 220 TBTCRL tbt_crl_2 = new TBTCRL() { 221 public byte[] getEncoded() { 222 return new byte[] {1, 2, 3}; 223 } 224 }; 225 226 TBTCRL tbt_crl_3 = new TBTCRL() { 227 public byte[] getEncoded() { 228 return new byte[] {3, 2, 1}; 229 } 230 }; 231 232 // checking for reflexive law: 233 assertTrue("The equivalence relation should be reflexive.", 234 tbt_crl.equals(tbt_crl)); 235 236 assertEquals("The CRLs with equal encoded form should be equal", 237 tbt_crl, tbt_crl_1); 238 // checking for symmetric law: 239 assertTrue("The equivalence relation should be symmetric.", 240 tbt_crl_1.equals(tbt_crl)); 241 242 assertEquals("The CRLs with equal encoded form should be equal", 243 tbt_crl_1, tbt_crl_2); 244 // checking for transitive law: 245 assertTrue("The equivalence relation should be transitive.", 246 tbt_crl.equals(tbt_crl_2)); 247 248 assertFalse("Should not be equal to null object.", 249 tbt_crl.equals(null)); 250 251 assertFalse("The CRLs with differing encoded form should not be equal", 252 tbt_crl.equals(tbt_crl_3)); 253 assertFalse("The CRL should not be equals to the object which is not " 254 + "an instance of X509CRL", tbt_crl.equals(new Object())); 255 } 256 257 /** 258 * hashCode() method testing. Tests that for equal objects hash codes 259 * are equal. 260 */ 261 @TestTargetNew( 262 level = TestLevel.COMPLETE, 263 notes = "", 264 method = "hashCode", 265 args = {} 266 ) 267 public void testHashCode() { 268 TBTCRL tbt_crl_1 = new TBTCRL() { 269 public byte[] getEncoded() { 270 return new byte[] {1, 2, 3}; 271 } 272 }; 273 assertTrue("Equal objects should have the same hash codes.", 274 tbt_crl.hashCode() == tbt_crl_1.hashCode()); 275 } 276 277 /** 278 * @tests java.security.cert.X509CRL#getIssuerX500Principal() 279 */ 280 @TestTargetNew( 281 level = TestLevel.COMPLETE, 282 notes = "", 283 method = "getIssuerX500Principal", 284 args = {} 285 ) 286 public void testGetIssuerX500Principal() { 287 // return valid encoding 288 TBTCRL crl = new TBTCRL() { 289 public byte[] getEncoded() { 290 return TestUtils.getX509CRL_v1(); 291 } 292 }; 293 294 assertEquals(new X500Principal("CN=Z"), crl.getIssuerX500Principal()); 295 } 296 297 /** 298 * getRevokedCertificate(X509Certificate certificate) method testing. 299 * Check if the default implementation throws NullPointerException 300 * on null input data. 301 */ 302 @TestTargetNew( 303 level = TestLevel.COMPLETE, 304 notes = "", 305 method = "getRevokedCertificate", 306 args = {java.security.cert.X509Certificate.class} 307 ) 308 @AndroidOnly("Test filed on RI: getRevokedCertificate throws " + 309 "RuntimeException.") 310 public void testGetRevokedCertificate() { 311 try { 312 tbt_crl.getRevokedCertificate((X509Certificate) null); 313 fail("NullPointerException should be thrown " 314 + "in the case of null input data."); 315 } catch (NullPointerException e) { 316 } 317 318 319 try { 320 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 321 X509Certificate cert = (X509Certificate) cf.generateCertificate(certArray); 322 tbt_crl.getRevokedCertificate(cert); 323 } catch (Exception e) { 324 fail("Unexpected exception: " + e); 325 } 326 } 327 328 @TestTargets({ 329 @TestTargetNew( 330 level = TestLevel.COMPLETE, 331 notes = "", 332 method = "getEncoded", 333 args = {} 334 ), 335 @TestTargetNew( 336 level = TestLevel.COMPLETE, 337 notes = "", 338 method = "getIssuerDN", 339 args = {} 340 ), 341 @TestTargetNew( 342 level = TestLevel.COMPLETE, 343 notes = "", 344 method = "getNextUpdate", 345 args = {} 346 ), 347 @TestTargetNew( 348 level = TestLevel.COMPLETE, 349 notes = "", 350 method = "getRevokedCertificate", 351 args = {java.math.BigInteger.class} 352 ), 353 @TestTargetNew( 354 level = TestLevel.COMPLETE, 355 notes = "", 356 method = "getRevokedCertificates", 357 args = {} 358 ), 359 @TestTargetNew( 360 level = TestLevel.COMPLETE, 361 notes = "", 362 method = "getSigAlgName", 363 args = {} 364 ), 365 @TestTargetNew( 366 level = TestLevel.COMPLETE, 367 notes = "", 368 method = "getSigAlgOID", 369 args = {} 370 ), 371 @TestTargetNew( 372 level = TestLevel.COMPLETE, 373 notes = "", 374 method = "getSigAlgParams", 375 args = {} 376 ), 377 @TestTargetNew( 378 level = TestLevel.COMPLETE, 379 notes = "", 380 method = "getSignature", 381 args = {} 382 ), 383 @TestTargetNew( 384 level = TestLevel.COMPLETE, 385 notes = "", 386 method = "getTBSCertList", 387 args = {} 388 ), 389 @TestTargetNew( 390 level = TestLevel.COMPLETE, 391 notes = "", 392 method = "getThisUpdate", 393 args = {} 394 ), 395 @TestTargetNew( 396 level = TestLevel.COMPLETE, 397 notes = "", 398 method = "getVersion", 399 args = {} 400 ), 401 @TestTargetNew( 402 level = TestLevel.COMPLETE, 403 notes = "", 404 method = "verify", 405 args = {java.security.PublicKey.class} 406 ), 407 @TestTargetNew( 408 level = TestLevel.COMPLETE, 409 notes = "", 410 method = "verify", 411 args = {java.security.PublicKey.class, java.lang.String.class} 412 ) 413 }) 414 public void testAbstractMethods() { 415 TBTCRL crl = new TBTCRL() { 416 public byte[] getEncoded() { 417 return TestUtils.getX509CRL_v1(); 418 } 419 }; 420 421 try { 422 crl.getEncoded(); 423 crl.getIssuerDN(); 424 crl.getNextUpdate(); 425 crl.getRevokedCertificate(BigInteger.ONE); 426 crl.getRevokedCertificates(); 427 crl.getSigAlgName(); 428 crl.getSigAlgOID(); 429 crl.getSigAlgParams(); 430 crl.getSignature(); 431 crl.getTBSCertList(); 432 crl.getThisUpdate(); 433 crl.getVersion(); 434 435 crl.verify(null); 436 crl.verify(null, "test"); 437 } catch (Exception e) { 438 fail("Unexpected exception for constructor"); 439 } 440 } 441 442 @TestTargetNew( 443 level = TestLevel.COMPLETE, 444 notes = "", 445 method = "X509CRL", 446 args = {} 447 ) 448 public void testX509CRL() { 449 try { 450 TBTCRL crl = new TBTCRL(); 451 assertTrue(crl instanceof X509CRL); 452 } catch (Exception e) { 453 fail("Unexpected exception for constructor"); 454 } 455 } 456 457 public static Test suite() { 458 return new TestSuite(X509CRLTest.class); 459 } 460 461 public static void main(String[] args) { 462 junit.textui.TestRunner.run(suite()); 463 } 464} 465 466