tests/libjava-mauve/src/gnu/testlet/java/lang/Thread/insecurity.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Copyright (C) 2006, 2007 Red Hat, Inc.
       
     2 // Written by Gary Benson <gbenson@redhat.com>
       
     3 
       
     4 // This file is part of Mauve.
       
     5 
       
     6 // Mauve is free software; you can redistribute it and/or modify
       
     7 // it under the terms of the GNU General Public License as published by
       
     8 // the Free Software Foundation; either version 2, or (at your option)
       
     9 // any later version.
       
    10 
       
    11 // Mauve is distributed in the hope that it will be useful,
       
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 // GNU General Public License for more details.
       
    15 
       
    16 // You should have received a copy of the GNU General Public License
       
    17 // along with Mauve; see the file COPYING.  If not, write to
       
    18 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    19 // Boston, MA 02111-1307, USA.
       
    20 
       
    21 // Tags: JDK1.2
       
    22 
       
    23 package gnu.testlet.java.lang.Thread;
       
    24 
       
    25 import java.security.Permission;
       
    26 
       
    27 import gnu.testlet.Testlet;
       
    28 import gnu.testlet.TestHarness;
       
    29 import gnu.testlet.TestSecurityManager;
       
    30 
       
    31 public class insecurity implements Testlet
       
    32 {
       
    33   private static Permission[] noChecks = new Permission[0];
       
    34 
       
    35   public void test(TestHarness harness)
       
    36   {
       
    37     try {
       
    38       harness.checkPoint("setup");
       
    39 
       
    40       TestSecurityManager sm = new TestSecurityManager(harness);
       
    41 
       
    42       // The default SecurityManager.checkAccess(Thread) method should
       
    43       // only check permissions when the thread in question is a system
       
    44       // thread.  System threads are those whose parent is the system
       
    45       // threadgroup, which is the threadgroup with no parent.
       
    46       // 
       
    47       // The default SecurityManager.checkAccess(ThreadGroup) method
       
    48       // should only check permissions when the threadgroup in
       
    49       // question is the system threadgroup.
       
    50       ThreadGroup systemGroup = Thread.currentThread().getThreadGroup();
       
    51       while (systemGroup.getParent() != null)
       
    52 	systemGroup = systemGroup.getParent();
       
    53 
       
    54       ThreadGroup nonSystemGroup = new ThreadGroup(systemGroup, "test group");
       
    55 
       
    56       Thread testThread = new Thread(nonSystemGroup, "test thread");
       
    57       harness.check(testThread.getThreadGroup().getParent() != null);
       
    58 
       
    59       Thread modifyGroupThread = new Thread(
       
    60 	nonSystemGroup, new SysTestRunner(harness, sm));
       
    61       harness.check(modifyGroupThread.getThreadGroup().getParent() != null);
       
    62 
       
    63       Throwable threadDeath = new ThreadDeath();
       
    64       Throwable notThreadDeath = new ClassNotFoundException();
       
    65 
       
    66       Runnable runnable = new Runnable()
       
    67       {
       
    68 	public void run()
       
    69 	{
       
    70 	}
       
    71       };
       
    72 
       
    73       Permission[] stopThread = new Permission[] {
       
    74 	new RuntimePermission("stopThread")};
       
    75 
       
    76       try {
       
    77 	sm.install();
       
    78 
       
    79 	// corresponding throwpoint: java.lang.Thread-checkAccess
       
    80 	harness.checkPoint("checkAccess");
       
    81 	try {
       
    82 	  sm.prepareChecks(noChecks);
       
    83 	  testThread.checkAccess();
       
    84 	  sm.checkAllChecked();
       
    85 	}
       
    86 	catch (SecurityException ex) {
       
    87 	  harness.debug(ex);
       
    88 	  harness.check(false, "unexpected check");
       
    89 	}
       
    90 
       
    91 	// corresponding throwpoint: java.lang.Thread-interrupt
       
    92 	harness.checkPoint("interrupt");
       
    93 	try {
       
    94 	  sm.prepareChecks(noChecks);
       
    95 	  testThread.interrupt();
       
    96 	  sm.checkAllChecked();
       
    97 	}
       
    98 	catch (SecurityException ex) {
       
    99 	  harness.debug(ex);
       
   100 	  harness.check(false, "unexpected check");
       
   101 	}
       
   102 
       
   103 	// corresponding throwpoint: java.lang.Thread-suspend
       
   104 	harness.checkPoint("suspend");
       
   105 	try {
       
   106 	  sm.prepareChecks(noChecks);
       
   107 	  testThread.suspend();
       
   108 	  sm.checkAllChecked();
       
   109 	}
       
   110 	catch (SecurityException ex) {
       
   111 	  harness.debug(ex);
       
   112 	  harness.check(false, "unexpected check");
       
   113 	}
       
   114 
       
   115 	// corresponding throwpoint: java.lang.Thread-resume
       
   116 	harness.checkPoint("resume");
       
   117 	try {
       
   118 	  sm.prepareChecks(noChecks);
       
   119 	  testThread.resume();
       
   120 	  sm.checkAllChecked();
       
   121 	}
       
   122 	catch (SecurityException ex) {
       
   123 	  harness.debug(ex);
       
   124 	  harness.check(false, "unexpected check");
       
   125 	}
       
   126 
       
   127 	// corresponding throwpoint: java.lang.Thread-setPriority
       
   128 	harness.checkPoint("setPriority");
       
   129 	try {
       
   130 	  int priority = testThread.getPriority();
       
   131 	  sm.prepareChecks(noChecks);
       
   132 	  testThread.setPriority(priority);
       
   133 	  sm.checkAllChecked();
       
   134 	}
       
   135 	catch (SecurityException ex) {
       
   136 	  harness.debug(ex);
       
   137 	  harness.check(false, "unexpected check");
       
   138 	}
       
   139 
       
   140 	// corresponding throwpoint: java.lang.Thread-setName
       
   141 	harness.checkPoint("setName");
       
   142 	try {
       
   143 	  sm.prepareChecks(noChecks);
       
   144 	  testThread.setName("a test thread");
       
   145 	  sm.checkAllChecked();
       
   146 	}
       
   147 	catch (SecurityException ex) {
       
   148 	  harness.debug(ex);
       
   149 	  harness.check(false, "unexpected check");
       
   150 	}
       
   151 
       
   152 	// corresponding throwpoint: java.lang.Thread-setDaemon
       
   153 	harness.checkPoint("setDaemon");
       
   154 	try {
       
   155 	  sm.prepareChecks(noChecks);
       
   156 	  testThread.setDaemon(false);
       
   157 	  sm.checkAllChecked();
       
   158 	}
       
   159 	catch (SecurityException ex) {
       
   160 	  harness.debug(ex);
       
   161 	  harness.check(false, "unexpected check");
       
   162 	}
       
   163 
       
   164 	// corresponding throwpoint: java.lang.Thread-stop()
       
   165 	harness.checkPoint("stop()");
       
   166 	try {
       
   167 	  sm.prepareChecks(stopThread);
       
   168 	  testThread.stop();
       
   169 	  sm.checkAllChecked();
       
   170 	}
       
   171 	catch (SecurityException ex) {
       
   172 	  harness.debug(ex);
       
   173 	  harness.check(false, "unexpected check");
       
   174 	}
       
   175 
       
   176 	// corresponding throwpoint: java.lang.Thread-stop(Throwable)
       
   177 	harness.checkPoint("stop(Throwable)");
       
   178 	try {
       
   179 	  sm.prepareChecks(stopThread);
       
   180 	  testThread.stop(threadDeath);
       
   181 	  sm.checkAllChecked();
       
   182 	}
       
   183 	catch (SecurityException ex) {
       
   184 	  harness.debug(ex);
       
   185 	  harness.check(false, "unexpected check");
       
   186 	}
       
   187 
       
   188 	try {
       
   189 	  sm.prepareChecks(stopThread);
       
   190 	  testThread.stop(notThreadDeath);
       
   191 	  sm.checkAllChecked();
       
   192 	}
       
   193 	catch (SecurityException ex) {
       
   194 	  harness.debug(ex);
       
   195 	  harness.check(false, "unexpected check");
       
   196 	}
       
   197 	
       
   198 	// The noChecksGroup tests get run in a system thread.
       
   199 	modifyGroupThread.start();
       
   200 	modifyGroupThread.join();
       
   201 
       
   202 	// corresponding throwpoints: java.lang.Thread-Thread(ThreadGroup, ...)
       
   203 	harness.checkPoint("ThreadGroup constructors");
       
   204 	for (int i = 1; i <= 4; i++) {
       
   205 	  try {
       
   206 	    sm.prepareChecks(noChecks);
       
   207 	    switch (i) {
       
   208 	    case 1:
       
   209 	      new Thread(nonSystemGroup, runnable);
       
   210 	      break;
       
   211 	    case 2:
       
   212 	      new Thread(nonSystemGroup, runnable, "test thread");
       
   213 	      break;
       
   214 	    case 3:
       
   215 	      new Thread(nonSystemGroup, runnable, "test thread", 1024);
       
   216 	      break;
       
   217 	    case 4:
       
   218 	      new Thread(nonSystemGroup, "test thread");
       
   219 	      break;
       
   220 	    }
       
   221 	    sm.checkAllChecked();
       
   222 	  }
       
   223 	  catch (SecurityException ex) {
       
   224 	    harness.debug(ex);
       
   225 	    harness.check(false, "unexpected check");
       
   226 	  }
       
   227 	}
       
   228       }
       
   229       finally {
       
   230 	sm.uninstall();
       
   231       }
       
   232     }
       
   233     catch (Exception ex) {
       
   234       harness.debug(ex);
       
   235       harness.check(false, "Unexpected exception");
       
   236     }
       
   237   }
       
   238 
       
   239   // Stuff for the modifyThreadGroup tests
       
   240   public static class SysTestRunner implements Runnable
       
   241   {
       
   242     private TestHarness harness;
       
   243     private TestSecurityManager sm;
       
   244 
       
   245     private static Runnable runnable = new Runnable()
       
   246     {
       
   247       public void run()
       
   248       {
       
   249       }
       
   250     };
       
   251 
       
   252     public SysTestRunner(TestHarness harness, TestSecurityManager sm)
       
   253     {
       
   254       this.harness = harness;
       
   255       this.sm = sm;
       
   256     }
       
   257 
       
   258     public void run()
       
   259     {
       
   260       try {
       
   261 	// corresponding throwpoint: java.lang.Thread-enumerate
       
   262 	harness.checkPoint("enumerate");
       
   263 	try {
       
   264 	  sm.prepareChecks(noChecks);
       
   265 	  Thread.enumerate(new Thread[0]);
       
   266 	  sm.checkAllChecked();
       
   267 	}
       
   268 	catch (SecurityException ex) {
       
   269 	  harness.debug(ex);
       
   270 	  harness.check(false, "unexpected check");
       
   271 	}	
       
   272 
       
   273 	// corresponding throwpoint: java.lang.Thread-Thread()
       
   274 	// corresponding throwpoint: java.lang.Thread-Thread(Runnable)
       
   275 	// corresponding throwpoint: java.lang.Thread-Thread(String)
       
   276 	// corresponding throwpoint: java.lang.Thread-Thread(Runnable, String)
       
   277 	harness.checkPoint("basic constructors");
       
   278 	for (int i = 1; i <= 4; i++) {
       
   279 	  try {
       
   280 	    sm.prepareChecks(noChecks);
       
   281 	    switch (i) {
       
   282 	    case 1:
       
   283 	      new Thread();
       
   284 	      break;
       
   285 	    case 2:
       
   286 	      new Thread(runnable);
       
   287 	      break;
       
   288 	    case 3:
       
   289 	      new Thread("test thread");
       
   290 	      break;
       
   291 	    case 4:
       
   292 	      new Thread(runnable, "test thread");
       
   293 	      break;
       
   294 	    }
       
   295 	    sm.checkAllChecked();
       
   296 	  }
       
   297 	  catch (SecurityException ex) {
       
   298 	    harness.debug(ex);
       
   299 	    harness.check(false, "unexpected check");
       
   300 	  }
       
   301 	}
       
   302       }
       
   303       catch (Exception ex) {
       
   304 	harness.debug(ex);
       
   305 	harness.check(false, "Unexpected exception");
       
   306       }
       
   307     }
       
   308   }
       
   309 }