blob: b688996c9cba5dad8ef0b6a9acccce2ff334d23d [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/*
Alexander Afanasyeva1583702020-06-03 13:55:45 -04003 * Copyright (c) 2014-2020, 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
Vince Lehman72446ec2014-07-09 10:50:02 -050037namespace nfd {
Vince Lehman72446ec2014-07-09 10:50:02 -050038namespace tests {
39
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 {
Davide Pesavento21353752020-11-20 00:43:44 -0500131 m_keyChain.createIdentity(m_anchorId);
132 m_keyChain.createIdentity(m_derivedId);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700133
134 m_derivedCert = m_keyChain.getPib().getIdentity(m_derivedId).getDefaultKey().getDefaultCertificate();
135 ndn::SignatureInfo signatureInfo;
136 signatureInfo.setValidityPeriod(m_derivedCert.getValidityPeriod());
137 ndn::security::SigningInfo signingInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
138 m_anchorId, signatureInfo);
139 m_keyChain.sign(m_derivedCert, signingInfo);
Davide Pesavento21353752020-11-20 00:43:44 -0500140 saveIdentityCert(m_anchorId, "signer.ndncert", true);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700141
Yanbiao Licf0db022016-01-29 00:54:25 -0800142 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +0000143 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800144 }
145 if (m_status.isLocalhopConfigured) {
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700146 m_manager.enableLocalhop(getLocalhopValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -0800147 }
Junxiao Shif4cfed12018-08-22 23:26:29 +0000148 else {
149 m_manager.disableLocalhop();
150 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800151
152 registerWithNfd();
153
154 if (shouldClearRib) {
155 clearRib();
156 }
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700157
158 m_face.onSendInterest.connect([this] (const Interest& interest) {
159 if (interest.getName().isPrefixOf(m_derivedCert.getName())) {
160 if (m_status.isLocalhopConfigured && interest.template getTag<lp::NextHopFaceIdTag>() != nullptr) {
161 m_face.put(m_derivedCert);
162 }
163 }
164 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800165 }
166
167private:
168 void
169 registerWithNfd()
170 {
171 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500172 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800173
174 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500175 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000176 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800177 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500178 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -0800179
180 resp.setCode(200).setBody(params.wireEncode());
181 shared_ptr<Data> data = make_shared<Data>(interest.getName());
182 data->setContent(resp.wireEncode());
183
184 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
185
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000186 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800187 };
188
189 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000190 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800191 if (commandPrefix.isPrefixOf(command.getName())) {
192 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500193 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800194 }
195 }
196
197 // clear commands and responses
198 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000199 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800200 }
201
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000202 void
203 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800204 {
205 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000206 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800207 }
208 }
209
210public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400211 static ControlParameters
212 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400213 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800214 {
215 return ControlParameters()
216 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400217 .setFaceId(faceId)
218 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800219 .setCost(10)
220 .setFlags(0)
221 .setExpirationPeriod(expiry);
222 }
223
Davide Pesavento22db5392017-04-14 00:56:43 -0400224 static ControlParameters
225 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 {
227 return ControlParameters()
228 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400229 .setFaceId(faceId)
230 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 }
232
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000233protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800234 ConfigurationStatus m_status;
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700235 Name m_anchorId;
236 Name m_derivedId;
Alexander Afanasyeva1583702020-06-03 13:55:45 -0400237 ndn::security::Certificate m_derivedCert;
Yanbiao Licf0db022016-01-29 00:54:25 -0800238
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000239 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500240 rib::Rib m_rib;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000241 rib::tests::MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800242 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800243};
244
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500245BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800246BOOST_AUTO_TEST_SUITE(TestRibManager)
247
248class AddTopPrefixFixture : public RibManagerFixture
249{
250public:
251 AddTopPrefixFixture()
252 : RibManagerFixture({true, true}, false)
253 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500254 }
255};
256
Yanbiao Licf0db022016-01-29 00:54:25 -0800257BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700258{
Yanbiao Licf0db022016-01-29 00:54:25 -0800259 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600260
Yanbiao Licf0db022016-01-29 00:54:25 -0800261 std::vector<Name> ribEntryNames;
262 for (auto&& entry : m_rib) {
263 ribEntryNames.push_back(entry.first);
264 }
265 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
266 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700267}
268
Yanbiao Licf0db022016-01-29 00:54:25 -0800269class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500270{
Yanbiao Licf0db022016-01-29 00:54:25 -0800271public:
272 UnauthorizedRibManagerFixture()
273 : RibManagerFixture({false, false}, true)
274 {
275 }
276};
Vince Lehman72446ec2014-07-09 10:50:02 -0500277
Yanbiao Licf0db022016-01-29 00:54:25 -0800278class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
279{
280public:
281 LocalhostAuthorizedRibManagerFixture()
282 : RibManagerFixture({true, false}, true)
283 {
284 }
285};
Vince Lehman72446ec2014-07-09 10:50:02 -0500286
Yanbiao Licf0db022016-01-29 00:54:25 -0800287class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
288{
289public:
290 LocalhopAuthorizedRibManagerFixture()
291 : RibManagerFixture({false, true}, true)
292 {
293 }
294};
295
296class AuthorizedRibManagerFixture : public RibManagerFixture
297{
298public:
299 AuthorizedRibManagerFixture()
300 : RibManagerFixture({true, true}, true)
301 {
302 }
303};
304
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400305using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800306 UnauthorizedRibManagerFixture,
307 LocalhostAuthorizedRibManagerFixture,
308 LocalhopAuthorizedRibManagerFixture,
309 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400310>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800311
312BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
313{
314 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
315 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
Zhiyi Zhanga499aa22019-09-24 15:00:40 -0700316 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters,
317 this->m_derivedId);
318 if (this->m_status.isLocalhopConfigured) {
319 commandHop.setTag(make_shared<lp::IncomingFaceIdTag>(123));
320 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800321 auto successResp = this->makeResponse(200, "Success", parameters);
322 auto failureResp = ControlResponse(403, "authorization rejected");
323
324 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
325 this->receiveInterest(commandHost);
326 this->receiveInterest(commandHop);
327
328 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
329 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
330 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
331
332 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000333 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800334 ManagerCommonFixture::CheckResponseResult::OK);
335 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000336 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800337 ManagerCommonFixture::CheckResponseResult::OK);
338 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500339}
340
Yanbiao Licf0db022016-01-29 00:54:25 -0800341BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
342
343BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500344{
Yanbiao Licf0db022016-01-29 00:54:25 -0800345 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
346 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500347
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000348 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
349 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
350 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
351 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500352
Yanbiao Licf0db022016-01-29 00:54:25 -0800353 receiveInterest(commandRegister);
354 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500355
Yanbiao Licf0db022016-01-29 00:54:25 -0800356 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000357 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800358 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000359 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500361
Junxiao Shidf1dc652019-08-30 19:03:19 +0000362 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
363 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
364 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
365 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
366 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500367}
368
Yanbiao Licf0db022016-01-29 00:54:25 -0800369BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500370{
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
372 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
373 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
374 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500375
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000377 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
378 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
379 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
380 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500381
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 receiveInterest(commandRegister);
383 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500384
Yanbiao Licf0db022016-01-29 00:54:25 -0800385 paramsRegister.setFaceId(inFaceId);
386 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500387
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000389 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800390 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000391 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800392 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500393
Junxiao Shidf1dc652019-08-30 19:03:19 +0000394 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
395 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
396 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
397 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
398 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500399}
400
Yanbiao Licf0db022016-01-29 00:54:25 -0800401BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000402{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500403 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800404 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
405 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000406
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500407 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000408 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2); // the registered route has expired
409 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
410 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
411 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
412 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000413
Junxiao Shidf1dc652019-08-30 19:03:19 +0000414 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500415 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000417
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500418 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000419 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
420 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
421 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000422}
423
Junxiao Shi75306352018-02-01 21:59:44 +0000424BOOST_AUTO_TEST_CASE(NameTooLong)
425{
426 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400427 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000428 prefix.append("A");
429 }
430 auto params = makeRegisterParameters(prefix, 2899);
431 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
432 receiveInterest(command);
433
434 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400435 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
436 ControlResponse(414, "Route prefix cannot exceed " +
437 to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000438 CheckResponseResult::OK);
439
Junxiao Shidf1dc652019-08-30 19:03:19 +0000440 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000441}
442
Yanbiao Licf0db022016-01-29 00:54:25 -0800443BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
444
Yanbiao Licf0db022016-01-29 00:54:25 -0800445BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500446{
Yanbiao Licf0db022016-01-29 00:54:25 -0800447 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500448 auto generateRoute = [&faceId] () -> rib::Route {
449 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800450 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400451 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400452 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800453 return route;
454 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500455
Yanbiao Licf0db022016-01-29 00:54:25 -0800456 const size_t nEntries = 108;
457 std::set<Name> actualPrefixes;
458 for (size_t i = 0; i < nEntries; ++i) {
459 Name prefix = Name("/test-dataset").appendNumber(i);
460 actualPrefixes.insert(prefix);
461 m_rib.insert(prefix, generateRoute());
462 if (i & 0x1) {
463 m_rib.insert(prefix, generateRoute());
464 m_rib.insert(prefix, generateRoute());
465 }
466 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500467
Junxiao Shi9d727852019-05-14 13:44:22 -0600468 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500469
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400470 Block content = concatenateResponses();
471 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800472 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
473
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400474 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800475 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400476 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800477 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 actualPrefixes.erase(decodedEntry.getName());
479
480 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
481 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
482
483 auto matchedEntry = matchedEntryIt->second;
484 BOOST_REQUIRE(matchedEntry != nullptr);
485
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400486 expectedRecords.emplace_back();
487 expectedRecords.back().setName(matchedEntry->getName());
488 for (const auto& route : matchedEntry->getRoutes()) {
489 expectedRecords.back().addRoute(ndn::nfd::Route()
490 .setFaceId(route.faceId)
491 .setOrigin(route.origin)
492 .setCost(route.cost)
493 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800494 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800495 }
496
497 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
499 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500500}
501
Yanbiao Licf0db022016-01-29 00:54:25 -0800502BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
503
504BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700505{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000506 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600507
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500508 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000509 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
510 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
511 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700512}
513
Yanbiao Licf0db022016-01-29 00:54:25 -0800514BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500515{
Yanbiao Licf0db022016-01-29 00:54:25 -0800516 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
517 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
518 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
519 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
520 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
521 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500522
Vince Lehman26b215c2014-08-17 15:00:41 -0500523 ndn::nfd::FaceStatus status;
524 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700525 std::vector<ndn::nfd::FaceStatus> activeFaces;
526 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500527
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700528 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500529 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800530 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500531
Yanbiao Licf0db022016-01-29 00:54:25 -0800532 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
533 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
534 BOOST_CHECK(it2 == m_rib.end());
535 BOOST_REQUIRE(it1 != m_rib.end());
536 BOOST_CHECK(it1->second->hasFaceId(1));
537 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500538}
539
Yanbiao Licf0db022016-01-29 00:54:25 -0800540BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600541{
Yanbiao Licf0db022016-01-29 00:54:25 -0800542 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
543 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
544 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
545 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
546 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600547
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500548 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
549 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800550 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600551
Yanbiao Licf0db022016-01-29 00:54:25 -0800552 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500553 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800554 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600555
Yanbiao Licf0db022016-01-29 00:54:25 -0800556 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500557 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800558 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600559}
560
Yanbiao Licf0db022016-01-29 00:54:25 -0800561BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500562
Yanbiao Licf0db022016-01-29 00:54:25 -0800563BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500564BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500565
566} // namespace tests
Vince Lehman72446ec2014-07-09 10:50:02 -0500567} // namespace nfd