blob: 1714e8a0ce00a236b3dbd13c841f822c4fd6739f [file] [log] [blame]
Mengjin Yanaec70742014-08-25 10:37:45 -07001#include "chatroom-discovery-logic.hpp"
2#include <boost/test/unit_test.hpp>
3#include "dummy-client-face.hpp"
4
5namespace chronos {
6
7namespace test {
8
9using std::string;
10
11BOOST_AUTO_TEST_SUITE(TestChatroomDiscoveryLogic)
12
13class Fixture
14{
15public:
16 Fixture()
17 : face(makeDummyClientFace())
18 {
19 }
20
21 void
22 update(const ChatroomInfo& chatroomName, bool isAdd)
23 {
24 }
25
26public:
27 shared_ptr<DummyClientFace> face;
28};
29
30BOOST_FIXTURE_TEST_CASE(AddLocalChatroom, Fixture)
31{
32 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
33
34 ChatroomInfo chatroom;
35 chatroom.setName(ndn::Name::Component("lunch-talk"));
36 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
37 chatroom.addParticipant(Name("/ndn/ucla/alice"));
38 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
39 chatroomDiscoveryLogic.addLocalChatroom(chatroom);
40
41 ndn::Name::Component chatroomName("lunch-talk");
42
43 BOOST_CHECK_EQUAL(chatroomDiscoveryLogic.getChatrooms().size(), 1);
44 BOOST_CHECK_EQUAL(chatroom.getName(),
45 chatroomDiscoveryLogic.getChatrooms().find(chatroomName)
46 ->second.getName());
47 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(),
48 chatroomDiscoveryLogic
49 .getChatrooms().find(chatroomName)->second.getParticipants().size());
50 BOOST_CHECK_EQUAL(chatroom.getParticipants()[0].toUri(),
51 chatroomDiscoveryLogic
52 .getChatrooms().find(chatroomName)->second
53 .getParticipants()[0].toUri());
54 BOOST_CHECK_EQUAL(chatroom.getParticipants()[1].toUri(),
55 chatroomDiscoveryLogic
56 .getChatrooms().find(chatroomName)->second
57 .getParticipants()[1].toUri());
58 BOOST_CHECK_EQUAL(chatroom.getTrustModel(),
59 chatroomDiscoveryLogic
60 .getChatrooms().find(chatroomName)->second.getTrustModel());
61}
62
63BOOST_FIXTURE_TEST_CASE(RemoveLocalChatroom, Fixture)
64{
65 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
66
67 ChatroomInfo chatroom;
68 chatroom.setName(ndn::Name::Component("lunch-talk"));
69 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
70 chatroom.addParticipant(Name("/ndn/ucla/alice"));
71 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
72 chatroomDiscoveryLogic.addLocalChatroom(chatroom);
73
74 ndn::Name::Component chatroomName1("lunch-talk");
75 ndn::Name::Component chatroomName2("supper-talk");
76
77 chatroomDiscoveryLogic.removeLocalChatroom(chatroomName2);
78
79 BOOST_CHECK_EQUAL(chatroomDiscoveryLogic.getChatrooms().size(), 1);
80 BOOST_CHECK_EQUAL(chatroom.getName(),
81 chatroomDiscoveryLogic.getChatrooms().find(chatroomName1)
82 ->second.getName());
83 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(),
84 chatroomDiscoveryLogic
85 .getChatrooms().find(chatroomName1)->second.getParticipants().size());
86 BOOST_CHECK_EQUAL(chatroom.getParticipants()[0].toUri(),
87 chatroomDiscoveryLogic
88 .getChatrooms().find(chatroomName1)->second
89 .getParticipants()[0].toUri());
90 BOOST_CHECK_EQUAL(chatroom.getParticipants()[1].toUri(),
91 chatroomDiscoveryLogic
92 .getChatrooms().find(chatroomName1)->second
93 .getParticipants()[1].toUri());
94 BOOST_CHECK_EQUAL(chatroom.getTrustModel(),
95 chatroomDiscoveryLogic
96 .getChatrooms().find(chatroomName1)->second.getTrustModel());
97
98 chatroomDiscoveryLogic.removeLocalChatroom(chatroomName1);
99 BOOST_CHECK_EQUAL(chatroomDiscoveryLogic.getChatrooms().size(), 0);
100}
101
102BOOST_FIXTURE_TEST_CASE(sendChatroomInterestFunction, Fixture)
103{
104 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
105 chatroomDiscoveryLogic.sendDiscoveryInterest();
106
107 face->processEvents(time::milliseconds(100));
108
109 //with no exclude filter
110 BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 2);//first is nfd register interest
111 BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 0);
112 BOOST_CHECK_EQUAL(face->m_sentInterests[1].getName().toUri(),
113 ChatroomDiscoveryLogic::DISCOVERY_PREFIX.toUri());
114 BOOST_CHECK_EQUAL(face->m_sentInterests[1].getExclude().size(), 0);
115
116 face->m_sentInterests.clear();
117
118 //with exclude filter
119 ChatroomInfo chatroom;
120 chatroom.setName(ndn::Name::Component("lunch-talk"));
121 chatroom.addParticipant(Name("/ndn/ucla/ymj"));
122 chatroom.addParticipant(Name("/ndn/ucla/alice"));
123 chatroom.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
124 chatroomDiscoveryLogic.addLocalChatroom(chatroom);
125
126 chatroomDiscoveryLogic.sendDiscoveryInterest();
127 face->processEvents(time::milliseconds(100));
128
129 BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 1);
130 BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 0);
131 BOOST_CHECK_EQUAL(face->m_sentInterests[0].getName().toUri(),
132 ChatroomDiscoveryLogic::DISCOVERY_PREFIX.toUri());
133 BOOST_CHECK_EQUAL(face->m_sentInterests[0].getExclude().size(), 1);
134 BOOST_CHECK_EQUAL(face->m_sentInterests[0].getExclude().toUri(), "lunch-talk");
135}
136
137BOOST_FIXTURE_TEST_CASE(onDiscoveryInterest, Fixture)
138{
139 face->m_sentInterests.clear();
140 face->m_sentDatas.clear();
141
142 Interest discovery(ChatroomDiscoveryLogic::DISCOVERY_PREFIX);
143 discovery.setMustBeFresh(true);
144 discovery.setInterestLifetime(time::milliseconds(10000));
145
146 Exclude exclude;
147 exclude.excludeOne(ndn::Name::Component("lunch-talk"));
148 discovery.setExclude(exclude);
149
150 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
151
152 ChatroomInfo chatroom1;
153 chatroom1.setName(ndn::Name::Component("lunch-talk"));
154 chatroom1.addParticipant(Name("/ndn/ucla/ymj"));
155 chatroom1.addParticipant(Name("/ndn/ucla/alice"));
156 chatroom1.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
157 chatroomDiscoveryLogic.addLocalChatroom(chatroom1);
158
159 ChatroomInfo chatroom2;
160 chatroom2.setName(ndn::Name::Component("supper-talk"));
161 chatroom2.addParticipant(Name("/ndn/ucla/bob"));
162 chatroom2.addParticipant(Name("/ndn/ucla/peter"));
163 chatroom2.setTrustModel(ChatroomInfo::TRUST_MODEL_HIERARCHICAL);
164 chatroomDiscoveryLogic.addLocalChatroom(chatroom2);
165
166 //discovery
167 chatroomDiscoveryLogic.onDiscoveryInterest(ChatroomDiscoveryLogic::DISCOVERY_PREFIX, discovery);
168
169 face->processEvents(time::milliseconds(100));
170
171 BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 1);//the interest is for nfd register
172 BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 1);
173
174 ChatroomInfo chatroom;
175 chatroom.wireDecode(face->m_sentDatas[0].getContent().blockFromValue());
176 chatroom.setName(face->m_sentDatas[0].getName()
177 .get(ChatroomDiscoveryLogic::OFFSET_CHATROOM_NAME));
178
179 BOOST_CHECK_EQUAL(chatroom.getName(), chatroom2.getName());
180 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(),
181 chatroom2.getParticipants().size());
182 BOOST_CHECK_EQUAL(chatroom.getParticipants()[0].toUri(),
183 chatroom2.getParticipants()[0].toUri());
184 BOOST_CHECK_EQUAL(chatroom.getParticipants()[1].toUri(),
185 chatroom2.getParticipants()[1].toUri());
186 BOOST_CHECK_EQUAL(chatroom.getTrustModel(),
187 chatroom2.getTrustModel());
188
189 //refreshing
190 face->m_sentInterests.clear();
191 face->m_sentDatas.clear();
192
193 Name name = ChatroomDiscoveryLogic::DISCOVERY_PREFIX;
194 name.append(Name("supper-talk"));
195
196 Interest refreshing(name);
197 refreshing.setMustBeFresh(true);
198 refreshing.setInterestLifetime(time::milliseconds(10000));
199
200 chatroomDiscoveryLogic.onDiscoveryInterest(name, refreshing);
201 face->processEvents(time::milliseconds(100));
202
203 BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 0);
204 BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 1);
205
206 chatroom.wireDecode(face->m_sentDatas[0].getContent().blockFromValue());
207 chatroom.setName(face->m_sentDatas[0].getName()
208 .get(ChatroomDiscoveryLogic::OFFSET_CHATROOM_NAME));
209
210 BOOST_CHECK_EQUAL(chatroom.getName(), chatroom2.getName());
211 BOOST_CHECK_EQUAL(chatroom.getParticipants().size(),
212 chatroom2.getParticipants().size());
213 BOOST_CHECK_EQUAL(chatroom.getParticipants()[0].toUri(),
214 chatroom2.getParticipants()[0].toUri());
215 BOOST_CHECK_EQUAL(chatroom.getParticipants()[1].toUri(),
216 chatroom2.getParticipants()[1].toUri());
217 BOOST_CHECK_EQUAL(chatroom.getTrustModel(),
218 chatroom2.getTrustModel());
219}
220
221BOOST_FIXTURE_TEST_CASE(refreshChatroomFunction, Fixture)
222{
223 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
224
225 ChatroomInfo chatroom1;
226 chatroom1.setName(ndn::Name::Component("lunch-talk"));
227 chatroom1.addParticipant(Name("/ndn/ucla/ymj"));
228 chatroom1.addParticipant(Name("/ndn/ucla/alice"));
229 chatroom1.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
230 chatroomDiscoveryLogic.addLocalChatroom(chatroom1);
231
232 chatroomDiscoveryLogic.refreshChatroom(ndn::Name::Component("lunch-talk"));
233 face->processEvents(time::milliseconds(100));
234
235 BOOST_CHECK_EQUAL(face->m_sentInterests.size(), 2);
236 BOOST_CHECK_EQUAL(face->m_sentDatas.size(), 0);
237
238 Name name = ChatroomDiscoveryLogic::DISCOVERY_PREFIX;
239 name.append(Name("lunch-talk"));
240
241 BOOST_CHECK_EQUAL(face->m_sentInterests[1].getName().toUri(), name.toUri());
242 BOOST_CHECK_EQUAL(face->m_sentInterests[1].getExclude().size(), 0);
243}
244
245BOOST_FIXTURE_TEST_CASE(onReceiveData, Fixture)
246{
247 ChatroomDiscoveryLogic chatroomDiscoveryLogic(face, bind(&Fixture::update, this, _1, _2));
248
249 //discovery
250 Interest discovery(ChatroomDiscoveryLogic::DISCOVERY_PREFIX);
251 discovery.setMustBeFresh(true);
252 discovery.setInterestLifetime(time::milliseconds(10000));
253
254 Name dataName(ChatroomDiscoveryLogic::DISCOVERY_PREFIX);
255 dataName.append(ndn::Name::Component("lunch-talk"));
256
257 ChatroomInfo chatroom1;
258 chatroom1.setName(ndn::Name::Component("lunch-talk"));
259 chatroom1.addParticipant(Name("/ndn/ucla/ymj"));
260 chatroom1.addParticipant(Name("/ndn/ucla/alice"));
261 chatroom1.setTrustModel(ChatroomInfo::TRUST_MODEL_WEBOFTRUST);
262
263 shared_ptr<Data> chatroomInfo = make_shared<Data>(dataName);
264 chatroomInfo->setFreshnessPeriod(time::seconds(10));
265 chatroomInfo->setContent(chatroom1.wireEncode());
266
267 chatroomDiscoveryLogic.onReceiveData(discovery, *chatroomInfo, false);
268
269 face->processEvents(time::milliseconds(1000));
270
271 ndn::Name::Component chatroomName("lunch-talk");
272
273 BOOST_CHECK_EQUAL(chatroomDiscoveryLogic.getChatrooms().size(), 1);
274 BOOST_CHECK_EQUAL(chatroom1.getName(),
275 chatroomDiscoveryLogic.getChatrooms().find(chatroomName)
276 ->second.getName());
277 BOOST_CHECK_EQUAL(chatroom1.getParticipants().size(),
278 chatroomDiscoveryLogic
279 .getChatrooms().find(chatroomName)->second.getParticipants().size());
280 BOOST_CHECK_EQUAL(chatroom1.getParticipants()[0].toUri(),
281 chatroomDiscoveryLogic
282 .getChatrooms().find(chatroomName)->second
283 .getParticipants()[0].toUri());
284 BOOST_CHECK_EQUAL(chatroom1.getParticipants()[1].toUri(),
285 chatroomDiscoveryLogic
286 .getChatrooms().find(chatroomName)->second
287 .getParticipants()[1].toUri());
288 BOOST_CHECK_EQUAL(chatroom1.getTrustModel(),
289 chatroomDiscoveryLogic
290 .getChatrooms().find(chatroomName)->second.getTrustModel());
291
292 //refreshing
293 Name name = ChatroomDiscoveryLogic::DISCOVERY_PREFIX;
294 name.append(Name("lunch-talk"));
295
296 Interest refreshing(name);
297 refreshing.setMustBeFresh(true);
298 refreshing.setInterestLifetime(time::milliseconds(10000));
299
300 chatroomDiscoveryLogic.onReceiveData(discovery, *chatroomInfo, false);
301
302 face->processEvents(time::milliseconds(1000));
303
304 BOOST_CHECK_EQUAL(chatroomDiscoveryLogic.getChatrooms().size(), 1);
305 BOOST_CHECK_EQUAL(chatroom1.getName(),
306 chatroomDiscoveryLogic.getChatrooms().find(chatroomName)
307 ->second.getName());
308 BOOST_CHECK_EQUAL(chatroom1.getParticipants().size(),
309 chatroomDiscoveryLogic
310 .getChatrooms().find(chatroomName)->second.getParticipants().size());
311 BOOST_CHECK_EQUAL(chatroom1.getParticipants()[0].toUri(),
312 chatroomDiscoveryLogic
313 .getChatrooms().find(chatroomName)->second
314 .getParticipants()[0].toUri());
315 BOOST_CHECK_EQUAL(chatroom1.getParticipants()[1].toUri(),
316 chatroomDiscoveryLogic
317 .getChatrooms().find(chatroomName)->second
318 .getParticipants()[1].toUri());
319 BOOST_CHECK_EQUAL(chatroom1.getTrustModel(),
320 chatroomDiscoveryLogic
321 .getChatrooms().find(chatroomName)->second.getTrustModel());
322}
323
324BOOST_AUTO_TEST_SUITE_END()
325
326} // namespace test
327
328} // namespace chronos