Package gnu.testlet

Examples of gnu.testlet.TestSecurityManager


      Permission[] enableSubstitution = new Permission[] {
  new SerializablePermission("enableSubstitution")};

      Permission[] noPerms = new Permission[] {};

      TestSecurityManager sm = new TestSecurityManager(harness);
      try {
  sm.install();

  // throwpoint: java.io.ObjectOutputStream-ObjectOutputStream
  harness.checkPoint("constructor");
  try {
    sm.prepareChecks(enableSubclassImplementation);
    new TestObjectOutputStream();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.io.ObjectOutputStream-enableReplaceObject
  harness.checkPoint("enableReplaceObject");
  try {
    sm.prepareChecks(noPerms);
    teststream.testEnableReplaceObject(false);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
  try {
    sm.prepareChecks(enableSubstitution);
    teststream.testEnableReplaceObject(true);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
      }
      finally {
  sm.uninstall();
      }
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
View Full Code Here


    // Keep a record of created temp files so we can delete them later.
    File tf1 = null;
    File tf2 = null;

    TestSecurityManager sm = new TestSecurityManager(harness);
    try {
      sm.install();
 
      // throwpoint: java.io.File-canWrite-DIR
      harness.checkPoint("dir.canWrite");
      try {
  sm.prepareChecks(new Permission[]{tmpdirWritePerm});
  tmpdir.canWrite();
  sm.checkAllChecked();
      }
      catch (SecurityException ex) {
  harness.debug(ex);
  harness.check(false, "dir.canWrite - unexpected exception");
      }

      // throwpoint: java.io.File-canRead-DIR
      harness.checkPoint("dir.canRead");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.canRead();
  sm.checkAllChecked();
      }
      catch (SecurityException ex) {
  harness.debug(ex);
  harness.check(false, "dir.canRead - unexpected exception");
      }

      // throwpoint: java.io.File-createNewFile
      harness.checkPoint("file.createNewFile");
      try {
  sm.prepareChecks(new Permission[]{tmpfileWritePerm});
  tmpfile.createNewFile();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "file.createNewFile - unexpected exception");
      }

      // throwpoint: java.io.File-delete-FILE
      harness.checkPoint("file.delete");
      try {
  sm.prepareChecks(new Permission[]{tmpfileDeletePerm});
  tmpfile.delete();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "file.delete - unexpected exception");
      }

      // throwpoint: java.io.File-list(FilenameFilter)
      harness.checkPoint("dir.list(null)");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.list(null);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.list(null) - unexpected exception");
      }

      // throwpoint: java.io.File-list
      harness.checkPoint("dir.list()");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.list();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.list() - unexpected exception");
      }

      // throwpoint: java.io.File-listFiles
      harness.checkPoint("dir.listFiles()");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.listFiles();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.listFiles() - unexpected exception");
      }

      // throwpoint: java.io.File-listFiles(FilenameFilter)
      harness.checkPoint("dir.listFiles(FilenameFilter)");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.listFiles((FilenameFilter) null);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false,
          "dir.listFiles(FilenameFilter) - unexpected exception");
      }

      // throwpoint: java.io.File-listFiles(FileFilter)
      harness.checkPoint("dir.listFiles(FileFilter)");
      try {
  sm.prepareChecks(new Permission[]{tmpdirReadPerm});
  tmpdir.listFiles((FileFilter) null);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false,
          "dir.listFiles(FileFilter) - unexpected exception");
      }

      // throwpoint: java.io.File-createTempFile(String, String)
      harness.checkPoint("createTempFile(2-args)");
      try {
  sm.prepareChecks(new Permission[]{tmpallWritePerm},
       new Permission[]{tmpdirPropPerm});
  sm.setComparisonStyle(TestSecurityManager.IMPLIES);
  tf1 = File.createTempFile("pfx", "sfx");
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "createTempFile(2-args) - unexpected exception");
      }

      // throwpoint: java.io.File-createTempFile(String, String, File)
      harness.checkPoint("createTempFile(3-args)");
      try {
  sm.prepareChecks(new Permission[]{tmpdirallWritePerm});
  sm.setComparisonStyle(TestSecurityManager.IMPLIES);
  tf2 = File.createTempFile("pfx", "sfx", tmpdir);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "createTempFile(3-args) - unexpected exception");
      }

      // throwpoint: java.io.File-setReadOnly-DIR
      harness.checkPoint("dir.setReadOnly");
      try {
  sm.prepareChecks(new Permission[]{tmpdir2WritePerm});
  tmpdir2.setReadOnly();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.setReadOnly - unexpected exception");
      }

      // throwpoint: java.io.File-delete-DIR
      // Make sure we remove the read only temp dir
      harness.checkPoint("dir.delete");
      try {
  sm.prepareChecks(new Permission[]{tmpdir2DeletePerm});
  tmpdir2.delete();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.delete - unexpected exception");
      }
     
      // throwpoint: java.io.File-listRoots
      harness.checkPoint("listRoots()");
      try {
  sm.prepareChecks(new Permission[]{rootReadPerm});
  File[] roots = File.listRoots();
  harness.check(roots.length >= 1, "File.listRoots()");
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "listRoots() - unexpected exception");
      }

      // throwpoint: java.io.File-renameTo
      harness.checkPoint("file.renameTo");
      try {
  sm.prepareChecks(new Permission[]{tmpfileWritePerm,
            tmpfile2WritePerm});
  tmpfile.renameTo(tmpfile2);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "file.renameTo - unexpected exception");
      }

      // throwpoint: java.io.File-setLastModified-DIR
      harness.checkPoint("dir.setLastModified()");
      try {
  sm.prepareChecks(new Permission[]{tmpdirWritePerm});
  tmpdir.setLastModified(0);
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.setLastModified() - unexpected exception");
      }

      // throwpoint: java.io.File-deleteOnExit-DIR
      harness.checkPoint("dir.deleteOnExit()");
      try {
  sm.prepareChecks(new Permission[]{tmpdirDeletePerm},
       new Permission[]{modifyThreadGroup, shutdownHooks});
  tmpdir.deleteOnExit();
  sm.checkAllChecked();
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "dir.deleteOnExit() - unexpected exception");
      }

      // throwpoint: TODO: java.io.File-deleteOnExit-FILE
      // throwpoint: TODO: java.io.File-exists
      // throwpoint: TODO: java.io.File-canRead-FILE
      // throwpoint: TODO: java.io.File-isFile
      // throwpoint: TODO: java.io.File-isDirectory
      // throwpoint: TODO: java.io.File-isHidden
      // throwpoint: TODO: java.io.File-lastModified
      // throwpoint: TODO: java.io.File-length
      // throwpoint: TODO: java.io.File-canWrite-FILE
      // throwpoint: TODO: java.io.File-mkdir
      // throwpoint: TODO: java.io.File-mkdirs
      // throwpoint: TODO: java.io.File-setLastModified-FILE
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "outer handler - unexpected exception");
    }
    finally {
      sm.uninstall();

      if (tmpfile != null) tmpfile.delete();
      if (tmpfile2 != null) tmpfile2.delete();
      if (tf1 != null) tf1.delete();
      if (tf2 != null) tf2.delete();
View Full Code Here

  new RuntimePermission("createClassLoader")};

      Permission[] getClassLoader = new Permission[] {
  new RuntimePermission("getClassLoader")};

      TestSecurityManager sm = new TestSecurityManager(harness);
      try {
  sm.install();

  // throwpoint: java.lang.ClassLoader-ClassLoader()
  harness.checkPoint("Constructor (no-args)");
  try {
    sm.prepareChecks(createClassLoader);
    new TestClassLoader();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.ClassLoader-ClassLoader(ClassLoader)
  harness.checkPoint("Constructor (one-arg)");
  try {
    sm.prepareChecks(createClassLoader);
    new TestClassLoader(ourLoader);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.ClassLoader-getSystemClassLoader
  harness.checkPoint("getSystemClassLoader");
  try {
    sm.prepareChecks(getClassLoader);
    getSystemClassLoaderTest.invoke(null, new Object[] {});
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.ClassLoader-getParent
  harness.checkPoint("getParent");
  try {
    sm.prepareChecks(getClassLoader);
    getParentTest.invoke(null, new Object[] {ourLoader});
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
      }
      finally {
  sm.uninstall();
      }
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
View Full Code Here

      group = group.getParent();
    if (thread.getThreadGroup() != group)
      thread = new Thread(group, "dummy");

    // Check we're checking
    TestSecurityManager sm = new TestSecurityManager(harness);

    sm.prepareChecks(new Permission[] {
      new RuntimePermission("modifyThread")});
    sm.checkAccess(thread);
    sm.checkAllChecked();

    sm.prepareChecks(new Permission[] {
      new RuntimePermission("modifyThreadGroup")});
    sm.checkAccess(group);
    sm.checkAllChecked();
  }
View Full Code Here

  new AWTPermission("accessClipboard")};

      Permission[] accessEventQueue = new Permission[] {
  new AWTPermission("accessEventQueue")};

      TestSecurityManager sm = new TestSecurityManager(harness);
      try {
  sm.install();

  // throwpoint: java.awt.Toolkit-addAWTEventListener
  harness.checkPoint("addAWTEventListener");
  try {
    sm.prepareChecks(listenToAllAWTEvents);
    toolkit.addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.awt.Toolkit-removeAWTEventListener
  harness.checkPoint("removeAWTEventListener");
  try {
    sm.prepareChecks(listenToAllAWTEvents);
    toolkit.removeAWTEventListener(listener);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.awt.Toolkit-getPrintJob(Frame, String, Properties)
  harness.checkPoint("getPrintJob(3-arg)");
  try {
    sm.prepareHaltingChecks(queuePrintJob);
    toolkit.getPrintJob(frame, "Test job", props);
    harness.check(false);   
  }
  catch (TestSecurityManager.SuccessException ex) {
    harness.check(true);
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.awt.Toolkit-getPrintJob(Frame, String, JobAttributes, PageAttributes)
  harness.checkPoint("getPrintJob(4-arg)");
  try {
    sm.prepareHaltingChecks(queuePrintJob);
    toolkit.getPrintJob(frame, "Test job", jobattrs, pageattrs);
    harness.check(false);   
  }
  catch (TestSecurityManager.SuccessException ex) {
    harness.check(true);
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.awt.Toolkit-getSystemClipboard
  harness.checkPoint("getSystemClipboard");
  try {
    sm.prepareChecks(accessClipboard);
    toolkit.getSystemClipboard();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.awt.Toolkit-getSystemEventQueue
  harness.checkPoint("getSystemEventQueue");
  try {
    sm.prepareChecks(accessEventQueue);
    toolkit.getSystemEventQueue();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
      }
      finally {
  sm.uninstall();
      }
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
View Full Code Here

  public void test(TestHarness harness)
  {
    try {
      harness.checkPoint("setup");

      TestSecurityManager sm = new TestSecurityManager(harness);

      // The default SecurityManager.checkAccess(Thread) method should
      // only check 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
      // should only check permissions when the threadgroup in
      // question is the system threadgroup.
      ThreadGroup systemGroup = Thread.currentThread().getThreadGroup();
      while (systemGroup.getParent() != null)
  systemGroup = systemGroup.getParent();

      ThreadGroup nonSystemGroup = new ThreadGroup(systemGroup, "test group");

      Thread testThread = new Thread(nonSystemGroup, "test thread");
      harness.check(testThread.getThreadGroup().getParent() != null);

      Thread modifyGroupThread = new Thread(
  nonSystemGroup, new SysTestRunner(harness, sm));
      harness.check(modifyGroupThread.getThreadGroup().getParent() != null);

      Throwable threadDeath = new ThreadDeath();
      Throwable notThreadDeath = new ClassNotFoundException();

      Runnable runnable = new Runnable()
      {
  public void run()
  {
  }
      };

      Permission[] stopThread = new Permission[] {
  new RuntimePermission("stopThread")};

      try {
  sm.install();

  // corresponding throwpoint: java.lang.Thread-checkAccess
  harness.checkPoint("checkAccess");
  try {
    sm.prepareChecks(noChecks);
    testThread.checkAccess();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-interrupt
  harness.checkPoint("interrupt");
  try {
    sm.prepareChecks(noChecks);
    testThread.interrupt();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-suspend
  harness.checkPoint("suspend");
  try {
    sm.prepareChecks(noChecks);
    testThread.suspend();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-resume
  harness.checkPoint("resume");
  try {
    sm.prepareChecks(noChecks);
    testThread.resume();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-setPriority
  harness.checkPoint("setPriority");
  try {
    int priority = testThread.getPriority();
    sm.prepareChecks(noChecks);
    testThread.setPriority(priority);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-setName
  harness.checkPoint("setName");
  try {
    sm.prepareChecks(noChecks);
    testThread.setName("a test thread");
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-setDaemon
  harness.checkPoint("setDaemon");
  try {
    sm.prepareChecks(noChecks);
    testThread.setDaemon(false);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-stop()
  harness.checkPoint("stop()");
  try {
    sm.prepareChecks(stopThread);
    testThread.stop();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // corresponding throwpoint: java.lang.Thread-stop(Throwable)
  harness.checkPoint("stop(Throwable)");
  try {
    sm.prepareChecks(stopThread);
    testThread.stop(threadDeath);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  try {
    sm.prepareChecks(stopThread);
    testThread.stop(notThreadDeath);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
 
  // The noChecksGroup tests get run in a system thread.
  modifyGroupThread.start();
  modifyGroupThread.join();

  // corresponding throwpoints: java.lang.Thread-Thread(ThreadGroup, ...)
  harness.checkPoint("ThreadGroup constructors");
  for (int i = 1; i <= 4; i++) {
    try {
      sm.prepareChecks(noChecks);
      switch (i) {
      case 1:
        new Thread(nonSystemGroup, runnable);
        break;
      case 2:
        new Thread(nonSystemGroup, runnable, "test thread");
        break;
      case 3:
        new Thread(nonSystemGroup, runnable, "test thread", 1024);
        break;
      case 4:
        new Thread(nonSystemGroup, "test thread");
        break;
      }
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
      }
      finally {
  sm.uninstall();
      }
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
View Full Code Here

      harness.check(getClass().getClassLoader() != testClass.getClassLoader());

      Method getContextClassLoaderTest = testClass.getMethod(
  "testGetContextClassLoader", new Class[] {Thread.class});

      TestSecurityManager sm = new TestSecurityManager(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));
      harness.check(modifyGroupThread.getThreadGroup().getParent() == null);

      Throwable threadDeath = new ThreadDeath();
      Throwable notThreadDeath = new ClassNotFoundException();

      Runnable runnable = new Runnable()
      {
  public void run()
  {
  }
      };

      Permission[] getClassLoader = new Permission[] {
  new RuntimePermission("getClassLoader")};

      Permission[] setContextClassLoader = new Permission[] {
  new RuntimePermission("setContextClassLoader")};

      Permission[] stopThread = new Permission[] {
  new RuntimePermission("modifyThread"),
  new RuntimePermission("stopThread")};

      // 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;

      try {
  sm.install();

  // throwpoint: java.lang.Thread-getContextClassLoader
  harness.checkPoint("getContextClassLoader");
  try {
    sm.prepareChecks(getClassLoader);
     getContextClassLoaderTest.invoke(null, new Object[] {testThread});
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-setContextClassLoader
  harness.checkPoint("setContextClassLoader");
  try {
    ClassLoader loader = testThread.getContextClassLoader();
    sm.prepareChecks(setContextClassLoader);
    testThread.setContextClassLoader(loader);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-checkAccess
  harness.checkPoint("checkAccess");
  try {
    sm.prepareChecks(modifyThread);
    testThread.checkAccess();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-interrupt
  harness.checkPoint("interrupt");
  try {
    sm.prepareChecks(modifyThread);
    testThread.interrupt();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-suspend
  harness.checkPoint("suspend");
  try {
    sm.prepareChecks(modifyThread);
    testThread.suspend();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-resume
  harness.checkPoint("resume");
  try {
    sm.prepareChecks(modifyThread);
    testThread.resume();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-setPriority
  harness.checkPoint("setPriority");
  try {
    int priority = testThread.getPriority();
    sm.prepareChecks(modifyThread);
    testThread.setPriority(priority);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-setName
  harness.checkPoint("setName");
  try {
    sm.prepareChecks(modifyThread);
    testThread.setName("a test thread");
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-setDaemon
  harness.checkPoint("setDaemon");
  try {
    sm.prepareChecks(modifyThread);
    testThread.setDaemon(false);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-stop()
  harness.checkPoint("stop()");
  try {
    sm.prepareChecks(stopThread);
    testThread.stop();
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  try {
    sm.prepareHaltingChecks(modifyThread);
    if (we_are_gnu_classpath)
      Thread.currentThread().stop();
    harness.check(false);   
  }
  catch (TestSecurityManager.SuccessException ex) {
    harness.check(true);
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // throwpoint: java.lang.Thread-stop(Throwable)
  harness.checkPoint("stop(Throwable)");
  try {
    sm.prepareChecks(stopThread);
    testThread.stop(threadDeath);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  try {
    sm.prepareChecks(stopThread);
    testThread.stop(notThreadDeath);
    sm.checkAllChecked();
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }
 
  try {
    sm.prepareHaltingChecks(modifyThread);
    if (we_are_gnu_classpath)
      Thread.currentThread().stop(threadDeath);
    harness.check(false);   
  }
  catch (TestSecurityManager.SuccessException ex) {
    harness.check(true);
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  try {
    sm.prepareHaltingChecks(stopThread);
    if (we_are_gnu_classpath)
      Thread.currentThread().stop(notThreadDeath);
    harness.check(false);   
  }
  catch (TestSecurityManager.SuccessException ex) {
    harness.check(true);
  }
  catch (SecurityException ex) {
    harness.debug(ex);
    harness.check(false, "unexpected check");
  }

  // 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)
  // throwpoint: java.lang.Thread-Thread(ThreadGroup, String)
  harness.checkPoint("ThreadGroup constructors");
  for (int i = 1; i <= 4; i++) {
    try {
      sm.prepareChecks(modifyThreadGroup, modifyThread);
      switch (i) {
      case 1:
        new Thread(systemGroup, runnable);
        break;
      case 2:
        new Thread(systemGroup, runnable, "test thread");
        break;
      case 3:
        new Thread(systemGroup, runnable, "test thread", 1024);
        break;
      case 4:
        new Thread(systemGroup, "test thread");
        break;
      }
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
      }
      finally {
  sm.uninstall();
      }
    }
    catch (Exception ex) {
      harness.debug(ex);
      harness.check(false, "Unexpected exception");
View Full Code Here

  ThreadGroup nonSystemGroup = new ThreadGroup(testGroup, "test group");
  harness.check(nonSystemGroup.getParent() != null);
 
  Permission[] noChecks = new Permission[0];

  TestSecurityManager sm = new TestSecurityManager(harness);
  try {
    sm.install();

    // corresponding throwpoint:
    // java.lang.ThreadGroup-ThreadGroup(String)
    harness.checkPoint("ThreadGroup(String)");
    try {
      sm.prepareChecks(noChecks);
      new ThreadGroup("test");
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // corresponding throwpoint:
    // java.lang.ThreadGroup-ThreadGroup(ThreadGroup, String)
    harness.checkPoint("ThreadGroup(ThreadGroup, String)");
    try {
      sm.prepareChecks(noChecks);
      new ThreadGroup(testGroup, "test");
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint: java.lang.ThreadGroup-checkAccess
    harness.checkPoint("checkAccess");
    try {
      sm.prepareChecks(noChecks);
      testGroup.checkAccess();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint:
    // java.lang.ThreadGroup-enumerate(Thread[])
    harness.checkPoint("enumerate(Thread[])");
    try {
      sm.prepareChecks(noChecks);
      testGroup.enumerate(new Thread[0]);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
   

    // corresponding throwpoint:
    // java.lang.ThreadGroup-enumerate(Thread[], boolean)
    harness.checkPoint("enumerate(Thread[], boolean)");
    for (int i = 0; i <= 1; i++) {
      try {
        sm.prepareChecks(noChecks);
        testGroup.enumerate(new Thread[0], i == 1);
        sm.checkAllChecked();
      }
      catch (SecurityException ex) {
        harness.debug(ex);
        harness.check(false, "unexpected check");
     
    }

    // corresponding throwpoint:
    // java.lang.ThreadGroup-enumerate(ThreadGroup[])
    harness.checkPoint("enumerate(ThreadGroup[])");
    try {
      sm.prepareChecks(noChecks);
      testGroup.enumerate(new ThreadGroup[0]);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
   

    // corresponding throwpoint:
    // java.lang.ThreadGroup-enumerate(ThreadGroup[], boolean)
    harness.checkPoint("enumerate(ThreadGroup[], boolean)");
    for (int i = 0; i <= 1; i++) {
      try {
        sm.prepareChecks(noChecks);
        testGroup.enumerate(new ThreadGroup[0], i == 1);
        sm.checkAllChecked();
      }
      catch (SecurityException ex) {
        harness.debug(ex);
        harness.check(false, "unexpected check");
     
    }

    // corresponding throwpoint: java.lang.ThreadGroup-getParent
    harness.checkPoint("getParent");
    try {
      sm.prepareChecks(noChecks);
      nonSystemGroup.getParent();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // corresponding throwpoint: java.lang.ThreadGroup-setDaemon
    harness.checkPoint("setDaemon");
    try {
      sm.prepareChecks(noChecks);
      testGroup.setDaemon(false);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // corresponding throwpoint: java.lang.ThreadGroup-setMaxPriority
    harness.checkPoint("setMaxPriority");
    try {
      int priority = testGroup.getMaxPriority();
      sm.prepareChecks(noChecks);
      testGroup.setMaxPriority(priority);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint: java.lang.ThreadGroup-suspend
    harness.checkPoint("suspend");
    try {
      sm.prepareChecks(noChecks);
      nonSystemGroup.suspend();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint: java.lang.ThreadGroup-resume
    harness.checkPoint("resume");
    try {
      sm.prepareChecks(noChecks);
      nonSystemGroup.resume();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint: java.lang.ThreadGroup-interrupt
    harness.checkPoint("interrupt");
    try {
      sm.prepareChecks(noChecks);
      nonSystemGroup.interrupt();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // corresponding throwpoint: java.lang.ThreadGroup-stop
    harness.checkPoint("stop");
    try {
      sm.prepareChecks(noChecks);
      nonSystemGroup.stop();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
  finally {
    sm.uninstall();
  }
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "Unexpected exception");
View Full Code Here

  Permission[] stopThread = new Permission[] {
    new RuntimePermission("modifyThread"),
    new RuntimePermission("stopThread")};

  TestSecurityManager sm = new TestSecurityManager(harness);
  try {
    sm.install();

    // throwpoint: java.lang.ThreadGroup-ThreadGroup(String)
    harness.checkPoint("ThreadGroup(String)");
    try {
      sm.prepareChecks(modifyThreadGroup);
      new ThreadGroup("test");
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // throwpoint: java.lang.ThreadGroup-ThreadGroup(ThreadGroup, String)
    harness.checkPoint("ThreadGroup(ThreadGroup, String)");
    try {
      sm.prepareChecks(modifyThreadGroup);
      new ThreadGroup(testGroup, "test");
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.ThreadGroup-checkAccess
    harness.checkPoint("checkAccess");
    try {
      sm.prepareChecks(modifyThreadGroup);
      testGroup.checkAccess();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.ThreadGroup-enumerate(Thread[])
    harness.checkPoint("enumerate(Thread[])");
    try {
      sm.prepareChecks(modifyThreadGroup);
      testGroup.enumerate(new Thread[0]);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
   

    // throwpoint: java.lang.ThreadGroup-enumerate(Thread[], boolean)
    harness.checkPoint("enumerate(Thread[], boolean)");
    for (int i = 0; i <= 1; i++) {
      try {
        sm.prepareChecks(modifyThreadGroup);
        testGroup.enumerate(new Thread[0], i == 1);
        sm.checkAllChecked();
      }
      catch (SecurityException ex) {
        harness.debug(ex);
        harness.check(false, "unexpected check");
     
    }

    // throwpoint: java.lang.ThreadGroup-enumerate(ThreadGroup[])
    harness.checkPoint("enumerate(ThreadGroup[])");
    try {
      sm.prepareChecks(modifyThreadGroup);
      testGroup.enumerate(new ThreadGroup[0]);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
   

    // throwpoint: java.lang.ThreadGroup-enumerate(ThreadGroup[], boolean)
    harness.checkPoint("enumerate(ThreadGroup[], boolean)");
    for (int i = 0; i <= 1; i++) {
      try {
        sm.prepareChecks(modifyThreadGroup);
        testGroup.enumerate(new ThreadGroup[0], i == 1);
        sm.checkAllChecked();
      }
      catch (SecurityException ex) {
        harness.debug(ex);
        harness.check(false, "unexpected check");
     
    }

    // throwpoint: java.lang.ThreadGroup-getParent
    harness.checkPoint("getParent");
    try {
      sm.prepareChecks(modifyThreadGroup);
      nonSystemGroup.getParent();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // throwpoint: java.lang.ThreadGroup-setDaemon
    harness.checkPoint("setDaemon");
    try {
      sm.prepareChecks(modifyThreadGroup);
      testGroup.setDaemon(false);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
 
    // throwpoint: java.lang.ThreadGroup-setMaxPriority
    harness.checkPoint("setMaxPriority");
    try {
      int priority = testGroup.getMaxPriority();
      sm.prepareChecks(modifyThreadGroup);
      testGroup.setMaxPriority(priority);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.ThreadGroup-suspend
    harness.checkPoint("suspend");
    try {
      sm.prepareHaltingChecks(modifyThreadGroup, modifyThread);
      testGroup.suspend();
      harness.check(false);
    }
    catch (TestSecurityManager.SuccessException ex) {
      harness.check(true);
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.ThreadGroup-resume
    harness.checkPoint("resume");
    try {
      sm.prepareHaltingChecks(modifyThreadGroup, modifyThread);
      testGroup.resume();
      harness.check(false);
    }
    catch (TestSecurityManager.SuccessException ex) {
      harness.check(true);
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: TODO: java.lang.ThreadGroup-destroy
    // XXX I'm not sure you can test for this one.  It's an
    // XXX error to call this on a non-empty threadgroup, but
    // XXX the check only happens for the system group which
    // XXX will not be empty.

    // throwpoint: java.lang.ThreadGroup-interrupt
    harness.checkPoint("interrupt");
    try {
      sm.prepareHaltingChecks(modifyThreadGroup, modifyThread);
      testGroup.interrupt();
      harness.check(false);
    }
    catch (TestSecurityManager.SuccessException ex) {
      harness.check(true);
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.ThreadGroup-stop
    harness.checkPoint("stop");
    try {
      sm.prepareHaltingChecks(modifyThreadGroup, stopThread);
      testGroup.stop();
      harness.check(false);
    }
    catch (TestSecurityManager.SuccessException ex) {
      harness.check(true);
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
  finally {
    sm.uninstall();
  }

  sm = new SpecialSecurityManager(harness);
  try {
    sm.install();

    // throwpoint: java.lang.ThreadGroup-destroy
    harness.checkPoint("destroy");
    try {
      sm.prepareChecks(modifyThreadGroup);
      nonSystemGroup.destroy();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
  finally {
    sm.uninstall();
  }
      }
      catch (Exception ex) {
  harness.debug(ex);
  harness.check(false, "Unexpected exception");
View Full Code Here

      "accessClassInPackage.gnu.testlet.java.lang.Class")};

  Permission[] getProtectionDomain = new Permission[] {
    new RuntimePermission("getProtectionDomain")};
 
  TestSecurityManager sm = new TestSecurityManager(harness);
  try {
    sm.install();

    // throwpoint: java.lang.Class-forName
    harness.checkPoint("forName");
    try {
      sm.prepareChecks(getClassLoader);
      Class.forName("java.lang.Class", false, null);
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // throwpoint: java.lang.Class-getClassLoader
    harness.checkPoint("getClassLoader");
    try {
      sm.prepareChecks(getClassLoader);
      testClass.getClassLoader();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // If it is a bootstrap class, there is a null classloader
    // and no checks are necessary.
    try {
      sm.prepareChecks(noChecks);
      Thread.class.getClassLoader();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }

    // getDeclaredMember checks
    getMemberChecks(harness, sm, testClass, true, accessDeclaredMembers);
   
    // throwpoint: java.lang.Class-getProtectionDomain
    harness.checkPoint("getProtectionDomain");
    try {
      sm.prepareChecks(getProtectionDomain);
      testClass.getProtectionDomain();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
    try {
      sm.prepareChecks(getProtectionDomain);
      getClass().getProtectionDomain();
      sm.checkAllChecked();
    }
    catch (SecurityException ex) {
      harness.debug(ex);
      harness.check(false, "unexpected check");
    }
  }
  finally {
    sm.uninstall();
  }

  // these tests need a modified security manager
  sm = new TestSecurityManager3(harness);
  try {
    sm.install();

    // getMember checks
    getMemberChecks(harness, sm, testClass, false, accessPublicMembers);
  }
  finally {
    sm.uninstall();
  }
      }
      finally {
  Security.setProperty("package.access", oldrestrictions);
      }
View Full Code Here

TOP

Related Classes of gnu.testlet.TestSecurityManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.