blob: cdf1581dd463aa495bb4f713f1f9b3b3d17e116f [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
Yanbiao Licf0db022016-01-29 00:54:25 -080048class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050049{
50public:
Yanbiao Licf0db022016-01-29 00:54:25 -080051 explicit
52 RibManagerFixture(const ConfigurationStatus& status,
53 bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000054 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080055 , m_status(status)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000056 , m_manager(m_dispatcher, m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080057 , m_rib(m_manager.m_rib)
Vince Lehman72446ec2014-07-09 10:50:02 -050058 {
Yanbiao Licf0db022016-01-29 00:54:25 -080059 m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
Vince Lehman72446ec2014-07-09 10:50:02 -050060
Yanbiao Licf0db022016-01-29 00:54:25 -080061 const std::string prefix = "rib\n{\n";
62 const std::string suffix = "}";
63 const std::string localhostSection = " localhost_security\n"
Vince Lehman72446ec2014-07-09 10:50:02 -050064 " {\n"
65 " trust-anchor\n"
66 " {\n"
67 " type any\n"
68 " }\n"
Yanbiao Licf0db022016-01-29 00:54:25 -080069 " }\n";
70 const std::string localhopSection = " localhop_security\n"
71 " {\n"
72 " trust-anchor\n"
73 " {\n"
74 " type any\n"
75 " }\n"
76 " }\n";
Vince Lehman72446ec2014-07-09 10:50:02 -050077
Yanbiao Licf0db022016-01-29 00:54:25 -080078 std::string ribSection = "";
79 if (m_status.isLocalhostConfigured) {
80 ribSection += localhostSection;
81 }
82 if (m_status.isLocalhopConfigured) {
83 ribSection += localhopSection;
84 }
85 const std::string CONFIG_STR = prefix + ribSection + suffix;
86
87 ConfigFile config;
88 m_manager.setConfigFile(config);
89 config.parse(CONFIG_STR, true, "test-rib");
90
91 registerWithNfd();
92
93 if (shouldClearRib) {
94 clearRib();
95 }
96 }
97
98private:
99 void
100 registerWithNfd()
101 {
102 m_manager.registerWithNfd();
103 advanceClocks(time::milliseconds(1));
104
105 auto replyFibAddCommand = [this] (const Interest& interest) {
106 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000107 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800108 params.setFaceId(1).setCost(0);
109 nfd::ControlResponse resp;
110
111 resp.setCode(200).setBody(params.wireEncode());
112 shared_ptr<Data> data = make_shared<Data>(interest.getName());
113 data->setContent(resp.wireEncode());
114
115 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
116
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000117 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800118 };
119
120 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000121 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800122 if (commandPrefix.isPrefixOf(command.getName())) {
123 replyFibAddCommand(command);
124 advanceClocks(time::milliseconds(1));
125 }
126 }
127
128 // clear commands and responses
129 m_responses.clear();
130 m_commands.clear();
131 }
132
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000133 void
134 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800135 {
136 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000137 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800138 }
139 }
140
141public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400142 static ControlParameters
143 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400144 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800145 {
146 return ControlParameters()
147 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400148 .setFaceId(faceId)
149 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800150 .setCost(10)
151 .setFlags(0)
152 .setExpirationPeriod(expiry);
153 }
154
Davide Pesavento22db5392017-04-14 00:56:43 -0400155 static ControlParameters
156 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800157 {
158 return ControlParameters()
159 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400160 .setFaceId(faceId)
161 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800162 }
163
164 void
165 onSendBatchFromQueue(const RibUpdateBatch& batch)
166 {
167 BOOST_ASSERT(batch.begin() != batch.end());
168 RibUpdate update = *(batch.begin());
169
170 // Simulate a successful response from NFD
171 FibUpdater& updater = m_manager.m_fibUpdater;
172 m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
173 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
174 }
175
176public:
177 enum class CheckCommandResult {
178 OK,
179 OUT_OF_BOUNDARY,
180 WRONG_FORMAT,
181 WRONG_VERB,
182 WRONG_PARAMS_FORMAT,
183 WRONG_PARAMS_NAME,
184 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400185 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800186
187 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400188 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800189 {
190 if (idx > m_commands.size()) {
191 return CheckCommandResult::OUT_OF_BOUNDARY;
192 }
193 const auto& name = m_commands[idx].getName();
194
195 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
196 return CheckCommandResult::WRONG_FORMAT;
197 }
198 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
199
200 Name::Component expectedVerb(verbStr);
201 if (verb != expectedVerb) {
202 return CheckCommandResult::WRONG_VERB;
203 }
204
205 ControlParameters parameters;
206 try {
207 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
208 parameters.wireDecode(rawParameters);
209 }
210 catch (...) {
211 return CheckCommandResult::WRONG_PARAMS_FORMAT;
212 }
213
214 if (parameters.getName() != expectedParams.getName()) {
215 return CheckCommandResult::WRONG_PARAMS_NAME;
216 }
217
218 if (parameters.getFaceId() != expectedParams.getFaceId()) {
219 return CheckCommandResult::WRONG_PARAMS_FACE;
220 }
221
222 return CheckCommandResult::OK;
223 }
224
225public:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400226 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800227 std::vector<Interest>& m_commands;
228 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800229
230protected:
231 RibManager m_manager;
232 Rib& m_rib;
233};
234
235const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
236
237std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400238operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800239{
240 switch (result) {
241 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400242 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800243 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400244 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800245 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400246 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800247 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400248 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800249 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400250 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400252 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400254 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800255 };
256
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400257 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800258}
259
Yanbiao Licf0db022016-01-29 00:54:25 -0800260BOOST_AUTO_TEST_SUITE(TestRibManager)
261
262class AddTopPrefixFixture : public RibManagerFixture
263{
264public:
265 AddTopPrefixFixture()
266 : RibManagerFixture({true, true}, false)
267 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500268 }
269};
270
Yanbiao Licf0db022016-01-29 00:54:25 -0800271BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700272{
Yanbiao Licf0db022016-01-29 00:54:25 -0800273 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600274
Yanbiao Licf0db022016-01-29 00:54:25 -0800275 std::vector<Name> ribEntryNames;
276 for (auto&& entry : m_rib) {
277 ribEntryNames.push_back(entry.first);
278 }
279 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
280 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700281}
282
Yanbiao Licf0db022016-01-29 00:54:25 -0800283class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500284{
Yanbiao Licf0db022016-01-29 00:54:25 -0800285public:
286 UnauthorizedRibManagerFixture()
287 : RibManagerFixture({false, false}, true)
288 {
289 }
290};
Vince Lehman72446ec2014-07-09 10:50:02 -0500291
Yanbiao Licf0db022016-01-29 00:54:25 -0800292class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
293{
294public:
295 LocalhostAuthorizedRibManagerFixture()
296 : RibManagerFixture({true, false}, true)
297 {
298 }
299};
Vince Lehman72446ec2014-07-09 10:50:02 -0500300
Yanbiao Licf0db022016-01-29 00:54:25 -0800301class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
302{
303public:
304 LocalhopAuthorizedRibManagerFixture()
305 : RibManagerFixture({false, true}, true)
306 {
307 }
308};
309
310class AuthorizedRibManagerFixture : public RibManagerFixture
311{
312public:
313 AuthorizedRibManagerFixture()
314 : RibManagerFixture({true, true}, true)
315 {
316 }
317};
318
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400319using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800320 UnauthorizedRibManagerFixture,
321 LocalhostAuthorizedRibManagerFixture,
322 LocalhopAuthorizedRibManagerFixture,
323 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400324>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800325
326BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
327{
328 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
329 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
330 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
331 auto successResp = this->makeResponse(200, "Success", parameters);
332 auto failureResp = ControlResponse(403, "authorization rejected");
333
334 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
335 this->receiveInterest(commandHost);
336 this->receiveInterest(commandHop);
337
338 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
339 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
340 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
341
342 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000343 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800344 ManagerCommonFixture::CheckResponseResult::OK);
345 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000346 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800347 ManagerCommonFixture::CheckResponseResult::OK);
348 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500349}
350
Yanbiao Licf0db022016-01-29 00:54:25 -0800351BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
352
353BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500354{
Yanbiao Licf0db022016-01-29 00:54:25 -0800355 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
356 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500357
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000358 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
359 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
360 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
361 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500362
Yanbiao Licf0db022016-01-29 00:54:25 -0800363 receiveInterest(commandRegister);
364 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500365
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000367 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000369 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800370 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500371
Yanbiao Licf0db022016-01-29 00:54:25 -0800372 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
373 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
374 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500375}
376
Yanbiao Licf0db022016-01-29 00:54:25 -0800377BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500378{
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
380 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
381 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
382 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500383
Yanbiao Licf0db022016-01-29 00:54:25 -0800384 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000385 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
386 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
387 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
388 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500389
Yanbiao Licf0db022016-01-29 00:54:25 -0800390 receiveInterest(commandRegister);
391 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500392
Yanbiao Licf0db022016-01-29 00:54:25 -0800393 paramsRegister.setFaceId(inFaceId);
394 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000397 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000399 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500401
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
403 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
404 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500405}
406
Yanbiao Licf0db022016-01-29 00:54:25 -0800407BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000408{
Yanbiao Licf0db022016-01-29 00:54:25 -0800409 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
410 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
411 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000412
Yanbiao Licf0db022016-01-29 00:54:25 -0800413 advanceClocks(time::milliseconds(55));
414 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
415 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
416 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000417
Yanbiao Licf0db022016-01-29 00:54:25 -0800418 m_commands.clear();
419 paramsRegister.setExpirationPeriod(time::milliseconds(100));
420 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000421
Yanbiao Licf0db022016-01-29 00:54:25 -0800422 advanceClocks(time::milliseconds(55));
423 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
424 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000425}
426
Junxiao Shi75306352018-02-01 21:59:44 +0000427BOOST_AUTO_TEST_CASE(NameTooLong)
428{
429 Name prefix;
430 while (prefix.size() <= FIB_MAX_DEPTH) {
431 prefix.append("A");
432 }
433 auto params = makeRegisterParameters(prefix, 2899);
434 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
435 receiveInterest(command);
436
437 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
438 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
439 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
440 " components")),
441 CheckResponseResult::OK);
442
443 BOOST_CHECK_EQUAL(m_commands.size(), 0);
444}
445
Yanbiao Licf0db022016-01-29 00:54:25 -0800446BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
447
Yanbiao Licf0db022016-01-29 00:54:25 -0800448BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500449{
Yanbiao Licf0db022016-01-29 00:54:25 -0800450 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400451 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800452 Route route;
453 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400454 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400455 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800456 return route;
457 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500458
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 const size_t nEntries = 108;
460 std::set<Name> actualPrefixes;
461 for (size_t i = 0; i < nEntries; ++i) {
462 Name prefix = Name("/test-dataset").appendNumber(i);
463 actualPrefixes.insert(prefix);
464 m_rib.insert(prefix, generateRoute());
465 if (i & 0x1) {
466 m_rib.insert(prefix, generateRoute());
467 m_rib.insert(prefix, generateRoute());
468 }
469 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500470
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000471 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500472
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400473 Block content = concatenateResponses();
474 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800475 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
476
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400477 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400479 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800480 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800481 actualPrefixes.erase(decodedEntry.getName());
482
483 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
484 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
485
486 auto matchedEntry = matchedEntryIt->second;
487 BOOST_REQUIRE(matchedEntry != nullptr);
488
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400489 expectedRecords.emplace_back();
490 expectedRecords.back().setName(matchedEntry->getName());
491 for (const auto& route : matchedEntry->getRoutes()) {
492 expectedRecords.back().addRoute(ndn::nfd::Route()
493 .setFaceId(route.faceId)
494 .setOrigin(route.origin)
495 .setCost(route.cost)
496 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800497 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 }
499
500 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800501 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
502 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500503}
504
Yanbiao Licf0db022016-01-29 00:54:25 -0800505BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
506
507BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700508{
Yanbiao Licf0db022016-01-29 00:54:25 -0800509 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600510
Yanbiao Licf0db022016-01-29 00:54:25 -0800511 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
512 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
513 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
514 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700515}
516
Yanbiao Licf0db022016-01-29 00:54:25 -0800517BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500518{
Yanbiao Licf0db022016-01-29 00:54:25 -0800519 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
520 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
521 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
522 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
523 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
524 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500525
Vince Lehman26b215c2014-08-17 15:00:41 -0500526 ndn::nfd::FaceStatus status;
527 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700528 std::vector<ndn::nfd::FaceStatus> activeFaces;
529 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500530
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700531 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800532 advanceClocks(time::milliseconds(100));
533 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500534
Yanbiao Licf0db022016-01-29 00:54:25 -0800535 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
536 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
537 BOOST_CHECK(it2 == m_rib.end());
538 BOOST_REQUIRE(it1 != m_rib.end());
539 BOOST_CHECK(it1->second->hasFaceId(1));
540 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500541}
542
Yanbiao Licf0db022016-01-29 00:54:25 -0800543BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600544{
Yanbiao Licf0db022016-01-29 00:54:25 -0800545 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
546 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
547 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
548 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
549 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600550
Yanbiao Licf0db022016-01-29 00:54:25 -0800551 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
552 ndn::nfd::FaceEventNotification notification;
553 notification.setKind(eventKind).setFaceId(faceId);
554 return notification;
555 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600556
Yanbiao Licf0db022016-01-29 00:54:25 -0800557 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
558 advanceClocks(time::milliseconds(100));
559 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600560
Yanbiao Licf0db022016-01-29 00:54:25 -0800561 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
562 advanceClocks(time::milliseconds(100));
563 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600564}
565
Yanbiao Licf0db022016-01-29 00:54:25 -0800566BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800567BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500568
569} // namespace tests
570} // namespace rib
571} // namespace nfd