use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldFailIfTargetUsesInvalidSocks5Proxy.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
* proxy used by target is invalid.
*
* @throws InterruptedException if the calling thread was interrupted.
* @throws SmackException if Smack detected an exceptional situation.
* @throws XMPPException if an XMPP protocol error was received.
* @throws IOException if an I/O error occurred.
*/
@Test
public void shouldFailIfTargetUsesInvalidSocks5Proxy() throws SmackException, InterruptedException, IOException, XMPPException {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
final String sessionID = "session_id_shouldFailIfTargetUsesInvalidSocks5Proxy";
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
// TODO: It appears that it is not required to disable the local stream host for this unit test.
byteStreamManager.setAnnounceLocalStreamHost(false);
/**
* create responses in the order they should be queried specified by the XEP-0065
* specification
*/
// build discover info that supports the SOCKS5 feature
DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
discoverInfo.addFeature(Bytestream.NAMESPACE);
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover items containing a proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
Item item = new Item(proxyJID);
discoverItems.addItem(item);
// return the proxy item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover info for proxy containing information about being a SOCKS5 proxy
DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
proxyInfo.addIdentity(identity);
// return the socks5 bytestream proxy identity if proxy is queried
protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build a socks5 stream host info containing the address and the port of the
// proxy
Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
// return stream host info if it is queried
protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build used stream host response with unknown proxy
Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
streamHostUsedPacket.setSessionID(sessionID);
streamHostUsedPacket.setUsedHost(JidCreate.from("invalid.proxy"));
// return used stream host info as response to the bytestream initiation
protocol.addResponse(streamHostUsedPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID);
});
protocol.verifyAll();
assertTrue(e.getMessage().contains("Remote user responded with unknown host"));
}
use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldPrioritizeSecondSocks5ProxyOnSecondAttempt.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} the first time
* should successfully negotiate a SOCKS5 Bytestream via the second SOCKS5 proxy and should
* prioritize this proxy for a second SOCKS5 Bytestream negotiation.
* @throws InterruptedException if the calling thread was interrupted.
* @throws SmackException if Smack detected an exceptional situation.
* @throws XMPPException if an XMPP protocol error was received.
* @throws IOException if an I/O error occurred.
*/
@Test
public void shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws SmackException, InterruptedException, IOException, XMPPException {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
final String sessionID = "session_id_shouldPrioritizeSecondSocks5ProxyOnSecondAttempt";
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
byteStreamManager.setAnnounceLocalStreamHost(false);
assertTrue(byteStreamManager.isProxyPrioritizationEnabled());
Verification<Bytestream, Bytestream> streamHostUsedVerification1 = new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(2, request.getStreamHosts().size());
// verify that the used stream host is the second in list
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[1];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
};
// start a local SOCKS5 proxy
try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
createResponses(protocol, sessionID, streamHostUsedVerification1, socks5Proxy);
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
byte[] data = new byte[] { 1, 2, 3 };
outputStream.write(data);
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
Verification<Bytestream, Bytestream> streamHostUsedVerification2 = new Verification<Bytestream, Bytestream>() {
@Override
public void verify(Bytestream request, Bytestream response) {
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(2, request.getStreamHosts().size());
// verify that the used stream host is the first in list
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
};
createResponses(protocol, sessionID, streamHostUsedVerification2, socks5Proxy);
// call the method that should be tested again
outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
inputStream = socks5Proxy.getSocket(digest).getInputStream();
outputStream.write(data);
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
}
}
use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldBlacklistNonSocks5Proxies.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if no
* SOCKS5 proxy can be found. If it turns out that a proxy is not a SOCKS5 proxy it should not
* be queried again.
* @throws InterruptedException if the calling thread was interrupted.
* @throws SmackException if Smack detected an exceptional situation.
* @throws XMPPException if an XMPP protocol error was received.
* @throws IOException if an I/O error occurred.
*/
@Test
public void shouldBlacklistNonSocks5Proxies() throws SmackException, InterruptedException, IOException, XMPPException {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
final String sessionID = "session_id_shouldBlacklistNonSocks5Proxies";
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
byteStreamManager.setAnnounceLocalStreamHost(false);
/**
* create responses in the order they should be queried specified by the XEP-0065
* specification
*/
// build discover info that supports the SOCKS5 feature
DiscoverInfoBuilder discoverInfoBuilder = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
discoverInfoBuilder.addFeature(Bytestream.NAMESPACE);
DiscoverInfo discoverInfo = discoverInfoBuilder.build();
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover items containing a proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
Item item = new Item(proxyJID);
discoverItems.addItem(item);
// return the proxy item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover info for proxy containing information about NOT being a Socks5
// proxy
DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
Identity identity = new Identity("noproxy", proxyJID.toString(), "bytestreams");
proxyInfo.addIdentity(identity);
// return the proxy identity if proxy is queried
protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
SmackException e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID);
fail("exception should be thrown");
});
protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
/* retry to establish SOCKS5 Bytestream */
// add responses for service discovery again
protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
e = assertThrows(SmackException.class, () -> {
// start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID);
});
/*
* #verifyAll() tests if the number of requests and responses corresponds and should
* fail if the invalid proxy is queried again
*/
protocol.verifyAll();
assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldFailIfInitiatorCannotConnectToSocks5Proxy.
/**
* Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if
* initiator can not connect to the SOCKS5 proxy used by target.
*
* @throws InterruptedException if the calling thread was interrupted.
* @throws SmackException if Smack detected an exceptional situation.
* @throws XMPPException if an XMPP protocol error was received.
* @throws XmppStringprepException if the provided string is invalid.
*/
@Test
public void shouldFailIfInitiatorCannotConnectToSocks5Proxy() throws SmackException, InterruptedException, XMPPException, XmppStringprepException {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
final String sessionID = "session_id_shouldFailIfInitiatorCannotConnectToSocks5Proxy";
// TODO: The following two variables should be named initatorProxyJid and initiatorProxyAddress.
final DomainBareJid proxyJID = JidCreate.domainBareFrom("s5b-proxy.initiator.org");
// Use an TEST-NET-1 address from RFC 5737 to act as black hole.
final String proxyAddress = "192.0.2.1";
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
byteStreamManager.setAnnounceLocalStreamHost(false);
byteStreamManager.setProxyConnectionTimeout(3000);
/**
* create responses in the order they should be queried specified by the XEP-0065
* specification
*/
// build discover info that supports the SOCKS5 feature
DiscoverInfoBuilder discoverInfoBuilder = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
discoverInfoBuilder.addFeature(Bytestream.NAMESPACE);
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfoBuilder.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover items containing a proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
Item item = new Item(proxyJID);
discoverItems.addItem(item);
// return the proxy item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover info for proxy containing information about being a SOCKS5 proxy
DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
proxyInfo.addIdentity(identity);
// return the socks5 bytestream proxy identity if proxy is queried
protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build a socks5 stream host info containing the address and the port of the
// proxy
Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
// return stream host info if it is queried
protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build used stream host response
Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
streamHostUsedPacket.setSessionID(sessionID);
streamHostUsedPacket.setUsedHost(proxyJID);
// 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) {
// verify SOCKS5 Bytestream request
assertEquals(response.getSessionID(), request.getSessionID());
assertEquals(1, request.getStreamHosts().size());
StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
IOException e = assertThrows(IOException.class, () -> {
// start SOCKS5 Bytestream
byteStreamManager.establishSession(targetJID, sessionID);
});
// initiator can't connect to proxy because it is not running
protocol.verifyAll();
Throwable actualCause = e.getCause();
assertEquals(TimeoutException.class, actualCause.getClass(), "Unexpected throwable: " + actualCause + '.' + ExceptionUtil.getStackTrace(actualCause));
}
use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method shouldUseMultipleAddressesForLocalSocks5Proxy.
/**
* If multiple network addresses are added to the local SOCKS5 proxy, all of them should be
* contained in the SOCKS5 Bytestream request.
*
* @throws InterruptedException if the calling thread was interrupted.
* @throws SmackException if Smack detected an exceptional situation.
* @throws IOException if an I/O error occurred.
* @throws XMPPException if an XMPP protocol error was received.
* @throws TimeoutException if there was a timeout.
*/
@Test
public void shouldUseMultipleAddressesForLocalSocks5Proxy() throws SmackException, InterruptedException, IOException, TimeoutException, XMPPException {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
final String sessionID = "session_id_shouldUseMultipleAddressesForLocalSocks5Proxy";
// start a local SOCKS5 proxy
Socks5Proxy socks5Proxy = new Socks5Proxy();
socks5Proxy.start();
try {
assertTrue(socks5Proxy.isRunning());
// get Socks5ByteStreamManager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
/**
* create responses in the order they should be queried specified by the XEP-0065
* specification
*/
// build discover info that supports the SOCKS5 feature
DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
discoverInfo.addFeature(Bytestream.NAMESPACE);
// return that SOCKS5 is supported if target is queried
protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover items containing no proxy item
DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
// return the discover item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build used stream host response
Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
streamHostUsedPacket.setSessionID(sessionID);
// local proxy used
streamHostUsedPacket.setUsedHost(initiatorJID);
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());
List<StreamHost> streamHosts = request.getStreamHosts();
StreamHost streamHost1 = streamHosts.get(0);
assertEquals(response.getUsedHost().getJID(), streamHost1.getJID());
// 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(secondStreamHostIp, streamHost2.getAddress().toString());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// connect to proxy as target
socks5Proxy.addTransfer(digest);
StreamHost streamHost = new StreamHost(targetJID, socks5Proxy.getLocalAddresses().get(0), socks5Proxy.getPort());
Socks5Client socks5Client = new Socks5Client(streamHost, digest);
InputStream inputStream = socks5Client.getSocket(10000).getInputStream();
// add another network address before establishing SOCKS5 Bytestream
socks5Proxy.addLocalAddress(InetAddress.getByName(secondStreamHostIp));
// finally call the method that should be tested
OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
// test the established bytestream
byte[] data = new byte[] { 1, 2, 3 };
outputStream.write(data);
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
protocol.verifyAll();
} finally {
socks5Proxy.stop();
}
}
Aggregations