blob: 79e2d90380fdfce8cbe76df761bf2870ddc74537 [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/*
Junxiao Shi43f37a02023-08-09 00:09:00 +00003 * Copyright (c) 2014-2023, 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
alvy297f4162015-03-03 17:15:33 -060038namespace nlsr {
alvy297f4162015-03-03 17:15:33 -060039namespace test {
40
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040041using namespace ndn;
42
43class PrefixUpdateFixture : public IoKeyChainFixture
alvy297f4162015-03-03 17:15:33 -060044{
45public:
46 PrefixUpdateFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047 : face(m_io, 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
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040057 siteIdentity = m_keyChain.createIdentity(siteIdentityName);
58 saveIdentityCert(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"));
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;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 boost::property_tree::read_info(inputFile, pt);
75 for (const auto& tn : pt) {
76 if (tn.first == "security") {
77 for (const auto& it : tn.second) {
78 if (it.first == "prefix-update-validator") {
79 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
80 }
81 }
82 }
83 }
84 inputFile.close();
alvy297f4162015-03-03 17:15:33 -060085
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040086 m_keyChain.createIdentity(conf.getRouterPrefix());
alvy297f4162015-03-03 17:15:33 -060087
Ashlesh Gawande415676b2016-12-22 00:26:23 -060088 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -060089
Ashlesh Gawande415676b2016-12-22 00:26:23 -060090 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -060091 }
92
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050093 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -060094 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060095 // Need to send an interest now since ChronoSync
96 // no longer does face->put(*data) in publishData.
97 // Instead it does it in onInterest
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 ndn::Name lsaInterestName = conf.getLsaPrefix();
99 lsaInterestName.append(conf.getSiteName());
100 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800101 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500102
Ashlesh Gawande15052402018-12-12 20:20:00 -0600103 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500104
Junxiao Shi008c9b12019-01-13 23:15:56 +0000105 auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
106 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600107 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500108 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600109 }
110
alvy297f4162015-03-03 17:15:33 -0600111 bool
112 wasRoutingUpdatePublished()
113 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600114 sendInterestForPublishedData();
115
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600116 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
alvy297f4162015-03-03 17:15:33 -0600117
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600118 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600119 [lsaPrefix] (const ndn::Data& data) {
120 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500121 }
122 );
alvy297f4162015-03-03 17:15:33 -0600123
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600124 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600125 }
126
alvy297f4162015-03-03 17:15:33 -0600127public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000128 ndn::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600129
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130 ndn::Name siteIdentityName;
131 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600132
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500133 ndn::Name opIdentityName;
Saurab Dulal427e0122019-11-28 11:58:02 -0600134 ndn::Name routerIdName;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 ndn::security::pib::Identity opIdentity;
Saurab Dulal427e0122019-11-28 11:58:02 -0600136 ndn::security::pib::Identity routerId;
alvy297f4162015-03-03 17:15:33 -0600137
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600138 ConfParameter conf;
139 DummyConfFileProcessor confProcessor;
alvy297f4162015-03-03 17:15:33 -0600140 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600141 NamePrefixList& namePrefixList;
alvy297f4162015-03-03 17:15:33 -0600142
143 const boost::filesystem::path SITE_CERT_PATH;
144};
145
146BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
147
148BOOST_AUTO_TEST_CASE(Basic)
149{
Ashlesh Gawande15052402018-12-12 20:20:00 -0600150 uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500151
alvy297f4162015-03-03 17:15:33 -0600152 ndn::nfd::ControlParameters parameters;
153 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154
155 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
156 // /<timestamp>/<random-value>/<signed-interests-components>
157
158 // Advertise
alvy297f4162015-03-03 17:15:33 -0600159 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500160
161 // append /<control-parameters>
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400162 advertiseCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
alvy297f4162015-03-03 17:15:33 -0600163
Davide Pesavento6184c202021-05-17 02:28:03 -0400164 ndn::security::InterestSigner signer(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600165
Davide Pesavento6184c202021-05-17 02:28:03 -0400166 // InterestSigner::makeCommandInterest() will append the last
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
Davide Pesavento6184c202021-05-17 02:28:03 -0400168 auto advertiseInterest = signer.makeCommandInterest(advertiseCommand,
169 ndn::security::signingByIdentity(opIdentity));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500170
171 face.receive(advertiseInterest);
172
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600173 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600174
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600175 NamePrefixList& namePrefixList = conf.getNamePrefixList();
alvy297f4162015-03-03 17:15:33 -0600176
Nick Gordonff9a6272017-10-12 13:38:29 -0500177 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500178 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600179 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600180 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600181
182 face.sentData.clear();
Ashlesh Gawande15052402018-12-12 20:20:00 -0600183 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600184
Davide Pesavento6184c202021-05-17 02:28:03 -0400185 // Withdraw
alvy297f4162015-03-03 17:15:33 -0600186 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400187 withdrawCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
alvy297f4162015-03-03 17:15:33 -0600188
Davide Pesavento6184c202021-05-17 02:28:03 -0400189 auto withdrawInterest= signer.makeCommandInterest(withdrawCommand,
190 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600191
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600193 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600194
Nick Gordonff9a6272017-10-12 13:38:29 -0500195 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600196
197 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600198 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600199}
200
alvy297f4162015-03-03 17:15:33 -0600201BOOST_AUTO_TEST_SUITE_END()
202
203} // namespace test
alvy297f4162015-03-03 17:15:33 -0600204} // namespace nlsr