[jboss-cvs] JBossAS SVN: r71051 - in projects/jboss-reflect/trunk/src: tests/org/jboss/test/classinfo/support and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Mar 20 08:19:49 EDT 2008


Author: alesj
Date: 2008-03-20 08:19:49 -0400 (Thu, 20 Mar 2008)
New Revision: 71051

Added:
   projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/ErrorHolderThread.java
   projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/FieldsClass.java
   projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/AccessRestrictionTestCase.java
Modified:
   projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectFieldInfoImpl.java
   projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/ClassInfoTestSuite.java
Log:
Set accessible in privileged block.
Two tests, one of them still in TODO phase.

Modified: projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectFieldInfoImpl.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectFieldInfoImpl.java	2008-03-20 12:19:45 UTC (rev 71050)
+++ projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectFieldInfoImpl.java	2008-03-20 12:19:49 UTC (rev 71051)
@@ -25,7 +25,9 @@
 import java.io.ObjectInputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.ReflectPermission;
+import java.security.AccessController;
 import java.security.Permission;
+import java.security.PrivilegedAction;
 
 import org.jboss.reflect.plugins.FieldInfoImpl;
 import org.jboss.reflect.spi.AnnotationValue;
@@ -80,7 +82,7 @@
    {
       this.field = field;
       if (isPublic() == false && field != null)
-         field.setAccessible(true);
+         setAccessible();
    }
 
    /**
@@ -132,4 +134,28 @@
       oistream.defaultReadObject();
       setField(ReflectionUtils.findExactField(getDeclaringClass().getType(), name));
    }
+
+   /**
+    * Set field accessible to true
+    */
+   private void setAccessible()
+   {
+      SecurityManager sm = System.getSecurityManager();
+      if (sm == null)
+         field.setAccessible(true);
+      else
+         AccessController.doPrivileged(new SetAccessible());
+   }
+
+   /**
+    * Set accessible privileged block
+    */
+   private class SetAccessible implements PrivilegedAction<Object>
+   {
+      public Object run()
+      {
+         field.setAccessible(true);
+         return null;
+      }
+   }
 }

Added: projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/ErrorHolderThread.java
===================================================================
--- projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/ErrorHolderThread.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/ErrorHolderThread.java	2008-03-20 12:19:49 UTC (rev 71051)
@@ -0,0 +1,52 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ErrorHolderThread extends Thread
+{
+   private Throwable error;
+
+   public ErrorHolderThread(Runnable target)
+   {
+      super(target);
+   }
+
+   public void run()
+   {
+      try
+      {
+         super.run();
+      }
+      catch(Throwable t)
+      {
+         error = t;
+      }
+   }
+
+   public Throwable getError()
+   {
+      return error;
+   }
+}

Added: projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/FieldsClass.java
===================================================================
--- projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/FieldsClass.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/support/FieldsClass.java	2008-03-20 12:19:49 UTC (rev 71051)
@@ -0,0 +1,43 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class FieldsClass
+{
+   @SuppressWarnings("unused")
+   private String privString;
+   protected String protString;
+   public String pubString;
+
+   public String getPrivString()
+   {
+      return privString;
+   }
+
+   public String getProtString()
+   {
+      return protString;
+   }
+}

Added: projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/AccessRestrictionTestCase.java
===================================================================
--- projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/AccessRestrictionTestCase.java	                        (rev 0)
+++ projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/AccessRestrictionTestCase.java	2008-03-20 12:19:49 UTC (rev 71051)
@@ -0,0 +1,209 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.classinfo.test;
+
+import java.lang.reflect.Field;
+import java.security.AccessControlException;
+
+import junit.framework.Test;
+import org.jboss.beans.info.spi.BeanAccessMode;
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.config.plugins.BasicConfiguration;
+import org.jboss.config.spi.Configuration;
+import org.jboss.reflect.plugins.introspection.ReflectFieldInfoImpl;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.test.AbstractTestCaseWithSetup;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.test.classinfo.support.ErrorHolderThread;
+import org.jboss.test.classinfo.support.FieldsClass;
+
+/**
+ * Access restriction test.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class AccessRestrictionTestCase extends AbstractTestCaseWithSetup
+{
+   /** The bean info factory */
+   private Configuration configuration = new BasicConfiguration();
+
+   /**
+    * Create a new ContainerTest.
+    *
+    * @param name the test name
+    */
+   public AccessRestrictionTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(AccessRestrictionTestCase.class);
+   }
+
+   /**
+    * Default setup with security manager enabled
+    *
+    * @param clazz the class
+    * @return the delegate
+    * @throws Exception for any error
+    */
+   public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+   {
+      AbstractTestDelegate delegate = new AbstractTestDelegate(clazz);
+      delegate.enableSecurity = true;
+      return delegate;
+   }
+
+   protected BeanInfo getBeanInfo(Class<?> clazz, BeanAccessMode mode)
+   {
+      SecurityManager sm = suspendSecurity();
+      try
+      {
+         return configuration.getBeanInfo(clazz, mode);
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+   }
+
+   protected ClassInfo getClassInfo(Class<?> clazz)
+   {
+      SecurityManager sm = suspendSecurity();
+      try
+      {
+         return configuration.getClassInfo(clazz);
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+   }
+
+   public void testFieldAcessFromMain() throws Throwable
+   {
+      final FieldsClass tester = new FieldsClass();
+      final ReflectFieldInfoImpl impl = new ReflectFieldInfoImpl();
+
+      // I can't do setAccesible
+      Field field = FieldsClass.class.getDeclaredField("privString");
+      // let's try accessible
+      try
+      {
+         field.setAccessible(true);
+         fail("Should not be here.");
+      }
+      catch (Throwable t)
+      {
+         assertInstanceOf(t, AccessControlException.class);
+      }
+      // ok, setAccessible not set, so set should also fail
+      try
+      {
+         field.set(tester, "foobar");
+         fail("Should not be here.");
+      }
+      catch (Throwable t)
+      {
+         assertInstanceOf(t, IllegalAccessException.class);
+      }
+
+      impl.setField(field); // So I'll use this hole
+      field = impl.getField(); // This should have an access check
+      // why does this work?!?
+      field.set(tester, "foobar");
+      assertEquals("foobar", tester.getPrivString());
+
+      Runnable runnable = new Runnable()
+      {
+         public void run()
+         {
+            try
+            {
+               Field fi = impl.getField(); // This should have an access check
+               fi.set(tester, "something"); // this should check for caller
+            }
+            catch (Throwable t)
+            {
+               throw new RuntimeException(t);
+            }
+         }
+      };
+      ErrorHolderThread other = new ErrorHolderThread(runnable);
+      other.start();
+      other.join();
+      // we should get an error here
+/*
+      assertNotNull("Should get access restriction exception.", other.getError());
+      RuntimeException re = assertInstanceOf(other.getError(), RuntimeException.class);
+      Throwable cause = re.getCause();
+      assertNotNull(cause);
+      assertInstanceOf(cause, AccessControlException.class, false);
+*/
+   }
+
+   public void testFieldAccessFromOther() throws Throwable
+   {
+      final FieldsClass tester = new FieldsClass();
+      ClassInfo classInfo = configuration.getClassInfo(FieldsClass.class);
+      // we should not fail
+      FieldInfo pub = classInfo.getDeclaredField("pubString");
+      assertNotNull(pub);
+      final FieldInfo pri = classInfo.getDeclaredField("privString");
+      try
+      {
+         pri.set(tester, "foobar");
+         fail("Should not be here.");
+      }
+      catch (Throwable t)
+      {
+         assertInstanceOf(t, AccessControlException.class);
+      }
+      ErrorHolderThread other = new ErrorHolderThread(new Runnable()
+      {
+         public void run()
+         {
+            SecurityManager sm = suspendSecurity();
+            try
+            {
+               pri.set(tester, "foobar");
+            }
+            catch(Throwable t)
+            {
+               throw new RuntimeException(t);
+            }
+            finally
+            {
+               resumeSecurity(sm);
+            }
+         }
+      });
+      assertNull(tester.getPrivString());
+      other.start();
+      other.join();
+      assertNull(other.getError());
+      assertEquals("foobar", tester.getPrivString());
+   }
+}

Modified: projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/ClassInfoTestSuite.java
===================================================================
--- projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/ClassInfoTestSuite.java	2008-03-20 12:19:45 UTC (rev 71050)
+++ projects/jboss-reflect/trunk/src/tests/org/jboss/test/classinfo/test/ClassInfoTestSuite.java	2008-03-20 12:19:49 UTC (rev 71051)
@@ -58,7 +58,8 @@
       suite.addTest(JavassistAnnotatedClassInfoTestCase.suite());
       suite.addTest(IntrospectionGenericInterfaceUnitTestCase.suite());
       suite.addTest(IntrospectionGenericClassUnitTestCase.suite());
-      
+      suite.addTest(AccessRestrictionTestCase.suite());
+
       return suite;
    }
 }




More information about the jboss-cvs-commits mailing list