blob: 9756a36079c1d11240e383db59e3d43abdd0e119 [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"
Junxiao Shi75306352018-02-01 21:59:44 +000027#include "core/fib-max-depth.hpp"
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050028#include "rib/fib-updater.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050029
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "manager-common-fixture.hpp"
Davide Pesavento0064c1d2018-03-03 18:43:53 -050031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Davide Pesaventod2ea2352017-03-12 20:38:09 -040033#include <ndn-cxx/mgmt/nfd/face-status.hpp>
34#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050035
Vince Lehman72446ec2014-07-09 10:50:02 -050036namespace nfd {
Vince Lehman72446ec2014-07-09 10:50:02 -050037namespace tests {
38
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
Yanbiao Licf0db022016-01-29 00:54:25 -080053class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050054{
55public:
Yanbiao Licf0db022016-01-29 00:54:25 -080056 explicit
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040057 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000058 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080059 , m_status(status)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040060 , m_scheduler(g_io)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000061 , m_nfdController(m_face, m_keyChain)
62 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040063 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
Vince Lehman72446ec2014-07-09 10:50:02 -050064 {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050065 m_rib.mockFibResponse = [] (const rib::RibUpdateBatch& batch) {
Junxiao Shi52009042018-09-10 12:33:56 +000066 BOOST_CHECK(batch.begin() != batch.end());
67 return true;
68 };
69 m_rib.wantMockFibResponseOnce = false;
Vince Lehman72446ec2014-07-09 10:50:02 -050070
Yanbiao Licf0db022016-01-29 00:54:25 -080071 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000072 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080073 }
74 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000075 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080076 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000077 else {
78 m_manager.disableLocalhop();
79 }
Yanbiao Licf0db022016-01-29 00:54:25 -080080
81 registerWithNfd();
82
83 if (shouldClearRib) {
84 clearRib();
85 }
86 }
87
88private:
89 void
90 registerWithNfd()
91 {
92 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050093 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -080094
95 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -050096 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +000097 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -080098 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050099 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -0800100
101 resp.setCode(200).setBody(params.wireEncode());
102 shared_ptr<Data> data = make_shared<Data>(interest.getName());
103 data->setContent(resp.wireEncode());
104
105 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
106
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000107 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800108 };
109
110 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000111 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800112 if (commandPrefix.isPrefixOf(command.getName())) {
113 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500114 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800115 }
116 }
117
118 // clear commands and responses
119 m_responses.clear();
120 m_commands.clear();
121 }
122
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000123 void
124 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800125 {
126 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000127 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800128 }
129 }
130
131public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400132 static ControlParameters
133 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400134 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800135 {
136 return ControlParameters()
137 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400138 .setFaceId(faceId)
139 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800140 .setCost(10)
141 .setFlags(0)
142 .setExpirationPeriod(expiry);
143 }
144
Davide Pesavento22db5392017-04-14 00:56:43 -0400145 static ControlParameters
146 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800147 {
148 return ControlParameters()
149 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400150 .setFaceId(faceId)
151 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800152 }
153
Yanbiao Licf0db022016-01-29 00:54:25 -0800154public:
155 enum class CheckCommandResult {
156 OK,
157 OUT_OF_BOUNDARY,
158 WRONG_FORMAT,
159 WRONG_VERB,
160 WRONG_PARAMS_FORMAT,
161 WRONG_PARAMS_NAME,
162 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400163 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800164
165 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400166 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800167 {
168 if (idx > m_commands.size()) {
169 return CheckCommandResult::OUT_OF_BOUNDARY;
170 }
171 const auto& name = m_commands[idx].getName();
172
173 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
174 return CheckCommandResult::WRONG_FORMAT;
175 }
176 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
177
178 Name::Component expectedVerb(verbStr);
179 if (verb != expectedVerb) {
180 return CheckCommandResult::WRONG_VERB;
181 }
182
183 ControlParameters parameters;
184 try {
185 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
186 parameters.wireDecode(rawParameters);
187 }
188 catch (...) {
189 return CheckCommandResult::WRONG_PARAMS_FORMAT;
190 }
191
192 if (parameters.getName() != expectedParams.getName()) {
193 return CheckCommandResult::WRONG_PARAMS_NAME;
194 }
195
196 if (parameters.getFaceId() != expectedParams.getFaceId()) {
197 return CheckCommandResult::WRONG_PARAMS_FACE;
198 }
199
200 return CheckCommandResult::OK;
201 }
202
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000203protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400204 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800205 std::vector<Interest>& m_commands;
206 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800207
Davide Pesavento3dade002019-03-19 11:29:56 -0600208 Scheduler m_scheduler;
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000209 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500210 rib::Rib m_rib;
211 rib::FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800212 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800213};
214
215const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
216
217std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400218operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800219{
220 switch (result) {
221 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400222 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800223 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400224 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400226 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400228 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400230 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400232 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400234 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800235 };
236
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400237 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800238}
239
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500240BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800241BOOST_AUTO_TEST_SUITE(TestRibManager)
242
243class AddTopPrefixFixture : public RibManagerFixture
244{
245public:
246 AddTopPrefixFixture()
247 : RibManagerFixture({true, true}, false)
248 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500249 }
250};
251
Yanbiao Licf0db022016-01-29 00:54:25 -0800252BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700253{
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600255
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 std::vector<Name> ribEntryNames;
257 for (auto&& entry : m_rib) {
258 ribEntryNames.push_back(entry.first);
259 }
260 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
261 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700262}
263
Yanbiao Licf0db022016-01-29 00:54:25 -0800264class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500265{
Yanbiao Licf0db022016-01-29 00:54:25 -0800266public:
267 UnauthorizedRibManagerFixture()
268 : RibManagerFixture({false, false}, true)
269 {
270 }
271};
Vince Lehman72446ec2014-07-09 10:50:02 -0500272
Yanbiao Licf0db022016-01-29 00:54:25 -0800273class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
274{
275public:
276 LocalhostAuthorizedRibManagerFixture()
277 : RibManagerFixture({true, false}, true)
278 {
279 }
280};
Vince Lehman72446ec2014-07-09 10:50:02 -0500281
Yanbiao Licf0db022016-01-29 00:54:25 -0800282class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
283{
284public:
285 LocalhopAuthorizedRibManagerFixture()
286 : RibManagerFixture({false, true}, true)
287 {
288 }
289};
290
291class AuthorizedRibManagerFixture : public RibManagerFixture
292{
293public:
294 AuthorizedRibManagerFixture()
295 : RibManagerFixture({true, true}, true)
296 {
297 }
298};
299
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400300using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800301 UnauthorizedRibManagerFixture,
302 LocalhostAuthorizedRibManagerFixture,
303 LocalhopAuthorizedRibManagerFixture,
304 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400305>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800306
307BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
308{
309 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
310 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
311 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
312 auto successResp = this->makeResponse(200, "Success", parameters);
313 auto failureResp = ControlResponse(403, "authorization rejected");
314
315 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
316 this->receiveInterest(commandHost);
317 this->receiveInterest(commandHop);
318
319 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
320 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
321 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
322
323 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000324 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800325 ManagerCommonFixture::CheckResponseResult::OK);
326 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000327 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800328 ManagerCommonFixture::CheckResponseResult::OK);
329 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500330}
331
Yanbiao Licf0db022016-01-29 00:54:25 -0800332BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
333
334BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500335{
Yanbiao Licf0db022016-01-29 00:54:25 -0800336 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
337 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500338
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000339 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
340 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
341 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
342 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500343
Yanbiao Licf0db022016-01-29 00:54:25 -0800344 receiveInterest(commandRegister);
345 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500346
Yanbiao Licf0db022016-01-29 00:54:25 -0800347 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000348 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800349 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000350 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800351 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500352
Yanbiao Licf0db022016-01-29 00:54:25 -0800353 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
354 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
355 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500356}
357
Yanbiao Licf0db022016-01-29 00:54:25 -0800358BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500359{
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
361 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
362 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
363 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500364
Yanbiao Licf0db022016-01-29 00:54:25 -0800365 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000366 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
367 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
368 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
369 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500370
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 receiveInterest(commandRegister);
372 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Yanbiao Licf0db022016-01-29 00:54:25 -0800374 paramsRegister.setFaceId(inFaceId);
375 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500376
Yanbiao Licf0db022016-01-29 00:54:25 -0800377 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000378 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000380 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800381 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500382
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
384 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
385 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500386}
387
Yanbiao Licf0db022016-01-29 00:54:25 -0800388BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000389{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500390 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
392 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000393
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500394 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
396 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
397 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000398
Yanbiao Licf0db022016-01-29 00:54:25 -0800399 m_commands.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500400 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000402
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500403 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800404 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
405 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000406}
407
Junxiao Shi75306352018-02-01 21:59:44 +0000408BOOST_AUTO_TEST_CASE(NameTooLong)
409{
410 Name prefix;
411 while (prefix.size() <= FIB_MAX_DEPTH) {
412 prefix.append("A");
413 }
414 auto params = makeRegisterParameters(prefix, 2899);
415 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
416 receiveInterest(command);
417
418 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
419 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
420 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
421 " components")),
422 CheckResponseResult::OK);
423
424 BOOST_CHECK_EQUAL(m_commands.size(), 0);
425}
426
Yanbiao Licf0db022016-01-29 00:54:25 -0800427BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
428
Yanbiao Licf0db022016-01-29 00:54:25 -0800429BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500430{
Yanbiao Licf0db022016-01-29 00:54:25 -0800431 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500432 auto generateRoute = [&faceId] () -> rib::Route {
433 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800434 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400435 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400436 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800437 return route;
438 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500439
Yanbiao Licf0db022016-01-29 00:54:25 -0800440 const size_t nEntries = 108;
441 std::set<Name> actualPrefixes;
442 for (size_t i = 0; i < nEntries; ++i) {
443 Name prefix = Name("/test-dataset").appendNumber(i);
444 actualPrefixes.insert(prefix);
445 m_rib.insert(prefix, generateRoute());
446 if (i & 0x1) {
447 m_rib.insert(prefix, generateRoute());
448 m_rib.insert(prefix, generateRoute());
449 }
450 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500451
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000452 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500453
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400454 Block content = concatenateResponses();
455 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800456 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
457
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400458 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400460 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800461 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 actualPrefixes.erase(decodedEntry.getName());
463
464 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
465 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
466
467 auto matchedEntry = matchedEntryIt->second;
468 BOOST_REQUIRE(matchedEntry != nullptr);
469
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400470 expectedRecords.emplace_back();
471 expectedRecords.back().setName(matchedEntry->getName());
472 for (const auto& route : matchedEntry->getRoutes()) {
473 expectedRecords.back().addRoute(ndn::nfd::Route()
474 .setFaceId(route.faceId)
475 .setOrigin(route.origin)
476 .setCost(route.cost)
477 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800479 }
480
481 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800482 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
483 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500484}
485
Yanbiao Licf0db022016-01-29 00:54:25 -0800486BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
487
488BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700489{
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600491
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500492 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Yanbiao Licf0db022016-01-29 00:54:25 -0800493 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
494 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
495 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700496}
497
Yanbiao Licf0db022016-01-29 00:54:25 -0800498BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500499{
Yanbiao Licf0db022016-01-29 00:54:25 -0800500 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
501 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
502 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
503 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
504 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
505 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500506
Vince Lehman26b215c2014-08-17 15:00:41 -0500507 ndn::nfd::FaceStatus status;
508 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700509 std::vector<ndn::nfd::FaceStatus> activeFaces;
510 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500511
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700512 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500513 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800514 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500515
Yanbiao Licf0db022016-01-29 00:54:25 -0800516 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
517 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
518 BOOST_CHECK(it2 == m_rib.end());
519 BOOST_REQUIRE(it1 != m_rib.end());
520 BOOST_CHECK(it1->second->hasFaceId(1));
521 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500522}
523
Yanbiao Licf0db022016-01-29 00:54:25 -0800524BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600525{
Yanbiao Licf0db022016-01-29 00:54:25 -0800526 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
527 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
528 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
529 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
530 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600531
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500532 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
533 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800534 };
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_DESTROYED, 1));
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
Yanbiao Licf0db022016-01-29 00:54:25 -0800540 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500541 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800542 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600543}
544
Yanbiao Licf0db022016-01-29 00:54:25 -0800545BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500546
Yanbiao Licf0db022016-01-29 00:54:25 -0800547BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500548BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500549
550} // namespace tests
Vince Lehman72446ec2014-07-09 10:50:02 -0500551} // namespace nfd