Search in sources :

Example 11 with Socks5BytestreamRequest

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest 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 {
    // start a local SOCKS5 proxy
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
    // create a fake SOCKS5 proxy that doesn't respond to a request
    ServerSocket serverSocket = new ServerSocket(7779);
    // build SOCKS5 Bytestream initialization request
    Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
    bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7779);
    bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7778);
    // 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 = serverSocket.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());
    serverSocket.close();
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Stanza(org.jivesoftware.smack.packet.Stanza) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 Stanza (org.jivesoftware.smack.packet.Stanza)6 Test (org.junit.Test)6 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)4 IQ (org.jivesoftware.smack.packet.IQ)4 Socks5BytestreamRequest (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest)4 Socks5BytestreamSession (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 XMPPException (org.jivesoftware.smack.XMPPException)3 Socks5BytestreamListener (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener)3 Socks5BytestreamManager (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager)3 IOException (java.io.IOException)1 PushbackInputStream (java.io.PushbackInputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 FutureTask (java.util.concurrent.FutureTask)1