8139375: [TESTBUG] compiler/jvmci/SecurityRestrictionsTest checks are too tight
Reviewed-by: twisti, iignatyev
This commit is contained in:
parent
815be268ee
commit
d3ad64d9d6
@ -51,6 +51,8 @@ import jdk.test.lib.Utils;
|
|||||||
import java.lang.InternalError;
|
import java.lang.InternalError;
|
||||||
import java.security.AccessControlException;
|
import java.security.AccessControlException;
|
||||||
import java.security.Permission;
|
import java.security.Permission;
|
||||||
|
import java.util.PropertyPermission;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class SecurityRestrictionsTest {
|
public class SecurityRestrictionsTest {
|
||||||
|
|
||||||
@ -121,9 +123,12 @@ public class SecurityRestrictionsTest {
|
|||||||
|
|
||||||
private boolean isJvmciPermission(Permission perm) {
|
private boolean isJvmciPermission(Permission perm) {
|
||||||
String name = perm.getName();
|
String name = perm.getName();
|
||||||
return perm instanceof RuntimePermission
|
boolean isJvmciRuntime = perm instanceof RuntimePermission
|
||||||
&& (JVMCI_SERVICES.equals(name)
|
&& (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
|
@Override
|
||||||
@ -134,10 +139,32 @@ public class SecurityRestrictionsTest {
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
System.setSecurityManager(getSecurityManager());
|
System.setSecurityManager(getSecurityManager());
|
||||||
Utils.runAndCheckException(
|
Consumer<Throwable> exceptionCheck = e -> {
|
||||||
// to run CompilerToVM::<cinit> inside runAndCheckException
|
if (e == null) {
|
||||||
() -> new CompilerToVM(),
|
if (getExpectedException() != null) {
|
||||||
getExpectedException());
|
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() {
|
public SecurityManager getSecurityManager() {
|
||||||
@ -152,5 +179,6 @@ public class SecurityRestrictionsTest {
|
|||||||
= "accessClassInPackage.jdk.vm.ci";
|
= "accessClassInPackage.jdk.vm.ci";
|
||||||
private static final String JVMCI_SERVICES = "jvmciServices";
|
private static final String JVMCI_SERVICES = "jvmciServices";
|
||||||
private static final String JVMCI_PROP_START = "jvmci.";
|
private static final String JVMCI_PROP_START = "jvmci.";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,9 +41,9 @@ import java.util.List;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.function.BooleanSupplier;
|
import java.util.function.BooleanSupplier;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import sun.misc.Unsafe;
|
import sun.misc.Unsafe;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -480,23 +480,34 @@ public final class Utils {
|
|||||||
* @param expectedException expected exception
|
* @param expectedException expected exception
|
||||||
*/
|
*/
|
||||||
public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
|
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 {
|
try {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
if (expectedException != null) {
|
checkException.accept(null);
|
||||||
expectedExceptionWasNotThrown = true;
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (expectedException == null) {
|
checkException.accept(t);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user