1
0

[LOGGING-37] Add testcase when calling Thread.getCurrentThread().getContextClassLoader() results in a SecurityException.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@1606029 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart
2014-06-27 11:10:16 +00:00
parent 5b215c911a
commit 921e21bf9e
2 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.apache.commons.logging.security;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DummyClass {
public DummyClass() {
Log log = LogFactory.getLog(DummyClass.class);
log.info("Some log message");
}
}

View File

@@ -47,6 +47,7 @@ import org.apache.commons.logging.PathableTestSuite;
public class SecurityForbiddenTestCase extends TestCase
{
private SecurityManager oldSecMgr;
private ClassLoader otherClassLoader;
// Dummy special hashtable, so we can tell JCL to use this instead of
// the standard one.
@@ -75,6 +76,12 @@ public class SecurityForbiddenTestCase extends TestCase
public void setUp() {
// save security manager so it can be restored in tearDown
oldSecMgr = System.getSecurityManager();
PathableClassLoader classLoader = new PathableClassLoader(null);
classLoader.addLogicalLib("commons-logging");
classLoader.addLogicalLib("testclasses");
otherClassLoader = classLoader;
}
public void tearDown() {
@@ -131,4 +138,54 @@ public class SecurityForbiddenTestCase extends TestCase
fail("Unexpected exception:" + t.getMessage() + ":" + sw.toString());
}
}
/**
* Test what happens when JCL is run with absolutely no security
* privileges at all and a class loaded with a different classloader
* than the context classloader of the current thread tries to log something.
*/
public void testContextClassLoader() {
System.setProperty(
LogFactory.HASHTABLE_IMPLEMENTATION_PROPERTY,
CustomHashtable.class.getName());
MockSecurityManager mySecurityManager = new MockSecurityManager();
System.setSecurityManager(mySecurityManager);
try {
// load a dummy class with another classloader
// to force a SecurityException when the LogFactory calls
// Thread.getCurrentThread().getContextClassLoader()
loadClass("org.apache.commons.logging.security.DummyClass", otherClassLoader);
System.setSecurityManager(oldSecMgr);
assertEquals(0, mySecurityManager.getUntrustedCodeCount());
} catch(Throwable t) {
// Restore original security manager so output can be generated; the
// PrintWriter constructor tries to read the line.separator
// system property.
System.setSecurityManager(oldSecMgr);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
fail("Unexpected exception:" + t.getMessage() + ":" + sw.toString());
}
}
/**
* Loads a class with the given classloader.
*/
private Object loadClass(String name, ClassLoader classLoader) {
try {
Class clazz = classLoader.loadClass(name);
Object obj = clazz.newInstance();
return obj;
} catch ( Exception e ) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
fail("Unexpected exception:" + e.getMessage() + ":" + sw.toString());
}
return null;
}
}