Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | using boost::test_tools::output_test_stream; |
| 26 | |
| 27 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 28 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 29 | |
Alexander Afanasyev | f46eac5 | 2013-07-26 11:27:39 -0700 | [diff] [blame] | 30 | #include "sync-logging.h" |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 31 | #include "sync-socket.h" |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 32 | #include "sync-validator.h" |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 33 | #include <ndn-cpp-dev/security/validator-null.hpp> |
Alexander Afanasyev | f46eac5 | 2013-07-26 11:27:39 -0700 | [diff] [blame] | 34 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 35 | extern "C" { |
| 36 | #include <unistd.h> |
| 37 | } |
| 38 | |
| 39 | using namespace Sync; |
| 40 | using namespace std; |
| 41 | using namespace boost; |
| 42 | |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 43 | INIT_LOGGER ("Test.AppSocket"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 44 | |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 45 | #define PRINT |
| 46 | //std::cout << "Line: " << __LINE__ << std::endl; |
| 47 | |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 48 | class TestSocketApp { |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 49 | public: |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 50 | TestSocketApp() |
| 51 | : sum(0) |
| 52 | {} |
| 53 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 54 | map<ndn::Name, string> data; |
| 55 | void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 56 | // _LOG_FUNCTION (this << ", " << str1); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 57 | 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 Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 60 | // cout << str1 << ", " << str2 << endl; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 61 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 63 | void set(ndn::Name name, const char * buf, int len) { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 64 | string str2(buf, len); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 65 | data.insert(make_pair(name, str2)); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 66 | } |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 68 | void setNum(const ndn::shared_ptr<const ndn::Data>& dataPacket) { |
| 69 | int n = dataPacket->getContent().value_size() / 4; |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 70 | uint32_t *numbers = new uint32_t [n]; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 71 | memcpy(numbers, dataPacket->getContent().value(), dataPacket->getContent().value_size()); |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 72 | for (int i = 0; i < n; i++) { |
| 73 | sum += numbers[i]; |
| 74 | } |
| 75 | delete numbers; |
| 76 | |
| 77 | } |
| 78 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 79 | void setNum(ndn::Name name, const char * buf, int len) { |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 80 | 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 Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 89 | uint32_t sum; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 91 | void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 92 | int n = v.size(); |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 93 | |
| 94 | PRINT |
| 95 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 96 | for (int i = 0; i < n; i++) { |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 97 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 98 | //PRINT |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 99 | socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1)); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 104 | void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) { |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 105 | int n = v.size(); |
| 106 | |
| 107 | PRINT |
| 108 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 109 | // std::cout << "In fetchNumbers. size of v is: " << n << std::endl; |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 110 | for (int i = 0; i < n; i++) { |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 111 | // 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 Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 112 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 113 | PRINT |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 114 | socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1)); |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 119 | void pass(const string &prefix) { |
| 120 | } |
| 121 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 122 | string toString(){ |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 123 | map<ndn::Name, string>::iterator it = data.begin(); |
Zhenkai Zhu | 1a0fd5d | 2012-03-12 21:34:42 -0700 | [diff] [blame] | 124 | string str = "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 125 | for (; it != data.end(); ++it){ |
| 126 | str += "<"; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 127 | str += it->first.toUri(); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 128 | str += "|"; |
| 129 | str += it->second; |
| 130 | str += ">"; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 131 | str += "\n"; |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 132 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 133 | |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 134 | return str; |
| 135 | } |
| 136 | |
| 137 | }; |
| 138 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 139 | class TestSet1{ |
| 140 | public: |
| 141 | TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 142 | : m_face1(new ndn::Face(ioService)) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 143 | , m_face2(new ndn::Face(ioService)) |
| 144 | , m_face3(new ndn::Face(ioService)) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 145 | , 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 Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 148 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 149 | 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 Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 152 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 153 | m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$", |
| 154 | "^(<>*)<><KEY><ksk-.*><ID-CERT>$", |
| 155 | "==", "\\1", "\\1", true); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 156 | } |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 157 | |
| 158 | void |
| 159 | createSyncSocket1() |
| 160 | { |
| 161 | _LOG_DEBUG ("s1"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 162 | |
| 163 | m_s1 = ndn::make_shared<SyncSocket>("/let/us/sync", |
| 164 | "/irl.cs.ucla.edu", |
| 165 | 0, |
| 166 | *m_id1, |
| 167 | m_rule, |
| 168 | m_face1, |
| 169 | bind(&TestSocketApp::fetchAll, &m_a1, _1, _2), |
| 170 | bind(&TestSocketApp::pass, &m_a1, _1)); |
| 171 | m_s1->addParticipant(*m_id2); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void |
| 175 | createSyncSocket2() |
| 176 | { |
| 177 | _LOG_DEBUG ("s2"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 178 | |
| 179 | m_s2 = ndn::make_shared<SyncSocket>("/let/us/sync", |
| 180 | "/yakshi.org", |
| 181 | 0, |
| 182 | *m_id2, |
| 183 | m_rule, |
| 184 | m_face2, |
| 185 | bind(&TestSocketApp::fetchAll, &m_a2, _1, _2), |
| 186 | bind(&TestSocketApp::pass, &m_a2, _1)); |
| 187 | m_s2->addParticipant(*m_id1); |
| 188 | m_s2->addParticipant(*m_id3); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void |
| 192 | createSyncSocket3() |
| 193 | { |
| 194 | _LOG_DEBUG ("s3"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 195 | |
| 196 | m_s3 = ndn::make_shared<SyncSocket>("/let/us/sync", |
| 197 | "/google.com", |
| 198 | 0, |
| 199 | *m_id3, |
| 200 | m_rule, |
| 201 | m_face3, |
| 202 | bind(&TestSocketApp::fetchAll, &m_a3, _1, _2), |
| 203 | bind(&TestSocketApp::pass, &m_a3, _1)); |
| 204 | m_s3->addParticipant(*m_id2); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 208 | publishSocket1(string data) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 209 | { |
| 210 | _LOG_DEBUG ("s1 publish"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 211 | m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 215 | publishSocket2(string data) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 216 | { |
| 217 | _LOG_DEBUG ("s2 publish"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 218 | m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 222 | publishSocket3(string data) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 223 | { |
| 224 | _LOG_DEBUG ("s3 publish"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 225 | m_s3->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | setSocket1(string suffix, string data) |
| 230 | { |
| 231 | _LOG_DEBUG ("a1 set"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 232 | ndn::Name name("/irl.cs.ucla.edu"); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 233 | name.append(suffix); |
| 234 | m_a1.set (name, data.c_str(), data.size()); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | setSocket2(string suffix, string data) |
| 239 | { |
| 240 | _LOG_DEBUG ("a2 set"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 241 | ndn::Name name("/yakshi.org"); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 242 | name.append(suffix); |
| 243 | m_a2.set (name, data.c_str(), data.size()); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | setSocket3(string suffix, string data) |
| 248 | { |
| 249 | _LOG_DEBUG ("a3 set"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 250 | ndn::Name name("/google.com"); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 251 | name.append(suffix); |
| 252 | m_a3.set (name, data.c_str(), data.size()); |
| 253 | } |
| 254 | |
| 255 | void |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame] | 256 | check(int round) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 257 | { |
| 258 | BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString()); |
| 259 | BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString()); |
| 260 | } |
| 261 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 262 | void |
| 263 | done() |
| 264 | { |
| 265 | m_s1.reset(); |
| 266 | m_s2.reset(); |
| 267 | m_s3.reset(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 268 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 269 | m_keyChain.deleteIdentity(m_name1); |
| 270 | m_keyChain.deleteIdentity(m_name2); |
| 271 | m_keyChain.deleteIdentity(m_name3); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 272 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 273 | } |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 274 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 275 | ndn::KeyChain m_keyChain; |
| 276 | ndn::shared_ptr<ndn::SecRuleRelative> m_rule; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 277 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 278 | ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 279 | ndn::Name m_name1, m_name2, m_name3; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 280 | TestSocketApp m_a1, m_a2, m_a3; |
| 281 | ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2, m_id3; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 282 | ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | class TestSet2{ |
| 286 | public: |
| 287 | TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 288 | : m_face1(new ndn::Face(ioService)) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 289 | , m_face2(new ndn::Face(ioService)) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 290 | , m_name1("/xiaonei.com/" + boost::lexical_cast<std::string>(ndn::time::now())) |
| 291 | , m_name2("/mitbbs.com/" + boost::lexical_cast<std::string>(ndn::time::now())) |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 292 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 293 | m_id1 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name1)); |
| 294 | m_id2 = m_keyChain.getCertificate(m_keyChain.createIdentity(m_name2)); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 295 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 296 | m_rule = ndn::make_shared<ndn::SecRuleRelative>("^(<>*)<><>$", |
| 297 | "^(<>*)<><KEY><ksk-.*><ID-CERT>$", |
| 298 | "==", "\\1", "\\1", true); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 299 | } |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 300 | |
| 301 | void |
| 302 | createSyncSocket1() |
| 303 | { |
| 304 | _LOG_DEBUG ("s1"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 305 | |
| 306 | m_s1 = ndn::make_shared<SyncSocket>("/this/is/the/prefix", |
| 307 | "/xiaonei.com", |
| 308 | 0, |
| 309 | *m_id1, |
| 310 | m_rule, |
| 311 | m_face1, |
| 312 | bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2), |
| 313 | bind(&TestSocketApp::pass, &m_a1, _1)); |
| 314 | |
| 315 | m_s1->addParticipant(*m_id2); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
| 319 | createSyncSocket2() |
| 320 | { |
| 321 | _LOG_DEBUG ("s2"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 322 | |
| 323 | m_s2 = ndn::make_shared<SyncSocket>("/this/is/the/prefix", |
| 324 | "/mitbbs.com", |
| 325 | 0, |
| 326 | *m_id2, |
| 327 | m_rule, |
| 328 | m_face2, |
| 329 | bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2), |
| 330 | bind(&TestSocketApp::pass, &m_a2, _1)); |
| 331 | |
| 332 | m_s2->addParticipant(*m_id1); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 336 | publishSocket1(string data) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 337 | { |
| 338 | _LOG_DEBUG ("s1 publish"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 339 | m_s1->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 343 | publishSocket2(string data) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 344 | { |
| 345 | _LOG_DEBUG ("s2 publish"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 346 | m_s2->publishData (reinterpret_cast<const uint8_t*>(data.c_str()), data.size(), 1000); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void |
| 350 | setSocket1(const char* ptr, size_t size) |
| 351 | { |
| 352 | _LOG_DEBUG ("a1 setNum"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 353 | m_a1.setNum ("/xiaonei.com", ptr, size); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void |
| 357 | setSocket2(const char* ptr, size_t size) |
| 358 | { |
| 359 | _LOG_DEBUG ("a2 setNum"); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 360 | m_a2.setNum ("/mitbbs.com", ptr, size); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
| 364 | check(int num) |
| 365 | { |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 366 | _LOG_DEBUG ("codnum " << num); |
| 367 | _LOG_DEBUG ("a1 sum " << m_a1.sum); |
| 368 | _LOG_DEBUG ("a2 sum " << m_a2.sum); |
| 369 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 370 | BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num); |
| 371 | } |
| 372 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 373 | void |
| 374 | done() |
| 375 | { |
| 376 | m_s1.reset(); |
| 377 | m_s2.reset(); |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 378 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 379 | m_keyChain.deleteIdentity(m_name1); |
| 380 | m_keyChain.deleteIdentity(m_name2); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 381 | } |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 382 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 383 | ndn::KeyChain m_keyChain; |
| 384 | ndn::shared_ptr<ndn::SecRuleRelative> m_rule; |
| 385 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 386 | TestSocketApp m_a1, m_a2; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 387 | ndn::shared_ptr<ndn::IdentityCertificate> m_id1, m_id2; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 388 | ndn::shared_ptr<ndn::Face> m_face1, m_face2; |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 389 | ndn::Name m_name1, m_name2; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 390 | ndn::shared_ptr<SyncSocket> m_s1, m_s2; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | BOOST_AUTO_TEST_CASE (AppSocketTest1) |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 394 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 395 | INIT_LOGGERS (); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 396 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 397 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 398 | ndn::Scheduler scheduler(*ioService); |
| 399 | TestSet1 testSet1(ioService); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 400 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 401 | scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet1::createSyncSocket1, &testSet1)); |
| 402 | scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet1::createSyncSocket2, &testSet1)); |
| 403 | scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet1::createSyncSocket3, &testSet1)); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 404 | string data0 = "Very funny Scotty, now beam down my clothes"; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 405 | scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet1::publishSocket1, &testSet1, data0)); |
| 406 | scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data0)); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame] | 407 | scheduler.scheduleEvent(ndn::time::seconds(1.16), ndn::bind(&TestSet1::check, &testSet1, 1)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 408 | string data1 = "Yes, give me that ketchup"; |
| 409 | string data2 = "Don't look conspicuous, it draws fire"; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 410 | scheduler.scheduleEvent(ndn::time::seconds(1.17), ndn::bind(&TestSet1::publishSocket1, &testSet1, data1)); |
| 411 | scheduler.scheduleEvent(ndn::time::seconds(1.18), ndn::bind(&TestSet1::publishSocket1, &testSet1, data2)); |
| 412 | scheduler.scheduleEvent(ndn::time::seconds(2.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data1)); |
| 413 | scheduler.scheduleEvent(ndn::time::seconds(2.16), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data2)); |
Yingdi Yu | 7ad4e4f | 2014-02-03 18:27:36 -0800 | [diff] [blame] | 414 | scheduler.scheduleEvent(ndn::time::seconds(2.17), ndn::bind(&TestSet1::check, &testSet1, 2)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 415 | string data3 = "You surf the Internet, I surf the real world"; |
| 416 | string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 417 | string data5 = "Real men wear pink. Why? Because their wives make them"; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 418 | scheduler.scheduleEvent(ndn::time::seconds(3.18), ndn::bind(&TestSet1::publishSocket3, &testSet1, data3)); |
| 419 | scheduler.scheduleEvent(ndn::time::seconds(3.20), ndn::bind(&TestSet1::publishSocket2, &testSet1, data4)); |
| 420 | scheduler.scheduleEvent(ndn::time::seconds(3.21), ndn::bind(&TestSet1::publishSocket2, &testSet1, data5)); |
| 421 | scheduler.scheduleEvent(ndn::time::seconds(4.71), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/1", data3)); |
| 422 | scheduler.scheduleEvent(ndn::time::seconds(4.72), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data4)); |
| 423 | scheduler.scheduleEvent(ndn::time::seconds(4.73), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/3", data5)); |
| 424 | scheduler.scheduleEvent(ndn::time::seconds(4.80), ndn::bind(&TestSet1::check, &testSet1, 3)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 425 | // not sure weither this is simultanous data generation from multiple sources |
| 426 | _LOG_DEBUG ("Simultaneous publishing"); |
| 427 | string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 428 | string data7 = "Pick good people, talent never wears out"; |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 429 | scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket1, &testSet1, data6)); |
| 430 | scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket2, &testSet1, data7)); |
| 431 | scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/4", data6)); |
| 432 | scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/4", data7)); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 433 | scheduler.scheduleEvent(ndn::time::seconds(6.90), ndn::bind(&TestSet1::check, &testSet1, 4)); |
| 434 | scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet1::done, &testSet1)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 435 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 436 | ioService->run(); |
| 437 | } |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 438 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 439 | BOOST_AUTO_TEST_CASE (AppSocketTest2) |
| 440 | { |
| 441 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 442 | ndn::Scheduler scheduler(*ioService); |
| 443 | TestSet2 testSet2(ioService); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 444 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 445 | scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet2::createSyncSocket1, &testSet2)); |
| 446 | scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet2::createSyncSocket2, &testSet2)); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 447 | uint32_t num[5] = {0, 1, 2, 3, 4}; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 448 | string data0((const char *) num, sizeof(num)); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 449 | scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet2::publishSocket1, &testSet2, data0)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 450 | scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num))); |
| 451 | scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet2::check, &testSet2, 10)); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 452 | uint32_t newNum[5] = {9, 7, 2, 1, 1}; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 453 | string data1((const char *) newNum, sizeof(newNum)); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame^] | 454 | scheduler.scheduleEvent(ndn::time::seconds(1.10), ndn::bind(&TestSet2::publishSocket2, &testSet2, data1)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 455 | scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum))); |
| 456 | scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet2::check, &testSet2, 30)); |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 457 | scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet2::done, &testSet2)); |
Zhenkai Zhu | 0030400 | 2012-03-12 21:32:30 -0700 | [diff] [blame] | 458 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 459 | ioService->run(); |
Zhenkai Zhu | 64db03a | 2012-03-12 19:25:56 -0700 | [diff] [blame] | 460 | } |