Compare commits
10 Commits
i2p-2.8.0-
...
test-socks
Author | SHA1 | Date | |
---|---|---|---|
1c1b1e8768 | |||
c42390d983 | |||
8b38d4481b | |||
cce39bb35c | |||
6a841fced5 | |||
d614fa6cbd | |||
472f2776fb | |||
3cdffcfa4c | |||
0273ecaa9c | |||
a79a98f1d0 |
195
core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java
Normal file
195
core/java/test/junit/net/i2p/socks/SOCKS4ClientTest.java
Normal file
@@ -0,0 +1,195 @@
|
||||
package net.i2p.socks;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @since 0.9.49
|
||||
*/
|
||||
public class SOCKS4ClientTest {
|
||||
|
||||
ByteArrayInputStream inputStream;
|
||||
ByteArrayOutputStream outputStream;
|
||||
|
||||
@Before
|
||||
public void openStreams(){
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeStreams() throws IOException {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
inputStream = new ByteArrayInputStream(new byte[]{
|
||||
0, // dummy
|
||||
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
||||
0, 0, 0, 0, 0, 0 // filler
|
||||
});
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
|
||||
// Test overloaded function
|
||||
try {
|
||||
if (useSocket) {
|
||||
Socket socket = Mockito.mock(Socket.class);
|
||||
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
|
||||
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
|
||||
|
||||
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
|
||||
} else {
|
||||
SOCKS4Client.connect(inputStream, outputStream, hostIPv4, connectionPort);
|
||||
}
|
||||
} finally {
|
||||
writerStream.close();
|
||||
}
|
||||
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.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);
|
||||
|
||||
inputStream = new ByteArrayInputStream(new byte[]{
|
||||
0, // dummy
|
||||
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
||||
0, 0, 0, 0, 0, 0 // filler
|
||||
});
|
||||
try {
|
||||
SOCKS4Client.connect(SOCKS4ClientTest.this.inputStream, outputStream, host, connectionPort);
|
||||
} finally {
|
||||
expectedByteStream.close();
|
||||
}
|
||||
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Run into IOException while trying to connect due to no input/response
|
||||
*/
|
||||
@Test
|
||||
public void connect__ioException() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
assertThrows(IOException.class, () -> {
|
||||
SOCKS4Client.connect(
|
||||
inputStream,
|
||||
outputStream,
|
||||
"127.0.0.1",
|
||||
80);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run into IOException while trying to connect due to closed input stream
|
||||
*/
|
||||
@Test
|
||||
public void connect__ioExceptionWithSocket() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
assertThrows(IOException.class, () -> {
|
||||
Socket socket = Mockito.mock(Socket.class);
|
||||
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
|
||||
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
|
||||
|
||||
SOCKS4Client.connect(
|
||||
socket,
|
||||
"127.0.0.1",
|
||||
80
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that CONNECTION_REFUSED throws exception
|
||||
*/
|
||||
@Test
|
||||
public void connect__responseCONNECTION_REFUSED() throws IOException {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{
|
||||
0, // dummy
|
||||
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
|
||||
});
|
||||
assertThrows(SOCKSException.class, () -> {
|
||||
SOCKS4Client.connect(inputStream,
|
||||
outputStream,
|
||||
"1.1.1.1",
|
||||
80
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* IPv6 is not supported by this SOCKS client so it just throws an exception
|
||||
*/
|
||||
@Test
|
||||
public void connect__IPv6() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
assertThrows(SOCKSException.class, () -> {
|
||||
SOCKS4Client.connect(
|
||||
inputStream,
|
||||
outputStream,
|
||||
"::1",
|
||||
80);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user