From 82dd5a11131b3bd3641316b87aa279d806c970b0 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Wed, 29 Jun 2016 17:06:30 +0200
Subject: Fail file-transfer if proxy session of candidate does not exist

Test-Information:

Tested with a buggy XMPP client that calculates S5B DSTADDR
in a non-standard way. Previously this lead to Swift crashing
on an assert. Now Swift just indicates file-transfer failure
to the other party.

Change-Id: I4ed47f9647a1851f2a5101657bcd569c106d638f

diff --git a/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp b/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
index 9428d0b..aefafd9 100644
--- a/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
+++ b/Swiften/FileTransfer/DefaultFileTransferTransporter.cpp
@@ -204,8 +204,12 @@ std::shared_ptr<TransportSession> DefaultFileTransferTransporter::createLocalCan
     std::shared_ptr<TransportSession> transportSession;
     if (candidate.type == JingleS5BTransportPayload::Candidate::ProxyType) {
         SOCKS5BytestreamClientSession::ref proxySession = s5bProxy->getProxySessionAndCloseOthers(candidate.jid, getLocalCandidateSOCKS5DstAddr());
-        assert(proxySession);
-        transportSession = std::make_shared<S5BTransportSession<SOCKS5BytestreamClientSession> >(proxySession, stream);
+        if (proxySession) {
+            transportSession = std::make_shared<S5BTransportSession<SOCKS5BytestreamClientSession> >(proxySession, stream);
+        }
+        else {
+            SWIFT_LOG(error) << "Failed obtaining proxy session with candidate JID " << candidate.jid << " and dstAddr " << getLocalCandidateSOCKS5DstAddr() << "." << std::endl;
+        }
     }
 
     if (!transportSession) {
@@ -227,8 +231,12 @@ std::shared_ptr<TransportSession> DefaultFileTransferTransporter::createLocalCan
     std::shared_ptr<TransportSession> transportSession;
     if (candidate.type == JingleS5BTransportPayload::Candidate::ProxyType) {
         SOCKS5BytestreamClientSession::ref proxySession = s5bProxy->getProxySessionAndCloseOthers(candidate.jid, getLocalCandidateSOCKS5DstAddr());
-        assert(proxySession);
-        transportSession = std::make_shared<S5BTransportSession<SOCKS5BytestreamClientSession> >(proxySession, stream);
+        if (proxySession) {
+            transportSession = std::make_shared<S5BTransportSession<SOCKS5BytestreamClientSession> >(proxySession, stream);
+        }
+        else {
+            SWIFT_LOG(error) << "Failed obtaining proxy session with candidate JID " << candidate.jid << " and dstAddr " << getLocalCandidateSOCKS5DstAddr() << "." << std::endl;
+        }
     }
 
     if (!transportSession) {
diff --git a/Swiften/FileTransfer/FailingTransportSession.cpp b/Swiften/FileTransfer/FailingTransportSession.cpp
new file mode 100644
index 0000000..d1eb6a6
--- /dev/null
+++ b/Swiften/FileTransfer/FailingTransportSession.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2016 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swiften/FileTransfer/FailingTransportSession.h>
+
+#include <Swiften/Base/Log.h>
+
+namespace Swift {
+
+FailingTransportSession::~FailingTransportSession() {
+}
+
+void FailingTransportSession::start() {
+    SWIFT_LOG(error) << "Trying to start failing transport." << std::endl;
+    onFinished(FileTransferError(FileTransferError::PeerError));
+}
+
+void FailingTransportSession::stop() {
+}
+
+}
diff --git a/Swiften/FileTransfer/FailingTransportSession.h b/Swiften/FileTransfer/FailingTransportSession.h
index f0f21eb..6e0e16e 100644
--- a/Swiften/FileTransfer/FailingTransportSession.h
+++ b/Swiften/FileTransfer/FailingTransportSession.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Isode Limited.
+ * Copyright (c) 2015-2016 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -13,16 +13,10 @@ namespace Swift {
 
 class SWIFTEN_API FailingTransportSession : public TransportSession {
     public:
-        virtual ~FailingTransportSession() {
-        }
+        virtual ~FailingTransportSession();
 
-        virtual void start() SWIFTEN_OVERRIDE {
-            assert(false);
-            onFinished(FileTransferError(FileTransferError::PeerError));
-        }
-
-        virtual void stop() SWIFTEN_OVERRIDE {
-        }
+        virtual void start() SWIFTEN_OVERRIDE;
+        virtual void stop() SWIFTEN_OVERRIDE;
 };
 
 }
diff --git a/Swiften/FileTransfer/SConscript b/Swiften/FileTransfer/SConscript
index 83f229b..d49b596 100644
--- a/Swiften/FileTransfer/SConscript
+++ b/Swiften/FileTransfer/SConscript
@@ -2,51 +2,52 @@ Import("swiften_env", "env")
 
 sources = [
         "ByteArrayReadBytestream.cpp",
-        "OutgoingFileTransfer.cpp",
-        "OutgoingJingleFileTransfer.cpp",
-        "OutgoingFileTransferManager.cpp",
-        "IncomingFileTransfer.cpp",
-        "IncomingJingleFileTransfer.cpp",
-        "IncomingFileTransferManager.cpp",
-        "JingleFileTransfer.cpp",
-        "FileTransferOptions.cpp",
-        "FileTransferTransporter.cpp",
-        "FileTransferTransporterFactory.cpp",
         "DefaultFileTransferTransporter.cpp",
         "DefaultFileTransferTransporterFactory.cpp",
-        "RemoteJingleTransportCandidateSelector.cpp",
-        "LocalJingleTransportCandidateGenerator.cpp",
-        "ReadBytestream.cpp",
-        "WriteBytestream.cpp",
+        "FailingTransportSession.cpp",
         "FileReadBytestream.cpp",
-        "FileWriteBytestream.cpp",
         "FileTransfer.cpp",
-        "TransportSession.cpp",
-        "IBBSendTransportSession.cpp",
+        "FileTransferManager.cpp",
+        "FileTransferManagerImpl.cpp",
+        "FileTransferOptions.cpp",
+        "FileTransferTransporter.cpp",
+        "FileTransferTransporterFactory.cpp",
+        "FileWriteBytestream.cpp",
+        "IBBReceiveSession.cpp",
         "IBBReceiveTransportSession.cpp",
+        "IBBSendSession.cpp",
+        "IBBSendTransportSession.cpp",
+        "IncomingFileTransfer.cpp",
+        "IncomingFileTransferManager.cpp",
+        "IncomingJingleFileTransfer.cpp",
+        "IncrementalBytestreamHashCalculator.cpp",
+        "JingleFileTransfer.cpp",
+        "LocalJingleTransportCandidateGenerator.cpp",
+        "OutgoingFileTransfer.cpp",
+        "OutgoingFileTransferManager.cpp",
+        "OutgoingJingleFileTransfer.cpp",
+        "ReadBytestream.cpp",
+        "RemoteJingleTransportCandidateSelector.cpp",
         "SOCKS5BytestreamClientSession.cpp",
-        "SOCKS5BytestreamServerManager.cpp",
-        "SOCKS5BytestreamServer.cpp",
-        "SOCKS5BytestreamServerSession.cpp",
-        "SOCKS5BytestreamRegistry.cpp",
         "SOCKS5BytestreamProxiesManager.cpp",
         "SOCKS5BytestreamProxyFinder.cpp",
-        "IBBSendSession.cpp",
-        "IBBReceiveSession.cpp",
-        "FileTransferManager.cpp",
-        "FileTransferManagerImpl.cpp",
-        "IncrementalBytestreamHashCalculator.cpp",
-        "SOCKS5BytestreamServerResourceUser.cpp",
+        "SOCKS5BytestreamRegistry.cpp",
+        "SOCKS5BytestreamServer.cpp",
+        "SOCKS5BytestreamServerManager.cpp",
         "SOCKS5BytestreamServerPortForwardingUser.cpp",
+        "SOCKS5BytestreamServerResourceUser.cpp",
+        "SOCKS5BytestreamServerSession.cpp",
+        "TransportSession.cpp",
+        "WriteBytestream.cpp",
     ]
 
 swiften_env.Append(SWIFTEN_OBJECTS = swiften_env.SwiftenObject(sources))
 
 env.Append(UNITTEST_SOURCES = [
-            File("UnitTest/SOCKS5BytestreamServerSessionTest.cpp"),
-            File("UnitTest/SOCKS5BytestreamClientSessionTest.cpp"),
-            File("UnitTest/IBBSendSessionTest.cpp"),
             File("UnitTest/IBBReceiveSessionTest.cpp"),
+            File("UnitTest/IBBSendSessionTest.cpp"),
             File("UnitTest/IncomingJingleFileTransferTest.cpp"),
             File("UnitTest/OutgoingJingleFileTransferTest.cpp"),
+            File("UnitTest/SOCKS5BytestreamClientSessionTest.cpp"),
+            File("UnitTest/SOCKS5BytestreamServerSessionTest.cpp"),
     ])
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp b/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp
index 6a738ee..367676b 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.cpp
@@ -102,6 +102,7 @@ std::shared_ptr<SOCKS5BytestreamClientSession> SOCKS5BytestreamProxiesManager::g
             i->second->stop();
         }
     }
+    SWIFT_LOG_ASSERT(activeSession, warning) << "No active session with matching ID found." << std::endl;
 
     proxySessions_.erase(sessionID);
 
-- 
cgit v0.10.2-6-g49f6