diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-03-28 15:46:49 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-03-28 15:46:49 (GMT) |
commit | f53a1ef582494458301b97bf6e546be52d7ff7e8 (patch) | |
tree | 7571b5cbcbd8a8f1dd1c966c9045b6cb69f0e295 /Swiften/QA/NetworkTest/BoostConnectionTest.cpp | |
parent | 638345680d72ca6acaf123f2c8c1c391f696e371 (diff) | |
download | swift-contrib-f53a1ef582494458301b97bf6e546be52d7ff7e8.zip swift-contrib-f53a1ef582494458301b97bf6e546be52d7ff7e8.tar.bz2 |
Moving submodule contents back.
Diffstat (limited to 'Swiften/QA/NetworkTest/BoostConnectionTest.cpp')
-rw-r--r-- | Swiften/QA/NetworkTest/BoostConnectionTest.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Swiften/QA/NetworkTest/BoostConnectionTest.cpp b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp new file mode 100644 index 0000000..af4e003 --- /dev/null +++ b/Swiften/QA/NetworkTest/BoostConnectionTest.cpp @@ -0,0 +1,88 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> +#include <boost/shared_ptr.hpp> + +#include "Swiften/Base/String.h" +#include "Swiften/Base/sleep.h" +#include "Swiften/Network/BoostConnection.h" +#include "Swiften/Network/HostAddress.h" +#include "Swiften/Network/HostAddressPort.h" +#include "Swiften/Network/BoostIOServiceThread.h" +#include "Swiften/EventLoop/DummyEventLoop.h" + +const unsigned char* address = reinterpret_cast<const unsigned char*>("\x41\x63\xde\x89"); + +using namespace Swift; + +class BoostConnectionTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(BoostConnectionTest); + CPPUNIT_TEST(testDestructor); + CPPUNIT_TEST(testDestructor_PendingEvents); + CPPUNIT_TEST(testWrite); + CPPUNIT_TEST_SUITE_END(); + + public: + BoostConnectionTest() {} + + void setUp() { + boostIOServiceThread_ = new BoostIOServiceThread(); + eventLoop_ = new DummyEventLoop(); + disconnected = false; + } + + void tearDown() { + delete eventLoop_; + delete boostIOServiceThread_; + } + + void testDestructor() { + { + boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); + testling->connect(HostAddressPort(HostAddress(address, 4), 5222)); + } + } + + void testDestructor_PendingEvents() { + { + boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); + testling->connect(HostAddressPort(HostAddress(address, 4), 5222)); + while (!eventLoop_->hasEvents()) { + Swift::sleep(10); + } + } + eventLoop_->processEvents(); + } + + void testWrite() { + boost::shared_ptr<BoostConnection> testling(new BoostConnection(&boostIOServiceThread_->getIOService())); + 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("65.99.222.137"), 5222)); + while (receivedData.isEmpty()) { + Swift::sleep(10); + eventLoop_->processEvents(); + } + testling->disconnect(); + } + + void doWrite(BoostConnection* connection) { + connection->write(ByteArray("<stream:stream>")); + } + + void handleDataRead(const ByteArray& data) { + receivedData += data; + } + + void handleDisconnected() { + disconnected = true; + } + + private: + BoostIOServiceThread* boostIOServiceThread_; + DummyEventLoop* eventLoop_; + ByteArray receivedData; + bool disconnected; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(BoostConnectionTest); |