Hi all,

My previous commit fixes a number of problems with the Thread
throwpoint tests.  Firstly, they failed big-time on proprietary
runtimes: seems that Classpath's default SecurityManager's
checkAccess() methods perform permission checks in more cases
than is necessary.  Secondly, some constructor throwpoints were
not tested.  It turns out they aren't working either, so this
commit causes four new fails.  My next task is to fix all this :)

Cheers,
Gary
Index: ChangeLog
===================================================================
RCS file: /cvs/mauve/mauve/ChangeLog,v
retrieving revision 1.1604
diff -u -r1.1604 ChangeLog
--- ChangeLog   3 May 2006 00:13:37 -0000       1.1604
+++ ChangeLog   4 May 2006 14:37:33 -0000
@@ -1,3 +1,9 @@
+2006-05-04  Gary Benson  <[EMAIL PROTECTED]>
+
+       * gnu/testlet/java/lang/Thread/security.java: Added some missing
+       constructor tests, and rearranged to work with class libraries
+       other than Classpath.
+
 2006-05-03  Robert Schuster  <[EMAIL PROTECTED]>
 
        * gnu/testlet/javax/swing/text/GapContent/constructors.java:
Index: gnu/testlet/java/lang/Thread/security.java
===================================================================
RCS file: /cvs/mauve/mauve/gnu/testlet/java/lang/Thread/security.java,v
retrieving revision 1.2
diff -u -r1.2 security.java
--- gnu/testlet/java/lang/Thread/security.java  17 Feb 2006 13:11:14 -0000      
1.2
+++ gnu/testlet/java/lang/Thread/security.java  4 May 2006 14:37:33 -0000
@@ -32,13 +32,17 @@
 
 public class security implements Testlet
 {
+  private static Permission[] modifyThread = new Permission[] {
+    new RuntimePermission("modifyThread")};
+
+  private static Permission[] modifyThreadGroup = new Permission[] {
+    new RuntimePermission("modifyThreadGroup")};
+
   public void test(TestHarness harness)
   {
     try {
       harness.checkPoint("setup");
 
-      Thread testThread = new Thread();
-
       // we need a different classloader for some of the checks to occur.
       Class testClass = new URLClassLoader(new URL[] {
        new File(harness.getSourceDirectory()).toURL()}, null).loadClass(
@@ -48,11 +52,30 @@
       Method getContextClassLoaderTest = testClass.getMethod(
        "testGetContextClassLoader", new Class[] {Thread.class});
 
-      Thread currentThread = Thread.currentThread();
+      TestSecurityManager2 sm = new TestSecurityManager2(harness);
+
+      // The default SecurityManager.checkAccess(Thread) method only
+      // checks permissions when the thread in question is a system
+      // thread.  System threads are those whose parent is the system
+      // threadgroup, which is the threadgroup with no parent.
+      // 
+      // The default SecurityManager.checkAccess(ThreadGroup) method
+      // only checks permissions when the threadgroup in question is
+      // the system threadgroup.
+      ThreadGroup systemGroup = Thread.currentThread().getThreadGroup();
+      while (systemGroup.getParent() != null)
+       systemGroup = systemGroup.getParent();
+
+      Thread testThread = new Thread(systemGroup, "test thread");
+      harness.check(testThread.getThreadGroup().getParent() == null);
+
+      Thread modifyGroupThread = new Thread(
+       systemGroup, new SysTestRunner(harness, sm, testThread));
+      harness.check(modifyGroupThread.getThreadGroup().getParent() == null);
+
       Throwable threadDeath = new ThreadDeath();
       Throwable notThreadDeath = new ClassNotFoundException();
 
-      ThreadGroup group = new ThreadGroup("test group");
       Runnable runnable = new Runnable()
       {
        public void run()
@@ -66,17 +89,17 @@
       Permission[] setContextClassLoader = new Permission[] {
        new RuntimePermission("setContextClassLoader")};
 
-      Permission[] modifyThread = new Permission[] {
-       new RuntimePermission("modifyThread")};
-
       Permission[] stopThread = new Permission[] {
        new RuntimePermission("modifyThread"),
        new RuntimePermission("stopThread")};
 
-      Permission[] modifyThreadGroup = new Permission[] {
-       new RuntimePermission("modifyThreadGroup")};
+      // XXX Thread.stop() tests only work on Classpath
+      // XXX The checks don't happen otherwise, so calls
+      // XXX to Thread.currentThread().stop() actually
+      // XXX happen :(  So, we inhibit this.
+      boolean we_are_gnu_classpath =
+       System.getProperty("gnu.classpath.version") != null;
 
-      TestSecurityManager2 sm = new TestSecurityManager2(harness);
       try {
        sm.install();
 
@@ -189,18 +212,6 @@
          harness.debug(ex);
          harness.check(false, "unexpected check");
        }
-       
-       // throwpoint: java.lang.Thread-enumerate
-       harness.checkPoint("enumerate");
-       try {
-         sm.prepareChecks(modifyThreadGroup);
-         Thread.enumerate(new Thread[0]);
-         sm.checkAllChecked(harness);
-       }
-       catch (SecurityException ex) {
-         harness.debug(ex);
-         harness.check(false, "unexpected check");
-       }       
 
        // throwpoint: java.lang.Thread-stop()
        harness.checkPoint("stop()");
@@ -216,7 +227,8 @@
 
        try {
          sm.prepareChecks(modifyThread, true);
-         currentThread.stop();
+         if (we_are_gnu_classpath)
+           Thread.currentThread().stop();
          harness.check(false, "shouldn't be reached");   
        }
        catch (SecurityException ex) {
@@ -253,7 +265,8 @@
        
        try {
          sm.prepareChecks(modifyThread, true);
-         currentThread.stop(threadDeath);
+         if (we_are_gnu_classpath)
+           Thread.currentThread().stop(threadDeath);
          harness.check(false, "shouldn't be reached");   
        }
        catch (SecurityException ex) {
@@ -268,7 +281,8 @@
 
        try {
          sm.prepareChecks(stopThread, true);
-         currentThread.stop(notThreadDeath);
+         if (we_are_gnu_classpath)
+           Thread.currentThread().stop(notThreadDeath);
          harness.check(false, "shouldn't be reached");   
        }
        catch (SecurityException ex) {
@@ -281,6 +295,10 @@
          }
        }
 
+       // The modifyThreadGroup tests get run in a system thread.
+       modifyGroupThread.start();
+       modifyGroupThread.join();
+
        // throwpoint: java.lang.Thread-Thread(ThreadGroup, Runnable)
        // throwpoint: java.lang.Thread-Thread(ThreadGroup, Runnable, String)
        // throwpoint: java.lang.Thread-Thread(ThreadGroup, Runnable, String, 
long)
@@ -288,19 +306,19 @@
        harness.checkPoint("ThreadGroup constructors");
        for (int i = 1; i <= 4; i++) {
          try {
-           sm.prepareChecks(modifyThreadGroup);
+           sm.prepareChecks(modifyThreadGroup, modifyThread);
            switch (i) {
            case 1:
-             new Thread(group, runnable);
+             new Thread(systemGroup, runnable);
              break;
            case 2:
-             new Thread(group, runnable, "test thread");
+             new Thread(systemGroup, runnable, "test thread");
              break;
            case 3:
-             new Thread(group, runnable, "test thread", 1024);
+             new Thread(systemGroup, runnable, "test thread", 1024);
              break;
            case 4:
-             new Thread(group, "test thread");
+             new Thread(systemGroup, "test thread");
              break;
            }
            sm.checkAllChecked(harness);
@@ -326,4 +344,81 @@
   {
     t.getContextClassLoader();
   }
+
+  // Stuff for the modifyThreadGroup tests
+  public static class SysTestRunner implements Runnable
+  {
+    private TestHarness harness;
+    private TestSecurityManager2 sm;
+    private Thread testThread;
+
+    private static Runnable runnable = new Runnable()
+    {
+      public void run()
+      {
+      }
+    };
+
+    public SysTestRunner(TestHarness harness,
+                        TestSecurityManager2 sm,
+                        Thread testThread)
+    {
+      this.harness = harness;
+      this.sm = sm;
+      this.testThread = testThread;
+    }
+
+    public void run()
+    {
+      try {
+       Thread thisThread = Thread.currentThread();
+
+       // throwpoint: java.lang.Thread-enumerate
+       harness.checkPoint("enumerate");
+       try {
+         sm.prepareChecks(modifyThreadGroup);
+         Thread.enumerate(new Thread[0]);
+         sm.checkAllChecked(harness);
+       }
+       catch (SecurityException ex) {
+         harness.debug(ex);
+         harness.check(false, "unexpected check");
+       }       
+
+       // throwpoint: java.lang.Thread-Thread()
+       // throwpoint: java.lang.Thread-Thread(Runnable)
+       // throwpoint: java.lang.Thread-Thread(String)
+       // throwpoint: java.lang.Thread-Thread(Runnable, String)
+       harness.checkPoint("basic constructors");
+       for (int i = 1; i <= 4; i++) {
+         try {
+           sm.prepareChecks(modifyThreadGroup, modifyThread);
+           switch (i) {
+           case 1:
+             new Thread();
+             break;
+           case 2:
+             new Thread(runnable);
+             break;
+           case 3:
+             new Thread("test thread");
+             break;
+           case 4:
+             new Thread(runnable, "test thread");
+             break;
+           }
+           sm.checkAllChecked(harness);
+         }
+         catch (SecurityException ex) {
+           harness.debug(ex);
+           harness.check(false, "unexpected check");
+         }
+       }
+      }
+      catch (Exception ex) {
+       harness.debug(ex);
+       harness.check(false, "Unexpected exception");
+      }
+    }
+  }
 }

Reply via email to