blob: 77b1ed342438764ae2445449755b6d4ae0f45f02 [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"
Junxiao Shi75306352018-02-01 21:59:44 +000027#include "core/fib-max-depth.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050028
Davide Pesavento0064c1d2018-03-03 18:43:53 -050029#include "tests/manager-common-fixture.hpp"
30
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Davide Pesaventod2ea2352017-03-12 20:38:09 -040033#include <ndn-cxx/mgmt/nfd/face-status.hpp>
34#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Vince Lehman26b215c2014-08-17 15:00:41 -050035
Vince Lehman72446ec2014-07-09 10:50:02 -050036namespace nfd {
37namespace rib {
38namespace tests {
39
Junxiao Shi0de23a22015-12-03 20:07:02 +000040using namespace nfd::tests;
41
Yanbiao Licf0db022016-01-29 00:54:25 -080042struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050043{
Yanbiao Licf0db022016-01-29 00:54:25 -080044 bool isLocalhostConfigured;
45 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050046};
47
Yanbiao Licf0db022016-01-29 00:54:25 -080048class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050049{
50public:
Yanbiao Licf0db022016-01-29 00:54:25 -080051 explicit
52 RibManagerFixture(const ConfigurationStatus& status,
53 bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000054 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080055 , m_status(status)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000056 , m_nfdController(m_face, m_keyChain)
57 , m_fibUpdater(m_rib, m_nfdController)
58 , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
59 , m_manager(m_rib, m_dispatcher, m_face, m_nfdController, m_prefixPropagator)
Vince Lehman72446ec2014-07-09 10:50:02 -050060 {
Yanbiao Licf0db022016-01-29 00:54:25 -080061 m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
Vince Lehman72446ec2014-07-09 10:50:02 -050062
Yanbiao Licf0db022016-01-29 00:54:25 -080063 const std::string prefix = "rib\n{\n";
64 const std::string suffix = "}";
65 const std::string localhostSection = " localhost_security\n"
Vince Lehman72446ec2014-07-09 10:50:02 -050066 " {\n"
67 " trust-anchor\n"
68 " {\n"
69 " type any\n"
70 " }\n"
Yanbiao Licf0db022016-01-29 00:54:25 -080071 " }\n";
72 const std::string localhopSection = " localhop_security\n"
73 " {\n"
74 " trust-anchor\n"
75 " {\n"
76 " type any\n"
77 " }\n"
78 " }\n";
Vince Lehman72446ec2014-07-09 10:50:02 -050079
Yanbiao Licf0db022016-01-29 00:54:25 -080080 std::string ribSection = "";
81 if (m_status.isLocalhostConfigured) {
82 ribSection += localhostSection;
83 }
84 if (m_status.isLocalhopConfigured) {
85 ribSection += localhopSection;
86 }
87 const std::string CONFIG_STR = prefix + ribSection + suffix;
88
89 ConfigFile config;
90 m_manager.setConfigFile(config);
91 config.parse(CONFIG_STR, true, "test-rib");
92
93 registerWithNfd();
94
95 if (shouldClearRib) {
96 clearRib();
97 }
98 }
99
100private:
101 void
102 registerWithNfd()
103 {
104 m_manager.registerWithNfd();
105 advanceClocks(time::milliseconds(1));
106
107 auto replyFibAddCommand = [this] (const Interest& interest) {
108 nfd::ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +0000109 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -0800110 params.setFaceId(1).setCost(0);
111 nfd::ControlResponse resp;
112
113 resp.setCode(200).setBody(params.wireEncode());
114 shared_ptr<Data> data = make_shared<Data>(interest.getName());
115 data->setContent(resp.wireEncode());
116
117 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
118
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000119 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800120 };
121
122 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000123 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800124 if (commandPrefix.isPrefixOf(command.getName())) {
125 replyFibAddCommand(command);
126 advanceClocks(time::milliseconds(1));
127 }
128 }
129
130 // clear commands and responses
131 m_responses.clear();
132 m_commands.clear();
133 }
134
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000135 void
136 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800137 {
138 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000139 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800140 }
141 }
142
143public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400144 static ControlParameters
145 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400146 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800147 {
148 return ControlParameters()
149 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400150 .setFaceId(faceId)
151 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800152 .setCost(10)
153 .setFlags(0)
154 .setExpirationPeriod(expiry);
155 }
156
Davide Pesavento22db5392017-04-14 00:56:43 -0400157 static ControlParameters
158 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800159 {
160 return ControlParameters()
161 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400162 .setFaceId(faceId)
163 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800164 }
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
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000173 m_rib.onFibUpdateSuccess(batch, m_fibUpdater.m_inheritedRoutes,
174 bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
Yanbiao Licf0db022016-01-29 00:54:25 -0800175 }
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
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400186 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800187
188 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400189 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800190 {
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
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000226protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400227 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 std::vector<Interest>& m_commands;
229 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800230
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000231 ndn::nfd::Controller m_nfdController;
232 Rib m_rib;
233 FibUpdater m_fibUpdater;
234 AutoPrefixPropagator m_prefixPropagator;
Yanbiao Licf0db022016-01-29 00:54:25 -0800235 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800236};
237
238const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
239
240std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400241operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800242{
243 switch (result) {
244 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400245 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800246 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400247 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800248 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400249 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400251 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800252 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400253 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800254 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400255 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800256 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400257 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800258 };
259
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400260 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800261}
262
Yanbiao Licf0db022016-01-29 00:54:25 -0800263BOOST_AUTO_TEST_SUITE(TestRibManager)
264
265class AddTopPrefixFixture : public RibManagerFixture
266{
267public:
268 AddTopPrefixFixture()
269 : RibManagerFixture({true, true}, false)
270 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500271 }
272};
273
Yanbiao Licf0db022016-01-29 00:54:25 -0800274BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700275{
Yanbiao Licf0db022016-01-29 00:54:25 -0800276 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600277
Yanbiao Licf0db022016-01-29 00:54:25 -0800278 std::vector<Name> ribEntryNames;
279 for (auto&& entry : m_rib) {
280 ribEntryNames.push_back(entry.first);
281 }
282 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
283 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700284}
285
Yanbiao Licf0db022016-01-29 00:54:25 -0800286class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500287{
Yanbiao Licf0db022016-01-29 00:54:25 -0800288public:
289 UnauthorizedRibManagerFixture()
290 : RibManagerFixture({false, false}, true)
291 {
292 }
293};
Vince Lehman72446ec2014-07-09 10:50:02 -0500294
Yanbiao Licf0db022016-01-29 00:54:25 -0800295class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
296{
297public:
298 LocalhostAuthorizedRibManagerFixture()
299 : RibManagerFixture({true, false}, true)
300 {
301 }
302};
Vince Lehman72446ec2014-07-09 10:50:02 -0500303
Yanbiao Licf0db022016-01-29 00:54:25 -0800304class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
305{
306public:
307 LocalhopAuthorizedRibManagerFixture()
308 : RibManagerFixture({false, true}, true)
309 {
310 }
311};
312
313class AuthorizedRibManagerFixture : public RibManagerFixture
314{
315public:
316 AuthorizedRibManagerFixture()
317 : RibManagerFixture({true, true}, true)
318 {
319 }
320};
321
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400322using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800323 UnauthorizedRibManagerFixture,
324 LocalhostAuthorizedRibManagerFixture,
325 LocalhopAuthorizedRibManagerFixture,
326 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400327>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800328
329BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
330{
331 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
332 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
333 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
334 auto successResp = this->makeResponse(200, "Success", parameters);
335 auto failureResp = ControlResponse(403, "authorization rejected");
336
337 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
338 this->receiveInterest(commandHost);
339 this->receiveInterest(commandHop);
340
341 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
342 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
343 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
344
345 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000346 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800347 ManagerCommonFixture::CheckResponseResult::OK);
348 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000349 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800350 ManagerCommonFixture::CheckResponseResult::OK);
351 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500352}
353
Yanbiao Licf0db022016-01-29 00:54:25 -0800354BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
355
356BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500357{
Yanbiao Licf0db022016-01-29 00:54:25 -0800358 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
359 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500360
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000361 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
362 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
363 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
364 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500365
Yanbiao Licf0db022016-01-29 00:54:25 -0800366 receiveInterest(commandRegister);
367 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500368
Yanbiao Licf0db022016-01-29 00:54:25 -0800369 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000370 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000372 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800373 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500374
Yanbiao Licf0db022016-01-29 00:54:25 -0800375 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
376 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
377 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500378}
379
Yanbiao Licf0db022016-01-29 00:54:25 -0800380BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500381{
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
383 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
384 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
385 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500386
Yanbiao Licf0db022016-01-29 00:54:25 -0800387 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000388 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
389 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
390 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
391 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500392
Yanbiao Licf0db022016-01-29 00:54:25 -0800393 receiveInterest(commandRegister);
394 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 paramsRegister.setFaceId(inFaceId);
397 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500398
Yanbiao Licf0db022016-01-29 00:54:25 -0800399 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000400 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000402 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800403 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500404
Yanbiao Licf0db022016-01-29 00:54:25 -0800405 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
406 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
407 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500408}
409
Yanbiao Licf0db022016-01-29 00:54:25 -0800410BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000411{
Yanbiao Licf0db022016-01-29 00:54:25 -0800412 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, time::milliseconds(50));
413 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
414 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000415
Yanbiao Licf0db022016-01-29 00:54:25 -0800416 advanceClocks(time::milliseconds(55));
417 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
418 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
419 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000420
Yanbiao Licf0db022016-01-29 00:54:25 -0800421 m_commands.clear();
422 paramsRegister.setExpirationPeriod(time::milliseconds(100));
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(), 1); // the registered route is still active
427 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000428}
429
Junxiao Shi75306352018-02-01 21:59:44 +0000430BOOST_AUTO_TEST_CASE(NameTooLong)
431{
432 Name prefix;
433 while (prefix.size() <= FIB_MAX_DEPTH) {
434 prefix.append("A");
435 }
436 auto params = makeRegisterParameters(prefix, 2899);
437 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
438 receiveInterest(command);
439
440 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
441 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
442 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
443 " components")),
444 CheckResponseResult::OK);
445
446 BOOST_CHECK_EQUAL(m_commands.size(), 0);
447}
448
Yanbiao Licf0db022016-01-29 00:54:25 -0800449BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
450
Yanbiao Licf0db022016-01-29 00:54:25 -0800451BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500452{
Yanbiao Licf0db022016-01-29 00:54:25 -0800453 uint64_t faceId = 0;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400454 auto generateRoute = [&faceId] () -> Route {
Yanbiao Licf0db022016-01-29 00:54:25 -0800455 Route route;
456 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400457 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400458 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 return route;
460 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500461
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 const size_t nEntries = 108;
463 std::set<Name> actualPrefixes;
464 for (size_t i = 0; i < nEntries; ++i) {
465 Name prefix = Name("/test-dataset").appendNumber(i);
466 actualPrefixes.insert(prefix);
467 m_rib.insert(prefix, generateRoute());
468 if (i & 0x1) {
469 m_rib.insert(prefix, generateRoute());
470 m_rib.insert(prefix, generateRoute());
471 }
472 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500473
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000474 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500475
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400476 Block content = concatenateResponses();
477 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800478 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
479
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400480 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800481 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400482 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800483 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800484 actualPrefixes.erase(decodedEntry.getName());
485
486 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
487 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
488
489 auto matchedEntry = matchedEntryIt->second;
490 BOOST_REQUIRE(matchedEntry != nullptr);
491
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400492 expectedRecords.emplace_back();
493 expectedRecords.back().setName(matchedEntry->getName());
494 for (const auto& route : matchedEntry->getRoutes()) {
495 expectedRecords.back().addRoute(ndn::nfd::Route()
496 .setFaceId(route.faceId)
497 .setOrigin(route.origin)
498 .setCost(route.cost)
499 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800500 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800501 }
502
503 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800504 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
505 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500506}
507
Yanbiao Licf0db022016-01-29 00:54:25 -0800508BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
509
510BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700511{
Yanbiao Licf0db022016-01-29 00:54:25 -0800512 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600513
Yanbiao Licf0db022016-01-29 00:54:25 -0800514 advanceClocks(time::seconds(301)); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
515 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
516 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
517 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700518}
519
Yanbiao Licf0db022016-01-29 00:54:25 -0800520BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500521{
Yanbiao Licf0db022016-01-29 00:54:25 -0800522 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
523 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
524 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
525 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
526 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
527 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500528
Vince Lehman26b215c2014-08-17 15:00:41 -0500529 ndn::nfd::FaceStatus status;
530 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700531 std::vector<ndn::nfd::FaceStatus> activeFaces;
532 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500533
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700534 m_manager.removeInvalidFaces(activeFaces);
Yanbiao Licf0db022016-01-29 00:54:25 -0800535 advanceClocks(time::milliseconds(100));
536 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500537
Yanbiao Licf0db022016-01-29 00:54:25 -0800538 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
539 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
540 BOOST_CHECK(it2 == m_rib.end());
541 BOOST_REQUIRE(it1 != m_rib.end());
542 BOOST_CHECK(it1->second->hasFaceId(1));
543 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500544}
545
Yanbiao Licf0db022016-01-29 00:54:25 -0800546BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600547{
Yanbiao Licf0db022016-01-29 00:54:25 -0800548 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
549 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
550 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
551 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
552 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600553
Yanbiao Licf0db022016-01-29 00:54:25 -0800554 auto makeNotification = [] (ndn::nfd::FaceEventKind eventKind, uint64_t faceId) -> ndn::nfd::FaceEventNotification {
555 ndn::nfd::FaceEventNotification notification;
556 notification.setKind(eventKind).setFaceId(faceId);
557 return notification;
558 };
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_DESTROYED, 1));
561 advanceClocks(time::milliseconds(100));
562 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600563
Yanbiao Licf0db022016-01-29 00:54:25 -0800564 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
565 advanceClocks(time::milliseconds(100));
566 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600567}
568
Yanbiao Licf0db022016-01-29 00:54:25 -0800569BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Yanbiao Licf0db022016-01-29 00:54:25 -0800570BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Vince Lehman72446ec2014-07-09 10:50:02 -0500571
572} // namespace tests
573} // namespace rib
574} // namespace nfd