blob: 2c0d216dd24598ae2ee4485949d60931b62d2c42 [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>
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070021 */
22
23#include <boost/test/unit_test.hpp>
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070024#include <boost/test/output_test_stream.hpp>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070025using 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 Yu7c64e5c2014-04-30 14:06:37 -070033#include <ndn-cxx/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
Yingdi Yu7c64e5c2014-04-30 14:06:37 -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 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -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 Yu7c64e5c2014-04-30 14:06:37 -0700123 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)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700142 : m_face1(new ndn::Face(*ioService))
143 , m_face2(new ndn::Face(*ioService))
144 , m_face3(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700145 , m_name1("/irl.cs.ucla.edu/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
146 , m_name2("/yakshi.org/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
147 , 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 -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,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700172 bind(&TestSocketApp::fetchAll, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800173 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,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700192 bind(&TestSocketApp::fetchAll, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800193 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 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700198
Yingdi Yu280bb962014-01-30 09:52:43 -0800199 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,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700212 m_rule,
213 bind(&TestSocketApp::fetchAll, &m_a3, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800214 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 Yu7c64e5c2014-04-30 14:06:37 -0700223 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 Yu7c64e5c2014-04-30 14:06:37 -0700230 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 Yu7c64e5c2014-04-30 14:06:37 -0700237 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);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700246 m_a1.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800247 }
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);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700255 m_a2.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800256 }
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);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700264 m_a3.set (name, data.c_str(), data.size());
Yingdi Yu280bb962014-01-30 09:52:43 -0800265 }
266
267 void
Yingdi Yu7ad4e4f2014-02-03 18:27:36 -0800268 check(int round)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700269 {
Yingdi Yu280bb962014-01-30 09:52:43 -0800270 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
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700275 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800276 {
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
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700285 ioService->stop();
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800286 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800287
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800288 ndn::KeyChain m_keyChain;
289 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
Yingdi Yu280bb962014-01-30 09:52:43 -0800290
Yingdi Yu280bb962014-01-30 09:52:43 -0800291 ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800292 ndn::Name m_name1, m_name2, m_name3;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800293 TestSocketApp m_a1, m_a2, m_a3;
294 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2, m_id3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800295 ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3;
Yingdi Yu280bb962014-01-30 09:52:43 -0800296};
297
298class TestSet2{
299public:
300 TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700301 : m_face1(new ndn::Face(*ioService))
302 , m_face2(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700303 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
304 , 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 -0800305 {
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800306 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
307 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800308
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800309 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
310 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
311 "==", "\\1", "\\1", true);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800312 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800313
314 void
315 createSyncSocket1()
316 {
317 _LOG_DEBUG ("s1");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700318
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800319 m_s1 = ndn::shared_ptr<SyncSocket>
320 (new SyncSocket("/this/is/the/prefix",
321 "/xiaonei.com",
322 0,
323 false,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700324 "/",
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800325 m_face1,
326 *m_id1,
327 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700328 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800329 bind(&TestSocketApp::pass, &m_a1, _1)));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800330
331 m_s1->addParticipant(*m_id2);
Yingdi Yu280bb962014-01-30 09:52:43 -0800332 }
333
334 void
335 createSyncSocket2()
336 {
337 _LOG_DEBUG ("s2");
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800338
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800339 m_s2 = ndn::shared_ptr<SyncSocket>
340 (new SyncSocket("/this/is/the/prefix",
341 "/mitbbs.com",
342 0,
343 false,
344 "/",
345 m_face2,
346 *m_id2,
347 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700348 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800349 bind(&TestSocketApp::pass, &m_a2, _1)));
350
351 m_s2->addParticipant(*m_id1);
352 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700353
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800354 void
355 publishSocket1(string data)
356 {
357 _LOG_DEBUG ("s1 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700358 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800359 }
360
361 void
362 publishSocket2(string data)
363 {
364 _LOG_DEBUG ("s2 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700365 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800366 }
367
368 void
369 setSocket1(const char* ptr, size_t size)
370 {
371 _LOG_DEBUG ("a1 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700372 m_a1.setNum ("/xiaonei.com", ptr, size);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800373 }
374
375 void
376 setSocket2(const char* ptr, size_t size)
377 {
378 _LOG_DEBUG ("a2 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700379 m_a2.setNum ("/mitbbs.com", ptr, size);
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800380 }
381
382 void
383 check(int num)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700384 {
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800385 _LOG_DEBUG ("codnum " << num);
386 _LOG_DEBUG ("a1 sum " << m_a1.sum);
387 _LOG_DEBUG ("a2 sum " << m_a2.sum);
388
389 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
390 }
391
392 void
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700393 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800394 {
395 m_s1.reset();
396 m_s2.reset();
397
398 m_keyChain.deleteIdentity(m_name1);
399 m_keyChain.deleteIdentity(m_name2);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700400
401 ioService->stop();
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800402 }
403
404 ndn::KeyChain m_keyChain;
405 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
406
407 TestSocketApp m_a1, m_a2;
408 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2;
409 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
410 ndn::Name m_name1, m_name2;
411 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
412};
413
414
415
416class TestSet3{
417public:
418 TestSet3(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700419 : m_face1(new ndn::Face(*ioService))
420 , m_face2(new ndn::Face(*ioService))
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700421 , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()))
422 , 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 -0800423 {
424 m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1));
425 m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2));
426
427 m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$",
428 "^(<>*)<><KEY><ksk-.*><ID-CERT>$",
429 "==", "\\1", "\\1", true);
430 }
431
432 void
433 createSyncSocket1()
434 {
435 _LOG_DEBUG ("s1");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700436
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800437 m_s1 = ndn::shared_ptr<SyncSocket>
438 (new SyncSocket("/this/is/the/prefix",
439 "/xiaonei.com",
440 1,
441 true,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700442 "/abc",
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800443 m_face1,
444 *m_id1,
445 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700446 bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800447 bind(&TestSocketApp::pass, &m_a1, _1)));
448
449 m_s1->addParticipant(*m_id2);
450 }
451
452 void
453 createSyncSocket2()
454 {
455 _LOG_DEBUG ("s2");
456
457 m_s2 = ndn::shared_ptr<SyncSocket>
458 (new SyncSocket("/this/is/the/prefix",
459 "/mitbbs.com",
460 1,
461 false,
462 "/",
463 m_face2,
464 *m_id2,
465 m_rule,
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700466 bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2),
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800467 bind(&TestSocketApp::pass, &m_a2, _1)));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800468
469 m_s2->addParticipant(*m_id1);
Yingdi Yu280bb962014-01-30 09:52:43 -0800470 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700471
Yingdi Yu280bb962014-01-30 09:52:43 -0800472 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800473 publishSocket1(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800474 {
475 _LOG_DEBUG ("s1 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700476 m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800477 }
478
479 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800480 publishSocket2(string data)
Yingdi Yu280bb962014-01-30 09:52:43 -0800481 {
482 _LOG_DEBUG ("s2 publish");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700483 m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000);
Yingdi Yu280bb962014-01-30 09:52:43 -0800484 }
485
486 void
487 setSocket1(const char* ptr, size_t size)
488 {
489 _LOG_DEBUG ("a1 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700490 m_a1.setNum ("/xiaonei.com", ptr, size);
Yingdi Yu280bb962014-01-30 09:52:43 -0800491 }
492
493 void
494 setSocket2(const char* ptr, size_t size)
495 {
496 _LOG_DEBUG ("a2 setNum");
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700497 m_a2.setNum ("/mitbbs.com", ptr, size);
Yingdi Yu280bb962014-01-30 09:52:43 -0800498 }
499
500 void
501 check(int num)
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700502 {
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800503 _LOG_DEBUG ("codnum " << num);
504 _LOG_DEBUG ("a1 sum " << m_a1.sum);
505 _LOG_DEBUG ("a2 sum " << m_a2.sum);
506
Yingdi Yu280bb962014-01-30 09:52:43 -0800507 BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num);
508 }
509
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800510 void
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700511 done(ndn::shared_ptr<boost::asio::io_service> ioService)
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800512 {
513 m_s1.reset();
514 m_s2.reset();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800515
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800516 m_keyChain.deleteIdentity(m_name1);
517 m_keyChain.deleteIdentity(m_name2);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700518
519 ioService->stop();
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800520 }
Yingdi Yu280bb962014-01-30 09:52:43 -0800521
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800522 ndn::KeyChain m_keyChain;
523 ndn::shared_ptr<ndn::SecRuleRelative> m_rule;
524
Yingdi Yu280bb962014-01-30 09:52:43 -0800525 TestSocketApp m_a1, m_a2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800526 ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800527 ndn::shared_ptr<ndn::Face> m_face1, m_face2;
Yingdi Yu0eee6002014-02-11 15:54:17 -0800528 ndn::Name m_name1, m_name2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800529 ndn::shared_ptr<SyncSocket> m_s1, m_s2;
Yingdi Yu280bb962014-01-30 09:52:43 -0800530};
531
532BOOST_AUTO_TEST_CASE (AppSocketTest1)
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700533{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700534 INIT_LOGGERS ();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700535
Yingdi Yu280bb962014-01-30 09:52:43 -0800536 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
537 ndn::Scheduler scheduler(*ioService);
538 TestSet1 testSet1(ioService);
Yingdi Yu43e71612013-10-30 22:19:31 -0700539
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700540 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet1::createSyncSocket1, &testSet1));
541 scheduler.scheduleEvent(ndn::time::milliseconds(50), ndn::bind(&TestSet1::createSyncSocket2, &testSet1));
542 scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestSet1::createSyncSocket3, &testSet1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700543 string data0 = "Very funny Scotty, now beam down my clothes";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700544 scheduler.scheduleEvent(ndn::time::milliseconds(150), ndn::bind(&TestSet1::publishSocket1, &testSet1, data0));
545 scheduler.scheduleEvent(ndn::time::milliseconds(1150), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data0));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700546 scheduler.scheduleEvent(ndn::time::milliseconds(1160), ndn::bind(&TestSet1::check, &testSet1, 1));
Yingdi Yu280bb962014-01-30 09:52:43 -0800547 string data1 = "Yes, give me that ketchup";
548 string data2 = "Don't look conspicuous, it draws fire";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700549 scheduler.scheduleEvent(ndn::time::milliseconds(1170), ndn::bind(&TestSet1::publishSocket1, &testSet1, data1));
550 scheduler.scheduleEvent(ndn::time::milliseconds(1180), ndn::bind(&TestSet1::publishSocket1, &testSet1, data2));
551 scheduler.scheduleEvent(ndn::time::milliseconds(2150), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data1));
552 scheduler.scheduleEvent(ndn::time::milliseconds(2160), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data2));
553 scheduler.scheduleEvent(ndn::time::milliseconds(2170), ndn::bind(&TestSet1::check, &testSet1, 2));
Yingdi Yu280bb962014-01-30 09:52:43 -0800554 string data3 = "You surf the Internet, I surf the real world";
555 string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
556 string data5 = "Real men wear pink. Why? Because their wives make them";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700557 scheduler.scheduleEvent(ndn::time::milliseconds(3180), ndn::bind(&TestSet1::publishSocket3, &testSet1, data3));
558 scheduler.scheduleEvent(ndn::time::milliseconds(3200), ndn::bind(&TestSet1::publishSocket2, &testSet1, data4));
559 scheduler.scheduleEvent(ndn::time::milliseconds(3210), ndn::bind(&TestSet1::publishSocket2, &testSet1, data5));
560 scheduler.scheduleEvent(ndn::time::milliseconds(4710), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/1", data3));
561 scheduler.scheduleEvent(ndn::time::milliseconds(4720), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data4));
562 scheduler.scheduleEvent(ndn::time::milliseconds(4730), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/3", data5));
563 scheduler.scheduleEvent(ndn::time::milliseconds(4800), ndn::bind(&TestSet1::check, &testSet1, 3));
Yingdi Yu280bb962014-01-30 09:52:43 -0800564 // not sure weither this is simultanous data generation from multiple sources
565 _LOG_DEBUG ("Simultaneous publishing");
566 string data6 = "Shakespeare says: 'Prose before hos.'";
567 string data7 = "Pick good people, talent never wears out";
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700568 scheduler.scheduleEvent(ndn::time::milliseconds(5500), ndn::bind(&TestSet1::publishSocket1, &testSet1, data6));
569 scheduler.scheduleEvent(ndn::time::milliseconds(5500), ndn::bind(&TestSet1::publishSocket2, &testSet1, data7));
570 scheduler.scheduleEvent(ndn::time::milliseconds(6800), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/4", data6));
571 scheduler.scheduleEvent(ndn::time::milliseconds(6800), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/4", data7));
572 scheduler.scheduleEvent(ndn::time::milliseconds(6900), ndn::bind(&TestSet1::check, &testSet1, 4));
573 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet1::done, &testSet1, ioService));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700574
Yingdi Yu280bb962014-01-30 09:52:43 -0800575 ioService->run();
576}
Zhenkai Zhu00304002012-03-12 21:32:30 -0700577
Yingdi Yu280bb962014-01-30 09:52:43 -0800578BOOST_AUTO_TEST_CASE (AppSocketTest2)
579{
580 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
581 ndn::Scheduler scheduler(*ioService);
582 TestSet2 testSet2(ioService);
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700583
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700584 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet2::createSyncSocket1, &testSet2));
585 scheduler.scheduleEvent(ndn::time::milliseconds(50), ndn::bind(&TestSet2::createSyncSocket2, &testSet2));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800586 uint32_t num[5] = {0, 1, 2, 3, 4};
Yingdi Yu280bb962014-01-30 09:52:43 -0800587 string data0((const char *) num, sizeof(num));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700588 scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestSet2::publishSocket1, &testSet2, data0));
589 scheduler.scheduleEvent(ndn::time::milliseconds(150), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num)));
590 scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestSet2::check, &testSet2, 10));
Alexander Afanasyev531803b2014-02-05 15:57:35 -0800591 uint32_t newNum[5] = {9, 7, 2, 1, 1};
Yingdi Yu280bb962014-01-30 09:52:43 -0800592 string data1((const char *) newNum, sizeof(newNum));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700593 scheduler.scheduleEvent(ndn::time::milliseconds(1100), ndn::bind(&TestSet2::publishSocket2, &testSet2, data1));
594 scheduler.scheduleEvent(ndn::time::milliseconds(1150), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum)));
595 scheduler.scheduleEvent(ndn::time::milliseconds(2000), ndn::bind(&TestSet2::check, &testSet2, 30));
596 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet2::done, &testSet2, ioService));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700597
Yingdi Yu280bb962014-01-30 09:52:43 -0800598 ioService->run();
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700599}
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800600
601BOOST_AUTO_TEST_CASE (AppSocketTest3)
602{
603 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
604 ndn::Scheduler scheduler(*ioService);
605 TestSet3 testSet3(ioService);
606
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700607 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestSet3::createSyncSocket1, &testSet3));
608 scheduler.scheduleEvent(ndn::time::milliseconds(200), ndn::bind(&TestSet3::createSyncSocket2, &testSet3));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800609 uint32_t num[5] = {0, 1, 2, 3, 4};
610 string data0((const char *) num, sizeof(num));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700611 scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestSet3::publishSocket1, &testSet3, data0));
612 scheduler.scheduleEvent(ndn::time::milliseconds(1500), ndn::bind(&TestSet3::setSocket1, &testSet3, (const char *) num, sizeof (num)));
613 scheduler.scheduleEvent(ndn::time::milliseconds(2000), ndn::bind(&TestSet3::check, &testSet3, 10));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800614 uint32_t newNum[5] = {9, 7, 2, 1, 1};
615 string data1((const char *) newNum, sizeof(newNum));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700616 scheduler.scheduleEvent(ndn::time::milliseconds(3000), ndn::bind(&TestSet3::publishSocket2, &testSet3, data1));
617 scheduler.scheduleEvent(ndn::time::milliseconds(3500), ndn::bind(&TestSet3::setSocket2, &testSet3, (const char *) newNum, sizeof (newNum)));
618 scheduler.scheduleEvent(ndn::time::milliseconds(5000), ndn::bind(&TestSet3::check, &testSet3, 30));
619 scheduler.scheduleEvent(ndn::time::milliseconds(7000), ndn::bind(&TestSet3::done, &testSet3, ioService));
Yingdi Yud4ef5d32014-03-01 02:02:01 -0800620
621 ioService->run();
622}