forked from I2P_Developers/i2p.i2p
Compare commits
2 Commits
test-socks
...
test-socks
Author | SHA1 | Date | |
---|---|---|---|
1c1b1e8768 | |||
c42390d983 |
@@ -1,5 +1,7 @@
|
||||
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;
|
||||
@@ -15,8 +17,29 @@ 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
|
||||
*/
|
||||
@@ -24,6 +47,7 @@ public class SOCKS4ClientTest {
|
||||
public void connect() throws IOException {
|
||||
_testConnect(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* A successful connection to an IPv4 host using a socket
|
||||
*/
|
||||
@@ -46,24 +70,28 @@ public class SOCKS4ClientTest {
|
||||
writerStream.write(hostIPv4Bin);
|
||||
writerStream.write((byte) 0);
|
||||
|
||||
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
|
||||
inputStream = new ByteArrayInputStream(new byte[]{
|
||||
0, // dummy
|
||||
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
||||
0, 0, 0, 0, 0, 0 // filler
|
||||
});
|
||||
ByteArrayOutputStream ops = new ByteArrayOutputStream();
|
||||
outputStream = 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);
|
||||
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(ips, ops, hostIPv4, connectionPort);
|
||||
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
|
||||
} else {
|
||||
SOCKS4Client.connect(inputStream, outputStream, hostIPv4, connectionPort);
|
||||
}
|
||||
} finally {
|
||||
writerStream.close();
|
||||
}
|
||||
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
|
||||
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,15 +113,17 @@ public class SOCKS4ClientTest {
|
||||
writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1));
|
||||
writerStream.write((byte) 0);
|
||||
|
||||
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
|
||||
inputStream = 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());
|
||||
try {
|
||||
SOCKS4Client.connect(SOCKS4ClientTest.this.inputStream, outputStream, host, connectionPort);
|
||||
} finally {
|
||||
expectedByteStream.close();
|
||||
}
|
||||
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,10 +131,11 @@ public class SOCKS4ClientTest {
|
||||
*/
|
||||
@Test
|
||||
public void connect__ioException() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
assertThrows(IOException.class, () -> {
|
||||
SOCKS4Client.connect(
|
||||
new ByteArrayInputStream(new byte[]{}),
|
||||
new ByteArrayOutputStream(),
|
||||
inputStream,
|
||||
outputStream,
|
||||
"127.0.0.1",
|
||||
80);
|
||||
});
|
||||
@@ -115,14 +146,11 @@ public class SOCKS4ClientTest {
|
||||
*/
|
||||
@Test
|
||||
public void connect__ioExceptionWithSocket() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
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());
|
||||
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
|
||||
|
||||
SOCKS4Client.connect(
|
||||
socket,
|
||||
@@ -131,17 +159,19 @@ public class SOCKS4ClientTest {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that CONNECTION_REFUSED throws exception
|
||||
*/
|
||||
@Test
|
||||
public void connect__responseCONNECTION_REFUSED() {
|
||||
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(new ByteArrayInputStream(new byte[]{
|
||||
0, // dummy
|
||||
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
|
||||
}),
|
||||
new ByteArrayOutputStream(),
|
||||
SOCKS4Client.connect(inputStream,
|
||||
outputStream,
|
||||
"1.1.1.1",
|
||||
80
|
||||
);
|
||||
@@ -153,10 +183,11 @@ public class SOCKS4ClientTest {
|
||||
*/
|
||||
@Test
|
||||
public void connect__IPv6() {
|
||||
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||
assertThrows(SOCKSException.class, () -> {
|
||||
SOCKS4Client.connect(
|
||||
new ByteArrayInputStream(new byte[]{}),
|
||||
new ByteArrayOutputStream(),
|
||||
inputStream,
|
||||
outputStream,
|
||||
"::1",
|
||||
80);
|
||||
});
|
||||
|
Reference in New Issue
Block a user