Search in sources :

Example 16 with SmackException

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

the class SmackIntegrationTestFramework method prepareEnvironment.

protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
    XMPPTCPConnection conOne = null;
    XMPPTCPConnection conTwo = null;
    XMPPTCPConnection conThree = null;
    try {
        conOne = getConnectedConnectionFor(AccountNum.One);
        conTwo = getConnectedConnectionFor(AccountNum.Two);
        conThree = getConnectedConnectionFor(AccountNum.Three);
    } catch (Exception e) {
        // TODO Reverse the order, i.e. conThree should be disconnected first.
        if (conOne != null) {
            conOne.disconnect();
        }
        if (conTwo != null) {
            conTwo.disconnect();
        }
        if (conThree != null) {
            conThree.disconnect();
        }
        throw e;
    }
    return new SmackIntegrationTestEnvironment(conOne, conTwo, conThree, testRunResult.testRunId, config);
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 17 with SmackException

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

the class FileTransferIntegrationTest method genericfileTransferTest.

private void genericfileTransferTest() throws Exception {
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    final FileTransferListener receiveListener = new FileTransferListener() {

        @Override
        public void fileTransferRequest(FileTransferRequest request) {
            byte[] dataReceived = null;
            IncomingFileTransfer ift = request.accept();
            try {
                InputStream is = ift.recieveFile();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                int nRead;
                byte[] buf = new byte[1024];
                while ((nRead = is.read(buf, 0, buf.length)) != -1) {
                    os.write(buf, 0, nRead);
                }
                os.flush();
                dataReceived = os.toByteArray();
                if (Arrays.equals(dataToSend, dataReceived)) {
                    resultSyncPoint.signal("Received data matches send data. \\o/");
                } else {
                    resultSyncPoint.signal(new Exception("Received data does not match"));
                }
            } catch (SmackException | IOException | XMPPErrorException | InterruptedException e) {
                resultSyncPoint.signal(e);
            }
        }
    };
    ftManagerTwo.addFileTransferListener(receiveListener);
    OutgoingFileTransfer oft = ftManagerOne.createOutgoingFileTransfer(conTwo.getUser());
    oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting");
    int duration = 0;
    while (!oft.isDone()) {
        switch(oft.getStatus()) {
            case error:
                throw new Exception("Filetransfer error: " + oft.getError());
            default:
                LOGGER.info("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress());
                break;
        }
        Thread.sleep(1000);
        if (++duration > MAX_FT_DURATION) {
            throw new Exception("Max duration reached");
        }
    }
    resultSyncPoint.waitForResult(MAX_FT_DURATION * 1000);
    ftManagerTwo.removeFileTransferListener(receiveListener);
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmackException(org.jivesoftware.smack.SmackException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint)

Example 18 with SmackException

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

the class Socks5ByteStreamManagerTest method shouldBlacklistNonSocks5Proxies.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if no
     * SOCKS5 proxy can be found. If it turns out that a proxy is not a SOCKS5 proxy it should not
     * be queried again.
     */
@Test
public void shouldBlacklistNonSocks5Proxies() {
    // disable clients local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyEnabled(false);
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    /**
         * create responses in the order they should be queried specified by the XEP-0065
         * specification
         */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo, 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 NOT being a Socks5
    // proxy
    DiscoverInfo proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("noproxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the proxy identity if proxy is queried
    protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    try {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    } catch (SmackException e) {
        protocol.verifyAll();
        assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
    /* retry to establish SOCKS5 Bytestream */
    // add responses for service discovery again
    protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    try {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    } catch (SmackException e) {
        /*
             * #verifyAll() tests if the number of requests and responses corresponds and should
             * fail if the invalid proxy is queried again
             */
        protocol.verifyAll();
        assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XMPPException(org.jivesoftware.smack.XMPPException) Test(org.junit.Test)

Example 19 with SmackException

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

the class Socks5ByteStreamManagerTest method shouldFailIfTargetUsesInvalidSocks5Proxy.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
     * proxy used by target is invalid.
     * @throws XmppStringprepException 
     */
@Test
public void shouldFailIfTargetUsesInvalidSocks5Proxy() throws XmppStringprepException {
    // disable clients local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyEnabled(false);
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    /**
         * create responses in the order they should be queried specified by the XEP-0065
         * specification
         */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo, 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
    DiscoverInfo 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, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build used stream host response with unknown proxy
    Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
    streamHostUsedPacket.setSessionID(sessionID);
    streamHostUsedPacket.setUsedHost(JidCreate.from("invalid.proxy"));
    // return used stream host info as response to the bytestream initiation
    protocol.addResponse(streamHostUsedPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    try {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    } catch (SmackException e) {
        protocol.verifyAll();
        assertTrue(e.getMessage().contains("Remote user responded with unknown host"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XMPPException(org.jivesoftware.smack.XMPPException) Test(org.junit.Test)

Example 20 with SmackException

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

the class Socks5ClientTest method shouldCloseSocketIfServerRepliesWithError.

/**
     * The SOCKS5 client should close connection if server replies with an error.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldCloseSocketIfServerRepliesWithError() throws Exception {
    // start thread to connect to SOCKS5 proxy
    Thread serverThread = new Thread() {

        @Override
        public void run() {
            StreamHost streamHost = new StreamHost(proxyJID, serverAddress, serverPort);
            Socks5Client socks5Client = new Socks5Client(streamHost, digest);
            try {
                socks5Client.getSocket(10000);
                fail("exception should be thrown");
            } catch (SmackException e) {
                assertTrue(e.getMessage().contains("SOCKS5 negotiation failed"));
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    serverThread.start();
    Socket socket = serverSocket.accept();
    DataInputStream in = new DataInputStream(socket.getInputStream());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // validate authentication request
    // version
    assertEquals((byte) 0x05, (byte) in.read());
    // number of supported auth methods
    assertEquals((byte) 0x01, (byte) in.read());
    // no-authentication method
    assertEquals((byte) 0x00, (byte) in.read());
    // respond that no no-authentication method is used
    out.write(new byte[] { (byte) 0x05, (byte) 0x00 });
    out.flush();
    Socks5Utils.receiveSocks5Message(in);
    // reply with full SOCKS5 message with an error code (01 = general SOCKS server
    // failure)
    out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00, (byte) 0x03 });
    byte[] address = digest.getBytes(StringUtils.UTF8);
    out.write(address.length);
    out.write(address);
    out.write(new byte[] { (byte) 0x00, (byte) 0x00 });
    out.flush();
    // wait for client to shutdown
    serverThread.join();
    // assert socket is closed
    assertEquals(-1, in.read());
}
Also used : DataOutputStream(java.io.DataOutputStream) SmackException(org.jivesoftware.smack.SmackException) DataInputStream(java.io.DataInputStream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) SmackException(org.jivesoftware.smack.SmackException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

SmackException (org.jivesoftware.smack.SmackException)48 IOException (java.io.IOException)20 XMPPException (org.jivesoftware.smack.XMPPException)13 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)10 Test (org.junit.Test)9 Socket (java.net.Socket)8 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)6 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)5 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ConnectException (java.net.ConnectException)4 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServerSocket (java.net.ServerSocket)3 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)3