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/NetworkTest | |
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/NetworkTest')
-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 |
3 files changed, 535 insertions, 535 deletions
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); |