blob: 19deaf6f51f4f21d529f187b11522a2009b6cc06 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "update/prefix-update-processor.hpp"
23
24#include "../control-commands.hpp"
25#include "../test-common.hpp"
26#include "nlsr.hpp"
27
28#include <ndn-cxx/interest.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000029#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
30#include <ndn-cxx/mgmt/nfd/control-response.hpp>
alvy297f4162015-03-03 17:15:33 -060031#include <ndn-cxx/security/key-chain.hpp>
32#include <ndn-cxx/util/dummy-client-face.hpp>
33
34#include <boost/filesystem.hpp>
35
36using namespace ndn;
37
38namespace nlsr {
39namespace update {
40namespace test {
41
Ashlesh Gawande415676b2016-12-22 00:26:23 -060042class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060043{
44public:
45 PrefixUpdateFixture()
Ashlesh Gawande415676b2016-12-22 00:26:23 -060046 : face(g_ioService, keyChain, {true, true})
alvy297f4162015-03-03 17:15:33 -060047 , siteIdentity(ndn::Name("/ndn/edu/test-site").appendVersion())
48 , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator")).appendVersion())
Ashlesh Gawande415676b2016-12-22 00:26:23 -060049 , nlsr(g_ioService, g_scheduler, face, g_keyChain)
alvy297f4162015-03-03 17:15:33 -060050 , keyPrefix(("/ndn/broadcast"))
Laqin Fan54a43f02017-03-08 12:31:30 -060051 , namePrefixList(nlsr.getNamePrefixList())
52 , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
alvy297f4162015-03-03 17:15:33 -060053 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
54 {
55 createSiteCert();
56 BOOST_REQUIRE(siteCert != nullptr);
57
58 createOperatorCert();
59 BOOST_REQUIRE(opCert != nullptr);
60
61 const std::string CONFIG =
62 "rule\n"
63 "{\n"
64 " id \"NLSR ControlCommand Rule\"\n"
65 " for interest\n"
66 " filter\n"
67 " {\n"
68 " type name\n"
69 " regex ^<localhost><nlsr><prefix-update>[<advertise><withdraw>]<>$\n"
70 " }\n"
71 " checker\n"
72 " {\n"
73 " type customized\n"
74 " sig-type rsa-sha256\n"
75 " key-locator\n"
76 " {\n"
77 " type name\n"
78 " regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><ksk-.*><ID-CERT>$\n"
79 " }\n"
80 " }\n"
81 "}\n"
82 "rule\n"
83 "{\n"
84 " id \"NLSR Hierarchy Rule\"\n"
85 " for data\n"
86 " filter\n"
87 " {\n"
88 " type name\n"
89 " regex ^[^<KEY>]*<KEY><ksk-.*><ID-CERT><>$\n"
90 " }\n"
91 " checker\n"
92 " {\n"
93 " type hierarchical\n"
94 " sig-type rsa-sha256\n"
95 " }\n"
96 "}\n"
97 "trust-anchor\n"
98 "{\n"
99 " type file\n"
100 " file-name \"site.cert\"\n"
101 "}\n";
102
103 const boost::filesystem::path CONFIG_PATH =
104 (boost::filesystem::current_path() / std::string("unit-test.conf"));
105
Laqin Fan54a43f02017-03-08 12:31:30 -0600106 updatePrefixUpdateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native());
alvy297f4162015-03-03 17:15:33 -0600107
108 // Insert certs after the validator is loaded since ValidatorConfig::load() clears
109 // the certificate cache
110 nlsr.addCertificateToCache(opCert);
111
112 // Set the network so the LSA prefix is constructed
113 nlsr.getConfParameter().setNetwork("/ndn");
114
115 // Initialize NLSR so a sync socket is created
116 nlsr.initialize();
117
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600118 // Saving clock::now before any advanceClocks so that it will
119 // be the same value as what ChronoSync uses in setting the sessionName
120 sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
alvy297f4162015-03-03 17:15:33 -0600121
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600122 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -0600123
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600124 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600125 }
126
127 void
128 createSiteCert()
129 {
130 // Site cert
131 keyChain.createIdentity(siteIdentity);
132 siteCertName = keyChain.getDefaultCertificateNameForIdentity(siteIdentity);
133 siteCert = keyChain.getCertificate(siteCertName);
134
135 ndn::io::save(*siteCert, SITE_CERT_PATH.string());
136 }
137
138 void
139 createOperatorCert()
140 {
141 // Operator cert
142 ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(opIdentity, true);
143
dmcoomes9f936662017-03-02 10:33:09 -0600144 opCert = std::make_shared<ndn::IdentityCertificate>();
145 std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
alvy297f4162015-03-03 17:15:33 -0600146 opCertName = keyName.getPrefix(-1);
147 opCertName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
148 opCert->setName(opCertName);
149 opCert->setNotBefore(time::system_clock::now() - time::days(1));
150 opCert->setNotAfter(time::system_clock::now() + time::days(1));
151 opCert->setPublicKeyInfo(*pubKey);
Alexander Afanasyev4c2bb362017-03-14 12:29:19 -0700152 opCert->addSubjectDescription(ndn::security::v1::CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
153 keyName.toUri()));
alvy297f4162015-03-03 17:15:33 -0600154 opCert->encode();
155
156 keyChain.signByIdentity(*opCert, siteIdentity);
157
158 keyChain.addCertificateAsIdentityDefault(*opCert);
159 }
160
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600161 void sendInterestForPublishedData() {
162 // Need to send an interest now since ChronoSync
163 // no longer does face->put(*data) in publishData.
164 // Instead it does it in onInterest
165 ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA");
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500166 lsaInterestName.append(NameLsa::TYPE_STRING);
167
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600168 // The part after LSA is Chronosync getSession
169 lsaInterestName.append(sessionTime);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500170 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
171
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600172 shared_ptr<Interest> lsaInterest = make_shared<Interest>(lsaInterestName);
173
174 face.receive(*lsaInterest);
175 this->advanceClocks(ndn::time::milliseconds(10));
176 }
177
alvy297f4162015-03-03 17:15:33 -0600178 bool
179 wasRoutingUpdatePublished()
180 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600181 sendInterestForPublishedData();
182
alvy297f4162015-03-03 17:15:33 -0600183 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
184
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600185 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600186 [lsaPrefix] (const ndn::Data& data) {
187 return lsaPrefix.isPrefixOf(data.getName());
188 });
189
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600190 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600191 }
192
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500193 void
194 checkResponseCode(const Name& commandPrefix, uint64_t expectedCode)
195 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600196 std::vector<Data>::iterator it = std::find_if(face.sentData.begin(),
197 face.sentData.end(),
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500198 [commandPrefix] (const Data& data) {
199 return commandPrefix.isPrefixOf(data.getName());
200 });
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600201 BOOST_REQUIRE(it != face.sentData.end());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500202
203 ndn::nfd::ControlResponse response(it->getContent().blockFromValue());
204 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
205 }
206
alvy297f4162015-03-03 17:15:33 -0600207 ~PrefixUpdateFixture()
208 {
209 keyChain.deleteIdentity(siteIdentity);
210 keyChain.deleteIdentity(opIdentity);
211
212 boost::filesystem::remove(SITE_CERT_PATH);
213 }
214
215public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600216 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600217 ndn::KeyChain keyChain;
218
219 ndn::Name siteIdentity;
220 ndn::Name siteCertName;
dmcoomes9f936662017-03-02 10:33:09 -0600221 std::shared_ptr<IdentityCertificate> siteCert;
alvy297f4162015-03-03 17:15:33 -0600222
223 ndn::Name opIdentity;
224 ndn::Name opCertName;
dmcoomes9f936662017-03-02 10:33:09 -0600225 std::shared_ptr<IdentityCertificate> opCert;
alvy297f4162015-03-03 17:15:33 -0600226
227 Nlsr nlsr;
228 ndn::Name keyPrefix;
Laqin Fan54a43f02017-03-08 12:31:30 -0600229 NamePrefixList& namePrefixList;
230 PrefixUpdateProcessor& updatePrefixUpdateProcessor;
alvy297f4162015-03-03 17:15:33 -0600231
232 const boost::filesystem::path SITE_CERT_PATH;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600233 ndn::Name sessionTime;
alvy297f4162015-03-03 17:15:33 -0600234};
235
236BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
237
238BOOST_AUTO_TEST_CASE(Basic)
239{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500240 uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600241 // Advertise
242 ndn::nfd::ControlParameters parameters;
243 parameters.setName("/prefix/to/advertise/");
alvy297f4162015-03-03 17:15:33 -0600244 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
245 advertiseCommand.append(parameters.wireEncode());
246
dmcoomes9f936662017-03-02 10:33:09 -0600247 std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand);
alvy297f4162015-03-03 17:15:33 -0600248 keyChain.signByIdentity(*advertiseInterest, opIdentity);
249
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600250 face.receive(*advertiseInterest);
251 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600252
253 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
254
255 BOOST_REQUIRE_EQUAL(namePrefixList.getSize(), 1);
256 BOOST_CHECK_EQUAL(namePrefixList.getNameList().front(), parameters.getName());
257
258 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500259 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600260
261 face.sentData.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500262 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600263
264 // Withdraw
265 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
266 withdrawCommand.append(parameters.wireEncode());
267
dmcoomes9f936662017-03-02 10:33:09 -0600268 std::shared_ptr<Interest> withdrawInterest = std::make_shared<Interest>(withdrawCommand);
alvy297f4162015-03-03 17:15:33 -0600269 keyChain.signByIdentity(*withdrawInterest, opIdentity);
270
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600271 face.receive(*withdrawInterest);
272 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600273
274 BOOST_CHECK_EQUAL(namePrefixList.getSize(), 0);
275
276 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500277 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600278}
279
alvy297f4162015-03-03 17:15:33 -0600280BOOST_AUTO_TEST_SUITE_END()
281
282} // namespace test
283} // namespace update
284} // namespace nlsr