blob: fd17c446c96af1d91b8b5f55c685d4627b737d8c [file] [log] [blame]
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
27#include <boost/make_shared.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070028#include <boost/date_time/posix_time/posix_time.hpp>
29
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070030#include "sync-logging.h"
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080031#include "sync-socket.h"
Yingdi Yu280bb962014-01-30 09:52:43 -080032#include <ndn-cpp-dev/security/validator-null.hpp>
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070033
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070034extern "C" {
35#include <unistd.h>
36}
37
38using namespace Sync;
39using namespace std;
40using namespace boost;
41
Alexander Afanasyev7df73332012-03-15 12:31:49 -070042INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070043
Zhenkai Zhua30e1782012-06-01 11:52:57 -070044#define PRINT
45//std::cout << "Line: " << __LINE__ << std::endl;
46
Zhenkai Zhu00304002012-03-12 21:32:30 -070047class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070048public:
Alexander Afanasyev531803b2014-02-05 15:57:35 -080049 TestSocketApp()
50 : sum(0)
51 {}
52
Yingdi Yu280bb962014-01-30 09:52:43 -080053 map<ndn::Name, string> data;
54 void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070055 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu280bb962014-01-30 09:52:43 -080056 ndn::Name dataName(dataPacket->getName());
57 string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), dataPacket->getContent().value_size());
58 data.insert(make_pair(dataName, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070059 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070060 }
Yingdi Yu43e71612013-10-30 22:19:31 -070061
Yingdi Yu280bb962014-01-30 09:52:43 -080062 void set(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070063 string str2(buf, len);
Yingdi Yu280bb962014-01-30 09:52:43 -080064 data.insert(make_pair(name, str2));
Yingdi Yu43e71612013-10-30 22:19:31 -070065 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070066
Yingdi Yu280bb962014-01-30 09:52:43 -080067 void setNum(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
68 int n = dataPacket->getContent().value_size() / 4;
Alexander Afanasyev531803b2014-02-05 15:57:35 -080069 uint32_t *numbers = new uint32_t [n];
Yingdi Yu280bb962014-01-30 09:52:43 -080070 memcpy(numbers, dataPacket->getContent().value(), dataPacket->getContent().value_size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070071 for (int i = 0; i < n; i++) {
72 sum += numbers[i];
73 }
74 delete numbers;
75
76 }
77
Yingdi Yu280bb962014-01-30 09:52:43 -080078 void setNum(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070079 int n = len / 4;
80 int *numbers = new int [n];
81 memcpy(numbers, buf, len);
82 for (int i = 0; i < n; i++) {
83 sum += numbers[i];
84 }
85 delete numbers;
86 }
87
Alexander Afanasyev531803b2014-02-05 15:57:35 -080088 uint32_t sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070089
Yingdi Yu43e71612013-10-30 22:19:31 -070090 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070091 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070092
93 PRINT
94
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070095 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070096 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070097 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070098 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070099 }
100 }
101 }
102
Yingdi Yu43e71612013-10-30 22:19:31 -0700103 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700104 int n = v.size();
105
106 PRINT
107
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800108 // std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700109 for (int i = 0; i < n; i++) {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800110 // std::cout << "In fetchNumbers. v[i].low is (" <<v[i].low.getSession() <<", " << v[i].low.getSeq() << ") v[i].high is ("<<v[i].high.getSession() <<", " <<v[i].high.getSeq()<<")" << std::endl;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700111 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
112 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700113 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700114 }
115 }
116 }
117
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700118 void pass(const string &prefix) {
119 }
120
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700121 string toString(){
Yingdi Yu280bb962014-01-30 09:52:43 -0800122 map<ndn::Name, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700123 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700124 for (; it != data.end(); ++it){
125 str += "<";
Yingdi Yu280bb962014-01-30 09:52:43 -0800126 str += it->first.toUri();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700127 str += "|";
128 str += it->second;
129 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700130 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700131 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700132
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700133 return str;
134 }
135
136};
137
Yingdi Yu280bb962014-01-30 09:52:43 -0800138class TestSet1{
139public:
140 TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService)
141 : m_syncPrefix("/let/us/sync")
142 , m_validator(new ndn::ValidatorNull())
143 , m_face1(new ndn::Face(ioService))
144 , m_face2(new ndn::Face(ioService))
145 , m_face3(new ndn::Face(ioService))
146 , m_p1("/irl.cs.ucla.edu")
147 , m_p2("/yakshi.org")
148 , m_p3("/google.com")
149 {}
150
151 void
152 createSyncSocket1()
153 {
154 _LOG_DEBUG ("s1");
155 m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1,
156 bind(&TestSocketApp::fetchAll, &m_a1, _1, _2),
157 bind(&TestSocketApp::pass, &m_a1, _1));
158 }
159
160 void
161 createSyncSocket2()
162 {
163 _LOG_DEBUG ("s2");
164 m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2,
165 bind(&TestSocketApp::fetchAll, &m_a2, _1, _2),
166 bind(&TestSocketApp::pass, &m_a2, _1));
167 }
168
169 void
170 createSyncSocket3()
171 {
172 _LOG_DEBUG ("s3");
173 m_s3 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face3,
174 bind(&TestSocketApp::fetchAll, &m_a3, _1, _2),
175 bind(&TestSocketApp::pass, &m_a3, _1));
176 }
177
178 void
179 publishSocket1(uint32_t session, string data)
180 {
181 _LOG_DEBUG ("s1 publish");
182 m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000);
183 }
184
185 void
186 publishSocket2(uint32_t session, string data)
187 {
188 _LOG_DEBUG ("s2 publish");
189 m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000);
190 }
191
192 void
193 publishSocket3(uint32_t session, string data)
194 {
195 _LOG_DEBUG ("s3 publish");
196 m_s3->publishData (m_p3, session, data.c_str(), data.size(), 1000);
197 }
198
199 void
200 setSocket1(string suffix, string data)
201 {
202 _LOG_DEBUG ("a1 set");
203 ndn::Name name = m_p1;
204 name.append(suffix);
205 m_a1.set (name, data.c_str(), data.size());
206 }
207
208 void
209 setSocket2(string suffix, string data)
210 {
211 _LOG_DEBUG ("a2 set");
212 ndn::Name name = m_p2;
213 name.append(suffix);
214 m_a2.set (name, data.c_str(), data.size());
215 }
216
217 void
218 setSocket3(string suffix, string data)
219 {
220 _LOG_DEBUG ("a3 set");
221 ndn::Name name = m_p3;
222 name.append(suffix);
223 m_a3.set (name, data.c_str(), data.size());
224 }
225
226 void
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800227 check(int round)
Yingdi Yu280bb962014-01-30 09:52:43 -0800228 {
229 BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString());
230 BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString());
231 }
232
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800233 void
234 done()
235 {
236 m_s1.reset();
237 m_s2.reset();
238 m_s3.reset();
239 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800240
241
242 TestSocketApp m_a1, m_a2, m_a3;
243 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
244 ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3;
245 ndn::Name m_p1, m_p2, m_p3;
246 ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3;
247 ndn::Name m_syncPrefix;
248};
249
250class TestSet2{
251public:
252 TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService)
253 : m_syncPrefix("/this/is/the/prefix")
254 , m_validator(new ndn::ValidatorNull())
255 , m_face1(new ndn::Face(ioService))
256 , m_face2(new ndn::Face(ioService))
257 , m_p1("/xiaonei.com")
258 , m_p2("/mitbbs.com")
259 {}
260
261 void
262 createSyncSocket1()
263 {
264 _LOG_DEBUG ("s1");
265 m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1,
266 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
267 bind(&TestSocketApp::pass, &m_a1, _1));
268 }
269
270 void
271 createSyncSocket2()
272 {
273 _LOG_DEBUG ("s2");
274 m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2,
275 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
276 bind(&TestSocketApp::pass, &m_a2, _1));
277 }
278
279 void
280 publishSocket1(uint32_t session, string data)
281 {
282 _LOG_DEBUG ("s1 publish");
283 m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000);
284 }
285
286 void
287 publishSocket2(uint32_t session, string data)
288 {
289 _LOG_DEBUG ("s2 publish");
290 m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000);
291 }
292
293 void
294 setSocket1(const char* ptr, size_t size)
295 {
296 _LOG_DEBUG ("a1 setNum");
297 m_a1.setNum (m_p1, ptr, size);
298 }
299
300 void
301 setSocket2(const char* ptr, size_t size)
302 {
303 _LOG_DEBUG ("a2 setNum");
304 m_a2.setNum (m_p2, ptr, size);
305 }
306
307 void
308 check(int num)
309 {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800310 _LOG_DEBUG ("codnum " << num);
311 _LOG_DEBUG ("a1 sum " << m_a1.sum);
312 _LOG_DEBUG ("a2 sum " << m_a2.sum);
313
Yingdi Yu280bb962014-01-30 09:52:43 -0800314 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
315 }
316
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800317 void
318 done()
319 {
320 m_s1.reset();
321 m_s2.reset();
322 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800323
324 TestSocketApp m_a1, m_a2;
325 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
326 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
327 ndn::Name m_p1, m_p2;
328 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
329 ndn::Name m_syncPrefix;
330};
331
332BOOST_AUTO_TEST_CASE (AppSocketTest1)
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700333{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700334 INIT_LOGGERS ();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700335
Yingdi Yu280bb962014-01-30 09:52:43 -0800336 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
337 ndn::Scheduler scheduler(*ioService);
338 TestSet1 testSet1(ioService);
Yingdi Yu43e71612013-10-30 22:19:31 -0700339
Yingdi Yu280bb962014-01-30 09:52:43 -0800340 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet1::createSyncSocket1, &testSet1));
341 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet1::createSyncSocket2, &testSet1));
342 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet1::createSyncSocket3, &testSet1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700343 string data0 = "Very funny Scotty, now beam down my clothes";
Yingdi Yu280bb962014-01-30 09:52:43 -0800344 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data0));
345 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/0", data0));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800346 scheduler.scheduleEvent(ndn::time::seconds(1.16), ndn::bind(&TestSet1::check, &testSet1, 1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800347 string data1 = "Yes, give me that ketchup";
348 string data2 = "Don't look conspicuous, it draws fire";
349 scheduler.scheduleEvent(ndn::time::seconds(1.17), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data1));
350 scheduler.scheduleEvent(ndn::time::seconds(1.18), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data2));
351 scheduler.scheduleEvent(ndn::time::seconds(2.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data1));
352 scheduler.scheduleEvent(ndn::time::seconds(2.16), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data2));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800353 scheduler.scheduleEvent(ndn::time::seconds(2.17), ndn::bind(&TestSet1::check, &testSet1, 2));
Yingdi Yu280bb962014-01-30 09:52:43 -0800354 string data3 = "You surf the Internet, I surf the real world";
355 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
356 string data5 = "Real men wear pink. Why? Because their wives make them";
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800357 scheduler.scheduleEvent(ndn::time::seconds(3.18), ndn::bind(&TestSet1::publishSocket3, &testSet1, 0, data3));
358 scheduler.scheduleEvent(ndn::time::seconds(3.20), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data4));
359 scheduler.scheduleEvent(ndn::time::seconds(3.21), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data5));
360 scheduler.scheduleEvent(ndn::time::seconds(4.21), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/0", data3));
361 scheduler.scheduleEvent(ndn::time::seconds(4.22), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/0", data4));
362 scheduler.scheduleEvent(ndn::time::seconds(4.23), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/1", data5));
363 scheduler.scheduleEvent(ndn::time::seconds(4.30), ndn::bind(&TestSet1::check, &testSet1, 3));
Yingdi Yu280bb962014-01-30 09:52:43 -0800364 // not sure weither this is simultanous data generation from multiple sources
365 _LOG_DEBUG ("Simultaneous publishing");
366 string data6 = "Shakespeare says: 'Prose before hos.'";
367 string data7 = "Pick good people, talent never wears out";
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800368 scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data6));
369 scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data7));
370 scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data6));
371 scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data7));
372 scheduler.scheduleEvent(ndn::time::seconds(6.90), ndn::bind(&TestSet1::check, &testSet1, 4));
373 scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet1::done, &testSet1));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700374
Yingdi Yu280bb962014-01-30 09:52:43 -0800375 ioService->run();
376}
Zhenkai Zhu00304002012-03-12 21:32:30 -0700377
Yingdi Yu280bb962014-01-30 09:52:43 -0800378BOOST_AUTO_TEST_CASE (AppSocketTest2)
379{
380 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
381 ndn::Scheduler scheduler(*ioService);
382 TestSet2 testSet2(ioService);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700383
Yingdi Yu280bb962014-01-30 09:52:43 -0800384 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet2::createSyncSocket1, &testSet2));
385 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet2::createSyncSocket2, &testSet2));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800386 uint32_t num[5] = {0, 1, 2, 3, 4};
Yingdi Yu280bb962014-01-30 09:52:43 -0800387 string data0((const char *) num, sizeof(num));
388 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet2::publishSocket1, &testSet2, 0, data0));
389 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num)));
390 scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet2::check, &testSet2, 10));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800391 uint32_t newNum[5] = {9, 7, 2, 1, 1};
Yingdi Yu280bb962014-01-30 09:52:43 -0800392 string data1((const char *) newNum, sizeof(newNum));
393 scheduler.scheduleEvent(ndn::time::seconds(1.10), ndn::bind(&TestSet2::publishSocket2, &testSet2, 0, data1));
394 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum)));
395 scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet2::check, &testSet2, 30));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800396 scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet2::done, &testSet2));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700397
Yingdi Yu280bb962014-01-30 09:52:43 -0800398 ioService->run();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700399}