summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/LinkLocal/UnitTest')
-rw-r--r--Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp95
-rw-r--r--Swiften/LinkLocal/UnitTest/Makefile.inc1
-rw-r--r--Swiften/LinkLocal/UnitTest/MockDNSSDService.h74
3 files changed, 170 insertions, 0 deletions
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
new file mode 100644
index 0000000..449a823
--- /dev/null
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
@@ -0,0 +1,95 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <boost/bind.hpp>
+#include <map>
+
+#include "Swiften/LinkLocal/LinkLocalServiceBrowser.h"
+#include "Swiften/LinkLocal/LinkLocalService.h"
+#include "Swiften/LinkLocal/UnitTest/MockDNSSDService.h"
+#include "Swiften/LinkLocal/DNSSDService.h"
+#include "Swiften/EventLoop/DummyEventLoop.h"
+
+// Test IP address change
+
+using namespace Swift;
+
+class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(LinkLocalServiceBrowserTest);
+ CPPUNIT_TEST(testServiceAdded);
+ CPPUNIT_TEST(testServiceAdded_NoServiceInfo);
+ CPPUNIT_TEST(testServiceChanged);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ LinkLocalServiceBrowserTest() {}
+
+ void setUp() {
+ eventLoop = new DummyEventLoop();
+ dnsSDService = boost::shared_ptr<MockDNSSDService>(new MockDNSSDService());
+ testServiceID = new LinkLocalServiceID("foo", "bar.local");
+ testServiceInfo = new DNSSDService::ResolveResult("xmpp.bar.local", 1234, LinkLocalServiceInfo());
+ testServiceInfo2 = new DNSSDService::ResolveResult("xmpp.foo.local", 2345, LinkLocalServiceInfo());
+ }
+
+ void tearDown() {
+ delete testServiceInfo2;
+ delete testServiceInfo;
+ delete testServiceID;
+ delete eventLoop;
+ }
+
+ void testServiceAdded() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+
+ dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
+ dnsSDService->addService(*testServiceID);
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
+ CPPUNIT_ASSERT(addedServices[0] == *testServiceID);
+ // TODO: Check getServices
+ }
+
+ void testServiceAdded_NoServiceInfo() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+
+ dnsSDService->addService(*testServiceID);
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(addedServices.size()));
+ }
+
+ void testServiceChanged() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo);
+ dnsSDService->addService(*testServiceID);
+ eventLoop->processEvents();
+
+ dnsSDService->setServiceInfo(*testServiceID,*testServiceInfo2);
+ eventLoop->processEvents();
+ // TODO: Check getServices
+ }
+
+ private:
+ boost::shared_ptr<LinkLocalServiceBrowser> createTestling() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling(
+ new LinkLocalServiceBrowser(dnsSDService));
+ testling->onServiceAdded.connect(boost::bind(
+ &LinkLocalServiceBrowserTest::handleServiceAdded, this, _1));
+ return testling;
+ }
+
+ void handleServiceAdded(const LinkLocalServiceID& service) {
+ addedServices.push_back(service);
+ }
+
+ private:
+ DummyEventLoop* eventLoop;
+ boost::shared_ptr<MockDNSSDService> dnsSDService;
+ std::vector<LinkLocalServiceID> addedServices;
+ LinkLocalServiceID* testServiceID;
+ DNSSDService::ResolveResult* testServiceInfo;
+ DNSSDService::ResolveResult* testServiceInfo2;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(LinkLocalServiceBrowserTest);
diff --git a/Swiften/LinkLocal/UnitTest/Makefile.inc b/Swiften/LinkLocal/UnitTest/Makefile.inc
index abc1180..9640fa7 100644
--- a/Swiften/LinkLocal/UnitTest/Makefile.inc
+++ b/Swiften/LinkLocal/UnitTest/Makefile.inc
@@ -1,2 +1,3 @@
UNITTEST_SOURCES += \
+ Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp \
Swiften/LinkLocal/UnitTest/LinkLocalServiceInfoTest.cpp
diff --git a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
new file mode 100644
index 0000000..5d857fb
--- /dev/null
+++ b/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
@@ -0,0 +1,74 @@
+#pragma once
+
+#include <vector>
+#include <map>
+#include <boost/bind.hpp>
+
+#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/LinkLocal/DNSSDService.h"
+
+namespace Swift {
+ class MockDNSSDService : public DNSSDService {
+ public:
+ MockDNSSDService() {
+ }
+
+ void start() {
+ }
+
+ void stop() {
+ }
+
+ virtual void registerService(const String&, int, const LinkLocalServiceInfo&) {
+ assert(false);
+ }
+
+ virtual void updateService(const LinkLocalServiceInfo&) {
+ assert(false);
+ }
+
+ virtual void unregisterService() {
+ assert(false);
+ }
+
+ virtual void startResolvingService(const LinkLocalServiceID& id) {
+ resolvingServices.push_back(id);
+ broadcastServiceInfo(id);
+ }
+
+ virtual void stopResolvingService(const LinkLocalServiceID& id) {
+ resolvingServices.erase(std::remove(resolvingServices.begin(), resolvingServices.end(), id), resolvingServices.end());
+ }
+
+ virtual void resolveHostname(const String&, int) {
+ assert(false);
+ }
+
+ void addService(const LinkLocalServiceID& id) {
+ MainEventLoop::postEvent(boost::bind(boost::ref(onServiceAdded), id));
+ }
+
+ void setServiceInfo(const LinkLocalServiceID& id, const DNSSDService::ResolveResult& info) {
+ serviceInfo.insert(std::make_pair(id, info));
+ broadcastServiceInfo(id);
+ }
+
+ private:
+ void broadcastServiceInfo(const LinkLocalServiceID& id) {
+ if (std::find(resolvingServices.begin(), resolvingServices.end(), id) != resolvingServices.end()) {
+ ServiceInfoMap::const_iterator i = serviceInfo.find(id);
+ if (i != serviceInfo.end()) {
+ MainEventLoop::postEvent(
+ boost::bind(boost::ref(onServiceResolved), id, i->second));
+ }
+ }
+ }
+
+ private:
+ typedef std::map<LinkLocalServiceID,DNSSDService::ResolveResult> ServiceInfoMap;
+ ServiceInfoMap serviceInfo;
+ std::vector<LinkLocalServiceID> resolvingServices;
+ };
+
+
+}