diff options
Diffstat (limited to 'Swiften/LinkLocal/UnitTest')
-rw-r--r-- | Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp | 97 | ||||
-rw-r--r-- | Swiften/LinkLocal/UnitTest/Makefile.inc | 3 |
2 files changed, 99 insertions, 1 deletions
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp new file mode 100644 index 0000000..1b9b0aa --- /dev/null +++ b/Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp @@ -0,0 +1,97 @@ +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include "Swiften/LinkLocal/LinkLocalConnector.h" +#include "Swiften/LinkLocal/LinkLocalService.h" +#include "Swiften/LinkLocal/DNSSD/DNSSDServiceID.h" +#include "Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h" +#include "Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h" +#include "Swiften/EventLoop/DummyEventLoop.h" +#include "Swiften/Network/FakeConnection.h" + +using namespace Swift; + +class LinkLocalConnectorTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(LinkLocalConnectorTest); + CPPUNIT_TEST(testConnect); + CPPUNIT_TEST(testConnect_UnableToResolve); + CPPUNIT_TEST(testConnect_UnableToConnect); + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() { + eventLoop = new DummyEventLoop(); + querier = boost::shared_ptr<FakeDNSSDQuerier>( + new FakeDNSSDQuerier("rabbithole.local")); + connection = boost::shared_ptr<FakeConnection>(new FakeConnection()); + connectFinished = false; + } + + void tearDown() { + delete eventLoop; + } + + void testConnect() { + boost::shared_ptr<LinkLocalConnector> + testling(createConnector("rabbithole.local", 1234)); + querier->setAddress("rabbithole.local", HostAddress("192.168.1.1")); + + testling->connect(); + eventLoop->processEvents(); + + CPPUNIT_ASSERT(connectFinished); + CPPUNIT_ASSERT(!connectError); + CPPUNIT_ASSERT(connection->connectedTo); + CPPUNIT_ASSERT_EQUAL(String(connection->connectedTo->getAddress().toString()), String("192.168.1.1")); + CPPUNIT_ASSERT_EQUAL(connection->connectedTo->getPort(), 1234); + } + + void testConnect_UnableToResolve() { + boost::shared_ptr<LinkLocalConnector> + testling(createConnector("rabbithole.local", 1234)); + + testling->connect(); + eventLoop->processEvents(); + + CPPUNIT_ASSERT(connectFinished); + CPPUNIT_ASSERT(connectError); + CPPUNIT_ASSERT(!connection->connectedTo); + } + + void testConnect_UnableToConnect() { + boost::shared_ptr<LinkLocalConnector> + testling(createConnector("rabbithole.local", 1234)); + querier->setAddress("rabbithole.local", HostAddress("192.168.1.1")); + connection->setError(Connection::ReadError); + + testling->connect(); + eventLoop->processEvents(); + + CPPUNIT_ASSERT(connectFinished); + CPPUNIT_ASSERT(connectError); + CPPUNIT_ASSERT(!connection->connectedTo); + } + + private: + boost::shared_ptr<LinkLocalConnector> createConnector(const String& hostname, int port) { + boost::shared_ptr<LinkLocalConnector> result(new LinkLocalConnector( + JID("rabbit@teaparty"), hostname, 0, port, querier, connection)); + result->onConnectFinished.connect( + boost::bind(&LinkLocalConnectorTest::handleConnected, this, _1)); + return result; + } + + void handleConnected(bool e) { + connectFinished = true; + connectError = e; + } + + private: + DummyEventLoop* eventLoop; + boost::shared_ptr<FakeDNSSDQuerier> querier; + boost::shared_ptr<FakeConnection> connection; + bool connectFinished; + bool connectError; +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(LinkLocalConnectorTest); diff --git a/Swiften/LinkLocal/UnitTest/Makefile.inc b/Swiften/LinkLocal/UnitTest/Makefile.inc index e5f1bf0..330808a 100644 --- a/Swiften/LinkLocal/UnitTest/Makefile.inc +++ b/Swiften/LinkLocal/UnitTest/Makefile.inc @@ -1,4 +1,5 @@ UNITTEST_SOURCES += \ Swiften/LinkLocal/UnitTest/LinkLocalServiceTest.cpp \ Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp \ - Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp + Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp \ + Swiften/LinkLocal/UnitTest/LinkLocalConnectorTest.cpp |