8139375: [TESTBUG] compiler/jvmci/SecurityRestrictionsTest checks are too tight

Reviewed-by: twisti, iignatyev
This commit is contained in:
Tatiana Pivovarova 2015-10-15 01:58:28 +03:00
parent 815be268ee
commit d3ad64d9d6
2 changed files with 60 additions and 21 deletions

View File

@ -51,6 +51,8 @@ import jdk.test.lib.Utils;
import java.lang.InternalError;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.PropertyPermission;
import java.util.function.Consumer;
public class SecurityRestrictionsTest {
@ -121,9 +123,12 @@ public class SecurityRestrictionsTest {
private boolean isJvmciPermission(Permission perm) {
String name = perm.getName();
return perm instanceof RuntimePermission
boolean isJvmciRuntime = perm instanceof RuntimePermission
&& (JVMCI_SERVICES.equals(name)
|| name.startsWith(JVMCI_RT_PERM_START));
|| name.startsWith(JVMCI_RT_PERM_START));
boolean isJvmciProperty = perm instanceof PropertyPermission
&& name.startsWith(JVMCI_PROP_START);
return isJvmciRuntime || isJvmciProperty;
}
@Override
@ -134,10 +139,32 @@ public class SecurityRestrictionsTest {
public void run() {
System.setSecurityManager(getSecurityManager());
Utils.runAndCheckException(
// to run CompilerToVM::<cinit> inside runAndCheckException
() -> new CompilerToVM(),
getExpectedException());
Consumer<Throwable> exceptionCheck = e -> {
if (e == null) {
if (getExpectedException() != null) {
String message = name() + ": Didn't get expected exception "
+ getExpectedException();
throw new AssertionError(message);
}
} else {
String message = name() + ": Got unexpected exception "
+ e.getClass().getSimpleName();
if (getExpectedException() == null){
throw new AssertionError(message, e);
}
Throwable t = e;
while (t.getCause() != null) {
t = t.getCause();
}
if (!getExpectedException().isAssignableFrom(t.getClass())) {
message += " instead of " + getExpectedException()
.getSimpleName();
throw new AssertionError(message, e);
}
}
};
Utils.runAndCheckException(CompilerToVM::new, exceptionCheck);
}
public SecurityManager getSecurityManager() {
@ -152,5 +179,6 @@ public class SecurityRestrictionsTest {
= "accessClassInPackage.jdk.vm.ci";
private static final String JVMCI_SERVICES = "jvmciServices";
private static final String JVMCI_PROP_START = "jvmci.";
}
}

View File

@ -41,9 +41,9 @@ import java.util.List;
import java.util.Random;
import java.util.function.BooleanSupplier;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import sun.misc.Unsafe;
/**
@ -480,23 +480,34 @@ public final class Utils {
* @param expectedException expected exception
*/
public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
boolean expectedExceptionWasNotThrown = false;
runAndCheckException(runnable, t -> {
if (t == null) {
if (expectedException != null) {
throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName());
}
} else {
String message = "Got unexpected exception " + t.getClass().getSimpleName();
if (expectedException == null) {
throw new AssertionError(message, t);
} else if (!expectedException.isAssignableFrom(t.getClass())) {
message += " instead of " + expectedException.getSimpleName();
throw new AssertionError(message, t);
}
}
});
}
/**
* Runs runnable and makes some checks to ensure that it throws expected exception.
* @param runnable what we run
* @param checkException a consumer which checks that we got expected exception and raises a new exception otherwise
*/
public static void runAndCheckException(Runnable runnable, Consumer<Throwable> checkException) {
try {
runnable.run();
if (expectedException != null) {
expectedExceptionWasNotThrown = true;
}
checkException.accept(null);
} catch (Throwable t) {
if (expectedException == null) {
throw new AssertionError("Got unexpected exception ", t);
}
if (!expectedException.isAssignableFrom(t.getClass())) {
throw new AssertionError(String.format("Got unexpected exception %s instead of %s",
t.getClass().getSimpleName(), expectedException.getSimpleName()), t);
}
}
if (expectedExceptionWasNotThrown) {
throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName());
checkException.accept(t);
}
}