Search in sources :

Example 16 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testSocks5BytestreamWithRemoteSocks5Proxy.

/**
     * Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
     * XMPP server.
     * <p>
     * This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
     * only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
     * configuration, see xmpp.proxy.transfer.required flag).
     * 
     * @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
     *         bytestream
     */
public void testSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
    // disable local socks5 proxy
    SmackConfiguration.setLocalSocks5ProxyEnabled(false);
    Socks5Proxy.getSocks5Proxy().stop();
    assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    final byte[] data = new byte[] { 1, 2, 3 };
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
    Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {

        public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
            InputStream inputStream;
            try {
                Socks5BytestreamSession session = request.accept();
                inputStream = session.getInputStream();
                byte[] receivedData = new byte[3];
                inputStream.read(receivedData);
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
    Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    OutputStream outputStream = session.getOutputStream();
    assertTrue(session.isMediated());
    // verify stream
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
    // reset default configuration
    SmackConfiguration.setLocalSocks5ProxyEnabled(true);
    Socks5Proxy.getSocks5Proxy().start();
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) Socks5BytestreamListener(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener) Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) InputStream(java.io.InputStream) SynchronousQueue(java.util.concurrent.SynchronousQueue) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)

Example 17 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testBiDirectionalSocks5BytestreamWithRemoteSocks5Proxy.

/**
     * Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
     * XMPP server. The established connection should transfer data bidirectional if the Socks5
     * proxy supports it.
     * <p>
     * Support for bidirectional Socks5 bytestream:
     * <ul>
     * <li>Openfire (3.6.4 and below) - no</li>
     * <li>ejabberd (2.0.5 and higher) - yes</li>
     * </ul>
     * <p>
     * This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
     * only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
     * configuration, see xmpp.proxy.transfer.required flag).
     * 
     * @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
     *         bytestream
     */
public void testBiDirectionalSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
    XMPPConnection initiatorConnection = getConnection(0);
    // disable local socks5 proxy
    SmackConfiguration.setLocalSocks5ProxyEnabled(false);
    Socks5Proxy.getSocks5Proxy().stop();
    assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
    XMPPConnection targetConnection = getConnection(1);
    // test data
    final byte[] data = new byte[] { 1, 2, 3 };
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
    Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {

        public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
            try {
                Socks5BytestreamSession session = request.accept();
                OutputStream outputStream = session.getOutputStream();
                outputStream.write(data);
                outputStream.flush();
                InputStream inputStream = session.getInputStream();
                byte[] receivedData = new byte[3];
                inputStream.read(receivedData);
                queue.put(receivedData);
                session.close();
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
    Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    assertTrue(session.isMediated());
    // verify stream
    final byte[] receivedData = new byte[3];
    final InputStream inputStream = session.getInputStream();
    FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {

        public Integer call() throws Exception {
            return inputStream.read(receivedData);
        }
    });
    Thread executor = new Thread(futureTask);
    executor.start();
    try {
        futureTask.get(2000, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        // reset default configuration
        SmackConfiguration.setLocalSocks5ProxyEnabled(true);
        Socks5Proxy.getSocks5Proxy().start();
        fail("Couldn't send data from target to inititator");
    }
    assertEquals("sent data not equal to received data", data, receivedData);
    OutputStream outputStream = session.getOutputStream();
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
    session.close();
    // reset default configuration
    SmackConfiguration.setLocalSocks5ProxyEnabled(true);
    Socks5Proxy.getSocks5Proxy().start();
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession) Socks5BytestreamListener(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener) FutureTask(java.util.concurrent.FutureTask) SynchronousQueue(java.util.concurrent.SynchronousQueue) TimeoutException(java.util.concurrent.TimeoutException)

Example 18 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class MultiUserChatTest method testManyResources.

public void testManyResources() throws Exception {
    // Create 5 more connections for user2
    XMPPTCPConnection[] conns = new XMPPConnection[5];
    for (int i = 0; i < conns.length; i++) {
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
        connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        conns[i] = new XMPPTCPConnection(connectionConfiguration);
        conns[i].connect();
        conns[i].login(getUsername(1), getPassword(1), "resource-" + i);
        Thread.sleep(20);
    }
    // Join the 5 connections to the same room
    MultiUserChat[] mucs = new MultiUserChat[5];
    for (int i = 0; i < mucs.length; i++) {
        mucs[i] = new MultiUserChat(conns[i], room);
        mucs[i].join("resource-" + i);
    }
    Thread.sleep(200);
    // Each connection has something to say
    for (int i = 0; i < mucs.length; i++) {
        mucs[i].sendMessage("I'm resource-" + i);
    }
    Thread.sleep(200);
    // Each connection leaves the room and closes the connection
    for (MultiUserChat muc1 : mucs) {
        muc1.leave();
    }
    Thread.sleep(200);
    for (int i = 0; i < mucs.length; i++) {
        conns[i].disconnect();
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 19 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class MultiUserChatTest method testInvitationWithMessage.

public void testInvitationWithMessage() {
    final String[] answer = new String[2];
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        // User3 is listening to MUC invitations
        MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {

            public void invitationReceived(XMPPConnection conn, String room, String inviter, String reason, String password, Message message) {
                // Indicate that the invitation was received
                answer[0] = reason;
                XHTMLExtension extension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
                assertNotNull("An extension was not found in the invitation", extension);
                answer[1] = (String) extension.getBodies().next();
            }
        });
        // User2 invites user3 to join to the room
        Message msg = new Message();
        XHTMLExtension xhtmlExtension = new XHTMLExtension();
        xhtmlExtension.addBody("<body>Meet me in this excellent room</body>");
        msg.addExtension(xhtmlExtension);
        muc2.invite(msg, getFullJID(2), "Meet me in this excellent room");
        Thread.sleep(350);
        assertEquals("Invitation was not received", "Meet me in this excellent room", answer[0]);
        assertEquals("Rejection was not received", "<body>Meet me in this excellent room</body>", answer[1]);
        // User2 leaves the room
        muc2.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XHTMLExtension(org.jivesoftware.smackx.packet.XHTMLExtension) XMPPException(org.jivesoftware.smack.XMPPException)

Example 20 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class MultiUserChatTest method testAnonymousParticipant.

public void testAnonymousParticipant() {
    try {
        // Anonymous user joins the new room
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
        XMPPTCPConnection anonConnection = new XMPPConnection(connectionConfiguration);
        anonConnection.connect();
        anonConnection.loginAnonymously();
        MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
        muc2.join("testbot2");
        Thread.sleep(400);
        // User1 checks the presence of Anonymous user in the room
        Presence presence = muc.getOccupantPresence(room + "/testbot2");
        assertNotNull("Presence of user2 in room is missing", presence);
        assertTrue("Presence mode of user2 is wrong", presence.getMode() == null || presence.getMode() == Presence.Mode.available);
        // Anonymous user leaves the room
        muc2.leave();
        anonConnection.disconnect();
        Thread.sleep(250);
        // User1 checks the presence of Anonymous user in the room
        presence = muc.getOccupantPresence(room + "/testbot2");
        assertNull("Presence of participant testbotII still exists", presence);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

XMPPConnection (org.jivesoftware.smack.XMPPConnection)64 XMPPException (org.jivesoftware.smack.XMPPException)14 Message (org.jivesoftware.smack.packet.Message)9 SmackException (org.jivesoftware.smack.SmackException)7 IQ (org.jivesoftware.smack.packet.IQ)7 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 SynchronousQueue (java.util.concurrent.SynchronousQueue)6 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)6 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)6 TimeoutException (java.util.concurrent.TimeoutException)5 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)4 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)4 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)4 Packet (org.jivesoftware.smack.packet.Packet)4 Jid (org.jxmpp.jid.Jid)4