1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
// Written by Gary Benson <gbenson@redhat.com>
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Tags: JDK1.2
package gnu.testlet.java.lang.Class;
import java.io.File;
import java.lang.reflect.Member;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.Permission;
import java.security.Security;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import gnu.testlet.TestSecurityManager;
public class security implements Testlet
{
public void test(TestHarness harness)
{
try {
harness.checkPoint("setup");
// we need a class with a different loader for most of the
// checks to occur.
Class testClass = new URLClassLoader(new URL[] {
new File(harness.getBuildDirectory()).toURL()}, null).loadClass(
getClass().getName());
harness.check(getClass().getClassLoader() != testClass.getClassLoader());
// Make sure everything's fully resolved, or we'll be loading
// classes during tests and the extra checks will make us fail.
testClass.getDeclaredClasses();
testClass.getDeclaredMethods();
// we need to restrict access to some packages for some of the
// checks to occur.
String oldrestrictions = Security.getProperty("package.access");
Security.setProperty(
"package.access",
"gnu.testlet.java.lang.Class.");
try {
Permission[] noChecks = new Permission[] { };
Permission[] getClassLoader = new Permission[] {
new RuntimePermission("getClassLoader")};
Permission[] accessDeclaredMembers = new Permission[] {
new RuntimePermission("accessDeclaredMembers"),
new RuntimePermission(
"accessClassInPackage.gnu.testlet.java.lang.Class")};
Permission[] accessPublicMembers = new Permission[] {
new RuntimePermission(TestSecurityManager3.publicPerm),
new RuntimePermission(
"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 {
if (oldrestrictions != null)
{
Security.setProperty("package.access", oldrestrictions);
}
}
}
catch (Exception ex) {
harness.debug(ex);
harness.check(false, "Unexpected exception");
}
}
private void getMemberChecks(TestHarness harness, TestSecurityManager sm,
Class testClass, boolean declared,
Permission[] mustCheck)
{
int level;
// throwpoint: java.lang.Class-getClasses
// throwpoint: java.lang.Class-getDeclaredClasses
if (declared)
harness.checkPoint("getDeclaredClasses");
else
harness.checkPoint("getClasses");
try {
sm.prepareChecks(mustCheck);
if (declared)
testClass.getDeclaredClasses();
else
testClass.getClasses();
sm.checkAllChecked();
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getFields
// throwpoint: java.lang.Class-getDeclaredFields
if (declared)
harness.checkPoint("getDeclaredFields");
else
harness.checkPoint("getFields");
try {
sm.prepareChecks(mustCheck);
if (declared)
testClass.getDeclaredFields();
else
testClass.getFields();
sm.checkAllChecked();
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getMethods
// throwpoint: java.lang.Class-getDeclaredMethods
if (declared)
harness.checkPoint("getDeclaredMethods");
else
harness.checkPoint("getMethods");
try {
sm.prepareChecks(mustCheck);
if (declared)
testClass.getDeclaredMethods();
else
testClass.getMethods();
sm.checkAllChecked();
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getConstructors
// throwpoint: java.lang.Class-getDeclaredConstructors
if (declared)
harness.checkPoint("getDeclaredConstructors");
else
harness.checkPoint("getConstructors");
try {
sm.prepareChecks(mustCheck);
if (declared)
testClass.getDeclaredConstructors();
else
testClass.getConstructors();
sm.checkAllChecked();
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getField
// throwpoint: java.lang.Class-getDeclaredField
if (declared) {
harness.checkPoint("getDeclaredField");
level = 0;
}
else {
harness.checkPoint("getField");
level = 3;
}
try {
for (int i = 0; i < modifiers.length; i++) {
for (int j = 0; j < 5; j++) {
sm.prepareChecks(mustCheck);
boolean exists;
try {
String name = modifiers[i] + "field" + j;
if (declared)
testClass.getDeclaredField(name);
else
testClass.getField(name);
exists = true;
}
catch (NoSuchFieldException e) {
exists = false;
}
sm.checkAllChecked();
harness.check(exists == (j > level));
}
}
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getMethod
// throwpoint: java.lang.Class-getDeclaredMethod
if (declared)
harness.checkPoint("getDeclaredMethod");
else
harness.checkPoint("getMethod");
try {
for (int i = 0; i < modifiers.length; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < methodtypes.length; k++) {
sm.prepareChecks(mustCheck);
boolean exists;
try {
String name = modifiers[i] + "method" + j;
if (declared)
testClass.getDeclaredMethod(name, methodtypes[k]);
else
testClass.getMethod(name, methodtypes[k]);
exists = true;
}
catch (NoSuchMethodException e) {
exists = false;
}
sm.checkAllChecked();
harness.check(exists == (j > level && k > 0));
}
}
}
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
// throwpoint: java.lang.Class-getConstructor
// throwpoint: java.lang.Class-getDeclaredConstructor
if (declared) {
harness.checkPoint("getDeclaredConstructor");
level = 0;
}
else {
harness.checkPoint("getConstructor");
level = 6;
}
try {
for (int i = 0; i < constructortypes.length; i++) {
sm.prepareChecks(mustCheck);
boolean exists;
try {
if (declared)
testClass.getDeclaredConstructor(constructortypes[i]);
else
testClass.getConstructor(constructortypes[i]);
exists = true;
}
catch (NoSuchMethodException e) {
exists = false;
}
sm.checkAllChecked();
harness.check(exists == (i > level));
}
}
catch (SecurityException ex) {
harness.debug(ex);
harness.check(false, "unexpected check");
}
}
// Data tables for get{,Declared}{Field,Method,Constructor} checks
private static String[] modifiers = new String[] {"", "static"};
private static Class[][] methodtypes = new Class[][] {
new Class[] {short.class},
new Class[] {},
new Class[] {int.class},
new Class[] {String.class, boolean.class}};
private static Class[][] constructortypes = new Class[][] {
new Class[] {short.class},
new Class[] {int.class},
new Class[] {String.class, int.class},
new Class[] {int.class, int.class},
new Class[] {byte.class},
new Class[] {String.class},
new Class[] {int.class, String.class},
new Class[] {int.class, int.class, int.class},
new Class[] {}};
// Fields for getField and getDeclaredField checks
private boolean field1;
byte field2;
protected int field3;
public String field4;
private static boolean staticfield1;
static byte staticfield2;
protected static int staticfield3;
public static String staticfield4;
// Methods for getMethod and getDeclaredMethod checks
private void method1() {}
private void method1(int a) {}
private void method1(String b, boolean c) {}
char method2() { return 'a'; }
char method2(int a) { return 'b'; }
char method2(String b, boolean c) { return '1'; }
protected String method3() { return "x0x"; }
protected String method3(int a) { return "y"; }
protected String method3(String b, boolean c) { return "z"; }
public int method4() { return 1; }
public int method4(int a) { return 0; }
public int method4(String b, boolean c) { return -5; }
private static void staticmethod1() {}
private static void staticmethod1(int a) {}
private static void staticmethod1(String b, boolean c) {}
static char staticmethod2() { return 'a'; }
static char staticmethod2(int a) { return 'b'; }
static char staticmethod2(String b, boolean c) { return '1'; }
protected static String staticmethod3() { return "x0x"; }
protected static String staticmethod3(int a) { return "y"; }
protected static String staticmethod3(String b, boolean c) { return "z"; }
public static int staticmethod4() { return 1; }
public static int staticmethod4(int a) { return 0; }
public static int staticmethod4(String b, boolean c) { return -5; }
// Constructors for getConstructor and getDeclaredConstructor checks
private security(int a) {}
private security(String a, int b) {}
security(int a, int b) {}
security(byte a) {}
protected security(String a) {}
protected security(int a, String b) {}
public security(int a, int b, int c) {}
public security() {}
// The default implementation of SecurityManager.checkMemberAccess()
// checks no permissions if memberType is Member.PUBLIC. This class
// changes this, allowing us to test get{Field,Method,Constructor}.
// No other tests should use this class.
private static class TestSecurityManager3 extends TestSecurityManager
{
TestSecurityManager3(TestHarness harness)
{
super(harness);
}
static String publicPerm = "gnuAccessPublicMembers";
public void checkMemberAccess(Class c, int memberType)
{
if (c == null)
throw new NullPointerException();
if (memberType == Member.PUBLIC)
checkPermission(new RuntimePermission(publicPerm));
}
}
// Silence compiler warnings
public void shutUp()
{
field1 = staticfield1 = false;
new security(5).method1();
new security("hello", 5).method1(5);
method1("4", field1);
staticmethod1();
staticmethod1(5);
staticmethod1("4", staticfield1);
}
}
|