Compare commits

...

8 Commits

Author SHA1 Message Date
8b38d4481b Test SOCKS4 client: connect with faulty Socket 2021-01-22 22:23:02 +01:00
cce39bb35c Test SOCKS4 client: connect using mocked Socket 2021-01-22 22:16:48 +01:00
6a841fced5 Test SOCKS4 client: CONNECTION_REFUSED when connecting 2021-01-22 21:43:22 +01:00
d614fa6cbd Test SOCKS4 client: refactor IPv6 test
We don't need to test the response data since we expect an exception anyway
2021-01-22 21:42:49 +01:00
472f2776fb Test SOCKS4 client: no response to connect 2021-01-22 21:34:41 +01:00
3cdffcfa4c Test SOCKS4 client: connect to host over proxy 2021-01-22 21:17:49 +01:00
0273ecaa9c Test SOCKS4 client: IPv6 expected failure
IPv6 isn't supported
2021-01-22 20:50:23 +01:00
a79a98f1d0 Test SOCKS4 client: IPv4 success 2021-01-22 20:49:46 +01:00

View File

@@ -0,0 +1,164 @@
package net.i2p.socks;
import org.junit.Test;
import org.mockito.Mockito;
import sun.net.util.IPAddressUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import static net.i2p.socks.SOCKS4Constants.SOCKS_VERSION_4;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;
public class SOCKS4ClientTest {
/**
* A successful connection to an IPv4 host
*/
@Test
public void connect() throws IOException {
_testConnect(false);
}
/**
* A successful connection to an IPv4 host using a socket
*/
@Test
public void connect__withSocket() throws IOException {
_testConnect(true);
}
private void _testConnect(boolean useSocket) throws IOException {
String hostIPv4 = "11.22.33.44";
int connectionPort = 8080;
byte[] hostIPv4Bin = IPAddressUtil.textToNumericFormatV4(hostIPv4);
// Build sequence of bytes to be expected
ByteArrayOutputStream expectedByteStream = new ByteArrayOutputStream();
DataOutputStream writerStream = new DataOutputStream(expectedByteStream);
writerStream.writeByte(SOCKS_VERSION_4);
writerStream.writeByte(SOCKS4Constants.Command.CONNECT);
writerStream.writeShort(connectionPort);
writerStream.write(hostIPv4Bin);
writerStream.write((byte) 0);
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
0, 0, 0, 0, 0, 0 // filler
});
ByteArrayOutputStream ops = new ByteArrayOutputStream();
// Test overloaded function
if (useSocket) {
Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(ips);
Mockito.when(socket.getOutputStream()).thenReturn(ops);
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
} else {
SOCKS4Client.connect(ips, ops, hostIPv4, connectionPort);
}
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
}
/**
* Connect proxy with a domain name
*/
@Test
public void connect__host() throws IOException {
String host = "stats.i2p";
int connectionPort = 80;
// Build sequence of bytes to be expected
ByteArrayOutputStream expectedByteStream = new ByteArrayOutputStream();
DataOutputStream writerStream = new DataOutputStream(expectedByteStream);
writerStream.writeByte(SOCKS_VERSION_4);
writerStream.writeByte(SOCKS4Constants.Command.CONNECT);
writerStream.writeShort(connectionPort);
writerStream.write(new byte[]{0,0,0,1}); // 0.0.0.1
writerStream.write((byte) 0); // empty userID
writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1));
writerStream.write((byte) 0);
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
0, 0, 0, 0, 0, 0 // filler
});
ByteArrayOutputStream ops = new ByteArrayOutputStream();
SOCKS4Client.connect(ips, ops, host, connectionPort);
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
}
/**
* Run into IOException while trying to connect due to no input/response
*/
@Test
public void connect__ioException() {
assertThrows(IOException.class, () -> {
SOCKS4Client.connect(
new ByteArrayInputStream(new byte[]{}),
new ByteArrayOutputStream(),
"127.0.0.1",
80);
});
}
/**
* Run into IOException while trying to connect due to closed input stream
*/
@Test
public void connect__ioExceptionWithSocket() {
assertThrows(IOException.class, () -> {
// Create an IOException by closing input stream before it can used
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[]{});
inputStream.close();
Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
Mockito.when(socket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
SOCKS4Client.connect(
socket,
"127.0.0.1",
80
);
});
}
/**
* Check that CONNECTION_REFUSED throws exception
*/
@Test
public void connect__responseCONNECTION_REFUSED() {
assertThrows(SOCKSException.class, () -> {
SOCKS4Client.connect(new ByteArrayInputStream(new byte[]{
0, // dummy
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
}),
new ByteArrayOutputStream(),
"1.1.1.1",
80
);
});
}
/**
* IPv6 is not supported by this SOCKS client so it just throws an exception
*/
@Test
public void connect__IPv6() {
assertThrows(SOCKSException.class, () -> {
SOCKS4Client.connect(
new ByteArrayInputStream(new byte[]{}),
new ByteArrayOutputStream(),
"::1",
80);
});
}
}