blob: 44b7b292093054c7fca798bbb6d3811431237fb5 [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 Shif4cfed12018-08-22 23:26:29 +000066 , m_manager(m_rib, m_face, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -050067 {
Yanbiao Licf0db022016-01-29 00:54:25 -080068 m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
Vince Lehman72446ec2014-07-09 10:50:02 -050069
Yanbiao Licf0db022016-01-29 00:54:25 -080070 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000071 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080072 }
73 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000074 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080075 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000076 else {
77 m_manager.disableLocalhop();
78 }
Yanbiao Licf0db022016-01-29 00:54:25 -080079
80 registerWithNfd();
81
82 if (shouldClearRib) {
83 clearRib();
84 }
85 }
86
87private:
88 void
89 registerWithNfd()
90 {
91 m_manager.registerWithNfd();
92 advanceClocks(time::milliseconds(1));
93
94 auto replyFibAddCommand = [this] (const Interest& interest) {
95 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +000096 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -080097 params.setFaceId(1).setCost(0);
98 nfd::ControlResponse resp;
99
100 resp.setCode(200).setBody(params.wireEncode());
101 shared_ptr<Data> data = make_shared<Data>(interest.getName());
102 data->setContent(resp.wireEncode());
103
104 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
105
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000106 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800107 };
108
109 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000110 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800111 if (commandPrefix.isPrefixOf(command.getName())) {
112 replyFibAddCommand(command);
113 advanceClocks(time::milliseconds(1));
114 }
115 }
116
117 // clear commands and responses
118 m_responses.clear();
119 m_commands.clear();
120 }
121
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000122 void
123 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800124 {
125 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000126 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800127 }
128 }
129
130public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400131 static ControlParameters
132 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400133 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800134 {
135 return ControlParameters()
136 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400137 .setFaceId(faceId)
138 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800139 .setCost(10)
140 .setFlags(0)
141 .setExpirationPeriod(expiry);
142 }
143
Davide Pesavento22db5392017-04-14 00:56:43 -0400144 static ControlParameters
145 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800146 {
147 return ControlParameters()
148 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400149 .setFaceId(faceId)
150 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800151 }
152
153 void
154 onSendBatchFromQueue(const RibUpdateBatch& batch)
155 {
156 BOOST_ASSERT(batch.begin() != batch.end());
157 RibUpdate update = *(batch.begin());
158
159 // Simulate a successful response from NFD
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000160 m_rib.onFibUpdateSuccess(batch, m_fibUpdater.m_inheritedRoutes,
161 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
Yanbiao Licf0db022016-01-29 00:54:25 -0800162 }
163
164public:
165 enum class CheckCommandResult {
166 OK,
167 OUT_OF_BOUNDARY,
168 WRONG_FORMAT,
169 WRONG_VERB,
170 WRONG_PARAMS_FORMAT,
171 WRONG_PARAMS_NAME,
172 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400173 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800174
175 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400176 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800177 {
178 if (idx > m_commands.size()) {
179 return CheckCommandResult::OUT_OF_BOUNDARY;
180 }
181 const auto& name = m_commands[idx].getName();
182
183 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
184 return CheckCommandResult::WRONG_FORMAT;
185 }
186 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
187
188 Name::Component expectedVerb(verbStr);
189 if (verb != expectedVerb) {
190 return CheckCommandResult::WRONG_VERB;
191 }
192
193 ControlParameters parameters;
194 try {
195 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
196 parameters.wireDecode(rawParameters);
197 }
198 catch (...) {
199 return CheckCommandResult::WRONG_PARAMS_FORMAT;
200 }
201
202 if (parameters.getName() != expectedParams.getName()) {
203 return CheckCommandResult::WRONG_PARAMS_NAME;
204 }
205
206 if (parameters.getFaceId() != expectedParams.getFaceId()) {
207 return CheckCommandResult::WRONG_PARAMS_FACE;
208 }
209
210 return CheckCommandResult::OK;
211 }
212
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000213protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400214 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800215 std::vector<Interest>& m_commands;
216 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800217
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000218 ndn::nfd::Controller m_nfdController;
219 Rib m_rib;
220 FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800221 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800222};
223
224const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
225
226std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400227operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800228{
229 switch (result) {
230 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400231 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400233 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800234 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400235 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800236 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400237 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800238 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400239 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800240 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400241 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800242 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400243 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800244 };
245
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400246 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800247}
248
Yanbiao Licf0db022016-01-29 00:54:25 -0800249BOOST_AUTO_TEST_SUITE(TestRibManager)
250
251class AddTopPrefixFixture : public RibManagerFixture
252{
253public:
254 AddTopPrefixFixture()
255 : RibManagerFixture({true, true}, false)
256 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500257 }
258};
259
Yanbiao Licf0db022016-01-29 00:54:25 -0800260BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700261{
Yanbiao Licf0db022016-01-29 00:54:25 -0800262 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600263
Yanbiao Licf0db022016-01-29 00:54:25 -0800264 std::vector<Name> ribEntryNames;
265 for (auto&& entry : m_rib) {
266 ribEntryNames.push_back(entry.first);
267 }
268 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
269 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700270}
271
Yanbiao Licf0db022016-01-29 00:54:25 -0800272class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500273{
Yanbiao Licf0db022016-01-29 00:54:25 -0800274public:
275 UnauthorizedRibManagerFixture()
276 : RibManagerFixture({false, false}, true)
277 {
278 }
279};
Vince Lehman72446ec2014-07-09 10:50:02 -0500280
Yanbiao Licf0db022016-01-29 00:54:25 -0800281class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
282{
283public:
284 LocalhostAuthorizedRibManagerFixture()
285 : RibManagerFixture({true, false}, true)
286 {
287 }
288};
Vince Lehman72446ec2014-07-09 10:50:02 -0500289
Yanbiao Licf0db022016-01-29 00:54:25 -0800290class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
291{
292public:
293 LocalhopAuthorizedRibManagerFixture()
294 : RibManagerFixture({false, true}, true)
295 {
296 }
297};
298
299class AuthorizedRibManagerFixture : public RibManagerFixture
300{
301public:
302 AuthorizedRibManagerFixture()
303 : RibManagerFixture({true, true}, true)
304 {
305 }
306};
307
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400308using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800309 UnauthorizedRibManagerFixture,
310 LocalhostAuthorizedRibManagerFixture,
311 LocalhopAuthorizedRibManagerFixture,
312 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400313>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800314
315BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
316{
317 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
318 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
319 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
320 auto successResp = this->makeResponse(200, "Success", parameters);
321 auto failureResp = ControlResponse(403, "authorization rejected");
322
323 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
324 this->receiveInterest(commandHost);
325 this->receiveInterest(commandHop);
326
327 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
328 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
329 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
330
331 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000332 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800333 ManagerCommonFixture::CheckResponseResult::OK);
334 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000335 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800336 ManagerCommonFixture::CheckResponseResult::OK);
337 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500338}
339
Yanbiao Licf0db022016-01-29 00:54:25 -0800340BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
341
342BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500343{
Yanbiao Licf0db022016-01-29 00:54:25 -0800344 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
345 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500346
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000347 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
348 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
349 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
350 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500351
Yanbiao Licf0db022016-01-29 00:54:25 -0800352 receiveInterest(commandRegister);
353 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500354
Yanbiao Licf0db022016-01-29 00:54:25 -0800355 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000356 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800357 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000358 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800359 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500360
Yanbiao Licf0db022016-01-29 00:54:25 -0800361 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
362 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
363 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500364}
365
Yanbiao Licf0db022016-01-29 00:54:25 -0800366BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500367{
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
369 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
370 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
371 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500372
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000374 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
375 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
376 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
377 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500378
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 receiveInterest(commandRegister);
380 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500381
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 paramsRegister.setFaceId(inFaceId);
383 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500384
Yanbiao Licf0db022016-01-29 00:54:25 -0800385 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000386 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800387 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000388 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800389 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500390
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
392 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
393 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500394}
395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000397{
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
399 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
400 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000401
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 advanceClocks(time::milliseconds(55));
403 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
404 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
405 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000406
Yanbiao Licf0db022016-01-29 00:54:25 -0800407 m_commands.clear();
408 paramsRegister.setExpirationPeriod(time::milliseconds(100));
409 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000410
Yanbiao Licf0db022016-01-29 00:54:25 -0800411 advanceClocks(time::milliseconds(55));
412 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
413 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000414}
415
Junxiao Shi75306352018-02-01 21:59:44 +0000416BOOST_AUTO_TEST_CASE(NameTooLong)
417{
418 Name prefix;
419 while (prefix.size() <= FIB_MAX_DEPTH) {
420 prefix.append("A");
421 }
422 auto params = makeRegisterParameters(prefix, 2899);
423 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
424 receiveInterest(command);
425
426 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
427 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
428 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
429 " components")),
430 CheckResponseResult::OK);
431
432 BOOST_CHECK_EQUAL(m_commands.size(), 0);
433}
434
Yanbiao Licf0db022016-01-29 00:54:25 -0800435BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
436
Yanbiao Licf0db022016-01-29 00:54:25 -0800437BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500438{
Yanbiao Licf0db022016-01-29 00:54:25 -0800439 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400440 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800441 Route route;
442 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400443 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400444 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800445 return route;
446 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500447
Yanbiao Licf0db022016-01-29 00:54:25 -0800448 const size_t nEntries = 108;
449 std::set<Name> actualPrefixes;
450 for (size_t i = 0; i < nEntries; ++i) {
451 Name prefix = Name("/test-dataset").appendNumber(i);
452 actualPrefixes.insert(prefix);
453 m_rib.insert(prefix, generateRoute());
454 if (i & 0x1) {
455 m_rib.insert(prefix, generateRoute());
456 m_rib.insert(prefix, generateRoute());
457 }
458 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500459
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000460 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500461
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400462 Block content = concatenateResponses();
463 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800464 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
465
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400466 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800467 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400468 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800469 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800470 actualPrefixes.erase(decodedEntry.getName());
471
472 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
473 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
474
475 auto matchedEntry = matchedEntryIt->second;
476 BOOST_REQUIRE(matchedEntry != nullptr);
477
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400478 expectedRecords.emplace_back();
479 expectedRecords.back().setName(matchedEntry->getName());
480 for (const auto& route : matchedEntry->getRoutes()) {
481 expectedRecords.back().addRoute(ndn::nfd::Route()
482 .setFaceId(route.faceId)
483 .setOrigin(route.origin)
484 .setCost(route.cost)
485 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800486 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800487 }
488
489 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
491 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500492}
493
Yanbiao Licf0db022016-01-29 00:54:25 -0800494BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
495
496BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700497{
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600499
Yanbiao Licf0db022016-01-29 00:54:25 -0800500 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
501 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
502 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
503 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700504}
505
Yanbiao Licf0db022016-01-29 00:54:25 -0800506BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500507{
Yanbiao Licf0db022016-01-29 00:54:25 -0800508 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
509 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
510 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
511 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
512 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
513 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500514
Vince Lehman26b215c2014-08-17 15:00:41 -0500515 ndn::nfd::FaceStatus status;
516 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700517 std::vector<ndn::nfd::FaceStatus> activeFaces;
518 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500519
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700520 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800521 advanceClocks(time::milliseconds(100));
522 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500523
Yanbiao Licf0db022016-01-29 00:54:25 -0800524 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
525 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
526 BOOST_CHECK(it2 == m_rib.end());
527 BOOST_REQUIRE(it1 != m_rib.end());
528 BOOST_CHECK(it1->second->hasFaceId(1));
529 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500530}
531
Yanbiao Licf0db022016-01-29 00:54:25 -0800532BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600533{
Yanbiao Licf0db022016-01-29 00:54:25 -0800534 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
535 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
536 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
537 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
538 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600539
Yanbiao Licf0db022016-01-29 00:54:25 -0800540 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
541 ndn::nfd::FaceEventNotification notification;
542 notification.setKind(eventKind).setFaceId(faceId);
543 return notification;
544 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600545
Yanbiao Licf0db022016-01-29 00:54:25 -0800546 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
547 advanceClocks(time::milliseconds(100));
548 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600549
Yanbiao Licf0db022016-01-29 00:54:25 -0800550 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
551 advanceClocks(time::milliseconds(100));
552 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600553}
554
Yanbiao Licf0db022016-01-29 00:54:25 -0800555BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800556BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500557
558} // namespace tests
559} // namespace rib
560} // namespace nfd