use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class Socks5ByteStreamRequestTest method shouldFailIfRequestHasNoStreamHosts.
/**
* Accepting a SOCKS5 Bytestream request should fail if the request doesn't contain any Socks5
* proxies.
*
* @throws Exception should not happen
*/
@Test
public void shouldFailIfRequestHasNoStreamHosts() throws Exception {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
assertThrows(Socks5Exception.NoSocks5StreamHostsProvided.class, () -> {
// build SOCKS5 Bytestream initialization request with no SOCKS5 proxies
Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
// get SOCKS5 Bytestream manager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
// build SOCKS5 Bytestream request with the bytestream initialization
Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
// accept the stream (this is the call that is tested here)
byteStreamRequest.accept();
});
// verify targets response
assertEquals(1, protocol.getRequests().size());
Stanza targetResponse = protocol.getRequests().remove(0);
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(StanzaError.Condition.item_not_found, targetResponse.getError().getCondition());
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class Socks5ByteStreamRequestTest method shouldNotBlacklistInvalidProxy.
/**
* Target should not not blacklist any SOCKS5 proxies regardless of failing connections.
*
* @throws Exception should not happen
*/
@Test
public void shouldNotBlacklistInvalidProxy() throws Exception {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
// build SOCKS5 Bytestream initialization request
Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
bytestreamInitialization.addStreamHost(JidCreate.from("invalid." + proxyJID), "127.0.0.2", 7778);
// get SOCKS5 Bytestream manager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
// try to connect several times
for (int i = 0; i < 10; i++) {
assertThrows(Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host.class, () -> {
// build SOCKS5 Bytestream request with the bytestream initialization
Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
// set timeouts
byteStreamRequest.setTotalConnectTimeout(600);
byteStreamRequest.setMinimumConnectTimeout(300);
byteStreamRequest.setConnectFailureThreshold(0);
// accept the stream (this is the call that is tested here)
byteStreamRequest.accept();
});
// verify targets response
assertEquals(1, protocol.getRequests().size());
Stanza targetResponse = protocol.getRequests().remove(0);
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(StanzaError.Condition.item_not_found, targetResponse.getError().getCondition());
}
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class Socks5ByteStreamRequestTest method shouldFailIfRequestHasInvalidStreamHosts.
/**
* Accepting a SOCKS5 Bytestream request should fail if target is not able to connect to any of
* the provided SOCKS5 proxies.
*
* @throws Exception if an exception occurs.
*/
@Test
public void shouldFailIfRequestHasInvalidStreamHosts() throws Exception {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
assertThrows(Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host.class, () -> {
// build SOCKS5 Bytestream initialization request
Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
// add proxy that is not running
bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7778);
// get SOCKS5 Bytestream manager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
// build SOCKS5 Bytestream request with the bytestream initialization
Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
// accept the stream (this is the call that is tested here)
byteStreamRequest.accept();
});
// verify targets response
assertEquals(1, protocol.getRequests().size());
Stanza targetResponse = protocol.getRequests().remove(0);
assertTrue(IQ.class.isInstance(targetResponse));
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
assertEquals(StanzaError.Condition.item_not_found, targetResponse.getError().getCondition());
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ItemValidationTest method parseSimplePayloadItem.
@Test
public void parseSimplePayloadItem() throws Exception {
String itemContent = "<foo xmlns='smack:test'>Some text</foo>";
XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
Stanza message = PacketParserUtils.parseMessage(parser);
XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
EventElement event = (EventElement) eventExt;
NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem<?>);
PayloadItem<?> item = (PayloadItem<?>) itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
SimplePayload payload = (SimplePayload) item.getPayload();
assertEquals("foo", payload.getElementName());
assertEquals("smack:test", payload.getNamespace());
assertXmlSimilar(itemContent, payload.toXML().toString());
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ItemValidationTest method parseComplexItem.
@Test
public void parseComplexItem() throws Exception {
String itemContent = "<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title>Soliloquy</title>" + "<summary>" + "To be, or not to be: that is the question:" + "Whether 'tis nobler in the mind to suffer" + "The slings and arrows of outrageous fortune," + "Or to take arms against a sea of troubles," + "And by opposing end them?" + "</summary>" + "<link rel='alternate' type='text/html' href='http://denmark.lit/2003/12/13/atom03'/>" + "<id>tag:denmark.lit,2003:entry-32397</id>" + "<published>2003-12-13T18:30:02Z</published>" + "<updated>2003-12-13T18:30:02Z</updated>" + "</entry>";
XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
Stanza message = PacketParserUtils.parseMessage(parser);
XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
EventElement event = (EventElement) eventExt;
NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof PayloadItem<?>);
PayloadItem<?> item = (PayloadItem<?>) itemExt;
assertEquals("testid1", item.getId());
assertTrue(item.getPayload() instanceof SimplePayload);
SimplePayload payload = (SimplePayload) item.getPayload();
assertEquals("entry", payload.getElementName());
assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
assertXmlSimilar(itemContent, payload.toXML().toString());
}
Aggregations