blob: 9a063930c8bd0aa710c27f151ad6aac530123b40 [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,
138 ndn::security::signingByIdentity(anchorIdentity), opts);
139 m_keyChain.setDefaultCertificate(derivedKey, derivedCert);
140
Yanbiao Licf0db022016-01-29 00:54:25 -0800141 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000142 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800143 }
144 if (m_status.isLocalhopConfigured) {
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700145 m_manager.enableLocalhop(getLocalhopValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800146 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000147 else {
148 m_manager.disableLocalhop();
149 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800150
151 registerWithNfd();
152
153 if (shouldClearRib) {
154 clearRib();
155 }
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700156
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000157 m_face.onSendInterest.connect([=] (const Interest& interest) {
158 if (interest.matchesData(derivedCert) &&
159 m_status.isLocalhopConfigured &&
160 interest.template getTag<lp::NextHopFaceIdTag>() != nullptr) {
161 m_face.put(derivedCert);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700162 }
163 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800164 }
165
166private:
167 void
168 registerWithNfd()
169 {
170 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500171 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800172
173 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500174 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000175 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800176 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500177 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -0800178
179 resp.setCode(200).setBody(params.wireEncode());
180 shared_ptr<Data> data = make_shared<Data>(interest.getName());
181 data->setContent(resp.wireEncode());
182
183 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
184
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000185 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800186 };
187
188 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000189 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800190 if (commandPrefix.isPrefixOf(command.getName())) {
191 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500192 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800193 }
194 }
195
196 // clear commands and responses
197 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000198 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800199 }
200
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000201 void
202 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800203 {
204 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000205 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800206 }
207 }
208
209public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400210 static ControlParameters
211 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400212 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800213 {
214 return ControlParameters()
215 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400216 .setFaceId(faceId)
217 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800218 .setCost(10)
219 .setFlags(0)
220 .setExpirationPeriod(expiry);
221 }
222
Davide Pesavento22db5392017-04-14 00:56:43 -0400223 static ControlParameters
224 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 {
226 return ControlParameters()
227 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400228 .setFaceId(faceId)
229 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800230 }
231
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000232protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 ConfigurationStatus m_status;
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700234 Name m_anchorId;
235 Name m_derivedId;
Yanbiao Licf0db022016-01-29 00:54:25 -0800236
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000237 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500238 rib::Rib m_rib;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400239 MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800240 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800241};
242
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500243BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800244BOOST_AUTO_TEST_SUITE(TestRibManager)
245
246class AddTopPrefixFixture : public RibManagerFixture
247{
248public:
249 AddTopPrefixFixture()
250 : RibManagerFixture({true, true}, false)
251 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500252 }
253};
254
Yanbiao Licf0db022016-01-29 00:54:25 -0800255BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700256{
Yanbiao Licf0db022016-01-29 00:54:25 -0800257 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600258
Yanbiao Licf0db022016-01-29 00:54:25 -0800259 std::vector<Name> ribEntryNames;
260 for (auto&& entry : m_rib) {
261 ribEntryNames.push_back(entry.first);
262 }
263 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
264 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700265}
266
Yanbiao Licf0db022016-01-29 00:54:25 -0800267class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500268{
Yanbiao Licf0db022016-01-29 00:54:25 -0800269public:
270 UnauthorizedRibManagerFixture()
271 : RibManagerFixture({false, false}, true)
272 {
273 }
274};
Vince Lehman72446ec2014-07-09 10:50:02 -0500275
Yanbiao Licf0db022016-01-29 00:54:25 -0800276class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
277{
278public:
279 LocalhostAuthorizedRibManagerFixture()
280 : RibManagerFixture({true, false}, true)
281 {
282 }
283};
Vince Lehman72446ec2014-07-09 10:50:02 -0500284
Yanbiao Licf0db022016-01-29 00:54:25 -0800285class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
286{
287public:
288 LocalhopAuthorizedRibManagerFixture()
289 : RibManagerFixture({false, true}, true)
290 {
291 }
292};
293
294class AuthorizedRibManagerFixture : public RibManagerFixture
295{
296public:
297 AuthorizedRibManagerFixture()
298 : RibManagerFixture({true, true}, true)
299 {
300 }
301};
302
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400303using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800304 UnauthorizedRibManagerFixture,
305 LocalhostAuthorizedRibManagerFixture,
306 LocalhopAuthorizedRibManagerFixture,
307 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400308>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800309
310BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
311{
312 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
313 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700314 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters,
315 this->m_derivedId);
316 if (this->m_status.isLocalhopConfigured) {
317 commandHop.setTag(make_shared<lp::IncomingFaceIdTag>(123));
318 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800319 auto successResp = this->makeResponse(200, "Success", parameters);
320 auto failureResp = ControlResponse(403, "authorization rejected");
321
322 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
323 this->receiveInterest(commandHost);
324 this->receiveInterest(commandHop);
325
326 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
327 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
328 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
329
330 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000331 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800332 ManagerCommonFixture::CheckResponseResult::OK);
333 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000334 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800335 ManagerCommonFixture::CheckResponseResult::OK);
336 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500337}
338
Yanbiao Licf0db022016-01-29 00:54:25 -0800339BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
340
341BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500342{
Yanbiao Licf0db022016-01-29 00:54:25 -0800343 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
344 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500345
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000346 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
347 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
348 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
349 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500350
Yanbiao Licf0db022016-01-29 00:54:25 -0800351 receiveInterest(commandRegister);
352 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500353
Yanbiao Licf0db022016-01-29 00:54:25 -0800354 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000355 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800356 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000357 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800358 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500359
Junxiao Shidf1dc652019-08-30 19:03:19 +0000360 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
361 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
362 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
363 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
364 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500365}
366
Yanbiao Licf0db022016-01-29 00:54:25 -0800367BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500368{
Yanbiao Licf0db022016-01-29 00:54:25 -0800369 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
370 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
371 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
372 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Yanbiao Licf0db022016-01-29 00:54:25 -0800374 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000375 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
376 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
377 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
378 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500379
Yanbiao Licf0db022016-01-29 00:54:25 -0800380 receiveInterest(commandRegister);
381 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500382
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 paramsRegister.setFaceId(inFaceId);
384 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500385
Yanbiao Licf0db022016-01-29 00:54:25 -0800386 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000387 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000389 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800390 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500391
Junxiao Shidf1dc652019-08-30 19:03:19 +0000392 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
393 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
394 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
395 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
396 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500397}
398
Yanbiao Licf0db022016-01-29 00:54:25 -0800399BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000400{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500401 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
403 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000404
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500405 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000406 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2); // the registered route has expired
407 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
408 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
409 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
410 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000411
Junxiao Shidf1dc652019-08-30 19:03:19 +0000412 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500413 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800414 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000415
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500416 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000417 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
418 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
419 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000420}
421
Junxiao Shi75306352018-02-01 21:59:44 +0000422BOOST_AUTO_TEST_CASE(NameTooLong)
423{
424 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400425 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000426 prefix.append("A");
427 }
428 auto params = makeRegisterParameters(prefix, 2899);
429 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
430 receiveInterest(command);
431
432 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400433 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
434 ControlResponse(414, "Route prefix cannot exceed " +
435 to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000436 CheckResponseResult::OK);
437
Junxiao Shidf1dc652019-08-30 19:03:19 +0000438 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000439}
440
Yanbiao Licf0db022016-01-29 00:54:25 -0800441BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
442
Yanbiao Licf0db022016-01-29 00:54:25 -0800443BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500444{
Yanbiao Licf0db022016-01-29 00:54:25 -0800445 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500446 auto generateRoute = [&faceId] () -> rib::Route {
447 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800448 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400449 route.cost = route.faceId * 10;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400450 route.expires = std::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800451 return route;
452 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500453
Yanbiao Licf0db022016-01-29 00:54:25 -0800454 const size_t nEntries = 108;
455 std::set<Name> actualPrefixes;
456 for (size_t i = 0; i < nEntries; ++i) {
457 Name prefix = Name("/test-dataset").appendNumber(i);
458 actualPrefixes.insert(prefix);
459 m_rib.insert(prefix, generateRoute());
460 if (i & 0x1) {
461 m_rib.insert(prefix, generateRoute());
462 m_rib.insert(prefix, generateRoute());
463 }
464 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500465
Junxiao Shi9d727852019-05-14 13:44:22 -0600466 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500467
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400468 Block content = concatenateResponses();
469 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800470 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
471
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400472 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800473 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400474 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800475 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800476 actualPrefixes.erase(decodedEntry.getName());
477
478 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
479 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
480
481 auto matchedEntry = matchedEntryIt->second;
482 BOOST_REQUIRE(matchedEntry != nullptr);
483
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400484 expectedRecords.emplace_back();
485 expectedRecords.back().setName(matchedEntry->getName());
486 for (const auto& route : matchedEntry->getRoutes()) {
487 expectedRecords.back().addRoute(ndn::nfd::Route()
488 .setFaceId(route.faceId)
489 .setOrigin(route.origin)
490 .setCost(route.cost)
491 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800492 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800493 }
494
495 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400496 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Vince Lehman72446ec2014-07-09 10:50:02 -0500497}
498
Yanbiao Licf0db022016-01-29 00:54:25 -0800499BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
500
501BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700502{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000503 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600504
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500505 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000506 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
507 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
508 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700509}
510
Yanbiao Licf0db022016-01-29 00:54:25 -0800511BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500512{
Yanbiao Licf0db022016-01-29 00:54:25 -0800513 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
514 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
515 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
516 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
517 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
518 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500519
Vince Lehman26b215c2014-08-17 15:00:41 -0500520 ndn::nfd::FaceStatus status;
521 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700522 std::vector<ndn::nfd::FaceStatus> activeFaces;
523 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500524
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700525 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500526 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800527 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500528
Yanbiao Licf0db022016-01-29 00:54:25 -0800529 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
530 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
531 BOOST_CHECK(it2 == m_rib.end());
532 BOOST_REQUIRE(it1 != m_rib.end());
533 BOOST_CHECK(it1->second->hasFaceId(1));
534 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500535}
536
Yanbiao Licf0db022016-01-29 00:54:25 -0800537BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600538{
Yanbiao Licf0db022016-01-29 00:54:25 -0800539 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
540 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
541 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
542 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
543 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600544
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500545 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
546 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800547 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600548
Yanbiao Licf0db022016-01-29 00:54:25 -0800549 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500550 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800551 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600552
Yanbiao Licf0db022016-01-29 00:54:25 -0800553 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500554 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800555 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600556}
557
Yanbiao Licf0db022016-01-29 00:54:25 -0800558BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500559
Yanbiao Licf0db022016-01-29 00:54:25 -0800560BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500561BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500562
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400563} // namespace nfd::tests