blob: f3df261d13c8c6eb4d9926a3caa660ec662cb621 [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
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/rib-entry.hpp>
31#include <ndn-cxx/mgmt/nfd/face-status.hpp>
32#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Yanbiao Licf0db022016-01-29 00:54:25 -080033#include <ndn-cxx/util/random.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050034
Vince Lehman72446ec2014-07-09 10:50:02 -050035namespace nfd {
36namespace rib {
37namespace tests {
38
Junxiao Shi0de23a22015-12-03 20:07:02 +000039using namespace nfd::tests;
40
Yanbiao Licf0db022016-01-29 00:54:25 -080041struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050042{
Yanbiao Licf0db022016-01-29 00:54:25 -080043 bool isLocalhostConfigured;
44 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050045};
46
Yanbiao Licf0db022016-01-29 00:54:25 -080047class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050048{
49public:
Yanbiao Licf0db022016-01-29 00:54:25 -080050 explicit
51 RibManagerFixture(const ConfigurationStatus& status,
52 bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000053 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080054 , m_status(status)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000055 , m_manager(m_dispatcher, m_face, m_keyChain)
Yanbiao Licf0db022016-01-29 00:54:25 -080056 , m_rib(m_manager.m_rib)
Vince Lehman72446ec2014-07-09 10:50:02 -050057 {
Yanbiao Licf0db022016-01-29 00:54:25 -080058 m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
Vince Lehman72446ec2014-07-09 10:50:02 -050059
Yanbiao Licf0db022016-01-29 00:54:25 -080060 const std::string prefix = "rib\n{\n";
61 const std::string suffix = "}";
62 const std::string localhostSection = " localhost_security\n"
Vince Lehman72446ec2014-07-09 10:50:02 -050063 " {\n"
64 " trust-anchor\n"
65 " {\n"
66 " type any\n"
67 " }\n"
Yanbiao Licf0db022016-01-29 00:54:25 -080068 " }\n";
69 const std::string localhopSection = " localhop_security\n"
70 " {\n"
71 " trust-anchor\n"
72 " {\n"
73 " type any\n"
74 " }\n"
75 " }\n";
Vince Lehman72446ec2014-07-09 10:50:02 -050076
Yanbiao Licf0db022016-01-29 00:54:25 -080077 std::string ribSection = "";
78 if (m_status.isLocalhostConfigured) {
79 ribSection += localhostSection;
80 }
81 if (m_status.isLocalhopConfigured) {
82 ribSection += localhopSection;
83 }
84 const std::string CONFIG_STR = prefix + ribSection + suffix;
85
86 ConfigFile config;
87 m_manager.setConfigFile(config);
88 config.parse(CONFIG_STR, true, "test-rib");
89
90 registerWithNfd();
91
92 if (shouldClearRib) {
93 clearRib();
94 }
95 }
96
97private:
98 void
99 registerWithNfd()
100 {
101 m_manager.registerWithNfd();
102 advanceClocks(time::milliseconds(1));
103
104 auto replyFibAddCommand = [this] (const Interest& interest) {
105 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000106 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800107 params.setFaceId(1).setCost(0);
108 nfd::ControlResponse resp;
109
110 resp.setCode(200).setBody(params.wireEncode());
111 shared_ptr<Data> data = make_shared<Data>(interest.getName());
112 data->setContent(resp.wireEncode());
113
114 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
115
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000116 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800117 };
118
119 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000120 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800121 if (commandPrefix.isPrefixOf(command.getName())) {
122 replyFibAddCommand(command);
123 advanceClocks(time::milliseconds(1));
124 }
125 }
126
127 // clear commands and responses
128 m_responses.clear();
129 m_commands.clear();
130 }
131
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000132 void
133 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800134 {
135 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000136 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800137 }
138 }
139
140public:
141 ControlParameters
142 makeRegisterParameters(const Name& name, uint64_t id = 0,
143 const time::milliseconds expiry = time::milliseconds::max())
144 {
145 return ControlParameters()
146 .setName(name)
147 .setFaceId(id)
148 .setOrigin(128)
149 .setCost(10)
150 .setFlags(0)
151 .setExpirationPeriod(expiry);
152 }
153
154 ControlParameters
155 makeUnregisterParameters(const Name& name, uint64_t id = 0)
156 {
157 return ControlParameters()
158 .setName(name)
159 .setFaceId(id)
160 .setOrigin(128);
161 }
162
163 void
164 onSendBatchFromQueue(const RibUpdateBatch& batch)
165 {
166 BOOST_ASSERT(batch.begin() != batch.end());
167 RibUpdate update = *(batch.begin());
168
169 // Simulate a successful response from NFD
170 FibUpdater& updater = m_manager.m_fibUpdater;
171 m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
172 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
173 }
174
175public:
176 enum class CheckCommandResult {
177 OK,
178 OUT_OF_BOUNDARY,
179 WRONG_FORMAT,
180 WRONG_VERB,
181 WRONG_PARAMS_FORMAT,
182 WRONG_PARAMS_NAME,
183 WRONG_PARAMS_FACE
184 };
185
186 CheckCommandResult
187 checkCommand(size_t idx, const char* verbStr, ControlParameters expectedParams)
188 {
189 if (idx > m_commands.size()) {
190 return CheckCommandResult::OUT_OF_BOUNDARY;
191 }
192 const auto& name = m_commands[idx].getName();
193
194 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
195 return CheckCommandResult::WRONG_FORMAT;
196 }
197 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
198
199 Name::Component expectedVerb(verbStr);
200 if (verb != expectedVerb) {
201 return CheckCommandResult::WRONG_VERB;
202 }
203
204 ControlParameters parameters;
205 try {
206 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
207 parameters.wireDecode(rawParameters);
208 }
209 catch (...) {
210 return CheckCommandResult::WRONG_PARAMS_FORMAT;
211 }
212
213 if (parameters.getName() != expectedParams.getName()) {
214 return CheckCommandResult::WRONG_PARAMS_NAME;
215 }
216
217 if (parameters.getFaceId() != expectedParams.getFaceId()) {
218 return CheckCommandResult::WRONG_PARAMS_FACE;
219 }
220
221 return CheckCommandResult::OK;
222 }
223
224public:
225 std::vector<Interest>& m_commands;
226 ConfigurationStatus m_status;
227 static const Name FIB_COMMAND_PREFIX;
228
229protected:
230 RibManager m_manager;
231 Rib& m_rib;
232};
233
234const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
235
236std::ostream&
237operator<<(std::ostream& os, const RibManagerFixture::CheckCommandResult& result)
238{
239 switch (result) {
240 case RibManagerFixture::CheckCommandResult::OK:
241 os << "OK";
242 break;
243 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
244 os << "OUT_OF_BOUNDARY";
245 break;
246 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
247 os << "WRONG_FORMAT";
248 break;
249 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
250 os << "WRONG_VERB";
251 break;
252 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
253 os << "WRONG_COST";
254 break;
255 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
256 os << "WRONG_PARAMS_NAME";
257 break;
258 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
259 os << "WRONG_PARAMS_FACE";
260 break;
261 default:
262 break;
263 };
264
265 return os;
266}
267
268BOOST_AUTO_TEST_SUITE(Rib)
269BOOST_AUTO_TEST_SUITE(TestRibManager)
270
271class AddTopPrefixFixture : public RibManagerFixture
272{
273public:
274 AddTopPrefixFixture()
275 : RibManagerFixture({true, true}, false)
276 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500277 }
278};
279
Yanbiao Licf0db022016-01-29 00:54:25 -0800280BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700281{
Yanbiao Licf0db022016-01-29 00:54:25 -0800282 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600283
Yanbiao Licf0db022016-01-29 00:54:25 -0800284 std::vector<Name> ribEntryNames;
285 for (auto&& entry : m_rib) {
286 ribEntryNames.push_back(entry.first);
287 }
288 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
289 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700290}
291
Yanbiao Licf0db022016-01-29 00:54:25 -0800292class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500293{
Yanbiao Licf0db022016-01-29 00:54:25 -0800294public:
295 UnauthorizedRibManagerFixture()
296 : RibManagerFixture({false, false}, true)
297 {
298 }
299};
Vince Lehman72446ec2014-07-09 10:50:02 -0500300
Yanbiao Licf0db022016-01-29 00:54:25 -0800301class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
302{
303public:
304 LocalhostAuthorizedRibManagerFixture()
305 : RibManagerFixture({true, false}, true)
306 {
307 }
308};
Vince Lehman72446ec2014-07-09 10:50:02 -0500309
Yanbiao Licf0db022016-01-29 00:54:25 -0800310class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
311{
312public:
313 LocalhopAuthorizedRibManagerFixture()
314 : RibManagerFixture({false, true}, true)
315 {
316 }
317};
318
319class AuthorizedRibManagerFixture : public RibManagerFixture
320{
321public:
322 AuthorizedRibManagerFixture()
323 : RibManagerFixture({true, true}, true)
324 {
325 }
326};
327
328typedef boost::mpl::vector<
329 UnauthorizedRibManagerFixture,
330 LocalhostAuthorizedRibManagerFixture,
331 LocalhopAuthorizedRibManagerFixture,
332 AuthorizedRibManagerFixture
333> AllFixtures;
334
335BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
336{
337 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
338 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
339 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
340 auto successResp = this->makeResponse(200, "Success", parameters);
341 auto failureResp = ControlResponse(403, "authorization rejected");
342
343 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
344 this->receiveInterest(commandHost);
345 this->receiveInterest(commandHop);
346
347 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
348 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
349 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
350
351 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
352 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse),
353 ManagerCommonFixture::CheckResponseResult::OK);
354 if (nExpectedResponses == 2) {
355 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse),
356 ManagerCommonFixture::CheckResponseResult::OK);
357 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500358}
359
Yanbiao Licf0db022016-01-29 00:54:25 -0800360BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
361
362BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500363{
Yanbiao Licf0db022016-01-29 00:54:25 -0800364 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
365 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500366
Yanbiao Licf0db022016-01-29 00:54:25 -0800367 auto setInFaceId = [] (shared_ptr<Interest> commandInterest) {
368 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234));
369 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500370
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
372 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Yanbiao Licf0db022016-01-29 00:54:25 -0800374 receiveInterest(commandRegister);
375 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500376
Yanbiao Licf0db022016-01-29 00:54:25 -0800377 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
378 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
379 CheckResponseResult::OK);
380 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
381 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500382
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
384 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
385 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500386}
387
Yanbiao Licf0db022016-01-29 00:54:25 -0800388BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500389{
Yanbiao Licf0db022016-01-29 00:54:25 -0800390 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
391 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
392 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
393 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500394
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 const uint64_t inFaceId = 9527;
396 auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) {
397 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
398 };
399 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
400 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500401
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 receiveInterest(commandRegister);
403 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 paramsRegister.setFaceId(inFaceId);
406 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500407
Yanbiao Licf0db022016-01-29 00:54:25 -0800408 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
409 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
410 CheckResponseResult::OK);
411 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
412 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500413
Yanbiao Licf0db022016-01-29 00:54:25 -0800414 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
415 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
416 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500417}
418
Yanbiao Licf0db022016-01-29 00:54:25 -0800419BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000420{
Yanbiao Licf0db022016-01-29 00:54:25 -0800421 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
422 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
423 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000424
Yanbiao Licf0db022016-01-29 00:54:25 -0800425 advanceClocks(time::milliseconds(55));
426 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
427 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
428 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000429
Yanbiao Licf0db022016-01-29 00:54:25 -0800430 m_commands.clear();
431 paramsRegister.setExpirationPeriod(time::milliseconds(100));
432 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000433
Yanbiao Licf0db022016-01-29 00:54:25 -0800434 advanceClocks(time::milliseconds(55));
435 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
436 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000437}
438
Yanbiao Licf0db022016-01-29 00:54:25 -0800439BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
440
441// @todo Remove when ndn::nfd::RibEntry implements operator!=
442class RibEntry : public ndn::nfd::RibEntry
Junxiao Shi0de23a22015-12-03 20:07:02 +0000443{
Yanbiao Licf0db022016-01-29 00:54:25 -0800444public:
445 RibEntry() = default;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000446
Yanbiao Licf0db022016-01-29 00:54:25 -0800447 RibEntry(const ndn::nfd::RibEntry& entry)
448 : ndn::nfd::RibEntry(entry)
449 {
450 }
451};
Junxiao Shi0de23a22015-12-03 20:07:02 +0000452
Yanbiao Licf0db022016-01-29 00:54:25 -0800453bool
454operator!=(const RibEntry& left, const RibEntry& right)
455{
456 if (left.getName() != right.getName()) {
457 return true;
458 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000459
Yanbiao Licf0db022016-01-29 00:54:25 -0800460 auto leftRoutes = left.getRoutes();
461 auto rightRoutes = right.getRoutes();
462 if (leftRoutes.size() != rightRoutes.size()) {
463 return true;
464 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000465
Yanbiao Licf0db022016-01-29 00:54:25 -0800466 for (auto&& route : leftRoutes) {
467 auto hitEntry =
468 std::find_if(rightRoutes.begin(), rightRoutes.end(), [&] (const ndn::nfd::Route& record) {
469 return route.getFaceId() == record.getFaceId() &&
470 route.getCost() == record.getCost() &&
471 route.getOrigin() == record.getOrigin() &&
472 route.getFlags() == record.getFlags() &&
473 route.getExpirationPeriod() == record.getExpirationPeriod();
474 });
Junxiao Shi0de23a22015-12-03 20:07:02 +0000475
Yanbiao Licf0db022016-01-29 00:54:25 -0800476 if (hitEntry == rightRoutes.end()) {
477 return true;
478 }
479 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000480
Yanbiao Licf0db022016-01-29 00:54:25 -0800481 return false;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000482}
Vince Lehman76c751c2014-11-18 17:36:38 -0600483
Yanbiao Licf0db022016-01-29 00:54:25 -0800484BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500485{
Yanbiao Licf0db022016-01-29 00:54:25 -0800486 uint64_t faceId = 0;
487 auto generateRoute = [&faceId] () -> Route {
488 Route route;
489 route.faceId = ++faceId;
490 route.cost = ndn::random::generateWord64();
491 route.expires = time::steady_clock::TimePoint::max();
492 return route;
493 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500494
Yanbiao Licf0db022016-01-29 00:54:25 -0800495 const size_t nEntries = 108;
496 std::set<Name> actualPrefixes;
497 for (size_t i = 0; i < nEntries; ++i) {
498 Name prefix = Name("/test-dataset").appendNumber(i);
499 actualPrefixes.insert(prefix);
500 m_rib.insert(prefix, generateRoute());
501 if (i & 0x1) {
502 m_rib.insert(prefix, generateRoute());
503 m_rib.insert(prefix, generateRoute());
504 }
505 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500506
Yanbiao Licf0db022016-01-29 00:54:25 -0800507 receiveInterest(makeInterest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500508
Yanbiao Licf0db022016-01-29 00:54:25 -0800509 Block content;
510 BOOST_CHECK_NO_THROW(content = concatenateResponses());
511 BOOST_CHECK_NO_THROW(content.parse());
512 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
513
514 std::vector<RibEntry> receivedRecords, expectedRecords;
515 for (size_t idx = 0; idx < nEntries; ++idx) {
516 BOOST_TEST_MESSAGE("processing element: " << idx);
517
518 RibEntry decodedEntry;
519 BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
520 receivedRecords.push_back(decodedEntry);
521
522 actualPrefixes.erase(decodedEntry.getName());
523
524 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
525 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
526
527 auto matchedEntry = matchedEntryIt->second;
528 BOOST_REQUIRE(matchedEntry != nullptr);
529
530 RibEntry record;
531 record.setName(matchedEntry->getName());
532 const auto& routes = matchedEntry->getRoutes();
533 for (auto&& route : routes) {
534 ndn::nfd::Route routeRecord;
535 routeRecord.setFaceId(route.faceId);
536 routeRecord.setOrigin(route.origin);
537 routeRecord.setFlags(route.flags);
538 routeRecord.setCost(route.cost);
539 record.addRoute(routeRecord);
540 }
541 expectedRecords.push_back(record);
542 }
543
544 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
545
546 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
547 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500548}
549
Yanbiao Licf0db022016-01-29 00:54:25 -0800550BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
551
552BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700553{
Yanbiao Licf0db022016-01-29 00:54:25 -0800554 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600555
Yanbiao Licf0db022016-01-29 00:54:25 -0800556 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
557 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
558 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
559 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700560}
561
Yanbiao Licf0db022016-01-29 00:54:25 -0800562BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500563{
Yanbiao Licf0db022016-01-29 00:54:25 -0800564 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
565 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
566 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
567 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
568 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
569 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500570
Vince Lehman26b215c2014-08-17 15:00:41 -0500571 ndn::nfd::FaceStatus status;
572 status.setFaceId(1);
573
Yanbiao Licf0db022016-01-29 00:54:25 -0800574 auto data = makeData("/localhost/nfd/faces/list");
Vince Lehman26b215c2014-08-17 15:00:41 -0500575 data->setContent(status.wireEncode());
576
Yanbiao Licf0db022016-01-29 00:54:25 -0800577 auto buffer = make_shared<ndn::OBufferStream>();
Vince Lehman26b215c2014-08-17 15:00:41 -0500578 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
579 data->getContent().value_size());
580
Yanbiao Licf0db022016-01-29 00:54:25 -0800581 m_manager.removeInvalidFaces(buffer);
582 advanceClocks(time::milliseconds(100));
583 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500584
Yanbiao Licf0db022016-01-29 00:54:25 -0800585 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
586 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
587 BOOST_CHECK(it2 == m_rib.end());
588 BOOST_REQUIRE(it1 != m_rib.end());
589 BOOST_CHECK(it1->second->hasFaceId(1));
590 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500591}
592
Yanbiao Licf0db022016-01-29 00:54:25 -0800593BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600594{
Yanbiao Licf0db022016-01-29 00:54:25 -0800595 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
596 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
597 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
598 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
599 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600600
Yanbiao Licf0db022016-01-29 00:54:25 -0800601 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
602 ndn::nfd::FaceEventNotification notification;
603 notification.setKind(eventKind).setFaceId(faceId);
604 return notification;
605 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600606
Yanbiao Licf0db022016-01-29 00:54:25 -0800607 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
608 advanceClocks(time::milliseconds(100));
609 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600610
Yanbiao Licf0db022016-01-29 00:54:25 -0800611 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
612 advanceClocks(time::milliseconds(100));
613 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600614}
615
Yanbiao Licf0db022016-01-29 00:54:25 -0800616BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Vince Lehman76c751c2014-11-18 17:36:38 -0600617
Yanbiao Licf0db022016-01-29 00:54:25 -0800618BOOST_AUTO_TEST_SUITE_END() // TestRibManager
619BOOST_AUTO_TEST_SUITE_END() // Rib
Vince Lehman72446ec2014-07-09 10:50:02 -0500620
621} // namespace tests
622} // namespace rib
623} // namespace nfd