Search in sources :

Example 11 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class Socks5ByteStreamRequestTest method shouldNotBlacklistInvalidProxy.

/**
     * Target should not not blacklist any SOCKS5 proxies regardless of failing connections.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotBlacklistInvalidProxy() throws Exception {
    // disable blacklisting
    Socks5BytestreamRequest.setConnectFailureThreshold(0);
    // build SOCKS5 Bytestream initialization request
    Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
    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 < 10; i++) {
        try {
            // 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();
            fail("exception should be thrown");
        } catch (XMPPErrorException e) {
            assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
        }
        // 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(XMPPError.Condition.item_not_found, ((IQ) targetResponse).getError().getCondition());
    }
    // enable blacklisting
    Socks5BytestreamRequest.setConnectFailureThreshold(2);
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 12 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class Socks5ByteStreamRequestTest method shouldFailIfRequestHasInvalidStreamHosts.

/**
     * Accepting a SOCKS5 Bytestream request should fail if target is not able to connect to any of
     * the provided SOCKS5 proxies.
     * 
     * @throws Exception
     */
@Test
public void shouldFailIfRequestHasInvalidStreamHosts() throws Exception {
    try {
        // build SOCKS5 Bytestream initialization request
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
        // add proxy that is not running
        bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7778);
        // 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)
        byteStreamRequest.accept();
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
    }
    // 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(XMPPError.Condition.item_not_found, ((IQ) targetResponse).getError().getCondition());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 13 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class Socks5ClientForInitiatorTest method shouldFailIfActivateSocks5ProxyFails.

/**
     * If the initiator can connect to a SOCKS5 proxy but activating the stream fails an exception
     * should be thrown.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldFailIfActivateSocks5ProxyFails() throws Exception {
    // build error response as reply to the stream activation
    IQ error = new ErrorIQ(XMPPError.getBuilder(XMPPError.Condition.internal_server_error));
    error.setFrom(proxyJID);
    error.setTo(initiatorJID);
    protocol.addResponse(error, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    // start a local SOCKS5 proxy
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(proxyPort);
    socks5Proxy.start();
    StreamHost streamHost = new StreamHost(proxyJID, 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(10000);
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        assertTrue(XMPPError.Condition.internal_server_error.equals(e.getXMPPError().getCondition()));
        protocol.verifyAll();
    }
    socks5Proxy.stop();
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Test(org.junit.Test)

Example 14 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class InBandBytestreamManagerTest method shouldFailIfTargetDoesNotSupportIBB.

/**
     * Invoking {@link InBandBytestreamManager#establishSession(org.jxmpp.jid.Jid)} should
     * throw an exception if the given target does not support in-band
     * bytestream.
     * @throws SmackException 
     * @throws XMPPException 
     * @throws InterruptedException 
     */
@Test
public void shouldFailIfTargetDoesNotSupportIBB() throws SmackException, XMPPException, InterruptedException {
    InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
    try {
        IQ errorIQ = IBBPacketUtils.createErrorIQ(targetJID, initiatorJID, XMPPError.Condition.feature_not_implemented);
        protocol.addResponse(errorIQ);
        // start In-Band Bytestream
        byteStreamManager.establishSession(targetJID);
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        assertEquals(XMPPError.Condition.feature_not_implemented, e.getXMPPError().getCondition());
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 15 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project camel by apache.

the class XmppEndpoint method resolveRoom.

/*
     * If there is no "@" symbol in the room, find the chat service JID and
     * return fully qualified JID for the room as room@conference.server.domain
     */
public String resolveRoom(XMPPConnection connection) throws XMPPException, SmackException {
    ObjectHelper.notEmpty(room, "room");
    if (room.indexOf('@', 0) != -1) {
        return room;
    }
    Iterator<String> iterator = MultiUserChat.getServiceNames(connection).iterator();
    if (!iterator.hasNext()) {
        throw new XMPPErrorException("Cannot find Multi User Chat service", new XMPPError(new XMPPError.Condition("Cannot find Multi User Chat service on connection: " + getConnectionMessage(connection))));
    }
    String chatServer = iterator.next();
    LOG.debug("Detected chat server: {}", chatServer);
    return room + "@" + chatServer;
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XMPPError(org.jivesoftware.smack.packet.XMPPError)

Aggregations

XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)27 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)10 IQ (org.jivesoftware.smack.packet.IQ)9 Test (org.junit.Test)9 IOException (java.io.IOException)7 SmackException (org.jivesoftware.smack.SmackException)7 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)7 Stanza (org.jivesoftware.smack.packet.Stanza)6 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 XMPPError (org.jivesoftware.smack.packet.XMPPError)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 InputStream (java.io.InputStream)4 XMPPException (org.jivesoftware.smack.XMPPException)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)3 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)2 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)2