blob: c4bbaa71cce4de038a3d8ac1b175ed14f9a94f20 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -06003 * Copyright (c) 2014-2018, 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"
23
24#include "../control-commands.hpp"
25#include "../test-common.hpp"
26#include "nlsr.hpp"
27
28#include <ndn-cxx/interest.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000029#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
30#include <ndn-cxx/mgmt/nfd/control-response.hpp>
alvy297f4162015-03-03 17:15:33 -060031#include <ndn-cxx/security/key-chain.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050032#include <ndn-cxx/security/pib/identity.hpp>
33#include <ndn-cxx/security/signing-helpers.hpp>
34#include <ndn-cxx/security/command-interest-signer.hpp>
alvy297f4162015-03-03 17:15:33 -060035#include <ndn-cxx/util/dummy-client-face.hpp>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050036#include <ndn-cxx/lp/tags.hpp>
alvy297f4162015-03-03 17:15:33 -060037
38#include <boost/filesystem.hpp>
39
40using namespace ndn;
41
42namespace nlsr {
43namespace update {
44namespace test {
45
Ashlesh Gawande415676b2016-12-22 00:26:23 -060046class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060047{
48public:
49 PrefixUpdateFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050050 : face(m_ioService, m_keyChain, {true, true})
51 , siteIdentityName(ndn::Name("/edu/test-site"))
52 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
53 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Laqin Fan54a43f02017-03-08 12:31:30 -060054 , namePrefixList(nlsr.getNamePrefixList())
55 , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
alvy297f4162015-03-03 17:15:33 -060056 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
57 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050058 // Site cert
59 siteIdentity = addIdentity(siteIdentityName);
60 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
alvy297f4162015-03-03 17:15:33 -060061
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050062 // Operator cert
63 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
alvy297f4162015-03-03 17:15:33 -060064
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 const std::string CONFIG = R"CONF(
66 rule
67 {
68 id "NLSR ControlCommand Rule"
69 for interest
70 filter
71 {
72 type name
73 regex ^(<localhost><nlsr>)<prefix-update>[<advertise><withdraw>]<><><>$
74 }
75 checker
76 {
77 type customized
78 sig-type rsa-sha256
79 key-locator
80 {
81 type name
82 regex ^<>*<KEY><>$
83 }
84 }
85 }
86 rule
87 {
88 id "NLSR Hierarchy Rule"
89 for data
90 filter
91 {
92 type name
93 regex ^[^<KEY>]*<KEY><><><>$
94 }
95 checker
96 {
97 type hierarchical
98 sig-type rsa-sha256
99 }
100 }
101 trust-anchor
102 {
103 type file
104 file-name "site.cert"
105 }
106 )CONF";
alvy297f4162015-03-03 17:15:33 -0600107
108 const boost::filesystem::path CONFIG_PATH =
109 (boost::filesystem::current_path() / std::string("unit-test.conf"));
110
Laqin Fan54a43f02017-03-08 12:31:30 -0600111 updatePrefixUpdateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native());
alvy297f4162015-03-03 17:15:33 -0600112
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
alvy297f4162015-03-03 17:15:33 -0600114
115 // Set the network so the LSA prefix is constructed
116 nlsr.getConfParameter().setNetwork("/ndn");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117 nlsr.getConfParameter().setSiteName("/edu/test-site");
118 nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
119 nlsr.getConfParameter().buildRouterPrefix();
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600120 // Otherwise code coverage node fails with default 60 seconds lifetime
121 nlsr.getConfParameter().setSyncInterestLifetime(1000);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122
123 addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router"));
alvy297f4162015-03-03 17:15:33 -0600124
125 // Initialize NLSR so a sync socket is created
126 nlsr.initialize();
127
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600128 this->advanceClocks(ndn::time::milliseconds(10));
Laqin Fan54a43f02017-03-08 12:31:30 -0600129
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600130 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600131 }
132
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500133 void sendInterestForPublishedData()
alvy297f4162015-03-03 17:15:33 -0600134 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600135 // Need to send an interest now since ChronoSync
136 // no longer does face->put(*data) in publishData.
137 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500138 ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix();
139 lsaInterestName.append(nlsr.getConfParameter().getSiteName());
140 lsaInterestName.append(nlsr.getConfParameter().getRouterName());
Nick Gordon727d4832017-10-13 18:04:25 -0500141 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500142
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500143 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
144
Nick Gordone98480b2017-05-24 11:23:03 -0500145 std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600146
147 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500148 this->advanceClocks(ndn::time::milliseconds(100));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600149 }
150
alvy297f4162015-03-03 17:15:33 -0600151 bool
152 wasRoutingUpdatePublished()
153 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600154 sendInterestForPublishedData();
155
alvy297f4162015-03-03 17:15:33 -0600156 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
157
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600158 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600159 [lsaPrefix] (const ndn::Data& data) {
160 return lsaPrefix.isPrefixOf(data.getName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500161 }
162 );
alvy297f4162015-03-03 17:15:33 -0600163
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600164 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600165 }
166
alvy297f4162015-03-03 17:15:33 -0600167public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600168 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600169
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500170 ndn::Name siteIdentityName;
171 ndn::security::pib::Identity siteIdentity;
alvy297f4162015-03-03 17:15:33 -0600172
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500173 ndn::Name opIdentityName;
174 ndn::security::pib::Identity opIdentity;
alvy297f4162015-03-03 17:15:33 -0600175
176 Nlsr nlsr;
Laqin Fan54a43f02017-03-08 12:31:30 -0600177 NamePrefixList& namePrefixList;
178 PrefixUpdateProcessor& updatePrefixUpdateProcessor;
alvy297f4162015-03-03 17:15:33 -0600179
180 const boost::filesystem::path SITE_CERT_PATH;
181};
182
183BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
184
185BOOST_AUTO_TEST_CASE(Basic)
186{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500187 uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500188
alvy297f4162015-03-03 17:15:33 -0600189 ndn::nfd::ControlParameters parameters;
190 parameters.setName("/prefix/to/advertise/");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191
192 // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
193 // /<timestamp>/<random-value>/<signed-interests-components>
194
195 // Advertise
alvy297f4162015-03-03 17:15:33 -0600196 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500197
198 // append /<control-parameters>
alvy297f4162015-03-03 17:15:33 -0600199 advertiseCommand.append(parameters.wireEncode());
200
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500201 ndn::security::CommandInterestSigner cis(m_keyChain);
alvy297f4162015-03-03 17:15:33 -0600202
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500203 // CommandInterestSigner::makeCommandInterest() will append the last
204 // three components: (<timestamp>/<random-value>/<signed-interests-components>)
205 ndn::Interest advertiseInterest =
206 cis.makeCommandInterest(advertiseCommand,
207 ndn::security::signingByIdentity(opIdentity));
208
209 face.receive(advertiseInterest);
210
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600211 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600212
213 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
214
Nick Gordonff9a6272017-10-12 13:38:29 -0500215 BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1);
Nick Gordonf14ec352017-07-24 16:09:58 -0500216 BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
alvy297f4162015-03-03 17:15:33 -0600217
218 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500219 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600220
221 face.sentData.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500222 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
alvy297f4162015-03-03 17:15:33 -0600223
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500224 //Withdraw
alvy297f4162015-03-03 17:15:33 -0600225 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
226 withdrawCommand.append(parameters.wireEncode());
227
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500228 ndn::Interest withdrawInterest
229 = cis.makeCommandInterest(withdrawCommand,
230 ndn::security::signingByIdentity(opIdentity));
alvy297f4162015-03-03 17:15:33 -0600231
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500232 face.receive(withdrawInterest);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600233 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600234
Nick Gordonff9a6272017-10-12 13:38:29 -0500235 BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
alvy297f4162015-03-03 17:15:33 -0600236
237 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500238 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
alvy297f4162015-03-03 17:15:33 -0600239}
240
alvy297f4162015-03-03 17:15:33 -0600241BOOST_AUTO_TEST_SUITE_END()
242
243} // namespace test
244} // namespace update
245} // namespace nlsr