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 package tests.api.javax.security.auth; 19 20 import dalvik.annotation.TestLevel; 21 import dalvik.annotation.TestTargetClass; 22 import dalvik.annotation.TestTargetNew; 23 import dalvik.annotation.TestTargets; 24 25 import junit.framework.TestCase; 26 27 import javax.security.auth.AuthPermission; 28 import javax.security.auth.PrivateCredentialPermission; 29 import javax.security.auth.Subject; 30 31 import java.util.Set; 32 import java.util.HashSet; 33 import java.security.Permission; 34 import java.security.Principal; 35 import java.security.PrivilegedAction; 36 import java.security.PrivilegedActionException; 37 import java.security.PrivilegedExceptionAction; 38 import java.security.AccessControlContext; 39 import java.security.AccessController; 40 import java.security.ProtectionDomain; 41 42 import org.apache.harmony.security.tests.support.acl.PrincipalImpl; 43 44 45 /** 46 * Tests for <code>Subject</code> class constructors and methods. 47 * 48 */ 49 @TestTargetClass(Subject.class) 50 public class SubjectTest extends TestCase { 51 52 /** 53 * @tests javax.security.auth.Subject#Subject() 54 */ 55 @TestTargetNew( 56 level = TestLevel.COMPLETE, 57 notes = "", 58 method = "Subject", 59 args = {} 60 ) 61 public void test_Constructor_01() { 62 try { 63 Subject s = new Subject(); 64 assertNotNull("Null object returned", s); 65 assertTrue("Set of principal is not empty", s.getPrincipals().isEmpty()); 66 assertTrue("Set of private credentials is not empty", s.getPrivateCredentials().isEmpty()); 67 assertTrue("Set of public credentials is not empty", s.getPublicCredentials().isEmpty()); 68 } catch (Exception e) { 69 fail("Unexpected exception: " + e); 70 } 71 } 72 73 /** 74 * @tests javax.security.auth.Subject#Subject(boolean readOnly, 75 * Set<? extends Principal> principals, 76 * Set<?> pubCredentials, 77 * Set<?> privCredentials) 78 */ 79 @TestTargetNew( 80 level = TestLevel.COMPLETE, 81 notes = "", 82 method = "Subject", 83 args = {boolean.class, Set.class, Set.class, Set.class} 84 ) 85 public void test_Constructor_02() { 86 Set <Principal> principal = new HashSet<Principal>(); 87 Set <Object> pubCredentials = new HashSet<Object>(); 88 Set <Object> privCredentials = new HashSet<Object>(); 89 Principal pr1 = new PrincipalImpl("TestPrincipal1"); 90 Principal pr2 = new PrincipalImpl("TestPrincipal2"); 91 principal.add(pr1); 92 principal.add(pr2); 93 Object pubCredential1 = new Object(); 94 Object pubCredential2 = new Object(); 95 pubCredentials.add(pubCredential1); 96 pubCredentials.add(pubCredential2); 97 Object privCredential1 = new Object(); 98 Object privCredential2 = new Object(); 99 privCredentials.add(privCredential1); 100 privCredentials.add(privCredential2); 101 102 try { 103 Subject s = new Subject(true, principal, pubCredentials, privCredentials); 104 assertNotNull("Null object returned", s); 105 assertTrue("Not read-only object", s.isReadOnly()); 106 assertFalse("Set of principal is empty", s.getPrincipals().isEmpty()); 107 assertFalse("Set of private credentials is empty", s.getPrivateCredentials().isEmpty()); 108 assertFalse("Set of public credentials is empty", s.getPublicCredentials().isEmpty()); 109 } catch (Exception e) { 110 fail("Unexpected exception: " + e); 111 } 112 113 try { 114 Subject s = new Subject(false, principal, pubCredentials, privCredentials); 115 assertNotNull("Null object returned", s); 116 assertFalse("Read-only object", s.isReadOnly()); 117 assertFalse("Set of principal is empty", s.getPrincipals().isEmpty()); 118 assertFalse("Set of private credentials is empty", s.getPrivateCredentials().isEmpty()); 119 assertFalse("Set of public credentials is empty", s.getPublicCredentials().isEmpty()); 120 } catch (Exception e) { 121 fail("Unexpected exception: " + e); 122 } 123 124 try { 125 Subject s = new Subject(true, null, pubCredentials, privCredentials); 126 fail("NullPointerException wasn't thrown"); 127 } catch (NullPointerException npe) { 128 } 129 130 try { 131 Subject s = new Subject(true, principal, null, privCredentials); 132 fail("NullPointerException wasn't thrown"); 133 } catch (NullPointerException npe) { 134 } 135 136 try { 137 Subject s = new Subject(true, principal, pubCredentials, null); 138 fail("NullPointerException wasn't thrown"); 139 } catch (NullPointerException npe) { 140 } 141 142 try { 143 Subject s = new Subject(true, null, null, null); 144 fail("NullPointerException wasn't thrown"); 145 } catch (NullPointerException npe) { 146 } 147 } 148 149 /** 150 * @tests javax.security.auth.Subject#doAs(Subject subject, PrivilegedAction action) 151 */ 152 @TestTargetNew( 153 level = TestLevel.COMPLETE, 154 notes = "", 155 method = "doAs", 156 args = {Subject.class, PrivilegedAction.class} 157 ) 158 public void test_doAs_01() { 159 Subject subj = new Subject(); 160 PrivilegedAction<Object> pa = new myPrivilegedAction(); 161 PrivilegedAction<Object> paNull = null; 162 163 try { 164 Object obj = Subject.doAs(null, pa); 165 } catch (Exception e) { 166 fail("Unexpected exception: " + e); 167 } 168 169 try { 170 Object obj = Subject.doAs(subj, pa); 171 } catch (Exception e) { 172 fail("Unexpected exception: " + e); 173 } 174 175 try { 176 Object obj = Subject.doAs(subj, paNull); 177 fail("NullPointerException wasn't thrown"); 178 } catch (NullPointerException npe) { 179 } 180 } 181 182 /** 183 * @tests javax.security.auth.Subject#doAs(Subject subject, PrivilegedExceptionAction action) 184 */ 185 @TestTargetNew( 186 level = TestLevel.COMPLETE, 187 notes = "", 188 method = "doAs", 189 args = {Subject.class, PrivilegedExceptionAction.class} 190 ) 191 public void test_doAs_02() { 192 Subject subj = new Subject(); 193 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 194 PrivilegedExceptionAction<Object> peaNull = null; 195 196 try { 197 Object obj = Subject.doAs(null, pea); 198 } catch (Exception e) { 199 fail("Unexpected exception: " + e); 200 } 201 202 try { 203 Object obj = Subject.doAs(subj, pea); 204 } catch (Exception e) { 205 fail("Unexpected exception: " + e); 206 } 207 208 try { 209 Object obj = Subject.doAs(subj, peaNull); 210 fail("NullPointerException wasn't thrown"); 211 } catch (NullPointerException npe) { 212 } catch (Exception e) { 213 fail(e + " was thrown instead of NullPointerException"); 214 } 215 216 try { 217 Subject.doAs(subj, new PrivilegedExceptionAction<Object>(){ 218 public Object run() throws PrivilegedActionException { 219 throw new PrivilegedActionException(null); 220 } 221 }); 222 fail("PrivilegedActionException wasn't thrown"); 223 } catch (PrivilegedActionException e) { 224 } 225 } 226 227 /** 228 * @tests javax.security.auth.Subject#doAsPrivileged(Subject subject, 229 * PrivilegedAction action, 230 * AccessControlContext acc) 231 */ 232 @TestTargetNew( 233 level = TestLevel.COMPLETE, 234 notes = "", 235 method = "doAsPrivileged", 236 args = {Subject.class, PrivilegedAction.class, AccessControlContext.class} 237 ) 238 public void test_doAsPrivileged_01() { 239 Subject subj = new Subject(); 240 PrivilegedAction<Object> pa = new myPrivilegedAction(); 241 PrivilegedAction<Object> paNull = null; 242 AccessControlContext acc = AccessController.getContext(); 243 244 try { 245 Object obj = Subject.doAsPrivileged(null, pa, acc); 246 } catch (Exception e) { 247 fail("Unexpected exception: " + e); 248 } 249 250 try { 251 Object obj = Subject.doAsPrivileged(subj, pa, acc); 252 } catch (Exception e) { 253 fail("Unexpected exception: " + e); 254 } 255 256 try { 257 Object obj = Subject.doAsPrivileged(subj, paNull, acc); 258 fail("NullPointerException wasn't thrown"); 259 } catch (NullPointerException npe) { 260 } 261 } 262 263 /** 264 * @tests javax.security.auth.Subject#doAsPrivileged(Subject subject, 265 * PrivilegedExceptionAction action, 266 * AccessControlContext acc) 267 */ 268 @TestTargetNew( 269 level = TestLevel.COMPLETE, 270 notes = "", 271 method = "doAsPrivileged", 272 args = {Subject.class, PrivilegedExceptionAction.class, AccessControlContext.class} 273 ) 274 public void test_doAsPrivileged_02() { 275 Subject subj = new Subject(); 276 PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction(); 277 PrivilegedExceptionAction<Object> peaNull = null; 278 AccessControlContext acc = AccessController.getContext(); 279 280 try { 281 Object obj = Subject.doAsPrivileged(null, pea, acc); 282 } catch (Exception e) { 283 fail("Unexpected exception: " + e); 284 } 285 286 try { 287 Object obj = Subject.doAsPrivileged(subj, pea, acc); 288 } catch (Exception e) { 289 fail("Unexpected exception: " + e); 290 } 291 292 try { 293 Object obj = Subject.doAsPrivileged(subj, peaNull, acc); 294 fail("NullPointerException wasn't thrown"); 295 } catch (NullPointerException npe) { 296 } catch (Exception e) { 297 fail(e + " was thrown instead of NullPointerException"); 298 } 299 300 try { 301 Subject.doAsPrivileged(subj, new PrivilegedExceptionAction<Object>(){ 302 public Object run() throws PrivilegedActionException { 303 throw new PrivilegedActionException(null); 304 } 305 }, acc); 306 fail("PrivilegedActionException wasn't thrown"); 307 } catch (PrivilegedActionException e) { 308 } 309 } 310 311 /** 312 * @tests javax.security.auth.Subject#equals(Object o) 313 */ 314 @TestTargetNew( 315 level = TestLevel.SUFFICIENT, 316 notes = "SecurityException wasn't tested", 317 method = "equals", 318 args = {Object.class} 319 ) 320 public void test_equals() { 321 Set <Principal> principal = new HashSet<Principal>(); 322 Set <Principal> principal1 = new HashSet<Principal>(); 323 Set <Object> pubCredentials = new HashSet<Object>(); 324 Set <Object> privCredentials = new HashSet<Object>(); 325 Principal pr1 = new PrincipalImpl("TestPrincipal1"); 326 Principal pr2 = new PrincipalImpl("TestPrincipal2"); 327 principal.add(pr1); 328 principal.add(pr2); 329 principal1.add(pr1); 330 Object pubCredential1 = new Object(); 331 Object pubCredential2 = new Object(); 332 pubCredentials.add(pubCredential1); 333 pubCredentials.add(pubCredential2); 334 Object privCredential1 = new Object(); 335 Object privCredential2 = new Object(); 336 privCredentials.add(privCredential1); 337 privCredentials.add(privCredential2); 338 339 Subject s1 = new Subject(true, principal, pubCredentials, privCredentials); 340 Subject s2 = new Subject(true, principal1, pubCredentials, privCredentials); 341 Subject s3 = new Subject(true, principal, pubCredentials, privCredentials); 342 343 try { 344 assertTrue(s1.equals(s1)); 345 assertFalse(s1.equals(s2)); 346 assertTrue(s1.equals(s3)); 347 assertFalse(s1.equals(new Object())); 348 } catch (Exception e) { 349 fail("Unexpected exception: " + e); 350 } 351 } 352 353 /** 354 * @tests javax.security.auth.Subject#getPrincipals() 355 * @tests javax.security.auth.Subject#getPrivateCredentials() 356 * @tests javax.security.auth.Subject#getPublicCredentials() 357 * @tests javax.security.auth.Subject#isReadOnly() 358 * @tests javax.security.auth.Subject#setReadOnly() 359 */ 360 @TestTargets({ 361 @TestTargetNew( 362 level = TestLevel.COMPLETE, 363 notes = "", 364 method = "getPrincipals", 365 args = {} 366 ), 367 @TestTargetNew( 368 level = TestLevel.COMPLETE, 369 notes = "", 370 method = "getPrivateCredentials", 371 args = {} 372 ), 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 notes = "", 376 method = "getPublicCredentials", 377 args = {} 378 ) 379 }) 380 public void test_getPrincipals() { 381 Set <Principal> principal = new HashSet<Principal>(); 382 Set <Object> pubCredentials = new HashSet<Object>(); 383 Set <Object> privCredentials = new HashSet<Object>(); 384 Principal pr1 = new PrincipalImpl("TestPrincipal1"); 385 Principal pr2 = new PrincipalImpl("TestPrincipal2"); 386 principal.add(pr1); 387 principal.add(pr2); 388 Object pubCredential1 = new Object(); 389 pubCredentials.add(pubCredential1); 390 Object privCredential1 = new Object(); 391 Object privCredential2 = new Object(); 392 privCredentials.add(privCredential1); 393 privCredentials.add(privCredential2); 394 395 Subject s = new Subject(false, principal, pubCredentials, privCredentials); 396 397 try { 398 Set<Principal> pr = s.getPrincipals(); 399 assertNotNull(pr); 400 assertEquals(principal.size(), pr.size()); 401 } catch (Exception e) { 402 fail("Unexpected exception: " + e); 403 } 404 405 try { 406 Set<Object> privC = s.getPrivateCredentials(); 407 assertNotNull(privC); 408 assertEquals(privCredentials.size(), privC.size()); 409 } catch (Exception e) { 410 fail("Unexpected exception: " + e); 411 } 412 413 try { 414 Set<Object> pubC = s.getPublicCredentials(); 415 assertNotNull(pubC); 416 assertEquals(pubCredentials.size(), pubC.size()); 417 } catch (Exception e) { 418 fail("Unexpected exception: " + e); 419 } 420 } 421 422 /** 423 * @tests javax.security.auth.Subject#isReadOnly() 424 * @tests javax.security.auth.Subject#setReadOnly() 425 */ 426 @TestTargets({ 427 @TestTargetNew( 428 level = TestLevel.COMPLETE, 429 notes = "", 430 method = "isReadOnly", 431 args = {} 432 ), 433 @TestTargetNew( 434 level = TestLevel.COMPLETE, 435 notes = "", 436 method = "setReadOnly", 437 args = {} 438 ) 439 }) 440 public void test_ReadOnly() { 441 Set <Principal> principal = new HashSet<Principal>(); 442 Set <Object> pubCredentials = new HashSet<Object>(); 443 Set <Object> privCredentials = new HashSet<Object>(); 444 Principal pr1 = new PrincipalImpl("TestPrincipal1"); 445 Principal pr2 = new PrincipalImpl("TestPrincipal2"); 446 principal.add(pr1); 447 principal.add(pr2); 448 Object pubCredential1 = new Object(); 449 pubCredentials.add(pubCredential1); 450 Object privCredential1 = new Object(); 451 Object privCredential2 = new Object(); 452 privCredentials.add(privCredential1); 453 privCredentials.add(privCredential2); 454 455 Subject s = new Subject(false, principal, pubCredentials, privCredentials); 456 457 try { 458 assertFalse(s.isReadOnly()); 459 s.setReadOnly(); 460 assertTrue(s.isReadOnly()); 461 } catch (Exception e) { 462 fail("Unexpected exception " + e); 463 } 464 } 465 466 /** 467 * @tests javax.security.auth.Subject#getSubject(AccessControlContext acc) 468 */ 469 @TestTargetNew( 470 level = TestLevel.COMPLETE, 471 notes = "", 472 method = "getSubject", 473 args = {AccessControlContext.class} 474 ) 475 public void test_getSubject() { 476 Subject subj = new Subject(); 477 AccessControlContext acc = new AccessControlContext(new ProtectionDomain[0]); 478 479 try { 480 assertNull(Subject.getSubject(acc)); 481 } catch (Exception e) { 482 fail("Unexpected exception " + e); 483 } 484 } 485 486 /** 487 * @tests javax.security.auth.Subject#toString() 488 */ 489 @TestTargetNew( 490 level = TestLevel.COMPLETE, 491 notes = "", 492 method = "toString", 493 args = {} 494 ) 495 public void test_toString() { 496 Subject subj = new Subject(); 497 498 try { 499 assertNotNull("Null returned", subj.toString()); 500 } catch (Exception e) { 501 fail("Unexpected exception: " + e); 502 } 503 } 504 505 /** 506 * @tests javax.security.auth.Subject#hashCode() 507 */ 508 @TestTargetNew( 509 level = TestLevel.SUFFICIENT, 510 notes = "SecurityException wasn't tested", 511 method = "hashCode", 512 args = {} 513 ) 514 public void test_hashCode() { 515 Subject subj = new Subject(); 516 517 try { 518 assertNotNull("Null returned", subj.hashCode()); 519 } catch (Exception e) { 520 fail("Unexpected exception: " + e); 521 } 522 } 523 524 /** 525 * @tests javax.security.auth.Subject#getPrincipals(Class<T> c) 526 * @tests javax.security.auth.Subject#getPrivateCredentials(Class<T> c) 527 * @tests javax.security.auth.Subject#getPublicCredentials(Class<T> c) 528 */ 529 @TestTargets({ 530 @TestTargetNew( 531 level = TestLevel.COMPLETE, 532 notes = "", 533 method = "getPrincipals", 534 args = {Class.class} 535 ), 536 @TestTargetNew( 537 level = TestLevel.SUFFICIENT, 538 notes = "", 539 method = "getPrivateCredentials", 540 args = {Class.class} 541 ), 542 @TestTargetNew( 543 level = TestLevel.SUFFICIENT, 544 notes = "", 545 method = "getPublicCredentials", 546 args = {Class.class} 547 ) 548 }) 549 public void test_getPrincipals_Class() { 550 Set <Principal> principal = new HashSet<Principal>(); 551 Set <Object> pubCredentials = new HashSet<Object>(); 552 Set <Object> privCredentials = new HashSet<Object>(); 553 Principal pr1 = new PrincipalImpl("TestPrincipal1"); 554 Principal pr2 = new PrincipalImpl("TestPrincipal2"); 555 principal.add(pr1); 556 principal.add(pr2); 557 Object pubCredential1 = new Object(); 558 pubCredentials.add(pubCredential1); 559 Object privCredential1 = new Object(); 560 Object privCredential2 = new Object(); 561 privCredentials.add(privCredential1); 562 privCredentials.add(privCredential2); 563 564 Subject s = new Subject(true, principal, pubCredentials, privCredentials); 565 566 try { 567 Set<Principal> pr = s.getPrincipals(null); 568 fail("NullPointerException wasn't thrown"); 569 } catch (NullPointerException npe) { 570 } 571 572 try { 573 Set<Object> privC = s.getPrivateCredentials(null); 574 fail("NullPointerException wasn't thrown"); 575 } catch (NullPointerException npe) { 576 } 577 578 try { 579 Set<Object> pubC = s.getPublicCredentials(null); 580 fail("NullPointerException wasn't thrown"); 581 } catch (NullPointerException npe) { 582 } 583 584 try { 585 Set<Principal> pr = s.getPrincipals(Principal.class); 586 assertNotNull(pr); 587 assertEquals(principal.size(), pr.size()); 588 } catch (Exception e) { 589 fail("Unexpected exception: " + e); 590 } 591 592 try { 593 Set<Object> privC = s.getPrivateCredentials(Object.class); 594 assertNotNull(privC); 595 assertEquals(privCredentials.size(), privC.size()); 596 } catch (Exception e) { 597 fail("Unexpected exception: " + e); 598 } 599 600 try { 601 Set<Object> pubC = s.getPublicCredentials(Object.class); 602 assertNotNull(pubC); 603 assertEquals(pubCredentials.size(), pubC.size()); 604 } catch (Exception e) { 605 fail("Unexpected exception: " + e); 606 } 607 } 608 } 609 610 611 class myPrivilegedAction implements PrivilegedAction <Object> { 612 myPrivilegedAction(){} 613 public Object run() { 614 return new Object(); 615 } 616 } 617 618 class myPrivilegedExceptionAction implements PrivilegedExceptionAction <Object> { 619 myPrivilegedExceptionAction(){} 620 public Object run() { 621 return new Object(); 622 } 623 } 624