Search in sources :

Example 26 with Protocol

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

the class InBandBytestreamSessionMessageTest method setup.

/**
 * Initialize fields used in the tests.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws InterruptedException if the calling thread was interrupted.
 */
@BeforeEach
public void setup() throws XMPPException, SmackException, InterruptedException {
    // build protocol verifier
    protocol = new Protocol();
    // create mocked XMPP connection
    connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    // create a In-Band Bytestream open packet with message stanza
    initBytestream = new Open(sessionID, blockSize, StanzaType.MESSAGE);
    initBytestream.setFrom(initiatorJID);
    initBytestream.setTo(targetJID);
    incrementingSequence = new Verification<Message, IQ>() {

        long lastSeq = 0;

        @Override
        public void verify(Message request, IQ response) {
            DataPacketExtension dpe = request.getExtension(DataPacketExtension.class);
            assertEquals(lastSeq++, dpe.getSeq().longValue());
        }
    };
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Protocol(org.jivesoftware.util.Protocol) Open(org.jivesoftware.smackx.bytestreams.ibb.packet.Open) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with Protocol

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

the class Socks5ByteStreamRequestTest method shouldAcceptSocks5BytestreamRequestAndReceiveData.

/**
 * Accepting the SOCKS5 Bytestream request should be successfully.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldAcceptSocks5BytestreamRequestAndReceiveData() throws Exception {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        // build SOCKS5 Bytestream initialization request
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
        bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, socks5Proxy.getPort());
        // create test data for stream
        byte[] data = new byte[] { 1, 2, 3 };
        // 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)
        InputStream inputStream = byteStreamRequest.accept().getInputStream();
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // test stream by sending some data
        OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
        outputStream.write(data);
        // verify that data is transferred correctly
        byte[] result = new byte[3];
        inputStream.read(result);
        assertArrayEquals(data, result);
        // verify targets response
        assertEquals(1, protocol.getRequests().size());
        Stanza targetResponse = protocol.getRequests().remove(0);
        assertEquals(Bytestream.class, targetResponse.getClass());
        assertEquals(initiatorJID, targetResponse.getTo());
        assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
        assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
    }
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Stanza(org.jivesoftware.smack.packet.Stanza) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 28 with Protocol

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

the class Socks5ByteStreamRequestTest method shouldBlacklistInvalidProxyAfter2Failures.

/**
 * Target should not try to connect to SOCKS5 proxies that already failed twice.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldBlacklistInvalidProxyAfter2Failures() 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);
    // Add an unreachable stream host.
    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 < 2; 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);
            // 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());
    }
    // create test data for stream
    byte[] data = new byte[] { 1, 2, 3 };
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        assertTrue(socks5Proxy.isRunning());
        // add a valid SOCKS5 proxy
        bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, socks5Proxy.getPort());
        // build SOCKS5 Bytestream request with the bytestream initialization
        Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
        // set timeouts
        byteStreamRequest.setTotalConnectTimeout(600);
        byteStreamRequest.setMinimumConnectTimeout(300);
        // accept the stream (this is the call that is tested here)
        InputStream inputStream = byteStreamRequest.accept().getInputStream();
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // test stream by sending some data
        OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
        outputStream.write(data);
        // verify that data is transferred correctly
        byte[] result = new byte[3];
        inputStream.read(result);
        assertArrayEquals(data, result);
        // verify targets response
        assertEquals(1, protocol.getRequests().size());
        Stanza targetResponse = protocol.getRequests().remove(0);
        assertEquals(Bytestream.class, targetResponse.getClass());
        assertEquals(initiatorJID, targetResponse.getTo());
        assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
        assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
    }
}
Also used : InputStream(java.io.InputStream) Stanza(org.jivesoftware.smack.packet.Stanza) OutputStream(java.io.OutputStream) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 29 with Protocol

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

the class Socks5ByteStreamRequestTest method shouldNotTimeoutIfFirstSocks5ProxyDoesNotRespond.

/**
 * If the SOCKS5 Bytestream request contains multiple SOCKS5 proxies and the first one doesn't
 * respond, the connection attempt to this proxy should not consume the whole timeout for
 * connecting to the proxies.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldNotTimeoutIfFirstSocks5ProxyDoesNotRespond() throws Exception {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        // create a fake SOCKS5 proxy that doesn't respond to a request
        ServerSocket unresponsiveSocks5Socket = NetworkUtil.getSocketOnLoopback();
        try {
            // build SOCKS5 Bytestream initialization request
            Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
            bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, unresponsiveSocks5Socket.getLocalPort());
            bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, socks5Proxy.getPort());
            // create test data for stream
            byte[] data = new byte[] { 1, 2, 3 };
            // 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);
            // set timeouts
            byteStreamRequest.setTotalConnectTimeout(2000);
            byteStreamRequest.setMinimumConnectTimeout(1000);
            // accept the stream (this is the call that is tested here)
            InputStream inputStream = byteStreamRequest.accept().getInputStream();
            // assert that client tries to connect to dumb SOCKS5 proxy
            Socket socket = unresponsiveSocks5Socket.accept();
            assertNotNull(socket);
            // create digest to get the socket opened by target
            String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
            // test stream by sending some data
            OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
            outputStream.write(data);
            // verify that data is transferred correctly
            byte[] result = new byte[3];
            inputStream.read(result);
            assertArrayEquals(data, result);
            // verify targets response
            assertEquals(1, protocol.getRequests().size());
            Stanza targetResponse = protocol.getRequests().remove(0);
            assertEquals(Bytestream.class, targetResponse.getClass());
            assertEquals(initiatorJID, targetResponse.getTo());
            assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
            assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
        } finally {
            unresponsiveSocks5Socket.close();
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Stanza(org.jivesoftware.smack.packet.Stanza) ServerSocket(java.net.ServerSocket) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Protocol(org.jivesoftware.util.Protocol) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.jupiter.api.Test)

Example 30 with Protocol

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

the class PubSubNodeTest method getAffiliationsAsOwnerTest.

@Test
public void getAffiliationsAsOwnerTest() throws InterruptedException, SmackException, IOException, XMPPException, Exception {
    Protocol protocol = new Protocol();
    XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, JidTestUtil.FULL_JID_1_RESOURCE_1);
    PubSubManager mgr = new PubSubManager(connection, JidTestUtil.PUBSUB_EXAMPLE_ORG);
    Node testNode = new LeafNode(mgr, "princely_musings");
    List<Affiliation> affiliations = Arrays.asList(new Affiliation(JidTestUtil.BARE_JID_1, Affiliation.Type.member), new Affiliation(JidTestUtil.BARE_JID_2, Affiliation.Type.publisher));
    AffiliationsExtension affiliationsExtension = new AffiliationsExtension(AffiliationNamespace.owner, affiliations);
    PubSub response = new PubSub(JidTestUtil.PUBSUB_EXAMPLE_ORG, IQ.Type.result, PubSubNamespace.owner);
    response.addExtension(affiliationsExtension);
    protocol.addResponse(response);
    List<Affiliation> returnedAffiliations = testNode.getAffiliationsAsOwner();
    PubSub request = (PubSub) protocol.getRequests().get(0);
    assertEquals("http://jabber.org/protocol/pubsub#owner", request.getChildElementNamespace());
    assertEquals("pubsub", request.getChildElementName());
    Affiliation affiliationOne = returnedAffiliations.get(0);
    assertEquals(affiliationOne.getJid(), JidTestUtil.BARE_JID_1);
    assertEquals(affiliationOne.getAffiliation(), Affiliation.Type.member);
    Affiliation affiliationTwo = returnedAffiliations.get(1);
    assertEquals(affiliationTwo.getJid(), JidTestUtil.BARE_JID_2);
    assertEquals(affiliationTwo.getAffiliation(), Affiliation.Type.publisher);
}
Also used : PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

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