blob: 0264b9fbe5cca8274430396f5c0ebc243875a1d7 [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:
140 ControlParameters
141 makeRegisterParameters(const Name& name, uint64_t id = 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)
146 .setFaceId(id)
147 .setOrigin(128)
148 .setCost(10)
149 .setFlags(0)
150 .setExpirationPeriod(expiry);
151 }
152
153 ControlParameters
154 makeUnregisterParameters(const Name& name, uint64_t id = 0)
155 {
156 return ControlParameters()
157 .setName(name)
158 .setFaceId(id)
159 .setOrigin(128);
160 }
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);
341 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse),
342 ManagerCommonFixture::CheckResponseResult::OK);
343 if (nExpectedResponses == 2) {
344 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse),
345 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
Yanbiao Licf0db022016-01-29 00:54:25 -0800356 auto setInFaceId = [] (shared_ptr<Interest> commandInterest) {
357 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234));
358 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500359
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
361 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
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);
367 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
368 CheckResponseResult::OK);
369 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
370 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;
385 auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) {
386 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
387 };
388 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
389 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500390
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 receiveInterest(commandRegister);
392 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500393
Yanbiao Licf0db022016-01-29 00:54:25 -0800394 paramsRegister.setFaceId(inFaceId);
395 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500396
Yanbiao Licf0db022016-01-29 00:54:25 -0800397 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
398 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
399 CheckResponseResult::OK);
400 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
401 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500402
Yanbiao Licf0db022016-01-29 00:54:25 -0800403 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
404 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
405 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500406}
407
Yanbiao Licf0db022016-01-29 00:54:25 -0800408BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000409{
Yanbiao Licf0db022016-01-29 00:54:25 -0800410 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
411 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
412 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000413
Yanbiao Licf0db022016-01-29 00:54:25 -0800414 advanceClocks(time::milliseconds(55));
415 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
416 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
417 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000418
Yanbiao Licf0db022016-01-29 00:54:25 -0800419 m_commands.clear();
420 paramsRegister.setExpirationPeriod(time::milliseconds(100));
421 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000422
Yanbiao Licf0db022016-01-29 00:54:25 -0800423 advanceClocks(time::milliseconds(55));
424 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
425 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000426}
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;
Yanbiao Licf0db022016-01-29 00:54:25 -0800437 route.expires = time::steady_clock::TimePoint::max();
438 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
Yanbiao Licf0db022016-01-29 00:54:25 -0800453 receiveInterest(makeInterest("/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