blob: 43c69dfaa1a06b42a1efeb198532562b57c36cfb [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/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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
Yanbiao Licf0db022016-01-29 00:54:25 -0800120class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500121{
122public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400123 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shidf1dc652019-08-30 19:03:19 +0000124 : m_status(status)
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700125 , m_anchorId("/TrustAnchor-identity")
126 , m_derivedId("/TrustAnchor-identity/Derived-identity")
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000127 , m_nfdController(m_face, m_keyChain)
128 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400129 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -0500130 {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000131 auto anchorIdentity = m_keyChain.createIdentity(m_anchorId);
Davide Pesavento21353752020-11-20 00:43:44 -0500132 saveIdentityCert(m_anchorId, "signer.ndncert", true);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700133
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000134 auto derivedKey = m_keyChain.createIdentity(m_derivedId).getDefaultKey();
135 auto derivedSelfSigned = derivedKey.getDefaultCertificate();
136 ndn::security::MakeCertificateOptions opts;
137 opts.validity = derivedSelfSigned.getValidityPeriod();
138 auto derivedCert = m_keyChain.makeCertificate(derivedSelfSigned,
Davide Pesavento20cafa82022-07-25 01:15:03 -0400139 ndn::security::signingByIdentity(anchorIdentity),
140 opts);
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000141 m_keyChain.setDefaultCertificate(derivedKey, derivedCert);
142
Yanbiao Licf0db022016-01-29 00:54:25 -0800143 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000144 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800145 }
Davide Pesavento20cafa82022-07-25 01:15:03 -0400146
Yanbiao Licf0db022016-01-29 00:54:25 -0800147 if (m_status.isLocalhopConfigured) {
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700148 m_manager.enableLocalhop(getLocalhopValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800149 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000150 else {
151 m_manager.disableLocalhop();
152 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800153
154 registerWithNfd();
155
156 if (shouldClearRib) {
157 clearRib();
158 }
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700159
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000160 m_face.onSendInterest.connect([=] (const Interest& interest) {
161 if (interest.matchesData(derivedCert) &&
162 m_status.isLocalhopConfigured &&
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400163 interest.getTag<lp::NextHopFaceIdTag>() != nullptr) {
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000164 m_face.put(derivedCert);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700165 }
166 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800167 }
168
169private:
170 void
171 registerWithNfd()
172 {
173 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500174 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800175
176 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400177 ControlParameters params(interest.getName().at(4).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000178 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Davide Pesavento20cafa82022-07-25 01:15:03 -0400179 params.setFaceId(1)
180 .setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500181 ControlResponse resp;
Davide Pesavento20cafa82022-07-25 01:15:03 -0400182 resp.setCode(200)
183 .setBody(params.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800184
Davide Pesavento20cafa82022-07-25 01:15:03 -0400185 auto data = make_shared<Data>(interest.getName());
Yanbiao Licf0db022016-01-29 00:54:25 -0800186 data->setContent(resp.wireEncode());
Yanbiao Licf0db022016-01-29 00:54:25 -0800187 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
188
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500189 boost::asio::post(m_face.getIoContext(), [this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800190 };
191
Davide Pesavento20cafa82022-07-25 01:15:03 -0400192 const Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000193 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800194 if (commandPrefix.isPrefixOf(command.getName())) {
195 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500196 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800197 }
198 }
199
200 // clear commands and responses
201 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000202 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800203 }
204
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000205 void
206 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800207 {
208 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000209 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800210 }
211 }
212
213public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400214 static ControlParameters
215 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400216 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800217 {
218 return ControlParameters()
219 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400220 .setFaceId(faceId)
221 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800222 .setCost(10)
223 .setFlags(0)
224 .setExpirationPeriod(expiry);
225 }
226
Davide Pesavento22db5392017-04-14 00:56:43 -0400227 static ControlParameters
228 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 {
230 return ControlParameters()
231 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400232 .setFaceId(faceId)
233 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800234 }
235
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000236protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800237 ConfigurationStatus m_status;
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700238 Name m_anchorId;
239 Name m_derivedId;
Yanbiao Licf0db022016-01-29 00:54:25 -0800240
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000241 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500242 rib::Rib m_rib;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400243 MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800244 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800245};
246
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500247BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800248BOOST_AUTO_TEST_SUITE(TestRibManager)
249
250class AddTopPrefixFixture : public RibManagerFixture
251{
252public:
253 AddTopPrefixFixture()
254 : RibManagerFixture({true, true}, false)
255 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500256 }
257};
258
Yanbiao Licf0db022016-01-29 00:54:25 -0800259BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700260{
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400261 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600262
Yanbiao Licf0db022016-01-29 00:54:25 -0800263 std::vector<Name> ribEntryNames;
264 for (auto&& entry : m_rib) {
265 ribEntryNames.push_back(entry.first);
266 }
267 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
268 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700269}
270
Yanbiao Licf0db022016-01-29 00:54:25 -0800271class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500272{
Yanbiao Licf0db022016-01-29 00:54:25 -0800273public:
274 UnauthorizedRibManagerFixture()
275 : RibManagerFixture({false, false}, true)
276 {
277 }
278};
Vince Lehman72446ec2014-07-09 10:50:02 -0500279
Yanbiao Licf0db022016-01-29 00:54:25 -0800280class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
281{
282public:
283 LocalhostAuthorizedRibManagerFixture()
284 : RibManagerFixture({true, false}, true)
285 {
286 }
287};
Vince Lehman72446ec2014-07-09 10:50:02 -0500288
Yanbiao Licf0db022016-01-29 00:54:25 -0800289class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
290{
291public:
292 LocalhopAuthorizedRibManagerFixture()
293 : RibManagerFixture({false, true}, true)
294 {
295 }
296};
297
298class AuthorizedRibManagerFixture : public RibManagerFixture
299{
300public:
301 AuthorizedRibManagerFixture()
302 : RibManagerFixture({true, true}, true)
303 {
304 }
305};
306
Davide Pesaventof56cf632024-01-27 22:22:06 -0500307template<typename Fixture, typename Format>
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400308struct FixtureWithFormat : public Fixture
309{
Davide Pesaventof56cf632024-01-27 22:22:06 -0500310 static constexpr ndn::security::SignedInterestFormat signedInterestFmt = Format::value;
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400311};
312
Davide Pesaventof56cf632024-01-27 22:22:06 -0500313using AllFixtures = boost::mp11::mp_product<
314 FixtureWithFormat,
315 boost::mp11::mp_list<UnauthorizedRibManagerFixture,
316 LocalhostAuthorizedRibManagerFixture,
317 LocalhopAuthorizedRibManagerFixture,
318 AuthorizedRibManagerFixture>,
319 boost::mp11::mp_list_c<ndn::security::SignedInterestFormat,
320 ndn::security::SignedInterestFormat::V02,
321 ndn::security::SignedInterestFormat::V03>
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400322>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800323
324BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
325{
326 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400327 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters,
328 T::signedInterestFmt);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700329 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters,
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400330 T::signedInterestFmt, this->m_derivedId);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700331 if (this->m_status.isLocalhopConfigured) {
Davide Pesaventob83d3df2022-09-13 14:04:34 -0400332 commandHop.setTag(std::make_shared<lp::IncomingFaceIdTag>(123));
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700333 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800334 auto successResp = this->makeResponse(200, "Success", parameters);
335 auto failureResp = ControlResponse(403, "authorization rejected");
336
337 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
338 this->receiveInterest(commandHost);
339 this->receiveInterest(commandHop);
340
341 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
342 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
343 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
344
345 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000346 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800347 ManagerCommonFixture::CheckResponseResult::OK);
348 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000349 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800350 ManagerCommonFixture::CheckResponseResult::OK);
351 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500352}
353
Yanbiao Licf0db022016-01-29 00:54:25 -0800354BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
355
356BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500357{
Yanbiao Licf0db022016-01-29 00:54:25 -0800358 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
359 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500360
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000361 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
362 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
363 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
364 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500365
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 receiveInterest(commandRegister);
367 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500368
Yanbiao Licf0db022016-01-29 00:54:25 -0800369 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000370 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000372 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500374
Junxiao Shidf1dc652019-08-30 19:03:19 +0000375 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
376 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
377 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
378 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
379 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500380}
381
Yanbiao Licf0db022016-01-29 00:54:25 -0800382BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500383{
Yanbiao Licf0db022016-01-29 00:54:25 -0800384 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
385 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
386 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
387 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500388
Yanbiao Licf0db022016-01-29 00:54:25 -0800389 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000390 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
391 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
392 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
393 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500394
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 receiveInterest(commandRegister);
396 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500397
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 paramsRegister.setFaceId(inFaceId);
399 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500400
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000402 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800403 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000404 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500406
Junxiao Shidf1dc652019-08-30 19:03:19 +0000407 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
408 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
409 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
410 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
411 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500412}
413
Yanbiao Licf0db022016-01-29 00:54:25 -0800414BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000415{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500416 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800417 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
418 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000419
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500420 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000421 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2); // the registered route has expired
422 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
423 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
424 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
425 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000426
Junxiao Shidf1dc652019-08-30 19:03:19 +0000427 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500428 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800429 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000430
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500431 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000432 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
433 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
434 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000435}
436
Junxiao Shi75306352018-02-01 21:59:44 +0000437BOOST_AUTO_TEST_CASE(NameTooLong)
438{
439 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400440 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000441 prefix.append("A");
442 }
443 auto params = makeRegisterParameters(prefix, 2899);
444 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
445 receiveInterest(command);
446
447 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400448 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
449 ControlResponse(414, "Route prefix cannot exceed " +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500450 std::to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000451 CheckResponseResult::OK);
452
Junxiao Shidf1dc652019-08-30 19:03:19 +0000453 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000454}
455
Yanbiao Licf0db022016-01-29 00:54:25 -0800456BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
457
Yanbiao Licf0db022016-01-29 00:54:25 -0800458BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500459{
Yanbiao Licf0db022016-01-29 00:54:25 -0800460 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500461 auto generateRoute = [&faceId] () -> rib::Route {
462 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800463 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400464 route.cost = route.faceId * 10;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400465 route.expires = std::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800466 return route;
467 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500468
Yanbiao Licf0db022016-01-29 00:54:25 -0800469 const size_t nEntries = 108;
470 std::set<Name> actualPrefixes;
471 for (size_t i = 0; i < nEntries; ++i) {
472 Name prefix = Name("/test-dataset").appendNumber(i);
473 actualPrefixes.insert(prefix);
474 m_rib.insert(prefix, generateRoute());
475 if (i & 0x1) {
476 m_rib.insert(prefix, generateRoute());
477 m_rib.insert(prefix, generateRoute());
478 }
479 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500480
Junxiao Shi9d727852019-05-14 13:44:22 -0600481 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500482
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400483 Block content = concatenateResponses();
484 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800485 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
486
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400487 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800488 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400489 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400490 BOOST_TEST_INFO_SCOPE(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800491 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800492 actualPrefixes.erase(decodedEntry.getName());
493
494 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
495 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
496
497 auto matchedEntry = matchedEntryIt->second;
498 BOOST_REQUIRE(matchedEntry != nullptr);
499
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400500 expectedRecords.emplace_back();
501 expectedRecords.back().setName(matchedEntry->getName());
502 for (const auto& route : matchedEntry->getRoutes()) {
503 expectedRecords.back().addRoute(ndn::nfd::Route()
504 .setFaceId(route.faceId)
505 .setOrigin(route.origin)
506 .setCost(route.cost)
507 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800508 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800509 }
510
511 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400512 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Vince Lehman72446ec2014-07-09 10:50:02 -0500513}
514
Yanbiao Licf0db022016-01-29 00:54:25 -0800515BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
516
517BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700518{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000519 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600520
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500521 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000522 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
523 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
524 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700525}
526
Yanbiao Licf0db022016-01-29 00:54:25 -0800527BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500528{
Yanbiao Licf0db022016-01-29 00:54:25 -0800529 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
530 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
531 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
532 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
533 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
534 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500535
Vince Lehman26b215c2014-08-17 15:00:41 -0500536 ndn::nfd::FaceStatus status;
537 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700538 std::vector<ndn::nfd::FaceStatus> activeFaces;
539 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500540
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700541 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500542 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800543 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500544
Yanbiao Licf0db022016-01-29 00:54:25 -0800545 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
546 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
547 BOOST_CHECK(it2 == m_rib.end());
548 BOOST_REQUIRE(it1 != m_rib.end());
549 BOOST_CHECK(it1->second->hasFaceId(1));
550 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500551}
552
Yanbiao Licf0db022016-01-29 00:54:25 -0800553BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600554{
Yanbiao Licf0db022016-01-29 00:54:25 -0800555 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
556 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
557 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
558 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
559 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600560
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500561 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
562 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800563 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600564
Yanbiao Licf0db022016-01-29 00:54:25 -0800565 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500566 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800567 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600568
Yanbiao Licf0db022016-01-29 00:54:25 -0800569 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500570 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800571 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600572}
573
Yanbiao Licf0db022016-01-29 00:54:25 -0800574BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500575
Yanbiao Licf0db022016-01-29 00:54:25 -0800576BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500577BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500578
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400579} // namespace nfd::tests