blob: 493d73d903e254056612f5e47df6f2d3b344254c [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/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040025#include "tests/io-key-chain-fixture.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060026#include "tests/test-common.hpp"
alvy297f4162015-03-03 17:15:33 -060027
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040028#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Davide Pesavento6184c202021-05-17 02:28:03 -040029#include <ndn-cxx/security/interest-signer.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050030#include <ndn-cxx/security/signing-helpers.hpp>
alvy297f4162015-03-03 17:15:33 -060031
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040032#include <boost/filesystem/operations.hpp>
33#include <boost/filesystem/path.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
Davide Pesavento288141a2024-02-13 17:30:35 -050038namespace nlsr::tests {
alvy297f4162015-03-03 17:15:33 -060039
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040040using namespace ndn;
41
42class PrefixUpdateFixture : public IoKeyChainFixture
alvy297f4162015-03-03 17:15:33 -060043{
44public:
45 PrefixUpdateFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040046 : face(m_io, 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
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040056 siteIdentity = m_keyChain.createIdentity(siteIdentityName);
57 saveIdentityCert(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"));
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 BOOST_REQUIRE(inputFile.is_open());
alvy297f4162015-03-03 17:15:33 -060071
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 boost::property_tree::ptree pt;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 boost::property_tree::read_info(inputFile, pt);
74 for (const auto& tn : pt) {
75 if (tn.first == "security") {
76 for (const auto& it : tn.second) {
77 if (it.first == "prefix-update-validator") {
78 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
79 }
80 }
81 }
82 }
83 inputFile.close();
alvy297f4162015-03-03 17:15:33 -060084
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040085 m_keyChain.createIdentity(conf.getRouterPrefix());
alvy297f4162015-03-03 17:15:33 -060086
Ashlesh Gawande415676b2016-12-22 00:26:23 -060087 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -060088
Ashlesh Gawande415676b2016-12-22 00:26:23 -060089 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -060090 }
91
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050092 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -060093 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060094 // Need to send an interest now since ChronoSync
95 // no longer does face->put(*data) in publishData.
96 // Instead it does it in onInterest
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 ndn::Name lsaInterestName = conf.getLsaPrefix();
98 lsaInterestName.append(conf.getSiteName());
99 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800100 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500101
Ashlesh Gawande15052402018-12-12 20:20:00 -0600102 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500103
Junxiao Shi008c9b12019-01-13 23:15:56 +0000104 auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
105 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600106 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500107 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600108 }
109
alvy297f4162015-03-03 17:15:33 -0600110 bool
111 wasRoutingUpdatePublished()
112 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600113 sendInterestForPublishedData();
114
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600115 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
alvy297f4162015-03-03 17:15:33 -0600116
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600117 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600118 [lsaPrefix] (const ndn::Data& data) {
119 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500120 }
121 );
alvy297f4162015-03-03 17:15:33 -0600122
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600123 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600124 }
125
alvy297f4162015-03-03 17:15:33 -0600126public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000127 ndn::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600128
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500129 ndn::Name siteIdentityName;
130 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600131
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500132 ndn::Name opIdentityName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600133 ndn::Name routerIdName;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500134 ndn::security::pib::Identity opIdentity;
Saurab Dulal427e0122019-11-28 11:58:02 -0600135 ndn::security::pib::Identity routerId;
alvy297f4162015-03-03 17:15:33 -0600136
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600137 ConfParameter conf;
138 DummyConfFileProcessor confProcessor;
alvy297f4162015-03-03 17:15:33 -0600139 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600140 NamePrefixList& namePrefixList;
alvy297f4162015-03-03 17:15:33 -0600141
142 const boost::filesystem::path SITE_CERT_PATH;
143};
144
145BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
146
147BOOST_AUTO_TEST_CASE(Basic)
148{
Ashlesh Gawande15052402018-12-12 20:20:00 -0600149 uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150
alvy297f4162015-03-03 17:15:33 -0600151 ndn::nfd::ControlParameters parameters;
152 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153
154 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
155 // /<timestamp>/<random-value>/<signed-interests-components>
156
157 // Advertise
alvy297f4162015-03-03 17:15:33 -0600158 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500159
160 // append /<control-parameters>
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400161 advertiseCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
alvy297f4162015-03-03 17:15:33 -0600162
Davide Pesavento6184c202021-05-17 02:28:03 -0400163 ndn::security::InterestSigner signer(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600164
Davide Pesavento6184c202021-05-17 02:28:03 -0400165 // InterestSigner::makeCommandInterest() will append the last
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
Davide Pesavento6184c202021-05-17 02:28:03 -0400167 auto advertiseInterest = signer.makeCommandInterest(advertiseCommand,
168 ndn::security::signingByIdentity(opIdentity));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169
170 face.receive(advertiseInterest);
171
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600172 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600173
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 NamePrefixList& namePrefixList = conf.getNamePrefixList();
alvy297f4162015-03-03 17:15:33 -0600175
Nick Gordonff9a6272017-10-12 13:38:29 -0500176 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500177 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600178 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600179 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600180
181 face.sentData.clear();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600182 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600183
Davide Pesavento6184c202021-05-17 02:28:03 -0400184 // Withdraw
alvy297f4162015-03-03 17:15:33 -0600185 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400186 withdrawCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
alvy297f4162015-03-03 17:15:33 -0600187
Davide Pesavento6184c202021-05-17 02:28:03 -0400188 auto withdrawInterest= signer.makeCommandInterest(withdrawCommand,
189 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600190
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600192 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600193
Nick Gordonff9a6272017-10-12 13:38:29 -0500194 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600195
196 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600197 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600198}
199
alvy297f4162015-03-03 17:15:33 -0600200BOOST_AUTO_TEST_SUITE_END()
201
Davide Pesavento288141a2024-02-13 17:30:35 -0500202} // namespace nlsr::tests