blob: e141368dc8695734a15eb7522946fa1693bb9b9b [file] [log] [blame]
Vince Lehman72446ec2014-07-09 10:50:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
3 * Copyright (c) 2014-2018, 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"
Junxiao Shi75306352018-02-01 21:59:44 +000028#include "core/fib-max-depth.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050029
Junxiao Shicbc8e942016-09-06 03:17:45 +000030#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000031#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Davide Pesaventod2ea2352017-03-12 20:38:09 -040032#include <ndn-cxx/mgmt/nfd/face-status.hpp>
33#include <ndn-cxx/mgmt/nfd/rib-entry.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:
Davide Pesavento22db5392017-04-14 00:56:43 -0400141 static ControlParameters
142 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400143 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800144 {
145 return ControlParameters()
146 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400147 .setFaceId(faceId)
148 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800149 .setCost(10)
150 .setFlags(0)
151 .setExpirationPeriod(expiry);
152 }
153
Davide Pesavento22db5392017-04-14 00:56:43 -0400154 static ControlParameters
155 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800156 {
157 return ControlParameters()
158 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400159 .setFaceId(faceId)
160 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800161 }
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
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400184 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800185
186 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400187 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800188 {
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:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400225 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 std::vector<Interest>& m_commands;
227 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800228
229protected:
230 RibManager m_manager;
231 Rib& m_rib;
232};
233
234const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
235
236std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400237operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800238{
239 switch (result) {
240 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400241 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800242 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400243 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800244 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400245 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800246 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400247 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800248 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400249 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400251 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800252 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400253 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 };
255
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400256 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800257}
258
Yanbiao Licf0db022016-01-29 00:54:25 -0800259BOOST_AUTO_TEST_SUITE(TestRibManager)
260
261class AddTopPrefixFixture : public RibManagerFixture
262{
263public:
264 AddTopPrefixFixture()
265 : RibManagerFixture({true, true}, false)
266 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500267 }
268};
269
Yanbiao Licf0db022016-01-29 00:54:25 -0800270BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700271{
Yanbiao Licf0db022016-01-29 00:54:25 -0800272 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600273
Yanbiao Licf0db022016-01-29 00:54:25 -0800274 std::vector<Name> ribEntryNames;
275 for (auto&& entry : m_rib) {
276 ribEntryNames.push_back(entry.first);
277 }
278 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
279 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700280}
281
Yanbiao Licf0db022016-01-29 00:54:25 -0800282class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500283{
Yanbiao Licf0db022016-01-29 00:54:25 -0800284public:
285 UnauthorizedRibManagerFixture()
286 : RibManagerFixture({false, false}, true)
287 {
288 }
289};
Vince Lehman72446ec2014-07-09 10:50:02 -0500290
Yanbiao Licf0db022016-01-29 00:54:25 -0800291class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
292{
293public:
294 LocalhostAuthorizedRibManagerFixture()
295 : RibManagerFixture({true, false}, true)
296 {
297 }
298};
Vince Lehman72446ec2014-07-09 10:50:02 -0500299
Yanbiao Licf0db022016-01-29 00:54:25 -0800300class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
301{
302public:
303 LocalhopAuthorizedRibManagerFixture()
304 : RibManagerFixture({false, true}, true)
305 {
306 }
307};
308
309class AuthorizedRibManagerFixture : public RibManagerFixture
310{
311public:
312 AuthorizedRibManagerFixture()
313 : RibManagerFixture({true, true}, true)
314 {
315 }
316};
317
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400318using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800319 UnauthorizedRibManagerFixture,
320 LocalhostAuthorizedRibManagerFixture,
321 LocalhopAuthorizedRibManagerFixture,
322 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400323>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800324
325BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
326{
327 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
328 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
329 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
330 auto successResp = this->makeResponse(200, "Success", parameters);
331 auto failureResp = ControlResponse(403, "authorization rejected");
332
333 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
334 this->receiveInterest(commandHost);
335 this->receiveInterest(commandHop);
336
337 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
338 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
339 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
340
341 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000342 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800343 ManagerCommonFixture::CheckResponseResult::OK);
344 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000345 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 ManagerCommonFixture::CheckResponseResult::OK);
347 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500348}
349
Yanbiao Licf0db022016-01-29 00:54:25 -0800350BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
351
352BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500353{
Yanbiao Licf0db022016-01-29 00:54:25 -0800354 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
355 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500356
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000357 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
358 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
359 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
360 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500361
Yanbiao Licf0db022016-01-29 00:54:25 -0800362 receiveInterest(commandRegister);
363 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500364
Yanbiao Licf0db022016-01-29 00:54:25 -0800365 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000366 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800367 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000368 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800369 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500370
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
372 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
373 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500374}
375
Yanbiao Licf0db022016-01-29 00:54:25 -0800376BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500377{
Yanbiao Licf0db022016-01-29 00:54:25 -0800378 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
379 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
380 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
381 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500382
Yanbiao Licf0db022016-01-29 00:54:25 -0800383 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000384 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
385 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
386 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
387 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500388
Yanbiao Licf0db022016-01-29 00:54:25 -0800389 receiveInterest(commandRegister);
390 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500391
Yanbiao Licf0db022016-01-29 00:54:25 -0800392 paramsRegister.setFaceId(inFaceId);
393 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500394
Yanbiao Licf0db022016-01-29 00:54:25 -0800395 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000396 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800397 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000398 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800399 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500400
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
402 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
403 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500404}
405
Yanbiao Licf0db022016-01-29 00:54:25 -0800406BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000407{
Yanbiao Licf0db022016-01-29 00:54:25 -0800408 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
409 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
410 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000411
Yanbiao Licf0db022016-01-29 00:54:25 -0800412 advanceClocks(time::milliseconds(55));
413 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
414 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
415 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000416
Yanbiao Licf0db022016-01-29 00:54:25 -0800417 m_commands.clear();
418 paramsRegister.setExpirationPeriod(time::milliseconds(100));
419 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000420
Yanbiao Licf0db022016-01-29 00:54:25 -0800421 advanceClocks(time::milliseconds(55));
422 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
423 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000424}
425
Junxiao Shi75306352018-02-01 21:59:44 +0000426BOOST_AUTO_TEST_CASE(NameTooLong)
427{
428 Name prefix;
429 while (prefix.size() <= FIB_MAX_DEPTH) {
430 prefix.append("A");
431 }
432 auto params = makeRegisterParameters(prefix, 2899);
433 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
434 receiveInterest(command);
435
436 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
437 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
438 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
439 " components")),
440 CheckResponseResult::OK);
441
442 BOOST_CHECK_EQUAL(m_commands.size(), 0);
443}
444
Yanbiao Licf0db022016-01-29 00:54:25 -0800445BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
446
Yanbiao Licf0db022016-01-29 00:54:25 -0800447BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500448{
Yanbiao Licf0db022016-01-29 00:54:25 -0800449 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400450 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800451 Route route;
452 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400453 route.cost = route.faceId * 10;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000454 route.expires = ndn::nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800455 return route;
456 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500457
Yanbiao Licf0db022016-01-29 00:54:25 -0800458 const size_t nEntries = 108;
459 std::set<Name> actualPrefixes;
460 for (size_t i = 0; i < nEntries; ++i) {
461 Name prefix = Name("/test-dataset").appendNumber(i);
462 actualPrefixes.insert(prefix);
463 m_rib.insert(prefix, generateRoute());
464 if (i & 0x1) {
465 m_rib.insert(prefix, generateRoute());
466 m_rib.insert(prefix, generateRoute());
467 }
468 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500469
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000470 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500471
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400472 Block content = concatenateResponses();
473 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800474 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
475
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400476 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800477 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400478 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800479 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800480 actualPrefixes.erase(decodedEntry.getName());
481
482 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
483 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
484
485 auto matchedEntry = matchedEntryIt->second;
486 BOOST_REQUIRE(matchedEntry != nullptr);
487
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400488 expectedRecords.emplace_back();
489 expectedRecords.back().setName(matchedEntry->getName());
490 for (const auto& route : matchedEntry->getRoutes()) {
491 expectedRecords.back().addRoute(ndn::nfd::Route()
492 .setFaceId(route.faceId)
493 .setOrigin(route.origin)
494 .setCost(route.cost)
495 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800496 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800497 }
498
499 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800500 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
501 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500502}
503
Yanbiao Licf0db022016-01-29 00:54:25 -0800504BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
505
506BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700507{
Yanbiao Licf0db022016-01-29 00:54:25 -0800508 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600509
Yanbiao Licf0db022016-01-29 00:54:25 -0800510 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
511 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
512 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
513 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700514}
515
Yanbiao Licf0db022016-01-29 00:54:25 -0800516BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500517{
Yanbiao Licf0db022016-01-29 00:54:25 -0800518 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
519 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
520 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
521 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
522 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
523 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500524
Vince Lehman26b215c2014-08-17 15:00:41 -0500525 ndn::nfd::FaceStatus status;
526 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700527 std::vector<ndn::nfd::FaceStatus> activeFaces;
528 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500529
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700530 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800531 advanceClocks(time::milliseconds(100));
532 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500533
Yanbiao Licf0db022016-01-29 00:54:25 -0800534 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
535 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
536 BOOST_CHECK(it2 == m_rib.end());
537 BOOST_REQUIRE(it1 != m_rib.end());
538 BOOST_CHECK(it1->second->hasFaceId(1));
539 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500540}
541
Yanbiao Licf0db022016-01-29 00:54:25 -0800542BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600543{
Yanbiao Licf0db022016-01-29 00:54:25 -0800544 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
545 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
546 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
547 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
548 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600549
Yanbiao Licf0db022016-01-29 00:54:25 -0800550 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
551 ndn::nfd::FaceEventNotification notification;
552 notification.setKind(eventKind).setFaceId(faceId);
553 return notification;
554 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600555
Yanbiao Licf0db022016-01-29 00:54:25 -0800556 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
557 advanceClocks(time::milliseconds(100));
558 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600559
Yanbiao Licf0db022016-01-29 00:54:25 -0800560 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
561 advanceClocks(time::milliseconds(100));
562 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600563}
564
Yanbiao Licf0db022016-01-29 00:54:25 -0800565BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800566BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500567
568} // namespace tests
569} // namespace rib
570} // namespace nfd