Injecting fault in java.net.Socket
gberche Aug 26, 2013 11:32 AMHello,
I'm trying to use byteman as part of an integration test [1], to make sure a client configured to use an HTTP proxy for a REST API is indeed going through the HTTP proxy instead of making straight network calls. I'd like to invoke a custom helper within java.net.Socket but I understand the custom rules are expected to be loaded from the target classloader. What would you suggest to embedd this custom helper into the proper classloader? Is there any support in the BMUnit package to do so ? Any other suggestion on how to perform this integration test ?
The unsuccesfull attempts I made are reproduced below
When I have the following rule, I indeed see the IOException being thrown
@RunWith(BMUnitRunner.class)
@BMScript(value="trace", dir="target/test-classes")
@BMRules(rules={
@BMRule(name="throw IOException socket opening",
targetClass = "^java.net.Socket",
targetMethod = "<init> (String , int , InetAddress, int)",
action = "throw new IOException()")})
public class CloudFoundryClientTest {
When I try to invoke a custom Helper, the helper seems silently not called.
@RunWith(BMUnitRunner.class)
@BMRules(rules={
@BMRule(name="throw IOException socket opening",
targetClass = "^java.net.Socket",
targetMethod = "<init> (String , int , InetAddress, int)",
helper = "org.cloudfoundry.client.lib.SocketDestHelper",
action = "alwaysThrowException")})
public class SocketDestHelper {
static ThreadLocal<Boolean> isSocketRestrictingOnlyLocalHost = new ThreadLocal<Boolean>();
public static void setForbiddenOnCurrentThread() {
isSocketRestrictingOnlyLocalHost.set(true);
}
public static void alwaysThrowException() throws IOException {
throw new IOException("only proxy threads should go through external hosts");
}
public static void throwExceptionIfForbidden(String host, int port) throws IOException {
if (isSocketRestrictingOnlyLocalHost.get()) {
if (host != "127.0.0.1") {
throw new IOException("only proxy threads should go through external hosts, got:host=" + host + " port=" + port);
}
}
}
}
Thanks in advance for your help,
Guillaume.