blob: 8ccde7de3a4979859873722a20bf0c3451d459be [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
60 RibManagerFixture(const ConfigurationStatus& status,
61 bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000062 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080063 , m_status(status)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000064 , m_nfdController(m_face, m_keyChain)
65 , m_fibUpdater(m_rib, m_nfdController)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000066 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
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 Pesavento9f8b10e2018-08-22 08:45:37 +0000211 ndn::nfd::Controller m_nfdController;
212 Rib m_rib;
213 FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800214 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800215};
216
217const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
218
219std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400220operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800221{
222 switch (result) {
223 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400224 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400226 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400228 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800229 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400230 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800231 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400232 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800233 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400234 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800235 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400236 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800237 };
238
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400239 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800240}
241
Yanbiao Licf0db022016-01-29 00:54:25 -0800242BOOST_AUTO_TEST_SUITE(TestRibManager)
243
244class AddTopPrefixFixture : public RibManagerFixture
245{
246public:
247 AddTopPrefixFixture()
248 : RibManagerFixture({true, true}, false)
249 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500250 }
251};
252
Yanbiao Licf0db022016-01-29 00:54:25 -0800253BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700254{
Yanbiao Licf0db022016-01-29 00:54:25 -0800255 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600256
Yanbiao Licf0db022016-01-29 00:54:25 -0800257 std::vector<Name> ribEntryNames;
258 for (auto&& entry : m_rib) {
259 ribEntryNames.push_back(entry.first);
260 }
261 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
262 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700263}
264
Yanbiao Licf0db022016-01-29 00:54:25 -0800265class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500266{
Yanbiao Licf0db022016-01-29 00:54:25 -0800267public:
268 UnauthorizedRibManagerFixture()
269 : RibManagerFixture({false, false}, true)
270 {
271 }
272};
Vince Lehman72446ec2014-07-09 10:50:02 -0500273
Yanbiao Licf0db022016-01-29 00:54:25 -0800274class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
275{
276public:
277 LocalhostAuthorizedRibManagerFixture()
278 : RibManagerFixture({true, false}, true)
279 {
280 }
281};
Vince Lehman72446ec2014-07-09 10:50:02 -0500282
Yanbiao Licf0db022016-01-29 00:54:25 -0800283class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
284{
285public:
286 LocalhopAuthorizedRibManagerFixture()
287 : RibManagerFixture({false, true}, true)
288 {
289 }
290};
291
292class AuthorizedRibManagerFixture : public RibManagerFixture
293{
294public:
295 AuthorizedRibManagerFixture()
296 : RibManagerFixture({true, true}, true)
297 {
298 }
299};
300
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400301using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800302 UnauthorizedRibManagerFixture,
303 LocalhostAuthorizedRibManagerFixture,
304 LocalhopAuthorizedRibManagerFixture,
305 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400306>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800307
308BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
309{
310 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
311 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
312 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
313 auto successResp = this->makeResponse(200, "Success", parameters);
314 auto failureResp = ControlResponse(403, "authorization rejected");
315
316 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
317 this->receiveInterest(commandHost);
318 this->receiveInterest(commandHop);
319
320 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
321 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
322 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
323
324 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000325 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800326 ManagerCommonFixture::CheckResponseResult::OK);
327 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000328 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800329 ManagerCommonFixture::CheckResponseResult::OK);
330 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500331}
332
Yanbiao Licf0db022016-01-29 00:54:25 -0800333BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
334
335BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500336{
Yanbiao Licf0db022016-01-29 00:54:25 -0800337 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
338 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500339
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000340 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
341 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
342 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
343 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500344
Yanbiao Licf0db022016-01-29 00:54:25 -0800345 receiveInterest(commandRegister);
346 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500347
Yanbiao Licf0db022016-01-29 00:54:25 -0800348 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000349 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800350 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000351 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800352 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500353
Yanbiao Licf0db022016-01-29 00:54:25 -0800354 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
355 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
356 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500357}
358
Yanbiao Licf0db022016-01-29 00:54:25 -0800359BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500360{
Yanbiao Licf0db022016-01-29 00:54:25 -0800361 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
362 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
363 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
364 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500365
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000367 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
368 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
369 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
370 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500371
Yanbiao Licf0db022016-01-29 00:54:25 -0800372 receiveInterest(commandRegister);
373 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500374
Yanbiao Licf0db022016-01-29 00:54:25 -0800375 paramsRegister.setFaceId(inFaceId);
376 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500377
Yanbiao Licf0db022016-01-29 00:54:25 -0800378 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000379 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800380 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000381 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500383
Yanbiao Licf0db022016-01-29 00:54:25 -0800384 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
385 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
386 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500387}
388
Yanbiao Licf0db022016-01-29 00:54:25 -0800389BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000390{
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
392 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
393 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000394
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 advanceClocks(time::milliseconds(55));
396 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
397 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
398 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000399
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 m_commands.clear();
401 paramsRegister.setExpirationPeriod(time::milliseconds(100));
402 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000403
Yanbiao Licf0db022016-01-29 00:54:25 -0800404 advanceClocks(time::milliseconds(55));
405 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
406 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000407}
408
Junxiao Shi75306352018-02-01 21:59:44 +0000409BOOST_AUTO_TEST_CASE(NameTooLong)
410{
411 Name prefix;
412 while (prefix.size() <= FIB_MAX_DEPTH) {
413 prefix.append("A");
414 }
415 auto params = makeRegisterParameters(prefix, 2899);
416 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
417 receiveInterest(command);
418
419 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
420 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
421 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
422 " components")),
423 CheckResponseResult::OK);
424
425 BOOST_CHECK_EQUAL(m_commands.size(), 0);
426}
427
Yanbiao Licf0db022016-01-29 00:54:25 -0800428BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
429
Yanbiao Licf0db022016-01-29 00:54:25 -0800430BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500431{
Yanbiao Licf0db022016-01-29 00:54:25 -0800432 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400433 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800434 Route route;
435 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400436 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400437 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800438 return route;
439 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500440
Yanbiao Licf0db022016-01-29 00:54:25 -0800441 const size_t nEntries = 108;
442 std::set<Name> actualPrefixes;
443 for (size_t i = 0; i < nEntries; ++i) {
444 Name prefix = Name("/test-dataset").appendNumber(i);
445 actualPrefixes.insert(prefix);
446 m_rib.insert(prefix, generateRoute());
447 if (i & 0x1) {
448 m_rib.insert(prefix, generateRoute());
449 m_rib.insert(prefix, generateRoute());
450 }
451 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500452
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000453 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500454
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400455 Block content = concatenateResponses();
456 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800457 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
458
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400459 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800460 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400461 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800463 actualPrefixes.erase(decodedEntry.getName());
464
465 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
466 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
467
468 auto matchedEntry = matchedEntryIt->second;
469 BOOST_REQUIRE(matchedEntry != nullptr);
470
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400471 expectedRecords.emplace_back();
472 expectedRecords.back().setName(matchedEntry->getName());
473 for (const auto& route : matchedEntry->getRoutes()) {
474 expectedRecords.back().addRoute(ndn::nfd::Route()
475 .setFaceId(route.faceId)
476 .setOrigin(route.origin)
477 .setCost(route.cost)
478 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800479 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800480 }
481
482 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800483 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
484 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500485}
486
Yanbiao Licf0db022016-01-29 00:54:25 -0800487BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
488
489BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700490{
Yanbiao Licf0db022016-01-29 00:54:25 -0800491 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600492
Yanbiao Licf0db022016-01-29 00:54:25 -0800493 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
494 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
495 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
496 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700497}
498
Yanbiao Licf0db022016-01-29 00:54:25 -0800499BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500500{
Yanbiao Licf0db022016-01-29 00:54:25 -0800501 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
502 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
503 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
504 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
505 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
506 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500507
Vince Lehman26b215c2014-08-17 15:00:41 -0500508 ndn::nfd::FaceStatus status;
509 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700510 std::vector<ndn::nfd::FaceStatus> activeFaces;
511 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500512
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700513 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800514 advanceClocks(time::milliseconds(100));
515 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500516
Yanbiao Licf0db022016-01-29 00:54:25 -0800517 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
518 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
519 BOOST_CHECK(it2 == m_rib.end());
520 BOOST_REQUIRE(it1 != m_rib.end());
521 BOOST_CHECK(it1->second->hasFaceId(1));
522 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500523}
524
Yanbiao Licf0db022016-01-29 00:54:25 -0800525BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600526{
Yanbiao Licf0db022016-01-29 00:54:25 -0800527 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
528 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
529 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
530 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
531 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600532
Yanbiao Licf0db022016-01-29 00:54:25 -0800533 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
534 ndn::nfd::FaceEventNotification notification;
535 notification.setKind(eventKind).setFaceId(faceId);
536 return notification;
537 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600538
Yanbiao Licf0db022016-01-29 00:54:25 -0800539 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
540 advanceClocks(time::milliseconds(100));
541 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600542
Yanbiao Licf0db022016-01-29 00:54:25 -0800543 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
544 advanceClocks(time::milliseconds(100));
545 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600546}
547
Yanbiao Licf0db022016-01-29 00:54:25 -0800548BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800549BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500550
551} // namespace tests
552} // namespace rib
553} // namespace nfd