1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#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 canCreate() == false
using namespace Swift;
class LinkLocalServiceBrowserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(LinkLocalServiceBrowserTest);
CPPUNIT_TEST(testServiceAdded);
CPPUNIT_TEST(testServiceAdded_NoServiceInfo);
CPPUNIT_TEST(testServiceChanged);
CPPUNIT_TEST(testServiceRemoved);
CPPUNIT_TEST_SUITE_END();
public:
LinkLocalServiceBrowserTest() {}
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());
}
void tearDown() {
addedServices.clear();
removedServices.clear();
changedServices.clear();
delete testServiceInfo2;
delete testServiceInfo;
delete testServiceID;
delete dnsSDServiceFactory;
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);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(changedServices.size()));
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(removedServices.size()));
std::vector<LinkLocalService> services = testling->getServices();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(services.size()));
CPPUNIT_ASSERT(*testServiceID == services[0].getID());
CPPUNIT_ASSERT(testServiceInfo->port == services[0].getPort());
CPPUNIT_ASSERT(testServiceInfo->host == services[0].getHostname());
}
void testServiceAdded_NoServiceInfo() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
dnsSDService()->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()));
}
void testServiceChanged() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
dnsSDService()->addService(*testServiceID);
eventLoop->processEvents();
dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo2);
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(addedServices.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(changedServices.size()));
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(removedServices.size()));
CPPUNIT_ASSERT(changedServices[0] == *testServiceID);
std::vector<LinkLocalService> services = testling->getServices();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(services.size()));
CPPUNIT_ASSERT(*testServiceID == services[0].getID());
CPPUNIT_ASSERT(testServiceInfo2->port == services[0].getPort());
CPPUNIT_ASSERT(testServiceInfo2->host == services[0].getHostname());
}
void testServiceRemoved() {
boost::shared_ptr<LinkLocalServiceBrowser> testling = createTestling();
dnsSDService()->setServiceInfo(*testServiceID,*testServiceInfo);
dnsSDService()->addService(*testServiceID);
eventLoop->processEvents();
dnsSDService()->removeService(*testServiceID);
eventLoop->processEvents();
dnsSDService()->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()));
}
private:
boost::shared_ptr<LinkLocalServiceBrowser> createTestling() {
boost::shared_ptr<LinkLocalServiceBrowser> testling(
new LinkLocalServiceBrowser(dnsSDServiceFactory));
testling->onServiceAdded.connect(boost::bind(
&LinkLocalServiceBrowserTest::handleServiceAdded, this, _1));
testling->onServiceChanged.connect(boost::bind(
&LinkLocalServiceBrowserTest::handleServiceChanged, this, _1));
testling->onServiceRemoved.connect(boost::bind(
&LinkLocalServiceBrowserTest::handleServiceRemoved, this, _1));
return testling;
}
void handleServiceAdded(const LinkLocalServiceID& service) {
addedServices.push_back(service);
}
void handleServiceRemoved(const LinkLocalServiceID& service) {
removedServices.push_back(service);
}
void handleServiceChanged(const LinkLocalServiceID& 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;
};
CPPUNIT_TEST_SUITE_REGISTRATION(LinkLocalServiceBrowserTest);
|