blob: cfe0b3fd18db60d8b279ba1f915530358113f8e8 [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>
Yanbiao Licf0db022016-01-29 00:54:25 -080030#include <ndn-cxx/management/nfd-rib-entry.hpp>
31#include <ndn-cxx/management/nfd-face-status.hpp>
32#include <ndn-cxx/management/nfd-face-event-notification.hpp>
33#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");
120 for (auto&& command : m_commands) {
121 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
132 void clearRib()
133 {
134 while (!m_rib.empty()) {
135 auto& name = m_rib.begin()->first;
136 auto& routes = m_rib.begin()->second->getRoutes();
137 while (!routes.empty()) {
138 m_rib.erase(name, *routes.begin());
139 }
140 }
141 }
142
143public:
144 ControlParameters
145 makeRegisterParameters(const Name& name, uint64_t id = 0,
146 const time::milliseconds expiry = time::milliseconds::max())
147 {
148 return ControlParameters()
149 .setName(name)
150 .setFaceId(id)
151 .setOrigin(128)
152 .setCost(10)
153 .setFlags(0)
154 .setExpirationPeriod(expiry);
155 }
156
157 ControlParameters
158 makeUnregisterParameters(const Name& name, uint64_t id = 0)
159 {
160 return ControlParameters()
161 .setName(name)
162 .setFaceId(id)
163 .setOrigin(128);
164 }
165
166 void
167 onSendBatchFromQueue(const RibUpdateBatch& batch)
168 {
169 BOOST_ASSERT(batch.begin() != batch.end());
170 RibUpdate update = *(batch.begin());
171
172 // Simulate a successful response from NFD
173 FibUpdater& updater = m_manager.m_fibUpdater;
174 m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
175 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
176 }
177
178public:
179 enum class CheckCommandResult {
180 OK,
181 OUT_OF_BOUNDARY,
182 WRONG_FORMAT,
183 WRONG_VERB,
184 WRONG_PARAMS_FORMAT,
185 WRONG_PARAMS_NAME,
186 WRONG_PARAMS_FACE
187 };
188
189 CheckCommandResult
190 checkCommand(size_t idx, const char* verbStr, ControlParameters expectedParams)
191 {
192 if (idx > m_commands.size()) {
193 return CheckCommandResult::OUT_OF_BOUNDARY;
194 }
195 const auto& name = m_commands[idx].getName();
196
197 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
198 return CheckCommandResult::WRONG_FORMAT;
199 }
200 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
201
202 Name::Component expectedVerb(verbStr);
203 if (verb != expectedVerb) {
204 return CheckCommandResult::WRONG_VERB;
205 }
206
207 ControlParameters parameters;
208 try {
209 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
210 parameters.wireDecode(rawParameters);
211 }
212 catch (...) {
213 return CheckCommandResult::WRONG_PARAMS_FORMAT;
214 }
215
216 if (parameters.getName() != expectedParams.getName()) {
217 return CheckCommandResult::WRONG_PARAMS_NAME;
218 }
219
220 if (parameters.getFaceId() != expectedParams.getFaceId()) {
221 return CheckCommandResult::WRONG_PARAMS_FACE;
222 }
223
224 return CheckCommandResult::OK;
225 }
226
227public:
228 std::vector<Interest>& m_commands;
229 ConfigurationStatus m_status;
230 static const Name FIB_COMMAND_PREFIX;
231
232protected:
233 RibManager m_manager;
234 Rib& m_rib;
235};
236
237const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
238
239std::ostream&
240operator<<(std::ostream& os, const RibManagerFixture::CheckCommandResult& result)
241{
242 switch (result) {
243 case RibManagerFixture::CheckCommandResult::OK:
244 os << "OK";
245 break;
246 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
247 os << "OUT_OF_BOUNDARY";
248 break;
249 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
250 os << "WRONG_FORMAT";
251 break;
252 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
253 os << "WRONG_VERB";
254 break;
255 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
256 os << "WRONG_COST";
257 break;
258 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
259 os << "WRONG_PARAMS_NAME";
260 break;
261 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
262 os << "WRONG_PARAMS_FACE";
263 break;
264 default:
265 break;
266 };
267
268 return os;
269}
270
271BOOST_AUTO_TEST_SUITE(Rib)
272BOOST_AUTO_TEST_SUITE(TestRibManager)
273
274class AddTopPrefixFixture : public RibManagerFixture
275{
276public:
277 AddTopPrefixFixture()
278 : RibManagerFixture({true, true}, false)
279 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500280 }
281};
282
Yanbiao Licf0db022016-01-29 00:54:25 -0800283BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700284{
Yanbiao Licf0db022016-01-29 00:54:25 -0800285 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600286
Yanbiao Licf0db022016-01-29 00:54:25 -0800287 std::vector<Name> ribEntryNames;
288 for (auto&& entry : m_rib) {
289 ribEntryNames.push_back(entry.first);
290 }
291 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
292 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700293}
294
Yanbiao Licf0db022016-01-29 00:54:25 -0800295class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500296{
Yanbiao Licf0db022016-01-29 00:54:25 -0800297public:
298 UnauthorizedRibManagerFixture()
299 : RibManagerFixture({false, false}, true)
300 {
301 }
302};
Vince Lehman72446ec2014-07-09 10:50:02 -0500303
Yanbiao Licf0db022016-01-29 00:54:25 -0800304class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
305{
306public:
307 LocalhostAuthorizedRibManagerFixture()
308 : RibManagerFixture({true, false}, true)
309 {
310 }
311};
Vince Lehman72446ec2014-07-09 10:50:02 -0500312
Yanbiao Licf0db022016-01-29 00:54:25 -0800313class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
314{
315public:
316 LocalhopAuthorizedRibManagerFixture()
317 : RibManagerFixture({false, true}, true)
318 {
319 }
320};
321
322class AuthorizedRibManagerFixture : public RibManagerFixture
323{
324public:
325 AuthorizedRibManagerFixture()
326 : RibManagerFixture({true, true}, true)
327 {
328 }
329};
330
331typedef boost::mpl::vector<
332 UnauthorizedRibManagerFixture,
333 LocalhostAuthorizedRibManagerFixture,
334 LocalhopAuthorizedRibManagerFixture,
335 AuthorizedRibManagerFixture
336> AllFixtures;
337
338BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
339{
340 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
341 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
342 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
343 auto successResp = this->makeResponse(200, "Success", parameters);
344 auto failureResp = ControlResponse(403, "authorization rejected");
345
346 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
347 this->receiveInterest(commandHost);
348 this->receiveInterest(commandHop);
349
350 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
351 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
352 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
353
354 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
355 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost->getName(), expectedLocalhostResponse),
356 ManagerCommonFixture::CheckResponseResult::OK);
357 if (nExpectedResponses == 2) {
358 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop->getName(), expectedLocalhopResponse),
359 ManagerCommonFixture::CheckResponseResult::OK);
360 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500361}
362
Yanbiao Licf0db022016-01-29 00:54:25 -0800363BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
364
365BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500366{
Yanbiao Licf0db022016-01-29 00:54:25 -0800367 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
368 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500369
Yanbiao Licf0db022016-01-29 00:54:25 -0800370 auto setInFaceId = [] (shared_ptr<Interest> commandInterest) {
371 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(1234));
372 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Yanbiao Licf0db022016-01-29 00:54:25 -0800374 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
375 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500376
Yanbiao Licf0db022016-01-29 00:54:25 -0800377 receiveInterest(commandRegister);
378 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500379
Yanbiao Licf0db022016-01-29 00:54:25 -0800380 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
381 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
382 CheckResponseResult::OK);
383 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
384 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500385
Yanbiao Licf0db022016-01-29 00:54:25 -0800386 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
387 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
388 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500389}
390
Yanbiao Licf0db022016-01-29 00:54:25 -0800391BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500392{
Yanbiao Licf0db022016-01-29 00:54:25 -0800393 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
394 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
395 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
396 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500397
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 const uint64_t inFaceId = 9527;
399 auto setInFaceId = [&inFaceId] (shared_ptr<Interest> commandInterest) {
400 commandInterest->setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
401 };
402 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister, setInFaceId);
403 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister, setInFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 receiveInterest(commandRegister);
406 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500407
Yanbiao Licf0db022016-01-29 00:54:25 -0800408 paramsRegister.setFaceId(inFaceId);
409 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500410
Yanbiao Licf0db022016-01-29 00:54:25 -0800411 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
412 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister->getName(), makeResponse(200, "Success", paramsRegister)),
413 CheckResponseResult::OK);
414 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister->getName(), makeResponse(200, "Success", paramsUnregister)),
415 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500416
Yanbiao Licf0db022016-01-29 00:54:25 -0800417 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
418 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
419 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500420}
421
Yanbiao Licf0db022016-01-29 00:54:25 -0800422BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000423{
Yanbiao Licf0db022016-01-29 00:54:25 -0800424 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
425 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
426 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000427
Yanbiao Licf0db022016-01-29 00:54:25 -0800428 advanceClocks(time::milliseconds(55));
429 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
430 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
431 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000432
Yanbiao Licf0db022016-01-29 00:54:25 -0800433 m_commands.clear();
434 paramsRegister.setExpirationPeriod(time::milliseconds(100));
435 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000436
Yanbiao Licf0db022016-01-29 00:54:25 -0800437 advanceClocks(time::milliseconds(55));
438 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
439 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000440}
441
Yanbiao Licf0db022016-01-29 00:54:25 -0800442BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
443
444// @todo Remove when ndn::nfd::RibEntry implements operator!=
445class RibEntry : public ndn::nfd::RibEntry
Junxiao Shi0de23a22015-12-03 20:07:02 +0000446{
Yanbiao Licf0db022016-01-29 00:54:25 -0800447public:
448 RibEntry() = default;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000449
Yanbiao Licf0db022016-01-29 00:54:25 -0800450 RibEntry(const ndn::nfd::RibEntry& entry)
451 : ndn::nfd::RibEntry(entry)
452 {
453 }
454};
Junxiao Shi0de23a22015-12-03 20:07:02 +0000455
Yanbiao Licf0db022016-01-29 00:54:25 -0800456bool
457operator!=(const RibEntry& left, const RibEntry& right)
458{
459 if (left.getName() != right.getName()) {
460 return true;
461 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000462
Yanbiao Licf0db022016-01-29 00:54:25 -0800463 auto leftRoutes = left.getRoutes();
464 auto rightRoutes = right.getRoutes();
465 if (leftRoutes.size() != rightRoutes.size()) {
466 return true;
467 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000468
Yanbiao Licf0db022016-01-29 00:54:25 -0800469 for (auto&& route : leftRoutes) {
470 auto hitEntry =
471 std::find_if(rightRoutes.begin(), rightRoutes.end(), [&] (const ndn::nfd::Route& record) {
472 return route.getFaceId() == record.getFaceId() &&
473 route.getCost() == record.getCost() &&
474 route.getOrigin() == record.getOrigin() &&
475 route.getFlags() == record.getFlags() &&
476 route.getExpirationPeriod() == record.getExpirationPeriod();
477 });
Junxiao Shi0de23a22015-12-03 20:07:02 +0000478
Yanbiao Licf0db022016-01-29 00:54:25 -0800479 if (hitEntry == rightRoutes.end()) {
480 return true;
481 }
482 }
Junxiao Shi0de23a22015-12-03 20:07:02 +0000483
Yanbiao Licf0db022016-01-29 00:54:25 -0800484 return false;
Junxiao Shi0de23a22015-12-03 20:07:02 +0000485}
Vince Lehman76c751c2014-11-18 17:36:38 -0600486
Yanbiao Licf0db022016-01-29 00:54:25 -0800487BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500488{
Yanbiao Licf0db022016-01-29 00:54:25 -0800489 uint64_t faceId = 0;
490 auto generateRoute = [&faceId] () -> Route {
491 Route route;
492 route.faceId = ++faceId;
493 route.cost = ndn::random::generateWord64();
494 route.expires = time::steady_clock::TimePoint::max();
495 return route;
496 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500497
Yanbiao Licf0db022016-01-29 00:54:25 -0800498 const size_t nEntries = 108;
499 std::set<Name> actualPrefixes;
500 for (size_t i = 0; i < nEntries; ++i) {
501 Name prefix = Name("/test-dataset").appendNumber(i);
502 actualPrefixes.insert(prefix);
503 m_rib.insert(prefix, generateRoute());
504 if (i & 0x1) {
505 m_rib.insert(prefix, generateRoute());
506 m_rib.insert(prefix, generateRoute());
507 }
508 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500509
Yanbiao Licf0db022016-01-29 00:54:25 -0800510 receiveInterest(makeInterest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500511
Yanbiao Licf0db022016-01-29 00:54:25 -0800512 Block content;
513 BOOST_CHECK_NO_THROW(content = concatenateResponses());
514 BOOST_CHECK_NO_THROW(content.parse());
515 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
516
517 std::vector<RibEntry> receivedRecords, expectedRecords;
518 for (size_t idx = 0; idx < nEntries; ++idx) {
519 BOOST_TEST_MESSAGE("processing element: " << idx);
520
521 RibEntry decodedEntry;
522 BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
523 receivedRecords.push_back(decodedEntry);
524
525 actualPrefixes.erase(decodedEntry.getName());
526
527 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
528 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
529
530 auto matchedEntry = matchedEntryIt->second;
531 BOOST_REQUIRE(matchedEntry != nullptr);
532
533 RibEntry record;
534 record.setName(matchedEntry->getName());
535 const auto& routes = matchedEntry->getRoutes();
536 for (auto&& route : routes) {
537 ndn::nfd::Route routeRecord;
538 routeRecord.setFaceId(route.faceId);
539 routeRecord.setOrigin(route.origin);
540 routeRecord.setFlags(route.flags);
541 routeRecord.setCost(route.cost);
542 record.addRoute(routeRecord);
543 }
544 expectedRecords.push_back(record);
545 }
546
547 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
548
549 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
550 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500551}
552
Yanbiao Licf0db022016-01-29 00:54:25 -0800553BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
554
555BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700556{
Yanbiao Licf0db022016-01-29 00:54:25 -0800557 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600558
Yanbiao Licf0db022016-01-29 00:54:25 -0800559 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
560 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
561 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
562 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700563}
564
Yanbiao Licf0db022016-01-29 00:54:25 -0800565BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500566{
Yanbiao Licf0db022016-01-29 00:54:25 -0800567 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
568 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
569 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
570 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
571 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
572 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500573
Vince Lehman26b215c2014-08-17 15:00:41 -0500574 ndn::nfd::FaceStatus status;
575 status.setFaceId(1);
576
Yanbiao Licf0db022016-01-29 00:54:25 -0800577 auto data = makeData("/localhost/nfd/faces/list");
Vince Lehman26b215c2014-08-17 15:00:41 -0500578 data->setContent(status.wireEncode());
579
Yanbiao Licf0db022016-01-29 00:54:25 -0800580 auto buffer = make_shared<ndn::OBufferStream>();
Vince Lehman26b215c2014-08-17 15:00:41 -0500581 buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
582 data->getContent().value_size());
583
Yanbiao Licf0db022016-01-29 00:54:25 -0800584 m_manager.removeInvalidFaces(buffer);
585 advanceClocks(time::milliseconds(100));
586 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500587
Yanbiao Licf0db022016-01-29 00:54:25 -0800588 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
589 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
590 BOOST_CHECK(it2 == m_rib.end());
591 BOOST_REQUIRE(it1 != m_rib.end());
592 BOOST_CHECK(it1->second->hasFaceId(1));
593 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500594}
595
Yanbiao Licf0db022016-01-29 00:54:25 -0800596BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600597{
Yanbiao Licf0db022016-01-29 00:54:25 -0800598 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
599 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
600 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
601 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
602 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600603
Yanbiao Licf0db022016-01-29 00:54:25 -0800604 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
605 ndn::nfd::FaceEventNotification notification;
606 notification.setKind(eventKind).setFaceId(faceId);
607 return notification;
608 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600609
Yanbiao Licf0db022016-01-29 00:54:25 -0800610 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
611 advanceClocks(time::milliseconds(100));
612 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600613
Yanbiao Licf0db022016-01-29 00:54:25 -0800614 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
615 advanceClocks(time::milliseconds(100));
616 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600617}
618
Yanbiao Licf0db022016-01-29 00:54:25 -0800619BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Vince Lehman76c751c2014-11-18 17:36:38 -0600620
Yanbiao Licf0db022016-01-29 00:54:25 -0800621BOOST_AUTO_TEST_SUITE_END() // TestRibManager
622BOOST_AUTO_TEST_SUITE_END() // Rib
Vince Lehman72446ec2014-07-09 10:50:02 -0500623
624} // namespace tests
625} // namespace rib
626} // namespace nfd