blob: 04d9dca294335165596548d29d58cf40fd4a532c [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/*
Davide Pesavento8a05c7f2019-02-28 02:26:19 -05003 * Copyright (c) 2014-2019, 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
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050026#include "mgmt/rib-manager.hpp"
Junxiao Shi75306352018-02-01 21:59:44 +000027#include "core/fib-max-depth.hpp"
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050028#include "rib/fib-updater.hpp"
Vince Lehman72446ec2014-07-09 10:50:02 -050029
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "manager-common-fixture.hpp"
Davide Pesavento0064c1d2018-03-03 18:43:53 -050031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.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 {
Vince Lehman72446ec2014-07-09 10:50:02 -050037namespace tests {
38
Yanbiao Licf0db022016-01-29 00:54:25 -080039struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050040{
Yanbiao Licf0db022016-01-29 00:54:25 -080041 bool isLocalhostConfigured;
42 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050043};
44
Junxiao Shif4cfed12018-08-22 23:26:29 +000045static ConfigSection
46getValidatorConfigSection()
47{
48 ConfigSection section;
49 section.put("trust-anchor.type", "any");
50 return section;
51}
52
Yanbiao Licf0db022016-01-29 00:54:25 -080053class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050054{
55public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040056 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shi221b6fe2016-07-14 18:21:56 +000057 : m_commands(m_face.sentInterests)
Yanbiao Licf0db022016-01-29 00:54:25 -080058 , m_status(status)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000059 , m_nfdController(m_face, m_keyChain)
60 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040061 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -050062 {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050063 m_rib.mockFibResponse = [] (const rib::RibUpdateBatch& batch) {
Junxiao Shi52009042018-09-10 12:33:56 +000064 BOOST_CHECK(batch.begin() != batch.end());
65 return true;
66 };
67 m_rib.wantMockFibResponseOnce = false;
Vince Lehman72446ec2014-07-09 10:50:02 -050068
Yanbiao Licf0db022016-01-29 00:54:25 -080069 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000070 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080071 }
72 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000073 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080074 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000075 else {
76 m_manager.disableLocalhop();
77 }
Yanbiao Licf0db022016-01-29 00:54:25 -080078
79 registerWithNfd();
80
81 if (shouldClearRib) {
82 clearRib();
83 }
84 }
85
86private:
87 void
88 registerWithNfd()
89 {
90 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050091 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -080092
93 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -050094 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +000095 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -080096 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050097 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -080098
99 resp.setCode(200).setBody(params.wireEncode());
100 shared_ptr<Data> data = make_shared<Data>(interest.getName());
101 data->setContent(resp.wireEncode());
102
103 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
104
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000105 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800106 };
107
108 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000109 for (const auto& command : m_commands) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800110 if (commandPrefix.isPrefixOf(command.getName())) {
111 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500112 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800113 }
114 }
115
116 // clear commands and responses
117 m_responses.clear();
118 m_commands.clear();
119 }
120
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000121 void
122 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800123 {
124 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000125 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800126 }
127 }
128
129public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400130 static ControlParameters
131 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400132 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800133 {
134 return ControlParameters()
135 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400136 .setFaceId(faceId)
137 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800138 .setCost(10)
139 .setFlags(0)
140 .setExpirationPeriod(expiry);
141 }
142
Davide Pesavento22db5392017-04-14 00:56:43 -0400143 static ControlParameters
144 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800145 {
146 return ControlParameters()
147 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400148 .setFaceId(faceId)
149 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800150 }
151
Yanbiao Licf0db022016-01-29 00:54:25 -0800152public:
153 enum class CheckCommandResult {
154 OK,
155 OUT_OF_BOUNDARY,
156 WRONG_FORMAT,
157 WRONG_VERB,
158 WRONG_PARAMS_FORMAT,
159 WRONG_PARAMS_NAME,
160 WRONG_PARAMS_FACE
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400161 };
Yanbiao Licf0db022016-01-29 00:54:25 -0800162
163 CheckCommandResult
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400164 checkCommand(size_t idx, const char* verbStr, const ControlParameters& expectedParams) const
Yanbiao Licf0db022016-01-29 00:54:25 -0800165 {
166 if (idx > m_commands.size()) {
167 return CheckCommandResult::OUT_OF_BOUNDARY;
168 }
169 const auto& name = m_commands[idx].getName();
170
171 if (!FIB_COMMAND_PREFIX.isPrefixOf(name) || FIB_COMMAND_PREFIX.size() >= name.size()) {
172 return CheckCommandResult::WRONG_FORMAT;
173 }
174 const auto& verb = name[FIB_COMMAND_PREFIX.size()];
175
176 Name::Component expectedVerb(verbStr);
177 if (verb != expectedVerb) {
178 return CheckCommandResult::WRONG_VERB;
179 }
180
181 ControlParameters parameters;
182 try {
183 Block rawParameters = name[FIB_COMMAND_PREFIX.size() + 1].blockFromValue();
184 parameters.wireDecode(rawParameters);
185 }
186 catch (...) {
187 return CheckCommandResult::WRONG_PARAMS_FORMAT;
188 }
189
190 if (parameters.getName() != expectedParams.getName()) {
191 return CheckCommandResult::WRONG_PARAMS_NAME;
192 }
193
194 if (parameters.getFaceId() != expectedParams.getFaceId()) {
195 return CheckCommandResult::WRONG_PARAMS_FACE;
196 }
197
198 return CheckCommandResult::OK;
199 }
200
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000201protected:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400202 static const Name FIB_COMMAND_PREFIX;
Yanbiao Licf0db022016-01-29 00:54:25 -0800203 std::vector<Interest>& m_commands;
204 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800205
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000206 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500207 rib::Rib m_rib;
208 rib::FibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800209 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800210};
211
212const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");
213
214std::ostream&
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400215operator<<(std::ostream& os, RibManagerFixture::CheckCommandResult result)
Yanbiao Licf0db022016-01-29 00:54:25 -0800216{
217 switch (result) {
218 case RibManagerFixture::CheckCommandResult::OK:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400219 return os << "OK";
Yanbiao Licf0db022016-01-29 00:54:25 -0800220 case RibManagerFixture::CheckCommandResult::OUT_OF_BOUNDARY:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400221 return os << "OUT_OF_BOUNDARY";
Yanbiao Licf0db022016-01-29 00:54:25 -0800222 case RibManagerFixture::CheckCommandResult::WRONG_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400223 return os << "WRONG_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800224 case RibManagerFixture::CheckCommandResult::WRONG_VERB:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400225 return os << "WRONG_VERB";
Yanbiao Licf0db022016-01-29 00:54:25 -0800226 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FORMAT:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400227 return os << "WRONG_PARAMS_FORMAT";
Yanbiao Licf0db022016-01-29 00:54:25 -0800228 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_NAME:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400229 return os << "WRONG_PARAMS_NAME";
Yanbiao Licf0db022016-01-29 00:54:25 -0800230 case RibManagerFixture::CheckCommandResult::WRONG_PARAMS_FACE:
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400231 return os << "WRONG_PARAMS_FACE";
Yanbiao Licf0db022016-01-29 00:54:25 -0800232 };
233
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400234 return os << static_cast<int>(result);
Yanbiao Licf0db022016-01-29 00:54:25 -0800235}
236
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500237BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800238BOOST_AUTO_TEST_SUITE(TestRibManager)
239
240class AddTopPrefixFixture : public RibManagerFixture
241{
242public:
243 AddTopPrefixFixture()
244 : RibManagerFixture({true, true}, false)
245 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500246 }
247};
248
Yanbiao Licf0db022016-01-29 00:54:25 -0800249BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700250{
Yanbiao Licf0db022016-01-29 00:54:25 -0800251 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600252
Yanbiao Licf0db022016-01-29 00:54:25 -0800253 std::vector<Name> ribEntryNames;
254 for (auto&& entry : m_rib) {
255 ribEntryNames.push_back(entry.first);
256 }
257 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
258 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700259}
260
Yanbiao Licf0db022016-01-29 00:54:25 -0800261class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500262{
Yanbiao Licf0db022016-01-29 00:54:25 -0800263public:
264 UnauthorizedRibManagerFixture()
265 : RibManagerFixture({false, false}, true)
266 {
267 }
268};
Vince Lehman72446ec2014-07-09 10:50:02 -0500269
Yanbiao Licf0db022016-01-29 00:54:25 -0800270class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
271{
272public:
273 LocalhostAuthorizedRibManagerFixture()
274 : RibManagerFixture({true, false}, true)
275 {
276 }
277};
Vince Lehman72446ec2014-07-09 10:50:02 -0500278
Yanbiao Licf0db022016-01-29 00:54:25 -0800279class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
280{
281public:
282 LocalhopAuthorizedRibManagerFixture()
283 : RibManagerFixture({false, true}, true)
284 {
285 }
286};
287
288class AuthorizedRibManagerFixture : public RibManagerFixture
289{
290public:
291 AuthorizedRibManagerFixture()
292 : RibManagerFixture({true, true}, true)
293 {
294 }
295};
296
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400297using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800298 UnauthorizedRibManagerFixture,
299 LocalhostAuthorizedRibManagerFixture,
300 LocalhopAuthorizedRibManagerFixture,
301 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400302>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800303
304BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
305{
306 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
307 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
308 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
309 auto successResp = this->makeResponse(200, "Success", parameters);
310 auto failureResp = ControlResponse(403, "authorization rejected");
311
312 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
313 this->receiveInterest(commandHost);
314 this->receiveInterest(commandHop);
315
316 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
317 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
318 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
319
320 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000321 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800322 ManagerCommonFixture::CheckResponseResult::OK);
323 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000324 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800325 ManagerCommonFixture::CheckResponseResult::OK);
326 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500327}
328
Yanbiao Licf0db022016-01-29 00:54:25 -0800329BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
330
331BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500332{
Yanbiao Licf0db022016-01-29 00:54:25 -0800333 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
334 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500335
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000336 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
337 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
338 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
339 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500340
Yanbiao Licf0db022016-01-29 00:54:25 -0800341 receiveInterest(commandRegister);
342 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500343
Yanbiao Licf0db022016-01-29 00:54:25 -0800344 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000345 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800346 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000347 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800348 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500349
Yanbiao Licf0db022016-01-29 00:54:25 -0800350 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
351 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
352 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500353}
354
Yanbiao Licf0db022016-01-29 00:54:25 -0800355BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500356{
Yanbiao Licf0db022016-01-29 00:54:25 -0800357 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
358 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
359 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
360 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500361
Yanbiao Licf0db022016-01-29 00:54:25 -0800362 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000363 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
364 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
365 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
366 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500367
Yanbiao Licf0db022016-01-29 00:54:25 -0800368 receiveInterest(commandRegister);
369 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500370
Yanbiao Licf0db022016-01-29 00:54:25 -0800371 paramsRegister.setFaceId(inFaceId);
372 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Yanbiao Licf0db022016-01-29 00:54:25 -0800374 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000375 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000377 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800378 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500379
Yanbiao Licf0db022016-01-29 00:54:25 -0800380 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
381 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
382 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500383}
384
Yanbiao Licf0db022016-01-29 00:54:25 -0800385BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000386{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500387 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800388 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
389 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000390
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500391 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800392 BOOST_REQUIRE_EQUAL(m_commands.size(), 2); // the registered route has expired
393 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
394 BOOST_CHECK_EQUAL(checkCommand(1, "remove-nexthop", paramsUnregister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000395
Yanbiao Licf0db022016-01-29 00:54:25 -0800396 m_commands.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500397 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000399
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500400 advanceClocks(55_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800401 BOOST_REQUIRE_EQUAL(m_commands.size(), 1); // the registered route is still active
402 BOOST_CHECK_EQUAL(checkCommand(0, "add-nexthop", paramsRegister), CheckCommandResult::OK);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000403}
404
Junxiao Shi75306352018-02-01 21:59:44 +0000405BOOST_AUTO_TEST_CASE(NameTooLong)
406{
407 Name prefix;
408 while (prefix.size() <= FIB_MAX_DEPTH) {
409 prefix.append("A");
410 }
411 auto params = makeRegisterParameters(prefix, 2899);
412 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
413 receiveInterest(command);
414
415 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
416 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(), ControlResponse(414,
417 "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) +
418 " components")),
419 CheckResponseResult::OK);
420
421 BOOST_CHECK_EQUAL(m_commands.size(), 0);
422}
423
Yanbiao Licf0db022016-01-29 00:54:25 -0800424BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
425
Yanbiao Licf0db022016-01-29 00:54:25 -0800426BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500427{
Yanbiao Licf0db022016-01-29 00:54:25 -0800428 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500429 auto generateRoute = [&faceId] () -> rib::Route {
430 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800431 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400432 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400433 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800434 return route;
435 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500436
Yanbiao Licf0db022016-01-29 00:54:25 -0800437 const size_t nEntries = 108;
438 std::set<Name> actualPrefixes;
439 for (size_t i = 0; i < nEntries; ++i) {
440 Name prefix = Name("/test-dataset").appendNumber(i);
441 actualPrefixes.insert(prefix);
442 m_rib.insert(prefix, generateRoute());
443 if (i & 0x1) {
444 m_rib.insert(prefix, generateRoute());
445 m_rib.insert(prefix, generateRoute());
446 }
447 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500448
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000449 receiveInterest(Interest("/localhost/nfd/rib/list"));
Vince Lehman72446ec2014-07-09 10:50:02 -0500450
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400451 Block content = concatenateResponses();
452 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800453 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
454
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400455 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800456 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400457 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800458 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800459 actualPrefixes.erase(decodedEntry.getName());
460
461 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
462 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
463
464 auto matchedEntry = matchedEntryIt->second;
465 BOOST_REQUIRE(matchedEntry != nullptr);
466
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400467 expectedRecords.emplace_back();
468 expectedRecords.back().setName(matchedEntry->getName());
469 for (const auto& route : matchedEntry->getRoutes()) {
470 expectedRecords.back().addRoute(ndn::nfd::Route()
471 .setFaceId(route.faceId)
472 .setOrigin(route.origin)
473 .setCost(route.cost)
474 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800475 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800476 }
477
478 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800479 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
480 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500481}
482
Yanbiao Licf0db022016-01-29 00:54:25 -0800483BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
484
485BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700486{
Yanbiao Licf0db022016-01-29 00:54:25 -0800487 BOOST_CHECK_EQUAL(m_commands.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600488
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500489 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Yanbiao Licf0db022016-01-29 00:54:25 -0800490 BOOST_REQUIRE_EQUAL(m_commands.size(), 2);
491 BOOST_CHECK_EQUAL(m_commands[0].getName(), "/localhost/nfd/faces/events");
492 BOOST_CHECK_EQUAL(m_commands[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700493}
494
Yanbiao Licf0db022016-01-29 00:54:25 -0800495BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500496{
Yanbiao Licf0db022016-01-29 00:54:25 -0800497 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
498 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
499 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
500 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
501 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
502 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500503
Vince Lehman26b215c2014-08-17 15:00:41 -0500504 ndn::nfd::FaceStatus status;
505 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700506 std::vector<ndn::nfd::FaceStatus> activeFaces;
507 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500508
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700509 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500510 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800511 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500512
Yanbiao Licf0db022016-01-29 00:54:25 -0800513 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
514 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
515 BOOST_CHECK(it2 == m_rib.end());
516 BOOST_REQUIRE(it1 != m_rib.end());
517 BOOST_CHECK(it1->second->hasFaceId(1));
518 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500519}
520
Yanbiao Licf0db022016-01-29 00:54:25 -0800521BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600522{
Yanbiao Licf0db022016-01-29 00:54:25 -0800523 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
524 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
525 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
526 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
527 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600528
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500529 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
530 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800531 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600532
Yanbiao Licf0db022016-01-29 00:54:25 -0800533 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500534 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800535 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600536
Yanbiao Licf0db022016-01-29 00:54:25 -0800537 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500538 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800539 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600540}
541
Yanbiao Licf0db022016-01-29 00:54:25 -0800542BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500543
Yanbiao Licf0db022016-01-29 00:54:25 -0800544BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500545BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500546
547} // namespace tests
Vince Lehman72446ec2014-07-09 10:50:02 -0500548} // namespace nfd