1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-07 13:41:08 +01:00

Introduce util.InternetAddress

and use it where sensible. Also fixes a few unit tests along the way.
This commit is contained in:
Florian Schmaus 2019-06-03 17:41:10 +02:00
parent 4334ca33ff
commit b288768f77
11 changed files with 462 additions and 268 deletions

View file

@ -224,10 +224,8 @@ public class InitiationListenerTest {
*
* @throws Exception should not happen
*/
@SuppressWarnings("UnusedVariable")
@Test
public void shouldInvokeAllRequestsListenerIfUserListenerExists() throws Exception {
// add listener for all request
Socks5BytestreamListener allRequestsListener = mock(Socks5BytestreamListener.class);
byteStreamManager.addIncomingBytestreamListener(allRequestsListener);
@ -241,15 +239,11 @@ public class InitiationListenerTest {
initiationListener.handleIQRequest(initBytestream);
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
// assert all requests listener is called
byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(allRequestsListener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert user request listener is not called
verify(userRequestsListener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
}
/**

View file

@ -29,7 +29,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
@ -776,16 +778,24 @@ public class Socks5ByteStreamManagerTest {
streamHostUsedPacket.setSessionID(sessionID);
streamHostUsedPacket.setUsedHost(initiatorJID); // local proxy used
final String secondStreamHostIp = "192.0.0.1";
// return used stream host info as response to the bytestream initiation
protocol.addResponse(streamHostUsedPacket, new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
StreamHost streamHost1 = request.getStreamHosts().get(0);
List<StreamHost> streamHosts = request.getStreamHosts();
StreamHost streamHost1 = streamHosts.get(0);
assertEquals(response.getUsedHost().getJID(), streamHost1.getJID());
StreamHost streamHost2 = request.getStreamHosts().get(request.getStreamHosts().size() - 1);
// Get the last stream host. Note that there may be multiple, but since this unit test added
// secondStreamHostIp as last, it should also be the last entry since the API contract assures that
// the order is preserved.
StreamHost streamHost2 = streamHosts.get(streamHosts.size() - 1);
assertEquals(response.getUsedHost().getJID(), streamHost2.getJID());
assertEquals("localAddress", streamHost2.getAddress());
assertEquals(secondStreamHostIp, streamHost2.getAddress().toString());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
@ -798,10 +808,10 @@ public class Socks5ByteStreamManagerTest {
socks5Proxy.getLocalAddresses().get(0),
socks5Proxy.getPort());
Socks5Client socks5Client = new Socks5Client(streamHost, digest);
InputStream inputStream = socks5Client.getSocket(2000).getInputStream();
InputStream inputStream = socks5Client.getSocket(10000).getInputStream();
// add another network address before establishing SOCKS5 Bytestream
socks5Proxy.addLocalAddress("localAddress");
socks5Proxy.addLocalAddress(InetAddress.getByName(secondStreamHostIp));
// finally call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
@ -816,9 +826,9 @@ public class Socks5ByteStreamManagerTest {
assertArrayEquals(data, result);
protocol.verifyAll();
} finally {
socks5Proxy.stop();
}
} finally {
socks5Proxy.stop();
}
}
/**

View file

@ -35,7 +35,6 @@ import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import org.junit.After;
import org.junit.Test;
/**
@ -48,12 +47,10 @@ public class Socks5ProxyTest {
private static final String loopbackAddress = InetAddress.getLoopbackAddress().getHostAddress();
/**
* The SOCKS5 proxy should be a singleton used by all XMPP connections.
* The SOCKS5 proxy should be a quasi singleton used by all XMPP connections.
*/
@Test
public void shouldBeASingleton() {
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
public void shouldBeAQuasiSingleton() {
Socks5Proxy proxy1 = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy2 = Socks5Proxy.getSocks5Proxy();
@ -62,16 +59,6 @@ public class Socks5ProxyTest {
assertSame(proxy1, proxy2);
}
/**
* The SOCKS5 proxy should not be started if disabled by configuration.
*/
@Test
public void shouldNotBeRunningIfDisabled() {
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
assertFalse(proxy.isRunning());
}
/**
* The SOCKS5 proxy should use a free port above the one configured.
*
@ -79,44 +66,45 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldUseFreePortOnNegativeValues() throws Exception {
Socks5Proxy.setLocalSocks5ProxyEnabled(false);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy = new Socks5Proxy();
assertFalse(proxy.isRunning());
ServerSocket serverSocket = new ServerSocket(0);
Socks5Proxy.setLocalSocks5ProxyPort(-serverSocket.getLocalPort());
try (ServerSocket serverSocket = new ServerSocket(0)) {
proxy.setLocalSocks5ProxyPort(-serverSocket.getLocalPort());
proxy.start();
proxy.start();
assertTrue(proxy.isRunning());
serverSocket.close();
assertTrue(proxy.getPort() > serverSocket.getLocalPort());
assertTrue(proxy.isRunning());
assertTrue(proxy.getPort() > serverSocket.getLocalPort());
} finally {
proxy.stop();
}
}
/**
* When inserting new network addresses to the proxy the order should remain in the order they
* were inserted.
*
* @throws UnknownHostException
*/
@Test
public void shouldPreserveAddressOrderOnInsertions() {
public void shouldPreserveAddressOrderOnInsertions() throws UnknownHostException {
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
LinkedHashSet<String> addresses = new LinkedHashSet<>(proxy.getLocalAddresses());
LinkedHashSet<InetAddress> addresses = new LinkedHashSet<>(proxy.getLocalAddresses());
for (int i = 1 ; i <= 3; i++) {
addresses.add(Integer.toString(i));
addresses.add(InetAddress.getByName(Integer.toString(i)));
}
for (String address : addresses) {
for (InetAddress address : addresses) {
proxy.addLocalAddress(address);
}
List<String> localAddresses = proxy.getLocalAddresses();
List<InetAddress> localAddresses = proxy.getLocalAddresses();
Iterator<String> iterator = addresses.iterator();
Iterator<InetAddress> iterator = addresses.iterator();
for (int i = 0; i < addresses.size(); i++) {
assertEquals(iterator.next(), localAddresses.get(i));
}
@ -125,44 +113,25 @@ public class Socks5ProxyTest {
/**
* When replacing network addresses of the proxy the order should remain in the order if the
* given list.
*
* @throws UnknownHostException
*/
@Test
public void shouldPreserveAddressOrderOnReplace() {
public void shouldPreserveAddressOrderOnReplace() throws UnknownHostException {
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
List<String> addresses = new ArrayList<>(proxy.getLocalAddresses());
addresses.add("1");
addresses.add("2");
addresses.add("3");
List<InetAddress> addresses = new ArrayList<>(proxy.getLocalAddresses());
addresses.add(InetAddress.getByName("1"));
addresses.add(InetAddress.getByName("2"));
addresses.add(InetAddress.getByName("3"));
proxy.replaceLocalAddresses(addresses);
List<String> localAddresses = proxy.getLocalAddresses();
List<InetAddress> localAddresses = proxy.getLocalAddresses();
for (int i = 0; i < addresses.size(); i++) {
assertEquals(addresses.get(i), localAddresses.get(i));
}
}
/**
* Inserting the same address multiple times should not cause the proxy to return this address
* multiple times.
*/
@Test
public void shouldNotReturnMultipleSameAddress() {
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
proxy.addLocalAddress("same");
proxy.addLocalAddress("same");
proxy.addLocalAddress("same");
int sameCount = 0;
for (String localAddress : proxy.getLocalAddresses()) {
if ("same".equals(localAddress)) {
sameCount++;
}
}
assertEquals(1, sameCount);
}
/**
* If the SOCKS5 proxy accepts a connection that is not a SOCKS5 connection it should close the
* corresponding socket.
@ -171,27 +140,24 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldCloseSocketIfNoSocks5Request() throws Exception {
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy = new Socks5Proxy();
proxy.start();
@SuppressWarnings("resource")
Socket socket = new Socket(loopbackAddress, proxy.getPort());
try (Socket socket = new Socket(loopbackAddress, proxy.getPort())) {
OutputStream out = socket.getOutputStream();
out.write(new byte[] { 1, 2, 3 });
OutputStream out = socket.getOutputStream();
out.write(new byte[] { 1, 2, 3 });
int res;
try {
res = socket.getInputStream().read();
} catch (SocketException e) {
res = -1;
}
int res;
try {
res = socket.getInputStream().read();
} catch (SocketException e) {
res = -1;
assertEquals(-1, res);
} finally {
proxy.stop();
}
assertEquals(-1, res);
proxy.stop();
}
/**
@ -202,27 +168,24 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldRespondWithErrorIfNoSupportedAuthenticationMethod() throws Exception {
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy = new Socks5Proxy();
proxy.start();
@SuppressWarnings("resource")
Socket socket = new Socket(loopbackAddress, proxy.getPort());
try (Socket socket = new Socket(loopbackAddress, proxy.getPort())) {
OutputStream out = socket.getOutputStream();
OutputStream out = socket.getOutputStream();
// request username/password-authentication
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x02 });
// request username/password-authentication
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x02 });
InputStream in = socket.getInputStream();
InputStream in = socket.getInputStream();
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0xFF, (byte) in.read());
assertEquals(-1, in.read());
proxy.stop();
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0xFF, (byte) in.read());
assertEquals(-1, in.read());
} finally {
proxy.stop();
}
}
/**
@ -233,39 +196,36 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldRespondWithErrorIfConnectionIsNotAllowed() throws Exception {
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy = new Socks5Proxy();
proxy.start();
@SuppressWarnings("resource")
Socket socket = new Socket(loopbackAddress, proxy.getPort());
try (Socket socket = new Socket(loopbackAddress, proxy.getPort())) {
OutputStream out = socket.getOutputStream();
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });
OutputStream out = socket.getOutputStream();
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });
InputStream in = socket.getInputStream();
InputStream in = socket.getInputStream();
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
// send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01, (byte) 0xAA,
(byte) 0x00, (byte) 0x00 });
// send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
(byte) 0xAA, (byte) 0x00, (byte) 0x00 });
// verify error message
assertEquals((byte) 0x05, (byte) in.read());
assertFalse((byte) 0x00 == (byte) in.read()); // something other than 0 == success
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x03, (byte) in.read());
assertEquals((byte) 0x01, (byte) in.read());
assertEquals((byte) 0xAA, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals(-1, in.read());
proxy.stop();
// verify error message
assertEquals((byte) 0x05, (byte) in.read());
assertFalse((byte) 0x00 == (byte) in.read()); // something other than 0 == success
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x03, (byte) in.read());
assertEquals((byte) 0x01, (byte) in.read());
assertEquals((byte) 0xAA, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals(-1, in.read());
} finally {
proxy.stop();
}
}
/**
@ -275,85 +235,63 @@ public class Socks5ProxyTest {
*/
@Test
public void shouldSuccessfullyEstablishConnection() throws Exception {
Socks5Proxy.setLocalSocks5ProxyPort(7890);
Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
Socks5Proxy proxy = new Socks5Proxy();
proxy.start();
assertTrue(proxy.isRunning());
String digest = new String(new byte[] { (byte) 0xAA }, StandardCharsets.UTF_8);
// add digest to allow connection
proxy.addTransfer(digest);
@SuppressWarnings("resource")
Socket socket = new Socket(loopbackAddress, proxy.getPort());
OutputStream out = socket.getOutputStream();
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });
InputStream in = socket.getInputStream();
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
// send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
(byte) 0xAA, (byte) 0x00, (byte) 0x00 });
// verify response
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read()); // success
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x03, (byte) in.read());
assertEquals((byte) 0x01, (byte) in.read());
assertEquals((byte) 0xAA, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
Thread.sleep(200);
Socket remoteSocket = proxy.getSocket(digest);
// remove digest
proxy.removeTransfer(digest);
// test stream
OutputStream remoteOut = remoteSocket.getOutputStream();
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
remoteOut.write(data);
remoteOut.flush();
for (int i = 0; i < data.length; i++) {
assertEquals(data[i], in.read());
}
remoteSocket.close();
assertEquals(-1, in.read());
proxy.stop();
}
/**
* Reset SOCKS5 proxy settings.
*/
@After
public void cleanup() {
Socks5Proxy.setLocalSocks5ProxyEnabled(true);
Socks5Proxy.setLocalSocks5ProxyPort(7777);
Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
try {
String address = InetAddress.getLocalHost().getHostAddress();
List<String> addresses = new ArrayList<>();
addresses.add(address);
socks5Proxy.replaceLocalAddresses(addresses);
}
catch (UnknownHostException e) {
// ignore
}
assertTrue(proxy.isRunning());
String digest = new String(new byte[] { (byte) 0xAA }, StandardCharsets.UTF_8);
socks5Proxy.stop();
// add digest to allow connection
proxy.addTransfer(digest);
@SuppressWarnings("resource")
Socket socket = new Socket(loopbackAddress, proxy.getPort());
OutputStream out = socket.getOutputStream();
out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });
InputStream in = socket.getInputStream();
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
// send valid SOCKS5 message
out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01, (byte) 0xAA,
(byte) 0x00, (byte) 0x00 });
// verify response
assertEquals((byte) 0x05, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read()); // success
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x03, (byte) in.read());
assertEquals((byte) 0x01, (byte) in.read());
assertEquals((byte) 0xAA, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
assertEquals((byte) 0x00, (byte) in.read());
Socket remoteSocket = proxy.getSocket(digest);
try {
// remove digest
proxy.removeTransfer(digest);
// test stream
OutputStream remoteOut = remoteSocket.getOutputStream();
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
remoteOut.write(data);
remoteOut.flush();
for (int i = 0; i < data.length; i++) {
assertEquals(data[i], in.read());
}
} finally {
remoteSocket.close();
}
assertEquals(-1, in.read());
} finally {
proxy.stop();
}
}
}

View file

@ -22,6 +22,8 @@ import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;
import java.net.UnknownHostException;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
@ -89,7 +91,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertNotNull(candidate1.getStreamHost());
assertEquals(JingleS5BTransportCandidate.Type.direct.getWeight(), candidate1.getType().getWeight());
assertEquals("hft54dqy", candidate1.getCandidateId());
assertEquals("192.168.4.1", candidate1.getHost());
assertEquals("192.168.4.1", candidate1.getHost().toString());
assertEquals(JidCreate.from("romeo@montague.lit/orchard"), candidate1.getJid());
assertEquals(5086, candidate1.getPort());
assertEquals(8257636, candidate1.getPriority());
@ -98,7 +100,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
JingleS5BTransportCandidate candidate2 =
(JingleS5BTransportCandidate) transport.getCandidates().get(1);
assertEquals("hutr46fe", candidate2.getCandidateId());
assertEquals("24.24.24.1", candidate2.getHost());
assertEquals("24.24.24.1", candidate2.getHost().toString());
assertEquals(JidCreate.from("romeo@montague.lit/orchard"), candidate2.getJid());
assertEquals(5087, candidate2.getPort());
assertEquals(8258636, candidate2.getPriority());
@ -107,7 +109,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
JingleS5BTransportCandidate candidate3 =
(JingleS5BTransportCandidate) transport.getCandidates().get(2);
assertEquals("xmdh4b7i", candidate3.getCandidateId());
assertEquals("123.456.7.8", candidate3.getHost());
assertEquals("123.456.7.8", candidate3.getHost().toString());
assertEquals(JidCreate.domainBareFrom("streamer.shakespeare.lit"), candidate3.getJid());
assertEquals(7625, candidate3.getPort());
assertEquals(7878787, candidate3.getPriority());
@ -187,15 +189,15 @@ public class JingleS5BTransportTest extends SmackTestSuite {
}
@Test(expected = IllegalArgumentException.class)
public void transportCandidateIllegalPriorityTest() throws XmppStringprepException {
public void transportCandidateIllegalPriorityTest() throws XmppStringprepException, UnknownHostException {
FullJid jid = JidCreate.fullFrom("test@test.test/test");
@SuppressWarnings("unused")
JingleS5BTransportCandidate candidate = new JingleS5BTransportCandidate(
"cid", "host", jid, 5555, -30, JingleS5BTransportCandidate.Type.proxy);
"cid", "localhost", jid, 5555, -30, JingleS5BTransportCandidate.Type.proxy);
}
@Test(expected = IllegalArgumentException.class)
public void transportCandidateIllegalPortTest() throws XmppStringprepException {
public void transportCandidateIllegalPortTest() throws XmppStringprepException, UnknownHostException {
FullJid jid = JidCreate.fullFrom("test@test.test/test");
@SuppressWarnings("unused")
JingleS5BTransportCandidate candidate = new JingleS5BTransportCandidate(
@ -203,9 +205,9 @@ public class JingleS5BTransportTest extends SmackTestSuite {
}
@Test
public void candidateFromStreamHostTest() throws XmppStringprepException {
public void candidateFromStreamHostTest() throws XmppStringprepException, UnknownHostException {
FullJid jid = JidCreate.fullFrom("test@test.test/test");
String host = "host.address";
String host = "localhost";
int port = 1234;
Bytestream.StreamHost streamHost = new Bytestream.StreamHost(jid, host, port);
@ -213,7 +215,7 @@ public class JingleS5BTransportTest extends SmackTestSuite {
assertEquals(2000, candidate.getPriority());
assertEquals(jid, candidate.getJid());
assertEquals(host, candidate.getHost());
assertEquals(host, candidate.getHost().toString());
assertEquals(port, candidate.getPort());
assertEquals(streamHost.toXML().toString(), candidate.getStreamHost().toXML().toString());