public class IsTrustedObjectTest extends AbstractTrustVerifierTest {
//inherit javadoc
public void run() throws Exception {
//Verify that an object exported by BasicJeriExporter is trusted
BasicJeriTrustVerifier verifier = new BasicJeriTrustVerifier();
Integer port = new Integer(getStringValue("listenPort"));
BasicJeriExporter exporter = new BasicJeriExporter(
TcpServerEndpoint.getInstance(port.intValue()),
new BasicILFactory());
Remote stub = exporter.export(new TestServiceImpl());
exporter.unexport(true);
ClassLoader loader = Thread.currentThread().getContextClassLoader();
TestTrustVerifierCtx ctx = new TestTrustVerifierCtx(
true, loader);
if (!verifier.isTrustedObject(stub, ctx)){
throw new TestException("Returned false for an"
+ " object that should be trusted: " + stub);
}
//Verify that a proxy object without BasicInvocationHandler is
//not trusted
Object proxy = Proxy.newProxyInstance(loader,new Class[]{},
new InvocationHandler() {
public Object invoke(Object proxy, Method method,
Object[] args) {
return null;
}
});
if (verifier.isTrustedObject(proxy,ctx)){
throw new TestException("Verifier trusts an "
+ " untrusted object.");
}
//Verify that using a classloader outside the object's classloader
//hierearchy causes the object to not be trusted.
String jarPath = sysConfig.getStringConfigVal(
"com.sun.jini.qa.home",null);
jarPath += "/lib";
jarPath = jarPath.replace(File.separatorChar, '/');
String jarURL = "file:" + jarPath + "/qa1.jar";
URLClassLoader sibling = new URLClassLoader(new URL[]{
new URL(jarURL)}, loader.getParent());
Object proxy2 =
Proxy.newProxyInstance(sibling, new Class[0],
Proxy.getInvocationHandler(stub));
if (verifier.isTrustedObject(proxy2,ctx)){
throw new TestException("Verifier trusts a "
+ "proxy outside the class loader hierarchy.");
}
//Verify Exceptions
boolean exceptionThrown = false;
try {
verifier.isTrustedObject(stub, null);
} catch (NullPointerException e) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new TestException("Null pointer exception"
+ " not thrown with null context");
}
exceptionThrown = false;
try {
verifier.isTrustedObject(null, ctx);
} catch (NullPointerException e) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new TestException("Null pointer exception"
+ " not thrown with null object");
}
exceptionThrown = false;
TestTrustVerifierCtx ctx2 =
new TestTrustVerifierCtx(true,loader,new RemoteException());
try {
verifier.isTrustedObject(stub,ctx2);
} catch (RemoteException e) {
exceptionThrown = true;
}
if (!exceptionThrown) {
throw new TestException("RemoteException thrown in"
+ " context was not propagated");
}
Object proxy3 = Proxy.newProxyInstance(
RMIClassLoader.getClassLoader(jarURL),
stub.getClass().getInterfaces(),
Proxy.getInvocationHandler(stub));
if (verifier.isTrustedObject(proxy3,ctx)){
throw new TestException("Verifier trusts an "
+ " untrusted object.");
}
}