blob: 3cdb8c41343227a20a8fe34581c85845e86ecb0e [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>
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")))
49 , conf(face)
50 , 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
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 std::ifstream inputFile;
63 inputFile.open(std::string("nlsr.conf"));
alvy297f4162015-03-03 17:15:33 -060064
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065 BOOST_REQUIRE(inputFile.is_open());
alvy297f4162015-03-03 17:15:33 -060066
Ashlesh Gawande85998a12017-12-07 22:22:13 -060067 boost::property_tree::ptree pt;
68
69 boost::property_tree::read_info(inputFile, pt);
70 for (const auto& tn : pt) {
71 if (tn.first == "security") {
72 for (const auto& it : tn.second) {
73 if (it.first == "prefix-update-validator") {
74 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
75 }
76 }
77 }
78 }
79 inputFile.close();
alvy297f4162015-03-03 17:15:33 -060080
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050081 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
alvy297f4162015-03-03 17:15:33 -060082
Ashlesh Gawande85998a12017-12-07 22:22:13 -060083 addIdentity(conf.getRouterPrefix());
alvy297f4162015-03-03 17:15:33 -060084
85 // Initialize NLSR so a sync socket is created
86 nlsr.initialize();
87
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());
Nick Gordon727d4832017-10-13 18:04:25 -0500101 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500102
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600103 lsaInterestName.appendNumber(nlsr.m_lsdb.getSequencingManager().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:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600128 ndn::util::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;
134 ndn::security::pib::Identity opIdentity;
alvy297f4162015-03-03 17:15:33 -0600135
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600136 ConfParameter conf;
137 DummyConfFileProcessor confProcessor;
alvy297f4162015-03-03 17:15:33 -0600138 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600139 NamePrefixList& namePrefixList;
alvy297f4162015-03-03 17:15:33 -0600140
141 const boost::filesystem::path SITE_CERT_PATH;
142};
143
144BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
145
146BOOST_AUTO_TEST_CASE(Basic)
147{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600148 uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.getSequencingManager().getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500149
alvy297f4162015-03-03 17:15:33 -0600150 ndn::nfd::ControlParameters parameters;
151 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152
153 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
154 // /<timestamp>/<random-value>/<signed-interests-components>
155
156 // Advertise
alvy297f4162015-03-03 17:15:33 -0600157 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500158
159 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600160 advertiseCommand.append(parameters.wireEncode());
161
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600163
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500164 // CommandInterestSigner::makeCommandInterest() will append the last
165 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
166 ndn::Interest advertiseInterest =
167 cis.makeCommandInterest(advertiseCommand,
168 ndn::security::signingByIdentity(opIdentity));
169
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
179 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600180 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600181
182 face.sentData.clear();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600183 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600184
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500185 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600186 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
187 withdrawCommand.append(parameters.wireEncode());
188
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189 ndn::Interest withdrawInterest
190 = cis.makeCommandInterest(withdrawCommand,
191 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600192
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500193 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600194 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600195
Nick Gordonff9a6272017-10-12 13:38:29 -0500196 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600197
198 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600199 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600200}
201
alvy297f4162015-03-03 17:15:33 -0600202BOOST_AUTO_TEST_SUITE_END()
203
204} // namespace test
alvy297f4162015-03-03 17:15:33 -0600205} // namespace nlsr