blob: 70d18c941c703587fe2be9d9d45a6d75d8538bdf [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -06003 * Copyright (c) 2014-2018, 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();
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600120 // Otherwise code coverage node fails with default 60 seconds lifetime
121 nlsr.getConfParameter().setSyncInterestLifetime(1000);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122
123 addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router"));
alvy297f4162015-03-03 17:15:33 -0600124
125 // Initialize NLSR so a sync socket is created
126 nlsr.initialize();
127
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600128 // Saving clock::now before any advanceClocks so that it will
129 // be the same value as what ChronoSync uses in setting the sessionName
130 sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
alvy297f4162015-03-03 17:15:33 -0600131
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600132 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -0600133
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600134 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600135 }
136
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500137 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -0600138 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600139 // Need to send an interest now since ChronoSync
140 // no longer does face->put(*data) in publishData.
141 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500142 ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix();
143 lsaInterestName.append(nlsr.getConfParameter().getSiteName());
144 lsaInterestName.append(nlsr.getConfParameter().getRouterName());
Nick Gordon727d4832017-10-13 18:04:25 -0500145 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500146
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600147 // The part after LSA is Chronosync getSession
148 lsaInterestName.append(sessionTime);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500149 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
150
Nick Gordone98480b2017-05-24 11:23:03 -0500151 std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600152
153 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600155 }
156
alvy297f4162015-03-03 17:15:33 -0600157 bool
158 wasRoutingUpdatePublished()
159 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600160 sendInterestForPublishedData();
161
alvy297f4162015-03-03 17:15:33 -0600162 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
163
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600164 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600165 [lsaPrefix] (const ndn::Data& data) {
166 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 }
168 );
alvy297f4162015-03-03 17:15:33 -0600169
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600170 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600171 }
172
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500173 void
174 checkResponseCode(const Name& commandPrefix, uint64_t expectedCode)
175 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600176 std::vector<Data>::iterator it = std::find_if(face.sentData.begin(),
177 face.sentData.end(),
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500178 [commandPrefix] (const Data& data) {
179 return commandPrefix.isPrefixOf(data.getName());
180 });
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600181 BOOST_REQUIRE(it != face.sentData.end());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500182
183 ndn::nfd::ControlResponse response(it->getContent().blockFromValue());
184 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
185 }
186
alvy297f4162015-03-03 17:15:33 -0600187public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600188 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600189
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500190 ndn::Name siteIdentityName;
191 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600192
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500193 ndn::Name opIdentityName;
194 ndn::security::pib::Identity opIdentity;
alvy297f4162015-03-03 17:15:33 -0600195
196 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600197 NamePrefixList& namePrefixList;
198 PrefixUpdateProcessor& updatePrefixUpdateProcessor;
alvy297f4162015-03-03 17:15:33 -0600199
200 const boost::filesystem::path SITE_CERT_PATH;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600201 ndn::Name sessionTime;
alvy297f4162015-03-03 17:15:33 -0600202};
203
204BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
205
206BOOST_AUTO_TEST_CASE(Basic)
207{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500208 uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500209
alvy297f4162015-03-03 17:15:33 -0600210 ndn::nfd::ControlParameters parameters;
211 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500212
213 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
214 // /<timestamp>/<random-value>/<signed-interests-components>
215
216 // Advertise
alvy297f4162015-03-03 17:15:33 -0600217 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500218
219 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600220 advertiseCommand.append(parameters.wireEncode());
221
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500222 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600223
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500224 // CommandInterestSigner::makeCommandInterest() will append the last
225 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
226 ndn::Interest advertiseInterest =
227 cis.makeCommandInterest(advertiseCommand,
228 ndn::security::signingByIdentity(opIdentity));
229
230 face.receive(advertiseInterest);
231
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600232 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600233
234 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
235
Nick Gordonff9a6272017-10-12 13:38:29 -0500236 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500237 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600238
239 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500240 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600241
242 face.sentData.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500243 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600244
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500245 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600246 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
247 withdrawCommand.append(parameters.wireEncode());
248
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500249 ndn::Interest withdrawInterest
250 = cis.makeCommandInterest(withdrawCommand,
251 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600252
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500253 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600254 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600255
Nick Gordonff9a6272017-10-12 13:38:29 -0500256 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600257
258 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500259 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600260}
261
alvy297f4162015-03-03 17:15:33 -0600262BOOST_AUTO_TEST_SUITE_END()
263
264} // namespace test
265} // namespace update
266} // namespace nlsr