Test SOCKS4 client: IPv4 success

This commit is contained in:
2021-01-22 20:49:46 +01:00
parent f71e59a049
commit a79a98f1d0

View File

@@ -0,0 +1,44 @@
package net.i2p.socks;
import org.junit.Test;
import sun.net.util.IPAddressUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static net.i2p.socks.SOCKS4Constants.SOCKS_VERSION_4;
import static org.junit.Assert.assertArrayEquals;
public class SOCKS4ClientTest {
/**
* A successful connection to an IPv4 host
*/
@Test
public void connect() 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();
SOCKS4Client.connect(ips, ops, hostIPv4, connectionPort);
assertArrayEquals(expectedByteStream.toByteArray(), ops.toByteArray());
}
}