blob: be59ca5109574ee7a5254b61f03365f20350b853 [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/*
3 * Copyright (c) 2014-2018, 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
26#include "rib/rib-manager.hpp"
Junxiao Shi75306352018-02-01 21:59:44 +000027#include "core/fib-max-depth.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050028
Davide Pesavento0064c1d2018-03-03 18:43:53 -050029#include "tests/manager-common-fixture.hpp"
30
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/face-event-notification.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 {
37namespace rib {
38namespace tests {
39
Junxiao Shi0de23a22015-12-03 20:07:02 +000040using namespace nfd::tests;
41
Yanbiao Licf0db022016-01-29 00:54:25 -080042struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050043{
Yanbiao Licf0db022016-01-29 00:54:25 -080044 bool isLocalhostConfigured;
45 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050046};
47
Junxiao Shif4cfed12018-08-22 23:26:29 +000048static ConfigSection
49getValidatorConfigSection()
50{
51 ConfigSection section;
52 section.put("trust-anchor.type", "any");
53 return section;
54}
55
Yanbiao Licf0db022016-01-29 00:54:25 -080056class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050057{
58public:
Yanbiao Licf0db022016-01-29 00:54:25 -080059 explicit
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040060 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000061 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080062 , m_status(status)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040063 , m_scheduler(g_io)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000064 , m_nfdController(m_face, m_keyChain)
65 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040066 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher, m_scheduler)
Vince Lehman72446ec2014-07-09 10:50:02 -050067 {
Junxiao Shi52009042018-09-10 12:33:56 +000068 m_rib.mockFibResponse = [] (const RibUpdateBatch& batch) {
69 BOOST_CHECK(batch.begin() != batch.end());
70 return true;
71 };
72 m_rib.wantMockFibResponseOnce = false;
Vince Lehman72446ec2014-07-09 10:50:02 -050073
Yanbiao Licf0db022016-01-29 00:54:25 -080074 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000075 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080076 }
77 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000078 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080079 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000080 else {
81 m_manager.disableLocalhop();
82 }
Yanbiao Licf0db022016-01-29 00:54:25 -080083
84 registerWithNfd();
85
86 if (shouldClearRib) {
87 clearRib();
88 }
89 }
90
91private:
92 void
93 registerWithNfd()
94 {
95 m_manager.registerWithNfd();
96 advanceClocks(time::milliseconds(1));
97
98 auto replyFibAddCommand = [this] (const Interest& interest) {
99 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000100 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800101 params.setFaceId(1).setCost(0);
102 nfd::ControlResponse resp;
103
104 resp.setCode(200).setBody(params.wireEncode());
105 shared_ptr<Data> data = make_shared<Data>(interest.getName());
106 data->setContent(resp.wireEncode());
107
108 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
109
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000110 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800111 };
112
113 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000114 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800115 if (commandPrefix.isPrefixOf(command.getName())) {
116 replyFibAddCommand(command);
117 advanceClocks(time::milliseconds(1));
118 }
119 }
120
121 // clear commands and responses
122 m_responses.clear();
123 m_commands.clear();
124 }
125
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000126 void
127 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800128 {
129 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000130 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800131 }
132 }
133
134public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400135 static ControlParameters
136 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400137 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800138 {
139 return ControlParameters()
140 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400141 .setFaceId(faceId)
142 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800143 .setCost(10)
144 .setFlags(0)
145 .setExpirationPeriod(expiry);
146 }
147
Davide Pesavento22db5392017-04-14 00:56:43 -0400148 static ControlParameters
149 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800150 {
151 return ControlParameters()
152 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400153 .setFaceId(faceId)
154 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800155 }
156
Yanbiao Licf0db022016-01-29 00:54:25 -0800157public:
158 enum class CheckCommandResult {
159 OK,
160 OUT_OF_BOUNDARY,
161 WRONG_FORMAT,
162 WRONG_VERB,
163 WRONG_PARAMS_FORMAT,
164 WRONG_PARAMS_NAME,
165 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400166 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800167
168 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400169 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800170 {
171 if (idx > m_commands.size()) {
172 return CheckCommandResult::OUT_OF_BOUNDARY;
173 }
174 const auto& name = m_commands[idx].getName();
175
176 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
177 return CheckCommandResult::WRONG_FORMAT;
178 }
179 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
180
181 Name::Component expectedVerb(verbStr);
182 if (verb != expectedVerb) {
183 return CheckCommandResult::WRONG_VERB;
184 }
185
186 ControlParameters parameters;
187 try {
188 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
189 parameters.wireDecode(rawParameters);
190 }
191 catch (...) {
192 return CheckCommandResult::WRONG_PARAMS_FORMAT;
193 }
194
195 if (parameters.getName() != expectedParams.getName()) {
196 return CheckCommandResult::WRONG_PARAMS_NAME;
197 }
198
199 if (parameters.getFaceId() != expectedParams.getFaceId()) {
200 return CheckCommandResult::WRONG_PARAMS_FACE;
201 }
202
203 return CheckCommandResult::OK;
204 }
205
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000206protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400207 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800208 std::vector<Interest>& m_commands;
209 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800210
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400211 ndn::util::Scheduler m_scheduler;
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000212 ndn::nfd::Controller m_nfdController;
213 Rib m_rib;
214 FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800215 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800216};
217
218const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
219
220std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400221operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800222{
223 switch (result) {
224 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400225 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400227 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400229 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800230 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400231 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400233 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800234 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400235 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800236 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400237 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800238 };
239
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400240 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800241}
242
Yanbiao Licf0db022016-01-29 00:54:25 -0800243BOOST_AUTO_TEST_SUITE(TestRibManager)
244
245class AddTopPrefixFixture : public RibManagerFixture
246{
247public:
248 AddTopPrefixFixture()
249 : RibManagerFixture({true, true}, false)
250 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500251 }
252};
253
Yanbiao Licf0db022016-01-29 00:54:25 -0800254BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700255{
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600257
Yanbiao Licf0db022016-01-29 00:54:25 -0800258 std::vector<Name> ribEntryNames;
259 for (auto&& entry : m_rib) {
260 ribEntryNames.push_back(entry.first);
261 }
262 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
263 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700264}
265
Yanbiao Licf0db022016-01-29 00:54:25 -0800266class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500267{
Yanbiao Licf0db022016-01-29 00:54:25 -0800268public:
269 UnauthorizedRibManagerFixture()
270 : RibManagerFixture({false, false}, true)
271 {
272 }
273};
Vince Lehman72446ec2014-07-09 10:50:02 -0500274
Yanbiao Licf0db022016-01-29 00:54:25 -0800275class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
276{
277public:
278 LocalhostAuthorizedRibManagerFixture()
279 : RibManagerFixture({true, false}, true)
280 {
281 }
282};
Vince Lehman72446ec2014-07-09 10:50:02 -0500283
Yanbiao Licf0db022016-01-29 00:54:25 -0800284class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
285{
286public:
287 LocalhopAuthorizedRibManagerFixture()
288 : RibManagerFixture({false, true}, true)
289 {
290 }
291};
292
293class AuthorizedRibManagerFixture : public RibManagerFixture
294{
295public:
296 AuthorizedRibManagerFixture()
297 : RibManagerFixture({true, true}, true)
298 {
299 }
300};
301
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400302using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800303 UnauthorizedRibManagerFixture,
304 LocalhostAuthorizedRibManagerFixture,
305 LocalhopAuthorizedRibManagerFixture,
306 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400307>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800308
309BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
310{
311 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
312 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
313 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
314 auto successResp = this->makeResponse(200, "Success", parameters);
315 auto failureResp = ControlResponse(403, "authorization rejected");
316
317 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
318 this->receiveInterest(commandHost);
319 this->receiveInterest(commandHop);
320
321 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
322 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
323 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
324
325 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000326 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800327 ManagerCommonFixture::CheckResponseResult::OK);
328 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000329 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800330 ManagerCommonFixture::CheckResponseResult::OK);
331 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500332}
333
Yanbiao Licf0db022016-01-29 00:54:25 -0800334BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
335
336BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500337{
Yanbiao Licf0db022016-01-29 00:54:25 -0800338 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
339 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500340
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000341 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
342 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
343 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
344 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500345
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 receiveInterest(commandRegister);
347 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500348
Yanbiao Licf0db022016-01-29 00:54:25 -0800349 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000350 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800351 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000352 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800353 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500354
Yanbiao Licf0db022016-01-29 00:54:25 -0800355 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
356 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
357 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500358}
359
Yanbiao Licf0db022016-01-29 00:54:25 -0800360BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500361{
Yanbiao Licf0db022016-01-29 00:54:25 -0800362 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
363 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
364 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
365 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500366
Yanbiao Licf0db022016-01-29 00:54:25 -0800367 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000368 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
369 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
370 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
371 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500372
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 receiveInterest(commandRegister);
374 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500375
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 paramsRegister.setFaceId(inFaceId);
377 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500378
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000380 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800381 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000382 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500384
Yanbiao Licf0db022016-01-29 00:54:25 -0800385 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
386 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
387 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500388}
389
Yanbiao Licf0db022016-01-29 00:54:25 -0800390BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000391{
Yanbiao Licf0db022016-01-29 00:54:25 -0800392 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
393 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
394 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 advanceClocks(time::milliseconds(55));
397 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
398 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
399 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000400
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 m_commands.clear();
402 paramsRegister.setExpirationPeriod(time::milliseconds(100));
403 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 advanceClocks(time::milliseconds(55));
406 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
407 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000408}
409
Junxiao Shi75306352018-02-01 21:59:44 +0000410BOOST_AUTO_TEST_CASE(NameTooLong)
411{
412 Name prefix;
413 while (prefix.size() <= FIB_MAX_DEPTH) {
414 prefix.append("A");
415 }
416 auto params = makeRegisterParameters(prefix, 2899);
417 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
418 receiveInterest(command);
419
420 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
421 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
422 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
423 " components")),
424 CheckResponseResult::OK);
425
426 BOOST_CHECK_EQUAL(m_commands.size(), 0);
427}
428
Yanbiao Licf0db022016-01-29 00:54:25 -0800429BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
430
Yanbiao Licf0db022016-01-29 00:54:25 -0800431BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500432{
Yanbiao Licf0db022016-01-29 00:54:25 -0800433 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400434 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800435 Route route;
436 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400437 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400438 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800439 return route;
440 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500441
Yanbiao Licf0db022016-01-29 00:54:25 -0800442 const size_t nEntries = 108;
443 std::set<Name> actualPrefixes;
444 for (size_t i = 0; i < nEntries; ++i) {
445 Name prefix = Name("/test-dataset").appendNumber(i);
446 actualPrefixes.insert(prefix);
447 m_rib.insert(prefix, generateRoute());
448 if (i & 0x1) {
449 m_rib.insert(prefix, generateRoute());
450 m_rib.insert(prefix, generateRoute());
451 }
452 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500453
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000454 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500455
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400456 Block content = concatenateResponses();
457 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800458 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
459
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400460 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800461 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400462 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800463 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800464 actualPrefixes.erase(decodedEntry.getName());
465
466 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
467 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
468
469 auto matchedEntry = matchedEntryIt->second;
470 BOOST_REQUIRE(matchedEntry != nullptr);
471
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400472 expectedRecords.emplace_back();
473 expectedRecords.back().setName(matchedEntry->getName());
474 for (const auto& route : matchedEntry->getRoutes()) {
475 expectedRecords.back().addRoute(ndn::nfd::Route()
476 .setFaceId(route.faceId)
477 .setOrigin(route.origin)
478 .setCost(route.cost)
479 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800480 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800481 }
482
483 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800484 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
485 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500486}
487
Yanbiao Licf0db022016-01-29 00:54:25 -0800488BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
489
490BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700491{
Yanbiao Licf0db022016-01-29 00:54:25 -0800492 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600493
Yanbiao Licf0db022016-01-29 00:54:25 -0800494 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
495 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
496 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
497 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700498}
499
Yanbiao Licf0db022016-01-29 00:54:25 -0800500BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500501{
Yanbiao Licf0db022016-01-29 00:54:25 -0800502 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
503 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
504 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
505 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
506 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
507 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500508
Vince Lehman26b215c2014-08-17 15:00:41 -0500509 ndn::nfd::FaceStatus status;
510 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700511 std::vector<ndn::nfd::FaceStatus> activeFaces;
512 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500513
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700514 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800515 advanceClocks(time::milliseconds(100));
516 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500517
Yanbiao Licf0db022016-01-29 00:54:25 -0800518 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
519 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
520 BOOST_CHECK(it2 == m_rib.end());
521 BOOST_REQUIRE(it1 != m_rib.end());
522 BOOST_CHECK(it1->second->hasFaceId(1));
523 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500524}
525
Yanbiao Licf0db022016-01-29 00:54:25 -0800526BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600527{
Yanbiao Licf0db022016-01-29 00:54:25 -0800528 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
529 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
530 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
531 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
532 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600533
Yanbiao Licf0db022016-01-29 00:54:25 -0800534 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
535 ndn::nfd::FaceEventNotification notification;
536 notification.setKind(eventKind).setFaceId(faceId);
537 return notification;
538 };
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_DESTROYED, 1));
541 advanceClocks(time::milliseconds(100));
542 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600543
Yanbiao Licf0db022016-01-29 00:54:25 -0800544 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
545 advanceClocks(time::milliseconds(100));
546 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600547}
548
Yanbiao Licf0db022016-01-29 00:54:25 -0800549BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800550BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500551
552} // namespace tests
553} // namespace rib
554} // namespace nfd