Update of /cvsroot/mx4j/mx4j/src/test/test/javax/management
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3968/test/test/javax/management
Added Files:
SecurityManagerTestCase.java LocalSecurityManagerTest.java
Log Message:
Rewriting security tests based on a modifiable policy, for both remote and local tests
--- NEW FILE: SecurityManagerTestCase.java ---
/*
* Copyright (C) MX4J.
* All rights reserved.
*
* This software is distributed under the terms of the MX4J License version 1.0.
* See the terms of the MX4J License in the documentation provided with this software.
*/
package test.javax.management;
import java.security.Permission;
import java.util.Set;
import java.util.Iterator;
import javax.management.MBeanPermission;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import javax.management.AttributeList;
import javax.management.Attribute;
import javax.management.MBeanInfo;
import javax.management.MBeanTrustPermission;
import javax.management.QueryExp;
import javax.management.Query;
import test.MX4JTestCase;
/**
* Test case class that defines all tests to be performed by a security manager test
* both for the local (that is, MBeanServer tests) and the remote (that is, for MBeanServerConnection
* tests) case.
* It also defines the implementations of the test methods, leaving to subclasses
* only the creation of the MBeanServerConnection (or MBeanServer) object.
*
* @author <a href="mailto:[email protected]">Simone Bordet</a>
* @version $Revision: 1.1 $
*/
public abstract class SecurityManagerTestCase extends MX4JTestCase
{
public SecurityManagerTestCase(String s)
{
super(s);
}
/**
* Adds the given permission to the client codesource, that is the test codesource
* @see #resetPermissions
*/
protected abstract void addPermission(Permission p);
/**
* Removes all permissions add via {@link #addPermission}
*/
protected abstract void resetPermissions();
public abstract void testAddRemoveNotificationListener() throws Exception;
public abstract void testCreateMBean4Params() throws Exception;
public abstract void testCreateMBean5Params() throws Exception;
public abstract void testGetAttribute() throws Exception;
public abstract void testGetAttributes() throws Exception;
public abstract void testGetDefaultDomain() throws Exception;
public abstract void testGetDomains() throws Exception;
public abstract void testGetMBeanCount() throws Exception;
public abstract void testGetMBeanInfo() throws Exception;
public abstract void testGetObjectInstance() throws Exception;
public abstract void testInvoke() throws Exception;
public abstract void testIsInstanceOf() throws Exception;
public abstract void testIsRegistered() throws Exception;
public abstract void testQueryMBeans() throws Exception;
public abstract void testQueryNames() throws Exception;
public abstract void testSetAttribute() throws Exception;
public abstract void testSetAttributes() throws Exception;
public abstract void testUnregisterMBean() throws Exception;
protected void testAddRemoveNotificationListener(MBeanServerConnection server) throws Exception
{
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
}
};
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
try
{
server.addNotificationListener(delegate, listener, null, null);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "addNotificationListener"));
server.addNotificationListener(delegate, listener, null, null);
// Clean up
try
{
server.removeNotificationListener(delegate, listener);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "removeNotificationListener"));
server.removeNotificationListener(delegate, listener);
}
protected void testCreateMBean4Params(MBeanServerConnection server) throws Exception
{
// Needed to create an MLet, which is a ClassLoader
addPermission(new RuntimePermission("createClassLoader"));
ObjectName mletName = new ObjectName(":name=mlet");
String mletClassName = "javax.management.loading.MLet";
addPermission(new MBeanPermission(mletClassName + "[" + mletName.getCanonicalName() + "]", "instantiate, registerMBean"));
server.createMBean(mletClassName, mletName, null, null, null);
// Now we have something in the CLR
String mbeanClassName = "javax.management.MBeanServerDelegate";
ObjectName mbeanName = new ObjectName(":name=delegate");
try
{
server.createMBean(mbeanClassName, mbeanName);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(mbeanClassName + "[" + mbeanName.getCanonicalName() + "]", "instantiate, registerMBean"));
ObjectInstance result = server.createMBean(mbeanClassName, mbeanName, null, null);
assertNotNull(result);
}
protected void testCreateMBean5Params(MBeanServerConnection server) throws Exception
{
// Needed to create an MLet, which is a ClassLoader
addPermission(new RuntimePermission("createClassLoader"));
ObjectName mletName = new ObjectName(":name=mlet");
String mletClassName = "javax.management.loading.MLet";
try
{
server.createMBean(mletClassName, mletName, null);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(mletClassName + "[" + mletName.getCanonicalName() + "]", "instantiate, registerMBean"));
ObjectInstance result = server.createMBean(mletClassName, mletName, null, null, null);
assertNotNull(result);
}
protected void testGetAttribute(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
String attribute = "ImplementationName";
try
{
server.getAttribute(delegate, attribute);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("#" + attribute + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
String result = (String)server.getAttribute(delegate, attribute);
assertNotNull(result);
}
protected void testGetAttributes(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
String[] allAttributes = new String[]{"MBeanServerId", "ImplementationName", "ImplementationVendor", "ImplementationVersion", "SpecificationName", "SpecificationVendor", "SpecificationVersion"};
String[] allowed = new String[0];
String[] wanted = new String[0];
try
{
server.getAttributes(delegate, allAttributes);
fail();
}
catch (SecurityException ignored)
{
}
// Check that for wrong attribute I get an empty list
allowed = new String[]{allAttributes[1]};
wanted = new String[]{allAttributes[0]};
addPermission(new MBeanPermission("#" + allowed[0] + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
AttributeList list = server.getAttributes(delegate, wanted);
assertEquals(list.size(), 0);
// Check that for the right attribute I get it
resetPermissions();
allowed = new String[]{allAttributes[0]};
wanted = allAttributes;
addPermission(new MBeanPermission("#" + allowed[0] + "[" + delegate.getCanonicalName() + "]", "getAttribute"));
list = server.getAttributes(delegate, wanted);
assertEquals(list.size(), allowed.length);
Attribute attrib = (Attribute)list.get(0);
assertEquals(attrib.getName(), allowed[0]);
// Check that if I grant some I only get some
resetPermissions();
// Only attributes that start with 'Implementation'
allowed = new String[]{allAttributes[1], allAttributes[2], allAttributes[3]};
wanted = allAttributes;
addPermission(new MBeanPermission("#Implementation*[" + delegate.getCanonicalName() + "]", "getAttribute"));
list = server.getAttributes(delegate, wanted);
assertEquals(list.size(), allowed.length);
for (int i = 0; i < list.size(); ++i)
{
Attribute attr = (Attribute)list.get(i);
assertEquals(attr.getName(), allowed[i]);
}
// Check that if I grant all I get them all
resetPermissions();
allowed = allAttributes;
wanted = allAttributes;
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getAttribute"));
list = server.getAttributes(delegate, allAttributes);
assertEquals(list.size(), allowed.length);
for (int i = 0; i < list.size(); ++i)
{
Attribute attr = (Attribute)list.get(i);
assertEquals(attr.getName(), allowed[i]);
}
}
protected void testGetDefaultDomain(MBeanServerConnection server, String domain) throws Exception
{
String result = server.getDefaultDomain();
assertEquals(result, domain);
}
protected void testGetDomains(MBeanServerConnection server) throws Exception
{
String[] results = server.getDomains();
assertNotNull(results);
assertEquals(results.length, 1);
assertEquals(results[0], "JMImplementation");
}
protected void testGetMBeanCount(MBeanServerConnection server) throws Exception
{
Integer count = server.getMBeanCount();
assertNotNull(count);
assertTrue(count.intValue() > 1);
}
protected void testGetMBeanInfo(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
try
{
server.getMBeanInfo(delegate);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getMBeanInfo"));
MBeanInfo info = server.getMBeanInfo(delegate);
assertNotNull(info);
}
protected void testGetObjectInstance(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
try
{
server.getObjectInstance(delegate);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getObjectInstance"));
ObjectInstance instance = server.getObjectInstance(delegate);
assertNotNull(instance);
}
protected void testInvoke(MBeanServerConnection server) throws Exception
{
ObjectName mbeanName = new ObjectName(":mbean=simple");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), mbeanName, null);
addPermission(new MBeanPermission(Simple.class.getName(), "setAttribute"));
String initial = "mx4j";
server.setAttribute(mbeanName, new Attribute("FirstAttribute", initial));
String value = "simon";
String operation = "concatenateWithFirstAttribute";
try
{
server.invoke(mbeanName, operation, new Object[]{value}, new String[]{String.class.getName()});
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("#" + operation + "[" + mbeanName.getCanonicalName() + "]", "invoke"));
String result = (String)server.invoke(mbeanName, operation, new Object[]{value}, new String[]{String.class.getName()});
assertEquals(result, initial + value);
}
protected void testIsInstanceOf(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
String className = "javax.management.MBeanServerDelegateMBean";
try
{
server.isInstanceOf(delegate, className);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "isInstanceOf"));
boolean isInstance = server.isInstanceOf(delegate, className);
assertTrue(isInstance);
}
protected void testIsRegistered(MBeanServerConnection server) throws Exception
{
ObjectName delegate = new ObjectName("JMImplementation", "type", "MBeanServerDelegate");
boolean registered = server.isRegistered(delegate);
assertTrue(registered);
}
protected void testQueryMBeans(MBeanServerConnection server) throws Exception
{
ObjectName mbeanName = new ObjectName(":mbean=simple");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), mbeanName, null);
try
{
server.queryMBeans(null, null);
fail();
}
catch (SecurityException ignored)
{
}
// Check that an ObjectName that does not match returns an empty list
ObjectName dummy = new ObjectName(":type=dummy");
addPermission(new MBeanPermission("[" + dummy.getCanonicalName() + "]", "queryMBeans"));
Set mbeans = server.queryMBeans(null, null);
assertEquals(mbeans.size(), 0);
// Now check the right one
resetPermissions();
addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "queryMBeans"));
mbeans = server.queryMBeans(null, null);
assertEquals(mbeans.size(), 1);
ObjectInstance instance = (ObjectInstance)mbeans.iterator().next();
assertEquals(instance.getObjectName(), mbeanName);
// Check the right one for a pattern in permission
resetPermissions();
addPermission(new MBeanPermission("[:*]", "queryMBeans"));
mbeans = server.queryMBeans(null, null);
assertEquals(mbeans.size(), 1);
instance = (ObjectInstance)mbeans.iterator().next();
assertEquals(instance.getObjectName(), mbeanName);
// Check for another pattern
resetPermissions();
addPermission(new MBeanPermission("[JMImplementation:*]", "queryMBeans"));
mbeans = server.queryMBeans(null, null);
assertTrue(mbeans.size() >= 1);
for (Iterator iterator = mbeans.iterator(); iterator.hasNext();)
{
instance = (ObjectInstance)iterator.next();
assertEquals(instance.getObjectName().getDomain(), "JMImplementation");
}
// Check for all
resetPermissions();
addPermission(new MBeanPermission("*", "queryMBeans"));
// Try queryNames to see if the permission is implies
mbeans = server.queryNames(null, null);
assertEquals(mbeans.size(), server.getMBeanCount().intValue());
// Check for the query expression
resetPermissions();
String className = "mx4j.server.MX4JMBeanServerDelegate";
addPermission(new MBeanPermission(className, "queryMBeans"));
QueryExp exp = Query.eq(Query.attr(className, "ImplementationName"), Query.value("MX4J"));
mbeans = server.queryMBeans(null, exp);
assertEquals(mbeans.size(), 0);
// Now grant also the permission to retrieve the attribute
addPermission(new MBeanPermission(className, "getAttribute, getObjectInstance"));
mbeans = server.queryMBeans(null, exp);
assertEquals(mbeans.size(), 1);
instance = (ObjectInstance)mbeans.iterator().next();
assertEquals(instance.getClassName(), className);
}
protected void testQueryNames(MBeanServerConnection server) throws Exception
{
ObjectName mbeanName = new ObjectName(":mbean=simple");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), mbeanName, null);
try
{
server.queryNames(null, null);
fail();
}
catch (SecurityException ignored)
{
}
// Check that an ObjectName that does not match returns an empty list
ObjectName dummy = new ObjectName(":type=dummy");
addPermission(new MBeanPermission("[" + dummy.getCanonicalName() + "]", "queryNames"));
Set mbeans = server.queryNames(null, null);
assertEquals(mbeans.size(), 0);
// Now check the right one
resetPermissions();
addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "queryNames"));
mbeans = server.queryNames(null, null);
assertEquals(mbeans.size(), 1);
ObjectName name = (ObjectName)mbeans.iterator().next();
assertEquals(name, mbeanName);
// Check the right one for a pattern in permission
resetPermissions();
addPermission(new MBeanPermission("[:*]", "queryNames"));
mbeans = server.queryNames(null, null);
assertEquals(mbeans.size(), 1);
name = (ObjectName)mbeans.iterator().next();
assertEquals(name, mbeanName);
// Check for another pattern
resetPermissions();
addPermission(new MBeanPermission("[JMImplementation:*]", "queryNames"));
mbeans = server.queryNames(null, null);
assertTrue(mbeans.size() >= 1);
for (Iterator iterator = mbeans.iterator(); iterator.hasNext();)
{
name = (ObjectName)iterator.next();
assertEquals(name.getDomain(), "JMImplementation");
}
// Check for all
resetPermissions();
addPermission(new MBeanPermission("*", "queryNames"));
mbeans = server.queryNames(null, null);
assertEquals(mbeans.size(), server.getMBeanCount().intValue());
// Check for the query expression
resetPermissions();
String className = "mx4j.server.MX4JMBeanServerDelegate";
addPermission(new MBeanPermission(className, "queryNames"));
QueryExp exp = Query.eq(Query.attr(className, "ImplementationName"), Query.value("MX4J"));
mbeans = server.queryNames(null, exp);
assertEquals(mbeans.size(), 0);
// Now grant also the permission to retrieve the attribute
addPermission(new MBeanPermission(className, "getAttribute, getObjectInstance"));
mbeans = server.queryNames(null, exp);
assertEquals(mbeans.size(), 1);
name = (ObjectName)mbeans.iterator().next();
assertEquals(name, new ObjectName("JMImplementation", "type", "MBeanServerDelegate"));
}
protected void testSetAttribute(MBeanServerConnection server) throws Exception
{
ObjectName name = new ObjectName(":name=test");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), name, null);
String attributeName = "FirstAttribute";
String value = "first";
Attribute attribute = new Attribute(attributeName, value);
try
{
server.setAttribute(name, attribute);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission("#" + attributeName + "[" + name.getCanonicalName() + "]", "setAttribute"));
server.setAttribute(name, attribute);
// Check that it worked
addPermission(new MBeanPermission("#" + attributeName, "getAttribute"));
String result = (String)server.getAttribute(name, attributeName);
assertEquals(result, value);
}
protected void testSetAttributes(MBeanServerConnection server) throws Exception
{
ObjectName mbeanName = new ObjectName(":mbean=simple");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), mbeanName, null);
AttributeList allAttributes = new AttributeList();
allAttributes.add(new Attribute("FirstAttribute", "first"));
allAttributes.add(new Attribute("SecondAttribute", "second"));
allAttributes.add(new Attribute("ThirdAttribute", "third"));
allAttributes.add(new Attribute("Running", Boolean.TRUE));
AttributeList allowed = new AttributeList();
AttributeList wanted = new AttributeList();
try
{
server.setAttributes(mbeanName, allAttributes);
fail();
}
catch (SecurityException ignored)
{
}
// Check that for wrong attribute I get an empty list
allowed.clear();
allowed.add(allAttributes.get(0));
wanted.clear();
wanted.add(allAttributes.get(1));
addPermission(new MBeanPermission("#" + ((Attribute)allowed.get(0)).getName() + "[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
AttributeList list = server.setAttributes(mbeanName, wanted);
assertEquals(list.size(), 0);
// Check that for the right attribute I get it
resetPermissions();
allowed.clear();
allowed.add(allAttributes.get(0));
wanted.clear();
wanted.addAll(allAttributes);
addPermission(new MBeanPermission("#" + ((Attribute)allowed.get(0)).getName() + "[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
list = server.setAttributes(mbeanName, wanted);
assertEquals(list.size(), allowed.size());
Attribute attrib = (Attribute)list.get(0);
assertEquals(attrib, allowed.get(0));
// Check that if I grant some I only get some
resetPermissions();
// Only attributes that ends with 'Attribute'
allowed.clear();
allowed.add(allAttributes.get(0));
allowed.add(allAttributes.get(1));
allowed.add(allAttributes.get(2));
wanted.clear();
wanted.addAll(allAttributes);
addPermission(new MBeanPermission("#*Attribute[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
list = server.setAttributes(mbeanName, wanted);
assertEquals(list.size(), allowed.size());
for (int i = 0; i < list.size(); ++i)
{
Attribute attr = (Attribute)list.get(i);
assertEquals(attr, allowed.get(i));
}
// Check that if I grant all I get them all
resetPermissions();
allowed.clear();
allowed.addAll(allAttributes);
wanted.clear();
wanted.addAll(allAttributes);
addPermission(new MBeanPermission("[" + mbeanName.getCanonicalName() + "]", "setAttribute"));
list = server.setAttributes(mbeanName, wanted);
assertEquals(list.size(), allowed.size());
for (int i = 0; i < list.size(); ++i)
{
Attribute attr = (Attribute)list.get(i);
assertEquals(attr, allowed.get(i));
}
}
protected void testUnregisterMBean(MBeanServerConnection server) throws Exception
{
ObjectName name = new ObjectName(":name=test");
addPermission(new MBeanPermission(Simple.class.getName(), "instantiate, registerMBean"));
addPermission(new MBeanTrustPermission("register"));
server.createMBean(Simple.class.getName(), name, null);
try
{
server.unregisterMBean(name);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(Simple.class.getName() + "[" + name.getCanonicalName() + "]", "unregisterMBean"));
server.unregisterMBean(name);
}
public interface SimpleMBean
{
public void setFirstAttribute(String value);
public String getFirstAttribute();
public void setSecondAttribute(String value);
public String getSecondAttribute();
public void setThirdAttribute(String value);
public String getThirdAttribute();
public void setRunning(boolean value);
public boolean isRunning();
public String concatenateWithFirstAttribute(String value);
}
public static class Simple implements SimpleMBean
{
private String firstAttribute;
private String secondAttribute;
private String thirdAttribute;
private boolean running;
public String getFirstAttribute()
{
return firstAttribute;
}
public void setFirstAttribute(String value)
{
this.firstAttribute = value;
}
public String getSecondAttribute()
{
return secondAttribute;
}
public void setSecondAttribute(String secondAttribute)
{
this.secondAttribute = secondAttribute;
}
public String getThirdAttribute()
{
return thirdAttribute;
}
public void setThirdAttribute(String thirdAttribute)
{
this.thirdAttribute = thirdAttribute;
}
public boolean isRunning()
{
return running;
}
public void setRunning(boolean running)
{
this.running = running;
}
public String concatenateWithFirstAttribute(String value)
{
return this.firstAttribute + value;
}
}
}
--- NEW FILE: LocalSecurityManagerTest.java ---
/*
* Copyright (C) MX4J.
* All rights reserved.
*
* This software is distributed under the terms of the MX4J License version 1.0.
* See the terms of the MX4J License in the documentation provided with this software.
*/
package test.javax.management;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.security.SecurityPermission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanPermission;
import javax.management.MBeanServer;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerPermission;
import javax.management.ObjectName;
import javax.management.loading.ClassLoaderRepository;
import javax.management.loading.MLet;
import junit.framework.TestCase;
import mx4j.server.MX4JMBeanServer;
/**
* @author <a href="mailto:[email protected]">Simone Bordet</a>
* @version $Revision: 1.1 $
*/
public class LocalSecurityManagerTest extends SecurityManagerTestCase
{
static
{
// For the way JUnit works, we have one JVM per test class
Policy.setPolicy(new LocalModifiablePolicy());
System.setSecurityManager(new SecurityManager());
}
public LocalSecurityManagerTest(String s)
{
super(s);
}
protected void setUp() throws Exception
{
// Be sure we have a security manager and the right policy
SecurityManager sm = System.getSecurityManager();
if (sm == null) fail();
Policy policy = Policy.getPolicy();
if (!(policy instanceof LocalModifiablePolicy)) fail();
((LocalModifiablePolicy)policy).initialize();
}
protected void addPermission(Permission p)
{
LocalModifiablePolicy policy = (LocalModifiablePolicy)Policy.getPolicy();
policy.addPermission(p);
}
protected void resetPermissions()
{
LocalModifiablePolicy policy = (LocalModifiablePolicy)Policy.getPolicy();
policy.initialize();
}
public void testNewMBeanServer() throws Exception
{
try
{
MBeanServerFactory.newMBeanServer();
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServerFactory.newMBeanServer();
try
{
MBeanServerFactory.createMBeanServer();
fail();
}
catch (SecurityException ignored)
{
}
}
public void testCreateMBeanServer() throws Exception
{
try
{
MBeanServerFactory.createMBeanServer();
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanServerPermission("createMBeanServer"));
MBeanServer server = MBeanServerFactory.createMBeanServer();
MBeanServerFactory.newMBeanServer();
try
{
MBeanServerFactory.releaseMBeanServer(server);
fail();
}
catch (SecurityException ignored)
{
}
// Clean up
addPermission(new MBeanServerPermission("releaseMBeanServer"));
MBeanServerFactory.releaseMBeanServer(server);
}
public void testReleaseMBeanServer() throws Exception
{
addPermission(new MBeanServerPermission("createMBeanServer"));
MBeanServer server = MBeanServerFactory.createMBeanServer();
try
{
MBeanServerFactory.releaseMBeanServer(server);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanServerPermission("releaseMBeanServer"));
MBeanServerFactory.releaseMBeanServer(server);
}
public void testReleaseMBeanServer2() throws Exception
{
addPermission(new MBeanServerPermission("createMBeanServer, releaseMBeanServer"));
MBeanServer server = MBeanServerFactory.createMBeanServer();
MBeanServerFactory.releaseMBeanServer(server);
}
public void testFindMBeanServer() throws Exception
{
addPermission(new MBeanServerPermission("createMBeanServer"));
MBeanServer server = MBeanServerFactory.createMBeanServer();
try
{
MBeanServerFactory.findMBeanServer(null);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanServerPermission("findMBeanServer"));
ArrayList list = MBeanServerFactory.findMBeanServer(null);
if (!list.contains(server)) fail();
// Clean up
addPermission(new MBeanServerPermission("releaseMBeanServer"));
MBeanServerFactory.releaseMBeanServer(server);
}
public void testAddRemoveNotificationListener() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testAddRemoveNotificationListener(server);
}
public void testCreateMBean4Params() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testCreateMBean4Params(server);
}
public void testCreateMBean5Params() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testCreateMBean5Params(server);
}
public void testGetAttribute() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetAttribute(server);
}
public void testGetAttributes() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetAttributes(server);
}
public void testGetDefaultDomain() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
String domain = "simon";
MBeanServer server = MBeanServerFactory.newMBeanServer(domain);
testGetDefaultDomain(server, domain);
}
public void testGetDomains() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetDomains(server);
}
public void testGetMBeanCount() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetMBeanCount(server);
}
public void testGetMBeanInfo() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetMBeanInfo(server);
}
public void testGetObjectInstance() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testGetObjectInstance(server);
}
public void testInstantiate() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
String className = "java.lang.String";
try
{
server.instantiate(className, null);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(className, "instantiate"));
server.instantiate(className, null);
// Check also the overloaded version, we need an MLet
String mletClassName = "javax.management.loading.MLet";
ObjectName name = new ObjectName(":mbean=mlet");
resetPermissions();
addPermission(new MBeanPermission(mletClassName, "instantiate, registerMBean"));
addPermission(new RuntimePermission("createClassLoader"));
server.createMBean(mletClassName, name, null);
try
{
server.instantiate(className, null);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(className, "instantiate"));
server.instantiate(className, null);
}
public void testInvoke() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testInvoke(server);
}
public void testIsInstanceOf() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testIsInstanceOf(server);
}
public void testIsRegistered() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testIsRegistered(server);
}
public void testQueryMBeans() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testQueryMBeans(server);
}
public void testQueryNames() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testQueryNames(server);
}
public void testRegisterMBean() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
MBeanServerDelegate mbean = new MBeanServerDelegate();
ObjectName name = new ObjectName(":name=test");
try
{
server.registerMBean(mbean, name);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(mbean.getClass().getName() + "[" + name.getCanonicalName() + "]", "registerMBean"));
server.registerMBean(mbean, name);
}
public void testSetAttribute() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testSetAttribute(server);
}
public void testSetAttributes() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testSetAttributes(server);
}
public void testUnregisterMBean() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
testUnregisterMBean(server);
}
public void testGetClassLoader() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
// Needed to create an MLet, which is a ClassLoader
addPermission(new RuntimePermission("createClassLoader"));
ObjectName name = new ObjectName(":mbean=mlet");
MLet mlet = new MLet();
addPermission(new MBeanPermission(mlet.getClass().getName(), "registerMBean"));
server.registerMBean(mlet, name);
try
{
server.getClassLoader(null);
fail();
}
catch (SecurityException ignored)
{
}
// Dummy class
addPermission(new MBeanPermission("foo[" + name.getCanonicalName() + "]","getClassLoader"));
try
{
server.getClassLoader(name);
fail();
}
catch (SecurityException ignored)
{
}
addPermission(new MBeanPermission(mlet.getClass().getName() + "[" + name.getCanonicalName() + "]","getClassLoader"));
ClassLoader result = server.getClassLoader(name);
assertSame(result, mlet);
}
public void testGetClassLoaderFor() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
ObjectName delegate = ObjectName.getInstance("JMImplementation", "type", "MBeanServerDelegate");
try
{
server.getClassLoaderFor(delegate);
fail();
}
catch (SecurityException x)
{
}
addPermission(new MBeanPermission("[" + delegate.getCanonicalName() + "]", "getClassLoaderFor"));
ClassLoader loader = server.getClassLoaderFor(delegate);
assertNotNull(loader);
}
public void testGetClassLoaderRepository() throws Exception
{
addPermission(new MBeanServerPermission("newMBeanServer"));
MBeanServer server = MBeanServerFactory.newMBeanServer();
try
{
server.getClassLoaderRepository();
fail();
}
catch (SecurityException x)
{
}
addPermission(new MBeanPermission("*", "getClassLoaderRepository"));
ClassLoaderRepository loader = server.getClassLoaderRepository();
assertNotNull(loader);
}
/**
* A modifiable policy that allow permissions to be added at runtime, used for tests purposes only.
*/
public static class LocalModifiablePolicy extends Policy
{
private final ProtectionDomain testDomain;
private final Map permissionsMap = new HashMap();
public LocalModifiablePolicy()
{
// Here we still have no security manager installed
testDomain = LocalModifiablePolicy.class.getProtectionDomain();
// Add the permissions needed to run the tests
CodeSource junitCodeSource = TestCase.class.getProtectionDomain().getCodeSource();
permissionsMap.put(junitCodeSource, createAllPermissions());
CodeSource mx4jCodeSource = MBeanServerFactory.class.getProtectionDomain().getCodeSource();
permissionsMap.put(mx4jCodeSource, createAllPermissions());
CodeSource implCodeSource = MX4JMBeanServer.class.getProtectionDomain().getCodeSource();
permissionsMap.put(implCodeSource, createAllPermissions());
ClassLoader loader = getClass().getClassLoader();
// BCEL
try
{
Class cls = loader.loadClass("org.apache.bcel.generic.Type");
CodeSource bcelCodeSource = cls.getProtectionDomain().getCodeSource();
permissionsMap.put(bcelCodeSource, createAllPermissions());
}
catch (ClassNotFoundException ignored)
{
}
// When we run automated, we need also permissions for Ant jars
try
{
Class cls = loader.loadClass("org.apache.tools.ant.Task");
CodeSource antCodeSource = cls.getProtectionDomain().getCodeSource();
permissionsMap.put(antCodeSource, createAllPermissions());
cls = loader.loadClass("org.apache.tools.ant.taskdefs.optional.junit.JUnitTask");
antCodeSource = cls.getProtectionDomain().getCodeSource();
permissionsMap.put(antCodeSource, createAllPermissions());
}
catch (ClassNotFoundException ignored)
{
}
initialize();
}
private Permissions createAllPermissions()
{
Permissions allPermissions = new Permissions();
allPermissions.add(new AllPermission());
return allPermissions;
}
public PermissionCollection getPermissions(CodeSource codesource)
{
Permissions permissions = (Permissions)permissionsMap.get(codesource);
if (permissions == null)
{
permissions = new Permissions();
permissionsMap.put(codesource, permissions);
}
return permissions;
}
public void refresh()
{
}
/**
* For JDK 1.4 overriding this method disables caching of Permissions done by the
* standard Policy implementation.
* This is done because we install this policy *before* installing the security manager.
* By doing so, in JDK 1.4 the permissions granted at the moment of policy installation
* (no security manager == AllPermission) are cached and will invalidate all tests, since
* they will become unmodifiable.
*
* The stack trace is when checking a permission is:
*
* SecurityManager.checkPermission()
* AccessController.checkPermission()
* AccessControlContext.checkPermission()
* ProtectionDomain.implies()
* LocalModifiablePolicy.implies()
*/
public boolean implies(ProtectionDomain domain, Permission permission)
{
PermissionCollection perms = getPermissions(domain.getCodeSource());
return perms.implies(permission);
}
/**
* Adds the given permission to the client (the test in this case) codesource
*/
public void addPermission(Permission p)
{
Permissions permissions = (Permissions)getPermissions(testDomain.getCodeSource());
permissions.add(p);
}
/**
* Initializes the permissions for the client (the test in this case) codesource
*/
public synchronized void initialize()
{
Permissions permissions = new Permissions();
permissions.add(new SecurityPermission("getPolicy"));
permissionsMap.put(testDomain.getCodeSource(), permissions);
}
}
}
-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.