Search in sources :

Example 11 with Protocol

use of org.jivesoftware.util.Protocol 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 12 with Protocol

use of org.jivesoftware.util.Protocol 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 13 with Protocol

use of org.jivesoftware.util.Protocol 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 14 with Protocol

use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.

the class SoftwareInfoManagerTest method setup.

@BeforeEach
public void setup() throws XMPPException, SmackException, InterruptedException {
    protocol = new Protocol();
    connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
}
Also used : Protocol(org.jivesoftware.util.Protocol) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 15 with Protocol

use of org.jivesoftware.util.Protocol in project Smack by igniterealtime.

the class Socks5ClientForInitiatorTest method setup.

/**
     * Initialize fields used in the tests.
     * @throws XMPPException 
     * @throws SmackException 
     * @throws InterruptedException 
     */
@Before
public void setup() throws XMPPException, SmackException, InterruptedException {
    // build protocol verifier
    protocol = new Protocol();
    // create mocked XMPP connection
    connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID, xmppServer);
}
Also used : Protocol(org.jivesoftware.util.Protocol) Before(org.junit.Before)

Aggregations

Protocol (org.jivesoftware.util.Protocol)30 XMPPConnection (org.jivesoftware.smack.XMPPConnection)23 Test (org.junit.jupiter.api.Test)23 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)14 IQ (org.jivesoftware.smack.packet.IQ)10 InputStream (java.io.InputStream)9 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)9 OutputStream (java.io.OutputStream)8 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)8 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)8 SmackException (org.jivesoftware.smack.SmackException)6 Stanza (org.jivesoftware.smack.packet.Stanza)6 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)6 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)6 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Socket (java.net.Socket)3 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)3 Before (org.junit.Before)3 ServerSocket (java.net.ServerSocket)2