diff options
author | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2016-03-31 14:57:35 (GMT) |
commit | cfbdb43d2cadd40aa87338d41548e4bf89e146e6 (patch) | |
tree | 18d94153a302445196fc0c18586abf44a1ce4a38 /Swiften/QA | |
parent | 1d545a4a7fb877f021508094b88c1f17b30d8b4e (diff) | |
download | swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.zip swift-cfbdb43d2cadd40aa87338d41548e4bf89e146e6.tar.bz2 |
Convert tabs to 4 spaces for all source files
Removed trailing spaces and whitespace on empty lines
in the process.
Changed CheckTabs.py tool to disallow hard tabs in source
files.
Test-Information:
Manually checked 30 random files that the conversion worked
as expected.
Change-Id: I874f99d617bd3d2bb55f02d58f22f58f9b094480
Diffstat (limited to 'Swiften/QA')
-rw-r--r-- | Swiften/QA/ClientTest/ClientTest.cpp | 114 | ||||
-rw-r--r-- | Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp | 40 | ||||
-rw-r--r-- | Swiften/QA/DNSSDTest/DNSSDTest.cpp | 228 | ||||
-rw-r--r-- | Swiften/QA/FileTransferTest/FileTransferTest.cpp | 672 | ||||
-rw-r--r-- | Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp | 384 | ||||
-rwxr-xr-x | Swiften/QA/NetworkTest/BoostConnectionTest.cpp | 276 | ||||
-rw-r--r-- | Swiften/QA/NetworkTest/DomainNameResolverTest.cpp | 410 | ||||
-rw-r--r-- | Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp | 38 | ||||
-rw-r--r-- | Swiften/QA/ReconnectTest/ReconnectTest.cpp | 78 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/FileReadBytestreamTest.cpp | 78 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp | 54 | ||||
-rw-r--r-- | Swiften/QA/StorageTest/VCardFileStorageTest.cpp | 182 | ||||
-rw-r--r-- | Swiften/QA/TLSTest/CertificateErrorTest.cpp | 364 | ||||
-rw-r--r-- | Swiften/QA/TLSTest/CertificateTest.cpp | 146 |
14 files changed, 1532 insertions, 1532 deletions
diff --git a/Swiften/QA/ClientTest/ClientTest.cpp b/Swiften/QA/ClientTest/ClientTest.cpp index 542af09..3a8f303 100644 --- a/Swiften/QA/ClientTest/ClientTest.cpp +++ b/Swiften/QA/ClientTest/ClientTest.cpp @@ -25,75 +25,75 @@ static BoostNetworkFactories networkFactories(&eventLoop); static Client* client = 0; 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(); + 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/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp index a4f145a..b9ec7b8 100644 --- a/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp +++ b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp @@ -42,40 +42,40 @@ static boost::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_; + boost::shared_ptr<Client> clientA_; + std::map<std::string, ByteArray> clientASendFiles_; - int clientBCandidates_; - boost::shared_ptr<Client> clientB_; + int clientBCandidates_; + boost::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/DNSSDTest/DNSSDTest.cpp b/Swiften/QA/DNSSDTest/DNSSDTest.cpp index bd06445..3b7191d 100644 --- a/Swiften/QA/DNSSDTest/DNSSDTest.cpp +++ b/Swiften/QA/DNSSDTest/DNSSDTest.cpp @@ -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 = 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; }; #ifdef HAVE_AVAHI diff --git a/Swiften/QA/FileTransferTest/FileTransferTest.cpp b/Swiften/QA/FileTransferTest/FileTransferTest.cpp index 7a50e9f..8017927 100644 --- a/Swiften/QA/FileTransferTest/FileTransferTest.cpp +++ b/Swiften/QA/FileTransferTest/FileTransferTest.cpp @@ -41,360 +41,360 @@ static boost::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_ = 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_; }; 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 = boost::make_shared<SimpleEventLoop>(); + networkFactories = boost::make_shared<BoostNetworkFactories>(eventLoop.get()); - boost::shared_ptr<FileTransferTest> testRun = boost::make_shared<FileTransferTest>(senderCandidates, receiverCandidates); + boost::shared_ptr<FileTransferTest> testRun = boost::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("|")); + 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; } diff --git a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp index 67b4bfa..a415a0f 100644 --- a/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionServerTest.cpp @@ -19,198 +19,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("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_REGISTRATION(BoostConnectionServerTest); diff --git a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp index a122686..f09e1a7 100755 --- a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp +++ b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp @@ -24,146 +24,146 @@ 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_ = 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_REGISTRATION(BoostConnectionTest); diff --git a/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp b/Swiften/QA/NetworkTest/DomainNameResolverTest.cpp index baa42f9..05dd3a1 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 = boost::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() { + 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; }; CPPUNIT_TEST_SUITE_REGISTRATION(DomainNameResolverTest); diff --git a/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp b/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp index 3a9707b..2b6bfa8 100644 --- a/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp +++ b/Swiften/QA/ProxyProviderTest/ProxyProviderTest.cpp @@ -19,23 +19,23 @@ 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/ReconnectTest/ReconnectTest.cpp b/Swiften/QA/ReconnectTest/ReconnectTest.cpp index fc47312..611c846 100644 --- a/Swiften/QA/ReconnectTest/ReconnectTest.cpp +++ b/Swiften/QA/ReconnectTest/ReconnectTest.cpp @@ -26,48 +26,48 @@ 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(); + 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(); } 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(boost::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/StorageTest/FileReadBytestreamTest.cpp b/Swiften/QA/StorageTest/FileReadBytestreamTest.cpp index d70d9c9..5b3867c 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() { + boost::shared_ptr<FileReadBytestream> testling(createTestling()); - boost::shared_ptr< std::vector<unsigned char> > result = testling->read(10); + boost::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() { + boost::shared_ptr<FileReadBytestream> testling(createTestling()); - testling->read(10); - boost::shared_ptr< std::vector<unsigned char> > result = testling->read(10); + testling->read(10); + boost::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() { + boost::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() { + boost::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..e847c28 100644 --- a/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp +++ b/Swiften/QA/StorageTest/FileWriteBytestreamTest.cpp @@ -17,42 +17,42 @@ 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"); + boost::shared_ptr<WriteBytestream> writeBytestream = boost::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() { + boost::shared_ptr<WriteBytestream> writeBytestream = boost::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/VCardFileStorageTest.cpp b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp index 4d639e6..69e5917 100644 --- a/Swiften/QA/StorageTest/VCardFileStorageTest.cpp +++ b/Swiften/QA/StorageTest/VCardFileStorageTest.cpp @@ -20,97 +20,97 @@ 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() { + 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_REGISTRATION(VCardFileStorageTest); diff --git a/Swiften/QA/TLSTest/CertificateErrorTest.cpp b/Swiften/QA/TLSTest/CertificateErrorTest.cpp index ec8c4e3..b928a3f 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_ = 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()); #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() { + 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_; }; diff --git a/Swiften/QA/TLSTest/CertificateTest.cpp b/Swiften/QA/TLSTest/CertificateTest.cpp index c9315d2..ede1063 100644 --- a/Swiften/QA/TLSTest/CertificateTest.cpp +++ b/Swiften/QA/TLSTest/CertificateTest.cpp @@ -20,81 +20,81 @@ 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 = 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()); + } /* - 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: + PlatformApplicationPathProvider* pathProvider; + ByteArray certificateData; + CertificateFactory* certificateFactory; }; #ifdef HAVE_OPENSSL |