blob: 0f1e3f7d7a712420cbaba2efa4af5a41895db473 [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Licf0db022016-01-29 00:54:25 -08003 * Copyright (c) 2014-2016, 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
Yanbiao Licf0db022016-01-29 00:54:25 -080029#include <ndn-cxx/management/nfd-rib-entry.hpp>
30#include <ndn-cxx/management/nfd-face-status.hpp>
31#include <ndn-cxx/management/nfd-face-event-notification.hpp>
32#include <ndn-cxx/util/random.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)
52 : m_commands(m_face->sentInterests)
53 , m_status(status)
54 , m_manager(m_dispatcher, *m_face, m_keyChain)
55 , 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());
105 params.setFaceId(1).setCost(0);
106 nfd::ControlResponse resp;
107
108 resp.setCode(200).setBody(params.wireEncode());
109 shared_ptr<Data> data = make_shared<Data>(interest.getName());
110 data->setContent(resp.wireEncode());
111
112 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
113
114 m_face->getIoService().post([this, data] { m_face->receive(*data); });
115 };
116
117 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
118 for (auto&& command : m_commands) {
119 if (commandPrefix.isPrefixOf(command.getName())) {
120 replyFibAddCommand(command);
121 advanceClocks(time::milliseconds(1));
122 }
123 }
124
125 // clear commands and responses
126 m_responses.clear();
127 m_commands.clear();
128 }
129
130 void clearRib()
131 {
132 while (!m_rib.empty()) {
133 auto& name = m_rib.begin()->first;
134 auto& routes = m_rib.begin()->second->getRoutes();
135 while (!routes.empty()) {
136 m_rib.erase(name, *routes.begin());
137 }
138 }
139 }
140
141public:
142 ControlParameters
143 makeRegisterParameters(const Name& name, uint64_t id = 0,
144 const time::milliseconds expiry = time::milliseconds::max())
145 {
146 return ControlParameters()
147 .setName(name)
148 .setFaceId(id)
149 .setOrigin(128)
150 .setCost(10)
151 .setFlags(0)
152 .setExpirationPeriod(expiry);
153 }
154
155 ControlParameters
156 makeUnregisterParameters(const Name& name, uint64_t id = 0)
157 {
158 return ControlParameters()
159 .setName(name)
160 .setFaceId(id)
161 .setOrigin(128);
162 }
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
185 };
186
187 CheckCommandResult
188 checkCommand(size_t idx, const char* verbStr, ControlParameters expectedParams)
189 {
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:
226 std::vector<Interest>& m_commands;
227 ConfigurationStatus m_status;
228 static const Name FIB_COMMAND_PREFIX;
229
230protected:
231 RibManager m_manager;
232 Rib& m_rib;
233};
234
235const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
236
237std::ostream&
238operator<<(std::ostream& os, const RibManagerFixture::CheckCommandResult& result)
239{
240 switch (result) {
241 case RibManagerFixture::CheckCommandResult::OK:
242 os << "OK";
243 break;
244 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
245 os << "OUT_OF_BOUNDARY";
246 break;
247 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
248 os << "WRONG_FORMAT";
249 break;
250 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
251 os << "WRONG_VERB";
252 break;
253 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
254 os << "WRONG_COST";
255 break;
256 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
257 os << "WRONG_PARAMS_NAME";
258 break;
259 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
260 os << "WRONG_PARAMS_FACE";
261 break;
262 default:
263 break;
264 };
265
266 return os;
267}
268
269BOOST_AUTO_TEST_SUITE(Rib)
270BOOST_AUTO_TEST_SUITE(TestRibManager)
271
272class AddTopPrefixFixture : public RibManagerFixture
273{
274public:
275 AddTopPrefixFixture()
276 : RibManagerFixture({true, true}, false)
277 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500278 }
279};
280
Yanbiao Licf0db022016-01-29 00:54:25 -0800281BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700282{
Yanbiao Licf0db022016-01-29 00:54:25 -0800283 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600284
Yanbiao Licf0db022016-01-29 00:54:25 -0800285 std::vector<Name> ribEntryNames;
286 for (auto&& entry : m_rib) {
287 ribEntryNames.push_back(entry.first);
288 }
289 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
290 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700291}
292
Yanbiao Licf0db022016-01-29 00:54:25 -0800293class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500294{
Yanbiao Licf0db022016-01-29 00:54:25 -0800295public:
296 UnauthorizedRibManagerFixture()
297 : RibManagerFixture({false, false}, true)
298 {
299 }
300};
Vince Lehman72446ec2014-07-09 10:50:02 -0500301
Yanbiao Licf0db022016-01-29 00:54:25 -0800302class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
303{
304public:
305 LocalhostAuthorizedRibManagerFixture()
306 : RibManagerFixture({true, false}, true)
307 {
308 }
309};
Vince Lehman72446ec2014-07-09 10:50:02 -0500310
Yanbiao Licf0db022016-01-29 00:54:25 -0800311class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
312{
313public:
314 LocalhopAuthorizedRibManagerFixture()
315 : RibManagerFixture({false, true}, true)
316 {
317 }
318};
319
320class AuthorizedRibManagerFixture : public RibManagerFixture
321{
322public:
323 AuthorizedRibManagerFixture()
324 : RibManagerFixture({true, true}, true)
325 {
326 }
327};
328
329typedef boost::mpl::vector<
330 UnauthorizedRibManagerFixture,
331 LocalhostAuthorizedRibManagerFixture,
332 LocalhopAuthorizedRibManagerFixture,
333 AuthorizedRibManagerFixture
334> AllFixtures;
335
336BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
337{
338 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
339 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
340 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
341 auto successResp = this->makeResponse(200, "Success", parameters);
342 auto failureResp = ControlResponse(403, "authorization rejected");
343
344 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
345 this->receiveInterest(commandHost);
346 this->receiveInterest(commandHop);
347
348 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
349 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
350 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
351
352 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
353 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse),
354 ManagerCommonFixture::CheckResponseResult::OK);
355 if (nExpectedResponses == 2) {
356 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse),
357 ManagerCommonFixture::CheckResponseResult::OK);
358 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500359}
360
Yanbiao Licf0db022016-01-29 00:54:25 -0800361BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
362
363BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500364{
Yanbiao Licf0db022016-01-29 00:54:25 -0800365 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
366 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500367
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 auto setInFaceId = [] (shared_ptr<Interest> commandInterest) {
369 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234));
370 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500371
Yanbiao Licf0db022016-01-29 00:54:25 -0800372 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
373 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500374
Yanbiao Licf0db022016-01-29 00:54:25 -0800375 receiveInterest(commandRegister);
376 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500377
Yanbiao Licf0db022016-01-29 00:54:25 -0800378 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
379 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
380 CheckResponseResult::OK);
381 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
382 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500383
Yanbiao Licf0db022016-01-29 00:54:25 -0800384 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
385 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
386 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500387}
388
Yanbiao Licf0db022016-01-29 00:54:25 -0800389BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500390{
Yanbiao Licf0db022016-01-29 00:54:25 -0800391 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
392 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
393 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
394 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 const uint64_t inFaceId = 9527;
397 auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) {
398 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
399 };
400 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
401 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500402
Yanbiao Licf0db022016-01-29 00:54:25 -0800403 receiveInterest(commandRegister);
404 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500405
Yanbiao Licf0db022016-01-29 00:54:25 -0800406 paramsRegister.setFaceId(inFaceId);
407 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500408
Yanbiao Licf0db022016-01-29 00:54:25 -0800409 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
410 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
411 CheckResponseResult::OK);
412 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
413 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500414
Yanbiao Licf0db022016-01-29 00:54:25 -0800415 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
416 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
417 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500418}
419
Yanbiao Licf0db022016-01-29 00:54:25 -0800420BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000421{
Yanbiao Licf0db022016-01-29 00:54:25 -0800422 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
423 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
424 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000425
Yanbiao Licf0db022016-01-29 00:54:25 -0800426 advanceClocks(time::milliseconds(55));
427 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
428 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
429 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000430
Yanbiao Licf0db022016-01-29 00:54:25 -0800431 m_commands.clear();
432 paramsRegister.setExpirationPeriod(time::milliseconds(100));
433 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000434
Yanbiao Licf0db022016-01-29 00:54:25 -0800435 advanceClocks(time::milliseconds(55));
436 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
437 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000438}
439
Yanbiao Licf0db022016-01-29 00:54:25 -0800440BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
441
442// @todo Remove when ndn::nfd::RibEntry implements operator!=
443class RibEntry : public ndn::nfd::RibEntry
Junxiao Shi0de23a22015-12-03 20:07:02 +0000444{
Yanbiao Licf0db022016-01-29 00:54:25 -0800445public:
446 RibEntry() = default;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000447
Yanbiao Licf0db022016-01-29 00:54:25 -0800448 RibEntry(const ndn::nfd::RibEntry& entry)
449 : ndn::nfd::RibEntry(entry)
450 {
451 }
452};
Junxiao Shi0de23a22015-12-03 20:07:02 +0000453
Yanbiao Licf0db022016-01-29 00:54:25 -0800454bool
455operator!=(const RibEntry& left, const RibEntry& right)
456{
457 if (left.getName() != right.getName()) {
458 return true;
459 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000460
Yanbiao Licf0db022016-01-29 00:54:25 -0800461 auto leftRoutes = left.getRoutes();
462 auto rightRoutes = right.getRoutes();
463 if (leftRoutes.size() != rightRoutes.size()) {
464 return true;
465 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000466
Yanbiao Licf0db022016-01-29 00:54:25 -0800467 for (auto&& route : leftRoutes) {
468 auto hitEntry =
469 std::find_if(rightRoutes.begin(), rightRoutes.end(), [&] (const ndn::nfd::Route& record) {
470 return route.getFaceId() == record.getFaceId() &&
471 route.getCost() == record.getCost() &&
472 route.getOrigin() == record.getOrigin() &&
473 route.getFlags() == record.getFlags() &&
474 route.getExpirationPeriod() == record.getExpirationPeriod();
475 });
Junxiao Shi0de23a22015-12-03 20:07:02 +0000476
Yanbiao Licf0db022016-01-29 00:54:25 -0800477 if (hitEntry == rightRoutes.end()) {
478 return true;
479 }
480 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000481
Yanbiao Licf0db022016-01-29 00:54:25 -0800482 return false;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000483}
Vince Lehman76c751c2014-11-18 17:36:38 -0600484
Yanbiao Licf0db022016-01-29 00:54:25 -0800485BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500486{
Yanbiao Licf0db022016-01-29 00:54:25 -0800487 uint64_t faceId = 0;
488 auto generateRoute = [&faceId] () -> Route {
489 Route route;
490 route.faceId = ++faceId;
491 route.cost = ndn::random::generateWord64();
492 route.expires = time::steady_clock::TimePoint::max();
493 return route;
494 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500495
Yanbiao Licf0db022016-01-29 00:54:25 -0800496 const size_t nEntries = 108;
497 std::set<Name> actualPrefixes;
498 for (size_t i = 0; i < nEntries; ++i) {
499 Name prefix = Name("/test-dataset").appendNumber(i);
500 actualPrefixes.insert(prefix);
501 m_rib.insert(prefix, generateRoute());
502 if (i & 0x1) {
503 m_rib.insert(prefix, generateRoute());
504 m_rib.insert(prefix, generateRoute());
505 }
506 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500507
Yanbiao Licf0db022016-01-29 00:54:25 -0800508 receiveInterest(makeInterest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500509
Yanbiao Licf0db022016-01-29 00:54:25 -0800510 Block content;
511 BOOST_CHECK_NO_THROW(content = concatenateResponses());
512 BOOST_CHECK_NO_THROW(content.parse());
513 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
514
515 std::vector<RibEntry> receivedRecords, expectedRecords;
516 for (size_t idx = 0; idx < nEntries; ++idx) {
517 BOOST_TEST_MESSAGE("processing element: " << idx);
518
519 RibEntry decodedEntry;
520 BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
521 receivedRecords.push_back(decodedEntry);
522
523 actualPrefixes.erase(decodedEntry.getName());
524
525 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
526 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
527
528 auto matchedEntry = matchedEntryIt->second;
529 BOOST_REQUIRE(matchedEntry != nullptr);
530
531 RibEntry record;
532 record.setName(matchedEntry->getName());
533 const auto& routes = matchedEntry->getRoutes();
534 for (auto&& route : routes) {
535 ndn::nfd::Route routeRecord;
536 routeRecord.setFaceId(route.faceId);
537 routeRecord.setOrigin(route.origin);
538 routeRecord.setFlags(route.flags);
539 routeRecord.setCost(route.cost);
540 record.addRoute(routeRecord);
541 }
542 expectedRecords.push_back(record);
543 }
544
545 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
546
547 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
548 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500549}
550
Yanbiao Licf0db022016-01-29 00:54:25 -0800551BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
552
553BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700554{
Yanbiao Licf0db022016-01-29 00:54:25 -0800555 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600556
Yanbiao Licf0db022016-01-29 00:54:25 -0800557 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
558 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
559 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
560 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700561}
562
Yanbiao Licf0db022016-01-29 00:54:25 -0800563BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500564{
Yanbiao Licf0db022016-01-29 00:54:25 -0800565 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
566 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
567 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
568 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
569 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
570 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500571
Vince Lehman26b215c2014-08-17 15:00:41 -0500572 ndn::nfd::FaceStatus status;
573 status.setFaceId(1);
574
Yanbiao Licf0db022016-01-29 00:54:25 -0800575 auto data = makeData("/localhost/nfd/faces/list");
Vince Lehman26b215c2014-08-17 15:00:41 -0500576 data->setContent(status.wireEncode());
577
Yanbiao Licf0db022016-01-29 00:54:25 -0800578 auto buffer = make_shared<ndn::OBufferStream>();
Vince Lehman26b215c2014-08-17 15:00:41 -0500579 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
580 data->getContent().value_size());
581
Yanbiao Licf0db022016-01-29 00:54:25 -0800582 m_manager.removeInvalidFaces(buffer);
583 advanceClocks(time::milliseconds(100));
584 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500585
Yanbiao Licf0db022016-01-29 00:54:25 -0800586 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
587 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
588 BOOST_CHECK(it2 == m_rib.end());
589 BOOST_REQUIRE(it1 != m_rib.end());
590 BOOST_CHECK(it1->second->hasFaceId(1));
591 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500592}
593
Yanbiao Licf0db022016-01-29 00:54:25 -0800594BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600595{
Yanbiao Licf0db022016-01-29 00:54:25 -0800596 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
597 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
598 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
599 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
600 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600601
Yanbiao Licf0db022016-01-29 00:54:25 -0800602 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
603 ndn::nfd::FaceEventNotification notification;
604 notification.setKind(eventKind).setFaceId(faceId);
605 return notification;
606 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600607
Yanbiao Licf0db022016-01-29 00:54:25 -0800608 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
609 advanceClocks(time::milliseconds(100));
610 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600611
Yanbiao Licf0db022016-01-29 00:54:25 -0800612 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
613 advanceClocks(time::milliseconds(100));
614 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600615}
616
Yanbiao Licf0db022016-01-29 00:54:25 -0800617BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Vince Lehman76c751c2014-11-18 17:36:38 -0600618
Yanbiao Licf0db022016-01-29 00:54:25 -0800619BOOST_AUTO_TEST_SUITE_END() // TestRibManager
620BOOST_AUTO_TEST_SUITE_END() // Rib
Vince Lehman72446ec2014-07-09 10:50:02 -0500621
622} // namespace tests
623} // namespace rib
624} // namespace nfd