blob: e25ef17932ba7450f828b803e79659675f13e967 [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 Yu0eee6002014-02-11 15:54:17 -080032#include "sync-validator.h"
Yingdi Yu280bb962014-01-30 09:52:43 -080033#include <ndn-cpp-dev/security/validator-null.hpp>
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070034
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070035extern "C" {
36#include <unistd.h>
37}
38
39using namespace Sync;
40using namespace std;
41using namespace boost;
42
Alexander Afanasyev7df73332012-03-15 12:31:49 -070043INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070044
Zhenkai Zhua30e1782012-06-01 11:52:57 -070045#define PRINT
Yingdi Yud4ef5d32014-03-01 02:02:01 -080046// std::cout << "Line: " << __LINE__ << std::endl;
Zhenkai Zhua30e1782012-06-01 11:52:57 -070047
Zhenkai Zhu00304002012-03-12 21:32:30 -070048class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070049public:
Alexander Afanasyev531803b2014-02-05 15:57:35 -080050 TestSocketApp()
51 : sum(0)
52 {}
53
Yingdi Yu280bb962014-01-30 09:52:43 -080054 map<ndn::Name, string> data;
55 void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070056 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu280bb962014-01-30 09:52:43 -080057 ndn::Name dataName(dataPacket->getName());
58 string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), dataPacket->getContent().value_size());
59 data.insert(make_pair(dataName, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070060 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070061 }
Yingdi Yu43e71612013-10-30 22:19:31 -070062
Yingdi Yu280bb962014-01-30 09:52:43 -080063 void set(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070064 string str2(buf, len);
Yingdi Yu280bb962014-01-30 09:52:43 -080065 data.insert(make_pair(name, str2));
Yingdi Yu43e71612013-10-30 22:19:31 -070066 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070067
Yingdi Yud4ef5d32014-03-01 02:02:01 -080068 void setNum(const ndn::shared_ptr<const ndn::Data>& data) {
69 int n = data->getContent().value_size() / 4;
Alexander Afanasyev531803b2014-02-05 15:57:35 -080070 uint32_t *numbers = new uint32_t [n];
Yingdi Yud4ef5d32014-03-01 02:02:01 -080071 memcpy(numbers, data->getContent().value(), data->getContent().value_size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070072 for (int i = 0; i < n; i++) {
73 sum += numbers[i];
74 }
75 delete numbers;
76
77 }
78
Yingdi Yu280bb962014-01-30 09:52:43 -080079 void setNum(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070080 int n = len / 4;
81 int *numbers = new int [n];
82 memcpy(numbers, buf, len);
83 for (int i = 0; i < n; i++) {
84 sum += numbers[i];
85 }
86 delete numbers;
87 }
88
Alexander Afanasyev531803b2014-02-05 15:57:35 -080089 uint32_t sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070090
Yingdi Yu43e71612013-10-30 22:19:31 -070091 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070092 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070093
94 PRINT
95
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070096 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070097 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070098 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070099 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700100 }
101 }
102 }
103
Yingdi Yu43e71612013-10-30 22:19:31 -0700104 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700105 int n = v.size();
106
107 PRINT
108
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800109 // std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700110 for (int i = 0; i < n; i++) {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800111 // 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 -0700112 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
113 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700114 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700115 }
116 }
117 }
118
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700119 void pass(const string &prefix) {
120 }
121
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700122 string toString(){
Yingdi Yu280bb962014-01-30 09:52:43 -0800123 map<ndn::Name, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700124 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700125 for (; it != data.end(); ++it){
126 str += "<";
Yingdi Yu280bb962014-01-30 09:52:43 -0800127 str += it->first.toUri();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700128 str += "|";
129 str += it->second;
130 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700131 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700132 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700133
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700134 return str;
135 }
136
137};
138
Yingdi Yu280bb962014-01-30 09:52:43 -0800139class TestSet1{
140public:
141 TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800142 : m_face1(new ndn::Face(ioService))
Yingdi Yu280bb962014-01-30 09:52:43 -0800143 , m_face2(new ndn::Face(ioService))
144 , m_face3(new ndn::Face(ioService))
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800145 , m_name1("/irl.cs.ucla.edu/" + boost::lexical_cast<std::string>(ndn::time::now()))
146 , m_name2("/yakshi.org/" + boost::lexical_cast<std::string>(ndn::time::now()))
147 , m_name3("/google.com/" + boost::lexical_cast<std::string>(ndn::time::now()))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800148 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800149 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
150 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
151 m_id3 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name3));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800152
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800153 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
154 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
155 "==", "\\1", "\\1", true);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800156 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800157
158 void
159 createSyncSocket1()
160 {
161 _LOG_DEBUG ("s1");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800162
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800163 m_s1 = ndn::shared_ptr<SyncSocket>
164 (new SyncSocket("/let/us/sync",
165 "/irl.cs.ucla.edu",
166 0,
167 false,
168 "/",
169 m_face1,
170 *m_id1,
171 m_rule,
172 bind(&TestSocketApp::fetchAll, &m_a1, _1, _2),
173 bind(&TestSocketApp::pass, &m_a1, _1)));
174
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800175 m_s1->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800176 }
177
178 void
179 createSyncSocket2()
180 {
181 _LOG_DEBUG ("s2");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800182
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800183 m_s2 = ndn::shared_ptr<SyncSocket>
184 (new SyncSocket("/let/us/sync",
185 "/yakshi.org",
186 0,
187 false,
188 "/",
189 m_face2,
190 *m_id2,
191 m_rule,
192 bind(&TestSocketApp::fetchAll, &m_a2, _1, _2),
193 bind(&TestSocketApp::pass, &m_a2, _1)));
194
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800195 m_s2->addParticipant(*m_id1);
196 m_s2->addParticipant(*m_id3);
Yingdi Yu280bb962014-01-30 09:52:43 -0800197 }
198
199 void
200 createSyncSocket3()
201 {
202 _LOG_DEBUG ("s3");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800203
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800204 m_s3 = ndn::shared_ptr<SyncSocket>
205 (new SyncSocket("/let/us/sync",
206 "/google.com",
207 0,
208 false,
209 "/",
210 m_face3,
211 *m_id3,
212 m_rule,
213 bind(&TestSocketApp::fetchAll, &m_a3, _1, _2),
214 bind(&TestSocketApp::pass, &m_a3, _1)));
215
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800216 m_s3->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800217 }
218
219 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800220 publishSocket1(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800221 {
222 _LOG_DEBUG ("s1 publish");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800223 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800224 }
225
226 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800227 publishSocket2(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800228 {
229 _LOG_DEBUG ("s2 publish");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800230 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800231 }
232
233 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800234 publishSocket3(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800235 {
236 _LOG_DEBUG ("s3 publish");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800237 m_s3->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800238 }
239
240 void
241 setSocket1(string suffix, string data)
242 {
243 _LOG_DEBUG ("a1 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800244 ndn::Name name("/irl.cs.ucla.edu");
Yingdi Yu280bb962014-01-30 09:52:43 -0800245 name.append(suffix);
246 m_a1.set (name, data.c_str(), data.size());
247 }
248
249 void
250 setSocket2(string suffix, string data)
251 {
252 _LOG_DEBUG ("a2 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800253 ndn::Name name("/yakshi.org");
Yingdi Yu280bb962014-01-30 09:52:43 -0800254 name.append(suffix);
255 m_a2.set (name, data.c_str(), data.size());
256 }
257
258 void
259 setSocket3(string suffix, string data)
260 {
261 _LOG_DEBUG ("a3 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800262 ndn::Name name("/google.com");
Yingdi Yu280bb962014-01-30 09:52:43 -0800263 name.append(suffix);
264 m_a3.set (name, data.c_str(), data.size());
265 }
266
267 void
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800268 check(int round)
Yingdi Yu280bb962014-01-30 09:52:43 -0800269 {
270 BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString());
271 BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString());
272 }
273
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800274 void
275 done()
276 {
277 m_s1.reset();
278 m_s2.reset();
279 m_s3.reset();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800280
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800281 m_keyChain.deleteIdentity(m_name1);
282 m_keyChain.deleteIdentity(m_name2);
283 m_keyChain.deleteIdentity(m_name3);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800284
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800285 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800286
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800287 ndn::KeyChain m_keyChain;
288 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
Yingdi Yu280bb962014-01-30 09:52:43 -0800289
Yingdi Yu280bb962014-01-30 09:52:43 -0800290 ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800291 ndn::Name m_name1, m_name2, m_name3;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800292 TestSocketApp m_a1, m_a2, m_a3;
293 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2, m_id3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800294 ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800295};
296
297class TestSet2{
298public:
299 TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800300 : m_face1(new ndn::Face(ioService))
Yingdi Yu280bb962014-01-30 09:52:43 -0800301 , m_face2(new ndn::Face(ioService))
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800302 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::now()))
303 , m_name2("/mitbbs.com/" + boost::lexical_cast<std::string>(ndn::time::now()))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800304 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800305 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
306 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800307
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800308 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
309 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
310 "==", "\\1", "\\1", true);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800311 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800312
313 void
314 createSyncSocket1()
315 {
316 _LOG_DEBUG ("s1");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800317
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800318 m_s1 = ndn::shared_ptr<SyncSocket>
319 (new SyncSocket("/this/is/the/prefix",
320 "/xiaonei.com",
321 0,
322 false,
323 "/",
324 m_face1,
325 *m_id1,
326 m_rule,
327 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
328 bind(&TestSocketApp::pass, &m_a1, _1)));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800329
330 m_s1->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800331 }
332
333 void
334 createSyncSocket2()
335 {
336 _LOG_DEBUG ("s2");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800337
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800338 m_s2 = ndn::shared_ptr<SyncSocket>
339 (new SyncSocket("/this/is/the/prefix",
340 "/mitbbs.com",
341 0,
342 false,
343 "/",
344 m_face2,
345 *m_id2,
346 m_rule,
347 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
348 bind(&TestSocketApp::pass, &m_a2, _1)));
349
350 m_s2->addParticipant(*m_id1);
351 }
352
353 void
354 publishSocket1(string data)
355 {
356 _LOG_DEBUG ("s1 publish");
357 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
358 }
359
360 void
361 publishSocket2(string data)
362 {
363 _LOG_DEBUG ("s2 publish");
364 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
365 }
366
367 void
368 setSocket1(const char* ptr, size_t size)
369 {
370 _LOG_DEBUG ("a1 setNum");
371 m_a1.setNum ("/xiaonei.com", ptr, size);
372 }
373
374 void
375 setSocket2(const char* ptr, size_t size)
376 {
377 _LOG_DEBUG ("a2 setNum");
378 m_a2.setNum ("/mitbbs.com", ptr, size);
379 }
380
381 void
382 check(int num)
383 {
384 _LOG_DEBUG ("codnum " << num);
385 _LOG_DEBUG ("a1 sum " << m_a1.sum);
386 _LOG_DEBUG ("a2 sum " << m_a2.sum);
387
388 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
389 }
390
391 void
392 done()
393 {
394 m_s1.reset();
395 m_s2.reset();
396
397 m_keyChain.deleteIdentity(m_name1);
398 m_keyChain.deleteIdentity(m_name2);
399 }
400
401 ndn::KeyChain m_keyChain;
402 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
403
404 TestSocketApp m_a1, m_a2;
405 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2;
406 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
407 ndn::Name m_name1, m_name2;
408 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
409};
410
411
412
413class TestSet3{
414public:
415 TestSet3(ndn::shared_ptr<boost::asio::io_service> ioService)
416 : m_face1(new ndn::Face(ioService))
417 , m_face2(new ndn::Face(ioService))
418 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::now()))
419 , m_name2("/mitbbs.com/" + boost::lexical_cast<std::string>(ndn::time::now()))
420 {
421 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
422 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
423
424 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
425 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
426 "==", "\\1", "\\1", true);
427 }
428
429 void
430 createSyncSocket1()
431 {
432 _LOG_DEBUG ("s1");
433
434 m_s1 = ndn::shared_ptr<SyncSocket>
435 (new SyncSocket("/this/is/the/prefix",
436 "/xiaonei.com",
437 1,
438 true,
439 "/abc",
440 m_face1,
441 *m_id1,
442 m_rule,
443 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
444 bind(&TestSocketApp::pass, &m_a1, _1)));
445
446 m_s1->addParticipant(*m_id2);
447 }
448
449 void
450 createSyncSocket2()
451 {
452 _LOG_DEBUG ("s2");
453
454 m_s2 = ndn::shared_ptr<SyncSocket>
455 (new SyncSocket("/this/is/the/prefix",
456 "/mitbbs.com",
457 1,
458 false,
459 "/",
460 m_face2,
461 *m_id2,
462 m_rule,
463 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
464 bind(&TestSocketApp::pass, &m_a2, _1)));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800465
466 m_s2->addParticipant(*m_id1);
Yingdi Yu280bb962014-01-30 09:52:43 -0800467 }
468
469 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800470 publishSocket1(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800471 {
472 _LOG_DEBUG ("s1 publish");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800473 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800474 }
475
476 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800477 publishSocket2(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800478 {
479 _LOG_DEBUG ("s2 publish");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800480 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800481 }
482
483 void
484 setSocket1(const char* ptr, size_t size)
485 {
486 _LOG_DEBUG ("a1 setNum");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800487 m_a1.setNum ("/xiaonei.com", ptr, size);
Yingdi Yu280bb962014-01-30 09:52:43 -0800488 }
489
490 void
491 setSocket2(const char* ptr, size_t size)
492 {
493 _LOG_DEBUG ("a2 setNum");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800494 m_a2.setNum ("/mitbbs.com", ptr, size);
Yingdi Yu280bb962014-01-30 09:52:43 -0800495 }
496
497 void
498 check(int num)
499 {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800500 _LOG_DEBUG ("codnum " << num);
501 _LOG_DEBUG ("a1 sum " << m_a1.sum);
502 _LOG_DEBUG ("a2 sum " << m_a2.sum);
503
Yingdi Yu280bb962014-01-30 09:52:43 -0800504 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
505 }
506
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800507 void
508 done()
509 {
510 m_s1.reset();
511 m_s2.reset();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800512
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800513 m_keyChain.deleteIdentity(m_name1);
514 m_keyChain.deleteIdentity(m_name2);
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800515 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800516
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800517 ndn::KeyChain m_keyChain;
518 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
519
Yingdi Yu280bb962014-01-30 09:52:43 -0800520 TestSocketApp m_a1, m_a2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800521 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800522 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800523 ndn::Name m_name1, m_name2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800524 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800525};
526
527BOOST_AUTO_TEST_CASE (AppSocketTest1)
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700528{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700529 INIT_LOGGERS ();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700530
Yingdi Yu280bb962014-01-30 09:52:43 -0800531 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
532 ndn::Scheduler scheduler(*ioService);
533 TestSet1 testSet1(ioService);
Yingdi Yu43e71612013-10-30 22:19:31 -0700534
Yingdi Yu280bb962014-01-30 09:52:43 -0800535 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet1::createSyncSocket1, &testSet1));
536 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet1::createSyncSocket2, &testSet1));
537 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet1::createSyncSocket3, &testSet1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700538 string data0 = "Very funny Scotty, now beam down my clothes";
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800539 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet1::publishSocket1, &testSet1, data0));
540 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data0));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800541 scheduler.scheduleEvent(ndn::time::seconds(1.16), ndn::bind(&TestSet1::check, &testSet1, 1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800542 string data1 = "Yes, give me that ketchup";
543 string data2 = "Don't look conspicuous, it draws fire";
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800544 scheduler.scheduleEvent(ndn::time::seconds(1.17), ndn::bind(&TestSet1::publishSocket1, &testSet1, data1));
545 scheduler.scheduleEvent(ndn::time::seconds(1.18), ndn::bind(&TestSet1::publishSocket1, &testSet1, data2));
546 scheduler.scheduleEvent(ndn::time::seconds(2.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data1));
547 scheduler.scheduleEvent(ndn::time::seconds(2.16), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data2));
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800548 scheduler.scheduleEvent(ndn::time::seconds(2.17), ndn::bind(&TestSet1::check, &testSet1, 2));
Yingdi Yu280bb962014-01-30 09:52:43 -0800549 string data3 = "You surf the Internet, I surf the real world";
550 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
551 string data5 = "Real men wear pink. Why? Because their wives make them";
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800552 scheduler.scheduleEvent(ndn::time::seconds(3.18), ndn::bind(&TestSet1::publishSocket3, &testSet1, data3));
553 scheduler.scheduleEvent(ndn::time::seconds(3.20), ndn::bind(&TestSet1::publishSocket2, &testSet1, data4));
554 scheduler.scheduleEvent(ndn::time::seconds(3.21), ndn::bind(&TestSet1::publishSocket2, &testSet1, data5));
555 scheduler.scheduleEvent(ndn::time::seconds(4.71), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/1", data3));
556 scheduler.scheduleEvent(ndn::time::seconds(4.72), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data4));
557 scheduler.scheduleEvent(ndn::time::seconds(4.73), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/3", data5));
558 scheduler.scheduleEvent(ndn::time::seconds(4.80), ndn::bind(&TestSet1::check, &testSet1, 3));
Yingdi Yu280bb962014-01-30 09:52:43 -0800559 // not sure weither this is simultanous data generation from multiple sources
560 _LOG_DEBUG ("Simultaneous publishing");
561 string data6 = "Shakespeare says: 'Prose before hos.'";
562 string data7 = "Pick good people, talent never wears out";
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800563 scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket1, &testSet1, data6));
564 scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket2, &testSet1, data7));
565 scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/4", data6));
566 scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/4", data7));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800567 scheduler.scheduleEvent(ndn::time::seconds(6.90), ndn::bind(&TestSet1::check, &testSet1, 4));
568 scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet1::done, &testSet1));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700569
Yingdi Yu280bb962014-01-30 09:52:43 -0800570 ioService->run();
571}
Zhenkai Zhu00304002012-03-12 21:32:30 -0700572
Yingdi Yu280bb962014-01-30 09:52:43 -0800573BOOST_AUTO_TEST_CASE (AppSocketTest2)
574{
575 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
576 ndn::Scheduler scheduler(*ioService);
577 TestSet2 testSet2(ioService);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700578
Yingdi Yu280bb962014-01-30 09:52:43 -0800579 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet2::createSyncSocket1, &testSet2));
580 scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet2::createSyncSocket2, &testSet2));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800581 uint32_t num[5] = {0, 1, 2, 3, 4};
Yingdi Yu280bb962014-01-30 09:52:43 -0800582 string data0((const char *) num, sizeof(num));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800583 scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet2::publishSocket1, &testSet2, data0));
Yingdi Yu280bb962014-01-30 09:52:43 -0800584 scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num)));
585 scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet2::check, &testSet2, 10));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800586 uint32_t newNum[5] = {9, 7, 2, 1, 1};
Yingdi Yu280bb962014-01-30 09:52:43 -0800587 string data1((const char *) newNum, sizeof(newNum));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800588 scheduler.scheduleEvent(ndn::time::seconds(1.10), ndn::bind(&TestSet2::publishSocket2, &testSet2, data1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800589 scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum)));
590 scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet2::check, &testSet2, 30));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800591 scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet2::done, &testSet2));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700592
Yingdi Yu280bb962014-01-30 09:52:43 -0800593 ioService->run();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700594}
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800595
596BOOST_AUTO_TEST_CASE (AppSocketTest3)
597{
598 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
599 ndn::Scheduler scheduler(*ioService);
600 TestSet3 testSet3(ioService);
601
602 scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet3::createSyncSocket1, &testSet3));
603 scheduler.scheduleEvent(ndn::time::seconds(0.20), ndn::bind(&TestSet3::createSyncSocket2, &testSet3));
604 uint32_t num[5] = {0, 1, 2, 3, 4};
605 string data0((const char *) num, sizeof(num));
606 scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet3::publishSocket1, &testSet3, data0));
607 scheduler.scheduleEvent(ndn::time::seconds(1.50), ndn::bind(&TestSet3::setSocket1, &testSet3, (const char *) num, sizeof (num)));
608 scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet3::check, &testSet3, 10));
609 uint32_t newNum[5] = {9, 7, 2, 1, 1};
610 string data1((const char *) newNum, sizeof(newNum));
611 scheduler.scheduleEvent(ndn::time::seconds(3.00), ndn::bind(&TestSet3::publishSocket2, &testSet3, data1));
612 scheduler.scheduleEvent(ndn::time::seconds(3.50), ndn::bind(&TestSet3::setSocket2, &testSet3, (const char *) newNum, sizeof (newNum)));
613 scheduler.scheduleEvent(ndn::time::seconds(5.00), ndn::bind(&TestSet3::check, &testSet3, 30));
614 scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet3::done, &testSet3));
615
616 ioService->run();
617}