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;
|
package net.i2p.socks;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import sun.net.util.IPAddressUtil;
|
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.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertThrows;
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 0.9.49
|
||||||
|
*/
|
||||||
public class SOCKS4ClientTest {
|
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
|
* A successful connection to an IPv4 host
|
||||||
*/
|
*/
|
||||||
@@ -24,6 +47,7 @@ public class SOCKS4ClientTest {
|
|||||||
public void connect() throws IOException {
|
public void connect() throws IOException {
|
||||||
_testConnect(false);
|
_testConnect(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A successful connection to an IPv4 host using a socket
|
* A successful connection to an IPv4 host using a socket
|
||||||
*/
|
*/
|
||||||
@@ -46,24 +70,28 @@ public class SOCKS4ClientTest {
|
|||||||
writerStream.write(hostIPv4Bin);
|
writerStream.write(hostIPv4Bin);
|
||||||
writerStream.write((byte) 0);
|
writerStream.write((byte) 0);
|
||||||
|
|
||||||
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
|
inputStream = new ByteArrayInputStream(new byte[]{
|
||||||
0, // dummy
|
0, // dummy
|
||||||
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
||||||
0, 0, 0, 0, 0, 0 // filler
|
0, 0, 0, 0, 0, 0 // filler
|
||||||
});
|
});
|
||||||
ByteArrayOutputStream ops = new ByteArrayOutputStream();
|
outputStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
// Test overloaded function
|
// Test overloaded function
|
||||||
|
try {
|
||||||
if (useSocket) {
|
if (useSocket) {
|
||||||
Socket socket = Mockito.mock(Socket.class);
|
Socket socket = Mockito.mock(Socket.class);
|
||||||
Mockito.when(socket.getInputStream()).thenReturn(ips);
|
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
|
||||||
Mockito.when(socket.getOutputStream()).thenReturn(ops);
|
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
|
||||||
|
|
||||||
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
|
SOCKS4Client.connect(socket, hostIPv4, connectionPort);
|
||||||
} else {
|
} else {
|
||||||
SOCKS4Client.connect(ips, ops, hostIPv4, connectionPort);
|
SOCKS4Client.connect(inputStream, outputStream, hostIPv4, connectionPort);
|
||||||
}
|
}
|
||||||
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
|
} finally {
|
||||||
|
writerStream.close();
|
||||||
|
}
|
||||||
|
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,15 +113,17 @@ public class SOCKS4ClientTest {
|
|||||||
writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1));
|
writerStream.write(host.getBytes(StandardCharsets.ISO_8859_1));
|
||||||
writerStream.write((byte) 0);
|
writerStream.write((byte) 0);
|
||||||
|
|
||||||
ByteArrayInputStream ips = new ByteArrayInputStream(new byte[]{
|
inputStream = new ByteArrayInputStream(new byte[]{
|
||||||
0, // dummy
|
0, // dummy
|
||||||
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
SOCKS4Constants.Reply.SUCCEEDED, // Connection succeeded
|
||||||
0, 0, 0, 0, 0, 0 // filler
|
0, 0, 0, 0, 0, 0 // filler
|
||||||
});
|
});
|
||||||
ByteArrayOutputStream ops = new ByteArrayOutputStream();
|
try {
|
||||||
|
SOCKS4Client.connect(SOCKS4ClientTest.this.inputStream, outputStream, host, connectionPort);
|
||||||
SOCKS4Client.connect(ips, ops, host, connectionPort);
|
} finally {
|
||||||
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
|
expectedByteStream.close();
|
||||||
|
}
|
||||||
|
assertArrayEquals(expectedByteStream.toByteArray(), outputStream.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,10 +131,11 @@ public class SOCKS4ClientTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void connect__ioException() {
|
public void connect__ioException() {
|
||||||
|
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||||
assertThrows(IOException.class, () -> {
|
assertThrows(IOException.class, () -> {
|
||||||
SOCKS4Client.connect(
|
SOCKS4Client.connect(
|
||||||
new ByteArrayInputStream(new byte[]{}),
|
inputStream,
|
||||||
new ByteArrayOutputStream(),
|
outputStream,
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
80);
|
80);
|
||||||
});
|
});
|
||||||
@@ -115,14 +146,11 @@ public class SOCKS4ClientTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void connect__ioExceptionWithSocket() {
|
public void connect__ioExceptionWithSocket() {
|
||||||
|
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||||
assertThrows(IOException.class, () -> {
|
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);
|
Socket socket = Mockito.mock(Socket.class);
|
||||||
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
|
Mockito.when(socket.getInputStream()).thenReturn(inputStream);
|
||||||
Mockito.when(socket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
|
Mockito.when(socket.getOutputStream()).thenReturn(outputStream);
|
||||||
|
|
||||||
SOCKS4Client.connect(
|
SOCKS4Client.connect(
|
||||||
socket,
|
socket,
|
||||||
@@ -131,17 +159,19 @@ public class SOCKS4ClientTest {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check that CONNECTION_REFUSED throws exception
|
* Check that CONNECTION_REFUSED throws exception
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void connect__responseCONNECTION_REFUSED() {
|
public void connect__responseCONNECTION_REFUSED() throws IOException {
|
||||||
assertThrows(SOCKSException.class, () -> {
|
inputStream = new ByteArrayInputStream(new byte[]{
|
||||||
SOCKS4Client.connect(new ByteArrayInputStream(new byte[]{
|
|
||||||
0, // dummy
|
0, // dummy
|
||||||
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
|
SOCKS4Constants.Reply.CONNECTION_REFUSED, // Connection succeeded
|
||||||
}),
|
});
|
||||||
new ByteArrayOutputStream(),
|
assertThrows(SOCKSException.class, () -> {
|
||||||
|
SOCKS4Client.connect(inputStream,
|
||||||
|
outputStream,
|
||||||
"1.1.1.1",
|
"1.1.1.1",
|
||||||
80
|
80
|
||||||
);
|
);
|
||||||
@@ -153,10 +183,11 @@ public class SOCKS4ClientTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void connect__IPv6() {
|
public void connect__IPv6() {
|
||||||
|
inputStream = new ByteArrayInputStream(new byte[]{});
|
||||||
assertThrows(SOCKSException.class, () -> {
|
assertThrows(SOCKSException.class, () -> {
|
||||||
SOCKS4Client.connect(
|
SOCKS4Client.connect(
|
||||||
new ByteArrayInputStream(new byte[]{}),
|
inputStream,
|
||||||
new ByteArrayOutputStream(),
|
outputStream,
|
||||||
"::1",
|
"::1",
|
||||||
80);
|
80);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user