blob: 9cab78f07446ba0455165ad05b142a634bdc5711 [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 Pesavento21e24f92025-01-10 22:22:43 -05003 * Copyright (c) 2014-2025, 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 Pesavento972de802024-02-16 18:42:55 -050027#include "table/fib-entry.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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Yanbiao Li711c7932015-08-19 16:30:16 -070036
Davide Pesavento78ddcab2019-02-28 22:00:03 -050037class FibManagerFixture : public ManagerFixtureWithAuthenticator
Yanbiao Li711c7932015-08-19 16:30:16 -070038{
39public:
40 FibManagerFixture()
41 : m_fib(m_forwarder.getFib())
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000042 , m_manager(m_fib, m_faceTable, m_dispatcher, *m_authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070043 {
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000044 setTopPrefix();
Yanbiao Lidf846e52016-01-30 21:53:47 -080045 setPrivilege("fib");
Yanbiao Li711c7932015-08-19 16:30:16 -070046 }
47
48public: // for test
Davide Pesavento1586aff2017-02-19 23:17:51 -050049 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070050 makeParameters(const Name& name, const FaceId& id)
51 {
52 return ControlParameters().setName(name).setFaceId(id);
53 }
54
Davide Pesavento1586aff2017-02-19 23:17:51 -050055 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070056 makeParameters(const Name& name, const FaceId& id, const uint32_t& cost)
57 {
58 return ControlParameters().setName(name).setFaceId(id).setCost(cost);
59 }
60
61 FaceId
62 addFace()
63 {
64 auto face = make_shared<DummyFace>();
65 m_faceTable.add(face);
Davide Pesavento14e71f02019-03-28 17:35:25 -040066 advanceClocks(1_ms, 10);
Yanbiao Li711c7932015-08-19 16:30:16 -070067 m_responses.clear(); // clear all event notifications, if any
68 return face->getId();
69 }
70
71public: // for check
72 enum class CheckNextHopResult
73 {
74 OK,
75 NO_FIB_ENTRY,
76 WRONG_N_NEXTHOPS,
77 NO_NEXTHOP,
78 WRONG_COST
Davide Pesavento1586aff2017-02-19 23:17:51 -050079 };
Yanbiao Li711c7932015-08-19 16:30:16 -070080
81 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040082 * @brief Check whether the nexthop record is added / removed properly.
Yanbiao Li711c7932015-08-19 16:30:16 -070083 *
Davide Pesavento1586aff2017-02-19 23:17:51 -050084 * @param expectedNNextHops use nullopt to skip this check
85 * @param faceId use nullopt to skip NextHopRecord checks
86 * @param expectedCost use nullopt to skip this check
Yanbiao Li711c7932015-08-19 16:30:16 -070087 *
88 * @retval OK FIB entry is found by exact match and has the expected number of nexthops;
89 * NextHopRe record for faceId is found and has the expected cost
90 * @retval NO_FIB_ENTRY FIB entry is not found
91 * @retval WRONG_N_NEXTHOPS FIB entry is found but has wrong number of nexthops
92 * @retval NO_NEXTHOP NextHopRecord for faceId is not found
93 * @retval WRONG_COST NextHopRecord for faceId has wrong cost
94 */
95 CheckNextHopResult
Davide Pesavento1586aff2017-02-19 23:17:51 -050096 checkNextHop(const Name& prefix,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040097 std::optional<size_t> expectedNNextHops = std::nullopt,
98 std::optional<FaceId> faceId = std::nullopt,
99 std::optional<uint64_t> expectedCost = std::nullopt) const
Yanbiao Li711c7932015-08-19 16:30:16 -0700100 {
Junxiao Shia6de4292016-07-12 02:08:10 +0000101 const fib::Entry* entry = m_fib.findExactMatch(prefix);
102 if (entry == nullptr) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700103 return CheckNextHopResult::NO_FIB_ENTRY;
104 }
105
Davide Pesavento1586aff2017-02-19 23:17:51 -0500106 const auto& nextHops = entry->getNextHops();
107 if (expectedNNextHops && nextHops.size() != *expectedNNextHops) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700108 return CheckNextHopResult::WRONG_N_NEXTHOPS;
109 }
110
Davide Pesavento1586aff2017-02-19 23:17:51 -0500111 if (faceId) {
Davide Pesavento97210d52016-10-14 15:45:48 +0200112 for (const auto& record : nextHops) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500113 if (record.getFace().getId() == *faceId) {
114 if (expectedCost && record.getCost() != *expectedCost)
Davide Pesavento97210d52016-10-14 15:45:48 +0200115 return CheckNextHopResult::WRONG_COST;
116 else
117 return CheckNextHopResult::OK;
Yanbiao Li711c7932015-08-19 16:30:16 -0700118 }
119 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700120 return CheckNextHopResult::NO_NEXTHOP;
121 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700122 return CheckNextHopResult::OK;
123 }
124
125protected:
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500126 static inline const Name ADD_NH_REQUEST = Name("/localhost/nfd")
127 .append(ndn::nfd::FibAddNextHopCommand::getName());
128 static inline const Name REMOVE_NH_REQUEST = Name("/localhost/nfd")
129 .append(ndn::nfd::FibRemoveNextHopCommand::getName());
130
Yanbiao Li711c7932015-08-19 16:30:16 -0700131 Fib& m_fib;
Yanbiao Li711c7932015-08-19 16:30:16 -0700132 FibManager m_manager;
133};
134
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400135static std::ostream&
Davide Pesavento1586aff2017-02-19 23:17:51 -0500136operator<<(std::ostream& os, FibManagerFixture::CheckNextHopResult result)
Yanbiao Li711c7932015-08-19 16:30:16 -0700137{
138 switch (result) {
139 case FibManagerFixture::CheckNextHopResult::OK:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500140 return os << "OK";
Yanbiao Li711c7932015-08-19 16:30:16 -0700141 case FibManagerFixture::CheckNextHopResult::NO_FIB_ENTRY:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500142 return os << "NO_FIB_ENTRY";
Yanbiao Li711c7932015-08-19 16:30:16 -0700143 case FibManagerFixture::CheckNextHopResult::WRONG_N_NEXTHOPS:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500144 return os << "WRONG_N_NEXTHOPS";
Yanbiao Li711c7932015-08-19 16:30:16 -0700145 case FibManagerFixture::CheckNextHopResult::NO_NEXTHOP:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500146 return os << "NO_NEXTHOP";
Yanbiao Li711c7932015-08-19 16:30:16 -0700147 case FibManagerFixture::CheckNextHopResult::WRONG_COST:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500148 return os << "WRONG_COST";
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400149 }
Davide Pesavento4296fe32025-01-14 23:00:41 -0500150 return os << ndn::to_underlying(result);
Yanbiao Li711c7932015-08-19 16:30:16 -0700151}
152
Davide Pesavento97210d52016-10-14 15:45:48 +0200153BOOST_AUTO_TEST_SUITE(Mgmt)
154BOOST_FIXTURE_TEST_SUITE(TestFibManager, FibManagerFixture)
Yanbiao Li711c7932015-08-19 16:30:16 -0700155
156BOOST_AUTO_TEST_SUITE(AddNextHop)
157
158BOOST_AUTO_TEST_CASE(UnknownFaceId)
159{
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500160 auto req = makeControlCommandRequest(ADD_NH_REQUEST, makeParameters("hello", face::FACEID_NULL, 101));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000161 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700162 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
163
164 // check response
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000165 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), ControlResponse(410, "Face not found")),
Yanbiao Li711c7932015-08-19 16:30:16 -0700166 CheckResponseResult::OK);
167
168 // double check that the next hop was not added
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400169 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, std::nullopt, 101),
170 CheckNextHopResult::NO_FIB_ENTRY);
Yanbiao Li711c7932015-08-19 16:30:16 -0700171}
172
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000173BOOST_AUTO_TEST_CASE(NameTooLong)
174{
175 Name prefix;
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400176 while (prefix.size() <= Fib::getMaxDepth()) {
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000177 prefix.append("A");
178 }
179
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500180 auto req = makeControlCommandRequest(ADD_NH_REQUEST, makeParameters(prefix, addFace()));
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000181 receiveInterest(req);
182
183 ControlResponse expected(414, "FIB entry prefix cannot exceed " +
Davide Pesavento40641272023-03-16 13:31:12 -0400184 std::to_string(Fib::getMaxDepth()) + " components");
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000185 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expected), CheckResponseResult::OK);
186
187 BOOST_CHECK_EQUAL(checkNextHop(prefix), CheckNextHopResult::NO_FIB_ENTRY);
188}
189
Yanbiao Li711c7932015-08-19 16:30:16 -0700190BOOST_AUTO_TEST_CASE(ImplicitFaceId)
191{
192 auto face1 = addFace();
193 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700194 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
195 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700196
197 Name expectedName;
198 ControlResponse expectedResponse;
199 auto testAddNextHop = [&] (ControlParameters parameters, const FaceId& faceId) {
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500200 auto req = makeControlCommandRequest(ADD_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000201 req.setTag(make_shared<lp::IncomingFaceIdTag>(faceId));
Yanbiao Li711c7932015-08-19 16:30:16 -0700202 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000203 expectedName = req.getName();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500204 expectedResponse = makeResponse(200, "OK", parameters.setFaceId(faceId));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000205 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700206 };
207
208 testAddNextHop(ControlParameters().setName("/hello").setCost(100).setFaceId(0), face1);
209 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
210 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
211 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 100), CheckNextHopResult::OK);
212
213 testAddNextHop(ControlParameters().setName("/hello").setCost(100), face2);
214 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
215 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
216 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face2, 100), CheckNextHopResult::OK);
217}
218
219BOOST_AUTO_TEST_CASE(InitialAdd)
220{
221 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700222 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700223
224 auto parameters = makeParameters("hello", addedFaceId, 101);
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500225 auto req = makeControlCommandRequest(ADD_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000226 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700227
Yanbiao Li711c7932015-08-19 16:30:16 -0700228 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500229 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "OK", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700230 CheckResponseResult::OK);
231 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 101), CheckNextHopResult::OK);
232}
233
234BOOST_AUTO_TEST_CASE(ImplicitCost)
235{
236 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700237 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700238
239 auto originalParameters = ControlParameters().setName("/hello").setFaceId(addedFaceId);
240 auto parameters = makeParameters("/hello", addedFaceId, 0);
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500241 auto req = makeControlCommandRequest(ADD_NH_REQUEST, originalParameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000242 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700243
Yanbiao Li711c7932015-08-19 16:30:16 -0700244 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Davide Pesavento21e24f92025-01-10 22:22:43 -0500245 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "OK", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700246 CheckResponseResult::OK);
247 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 0), CheckNextHopResult::OK);
248}
249
250BOOST_AUTO_TEST_CASE(AddToExisting)
251{
252 FaceId face = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700253 BOOST_REQUIRE_NE(face, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700254
255 Name expectedName;
256 ControlResponse expectedResponse;
257 auto testAddNextHop = [&] (const ControlParameters& parameters) {
258 m_responses.clear();
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500259 auto req = makeControlCommandRequest(ADD_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000260 expectedName = req.getName();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500261 expectedResponse = makeResponse(200, "OK", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000262 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700263 };
264
265 // add initial, succeeds
266 testAddNextHop(makeParameters("/hello", face, 101));
267 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
268 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
269
270 // add to existing --> update cost, succeeds
271 testAddNextHop(makeParameters("/hello", face, 102));
272 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
273 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
274
275 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face, 102), CheckNextHopResult::WRONG_N_NEXTHOPS);
276 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 101), CheckNextHopResult::WRONG_COST);
277 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 102), CheckNextHopResult::OK);
278}
279
280BOOST_AUTO_TEST_SUITE_END() // AddNextHop
281
282BOOST_AUTO_TEST_SUITE(RemoveNextHop)
283
284BOOST_AUTO_TEST_CASE(Basic)
285{
286 Name expectedName;
287 ControlResponse expectedResponse;
288 auto testRemoveNextHop = [&] (const ControlParameters& parameters) {
289 m_responses.clear();
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500290 auto req = makeControlCommandRequest(REMOVE_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000291 expectedName = req.getName();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500292 expectedResponse = makeResponse(200, "OK", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000293 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700294 };
295
296 FaceId face1 = addFace();
297 FaceId face2 = addFace();
298 FaceId face3 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700299 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
300 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
301 BOOST_REQUIRE_NE(face3, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700302
Junxiao Shia6de4292016-07-12 02:08:10 +0000303 fib::Entry* entry = m_fib.insert("/hello").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000304 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
305 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face2), 202);
306 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face3), 303);
Yanbiao Li711c7932015-08-19 16:30:16 -0700307
308 testRemoveNextHop(makeParameters("/hello", face1));
309 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
310 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
311 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face1, 101), CheckNextHopResult::NO_NEXTHOP);
312
313 testRemoveNextHop(makeParameters("/hello", face2));
314 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
315 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
316 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face2, 202), CheckNextHopResult::NO_NEXTHOP);
317
318 testRemoveNextHop(makeParameters("/hello", face3));
319 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
320 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
321 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face3, 303), CheckNextHopResult::NO_FIB_ENTRY);
322}
323
324BOOST_AUTO_TEST_CASE(PrefixNotFound)
325{
326 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700327 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700328
329 auto parameters = makeParameters("hello", addedFaceId);
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500330 auto req = makeControlCommandRequest(REMOVE_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000331 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700332 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000333
Davide Pesavento21e24f92025-01-10 22:22:43 -0500334 auto expectedResponse = makeResponse(200, "OK", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000335 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResponse), CheckResponseResult::OK);
Yanbiao Li711c7932015-08-19 16:30:16 -0700336}
337
338BOOST_AUTO_TEST_CASE(ImplicitFaceId)
339{
340 auto face1 = addFace();
341 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700342 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
343 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700344
345 Name expectedName;
346 ControlResponse expectedResponse;
347 auto testWithImplicitFaceId = [&] (ControlParameters parameters, FaceId face) {
348 m_responses.clear();
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500349 auto req = makeControlCommandRequest(REMOVE_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000350 req.setTag(make_shared<lp::IncomingFaceIdTag>(face));
351 expectedName = req.getName();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500352 expectedResponse = makeResponse(200, "OK", parameters.setFaceId(face));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000353 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700354 };
355
Junxiao Shia6de4292016-07-12 02:08:10 +0000356 fib::Entry* entry = m_fib.insert("/hello").first;
Ju Pand8315bf2019-07-31 06:59:07 +0000357 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
358 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face2), 202);
Yanbiao Li711c7932015-08-19 16:30:16 -0700359
360 testWithImplicitFaceId(ControlParameters().setName("/hello").setFaceId(0), face1);
361 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
362 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
363 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 101), CheckNextHopResult::NO_NEXTHOP);
364
365 testWithImplicitFaceId(ControlParameters().setName("/hello"), face2);
366 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
367 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
368 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face2, 202), CheckNextHopResult::NO_FIB_ENTRY);
369}
370
371BOOST_AUTO_TEST_CASE(RecordNotExist)
372{
373 auto face1 = addFace();
374 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700375 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
376 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700377
378 Name expectedName;
379 ControlResponse expectedResponse;
380 auto testRemoveNextHop = [&] (ControlParameters parameters) {
381 m_responses.clear();
Davide Pesavento1d2d3372025-01-14 12:04:12 -0500382 auto req = makeControlCommandRequest(REMOVE_NH_REQUEST, parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000383 expectedName = req.getName();
Davide Pesavento21e24f92025-01-10 22:22:43 -0500384 expectedResponse = makeResponse(200, "OK", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000385 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700386 };
387
Ju Pand8315bf2019-07-31 06:59:07 +0000388 fib::Entry* entry = m_fib.insert("/hello").first;
389 m_fib.addOrUpdateNextHop(*entry, *m_faceTable.get(face1), 101);
Yanbiao Li711c7932015-08-19 16:30:16 -0700390
391 testRemoveNextHop(makeParameters("/hello", face2 + 100));
392 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist
393 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400394 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700395
396 testRemoveNextHop(makeParameters("/hello", face2));
397 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist
398 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400399 BOOST_CHECK_EQUAL(checkNextHop("/hello", std::nullopt, face2), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700400}
401
402BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop
403
Davide Pesavento97210d52016-10-14 15:45:48 +0200404BOOST_AUTO_TEST_SUITE(List)
405
Yanbiao Li711c7932015-08-19 16:30:16 -0700406BOOST_AUTO_TEST_CASE(FibDataset)
407{
408 const size_t nEntries = 108;
409 std::set<Name> actualPrefixes;
410 for (size_t i = 0 ; i < nEntries ; i ++) {
411 Name prefix = Name("test").appendSegment(i);
412 actualPrefixes.insert(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000413 fib::Entry* fibEntry = m_fib.insert(prefix).first;
Ju Pand8315bf2019-07-31 06:59:07 +0000414 m_fib.addOrUpdateNextHop(*fibEntry, *m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 1);
415 m_fib.addOrUpdateNextHop(*fibEntry, *m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 2);
Yanbiao Li711c7932015-08-19 16:30:16 -0700416 }
417
Davide Pesavento28181322018-11-08 16:44:50 -0500418 receiveInterest(Interest("/localhost/nfd/fib/list").setCanBePrefix(true));
Yanbiao Li711c7932015-08-19 16:30:16 -0700419
Davide Pesavento1586aff2017-02-19 23:17:51 -0500420 Block content = concatenateResponses();
421 content.parse();
Yanbiao Li711c7932015-08-19 16:30:16 -0700422 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
423
Davide Pesavento1586aff2017-02-19 23:17:51 -0500424 std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords;
Davide Pesavento4296fe32025-01-14 23:00:41 -0500425 for (const auto& el : content.elements()) {
426 ndn::nfd::FibEntry decodedEntry(el);
Davide Pesaventod7083a52023-10-19 17:51:16 -0400427 BOOST_TEST_INFO_SCOPE(decodedEntry);
Yanbiao Li711c7932015-08-19 16:30:16 -0700428 receivedRecords.push_back(decodedEntry);
Yanbiao Li711c7932015-08-19 16:30:16 -0700429 actualPrefixes.erase(decodedEntry.getPrefix());
430
431 auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix());
Davide Pesavento4296fe32025-01-14 23:00:41 -0500432 BOOST_TEST_REQUIRE(matchedEntry != nullptr);
Yanbiao Li711c7932015-08-19 16:30:16 -0700433
Davide Pesavento1586aff2017-02-19 23:17:51 -0500434 expectedRecords.emplace_back();
435 expectedRecords.back().setPrefix(matchedEntry->getPrefix());
436 for (const auto& nh : matchedEntry->getNextHops()) {
437 expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord()
438 .setFaceId(nh.getFace().getId())
439 .setCost(nh.getCost()));
Yanbiao Li711c7932015-08-19 16:30:16 -0700440 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700441 }
442
443 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400444 BOOST_TEST(receivedRecords == expectedRecords, boost::test_tools::per_element());
Yanbiao Li711c7932015-08-19 16:30:16 -0700445}
446
Davide Pesavento97210d52016-10-14 15:45:48 +0200447BOOST_AUTO_TEST_SUITE_END() // List
448
Yanbiao Li711c7932015-08-19 16:30:16 -0700449BOOST_AUTO_TEST_SUITE_END() // TestFibManager
450BOOST_AUTO_TEST_SUITE_END() // Mgmt
451
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400452} // namespace nfd::tests