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 18package org.apache.harmony.security.tests.java.security; 19 20import java.io.ByteArrayInputStream; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.io.UnsupportedEncodingException; 24import java.security.Certificate; 25import java.security.Identity; 26import java.security.IdentityScope; 27import java.security.KeyManagementException; 28import java.security.KeyPairGenerator; 29import java.security.Principal; 30import java.security.PublicKey; 31import java.security.cert.CertificateFactory; 32import java.security.cert.X509Certificate; 33 34import org.apache.harmony.security.tests.java.security.IdentityScope2Test.IdentityScopeSubclass; 35 36import dalvik.annotation.AndroidOnly; 37import dalvik.annotation.TestLevel; 38import dalvik.annotation.TestTargetClass; 39import dalvik.annotation.TestTargetNew; 40import dalvik.annotation.TestTargets; 41 42@SuppressWarnings("deprecation") 43@TestTargetClass(value=Identity.class, 44 untestedMethods={ 45 @TestTargetNew( 46 level=TestLevel.NOT_NECESSARY, 47 clazz=Certificate.class, 48 method="getGuarantor", 49 args={}, 50 notes="no implementation" 51 ), 52 @TestTargetNew( 53 level=TestLevel.NOT_NECESSARY, 54 clazz=Certificate.class, 55 method="encode", 56 args={OutputStream.class}, 57 notes="no implementation" 58 ), 59 @TestTargetNew( 60 level=TestLevel.NOT_NECESSARY, 61 clazz=Certificate.class, 62 method="decode", 63 args={InputStream.class}, 64 notes="no implementation" 65 ), 66 @TestTargetNew( 67 level=TestLevel.NOT_NECESSARY, 68 clazz=Certificate.class, 69 method="toString", 70 args={boolean.class}, 71 notes="no implementation" 72 ), 73 @TestTargetNew( 74 level=TestLevel.NOT_NECESSARY, 75 clazz=Certificate.class, 76 method="getFormat", 77 args={}, 78 notes="no implementation" 79 ), 80 @TestTargetNew( 81 level=TestLevel.NOT_NECESSARY, 82 clazz=Certificate.class, 83 method="getPrincipal", 84 args={}, 85 notes="no implementation" 86 ), 87 @TestTargetNew( 88 level=TestLevel.NOT_NECESSARY, 89 clazz=Certificate.class, 90 method="getPublicKey", 91 args={}, 92 notes="no implementation" 93 ) 94}) 95public class Identity2Test extends junit.framework.TestCase { 96 97 static PublicKey pubKey; 98 static { 99 try { 100 pubKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); 101 } catch (Exception e) { 102 fail(e.toString()); 103 } 104 } 105 106 public static class CertificateImpl implements java.security.Certificate { 107 108 X509Certificate cert; 109 110 public CertificateImpl(X509Certificate cert) { 111 this.cert = cert; 112 } 113 114 public Principal getGuarantor() { 115 return cert.getIssuerDN(); 116 } 117 118 public void encode(OutputStream out) { 119 } 120 121 public void decode(InputStream in) { 122 } 123 124 public String toString() { 125 return ""; 126 } 127 128 public String toString(boolean b) { 129 return ""; 130 } 131 132 public String getFormat() { 133 return cert.getType(); 134 } 135 136 public Principal getPrincipal() { 137 return cert.getSubjectDN(); 138 } 139 140 public PublicKey getPublicKey() { 141 return cert.getPublicKey(); 142 } 143 } 144 145 String certificate = "-----BEGIN CERTIFICATE-----\n" 146 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" 147 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" 148 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" 149 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" 150 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" 151 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" 152 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" 153 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" 154 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" 155 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" 156 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" 157 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" 158 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" 159 + "-----END CERTIFICATE-----\n"; 160 161 ByteArrayInputStream certArray = new ByteArrayInputStream(certificate 162 .getBytes()); 163 164 String certificate2 = "-----BEGIN CERTIFICATE-----\n" 165 + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n" 166 + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n" 167 + "A1BLSTEcMBoGA1UEAxMTRG9EIFBLSSBNZWQgUm9vdCBDQTAeFw05ODA4MDMyMjAy\n" 168 + "MjlaFw0wODA4MDQyMjAyMjlaMGExCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9VLlMu\n" 169 + "IEdvdmVybm1lbnQxDDAKBgNVBAsTA0RvRDEMMAoGA1UECxMDUEtJMRwwGgYDVQQD\n" 170 + "ExNEb0QgUEtJIE1lZCBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n" 171 + "gQDbrM/J9FrJSX+zxFUbsI9Vw5QbguVBIa95rwW/0M8+sM0r5gd+DY6iubm6wnXk\n" 172 + "CSvbfQlFEDSKr4WYeeGp+d9WlDnQdtDFLdA45tCi5SHjnW+hGAmZnld0rz6wQekF\n" 173 + "5xQaa5A6wjhMlLOjbh27zyscrorMJ1O5FBOWnEHcRv6xqQIDAQABoy8wLTAdBgNV\n" 174 + "HQ4EFgQUVrmYR6m9701cHQ3r5kXyG7zsCN0wDAYDVR0TBAUwAwEB/zANBgkqhkiG\n" 175 + "9w0BAQUFAAOBgQDVX1Y0YqC7vekeZjVxtyuC8Mnxbrz6D109AX07LEIRzNYzwZ0w\n" 176 + "MTImSp9sEzWW+3FueBIU7AxGys2O7X0qmN3zgszPfSiocBuQuXIYQctJhKjF5KVc\n" 177 + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n" 178 + "-----END CERTIFICATE-----\n"; 179 180 ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2 181 .getBytes()); 182 183 184 public static class IdentitySubclass extends Identity { 185 private static final long serialVersionUID = 1L; 186 187 public IdentitySubclass() { 188 super(); 189 } 190 191 public IdentitySubclass(String name) { 192 super(name); 193 } 194 195 public IdentitySubclass(String name, IdentityScope scope) 196 throws KeyManagementException { 197 super(name, scope); 198 } 199 } 200 201 /** 202 * @tests java.security.Identity#Identity() 203 */ 204 @TestTargetNew( 205 level = TestLevel.COMPLETE, 206 notes = "", 207 method = "Identity", 208 args = {} 209 ) 210 public void test_Constructor() { 211 new IdentitySubclass(); 212 } 213 214 /** 215 * @tests java.security.Identity#Identity(java.lang.String) 216 */ 217 @TestTargetNew( 218 level = TestLevel.COMPLETE, 219 notes = "", 220 method = "Identity", 221 args = {java.lang.String.class} 222 ) 223 public void test_ConstructorLjava_lang_String() { 224 String[] str = {"test", "", null}; 225 IdentitySubclass is; 226 for (int i = 0; i < str.length; i++) { 227 try { 228 is = new IdentitySubclass(str[i]); 229 assertNotNull(is); 230 assertTrue(is instanceof Identity); 231 } catch (Exception e) { 232 fail("Unexpected exception for Identity(java.lang.String) with parameter " + str[i]); 233 } 234 } 235 } 236 237 /** 238 * @tests java.security.Identity#Identity(java.lang.String, 239 * java.security.IdentityScope) 240 */ 241 @TestTargetNew( 242 level = TestLevel.COMPLETE, 243 notes = "", 244 method = "Identity", 245 args = {java.lang.String.class, java.security.IdentityScope.class} 246 ) 247 public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() { 248 String nameNull = null; 249 String[] str = {"test", "", "!@#$%^&*()", "identity name"}; 250 IdentityScopeSubclass iss = new IdentityScopeSubclass("name"); 251 IdentitySubclass is; 252 253 for (int i = 0; i < str.length; i++) { 254 try { 255 is = new IdentitySubclass(str[i], new IdentityScopeSubclass()); 256 assertNotNull(is); 257 assertTrue(is instanceof Identity); 258 } catch (Exception e) { 259 System.out.println(e); 260 fail("Unexpected exception for parameter " + str[i]); 261 } 262 } 263 264 try { 265 is = new IdentitySubclass(nameNull, new IdentityScopeSubclass()); 266 } catch (NullPointerException npe) { 267 } catch (Exception e) { 268 fail("Incorrect exception " + e + " was thrown"); 269 } 270 271 try { 272 is = new IdentitySubclass("test", iss); 273 is = new IdentitySubclass("test", iss); 274 fail("KeyManagementException was not thrown"); 275 } catch (KeyManagementException npe) { 276 //expected 277 } catch (Exception e) { 278 fail("Incorrect exception " + e + " was thrown instead of KeyManagementException"); 279 } 280 } 281 282 /** 283 * @tests java.security.Identity#getScope() 284 */ 285 @TestTargetNew( 286 level = TestLevel.COMPLETE, 287 notes = "", 288 method = "getScope", 289 args = {} 290 ) 291 public void test_getScope() throws Exception { 292 IdentityScope scope = new IdentityScopeSubclass(); 293 IdentitySubclass sub = new IdentitySubclass("test", scope); 294 IdentityScope returnedScope = sub.getScope(); 295 assertEquals("Wrong Scope returned", scope, returnedScope); 296 } 297 298 /** 299 * @tests java.security.Identity#getPublicKey() 300 */ 301 @TestTargetNew( 302 level = TestLevel.COMPLETE, 303 notes = "", 304 method = "getPublicKey", 305 args = {} 306 ) 307 public void test_getPublicKey() throws Exception { 308 IdentitySubclass sub = new IdentitySubclass("test", 309 new IdentityScopeSubclass()); 310 sub.setPublicKey(pubKey); 311 PublicKey returnedPubKey = sub.getPublicKey(); 312 assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey); 313 } 314 315 /** 316 * @tests java.security.Identity#getName() 317 */ 318 @TestTargetNew( 319 level = TestLevel.COMPLETE, 320 notes = "", 321 method = "getName", 322 args = {} 323 ) 324 public void test_getName() throws Exception { 325 String name = "test"; 326 IdentitySubclass sub = new IdentitySubclass(name, 327 new IdentityScopeSubclass()); 328 assertEquals("Wrong Name returned", name, sub.getName()); 329 } 330 331 /** 332 * @tests java.security.Identity#getInfo() 333 */ 334 @TestTargetNew( 335 level = TestLevel.COMPLETE, 336 notes = "", 337 method = "getInfo", 338 args = {} 339 ) 340 public void test_getInfo() throws Exception { 341 String info = "This is the general information."; 342 IdentitySubclass sub = new IdentitySubclass("test", 343 new IdentityScopeSubclass()); 344 sub.setInfo(info); 345 assertEquals("Wrong Info returned", info, sub.getInfo()); 346 } 347 348 /** 349 * @tests java.security.Identity#certificates() 350 */ 351 @TestTargetNew( 352 level = TestLevel.COMPLETE, 353 notes = "", 354 method = "certificates", 355 args = {} 356 ) 357 public void test_certificates() throws Exception { 358 IdentitySubclass sub = new IdentitySubclass("test", 359 new IdentityScopeSubclass()); 360 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 361 X509Certificate cert[] = new X509Certificate[1]; 362 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 363 sub.setPublicKey(cert[0].getPublicKey()); 364 CertificateImpl certImpl = new CertificateImpl(cert[0]); 365 sub.addCertificate(certImpl); 366 java.security.Certificate[] certs = sub.certificates(); 367 assertEquals("Certificate not contained in the identity", 368 certs[0], certImpl); 369 } 370 371 /** 372 * @tests java.security.Identity#removeCertificate(java.security.Certificate) 373 */ 374 @TestTargets({ 375 @TestTargetNew( 376 level = TestLevel.PARTIAL_COMPLETE, 377 method = "addCertificate", 378 args = {java.security.Certificate.class} 379 ), 380 @TestTargetNew( 381 level = TestLevel.PARTIAL_COMPLETE, 382 method = "removeCertificate", 383 args = {java.security.Certificate.class} 384 ) 385 }) 386 @AndroidOnly("Spec says: Removing unknown certificates throw an exception. " 387 + "The RI ignores unknown certificates.") 388 public void test_removeCertificateLjava_security_Certificate() throws Exception { 389 IdentitySubclass sub = new IdentitySubclass("test", 390 new IdentityScopeSubclass()); 391 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 392 X509Certificate cert[] = new X509Certificate[1]; 393 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 394 sub.setPublicKey(cert[0].getPublicKey()); 395 CertificateImpl certImpl = new CertificateImpl(cert[0]); 396 sub.addCertificate(certImpl); 397 398 try { 399 sub.removeCertificate(null); 400 fail("Test 1: KeyManagementException expected."); 401 } catch (KeyManagementException e) { 402 // Expected. 403 } 404 assertEquals("Test 2: Certificate should not have been removed.", 405 1, sub.certificates().length); 406 407 sub.removeCertificate(certImpl); 408 assertEquals("Test 3: Certificate has not been removed.", 409 0, sub.certificates().length); 410 411 // Removing the same certificate a second time should fail. 412 try { 413 sub.removeCertificate(certImpl); 414 fail("Test 4: KeyManagementException expected."); 415 } catch (KeyManagementException e) { 416 // Expected. 417 } 418 419 } 420 421 /** 422 * @tests java.security.Identity#equals(java.lang.Object) 423 */ 424 @TestTargetNew( 425 level = TestLevel.COMPLETE, 426 notes = "", 427 method = "equals", 428 args = {java.lang.Object.class} 429 ) 430 public void test_equalsLjava_lang_Object() throws Exception { 431 IdentitySubclass sub = new IdentitySubclass("test", 432 new IdentityScopeSubclass()); 433 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 434 X509Certificate cert[] = new X509Certificate[1]; 435 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 436 sub.setPublicKey(cert[0].getPublicKey()); 437 CertificateImpl certImpl = new CertificateImpl(cert[0]); 438 sub.addCertificate(certImpl); 439 IdentitySubclass sub2 = new IdentitySubclass("test", 440 new IdentityScopeSubclass()); 441 IdentitySubclass sub3 = new IdentitySubclass("identity name", 442 new IdentityScopeSubclass()); 443 assertEquals("the two Identity objects are not equal", sub2, sub); 444 boolean res1 = sub.equals(sub2); //true 445 if (!res1) fail("Method equals() should returned TRUE"); 446 res1 = sub.equals(sub3); //false 447 if (res1) fail("Method equals() should returned FALSE"); 448 } 449 450 /** 451 * @tests java.security.Identity#identityEquals(java.security.Identity) 452 */ 453 @TestTargetNew( 454 level = TestLevel.PARTIAL, 455 notes = "Method identityEquals(java.security.Identity) is not tested", 456 method = "identityEquals", 457 args = {java.security.Identity.class} 458 ) 459 public void test_identityEqualsLjava_security_Identity() throws Exception { 460 IdentitySubclass sub = new IdentitySubclass("test", null); 461 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 462 X509Certificate cert[] = new X509Certificate[1]; 463 cert[0] = (X509Certificate) cf.generateCertificate(certArray); 464 sub.setPublicKey(cert[0].getPublicKey()); 465 CertificateImpl certImpl = new CertificateImpl(cert[0]); 466 sub.addCertificate(certImpl); 467 IdentitySubclass sub2 = new IdentitySubclass("test", null); 468 sub2.setPublicKey(cert[0].getPublicKey()); 469 assertEquals("the two Identity objects are not identity-equal", 470 sub2, sub); 471 } 472 473 /** 474 * @tests java.security.Identity#toString() 475 */ 476 @TestTargetNew( 477 level = TestLevel.COMPLETE, 478 notes = "", 479 method = "toString", 480 args = {} 481 ) 482 public void test_toString() throws Exception { 483 IdentitySubclass sub = new IdentitySubclass("test", null); 484 assertNotNull(sub.toString()); 485 assertTrue("The String returned is not valid", sub.toString() 486 .length() > 0); 487 // Regression for HARMONY-1566 488 assertNotNull(new IdentitySubclass().toString()); 489 } 490 491 /** 492 * @tests java.security.Identity#toString(boolean) 493 */ 494 @TestTargetNew( 495 level = TestLevel.COMPLETE, 496 notes = "", 497 method = "toString", 498 args = {boolean.class} 499 ) 500 public void test_toStringZ() throws Exception { 501 IdentitySubclass sub = new IdentitySubclass("test", null); 502 assertNotNull(sub.toString(true)); 503 assertTrue("The String returned is not valid", sub.toString(true) 504 .length() > 0); 505 } 506 507 /** 508 * @tests java.security.Identity#hashCode() 509 */ 510 @TestTargetNew( 511 level = TestLevel.COMPLETE, 512 notes = "", 513 method = "hashCode", 514 args = {} 515 ) 516 public void test_hashCode() throws Exception { 517 IdentitySubclass sub = new IdentitySubclass("test", null); 518 IdentitySubclass sub2 = new IdentitySubclass("test", null); 519 assertEquals("The 2 hash codes are not equal", sub.hashCode(), sub2 520 .hashCode()); 521 } 522 523 /** 524 * @tests java.security.Identity#setInfo(String) 525 */ 526 @TestTargetNew( 527 level = TestLevel.COMPLETE, 528 notes = "", 529 method = "setInfo", 530 args = {java.lang.String.class} 531 ) 532 public void testSetInfo() throws Exception{ 533 String[] info = {"This is the general information.", "test", "", null}; 534 IdentitySubclass sub = new IdentitySubclass("test", new IdentityScopeSubclass()); 535 536 for (int i = 0; i < info.length; i++) { 537 try { 538 sub.setInfo(info[i]); 539 assertEquals("Wrong Info returned", info[i], sub.getInfo()); 540 } catch (Exception e) { 541 fail("Unexpected exception for parameter " + info[i]); 542 } 543 } 544 545 } 546 547 /** 548 * @tests java.security.Identity#setPublicKey(PublicKey key) 549 */ 550 @TestTargetNew( 551 level = TestLevel.PARTIAL, 552 notes = "SecurityException is not checked", 553 method = "setPublicKey", 554 args = {java.security.PublicKey.class} 555 ) 556 public void testSetPublicKey() throws Exception{ 557 IdentitySubclass sub = new IdentitySubclass("test", 558 new IdentityScopeSubclass()); 559 sub.setPublicKey(pubKey); 560 PublicKey returnedPubKey = sub.getPublicKey(); 561 assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey); 562 563 sub.setPublicKey(null); 564 assertEquals("Wrong PublicKey returned", null, sub.getPublicKey()); 565 } 566 567} 568