Skip to content

Commit

Permalink
junit5: add property to allow SecurityManager override in tests (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalvador authored Nov 10, 2023
1 parent a6ec44e commit 3d8dc43
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ you are using the standard `@maven` namespace for your
**Note**: The junit5 runner prevents `System.exit` being called
using a `SecurityManager`, which means that one test can't
prematurely cause an entire test run to finish unexpectedly.
This security measure prohibits tests from setting their own `SecurityManager`.
To override this, set the `bazel.junit5runner.allowSettingSecurityManager` system property.

While the `SecurityManager` has been deprecated in recent Java
releases, there's no replacement yet. JEP 411 has this as one of
Expand Down
2 changes: 2 additions & 0 deletions java/private/junit5.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def java_junit5_test(
**Note**: The junit5 runner prevents `System.exit` being called
using a `SecurityManager`, which means that one test can't
prematurely cause an entire test run to finish unexpectedly.
This security measure prohibits tests from setting their own `SecurityManager`.
To override this, set the `bazel.junit5runner.allowSettingSecurityManager` system property.
While the `SecurityManager` has been deprecated in recent Java
releases, there's no replacement yet. JEP 411 has this as one of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public void checkPermission(Permission perm) {
if (allowExitCall) {
return;
}
throw new SecurityException("Replacing the security manager is not allowed");
if (System.getProperty("bazel.junit5runner.allowSettingSecurityManager") != null) {
System.err.println(
"Warning: junit runner security manager replaced, calls to System.exit will not be"
+ " blocked");
} else {
throw new SecurityException("Replacing the security manager is not allowed");
}
}

if (delegateSecurityManager != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.bazel_contrib.contrib_rules_jvm.junit5;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.security.Permission;
import org.junit.jupiter.api.Test;

@SuppressFBWarnings("THROWS_METHOD_THROWS_CLAUSE_THROWABLE")
public class SecurityManagerSettingTest {

private static final String ALLOW_SETTING_SECURITY_MANAGER_PROPERTY =
"bazel.junit5runner.allowSettingSecurityManager";

@Test
void testCanSetSecurityManagerWhenPropertyIsTrue() {
System.setProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY, "true");
SecurityManager originalSecurityManager = System.getSecurityManager();
SecurityManager testSecurityManager =
new SecurityManager() {
@Override
public void checkPermission(Permission perm) {}
};

try {
System.setSecurityManager(testSecurityManager);
assertEquals(testSecurityManager, System.getSecurityManager());
} finally {
System.setSecurityManager(originalSecurityManager);
System.clearProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY);
}
}

@Test
void testCannotSetSecurityManagerWhenPropertyIsNotSet() {
assertNull(System.getProperty(ALLOW_SETTING_SECURITY_MANAGER_PROPERTY));
assertThrows(SecurityException.class, () -> System.setSecurityManager(new SecurityManager()));
}
}

0 comments on commit 3d8dc43

Please sign in to comment.