use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.
the class Socks5ByteStreamManagerTest method createResponses.
private static void createResponses(Protocol protocol, String sessionID, Verification<Bytestream, Bytestream> streamHostUsedVerification, Socks5TestProxy socks5TestProxy) throws XmppStringprepException {
// 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);
discoverItems.addItem(new Item(JidCreate.from("proxy2.xmpp-server")));
discoverItems.addItem(new Item(proxyJID));
// return the proxy item if XMPP server is queried
protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
/*
* build discover info for proxy "proxy2.xmpp-server" containing information about being a
* SOCKS5 proxy
*/
DiscoverInfoBuilder proxyInfo1 = Socks5PacketUtils.createDiscoverInfo(JidCreate.from("proxy2.xmpp-server"), initiatorJID);
Identity identity1 = new Identity("proxy", "proxy2.xmpp-server", "bytestreams");
proxyInfo1.addIdentity(identity1);
// return the SOCKS5 bytestream proxy identity if proxy is queried
protocol.addResponse(proxyInfo1.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build discover info for proxy containing information about being a SOCKS5 proxy
DiscoverInfoBuilder proxyInfo2 = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
Identity identity2 = new Identity("proxy", proxyJID.toString(), "bytestreams");
proxyInfo2.addIdentity(identity2);
// return the SOCKS5 bytestream proxy identity if proxy is queried
protocol.addResponse(proxyInfo2.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
/*
* build a SOCKS5 stream host info for "proxy2.xmpp-server" containing the address and the
* port of the proxy
*/
Bytestream streamHostInfo1 = Socks5PacketUtils.createBytestreamResponse(JidCreate.from("proxy2.xmpp-server"), initiatorJID);
streamHostInfo1.addStreamHost(JidCreate.from("proxy2.xmpp-server"), proxyAddress, socks5TestProxy.getPort());
// return stream host info if it is queried
protocol.addResponse(streamHostInfo1, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
// build a SOCKS5 stream host info containing the address and the port of the proxy
Bytestream streamHostInfo2 = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
streamHostInfo2.addStreamHost(proxyJID, proxyAddress, socks5TestProxy.getPort());
// return stream host info if it is queried
protocol.addResponse(streamHostInfo2, 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, streamHostUsedVerification, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
// build response to proxy activation
IQ activationResponse = Socks5PacketUtils.createActivationConfirmation(proxyJID, initiatorJID);
// return proxy activation response if proxy should be activated
protocol.addResponse(activationResponse, new Verification<Bytestream, IQ>() {
@Override
public void verify(Bytestream request, IQ response) {
assertEquals(targetJID, request.getToActivate().getTarget());
}
}, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
}
use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder 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();
}
}
use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.
the class ConfigureFormTest method getConfigFormWithInsufficientPrivileges.
@Test
public void getConfigFormWithInsufficientPrivileges() throws XMPPException, SmackException, IOException, InterruptedException {
ThreadedDummyConnection con = ThreadedDummyConnection.newInstance();
PubSubManager mgr = new PubSubManager(con, PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
DiscoverInfoBuilder info = DiscoverInfo.builder("disco-result").ofType(IQ.Type.result).from(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
Identity ident = new Identity("pubsub", null, "leaf");
info.addIdentity(ident);
DiscoverInfo discoverInfo = info.build();
con.addIQReply(discoverInfo);
Node node = mgr.getNode("princely_musings");
PubSub errorIq = new PubSub();
errorIq.setType(IQ.Type.error);
errorIq.setFrom(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
StanzaError error = StanzaError.getBuilder(Condition.forbidden).build();
errorIq.setError(error);
con.addIQReply(errorIq);
try {
node.getNodeConfiguration();
fail();
} catch (XMPPErrorException e) {
assertEquals(StanzaError.Type.AUTH, e.getStanzaError().getType());
}
}
use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.
the class EntityCapsManagerTest method createComplexSamplePacket.
private static DiscoverInfo createComplexSamplePacket() throws XmppStringprepException {
DiscoverInfoBuilder di = DiscoverInfo.builder("disco1");
di.from(JidCreate.from("benvolio@capulet.lit/230193"));
di.to(JidCreate.from("juliet@capulet.lit/chamber"));
di.ofType(IQ.Type.result);
Collection<DiscoverInfo.Identity> identities = new LinkedList<DiscoverInfo.Identity>();
DiscoverInfo.Identity i = new DiscoverInfo.Identity("client", "pc", "Psi 0.11", "en");
identities.add(i);
i = new DiscoverInfo.Identity("client", "pc", "Ψ 0.11", "el");
identities.add(i);
di.addIdentities(identities);
di.addFeature("http://jabber.org/protocol/disco#items");
di.addFeature(EntityCapsManager.NAMESPACE);
di.addFeature("http://jabber.org/protocol/muc");
di.addFeature("http://jabber.org/protocol/disco#info");
DataForm softwareInfoDataForm = createSampleSoftwareInfoDataForm();
di.addExtension(softwareInfoDataForm);
return di.build();
}
use of org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder in project Smack by igniterealtime.
the class EntityCapsManagerTest method createMalformedDiscoverInfo.
private static DiscoverInfo createMalformedDiscoverInfo() throws XmppStringprepException {
DiscoverInfoBuilder di = DiscoverInfo.builder("disco1");
di.from("benvolio@capulet.lit/230193");
di.to(")juliet@capulet.lit/chamber");
di.ofType(IQ.Type.result);
Collection<DiscoverInfo.Identity> identities = new LinkedList<DiscoverInfo.Identity>();
DiscoverInfo.Identity i = new DiscoverInfo.Identity("client", "pc", "Psi 0.11", "en");
identities.add(i);
i = new DiscoverInfo.Identity("client", "pc", "Ψ 0.11", "el");
identities.add(i);
di.addIdentities(identities);
// Failure 1: Duplicate identities
i = new DiscoverInfo.Identity("client", "pc", "Ψ 0.11", "el");
identities.add(i);
di.addIdentities(identities);
di.addFeature("http://jabber.org/protocol/disco#items");
di.addFeature(EntityCapsManager.NAMESPACE);
di.addFeature("http://jabber.org/protocol/muc");
di.addFeature("http://jabber.org/protocol/disco#info");
// Failure 2: Duplicate features
di.addFeature("http://jabber.org/protocol/disco#info");
DataForm softwareInfoDataForm = createSampleSoftwareInfoDataForm();
di.addExtension(softwareInfoDataForm);
DiscoverInfo discoverInfo = di.buildWithoutValidiation();
return discoverInfo;
}
Aggregations