Search in sources :

Example 21 with Protocol

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

the class Socks5ByteStreamManagerTest method shouldDisableService.

/**
 * The SOCKS5 Bytestream feature should be removed form the service discovery manager if Socks5
 * bytestream feature is disabled.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPErrorException if there was an XMPP error returned.
 */
@Test
public void shouldDisableService() throws XMPPErrorException, SmackException, InterruptedException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
    assertTrue(discoveryManager.includesFeature(Bytestream.NAMESPACE));
    byteStreamManager.disableService();
    assertFalse(discoveryManager.includesFeature(Bytestream.NAMESPACE));
}
Also used : XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) Test(org.junit.jupiter.api.Test)

Example 22 with Protocol

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

the class Socks5ByteStreamManagerTest method shouldFailIfNoSocks5ProxyFound1.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if XMPP
 * server doesn't return any proxies.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfNoSocks5ProxyFound1() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfNoSocks5ProxyFound1";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * 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 with no proxy items
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    // return the item with no proxy if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    SmackException e = assertThrows(SmackException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    });
    protocol.verifyAll();
    assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
Also used : DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 23 with Protocol

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

the class Socks5ByteStreamManagerTest method shouldNegotiateSocks5BytestreamAndTransferData.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should successfully
 * negotiate and return a SOCKS5 Bytestream connection.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldNegotiateSocks5BytestreamAndTransferData() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldNegotiateSocks5BytestreamAndTransferData";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * 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 a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    ServerSocket proxyServerSocket = NetworkUtil.getSocketOnLoopback();
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, proxyServerSocket.getLocalPort());
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, 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, new Verification<Bytestream, Bytestream>() {

        @Override
        public void verify(Bytestream request, Bytestream response) {
            assertEquals(response.getSessionID(), request.getSessionID());
            assertEquals(1, request.getStreamHosts().size());
            StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
            assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
        }
    }, 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);
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy(proxyServerSocket)) {
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // finally call the method that should be tested
        OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
        // test the established bytestream
        InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
        byte[] data = new byte[] { 1, 2, 3 };
        outputStream.write(data);
        byte[] result = new byte[3];
        inputStream.read(result);
        assertArrayEquals(data, result);
    }
    protocol.verifyAll();
}
Also used : InputStream(java.io.InputStream) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) OutputStream(java.io.OutputStream) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) ServerSocket(java.net.ServerSocket) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Test(org.junit.jupiter.api.Test)

Example 24 with Protocol

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

the class Socks5ClientForInitiatorTest method shouldSuccessfullyConnectThroughLocalSocks5Proxy.

/**
 * Initiator and target should successfully connect to the local SOCKS5 proxy.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldSuccessfullyConnectThroughLocalSocks5Proxy() throws Exception {
    Protocol protocol = new Protocol();
    XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    // start a local SOCKS5 proxy
    Socks5Proxy socks5Proxy = new Socks5Proxy();
    socks5Proxy.start();
    try {
        // test data
        final byte[] data = new byte[] { 1, 2, 3 };
        // create digest
        final String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // allow connection of target with this digest
        socks5Proxy.addTransfer(digest);
        // build stream host information
        final StreamHost streamHost = new StreamHost(connection.getUser(), loopbackAddress, socks5Proxy.getPort());
        // target connects to local SOCKS5 proxy
        Thread targetThread = new Thread() {

            @Override
            public void run() {
                try {
                    Socks5Client targetClient = new Socks5Client(streamHost, digest);
                    Socket socket = targetClient.getSocket(10000);
                    socket.getOutputStream().write(data);
                } catch (Exception e) {
                    // TODO: This does not work.
                    fail(e.getMessage());
                }
            }
        };
        targetThread.start();
        // TODO: Replace this Thread.sleep().
        Thread.sleep(200);
        // initiator connects
        Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID);
        Socket socket = socks5Client.getSocket(GET_SOCKET_TIMEOUT);
        // verify test data
        InputStream in = socket.getInputStream();
        for (int i = 0; i < data.length; i++) {
            assertEquals(data[i], in.read());
        }
        targetThread.join();
        // assert no XMPP messages were sent
        protocol.verifyAll();
        socks5Proxy.removeTransfer(digest);
    } finally {
        socks5Proxy.stop();
    }
}
Also used : InputStream(java.io.InputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) SmackException(org.jivesoftware.smack.SmackException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Protocol(org.jivesoftware.util.Protocol) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Socket(java.net.Socket) Test(org.junit.jupiter.api.Test)

Example 25 with Protocol

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

the class Socks5ClientForInitiatorTest method shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy.

/**
 * If the target is not connected to the local SOCKS5 proxy an exception should be thrown.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception {
    Protocol protocol = new Protocol();
    XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    // start a local SOCKS5 proxy
    Socks5Proxy socks5Proxy = new Socks5Proxy();
    socks5Proxy.start();
    try {
        // build stream host information for local SOCKS5 proxy
        StreamHost streamHost = new StreamHost(connection.getUser(), loopbackAddress, socks5Proxy.getPort());
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID);
        try {
            socks5Client.getSocket(GET_SOCKET_TIMEOUT);
            fail("exception should be thrown");
        } catch (SmackException e) {
            assertTrue(e.getMessage().contains("target is not connected to SOCKS5 proxy"));
            // assert no XMPP messages were sent
            protocol.verifyAll();
        }
    } finally {
        socks5Proxy.stop();
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) 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