blob: 5bb61f1d7d7326e1a5ddca1c6e5c57c4eeb75b69 [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>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050032#include <ndn-cxx/security/pib/identity.hpp>
33#include <ndn-cxx/security/signing-helpers.hpp>
34#include <ndn-cxx/security/command-interest-signer.hpp>
alvy297f4162015-03-03 17:15:33 -060035#include <ndn-cxx/util/dummy-client-face.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050036#include <ndn-cxx/lp/tags.hpp>
alvy297f4162015-03-03 17:15:33 -060037
38#include <boost/filesystem.hpp>
39
40using namespace ndn;
41
42namespace nlsr {
43namespace update {
44namespace test {
45
Ashlesh Gawande415676b2016-12-22 00:26:23 -060046class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060047{
48public:
49 PrefixUpdateFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050050 : face(m_ioService, m_keyChain, {true, true})
51 , siteIdentityName(ndn::Name("/edu/test-site"))
52 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
53 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Laqin Fan54a43f02017-03-08 12:31:30 -060054 , namePrefixList(nlsr.getNamePrefixList())
55 , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
alvy297f4162015-03-03 17:15:33 -060056 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
57 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 // Site cert
59 siteIdentity = addIdentity(siteIdentityName);
60 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
alvy297f4162015-03-03 17:15:33 -060061
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050062 // Operator cert
63 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
alvy297f4162015-03-03 17:15:33 -060064
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 const std::string CONFIG = R"CONF(
66 rule
67 {
68 id "NLSR ControlCommand Rule"
69 for interest
70 filter
71 {
72 type name
73 regex ^(<localhost><nlsr>)<prefix-update>[<advertise><withdraw>]<><><>$
74 }
75 checker
76 {
77 type customized
78 sig-type rsa-sha256
79 key-locator
80 {
81 type name
82 regex ^<>*<KEY><>$
83 }
84 }
85 }
86 rule
87 {
88 id "NLSR Hierarchy Rule"
89 for data
90 filter
91 {
92 type name
93 regex ^[^<KEY>]*<KEY><><><>$
94 }
95 checker
96 {
97 type hierarchical
98 sig-type rsa-sha256
99 }
100 }
101 trust-anchor
102 {
103 type file
104 file-name "site.cert"
105 }
106 )CONF";
alvy297f4162015-03-03 17:15:33 -0600107
108 const boost::filesystem::path CONFIG_PATH =
109 (boost::filesystem::current_path() / std::string("unit-test.conf"));
110
Laqin Fan54a43f02017-03-08 12:31:30 -0600111 updatePrefixUpdateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native());
alvy297f4162015-03-03 17:15:33 -0600112
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
alvy297f4162015-03-03 17:15:33 -0600114
115 // Set the network so the LSA prefix is constructed
116 nlsr.getConfParameter().setNetwork("/ndn");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117 nlsr.getConfParameter().setSiteName("/edu/test-site");
118 nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
119 nlsr.getConfParameter().buildRouterPrefix();
120
121 addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router"));
alvy297f4162015-03-03 17:15:33 -0600122
123 // Initialize NLSR so a sync socket is created
124 nlsr.initialize();
125
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600126 // Saving clock::now before any advanceClocks so that it will
127 // be the same value as what ChronoSync uses in setting the sessionName
128 sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
alvy297f4162015-03-03 17:15:33 -0600129
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600130 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -0600131
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600132 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600133 }
134
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -0600136 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600137 // Need to send an interest now since ChronoSync
138 // no longer does face->put(*data) in publishData.
139 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500140 ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix();
141 lsaInterestName.append(nlsr.getConfParameter().getSiteName());
142 lsaInterestName.append(nlsr.getConfParameter().getRouterName());
Nick Gordon727d4832017-10-13 18:04:25 -0500143 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500144
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600145 // The part after LSA is Chronosync getSession
146 lsaInterestName.append(sessionTime);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500147 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
148
Nick Gordone98480b2017-05-24 11:23:03 -0500149 std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600150
151 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600153 }
154
alvy297f4162015-03-03 17:15:33 -0600155 bool
156 wasRoutingUpdatePublished()
157 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600158 sendInterestForPublishedData();
159
alvy297f4162015-03-03 17:15:33 -0600160 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
161
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600162 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600163 [lsaPrefix] (const ndn::Data& data) {
164 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500165 }
166 );
alvy297f4162015-03-03 17:15:33 -0600167
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600168 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600169 }
170
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500171 void
172 checkResponseCode(const Name& commandPrefix, uint64_t expectedCode)
173 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600174 std::vector<Data>::iterator it = std::find_if(face.sentData.begin(),
175 face.sentData.end(),
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500176 [commandPrefix] (const Data& data) {
177 return commandPrefix.isPrefixOf(data.getName());
178 });
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600179 BOOST_REQUIRE(it != face.sentData.end());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500180
181 ndn::nfd::ControlResponse response(it->getContent().blockFromValue());
182 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
183 }
184
alvy297f4162015-03-03 17:15:33 -0600185public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600186 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600187
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500188 ndn::Name siteIdentityName;
189 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600190
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191 ndn::Name opIdentityName;
192 ndn::security::pib::Identity opIdentity;
alvy297f4162015-03-03 17:15:33 -0600193
194 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600195 NamePrefixList& namePrefixList;
196 PrefixUpdateProcessor& updatePrefixUpdateProcessor;
alvy297f4162015-03-03 17:15:33 -0600197
198 const boost::filesystem::path SITE_CERT_PATH;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600199 ndn::Name sessionTime;
alvy297f4162015-03-03 17:15:33 -0600200};
201
202BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
203
204BOOST_AUTO_TEST_CASE(Basic)
205{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500206 uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500207
alvy297f4162015-03-03 17:15:33 -0600208 ndn::nfd::ControlParameters parameters;
209 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500210
211 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
212 // /<timestamp>/<random-value>/<signed-interests-components>
213
214 // Advertise
alvy297f4162015-03-03 17:15:33 -0600215 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500216
217 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600218 advertiseCommand.append(parameters.wireEncode());
219
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500220 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600221
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500222 // CommandInterestSigner::makeCommandInterest() will append the last
223 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
224 ndn::Interest advertiseInterest =
225 cis.makeCommandInterest(advertiseCommand,
226 ndn::security::signingByIdentity(opIdentity));
227
228 face.receive(advertiseInterest);
229
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600230 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600231
232 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
233
Nick Gordonff9a6272017-10-12 13:38:29 -0500234 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500235 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600236
237 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500238 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600239
240 face.sentData.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500241 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600242
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500243 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600244 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
245 withdrawCommand.append(parameters.wireEncode());
246
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500247 ndn::Interest withdrawInterest
248 = cis.makeCommandInterest(withdrawCommand,
249 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600250
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600252 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600253
Nick Gordonff9a6272017-10-12 13:38:29 -0500254 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600255
256 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500257 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600258}
259
alvy297f4162015-03-03 17:15:33 -0600260BOOST_AUTO_TEST_SUITE_END()
261
262} // namespace test
263} // namespace update
264} // namespace nlsr