blob: 7ef01943e7720a7ddc8717d62fade9b6b0d6a001 [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventod2ea2352017-03-12 20:38:09 -04003 * Copyright (c) 2014-2017, 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"
Yanbiao Licf0db022016-01-29 00:54:25 -080027#include "manager-common-fixture.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050028
Junxiao Shicbc8e942016-09-06 03:17:45 +000029#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000030#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Davide Pesaventod2ea2352017-03-12 20:38:09 -040031#include <ndn-cxx/mgmt/nfd/face-status.hpp>
32#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050033
Vince Lehman72446ec2014-07-09 10:50:02 -050034namespace nfd {
35namespace rib {
36namespace tests {
37
Junxiao Shi0de23a22015-12-03 20:07:02 +000038using namespace nfd::tests;
39
Yanbiao Licf0db022016-01-29 00:54:25 -080040struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050041{
Yanbiao Licf0db022016-01-29 00:54:25 -080042 bool isLocalhostConfigured;
43 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050044};
45
Yanbiao Licf0db022016-01-29 00:54:25 -080046class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050047{
48public:
Yanbiao Licf0db022016-01-29 00:54:25 -080049 explicit
50 RibManagerFixture(const ConfigurationStatus& status,
51 bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000052 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080053 , m_status(status)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000054 , m_manager(m_dispatcher, m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080055 , m_rib(m_manager.m_rib)
Vince Lehman72446ec2014-07-09 10:50:02 -050056 {
Yanbiao Licf0db022016-01-29 00:54:25 -080057 m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
Vince Lehman72446ec2014-07-09 10:50:02 -050058
Yanbiao Licf0db022016-01-29 00:54:25 -080059 const std::string prefix = "rib\n{\n";
60 const std::string suffix = "}";
61 const std::string localhostSection = " localhost_security\n"
Vince Lehman72446ec2014-07-09 10:50:02 -050062 " {\n"
63 " trust-anchor\n"
64 " {\n"
65 " type any\n"
66 " }\n"
Yanbiao Licf0db022016-01-29 00:54:25 -080067 " }\n";
68 const std::string localhopSection = " localhop_security\n"
69 " {\n"
70 " trust-anchor\n"
71 " {\n"
72 " type any\n"
73 " }\n"
74 " }\n";
Vince Lehman72446ec2014-07-09 10:50:02 -050075
Yanbiao Licf0db022016-01-29 00:54:25 -080076 std::string ribSection = "";
77 if (m_status.isLocalhostConfigured) {
78 ribSection += localhostSection;
79 }
80 if (m_status.isLocalhopConfigured) {
81 ribSection += localhopSection;
82 }
83 const std::string CONFIG_STR = prefix + ribSection + suffix;
84
85 ConfigFile config;
86 m_manager.setConfigFile(config);
87 config.parse(CONFIG_STR, true, "test-rib");
88
89 registerWithNfd();
90
91 if (shouldClearRib) {
92 clearRib();
93 }
94 }
95
96private:
97 void
98 registerWithNfd()
99 {
100 m_manager.registerWithNfd();
101 advanceClocks(time::milliseconds(1));
102
103 auto replyFibAddCommand = [this] (const Interest& interest) {
104 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000105 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800106 params.setFaceId(1).setCost(0);
107 nfd::ControlResponse resp;
108
109 resp.setCode(200).setBody(params.wireEncode());
110 shared_ptr<Data> data = make_shared<Data>(interest.getName());
111 data->setContent(resp.wireEncode());
112
113 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
114
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000115 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800116 };
117
118 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000119 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800120 if (commandPrefix.isPrefixOf(command.getName())) {
121 replyFibAddCommand(command);
122 advanceClocks(time::milliseconds(1));
123 }
124 }
125
126 // clear commands and responses
127 m_responses.clear();
128 m_commands.clear();
129 }
130
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000131 void
132 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800133 {
134 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000135 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800136 }
137 }
138
139public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400140 static ControlParameters
141 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400142 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800143 {
144 return ControlParameters()
145 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400146 .setFaceId(faceId)
147 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800148 .setCost(10)
149 .setFlags(0)
150 .setExpirationPeriod(expiry);
151 }
152
Davide Pesavento22db5392017-04-14 00:56:43 -0400153 static ControlParameters
154 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800155 {
156 return ControlParameters()
157 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400158 .setFaceId(faceId)
159 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800160 }
161
162 void
163 onSendBatchFromQueue(const RibUpdateBatch& batch)
164 {
165 BOOST_ASSERT(batch.begin() != batch.end());
166 RibUpdate update = *(batch.begin());
167
168 // Simulate a successful response from NFD
169 FibUpdater& updater = m_manager.m_fibUpdater;
170 m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
171 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
172 }
173
174public:
175 enum class CheckCommandResult {
176 OK,
177 OUT_OF_BOUNDARY,
178 WRONG_FORMAT,
179 WRONG_VERB,
180 WRONG_PARAMS_FORMAT,
181 WRONG_PARAMS_NAME,
182 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400183 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800184
185 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400186 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800187 {
188 if (idx > m_commands.size()) {
189 return CheckCommandResult::OUT_OF_BOUNDARY;
190 }
191 const auto& name = m_commands[idx].getName();
192
193 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
194 return CheckCommandResult::WRONG_FORMAT;
195 }
196 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
197
198 Name::Component expectedVerb(verbStr);
199 if (verb != expectedVerb) {
200 return CheckCommandResult::WRONG_VERB;
201 }
202
203 ControlParameters parameters;
204 try {
205 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
206 parameters.wireDecode(rawParameters);
207 }
208 catch (...) {
209 return CheckCommandResult::WRONG_PARAMS_FORMAT;
210 }
211
212 if (parameters.getName() != expectedParams.getName()) {
213 return CheckCommandResult::WRONG_PARAMS_NAME;
214 }
215
216 if (parameters.getFaceId() != expectedParams.getFaceId()) {
217 return CheckCommandResult::WRONG_PARAMS_FACE;
218 }
219
220 return CheckCommandResult::OK;
221 }
222
223public:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400224 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 std::vector<Interest>& m_commands;
226 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800227
228protected:
229 RibManager m_manager;
230 Rib& m_rib;
231};
232
233const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
234
235std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400236operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800237{
238 switch (result) {
239 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400240 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800241 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400242 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800243 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400244 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800245 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400246 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800247 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400248 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800249 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400250 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400252 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 };
254
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400255 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800256}
257
Yanbiao Licf0db022016-01-29 00:54:25 -0800258BOOST_AUTO_TEST_SUITE(TestRibManager)
259
260class AddTopPrefixFixture : public RibManagerFixture
261{
262public:
263 AddTopPrefixFixture()
264 : RibManagerFixture({true, true}, false)
265 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500266 }
267};
268
Yanbiao Licf0db022016-01-29 00:54:25 -0800269BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700270{
Yanbiao Licf0db022016-01-29 00:54:25 -0800271 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600272
Yanbiao Licf0db022016-01-29 00:54:25 -0800273 std::vector<Name> ribEntryNames;
274 for (auto&& entry : m_rib) {
275 ribEntryNames.push_back(entry.first);
276 }
277 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
278 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700279}
280
Yanbiao Licf0db022016-01-29 00:54:25 -0800281class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500282{
Yanbiao Licf0db022016-01-29 00:54:25 -0800283public:
284 UnauthorizedRibManagerFixture()
285 : RibManagerFixture({false, false}, true)
286 {
287 }
288};
Vince Lehman72446ec2014-07-09 10:50:02 -0500289
Yanbiao Licf0db022016-01-29 00:54:25 -0800290class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
291{
292public:
293 LocalhostAuthorizedRibManagerFixture()
294 : RibManagerFixture({true, false}, true)
295 {
296 }
297};
Vince Lehman72446ec2014-07-09 10:50:02 -0500298
Yanbiao Licf0db022016-01-29 00:54:25 -0800299class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
300{
301public:
302 LocalhopAuthorizedRibManagerFixture()
303 : RibManagerFixture({false, true}, true)
304 {
305 }
306};
307
308class AuthorizedRibManagerFixture : public RibManagerFixture
309{
310public:
311 AuthorizedRibManagerFixture()
312 : RibManagerFixture({true, true}, true)
313 {
314 }
315};
316
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400317using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800318 UnauthorizedRibManagerFixture,
319 LocalhostAuthorizedRibManagerFixture,
320 LocalhopAuthorizedRibManagerFixture,
321 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400322>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800323
324BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
325{
326 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
327 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
328 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
329 auto successResp = this->makeResponse(200, "Success", parameters);
330 auto failureResp = ControlResponse(403, "authorization rejected");
331
332 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
333 this->receiveInterest(commandHost);
334 this->receiveInterest(commandHop);
335
336 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
337 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
338 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
339
340 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000341 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800342 ManagerCommonFixture::CheckResponseResult::OK);
343 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000344 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800345 ManagerCommonFixture::CheckResponseResult::OK);
346 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500347}
348
Yanbiao Licf0db022016-01-29 00:54:25 -0800349BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
350
351BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500352{
Yanbiao Licf0db022016-01-29 00:54:25 -0800353 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
354 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500355
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000356 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
357 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
358 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
359 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500360
Yanbiao Licf0db022016-01-29 00:54:25 -0800361 receiveInterest(commandRegister);
362 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500363
Yanbiao Licf0db022016-01-29 00:54:25 -0800364 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000365 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000367 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500369
Yanbiao Licf0db022016-01-29 00:54:25 -0800370 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
371 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
372 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373}
374
Yanbiao Licf0db022016-01-29 00:54:25 -0800375BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500376{
Yanbiao Licf0db022016-01-29 00:54:25 -0800377 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
378 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
379 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
380 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500381
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000383 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
384 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
385 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
386 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500387
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 receiveInterest(commandRegister);
389 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500390
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 paramsRegister.setFaceId(inFaceId);
392 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500393
Yanbiao Licf0db022016-01-29 00:54:25 -0800394 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000395 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000397 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500399
Yanbiao Licf0db022016-01-29 00:54:25 -0800400 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
401 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
402 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500403}
404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000406{
Yanbiao Licf0db022016-01-29 00:54:25 -0800407 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
408 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
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(), 2); // the registered route has expired
413 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
414 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000415
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 m_commands.clear();
417 paramsRegister.setExpirationPeriod(time::milliseconds(100));
418 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000419
Yanbiao Licf0db022016-01-29 00:54:25 -0800420 advanceClocks(time::milliseconds(55));
421 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
422 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000423}
424
Yanbiao Licf0db022016-01-29 00:54:25 -0800425BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
426
Yanbiao Licf0db022016-01-29 00:54:25 -0800427BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500428{
Yanbiao Licf0db022016-01-29 00:54:25 -0800429 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400430 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800431 Route route;
432 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400433 route.cost = route.faceId * 10;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000434 route.expires = ndn::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800435 return route;
436 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500437
Yanbiao Licf0db022016-01-29 00:54:25 -0800438 const size_t nEntries = 108;
439 std::set<Name> actualPrefixes;
440 for (size_t i = 0; i < nEntries; ++i) {
441 Name prefix = Name("/test-dataset").appendNumber(i);
442 actualPrefixes.insert(prefix);
443 m_rib.insert(prefix, generateRoute());
444 if (i & 0x1) {
445 m_rib.insert(prefix, generateRoute());
446 m_rib.insert(prefix, generateRoute());
447 }
448 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500449
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000450 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500451
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400452 Block content = concatenateResponses();
453 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800454 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
455
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400456 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800457 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400458 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800460 actualPrefixes.erase(decodedEntry.getName());
461
462 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
463 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
464
465 auto matchedEntry = matchedEntryIt->second;
466 BOOST_REQUIRE(matchedEntry != nullptr);
467
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400468 expectedRecords.emplace_back();
469 expectedRecords.back().setName(matchedEntry->getName());
470 for (const auto& route : matchedEntry->getRoutes()) {
471 expectedRecords.back().addRoute(ndn::nfd::Route()
472 .setFaceId(route.faceId)
473 .setOrigin(route.origin)
474 .setCost(route.cost)
475 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800476 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800477 }
478
479 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800480 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
481 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500482}
483
Yanbiao Licf0db022016-01-29 00:54:25 -0800484BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
485
486BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700487{
Yanbiao Licf0db022016-01-29 00:54:25 -0800488 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600489
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
491 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
492 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
493 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700494}
495
Yanbiao Licf0db022016-01-29 00:54:25 -0800496BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500497{
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
499 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
500 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
501 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
502 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
503 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500504
Vince Lehman26b215c2014-08-17 15:00:41 -0500505 ndn::nfd::FaceStatus status;
506 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700507 std::vector<ndn::nfd::FaceStatus> activeFaces;
508 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500509
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700510 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800511 advanceClocks(time::milliseconds(100));
512 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500513
Yanbiao Licf0db022016-01-29 00:54:25 -0800514 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
515 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
516 BOOST_CHECK(it2 == m_rib.end());
517 BOOST_REQUIRE(it1 != m_rib.end());
518 BOOST_CHECK(it1->second->hasFaceId(1));
519 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500520}
521
Yanbiao Licf0db022016-01-29 00:54:25 -0800522BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600523{
Yanbiao Licf0db022016-01-29 00:54:25 -0800524 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
525 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
526 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
527 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
528 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600529
Yanbiao Licf0db022016-01-29 00:54:25 -0800530 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
531 ndn::nfd::FaceEventNotification notification;
532 notification.setKind(eventKind).setFaceId(faceId);
533 return notification;
534 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600535
Yanbiao Licf0db022016-01-29 00:54:25 -0800536 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
537 advanceClocks(time::milliseconds(100));
538 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600539
Yanbiao Licf0db022016-01-29 00:54:25 -0800540 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
541 advanceClocks(time::milliseconds(100));
542 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600543}
544
Yanbiao Licf0db022016-01-29 00:54:25 -0800545BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800546BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500547
548} // namespace tests
549} // namespace rib
550} // namespace nfd