/** * Asserts that no exceptions are thrown when an action which is not restricted by {@link SecurityManager} is executed under * {@link SecurityManagerHelper#doPrivileged(PrivilegedAction)}. * This test behaves similarly whether {@link SecurityManager} is enabled or not. */ @Test public void testDoPrivilegedWhenNoPermissionsUnrestrictedActionDoesNotThrow() { testDoPrivileged(null, new PrivilegedAction<Void>() { @Override public Void run() { /* * Any non-security manager related operations can do. * The simplest operation that could be done is String.length() (nothing particular just some arbitrary operation) */ "".length(); return null; } }); }
/** * Asserts that no exception is thrown when a security manager restricted action is executed under * {@link SecurityManagerHelper#doPrivileged(PrivilegedAction, Collection)} and the permission given matches the action being executed. * This test behaves similarly whether {@link SecurityManager} is enabled or not. */ @Test public void testDoPrivilegedWhenPermissionMatches() { testDoPrivileged(null, new PrivilegedAction<Void>() { @Override public Void run() { System.getProperty("test"); return null; } }, Arrays.<Permission> asList(new PropertyPermission("test", "read"))); }
/** * When {@link org.finra.herd.core.helper.SecurityManagerHelper#doPrivileged(PrivilegedAction)} is called which executes an action restricted by security manager, the method should * throw an {@link AccessControlException}. * If SecurityManager is disabled, this test asserts that no exception was thrown. */ @Test public void testDoPrivilegedWhenNoPermissionsRestrictedActionThrows() { Class<? extends Exception> expectedExceptionType = null; if (SecurityManagerHelper.isSecurityManagerEnabled()) { expectedExceptionType = AccessControlException.class; } testDoPrivileged(expectedExceptionType, new PrivilegedAction<Void>() { @Override public Void run() { System.getProperty("test"); return null; } }); }
/** * Asserts that an {@link AccessControlException} is thrown when a SecurityManager restricted action is taken under * {@link SecurityManagerHelper#doPrivileged(PrivilegedAction, Collection)} where a permission is given, but it does not match the action that is being * executed. * This test asserts that no exception is thrown when {@link SecurityManager} is disabled. */ @Test public void testDoPrivilegedWhenNoMatchingPermission() { Class<? extends Exception> expectedException = null; if (SecurityManagerHelper.isSecurityManagerEnabled()) { expectedException = AccessControlException.class; } testDoPrivileged(expectedException, new PrivilegedAction<Void>() { @Override public Void run() { System.getProperty("test"); return null; } }, Arrays.<Permission> asList(new PropertyPermission("anotherValue", "read"))); }