Skip to content

Commit

Permalink
Remove SecurityManager references from logging package #16
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens <[email protected]>
  • Loading branch information
jmehrens committed Jan 14, 2024
1 parent 6ee9fec commit 576e4cc
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 808 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ final void testLogManagerModifiers(final Class<?> c) throws Exception {
/**
* Checks that the given class is not dependent on the
* {@code javax.annotation} classes as they are not present in all
* environments.
* environments and were present in JDK.
*
* @param k the class to inspect.
* @throws Exception if there is a problem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.UndeclaredThrowableException;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
Expand All @@ -47,14 +48,14 @@
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.LoggingPermission;
import java.util.logging.SimpleFormatter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -70,11 +71,6 @@ public class LogManagerPropertiesTest extends AbstractLogging {
*/
private final static ThreadLocal<Throwable> PENDING = new ThreadLocal<>();

@BeforeClass
public static void setUpClass() throws Exception {
Assert.assertNull(System.getSecurityManager());
}

private static void fullFence() {
LogManager.getLogManager().getProperty("");
}
Expand All @@ -96,33 +92,102 @@ public void testDeclaredClasses() throws Exception {
}

@Test
public void testCheckAccessPresent() {
LogManager m = LogManager.getLogManager();
m.checkAccess();
LogManagerProperties.checkLogManagerAccess();

LogPermSecurityManager sm = new LogPermSecurityManager();
sm.secure = false;
System.setSecurityManager(sm);
public void testCheckLogManagerAccess() {
try {
sm.secure = true;
try {
m.checkAccess();
fail(m.toString());
} catch (SecurityException expect) {
LogManagerProperties.checkLogManagerAccess();
//okay if we return normally, this is a weak test.
} catch (SecurityException allowed) {
}
}

private static boolean isAccessController() {
for (StackTraceElement frame : new Throwable().getStackTrace()) {
if ("invokeAccessController".equals(frame.getMethodName())
&& LogManagerProperties.class.getName().equals(frame.getClassName())) {
return true;
}
}
return false;
}

try {
LogManagerProperties.checkLogManagerAccess();
fail(LogManagerProperties.class.getName());
} catch (SecurityException expect) {
@Test
public void testRun() {
PrivilegedAction<Boolean> p = () -> {
assertFalse(isAccessController());
return true;
};
assertTrue(LogManagerProperties.runOrDoPrivileged(p));
}

@Test
public void testDoPrivileged() {
PrivilegedAction<Boolean> p = () -> {
if (isAccessController()) {
return true;
}
} finally {
sm.secure = false;
System.setSecurityManager((SecurityManager) null);
throw new SecurityException();
};

try {
assertTrue(LogManagerProperties.runOrDoPrivileged(p));
} catch (UndeclaredThrowableException removed) {
assertTrue(removed.getCause() instanceof ClassNotFoundException);
} catch (UnsupportedOperationException terminal) {
}
}

@Test
public void testDoPrivilegedRuntimeException() {
RuntimeException cause = new RuntimeException();
PrivilegedAction<Boolean> p = () -> {
if (isAccessController()) {
throw cause;
}
throw new SecurityException();
};

boolean ran = false;
try {
LogManagerProperties.runOrDoPrivileged(p);
ran = true;
} catch (UndeclaredThrowableException removed) {
assertTrue(removed.getCause() instanceof ClassNotFoundException);
} catch (UnsupportedOperationException terminal) {
} catch (RuntimeException re) {
assertSame(re, cause);
}
assertFalse(ran);
}

@Test
public void testDoPrivilegedError() {
Error cause = new Error();
PrivilegedAction<Boolean> p = () -> {
if (isAccessController()) {
throw cause;
}
throw new SecurityException();
};

boolean ran = false;
try {
LogManagerProperties.runOrDoPrivileged(p);
ran = true;
} catch (UndeclaredThrowableException removed) {
assertTrue(removed.getCause() instanceof ClassNotFoundException);
} catch (UnsupportedOperationException terminal) {
} catch (Error e) {
assertSame(e, cause);
}
assertFalse(ran);
}

@Test(expected=NullPointerException.class)
public void testRunOrDoPrivilegedNull() {
LogManagerProperties.runOrDoPrivileged((PrivilegedAction<?>) null);
}


@Test
public void testFromLogManagerPresent() throws Exception {
String prefix = LogManagerPropertiesTest.class.getName();
Expand Down Expand Up @@ -1319,38 +1384,4 @@ public String getLocalHost() {
throw new SecurityException();
}
}

private static final class LogPermSecurityManager extends SecurityManager {

volatile boolean secure = false;

LogPermSecurityManager() {
}

@Override
public void checkPermission(java.security.Permission perm) {
try { //Call super class always for java.security.debug tracing.
super.checkPermission(perm);
checkPermission(perm, new SecurityException(perm.toString()));
} catch (SecurityException se) {
checkPermission(perm, se);
}
}

@Override
public void checkPermission(java.security.Permission perm, Object context) {
try { //Call super class always for java.security.debug tracing.
super.checkPermission(perm, context);
checkPermission(perm, new SecurityException(perm.toString()));
} catch (SecurityException se) {
checkPermission(perm, se);
}
}

private void checkPermission(java.security.Permission perm, SecurityException se) {
if (secure && perm instanceof LoggingPermission) {
throw se;
}
}
}
}
Loading

0 comments on commit 576e4cc

Please sign in to comment.