blob: 4713eb3fc378dd8160c4c650d0e5ea69ff972340 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi008c9b12019-01-13 23:15:56 +00003 * Copyright (c) 2014-2019, 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"
Junxiao Shi008c9b12019-01-13 23:15:56 +000023#include "nlsr.hpp"
alvy297f4162015-03-03 17:15:33 -060024
25#include "../control-commands.hpp"
26#include "../test-common.hpp"
alvy297f4162015-03-03 17:15:33 -060027
Junxiao Shi3e5120c2016-09-10 16:58:34 +000028#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi008c9b12019-01-13 23:15:56 +000029#include <ndn-cxx/security/command-interest-signer.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050030#include <ndn-cxx/security/pib/identity.hpp>
31#include <ndn-cxx/security/signing-helpers.hpp>
alvy297f4162015-03-03 17:15:33 -060032
33#include <boost/filesystem.hpp>
34
35using namespace ndn;
36
37namespace nlsr {
38namespace update {
39namespace test {
40
Ashlesh Gawande415676b2016-12-22 00:26:23 -060041class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060042{
43public:
44 PrefixUpdateFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050045 : face(m_ioService, m_keyChain, {true, true})
46 , siteIdentityName(ndn::Name("/edu/test-site"))
47 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
48 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Laqin Fan54a43f02017-03-08 12:31:30 -060049 , namePrefixList(nlsr.getNamePrefixList())
50 , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
alvy297f4162015-03-03 17:15:33 -060051 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
52 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050053 // Site cert
54 siteIdentity = addIdentity(siteIdentityName);
55 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
alvy297f4162015-03-03 17:15:33 -060056
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 // Operator cert
58 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
alvy297f4162015-03-03 17:15:33 -060059
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 const std::string CONFIG = R"CONF(
61 rule
62 {
63 id "NLSR ControlCommand Rule"
64 for interest
65 filter
66 {
67 type name
68 regex ^(<localhost><nlsr>)<prefix-update>[<advertise><withdraw>]<><><>$
69 }
70 checker
71 {
72 type customized
73 sig-type rsa-sha256
74 key-locator
75 {
76 type name
77 regex ^<>*<KEY><>$
78 }
79 }
80 }
81 rule
82 {
83 id "NLSR Hierarchy Rule"
84 for data
85 filter
86 {
87 type name
88 regex ^[^<KEY>]*<KEY><><><>$
89 }
90 checker
91 {
92 type hierarchical
93 sig-type rsa-sha256
94 }
95 }
96 trust-anchor
97 {
98 type file
99 file-name "site.cert"
100 }
101 )CONF";
alvy297f4162015-03-03 17:15:33 -0600102
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
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500108 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
alvy297f4162015-03-03 17:15:33 -0600109
110 // Set the network so the LSA prefix is constructed
111 nlsr.getConfParameter().setNetwork("/ndn");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500112 nlsr.getConfParameter().setSiteName("/edu/test-site");
113 nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
114 nlsr.getConfParameter().buildRouterPrefix();
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600115 // Otherwise code coverage node fails with default 60 seconds lifetime
116 nlsr.getConfParameter().setSyncInterestLifetime(1000);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117
118 addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router"));
alvy297f4162015-03-03 17:15:33 -0600119
120 // Initialize NLSR so a sync socket is created
121 nlsr.initialize();
122
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600123 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -0600124
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600125 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600126 }
127
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500128 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -0600129 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600130 // Need to send an interest now since ChronoSync
131 // no longer does face->put(*data) in publishData.
132 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500133 ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix();
134 lsaInterestName.append(nlsr.getConfParameter().getSiteName());
135 lsaInterestName.append(nlsr.getConfParameter().getRouterName());
Nick Gordon727d4832017-10-13 18:04:25 -0500136 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500137
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500138 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
139
Junxiao Shi008c9b12019-01-13 23:15:56 +0000140 auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
141 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600142 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500143 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600144 }
145
alvy297f4162015-03-03 17:15:33 -0600146 bool
147 wasRoutingUpdatePublished()
148 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600149 sendInterestForPublishedData();
150
alvy297f4162015-03-03 17:15:33 -0600151 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
152
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600153 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600154 [lsaPrefix] (const ndn::Data& data) {
155 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156 }
157 );
alvy297f4162015-03-03 17:15:33 -0600158
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600159 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600160 }
161
alvy297f4162015-03-03 17:15:33 -0600162public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600163 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600164
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500165 ndn::Name siteIdentityName;
166 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600167
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500168 ndn::Name opIdentityName;
169 ndn::security::pib::Identity opIdentity;
alvy297f4162015-03-03 17:15:33 -0600170
171 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600172 NamePrefixList& namePrefixList;
173 PrefixUpdateProcessor& updatePrefixUpdateProcessor;
alvy297f4162015-03-03 17:15:33 -0600174
175 const boost::filesystem::path SITE_CERT_PATH;
176};
177
178BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
179
180BOOST_AUTO_TEST_CASE(Basic)
181{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500182 uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183
alvy297f4162015-03-03 17:15:33 -0600184 ndn::nfd::ControlParameters parameters;
185 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500186
187 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
188 // /<timestamp>/<random-value>/<signed-interests-components>
189
190 // Advertise
alvy297f4162015-03-03 17:15:33 -0600191 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192
193 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600194 advertiseCommand.append(parameters.wireEncode());
195
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600197
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500198 // CommandInterestSigner::makeCommandInterest() will append the last
199 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
200 ndn::Interest advertiseInterest =
201 cis.makeCommandInterest(advertiseCommand,
202 ndn::security::signingByIdentity(opIdentity));
203
204 face.receive(advertiseInterest);
205
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600206 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600207
208 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
209
Nick Gordonff9a6272017-10-12 13:38:29 -0500210 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500211 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600212
213 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500214 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600215
216 face.sentData.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500217 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600218
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500219 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600220 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
221 withdrawCommand.append(parameters.wireEncode());
222
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500223 ndn::Interest withdrawInterest
224 = cis.makeCommandInterest(withdrawCommand,
225 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600226
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500227 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600228 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600229
Nick Gordonff9a6272017-10-12 13:38:29 -0500230 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600231
232 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500233 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600234}
235
alvy297f4162015-03-03 17:15:33 -0600236BOOST_AUTO_TEST_SUITE_END()
237
238} // namespace test
239} // namespace update
240} // namespace nlsr