tests/libjava-mauve/src/gnu/testlet/java/lang/reflect/AccessibleObject/security.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.reflect.AccessibleObject;
       
    24 
       
    25 import java.lang.reflect.AccessibleObject;
       
    26 import java.lang.reflect.Constructor;
       
    27 import java.lang.reflect.Field;
       
    28 import java.lang.reflect.Method;
       
    29 import java.lang.reflect.Modifier;
       
    30 import java.lang.reflect.ReflectPermission;
       
    31 import java.security.Permission;
       
    32 
       
    33 import gnu.testlet.Testlet;
       
    34 import gnu.testlet.TestHarness;
       
    35 import gnu.testlet.TestSecurityManager;
       
    36 
       
    37 public class security implements Testlet
       
    38 {
       
    39   public void test(TestHarness harness)
       
    40   {
       
    41     try {
       
    42       harness.checkPoint("constructor");
       
    43       Constructor constructor =
       
    44 	ClassLoader.class.getDeclaredConstructor(new Class[0]);
       
    45       int mods = constructor.getModifiers();
       
    46       harness.check(Modifier.isPrivate(mods) || Modifier.isProtected(mods));
       
    47       try {
       
    48 	constructor.newInstance(new Object[0]);
       
    49 	harness.check(false);
       
    50       }
       
    51       catch (IllegalAccessException ex) {
       
    52 	harness.check(true);
       
    53       }
       
    54 
       
    55       harness.checkPoint("field");
       
    56       Field field = String.class.getDeclaredField("serialVersionUID");
       
    57       mods = field.getModifiers();
       
    58       harness.check(Modifier.isPrivate(mods) || Modifier.isProtected(mods));
       
    59       try {
       
    60 	field.get("");
       
    61 	harness.check(false);
       
    62       }
       
    63       catch (IllegalAccessException ex) {
       
    64 	harness.check(true);
       
    65       }
       
    66 
       
    67       harness.checkPoint("method");
       
    68       Method method =
       
    69 	ClassLoader.class.getDeclaredMethod("getPackages", new Class[0]);
       
    70       mods = method.getModifiers();
       
    71       harness.check(Modifier.isPrivate(mods) || Modifier.isProtected(mods));
       
    72       try {
       
    73 	method.invoke(getClass().getClassLoader(), new Object[0]);
       
    74 	harness.check(false);
       
    75       }
       
    76       catch (IllegalAccessException ex) {
       
    77 	harness.check(true);
       
    78       }
       
    79 
       
    80       AccessibleObject[] objects =
       
    81 	new AccessibleObject[] {constructor, field, method};
       
    82 
       
    83       AccessibleObject class_constructor =
       
    84 	Class.class.getDeclaredConstructors()[0];
       
    85 
       
    86       Permission[] suppressAccessChecks = new Permission[] {
       
    87  	new ReflectPermission("suppressAccessChecks")};
       
    88 
       
    89       TestSecurityManager sm = new TestSecurityManager(harness);
       
    90       try {
       
    91 	sm.install();
       
    92 
       
    93 	// throwpoint: java.lang.reflect.AccessibleObject-setAccessible(boolean)
       
    94 	harness.checkPoint("setAccessible (per-object)");
       
    95 	for (int i = 0; i < objects.length; i++) {
       
    96 	  try {
       
    97 	    sm.prepareChecks(suppressAccessChecks);
       
    98 	    objects[i].setAccessible(true);
       
    99 	    sm.checkAllChecked();
       
   100 	  }
       
   101 	  catch (SecurityException ex) {
       
   102 	    harness.debug(ex);
       
   103 	    harness.check(false, "unexpected check");
       
   104 	  }
       
   105 	}
       
   106 
       
   107 	harness.checkPoint("setAccessible (class constructor)");
       
   108 	try {
       
   109 	  sm.prepareChecks(suppressAccessChecks);
       
   110 	  class_constructor.setAccessible(true);
       
   111 	  harness.check(false);
       
   112 	}
       
   113 	catch (SecurityException ex) {
       
   114 	  sm.checkAllChecked();
       
   115 	}
       
   116 
       
   117 	// throwpoint: java.lang.reflect.AccessibleObject-setAccessible(AccessibleObject[], boolean)
       
   118 	harness.checkPoint("setAccessible (array)");
       
   119 	try {
       
   120 	  sm.prepareChecks(suppressAccessChecks);
       
   121 	  AccessibleObject.setAccessible(objects, true);
       
   122 	  sm.checkAllChecked();
       
   123 	}
       
   124 	catch (SecurityException ex) {
       
   125 	  harness.debug(ex);
       
   126 	  harness.check(false, "unexpected check");
       
   127 	}
       
   128       }
       
   129       finally {
       
   130 	sm.uninstall();
       
   131       }
       
   132     }
       
   133     catch (Exception ex) {
       
   134       harness.debug(ex);
       
   135       harness.check(false, "Unexpected exception");
       
   136     }
       
   137   }
       
   138 }