summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-26 08:21:20 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-26 14:52:17 (GMT)
commit26d623d3cfd8937fb52acf76ef33d230f5010538 (patch)
treeade8246f719fa4dee9de1daaa2ba3f7b6c324e53 /Swiften/LinkLocal/UnitTest
parent2833b8f09c9aef09004662a2a89eefbaee1e4247 (diff)
downloadswift-26d623d3cfd8937fb52acf76ef33d230f5010538.zip
swift-26d623d3cfd8937fb52acf76ef33d230f5010538.tar.bz2
Implement fake DNSSD querier.
Diffstat (limited to 'Swiften/LinkLocal/UnitTest')
-rw-r--r--Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp162
-rw-r--r--Swiften/LinkLocal/UnitTest/Makefile.inc1
-rw-r--r--Swiften/LinkLocal/UnitTest/MockDNSSDService.h97
3 files changed, 126 insertions, 134 deletions
diff --git a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
index 34f797c..7e2dd26 100644
--- a/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
+++ b/Swiften/LinkLocal/UnitTest/LinkLocalServiceBrowserTest.cpp
@@ -5,20 +5,26 @@
#include "Swiften/LinkLocal/LinkLocalServiceBrowser.h"
#include "Swiften/LinkLocal/LinkLocalService.h"
-#include "Swiften/LinkLocal/UnitTest/MockDNSSDService.h"
-#include "Swiften/LinkLocal/DNSSDService.h"
+#include "Swiften/LinkLocal/DNSSD/DNSSDServiceID.h"
+#include "Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h"
+#include "Swiften/LinkLocal/DNSSD/Fake/FakeDNSSDQuerier.h"
#include "Swiften/EventLoop/DummyEventLoop.h"
-// Test canCreate() == false
-
using namespace Swift;
class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(LinkLocalServiceBrowserTest);
+ CPPUNIT_TEST(testConstructor);
+ CPPUNIT_TEST(testStart);
CPPUNIT_TEST(testServiceAdded);
CPPUNIT_TEST(testServiceAdded_NoServiceInfo);
CPPUNIT_TEST(testServiceChanged);
CPPUNIT_TEST(testServiceRemoved);
+ CPPUNIT_TEST(testError_BrowseErrorAfterStart);
+ CPPUNIT_TEST(testError_BrowseErrorAfterResolve);
+ CPPUNIT_TEST(testRegisterService);
+ CPPUNIT_TEST(testRegisterService_Error);
+ CPPUNIT_TEST(testRegisterService_Reregister);
CPPUNIT_TEST_SUITE_END();
public:
@@ -26,10 +32,10 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
void setUp() {
eventLoop = new DummyEventLoop();
- dnsSDServiceFactory = new MockDNSSDServiceFactory();
- testServiceID = new LinkLocalServiceID("foo", "bar.local");
- testServiceInfo = new DNSSDService::ResolveResult("xmpp.bar.local", 1234, LinkLocalServiceInfo());
- testServiceInfo2 = new DNSSDService::ResolveResult("xmpp.foo.local", 2345, LinkLocalServiceInfo());
+ querier = boost::shared_ptr<FakeDNSSDQuerier>(new FakeDNSSDQuerier());
+ testServiceID = new DNSSDServiceID("foo", "bar.local");
+ testServiceInfo = new DNSSDResolveServiceQuery::Result("_presence._tcp.bar.local", "xmpp.bar.local", 1234, LinkLocalServiceInfo());
+ testServiceInfo2 = new DNSSDResolveServiceQuery::Result("_presence.tcp.bar.local", "xmpp.foo.local", 2345, LinkLocalServiceInfo());
}
void tearDown() {
@@ -40,15 +46,33 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
delete testServiceInfo2;
delete testServiceInfo;
delete testServiceID;
- delete dnsSDServiceFactory;
delete eventLoop;
}
+ void testConstructor() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+
+ CPPUNIT_ASSERT(!testling->isRunning());
+ CPPUNIT_ASSERT(!testling->hasError());
+ }
+
+ void testStart() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+
+ CPPUNIT_ASSERT(testling->isRunning());
+ CPPUNIT_ASSERT(!testling->hasError());
+
+ testling->stop();
+ }
+
void testServiceAdded() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ eventLoop->processEvents();
- dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
- dnsSDService()->addService(*testServiceID);
+ querier->setServiceInfo(*testServiceID,*testServiceInfo);
+ querier->addService(*testServiceID);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
@@ -60,26 +84,33 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(*testServiceID == services[0].getID());
CPPUNIT_ASSERT(testServiceInfo->port == services[0].getPort());
CPPUNIT_ASSERT(testServiceInfo->host == services[0].getHostname());
+
+ testling->stop();
}
void testServiceAdded_NoServiceInfo() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ eventLoop->processEvents();
- dnsSDService()->addService(*testServiceID);
+ querier->addService(*testServiceID);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(addedServices.size()));
std::vector<LinkLocalService> services = testling->getServices();
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(services.size()));
+
+ testling->stop();
}
void testServiceChanged() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
- dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
- dnsSDService()->addService(*testServiceID);
+ testling->start();
+ querier->setServiceInfo(*testServiceID,*testServiceInfo);
+ querier->addService(*testServiceID);
eventLoop->processEvents();
- dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo2);
+ querier->setServiceInfo(*testServiceID,*testServiceInfo2);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
@@ -91,31 +122,93 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(*testServiceID == services[0].getID());
CPPUNIT_ASSERT(testServiceInfo2->port == services[0].getPort());
CPPUNIT_ASSERT(testServiceInfo2->host == services[0].getHostname());
+
+ testling->stop();
}
void testServiceRemoved() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
- dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
- dnsSDService()->addService(*testServiceID);
+ testling->start();
+ querier->setServiceInfo(*testServiceID,*testServiceInfo);
+ querier->addService(*testServiceID);
eventLoop->processEvents();
- dnsSDService()->removeService(*testServiceID);
+ querier->removeService(*testServiceID);
eventLoop->processEvents();
- dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo2);
+ querier->setServiceInfo(*testServiceID,*testServiceInfo2);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changedServices.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(removedServices.size()));
CPPUNIT_ASSERT(removedServices[0] == *testServiceID);
- std::vector<LinkLocalService> services = testling->getServices();
- CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(services.size()));
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(testling->getServices().size()));
+
+ testling->stop();
+ }
+
+ void testError_BrowseErrorAfterStart() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+
+ querier->setBrowseError();
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT(!testling->isRunning());
+ CPPUNIT_ASSERT(testling->hasError());
+ }
+
+ void testError_BrowseErrorAfterResolve() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ querier->setServiceInfo(*testServiceID,*testServiceInfo);
+ querier->addService(*testServiceID);
+ eventLoop->processEvents();
+
+ querier->setBrowseError();
+ eventLoop->processEvents();
+ querier->setServiceInfo(*testServiceID,*testServiceInfo2);
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT(!testling->isRunning());
+ CPPUNIT_ASSERT(testling->hasError());
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(testling->getServices().size()));
+ CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changedServices.size()));
+ }
+
+ void testRegisterService() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ eventLoop->processEvents();
+
+ testling->stop();
+ }
+
+ void testRegisterService_Error() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ testling->registerService("", 1234);
+ eventLoop->processEvents();
+
+ querier->setRegisterError();
+ eventLoop->processEvents();
+
+ CPPUNIT_ASSERT(!testling->isRunning());
+ CPPUNIT_ASSERT(testling->hasError());
+ }
+
+ void testRegisterService_Reregister() {
+ boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
+ testling->start();
+ eventLoop->processEvents();
+
+ testling->stop();
}
private:
boost::shared_ptr<LinkLocalServiceBrowser> createTestling() {
boost::shared_ptr<LinkLocalServiceBrowser> testling(
- new LinkLocalServiceBrowser(dnsSDServiceFactory));
+ new LinkLocalServiceBrowser(querier));
testling->onServiceAdded.connect(boost::bind(
&LinkLocalServiceBrowserTest::handleServiceAdded, this, _1));
testling->onServiceChanged.connect(boost::bind(
@@ -125,32 +218,27 @@ class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
return testling;
}
- void handleServiceAdded(const LinkLocalServiceID& service) {
+ void handleServiceAdded(const DNSSDServiceID& service) {
addedServices.push_back(service);
}
- void handleServiceRemoved(const LinkLocalServiceID& service) {
+ void handleServiceRemoved(const DNSSDServiceID& service) {
removedServices.push_back(service);
}
- void handleServiceChanged(const LinkLocalServiceID& service) {
+ void handleServiceChanged(const DNSSDServiceID& service) {
changedServices.push_back(service);
}
- boost::shared_ptr<MockDNSSDService> dnsSDService() const {
- CPPUNIT_ASSERT(dnsSDServiceFactory->services.size() > 0);
- return dnsSDServiceFactory->services[0];
- }
-
private:
DummyEventLoop* eventLoop;
- MockDNSSDServiceFactory* dnsSDServiceFactory;
- std::vector<LinkLocalServiceID> addedServices;
- std::vector<LinkLocalServiceID> changedServices;
- std::vector<LinkLocalServiceID> removedServices;
- LinkLocalServiceID* testServiceID;
- DNSSDService::ResolveResult* testServiceInfo;
- DNSSDService::ResolveResult* testServiceInfo2;
+ boost::shared_ptr<FakeDNSSDQuerier> querier;
+ std::vector<DNSSDServiceID> addedServices;
+ std::vector<DNSSDServiceID> changedServices;
+ std::vector<DNSSDServiceID> removedServices;
+ DNSSDServiceID* testServiceID;
+ DNSSDResolveServiceQuery::Result* testServiceInfo;
+ DNSSDResolveServiceQuery::Result* 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
deleted file mode 100644
index 69ac06d..0000000
--- a/Swiften/LinkLocal/UnitTest/MockDNSSDService.h
+++ /dev/null
@@ -1,97 +0,0 @@
-#pragma once
-
-#include <vector>
-#include <map>
-#include <boost/bind.hpp>
-
-#include "Swiften/EventLoop/MainEventLoop.h"
-#include "Swiften/LinkLocal/DNSSDService.h"
-#include "Swiften/LinkLocal/DNSSDServiceFactory.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 removeService(const LinkLocalServiceID& id) {
- serviceInfo.erase(id);
- MainEventLoop::postEvent(boost::bind(boost::ref(onServiceRemoved), id));
- }
-
- void setServiceInfo(const LinkLocalServiceID& id, const DNSSDService::ResolveResult& info) {
- std::pair<ServiceInfoMap::iterator, bool> r = serviceInfo.insert(std::make_pair(id, info));
- if (!r.second) {
- r.first->second = 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;
- };
-
- class MockDNSSDServiceFactory : public DNSSDServiceFactory {
- public:
- boost::shared_ptr<DNSSDService> createDNSSDService() {
- boost::shared_ptr<MockDNSSDService> result(new MockDNSSDService());
- services.push_back(result);
- return result;
- }
-
- bool canCreate() const {
- return true;
- }
-
- std::vector<boost::shared_ptr<MockDNSSDService> > services;
- };
-
-}