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 Aleksei Y. Semenov 20* @version $Revision$ 21*/ 22 23package tests.java.security; 24 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargets; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetNew; 29 30import java.security.Identity; 31import java.security.IdentityScope; 32import java.security.KeyManagementException; 33import java.security.Permission; 34import java.security.Permissions; 35import java.security.PublicKey; 36import java.security.SecurityPermission; 37 38import org.apache.harmony.security.tests.support.CertificateStub; 39import org.apache.harmony.security.tests.support.IdentityStub; 40import org.apache.harmony.security.tests.support.PublicKeyStub; 41 42import junit.framework.TestCase; 43 44/** 45 * Tests for class Identity 46 * 47 */ 48@SuppressWarnings("deprecation") 49@TestTargetClass(Identity.class) 50public class IdentityTest extends TestCase { 51 52 public static class MySecurityManager extends SecurityManager { 53 public Permissions denied = new Permissions(); 54 public void checkPermission(Permission permission){ 55 if (denied!=null && denied.implies(permission)) throw new SecurityException(); 56 } 57 } 58 59 public static void main(String[] args) { 60 junit.textui.TestRunner.run(IdentityTest.class); 61 } 62 63 @TestTargetNew( 64 level = TestLevel.PARTIAL, 65 notes = "Method's returned variable is not checked", 66 method = "hashCode", 67 args = {} 68 ) 69 public void testHashCode() { 70 new IdentityStub("testHashCode").hashCode(); 71 } 72 73 @TestTargetNew( 74 level = TestLevel.COMPLETE, 75 notes = "", 76 method = "equals", 77 args = {java.lang.Object.class} 78 ) 79 public void testEquals() throws Exception { 80 IdentityStub i1 = new IdentityStub("testEquals"); 81 Object value[] = { 82 null, Boolean.FALSE, 83 new Object(), Boolean.FALSE, 84 i1, Boolean.TRUE, 85 new IdentityStub(i1.getName()), Boolean.TRUE 86 }; 87 88 for (int k=0; k<value.length; k+=2) { 89 assertEquals(value[k+1], new Boolean(i1.equals(value[k]))); 90 if (Boolean.TRUE.equals(value[k+1])) assertEquals(i1.hashCode(), value[k].hashCode()); 91 } 92 // check other cases 93 Identity i2 = new IdentityStub("testEquals", IdentityScope.getSystemScope()); 94 assertEquals(i1.identityEquals(i2), i1.equals(i2)); 95 Identity i3 = new IdentityStub("testEquals3"); 96 assertEquals(i1.identityEquals(i3), i1.equals(i3)); 97 98 } 99 100 /** 101 * verify Identity.toString() throws Exception is permission is denied 102 */ 103 @TestTargetNew( 104 level = TestLevel.PARTIAL_COMPLETE, 105 notes = "", 106 method = "toString", 107 args = {} 108 ) 109 public void testToString1() { 110 MySecurityManager sm = new MySecurityManager(); 111 sm.denied.add(new SecurityPermission("printIdentity")); 112 System.setSecurityManager(sm); 113 try { 114 new IdentityStub("testToString").toString(); 115 fail("SecurityException should be thrown"); 116 } catch (SecurityException ok) { 117 } finally { 118 System.setSecurityManager(null); 119 } 120 } 121 /** 122 * verify Identity.toString() 123 */ 124 @TestTargetNew( 125 level = TestLevel.PARTIAL_COMPLETE, 126 notes = "", 127 method = "toString", 128 args = {} 129 ) 130 public void testToString2() { 131 assertNotNull(new IdentityStub("testToString2").toString()); 132 } 133 134 /** 135 * verify Identity() creates instance 136 */ 137 @TestTargetNew( 138 level = TestLevel.COMPLETE, 139 notes = "", 140 method = "Identity", 141 args = {} 142 ) 143 public void testIdentity() { 144 assertNotNull(new IdentityStub()); 145 } 146 147 /* 148 * verify Identity(String) creates instance with given name 149 */ 150 @TestTargetNew( 151 level = TestLevel.PARTIAL, 152 notes = "Empty string for parameter is not tested", 153 method = "Identity", 154 args = {java.lang.String.class} 155 ) 156 public void testIdentityString() { 157 Identity i = new IdentityStub("iii"); 158 assertNotNull(i); 159 assertEquals("iii", i.getName()); 160 i=new IdentityStub(null); 161 assertNotNull(i); 162 assertNull(i.getName()); 163 } 164 165 /** 166 * verify Identity(String, IdentityScope) creates instance with given name and in give scope 167 */ 168 @TestTargetNew( 169 level = TestLevel.PARTIAL, 170 notes = "KeyManagementException checking missed. Null parameters are not checked.", 171 method = "Identity", 172 args = {java.lang.String.class, java.security.IdentityScope.class} 173 ) 174 public void testIdentityStringIdentityScope() throws Exception { 175 IdentityScope s = IdentityScope.getSystemScope(); 176 Identity i = new IdentityStub("iii2", s); 177 assertNotNull(i); 178 assertEquals("iii2", i.getName()); 179 assertSame(s, i.getScope()); 180 assertSame(i, s.getIdentity(i.getName())); 181 } 182 183 /** 184 * verify addCertificate(Certificate certificate) adds a certificate for this identity. 185 * If the identity has a public key, the public key in the certificate must be the same 186 * 187 */ 188 @TestTargetNew( 189 level = TestLevel.PARTIAL_COMPLETE, 190 notes = "", 191 method = "addCertificate", 192 args = {java.security.Certificate.class} 193 ) 194 public void testAddCertificate1() throws Exception { 195 Identity i = new IdentityStub("iii"); 196 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5}); 197 i.setPublicKey(pk1); 198 // try with the same key 199 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 200 i.addCertificate(c1); 201 assertSame(c1, i.certificates()[0]); 202 // try Certificate with different key 203 try { 204 i.addCertificate(new CertificateStub("ccc", null, null, new PublicKeyStub("k2", "fff", new byte[]{6,7,8,9,0}))); 205 fail("KeyManagementException should be thrown"); 206 } catch (KeyManagementException ok) {} 207 } 208 209 /** 210 * verify addCertificate(Certificate certificate) adds a certificate for this identity. 211 * if the identity does not have a public key, the identity's public key is set to be that specified in the certificate. 212 */ 213 @TestTargetNew( 214 level = TestLevel.PARTIAL_COMPLETE, 215 notes = "", 216 method = "addCertificate", 217 args = {java.security.Certificate.class} 218 ) 219 public void testAddCertificate2() throws Exception { 220 Identity i = new IdentityStub("iii"); 221 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 222 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 223 i.addCertificate(c1); 224 assertSame(c1, i.certificates()[0]); 225 assertSame(pk1, i.getPublicKey()); 226 227 } 228 229 /** 230 * verify addCertificate(Certificate certificate) throws SecurityException is permission is denied 231 */ 232 @TestTargetNew( 233 level = TestLevel.PARTIAL_COMPLETE, 234 notes = "", 235 method = "addCertificate", 236 args = {java.security.Certificate.class} 237 ) 238 public void testAddCertificate3() throws Exception { 239 MySecurityManager sm = new MySecurityManager(); 240 sm.denied.add(new SecurityPermission("addIdentityCertificate")); 241 System.setSecurityManager(sm); 242 try { 243 new IdentityStub("iii").addCertificate(new CertificateStub("ccc", null, null, null)); 244 fail("SecurityException should be thrown"); 245 } catch (SecurityException ok) { 246 } finally { 247 System.setSecurityManager(null); 248 } 249 } 250 251 /** 252 * verify addCertificate(Certificate certificate) throws KeyManagementException if certificate is null 253 */ 254 @TestTargetNew( 255 level = TestLevel.PARTIAL_COMPLETE, 256 notes = "", 257 method = "addCertificate", 258 args = {java.security.Certificate.class} 259 ) 260 public void testAddCertificate4() throws Exception { 261 try { 262 new IdentityStub("aaa").addCertificate(null); 263 fail("KeyManagementException should be thrown"); 264 } catch (KeyManagementException ok) { 265 } catch (NullPointerException ok) {} 266 267 } 268// 269// Commented out since there will no be fix for the test failure 270// /** 271// * verify removeCertificate(Certificate certificate) removes certificate 272// */ 273// public void testRemoveCertificate1() throws Exception{ 274// Identity i = new IdentityStub("iii"); 275// PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 276// CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 277// i.addCertificate(c1); 278// assertSame(c1, i.certificates()[0]); 279// i.removeCertificate(c1); 280// assertEquals(0, i.certificates().length); 281// // throw KeyManagementException if certificate not found 282// try { 283// i.removeCertificate(c1); 284// fail("KeyManagementException should be thrown"); 285// } catch (KeyManagementException ok) { 286// } 287// try { 288// i.removeCertificate(null); 289// fail("KeyManagementException should be thrown"); 290// } catch (KeyManagementException ok) { 291// 292// } 293// } 294 /** 295 * verify removeCertificate(Certificate certificate) throws SecurityException if permission is denied 296 */ 297 @TestTargetNew( 298 level = TestLevel.PARTIAL_COMPLETE, 299 notes = "Checks SecurityException.", 300 method = "removeCertificate", 301 args = {java.security.Certificate.class} 302 ) 303 public void testRemoveCertificate2() throws Exception{ 304 MySecurityManager sm = new MySecurityManager(); 305 sm.denied.add(new SecurityPermission("removeIdentityCertificate")); 306 Identity i = new IdentityStub("iii"); 307 i.addCertificate(new CertificateStub("ccc", null, null, null)); 308 System.setSecurityManager(sm); 309 try { 310 i.removeCertificate(i.certificates()[0]); 311 fail("SecurityException should be thrown"); 312 } catch (SecurityException ok) { 313 } finally { 314 System.setSecurityManager(null); 315 } 316 317 } 318 319 /** 320 * verify certificates() returns a copy of all certificates for this identity 321 */ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "", 325 method = "certificates", 326 args = {} 327 ) 328 public void testCertificates() throws Exception { 329 Identity i = new IdentityStub("iii"); 330 PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); 331 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 332 CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); 333 i.addCertificate(c1); 334 i.addCertificate(c2); 335 java.security.Certificate[] s = i.certificates(); 336 assertEquals(2, s.length); 337 assertTrue(c1.equals(s[0]) || c2.equals(s[0])); 338 assertTrue(c1.equals(s[1]) || c2.equals(s[1])); 339 s[0] = null; 340 s[1] = null; 341 // check that the copy was modified 342 s = i.certificates(); 343 assertEquals(2, s.length); 344 assertTrue(c1.equals(s[0]) || c2.equals(s[0])); 345 assertTrue(c1.equals(s[1]) || c2.equals(s[1])); 346 } 347 348 /** 349 * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal 350 */ 351 @TestTargetNew( 352 level = TestLevel.COMPLETE, 353 notes = "", 354 method = "identityEquals", 355 args = {java.security.Identity.class} 356 ) 357 public void testIdentityEquals() throws Exception { 358 String name = "nnn"; 359 PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[]{1,2,3,4,5}); 360 IdentityStub i = new IdentityStub(name); 361 i.setPublicKey(pk); 362 Object[] value = { 363 //null, Boolean.FALSE, 364 //new Object(), Boolean.FALSE, 365 new IdentityStub("111"), Boolean.FALSE, 366 new IdentityStub(name), Boolean.FALSE, 367 new IdentityStub(name, IdentityScope.getSystemScope()), Boolean.FALSE, 368 i, Boolean.TRUE, 369 new IdentityStub(name, pk), Boolean.TRUE 370 }; 371 for (int k=0; k<value.length; k+=2){ 372 assertEquals(value[k+1], new Boolean(i.identityEquals((Identity)value[k]))); 373 if (Boolean.TRUE.equals(value[k+1])) assertEquals(i.hashCode(), value[k].hashCode()); 374 } 375 Identity i2 = IdentityScope.getSystemScope().getIdentity(name); 376 i2.setPublicKey(pk); 377 assertTrue(i.identityEquals(i2)); 378 } 379 380 /** 381 * verify Identity.toString(boolean) return string representation of identity 382 */ 383 @TestTargetNew( 384 level = TestLevel.PARTIAL, 385 notes = "Method's returned value is not checked. SecurityException checking missed.", 386 method = "toString", 387 args = {boolean.class} 388 ) 389 public void testToStringboolean() throws Exception { 390 new IdentityStub("aaa").toString(false); 391 new IdentityStub("aaa2", IdentityScope.getSystemScope()).toString(false); 392 new IdentityStub("bbb").toString(true); 393 new IdentityStub("bbb2", IdentityScope.getSystemScope()).toString(true); 394 } 395 396 /** 397 * verify Identity.getScope() returns identity's scope 398 */ 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 notes = "", 402 method = "getScope", 403 args = {} 404 ) 405 public void testGetScope() throws Exception { 406 Identity i = new IdentityStub("testGetScope"); 407 assertNull(i.getScope()); 408 IdentityScope s = IdentityScope.getSystemScope(); 409 410 Identity i2 = new IdentityStub("testGetScope2", s); 411 assertSame(s, i2.getScope()); 412 413 } 414 /** 415 * 416 * verify Identity.setPublicKey() throws SecurityException if permission is denied 417 */ 418 @TestTargetNew( 419 level = TestLevel.PARTIAL_COMPLETE, 420 notes = "", 421 method = "setPublicKey", 422 args = {java.security.PublicKey.class} 423 ) 424 public void testSetPublicKey1() throws Exception { 425 MySecurityManager sm = new MySecurityManager(); 426 sm.denied.add(new SecurityPermission("setIdentityPublicKey")); 427 System.setSecurityManager(sm); 428 try { 429 new IdentityStub("testSetPublicKey1").setPublicKey(new PublicKeyStub("kkk", "testSetPublicKey1", null)); 430 fail("SecurityException should be thrown"); 431 } catch (SecurityException ok) { 432 } finally { 433 System.setSecurityManager(null); 434 } 435 436 } 437 /** 438 * 439 * verify Identity.setPublicKey() throws KeyManagementException if key is invalid 440 * 441 */ 442 @TestTargetNew( 443 level = TestLevel.PARTIAL_COMPLETE, 444 notes = "", 445 method = "setPublicKey", 446 args = {java.security.PublicKey.class} 447 ) 448 public void testSetPublicKey2() throws Exception { 449 Identity i2 = new IdentityStub("testSetPublicKey2_2", IdentityScope.getSystemScope()); 450 new PublicKeyStub("kkk", "testSetPublicKey2", new byte[]{1,2,3,4,5}); 451 try { 452 i2.setPublicKey(null); 453 //fail("KeyManagementException should be thrown - key is null"); 454 } catch (KeyManagementException ok) {} 455 } 456 457// 458// Commented out since there will no be fix for the test failure 459// /** 460// * 461// * verify Identity.setPublicKey() throws KeyManagementException if key is already used 462// * 463// */ 464// public void testSetPublicKey3() throws Exception { 465// Identity i1 = new IdentityStub("testSetPublicKey3_1", IdentityScope.getSystemScope()); 466// Identity i2 = new IdentityStub("testSetPublicKey3_2", IdentityScope.getSystemScope()); 467// PublicKey pk = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5}); 468// i1.setPublicKey(pk); 469// try { 470// i2.setPublicKey(pk); 471// fail("KeyManagementException should be thrown - key already used"); 472// } catch (KeyManagementException ok) {}; 473// } 474 /** 475 * 476 * verify Identity.setPublicKey() removes old key and all identity's certificates 477 * 478 */ 479 @TestTargetNew( 480 level = TestLevel.PARTIAL_COMPLETE, 481 notes = "", 482 method = "setPublicKey", 483 args = {java.security.PublicKey.class} 484 ) 485 public void testSetPublicKey4() throws Exception { 486 Identity i = new IdentityStub("testSetPublicKey4"); 487 PublicKeyStub pk1 = new PublicKeyStub("kkk", "Identity.testSetPublicKey4", null); 488 CertificateStub c1 = new CertificateStub("fff", null, null, pk1); 489 CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); 490 i.addCertificate(c1); 491 i.addCertificate(c2); 492 assertEquals(2, i.certificates().length); 493 assertSame(pk1, i.getPublicKey()); 494 495 PublicKeyStub pk2 = new PublicKeyStub("zzz", "Identity.testSetPublicKey4", null); 496 i.setPublicKey(pk2); 497 assertSame(pk2, i.getPublicKey()); 498 assertEquals(0, i.certificates().length); 499 } 500 501 /** 502 * verify Identity.getPublicKey() returns public key 503 */ 504 @TestTargetNew( 505 level = TestLevel.COMPLETE, 506 notes = "", 507 method = "getPublicKey", 508 args = {} 509 ) 510 public void testGetPublicKey() throws Exception { 511 Identity i = new IdentityStub("testGetPublicKey"); 512 assertNull(i.getPublicKey()); 513 PublicKey pk = new PublicKeyStub("kkk", "Identity.testGetPublicKey", null); 514 i.setPublicKey(pk); 515 assertSame(pk, i.getPublicKey()); 516 } 517 518 /** 519 * 520 * verify Identity.setInfo() throws SecurityException if permission is denied 521 * 522 * 523 */ 524 @TestTargetNew( 525 level = TestLevel.PARTIAL_COMPLETE, 526 notes = "Just SecurityException verification", 527 method = "setInfo", 528 args = {java.lang.String.class} 529 ) 530 public void testSetInfo() throws Exception { 531 MySecurityManager sm = new MySecurityManager(); 532 sm.denied.add(new SecurityPermission("setIdentityInfo")); 533 System.setSecurityManager(sm); 534 try { 535 new IdentityStub("testSetInfo").setInfo("some info"); 536 fail("SecurityException should be thrown"); 537 } catch (SecurityException ok) { 538 } finally { 539 System.setSecurityManager(null); 540 } 541 } 542 543 @TestTargets({ 544 @TestTargetNew( 545 level = TestLevel.PARTIAL_COMPLETE, 546 notes = "Both method were verified", 547 method = "getInfo", 548 args = {} 549 ), 550 @TestTargetNew( 551 level = TestLevel.PARTIAL_COMPLETE, 552 notes = "Both method were verified", 553 method = "setInfo", 554 args = {java.lang.String.class} 555 ) 556 }) 557 public void testGetInfo() { 558 559 Identity i = new IdentityStub("testGetInfo"); 560 i.setInfo("some info"); 561 assertEquals("some info", i.getInfo()); 562 } 563 564 @TestTargetNew( 565 level = TestLevel.COMPLETE, 566 notes = "", 567 method = "getName", 568 args = {} 569 ) 570 public void testGetName() { 571 Identity i = new IdentityStub("testGetName"); 572 assertEquals ("testGetName", i.getName()); 573 } 574 575} 576