8139375: [TESTBUG] compiler/jvmci/SecurityRestrictionsTest checks are too tight
authortpivovarova
Thu, 15 Oct 2015 01:58:28 +0300
changeset 33183 1ee801e82dda
parent 33182 5dd8cb6f5431
child 33184 db796ab93ba7
8139375: [TESTBUG] compiler/jvmci/SecurityRestrictionsTest checks are too tight Reviewed-by: twisti, iignatyev
hotspot/test/compiler/jvmci/SecurityRestrictionsTest.java
hotspot/test/testlibrary/jdk/test/lib/Utils.java
--- a/hotspot/test/compiler/jvmci/SecurityRestrictionsTest.java	Tue Oct 13 16:21:25 2015 +0300
+++ b/hotspot/test/compiler/jvmci/SecurityRestrictionsTest.java	Thu Oct 15 01:58:28 2015 +0300
@@ -51,6 +51,8 @@
 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 @@
 
             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 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 @@
                 = "accessClassInPackage.jdk.vm.ci";
         private static final String JVMCI_SERVICES = "jvmciServices";
         private static final String JVMCI_PROP_START = "jvmci.";
+
     }
 }
--- a/hotspot/test/testlibrary/jdk/test/lib/Utils.java	Tue Oct 13 16:21:25 2015 +0300
+++ b/hotspot/test/testlibrary/jdk/test/lib/Utils.java	Thu Oct 15 01:58:28 2015 +0300
@@ -41,9 +41,9 @@
 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 @@
      * @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);
         }
     }