blob: d430a464f358359872d1b87b65a97b4e71b49ca1 [file] [log] [blame]
Yingdi Yu280bb962014-01-30 09:52:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -07002/*
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>
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25#include <map>
26using boost::test_tools::output_test_stream;
27
28#include <boost/make_shared.hpp>
29
Yingdi Yu280bb962014-01-30 09:52:43 -080030#include <ndn-cpp-dev/security/validator-null.hpp>
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070031#include "sync-logic.h"
32#include "sync-seq-no.h"
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070033
34using namespace std;
35using namespace boost;
36using namespace Sync;
37
38struct Handler
39{
40 string instance;
41
42 Handler (const string &_instance)
43 : instance (_instance)
44 {
45 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070046
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070047 void wrapper (const vector<MissingDataInfo> &v) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070048 int n = v.size();
49 for (int i = 0; i < n; i++) {
50 onUpdate (v[i].prefix, v[i].high, v[i].low);
51 }
52 }
53
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070054 void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070055 {
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070056 m_map[p] = seq.getSeq ();
57
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070058 // cout << instance << "\t";
59 // if (!oldSeq.isValid ())
60 // cout << "Inserted: " << p << " (" << seq << ")" << endl;
61 // else
62 // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070063 }
64
65 void onRemove (const string &p/*prefix*/)
66 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070067 // cout << instance << "\tRemoved: " << p << endl;
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070068 m_map.erase (p);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070069 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070070
71 map<string, uint32_t> m_map;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070072};
73
Yingdi Yu280bb962014-01-30 09:52:43 -080074class TestCore
75{
76public:
77 TestCore(ndn::shared_ptr<boost::asio::io_service> ioService)
78 : m_ioService(ioService)
79 {
80 m_l[0] = 0;
81 m_l[1] = 0;
82
83 m_validator = ndn::make_shared<ndn::ValidatorNull>();
84 }
85
86 ~TestCore()
87 {
88 if(m_l[0] != 0)
89 delete m_l[0];
90
91 if(m_l[1] != 0)
92 delete m_l[1];
93 }
94
95 void
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070096 finish(ndn::shared_ptr<boost::asio::io_service> ioService)
Yingdi Yu280bb962014-01-30 09:52:43 -080097 {
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070098 ioService->stop();
Yingdi Yu280bb962014-01-30 09:52:43 -080099 }
100
101 void
102 createSyncLogic(int index,
103 ndn::shared_ptr<Handler> h)
104 {
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700105 ndn::Name identity("/tmp-" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800106 ndn::shared_ptr<ndn::IdentityCertificate> cert = m_keyChain.getCertificate(m_keyChain.createIdentity(identity));
Yingdi Yu280bb962014-01-30 09:52:43 -0800107 m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService);
108 m_l[index] = new SyncLogic(ndn::Name("/bcast"),
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800109 *cert,
Yingdi Yu280bb962014-01-30 09:52:43 -0800110 m_validator, m_faces[index],
111 bind (&Handler::wrapper, &*h, _1),
112 bind (&Handler::onRemove, &*h, _1));
113 }
114
115 void
116 getOldDigestForOne()
117 {
118 m_oldDigest = m_l[0]->getRootDigest();
119 }
120
121 void
122 getNewDigestForOne()
123 {
124 m_newDigest = m_l[0]->getRootDigest();
125 }
126
127 void
128 addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq)
129 {
130 m_l[0]->addLocalNames(name, session, seq);
131 }
132
133 void
134 removeForOne(ndn::Name name)
135 {
136 m_l[0]->remove(name);
137 }
138
139 void
140 checkDigest()
141 {
142 BOOST_CHECK(m_oldDigest != m_newDigest);
143 }
144
145
146public:
Yingdi Yu0eee6002014-02-11 15:54:17 -0800147 ndn::KeyChain m_keyChain;
Yingdi Yu280bb962014-01-30 09:52:43 -0800148 ndn::shared_ptr<boost::asio::io_service> m_ioService;
149 SyncLogic* m_l[2];
150 ndn::shared_ptr<ndn::Face> m_faces[2];
151 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
152 string m_oldDigest;
153 string m_newDigest;
154};
155
156void
157checkMapSize(ndn::shared_ptr<Handler> h, int size)
158{ BOOST_CHECK_EQUAL (h->m_map.size (), size); }
159
160
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700161BOOST_AUTO_TEST_CASE (SyncLogicTest)
162{
Yingdi Yu280bb962014-01-30 09:52:43 -0800163 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
164 ndn::Scheduler scheduler(*ioService);
165 TestCore testCore(ioService);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700166
Yingdi Yu280bb962014-01-30 09:52:43 -0800167 ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1");
168 ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2");
Yingdi Yu43e71612013-10-30 22:19:31 -0700169
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700170 scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1));
171 scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestCore::getOldDigestForOne, &testCore));
172 scheduler.scheduleEvent(ndn::time::milliseconds(200), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2));
173 scheduler.scheduleEvent(ndn::time::milliseconds(300), ndn::bind(&checkMapSize, h1, 0));
174 scheduler.scheduleEvent(ndn::time::milliseconds(400), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2));
175 scheduler.scheduleEvent(ndn::time::milliseconds(500), ndn::bind(&checkMapSize, h1, 0));
176 scheduler.scheduleEvent(ndn::time::milliseconds(600), ndn::bind(&checkMapSize, h2, 1));
177 scheduler.scheduleEvent(ndn::time::milliseconds(700), ndn::bind(&TestCore::removeForOne, &testCore, "/one"));
178 scheduler.scheduleEvent(ndn::time::milliseconds(800), ndn::bind(&TestCore::getNewDigestForOne, &testCore));
179 scheduler.scheduleEvent(ndn::time::milliseconds(900), ndn::bind(&TestCore::checkDigest, &testCore));
180 scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestCore::finish, &testCore, ioService));
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700181
Yingdi Yu280bb962014-01-30 09:52:43 -0800182 ioService->run();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700183
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700184}