blob: d04ded9532c596b12caf65a9858f4deec205e117 [file] [log] [blame]
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070018 */
19
20#include <boost/test/unit_test.hpp>
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070021#include <boost/test/output_test_stream.hpp>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070022using boost::test_tools::output_test_stream;
23
24#include <boost/make_shared.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070025#include <boost/date_time/posix_time/posix_time.hpp>
26
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070027#include "sync-logging.h"
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080028#include "sync-socket.h"
Yingdi Yu0eee6002014-02-11 15:54:17 -080029#include "sync-validator.h"
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070030#include <ndn-cxx/security/validator-null.hpp>
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070031
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070032extern "C" {
33#include <unistd.h>
34}
35
36using namespace Sync;
37using namespace std;
38using namespace boost;
39
Alexander Afanasyev7df73332012-03-15 12:31:49 -070040INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070041
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070042#define PRINT
Yingdi Yud4ef5d32014-03-01 02:02:01 -080043// std::cout << "Line: " << __LINE__ << std::endl;
Zhenkai Zhua30e1782012-06-01 11:52:57 -070044
Zhenkai Zhu00304002012-03-12 21:32:30 -070045class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070046public:
Alexander Afanasyev531803b2014-02-05 15:57:35 -080047 TestSocketApp()
48 : sum(0)
49 {}
50
Yingdi Yu280bb962014-01-30 09:52:43 -080051 map<ndn::Name, string> data;
52 void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070053 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu280bb962014-01-30 09:52:43 -080054 ndn::Name dataName(dataPacket->getName());
55 string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), dataPacket->getContent().value_size());
56 data.insert(make_pair(dataName, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070057 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070058 }
Yingdi Yu43e71612013-10-30 22:19:31 -070059
Yingdi Yu280bb962014-01-30 09:52:43 -080060 void set(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070061 string str2(buf, len);
Yingdi Yu280bb962014-01-30 09:52:43 -080062 data.insert(make_pair(name, str2));
Yingdi Yu43e71612013-10-30 22:19:31 -070063 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070064
Yingdi Yud4ef5d32014-03-01 02:02:01 -080065 void setNum(const ndn::shared_ptr<const ndn::Data>& data) {
66 int n = data->getContent().value_size() / 4;
Alexander Afanasyev531803b2014-02-05 15:57:35 -080067 uint32_t *numbers = new uint32_t [n];
Yingdi Yud4ef5d32014-03-01 02:02:01 -080068 memcpy(numbers, data->getContent().value(), data->getContent().value_size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070069 for (int i = 0; i < n; i++) {
70 sum += numbers[i];
71 }
72 delete numbers;
73
74 }
75
Yingdi Yu280bb962014-01-30 09:52:43 -080076 void setNum(ndn::Name name, const char * buf, int len) {
Yingdi Yu43e71612013-10-30 22:19:31 -070077 int n = len / 4;
78 int *numbers = new int [n];
79 memcpy(numbers, buf, len);
80 for (int i = 0; i < n; i++) {
81 sum += numbers[i];
82 }
83 delete numbers;
84 }
85
Alexander Afanasyev531803b2014-02-05 15:57:35 -080086 uint32_t sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070087
Yingdi Yu43e71612013-10-30 22:19:31 -070088 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070089 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070090
91 PRINT
92
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070093 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070094 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070095 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070096 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070097 }
98 }
99 }
100
Yingdi Yu43e71612013-10-30 22:19:31 -0700101 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700102 int n = v.size();
103
104 PRINT
105
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800106 // std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700107 for (int i = 0; i < n; i++) {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800108 // 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 -0700109 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
110 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700111 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700112 }
113 }
114 }
115
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700116 void pass(const string &prefix) {
117 }
118
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700119 string toString(){
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700120 map<ndn::Name, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700121 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700122 for (; it != data.end(); ++it){
123 str += "<";
Yingdi Yu280bb962014-01-30 09:52:43 -0800124 str += it->first.toUri();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700125 str += "|";
126 str += it->second;
127 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700128 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700129 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700130
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700131 return str;
132 }
133
134};
135
Yingdi Yu280bb962014-01-30 09:52:43 -0800136class TestSet1{
137public:
138 TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700139 : m_face1(new ndn::Face(*ioService))
140 , m_face2(new ndn::Face(*ioService))
141 , m_face3(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700142 , m_name1("/irl.cs.ucla.edu/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
143 , m_name2("/yakshi.org/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
144 , m_name3("/google.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800145 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800146 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
147 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
148 m_id3 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name3));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800149
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800150 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
151 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
152 "==", "\\1", "\\1", true);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800153 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800154
155 void
156 createSyncSocket1()
157 {
158 _LOG_DEBUG ("s1");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800159
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800160 m_s1 = ndn::shared_ptr<SyncSocket>
161 (new SyncSocket("/let/us/sync",
162 "/irl.cs.ucla.edu",
163 0,
164 false,
165 "/",
166 m_face1,
167 *m_id1,
168 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700169 bind(&TestSocketApp::fetchAll, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800170 bind(&TestSocketApp::pass, &m_a1, _1)));
171
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800172 m_s1->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800173 }
174
175 void
176 createSyncSocket2()
177 {
178 _LOG_DEBUG ("s2");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800179
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800180 m_s2 = ndn::shared_ptr<SyncSocket>
181 (new SyncSocket("/let/us/sync",
182 "/yakshi.org",
183 0,
184 false,
185 "/",
186 m_face2,
187 *m_id2,
188 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700189 bind(&TestSocketApp::fetchAll, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800190 bind(&TestSocketApp::pass, &m_a2, _1)));
191
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800192 m_s2->addParticipant(*m_id1);
193 m_s2->addParticipant(*m_id3);
Yingdi Yu280bb962014-01-30 09:52:43 -0800194 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700195
Yingdi Yu280bb962014-01-30 09:52:43 -0800196 void
197 createSyncSocket3()
198 {
199 _LOG_DEBUG ("s3");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800200
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800201 m_s3 = ndn::shared_ptr<SyncSocket>
202 (new SyncSocket("/let/us/sync",
203 "/google.com",
204 0,
205 false,
206 "/",
207 m_face3,
208 *m_id3,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700209 m_rule,
210 bind(&TestSocketApp::fetchAll, &m_a3, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800211 bind(&TestSocketApp::pass, &m_a3, _1)));
212
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800213 m_s3->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800214 }
215
216 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800217 publishSocket1(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800218 {
219 _LOG_DEBUG ("s1 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700220 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800221 }
222
223 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800224 publishSocket2(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800225 {
226 _LOG_DEBUG ("s2 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700227 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800228 }
229
230 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800231 publishSocket3(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800232 {
233 _LOG_DEBUG ("s3 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700234 m_s3->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800235 }
236
237 void
238 setSocket1(string suffix, string data)
239 {
240 _LOG_DEBUG ("a1 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800241 ndn::Name name("/irl.cs.ucla.edu");
Yingdi Yu280bb962014-01-30 09:52:43 -0800242 name.append(suffix);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700243 m_a1.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800244 }
245
246 void
247 setSocket2(string suffix, string data)
248 {
249 _LOG_DEBUG ("a2 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800250 ndn::Name name("/yakshi.org");
Yingdi Yu280bb962014-01-30 09:52:43 -0800251 name.append(suffix);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700252 m_a2.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800253 }
254
255 void
256 setSocket3(string suffix, string data)
257 {
258 _LOG_DEBUG ("a3 set");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800259 ndn::Name name("/google.com");
Yingdi Yu280bb962014-01-30 09:52:43 -0800260 name.append(suffix);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700261 m_a3.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800262 }
263
264 void
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800265 check(int round)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700266 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800267 BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString());
268 BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString());
269 }
270
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800271 void
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700272 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800273 {
274 m_s1.reset();
275 m_s2.reset();
276 m_s3.reset();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800277
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800278 m_keyChain.deleteIdentity(m_name1);
279 m_keyChain.deleteIdentity(m_name2);
280 m_keyChain.deleteIdentity(m_name3);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800281
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700282 ioService->stop();
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800283 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800284
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800285 ndn::KeyChain m_keyChain;
286 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
Yingdi Yu280bb962014-01-30 09:52:43 -0800287
Yingdi Yu280bb962014-01-30 09:52:43 -0800288 ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800289 ndn::Name m_name1, m_name2, m_name3;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800290 TestSocketApp m_a1, m_a2, m_a3;
291 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2, m_id3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800292 ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800293};
294
295class TestSet2{
296public:
297 TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700298 : m_face1(new ndn::Face(*ioService))
299 , m_face2(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700300 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
301 , m_name2("/mitbbs.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
Yingdi Yu0eee6002014-02-11 15:54:17 -0800302 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800303 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
304 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800305
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800306 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
307 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
308 "==", "\\1", "\\1", true);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800309 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800310
311 void
312 createSyncSocket1()
313 {
314 _LOG_DEBUG ("s1");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700315
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800316 m_s1 = ndn::shared_ptr<SyncSocket>
317 (new SyncSocket("/this/is/the/prefix",
318 "/xiaonei.com",
319 0,
320 false,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700321 "/",
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800322 m_face1,
323 *m_id1,
324 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700325 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800326 bind(&TestSocketApp::pass, &m_a1, _1)));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800327
328 m_s1->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800329 }
330
331 void
332 createSyncSocket2()
333 {
334 _LOG_DEBUG ("s2");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800335
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800336 m_s2 = ndn::shared_ptr<SyncSocket>
337 (new SyncSocket("/this/is/the/prefix",
338 "/mitbbs.com",
339 0,
340 false,
341 "/",
342 m_face2,
343 *m_id2,
344 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700345 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800346 bind(&TestSocketApp::pass, &m_a2, _1)));
347
348 m_s2->addParticipant(*m_id1);
349 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700350
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800351 void
352 publishSocket1(string data)
353 {
354 _LOG_DEBUG ("s1 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700355 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800356 }
357
358 void
359 publishSocket2(string data)
360 {
361 _LOG_DEBUG ("s2 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700362 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800363 }
364
365 void
366 setSocket1(const char* ptr, size_t size)
367 {
368 _LOG_DEBUG ("a1 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700369 m_a1.setNum ("/xiaonei.com", ptr, size);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800370 }
371
372 void
373 setSocket2(const char* ptr, size_t size)
374 {
375 _LOG_DEBUG ("a2 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700376 m_a2.setNum ("/mitbbs.com", ptr, size);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800377 }
378
379 void
380 check(int num)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700381 {
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800382 _LOG_DEBUG ("codnum " << num);
383 _LOG_DEBUG ("a1 sum " << m_a1.sum);
384 _LOG_DEBUG ("a2 sum " << m_a2.sum);
385
386 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
387 }
388
389 void
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700390 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800391 {
392 m_s1.reset();
393 m_s2.reset();
394
395 m_keyChain.deleteIdentity(m_name1);
396 m_keyChain.deleteIdentity(m_name2);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700397
398 ioService->stop();
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800399 }
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)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700416 : m_face1(new ndn::Face(*ioService))
417 , m_face2(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700418 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
419 , m_name2("/mitbbs.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800420 {
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");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700433
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800434 m_s1 = ndn::shared_ptr<SyncSocket>
435 (new SyncSocket("/this/is/the/prefix",
436 "/xiaonei.com",
437 1,
438 true,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700439 "/abc",
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800440 m_face1,
441 *m_id1,
442 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700443 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800444 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,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700463 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800464 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 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700468
Yingdi Yu280bb962014-01-30 09:52:43 -0800469 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 Yu7c64e5c2014-04-30 14:06:37 -0700473 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 Yu7c64e5c2014-04-30 14:06:37 -0700480 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 Yu7c64e5c2014-04-30 14:06:37 -0700487 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 Yu7c64e5c2014-04-30 14:06:37 -0700494 m_a2.setNum ("/mitbbs.com", ptr, size);
Yingdi Yu280bb962014-01-30 09:52:43 -0800495 }
496
497 void
498 check(int num)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700499 {
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
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700508 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800509 {
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);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700515
516 ioService->stop();
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800517 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800518
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800519 ndn::KeyChain m_keyChain;
520 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
521
Yingdi Yu280bb962014-01-30 09:52:43 -0800522 TestSocketApp m_a1, m_a2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800523 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800524 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800525 ndn::Name m_name1, m_name2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800526 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800527};
528
529BOOST_AUTO_TEST_CASE (AppSocketTest1)
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700530{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700531 INIT_LOGGERS ();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700532
Yingdi Yu280bb962014-01-30 09:52:43 -0800533 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
534 ndn::Scheduler scheduler(*ioService);
535 TestSet1 testSet1(ioService);
Yingdi Yu43e71612013-10-30 22:19:31 -0700536
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700537 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet1::createSyncSocket1, &testSet1));
538 scheduler.scheduleEvent(ndn::time::milliseconds(50), ndn::bind(&TestSet1::createSyncSocket2, &testSet1));
539 scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestSet1::createSyncSocket3, &testSet1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700540 string data0 = "Very funny Scotty, now beam down my clothes";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700541 scheduler.scheduleEvent(ndn::time::milliseconds(150), ndn::bind(&TestSet1::publishSocket1, &testSet1, data0));
542 scheduler.scheduleEvent(ndn::time::milliseconds(1150), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data0));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700543 scheduler.scheduleEvent(ndn::time::milliseconds(1160), ndn::bind(&TestSet1::check, &testSet1, 1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800544 string data1 = "Yes, give me that ketchup";
545 string data2 = "Don't look conspicuous, it draws fire";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700546 scheduler.scheduleEvent(ndn::time::milliseconds(1170), ndn::bind(&TestSet1::publishSocket1, &testSet1, data1));
547 scheduler.scheduleEvent(ndn::time::milliseconds(1180), ndn::bind(&TestSet1::publishSocket1, &testSet1, data2));
548 scheduler.scheduleEvent(ndn::time::milliseconds(2150), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data1));
549 scheduler.scheduleEvent(ndn::time::milliseconds(2160), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data2));
550 scheduler.scheduleEvent(ndn::time::milliseconds(2170), ndn::bind(&TestSet1::check, &testSet1, 2));
Yingdi Yu280bb962014-01-30 09:52:43 -0800551 string data3 = "You surf the Internet, I surf the real world";
552 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
553 string data5 = "Real men wear pink. Why? Because their wives make them";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700554 scheduler.scheduleEvent(ndn::time::milliseconds(3180), ndn::bind(&TestSet1::publishSocket3, &testSet1, data3));
555 scheduler.scheduleEvent(ndn::time::milliseconds(3200), ndn::bind(&TestSet1::publishSocket2, &testSet1, data4));
556 scheduler.scheduleEvent(ndn::time::milliseconds(3210), ndn::bind(&TestSet1::publishSocket2, &testSet1, data5));
557 scheduler.scheduleEvent(ndn::time::milliseconds(4710), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/1", data3));
558 scheduler.scheduleEvent(ndn::time::milliseconds(4720), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data4));
559 scheduler.scheduleEvent(ndn::time::milliseconds(4730), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/3", data5));
560 scheduler.scheduleEvent(ndn::time::milliseconds(4800), ndn::bind(&TestSet1::check, &testSet1, 3));
Yingdi Yu280bb962014-01-30 09:52:43 -0800561 // not sure weither this is simultanous data generation from multiple sources
562 _LOG_DEBUG ("Simultaneous publishing");
563 string data6 = "Shakespeare says: 'Prose before hos.'";
564 string data7 = "Pick good people, talent never wears out";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700565 scheduler.scheduleEvent(ndn::time::milliseconds(5500), ndn::bind(&TestSet1::publishSocket1, &testSet1, data6));
566 scheduler.scheduleEvent(ndn::time::milliseconds(5500), ndn::bind(&TestSet1::publishSocket2, &testSet1, data7));
567 scheduler.scheduleEvent(ndn::time::milliseconds(6800), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/4", data6));
568 scheduler.scheduleEvent(ndn::time::milliseconds(6800), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/4", data7));
569 scheduler.scheduleEvent(ndn::time::milliseconds(6900), ndn::bind(&TestSet1::check, &testSet1, 4));
570 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet1::done, &testSet1, ioService));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700571
Yingdi Yu280bb962014-01-30 09:52:43 -0800572 ioService->run();
573}
Zhenkai Zhu00304002012-03-12 21:32:30 -0700574
Yingdi Yu280bb962014-01-30 09:52:43 -0800575BOOST_AUTO_TEST_CASE (AppSocketTest2)
576{
577 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
578 ndn::Scheduler scheduler(*ioService);
579 TestSet2 testSet2(ioService);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700580
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700581 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet2::createSyncSocket1, &testSet2));
582 scheduler.scheduleEvent(ndn::time::milliseconds(50), ndn::bind(&TestSet2::createSyncSocket2, &testSet2));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800583 uint32_t num[5] = {0, 1, 2, 3, 4};
Yingdi Yu280bb962014-01-30 09:52:43 -0800584 string data0((const char *) num, sizeof(num));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700585 scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestSet2::publishSocket1, &testSet2, data0));
586 scheduler.scheduleEvent(ndn::time::milliseconds(150), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num)));
587 scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestSet2::check, &testSet2, 10));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800588 uint32_t newNum[5] = {9, 7, 2, 1, 1};
Yingdi Yu280bb962014-01-30 09:52:43 -0800589 string data1((const char *) newNum, sizeof(newNum));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700590 scheduler.scheduleEvent(ndn::time::milliseconds(1100), ndn::bind(&TestSet2::publishSocket2, &testSet2, data1));
591 scheduler.scheduleEvent(ndn::time::milliseconds(1150), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum)));
592 scheduler.scheduleEvent(ndn::time::milliseconds(2000), ndn::bind(&TestSet2::check, &testSet2, 30));
593 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet2::done, &testSet2, ioService));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700594
Yingdi Yu280bb962014-01-30 09:52:43 -0800595 ioService->run();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700596}
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800597
598BOOST_AUTO_TEST_CASE (AppSocketTest3)
599{
600 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
601 ndn::Scheduler scheduler(*ioService);
602 TestSet3 testSet3(ioService);
603
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700604 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet3::createSyncSocket1, &testSet3));
605 scheduler.scheduleEvent(ndn::time::milliseconds(200), ndn::bind(&TestSet3::createSyncSocket2, &testSet3));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800606 uint32_t num[5] = {0, 1, 2, 3, 4};
607 string data0((const char *) num, sizeof(num));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700608 scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestSet3::publishSocket1, &testSet3, data0));
609 scheduler.scheduleEvent(ndn::time::milliseconds(1500), ndn::bind(&TestSet3::setSocket1, &testSet3, (const char *) num, sizeof (num)));
610 scheduler.scheduleEvent(ndn::time::milliseconds(2000), ndn::bind(&TestSet3::check, &testSet3, 10));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800611 uint32_t newNum[5] = {9, 7, 2, 1, 1};
612 string data1((const char *) newNum, sizeof(newNum));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700613 scheduler.scheduleEvent(ndn::time::milliseconds(3000), ndn::bind(&TestSet3::publishSocket2, &testSet3, data1));
614 scheduler.scheduleEvent(ndn::time::milliseconds(3500), ndn::bind(&TestSet3::setSocket2, &testSet3, (const char *) newNum, sizeof (newNum)));
615 scheduler.scheduleEvent(ndn::time::milliseconds(5000), ndn::bind(&TestSet3::check, &testSet3, 30));
616 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet3::done, &testSet3, ioService));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800617
618 ioService->run();
619}