blob: dd34b5e9dbaa8e3c2d55554fe7d49b44603d771b [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 Pesavento8a05c7f2019-02-28 02:26:19 -05003 * Copyright (c) 2014-2019, 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"
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050027#include "rib/fib-updater.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050028
Davide Pesavento78ddcab2019-02-28 22:00:03 -050029#include "manager-common-fixture.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>
Vince Lehman26b215c2014-08-17 15:00:41 -050034
Vince Lehman72446ec2014-07-09 10:50:02 -050035namespace nfd {
Vince Lehman72446ec2014-07-09 10:50:02 -050036namespace tests {
37
Yanbiao Licf0db022016-01-29 00:54:25 -080038struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050039{
Yanbiao Licf0db022016-01-29 00:54:25 -080040 bool isLocalhostConfigured;
41 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050042};
43
Junxiao Shif4cfed12018-08-22 23:26:29 +000044static ConfigSection
45getValidatorConfigSection()
46{
47 ConfigSection section;
48 section.put("trust-anchor.type", "any");
49 return section;
50}
51
Yanbiao Licf0db022016-01-29 00:54:25 -080052class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050053{
54public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040055 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000056 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080057 , m_status(status)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000058 , m_nfdController(m_face, m_keyChain)
59 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040060 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -050061 {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050062 m_rib.mockFibResponse = [] (const rib::RibUpdateBatch& batch) {
Junxiao Shi52009042018-09-10 12:33:56 +000063 BOOST_CHECK(batch.begin() != batch.end());
64 return true;
65 };
66 m_rib.wantMockFibResponseOnce = false;
Vince Lehman72446ec2014-07-09 10:50:02 -050067
Yanbiao Licf0db022016-01-29 00:54:25 -080068 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000069 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080070 }
71 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000072 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080073 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000074 else {
75 m_manager.disableLocalhop();
76 }
Yanbiao Licf0db022016-01-29 00:54:25 -080077
78 registerWithNfd();
79
80 if (shouldClearRib) {
81 clearRib();
82 }
83 }
84
85private:
86 void
87 registerWithNfd()
88 {
89 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050090 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -080091
92 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -050093 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +000094 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -080095 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050096 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -080097
98 resp.setCode(200).setBody(params.wireEncode());
99 shared_ptr<Data> data = make_shared<Data>(interest.getName());
100 data->setContent(resp.wireEncode());
101
102 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
103
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000104 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800105 };
106
107 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000108 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800109 if (commandPrefix.isPrefixOf(command.getName())) {
110 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500111 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800112 }
113 }
114
115 // clear commands and responses
116 m_responses.clear();
117 m_commands.clear();
118 }
119
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000120 void
121 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800122 {
123 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000124 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800125 }
126 }
127
128public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400129 static ControlParameters
130 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400131 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800132 {
133 return ControlParameters()
134 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400135 .setFaceId(faceId)
136 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800137 .setCost(10)
138 .setFlags(0)
139 .setExpirationPeriod(expiry);
140 }
141
Davide Pesavento22db5392017-04-14 00:56:43 -0400142 static ControlParameters
143 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800144 {
145 return ControlParameters()
146 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400147 .setFaceId(faceId)
148 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800149 }
150
Yanbiao Licf0db022016-01-29 00:54:25 -0800151public:
152 enum class CheckCommandResult {
153 OK,
154 OUT_OF_BOUNDARY,
155 WRONG_FORMAT,
156 WRONG_VERB,
157 WRONG_PARAMS_FORMAT,
158 WRONG_PARAMS_NAME,
159 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400160 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800161
162 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400163 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800164 {
165 if (idx > m_commands.size()) {
166 return CheckCommandResult::OUT_OF_BOUNDARY;
167 }
168 const auto& name = m_commands[idx].getName();
169
170 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
171 return CheckCommandResult::WRONG_FORMAT;
172 }
173 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
174
175 Name::Component expectedVerb(verbStr);
176 if (verb != expectedVerb) {
177 return CheckCommandResult::WRONG_VERB;
178 }
179
180 ControlParameters parameters;
181 try {
182 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
183 parameters.wireDecode(rawParameters);
184 }
185 catch (...) {
186 return CheckCommandResult::WRONG_PARAMS_FORMAT;
187 }
188
189 if (parameters.getName() != expectedParams.getName()) {
190 return CheckCommandResult::WRONG_PARAMS_NAME;
191 }
192
193 if (parameters.getFaceId() != expectedParams.getFaceId()) {
194 return CheckCommandResult::WRONG_PARAMS_FACE;
195 }
196
197 return CheckCommandResult::OK;
198 }
199
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000200protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400201 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800202 std::vector<Interest>& m_commands;
203 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800204
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000205 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500206 rib::Rib m_rib;
207 rib::FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800208 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800209};
210
211const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
212
213std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400214operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800215{
216 switch (result) {
217 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400218 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800219 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400220 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800221 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400222 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800223 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400224 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400226 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400228 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400230 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 };
232
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400233 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800234}
235
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500236BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800237BOOST_AUTO_TEST_SUITE(TestRibManager)
238
239class AddTopPrefixFixture : public RibManagerFixture
240{
241public:
242 AddTopPrefixFixture()
243 : RibManagerFixture({true, true}, false)
244 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500245 }
246};
247
Yanbiao Licf0db022016-01-29 00:54:25 -0800248BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700249{
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600251
Yanbiao Licf0db022016-01-29 00:54:25 -0800252 std::vector<Name> ribEntryNames;
253 for (auto&& entry : m_rib) {
254 ribEntryNames.push_back(entry.first);
255 }
256 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
257 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700258}
259
Yanbiao Licf0db022016-01-29 00:54:25 -0800260class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500261{
Yanbiao Licf0db022016-01-29 00:54:25 -0800262public:
263 UnauthorizedRibManagerFixture()
264 : RibManagerFixture({false, false}, true)
265 {
266 }
267};
Vince Lehman72446ec2014-07-09 10:50:02 -0500268
Yanbiao Licf0db022016-01-29 00:54:25 -0800269class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
270{
271public:
272 LocalhostAuthorizedRibManagerFixture()
273 : RibManagerFixture({true, false}, true)
274 {
275 }
276};
Vince Lehman72446ec2014-07-09 10:50:02 -0500277
Yanbiao Licf0db022016-01-29 00:54:25 -0800278class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
279{
280public:
281 LocalhopAuthorizedRibManagerFixture()
282 : RibManagerFixture({false, true}, true)
283 {
284 }
285};
286
287class AuthorizedRibManagerFixture : public RibManagerFixture
288{
289public:
290 AuthorizedRibManagerFixture()
291 : RibManagerFixture({true, true}, true)
292 {
293 }
294};
295
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400296using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800297 UnauthorizedRibManagerFixture,
298 LocalhostAuthorizedRibManagerFixture,
299 LocalhopAuthorizedRibManagerFixture,
300 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400301>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800302
303BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
304{
305 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
306 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
307 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
308 auto successResp = this->makeResponse(200, "Success", parameters);
309 auto failureResp = ControlResponse(403, "authorization rejected");
310
311 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
312 this->receiveInterest(commandHost);
313 this->receiveInterest(commandHop);
314
315 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
316 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
317 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
318
319 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000320 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800321 ManagerCommonFixture::CheckResponseResult::OK);
322 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000323 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800324 ManagerCommonFixture::CheckResponseResult::OK);
325 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500326}
327
Yanbiao Licf0db022016-01-29 00:54:25 -0800328BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
329
330BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500331{
Yanbiao Licf0db022016-01-29 00:54:25 -0800332 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
333 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500334
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000335 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
336 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
337 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
338 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500339
Yanbiao Licf0db022016-01-29 00:54:25 -0800340 receiveInterest(commandRegister);
341 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500342
Yanbiao Licf0db022016-01-29 00:54:25 -0800343 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000344 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800345 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000346 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800347 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500348
Yanbiao Licf0db022016-01-29 00:54:25 -0800349 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
350 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
351 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500352}
353
Yanbiao Licf0db022016-01-29 00:54:25 -0800354BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500355{
Yanbiao Licf0db022016-01-29 00:54:25 -0800356 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
357 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
358 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
359 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500360
Yanbiao Licf0db022016-01-29 00:54:25 -0800361 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000362 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
363 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
364 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
365 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500366
Yanbiao Licf0db022016-01-29 00:54:25 -0800367 receiveInterest(commandRegister);
368 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500369
Yanbiao Licf0db022016-01-29 00:54:25 -0800370 paramsRegister.setFaceId(inFaceId);
371 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500372
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000374 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800375 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000376 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800377 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500378
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
380 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
381 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500382}
383
Yanbiao Licf0db022016-01-29 00:54:25 -0800384BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000385{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500386 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800387 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
388 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000389
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500390 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
392 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
393 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000394
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 m_commands.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500396 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800397 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000398
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500399 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
401 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000402}
403
Junxiao Shi75306352018-02-01 21:59:44 +0000404BOOST_AUTO_TEST_CASE(NameTooLong)
405{
406 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400407 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000408 prefix.append("A");
409 }
410 auto params = makeRegisterParameters(prefix, 2899);
411 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
412 receiveInterest(command);
413
414 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400415 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
416 ControlResponse(414, "Route prefix cannot exceed " +
417 to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000418 CheckResponseResult::OK);
419
420 BOOST_CHECK_EQUAL(m_commands.size(), 0);
421}
422
Yanbiao Licf0db022016-01-29 00:54:25 -0800423BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
424
Yanbiao Licf0db022016-01-29 00:54:25 -0800425BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500426{
Yanbiao Licf0db022016-01-29 00:54:25 -0800427 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500428 auto generateRoute = [&faceId] () -> rib::Route {
429 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800430 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400431 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400432 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800433 return route;
434 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500435
Yanbiao Licf0db022016-01-29 00:54:25 -0800436 const size_t nEntries = 108;
437 std::set<Name> actualPrefixes;
438 for (size_t i = 0; i < nEntries; ++i) {
439 Name prefix = Name("/test-dataset").appendNumber(i);
440 actualPrefixes.insert(prefix);
441 m_rib.insert(prefix, generateRoute());
442 if (i & 0x1) {
443 m_rib.insert(prefix, generateRoute());
444 m_rib.insert(prefix, generateRoute());
445 }
446 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500447
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000448 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500449
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400450 Block content = concatenateResponses();
451 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800452 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
453
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400454 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800455 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400456 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800457 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800458 actualPrefixes.erase(decodedEntry.getName());
459
460 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
461 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
462
463 auto matchedEntry = matchedEntryIt->second;
464 BOOST_REQUIRE(matchedEntry != nullptr);
465
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400466 expectedRecords.emplace_back();
467 expectedRecords.back().setName(matchedEntry->getName());
468 for (const auto& route : matchedEntry->getRoutes()) {
469 expectedRecords.back().addRoute(ndn::nfd::Route()
470 .setFaceId(route.faceId)
471 .setOrigin(route.origin)
472 .setCost(route.cost)
473 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800474 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800475 }
476
477 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
479 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500480}
481
Yanbiao Licf0db022016-01-29 00:54:25 -0800482BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
483
484BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700485{
Yanbiao Licf0db022016-01-29 00:54:25 -0800486 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600487
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500488 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Yanbiao Licf0db022016-01-29 00:54:25 -0800489 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
490 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
491 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700492}
493
Yanbiao Licf0db022016-01-29 00:54:25 -0800494BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500495{
Yanbiao Licf0db022016-01-29 00:54:25 -0800496 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
497 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
498 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
499 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
500 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
501 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500502
Vince Lehman26b215c2014-08-17 15:00:41 -0500503 ndn::nfd::FaceStatus status;
504 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700505 std::vector<ndn::nfd::FaceStatus> activeFaces;
506 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500507
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700508 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500509 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800510 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500511
Yanbiao Licf0db022016-01-29 00:54:25 -0800512 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
513 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
514 BOOST_CHECK(it2 == m_rib.end());
515 BOOST_REQUIRE(it1 != m_rib.end());
516 BOOST_CHECK(it1->second->hasFaceId(1));
517 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500518}
519
Yanbiao Licf0db022016-01-29 00:54:25 -0800520BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600521{
Yanbiao Licf0db022016-01-29 00:54:25 -0800522 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
523 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
524 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
525 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
526 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600527
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500528 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
529 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800530 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600531
Yanbiao Licf0db022016-01-29 00:54:25 -0800532 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500533 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800534 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600535
Yanbiao Licf0db022016-01-29 00:54:25 -0800536 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500537 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800538 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600539}
540
Yanbiao Licf0db022016-01-29 00:54:25 -0800541BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500542
Yanbiao Licf0db022016-01-29 00:54:25 -0800543BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500544BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500545
546} // namespace tests
Vince Lehman72446ec2014-07-09 10:50:02 -0500547} // namespace nfd