blob: 4bf919e6bc1cd02d987d73b3a73d1cad987caff7 [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"
Vince Lehman72446ec2014-07-09 10:50:02 -050027
Davide Pesavento78ddcab2019-02-28 22:00:03 -050028#include "manager-common-fixture.hpp"
Junxiao Shidf1dc652019-08-30 19:03:19 +000029#include "tests/daemon/rib/fib-updates-common.hpp"
Davide Pesavento0064c1d2018-03-03 18:43:53 -050030
Junxiao Shicbc8e942016-09-06 03:17:45 +000031#include <ndn-cxx/lp/tags.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 {
Vince Lehman72446ec2014-07-09 10:50:02 -050036namespace tests {
37
Yanbiao Licf0db022016-01-29 00:54:25 -080038struct ConfigurationStatus
Vince Lehman72446ec2014-07-09 10:50:02 -050039{
Yanbiao Licf0db022016-01-29 00:54:25 -080040 bool isLocalhostConfigured;
41 bool isLocalhopConfigured;
Vince Lehman72446ec2014-07-09 10:50:02 -050042};
43
Junxiao Shif4cfed12018-08-22 23:26:29 +000044static ConfigSection
45getValidatorConfigSection()
46{
47 ConfigSection section;
48 section.put("trust-anchor.type", "any");
49 return section;
50}
51
Yanbiao Licf0db022016-01-29 00:54:25 -080052class RibManagerFixture : public ManagerCommonFixture
Vince Lehman72446ec2014-07-09 10:50:02 -050053{
54public:
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040055 RibManagerFixture(const ConfigurationStatus& status, bool shouldClearRib)
Junxiao Shidf1dc652019-08-30 19:03:19 +000056 : m_status(status)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000057 , m_nfdController(m_face, m_keyChain)
58 , m_fibUpdater(m_rib, m_nfdController)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040059 , m_manager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
Vince Lehman72446ec2014-07-09 10:50:02 -050060 {
Yanbiao Licf0db022016-01-29 00:54:25 -080061 if (m_status.isLocalhostConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000062 m_manager.applyLocalhostConfig(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080063 }
64 if (m_status.isLocalhopConfigured) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000065 m_manager.enableLocalhop(getValidatorConfigSection(), "test");
Yanbiao Licf0db022016-01-29 00:54:25 -080066 }
Junxiao Shif4cfed12018-08-22 23:26:29 +000067 else {
68 m_manager.disableLocalhop();
69 }
Yanbiao Licf0db022016-01-29 00:54:25 -080070
71 registerWithNfd();
72
73 if (shouldClearRib) {
74 clearRib();
75 }
76 }
77
78private:
79 void
80 registerWithNfd()
81 {
82 m_manager.registerWithNfd();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050083 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -080084
85 auto replyFibAddCommand = [this] (const Interest& interest) {
Davide Pesavento78ddcab2019-02-28 22:00:03 -050086 ControlParameters params(interest.getName().get(-5).blockFromValue());
Junxiao Shifde3f542016-07-10 19:54:53 +000087 BOOST_CHECK(params.getName() == "/localhost/nfd/rib" || params.getName() == "/localhop/nfd/rib");
Yanbiao Licf0db022016-01-29 00:54:25 -080088 params.setFaceId(1).setCost(0);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050089 ControlResponse resp;
Yanbiao Licf0db022016-01-29 00:54:25 -080090
91 resp.setCode(200).setBody(params.wireEncode());
92 shared_ptr<Data> data = make_shared<Data>(interest.getName());
93 data->setContent(resp.wireEncode());
94
95 m_keyChain.sign(*data, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_SHA256));
96
Junxiao Shi221b6fe2016-07-14 18:21:56 +000097 m_face.getIoService().post([this, data] { m_face.receive(*data); });
Yanbiao Licf0db022016-01-29 00:54:25 -080098 };
99
100 Name commandPrefix("/localhost/nfd/fib/add-nexthop");
Junxiao Shidf1dc652019-08-30 19:03:19 +0000101 for (const auto& command : m_face.sentInterests) {
Yanbiao Licf0db022016-01-29 00:54:25 -0800102 if (commandPrefix.isPrefixOf(command.getName())) {
103 replyFibAddCommand(command);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500104 advanceClocks(1_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800105 }
106 }
107
108 // clear commands and responses
109 m_responses.clear();
Junxiao Shidf1dc652019-08-30 19:03:19 +0000110 m_face.sentInterests.clear();
Yanbiao Licf0db022016-01-29 00:54:25 -0800111 }
112
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000113 void
114 clearRib()
Yanbiao Licf0db022016-01-29 00:54:25 -0800115 {
116 while (!m_rib.empty()) {
Davide Pesaventoe94804b2016-09-19 17:23:21 +0000117 m_rib.erase(m_rib.begin()->first, *m_rib.begin()->second->begin());
Yanbiao Licf0db022016-01-29 00:54:25 -0800118 }
119 }
120
121public:
Davide Pesavento22db5392017-04-14 00:56:43 -0400122 static ControlParameters
123 makeRegisterParameters(const Name& name, uint64_t faceId = 0,
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400124 time::milliseconds expiry = time::milliseconds::max())
Yanbiao Licf0db022016-01-29 00:54:25 -0800125 {
126 return ControlParameters()
127 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400128 .setFaceId(faceId)
129 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
Yanbiao Licf0db022016-01-29 00:54:25 -0800130 .setCost(10)
131 .setFlags(0)
132 .setExpirationPeriod(expiry);
133 }
134
Davide Pesavento22db5392017-04-14 00:56:43 -0400135 static ControlParameters
136 makeUnregisterParameters(const Name& name, uint64_t faceId = 0)
Yanbiao Licf0db022016-01-29 00:54:25 -0800137 {
138 return ControlParameters()
139 .setName(name)
Davide Pesavento22db5392017-04-14 00:56:43 -0400140 .setFaceId(faceId)
141 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Yanbiao Licf0db022016-01-29 00:54:25 -0800142 }
143
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000144protected:
Yanbiao Licf0db022016-01-29 00:54:25 -0800145 ConfigurationStatus m_status;
Yanbiao Licf0db022016-01-29 00:54:25 -0800146
Davide Pesavento9f8b10e2018-08-22 08:45:37 +0000147 ndn::nfd::Controller m_nfdController;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500148 rib::Rib m_rib;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000149 rib::tests::MockFibUpdater m_fibUpdater;
Yanbiao Licf0db022016-01-29 00:54:25 -0800150 RibManager m_manager;
Yanbiao Licf0db022016-01-29 00:54:25 -0800151};
152
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500153BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Licf0db022016-01-29 00:54:25 -0800154BOOST_AUTO_TEST_SUITE(TestRibManager)
155
156class AddTopPrefixFixture : public RibManagerFixture
157{
158public:
159 AddTopPrefixFixture()
160 : RibManagerFixture({true, true}, false)
161 {
Vince Lehman72446ec2014-07-09 10:50:02 -0500162 }
163};
164
Yanbiao Licf0db022016-01-29 00:54:25 -0800165BOOST_FIXTURE_TEST_CASE(AddTopPrefix, AddTopPrefixFixture)
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700166{
Yanbiao Licf0db022016-01-29 00:54:25 -0800167 BOOST_CHECK_EQUAL(m_rib.size(), 2);
Vince Lehman76c751c2014-11-18 17:36:38 -0600168
Yanbiao Licf0db022016-01-29 00:54:25 -0800169 std::vector<Name> ribEntryNames;
170 for (auto&& entry : m_rib) {
171 ribEntryNames.push_back(entry.first);
172 }
173 BOOST_CHECK_EQUAL(ribEntryNames[0], "/localhop/nfd");
174 BOOST_CHECK_EQUAL(ribEntryNames[1], "/localhost/nfd");
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700175}
176
Yanbiao Licf0db022016-01-29 00:54:25 -0800177class UnauthorizedRibManagerFixture : public RibManagerFixture
Vince Lehman72446ec2014-07-09 10:50:02 -0500178{
Yanbiao Licf0db022016-01-29 00:54:25 -0800179public:
180 UnauthorizedRibManagerFixture()
181 : RibManagerFixture({false, false}, true)
182 {
183 }
184};
Vince Lehman72446ec2014-07-09 10:50:02 -0500185
Yanbiao Licf0db022016-01-29 00:54:25 -0800186class LocalhostAuthorizedRibManagerFixture : public RibManagerFixture
187{
188public:
189 LocalhostAuthorizedRibManagerFixture()
190 : RibManagerFixture({true, false}, true)
191 {
192 }
193};
Vince Lehman72446ec2014-07-09 10:50:02 -0500194
Yanbiao Licf0db022016-01-29 00:54:25 -0800195class LocalhopAuthorizedRibManagerFixture : public RibManagerFixture
196{
197public:
198 LocalhopAuthorizedRibManagerFixture()
199 : RibManagerFixture({false, true}, true)
200 {
201 }
202};
203
204class AuthorizedRibManagerFixture : public RibManagerFixture
205{
206public:
207 AuthorizedRibManagerFixture()
208 : RibManagerFixture({true, true}, true)
209 {
210 }
211};
212
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400213using AllFixtures = boost::mpl::vector<
Yanbiao Licf0db022016-01-29 00:54:25 -0800214 UnauthorizedRibManagerFixture,
215 LocalhostAuthorizedRibManagerFixture,
216 LocalhopAuthorizedRibManagerFixture,
217 AuthorizedRibManagerFixture
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400218>;
Yanbiao Licf0db022016-01-29 00:54:25 -0800219
220BOOST_FIXTURE_TEST_CASE_TEMPLATE(CommandAuthorization, T, AllFixtures, T)
221{
222 auto parameters = this->makeRegisterParameters("/test-authorization", 9527);
223 auto commandHost = this->makeControlCommandRequest("/localhost/nfd/rib/register", parameters);
224 auto commandHop = this->makeControlCommandRequest("/localhop/nfd/rib/register", parameters);
225 auto successResp = this->makeResponse(200, "Success", parameters);
226 auto failureResp = ControlResponse(403, "authorization rejected");
227
228 BOOST_CHECK_EQUAL(this->m_responses.size(), 0);
229 this->receiveInterest(commandHost);
230 this->receiveInterest(commandHop);
231
232 auto nExpectedResponses = this->m_status.isLocalhopConfigured ? 2 : 1;
233 auto expectedLocalhostResponse = this->m_status.isLocalhostConfigured ? successResp : failureResp;
234 auto expectedLocalhopResponse = this->m_status.isLocalhopConfigured ? successResp : failureResp;
235
236 BOOST_REQUIRE_EQUAL(this->m_responses.size(), nExpectedResponses);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000237 BOOST_CHECK_EQUAL(this->checkResponse(0, commandHost.getName(), expectedLocalhostResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800238 ManagerCommonFixture::CheckResponseResult::OK);
239 if (nExpectedResponses == 2) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000240 BOOST_CHECK_EQUAL(this->checkResponse(1, commandHop.getName(), expectedLocalhopResponse),
Yanbiao Licf0db022016-01-29 00:54:25 -0800241 ManagerCommonFixture::CheckResponseResult::OK);
242 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500243}
244
Yanbiao Licf0db022016-01-29 00:54:25 -0800245BOOST_FIXTURE_TEST_SUITE(RegisterUnregister, LocalhostAuthorizedRibManagerFixture)
246
247BOOST_AUTO_TEST_CASE(Basic)
Vince Lehman72446ec2014-07-09 10:50:02 -0500248{
Yanbiao Licf0db022016-01-29 00:54:25 -0800249 auto paramsRegister = makeRegisterParameters("/test-register-unregister", 9527);
250 auto paramsUnregister = makeUnregisterParameters("/test-register-unregister", 9527);
Vince Lehman72446ec2014-07-09 10:50:02 -0500251
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000252 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
253 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
254 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
255 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(1234));
Vince Lehman72446ec2014-07-09 10:50:02 -0500256
Yanbiao Licf0db022016-01-29 00:54:25 -0800257 receiveInterest(commandRegister);
258 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500259
Yanbiao Licf0db022016-01-29 00:54:25 -0800260 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000261 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800262 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000263 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800264 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500265
Junxiao Shidf1dc652019-08-30 19:03:19 +0000266 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
267 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
268 rib::FibUpdate::createAddUpdate("/test-register-unregister", 9527, 10));
269 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
270 rib::FibUpdate::createRemoveUpdate("/test-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500271}
272
Yanbiao Licf0db022016-01-29 00:54:25 -0800273BOOST_AUTO_TEST_CASE(SelfOperation)
Vince Lehman72446ec2014-07-09 10:50:02 -0500274{
Yanbiao Licf0db022016-01-29 00:54:25 -0800275 auto paramsRegister = makeRegisterParameters("/test-self-register-unregister");
276 auto paramsUnregister = makeUnregisterParameters("/test-self-register-unregister");
277 BOOST_CHECK_EQUAL(paramsRegister.getFaceId(), 0);
278 BOOST_CHECK_EQUAL(paramsUnregister.getFaceId(), 0);
Vince Lehman72446ec2014-07-09 10:50:02 -0500279
Yanbiao Licf0db022016-01-29 00:54:25 -0800280 const uint64_t inFaceId = 9527;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000281 auto commandRegister = makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister);
282 commandRegister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
283 auto commandUnregister = makeControlCommandRequest("/localhost/nfd/rib/unregister", paramsUnregister);
284 commandUnregister.setTag(make_shared<lp::IncomingFaceIdTag>(inFaceId));
Vince Lehman72446ec2014-07-09 10:50:02 -0500285
Yanbiao Licf0db022016-01-29 00:54:25 -0800286 receiveInterest(commandRegister);
287 receiveInterest(commandUnregister);
Vince Lehman72446ec2014-07-09 10:50:02 -0500288
Yanbiao Licf0db022016-01-29 00:54:25 -0800289 paramsRegister.setFaceId(inFaceId);
290 paramsUnregister.setFaceId(inFaceId);
Vince Lehman72446ec2014-07-09 10:50:02 -0500291
Yanbiao Licf0db022016-01-29 00:54:25 -0800292 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000293 BOOST_CHECK_EQUAL(checkResponse(0, commandRegister.getName(), makeResponse(200, "Success", paramsRegister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800294 CheckResponseResult::OK);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000295 BOOST_CHECK_EQUAL(checkResponse(1, commandUnregister.getName(), makeResponse(200, "Success", paramsUnregister)),
Yanbiao Licf0db022016-01-29 00:54:25 -0800296 CheckResponseResult::OK);
Vince Lehman72446ec2014-07-09 10:50:02 -0500297
Junxiao Shidf1dc652019-08-30 19:03:19 +0000298 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2);
299 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
300 rib::FibUpdate::createAddUpdate("/test-self-register-unregister", 9527, 10));
301 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
302 rib::FibUpdate::createRemoveUpdate("/test-self-register-unregister", 9527));
Vince Lehman72446ec2014-07-09 10:50:02 -0500303}
304
Yanbiao Licf0db022016-01-29 00:54:25 -0800305BOOST_AUTO_TEST_CASE(Expiration)
Junxiao Shi0de23a22015-12-03 20:07:02 +0000306{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500307 auto paramsRegister = makeRegisterParameters("/test-expiry", 9527, 50_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800308 auto paramsUnregister = makeRegisterParameters("/test-expiry", 9527);
309 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000310
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500311 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000312 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 2); // the registered route has expired
313 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
314 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
315 BOOST_CHECK_EQUAL(m_fibUpdater.updates.back(),
316 rib::FibUpdate::createRemoveUpdate("/test-expiry", 9527));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000317
Junxiao Shidf1dc652019-08-30 19:03:19 +0000318 m_fibUpdater.updates.clear();
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500319 paramsRegister.setExpirationPeriod(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800320 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", paramsRegister));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000321
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500322 advanceClocks(55_ms);
Junxiao Shidf1dc652019-08-30 19:03:19 +0000323 BOOST_REQUIRE_EQUAL(m_fibUpdater.updates.size(), 1); // the registered route is still active
324 BOOST_CHECK_EQUAL(m_fibUpdater.updates.front(),
325 rib::FibUpdate::createAddUpdate("/test-expiry", 9527, 10));
Junxiao Shi0de23a22015-12-03 20:07:02 +0000326}
327
Junxiao Shi75306352018-02-01 21:59:44 +0000328BOOST_AUTO_TEST_CASE(NameTooLong)
329{
330 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400331 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi75306352018-02-01 21:59:44 +0000332 prefix.append("A");
333 }
334 auto params = makeRegisterParameters(prefix, 2899);
335 auto command = makeControlCommandRequest("/localhost/nfd/rib/register", params);
336 receiveInterest(command);
337
338 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400339 BOOST_CHECK_EQUAL(checkResponse(0, command.getName(),
340 ControlResponse(414, "Route prefix cannot exceed " +
341 to_string(Fib::getMaxDepth()) + " components")),
Junxiao Shi75306352018-02-01 21:59:44 +0000342 CheckResponseResult::OK);
343
Junxiao Shidf1dc652019-08-30 19:03:19 +0000344 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Junxiao Shi75306352018-02-01 21:59:44 +0000345}
346
Yanbiao Licf0db022016-01-29 00:54:25 -0800347BOOST_AUTO_TEST_SUITE_END() // RegisterUnregister
348
Yanbiao Licf0db022016-01-29 00:54:25 -0800349BOOST_FIXTURE_TEST_CASE(RibDataset, UnauthorizedRibManagerFixture)
Vince Lehman72446ec2014-07-09 10:50:02 -0500350{
Yanbiao Licf0db022016-01-29 00:54:25 -0800351 uint64_t faceId = 0;
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500352 auto generateRoute = [&faceId] () -> rib::Route {
353 rib::Route route;
Yanbiao Licf0db022016-01-29 00:54:25 -0800354 route.faceId = ++faceId;
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400355 route.cost = route.faceId * 10;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400356 route.expires = nullopt;
Yanbiao Licf0db022016-01-29 00:54:25 -0800357 return route;
358 };
Vince Lehman72446ec2014-07-09 10:50:02 -0500359
Yanbiao Licf0db022016-01-29 00:54:25 -0800360 const size_t nEntries = 108;
361 std::set<Name> actualPrefixes;
362 for (size_t i = 0; i < nEntries; ++i) {
363 Name prefix = Name("/test-dataset").appendNumber(i);
364 actualPrefixes.insert(prefix);
365 m_rib.insert(prefix, generateRoute());
366 if (i & 0x1) {
367 m_rib.insert(prefix, generateRoute());
368 m_rib.insert(prefix, generateRoute());
369 }
370 }
Vince Lehman72446ec2014-07-09 10:50:02 -0500371
Junxiao Shi9d727852019-05-14 13:44:22 -0600372 receiveInterest(*makeInterest("/localhost/nfd/rib/list", true));
Vince Lehman72446ec2014-07-09 10:50:02 -0500373
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400374 Block content = concatenateResponses();
375 content.parse();
Yanbiao Licf0db022016-01-29 00:54:25 -0800376 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
377
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400378 std::vector<ndn::nfd::RibEntry> receivedRecords, expectedRecords;
Yanbiao Licf0db022016-01-29 00:54:25 -0800379 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400380 ndn::nfd::RibEntry decodedEntry(content.elements()[idx]);
Yanbiao Licf0db022016-01-29 00:54:25 -0800381 receivedRecords.push_back(decodedEntry);
Yanbiao Licf0db022016-01-29 00:54:25 -0800382 actualPrefixes.erase(decodedEntry.getName());
383
384 auto matchedEntryIt = m_rib.find(decodedEntry.getName());
385 BOOST_REQUIRE(matchedEntryIt != m_rib.end());
386
387 auto matchedEntry = matchedEntryIt->second;
388 BOOST_REQUIRE(matchedEntry != nullptr);
389
Davide Pesaventod2ea2352017-03-12 20:38:09 -0400390 expectedRecords.emplace_back();
391 expectedRecords.back().setName(matchedEntry->getName());
392 for (const auto& route : matchedEntry->getRoutes()) {
393 expectedRecords.back().addRoute(ndn::nfd::Route()
394 .setFaceId(route.faceId)
395 .setOrigin(route.origin)
396 .setCost(route.cost)
397 .setFlags(route.flags));
Yanbiao Licf0db022016-01-29 00:54:25 -0800398 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800399 }
400
401 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Licf0db022016-01-29 00:54:25 -0800402 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
403 expectedRecords.begin(), expectedRecords.end());
Vince Lehman72446ec2014-07-09 10:50:02 -0500404}
405
Yanbiao Licf0db022016-01-29 00:54:25 -0800406BOOST_FIXTURE_TEST_SUITE(FaceMonitor, LocalhostAuthorizedRibManagerFixture)
407
408BOOST_AUTO_TEST_CASE(FetchActiveFacesEvent)
Vince Lehmancd16c832014-07-23 15:14:55 -0700409{
Junxiao Shidf1dc652019-08-30 19:03:19 +0000410 BOOST_CHECK_EQUAL(m_fibUpdater.updates.size(), 0);
Vince Lehman76c751c2014-11-18 17:36:38 -0600411
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500412 advanceClocks(301_s); // RibManager::ACTIVE_FACE_FETCH_INTERVAL = 300s
Junxiao Shidf1dc652019-08-30 19:03:19 +0000413 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 2);
414 BOOST_CHECK_EQUAL(m_face.sentInterests[0].getName(), "/localhost/nfd/faces/events");
415 BOOST_CHECK_EQUAL(m_face.sentInterests[1].getName(), "/localhost/nfd/faces/list");
Vince Lehmancd16c832014-07-23 15:14:55 -0700416}
417
Yanbiao Licf0db022016-01-29 00:54:25 -0800418BOOST_AUTO_TEST_CASE(RemoveInvalidFaces)
Vince Lehman281ded72014-08-21 12:17:08 -0500419{
Yanbiao Licf0db022016-01-29 00:54:25 -0800420 auto parameters1 = makeRegisterParameters("/test-remove-invalid-faces-1");
421 auto parameters2 = makeRegisterParameters("/test-remove-invalid-faces-2");
422 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(1)));
423 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1.setFaceId(2)));
424 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2.setFaceId(2)));
425 BOOST_REQUIRE_EQUAL(m_rib.size(), 3);
Vince Lehman281ded72014-08-21 12:17:08 -0500426
Vince Lehman26b215c2014-08-17 15:00:41 -0500427 ndn::nfd::FaceStatus status;
428 status.setFaceId(1);
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700429 std::vector<ndn::nfd::FaceStatus> activeFaces;
430 activeFaces.push_back(status);
Vince Lehman26b215c2014-08-17 15:00:41 -0500431
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700432 m_manager.removeInvalidFaces(activeFaces);
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500433 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800434 BOOST_REQUIRE_EQUAL(m_rib.size(), 1);
Vince Lehman26b215c2014-08-17 15:00:41 -0500435
Yanbiao Licf0db022016-01-29 00:54:25 -0800436 auto it1 = m_rib.find("/test-remove-invalid-faces-1");
437 auto it2 = m_rib.find("/test-remove-invalid-faces-2");
438 BOOST_CHECK(it2 == m_rib.end());
439 BOOST_REQUIRE(it1 != m_rib.end());
440 BOOST_CHECK(it1->second->hasFaceId(1));
441 BOOST_CHECK(!it1->second->hasFaceId(2));
Vince Lehman26b215c2014-08-17 15:00:41 -0500442}
443
Yanbiao Licf0db022016-01-29 00:54:25 -0800444BOOST_AUTO_TEST_CASE(OnNotification)
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600445{
Yanbiao Licf0db022016-01-29 00:54:25 -0800446 auto parameters1 = makeRegisterParameters("/test-face-event-notification-1", 1);
447 auto parameters2 = makeRegisterParameters("/test-face-event-notification-2", 1);
448 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters1));
449 receiveInterest(makeControlCommandRequest("/localhost/nfd/rib/register", parameters2));
450 BOOST_REQUIRE_EQUAL(m_rib.size(), 2);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600451
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500452 auto makeNotification = [] (ndn::nfd::FaceEventKind kind, uint64_t faceId) {
453 return ndn::nfd::FaceEventNotification().setKind(kind).setFaceId(faceId);
Yanbiao Licf0db022016-01-29 00:54:25 -0800454 };
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600455
Yanbiao Licf0db022016-01-29 00:54:25 -0800456 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_DESTROYED, 1));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500457 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800458 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600459
Yanbiao Licf0db022016-01-29 00:54:25 -0800460 m_manager.onNotification(makeNotification(ndn::nfd::FaceEventKind::FACE_EVENT_CREATED, 2));
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500461 advanceClocks(100_ms);
Yanbiao Licf0db022016-01-29 00:54:25 -0800462 BOOST_CHECK_EQUAL(m_rib.size(), 0);
Vince Lehman7c7d33a2015-01-20 17:40:26 -0600463}
464
Yanbiao Licf0db022016-01-29 00:54:25 -0800465BOOST_AUTO_TEST_SUITE_END() // FaceMonitor
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500466
Yanbiao Licf0db022016-01-29 00:54:25 -0800467BOOST_AUTO_TEST_SUITE_END() // TestRibManager
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500468BOOST_AUTO_TEST_SUITE_END() // Mgmt
Vince Lehman72446ec2014-07-09 10:50:02 -0500469
470} // namespace tests
Vince Lehman72446ec2014-07-09 10:50:02 -0500471} // namespace nfd