blob: a96c0bd5f65edf4c386e38244cbd045cde7b3e34 [file] [log] [blame]
Yingdi Yu0eee6002014-02-11 15:54:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 */
5
6#include <boost/test/unit_test.hpp>
7#include "sync-validator.h"
8
Alexander Afanasyev7fe59832014-07-02 12:17:46 -07009#include <boost/lexical_cast.hpp>
10#include <boost/asio.hpp>
11#include <ndn-cxx/util/scheduler.hpp>
12
Yingdi Yu0eee6002014-02-11 15:54:17 -080013BOOST_AUTO_TEST_SUITE(TestSyncValidator)
14
Yingdi Yua873d642014-04-15 21:31:33 -070015void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080016onValidated(const ndn::shared_ptr<const ndn::Data>& data)
Yingdi Yu0eee6002014-02-11 15:54:17 -080017{
18 BOOST_CHECK(true);
19}
20
Yingdi Yua873d642014-04-15 21:31:33 -070021void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080022onValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
23 const std::string& failureInfo)
Yingdi Yu0eee6002014-02-11 15:54:17 -080024{
25 BOOST_CHECK(false);
26}
27
Yingdi Yua873d642014-04-15 21:31:33 -070028void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080029onValidated2(const ndn::shared_ptr<const ndn::Data>& data)
Yingdi Yu0eee6002014-02-11 15:54:17 -080030{
31 BOOST_CHECK(false);
32}
33
Yingdi Yua873d642014-04-15 21:31:33 -070034void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080035onValidationFailed2(const ndn::shared_ptr<const ndn::Data>& data,
36 const std::string& failureInfo)
Yingdi Yu0eee6002014-02-11 15:54:17 -080037{
38 BOOST_CHECK(true);
39}
40
Yingdi Yua873d642014-04-15 21:31:33 -070041void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080042publishData(const uint8_t* buf, size_t len, int freshness)
43{
44}
45
Yingdi Yu0eee6002014-02-11 15:54:17 -080046BOOST_AUTO_TEST_CASE (Graph)
47{
48 using namespace Sync;
49 using namespace ndn;
50
51 Name prefix("/Sync/TestSyncValidator/AddEdge");
52 KeyChain keychain;
53
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070054 Name identity1("/TestSyncValidator/AddEdge-1/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -080055 Name certName1 = keychain.createIdentity(identity1);
56 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
57
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070058 Name identity2("/TestSyncValidator/AddEdge-2/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -080059 Name certName2 = keychain.createIdentity(identity2);
60 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
61
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070062 Name identity3("/TestSyncValidator/AddEdge-3/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -080063 Name certName3 = keychain.createIdentity(identity3);
64 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
65
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070066 Name identity4("/TestSyncValidator/AddEdge-4/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -080067 Name certName4 = keychain.createIdentity(identity4);
68 shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4);
69
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070070 Name identity5("/TestSyncValidator/AddEdge-5/" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -080071 Name certName5 = keychain.createIdentity(identity5);
72 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5);
73
74 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
Alexander Afanasyev7fe59832014-07-02 12:17:46 -070075 shared_ptr<Face> face = make_shared<Face>(ref(*ioService));
Yingdi Yu3da10fe2014-02-27 16:37:34 -080076 shared_ptr<SecRuleRelative> rule;
Yingdi Yua873d642014-04-15 21:31:33 -070077 SyncValidator validator(prefix, *anchor, *face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080078 bind(&publishData, _1, _2, _3),
79 rule);
Yingdi Yu0eee6002014-02-11 15:54:17 -080080
81 validator.addParticipant(*introducer);
82 BOOST_CHECK(validator.canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -070083
Yingdi Yu0eee6002014-02-11 15:54:17 -080084 IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1));
85 keychain.sign(introCert, certName2);
86 validator.addParticipant(introCert);
87 BOOST_CHECK(validator.canTrust(certName3));
88
89 IntroCertificate introCert1(prefix, *anchor, certName3.getPrefix(-1));
90 keychain.sign(introCert1, certName3);
91 validator.addParticipant(introCert1);
92 validator.setAnchor(*introducer);
93 BOOST_CHECK(validator.canTrust(certName2));
94 BOOST_CHECK(validator.canTrust(certName3));
95 BOOST_CHECK(validator.canTrust(certName1));
96
97 IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1));
98 keychain.sign(introCert2, certName4);
99 validator.addParticipant(introCert2);
100 BOOST_CHECK(validator.canTrust(certName5) == false);
101 BOOST_CHECK(validator.canTrust(certName4) == false);
102
103 IntroCertificate introCert3(prefix, *introducee, certName5.getPrefix(-1));
104 keychain.sign(introCert3, certName5);
105 validator.addParticipant(introCert3);
106 BOOST_CHECK(validator.canTrust(certName5) == false);
107 BOOST_CHECK(validator.canTrust(certName4) == false);
108
109 validator.setAnchor(*introducee2);
110 BOOST_CHECK(validator.canTrust(certName1));
111 BOOST_CHECK(validator.canTrust(certName2));
112 BOOST_CHECK(validator.canTrust(certName3));
113 BOOST_CHECK(validator.canTrust(certName4) == false);
114 BOOST_CHECK(validator.canTrust(certName5));
Yingdi Yua873d642014-04-15 21:31:33 -0700115
Yingdi Yu0eee6002014-02-11 15:54:17 -0800116
117 keychain.deleteIdentity(identity1);
118 keychain.deleteIdentity(identity2);
119 keychain.deleteIdentity(identity3);
120 keychain.deleteIdentity(identity4);
121 keychain.deleteIdentity(identity5);
122}
123
124BOOST_AUTO_TEST_CASE (OfflineValidate)
125{
126 using namespace Sync;
127 using namespace ndn;
128
129 Name prefix("/Sync/TestSyncValidator/OfflineValidate");
130 KeyChain keychain;
131
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700132 Name identity1("/TestSyncValidator/OfflineValidate-1/"
133 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800134 Name certName1 = keychain.createIdentity(identity1);
135 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
136
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700137 Name identity2("/TestSyncValidator/OfflineValidate-2/"
138 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800139 Name certName2 = keychain.createIdentity(identity2);
140 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
141
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700142 Name identity3("/TestSyncValidator/OfflineValidate-3/"
143 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800144 Name certName3 = keychain.createIdentity(identity3);
145 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
146
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700147 Name identity4("/TestSyncValidator/OfflineValidate-4/"
148 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800149 Name certName4 = keychain.createIdentity(identity4);
150 shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4);
151
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700152 Name identity5("/TestSyncValidator/OfflineValidate-5/"
153 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800154 Name certName5 = keychain.createIdentity(identity5);
155 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5);
156
157 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700158 shared_ptr<Face> face = make_shared<Face>(ref(*ioService));
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800159 shared_ptr<SecRuleRelative> rule;
Yingdi Yua873d642014-04-15 21:31:33 -0700160 SyncValidator validator(prefix, *anchor, *face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800161 bind(&publishData, _1, _2, _3),
162 rule);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800163
164 validator.addParticipant(*introducer);
165 BOOST_CHECK(validator.canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -0700166
Yingdi Yu0eee6002014-02-11 15:54:17 -0800167 IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1));
168 keychain.sign(introCert, certName2);
169 validator.addParticipant(introCert);
170 BOOST_CHECK(validator.canTrust(certName3));
171
172 IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1));
173 keychain.sign(introCert2, certName4);
174 validator.addParticipant(introCert2);
175 BOOST_CHECK(validator.canTrust(certName5) == false);
176 BOOST_CHECK(validator.canTrust(certName4) == false);
177
178 validator.setAnchor(*introducer2);
179 BOOST_CHECK(validator.canTrust(certName1) == false);
180 BOOST_CHECK(validator.canTrust(certName2) == false);
181 BOOST_CHECK(validator.canTrust(certName3) == false);
182 BOOST_CHECK(validator.canTrust(certName4));
183 BOOST_CHECK(validator.canTrust(certName5));
184
185 Name dataName1 = prefix;
186 dataName1.append("data-1");
187 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
188 keychain.sign(*data1, certName5);
189
190 validator.validate(*data1,
191 bind(&onValidated, _1),
192 bind(&onValidationFailed, _1, _2));
193
194 Name dataName2 = prefix;
195 dataName2.append("data-2");
196 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
197 keychain.sign(*data2, certName1);
198
199 validator.validate(*data2,
200 bind(&onValidated2, _1),
201 bind(&onValidationFailed2, _1, _2));
202
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700203 // ioService->run();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800204
205 keychain.deleteIdentity(identity1);
206 keychain.deleteIdentity(identity2);
207 keychain.deleteIdentity(identity3);
208 keychain.deleteIdentity(identity4);
209 keychain.deleteIdentity(identity5);
210}
211
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700212struct FacesFixture
213{
214 FacesFixture()
215 : regPrefixId(0)
216 , regPrefixId2(0)
217 {}
Yingdi Yua873d642014-04-15 21:31:33 -0700218
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700219 void
220 onInterest(ndn::shared_ptr<ndn::Face> face, ndn::shared_ptr<ndn::Data> data)
221 {
222 face->put(*data);
223 face->unsetInterestFilter(regPrefixId);
224 }
225
226 void
227 onInterest2(ndn::shared_ptr<ndn::Face> face, ndn::shared_ptr<ndn::Data> data)
228 {
229 face->put(*data);
230 face->unsetInterestFilter(regPrefixId2);
231 }
232
233 void
234 onRegFailed()
235 {}
236
237 void
Yingdi Yua873d642014-04-15 21:31:33 -0700238 validate(ndn::shared_ptr<Sync::SyncValidator> validator, ndn::shared_ptr<ndn::Data> data,
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700239 const ndn::Name& certName3, const ndn::Name& certName4)
240 {
241 validator->validate(*data,
242 bind(&onValidated, _1),
243 bind(&onValidationFailed, _1, _2));
244
245
246 BOOST_CHECK(validator->canTrust(certName3));
247 BOOST_CHECK(validator->canTrust(certName4));
248 }
249
250 void
251 terminate(ndn::shared_ptr<ndn::Face> face)
252 {
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700253 face->getIoService().stop();
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700254 }
255
256 const ndn::RegisteredPrefixId* regPrefixId;
257 const ndn::RegisteredPrefixId* regPrefixId2;
258};
259
260BOOST_FIXTURE_TEST_CASE(OnlineValidate, FacesFixture)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800261{
262 using namespace Sync;
263 using namespace ndn;
264
265 Name prefix("/Sync/TestSyncValidator/OnlineValidate");
266 KeyChain keychain;
267
Yingdi Yua873d642014-04-15 21:31:33 -0700268 Name identity1("/TestSyncValidator/OnlineValidate-1/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700269 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800270 Name certName1 = keychain.createIdentity(identity1);
271 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
272
Yingdi Yua873d642014-04-15 21:31:33 -0700273 Name identity2("/TestSyncValidator/OnlineValidate-2/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700274 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800275 Name certName2 = keychain.createIdentity(identity2);
276 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
277
Yingdi Yua873d642014-04-15 21:31:33 -0700278 Name identity3("/TestSyncValidator/OnlineValidate-3/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700279 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800280 Name certName3 = keychain.createIdentity(identity3);
281 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
282
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700283 Name identity4("/TestSyncValidator/OnlineValidate-4/"
284 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800285 Name certName4 = keychain.createIdentity(identity4);
286 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName4);
287
288 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
Alexander Afanasyev7fe59832014-07-02 12:17:46 -0700289 shared_ptr<Face> face = make_shared<Face>(ref(*ioService));
290 shared_ptr<Face> face2 = make_shared<Face>(ref(*ioService));
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700291
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800292 shared_ptr<SecRuleRelative> rule;
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700293 shared_ptr<SyncValidator> validator = shared_ptr<SyncValidator>
Yingdi Yua873d642014-04-15 21:31:33 -0700294 (new SyncValidator(prefix, *anchor, *face2, bind(&publishData, _1, _2, _3), rule));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800295
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700296 validator->addParticipant(*introducer);
297 BOOST_CHECK(validator->canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -0700298
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700299 shared_ptr<IntroCertificate> introCert = shared_ptr<IntroCertificate>(new IntroCertificate(prefix, *introducee, certName2.getPrefix(-1)));
300 keychain.sign(*introCert, certName2);
301 BOOST_CHECK(validator->canTrust(certName3) == false);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800302
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700303 shared_ptr<IntroCertificate> introCert2 = shared_ptr<IntroCertificate>(new IntroCertificate(prefix, *introducee2, certName3.getPrefix(-1)));
304 keychain.sign(*introCert2, certName3);
305 BOOST_CHECK(validator->canTrust(certName4) == false);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800306
307 Name dataName1 = prefix;
308 dataName1.append("data-1");
309 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
310 keychain.sign(*data1, certName4);
311
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700312 ndn::Scheduler scheduler(*ioService);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800313
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700314 scheduler.scheduleEvent(time::seconds(1),
315 bind(&FacesFixture::terminate, this, face));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800316
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700317 regPrefixId = face->setInterestFilter(introCert->getName().getPrefix(-1),
318 bind(&FacesFixture::onInterest, this, face, introCert),
319 bind(&FacesFixture::onRegFailed, this));
Yingdi Yua873d642014-04-15 21:31:33 -0700320
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700321 regPrefixId2 = face->setInterestFilter(introCert2->getName().getPrefix(-1),
322 bind(&FacesFixture::onInterest2, this, face, introCert2),
323 bind(&FacesFixture::onRegFailed, this));
324
325 scheduler.scheduleEvent(time::milliseconds(200),
326 bind(&FacesFixture::validate, this,
327 validator, data1, certName3, certName4));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800328
329 keychain.deleteIdentity(identity1);
330 keychain.deleteIdentity(identity2);
331 keychain.deleteIdentity(identity3);
332 keychain.deleteIdentity(identity4);
333}
334
335BOOST_AUTO_TEST_SUITE_END()