blob: ec5071783eb7cd765f4460dbf63d43476e0348f3 [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
9BOOST_AUTO_TEST_SUITE(TestSyncValidator)
10
Yingdi Yua873d642014-04-15 21:31:33 -070011void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080012onValidated(const ndn::shared_ptr<const ndn::Data>& data)
Yingdi Yu0eee6002014-02-11 15:54:17 -080013{
14 BOOST_CHECK(true);
15}
16
Yingdi Yua873d642014-04-15 21:31:33 -070017void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080018onValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
19 const std::string& failureInfo)
Yingdi Yu0eee6002014-02-11 15:54:17 -080020{
21 BOOST_CHECK(false);
22}
23
Yingdi Yua873d642014-04-15 21:31:33 -070024void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080025onValidated2(const ndn::shared_ptr<const ndn::Data>& data)
Yingdi Yu0eee6002014-02-11 15:54:17 -080026{
27 BOOST_CHECK(false);
28}
29
Yingdi Yua873d642014-04-15 21:31:33 -070030void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080031onValidationFailed2(const ndn::shared_ptr<const ndn::Data>& data,
32 const std::string& failureInfo)
Yingdi Yu0eee6002014-02-11 15:54:17 -080033{
34 BOOST_CHECK(true);
35}
36
Yingdi Yua873d642014-04-15 21:31:33 -070037void
Yingdi Yu3da10fe2014-02-27 16:37:34 -080038publishData(const uint8_t* buf, size_t len, int freshness)
39{
40}
41
Yingdi Yu0eee6002014-02-11 15:54:17 -080042BOOST_AUTO_TEST_CASE (Graph)
43{
44 using namespace Sync;
45 using namespace ndn;
46
47 Name prefix("/Sync/TestSyncValidator/AddEdge");
48 KeyChain keychain;
49
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070050 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 -080051 Name certName1 = keychain.createIdentity(identity1);
52 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
53
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070054 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 -080055 Name certName2 = keychain.createIdentity(identity2);
56 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
57
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070058 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 -080059 Name certName3 = keychain.createIdentity(identity3);
60 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
61
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070062 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 -080063 Name certName4 = keychain.createIdentity(identity4);
64 shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4);
65
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -070066 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 -080067 Name certName5 = keychain.createIdentity(identity5);
68 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5);
69
70 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
71 shared_ptr<Face> face = make_shared<Face>(ioService);
Yingdi Yu3da10fe2014-02-27 16:37:34 -080072 shared_ptr<SecRuleRelative> rule;
Yingdi Yua873d642014-04-15 21:31:33 -070073 SyncValidator validator(prefix, *anchor, *face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080074 bind(&publishData, _1, _2, _3),
75 rule);
Yingdi Yu0eee6002014-02-11 15:54:17 -080076
77 validator.addParticipant(*introducer);
78 BOOST_CHECK(validator.canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -070079
Yingdi Yu0eee6002014-02-11 15:54:17 -080080 IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1));
81 keychain.sign(introCert, certName2);
82 validator.addParticipant(introCert);
83 BOOST_CHECK(validator.canTrust(certName3));
84
85 IntroCertificate introCert1(prefix, *anchor, certName3.getPrefix(-1));
86 keychain.sign(introCert1, certName3);
87 validator.addParticipant(introCert1);
88 validator.setAnchor(*introducer);
89 BOOST_CHECK(validator.canTrust(certName2));
90 BOOST_CHECK(validator.canTrust(certName3));
91 BOOST_CHECK(validator.canTrust(certName1));
92
93 IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1));
94 keychain.sign(introCert2, certName4);
95 validator.addParticipant(introCert2);
96 BOOST_CHECK(validator.canTrust(certName5) == false);
97 BOOST_CHECK(validator.canTrust(certName4) == false);
98
99 IntroCertificate introCert3(prefix, *introducee, certName5.getPrefix(-1));
100 keychain.sign(introCert3, certName5);
101 validator.addParticipant(introCert3);
102 BOOST_CHECK(validator.canTrust(certName5) == false);
103 BOOST_CHECK(validator.canTrust(certName4) == false);
104
105 validator.setAnchor(*introducee2);
106 BOOST_CHECK(validator.canTrust(certName1));
107 BOOST_CHECK(validator.canTrust(certName2));
108 BOOST_CHECK(validator.canTrust(certName3));
109 BOOST_CHECK(validator.canTrust(certName4) == false);
110 BOOST_CHECK(validator.canTrust(certName5));
Yingdi Yua873d642014-04-15 21:31:33 -0700111
Yingdi Yu0eee6002014-02-11 15:54:17 -0800112
113 keychain.deleteIdentity(identity1);
114 keychain.deleteIdentity(identity2);
115 keychain.deleteIdentity(identity3);
116 keychain.deleteIdentity(identity4);
117 keychain.deleteIdentity(identity5);
118}
119
120BOOST_AUTO_TEST_CASE (OfflineValidate)
121{
122 using namespace Sync;
123 using namespace ndn;
124
125 Name prefix("/Sync/TestSyncValidator/OfflineValidate");
126 KeyChain keychain;
127
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700128 Name identity1("/TestSyncValidator/OfflineValidate-1/"
129 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800130 Name certName1 = keychain.createIdentity(identity1);
131 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
132
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700133 Name identity2("/TestSyncValidator/OfflineValidate-2/"
134 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800135 Name certName2 = keychain.createIdentity(identity2);
136 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
137
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700138 Name identity3("/TestSyncValidator/OfflineValidate-3/"
139 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800140 Name certName3 = keychain.createIdentity(identity3);
141 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
142
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700143 Name identity4("/TestSyncValidator/OfflineValidate-4/"
144 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800145 Name certName4 = keychain.createIdentity(identity4);
146 shared_ptr<IdentityCertificate> introducer2 = keychain.getCertificate(certName4);
147
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700148 Name identity5("/TestSyncValidator/OfflineValidate-5/"
149 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800150 Name certName5 = keychain.createIdentity(identity5);
151 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName5);
152
153 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
154 shared_ptr<Face> face = make_shared<Face>(ioService);
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800155 shared_ptr<SecRuleRelative> rule;
Yingdi Yua873d642014-04-15 21:31:33 -0700156 SyncValidator validator(prefix, *anchor, *face,
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800157 bind(&publishData, _1, _2, _3),
158 rule);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800159
160 validator.addParticipant(*introducer);
161 BOOST_CHECK(validator.canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -0700162
Yingdi Yu0eee6002014-02-11 15:54:17 -0800163 IntroCertificate introCert(prefix, *introducee, certName2.getPrefix(-1));
164 keychain.sign(introCert, certName2);
165 validator.addParticipant(introCert);
166 BOOST_CHECK(validator.canTrust(certName3));
167
168 IntroCertificate introCert2(prefix, *introducee2, certName4.getPrefix(-1));
169 keychain.sign(introCert2, certName4);
170 validator.addParticipant(introCert2);
171 BOOST_CHECK(validator.canTrust(certName5) == false);
172 BOOST_CHECK(validator.canTrust(certName4) == false);
173
174 validator.setAnchor(*introducer2);
175 BOOST_CHECK(validator.canTrust(certName1) == false);
176 BOOST_CHECK(validator.canTrust(certName2) == false);
177 BOOST_CHECK(validator.canTrust(certName3) == false);
178 BOOST_CHECK(validator.canTrust(certName4));
179 BOOST_CHECK(validator.canTrust(certName5));
180
181 Name dataName1 = prefix;
182 dataName1.append("data-1");
183 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
184 keychain.sign(*data1, certName5);
185
186 validator.validate(*data1,
187 bind(&onValidated, _1),
188 bind(&onValidationFailed, _1, _2));
189
190 Name dataName2 = prefix;
191 dataName2.append("data-2");
192 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
193 keychain.sign(*data2, certName1);
194
195 validator.validate(*data2,
196 bind(&onValidated2, _1),
197 bind(&onValidationFailed2, _1, _2));
198
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700199 // ioService->run();
Yingdi Yu0eee6002014-02-11 15:54:17 -0800200
201 keychain.deleteIdentity(identity1);
202 keychain.deleteIdentity(identity2);
203 keychain.deleteIdentity(identity3);
204 keychain.deleteIdentity(identity4);
205 keychain.deleteIdentity(identity5);
206}
207
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700208struct FacesFixture
209{
210 FacesFixture()
211 : regPrefixId(0)
212 , regPrefixId2(0)
213 {}
Yingdi Yua873d642014-04-15 21:31:33 -0700214
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700215 void
216 onInterest(ndn::shared_ptr<ndn::Face> face, ndn::shared_ptr<ndn::Data> data)
217 {
218 face->put(*data);
219 face->unsetInterestFilter(regPrefixId);
220 }
221
222 void
223 onInterest2(ndn::shared_ptr<ndn::Face> face, ndn::shared_ptr<ndn::Data> data)
224 {
225 face->put(*data);
226 face->unsetInterestFilter(regPrefixId2);
227 }
228
229 void
230 onRegFailed()
231 {}
232
233 void
Yingdi Yua873d642014-04-15 21:31:33 -0700234 validate(ndn::shared_ptr<Sync::SyncValidator> validator, ndn::shared_ptr<ndn::Data> data,
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700235 const ndn::Name& certName3, const ndn::Name& certName4)
236 {
237 validator->validate(*data,
238 bind(&onValidated, _1),
239 bind(&onValidationFailed, _1, _2));
240
241
242 BOOST_CHECK(validator->canTrust(certName3));
243 BOOST_CHECK(validator->canTrust(certName4));
244 }
245
246 void
247 terminate(ndn::shared_ptr<ndn::Face> face)
248 {
249 face->ioService()->stop();
250 }
251
252 const ndn::RegisteredPrefixId* regPrefixId;
253 const ndn::RegisteredPrefixId* regPrefixId2;
254};
255
256BOOST_FIXTURE_TEST_CASE(OnlineValidate, FacesFixture)
Yingdi Yu0eee6002014-02-11 15:54:17 -0800257{
258 using namespace Sync;
259 using namespace ndn;
260
261 Name prefix("/Sync/TestSyncValidator/OnlineValidate");
262 KeyChain keychain;
263
Yingdi Yua873d642014-04-15 21:31:33 -0700264 Name identity1("/TestSyncValidator/OnlineValidate-1/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700265 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800266 Name certName1 = keychain.createIdentity(identity1);
267 shared_ptr<IdentityCertificate> anchor = keychain.getCertificate(certName1);
268
Yingdi Yua873d642014-04-15 21:31:33 -0700269 Name identity2("/TestSyncValidator/OnlineValidate-2/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700270 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800271 Name certName2 = keychain.createIdentity(identity2);
272 shared_ptr<IdentityCertificate> introducer = keychain.getCertificate(certName2);
273
Yingdi Yua873d642014-04-15 21:31:33 -0700274 Name identity3("/TestSyncValidator/OnlineValidate-3/"
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700275 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800276 Name certName3 = keychain.createIdentity(identity3);
277 shared_ptr<IdentityCertificate> introducee = keychain.getCertificate(certName3);
278
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700279 Name identity4("/TestSyncValidator/OnlineValidate-4/"
280 + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800281 Name certName4 = keychain.createIdentity(identity4);
282 shared_ptr<IdentityCertificate> introducee2 = keychain.getCertificate(certName4);
283
284 shared_ptr<boost::asio::io_service> ioService = make_shared<boost::asio::io_service>();
285 shared_ptr<Face> face = make_shared<Face>(ioService);
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700286 shared_ptr<Face> face2 = make_shared<Face>(ioService);
287
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800288 shared_ptr<SecRuleRelative> rule;
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700289 shared_ptr<SyncValidator> validator = shared_ptr<SyncValidator>
Yingdi Yua873d642014-04-15 21:31:33 -0700290 (new SyncValidator(prefix, *anchor, *face2, bind(&publishData, _1, _2, _3), rule));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800291
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700292 validator->addParticipant(*introducer);
293 BOOST_CHECK(validator->canTrust(certName2));
Yingdi Yua873d642014-04-15 21:31:33 -0700294
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700295 shared_ptr<IntroCertificate> introCert = shared_ptr<IntroCertificate>(new IntroCertificate(prefix, *introducee, certName2.getPrefix(-1)));
296 keychain.sign(*introCert, certName2);
297 BOOST_CHECK(validator->canTrust(certName3) == false);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800298
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700299 shared_ptr<IntroCertificate> introCert2 = shared_ptr<IntroCertificate>(new IntroCertificate(prefix, *introducee2, certName3.getPrefix(-1)));
300 keychain.sign(*introCert2, certName3);
301 BOOST_CHECK(validator->canTrust(certName4) == false);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800302
303 Name dataName1 = prefix;
304 dataName1.append("data-1");
305 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
306 keychain.sign(*data1, certName4);
307
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700308 ndn::Scheduler scheduler(*ioService);
Yingdi Yu0eee6002014-02-11 15:54:17 -0800309
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700310 scheduler.scheduleEvent(time::seconds(1),
311 bind(&FacesFixture::terminate, this, face));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800312
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700313 regPrefixId = face->setInterestFilter(introCert->getName().getPrefix(-1),
314 bind(&FacesFixture::onInterest, this, face, introCert),
315 bind(&FacesFixture::onRegFailed, this));
Yingdi Yua873d642014-04-15 21:31:33 -0700316
Yingdi Yu6e1c9cd2014-03-25 10:26:54 -0700317 regPrefixId2 = face->setInterestFilter(introCert2->getName().getPrefix(-1),
318 bind(&FacesFixture::onInterest2, this, face, introCert2),
319 bind(&FacesFixture::onRegFailed, this));
320
321 scheduler.scheduleEvent(time::milliseconds(200),
322 bind(&FacesFixture::validate, this,
323 validator, data1, certName3, certName4));
Yingdi Yu0eee6002014-02-11 15:54:17 -0800324
325 keychain.deleteIdentity(identity1);
326 keychain.deleteIdentity(identity2);
327 keychain.deleteIdentity(identity3);
328 keychain.deleteIdentity(identity4);
329}
330
331BOOST_AUTO_TEST_SUITE_END()