Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to simulate lack of network connectivity in unit testing

The general question is how to simulate (as part of a JUnit suite of test cases) lack of network connectivity as this is an important consideration in some test cases. Is there a way to do so via a Java API (or via a JVM option) so that certain test cases can be run under network dis-connectivity? (simulated or real?).

The more specific case (if there is no generally applicable solution) is that I am doing a bunch of XML-file processing (including XSD-validation) and I need to ensure that nothing is fetched over the network, specifically, that the xsi:schemaLocation attributes values (hints) are not used and that all XSDs are actually obtained from the classpath. I am using a Validator with a custom LSResourceResolver that loads any XSDs that may be needed from the classpath.

I guess I could implement the resolveResource method of the LSResourceResolver so as to never return null (and therefore, so as to never fall back on the default behavior of opening a regular URI connection to the resource) but I am not sure whether that would be sufficient and at any rate I would feel more confident if I could run my JUnit tests in a simulated island-mode (without manually bringing down the interfaces on my machine).

UPDATE

The accepted answer, namely the -DsocksProxyHost approach provided exactly the solution I needed as the JUnit task can accept VM parameters (if fork is set to true) and so I can have the following in my Ant file:

<junit printsummary="true" showoutput="true" fork="true" maxmemory="256m">
    <jvmarg value="-DsocksProxyHost=127.0.0.1"/>
    ...

... wrapped inside contrib:if so I can control from the command line whether to run JUnit tests under conditions of network connectivity or not.

like image 947
Marcus Junius Brutus Avatar asked Feb 08 '15 17:02

Marcus Junius Brutus


People also ask

What test should you use for connectivity problems?

The ping command is able to test connectivity by sending a series of ICMP echo packets to the destination host. If the destination host receives them, it will respond back with an ICMP echo-reply. The results of the ping test will tell you if packets were received or lost, as shown in Figure 2.6.


2 Answers

That's not what I would advise to do, but... you can try to use a security manager. By default JVM does not use a security manager, by setting -Djava.security.manager you will activate the default security manager. AFAIR, the default behaviour of the security manager is to block any connections (as long as the permission is not explicitly granted in security policy). But. You will get java.net.NetPermission Exception each time a connection will be blocked. You can also experience problems with local files access, reflection calls and so on.

Another possibility is to set the network proxy to a non-existent address, like -DsocksProxyHost=127.0.0.1. Then any TCP socket will try to use the SOCKS server and fail.

like image 194
AGV Avatar answered Sep 20 '22 12:09

AGV


Sniffy allows you to block outgoing network connections in your JUnit tests - it will throw a ConnectException whenever you try to establish a new connection.

@Rule public SniffyRule sniffyRule = new SniffyRule();

@Test
@DisableSockets
public void testDisableSockets() throws IOException {
    try {
        new Socket("google.com", 22);
        fail("Sniffy should have thrown ConnectException");
    } catch (ConnectException e) {
        assertNotNull(e);
    }
}

It also allows you to test how your application behaves with broken connectivity in runtime. Just add -javaagent:sniffy.jar=5559 to your JVM arguments and point your browser to localhost:5559 - it will open a web page with all discovered connections to downstream systems and controls to disable certain connections.

Sniffy connections console

Disclaimer: I'm the author of Sniffy

like image 29
bedrin Avatar answered Sep 20 '22 12:09

bedrin