Search in sources :

Example 1 with Verification

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

the class Socks5ByteStreamManagerTest method shouldPrioritizeSecondSocks5ProxyOnSecondAttempt.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} the first time
 * should successfully negotiate a SOCKS5 Bytestream via the second SOCKS5 proxy and should
 * prioritize this proxy for a second SOCKS5 Bytestream negotiation.
 * @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 shouldPrioritizeSecondSocks5ProxyOnSecondAttempt() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldPrioritizeSecondSocks5ProxyOnSecondAttempt";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    assertTrue(byteStreamManager.isProxyPrioritizationEnabled());
    Verification<Bytestream, Bytestream> streamHostUsedVerification1 = new Verification<Bytestream, Bytestream>() {

        @Override
        public void verify(Bytestream request, Bytestream response) {
            assertEquals(response.getSessionID(), request.getSessionID());
            assertEquals(2, request.getStreamHosts().size());
            // verify that the used stream host is the second in list
            StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[1];
            assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
        }
    };
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        createResponses(protocol, sessionID, streamHostUsedVerification1, socks5Proxy);
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // 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();
        Verification<Bytestream, Bytestream> streamHostUsedVerification2 = new Verification<Bytestream, Bytestream>() {

            @Override
            public void verify(Bytestream request, Bytestream response) {
                assertEquals(response.getSessionID(), request.getSessionID());
                assertEquals(2, request.getStreamHosts().size());
                // verify that the used stream host is the first in list
                StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
                assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
            }
        };
        createResponses(protocol, sessionID, streamHostUsedVerification2, socks5Proxy);
        // call the method that should be tested again
        outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
        // test the established bytestream
        inputStream = socks5Proxy.getSocket(digest).getInputStream();
        outputStream.write(data);
        inputStream.read(result);
        assertArrayEquals(data, result);
        protocol.verifyAll();
    }
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Verification(org.jivesoftware.util.Verification) 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)

Example 2 with Verification

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

the class InBandBytestreamSessionTest method shouldSendDataCorrectly.

/**
 * Test that the data is correctly chunked.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldSendDataCorrectly() throws Exception {
    // create random data
    Random rand = new Random();
    final byte[] controlData = new byte[256 * blockSize];
    rand.nextBytes(controlData);
    // compares the data of each packet with the control data
    Verification<Data, IQ> dataVerification = new Verification<Data, IQ>() {

        @Override
        public void verify(Data request, IQ response) {
            byte[] decodedData = request.getDataPacketExtension().getDecodedData();
            UInt16 seq = request.getDataPacketExtension().getSeq();
            for (int i = 0; i < decodedData.length; i++) {
                assertEquals(controlData[(seq.intValue() * blockSize) + i], decodedData[i]);
            }
        }
    };
    // set acknowledgments for the data packets
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    for (int i = 0; i < controlData.length / blockSize; i++) {
        protocol.addResponse(resultIQ, incrementingSequence, dataVerification);
    }
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    OutputStream outputStream = session.getOutputStream();
    outputStream.write(controlData);
    outputStream.flush();
    protocol.verifyAll();
}
Also used : Random(java.util.Random) OutputStream(java.io.OutputStream) IQ(org.jivesoftware.smack.packet.IQ) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) Verification(org.jivesoftware.util.Verification) UInt16(org.jivesoftware.smack.datatypes.UInt16) Test(org.junit.jupiter.api.Test)

Example 3 with Verification

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

the class Socks5ByteStreamManagerTest method shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} the first time
 * should successfully negotiate a SOCKS5 Bytestream via the second SOCKS5 proxy. The second
 * negotiation should run in the same manner if prioritization is disabled.
 *
 * @throws IOException if an I/O error occurred.
 * @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.
 */
@Test
public void shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled() throws IOException, SmackException, InterruptedException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldNotPrioritizeSocks5ProxyIfPrioritizationDisabled";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    byteStreamManager.setProxyPrioritizationEnabled(false);
    assertFalse(byteStreamManager.isProxyPrioritizationEnabled());
    Verification<Bytestream, Bytestream> streamHostUsedVerification = new Verification<Bytestream, Bytestream>() {

        @Override
        public void verify(Bytestream request, Bytestream response) {
            assertEquals(response.getSessionID(), request.getSessionID());
            assertEquals(2, request.getStreamHosts().size());
            // verify that the used stream host is the second in list
            StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[1];
            assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
        }
    };
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        createResponses(protocol, sessionID, streamHostUsedVerification, socks5Proxy);
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // 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();
        createResponses(protocol, sessionID, streamHostUsedVerification, socks5Proxy);
        // call the method that should be tested again
        outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
        // test the established bytestream
        inputStream = socks5Proxy.getSocket(digest).getInputStream();
        outputStream.write(data);
        inputStream.read(result);
        assertArrayEquals(data, result);
    }
    protocol.verifyAll();
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Verification(org.jivesoftware.util.Verification) 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

OutputStream (java.io.OutputStream)3 Verification (org.jivesoftware.util.Verification)3 Test (org.junit.jupiter.api.Test)3 InputStream (java.io.InputStream)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)2 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)2 Protocol (org.jivesoftware.util.Protocol)2 Random (java.util.Random)1 UInt16 (org.jivesoftware.smack.datatypes.UInt16)1 IQ (org.jivesoftware.smack.packet.IQ)1 Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)1