diff options
Diffstat (limited to 'Swiften/QA')
26 files changed, 1731 insertions, 1715 deletions
diff --git a/Swiften/QA/ClientTest/ClientTest.cpp b/Swiften/QA/ClientTest/ClientTest.cpp index eb116e9..5cb1765 100644 --- a/Swiften/QA/ClientTest/ClientTest.cpp +++ b/Swiften/QA/ClientTest/ClientTest.cpp @@ -1,98 +1,99 @@ /* - * Copyright (c) 2010-2015 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <boost/bind.hpp> -#include <boost/thread.hpp> #include <iostream> +#include <thread> + +#include <boost/bind.hpp> #include <Swiften/Client/Client.h> -#include <Swiften/Network/TimerFactory.h> -#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/EventLoop/EventLoop.h> #include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Network/TimerFactory.h> #include <Swiften/Roster/GetRosterRequest.h> -#include <Swiften/Client/ClientXMLTracer.h> using namespace Swift; static SimpleEventLoop eventLoop; static BoostNetworkFactories networkFactories(&eventLoop); -static Client* client = 0; +static Client* client = nullptr; static bool rosterReceived = false; enum TestStage { - FirstConnect, - Reconnect + FirstConnect, + Reconnect }; static TestStage stage; static ClientOptions options; static void handleDisconnected(boost::optional<ClientError> e) { - std::cout << "Disconnected: " << (e ? e.get().getType() : ClientError::UnknownError) << std::endl; - if (stage == FirstConnect) { - stage = Reconnect; - client->connect(options); - } - else { - eventLoop.stop(); - } + std::cout << "Disconnected: " << (e ? e.get().getType() : ClientError::UnknownError) << std::endl; + if (stage == FirstConnect) { + stage = Reconnect; + client->connect(options); + } + else { + eventLoop.stop(); + } } -static void handleRosterReceived(boost::shared_ptr<Payload>) { - rosterReceived = true; - std::cout << "Disconnecting" << std::endl; - client->disconnect(); +static void handleRosterReceived(std::shared_ptr<Payload>) { + rosterReceived = true; + std::cout << "Disconnecting" << std::endl; + client->disconnect(); } static void handleConnected() { - std::cout << "Connected" << std::endl; - rosterReceived = false; - GetRosterRequest::ref rosterRequest = GetRosterRequest::create(client->getIQRouter()); - rosterRequest->onResponse.connect(boost::bind(&handleRosterReceived, _1)); - rosterRequest->send(); + std::cout << "Connected" << std::endl; + rosterReceived = false; + GetRosterRequest::ref rosterRequest = GetRosterRequest::create(client->getIQRouter()); + rosterRequest->onResponse.connect(boost::bind(&handleRosterReceived, _1)); + rosterRequest->send(); } int main(int, char**) { - char* jid = getenv("SWIFT_CLIENTTEST_JID"); - if (!jid) { - std::cerr << "Please set the SWIFT_CLIENTTEST_JID environment variable" << std::endl; - return -1; - } - char* pass = getenv("SWIFT_CLIENTTEST_PASS"); - if (!pass) { - std::cerr << "Please set the SWIFT_CLIENTTEST_PASS environment variable" << std::endl; - return -1; - } - - char* boshHost = getenv("SWIFT_CLIENTTEST_BOSH_HOST"); - char* boshPort = getenv("SWIFT_CLIENTTEST_BOSH_PORT"); - char* boshPath = getenv("SWIFT_CLIENTTEST_BOSH_PATH"); - - if (boshHost && boshPort && boshPath) { - std::cout << "Using BOSH with URL: http://" << boshHost << ":" << boshPort << boshPath << std::endl; - options.boshURL = URL("http", boshHost, atoi(boshPort), boshPath); - } - - client = new Swift::Client(JID(jid), std::string(pass), &networkFactories); - ClientXMLTracer* tracer = new ClientXMLTracer(client, !options.boshURL.isEmpty()); - client->onConnected.connect(&handleConnected); - client->onDisconnected.connect(boost::bind(&handleDisconnected, _1)); - client->setAlwaysTrustCertificates(); - stage = FirstConnect; - client->connect(options); - - { - Timer::ref timer = networkFactories.getTimerFactory()->createTimer(60000); - timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); - timer->start(); - - eventLoop.run(); - } - - delete tracer; - delete client; - return !rosterReceived; + char* jid = getenv("SWIFT_CLIENTTEST_JID"); + if (!jid) { + std::cerr << "Please set the SWIFT_CLIENTTEST_JID environment variable" << std::endl; + return -1; + } + char* pass = getenv("SWIFT_CLIENTTEST_PASS"); + if (!pass) { + std::cerr << "Please set the SWIFT_CLIENTTEST_PASS environment variable" << std::endl; + return -1; + } + + char* boshHost = getenv("SWIFT_CLIENTTEST_BOSH_HOST"); + char* boshPort = getenv("SWIFT_CLIENTTEST_BOSH_PORT"); + char* boshPath = getenv("SWIFT_CLIENTTEST_BOSH_PATH"); + + if (boshHost && boshPort && boshPath) { + std::cout << "Using BOSH with URL: http://" << boshHost << ":" << boshPort << boshPath << std::endl; + options.boshURL = URL("http", boshHost, atoi(boshPort), boshPath); + } + + client = new Swift::Client(JID(jid), std::string(pass), &networkFactories); + ClientXMLTracer* tracer = new ClientXMLTracer(client, !options.boshURL.isEmpty()); + client->onConnected.connect(&handleConnected); + client->onDisconnected.connect(boost::bind(&handleDisconnected, _1)); + client->setAlwaysTrustCertificates(); + stage = FirstConnect; + client->connect(options); + + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(60000); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + timer->start(); + + eventLoop.run(); + } + + delete tracer; + delete client; + return !rosterReceived; } diff --git a/Swiften/QA/ClientTest/SConscript b/Swiften/QA/ClientTest/SConscript index 15a68c6..812c472 100644 --- a/Swiften/QA/ClientTest/SConscript +++ b/Swiften/QA/ClientTest/SConscript @@ -3,15 +3,15 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) - myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv = env.Clone() + myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) + myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) - for i in ["SWIFT_CLIENTTEST_JID", "SWIFT_CLIENTTEST_PASS"]: - if ARGUMENTS.get(i.lower(), False) : - myenv["ENV"][i] = ARGUMENTS[i.lower()] - elif os.environ.get(i, "") : - myenv["ENV"][i] = os.environ[i] + for i in ["SWIFT_CLIENTTEST_JID", "SWIFT_CLIENTTEST_PASS"]: + if ARGUMENTS.get(i.lower(), False) : + myenv["ENV"][i] = ARGUMENTS[i.lower()] + elif os.environ.get(i, "") : + myenv["ENV"][i] = os.environ[i] - tester = myenv.Program("ClientTest", ["ClientTest.cpp"]) - myenv.Test(tester, "system") + tester = myenv.Program("ClientTest", ["ClientTest.cpp"]) + myenv.Test(tester, "system") diff --git a/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp index 4da2672..664a87b 100644 --- a/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp +++ b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp @@ -1,81 +1,80 @@ /* - * Copyright (c) 2015 Isode Limited. + * Copyright (c) 2015-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <fstream> -#include <string> #include <map> +#include <string> -#include <boost/numeric/conversion/cast.hpp> #include <boost/filesystem.hpp> +#include <boost/numeric/conversion/cast.hpp> -#include <Swiften/Base/sleep.h> -#include <Swiften/Base/foreach.h> +#include <Swiften/Base/BoostRandomGenerator.h> +#include <Swiften/Base/Debug.h> #include <Swiften/Base/Log.h> -#include <Swiften/Client/ClientXMLTracer.h> +#include <Swiften/Base/sleep.h> #include <Swiften/Client/Client.h> -#include <Swiften/EventLoop/SimpleEventLoop.h> -#include <Swiften/Network/BoostNetworkFactories.h> -#include <Swiften/Network/Timer.h> -#include <Swiften/Network/TimerFactory.h> +#include <Swiften/Client/ClientXMLTracer.h> +#include <Swiften/Disco/ClientDiscoManager.h> #include <Swiften/Disco/EntityCapsProvider.h> #include <Swiften/Elements/Presence.h> -#include <Swiften/FileTransfer/ReadBytestream.h> -#include <Swiften/Base/BoostRandomGenerator.h> +#include <Swiften/EventLoop/SimpleEventLoop.h> #include <Swiften/FileTransfer/FileReadBytestream.h> -#include <Swiften/FileTransfer/OutgoingFileTransfer.h> #include <Swiften/FileTransfer/FileTransferManager.h> -#include <Swiften/Disco/ClientDiscoManager.h> #include <Swiften/FileTransfer/FileWriteBytestream.h> -#include <Swiften/Base/Debug.h> +#include <Swiften/FileTransfer/OutgoingFileTransfer.h> +#include <Swiften/FileTransfer/ReadBytestream.h> +#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Network/Timer.h> +#include <Swiften/Network/TimerFactory.h> using namespace Swift; static const std::string CLIENT_NAME = "Swiften FT Test"; static const std::string CLIENT_NODE = "http://swift.im"; -static boost::shared_ptr<SimpleEventLoop> eventLoop; -static boost::shared_ptr<BoostNetworkFactories> networkFactories; +static std::shared_ptr<SimpleEventLoop> eventLoop; +static std::shared_ptr<BoostNetworkFactories> networkFactories; BoostRandomGenerator randGen; enum Candidate { - InBandBytestream = 1, - S5B_Direct = 2, - S5B_Proxied = 4, - S5B_Assisted = 8, + InBandBytestream = 1, + S5B_Direct = 2, + S5B_Proxied = 4, + S5B_Assisted = 8, }; class ConcurrentFileTransferTest { - public: - ConcurrentFileTransferTest(int clientACandidates, int clientBCandidates) : clientACandidates_(clientACandidates), clientBCandidates_(clientBCandidates) { + public: + ConcurrentFileTransferTest(int clientACandidates, int clientBCandidates) : clientACandidates_(clientACandidates), clientBCandidates_(clientBCandidates) { - } + } - private: - int clientACandidates_; - boost::shared_ptr<Client> clientA_; - std::map<std::string, ByteArray> clientASendFiles_; + private: + int clientACandidates_; + std::shared_ptr<Client> clientA_; + std::map<std::string, ByteArray> clientASendFiles_; - int clientBCandidates_; - boost::shared_ptr<Client> clientB_; + int clientBCandidates_; + std::shared_ptr<Client> clientB_; }; /** - * This program tests the concurrent transfer of multiple file-transfers. - * + * This program tests the concurrent transfer of multiple file-transfers. + * */ int main(int argc, char** argv) { - int failedTests = 0; + int failedTests = 0; - if (!env("SWIFT_FILETRANSFERTEST_JID") && !env("SWIFT_FILETRANSFERTEST_PASS") && !env("SWIFT_FILETRANSFERTEST2_JID") && !env("SWIFT_FILETRANSFERTEST2_PASS")) { + if (!env("SWIFT_FILETRANSFERTEST_JID") && !env("SWIFT_FILETRANSFERTEST_PASS") && !env("SWIFT_FILETRANSFERTEST2_JID") && !env("SWIFT_FILETRANSFERTEST2_PASS")) { - return -1; - } + return -1; + } - return failedTests; + return failedTests; } diff --git a/Swiften/QA/ConcurrentFileTransferTest/SConscript b/Swiften/QA/ConcurrentFileTransferTest/SConscript index 1b4cdbc..94b37fd 100644 --- a/Swiften/QA/ConcurrentFileTransferTest/SConscript +++ b/Swiften/QA/ConcurrentFileTransferTest/SConscript @@ -3,15 +3,15 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) - myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv = env.Clone() + myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) + myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) - for i in ["SWIFT_FILETRANSFERTEST_JID", "SWIFT_FILETRANSFERTEST_PASS", "SWIFT_FILETRANSFERTEST2_JID", "SWIFT_FILETRANSFERTEST2_PASS"]: - if ARGUMENTS.get(i.lower(), False) : - myenv["ENV"][i] = ARGUMENTS[i.lower()] - elif os.environ.get(i, "") : - myenv["ENV"][i] = os.environ[i] + for i in ["SWIFT_FILETRANSFERTEST_JID", "SWIFT_FILETRANSFERTEST_PASS", "SWIFT_FILETRANSFERTEST2_JID", "SWIFT_FILETRANSFERTEST2_PASS"]: + if ARGUMENTS.get(i.lower(), False) : + myenv["ENV"][i] = ARGUMENTS[i.lower()] + elif os.environ.get(i, "") : + myenv["ENV"][i] = os.environ[i] - tester = myenv.Program("ConcurrentFileTransferTest", ["ConcurrentFileTransferTest.cpp"]) - myenv.Test(tester, "system") + tester = myenv.Program("ConcurrentFileTransferTest", ["ConcurrentFileTransferTest.cpp"]) + myenv.Test(tester, "system") diff --git a/Swiften/QA/DNSSDTest/DNSSDTest.cpp b/Swiften/QA/DNSSDTest/DNSSDTest.cpp index bd06445..ae2fafd 100644 --- a/Swiften/QA/DNSSDTest/DNSSDTest.cpp +++ b/Swiften/QA/DNSSDTest/DNSSDTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -28,120 +28,120 @@ using namespace Swift; template <typename DNSSDQuerierType> class DNSSDTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(DNSSDTest); - CPPUNIT_TEST(testPublish); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - eventLoop = new DummyEventLoop(); - querier = boost::shared_ptr<DNSSDQuerier>(new DNSSDQuerierType()); - querier->start(); - } - - void tearDown() { - querier->stop(); - querier.reset(); - delete eventLoop; - } - - void testPublish() { - boost::shared_ptr<DNSSDBrowseQuery> browseQuery = querier->createBrowseQuery(); - browseQuery->onServiceAdded.connect(boost::bind(&DNSSDTest::handleServiceAdded, this, _1)); - browseQuery->onServiceRemoved.connect(boost::bind(&DNSSDTest::handleServiceRemoved, this, _1)); - browseQuery->onError.connect(boost::bind(&DNSSDTest::handleBrowseError, this)); - browseQuery->startBrowsing(); - eventLoop->processEvents(); - - // Publish the service - LinkLocalServiceInfo info; - boost::shared_ptr<DNSSDRegisterQuery> registerQuery = querier->createRegisterQuery("DNSSDTest", 1234, info.toTXTRecord()); - registerQuery->onRegisterFinished.connect(boost::bind(&DNSSDTest::handleRegisterFinished, this, _1)); - registerQuery->registerService(); - - // Wait for a while - wait(); - - // Check that our registered queries are correct - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>((registered.size()))); - CPPUNIT_ASSERT_EQUAL(std::string("DNSSDTest"), registered[0].getName()); - CPPUNIT_ASSERT_EQUAL(std::string("local"), registered[0].getDomain()); - CPPUNIT_ASSERT_EQUAL(std::string("_presence._tcp"), registered[0].getType()); - - // Check that our browse query discovered us - std::sort(added.begin(), added.end()); - CPPUNIT_ASSERT(added.size() >= 1); - //for (size_t i = 0; i < added.size(); ++i) { - for (size_t i = 0; i < added.size(); ++i) { - CPPUNIT_ASSERT_EQUAL(std::string("DNSSDTest"), added[i].getName()); - CPPUNIT_ASSERT_EQUAL(std::string("local"), added[i].getDomain()); - CPPUNIT_ASSERT_EQUAL(std::string("_presence._tcp"), added[i].getType()); - CPPUNIT_ASSERT(added[i].getNetworkInterfaceID() != 0); - } - - // Resolve all added services - for (size_t i = 0; i < added.size(); ++i) { - resolvedServices.clear(); - boost::shared_ptr<DNSSDResolveServiceQuery> resolveServiceQuery = querier->createResolveServiceQuery(added[i]); - resolveServiceQuery->onServiceResolved.connect(boost::bind(&DNSSDTest::handleResolveFinished, this, _1)); - resolveServiceQuery->start(); - wait(); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(resolvedServices.size())); - resolveServiceQuery->stop(); - } - - // Unregister the service & check if the browse query picks this up - toRemove.clear(); - toRemove.insert(toRemove.begin(), added.begin(), added.end()); - registerQuery->unregisterService(); - while (!toRemove.empty()) { - Swift::sleep(100); - eventLoop->processEvents(); - } - - browseQuery->stopBrowsing(); - eventLoop->processEvents(); - } - - private: - void handleServiceAdded(const DNSSDServiceID& id) { - std::cout << "Service added: " << id.getNetworkInterfaceID() << std::endl; - added.push_back(id); - } - - void handleServiceRemoved(const DNSSDServiceID& id) { - CPPUNIT_ASSERT(std::find(toRemove.begin(), toRemove.end(), id) != toRemove.end()); - erase(toRemove, id); - } - - void handleRegisterFinished(boost::optional<DNSSDServiceID> id) { - if (id) { - registered.push_back(*id); - } - } - - void handleBrowseError() { - } - - void wait() { - for (int i = 0; i < SLEEP_INTERVALS; ++i) { - Swift::sleep(100); - eventLoop->processEvents(); - } - } - - void handleResolveFinished(const boost::optional<DNSSDResolveServiceQuery::Result>& result) { - CPPUNIT_ASSERT(result); - resolvedServices.push_back(*result); - } - - private: - DummyEventLoop* eventLoop; - boost::shared_ptr<DNSSDQuerier> querier; - std::vector<DNSSDServiceID> added; - std::vector<DNSSDServiceID> registered; - std::vector<DNSSDServiceID> toRemove; - std::vector<DNSSDResolveServiceQuery::Result> resolvedServices; + CPPUNIT_TEST_SUITE(DNSSDTest); + CPPUNIT_TEST(testPublish); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + eventLoop = new DummyEventLoop(); + querier = std::make_shared<DNSSDQuerier>(); + querier->start(); + } + + void tearDown() { + querier->stop(); + querier.reset(); + delete eventLoop; + } + + void testPublish() { + std::shared_ptr<DNSSDBrowseQuery> browseQuery = querier->createBrowseQuery(); + browseQuery->onServiceAdded.connect(boost::bind(&DNSSDTest::handleServiceAdded, this, _1)); + browseQuery->onServiceRemoved.connect(boost::bind(&DNSSDTest::handleServiceRemoved, this, _1)); + browseQuery->onError.connect(boost::bind(&DNSSDTest::handleBrowseError, this)); + browseQuery->startBrowsing(); + eventLoop->processEvents(); + + // Publish the service + LinkLocalServiceInfo info; + std::shared_ptr<DNSSDRegisterQuery> registerQuery = querier->createRegisterQuery("DNSSDTest", 1234, info.toTXTRecord()); + registerQuery->onRegisterFinished.connect(boost::bind(&DNSSDTest::handleRegisterFinished, this, _1)); + registerQuery->registerService(); + + // Wait for a while + wait(); + + // Check that our registered queries are correct + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>((registered.size()))); + CPPUNIT_ASSERT_EQUAL(std::string("DNSSDTest"), registered[0].getName()); + CPPUNIT_ASSERT_EQUAL(std::string("local"), registered[0].getDomain()); + CPPUNIT_ASSERT_EQUAL(std::string("_presence._tcp"), registered[0].getType()); + + // Check that our browse query discovered us + std::sort(added.begin(), added.end()); + CPPUNIT_ASSERT(added.size() >= 1); + //for (size_t i = 0; i < added.size(); ++i) { + for (size_t i = 0; i < added.size(); ++i) { + CPPUNIT_ASSERT_EQUAL(std::string("DNSSDTest"), added[i].getName()); + CPPUNIT_ASSERT_EQUAL(std::string("local"), added[i].getDomain()); + CPPUNIT_ASSERT_EQUAL(std::string("_presence._tcp"), added[i].getType()); + CPPUNIT_ASSERT(added[i].getNetworkInterfaceID() != 0); + } + + // Resolve all added services + for (size_t i = 0; i < added.size(); ++i) { + resolvedServices.clear(); + std::shared_ptr<DNSSDResolveServiceQuery> resolveServiceQuery = querier->createResolveServiceQuery(added[i]); + resolveServiceQuery->onServiceResolved.connect(boost::bind(&DNSSDTest::handleResolveFinished, this, _1)); + resolveServiceQuery->start(); + wait(); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(resolvedServices.size())); + resolveServiceQuery->stop(); + } + + // Unregister the service & check if the browse query picks this up + toRemove.clear(); + toRemove.insert(toRemove.begin(), added.begin(), added.end()); + registerQuery->unregisterService(); + while (!toRemove.empty()) { + Swift::sleep(100); + eventLoop->processEvents(); + } + + browseQuery->stopBrowsing(); + eventLoop->processEvents(); + } + + private: + void handleServiceAdded(const DNSSDServiceID& id) { + std::cout << "Service added: " << id.getNetworkInterfaceID() << std::endl; + added.push_back(id); + } + + void handleServiceRemoved(const DNSSDServiceID& id) { + CPPUNIT_ASSERT(std::find(toRemove.begin(), toRemove.end(), id) != toRemove.end()); + erase(toRemove, id); + } + + void handleRegisterFinished(boost::optional<DNSSDServiceID> id) { + if (id) { + registered.push_back(*id); + } + } + + void handleBrowseError() { + } + + void wait() { + for (int i = 0; i < SLEEP_INTERVALS; ++i) { + Swift::sleep(100); + eventLoop->processEvents(); + } + } + + void handleResolveFinished(const boost::optional<DNSSDResolveServiceQuery::Result>& result) { + CPPUNIT_ASSERT(result); + resolvedServices.push_back(*result); + } + + private: + DummyEventLoop* eventLoop; + std::shared_ptr<DNSSDQuerier> querier; + std::vector<DNSSDServiceID> added; + std::vector<DNSSDServiceID> registered; + std::vector<DNSSDServiceID> toRemove; + std::vector<DNSSDResolveServiceQuery::Result> resolvedServices; }; #ifdef HAVE_AVAHI diff --git a/Swiften/QA/DNSSDTest/SConscript b/Swiften/QA/DNSSDTest/SConscript index d35d06f..275a314 100644 --- a/Swiften/QA/DNSSDTest/SConscript +++ b/Swiften/QA/DNSSDTest/SConscript @@ -3,19 +3,19 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.MergeFlags(myenv["CHECKER_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - myenv.MergeFlags(myenv["BOOST_FLAGS"]) - myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) - if myenv.get("HAVE_BONJOUR", 0) : - myenv.Append(CPPDEFINES = "HAVE_BONJOUR") - elif myenv.get("HAVE_AVAHI", 0) : - myenv.Append(CPPDEFINES = ["HAVE_AVAHI"]) - myenv.MergeFlags(myenv["AVAHI_FLAGS"]) + myenv = env.Clone() + myenv.MergeFlags(myenv["CHECKER_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + myenv.MergeFlags(myenv["BOOST_FLAGS"]) + myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) + if myenv.get("HAVE_BONJOUR", 0) : + myenv.Append(CPPDEFINES = "HAVE_BONJOUR") + elif myenv.get("HAVE_AVAHI", 0) : + myenv.Append(CPPDEFINES = ["HAVE_AVAHI"]) + myenv.MergeFlags(myenv["AVAHI_FLAGS"]) - tester = myenv.Program("DNSSDTest", [ - "DNSSDTest.cpp", - ]) - myenv.Test(tester, "system") + tester = myenv.Program("DNSSDTest", [ + "DNSSDTest.cpp", + ]) + myenv.Test(tester, "system") diff --git a/Swiften/QA/FileTransferTest/FileTransferTest.cpp b/Swiften/QA/FileTransferTest/FileTransferTest.cpp index 7a50e9f..b627b30 100644 --- a/Swiften/QA/FileTransferTest/FileTransferTest.cpp +++ b/Swiften/QA/FileTransferTest/FileTransferTest.cpp @@ -13,7 +13,6 @@ #include <Swiften/Base/BoostRandomGenerator.h> #include <Swiften/Base/Debug.h> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Base/sleep.h> #include <Swiften/Client/Client.h> #include <Swiften/Client/ClientXMLTracer.h> @@ -35,366 +34,364 @@ using namespace Swift; static const std::string CLIENT_NAME = "Swiften FT Test"; static const std::string CLIENT_NODE = "http://swift.im"; -static boost::shared_ptr<SimpleEventLoop> eventLoop; -static boost::shared_ptr<BoostNetworkFactories> networkFactories; +static std::shared_ptr<SimpleEventLoop> eventLoop; +static std::shared_ptr<BoostNetworkFactories> networkFactories; BoostRandomGenerator randGen; enum Candidate { - InBandBytestream = 1, - S5B_Direct = 2, - S5B_Proxied = 4, - S5B_Assisted = 8, + InBandBytestream = 1, + S5B_Direct = 2, + S5B_Proxied = 4, + S5B_Assisted = 8, }; class FileTransferTest { - public: - FileTransferTest(int senderCandidates, int receiverCandidates) : senderCandidates_(senderCandidates), senderError_(FileTransferError::UnknownError), senderIsDone_(false), receiverCandidates_(receiverCandidates), receiverError_(FileTransferError::UnknownError), receiverIsDone_(false) { - sender_ = boost::make_shared<Client>(JID(getenv("SWIFT_FILETRANSFERTEST_JID")), getenv("SWIFT_FILETRANSFERTEST_PASS"), networkFactories.get()); - sender_->onDisconnected.connect(boost::bind(&FileTransferTest::handleSenderDisconnected, this, _1)); - sender_->onConnected.connect(boost::bind(&FileTransferTest::handleSenderConnected, this)); - sender_->getEntityCapsProvider()->onCapsChanged.connect(boost::bind(&FileTransferTest::handleSenderCapsChanged, this, _1)); - - receiver_ = boost::make_shared<Client>(JID(getenv("SWIFT_FILETRANSFERTEST2_JID")), getenv("SWIFT_FILETRANSFERTEST2_PASS"), networkFactories.get()); - receiver_->onConnected.connect(boost::bind(&FileTransferTest::handleReceiverConnected, this)); - receiver_->onDisconnected.connect(boost::bind(&FileTransferTest::handleReceiverDisconnected, this, _1)); - - senderTracer_ = new ClientXMLTracer(sender_.get()); - receiverTracer_ = new ClientXMLTracer(receiver_.get()); - - ClientOptions options; - options.useTLS = ClientOptions::NeverUseTLS; - options.useStreamCompression = false; - options.useStreamResumption = false; - options.useAcks = false; - - sender_->connect(options); - receiver_->connect(options); - - timeOut_ = networkFactories->getTimerFactory()->createTimer(60000); - timeOut_->onTick.connect(boost::bind(&FileTransferTest::handleTimeOut, this)); - - // Create randomly sized data to exchange. - sendFilePath_ = boost::filesystem::unique_path("ft_send_%%%%%%%%%%%%%%%%.bin"); - receiveFilePath_ = boost::filesystem::unique_path("ft_receive_%%%%%%%%%%%%%%%%.bin"); - - size_t size = 1024 + boost::numeric_cast<size_t>(randGen.generateRandomInteger(1024 * 10)); - sendData_.resize(size); - for (size_t n = 0; n < sendData_.size(); n++) { - sendData_[n] = boost::numeric_cast<unsigned char>(randGen.generateRandomInteger(255)); - } - - std::ofstream outfile(sendFilePath_.native().c_str(), std::ios::out | std::ios::binary); - outfile.write(reinterpret_cast<char *>(&sendData_[0]), boost::numeric_cast<ptrdiff_t>(sendData_.size())); - outfile.close(); - } - - ~FileTransferTest() { - timeOut_->stop(); - - delete senderTracer_; - delete receiverTracer_; - - // Free file-transfer objects so file handles are closed and files can be removed afterwards. - assert(!outgoingFileTransfer_ && incomingFileTransfers_.empty()); - - if(boost::filesystem::exists(sendFilePath_)) { - boost::filesystem::remove(sendFilePath_); - } - - if(boost::filesystem::exists(receiveFilePath_)) { - boost::filesystem::remove(receiveFilePath_); - } - } - - void handleSenderConnected() { - DiscoInfo discoInfo; - discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); - discoInfo.addFeature(DiscoInfo::JingleFeature); - discoInfo.addFeature(DiscoInfo::JingleFTFeature); - discoInfo.addFeature(DiscoInfo::Bytestream); - if (senderCandidates_ & InBandBytestream) { - discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); - } - if (senderCandidates_ & (S5B_Direct | S5B_Assisted | S5B_Proxied)) { - discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); - } - sender_->getDiscoManager()->setCapsNode(CLIENT_NODE); - sender_->getDiscoManager()->setDiscoInfo(discoInfo); - sender_->sendPresence(Presence::create()); - } - - void handleReceiverConnected() { - receiver_->getFileTransferManager()->onIncomingFileTransfer.connect(boost::bind(&FileTransferTest::handleReceiverIncomingFileTransfer, this, _1)); - - DiscoInfo discoInfo; - discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); - discoInfo.addFeature(DiscoInfo::JingleFeature); - discoInfo.addFeature(DiscoInfo::JingleFTFeature); - discoInfo.addFeature(DiscoInfo::Bytestream); - if (receiverCandidates_ & InBandBytestream) { - discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); - } - if (receiverCandidates_ & (S5B_Direct | S5B_Assisted | S5B_Proxied)) { - discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); - } - receiver_->getDiscoManager()->setCapsNode(CLIENT_NODE); - receiver_->getDiscoManager()->setDiscoInfo(discoInfo); - receiver_->getPresenceSender()->sendPresence(Presence::create()); - } - - void handleReceiverIncomingFileTransfer(IncomingFileTransfer::ref transfer) { - incomingFileTransfers_.push_back(transfer); - boost::shared_ptr<FileWriteBytestream> out = boost::make_shared<FileWriteBytestream>(receiveFilePath_.native()); - transfer->onFinished.connect(boost::bind(&FileTransferTest::handleReceiverFileTransferFinished, this, _1, out)); - - FileTransferOptions options; - options = options.withInBandAllowed(receiverCandidates_ & InBandBytestream); - options = options.withDirectAllowed(receiverCandidates_ & S5B_Direct); - options = options.withAssistedAllowed(receiverCandidates_ & S5B_Assisted); - options = options.withProxiedAllowed(receiverCandidates_ & S5B_Proxied); - - std::cout << "Incoming transfer options: " << "IBB (" << options.isInBandAllowed() << ")" << ", "; - std::cout << "S5B Direct (" << options.isDirectAllowed() << ")" << ", "; - std::cout << "S5B Assisted (" << options.isAssistedAllowed() << ")" << ", "; - std::cout << "S5B Proxied (" << options.isProxiedAllowed() << ")" << std::endl; - - transfer->accept(out, options); - } - - void handleSenderCapsChanged(const JID &jid) { - if (receiver_ && (receiver_->getJID().toBare() == jid.toBare())) { - boost::shared_ptr<FileReadBytestream> fileStream = boost::make_shared<FileReadBytestream>(sendFilePath_); - - FileTransferOptions options; - options = options.withInBandAllowed(senderCandidates_ & InBandBytestream); - options = options.withDirectAllowed(senderCandidates_ & S5B_Direct); - options = options.withAssistedAllowed(senderCandidates_ & S5B_Assisted); - options = options.withProxiedAllowed(senderCandidates_ & S5B_Proxied); - - std::cout << "Outgoing transfer options: " << "IBB (" << options.isInBandAllowed() << ")" << ", "; - std::cout << "S5B Direct (" << options.isDirectAllowed() << ")" << ", "; - std::cout << "S5B Assisted (" << options.isAssistedAllowed() << ")" << ", "; - std::cout << "S5B Proxied (" << options.isProxiedAllowed() << ")" << std::endl; - - outgoingFileTransfer_ = sender_->getFileTransferManager()->createOutgoingFileTransfer(jid.toBare(), sendFilePath_, "Some File!", fileStream, options); - - if (outgoingFileTransfer_) { - outgoingFileTransfer_->onFinished.connect(boost::bind(&FileTransferTest::handleSenderFileTransferFinished, this, _1)); - outgoingFileTransfer_->start(); - } else { - std::cout << "ERROR: No outgoing file transfer returned." << std::endl; - receiverIsDone_ = true; - senderIsDone_ = true; - endTest(); - } - } - } - - void handleReceiverFileTransferFinished(const boost::optional<FileTransferError>& error, boost::shared_ptr<FileWriteBytestream> out) { - out->close(); - receiverError_ = error; - receiverIsDone_ = true; - if (senderIsDone_) { - timeOut_->stop(); - timeOut_ = networkFactories->getTimerFactory()->createTimer(1000); - timeOut_->onTick.connect(boost::bind(&FileTransferTest::endTest, this)); - timeOut_->start(); - } - } - - void handleSenderDisconnected(const boost::optional<ClientError>& error) { - if (error) { - std::cout << this << " " << "handleSenderDisconnected: error: " << error.get() << std::endl; - } - - // All file-transfers related to a Client instance need to be freed - // *before* freeing the Client instance. - outgoingFileTransfer_.reset(); - - sender_.reset(); - if (!sender_ && !receiver_) { - eventLoop->stop(); - } - } - - void handleReceiverDisconnected(const boost::optional<ClientError>& error) { - if (error) { - std::cout << this << " " << "handleReceiverDisconnected: error: " << error.get() << std::endl; - } - - // All file-transfers related to a Client instance need to be freed - // *before* freeing the Client instance. - incomingFileTransfers_.clear(); - - receiver_.reset(); - if (!sender_ && !receiver_) { - eventLoop->stop(); - } - } - - void handleSenderFileTransferFinished(const boost::optional<FileTransferError>& error) { - senderError_ = error; - senderIsDone_ = true; - if (receiverIsDone_) { - timeOut_->stop(); - timeOut_ = networkFactories->getTimerFactory()->createTimer(1000); - timeOut_->onTick.connect(boost::bind(&FileTransferTest::endTest, this)); - timeOut_->start(); - } - else if (error) { - endTest(); - } - } - - void run() { - timeOut_->start(); - eventLoop->run(); - } - - void endTest() { - if (sender_) { - sender_->disconnect(); - } - if (receiver_) { - receiver_->disconnect(); - } - } - - void handleTimeOut() { - std::cout << "Test timed out!!!" << std::endl; - endTest(); - } - - bool isDone() const { - return senderIsDone_ && receiverIsDone_; - } - - bool wasSuccessful() const { - return !senderError_ && !receiverError_; - } - - private: - int senderCandidates_; - boost::shared_ptr<Client> sender_; - ClientXMLTracer* senderTracer_; - ByteArray sendData_; - OutgoingFileTransfer::ref outgoingFileTransfer_; - boost::filesystem::path sendFilePath_; - boost::optional<FileTransferError> senderError_; - bool senderIsDone_; - - int receiverCandidates_; - boost::shared_ptr<Client> receiver_; - ClientXMLTracer* receiverTracer_; - ByteArray receiveData_; - std::vector<IncomingFileTransfer::ref> incomingFileTransfers_; - boost::filesystem::path receiveFilePath_; - boost::optional<FileTransferError> receiverError_; - bool receiverIsDone_; - - Timer::ref timeOut_; + public: + FileTransferTest(int senderCandidates, int receiverCandidates) : senderCandidates_(senderCandidates), senderError_(FileTransferError::UnknownError), senderIsDone_(false), receiverCandidates_(receiverCandidates), receiverError_(FileTransferError::UnknownError), receiverIsDone_(false) { + sender_ = std::make_shared<Client>(JID(getenv("SWIFT_FILETRANSFERTEST_JID")), getenv("SWIFT_FILETRANSFERTEST_PASS"), networkFactories.get()); + sender_->onDisconnected.connect(boost::bind(&FileTransferTest::handleSenderDisconnected, this, _1)); + sender_->onConnected.connect(boost::bind(&FileTransferTest::handleSenderConnected, this)); + sender_->getEntityCapsProvider()->onCapsChanged.connect(boost::bind(&FileTransferTest::handleSenderCapsChanged, this, _1)); + + receiver_ = std::make_shared<Client>(JID(getenv("SWIFT_FILETRANSFERTEST2_JID")), getenv("SWIFT_FILETRANSFERTEST2_PASS"), networkFactories.get()); + receiver_->onConnected.connect(boost::bind(&FileTransferTest::handleReceiverConnected, this)); + receiver_->onDisconnected.connect(boost::bind(&FileTransferTest::handleReceiverDisconnected, this, _1)); + + senderTracer_ = new ClientXMLTracer(sender_.get()); + receiverTracer_ = new ClientXMLTracer(receiver_.get()); + + ClientOptions options; + options.useTLS = ClientOptions::NeverUseTLS; + options.useStreamCompression = false; + options.useStreamResumption = false; + options.useAcks = false; + + sender_->connect(options); + receiver_->connect(options); + + timeOut_ = networkFactories->getTimerFactory()->createTimer(60000); + timeOut_->onTick.connect(boost::bind(&FileTransferTest::handleTimeOut, this)); + + // Create randomly sized data to exchange. + sendFilePath_ = boost::filesystem::unique_path("ft_send_%%%%%%%%%%%%%%%%.bin"); + receiveFilePath_ = boost::filesystem::unique_path("ft_receive_%%%%%%%%%%%%%%%%.bin"); + + size_t size = 1024 + boost::numeric_cast<size_t>(randGen.generateRandomInteger(1024 * 10)); + sendData_.resize(size); + for (unsigned char& n : sendData_) { + n = boost::numeric_cast<unsigned char>(randGen.generateRandomInteger(255)); + } + + std::ofstream outfile(sendFilePath_.native().c_str(), std::ios::out | std::ios::binary); + outfile.write(reinterpret_cast<char *>(&sendData_[0]), boost::numeric_cast<ptrdiff_t>(sendData_.size())); + outfile.close(); + } + + ~FileTransferTest() { + timeOut_->stop(); + + delete senderTracer_; + delete receiverTracer_; + + // Free file-transfer objects so file handles are closed and files can be removed afterwards. + assert(!outgoingFileTransfer_ && incomingFileTransfers_.empty()); + + if(boost::filesystem::exists(sendFilePath_)) { + boost::filesystem::remove(sendFilePath_); + } + + if(boost::filesystem::exists(receiveFilePath_)) { + boost::filesystem::remove(receiveFilePath_); + } + } + + void handleSenderConnected() { + DiscoInfo discoInfo; + discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); + discoInfo.addFeature(DiscoInfo::JingleFeature); + discoInfo.addFeature(DiscoInfo::JingleFTFeature); + discoInfo.addFeature(DiscoInfo::Bytestream); + if (senderCandidates_ & InBandBytestream) { + discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); + } + if (senderCandidates_ & (S5B_Direct | S5B_Assisted | S5B_Proxied)) { + discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); + } + sender_->getDiscoManager()->setCapsNode(CLIENT_NODE); + sender_->getDiscoManager()->setDiscoInfo(discoInfo); + sender_->sendPresence(Presence::create()); + } + + void handleReceiverConnected() { + receiver_->getFileTransferManager()->onIncomingFileTransfer.connect(boost::bind(&FileTransferTest::handleReceiverIncomingFileTransfer, this, _1)); + + DiscoInfo discoInfo; + discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); + discoInfo.addFeature(DiscoInfo::JingleFeature); + discoInfo.addFeature(DiscoInfo::JingleFTFeature); + discoInfo.addFeature(DiscoInfo::Bytestream); + if (receiverCandidates_ & InBandBytestream) { + discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); + } + if (receiverCandidates_ & (S5B_Direct | S5B_Assisted | S5B_Proxied)) { + discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); + } + receiver_->getDiscoManager()->setCapsNode(CLIENT_NODE); + receiver_->getDiscoManager()->setDiscoInfo(discoInfo); + receiver_->getPresenceSender()->sendPresence(Presence::create()); + } + + void handleReceiverIncomingFileTransfer(IncomingFileTransfer::ref transfer) { + incomingFileTransfers_.push_back(transfer); + std::shared_ptr<FileWriteBytestream> out = std::make_shared<FileWriteBytestream>(receiveFilePath_.native()); + transfer->onFinished.connect(boost::bind(&FileTransferTest::handleReceiverFileTransferFinished, this, _1, out)); + + FileTransferOptions options; + options = options.withInBandAllowed(receiverCandidates_ & InBandBytestream); + options = options.withDirectAllowed(receiverCandidates_ & S5B_Direct); + options = options.withAssistedAllowed(receiverCandidates_ & S5B_Assisted); + options = options.withProxiedAllowed(receiverCandidates_ & S5B_Proxied); + + std::cout << "Incoming transfer options: " << "IBB (" << options.isInBandAllowed() << ")" << ", "; + std::cout << "S5B Direct (" << options.isDirectAllowed() << ")" << ", "; + std::cout << "S5B Assisted (" << options.isAssistedAllowed() << ")" << ", "; + std::cout << "S5B Proxied (" << options.isProxiedAllowed() << ")" << std::endl; + + transfer->accept(out, options); + } + + void handleSenderCapsChanged(const JID &jid) { + if (receiver_ && (receiver_->getJID().toBare() == jid.toBare())) { + std::shared_ptr<FileReadBytestream> fileStream = std::make_shared<FileReadBytestream>(sendFilePath_); + + FileTransferOptions options; + options = options.withInBandAllowed(senderCandidates_ & InBandBytestream); + options = options.withDirectAllowed(senderCandidates_ & S5B_Direct); + options = options.withAssistedAllowed(senderCandidates_ & S5B_Assisted); + options = options.withProxiedAllowed(senderCandidates_ & S5B_Proxied); + + std::cout << "Outgoing transfer options: " << "IBB (" << options.isInBandAllowed() << ")" << ", "; + std::cout << "S5B Direct (" << options.isDirectAllowed() << ")" << ", "; + std::cout << "S5B Assisted (" << options.isAssistedAllowed() << ")" << ", "; + std::cout << "S5B Proxied (" << options.isProxiedAllowed() << ")" << std::endl; + + outgoingFileTransfer_ = sender_->getFileTransferManager()->createOutgoingFileTransfer(jid.toBare(), sendFilePath_, "Some File!", fileStream, options); + + if (outgoingFileTransfer_) { + outgoingFileTransfer_->onFinished.connect(boost::bind(&FileTransferTest::handleSenderFileTransferFinished, this, _1)); + outgoingFileTransfer_->start(); + } else { + std::cout << "ERROR: No outgoing file transfer returned." << std::endl; + receiverIsDone_ = true; + senderIsDone_ = true; + endTest(); + } + } + } + + void handleReceiverFileTransferFinished(const boost::optional<FileTransferError>& error, std::shared_ptr<FileWriteBytestream> out) { + out->close(); + receiverError_ = error; + receiverIsDone_ = true; + if (senderIsDone_) { + timeOut_->stop(); + timeOut_ = networkFactories->getTimerFactory()->createTimer(1000); + timeOut_->onTick.connect(boost::bind(&FileTransferTest::endTest, this)); + timeOut_->start(); + } + } + + void handleSenderDisconnected(const boost::optional<ClientError>& error) { + if (error) { + std::cout << this << " " << "handleSenderDisconnected: error: " << error.get() << std::endl; + } + + // All file-transfers related to a Client instance need to be freed + // *before* freeing the Client instance. + outgoingFileTransfer_.reset(); + + sender_.reset(); + if (!sender_ && !receiver_) { + eventLoop->stop(); + } + } + + void handleReceiverDisconnected(const boost::optional<ClientError>& error) { + if (error) { + std::cout << this << " " << "handleReceiverDisconnected: error: " << error.get() << std::endl; + } + + // All file-transfers related to a Client instance need to be freed + // *before* freeing the Client instance. + incomingFileTransfers_.clear(); + + receiver_.reset(); + if (!sender_ && !receiver_) { + eventLoop->stop(); + } + } + + void handleSenderFileTransferFinished(const boost::optional<FileTransferError>& error) { + senderError_ = error; + senderIsDone_ = true; + if (receiverIsDone_) { + timeOut_->stop(); + timeOut_ = networkFactories->getTimerFactory()->createTimer(1000); + timeOut_->onTick.connect(boost::bind(&FileTransferTest::endTest, this)); + timeOut_->start(); + } + else if (error) { + endTest(); + } + } + + void run() { + timeOut_->start(); + eventLoop->run(); + } + + void endTest() { + if (sender_) { + sender_->disconnect(); + } + if (receiver_) { + receiver_->disconnect(); + } + } + + void handleTimeOut() { + std::cout << "Test timed out!!!" << std::endl; + endTest(); + } + + bool isDone() const { + return senderIsDone_ && receiverIsDone_; + } + + bool wasSuccessful() const { + return !senderError_ && !receiverError_; + } + + private: + int senderCandidates_; + std::shared_ptr<Client> sender_; + ClientXMLTracer* senderTracer_; + ByteArray sendData_; + OutgoingFileTransfer::ref outgoingFileTransfer_; + boost::filesystem::path sendFilePath_; + boost::optional<FileTransferError> senderError_; + bool senderIsDone_; + + int receiverCandidates_; + std::shared_ptr<Client> receiver_; + ClientXMLTracer* receiverTracer_; + ByteArray receiveData_; + std::vector<IncomingFileTransfer::ref> incomingFileTransfers_; + boost::filesystem::path receiveFilePath_; + boost::optional<FileTransferError> receiverError_; + bool receiverIsDone_; + + Timer::ref timeOut_; }; static bool runTest(int senderCandidates, int receiverCandidates) { - bool success = false; + bool success = false; - std::cout << "senderCandidates: " << senderCandidates << ", receiverCandidates: " << receiverCandidates << std::endl; - bool expectSuccess = (senderCandidates & receiverCandidates) > 0; + std::cout << "senderCandidates: " << senderCandidates << ", receiverCandidates: " << receiverCandidates << std::endl; + bool expectSuccess = (senderCandidates & receiverCandidates) > 0; - eventLoop = boost::make_shared<SimpleEventLoop>(); - networkFactories = boost::make_shared<BoostNetworkFactories>(eventLoop.get()); + eventLoop = std::make_shared<SimpleEventLoop>(); + networkFactories = std::make_shared<BoostNetworkFactories>(eventLoop.get()); - boost::shared_ptr<FileTransferTest> testRun = boost::make_shared<FileTransferTest>(senderCandidates, receiverCandidates); + std::shared_ptr<FileTransferTest> testRun = std::make_shared<FileTransferTest>(senderCandidates, receiverCandidates); - testRun->run(); + testRun->run(); - bool wasSuccessful = testRun->wasSuccessful(); - if (expectSuccess == wasSuccessful) { - success = true; - } - else { - if (!testRun->isDone()) { - std::cout << "Test did not finish transfer. Sender candidates = " << senderCandidates << ", receiver candidates = " << receiverCandidates << "." << std::endl; - } - } - std::cout << "expected success: " << expectSuccess << ", wasSuccessful: " << wasSuccessful << std::endl; + bool wasSuccessful = testRun->wasSuccessful(); + if (expectSuccess == wasSuccessful) { + success = true; + } + else { + if (!testRun->isDone()) { + std::cout << "Test did not finish transfer. Sender candidates = " << senderCandidates << ", receiver candidates = " << receiverCandidates << "." << std::endl; + } + } + std::cout << "expected success: " << expectSuccess << ", wasSuccessful: " << wasSuccessful << std::endl; - testRun.reset(); - networkFactories.reset(); - eventLoop->runUntilEvents(); + testRun.reset(); + networkFactories.reset(); + eventLoop->runUntilEvents(); - eventLoop->stop(); - eventLoop.reset(); + eventLoop->stop(); + eventLoop.reset(); - return success; + return success; } /** - * This program test file-transfer interop between Swift and itself with various connection candidates. - * The all combinations of the candidates, IBB, S5B (direct) and S5B (proxied), on sender and receiver side are tested. + * This program test file-transfer interop between Swift and itself with various connection candidates. + * The all combinations of the candidates, IBB, S5B (direct) and S5B (proxied), on sender and receiver side are tested. */ int main(int argc, char** argv) { - int failedTests = 0; - - std::vector<std::pair<int, int> > failedTestPairs; - std::cout << "Swiften File-Transfer Connectivity Test Suite" << std::endl; - if (argc == 1) { - if (getenv("SWIFT_FILETRANSFERTEST_CONFIG")) { - // test configuration described in SWIFT_FILETRANSFERTEST_CONFIG environment variable, e.g. "1:1|2:2" - std::vector<std::string> configurations; - std::string configs_env = std::string(getenv("SWIFT_FILETRANSFERTEST_CONFIG")); - boost::split(configurations, configs_env, boost::is_any_of("|")); - foreach(const std::string& config, configurations) { - std::vector<std::string> split_config; - boost::split(split_config, config, boost::is_any_of(":")); - assert(split_config.size() == 2); - - int senderCandidates = atoi(split_config[0].c_str()); - int receiverCandidates = atoi(split_config[1].c_str()); - - if (!runTest(senderCandidates, receiverCandidates)) { - failedTests++; - failedTestPairs.push_back(std::pair<int, int>(senderCandidates, receiverCandidates)); - } - } - - typedef std::pair<int, int> IntPair; - foreach(IntPair failedTest, failedTestPairs) { - std::cout << "Failed test: " << "( " << failedTest.first << ", " << failedTest.second << ") " << std::endl; - } - } - else { - // test all configurations - for (int n = 0; n < (1 << 7); n++) { - int senderCandidates = n & 0xF; - int receiverCandidates = (n >> 4) & 0xF; - std::cout << "Run test " << n + 1 << " of " << (1 << 7) << ", (" << senderCandidates << ", " << receiverCandidates << ")" << std::endl; - if (!runTest(senderCandidates, receiverCandidates)) { - failedTests++; - failedTestPairs.push_back(std::pair<int, int>(senderCandidates, receiverCandidates)); - } - } - - typedef std::pair<int, int> IntPair; - foreach(IntPair failedTest, failedTestPairs) { - std::cout << "Failed test: " << "( " << failedTest.first << ", " << failedTest.second << ") " << std::endl; - } - } - } - else if (argc == 3) { - Log::setLogLevel(Log::debug); - int senderCandidates = atoi(argv[1]); - int receiverCandidates = atoi(argv[2]); - if (!runTest(senderCandidates, receiverCandidates)) { - failedTests++; - } - } - else { - std::cout << "Usage:" << std::endl; - std::cout << "\t- to test all combinations pass no arguments" << std::endl; - std::cout << "\t- to test a specific combination pass two integers describing sender and receiver candidates" << std::endl; - } - return failedTests; + int failedTests = 0; + + std::vector<std::pair<int, int> > failedTestPairs; + std::cout << "Swiften File-Transfer Connectivity Test Suite" << std::endl; + if (argc == 1) { + if (getenv("SWIFT_FILETRANSFERTEST_CONFIG")) { + // test configuration described in SWIFT_FILETRANSFERTEST_CONFIG environment variable, e.g. "1:1|2:2" + std::vector<std::string> configurations; + std::string configs_env = std::string(getenv("SWIFT_FILETRANSFERTEST_CONFIG")); + boost::split(configurations, configs_env, boost::is_any_of("|")); + for (const auto& config : configurations) { + std::vector<std::string> split_config; + boost::split(split_config, config, boost::is_any_of(":")); + assert(split_config.size() == 2); + + int senderCandidates = atoi(split_config[0].c_str()); + int receiverCandidates = atoi(split_config[1].c_str()); + + if (!runTest(senderCandidates, receiverCandidates)) { + failedTests++; + failedTestPairs.push_back(std::pair<int, int>(senderCandidates, receiverCandidates)); + } + } + + for (auto&& failedTest : failedTestPairs) { + std::cout << "Failed test: " << "( " << failedTest.first << ", " << failedTest.second << ") " << std::endl; + } + } + else { + // test all configurations + for (int n = 0; n < (1 << 7); n++) { + int senderCandidates = n & 0xF; + int receiverCandidates = (n >> 4) & 0xF; + std::cout << "Run test " << n + 1 << " of " << (1 << 7) << ", (" << senderCandidates << ", " << receiverCandidates << ")" << std::endl; + if (!runTest(senderCandidates, receiverCandidates)) { + failedTests++; + failedTestPairs.push_back(std::pair<int, int>(senderCandidates, receiverCandidates)); + } + } + + for (auto&& failedTest : failedTestPairs) { + std::cout << "Failed test: " << "( " << failedTest.first << ", " << failedTest.second << ") " << std::endl; + } + } + } + else if (argc == 3) { + Log::setLogLevel(Log::debug); + int senderCandidates = atoi(argv[1]); + int receiverCandidates = atoi(argv[2]); + if (!runTest(senderCandidates, receiverCandidates)) { + failedTests++; + } + } + else { + std::cout << "Usage:" << std::endl; + std::cout << "\t- to test all combinations pass no arguments" << std::endl; + std::cout << "\t- to test a specific combination pass two integers describing sender and receiver candidates" << std::endl; + } + return failedTests; } diff --git a/Swiften/QA/FileTransferTest/SConscript b/Swiften/QA/FileTransferTest/SConscript index 3275985..4b77674 100644 --- a/Swiften/QA/FileTransferTest/SConscript +++ b/Swiften/QA/FileTransferTest/SConscript @@ -3,18 +3,18 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) - myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv = env.Clone() + myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) + myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) - for i in ["SWIFT_FILETRANSFERTEST_JID", "SWIFT_FILETRANSFERTEST_PASS", "SWIFT_FILETRANSFERTEST2_JID", "SWIFT_FILETRANSFERTEST2_PASS"]: - if ARGUMENTS.get(i.lower(), False) : - myenv["ENV"][i] = ARGUMENTS[i.lower()] - elif os.environ.get(i, "") : - myenv["ENV"][i] = os.environ[i] + for i in ["SWIFT_FILETRANSFERTEST_JID", "SWIFT_FILETRANSFERTEST_PASS", "SWIFT_FILETRANSFERTEST2_JID", "SWIFT_FILETRANSFERTEST2_PASS"]: + if ARGUMENTS.get(i.lower(), False) : + myenv["ENV"][i] = ARGUMENTS[i.lower()] + elif os.environ.get(i, "") : + myenv["ENV"][i] = os.environ[i] - # test in-band transfers, direct SOCKS5 bytestream transfers and proxied SOCKS5 bytestream transfers - myenv["ENV"]["SWIFT_FILETRANSFERTEST_CONFIG"] = "1:1|2:2|4:4" + # test in-band transfers, direct SOCKS5 bytestream transfers and proxied SOCKS5 bytestream transfers + myenv["ENV"]["SWIFT_FILETRANSFERTEST_CONFIG"] = "1:1|2:2|4:4" - tester = myenv.Program("FileTransferTest", ["FileTransferTest.cpp"]) - myenv.Test(tester, "system") + tester = myenv.Program("FileTransferTest", ["FileTransferTest.cpp"]) + myenv.Test(tester, "system") diff --git a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp index 67b4bfa..6982c0c 100644 --- a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp @@ -1,13 +1,12 @@ /* - * Copyright (c) 2010-2015 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <memory> #include <string> -#include <boost/shared_ptr.hpp> - #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> @@ -19,198 +18,198 @@ using namespace Swift; class BoostConnectionServerTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(BoostConnectionServerTest); - CPPUNIT_TEST(testConstructor_TwoServersOnSamePort); - CPPUNIT_TEST(testStart_Conflict); - CPPUNIT_TEST(testStop); - CPPUNIT_TEST(testIPv4Server); - CPPUNIT_TEST(testIPv6Server); - CPPUNIT_TEST(testIPv4IPv6DualStackServer); - CPPUNIT_TEST(testIPv6DualStackServerPeerAddress); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - eventLoop_ = new DummyEventLoop(); - boostIOServiceThread_ = new BoostIOServiceThread(); - stopped_ = false; - stoppedError_.reset(); - receivedNewConnection_ = false; - connectFinished_ = false; - remoteAddress_ = boost::optional<HostAddressPort>(); - } - - void tearDown() { - delete boostIOServiceThread_; - while (eventLoop_->hasEvents()) { - eventLoop_->processEvents(); - } - delete eventLoop_; - } - - void testConstructor_TwoServersOnSamePort() { - BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - } - - void testStart_Conflict() { - BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - testling->start(); - - BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - testling2->onStopped.connect( - boost::bind(&BoostConnectionServerTest::handleStopped_, this, _1)); - - testling->stop(); - } - - void testStop() { - BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - testling->start(); - - testling->stop(); - - BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); - testling2->start(); - - testling2->stop(); - } - - void testIPv4Server() { - BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("127.0.0.1"), 9999, boostIOServiceThread_->getIOService(), eventLoop_); - testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); - testling->start(); - - BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("127.0.0.1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - - testling->stop(); - } - - void testIPv6Server() { - BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("::1"), 9999, boostIOServiceThread_->getIOService(), eventLoop_); - testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); - testling->start(); - - BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("::1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - - testling->stop(); - } - - void testIPv4IPv6DualStackServer() { - BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("::"), 9999, boostIOServiceThread_->getIOService(), eventLoop_); - testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); - testling->start(); - - // Test IPv4. - BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("127.0.0.1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - - receivedNewConnection_ = false; - connectFinished_ = false; - - // Test IPv6. - clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("::1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - - testling->stop(); - } - - void testIPv6DualStackServerPeerAddress() { - BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress("::"), 9999, boostIOServiceThread_->getIOService(), eventLoop_); - testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); - testling->start(); - - // Test IPv4. - BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("127.0.0.1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - // The IPv4 localhost mapped to a IPv6 address is expected here. - CPPUNIT_ASSERT(HostAddress("::ffff:127.0.0.1") == remoteAddress_.get().getAddress()); - - receivedNewConnection_ = false; - connectFinished_ = false; - remoteAddress_ = boost::optional<HostAddressPort>(); - - // Test IPv6. - clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); - clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); - clientTestling->connect(HostAddressPort(HostAddress("::1"), 9999)); - - while (!connectFinished_) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); - // The IPv6 local host is expected here. - CPPUNIT_ASSERT(HostAddress("::1") == remoteAddress_.get().getAddress()); - - testling->stop(); - } - - void handleStopped_(boost::optional<BoostConnectionServer::Error> e) { - stopped_ = true; - stoppedError_ = e; - } - - void handleNewConnection(boost::shared_ptr<Connection> connection) { - receivedNewConnection_ = true; - remoteAddress_ = connection->getRemoteAddress(); - } - - void handleConnectFinished(bool /*error*/) { - connectFinished_ = true; - } - - private: - BoostIOServiceThread* boostIOServiceThread_; - DummyEventLoop* eventLoop_; - bool stopped_; - bool receivedNewConnection_; - bool connectFinished_; - boost::optional<BoostConnectionServer::Error> stoppedError_; - boost::optional<HostAddressPort> remoteAddress_; + CPPUNIT_TEST_SUITE(BoostConnectionServerTest); + CPPUNIT_TEST(testConstructor_TwoServersOnSamePort); + CPPUNIT_TEST(testStart_Conflict); + CPPUNIT_TEST(testStop); + CPPUNIT_TEST(testIPv4Server); + CPPUNIT_TEST(testIPv6Server); + CPPUNIT_TEST(testIPv4IPv6DualStackServer); + CPPUNIT_TEST(testIPv6DualStackServerPeerAddress); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + eventLoop_ = new DummyEventLoop(); + boostIOServiceThread_ = new BoostIOServiceThread(); + stopped_ = false; + stoppedError_.reset(); + receivedNewConnection_ = false; + connectFinished_ = false; + remoteAddress_ = boost::optional<HostAddressPort>(); + } + + void tearDown() { + delete boostIOServiceThread_; + while (eventLoop_->hasEvents()) { + eventLoop_->processEvents(); + } + delete eventLoop_; + } + + void testConstructor_TwoServersOnSamePort() { + BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + } + + void testStart_Conflict() { + BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + testling->start(); + + BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + testling2->onStopped.connect( + boost::bind(&BoostConnectionServerTest::handleStopped_, this, _1)); + + testling->stop(); + } + + void testStop() { + BoostConnectionServer::ref testling(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + testling->start(); + + testling->stop(); + + BoostConnectionServer::ref testling2(BoostConnectionServer::create(9999, boostIOServiceThread_->getIOService(), eventLoop_)); + testling2->start(); + + testling2->stop(); + } + + void testIPv4Server() { + BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("127.0.0.1").get(), 9999, boostIOServiceThread_->getIOService(), eventLoop_); + testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); + testling->start(); + + BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("127.0.0.1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + + testling->stop(); + } + + void testIPv6Server() { + BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::1").get(), 9999, boostIOServiceThread_->getIOService(), eventLoop_); + testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); + testling->start(); + + BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("::1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + + testling->stop(); + } + + void testIPv4IPv6DualStackServer() { + BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::").get(), 9999, boostIOServiceThread_->getIOService(), eventLoop_); + testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); + testling->start(); + + // Test IPv4. + BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("127.0.0.1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + + receivedNewConnection_ = false; + connectFinished_ = false; + + // Test IPv6. + clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("::1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + + testling->stop(); + } + + void testIPv6DualStackServerPeerAddress() { + BoostConnectionServer::ref testling = BoostConnectionServer::create(HostAddress::fromString("::").get(), 9999, boostIOServiceThread_->getIOService(), eventLoop_); + testling->onNewConnection.connect(boost::bind(&BoostConnectionServerTest::handleNewConnection, this, _1)); + testling->start(); + + // Test IPv4. + BoostConnection::ref clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("127.0.0.1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + // The IPv4 localhost mapped to a IPv6 address is expected here. + CPPUNIT_ASSERT(HostAddress::fromString("::ffff:127.0.0.1").get() == remoteAddress_.get().getAddress()); + + receivedNewConnection_ = false; + connectFinished_ = false; + remoteAddress_ = boost::optional<HostAddressPort>(); + + // Test IPv6. + clientTestling = BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_); + clientTestling->onConnectFinished.connect(boost::bind(&BoostConnectionServerTest::handleConnectFinished, this, _1)); + clientTestling->connect(HostAddressPort(HostAddress::fromString("::1").get(), 9999)); + + while (!connectFinished_) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(true, receivedNewConnection_); + // The IPv6 local host is expected here. + CPPUNIT_ASSERT(HostAddress::fromString("::1").get() == remoteAddress_.get().getAddress()); + + testling->stop(); + } + + void handleStopped_(boost::optional<BoostConnectionServer::Error> e) { + stopped_ = true; + stoppedError_ = e; + } + + void handleNewConnection(std::shared_ptr<Connection> connection) { + receivedNewConnection_ = true; + remoteAddress_ = connection->getRemoteAddress(); + } + + void handleConnectFinished(bool /*error*/) { + connectFinished_ = true; + } + + private: + BoostIOServiceThread* boostIOServiceThread_; + DummyEventLoop* eventLoop_; + bool stopped_; + bool receivedNewConnection_; + bool connectFinished_; + boost::optional<BoostConnectionServer::Error> stoppedError_; + boost::optional<HostAddressPort> remoteAddress_; }; CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionServerTest); diff --git a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp index a122686..e0890bf 100755 --- a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp @@ -4,11 +4,10 @@ * See the COPYING file for more information. */ +#include <memory> #include <string> #include <boost/date_time/posix_time/posix_time_types.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/smart_ptr/make_shared.hpp> #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> @@ -24,146 +23,157 @@ using namespace Swift; class BoostConnectionTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(BoostConnectionTest); - CPPUNIT_TEST(testDestructor); - CPPUNIT_TEST(testDestructor_PendingEvents); - CPPUNIT_TEST(testWrite); - CPPUNIT_TEST(testWriteMultipleSimultaniouslyQueuesWrites); + CPPUNIT_TEST_SUITE(BoostConnectionTest); + CPPUNIT_TEST(testDestructor); + CPPUNIT_TEST(testDestructor_PendingEvents); + CPPUNIT_TEST(testWrite); + CPPUNIT_TEST(testWriteMultipleSimultaniouslyQueuesWrites); #ifdef TEST_IPV6 - CPPUNIT_TEST(testWrite_IPv6); + CPPUNIT_TEST(testWrite_IPv6); #endif - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - eventLoop_ = new DummyEventLoop(); - boostIOServiceThread_ = new BoostIOServiceThread(); - boostIOService_ = boost::make_shared<boost::asio::io_service>(); - disconnected_ = false; - connectFinished_ = false; - } - - void tearDown() { - delete boostIOServiceThread_; - while (eventLoop_->hasEvents()) { - eventLoop_->processEvents(); - } - delete eventLoop_; - } - - void testDestructor() { - { - BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); - testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222)); - } - } - - void testDestructor_PendingEvents() { - { - BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); - testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222)); - while (!eventLoop_->hasEvents()) { - Swift::sleep(10); - } - } - eventLoop_->processEvents(); - } - - void testWrite() { - using namespace boost::posix_time; - - BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); - testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get())); - testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); - testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); - testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222)); - - boost::posix_time::ptime start = second_clock::local_time(); - while (receivedData_.empty() && ((second_clock::local_time() - start) < seconds(60))) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - CPPUNIT_ASSERT_EQUAL(false, receivedData_.empty()); - testling->disconnect(); - } - - void testWrite_IPv6() { - using namespace boost::posix_time; - - BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); - testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get())); - testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); - testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); - testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV6")), 5222)); - - boost::posix_time::ptime start = second_clock::local_time(); - while (receivedData_.empty() && ((second_clock::local_time() - start) < seconds(60))) { - Swift::sleep(10); - eventLoop_->processEvents(); - } - CPPUNIT_ASSERT_EQUAL(false, receivedData_.empty()); - testling->disconnect(); - } - - - void testWriteMultipleSimultaniouslyQueuesWrites() { - BoostConnection::ref testling(BoostConnection::create(boostIOService_, eventLoop_)); - testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::handleConnectFinished, this)); - testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); - testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); - testling->connect(HostAddressPort(HostAddress(getenv("SWIFT_NETWORK_TEST_IPV4")), 5222)); - while (!connectFinished_) { - boostIOService_->run_one(); - eventLoop_->processEvents(); - } - - testling->write(createSafeByteArray("<stream:strea")); - testling->write(createSafeByteArray("m")); - testling->write(createSafeByteArray(">")); - - // Check that we only did one write event, the others are queued - /*int runHandlers = */boostIOService_->poll(); - // Disabling this test, because poll runns all handlers that are added during poll() as well, so - // this test doesn't really work any more. We'll have to trust that things are queued. - //CPPUNIT_ASSERT_EQUAL(1, runHandlers); - // Process the other events - while (receivedData_.empty()) { - boostIOService_->run_one(); - eventLoop_->processEvents(); - } - - // Disconnect & clean up - testling->disconnect(); - while (!disconnected_) { - boostIOService_->run_one(); - eventLoop_->processEvents(); - } - } - - void doWrite(BoostConnection* connection) { - connection->write(createSafeByteArray("<stream:stream>")); - connection->write(createSafeByteArray("\r\n\r\n")); // Temporarily, while we don't have an xmpp server running on ipv6 - } - - void handleDataRead(boost::shared_ptr<SafeByteArray> data) { - append(receivedData_, *data); - } - - void handleDisconnected() { - disconnected_ = true; - } - - void handleConnectFinished() { - connectFinished_ = true; - } - - private: - BoostIOServiceThread* boostIOServiceThread_; - boost::shared_ptr<boost::asio::io_service> boostIOService_; - DummyEventLoop* eventLoop_; - ByteArray receivedData_; - bool disconnected_; - bool connectFinished_; + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + eventLoop_ = new DummyEventLoop(); + boostIOServiceThread_ = new BoostIOServiceThread(); + boostIOService_ = std::make_shared<boost::asio::io_service>(); + disconnected_ = false; + connectFinished_ = false; + } + + void tearDown() { + delete boostIOServiceThread_; + while (eventLoop_->hasEvents()) { + eventLoop_->processEvents(); + } + delete eventLoop_; + } + + void testDestructor() { + { + BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); + auto hostAddress = HostAddress::fromString(getenv("SWIFT_NETWORK_TEST_IPV4")); + CPPUNIT_ASSERT_EQUAL(true, hostAddress.is_initialized()); + testling->connect(HostAddressPort(hostAddress.get(), 5222)); + } + } + + void testDestructor_PendingEvents() { + { + BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); + auto hostAddress = HostAddress::fromString(getenv("SWIFT_NETWORK_TEST_IPV4")); + CPPUNIT_ASSERT_EQUAL(true, hostAddress.is_initialized()); + testling->connect(HostAddressPort(hostAddress.get(), 5222)); + while (!eventLoop_->hasEvents()) { + Swift::sleep(10); + } + } + eventLoop_->processEvents(); + } + + void testWrite() { + using namespace boost::posix_time; + + BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); + testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get())); + testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); + testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); + auto hostAddress = HostAddress::fromString(getenv("SWIFT_NETWORK_TEST_IPV4")); + CPPUNIT_ASSERT_EQUAL(true, hostAddress.is_initialized()); + testling->connect(HostAddressPort(hostAddress.get(), 5222)); + + boost::posix_time::ptime start = second_clock::local_time(); + while (receivedData_.empty() && ((second_clock::local_time() - start) < seconds(60))) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + CPPUNIT_ASSERT_EQUAL(false, receivedData_.empty()); + testling->disconnect(); + } + + void testWrite_IPv6() { + using namespace boost::posix_time; + + BoostConnection::ref testling(BoostConnection::create(boostIOServiceThread_->getIOService(), eventLoop_)); + testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::doWrite, this, testling.get())); + testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); + testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); + auto hostAddress = HostAddress::fromString(getenv("SWIFT_NETWORK_TEST_IPV6")); + CPPUNIT_ASSERT_EQUAL(true, hostAddress.is_initialized()); + testling->connect(HostAddressPort(hostAddress.get(), 5222)); + + boost::posix_time::ptime start = second_clock::local_time(); + while (receivedData_.empty() && ((second_clock::local_time() - start) < seconds(60))) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + CPPUNIT_ASSERT_EQUAL(false, receivedData_.empty()); + testling->disconnect(); + } + + + void testWriteMultipleSimultaniouslyQueuesWrites() { + BoostConnection::ref testling(BoostConnection::create(boostIOService_, eventLoop_)); + testling->onConnectFinished.connect(boost::bind(&BoostConnectionTest::handleConnectFinished, this)); + testling->onDataRead.connect(boost::bind(&BoostConnectionTest::handleDataRead, this, _1)); + testling->onDisconnected.connect(boost::bind(&BoostConnectionTest::handleDisconnected, this)); + + auto hostAddress = HostAddress::fromString(getenv("SWIFT_NETWORK_TEST_IPV4")); + CPPUNIT_ASSERT_EQUAL(true, hostAddress.is_initialized()); + testling->connect(HostAddressPort(hostAddress.get(), 5222)); + while (!connectFinished_) { + boostIOService_->run_one(); + eventLoop_->processEvents(); + } + + testling->write(createSafeByteArray("<stream:strea")); + testling->write(createSafeByteArray("m")); + testling->write(createSafeByteArray(">")); + + // Check that we only did one write event, the others are queued + /*int runHandlers = */boostIOService_->poll(); + // Disabling this test, because poll runns all handlers that are added during poll() as well, so + // this test doesn't really work any more. We'll have to trust that things are queued. + //CPPUNIT_ASSERT_EQUAL(1, runHandlers); + // Process the other events + while (receivedData_.empty()) { + boostIOService_->run_one(); + eventLoop_->processEvents(); + } + + // Disconnect & clean up + testling->disconnect(); + while (!disconnected_) { + boostIOService_->run_one(); + eventLoop_->processEvents(); + } + } + + void doWrite(BoostConnection* connection) { + connection->write(createSafeByteArray("<stream:stream>")); + connection->write(createSafeByteArray("\r\n\r\n")); // Temporarily, while we don't have an xmpp server running on ipv6 + } + + void handleDataRead(std::shared_ptr<SafeByteArray> data) { + append(receivedData_, *data); + } + + void handleDisconnected() { + disconnected_ = true; + } + + void handleConnectFinished() { + connectFinished_ = true; + } + + private: + BoostIOServiceThread* boostIOServiceThread_; + std::shared_ptr<boost::asio::io_service> boostIOService_; + DummyEventLoop* eventLoop_; + ByteArray receivedData_; + bool disconnected_; + bool connectFinished_; }; CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionTest); diff --git a/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp b/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp index baa42f9..95ebb6d 100644 --- a/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp +++ b/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp @@ -32,223 +32,223 @@ using namespace Swift; struct CompareHostAddresses { - bool operator()(const HostAddress& h1, const HostAddress& h2) { - return h1.toString() < h2.toString(); - } + bool operator()(const HostAddress& h1, const HostAddress& h2) { + return h1.toString() < h2.toString(); + } }; class DomainNameResolverTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(DomainNameResolverTest); - CPPUNIT_TEST(testResolveAddress); - CPPUNIT_TEST(testResolveAddress_Error); + CPPUNIT_TEST_SUITE(DomainNameResolverTest); + CPPUNIT_TEST(testResolveAddress); + CPPUNIT_TEST(testResolveAddress_Error); #ifndef USE_UNBOUND - /** - * The native DNS resolver of Windows behaves oddly if the system has no global IPv6 - * routed address and no IPv6 reachability. It will not return IPv6 records from DNS - * requests for an unspecified protocol (IPv6 or IPv4). - * The following tests are only enabled on Windows if scons is run with the 'test_ipv6=1' - * argument, indicating working IPv6 on the test machine. - */ + /** + * The native DNS resolver of Windows behaves oddly if the system has no global IPv6 + * routed address and no IPv6 reachability. It will not return IPv6 records from DNS + * requests for an unspecified protocol (IPv6 or IPv4). + * The following tests are only enabled on Windows if scons is run with the 'test_ipv6=1' + * argument, indicating working IPv6 on the test machine. + */ #if !defined(SWIFTEN_PLATFORM_WINDOWS) || defined(TEST_IPV6) - CPPUNIT_TEST(testResolveAddress_IPv6); - CPPUNIT_TEST(testResolveAddress_IPv4and6); + CPPUNIT_TEST(testResolveAddress_IPv6); + CPPUNIT_TEST(testResolveAddress_IPv4and6); #endif - CPPUNIT_TEST(testResolveAddress_International); + CPPUNIT_TEST(testResolveAddress_International); #endif - CPPUNIT_TEST(testResolveAddress_Localhost); - CPPUNIT_TEST(testResolveAddress_Parallel); + CPPUNIT_TEST(testResolveAddress_Localhost); + CPPUNIT_TEST(testResolveAddress_Parallel); #ifndef USE_UNBOUND - CPPUNIT_TEST(testResolveService); + CPPUNIT_TEST(testResolveService); #endif - CPPUNIT_TEST(testResolveService_Error); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - ioServiceThread = new BoostIOServiceThread(); - eventLoop = new DummyEventLoop(); - idnConverter = boost::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); + CPPUNIT_TEST(testResolveService_Error); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + ioServiceThread = new BoostIOServiceThread(); + eventLoop = new DummyEventLoop(); + idnConverter = std::shared_ptr<IDNConverter>(PlatformIDNConverter::create()); #ifdef USE_UNBOUND - resolver = new UnboundDomainNameResolver(idnConverter.get(), ioServiceThread->getIOService(), eventLoop); + resolver = new UnboundDomainNameResolver(idnConverter.get(), ioServiceThread->getIOService(), eventLoop); #else - resolver = new PlatformDomainNameResolver(idnConverter.get(), eventLoop); + resolver = new PlatformDomainNameResolver(idnConverter.get(), eventLoop); #endif - resultsAvailable = false; - } - - void tearDown() { - delete ioServiceThread; - delete resolver; - delete eventLoop; - } - - void testResolveAddress() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(!addressQueryError); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addressQueryResult.size())); - CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.0"), addressQueryResult[0].toString()); - } - - void testResolveAddress_Error() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("invalid.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(addressQueryError); - } - - void testResolveAddress_IPv6() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp-ipv6.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(!addressQueryError); - CPPUNIT_ASSERT_EQUAL(std::string("2001:470:1f0e:852::2"), addressQueryResult[0].toString()); - } - - void testResolveAddress_IPv4and6() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp-ipv46.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(!addressQueryError); - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(addressQueryResult.size())); - CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.7"), addressQueryResult[0].toString()); - CPPUNIT_ASSERT_EQUAL(std::string("1234:5678:9abc:def0:fed:cba9:8765:4321"), addressQueryResult[1].toString()); - } - - void testResolveAddress_International() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("tron\xc3\xa7on.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(!addressQueryError); - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addressQueryResult.size())); - CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.3"), addressQueryResult[0].toString()); - } - - void testResolveAddress_Localhost() { - boost::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("localhost")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT(!addressQueryError); - CPPUNIT_ASSERT(std::find(addressQueryResult.begin(), addressQueryResult.end(), HostAddress("127.0.0.1")) != addressQueryResult.end()); - } - - void testResolveAddress_Parallel() { - std::vector<DomainNameAddressQuery::ref> queries; - static const size_t numQueries = 100; - for (size_t i = 0; i < numQueries; ++i) { - DomainNameAddressQuery::ref query(createAddressQuery("xmpp.test.swift.im")); - queries.push_back(query); - query->run(); - } - - eventLoop->processEvents(); - int ticks = 0; - while (allAddressQueryResults.size() < numQueries) { - ticks++; - if (ticks > 1000) { - CPPUNIT_ASSERT(false); - } - Swift::sleep(10); - eventLoop->processEvents(); - } - - CPPUNIT_ASSERT_EQUAL(numQueries, allAddressQueryResults.size()); - for (size_t i = 0; i < numQueries; ++i) { - CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.0"), allAddressQueryResults[i].toString()); - } - } - - void testResolveService() { - boost::shared_ptr<DomainNameServiceQuery> query(createServiceQuery("_xmpp-client._tcp.", "xmpp-srv.test.swift.im")); - - query->run(); - waitForResults(); - - CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(serviceQueryResult.size())); - CPPUNIT_ASSERT_EQUAL(std::string("xmpp1.test.swift.im"), serviceQueryResult[0].hostname); - CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[0].port); - CPPUNIT_ASSERT_EQUAL(0, serviceQueryResult[0].priority); - CPPUNIT_ASSERT_EQUAL(1, serviceQueryResult[0].weight); - CPPUNIT_ASSERT_EQUAL(std::string("xmpp-invalid.test.swift.im"), serviceQueryResult[1].hostname); - CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[1].port); - CPPUNIT_ASSERT_EQUAL(1, serviceQueryResult[1].priority); - CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[1].weight); - CPPUNIT_ASSERT_EQUAL(std::string("xmpp3.test.swift.im"), serviceQueryResult[2].hostname); - CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[2].port); - CPPUNIT_ASSERT_EQUAL(3, serviceQueryResult[2].priority); - CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[2].weight); - CPPUNIT_ASSERT_EQUAL(std::string("xmpp2.test.swift.im"), serviceQueryResult[3].hostname); - CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[3].port); - CPPUNIT_ASSERT_EQUAL(5, serviceQueryResult[3].priority); - CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[3].weight); - } - - void testResolveService_Error() { - } - - private: - boost::shared_ptr<DomainNameAddressQuery> createAddressQuery(const std::string& domain) { - boost::shared_ptr<DomainNameAddressQuery> result = resolver->createAddressQuery(domain); - result->onResult.connect(boost::bind(&DomainNameResolverTest::handleAddressQueryResult, this, _1, _2)); - return result; - } - - void handleAddressQueryResult(const std::vector<HostAddress>& addresses, boost::optional<DomainNameResolveError> error) { - addressQueryResult = addresses; - std::sort(addressQueryResult.begin(), addressQueryResult.end(), CompareHostAddresses()); - allAddressQueryResults.insert(allAddressQueryResults.begin(), addresses.begin(), addresses.end()); - addressQueryError = error; - resultsAvailable = true; - } - - boost::shared_ptr<DomainNameServiceQuery> createServiceQuery(const std::string& serviceLookupPrefix, const std::string& domain) { - boost::shared_ptr<DomainNameServiceQuery> result = resolver->createServiceQuery(serviceLookupPrefix, domain); - result->onResult.connect(boost::bind(&DomainNameResolverTest::handleServiceQueryResult, this, _1)); - return result; - } - - void handleServiceQueryResult(const std::vector<DomainNameServiceQuery::Result>& result) { - serviceQueryResult = result; - resultsAvailable = true; - } - - void waitForResults() { - eventLoop->processEvents(); - int ticks = 0; - while (!resultsAvailable) { - ticks++; - if (ticks > 1000) { - CPPUNIT_ASSERT(false); - } - Swift::sleep(10); - eventLoop->processEvents(); - } - } - - private: - BoostIOServiceThread* ioServiceThread; - DummyEventLoop* eventLoop; - boost::shared_ptr<IDNConverter> idnConverter; - boost::shared_ptr<TimerFactory> timerFactory; - bool resultsAvailable; - std::vector<HostAddress> addressQueryResult; - std::vector<HostAddress> allAddressQueryResults; - boost::optional<DomainNameResolveError> addressQueryError; - std::vector<DomainNameServiceQuery::Result> serviceQueryResult; - DomainNameResolver* resolver; + resultsAvailable = false; + } + + void tearDown() { + delete ioServiceThread; + delete resolver; + delete eventLoop; + } + + void testResolveAddress() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(!addressQueryError); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addressQueryResult.size())); + CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.0"), addressQueryResult[0].toString()); + } + + void testResolveAddress_Error() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("invalid.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(addressQueryError); + } + + void testResolveAddress_IPv6() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp-ipv6.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(!addressQueryError); + CPPUNIT_ASSERT_EQUAL(std::string("2001:470:1f0e:852::2"), addressQueryResult[0].toString()); + } + + void testResolveAddress_IPv4and6() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("xmpp-ipv46.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(!addressQueryError); + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(addressQueryResult.size())); + CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.7"), addressQueryResult[0].toString()); + CPPUNIT_ASSERT_EQUAL(std::string("1234:5678:9abc:def0:fed:cba9:8765:4321"), addressQueryResult[1].toString()); + } + + void testResolveAddress_International() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("tron\xc3\xa7on.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(!addressQueryError); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addressQueryResult.size())); + CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.3"), addressQueryResult[0].toString()); + } + + void testResolveAddress_Localhost() { + std::shared_ptr<DomainNameAddressQuery> query(createAddressQuery("localhost")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT(!addressQueryError); + CPPUNIT_ASSERT(std::find(addressQueryResult.begin(), addressQueryResult.end(), HostAddress::fromString("127.0.0.1").get()) != addressQueryResult.end()); + } + + void testResolveAddress_Parallel() { + std::vector<DomainNameAddressQuery::ref> queries; + static const size_t numQueries = 100; + for (size_t i = 0; i < numQueries; ++i) { + DomainNameAddressQuery::ref query(createAddressQuery("xmpp.test.swift.im")); + queries.push_back(query); + query->run(); + } + + eventLoop->processEvents(); + int ticks = 0; + while (allAddressQueryResults.size() < numQueries) { + ticks++; + if (ticks > 1000) { + CPPUNIT_ASSERT(false); + } + Swift::sleep(10); + eventLoop->processEvents(); + } + + CPPUNIT_ASSERT_EQUAL(numQueries, allAddressQueryResults.size()); + for (size_t i = 0; i < numQueries; ++i) { + CPPUNIT_ASSERT_EQUAL(std::string("10.0.0.0"), allAddressQueryResults[i].toString()); + } + } + + void testResolveService() { + std::shared_ptr<DomainNameServiceQuery> query(createServiceQuery("_xmpp-client._tcp.", "xmpp-srv.test.swift.im")); + + query->run(); + waitForResults(); + + CPPUNIT_ASSERT_EQUAL(4, static_cast<int>(serviceQueryResult.size())); + CPPUNIT_ASSERT_EQUAL(std::string("xmpp1.test.swift.im"), serviceQueryResult[0].hostname); + CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[0].port); + CPPUNIT_ASSERT_EQUAL(0, serviceQueryResult[0].priority); + CPPUNIT_ASSERT_EQUAL(1, serviceQueryResult[0].weight); + CPPUNIT_ASSERT_EQUAL(std::string("xmpp-invalid.test.swift.im"), serviceQueryResult[1].hostname); + CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[1].port); + CPPUNIT_ASSERT_EQUAL(1, serviceQueryResult[1].priority); + CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[1].weight); + CPPUNIT_ASSERT_EQUAL(std::string("xmpp3.test.swift.im"), serviceQueryResult[2].hostname); + CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[2].port); + CPPUNIT_ASSERT_EQUAL(3, serviceQueryResult[2].priority); + CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[2].weight); + CPPUNIT_ASSERT_EQUAL(std::string("xmpp2.test.swift.im"), serviceQueryResult[3].hostname); + CPPUNIT_ASSERT_EQUAL(5000, serviceQueryResult[3].port); + CPPUNIT_ASSERT_EQUAL(5, serviceQueryResult[3].priority); + CPPUNIT_ASSERT_EQUAL(100, serviceQueryResult[3].weight); + } + + void testResolveService_Error() { + } + + private: + std::shared_ptr<DomainNameAddressQuery> createAddressQuery(const std::string& domain) { + std::shared_ptr<DomainNameAddressQuery> result = resolver->createAddressQuery(domain); + result->onResult.connect(boost::bind(&DomainNameResolverTest::handleAddressQueryResult, this, _1, _2)); + return result; + } + + void handleAddressQueryResult(const std::vector<HostAddress>& addresses, boost::optional<DomainNameResolveError> error) { + addressQueryResult = addresses; + std::sort(addressQueryResult.begin(), addressQueryResult.end(), CompareHostAddresses()); + allAddressQueryResults.insert(allAddressQueryResults.begin(), addresses.begin(), addresses.end()); + addressQueryError = error; + resultsAvailable = true; + } + + std::shared_ptr<DomainNameServiceQuery> createServiceQuery(const std::string& serviceLookupPrefix, const std::string& domain) { + std::shared_ptr<DomainNameServiceQuery> result = resolver->createServiceQuery(serviceLookupPrefix, domain); + result->onResult.connect(boost::bind(&DomainNameResolverTest::handleServiceQueryResult, this, _1)); + return result; + } + + void handleServiceQueryResult(const std::vector<DomainNameServiceQuery::Result>& result) { + serviceQueryResult = result; + resultsAvailable = true; + } + + void waitForResults() { + eventLoop->processEvents(); + int ticks = 0; + while (!resultsAvailable) { + ticks++; + if (ticks > 1000) { + CPPUNIT_ASSERT(false); + } + Swift::sleep(10); + eventLoop->processEvents(); + } + } + + private: + BoostIOServiceThread* ioServiceThread; + DummyEventLoop* eventLoop; + std::shared_ptr<IDNConverter> idnConverter; + std::shared_ptr<TimerFactory> timerFactory; + bool resultsAvailable; + std::vector<HostAddress> addressQueryResult; + std::vector<HostAddress> allAddressQueryResults; + boost::optional<DomainNameResolveError> addressQueryError; + std::vector<DomainNameServiceQuery::Result> serviceQueryResult; + DomainNameResolver* resolver; }; CPPUNIT_TEST_SUITE_REGISTRATION(DomainNameResolverTest); diff --git a/Swiften/QA/NetworkTest/SConscript b/Swiften/QA/NetworkTest/SConscript index b090165..387bafc 100644 --- a/Swiften/QA/NetworkTest/SConscript +++ b/Swiften/QA/NetworkTest/SConscript @@ -3,20 +3,20 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - if "test_ipv6" in ARGUMENTS : - myenv.Append(CPPDEFINES = ["TEST_IPV6"]) - myenv.MergeFlags(myenv["CHECKER_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) - myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + myenv = env.Clone() + if "test_ipv6" in ARGUMENTS : + myenv.Append(CPPDEFINES = ["TEST_IPV6"]) + myenv.MergeFlags(myenv["CHECKER_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - if env.get("unbound", False) : - myenv.Append(CPPDEFINES = ["USE_UNBOUND"]) + if env.get("unbound", False) : + myenv.Append(CPPDEFINES = ["USE_UNBOUND"]) - tester = myenv.Program("NetworkTest", [ - "BoostConnectionServerTest.cpp", - "BoostConnectionTest.cpp", - "DomainNameResolverTest.cpp", - ]) - myenv.Test(tester, "system", is_checker = True) + tester = myenv.Program("NetworkTest", [ + "BoostConnectionServerTest.cpp", + "BoostConnectionTest.cpp", + "DomainNameResolverTest.cpp", + ]) + myenv.Test(tester, "system", is_checker = True) diff --git a/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp b/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp index ddaee01..53f9e60 100644 --- a/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp +++ b/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp @@ -4,32 +4,37 @@ * See Documentation/Licenses/BSD.txt for more information. */ +/* + * Copyright (c) 2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + #include <iostream> #include <Swiften/Network/PlatformProxyProvider.h> -#include <Swiften/Base/foreach.h> using namespace Swift; int main(void) { - int ret = 0; - HostAddressPort hap; - - std::cout << "constructing PlatfromProxyProvider instance ..." << std::endl; - PlatformProxyProvider ppp; - - hap = ppp.getSOCKS5Proxy(); - std::cout << "SOCKS5 Proxy configured: " << hap.isValid() << std::endl; - if(hap.isValid()) { - std::cout << "SOCKS5 Proxy: " << hap.getAddress().toString() << ":" << hap.getPort() << std::endl; - } - - hap = ppp.getHTTPConnectProxy(); - std::cout << "HTTPConnect Proxy configured: " << hap.isValid() << std::endl; - if(hap.isValid()) { - std::cout << "HTTPConnect Proxy: " << hap.getAddress().toString() << ":" << hap.getPort() << std::endl; - } - - return ret; + int ret = 0; + HostAddressPort hap; + + std::cout << "constructing PlatfromProxyProvider instance ..." << std::endl; + PlatformProxyProvider ppp; + + hap = ppp.getSOCKS5Proxy(); + std::cout << "SOCKS5 Proxy configured: " << hap.isValid() << std::endl; + if(hap.isValid()) { + std::cout << "SOCKS5 Proxy: " << hap.getAddress().toString() << ":" << hap.getPort() << std::endl; + } + + hap = ppp.getHTTPConnectProxy(); + std::cout << "HTTPConnect Proxy configured: " << hap.isValid() << std::endl; + if(hap.isValid()) { + std::cout << "HTTPConnect Proxy: " << hap.getAddress().toString() << ":" << hap.getPort() << std::endl; + } + + return ret; } diff --git a/Swiften/QA/ProxyProviderTest/SConscript b/Swiften/QA/ProxyProviderTest/SConscript index 2eb123d..3e28360 100644 --- a/Swiften/QA/ProxyProviderTest/SConscript +++ b/Swiften/QA/ProxyProviderTest/SConscript @@ -7,5 +7,5 @@ myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) myenv.Program("ProxyProviderTest", [ - "ProxyProviderTest.cpp", - ]) + "ProxyProviderTest.cpp", + ]) diff --git a/Swiften/QA/ReconnectTest/ReconnectTest.cpp b/Swiften/QA/ReconnectTest/ReconnectTest.cpp index cfb7f80..af3369c 100644 --- a/Swiften/QA/ReconnectTest/ReconnectTest.cpp +++ b/Swiften/QA/ReconnectTest/ReconnectTest.cpp @@ -1,20 +1,21 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <thread> + #include <boost/bind.hpp> -#include <boost/thread.hpp> #include <Swiften/Client/Client.h> -#include <Swiften/Network/BoostTimer.h> +#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/EventLoop/EventLoop.h> #include <Swiften/EventLoop/SimpleEventLoop.h> -#include <Swiften/Roster/GetRosterRequest.h> -#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Network/BoostIOServiceThread.h> +#include <Swiften/Network/BoostTimer.h> #include <Swiften/Network/MainBoostIOServiceThread.h> +#include <Swiften/Roster/GetRosterRequest.h> using namespace Swift; @@ -25,49 +26,49 @@ Client* client_; SimpleEventLoop eventLoop_; int count = 0; -void handleTick(boost::shared_ptr<BoostTimer> timer) { - std::cout << "Count " << count++ << std::endl; - if (timer) { - timer->stop(); - } - if (connecting_) { - client_->disconnect(); - } else { - if (count > 60) { - eventLoop_.stop(); - return; - } - client_->connect(); - } - connecting_ = !connecting_; - - int delay = 500; -// int delay = 0; - boost::shared_ptr<BoostTimer> newTimer(BoostTimer::create(delay, &MainBoostIOServiceThread::getInstance().getIOService())); - newTimer->onTick.connect(boost::bind(&handleTick, timer)); - newTimer->start(); +void handleTick(std::shared_ptr<BoostTimer> timer) { + std::cout << "Count " << count++ << std::endl; + if (timer) { + timer->stop(); + } + if (connecting_) { + client_->disconnect(); + } else { + if (count > 60) { + eventLoop_.stop(); + return; + } + client_->connect(); + } + connecting_ = !connecting_; + + int delay = 500; +// int delay = 0; + std::shared_ptr<BoostTimer> newTimer(BoostTimer::create(delay, &MainBoostIOServiceThread::getInstance().getIOService())); + newTimer->onTick.connect(boost::bind(&handleTick, timer)); + newTimer->start(); } int main(int, char**) { - char* jidChars = getenv("SWIFT_CLIENTTEST_JID"); - if (!jidChars) { - std::cerr << "Please set the SWIFT_CLIENTTEST_JID environment variable" << std::endl; - return -1; - } - char* passChars = getenv("SWIFT_CLIENTTEST_PASS"); - if (!passChars) { - std::cerr << "Please set the SWIFT_CLIENTTEST_PASS environment variable" << std::endl; - return -1; - } - - JID jid(jidChars); - std::string pass(passChars); + char* jidChars = getenv("SWIFT_CLIENTTEST_JID"); + if (!jidChars) { + std::cerr << "Please set the SWIFT_CLIENTTEST_JID environment variable" << std::endl; + return -1; + } + char* passChars = getenv("SWIFT_CLIENTTEST_PASS"); + if (!passChars) { + std::cerr << "Please set the SWIFT_CLIENTTEST_PASS environment variable" << std::endl; + return -1; + } + + JID jid(jidChars); + std::string pass(passChars); + + client_ = new Swift::Client(jid, pass); + handleTick(std::shared_ptr<BoostTimer>()); + eventLoop_.run(); - client_ = new Swift::Client(jid, pass); - handleTick(boost::shared_ptr<BoostTimer>()); - eventLoop_.run(); - - delete client_; - return 0; + delete client_; + return 0; } diff --git a/Swiften/QA/ReconnectTest/SConscript b/Swiften/QA/ReconnectTest/SConscript index 6db6a6f..a94af5e 100644 --- a/Swiften/QA/ReconnectTest/SConscript +++ b/Swiften/QA/ReconnectTest/SConscript @@ -3,23 +3,23 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) - myenv.MergeFlags(myenv["BOOST_FLAGS"]) - myenv.MergeFlags(myenv.get("SQLITE_FLAGS", "")) - myenv.MergeFlags(myenv["ZLIB_FLAGS"]) - myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) - myenv.MergeFlags(myenv.get("LIBXML_FLAGS", "")) - myenv.MergeFlags(myenv.get("EXPAT_FLAGS", "")) - myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) -# myenv.Append(LIBPATH = ["/opt/local/lib"]) -# myenv.Append(LIBS = ["efence"]) + myenv = env.Clone() + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) + myenv.MergeFlags(myenv["BOOST_FLAGS"]) + myenv.MergeFlags(myenv.get("SQLITE_FLAGS", "")) + myenv.MergeFlags(myenv["ZLIB_FLAGS"]) + myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) + myenv.MergeFlags(myenv.get("LIBXML_FLAGS", "")) + myenv.MergeFlags(myenv.get("EXPAT_FLAGS", "")) + myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) +# myenv.Append(LIBPATH = ["/opt/local/lib"]) +# myenv.Append(LIBS = ["efence"]) - for i in ["SWIFT_CLIENTTEST_JID", "SWIFT_CLIENTTEST_PASS"]: - if os.environ.get(i, "") : - myenv["ENV"][i] = os.environ[i] + for i in ["SWIFT_CLIENTTEST_JID", "SWIFT_CLIENTTEST_PASS"]: + if os.environ.get(i, "") : + myenv["ENV"][i] = os.environ[i] - tester = myenv.Program("ReconnectTest", ["ReconnectTest.cpp"]) - myenv.Test(tester, "system", is_checker = True) + tester = myenv.Program("ReconnectTest", ["ReconnectTest.cpp"]) + myenv.Test(tester, "system", is_checker = True) diff --git a/Swiften/QA/SConscript b/Swiften/QA/SConscript index 2c588e5..2bb3e51 100644 --- a/Swiften/QA/SConscript +++ b/Swiften/QA/SConscript @@ -1,13 +1,13 @@ Import("swiften_env") SConscript(dirs = [ - "NetworkTest", -# "ReconnectTest", - "ClientTest", -# "DNSSDTest", - "StorageTest", - "TLSTest", - "ScriptedTests", - "ProxyProviderTest", - "FileTransferTest", - ]) + "NetworkTest", +# "ReconnectTest", + "ClientTest", +# "DNSSDTest", + "StorageTest", + "TLSTest", + "ScriptedTests", + "ProxyProviderTest", + "FileTransferTest", + ]) diff --git a/Swiften/QA/ScriptedTests/SConscript b/Swiften/QA/ScriptedTests/SConscript index 298c455..ae315fe 100644 --- a/Swiften/QA/ScriptedTests/SConscript +++ b/Swiften/QA/ScriptedTests/SConscript @@ -1,7 +1,7 @@ Import("env") if env["TEST"] : - env.ScriptTests([ - "SendMessage.lua", - "MultipleClients.lua", - ], "Swiften.QA.ScriptedTests", "system") + env.ScriptTests([ + "SendMessage.lua", + "MultipleClients.lua", + ], "Swiften.QA.ScriptedTests", "system") diff --git a/Swiften/QA/StorageTest/FileReadBytestreamTest.cpp b/Swiften/QA/StorageTest/FileReadBytestreamTest.cpp index d70d9c9..e601de9 100644 --- a/Swiften/QA/StorageTest/FileReadBytestreamTest.cpp +++ b/Swiften/QA/StorageTest/FileReadBytestreamTest.cpp @@ -15,61 +15,61 @@ using namespace Swift; class FileReadBytestreamTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(FileReadBytestreamTest); - CPPUNIT_TEST(testRead); - CPPUNIT_TEST(testRead_Twice); - CPPUNIT_TEST(testIsFinished_NotFinished); - CPPUNIT_TEST(testIsFinished_IsFinished); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(FileReadBytestreamTest); + CPPUNIT_TEST(testRead); + CPPUNIT_TEST(testRead_Twice); + CPPUNIT_TEST(testIsFinished_NotFinished); + CPPUNIT_TEST(testIsFinished_IsFinished); + CPPUNIT_TEST_SUITE_END(); - public: - void setUp() { - pathProvider = new PlatformApplicationPathProvider("FileReadBytestreamTest"); - } + public: + void setUp() { + pathProvider = new PlatformApplicationPathProvider("FileReadBytestreamTest"); + } - void tearDown() { - delete pathProvider; - } + void tearDown() { + delete pathProvider; + } - void testRead() { - boost::shared_ptr<FileReadBytestream> testling(createTestling()); + void testRead() { + std::shared_ptr<FileReadBytestream> testling(createTestling()); - boost::shared_ptr< std::vector<unsigned char> > result = testling->read(10); + std::shared_ptr< std::vector<unsigned char> > result = testling->read(10); - CPPUNIT_ASSERT(createByteArray("/*\n * Copy") == *result.get()); - } + CPPUNIT_ASSERT(createByteArray("/*\n * Copy") == *result.get()); + } - void testRead_Twice() { - boost::shared_ptr<FileReadBytestream> testling(createTestling()); + void testRead_Twice() { + std::shared_ptr<FileReadBytestream> testling(createTestling()); - testling->read(10); - boost::shared_ptr< std::vector<unsigned char> > result = testling->read(10); + testling->read(10); + std::shared_ptr< std::vector<unsigned char> > result = testling->read(10); - CPPUNIT_ASSERT_EQUAL(std::string("right (c) "), byteArrayToString(*result)); - } + CPPUNIT_ASSERT_EQUAL(std::string("right (c) "), byteArrayToString(*result)); + } - void testIsFinished_NotFinished() { - boost::shared_ptr<FileReadBytestream> testling(createTestling()); + void testIsFinished_NotFinished() { + std::shared_ptr<FileReadBytestream> testling(createTestling()); - testling->read(10); + testling->read(10); - CPPUNIT_ASSERT(!testling->isFinished()); - } + CPPUNIT_ASSERT(!testling->isFinished()); + } - void testIsFinished_IsFinished() { - boost::shared_ptr<FileReadBytestream> testling(createTestling()); + void testIsFinished_IsFinished() { + std::shared_ptr<FileReadBytestream> testling(createTestling()); - testling->read(4096); + testling->read(4096); - CPPUNIT_ASSERT(testling->isFinished()); - } + CPPUNIT_ASSERT(testling->isFinished()); + } - private: - FileReadBytestream* createTestling() { - return new FileReadBytestream(pathProvider->getExecutableDir() / "FileReadBytestreamTest.cpp"); - } + private: + FileReadBytestream* createTestling() { + return new FileReadBytestream(pathProvider->getExecutableDir() / "FileReadBytestreamTest.cpp"); + } - PlatformApplicationPathProvider* pathProvider; + PlatformApplicationPathProvider* pathProvider; }; CPPUNIT_TEST_SUITE_REGISTRATION(FileReadBytestreamTest); diff --git a/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp b/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp index 3686cf9..7823519 100644 --- a/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp +++ b/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp @@ -6,53 +6,53 @@ #include <boost/bind.hpp> #include <boost/filesystem.hpp> +#include <boost/signals2.hpp> #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <Swiften/Base/ByteArray.h> -#include <Swiften/Base/boost_bsignals.h> #include <Swiften/FileTransfer/FileWriteBytestream.h> using namespace Swift; class FileWriteBytestreamTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(FileWriteBytestreamTest); - CPPUNIT_TEST(testSuccessfulWrite); - CPPUNIT_TEST(testFailingWrite); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(FileWriteBytestreamTest); + CPPUNIT_TEST(testSuccessfulWrite); + CPPUNIT_TEST(testFailingWrite); + CPPUNIT_TEST_SUITE_END(); - public: - void setUp() { - onWriteWasCalled = false; - } + public: + void setUp() { + onWriteWasCalled = false; + } - void testSuccessfulWrite() { - boost::filesystem::path filename = boost::filesystem::unique_path("write_file_bytestream_test_%%%%%%%%%%%%%%%%.bin"); - boost::shared_ptr<WriteBytestream> writeBytestream = boost::make_shared<FileWriteBytestream>(filename.string()); - writeBytestream->onWrite.connect(boost::bind(&FileWriteBytestreamTest::handleOnWrite, this, _1)); + void testSuccessfulWrite() { + boost::filesystem::path filename = boost::filesystem::unique_path("write_file_bytestream_test_%%%%%%%%%%%%%%%%.bin"); + std::shared_ptr<WriteBytestream> writeBytestream = std::make_shared<FileWriteBytestream>(filename.string()); + writeBytestream->onWrite.connect(boost::bind(&FileWriteBytestreamTest::handleOnWrite, this, _1)); - CPPUNIT_ASSERT_EQUAL(true, writeBytestream->write(createByteArray("Some data."))); - CPPUNIT_ASSERT_EQUAL(true, onWriteWasCalled); + CPPUNIT_ASSERT_EQUAL(true, writeBytestream->write(createByteArray("Some data."))); + CPPUNIT_ASSERT_EQUAL(true, onWriteWasCalled); - boost::filesystem::remove(filename); - } + boost::filesystem::remove(filename); + } - void testFailingWrite() { - boost::shared_ptr<WriteBytestream> writeBytestream = boost::make_shared<FileWriteBytestream>(""); - writeBytestream->onWrite.connect(boost::bind(&FileWriteBytestreamTest::handleOnWrite, this, _1)); + void testFailingWrite() { + std::shared_ptr<WriteBytestream> writeBytestream = std::make_shared<FileWriteBytestream>(""); + writeBytestream->onWrite.connect(boost::bind(&FileWriteBytestreamTest::handleOnWrite, this, _1)); - CPPUNIT_ASSERT_EQUAL(false, writeBytestream->write(createByteArray("Some data."))); - CPPUNIT_ASSERT_EQUAL(false, onWriteWasCalled); - } + CPPUNIT_ASSERT_EQUAL(false, writeBytestream->write(createByteArray("Some data."))); + CPPUNIT_ASSERT_EQUAL(false, onWriteWasCalled); + } - void handleOnWrite(const std::vector<unsigned char>& /*data*/) { - onWriteWasCalled = true; - } + void handleOnWrite(const std::vector<unsigned char>& /*data*/) { + onWriteWasCalled = true; + } - private: - bool onWriteWasCalled; + private: + bool onWriteWasCalled; }; CPPUNIT_TEST_SUITE_REGISTRATION(FileWriteBytestreamTest); diff --git a/Swiften/QA/StorageTest/SConscript b/Swiften/QA/StorageTest/SConscript index fff2a2b..bb1a2c6 100644 --- a/Swiften/QA/StorageTest/SConscript +++ b/Swiften/QA/StorageTest/SConscript @@ -3,20 +3,20 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.MergeFlags(myenv["CHECKER_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTOOLS_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - myenv.MergeFlags(myenv["BOOST_FLAGS"]) - myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) - myenv.MergeFlags(myenv.get("EXPAT_FLAGS", {})) - myenv.MergeFlags(myenv.get("LIBXML_FLAGS", {})) - myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) + myenv = env.Clone() + myenv.MergeFlags(myenv["CHECKER_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTOOLS_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + myenv.MergeFlags(myenv["BOOST_FLAGS"]) + myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) + myenv.MergeFlags(myenv.get("EXPAT_FLAGS", {})) + myenv.MergeFlags(myenv.get("LIBXML_FLAGS", {})) + myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) - tester = myenv.Program("StorageTest", [ - #"VCardFileStorageTest.cpp", - "FileReadBytestreamTest.cpp", - "FileWriteBytestreamTest.cpp", - ]) - myenv.Test(tester, "system", is_checker = True) + tester = myenv.Program("StorageTest", [ + #"VCardFileStorageTest.cpp", + "FileReadBytestreamTest.cpp", + "FileWriteBytestreamTest.cpp", + ]) + myenv.Test(tester, "system", is_checker = True) diff --git a/Swiften/QA/StorageTest/VCardFileStorageTest.cpp b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp index e0a65a4..1be2490 100644 --- a/Swiften/QA/StorageTest/VCardFileStorageTest.cpp +++ b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp @@ -1,113 +1,117 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <memory> +#include <sstream> + +#include <boost/algorithm/string.hpp> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <boost/algorithm/string.hpp> -#include <sstream> -#include <Swiften/VCards/VCardFileStorage.h> -#include <Swiften/JID/JID.h> -#include "SwifTools/Application/PlatformApplicationPathProvider.h" #include <Swiften/Elements/VCard.h> +#include <Swiften/JID/JID.h> +#include <Swiften/VCards/VCardFileStorage.h> + +#include <SwifTools/Application/PlatformApplicationPathProvider.h> using namespace Swift; class VCardFileStorageTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(VCardFileStorageTest); - CPPUNIT_TEST(testSetVCard); - // Temporarily disabling this, because it generates error messages on console. Need to figure - // out something for not showing error messages during tests. - //CPPUNIT_TEST(testSetVCard_LargeFilename); - CPPUNIT_TEST(testGetVCard); - CPPUNIT_TEST(testGetVCard_FileDoesNotExist); - //CPPUNIT_TEST(testGetVCard_LargeFilename); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - pathProvider = new PlatformApplicationPathProvider("VCardStorageTest"); - vcardsPath = pathProvider->getExecutableDir() / "vcards"; - boost::filesystem::remove_all(vcardsPath); - } - - void tearDown() { - delete pathProvider; - boost::filesystem::remove_all(vcardsPath); - } - - void testSetVCard() { - boost::shared_ptr<VCardFileStorage> testling(createTestling()); - VCard::ref vcard(new VCard()); - vcard->setFullName("Alice In Wonderland"); - - testling->setVCard(JID("alice@wonderland.lit/TeaRoom"), vcard); - - boost::filesystem::path vcardFile(vcardsPath / "alice@wonderland.lit%2fTeaRoom.xml"); - CPPUNIT_ASSERT(boost::filesystem::exists(vcardFile)); - ByteArray data; - data.readFromFile(vcardFile); - CPPUNIT_ASSERT(boost::starts_with(data.toString(), "<vCard xmlns=\"vcard-temp\">")); - } - - void testSetVCard_LargeFilename() { - std::auto_ptr<VCardFileStorage> testling(createTestling()); - VCard::ref vcard(new VCard()); - vcard->setFullName("Alice In Wonderland"); - - std::ostringstream s; - for (int i = 0; i < 1000; ++i) { - s << "_"; - } - - JID jid("alice@wonderland.lit/" + s.str()); - testling->setVCard(jid, vcard); - - // Just check whether we don't crash - } - - void testGetVCard() { - boost::shared_ptr<VCardFileStorage> testling(createTestling()); - VCard::ref vcard(new VCard()); - vcard->setFullName("Alice In Wonderland"); - testling->setVCard(JID("alice@wonderland.lit"), vcard); - - VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); - CPPUNIT_ASSERT_EQUAL(std::string("Alice In Wonderland"), result->getFullName()); - } - - void testGetVCard_LargeFilename() { - std::auto_ptr<VCardFileStorage> testling(createTestling()); - VCard::ref vcard(new VCard()); - vcard->setFullName("Alice In Wonderland"); - - std::ostringstream s; - for (int i = 0; i < 1000; ++i) { - s << "_"; - } - JID jid("alice@wonderland.lit/" + s.str()); - - VCard::ref result = testling->getVCard(jid); - - // Just check that we don't have an exception - } - - void testGetVCard_FileDoesNotExist() { - boost::shared_ptr<VCardFileStorage> testling(createTestling()); - VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); - CPPUNIT_ASSERT(!result); - } - - private: - VCardFileStorage* createTestling() { - return new VCardFileStorage(vcardsPath); - } - - PlatformApplicationPathProvider* pathProvider; - boost::filesystem::path vcardsPath; + CPPUNIT_TEST_SUITE(VCardFileStorageTest); + CPPUNIT_TEST(testSetVCard); + // Temporarily disabling this, because it generates error messages on console. Need to figure + // out something for not showing error messages during tests. + //CPPUNIT_TEST(testSetVCard_LargeFilename); + CPPUNIT_TEST(testGetVCard); + CPPUNIT_TEST(testGetVCard_FileDoesNotExist); + //CPPUNIT_TEST(testGetVCard_LargeFilename); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + pathProvider = new PlatformApplicationPathProvider("VCardStorageTest"); + vcardsPath = pathProvider->getExecutableDir() / "vcards"; + boost::filesystem::remove_all(vcardsPath); + } + + void tearDown() { + delete pathProvider; + boost::filesystem::remove_all(vcardsPath); + } + + void testSetVCard() { + std::shared_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + + testling->setVCard(JID("alice@wonderland.lit/TeaRoom"), vcard); + + boost::filesystem::path vcardFile(vcardsPath / "alice@wonderland.lit%2fTeaRoom.xml"); + CPPUNIT_ASSERT(boost::filesystem::exists(vcardFile)); + ByteArray data; + data.readFromFile(vcardFile); + CPPUNIT_ASSERT(boost::starts_with(data.toString(), "<vCard xmlns=\"vcard-temp\">")); + } + + void testSetVCard_LargeFilename() { + std::unique_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + + std::ostringstream s; + for (int i = 0; i < 1000; ++i) { + s << "_"; + } + + JID jid("alice@wonderland.lit/" + s.str()); + testling->setVCard(jid, vcard); + + // Just check whether we don't crash + } + + void testGetVCard() { + std::shared_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + testling->setVCard(JID("alice@wonderland.lit"), vcard); + + VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); + CPPUNIT_ASSERT_EQUAL(std::string("Alice In Wonderland"), result->getFullName()); + } + + void testGetVCard_LargeFilename() { + std::unique_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref vcard(new VCard()); + vcard->setFullName("Alice In Wonderland"); + + std::ostringstream s; + for (int i = 0; i < 1000; ++i) { + s << "_"; + } + JID jid("alice@wonderland.lit/" + s.str()); + + VCard::ref result = testling->getVCard(jid); + + // Just check that we don't have an exception + } + + void testGetVCard_FileDoesNotExist() { + std::shared_ptr<VCardFileStorage> testling(createTestling()); + VCard::ref result = testling->getVCard(JID("alice@wonderland.lit")); + CPPUNIT_ASSERT(!result); + } + + private: + VCardFileStorage* createTestling() { + return new VCardFileStorage(vcardsPath); + } + + PlatformApplicationPathProvider* pathProvider; + boost::filesystem::path vcardsPath; }; CPPUNIT_TEST_SUITE_REGISTRATION(VCardFileStorageTest); diff --git a/Swiften/QA/TLSTest/CertificateErrorTest.cpp b/Swiften/QA/TLSTest/CertificateErrorTest.cpp index 1d87994..abd004e 100644 --- a/Swiften/QA/TLSTest/CertificateErrorTest.cpp +++ b/Swiften/QA/TLSTest/CertificateErrorTest.cpp @@ -5,7 +5,7 @@ */ /* - This file uses http://www.tls-o-matic.com/ to test the currently configured TLS backend for correct certificate validation behavior. + This file uses http://www.tls-o-matic.com/ to test the currently configured TLS backend for correct certificate validation behavior. */ #include <cppunit/extensions/HelperMacros.h> @@ -29,195 +29,195 @@ using namespace Swift; class CertificateErrorTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(CertificateErrorTest); + CPPUNIT_TEST_SUITE(CertificateErrorTest); - // These test require the TLS-O-Matic testing CA to be trusted. For more info see https://www.tls-o-matic.com/https/test1 . - CPPUNIT_TEST(testTLS_O_MaticTrusted); - CPPUNIT_TEST(testTLS_O_MaticCertificateFromTheFuture); - CPPUNIT_TEST(testTLS_O_MaticCertificateFromThePast); - CPPUNIT_TEST(testTLS_O_MaticCertificateFromUnknownCA); - CPPUNIT_TEST(testTLS_O_MaticCertificateWrongPurpose); + // These test require the TLS-O-Matic testing CA to be trusted. For more info see https://www.tls-o-matic.com/https/test1 . + CPPUNIT_TEST(testTLS_O_MaticTrusted); + CPPUNIT_TEST(testTLS_O_MaticCertificateFromTheFuture); + CPPUNIT_TEST(testTLS_O_MaticCertificateFromThePast); + CPPUNIT_TEST(testTLS_O_MaticCertificateFromUnknownCA); + CPPUNIT_TEST(testTLS_O_MaticCertificateWrongPurpose); #if !defined(HAVE_OPENSSL) - // Our OpenSSL backend does not support revocation. We excluded it from the revocation tests. - CPPUNIT_TEST(testRevokedCertificateRevocationDisabled); - CPPUNIT_TEST(testRevokedCertificateRevocationEnabled); + // Our OpenSSL backend does not support revocation. We excluded it from the revocation tests. + CPPUNIT_TEST(testRevokedCertificateRevocationDisabled); + CPPUNIT_TEST(testRevokedCertificateRevocationEnabled); #endif - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - eventLoop_ = new DummyEventLoop(); - boostIOServiceThread_ = new BoostIOServiceThread(); - boostIOService_ = boost::make_shared<boost::asio::io_service>(); - connectionFactory_ = new BoostConnectionFactory(boostIOServiceThread_->getIOService(), eventLoop_); - idnConverter_ = PlatformIDNConverter::create(); - domainNameResolver_ = new PlatformDomainNameResolver(idnConverter_, eventLoop_), - - tlsFactories_ = new PlatformTLSFactories(); - tlsContextFactory_ = tlsFactories_->getTLSContextFactory(); - - tlsContextFactory_->setCheckCertificateRevocation(false); - - tlsConnectionFactory_ = new TLSConnectionFactory(tlsContextFactory_, connectionFactory_, TLSOptions()); - - connectFinished_ = false; - connectFinishedWithError_ = false; - } - - void tearDown() { - delete tlsConnectionFactory_; - delete tlsFactories_; - - delete domainNameResolver_; - delete idnConverter_; - delete connectionFactory_; - delete boostIOServiceThread_; - while (eventLoop_->hasEvents()) { - eventLoop_->processEvents(); - } - delete eventLoop_; - } - - HostAddress resolveName(const std::string& name) { - boost::shared_ptr<DomainNameAddressQuery> query = domainNameResolver_->createAddressQuery(name); - query->onResult.connect(boost::bind(&CertificateErrorTest::handleAddressQueryResult, this, _1, _2)); - lastResoverResult_ = HostAddress(); - resolvingDone_ = false; - - query->run(); - while(!resolvingDone_) { - eventLoop_->processEvents(); - } - - return lastResoverResult_; - } - - void connectToServer(boost::shared_ptr<TLSConnection> connection, const std::string& hostname, int port) { - connection->onConnectFinished.connect(boost::bind(&CertificateErrorTest::handleConnectFinished, this, _1)); - - HostAddress address = resolveName(hostname); - - connection->connect(HostAddressPort(address, port)); - - while (!connectFinished_) { - eventLoop_->processEvents(); - } - } - - void testTLS_O_MaticTrusted() { - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "test1.tls-o-matic.com", 443); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::ref(), context->getPeerCertificateVerificationError()); - } - - void testTLS_O_MaticCertificateFromTheFuture() { - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "test5.tls-o-matic.com", 405); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + eventLoop_ = new DummyEventLoop(); + boostIOServiceThread_ = new BoostIOServiceThread(); + boostIOService_ = std::make_shared<boost::asio::io_service>(); + connectionFactory_ = new BoostConnectionFactory(boostIOServiceThread_->getIOService(), eventLoop_); + idnConverter_ = PlatformIDNConverter::create(); + domainNameResolver_ = new PlatformDomainNameResolver(idnConverter_, eventLoop_); + + tlsFactories_ = new PlatformTLSFactories(); + tlsContextFactory_ = tlsFactories_->getTLSContextFactory(); + + tlsContextFactory_->setCheckCertificateRevocation(false); + + tlsConnectionFactory_ = new TLSConnectionFactory(tlsContextFactory_, connectionFactory_, TLSOptions()); + + connectFinished_ = false; + connectFinishedWithError_ = false; + } + + void tearDown() { + delete tlsConnectionFactory_; + delete tlsFactories_; + + delete domainNameResolver_; + delete idnConverter_; + delete connectionFactory_; + delete boostIOServiceThread_; + while (eventLoop_->hasEvents()) { + eventLoop_->processEvents(); + } + delete eventLoop_; + } + + HostAddress resolveName(const std::string& name) { + std::shared_ptr<DomainNameAddressQuery> query = domainNameResolver_->createAddressQuery(name); + query->onResult.connect(boost::bind(&CertificateErrorTest::handleAddressQueryResult, this, _1, _2)); + lastResoverResult_ = HostAddress(); + resolvingDone_ = false; + + query->run(); + while(!resolvingDone_) { + eventLoop_->processEvents(); + } + + return lastResoverResult_; + } + + void connectToServer(std::shared_ptr<TLSConnection> connection, const std::string& hostname, int port) { + connection->onConnectFinished.connect(boost::bind(&CertificateErrorTest::handleConnectFinished, this, _1)); + + HostAddress address = resolveName(hostname); + + connection->connect(HostAddressPort(address, port)); + + while (!connectFinished_) { + eventLoop_->processEvents(); + } + } + + void testTLS_O_MaticTrusted() { + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "test1.tls-o-matic.com", 443); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::ref(), context->getPeerCertificateVerificationError()); + } + + void testTLS_O_MaticCertificateFromTheFuture() { + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "test5.tls-o-matic.com", 405); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); #if defined(HAVE_SCHANNEL) - // Windows SChannel API does not differentiate between expired and not yet valid. - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType()); + // Windows SChannel API does not differentiate between expired and not yet valid. + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType()); #else - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::NotYetValid, context->getPeerCertificateVerificationError()->getType()); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::NotYetValid, context->getPeerCertificateVerificationError()->getType()); #endif - } - - void testTLS_O_MaticCertificateFromThePast() { - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "test6.tls-o-matic.com", 406); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType()); - } - - void testTLS_O_MaticCertificateFromUnknownCA() { - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "test7.tls-o-matic.com", 407); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Untrusted, context->getPeerCertificateVerificationError()->getType()); - } - - // test14.tls-o-matic.com:414 - void testTLS_O_MaticCertificateWrongPurpose() { - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "test14.tls-o-matic.com", 414); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidPurpose, context->getPeerCertificateVerificationError()->getType()); - } - - void testRevokedCertificateRevocationDisabled() { - tlsContextFactory_->setCheckCertificateRevocation(false); - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "revoked.grc.com", 443); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(!context->getPeerCertificateVerificationError()); - } - - void testRevokedCertificateRevocationEnabled() { - tlsContextFactory_->setCheckCertificateRevocation(true); - boost::shared_ptr<TLSConnection> connection = boost::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); - TLSContext* context = connection->getTLSContext(); - - connectToServer(connection, "revoked.grc.com", 443); - - CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); - CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); - CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Revoked, context->getPeerCertificateVerificationError()->getType()); - } - - private: - void handleAddressQueryResult(const std::vector<HostAddress>& address, boost::optional<DomainNameResolveError> /* error */) { - if (address.size() > 0) { - lastResoverResult_ = address[0]; - } - resolvingDone_ = true; - } - - void handleConnectFinished(bool error) { - connectFinished_ = true; - connectFinishedWithError_ = error; - } - - private: - BoostIOServiceThread* boostIOServiceThread_; - boost::shared_ptr<boost::asio::io_service> boostIOService_; - DummyEventLoop* eventLoop_; - ConnectionFactory* connectionFactory_; - PlatformTLSFactories* tlsFactories_; - TLSContextFactory* tlsContextFactory_; - TLSConnectionFactory* tlsConnectionFactory_; - - IDNConverter* idnConverter_; - DomainNameResolver* domainNameResolver_; - HostAddress lastResoverResult_; - bool resolvingDone_; - - bool connectFinished_; - bool connectFinishedWithError_; + } + + void testTLS_O_MaticCertificateFromThePast() { + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "test6.tls-o-matic.com", 406); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Expired, context->getPeerCertificateVerificationError()->getType()); + } + + void testTLS_O_MaticCertificateFromUnknownCA() { + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "test7.tls-o-matic.com", 407); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Untrusted, context->getPeerCertificateVerificationError()->getType()); + } + + // test14.tls-o-matic.com:414 + void testTLS_O_MaticCertificateWrongPurpose() { + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "test14.tls-o-matic.com", 414); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::InvalidPurpose, context->getPeerCertificateVerificationError()->getType()); + } + + void testRevokedCertificateRevocationDisabled() { + tlsContextFactory_->setCheckCertificateRevocation(false); + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "revoked.grc.com", 443); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(!context->getPeerCertificateVerificationError()); + } + + void testRevokedCertificateRevocationEnabled() { + tlsContextFactory_->setCheckCertificateRevocation(true); + std::shared_ptr<TLSConnection> connection = std::dynamic_pointer_cast<TLSConnection>(tlsConnectionFactory_->createConnection()); + TLSContext* context = connection->getTLSContext(); + + connectToServer(connection, "revoked.grc.com", 443); + + CPPUNIT_ASSERT_EQUAL(false, connectFinishedWithError_); + CPPUNIT_ASSERT(context->getPeerCertificateVerificationError()); + CPPUNIT_ASSERT_EQUAL(CertificateVerificationError::Revoked, context->getPeerCertificateVerificationError()->getType()); + } + + private: + void handleAddressQueryResult(const std::vector<HostAddress>& address, boost::optional<DomainNameResolveError> /* error */) { + if (address.size() > 0) { + lastResoverResult_ = address[0]; + } + resolvingDone_ = true; + } + + void handleConnectFinished(bool error) { + connectFinished_ = true; + connectFinishedWithError_ = error; + } + + private: + BoostIOServiceThread* boostIOServiceThread_; + std::shared_ptr<boost::asio::io_service> boostIOService_; + DummyEventLoop* eventLoop_; + ConnectionFactory* connectionFactory_; + PlatformTLSFactories* tlsFactories_; + TLSContextFactory* tlsContextFactory_; + TLSConnectionFactory* tlsConnectionFactory_; + + IDNConverter* idnConverter_; + DomainNameResolver* domainNameResolver_; + HostAddress lastResoverResult_; + bool resolvingDone_; + + bool connectFinished_; + bool connectFinishedWithError_; }; diff --git a/Swiften/QA/TLSTest/CertificateTest.cpp b/Swiften/QA/TLSTest/CertificateTest.cpp index 1c3857a..b53cd2f 100644 --- a/Swiften/QA/TLSTest/CertificateTest.cpp +++ b/Swiften/QA/TLSTest/CertificateTest.cpp @@ -1,99 +1,97 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <Swiften/Base/ByteArray.h> +#include <memory> + +#include <boost/bind.hpp> #include <QA/Checker/IO.h> #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <boost/bind.hpp> +#include <Swiften/Base/ByteArray.h> #include <Swiften/TLS/CertificateFactory.h> -#include "SwifTools/Application/PlatformApplicationPathProvider.h" + +#include <SwifTools/Application/PlatformApplicationPathProvider.h> using namespace Swift; template<typename CERTIFICATE_FACTORY> class CertificateTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(CertificateTest); - CPPUNIT_TEST(testConstructFromDER); - CPPUNIT_TEST(testToDER); - //CPPUNIT_TEST(testGetSubjectName); - CPPUNIT_TEST(testGetCommonNames); - CPPUNIT_TEST(testGetSRVNames); - CPPUNIT_TEST(testGetDNSNames); - CPPUNIT_TEST(testGetXMPPAddresses); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() { - pathProvider = new PlatformApplicationPathProvider("FileReadBytestreamTest"); - readByteArrayFromFile(certificateData, (pathProvider->getExecutableDir() / "jabber_org.crt")); - certificateFactory = new CERTIFICATE_FACTORY(); - } - - void tearDown() { - delete certificateFactory; - delete pathProvider; - } - - void testConstructFromDER() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); - } - - void testToDER() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(certificateData, testling->toDER()); - } + CPPUNIT_TEST_SUITE(CertificateTest); + CPPUNIT_TEST(testConstructFromDER); + CPPUNIT_TEST(testToDER); + //CPPUNIT_TEST(testGetSubjectName); + CPPUNIT_TEST(testGetCommonNames); + CPPUNIT_TEST(testGetSRVNames); + CPPUNIT_TEST(testGetDNSNames); + CPPUNIT_TEST(testGetXMPPAddresses); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + pathProvider = std::unique_ptr<PlatformApplicationPathProvider>(new PlatformApplicationPathProvider("FileReadBytestreamTest")); + readByteArrayFromFile(certificateData, (pathProvider->getExecutableDir() / "jabber_org.crt")); + certificateFactory = std::unique_ptr<CertificateFactory>(new CERTIFICATE_FACTORY()); + } + + void testConstructFromDER() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); + } + + void testToDER() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(certificateData, testling->toDER()); + } /* - void testGetSubjectName() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData); - - CPPUNIT_ASSERT_EQUAL(std::string("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling->getSubjectName()); - } - */ - - void testGetCommonNames() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getCommonNames().size())); - CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); - } - - void testGetSRVNames() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getSRVNames().size())); - CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getSRVNames()[0]); - } - - void testGetDNSNames() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(testling->getDNSNames().size())); - CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getDNSNames()[0]); - CPPUNIT_ASSERT_EQUAL(std::string("jabber.org"), testling->getDNSNames()[1]); - } - - void testGetXMPPAddresses() { - Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); - - CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getXMPPAddresses().size())); - CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getXMPPAddresses()[0]); - } - - private: - PlatformApplicationPathProvider* pathProvider; - ByteArray certificateData; - CertificateFactory* certificateFactory; + void testGetSubjectName() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData); + + CPPUNIT_ASSERT_EQUAL(std::string("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling->getSubjectName()); + } + */ + + void testGetCommonNames() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getCommonNames().size())); + CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); + } + + void testGetSRVNames() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getSRVNames().size())); + CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getSRVNames()[0]); + } + + void testGetDNSNames() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(testling->getDNSNames().size())); + CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getDNSNames()[0]); + CPPUNIT_ASSERT_EQUAL(std::string("jabber.org"), testling->getDNSNames()[1]); + } + + void testGetXMPPAddresses() { + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getXMPPAddresses().size())); + CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getXMPPAddresses()[0]); + } + + private: + std::unique_ptr<PlatformApplicationPathProvider> pathProvider; + ByteArray certificateData; + std::unique_ptr<CertificateFactory> certificateFactory; }; #ifdef HAVE_OPENSSL diff --git a/Swiften/QA/TLSTest/SConscript b/Swiften/QA/TLSTest/SConscript index 0ac50e6..b7bfc7d 100644 --- a/Swiften/QA/TLSTest/SConscript +++ b/Swiften/QA/TLSTest/SConscript @@ -3,22 +3,24 @@ import os Import("env") if env["TEST"] : - myenv = env.Clone() - myenv.MergeFlags(myenv["CHECKER_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTOOLS_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) - myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) - - if myenv.get("HAVE_OPENSSL", 0) : - myenv.Append(CPPDEFINES = "HAVE_OPENSSL") - elif myenv.get("HAVE_SCHANNEL", 0) : - myenv.Append(CPPDEFINES = "HAVE_SCHANNEL") - elif myenv.get("HAVE_SECURETRANSPORT", 0) : - myenv.Append(CPPDEFINES = "HAVE_SECURETRANSPORT") - - tester = myenv.Program("TLSTest", [ - "CertificateTest.cpp", - "CertificateErrorTest.cpp" - ]) - myenv.Test(tester, "system")
\ No newline at end of file + myenv = env.Clone() + myenv.MergeFlags(myenv["CHECKER_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTOOLS_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv.MergeFlags(myenv["CPPUNIT_FLAGS"]) + + if myenv.get("HAVE_OPENSSL", 0) : + myenv.Append(CPPDEFINES = "HAVE_OPENSSL") + myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) + elif myenv.get("HAVE_SCHANNEL", 0) : + myenv.Append(CPPDEFINES = "HAVE_SCHANNEL") + elif myenv.get("HAVE_SECURETRANSPORT", 0) : + myenv.Append(CPPDEFINES = "HAVE_SECURETRANSPORT") + + tester = myenv.Program("TLSTest", [ + "CertificateTest.cpp", + # Reenable if either http://www.tls-o-matic.com/ is fixed or we have setup a replacement. + #"CertificateErrorTest.cpp" + ]) + myenv.Test(tester, "system")
\ No newline at end of file diff --git a/Swiften/QA/TLSTest/jabber_org.crt b/Swiften/QA/TLSTest/jabber_org.crt Binary files differindex f7d619f..5a9ddaa 100644 --- a/Swiften/QA/TLSTest/jabber_org.crt +++ b/Swiften/QA/TLSTest/jabber_org.crt |