blob: 8ae96aed593ae4c443e8766e260fa3ebfe0de7d1 [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
Junxiao Shi4b84a2c2022-04-28 03:17:05 +00003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Vince Lehman72446ec2014-07-09 10:50:02 -050010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050026#include "mgmt/rib-manager.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050027
Davide Pesavento78ddcab2019-02-28 22:00:03 -050028#include "manager-common-fixture.hpp"
Junxiao Shidf1dc652019-08-30 19:03:19 +000029#include "tests/daemon/rib/fib-updates-common.hpp"
Davide Pesavento0064c1d2018-03-03 18:43:53 -050030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Davide Pesaventod2ea2352017-03-12 20:38:09 -040032#include <ndn-cxx/mgmt/nfd/face-status.hpp>
33#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070034
35#include <boost/property_tree/info_parser.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Vince Lehman72446ec2014-07-09 10:50:02 -050038
Yanbiao Licf0db022016-01-29 00:54:25 -080039struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050040{
Yanbiao Licf0db022016-01-29 00:54:25 -080041 bool isLocalhostConfigured;
42 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050043};
44
Junxiao Shif4cfed12018-08-22 23:26:29 +000045static ConfigSection
46getValidatorConfigSection()
47{
48 ConfigSection section;
49 section.put("trust-anchor.type", "any");
50 return section;
51}
52
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070053static ConfigSection
54makeSection(const std::string& config)
55{
56 std::istringstream inputStream(config);
57 ConfigSection section;
58 boost::property_tree::read_info(inputStream, section);
59 return section;
60}
61
62static ConfigSection
63getLocalhopValidatorConfigSection()
64{
65 std::string config = R"CONF(
66 rule
67 {
68 id rule-id1
69 for interest
70 filter
71 {
72 type name
73 name /localhop/nfd/rib
74 relation is-prefix-of
75 }
76 checker
77 {
78 type customized
79 sig-type ecdsa-sha256
80 key-locator
81 {
82 type name
83 name /TrustAnchor-identity/Derived-identity
84 relation is-prefix-of
85 }
86 }
87 }
88 rule
89 {
90 id rule-id2
91 for data
92 filter
93 {
94 type name
95 name /TrustAnchor-identity/Derived-identity/KEY
96 relation is-prefix-of
97 }
98 checker
99 {
100 type customized
101 sig-type ecdsa-sha256
102 key-locator
103 {
104 type name
105 name /TrustAnchor-identity
106 relation is-prefix-of
107 }
108 }
109 }
110 trust-anchor
111 {
112 type file
113 file-name signer.ndncert
114 }
115 )CONF";
116 return makeSection(config);
117}
118
Yanbiao Licf0db022016-01-29 00:54:25 -0800119class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500120{
121public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400122 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shidf1dc652019-08-30 19:03:19 +0000123 : m_status(status)
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700124 , m_anchorId("/TrustAnchor-identity")
125 , m_derivedId("/TrustAnchor-identity/Derived-identity")
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000126 , m_nfdController(m_face, m_keyChain)
127 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400128 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -0500129 {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000130 auto anchorIdentity = m_keyChain.createIdentity(m_anchorId);
Davide Pesavento21353752020-11-20 00:43:44 -0500131 saveIdentityCert(m_anchorId, "signer.ndncert", true);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700132
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000133 auto derivedKey = m_keyChain.createIdentity(m_derivedId).getDefaultKey();
134 auto derivedSelfSigned = derivedKey.getDefaultCertificate();
135 ndn::security::MakeCertificateOptions opts;
136 opts.validity = derivedSelfSigned.getValidityPeriod();
137 auto derivedCert = m_keyChain.makeCertificate(derivedSelfSigned,
Davide Pesavento20cafa82022-07-25 01:15:03 -0400138 ndn::security::signingByIdentity(anchorIdentity),
139 opts);
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000140 m_keyChain.setDefaultCertificate(derivedKey, derivedCert);
141
Yanbiao Licf0db022016-01-29 00:54:25 -0800142 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000143 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800144 }
Davide Pesavento20cafa82022-07-25 01:15:03 -0400145
Yanbiao Licf0db022016-01-29 00:54:25 -0800146 if (m_status.isLocalhopConfigured) {
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700147 m_manager.enableLocalhop(getLocalhopValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800148 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000149 else {
150 m_manager.disableLocalhop();
151 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800152
153 registerWithNfd();
154
155 if (shouldClearRib) {
156 clearRib();
157 }
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700158
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000159 m_face.onSendInterest.connect([=] (const Interest& interest) {
160 if (interest.matchesData(derivedCert) &&
161 m_status.isLocalhopConfigured &&
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400162 interest.getTag<lp::NextHopFaceIdTag>() != nullptr) {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000163 m_face.put(derivedCert);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700164 }
165 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800166 }
167
168private:
169 void
170 registerWithNfd()
171 {
172 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500173 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800174
175 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400176 ControlParameters params(interest.getName().at(4).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000177 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Davide Pesavento20cafa82022-07-25 01:15:03 -0400178 params.setFaceId(1)
179 .setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500180 ControlResponse resp;
Davide Pesavento20cafa82022-07-25 01:15:03 -0400181 resp.setCode(200)
182 .setBody(params.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800183
Davide Pesavento20cafa82022-07-25 01:15:03 -0400184 auto data = make_shared<Data>(interest.getName());
Yanbiao Licf0db022016-01-29 00:54:25 -0800185 data->setContent(resp.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800186 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
187
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000188 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800189 };
190
Davide Pesavento20cafa82022-07-25 01:15:03 -0400191 const Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000192 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 if (commandPrefix.isPrefixOf(command.getName())) {
194 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500195 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800196 }
197 }
198
199 // clear commands and responses
200 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000201 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800202 }
203
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000204 void
205 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800206 {
207 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000208 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800209 }
210 }
211
212public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400213 static ControlParameters
214 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400215 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800216 {
217 return ControlParameters()
218 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400219 .setFaceId(faceId)
220 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800221 .setCost(10)
222 .setFlags(0)
223 .setExpirationPeriod(expiry);
224 }
225
Davide Pesavento22db5392017-04-14 00:56:43 -0400226 static ControlParameters
227 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 {
229 return ControlParameters()
230 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400231 .setFaceId(faceId)
232 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 }
234
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000235protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800236 ConfigurationStatus m_status;
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700237 Name m_anchorId;
238 Name m_derivedId;
Yanbiao Licf0db022016-01-29 00:54:25 -0800239
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000240 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500241 rib::Rib m_rib;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400242 MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800243 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800244};
245
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500246BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800247BOOST_AUTO_TEST_SUITE(TestRibManager)
248
249class AddTopPrefixFixture : public RibManagerFixture
250{
251public:
252 AddTopPrefixFixture()
253 : RibManagerFixture({true, true}, false)
254 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500255 }
256};
257
Yanbiao Licf0db022016-01-29 00:54:25 -0800258BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700259{
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400260 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600261
Yanbiao Licf0db022016-01-29 00:54:25 -0800262 std::vector<Name> ribEntryNames;
263 for (auto&& entry : m_rib) {
264 ribEntryNames.push_back(entry.first);
265 }
266 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
267 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700268}
269
Yanbiao Licf0db022016-01-29 00:54:25 -0800270class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500271{
Yanbiao Licf0db022016-01-29 00:54:25 -0800272public:
273 UnauthorizedRibManagerFixture()
274 : RibManagerFixture({false, false}, true)
275 {
276 }
277};
Vince Lehman72446ec2014-07-09 10:50:02 -0500278
Yanbiao Licf0db022016-01-29 00:54:25 -0800279class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
280{
281public:
282 LocalhostAuthorizedRibManagerFixture()
283 : RibManagerFixture({true, false}, true)
284 {
285 }
286};
Vince Lehman72446ec2014-07-09 10:50:02 -0500287
Yanbiao Licf0db022016-01-29 00:54:25 -0800288class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
289{
290public:
291 LocalhopAuthorizedRibManagerFixture()
292 : RibManagerFixture({false, true}, true)
293 {
294 }
295};
296
297class AuthorizedRibManagerFixture : public RibManagerFixture
298{
299public:
300 AuthorizedRibManagerFixture()
301 : RibManagerFixture({true, true}, true)
302 {
303 }
304};
305
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400306template<typename Fixture, auto Format>
307struct FixtureWithFormat : public Fixture
308{
309 static constexpr ndn::security::SignedInterestFormat signedInterestFmt = Format;
310};
311
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400312using AllFixtures = boost::mpl::vector<
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400313 FixtureWithFormat<UnauthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V02>,
314 FixtureWithFormat<UnauthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V03>,
315 FixtureWithFormat<LocalhostAuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V02>,
316 FixtureWithFormat<LocalhostAuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V03>,
317 FixtureWithFormat<LocalhopAuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V02>,
318 FixtureWithFormat<LocalhopAuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V03>,
319 FixtureWithFormat<AuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V02>,
320 FixtureWithFormat<AuthorizedRibManagerFixture, ndn::security::SignedInterestFormat::V03>
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400321>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800322
323BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
324{
325 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400326 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters,
327 T::signedInterestFmt);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700328 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters,
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400329 T::signedInterestFmt, this->m_derivedId);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700330 if (this->m_status.isLocalhopConfigured) {
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400331 commandHop.setTag(std::make_shared<lp::IncomingFaceIdTag>(123));
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700332 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800333 auto successResp = this->makeResponse(200, "Success", parameters);
334 auto failureResp = ControlResponse(403, "authorization rejected");
335
336 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
337 this->receiveInterest(commandHost);
338 this->receiveInterest(commandHop);
339
340 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
341 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
342 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
343
344 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000345 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 ManagerCommonFixture::CheckResponseResult::OK);
347 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000348 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800349 ManagerCommonFixture::CheckResponseResult::OK);
350 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500351}
352
Yanbiao Licf0db022016-01-29 00:54:25 -0800353BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
354
355BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500356{
Yanbiao Licf0db022016-01-29 00:54:25 -0800357 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
358 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500359
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000360 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
361 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
362 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
363 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500364
Yanbiao Licf0db022016-01-29 00:54:25 -0800365 receiveInterest(commandRegister);
366 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500367
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000369 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800370 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000371 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800372 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Junxiao Shidf1dc652019-08-30 19:03:19 +0000374 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
375 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
376 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
377 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
378 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500379}
380
Yanbiao Licf0db022016-01-29 00:54:25 -0800381BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500382{
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
384 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
385 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
386 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500387
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000389 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
390 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
391 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
392 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500393
Yanbiao Licf0db022016-01-29 00:54:25 -0800394 receiveInterest(commandRegister);
395 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500396
Yanbiao Licf0db022016-01-29 00:54:25 -0800397 paramsRegister.setFaceId(inFaceId);
398 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500399
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000401 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000403 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800404 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500405
Junxiao Shidf1dc652019-08-30 19:03:19 +0000406 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
407 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
408 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
409 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
410 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500411}
412
Yanbiao Licf0db022016-01-29 00:54:25 -0800413BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000414{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500415 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
417 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000418
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500419 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000420 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2); // the registered route has expired
421 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
422 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
423 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
424 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000425
Junxiao Shidf1dc652019-08-30 19:03:19 +0000426 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500427 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800428 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000429
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500430 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000431 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
432 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
433 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000434}
435
Junxiao Shi75306352018-02-01 21:59:44 +0000436BOOST_AUTO_TEST_CASE(NameTooLong)
437{
438 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400439 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000440 prefix.append("A");
441 }
442 auto params = makeRegisterParameters(prefix, 2899);
443 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
444 receiveInterest(command);
445
446 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400447 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
448 ControlResponse(414, "Route prefix cannot exceed " +
449 to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000450 CheckResponseResult::OK);
451
Junxiao Shidf1dc652019-08-30 19:03:19 +0000452 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000453}
454
Yanbiao Licf0db022016-01-29 00:54:25 -0800455BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
456
Yanbiao Licf0db022016-01-29 00:54:25 -0800457BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500458{
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500460 auto generateRoute = [&faceId] () -> rib::Route {
461 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400463 route.cost = route.faceId * 10;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400464 route.expires = std::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800465 return route;
466 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500467
Yanbiao Licf0db022016-01-29 00:54:25 -0800468 const size_t nEntries = 108;
469 std::set<Name> actualPrefixes;
470 for (size_t i = 0; i < nEntries; ++i) {
471 Name prefix = Name("/test-dataset").appendNumber(i);
472 actualPrefixes.insert(prefix);
473 m_rib.insert(prefix, generateRoute());
474 if (i & 0x1) {
475 m_rib.insert(prefix, generateRoute());
476 m_rib.insert(prefix, generateRoute());
477 }
478 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500479
Junxiao Shi9d727852019-05-14 13:44:22 -0600480 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500481
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400482 Block content = concatenateResponses();
483 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800484 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
485
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400486 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800487 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400488 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800489 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 actualPrefixes.erase(decodedEntry.getName());
491
492 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
493 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
494
495 auto matchedEntry = matchedEntryIt->second;
496 BOOST_REQUIRE(matchedEntry != nullptr);
497
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400498 expectedRecords.emplace_back();
499 expectedRecords.back().setName(matchedEntry->getName());
500 for (const auto& route : matchedEntry->getRoutes()) {
501 expectedRecords.back().addRoute(ndn::nfd::Route()
502 .setFaceId(route.faceId)
503 .setOrigin(route.origin)
504 .setCost(route.cost)
505 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800506 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800507 }
508
509 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400510 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Vince Lehman72446ec2014-07-09 10:50:02 -0500511}
512
Yanbiao Licf0db022016-01-29 00:54:25 -0800513BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
514
515BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700516{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000517 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600518
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500519 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000520 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
521 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
522 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700523}
524
Yanbiao Licf0db022016-01-29 00:54:25 -0800525BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500526{
Yanbiao Licf0db022016-01-29 00:54:25 -0800527 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
528 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
529 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
530 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
531 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
532 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500533
Vince Lehman26b215c2014-08-17 15:00:41 -0500534 ndn::nfd::FaceStatus status;
535 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700536 std::vector<ndn::nfd::FaceStatus> activeFaces;
537 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500538
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700539 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500540 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800541 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500542
Yanbiao Licf0db022016-01-29 00:54:25 -0800543 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
544 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
545 BOOST_CHECK(it2 == m_rib.end());
546 BOOST_REQUIRE(it1 != m_rib.end());
547 BOOST_CHECK(it1->second->hasFaceId(1));
548 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500549}
550
Yanbiao Licf0db022016-01-29 00:54:25 -0800551BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600552{
Yanbiao Licf0db022016-01-29 00:54:25 -0800553 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
554 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
555 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
556 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
557 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600558
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500559 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
560 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800561 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600562
Yanbiao Licf0db022016-01-29 00:54:25 -0800563 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500564 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800565 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600566
Yanbiao Licf0db022016-01-29 00:54:25 -0800567 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500568 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800569 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600570}
571
Yanbiao Licf0db022016-01-29 00:54:25 -0800572BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500573
Yanbiao Licf0db022016-01-29 00:54:25 -0800574BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500575BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500576
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400577} // namespace nfd::tests