blob: 544eda5d422bff75966ed8507f88fef92f73594d [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi02a0e0f2018-02-06 02:03:45 +00002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Yanbiao Li711c7932015-08-19 16:30:16 -07004 * 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.
10 *
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 */
Davide Pesavento97210d52016-10-14 15:45:48 +020025
Yanbiao Li711c7932015-08-19 16:30:16 -070026#include "mgmt/fib-manager.hpp"
Davide Pesavento97210d52016-10-14 15:45:48 +020027#include "table/fib-nexthop.hpp"
Yanbiao Li711c7932015-08-19 16:30:16 -070028
Davide Pesavento78ddcab2019-02-28 22:00:03 -050029#include "manager-common-fixture.hpp"
30#include "tests/daemon/face/dummy-face.hpp"
Davide Pesavento97210d52016-10-14 15:45:48 +020031
Junxiao Shicbc8e942016-09-06 03:17:45 +000032#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
Yanbiao Li711c7932015-08-19 16:30:16 -070034
35namespace nfd {
36namespace tests {
37
Davide Pesavento78ddcab2019-02-28 22:00:03 -050038class FibManagerFixture : public ManagerFixtureWithAuthenticator
Yanbiao Li711c7932015-08-19 16:30:16 -070039{
40public:
41 FibManagerFixture()
42 : m_fib(m_forwarder.getFib())
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000043 , m_manager(m_fib, m_faceTable, m_dispatcher, *m_authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070044 {
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000045 setTopPrefix();
Yanbiao Lidf846e52016-01-30 21:53:47 -080046 setPrivilege("fib");
Yanbiao Li711c7932015-08-19 16:30:16 -070047 }
48
49public: // for test
Davide Pesavento1586aff2017-02-19 23:17:51 -050050 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070051 makeParameters(const Name& name, const FaceId& id)
52 {
53 return ControlParameters().setName(name).setFaceId(id);
54 }
55
Davide Pesavento1586aff2017-02-19 23:17:51 -050056 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070057 makeParameters(const Name& name, const FaceId& id, const uint32_t& cost)
58 {
59 return ControlParameters().setName(name).setFaceId(id).setCost(cost);
60 }
61
62 FaceId
63 addFace()
64 {
65 auto face = make_shared<DummyFace>();
66 m_faceTable.add(face);
Davide Pesavento14e71f02019-03-28 17:35:25 -040067 advanceClocks(1_ms, 10);
Yanbiao Li711c7932015-08-19 16:30:16 -070068 m_responses.clear(); // clear all event notifications, if any
69 return face->getId();
70 }
71
72public: // for check
73 enum class CheckNextHopResult
74 {
75 OK,
76 NO_FIB_ENTRY,
77 WRONG_N_NEXTHOPS,
78 NO_NEXTHOP,
79 WRONG_COST
Davide Pesavento1586aff2017-02-19 23:17:51 -050080 };
Yanbiao Li711c7932015-08-19 16:30:16 -070081
82 /**
83 * @brief check whether the nexthop record is added / removed properly
84 *
Davide Pesavento1586aff2017-02-19 23:17:51 -050085 * @param expectedNNextHops use nullopt to skip this check
86 * @param faceId use nullopt to skip NextHopRecord checks
87 * @param expectedCost use nullopt to skip this check
Yanbiao Li711c7932015-08-19 16:30:16 -070088 *
89 * @retval OK FIB entry is found by exact match and has the expected number of nexthops;
90 * NextHopRe record for faceId is found and has the expected cost
91 * @retval NO_FIB_ENTRY FIB entry is not found
92 * @retval WRONG_N_NEXTHOPS FIB entry is found but has wrong number of nexthops
93 * @retval NO_NEXTHOP NextHopRecord for faceId is not found
94 * @retval WRONG_COST NextHopRecord for faceId has wrong cost
95 */
96 CheckNextHopResult
Davide Pesavento1586aff2017-02-19 23:17:51 -050097 checkNextHop(const Name& prefix,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040098 std::optional<size_t> expectedNNextHops = std::nullopt,
99 std::optional<FaceId> faceId = std::nullopt,
100 std::optional<uint64_t> expectedCost = std::nullopt) const
Yanbiao Li711c7932015-08-19 16:30:16 -0700101 {
Junxiao Shia6de4292016-07-12 02:08:10 +0000102 const fib::Entry* entry = m_fib.findExactMatch(prefix);
103 if (entry == nullptr) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700104 return CheckNextHopResult::NO_FIB_ENTRY;
105 }
106
Davide Pesavento1586aff2017-02-19 23:17:51 -0500107 const auto& nextHops = entry->getNextHops();
108 if (expectedNNextHops && nextHops.size() != *expectedNNextHops) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700109 return CheckNextHopResult::WRONG_N_NEXTHOPS;
110 }
111
Davide Pesavento1586aff2017-02-19 23:17:51 -0500112 if (faceId) {
Davide Pesavento97210d52016-10-14 15:45:48 +0200113 for (const auto& record : nextHops) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500114 if (record.getFace().getId() == *faceId) {
115 if (expectedCost && record.getCost() != *expectedCost)
Davide Pesavento97210d52016-10-14 15:45:48 +0200116 return CheckNextHopResult::WRONG_COST;
117 else
118 return CheckNextHopResult::OK;
Yanbiao Li711c7932015-08-19 16:30:16 -0700119 }
120 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700121 return CheckNextHopResult::NO_NEXTHOP;
122 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700123 return CheckNextHopResult::OK;
124 }
125
126protected:
127 Fib& m_fib;
Yanbiao Li711c7932015-08-19 16:30:16 -0700128 FibManager m_manager;
129};
130
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400131static std::ostream&
Davide Pesavento1586aff2017-02-19 23:17:51 -0500132operator<<(std::ostream& os, FibManagerFixture::CheckNextHopResult result)
Yanbiao Li711c7932015-08-19 16:30:16 -0700133{
134 switch (result) {
135 case FibManagerFixture::CheckNextHopResult::OK:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500136 return os << "OK";
Yanbiao Li711c7932015-08-19 16:30:16 -0700137 case FibManagerFixture::CheckNextHopResult::NO_FIB_ENTRY:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500138 return os << "NO_FIB_ENTRY";
Yanbiao Li711c7932015-08-19 16:30:16 -0700139 case FibManagerFixture::CheckNextHopResult::WRONG_N_NEXTHOPS:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500140 return os << "WRONG_N_NEXTHOPS";
Yanbiao Li711c7932015-08-19 16:30:16 -0700141 case FibManagerFixture::CheckNextHopResult::NO_NEXTHOP:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500142 return os << "NO_NEXTHOP";
Yanbiao Li711c7932015-08-19 16:30:16 -0700143 case FibManagerFixture::CheckNextHopResult::WRONG_COST:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500144 return os << "WRONG_COST";
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400145 }
Davide Pesavento1586aff2017-02-19 23:17:51 -0500146 return os << static_cast<int>(result);
Yanbiao Li711c7932015-08-19 16:30:16 -0700147}
148
Davide Pesavento97210d52016-10-14 15:45:48 +0200149BOOST_AUTO_TEST_SUITE(Mgmt)
150BOOST_FIXTURE_TEST_SUITE(TestFibManager, FibManagerFixture)
Yanbiao Li711c7932015-08-19 16:30:16 -0700151
152BOOST_AUTO_TEST_SUITE(AddNextHop)
153
154BOOST_AUTO_TEST_CASE(UnknownFaceId)
155{
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000156 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop",
157 makeParameters("hello", face::FACEID_NULL, 101));
158 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700159 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
160
161 // check response
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000162 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), ControlResponse(410, "Face not found")),
Yanbiao Li711c7932015-08-19 16:30:16 -0700163 CheckResponseResult::OK);
164
165 // double check that the next hop was not added
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400166 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, std::nullopt, 101),
167 CheckNextHopResult::NO_FIB_ENTRY);
Yanbiao Li711c7932015-08-19 16:30:16 -0700168}
169
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000170BOOST_AUTO_TEST_CASE(NameTooLong)
171{
172 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400173 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000174 prefix.append("A");
175 }
176
177 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop",
178 makeParameters(prefix, addFace()));
179 receiveInterest(req);
180
181 ControlResponse expected(414, "FIB entry prefix cannot exceed " +
182 ndn::to_string(Fib::getMaxDepth()) + " components");
183 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expected), CheckResponseResult::OK);
184
185 BOOST_CHECK_EQUAL(checkNextHop(prefix), CheckNextHopResult::NO_FIB_ENTRY);
186}
187
Yanbiao Li711c7932015-08-19 16:30:16 -0700188BOOST_AUTO_TEST_CASE(ImplicitFaceId)
189{
190 auto face1 = addFace();
191 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
193 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700194
195 Name expectedName;
196 ControlResponse expectedResponse;
197 auto testAddNextHop = [&] (ControlParameters parameters, const FaceId& faceId) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000198 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
199 req.setTag(make_shared<lp::IncomingFaceIdTag>(faceId));
Yanbiao Li711c7932015-08-19 16:30:16 -0700200 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000201 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700202 expectedResponse = makeResponse(200, "Success", parameters.setFaceId(faceId));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000203 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700204 };
205
206 testAddNextHop(ControlParameters().setName("/hello").setCost(100).setFaceId(0), face1);
207 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
208 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
209 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 100), CheckNextHopResult::OK);
210
211 testAddNextHop(ControlParameters().setName("/hello").setCost(100), face2);
212 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
213 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
214 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face2, 100), CheckNextHopResult::OK);
215}
216
217BOOST_AUTO_TEST_CASE(InitialAdd)
218{
219 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700220 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700221
222 auto parameters = makeParameters("hello", addedFaceId, 101);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000223 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
224 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700225
Yanbiao Li711c7932015-08-19 16:30:16 -0700226 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000227 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700228 CheckResponseResult::OK);
229 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 101), CheckNextHopResult::OK);
230}
231
232BOOST_AUTO_TEST_CASE(ImplicitCost)
233{
234 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700235 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700236
237 auto originalParameters = ControlParameters().setName("/hello").setFaceId(addedFaceId);
238 auto parameters = makeParameters("/hello", addedFaceId, 0);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000239 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", originalParameters);
240 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700241
Yanbiao Li711c7932015-08-19 16:30:16 -0700242 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000243 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700244 CheckResponseResult::OK);
245 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 0), CheckNextHopResult::OK);
246}
247
248BOOST_AUTO_TEST_CASE(AddToExisting)
249{
250 FaceId face = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700251 BOOST_REQUIRE_NE(face, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700252
253 Name expectedName;
254 ControlResponse expectedResponse;
255 auto testAddNextHop = [&] (const ControlParameters& parameters) {
256 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000257 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
258 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700259 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000260 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700261 };
262
263 // add initial, succeeds
264 testAddNextHop(makeParameters("/hello", face, 101));
265 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
266 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
267
268 // add to existing --> update cost, succeeds
269 testAddNextHop(makeParameters("/hello", face, 102));
270 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
271 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
272
273 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face, 102), CheckNextHopResult::WRONG_N_NEXTHOPS);
274 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 101), CheckNextHopResult::WRONG_COST);
275 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 102), CheckNextHopResult::OK);
276}
277
278BOOST_AUTO_TEST_SUITE_END() // AddNextHop
279
280BOOST_AUTO_TEST_SUITE(RemoveNextHop)
281
282BOOST_AUTO_TEST_CASE(Basic)
283{
284 Name expectedName;
285 ControlResponse expectedResponse;
286 auto testRemoveNextHop = [&] (const ControlParameters& parameters) {
287 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000288 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
289 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700290 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000291 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700292 };
293
294 FaceId face1 = addFace();
295 FaceId face2 = addFace();
296 FaceId face3 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700297 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
298 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
299 BOOST_REQUIRE_NE(face3, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700300
Junxiao Shia6de4292016-07-12 02:08:10 +0000301 fib::Entry* entry = m_fib.insert("/hello").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000302 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
303 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face2), 202);
304 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face3), 303);
Yanbiao Li711c7932015-08-19 16:30:16 -0700305
306 testRemoveNextHop(makeParameters("/hello", face1));
307 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
308 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
309 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face1, 101), CheckNextHopResult::NO_NEXTHOP);
310
311 testRemoveNextHop(makeParameters("/hello", face2));
312 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
313 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
314 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face2, 202), CheckNextHopResult::NO_NEXTHOP);
315
316 testRemoveNextHop(makeParameters("/hello", face3));
317 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
318 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
319 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face3, 303), CheckNextHopResult::NO_FIB_ENTRY);
320}
321
322BOOST_AUTO_TEST_CASE(PrefixNotFound)
323{
324 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700325 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700326
327 auto parameters = makeParameters("hello", addedFaceId);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000328 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
329 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700330 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000331
332 auto expectedResponse = makeResponse(200, "Success", parameters);
333 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResponse), CheckResponseResult::OK);
Yanbiao Li711c7932015-08-19 16:30:16 -0700334}
335
336BOOST_AUTO_TEST_CASE(ImplicitFaceId)
337{
338 auto face1 = addFace();
339 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700340 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
341 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700342
343 Name expectedName;
344 ControlResponse expectedResponse;
345 auto testWithImplicitFaceId = [&] (ControlParameters parameters, FaceId face) {
346 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000347 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
348 req.setTag(make_shared<lp::IncomingFaceIdTag>(face));
349 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700350 expectedResponse = makeResponse(200, "Success", parameters.setFaceId(face));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000351 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700352 };
353
Junxiao Shia6de4292016-07-12 02:08:10 +0000354 fib::Entry* entry = m_fib.insert("/hello").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000355 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
356 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face2), 202);
Yanbiao Li711c7932015-08-19 16:30:16 -0700357
358 testWithImplicitFaceId(ControlParameters().setName("/hello").setFaceId(0), face1);
359 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
360 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
361 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 101), CheckNextHopResult::NO_NEXTHOP);
362
363 testWithImplicitFaceId(ControlParameters().setName("/hello"), face2);
364 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
365 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
366 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face2, 202), CheckNextHopResult::NO_FIB_ENTRY);
367}
368
369BOOST_AUTO_TEST_CASE(RecordNotExist)
370{
371 auto face1 = addFace();
372 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700373 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
374 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700375
376 Name expectedName;
377 ControlResponse expectedResponse;
378 auto testRemoveNextHop = [&] (ControlParameters parameters) {
379 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000380 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
381 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700382 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000383 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700384 };
385
Ju Pand8315bf2019-07-31 06:59:07 +0000386 fib::Entry* entry = m_fib.insert("/hello").first;
387 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
Yanbiao Li711c7932015-08-19 16:30:16 -0700388
389 testRemoveNextHop(makeParameters("/hello", face2 + 100));
390 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist
391 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400392 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700393
394 testRemoveNextHop(makeParameters("/hello", face2));
395 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist
396 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400397 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, face2), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700398}
399
400BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop
401
Davide Pesavento97210d52016-10-14 15:45:48 +0200402BOOST_AUTO_TEST_SUITE(List)
403
Yanbiao Li711c7932015-08-19 16:30:16 -0700404BOOST_AUTO_TEST_CASE(FibDataset)
405{
406 const size_t nEntries = 108;
407 std::set<Name> actualPrefixes;
408 for (size_t i = 0 ; i < nEntries ; i ++) {
409 Name prefix = Name("test").appendSegment(i);
410 actualPrefixes.insert(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000411 fib::Entry* fibEntry = m_fib.insert(prefix).first;
Ju Pand8315bf2019-07-31 06:59:07 +0000412 m_fib.addOrUpdateNextHop(*fibEntry, *m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 1);
413 m_fib.addOrUpdateNextHop(*fibEntry, *m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 2);
Yanbiao Li711c7932015-08-19 16:30:16 -0700414 }
415
Davide Pesavento28181322018-11-08 16:44:50 -0500416 receiveInterest(Interest("/localhost/nfd/fib/list").setCanBePrefix(true));
Yanbiao Li711c7932015-08-19 16:30:16 -0700417
Davide Pesavento1586aff2017-02-19 23:17:51 -0500418 Block content = concatenateResponses();
419 content.parse();
Yanbiao Li711c7932015-08-19 16:30:16 -0700420 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
421
Davide Pesavento1586aff2017-02-19 23:17:51 -0500422 std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords;
Yanbiao Li711c7932015-08-19 16:30:16 -0700423 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500424 ndn::nfd::FibEntry decodedEntry(content.elements()[idx]);
Yanbiao Li711c7932015-08-19 16:30:16 -0700425 receivedRecords.push_back(decodedEntry);
Yanbiao Li711c7932015-08-19 16:30:16 -0700426 actualPrefixes.erase(decodedEntry.getPrefix());
427
428 auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix());
429 BOOST_REQUIRE(matchedEntry != nullptr);
430
Davide Pesavento1586aff2017-02-19 23:17:51 -0500431 expectedRecords.emplace_back();
432 expectedRecords.back().setPrefix(matchedEntry->getPrefix());
433 for (const auto& nh : matchedEntry->getNextHops()) {
434 expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord()
435 .setFaceId(nh.getFace().getId())
436 .setCost(nh.getCost()));
Yanbiao Li711c7932015-08-19 16:30:16 -0700437 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700438 }
439
440 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400441 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Yanbiao Li711c7932015-08-19 16:30:16 -0700442}
443
Davide Pesavento97210d52016-10-14 15:45:48 +0200444BOOST_AUTO_TEST_SUITE_END() // List
445
Yanbiao Li711c7932015-08-19 16:30:16 -0700446BOOST_AUTO_TEST_SUITE_END() // TestFibManager
447BOOST_AUTO_TEST_SUITE_END() // Mgmt
448
449} // namespace tests
450} // namespace nfd