blob: ebce9c02ffafa5b9b8a7d88284c0efb082648842 [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
96 finish()
97 {
98 }
99
100 void
101 createSyncLogic(int index,
102 ndn::shared_ptr<Handler> h)
103 {
Yingdi Yu0eee6002014-02-11 15:54:17 -0800104 ndn::Name identity("/tmp-" + boost::lexical_cast<std::string>(ndn::time::now()));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800105 ndn::shared_ptr<ndn::IdentityCertificate> cert = m_keyChain.getCertificate(m_keyChain.createIdentity(identity));
Yingdi Yu280bb962014-01-30 09:52:43 -0800106 m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService);
107 m_l[index] = new SyncLogic(ndn::Name("/bcast"),
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800108 *cert,
Yingdi Yu280bb962014-01-30 09:52:43 -0800109 m_validator, m_faces[index],
110 bind (&Handler::wrapper, &*h, _1),
111 bind (&Handler::onRemove, &*h, _1));
112 }
113
114 void
115 getOldDigestForOne()
116 {
117 m_oldDigest = m_l[0]->getRootDigest();
118 }
119
120 void
121 getNewDigestForOne()
122 {
123 m_newDigest = m_l[0]->getRootDigest();
124 }
125
126 void
127 addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq)
128 {
129 m_l[0]->addLocalNames(name, session, seq);
130 }
131
132 void
133 removeForOne(ndn::Name name)
134 {
135 m_l[0]->remove(name);
136 }
137
138 void
139 checkDigest()
140 {
141 BOOST_CHECK(m_oldDigest != m_newDigest);
142 }
143
144
145public:
Yingdi Yu0eee6002014-02-11 15:54:17 -0800146 ndn::KeyChain m_keyChain;
Yingdi Yu280bb962014-01-30 09:52:43 -0800147 ndn::shared_ptr<boost::asio::io_service> m_ioService;
148 SyncLogic* m_l[2];
149 ndn::shared_ptr<ndn::Face> m_faces[2];
150 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
151 string m_oldDigest;
152 string m_newDigest;
153};
154
155void
156checkMapSize(ndn::shared_ptr<Handler> h, int size)
157{ BOOST_CHECK_EQUAL (h->m_map.size (), size); }
158
159
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700160BOOST_AUTO_TEST_CASE (SyncLogicTest)
161{
Yingdi Yu280bb962014-01-30 09:52:43 -0800162 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
163 ndn::Scheduler scheduler(*ioService);
164 TestCore testCore(ioService);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700165
Yingdi Yu280bb962014-01-30 09:52:43 -0800166 ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1");
167 ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2");
Yingdi Yu43e71612013-10-30 22:19:31 -0700168
Yingdi Yu280bb962014-01-30 09:52:43 -0800169 scheduler.scheduleEvent(ndn::time::seconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1));
170 scheduler.scheduleEvent(ndn::time::seconds(0.1), ndn::bind(&TestCore::getOldDigestForOne, &testCore));
171 scheduler.scheduleEvent(ndn::time::seconds(0.2), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2));
172 scheduler.scheduleEvent(ndn::time::seconds(0.3), ndn::bind(&checkMapSize, h1, 0));
173 scheduler.scheduleEvent(ndn::time::seconds(0.4), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2));
174 scheduler.scheduleEvent(ndn::time::seconds(0.5), ndn::bind(&checkMapSize, h1, 0));
175 scheduler.scheduleEvent(ndn::time::seconds(0.6), ndn::bind(&checkMapSize, h2, 1));
176 scheduler.scheduleEvent(ndn::time::seconds(0.7), ndn::bind(&TestCore::removeForOne, &testCore, "/one"));
177 scheduler.scheduleEvent(ndn::time::seconds(0.8), ndn::bind(&TestCore::getNewDigestForOne, &testCore));
178 scheduler.scheduleEvent(ndn::time::seconds(0.9), ndn::bind(&TestCore::checkDigest, &testCore));
179 scheduler.scheduleEvent(ndn::time::seconds(1.0), ndn::bind(&TestCore::finish, &testCore));
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700180
Yingdi Yu280bb962014-01-30 09:52:43 -0800181 ioService->run();
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700182
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700183}