blob: 70fd4918ff1c86904ebcf6a18963b973e7d9a460 [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 Gawande30d96e42021-03-21 19:15:33 -070034#include <boost/lexical_cast.hpp>
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035#include <boost/property_tree/info_parser.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070036#include <boost/property_tree/ptree.hpp>
alvy297f4162015-03-03 17:15:33 -060037
38using namespace ndn;
39
40namespace nlsr {
alvy297f4162015-03-03 17:15:33 -060041namespace test {
42
Ashlesh Gawande415676b2016-12-22 00:26:23 -060043class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060044{
45public:
46 PrefixUpdateFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050047 : face(m_ioService, m_keyChain, {true, true})
Ashlesh Gawande85998a12017-12-07 22:22:13 -060048 , siteIdentityName(ndn::Name("site"))
49 , opIdentityName(ndn::Name("site").append(ndn::Name("%C1.Operator")))
Saurab Dulal427e0122019-11-28 11:58:02 -060050 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 , confProcessor(conf)
52 , nlsr(face, m_keyChain, conf)
53 , namePrefixList(conf.getNamePrefixList())
alvy297f4162015-03-03 17:15:33 -060054 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
55 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050056 // Site cert
57 siteIdentity = addIdentity(siteIdentityName);
58 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
alvy297f4162015-03-03 17:15:33 -060059
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 // Operator cert
61 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
alvy297f4162015-03-03 17:15:33 -060062
Saurab Dulal427e0122019-11-28 11:58:02 -060063 // Create certificate and load it to the validator
64 conf.initializeKey();
65
66 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
67 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
68
Ashlesh Gawande85998a12017-12-07 22:22:13 -060069 std::ifstream inputFile;
70 inputFile.open(std::string("nlsr.conf"));
alvy297f4162015-03-03 17:15:33 -060071
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 BOOST_REQUIRE(inputFile.is_open());
alvy297f4162015-03-03 17:15:33 -060073
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 boost::property_tree::ptree pt;
75
76 boost::property_tree::read_info(inputFile, pt);
77 for (const auto& tn : pt) {
78 if (tn.first == "security") {
79 for (const auto& it : tn.second) {
80 if (it.first == "prefix-update-validator") {
81 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
82 }
83 }
84 }
85 }
86 inputFile.close();
alvy297f4162015-03-03 17:15:33 -060087
Ashlesh Gawande85998a12017-12-07 22:22:13 -060088 addIdentity(conf.getRouterPrefix());
alvy297f4162015-03-03 17:15:33 -060089
Ashlesh Gawande415676b2016-12-22 00:26:23 -060090 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -060091
Ashlesh Gawande415676b2016-12-22 00:26:23 -060092 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -060093 }
94
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050095 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -060096 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060097 // Need to send an interest now since ChronoSync
98 // no longer does face->put(*data) in publishData.
99 // Instead it does it in onInterest
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100 ndn::Name lsaInterestName = conf.getLsaPrefix();
101 lsaInterestName.append(conf.getSiteName());
102 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800103 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500104
Ashlesh Gawande15052402018-12-12 20:20:00 -0600105 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500106
Junxiao Shi008c9b12019-01-13 23:15:56 +0000107 auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
108 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600109 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500110 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600111 }
112
alvy297f4162015-03-03 17:15:33 -0600113 bool
114 wasRoutingUpdatePublished()
115 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600116 sendInterestForPublishedData();
117
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600118 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
alvy297f4162015-03-03 17:15:33 -0600119
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600120 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600121 [lsaPrefix] (const ndn::Data& data) {
122 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500123 }
124 );
alvy297f4162015-03-03 17:15:33 -0600125
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600126 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600127 }
128
alvy297f4162015-03-03 17:15:33 -0600129public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600130 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600131
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500132 ndn::Name siteIdentityName;
133 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600134
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 ndn::Name opIdentityName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600136 ndn::Name routerIdName;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500137 ndn::security::pib::Identity opIdentity;
Saurab Dulal427e0122019-11-28 11:58:02 -0600138 ndn::security::pib::Identity routerId;
alvy297f4162015-03-03 17:15:33 -0600139
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600140 ConfParameter conf;
141 DummyConfFileProcessor confProcessor;
alvy297f4162015-03-03 17:15:33 -0600142 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600143 NamePrefixList& namePrefixList;
alvy297f4162015-03-03 17:15:33 -0600144
145 const boost::filesystem::path SITE_CERT_PATH;
146};
147
148BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
149
150BOOST_AUTO_TEST_CASE(Basic)
151{
Ashlesh Gawande15052402018-12-12 20:20:00 -0600152 uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153
alvy297f4162015-03-03 17:15:33 -0600154 ndn::nfd::ControlParameters parameters;
155 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156
157 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
158 // /<timestamp>/<random-value>/<signed-interests-components>
159
160 // Advertise
alvy297f4162015-03-03 17:15:33 -0600161 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162
163 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600164 advertiseCommand.append(parameters.wireEncode());
165
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600167
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500168 // CommandInterestSigner::makeCommandInterest() will append the last
169 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
170 ndn::Interest advertiseInterest =
171 cis.makeCommandInterest(advertiseCommand,
172 ndn::security::signingByIdentity(opIdentity));
173
174 face.receive(advertiseInterest);
175
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600176 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600177
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600178 NamePrefixList& namePrefixList = conf.getNamePrefixList();
alvy297f4162015-03-03 17:15:33 -0600179
Nick Gordonff9a6272017-10-12 13:38:29 -0500180 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500181 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600182 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600183 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600184
185 face.sentData.clear();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600186 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600187
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500188 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600189 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
190 withdrawCommand.append(parameters.wireEncode());
191
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 ndn::Interest withdrawInterest
193 = cis.makeCommandInterest(withdrawCommand,
194 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600195
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600197 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600198
Nick Gordonff9a6272017-10-12 13:38:29 -0500199 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600200
201 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600202 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600203}
204
alvy297f4162015-03-03 17:15:33 -0600205BOOST_AUTO_TEST_SUITE_END()
206
207} // namespace test
alvy297f4162015-03-03 17:15:33 -0600208} // namespace nlsr