SecurityManagerTest.java revision 5d709784bbf5001012d7f25172927d46f6c1abe1
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.luni.tests.java.lang; 18 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestTargetClass; 23 24import junit.framework.TestCase; 25 26import java.io.File; 27import java.io.FileDescriptor; 28import java.io.FilePermission; 29import java.io.IOException; 30import java.lang.reflect.Member; 31import java.net.InetAddress; 32import java.net.InetSocketAddress; 33import java.net.ServerSocket; 34import java.net.Socket; 35import java.net.SocketPermission; 36import java.net.UnknownHostException; 37import java.security.AccessControlContext; 38import java.security.AllPermission; 39import java.security.Permission; 40import java.security.ProtectionDomain; 41import java.security.Security; 42import java.security.SecurityPermission; 43 44import tests.support.Support_Exec; 45 46/** 47 * Test case for java.lang.SecurityManager 48 */ 49@TestTargetClass(value = SecurityManager.class, 50 untestedMethods = { 51 @TestTargetNew( 52 level = TestLevel.NOT_FEASIBLE, 53 notes = "AWTPermission class is not supported.", 54 method = "checkSystemClipboardAccess", 55 args = {} 56 ) 57}) 58public class SecurityManagerTest extends TestCase { 59 MutableSecurityManager mutableSM = null; 60 61 MockSecurityManager mockSM = null; 62 63 SecurityManager originalSM = null; 64 65 String deletedFile = "/"; 66 String readedFile = "/"; 67 String writedFile = "/"; 68 69 /** 70 * @tests java.lang.SecurityManager#SecurityManager() 71 */ 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 notes = "", 75 method = "SecurityManager", 76 args = {} 77 ) 78 public void test_Constructor() { 79 SecurityManager localManager = null; 80 try { 81 localManager = new MockSecurityManager(); 82 } catch (Exception e) { 83 fail("Unexpected exception " + e.toString()); 84 } 85 86 try { 87 assertNotNull("Incorrect SecurityManager", localManager); 88 System.setSecurityManager(localManager); 89 try { 90 new MockSecurityManager(); 91 fail("SecurityException was not thrown"); 92 } catch (SecurityException se) { 93 // expected 94 } 95 } finally { 96 System.setSecurityManager(null); 97 } 98 } 99 100 /** 101 * @tests java.lang.SecurityManager#checkPackageAccess(String) 102 */ 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 notes = "", 106 method = "checkPackageAccess", 107 args = {java.lang.String.class} 108 ) 109 public void test_checkPackageAccessLjava_lang_String() { 110 final String old = Security.getProperty("package.access"); 111 Security.setProperty("package.access", "a.,bbb, c.d."); 112 113 mutableSM 114 .denyPermission(new RuntimePermission("accessClassInPackage.*")); 115 116 try { 117 mutableSM.checkPackageAccess("z.z.z"); 118 mutableSM.checkPackageAccess("aa"); 119 mutableSM.checkPackageAccess("bb"); 120 mutableSM.checkPackageAccess("c"); 121 122 try { 123 mutableSM.checkPackageAccess("a"); 124 fail("This should throw a SecurityException."); 125 } catch (SecurityException ok) { 126 } 127 128 try { 129 mutableSM.checkPackageAccess("bbb"); 130 fail("This should throw a SecurityException."); 131 } catch (SecurityException ok) { 132 } 133 134 try { 135 mutableSM.checkPackageAccess("c.d.e"); 136 fail("This should throw a SecurityException."); 137 } catch (SecurityException ok) { 138 } 139 140 Security.setProperty("package.access", "QWERTY"); 141 mutableSM.checkPackageAccess("a"); 142 mutableSM.checkPackageAccess("qwerty"); 143 try { 144 mutableSM.checkPackageAccess("QWERTY"); 145 fail("This should throw a SecurityException."); 146 } catch (SecurityException ok) { 147 } 148 149 } finally { 150 Security.setProperty("package.access", old == null ? "" : old); 151 } 152 } 153 154 /** 155 * @tests java.lang.SecurityManager#checkPackageDefinition(String) 156 */ 157 @TestTargetNew( 158 level = TestLevel.COMPLETE, 159 notes = "", 160 method = "checkPackageDefinition", 161 args = {java.lang.String.class} 162 ) 163 public void test_checkPackageDefinitionLjava_lang_String() { 164 final String old = Security.getProperty("package.definition"); 165 Security.setProperty("package.definition", "a.,bbb, c.d."); 166 167 mutableSM 168 .denyPermission(new RuntimePermission("defineClassInPackage.*")); 169 170 try { 171 mutableSM.checkPackageDefinition("z.z.z"); 172 mutableSM.checkPackageDefinition("aa"); 173 mutableSM.checkPackageDefinition("bb"); 174 mutableSM.checkPackageDefinition("c"); 175 176 try { 177 mutableSM.checkPackageDefinition("a"); 178 fail("This should throw a SecurityException."); 179 } catch (SecurityException ok) { 180 } 181 182 try { 183 mutableSM.checkPackageDefinition("bbb"); 184 fail("This should throw a SecurityException."); 185 } catch (SecurityException ok) { 186 } 187 188 try { 189 mutableSM.checkPackageDefinition("c.d.e"); 190 fail("This should throw a SecurityException."); 191 } catch (SecurityException ok) { 192 } 193 194 Security.setProperty("package.definition", "QWERTY"); 195 mutableSM.checkPackageDefinition("a"); 196 mutableSM.checkPackageDefinition("qwerty"); 197 try { 198 mutableSM.checkPackageDefinition("QWERTY"); 199 fail("This should throw a SecurityException."); 200 } catch (SecurityException ok) { 201 } 202 203 } finally { 204 Security.setProperty("package.definition", old == null ? "" : old); 205 } 206 } 207 208 /** 209 * @tests java.lang.SecurityManager#checkMemberAccess(java.lang.Class, int) 210 */ 211 @TestTargetNew( 212 level = TestLevel.COMPLETE, 213 notes = "", 214 method = "checkMemberAccess", 215 args = {java.lang.Class.class, int.class} 216 ) 217 public void test_checkMemberAccessLjava_lang_ClassI() { 218 // enable all but one check 219 mutableSM.addPermission(new AllPermission()); 220 mutableSM.denyPermission( 221 new RuntimePermission("accessDeclaredMembers")); 222 System.setSecurityManager(mutableSM); 223 try { 224 getClass().getDeclaredFields(); 225 226 try { 227 Object.class.getDeclaredFields(); 228 fail("This should throw a SecurityException."); 229 } catch (SecurityException e) { 230 } 231 232 try { 233 mutableSM.checkMemberAccess(Object.class, Member.DECLARED); 234 fail("SecurityException was not thrown."); 235 } catch(SecurityException se) { 236 //expected 237 } 238 239 try { 240 mutableSM.checkMemberAccess(null, Member.PUBLIC); 241 fail("NullPointerException was not thrown."); 242 } catch(NullPointerException npe) { 243 //expected 244 } 245 } finally { 246 System.setSecurityManager(null); 247 } 248 } 249 250 /** 251 * @tests java.lang.SecurityManager#checkPermission(java.security.Permission) 252 */ 253 @TestTargetNew( 254 level = TestLevel.COMPLETE, 255 notes = "", 256 method = "checkPermission", 257 args = {java.security.Permission.class} 258 ) 259 public void test_checkPermissionLjava_security_Permission() 260 throws Exception { 261 262 // tmp user home to avoid presence of ${user.home}/.java.policy 263 //String tmpUserHome = System.getProperty("java.io.tmpdir") 264 // + File.separatorChar + "tmpUserHomeForSecurityManagerTest"; 265 //File dir = new File(tmpUserHome); 266 //if (!dir.exists()) { 267 // dir.mkdirs(); 268 // dir.deleteOnExit(); 269 //} 270 //String javaPolycy = tmpUserHome + File.separatorChar + ".java.policy"; 271 //assertFalse("There should be no java policy file: " + javaPolycy, 272 // new File(javaPolycy).exists()); 273 // 274 //String[] arg = new String[] { "-Duser.home=" + tmpUserHome, 275 // checkPermissionLjava_security_PermissionTesting.class.getName() }; 276 // 277 //Support_Exec.execJava(arg, null, true); 278 279 checkPermissionLjava_security_PermissionTesting.class.getName(); 280 281 try { 282 mutableSM.checkPermission(null); 283 fail("NullPointerException was not thrown."); 284 } catch(NullPointerException npe) { 285 //expected 286 } 287 } 288 289 private static class checkPermissionLjava_security_PermissionTesting { 290 public static void main(String[] args) { 291 MutableSecurityManager sm = new MutableSecurityManager(); 292 sm.addPermission(MutableSecurityManager.SET_SECURITY_MANAGER); 293 System.setSecurityManager(sm); 294 try { 295 try { 296 System.getSecurityManager().checkPermission( 297 new RuntimePermission("createClassLoader")); 298 fail("This should throw a SecurityException"); 299 } catch (SecurityException e) { 300 } 301 302 try { 303 sm.checkPermission(new SecurityPermission("setSystemScope")); 304 } catch(SecurityException se) { 305 fail("SecurityException is thrown."); 306 } 307 } finally { 308 System.setSecurityManager(null); 309 } 310 } 311 } 312 313 /** 314 * @tests java.lang.SecurityManager#checkAccess(java.lang.Thread) 315 */ 316 @TestTargetNew( 317 level = TestLevel.COMPLETE, 318 notes = "", 319 method = "checkAccess", 320 args = {java.lang.Thread.class} 321 ) 322 public void test_checkAccessLjava_lang_Thread() throws InterruptedException { 323 // Regression for HARMONY-66 324 Thread t = new Thread() { 325 @Override 326 public void run() { 327 } 328 }; 329 t.start(); 330 t.join(); 331 new SecurityManager().checkAccess(t); 332 333 mutableSM.addPermission(new AllPermission()); 334 mutableSM.denyPermission( new RuntimePermission("modifyThread")); 335 System.setSecurityManager(mutableSM); 336 337 try { 338 try { 339 mutableSM.checkAccess(t); 340 // should not throw SecurityException for not system thread. 341 } catch(SecurityException se) { 342 fail("SecurityException was thrown."); 343 } 344 345 try { 346 ThreadGroup initialThreadGroup = Thread.currentThread().getThreadGroup(); 347 348 while (initialThreadGroup.getParent() != null) { 349 initialThreadGroup = initialThreadGroup.getParent(); 350 } 351 Thread [] systemThread = new Thread[1]; 352 initialThreadGroup.enumerate(systemThread); 353 mutableSM.checkAccess(systemThread[0]); 354 fail("SecurityException was not thrown."); 355 } catch(SecurityException se) { 356 // expected 357 } 358 359 360 } finally { 361 System.setSecurityManager(null); 362 } 363 364 try { 365 mutableSM.checkAccess((Thread) null); 366 fail("NullPointerException was not thrown."); 367 } catch(NullPointerException npe) { 368 //expected 369 } 370 371 try { 372 new SecurityManager().checkAccess((Thread)null); 373 fail("NullPointerException was not thrown."); 374 } catch(NullPointerException npe){ 375 //expected 376 } 377 } 378 379 @TestTargetNew( 380 level = TestLevel.COMPLETE, 381 notes = "", 382 method = "checkAccess", 383 args = {java.lang.ThreadGroup.class} 384 ) 385 public void test_checkAccessLjava_lang_ThreadGroup() { 386 387 ThreadGroup tg = new ThreadGroup("name"); 388 389 RuntimePermission rp = new RuntimePermission("modifyThreadGroup"); 390 mutableSM.addPermission(new AllPermission()); 391 392 mutableSM.denyPermission(rp); 393 SecurityManager sm = System.getSecurityManager(); 394 System.setSecurityManager(mutableSM); 395 396 try { 397 try { 398 mutableSM.checkAccess(tg); 399 400 } catch(SecurityException se) { 401 fail("SecurityException was thrown."); 402 } 403 404 try { 405 ThreadGroup initialThreadGroup = Thread.currentThread().getThreadGroup(); 406 407 while (initialThreadGroup.getParent() != null) { 408 initialThreadGroup = initialThreadGroup.getParent(); 409 } 410 mutableSM.checkAccess(initialThreadGroup); 411 } catch(SecurityException se) { 412 413 } 414 } finally { 415 System.setSecurityManager(sm); 416 } 417 418 try { 419 mutableSM.checkAccess((ThreadGroup) null); 420 fail("NullPointerException was not thrown."); 421 } catch(NullPointerException npe) { 422 //expected 423 } 424 } 425 /** 426 * @tests {@link java.lang.SecurityManager#checkAccept(String, int)} 427 */ 428 @TestTargetNew( 429 level = TestLevel.COMPLETE, 430 notes = "", 431 method = "checkAccept", 432 args = {java.lang.String.class, int.class} 433 ) 434 @SuppressWarnings("nls") 435 public void test_checkAcceptLjava_lang_String_int() { 436 // enable all but one check 437 mutableSM.addPermission(new AllPermission()); 438 System.setSecurityManager(mutableSM); 439 try { 440 assertFalse(startServerSocket()); 441 assertTrue(mutableSM.isCheckAcceptCalled); 442 443 mutableSM.denyPermission(new SocketPermission("localhost:1024-", 444 "accept, connect, listen")); 445 assertTrue(startServerSocket()); 446 assertTrue(mutableSM.isCheckAcceptCalled); 447 448 try { 449 mutableSM.checkAccept(null, 0); 450 fail("NullPointerException is not thrown."); 451 } catch(NullPointerException npe) { 452 //expected 453 } 454 } finally { 455 System.setSecurityManager(null); 456 } 457 } 458 459 boolean startServerSocket() { 460 boolean isSecurityExceptionThrown = false; 461 ServerSocket ss = null; 462 try { 463 ss = new ServerSocket(3132); 464 Thread thr = new Thread() { 465 Socket s = null; 466 467 public void run() { 468 try { 469 s = new Socket(InetAddress.getLocalHost().getHostName(), 3132); 470 Thread.sleep(1); 471 } catch(InterruptedException ie) { 472 fail("InterruptedException was thrown."); 473 } catch(UnknownHostException uhe) { 474 fail("UnknownHostException was thrown."); 475 } catch(IOException ioe) { 476 fail("IOException was thrown."); 477 } finally { 478 try { 479 s.close(); 480 } catch(Exception e) {} 481 } 482 } 483 }; 484 thr.start(); 485 ss.accept(); 486 ss.close(); 487 } catch(IOException ioe) { 488 fail("IOException was thrown."); 489 } catch(SecurityException se) { 490 isSecurityExceptionThrown = true; 491 } finally { 492 try { 493 if(!ss.isClosed()) 494 ss.close(); 495 } catch(Exception e) { 496 } 497 498 } 499 return isSecurityExceptionThrown; 500 } 501 502 /** 503 * @tests {@link java.lang.SecurityManager#checkConnect(String, int)} 504 */ 505 @TestTargetNew( 506 level = TestLevel.COMPLETE, 507 notes = "", 508 method = "checkConnect", 509 args = {java.lang.String.class, int.class} 510 ) 511 public void test_checkConnectLjava_lang_StringI() { 512 String hostName = "localhost"; 513 int port = 1024; 514 515 // enable all but one check 516 mutableSM.addPermission(new AllPermission()); 517 mutableSM.denyPermission(new SocketPermission("localhost:1024-", 518 "accept, connect, listen")); 519 System.setSecurityManager(mutableSM); 520 try { 521 try { 522 mutableSM.checkConnect(hostName, port); 523 fail("This should throw a SecurityException."); 524 } catch (SecurityException e) { 525 // expected 526 } 527 528 assertTrue(createSocketAddress(hostName, port)); 529 530 try { 531 mutableSM.checkConnect(hostName, -1); 532 fail("This should throw a SecurityException."); 533 } catch (SecurityException e) { 534 // expected 535 } 536 537 try { 538 mutableSM.checkConnect(null, 1024); 539 fail("NullPointerException was not thrown."); 540 } catch(NullPointerException npe) { 541 //expected 542 } 543 } finally { 544 System.setSecurityManager(null); 545 } 546 547 assertFalse(createSocketAddress(hostName, port)); 548 } 549 550 boolean createSocketAddress(String hostname, int port) { 551 552 try { 553 new InetSocketAddress(hostname, port); 554 } catch(SecurityException se) { 555 return true; 556 } 557 return false; 558 } 559 560 /** 561 * @tests {@link java.lang.SecurityManager#checkConnect(String, int, Object)} 562 */ 563 @TestTargetNew( 564 level = TestLevel.COMPLETE, 565 notes = "", 566 method = "checkConnect", 567 args = {java.lang.String.class, int.class, java.lang.Object.class} 568 ) 569 @SuppressWarnings("nls") 570 public void test_checkConnectLjava_lang_String_int_Ljava_lang_Object() { 571 // enable all but one check 572 mutableSM.addPermission(new AllPermission()); 573 mutableSM.denyPermission(new SocketPermission("localhost:1024-", 574 "accept, connect, listen")); 575 System.setSecurityManager(mutableSM); 576 try { 577 ProtectionDomain pDomain = this.getClass().getProtectionDomain(); 578 ProtectionDomain[] pd = { pDomain }; 579 AccessControlContext acc = new AccessControlContext(pd); 580 try { 581 mutableSM.checkConnect("localhost", 1024, acc); 582 fail("This should throw a SecurityException."); 583 } catch (SecurityException e) { 584 // expected 585 } 586 587 try { 588 mutableSM.checkConnect("localhost", -1, acc); 589 // The action "resolve" is implicitely in the denied Permission 590 // that was added to the denied permissions at the beginning of 591 // this test. So this throws a security Exception on the RI and 592 // also on android. 593 fail("This should throw a SecurityException."); 594 } catch (SecurityException e) { 595 // expected 596 } 597 598 assertTrue(createSocketAddress("localhost", 1024)); 599 600 try { 601 mutableSM.checkConnect(null, 1024, acc); 602 fail("NullPointerException was not thrown."); 603 } catch(NullPointerException npe) { 604 //expected 605 } 606 System.setSecurityManager(null); 607 try { 608 mutableSM.checkConnect("localhost", 1024, null); 609 fail("SecurityException was not thrown."); 610 } catch(SecurityException se) { 611 //expected 612 } 613 } finally { 614 System.setSecurityManager(null); 615 } 616 assertFalse(createSocketAddress("localhost", 1024)); 617 } 618 619 @TestTargetNew( 620 level = TestLevel.COMPLETE, 621 notes = "", 622 method = "checkCreateClassLoader", 623 args = {} 624 ) 625 public void test_checkCreateClassLoader() { 626 // enable all but one check 627 mutableSM.addPermission(new AllPermission()); 628 System.setSecurityManager(mutableSM); 629 try { 630 mutableSM.checkCreateClassLoader(); 631 } catch (SecurityException e) { 632 fail("Unexpected SecurityException " + e.toString()); 633 } 634 635 SecurityManager localManager = new MockSecurityManager(); 636 try { 637 System.setSecurityManager(localManager); 638 try { 639 localManager.checkCreateClassLoader(); 640 fail("Expected SecurityException was not thrown"); 641 } catch (SecurityException e) { 642 // expected 643 } 644 } finally { 645 System.setSecurityManager(null); 646 } 647 } 648 649 @TestTargetNew( 650 level = TestLevel.COMPLETE, 651 notes = "", 652 method = "checkDelete", 653 args = {java.lang.String.class} 654 ) 655 public void test_checkDeleteLjava_lang_String() { 656 // enable all but one check 657 mutableSM.addPermission(new AllPermission()); 658 mutableSM 659 .denyPermission(new FilePermission("<<ALL FILES>>", "delete")); 660 try { 661 System.setSecurityManager(mutableSM); 662 663 try { 664 mutableSM.checkDelete("new.file"); 665 fail("SecurityException was not thrown"); 666 } catch (SecurityException npe) { 667 // expected 668 } 669 670 try { 671 mutableSM.checkDelete(null); 672 fail("NullPointerException was not thrown"); 673 } catch (NullPointerException npe) { 674 // expected 675 } 676 } finally { 677 System.setSecurityManager(null); 678 } 679 680 SecurityManager localManager = new MockSecurityManager(); 681 try { 682 System.setSecurityManager(localManager); 683 try { 684 localManager.checkDelete(deletedFile); 685 fail("Expected SecurityException was not thrown"); 686 } catch (SecurityException e) { 687 // expected 688 } 689 } finally { 690 System.setSecurityManager(null); 691 } 692 } 693 694 /** 695 * @tests {@link java.lang.SecurityManager#checkExec(String)} 696 */ 697 @TestTargetNew( 698 level = TestLevel.COMPLETE, 699 notes = "", 700 method = "checkExec", 701 args = {java.lang.String.class} 702 ) 703 @SuppressWarnings("nls") 704 public void test_checkExecLjava_lang_String() { 705 // enable all but one check 706 mutableSM.addPermission(new AllPermission()); 707 mutableSM 708 .denyPermission(new FilePermission("<<ALL FILES>>", "execute")); 709 System.setSecurityManager(mutableSM); 710 try { 711 mutableSM.checkExec("java"); 712 fail("This should throw a SecurityException."); 713 } catch (SecurityException e) { 714 // expected 715 } finally { 716 System.setSecurityManager(null); 717 } 718 719 try { 720 mutableSM.checkExec(null); 721 fail("NullPointerException was not thrown."); 722 } catch(NullPointerException npe) { 723 //expected 724 } 725 } 726 727 /** 728 * @tests {@link java.lang.SecurityManager#checkExit(int)} 729 */ 730 @TestTargetNew( 731 level = TestLevel.COMPLETE, 732 notes = "", 733 method = "checkExit", 734 args = {int.class} 735 ) 736 @SuppressWarnings("nls") 737 public void test_checkExit_int() { 738 // enable all but one check 739 mutableSM.addPermission(new AllPermission()); 740 try { 741 mutableSM.checkExit(0); 742 } catch(SecurityException se) { 743 fail("SecurityException was thrown."); 744 } 745 mutableSM.denyPermission(new RuntimePermission("exitVM")); 746 System.setSecurityManager(mutableSM); 747 try { 748 mutableSM.checkExit(0); 749 fail("This should throw a SecurityException."); 750 } catch (SecurityException e) { 751 // expected 752 } finally { 753 System.setSecurityManager(null); 754 } 755 } 756 757 /** 758 * @tests {@link java.lang.SecurityManager#checkLink(String)} 759 */ 760 @TestTargetNew( 761 level = TestLevel.COMPLETE, 762 notes = "", 763 method = "checkLink", 764 args = {java.lang.String.class} 765 ) 766 @SuppressWarnings("nls") 767 public void test_checkLinkLjava_lang_String() { 768 // enable all but one check 769 mutableSM.addPermission(new AllPermission()); 770 mutableSM.denyPermission(new RuntimePermission("loadLibrary.harmony")); 771 System.setSecurityManager(mutableSM); 772 try { 773 try { 774 mutableSM.checkLink("harmony"); 775 fail("This should throw a SecurityException."); 776 } catch (SecurityException e) { 777 // expected 778 } 779 780 try { 781 mutableSM.checkLink(null); 782 fail("NullPointerException is not thrown."); 783 } catch(NullPointerException npe) { 784 //expected 785 } 786 } finally { 787 System.setSecurityManager(null); 788 } 789 } 790 791 /** 792 * @tests {@link java.lang.SecurityManager#checkListen(int)} 793 */ 794 @TestTargetNew( 795 level = TestLevel.COMPLETE, 796 notes = "", 797 method = "checkListen", 798 args = {int.class} 799 ) 800 @SuppressWarnings("nls") 801 public void test_checkListen_int() { 802 // enable all but one check 803 mutableSM.addPermission(new AllPermission()); 804 try { 805 mutableSM.checkListen(80); 806 } catch(SecurityException se) { 807 fail("SecurityException was thrown."); 808 } 809 mutableSM 810 .denyPermission(new SocketPermission("localhost:80", "listen")); 811 System.setSecurityManager(mutableSM); 812 813 try { 814 mutableSM.checkListen(80); 815 fail("This should throw a SecurityException."); 816 } catch (SecurityException e) { 817 // expected 818 } 819 mutableSM.addPermission(new AllPermission()); 820 mutableSM.denyPermission(new SocketPermission("localhost:1024-", 821 "listen")); 822 System.setSecurityManager(mutableSM); 823 try { 824 mutableSM.checkListen(0); 825 fail("This should throw a SecurityException."); 826 } catch (SecurityException e) { 827 // expected 828 } 829 } 830 831 /** 832 * @throws UnknownHostException 833 * @tests {@link java.lang.SecurityManager#checkMulticast(java.net.InetAddress)} 834 */ 835 @TestTargetNew( 836 level = TestLevel.COMPLETE, 837 notes = "Verifies SecurityException.", 838 method = "checkMulticast", 839 args = {java.net.InetAddress.class} 840 ) 841 @SuppressWarnings("nls") 842 public void test_checkMulticastLjava_net_InetAddress() 843 throws UnknownHostException { 844 // enable all but one check 845 mutableSM.addPermission(new AllPermission()); 846 try { 847 mutableSM.checkMulticast(InetAddress.getByName("localhost")); 848 } catch(SecurityException se) { 849 fail("SecurityException is thrown."); 850 } 851 mutableSM.denyPermission(new SocketPermission(InetAddress.getByName( 852 "localhost").getHostAddress(), "accept,connect")); 853 System.setSecurityManager(mutableSM); 854 try { 855 mutableSM.checkMulticast(InetAddress.getByName("localhost")); 856 fail("This should throw a SecurityException."); 857 } catch (SecurityException e) { 858 // expected 859 } finally { 860 System.setSecurityManager(null); 861 } 862 863 try { 864 mutableSM.checkMulticast(null); 865 fail("NullPointerException was not thrown."); 866 } catch(NullPointerException e) { 867 //expected 868 } 869 } 870 871 /** 872 * @throws UnknownHostException 873 * @tests {@link java.lang.SecurityManager#checkMulticast(java.net.InetAddress,byte)} 874 */ 875 @TestTargetNew( 876 level = TestLevel.COMPLETE, 877 notes = "", 878 method = "checkMulticast", 879 args = {java.net.InetAddress.class, byte.class} 880 ) 881 @SuppressWarnings( { "nls", "deprecation" }) 882 public void test_checkMulticastLjava_net_InetAddress_int() 883 throws UnknownHostException { 884 // enable all but one check 885 mutableSM.addPermission(new AllPermission()); 886 try { 887 mutableSM.checkMulticast( 888 InetAddress.getByName("localhost"), (byte) 0); 889 } catch(SecurityException se) { 890 fail("SecurityException is thrown."); 891 } 892 mutableSM.denyPermission(new SocketPermission(InetAddress.getByName( 893 "localhost").getHostAddress(), "accept,connect")); 894 System.setSecurityManager(mutableSM); 895 try { 896 try { 897 // the second parameter is the TTL(time to live) 898 mutableSM.checkMulticast(InetAddress.getByName("localhost"), 899 (byte) 0); 900 fail("This should throw a SecurityException."); 901 } catch (SecurityException e) { 902 // expected 903 } 904 905 try { 906 mutableSM.checkMulticast(null, (byte) 0); 907 fail("NullPointerException is not thrown."); 908 } catch(NullPointerException ne) { 909 //expected 910 } 911 } finally { 912 System.setSecurityManager(null); 913 } 914 } 915 916 /** 917 * 918 * @tests {@link java.lang.SecurityManager#checkPermission(Permission, Object)} 919 */ 920 @TestTargetNew( 921 level = TestLevel.COMPLETE, 922 notes = "", 923 method = "checkPermission", 924 args = {java.security.Permission.class, java.lang.Object.class} 925 ) 926 @SuppressWarnings("nls") 927 public void test_checkPermissionLjava_security_PermissionLjava_lang_Object() { 928 // enable all but one check 929 mutableSM.addPermission(new AllPermission()); 930 Permission denyp = new SocketPermission("localhost:1024-", 931 "accept, connect, listen"); 932 mutableSM.denyPermission(denyp); 933 System.setSecurityManager(mutableSM); 934 ProtectionDomain pDomain = this.getClass().getProtectionDomain(); 935 ProtectionDomain[] pd = { pDomain }; 936 AccessControlContext acc = new AccessControlContext(pd); 937 try { 938 mutableSM.checkPermission(denyp, acc); 939 fail("This should throw a SecurityException."); 940 } catch (SecurityException e) { 941 // expected 942 } finally { 943 System.setSecurityManager(null); 944 } 945 946 try { 947 mutableSM.checkPermission(null, acc); 948 fail("NullPointerException was not thrown."); 949 } catch (NullPointerException npe) { 950 // expected 951 } 952 953 try { 954 mutableSM.checkPermission(denyp, null); 955 fail("SecurityException was not thrown."); 956 } catch (SecurityException se) { 957 // expected 958 } 959 } 960 961 /** 962 * @tests {@link java.lang.SecurityManager#checkPrintJobAccess()} 963 */ 964 @TestTargetNew( 965 level = TestLevel.COMPLETE, 966 notes = "", 967 method = "checkPrintJobAccess", 968 args = {} 969 ) 970 @SuppressWarnings("nls") 971 public void test_checkPrintJobAccess() { 972 // enable all but one check 973 mutableSM.addPermission(new AllPermission()); 974 try { 975 mutableSM.checkPrintJobAccess(); 976 } catch(SecurityException se) { 977 fail("SecurityException is thrown."); 978 } 979 mutableSM.denyPermission(new RuntimePermission("queuePrintJob")); 980 System.setSecurityManager(mutableSM); 981 try { 982 mutableSM.checkPrintJobAccess(); 983 fail("This should throw a SecurityException."); 984 } catch (SecurityException e) { 985 // expected 986 } 987 } 988 @TestTargetNew( 989 level = TestLevel.COMPLETE, 990 notes = "", 991 method = "checkPropertiesAccess", 992 args = {} 993 ) 994 public void test_checkPropertiesAccess() { 995 // enable all but one check 996 mutableSM.addPermission(new AllPermission()); 997 System.setSecurityManager(mutableSM); 998 try { 999 mutableSM.checkPropertiesAccess(); 1000 } catch (SecurityException e) { 1001 fail("Unexpected SecurityException " + e.toString()); 1002 } finally { 1003 System.setSecurityManager(null); 1004 } 1005 1006 SecurityManager localManager = new MockSecurityManager(); 1007 try { 1008 System.setSecurityManager(localManager); 1009 try { 1010 localManager.checkPropertiesAccess(); 1011 fail("Expected SecurityException was not thrown"); 1012 } catch (SecurityException e) { 1013 // expected 1014 } 1015 } finally { 1016 System.setSecurityManager(null); 1017 } 1018 } 1019 @TestTargetNew( 1020 level = TestLevel.COMPLETE, 1021 notes = "", 1022 method = "checkPropertyAccess", 1023 args = {java.lang.String.class} 1024 ) 1025 public void test_checkPropertyAccessLjava_lang_String() { 1026 // enable all but one check 1027 mutableSM.addPermission(new AllPermission()); 1028 System.setSecurityManager(mutableSM); 1029 try { 1030 mutableSM.checkPropertyAccess("key"); 1031 } catch (SecurityException e) { 1032 fail("Unexpected SecurityException " + e.toString()); 1033 } finally { 1034 System.setSecurityManager(null); 1035 } 1036 1037 SecurityManager localManager = new MockSecurityManager(); 1038 try { 1039 System.setSecurityManager(localManager); 1040 try { 1041 localManager.checkPropertyAccess("key"); 1042 fail("Expected SecurityException was not thrown"); 1043 } catch (SecurityException e) { 1044 // expected 1045 } 1046 try { 1047 localManager.checkPropertyAccess(""); 1048 fail("Expected IllegalArgumentException was not thrown"); 1049 } catch (IllegalArgumentException e) { 1050 // expected 1051 } 1052 try { 1053 localManager.checkPropertyAccess(null); 1054 fail("Expected NullPointerException was not thrown"); 1055 } catch (NullPointerException e) { 1056 // expected 1057 } 1058 } finally { 1059 System.setSecurityManager(null); 1060 } 1061 } 1062 1063 /** 1064 * @tests {@link java.lang.SecurityManager#checkRead(FileDescriptor)} 1065 */ 1066 @TestTargetNew( 1067 level = TestLevel.COMPLETE, 1068 notes = "", 1069 method = "checkRead", 1070 args = {java.io.FileDescriptor.class} 1071 ) 1072 @SuppressWarnings("nls") 1073 public void test_checkReadLjava_io_FileDescriptor() { 1074 // enable all but one check 1075 mutableSM.addPermission(new AllPermission()); 1076 try { 1077 mutableSM.checkRead(new FileDescriptor()); 1078 } catch(SecurityException se) { 1079 fail("SecurityException is thrown."); 1080 } 1081 mutableSM.denyPermission(new RuntimePermission("readFileDescriptor")); 1082 System.setSecurityManager(mutableSM); 1083 try { 1084 mutableSM.checkRead(new FileDescriptor()); 1085 fail("This should throw a SecurityException."); 1086 } catch (SecurityException e) { 1087 // expected 1088 } 1089 } 1090 @TestTargetNew( 1091 level = TestLevel.COMPLETE, 1092 notes = "", 1093 method = "checkRead", 1094 args = {java.lang.String.class} 1095 ) 1096 public void test_checkReadLjava_lang_String() { 1097 // enable all but one check 1098 mutableSM.addPermission(new AllPermission()); 1099 try { 1100 mutableSM.checkRead(readedFile); 1101 } catch(SecurityException se) { 1102 fail("SecurityException is thrown."); 1103 } 1104 mutableSM.denyPermission(new RuntimePermission("readFileDescriptor")); 1105 System.setSecurityManager(mutableSM); 1106 try { 1107 try { 1108 mutableSM.checkRead(readedFile); 1109 } catch (SecurityException e) { 1110 fail("Unexpected SecurityException " + e.toString()); 1111 } 1112 1113 SecurityManager localManager = new MockSecurityManager(); 1114 System.setSecurityManager(localManager); 1115 try { 1116 localManager.checkRead(readedFile); 1117 fail("Expected SecurityException was not thrown"); 1118 } catch (SecurityException e) { 1119 // expected 1120 } 1121 1122 try { 1123 localManager.checkRead((String) null); 1124 fail("NullPointerException was not thrown."); 1125 } catch(NullPointerException npe) { 1126 //expected 1127 } 1128 } finally { 1129 System.setSecurityManager(null); 1130 } 1131 } 1132 1133 /** 1134 * @tests {@link java.lang.SecurityManager#checkRead(String,Object)} 1135 */ 1136 @TestTargetNew( 1137 level = TestLevel.COMPLETE, 1138 notes = "Verifies SecurityException.", 1139 method = "checkRead", 1140 args = {java.lang.String.class, java.lang.Object.class} 1141 ) 1142 @SuppressWarnings("nls") 1143 public void test_checkReadLjava_lang_StringLjava_lang_Object() { 1144 // enable all but one check 1145 mutableSM.addPermission(new AllPermission()); 1146 ProtectionDomain pDomain = this.getClass().getProtectionDomain(); 1147 ProtectionDomain[] pd = { pDomain }; 1148 AccessControlContext acc = new AccessControlContext(pd); 1149 mutableSM.denyPermission(new FilePermission("<<ALL FILES>>", "read")); 1150 System.setSecurityManager(mutableSM); 1151 try { 1152 try { 1153 mutableSM.checkRead("aa", acc); 1154 fail("This should throw a SecurityException."); 1155 } catch (SecurityException e) { 1156 // expected 1157 } 1158 1159 try { 1160 mutableSM.checkRead(null, acc); 1161 fail("NullPointerException was not thrown."); 1162 } catch(NullPointerException npe) { 1163 //expected 1164 } 1165 } finally { 1166 System.setSecurityManager(null); 1167 } 1168 } 1169 @TestTargetNew( 1170 level = TestLevel.COMPLETE, 1171 notes = "", 1172 method = "checkSecurityAccess", 1173 args = {java.lang.String.class} 1174 ) 1175 public void test_checkSecurityAccessLjava_lang_String() { 1176 // enable all but one check 1177 mutableSM.addPermission(new AllPermission()); 1178 System.setSecurityManager(mutableSM); 1179 try { 1180 mutableSM.checkSecurityAccess("getPolicy"); 1181 } catch (SecurityException e) { 1182 fail("Unexpected SecurityException " + e.toString()); 1183 } finally { 1184 System.setSecurityManager(null); 1185 } 1186 1187 SecurityManager localManager = new MockSecurityManager(); 1188 try { 1189 System.setSecurityManager(localManager); 1190 try { 1191 localManager.checkSecurityAccess("getPolicy"); 1192 fail("Expected SecurityException was not thrown"); 1193 } catch (SecurityException e) { 1194 // expected 1195 } 1196 try { 1197 localManager.checkSecurityAccess(""); 1198 fail("Expected IllegalArgumentException was not thrown"); 1199 } catch (IllegalArgumentException e) { 1200 // expected 1201 } 1202 try { 1203 localManager.checkSecurityAccess(null); 1204 fail("Expected NullPointerException was not thrown"); 1205 } catch (NullPointerException e) { 1206 // expected 1207 } 1208 } finally { 1209 System.setSecurityManager(null); 1210 } 1211 } 1212 1213 /** 1214 * @tests {@link java.lang.SecurityManager#checkSetFactory()} 1215 */ 1216 @TestTargetNew( 1217 level = TestLevel.COMPLETE, 1218 notes = "", 1219 method = "checkSetFactory", 1220 args = {} 1221 ) 1222 @SuppressWarnings("nls") 1223 public void test_checkSetFactory() { 1224 // enable all but one check 1225 mutableSM.addPermission(new AllPermission()); 1226 assertFalse(setFactory()); 1227 mutableSM.denyPermission(new RuntimePermission("setFactory")); 1228 System.setSecurityManager(mutableSM); 1229 try { 1230 try { 1231 mutableSM.checkSetFactory(); 1232 fail("This should throw a SecurityException."); 1233 } catch (SecurityException e) { 1234 // expected 1235 } 1236 assertTrue(setFactory()); 1237 } finally { 1238 System.setSecurityManager(null); 1239 } 1240 } 1241 1242 boolean setFactory() { 1243 try { 1244 ServerSocket.setSocketFactory(null); 1245 } catch(IOException ioe) { 1246 fail("IOException was thrown."); 1247 } catch(SecurityException se) { 1248 return true; 1249 } 1250 return false; 1251 } 1252 1253 @TestTargetNew( 1254 level = TestLevel.NOT_NECESSARY, 1255 notes = "Mark this method not feasable: AWTPermission doesn't exist", 1256 method = "checkAwtEventQueueAccess", 1257 args = {} 1258 ) 1259 public void test_checkAwtEventQueueAccess() { 1260 mutableSM.addPermission(new AllPermission()); 1261 // TODO AWTPermission class is unavailable 1262 //mutableSM.denyPermission(new AWTPermission("accessEventQueue")); 1263 //System.setSecurityManager(mutableSM); 1264 //try { 1265 // try { 1266 // mutableSM.checkAwtEventQueueAccess(); 1267 // fail("This should throw a SecurityException."); 1268 // } catch (SecurityException e) { 1269 // expected 1270 // } 1271 //} finally { 1272 // System.setSecurityManager(null); 1273 //} 1274 } 1275 1276 @TestTargetNew( 1277 level = TestLevel.NOT_NECESSARY, 1278 notes = "Mark this method not feasable: AWTPermission doesn't exist", 1279 method = "checkTopLevelWindow", 1280 args = {java.lang.Object.class} 1281 ) 1282 public void test_checkTopLevelWindowLjava_lang_Object() { 1283 // assertFalse("Calling thread isn't trusted to bring up the top-level window", 1284 // mutableSM.checkTopLevelWindow(this)); 1285 1286 try { 1287 SecurityManager localManager = new MockSecurityManager(); 1288 System.setSecurityManager(localManager); 1289 assertTrue("Calling thread is trusted to bring up the top-level window", 1290 localManager.checkTopLevelWindow(this)); 1291 try { 1292 localManager.checkTopLevelWindow(null); 1293 fail("Expected NullPointerexception was not thrown"); 1294 } catch (NullPointerException e) { 1295 // expected 1296 } 1297 } finally { 1298 System.setSecurityManager(null); 1299 } 1300 //TODO AWTPermission class is unavailable 1301 //mutableSM.addPermission(new AllPermission()); 1302 //assertTrue(mutableSM.checkTopLevelWindow(new Object())); 1303 //mutableSM.denyPermission(new AWTPermission("showWindowWithoutWarningBanner")); 1304 //System.setSecurityManager(mutableSM); 1305 //try { 1306 // assertFalse(mutableSM.checkTopLevelWindow(new Object())); 1307 //} finally { 1308 // System.setSecurityManager(null); 1309 //} 1310 1311 } 1312 @TestTargetNew( 1313 level = TestLevel.COMPLETE, 1314 notes = "", 1315 method = "checkWrite", 1316 args = {java.io.FileDescriptor.class} 1317 ) 1318 public void test_checkWriteLjava_io_FileDescriptor() { 1319 // enable all but one check 1320 mutableSM.addPermission(new AllPermission()); 1321 try { 1322 mutableSM.checkWrite(new FileDescriptor()); 1323 } catch(SecurityException se) { 1324 fail("SecurityException was thrown."); 1325 } 1326 mutableSM.denyPermission(new RuntimePermission("writeFileDescriptor")); 1327 System.setSecurityManager(mutableSM); 1328 try { 1329 mutableSM.checkWrite(new FileDescriptor()); 1330 fail("This should throw a SecurityException."); 1331 } catch (SecurityException e) { 1332 // expected 1333 } finally { 1334 System.setSecurityManager(null); 1335 } 1336 1337 try { 1338 mutableSM.checkWrite((FileDescriptor) null); 1339 fail("NullPointerException was not thrown."); 1340 } catch(NullPointerException npe) { 1341 //expected 1342 } 1343 } 1344 @TestTargetNew( 1345 level = TestLevel.COMPLETE, 1346 notes = "", 1347 method = "checkWrite", 1348 args = {java.lang.String.class} 1349 ) 1350 public void test_checkWriteLjava_lang_String() { 1351 // enable all but one check 1352 mutableSM.addPermission(new AllPermission()); 1353 try { 1354 mutableSM.checkWrite(writedFile); 1355 } catch(SecurityException se) { 1356 fail("SecurityException was thrown."); 1357 } 1358 mutableSM.denyPermission(new RuntimePermission("writeFileDescriptor")); 1359 System.setSecurityManager(mutableSM); 1360 try { 1361 mutableSM.checkWrite(writedFile); 1362 } catch (SecurityException e) { 1363 fail("Unexpected SecurityException " + e.toString()); 1364 } finally { 1365 System.setSecurityManager(null); 1366 } 1367 1368 try { 1369 SecurityManager localManager = new MockSecurityManager(); 1370 System.setSecurityManager(localManager); 1371 try { 1372 localManager.checkWrite(writedFile); 1373 fail("Expected SecurityException was not thrown"); 1374 } catch (SecurityException e) { 1375 // expected 1376 } 1377 } finally { 1378 System.setSecurityManager(null); 1379 } 1380 1381 try { 1382 mutableSM.checkWrite((String) null); 1383 fail("NullPointerException was not thrown."); 1384 } catch(NullPointerException npe) { 1385 //expected 1386 } 1387 } 1388 1389 /** 1390 * @tests {@link java.lang.SecurityManager#getInCheck()} 1391 */ 1392 @TestTargetNew( 1393 level = TestLevel.COMPLETE, 1394 notes = "", 1395 method = "getInCheck", 1396 args = {} 1397 ) 1398 public void test_getIncheck() { 1399 mockSM.setInCheck(false); 1400 assertFalse(mockSM.getInCheck()); 1401 mockSM.setInCheck(true); 1402 assertTrue(mockSM.getInCheck()); 1403 } 1404 1405 /** 1406 * @tests {@link java.lang.SecurityManager#getSecurityContext()} 1407 */ 1408 @TestTargetNew( 1409 level = TestLevel.COMPLETE, 1410 notes = "", 1411 method = "getSecurityContext", 1412 args = {} 1413 ) 1414 @SuppressWarnings("nls") 1415 public void test_getSecurityContext() { 1416 // enable all but one check 1417 mutableSM.addPermission(new AllPermission()); 1418 mutableSM.denyPermission(new FilePermission("<<ALL FILES>>", "read")); 1419 System.setSecurityManager(mutableSM); 1420 try { 1421 try { 1422 mutableSM.getSecurityContext(); 1423 } catch(Exception e) { 1424 fail("Unexpected exception was thrown: " + e.toString()); 1425 } 1426 1427 try { 1428 mutableSM.checkRead("aa", mutableSM.getSecurityContext()); 1429 fail("This should throw a SecurityException."); 1430 } catch (SecurityException e) { 1431 // expected 1432 } 1433 1434 } finally { 1435 System.setSecurityManager(null); 1436 } 1437 } 1438 @TestTargetNew( 1439 level = TestLevel.COMPLETE, 1440 notes = "", 1441 method = "getThreadGroup", 1442 args = {} 1443 ) 1444 public void test_getThreadGroup() throws InterruptedException { 1445 final ThreadGroup tgroup = new ThreadGroup(mutableSM.getThreadGroup(), 1446 "groupName"); 1447 assertNotNull("Incorrect thread group", tgroup); 1448 class MyThread extends Thread{ 1449 public int newCount; 1450 1451 public MyThread() { 1452 super(tgroup, "threadName"); 1453 } 1454 1455 @Override 1456 public void run() { 1457 super.run(); 1458 newCount = tgroup.activeCount(); 1459 } 1460 } 1461 MyThread t = new MyThread(); 1462 t.start(); 1463 t.join(); 1464 assertEquals("Incorrect active count value", 1, t.newCount); 1465 } 1466 1467 /** 1468 * @tests {@link java.lang.SecurityManager#classDepth(String)} 1469 */ 1470 @TestTargetNew( 1471 level = TestLevel.COMPLETE, 1472 notes = ".", 1473 method = "classDepth", 1474 args = {java.lang.String.class} 1475 ) 1476 @SuppressWarnings("nls") 1477 public void test_classDepthLjava_lang_String() { 1478 assertEquals(-1, mockSM.classDepth("nothing")); 1479 } 1480 1481 /** 1482 * @tests {@link java.lang.SecurityManager#classLoaderDepth()} 1483 */ 1484 @TestTargetNew( 1485 level = TestLevel.COMPLETE, 1486 notes = "", 1487 method = "classLoaderDepth", 1488 args = {} 1489 ) 1490 public void test_classLoaderDepth() { 1491 assertEquals(-1, mockSM.classLoaderDepth()); 1492 } 1493 1494 /** 1495 * @tests {@link java.lang.SecurityManager#currentClassLoader()} 1496 */ 1497 @TestTargetNew( 1498 level = TestLevel.COMPLETE, 1499 notes = "", 1500 method = "currentClassLoader", 1501 args = {} 1502 ) 1503 public void test_currentClassLoader() { 1504 assertNull(mockSM.currentClassLoader()); 1505 } 1506 1507 /** 1508 * @tests {@link java.lang.SecurityManager#currentLoadedClass()} 1509 */ 1510 @TestTargetNew( 1511 level = TestLevel.COMPLETE, 1512 notes = "", 1513 method = "currentLoadedClass", 1514 args = {} 1515 ) 1516 public void test_currentLoadedClass() { 1517 assertNull(mockSM.currentLoadedClass()); 1518 } 1519 1520 /** 1521 * @tests {@link java.lang.SecurityManager#inClass(String)} 1522 */ 1523 @TestTargetNew( 1524 level = TestLevel.COMPLETE, 1525 notes = "", 1526 method = "inClass", 1527 args = {java.lang.String.class} 1528 ) 1529 @SuppressWarnings("nls") 1530 public void test_inClassLjava_lang_String() { 1531 assertFalse(mockSM.inClass("nothing")); 1532 assertTrue(mockSM.inClass(MockSecurityManager.class.getName())); 1533 } 1534 1535 /** 1536 * @tests {@link java.lang.SecurityManager#inClassLoader()} 1537 */ 1538 @TestTargetNew( 1539 level = TestLevel.COMPLETE, 1540 notes = "", 1541 method = "inClassLoader", 1542 args = {} 1543 ) 1544 public void test_inClassLoader() { 1545 assertFalse(mockSM.inClassLoader()); 1546 } 1547 1548 /** 1549 * @tests {@link java.lang.SecurityManager#getClassContext()} 1550 */ 1551 @TestTargetNew( 1552 level = TestLevel.COMPLETE, 1553 notes = "", 1554 method = "getClassContext", 1555 args = {} 1556 ) 1557 public void test_getClassContext() { 1558 1559 Class [] stack = {MockSecurityManager.class, 1560 getClass(), TestCase.class}; 1561 1562 Class [] returnedStack = mockSM.getClassContext(); 1563 1564 assertNotNull(returnedStack); 1565 assertTrue(returnedStack.length > stack.length); 1566 for(int i = 0; i < stack.length; i++) { 1567 assertEquals(stack[i].getName() + " class should have " + i + 1568 " position in the classes stack, but there is " + 1569 returnedStack[i].getName(), 1570 stack[i], returnedStack[i]); 1571 } 1572 } 1573 1574 // set some protected method to public for testing 1575 1576 @Override 1577 protected void setUp() throws Exception { 1578 super.setUp(); 1579 mutableSM = new MutableSecurityManager(); 1580 mockSM = new MockSecurityManager(); 1581 originalSM = System.getSecurityManager(); 1582 } 1583 1584 @Override 1585 protected void tearDown() throws Exception { 1586 super.tearDown(); 1587 System.setSecurityManager(originalSM); 1588 } 1589} 1590