Search in sources :

Example 31 with Stanza

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());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 32 with Stanza

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());
    }
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 33 with Stanza

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());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 34 with Stanza

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());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Example 35 with Stanza

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());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Aggregations

Stanza (org.jivesoftware.smack.packet.Stanza)101 StanzaListener (org.jivesoftware.smack.StanzaListener)24 Test (org.junit.Test)22 IQ (org.jivesoftware.smack.packet.IQ)20 Test (org.junit.jupiter.api.Test)18 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 Message (org.jivesoftware.smack.packet.Message)14 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)9 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)7 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 Protocol (org.jivesoftware.util.Protocol)6 EntityFullJid (org.jxmpp.jid.EntityFullJid)6 LinkedList (java.util.LinkedList)5