blob: cc6e56baf8229a843183ed4a05b50ffbb24d51c7 [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:
Yingdi Yu280bb962014-01-30 09:52:43 -080049 map<ndn::Name, string> data;
50 void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070051 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu280bb962014-01-30 09:52:43 -080052 ndn::Name dataName(dataPacket->getName());
53 string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), dataPacket->getContent().value_size());
54 data.insert(make_pair(dataName, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070055 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070056 }
Yingdi Yu43e71612013-10-30 22:19:31 -070057
Yingdi Yu280bb962014-01-30 09:52:43 -080058 void set(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070059 string str2(buf, len);
Yingdi Yu280bb962014-01-30 09:52:43 -080060 data.insert(make_pair(name, str2));
Yingdi Yu43e71612013-10-30 22:19:31 -070061 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070062
Yingdi Yu280bb962014-01-30 09:52:43 -080063 void setNum(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
64 int n = dataPacket->getContent().value_size() / 4;
Zhenkai Zhudc70a292012-06-01 14:00:59 -070065 int *numbers = new int [n];
Yingdi Yu280bb962014-01-30 09:52:43 -080066 memcpy(numbers, dataPacket->getContent().value(), dataPacket->getContent().value_size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070067 for (int i = 0; i < n; i++) {
68 sum += numbers[i];
69 }
70 delete numbers;
71
72 }
73
Yingdi Yu280bb962014-01-30 09:52:43 -080074 void setNum(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070075 int n = len / 4;
76 int *numbers = new int [n];
77 memcpy(numbers, buf, len);
78 for (int i = 0; i < n; i++) {
79 sum += numbers[i];
80 }
81 delete numbers;
82 }
83
Zhenkai Zhudc70a292012-06-01 14:00:59 -070084 int sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070085
Yingdi Yu43e71612013-10-30 22:19:31 -070086 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070087 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070088
89 PRINT
90
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070091 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070092 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070093 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070094 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070095 }
96 }
97 }
98
Yingdi Yu43e71612013-10-30 22:19:31 -070099 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700100 int n = v.size();
101
102 PRINT
103
104 std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
105 for (int i = 0; i < n; i++) {
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700106 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 -0700107 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
108 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700109 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700110 }
111 }
112 }
113
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700114 void pass(const string &prefix) {
115 }
116
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700117 string toString(){
Yingdi Yu280bb962014-01-30 09:52:43 -0800118 map<ndn::Name, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700119 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700120 for (; it != data.end(); ++it){
121 str += "<";
Yingdi Yu280bb962014-01-30 09:52:43 -0800122 str += it->first.toUri();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700123 str += "|";
124 str += it->second;
125 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700126 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700127 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700128
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700129 return str;
130 }
131
132};
133
Yingdi Yu280bb962014-01-30 09:52:43 -0800134class TestSet1{
135public:
136 TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService)
137 : m_syncPrefix("/let/us/sync")
138 , m_validator(new ndn::ValidatorNull())
139 , m_face1(new ndn::Face(ioService))
140 , m_face2(new ndn::Face(ioService))
141 , m_face3(new ndn::Face(ioService))
142 , m_p1("/irl.cs.ucla.edu")
143 , m_p2("/yakshi.org")
144 , m_p3("/google.com")
145 {}
146
147 void
148 createSyncSocket1()
149 {
150 _LOG_DEBUG ("s1");
151 m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1,
152 bind(&TestSocketApp::fetchAll, &m_a1, _1, _2),
153 bind(&TestSocketApp::pass, &m_a1, _1));
154 }
155
156 void
157 createSyncSocket2()
158 {
159 _LOG_DEBUG ("s2");
160 m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2,
161 bind(&TestSocketApp::fetchAll, &m_a2, _1, _2),
162 bind(&TestSocketApp::pass, &m_a2, _1));
163 }
164
165 void
166 createSyncSocket3()
167 {
168 _LOG_DEBUG ("s3");
169 m_s3 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face3,
170 bind(&TestSocketApp::fetchAll, &m_a3, _1, _2),
171 bind(&TestSocketApp::pass, &m_a3, _1));
172 }
173
174 void
175 publishSocket1(uint32_t session, string data)
176 {
177 _LOG_DEBUG ("s1 publish");
178 m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000);
179 }
180
181 void
182 publishSocket2(uint32_t session, string data)
183 {
184 _LOG_DEBUG ("s2 publish");
185 m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000);
186 }
187
188 void
189 publishSocket3(uint32_t session, string data)
190 {
191 _LOG_DEBUG ("s3 publish");
192 m_s3->publishData (m_p3, session, data.c_str(), data.size(), 1000);
193 }
194
195 void
196 setSocket1(string suffix, string data)
197 {
198 _LOG_DEBUG ("a1 set");
199 ndn::Name name = m_p1;
200 name.append(suffix);
201 m_a1.set (name, data.c_str(), data.size());
202 }
203
204 void
205 setSocket2(string suffix, string data)
206 {
207 _LOG_DEBUG ("a2 set");
208 ndn::Name name = m_p2;
209 name.append(suffix);
210 m_a2.set (name, data.c_str(), data.size());
211 }
212
213 void
214 setSocket3(string suffix, string data)
215 {
216 _LOG_DEBUG ("a3 set");
217 ndn::Name name = m_p3;
218 name.append(suffix);
219 m_a3.set (name, data.c_str(), data.size());
220 }
221
222 void
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800223 check(int round)
Yingdi Yu280bb962014-01-30 09:52:43 -0800224 {
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800225 cout << "++++++++++++++++++ " << round << " " << m_a1.toString() << endl;
Yingdi Yu280bb962014-01-30 09:52:43 -0800226 BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString());
227 BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString());
228 }
229
230
231
232 TestSocketApp m_a1, m_a2, m_a3;
233 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
234 ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3;
235 ndn::Name m_p1, m_p2, m_p3;
236 ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3;
237 ndn::Name m_syncPrefix;
238};
239
240class TestSet2{
241public:
242 TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService)
243 : m_syncPrefix("/this/is/the/prefix")
244 , m_validator(new ndn::ValidatorNull())
245 , m_face1(new ndn::Face(ioService))
246 , m_face2(new ndn::Face(ioService))
247 , m_p1("/xiaonei.com")
248 , m_p2("/mitbbs.com")
249 {}
250
251 void
252 createSyncSocket1()
253 {
254 _LOG_DEBUG ("s1");
255 m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1,
256 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
257 bind(&TestSocketApp::pass, &m_a1, _1));
258 }
259
260 void
261 createSyncSocket2()
262 {
263 _LOG_DEBUG ("s2");
264 m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2,
265 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
266 bind(&TestSocketApp::pass, &m_a2, _1));
267 }
268
269 void
270 publishSocket1(uint32_t session, string data)
271 {
272 _LOG_DEBUG ("s1 publish");
273 m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000);
274 }
275
276 void
277 publishSocket2(uint32_t session, string data)
278 {
279 _LOG_DEBUG ("s2 publish");
280 m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000);
281 }
282
283 void
284 setSocket1(const char* ptr, size_t size)
285 {
286 _LOG_DEBUG ("a1 setNum");
287 m_a1.setNum (m_p1, ptr, size);
288 }
289
290 void
291 setSocket2(const char* ptr, size_t size)
292 {
293 _LOG_DEBUG ("a2 setNum");
294 m_a2.setNum (m_p2, ptr, size);
295 }
296
297 void
298 check(int num)
299 {
300 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
301 }
302
303
304
305 TestSocketApp m_a1, m_a2;
306 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
307 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
308 ndn::Name m_p1, m_p2;
309 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
310 ndn::Name m_syncPrefix;
311};
312
313BOOST_AUTO_TEST_CASE (AppSocketTest1)
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700314{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700315 INIT_LOGGERS ();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700316
Yingdi Yu280bb962014-01-30 09:52:43 -0800317 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
318 ndn::Scheduler scheduler(*ioService);
319 TestSet1 testSet1(ioService);
Yingdi Yu43e71612013-10-30 22:19:31 -0700320
Yingdi Yu280bb962014-01-30 09:52:43 -0800321 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet1::createSyncSocket1, &testSet1));
322 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet1::createSyncSocket2, &testSet1));
323 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet1::createSyncSocket3, &testSet1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700324 string data0 = "Very funny Scotty, now beam down my clothes";
Yingdi Yu280bb962014-01-30 09:52:43 -0800325 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data0));
326 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/0", data0));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800327 scheduler.scheduleEvent(ndn::time::seconds(1.16), ndn::bind(&TestSet1::check, &testSet1, 1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800328 string data1 = "Yes, give me that ketchup";
329 string data2 = "Don't look conspicuous, it draws fire";
330 scheduler.scheduleEvent(ndn::time::seconds(1.17), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data1));
331 scheduler.scheduleEvent(ndn::time::seconds(1.18), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data2));
332 scheduler.scheduleEvent(ndn::time::seconds(2.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data1));
333 scheduler.scheduleEvent(ndn::time::seconds(2.16), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data2));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800334 scheduler.scheduleEvent(ndn::time::seconds(2.17), ndn::bind(&TestSet1::check, &testSet1, 2));
Yingdi Yu280bb962014-01-30 09:52:43 -0800335 string data3 = "You surf the Internet, I surf the real world";
336 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
337 string data5 = "Real men wear pink. Why? Because their wives make them";
338 scheduler.scheduleEvent(ndn::time::seconds(2.18), ndn::bind(&TestSet1::publishSocket3, &testSet1, 0, data3));
339 scheduler.scheduleEvent(ndn::time::seconds(2.20), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data4));
340 scheduler.scheduleEvent(ndn::time::seconds(2.21), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data5));
341 scheduler.scheduleEvent(ndn::time::seconds(3.21), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/0", data3));
342 scheduler.scheduleEvent(ndn::time::seconds(3.22), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/0", data4));
343 scheduler.scheduleEvent(ndn::time::seconds(3.23), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/1", data5));
344 // not sure weither this is simultanous data generation from multiple sources
345 _LOG_DEBUG ("Simultaneous publishing");
346 string data6 = "Shakespeare says: 'Prose before hos.'";
347 string data7 = "Pick good people, talent never wears out";
348 scheduler.scheduleEvent(ndn::time::seconds(3.30), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data6));
349 scheduler.scheduleEvent(ndn::time::seconds(3.30), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data7));
350 scheduler.scheduleEvent(ndn::time::seconds(4.80), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data6));
351 scheduler.scheduleEvent(ndn::time::seconds(4.80), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data7));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800352 scheduler.scheduleEvent(ndn::time::seconds(4.90), ndn::bind(&TestSet1::check, &testSet1, 3));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700353
Yingdi Yu280bb962014-01-30 09:52:43 -0800354 ioService->run();
355}
Zhenkai Zhu00304002012-03-12 21:32:30 -0700356
Yingdi Yu280bb962014-01-30 09:52:43 -0800357BOOST_AUTO_TEST_CASE (AppSocketTest2)
358{
359 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
360 ndn::Scheduler scheduler(*ioService);
361 TestSet2 testSet2(ioService);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700362
Yingdi Yu280bb962014-01-30 09:52:43 -0800363 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet2::createSyncSocket1, &testSet2));
364 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet2::createSyncSocket2, &testSet2));
365 int num[5] = {0, 1, 2, 3, 4};
366 string data0((const char *) num, sizeof(num));
367 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet2::publishSocket1, &testSet2, 0, data0));
368 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num)));
369 scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet2::check, &testSet2, 10));
370 int newNum[5] = {9, 7, 2, 1, 1};
371 string data1((const char *) newNum, sizeof(newNum));
372 scheduler.scheduleEvent(ndn::time::seconds(1.10), ndn::bind(&TestSet2::publishSocket2, &testSet2, 0, data1));
373 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum)));
374 scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet2::check, &testSet2, 30));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700375
Yingdi Yu280bb962014-01-30 09:52:43 -0800376 ioService->run();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700377}