blob: 610c61d061ed46deb159fe66f4586514ae7998d9 [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());
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
115 m_face->getIoService().post([this, data] { m_face->receive(*data); });
116 };
117
118 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
119 for (auto&& command : m_commands) {
120 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
131 void clearRib()
132 {
133 while (!m_rib.empty()) {
134 auto& name = m_rib.begin()->first;
135 auto& routes = m_rib.begin()->second->getRoutes();
136 while (!routes.empty()) {
137 m_rib.erase(name, *routes.begin());
138 }
139 }
140 }
141
142public:
143 ControlParameters
144 makeRegisterParameters(const Name& name, uint64_t id = 0,
145 const time::milliseconds expiry = time::milliseconds::max())
146 {
147 return ControlParameters()
148 .setName(name)
149 .setFaceId(id)
150 .setOrigin(128)
151 .setCost(10)
152 .setFlags(0)
153 .setExpirationPeriod(expiry);
154 }
155
156 ControlParameters
157 makeUnregisterParameters(const Name& name, uint64_t id = 0)
158 {
159 return ControlParameters()
160 .setName(name)
161 .setFaceId(id)
162 .setOrigin(128);
163 }
164
165 void
166 onSendBatchFromQueue(const RibUpdateBatch& batch)
167 {
168 BOOST_ASSERT(batch.begin() != batch.end());
169 RibUpdate update = *(batch.begin());
170
171 // Simulate a successful response from NFD
172 FibUpdater& updater = m_manager.m_fibUpdater;
173 m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
174 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
175 }
176
177public:
178 enum class CheckCommandResult {
179 OK,
180 OUT_OF_BOUNDARY,
181 WRONG_FORMAT,
182 WRONG_VERB,
183 WRONG_PARAMS_FORMAT,
184 WRONG_PARAMS_NAME,
185 WRONG_PARAMS_FACE
186 };
187
188 CheckCommandResult
189 checkCommand(size_t idx, const char* verbStr, ControlParameters expectedParams)
190 {
191 if (idx > m_commands.size()) {
192 return CheckCommandResult::OUT_OF_BOUNDARY;
193 }
194 const auto& name = m_commands[idx].getName();
195
196 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
197 return CheckCommandResult::WRONG_FORMAT;
198 }
199 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
200
201 Name::Component expectedVerb(verbStr);
202 if (verb != expectedVerb) {
203 return CheckCommandResult::WRONG_VERB;
204 }
205
206 ControlParameters parameters;
207 try {
208 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
209 parameters.wireDecode(rawParameters);
210 }
211 catch (...) {
212 return CheckCommandResult::WRONG_PARAMS_FORMAT;
213 }
214
215 if (parameters.getName() != expectedParams.getName()) {
216 return CheckCommandResult::WRONG_PARAMS_NAME;
217 }
218
219 if (parameters.getFaceId() != expectedParams.getFaceId()) {
220 return CheckCommandResult::WRONG_PARAMS_FACE;
221 }
222
223 return CheckCommandResult::OK;
224 }
225
226public:
227 std::vector<Interest>& m_commands;
228 ConfigurationStatus m_status;
229 static const Name FIB_COMMAND_PREFIX;
230
231protected:
232 RibManager m_manager;
233 Rib& m_rib;
234};
235
236const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
237
238std::ostream&
239operator<<(std::ostream& os, const RibManagerFixture::CheckCommandResult& result)
240{
241 switch (result) {
242 case RibManagerFixture::CheckCommandResult::OK:
243 os << "OK";
244 break;
245 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
246 os << "OUT_OF_BOUNDARY";
247 break;
248 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
249 os << "WRONG_FORMAT";
250 break;
251 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
252 os << "WRONG_VERB";
253 break;
254 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
255 os << "WRONG_COST";
256 break;
257 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
258 os << "WRONG_PARAMS_NAME";
259 break;
260 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
261 os << "WRONG_PARAMS_FACE";
262 break;
263 default:
264 break;
265 };
266
267 return os;
268}
269
270BOOST_AUTO_TEST_SUITE(Rib)
271BOOST_AUTO_TEST_SUITE(TestRibManager)
272
273class AddTopPrefixFixture : public RibManagerFixture
274{
275public:
276 AddTopPrefixFixture()
277 : RibManagerFixture({true, true}, false)
278 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500279 }
280};
281
Yanbiao Licf0db022016-01-29 00:54:25 -0800282BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700283{
Yanbiao Licf0db022016-01-29 00:54:25 -0800284 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600285
Yanbiao Licf0db022016-01-29 00:54:25 -0800286 std::vector<Name> ribEntryNames;
287 for (auto&& entry : m_rib) {
288 ribEntryNames.push_back(entry.first);
289 }
290 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
291 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700292}
293
Yanbiao Licf0db022016-01-29 00:54:25 -0800294class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500295{
Yanbiao Licf0db022016-01-29 00:54:25 -0800296public:
297 UnauthorizedRibManagerFixture()
298 : RibManagerFixture({false, false}, true)
299 {
300 }
301};
Vince Lehman72446ec2014-07-09 10:50:02 -0500302
Yanbiao Licf0db022016-01-29 00:54:25 -0800303class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
304{
305public:
306 LocalhostAuthorizedRibManagerFixture()
307 : RibManagerFixture({true, false}, true)
308 {
309 }
310};
Vince Lehman72446ec2014-07-09 10:50:02 -0500311
Yanbiao Licf0db022016-01-29 00:54:25 -0800312class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
313{
314public:
315 LocalhopAuthorizedRibManagerFixture()
316 : RibManagerFixture({false, true}, true)
317 {
318 }
319};
320
321class AuthorizedRibManagerFixture : public RibManagerFixture
322{
323public:
324 AuthorizedRibManagerFixture()
325 : RibManagerFixture({true, true}, true)
326 {
327 }
328};
329
330typedef boost::mpl::vector<
331 UnauthorizedRibManagerFixture,
332 LocalhostAuthorizedRibManagerFixture,
333 LocalhopAuthorizedRibManagerFixture,
334 AuthorizedRibManagerFixture
335> AllFixtures;
336
337BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
338{
339 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
340 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
341 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
342 auto successResp = this->makeResponse(200, "Success", parameters);
343 auto failureResp = ControlResponse(403, "authorization rejected");
344
345 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
346 this->receiveInterest(commandHost);
347 this->receiveInterest(commandHop);
348
349 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
350 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
351 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
352
353 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
354 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse),
355 ManagerCommonFixture::CheckResponseResult::OK);
356 if (nExpectedResponses == 2) {
357 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse),
358 ManagerCommonFixture::CheckResponseResult::OK);
359 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500360}
361
Yanbiao Licf0db022016-01-29 00:54:25 -0800362BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
363
364BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500365{
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
367 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500368
Yanbiao Licf0db022016-01-29 00:54:25 -0800369 auto setInFaceId = [] (shared_ptr<Interest> commandInterest) {
370 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234));
371 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500372
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
374 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500375
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 receiveInterest(commandRegister);
377 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500378
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
380 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
381 CheckResponseResult::OK);
382 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
383 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500384
Yanbiao Licf0db022016-01-29 00:54:25 -0800385 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
386 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
387 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500388}
389
Yanbiao Licf0db022016-01-29 00:54:25 -0800390BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500391{
Yanbiao Licf0db022016-01-29 00:54:25 -0800392 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
393 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
394 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
395 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500396
Yanbiao Licf0db022016-01-29 00:54:25 -0800397 const uint64_t inFaceId = 9527;
398 auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) {
399 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
400 };
401 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
402 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500403
Yanbiao Licf0db022016-01-29 00:54:25 -0800404 receiveInterest(commandRegister);
405 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500406
Yanbiao Licf0db022016-01-29 00:54:25 -0800407 paramsRegister.setFaceId(inFaceId);
408 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500409
Yanbiao Licf0db022016-01-29 00:54:25 -0800410 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
411 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
412 CheckResponseResult::OK);
413 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
414 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500415
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
417 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
418 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500419}
420
Yanbiao Licf0db022016-01-29 00:54:25 -0800421BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000422{
Yanbiao Licf0db022016-01-29 00:54:25 -0800423 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
424 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
425 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000426
Yanbiao Licf0db022016-01-29 00:54:25 -0800427 advanceClocks(time::milliseconds(55));
428 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
429 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
430 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000431
Yanbiao Licf0db022016-01-29 00:54:25 -0800432 m_commands.clear();
433 paramsRegister.setExpirationPeriod(time::milliseconds(100));
434 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000435
Yanbiao Licf0db022016-01-29 00:54:25 -0800436 advanceClocks(time::milliseconds(55));
437 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
438 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000439}
440
Yanbiao Licf0db022016-01-29 00:54:25 -0800441BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
442
443// @todo Remove when ndn::nfd::RibEntry implements operator!=
444class RibEntry : public ndn::nfd::RibEntry
Junxiao Shi0de23a22015-12-03 20:07:02 +0000445{
Yanbiao Licf0db022016-01-29 00:54:25 -0800446public:
447 RibEntry() = default;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000448
Yanbiao Licf0db022016-01-29 00:54:25 -0800449 RibEntry(const ndn::nfd::RibEntry& entry)
450 : ndn::nfd::RibEntry(entry)
451 {
452 }
453};
Junxiao Shi0de23a22015-12-03 20:07:02 +0000454
Yanbiao Licf0db022016-01-29 00:54:25 -0800455bool
456operator!=(const RibEntry& left, const RibEntry& right)
457{
458 if (left.getName() != right.getName()) {
459 return true;
460 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000461
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 auto leftRoutes = left.getRoutes();
463 auto rightRoutes = right.getRoutes();
464 if (leftRoutes.size() != rightRoutes.size()) {
465 return true;
466 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000467
Yanbiao Licf0db022016-01-29 00:54:25 -0800468 for (auto&& route : leftRoutes) {
469 auto hitEntry =
470 std::find_if(rightRoutes.begin(), rightRoutes.end(), [&] (const ndn::nfd::Route& record) {
471 return route.getFaceId() == record.getFaceId() &&
472 route.getCost() == record.getCost() &&
473 route.getOrigin() == record.getOrigin() &&
474 route.getFlags() == record.getFlags() &&
475 route.getExpirationPeriod() == record.getExpirationPeriod();
476 });
Junxiao Shi0de23a22015-12-03 20:07:02 +0000477
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 if (hitEntry == rightRoutes.end()) {
479 return true;
480 }
481 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000482
Yanbiao Licf0db022016-01-29 00:54:25 -0800483 return false;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000484}
Vince Lehman76c751c2014-11-18 17:36:38 -0600485
Yanbiao Licf0db022016-01-29 00:54:25 -0800486BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500487{
Yanbiao Licf0db022016-01-29 00:54:25 -0800488 uint64_t faceId = 0;
489 auto generateRoute = [&faceId] () -> Route {
490 Route route;
491 route.faceId = ++faceId;
492 route.cost = ndn::random::generateWord64();
493 route.expires = time::steady_clock::TimePoint::max();
494 return route;
495 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500496
Yanbiao Licf0db022016-01-29 00:54:25 -0800497 const size_t nEntries = 108;
498 std::set<Name> actualPrefixes;
499 for (size_t i = 0; i < nEntries; ++i) {
500 Name prefix = Name("/test-dataset").appendNumber(i);
501 actualPrefixes.insert(prefix);
502 m_rib.insert(prefix, generateRoute());
503 if (i & 0x1) {
504 m_rib.insert(prefix, generateRoute());
505 m_rib.insert(prefix, generateRoute());
506 }
507 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500508
Yanbiao Licf0db022016-01-29 00:54:25 -0800509 receiveInterest(makeInterest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500510
Yanbiao Licf0db022016-01-29 00:54:25 -0800511 Block content;
512 BOOST_CHECK_NO_THROW(content = concatenateResponses());
513 BOOST_CHECK_NO_THROW(content.parse());
514 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
515
516 std::vector<RibEntry> receivedRecords, expectedRecords;
517 for (size_t idx = 0; idx < nEntries; ++idx) {
518 BOOST_TEST_MESSAGE("processing element: " << idx);
519
520 RibEntry decodedEntry;
521 BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
522 receivedRecords.push_back(decodedEntry);
523
524 actualPrefixes.erase(decodedEntry.getName());
525
526 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
527 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
528
529 auto matchedEntry = matchedEntryIt->second;
530 BOOST_REQUIRE(matchedEntry != nullptr);
531
532 RibEntry record;
533 record.setName(matchedEntry->getName());
534 const auto& routes = matchedEntry->getRoutes();
535 for (auto&& route : routes) {
536 ndn::nfd::Route routeRecord;
537 routeRecord.setFaceId(route.faceId);
538 routeRecord.setOrigin(route.origin);
539 routeRecord.setFlags(route.flags);
540 routeRecord.setCost(route.cost);
541 record.addRoute(routeRecord);
542 }
543 expectedRecords.push_back(record);
544 }
545
546 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
547
548 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
549 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500550}
551
Yanbiao Licf0db022016-01-29 00:54:25 -0800552BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
553
554BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700555{
Yanbiao Licf0db022016-01-29 00:54:25 -0800556 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600557
Yanbiao Licf0db022016-01-29 00:54:25 -0800558 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
559 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
560 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
561 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700562}
563
Yanbiao Licf0db022016-01-29 00:54:25 -0800564BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500565{
Yanbiao Licf0db022016-01-29 00:54:25 -0800566 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
567 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
568 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
569 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
570 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
571 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500572
Vince Lehman26b215c2014-08-17 15:00:41 -0500573 ndn::nfd::FaceStatus status;
574 status.setFaceId(1);
575
Yanbiao Licf0db022016-01-29 00:54:25 -0800576 auto data = makeData("/localhost/nfd/faces/list");
Vince Lehman26b215c2014-08-17 15:00:41 -0500577 data->setContent(status.wireEncode());
578
Yanbiao Licf0db022016-01-29 00:54:25 -0800579 auto buffer = make_shared<ndn::OBufferStream>();
Vince Lehman26b215c2014-08-17 15:00:41 -0500580 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
581 data->getContent().value_size());
582
Yanbiao Licf0db022016-01-29 00:54:25 -0800583 m_manager.removeInvalidFaces(buffer);
584 advanceClocks(time::milliseconds(100));
585 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500586
Yanbiao Licf0db022016-01-29 00:54:25 -0800587 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
588 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
589 BOOST_CHECK(it2 == m_rib.end());
590 BOOST_REQUIRE(it1 != m_rib.end());
591 BOOST_CHECK(it1->second->hasFaceId(1));
592 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500593}
594
Yanbiao Licf0db022016-01-29 00:54:25 -0800595BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600596{
Yanbiao Licf0db022016-01-29 00:54:25 -0800597 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
598 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
599 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
600 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
601 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600602
Yanbiao Licf0db022016-01-29 00:54:25 -0800603 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
604 ndn::nfd::FaceEventNotification notification;
605 notification.setKind(eventKind).setFaceId(faceId);
606 return notification;
607 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600608
Yanbiao Licf0db022016-01-29 00:54:25 -0800609 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
610 advanceClocks(time::milliseconds(100));
611 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600612
Yanbiao Licf0db022016-01-29 00:54:25 -0800613 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
614 advanceClocks(time::milliseconds(100));
615 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600616}
617
Yanbiao Licf0db022016-01-29 00:54:25 -0800618BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Vince Lehman76c751c2014-11-18 17:36:38 -0600619
Yanbiao Licf0db022016-01-29 00:54:25 -0800620BOOST_AUTO_TEST_SUITE_END() // TestRibManager
621BOOST_AUTO_TEST_SUITE_END() // Rib
Vince Lehman72446ec2014-07-09 10:50:02 -0500622
623} // namespace tests
624} // namespace rib
625} // namespace nfd