blob: e5a58907d1424c3a9c7e88228f220c1b400b920f [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07002/*
3 * Copyright (c) 2014-2021, 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/>.
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070020 */
alvy297f4162015-03-03 17:15:33 -060021
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
Saurab Dulal427e0122019-11-28 11:58:02 -060025#include "tests/control-commands.hpp"
26#include "tests/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>
Ashlesh Gawande85998a12017-12-07 22:22:13 -060034#include <boost/property_tree/ptree.hpp>
35#include <boost/property_tree/info_parser.hpp>
alvy297f4162015-03-03 17:15:33 -060036
37using namespace ndn;
38
39namespace nlsr {
alvy297f4162015-03-03 17:15:33 -060040namespace 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()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050046 : face(m_ioService, m_keyChain, {true, true})
Ashlesh Gawande85998a12017-12-07 22:22:13 -060047 , siteIdentityName(ndn::Name("site"))
48 , opIdentityName(ndn::Name("site").append(ndn::Name("%C1.Operator")))
Saurab Dulal427e0122019-11-28 11:58:02 -060049 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060050 , confProcessor(conf)
51 , nlsr(face, m_keyChain, conf)
52 , namePrefixList(conf.getNamePrefixList())
alvy297f4162015-03-03 17:15:33 -060053 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
54 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055 // Site cert
56 siteIdentity = addIdentity(siteIdentityName);
57 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
alvy297f4162015-03-03 17:15:33 -060058
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059 // Operator cert
60 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
alvy297f4162015-03-03 17:15:33 -060061
Saurab Dulal427e0122019-11-28 11:58:02 -060062 // Create certificate and load it to the validator
63 conf.initializeKey();
64
65 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
66 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
67
Ashlesh Gawande85998a12017-12-07 22:22:13 -060068 std::ifstream inputFile;
69 inputFile.open(std::string("nlsr.conf"));
alvy297f4162015-03-03 17:15:33 -060070
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 BOOST_REQUIRE(inputFile.is_open());
alvy297f4162015-03-03 17:15:33 -060072
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 boost::property_tree::ptree pt;
74
75 boost::property_tree::read_info(inputFile, pt);
76 for (const auto& tn : pt) {
77 if (tn.first == "security") {
78 for (const auto& it : tn.second) {
79 if (it.first == "prefix-update-validator") {
80 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
81 }
82 }
83 }
84 }
85 inputFile.close();
alvy297f4162015-03-03 17:15:33 -060086
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087 addIdentity(conf.getRouterPrefix());
alvy297f4162015-03-03 17:15:33 -060088
Ashlesh Gawande415676b2016-12-22 00:26:23 -060089 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -060090
Ashlesh Gawande415676b2016-12-22 00:26:23 -060091 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -060092 }
93
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050094 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -060095 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060096 // Need to send an interest now since ChronoSync
97 // no longer does face->put(*data) in publishData.
98 // Instead it does it in onInterest
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099 ndn::Name lsaInterestName = conf.getLsaPrefix();
100 lsaInterestName.append(conf.getSiteName());
101 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800102 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500103
Ashlesh Gawande15052402018-12-12 20:20:00 -0600104 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500105
Junxiao Shi008c9b12019-01-13 23:15:56 +0000106 auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
107 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600108 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500109 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600110 }
111
alvy297f4162015-03-03 17:15:33 -0600112 bool
113 wasRoutingUpdatePublished()
114 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600115 sendInterestForPublishedData();
116
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
alvy297f4162015-03-03 17:15:33 -0600118
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600119 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600120 [lsaPrefix] (const ndn::Data& data) {
121 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122 }
123 );
alvy297f4162015-03-03 17:15:33 -0600124
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600125 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600126 }
127
alvy297f4162015-03-03 17:15:33 -0600128public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600129 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600130
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500131 ndn::Name siteIdentityName;
132 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600133
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500134 ndn::Name opIdentityName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600135 ndn::Name routerIdName;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500136 ndn::security::pib::Identity opIdentity;
Saurab Dulal427e0122019-11-28 11:58:02 -0600137 ndn::security::pib::Identity routerId;
alvy297f4162015-03-03 17:15:33 -0600138
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139 ConfParameter conf;
140 DummyConfFileProcessor confProcessor;
alvy297f4162015-03-03 17:15:33 -0600141 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600142 NamePrefixList& namePrefixList;
alvy297f4162015-03-03 17:15:33 -0600143
144 const boost::filesystem::path SITE_CERT_PATH;
145};
146
147BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
148
149BOOST_AUTO_TEST_CASE(Basic)
150{
Ashlesh Gawande15052402018-12-12 20:20:00 -0600151 uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152
alvy297f4162015-03-03 17:15:33 -0600153 ndn::nfd::ControlParameters parameters;
154 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500155
156 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
157 // /<timestamp>/<random-value>/<signed-interests-components>
158
159 // Advertise
alvy297f4162015-03-03 17:15:33 -0600160 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500161
162 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600163 advertiseCommand.append(parameters.wireEncode());
164
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500165 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600166
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 // CommandInterestSigner::makeCommandInterest() will append the last
168 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
169 ndn::Interest advertiseInterest =
170 cis.makeCommandInterest(advertiseCommand,
171 ndn::security::signingByIdentity(opIdentity));
172
173 face.receive(advertiseInterest);
174
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600175 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600176
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600177 NamePrefixList& namePrefixList = conf.getNamePrefixList();
alvy297f4162015-03-03 17:15:33 -0600178
Nick Gordonff9a6272017-10-12 13:38:29 -0500179 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500180 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600181 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600182 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600183
184 face.sentData.clear();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600185 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600186
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500187 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600188 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
189 withdrawCommand.append(parameters.wireEncode());
190
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191 ndn::Interest withdrawInterest
192 = cis.makeCommandInterest(withdrawCommand,
193 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600194
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500195 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600196 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600197
Nick Gordonff9a6272017-10-12 13:38:29 -0500198 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600199
200 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600201 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600202}
203
alvy297f4162015-03-03 17:15:33 -0600204BOOST_AUTO_TEST_SUITE_END()
205
206} // namespace test
alvy297f4162015-03-03 17:15:33 -0600207} // namespace nlsr