blob: 9dc55d15e3ca1a9eefee1d9b114f89387e20ae75 [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/*
jaczhib0657682025-01-08 23:01:45 -08003 * Copyright (c) 2014-2025, 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
Davide Pesaventof56cf632024-01-27 22:22:06 -050035#include <boost/mp11/algorithm.hpp>
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070036#include <boost/property_tree/info_parser.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::tests {
Vince Lehman72446ec2014-07-09 10:50:02 -050039
Yanbiao Licf0db022016-01-29 00:54:25 -080040struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050041{
Yanbiao Licf0db022016-01-29 00:54:25 -080042 bool isLocalhostConfigured;
43 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050044};
45
Junxiao Shif4cfed12018-08-22 23:26:29 +000046static ConfigSection
47getValidatorConfigSection()
48{
49 ConfigSection section;
50 section.put("trust-anchor.type", "any");
51 return section;
52}
53
Zhiyi Zhanga499aa22019-09-24 15:00:40 -070054static ConfigSection
55makeSection(const std::string& config)
56{
57 std::istringstream inputStream(config);
58 ConfigSection section;
59 boost::property_tree::read_info(inputStream, section);
60 return section;
61}
62
63static ConfigSection
64getLocalhopValidatorConfigSection()
65{
66 std::string config = R"CONF(
67 rule
68 {
69 id rule-id1
70 for interest
71 filter
72 {
73 type name
74 name /localhop/nfd/rib
75 relation is-prefix-of
76 }
77 checker
78 {
79 type customized
80 sig-type ecdsa-sha256
81 key-locator
82 {
83 type name
84 name /TrustAnchor-identity/Derived-identity
85 relation is-prefix-of
86 }
87 }
88 }
89 rule
90 {
91 id rule-id2
92 for data
93 filter
94 {
95 type name
96 name /TrustAnchor-identity/Derived-identity/KEY
97 relation is-prefix-of
98 }
99 checker
100 {
101 type customized
102 sig-type ecdsa-sha256
103 key-locator
104 {
105 type name
106 name /TrustAnchor-identity
107 relation is-prefix-of
108 }
109 }
110 }
111 trust-anchor
112 {
113 type file
114 file-name signer.ndncert
115 }
116 )CONF";
117 return makeSection(config);
118}
119
jaczhib0657682025-01-08 23:01:45 -0800120static ConfigSection
121getPaValidatorConfigSection()
122{
123 ConfigSection section;
124 section.put("trust-anchor.type", "any");
125 return section;
126}
127
Yanbiao Licf0db022016-01-29 00:54:25 -0800128class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500129{
130public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400131 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shidf1dc652019-08-30 19:03:19 +0000132 : m_status(status)
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700133 , m_anchorId("/TrustAnchor-identity")
134 , m_derivedId("/TrustAnchor-identity/Derived-identity")
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000135 , m_nfdController(m_face, m_keyChain)
136 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400137 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -0500138 {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000139 auto anchorIdentity = m_keyChain.createIdentity(m_anchorId);
Davide Pesavento21353752020-11-20 00:43:44 -0500140 saveIdentityCert(m_anchorId, "signer.ndncert", true);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700141
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000142 auto derivedKey = m_keyChain.createIdentity(m_derivedId).getDefaultKey();
143 auto derivedSelfSigned = derivedKey.getDefaultCertificate();
144 ndn::security::MakeCertificateOptions opts;
145 opts.validity = derivedSelfSigned.getValidityPeriod();
146 auto derivedCert = m_keyChain.makeCertificate(derivedSelfSigned,
Davide Pesavento20cafa82022-07-25 01:15:03 -0400147 ndn::security::signingByIdentity(anchorIdentity),
148 opts);
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000149 m_keyChain.setDefaultCertificate(derivedKey, derivedCert);
150
Yanbiao Licf0db022016-01-29 00:54:25 -0800151 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000152 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800153 }
Davide Pesavento20cafa82022-07-25 01:15:03 -0400154
Yanbiao Licf0db022016-01-29 00:54:25 -0800155 if (m_status.isLocalhopConfigured) {
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700156 m_manager.enableLocalhop(getLocalhopValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800157 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000158 else {
159 m_manager.disableLocalhop();
160 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800161
jaczhib0657682025-01-08 23:01:45 -0800162 m_manager.applyPaConfig(getPaValidatorConfigSection(), "testPa");
163
Yanbiao Licf0db022016-01-29 00:54:25 -0800164 registerWithNfd();
165
166 if (shouldClearRib) {
167 clearRib();
168 }
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700169
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000170 m_face.onSendInterest.connect([=] (const Interest& interest) {
171 if (interest.matchesData(derivedCert) &&
172 m_status.isLocalhopConfigured &&
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400173 interest.getTag<lp::NextHopFaceIdTag>() != nullptr) {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000174 m_face.put(derivedCert);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700175 }
176 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800177 }
178
179private:
180 void
181 registerWithNfd()
182 {
183 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500184 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800185
186 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400187 ControlParameters params(interest.getName().at(4).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000188 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Davide Pesavento20cafa82022-07-25 01:15:03 -0400189 params.setFaceId(1)
190 .setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500191 ControlResponse resp;
Davide Pesavento20cafa82022-07-25 01:15:03 -0400192 resp.setCode(200)
193 .setBody(params.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800194
Davide Pesavento20cafa82022-07-25 01:15:03 -0400195 auto data = make_shared<Data>(interest.getName());
Yanbiao Licf0db022016-01-29 00:54:25 -0800196 data->setContent(resp.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800197 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
198
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500199 boost::asio::post(m_face.getIoContext(), [this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800200 };
201
Davide Pesavento20cafa82022-07-25 01:15:03 -0400202 const Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000203 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800204 if (commandPrefix.isPrefixOf(command.getName())) {
205 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500206 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800207 }
208 }
209
210 // clear commands and responses
211 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000212 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800213 }
214
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000215 void
216 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800217 {
218 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000219 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800220 }
221 }
222
223public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400224 static ControlParameters
225 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400226 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 {
228 return ControlParameters()
229 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400230 .setFaceId(faceId)
231 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 .setCost(10)
233 .setFlags(0)
234 .setExpirationPeriod(expiry);
235 }
236
Davide Pesavento22db5392017-04-14 00:56:43 -0400237 static ControlParameters
238 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800239 {
240 return ControlParameters()
241 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400242 .setFaceId(faceId)
243 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800244 }
245
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000246protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800247 ConfigurationStatus m_status;
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700248 Name m_anchorId;
249 Name m_derivedId;
Yanbiao Licf0db022016-01-29 00:54:25 -0800250
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000251 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500252 rib::Rib m_rib;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400253 MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800255};
256
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500257BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800258BOOST_AUTO_TEST_SUITE(TestRibManager)
259
260class AddTopPrefixFixture : public RibManagerFixture
261{
262public:
263 AddTopPrefixFixture()
264 : RibManagerFixture({true, true}, false)
265 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500266 }
267};
268
Yanbiao Licf0db022016-01-29 00:54:25 -0800269BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700270{
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400271 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600272
Yanbiao Licf0db022016-01-29 00:54:25 -0800273 std::vector<Name> ribEntryNames;
274 for (auto&& entry : m_rib) {
275 ribEntryNames.push_back(entry.first);
276 }
277 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
278 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700279}
280
Yanbiao Licf0db022016-01-29 00:54:25 -0800281class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500282{
Yanbiao Licf0db022016-01-29 00:54:25 -0800283public:
284 UnauthorizedRibManagerFixture()
285 : RibManagerFixture({false, false}, true)
286 {
287 }
288};
Vince Lehman72446ec2014-07-09 10:50:02 -0500289
Yanbiao Licf0db022016-01-29 00:54:25 -0800290class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
291{
292public:
293 LocalhostAuthorizedRibManagerFixture()
294 : RibManagerFixture({true, false}, true)
295 {
296 }
297};
Vince Lehman72446ec2014-07-09 10:50:02 -0500298
Yanbiao Licf0db022016-01-29 00:54:25 -0800299class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
300{
301public:
302 LocalhopAuthorizedRibManagerFixture()
303 : RibManagerFixture({false, true}, true)
304 {
305 }
306};
307
308class AuthorizedRibManagerFixture : public RibManagerFixture
309{
310public:
311 AuthorizedRibManagerFixture()
312 : RibManagerFixture({true, true}, true)
313 {
314 }
315};
316
Davide Pesaventof56cf632024-01-27 22:22:06 -0500317template<typename Fixture, typename Format>
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400318struct FixtureWithFormat : public Fixture
319{
Davide Pesaventof56cf632024-01-27 22:22:06 -0500320 static constexpr ndn::security::SignedInterestFormat signedInterestFmt = Format::value;
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400321};
322
Davide Pesaventof56cf632024-01-27 22:22:06 -0500323using AllFixtures = boost::mp11::mp_product<
324 FixtureWithFormat,
325 boost::mp11::mp_list<UnauthorizedRibManagerFixture,
326 LocalhostAuthorizedRibManagerFixture,
327 LocalhopAuthorizedRibManagerFixture,
328 AuthorizedRibManagerFixture>,
329 boost::mp11::mp_list_c<ndn::security::SignedInterestFormat,
330 ndn::security::SignedInterestFormat::V02,
331 ndn::security::SignedInterestFormat::V03>
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400332>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800333
334BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
335{
336 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400337 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters,
338 T::signedInterestFmt);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700339 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters,
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400340 T::signedInterestFmt, this->m_derivedId);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700341 if (this->m_status.isLocalhopConfigured) {
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400342 commandHop.setTag(std::make_shared<lp::IncomingFaceIdTag>(123));
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700343 }
Davide Pesavento21e24f92025-01-10 22:22:43 -0500344 auto successResp = this->makeResponse(200, "OK", parameters);
Yanbiao Licf0db022016-01-29 00:54:25 -0800345 auto failureResp = ControlResponse(403, "authorization rejected");
346
347 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
348 this->receiveInterest(commandHost);
349 this->receiveInterest(commandHop);
350
351 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
352 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
353 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
354
355 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000356 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800357 ManagerCommonFixture::CheckResponseResult::OK);
358 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000359 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 ManagerCommonFixture::CheckResponseResult::OK);
361 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500362}
363
Yanbiao Licf0db022016-01-29 00:54:25 -0800364BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
365
366BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500367{
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
369 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500370
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000371 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
372 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
373 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
374 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500375
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 receiveInterest(commandRegister);
377 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500378
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500380 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "OK", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800381 CheckResponseResult::OK);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500382 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "OK", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500384
Junxiao Shidf1dc652019-08-30 19:03:19 +0000385 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
386 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
387 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
388 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
389 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500390}
391
Yanbiao Licf0db022016-01-29 00:54:25 -0800392BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500393{
Yanbiao Licf0db022016-01-29 00:54:25 -0800394 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
395 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
396 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
397 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500398
Yanbiao Licf0db022016-01-29 00:54:25 -0800399 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000400 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
401 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
402 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
403 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 receiveInterest(commandRegister);
406 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500407
Yanbiao Licf0db022016-01-29 00:54:25 -0800408 paramsRegister.setFaceId(inFaceId);
409 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500410
Yanbiao Licf0db022016-01-29 00:54:25 -0800411 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500412 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "OK", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800413 CheckResponseResult::OK);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500414 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "OK", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800415 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500416
Junxiao Shidf1dc652019-08-30 19:03:19 +0000417 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
418 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
419 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
420 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
421 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500422}
423
Yanbiao Licf0db022016-01-29 00:54:25 -0800424BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000425{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500426 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800427 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
428 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(), 2); // the registered route has expired
432 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
433 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
434 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
435 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000436
Junxiao Shidf1dc652019-08-30 19:03:19 +0000437 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500438 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800439 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000440
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500441 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000442 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
443 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
444 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000445}
446
Junxiao Shi75306352018-02-01 21:59:44 +0000447BOOST_AUTO_TEST_CASE(NameTooLong)
448{
449 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400450 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000451 prefix.append("A");
452 }
453 auto params = makeRegisterParameters(prefix, 2899);
454 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
jaczhib0657682025-01-08 23:01:45 -0800455
Junxiao Shi75306352018-02-01 21:59:44 +0000456 receiveInterest(command);
457
458 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400459 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
460 ControlResponse(414, "Route prefix cannot exceed " +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500461 std::to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000462 CheckResponseResult::OK);
463
Junxiao Shidf1dc652019-08-30 19:03:19 +0000464 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000465}
466
Yanbiao Licf0db022016-01-29 00:54:25 -0800467BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
468
jaczhib0657682025-01-08 23:01:45 -0800469BOOST_FIXTURE_TEST_SUITE(PrefixAnnounce, LocalhostAuthorizedRibManagerFixture)
470
471BOOST_AUTO_TEST_CASE(Basic)
472{
473 const uint64_t announceFaceId = 1234;
474
475 ndn::PrefixAnnouncement pa = signPrefixAnn(makePrefixAnn("/test-prefix-announce", 10_s), m_keyChain);
476 auto commandAnnounce = makeControlCommandRequest("/localhost/nfd/rib/announce", pa);
477 commandAnnounce.setTag(make_shared<lp::IncomingFaceIdTag>(announceFaceId));
478
479 auto paramsUnregister = makeUnregisterParameters("/test-prefix-announce");
480 paramsUnregister.setOrigin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
481 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
482 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
483 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(announceFaceId)); // same incoming face
484
485 receiveInterest(commandAnnounce);
486 receiveInterest(commandUnregister);
487
488 ControlParameters paramsAnnounceResponse;
489 paramsAnnounceResponse.setName("/test-prefix-announce")
490 .setFaceId(announceFaceId)
491 .setOrigin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
492 .setCost(rib::Route::PA_ROUTE_COST)
493 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
494 .setExpirationPeriod(10_s);
495 paramsUnregister.setFaceId(announceFaceId);
496
497 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500498 BOOST_CHECK_EQUAL(checkResponse(0, commandAnnounce.getName(), makeResponse(200, "OK", paramsAnnounceResponse)),
jaczhib0657682025-01-08 23:01:45 -0800499 CheckResponseResult::OK);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500500 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "OK", paramsUnregister)),
jaczhib0657682025-01-08 23:01:45 -0800501 CheckResponseResult::OK);
502
503 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
504 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
Davide Pesavento21e24f92025-01-10 22:22:43 -0500505 rib::FibUpdate::createAddUpdate("/test-prefix-announce", announceFaceId,
506 rib::Route::PA_ROUTE_COST));
jaczhib0657682025-01-08 23:01:45 -0800507 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
508 rib::FibUpdate::createRemoveUpdate("/test-prefix-announce", announceFaceId));
509}
510
511BOOST_AUTO_TEST_CASE(UnregisterFromDifferentFace)
512{
513 const uint64_t announceFaceId = 1234;
514
515 ndn::PrefixAnnouncement pa = signPrefixAnn(makePrefixAnn("/test-prefix-announce", 10_s), m_keyChain);
516 auto commandAnnounce = makeControlCommandRequest("/localhost/nfd/rib/announce", pa);
517 commandAnnounce.setTag(make_shared<lp::IncomingFaceIdTag>(announceFaceId));
518
519 auto paramsUnregister = makeUnregisterParameters("/test-prefix-announce", announceFaceId);
520 paramsUnregister.setOrigin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
521 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
522 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(999)); // unregister from different face
523
524 receiveInterest(commandAnnounce);
525 receiveInterest(commandUnregister);
526
527 ControlParameters paramsAnnounceResponse;
528 paramsAnnounceResponse.setName("/test-prefix-announce")
529 .setFaceId(announceFaceId)
530 .setOrigin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
531 .setCost(rib::Route::PA_ROUTE_COST)
532 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
533 .setExpirationPeriod(10_s);
534
535 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500536 BOOST_CHECK_EQUAL(checkResponse(0, commandAnnounce.getName(), makeResponse(200, "OK", paramsAnnounceResponse)),
jaczhib0657682025-01-08 23:01:45 -0800537 CheckResponseResult::OK);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500538 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "OK", paramsUnregister)),
jaczhib0657682025-01-08 23:01:45 -0800539 CheckResponseResult::OK);
540
541 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
542 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
Davide Pesavento21e24f92025-01-10 22:22:43 -0500543 rib::FibUpdate::createAddUpdate("/test-prefix-announce", announceFaceId,
544 rib::Route::PA_ROUTE_COST));
jaczhib0657682025-01-08 23:01:45 -0800545 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
546 rib::FibUpdate::createRemoveUpdate("/test-prefix-announce", announceFaceId));
547}
548
549BOOST_AUTO_TEST_CASE(NameTooLong)
550{
551 Name prefix;
552 while (prefix.size() <= Fib::getMaxDepth()) {
553 prefix.append("A");
554 }
555 ndn::PrefixAnnouncement pa = signPrefixAnn(makePrefixAnn(prefix, 10_s), m_keyChain);
556 auto command = makeControlCommandRequest("/localhost/nfd/rib/announce", pa);
557 command.setTag(make_shared<lp::IncomingFaceIdTag>(333));
558
559 receiveInterest(command);
560
561 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
562 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
563 ControlResponse(414, "Route prefix cannot exceed " +
564 std::to_string(Fib::getMaxDepth()) + " components")),
565 CheckResponseResult::OK);
566
567 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
568}
569
570BOOST_AUTO_TEST_SUITE_END() // PrefixAnnounce
571
Yanbiao Licf0db022016-01-29 00:54:25 -0800572BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500573{
Yanbiao Licf0db022016-01-29 00:54:25 -0800574 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500575 auto generateRoute = [&faceId] () -> rib::Route {
576 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800577 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400578 route.cost = route.faceId * 10;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400579 route.expires = std::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800580 return route;
581 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500582
Yanbiao Licf0db022016-01-29 00:54:25 -0800583 const size_t nEntries = 108;
584 std::set<Name> actualPrefixes;
585 for (size_t i = 0; i < nEntries; ++i) {
586 Name prefix = Name("/test-dataset").appendNumber(i);
587 actualPrefixes.insert(prefix);
588 m_rib.insert(prefix, generateRoute());
589 if (i & 0x1) {
590 m_rib.insert(prefix, generateRoute());
591 m_rib.insert(prefix, generateRoute());
592 }
593 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500594
Junxiao Shi9d727852019-05-14 13:44:22 -0600595 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500596
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400597 Block content = concatenateResponses();
598 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800599 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
600
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400601 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800602 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400603 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400604 BOOST_TEST_INFO_SCOPE(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800605 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800606 actualPrefixes.erase(decodedEntry.getName());
607
608 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
609 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
610
611 auto matchedEntry = matchedEntryIt->second;
612 BOOST_REQUIRE(matchedEntry != nullptr);
613
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400614 expectedRecords.emplace_back();
615 expectedRecords.back().setName(matchedEntry->getName());
616 for (const auto& route : matchedEntry->getRoutes()) {
617 expectedRecords.back().addRoute(ndn::nfd::Route()
618 .setFaceId(route.faceId)
619 .setOrigin(route.origin)
620 .setCost(route.cost)
621 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800622 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800623 }
624
625 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400626 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Vince Lehman72446ec2014-07-09 10:50:02 -0500627}
628
Yanbiao Licf0db022016-01-29 00:54:25 -0800629BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
630
631BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700632{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000633 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600634
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500635 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000636 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
637 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
638 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700639}
640
Yanbiao Licf0db022016-01-29 00:54:25 -0800641BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500642{
Yanbiao Licf0db022016-01-29 00:54:25 -0800643 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
644 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
645 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
646 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
647 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
648 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500649
Vince Lehman26b215c2014-08-17 15:00:41 -0500650 ndn::nfd::FaceStatus status;
651 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700652 std::vector<ndn::nfd::FaceStatus> activeFaces;
653 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500654
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700655 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500656 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800657 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500658
Yanbiao Licf0db022016-01-29 00:54:25 -0800659 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
660 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
661 BOOST_CHECK(it2 == m_rib.end());
662 BOOST_REQUIRE(it1 != m_rib.end());
663 BOOST_CHECK(it1->second->hasFaceId(1));
664 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500665}
666
Yanbiao Licf0db022016-01-29 00:54:25 -0800667BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600668{
Yanbiao Licf0db022016-01-29 00:54:25 -0800669 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
670 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
671 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
672 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
673 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600674
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500675 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
676 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800677 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600678
Yanbiao Licf0db022016-01-29 00:54:25 -0800679 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500680 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800681 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600682
Yanbiao Licf0db022016-01-29 00:54:25 -0800683 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500684 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800685 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600686}
687
Yanbiao Licf0db022016-01-29 00:54:25 -0800688BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500689
Yanbiao Licf0db022016-01-29 00:54:25 -0800690BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500691BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500692
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400693} // namespace nfd::tests