blob: 7c86e280ee402782f595a98ba64591653f8c0cd6 [file] [log] [blame]
Yanbiao Li711c7932015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento1586aff2017-02-19 23:17:51 -05003 * Copyright (c) 2014-2017, 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
Yanbiao Lidf846e52016-01-30 21:53:47 -080029#include "nfd-manager-common-fixture.hpp"
Yanbiao Li711c7932015-08-19 16:30:16 -070030#include "../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 Pesavento1586aff2017-02-19 23:17:51 -050038using ndn::nullopt;
39
Yanbiao Lidf846e52016-01-30 21:53:47 -080040class FibManagerFixture : public NfdManagerCommonFixture
Yanbiao Li711c7932015-08-19 16:30:16 -070041{
42public:
43 FibManagerFixture()
44 : m_fib(m_forwarder.getFib())
45 , m_faceTable(m_forwarder.getFaceTable())
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000046 , m_manager(m_fib, m_faceTable, m_dispatcher, *m_authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070047 {
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000048 setTopPrefix();
Yanbiao Lidf846e52016-01-30 21:53:47 -080049 setPrivilege("fib");
Yanbiao Li711c7932015-08-19 16:30:16 -070050 }
51
52public: // for test
Davide Pesavento1586aff2017-02-19 23:17:51 -050053 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070054 makeParameters(const Name& name, const FaceId& id)
55 {
56 return ControlParameters().setName(name).setFaceId(id);
57 }
58
Davide Pesavento1586aff2017-02-19 23:17:51 -050059 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070060 makeParameters(const Name& name, const FaceId& id, const uint32_t& cost)
61 {
62 return ControlParameters().setName(name).setFaceId(id).setCost(cost);
63 }
64
65 FaceId
66 addFace()
67 {
68 auto face = make_shared<DummyFace>();
69 m_faceTable.add(face);
70 advanceClocks(time::milliseconds(1), 10);
71 m_responses.clear(); // clear all event notifications, if any
72 return face->getId();
73 }
74
75public: // for check
76 enum class CheckNextHopResult
77 {
78 OK,
79 NO_FIB_ENTRY,
80 WRONG_N_NEXTHOPS,
81 NO_NEXTHOP,
82 WRONG_COST
Davide Pesavento1586aff2017-02-19 23:17:51 -050083 };
Yanbiao Li711c7932015-08-19 16:30:16 -070084
85 /**
86 * @brief check whether the nexthop record is added / removed properly
87 *
Davide Pesavento1586aff2017-02-19 23:17:51 -050088 * @param expectedNNextHops use nullopt to skip this check
89 * @param faceId use nullopt to skip NextHopRecord checks
90 * @param expectedCost use nullopt to skip this check
Yanbiao Li711c7932015-08-19 16:30:16 -070091 *
92 * @retval OK FIB entry is found by exact match and has the expected number of nexthops;
93 * NextHopRe record for faceId is found and has the expected cost
94 * @retval NO_FIB_ENTRY FIB entry is not found
95 * @retval WRONG_N_NEXTHOPS FIB entry is found but has wrong number of nexthops
96 * @retval NO_NEXTHOP NextHopRecord for faceId is not found
97 * @retval WRONG_COST NextHopRecord for faceId has wrong cost
98 */
99 CheckNextHopResult
Davide Pesavento1586aff2017-02-19 23:17:51 -0500100 checkNextHop(const Name& prefix,
101 ndn::optional<size_t> expectedNNextHops = nullopt,
102 ndn::optional<FaceId> faceId = nullopt,
103 ndn::optional<uint64_t> expectedCost = nullopt) const
Yanbiao Li711c7932015-08-19 16:30:16 -0700104 {
Junxiao Shia6de4292016-07-12 02:08:10 +0000105 const fib::Entry* entry = m_fib.findExactMatch(prefix);
106 if (entry == nullptr) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700107 return CheckNextHopResult::NO_FIB_ENTRY;
108 }
109
Davide Pesavento1586aff2017-02-19 23:17:51 -0500110 const auto& nextHops = entry->getNextHops();
111 if (expectedNNextHops && nextHops.size() != *expectedNNextHops) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700112 return CheckNextHopResult::WRONG_N_NEXTHOPS;
113 }
114
Davide Pesavento1586aff2017-02-19 23:17:51 -0500115 if (faceId) {
Davide Pesavento97210d52016-10-14 15:45:48 +0200116 for (const auto& record : nextHops) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500117 if (record.getFace().getId() == *faceId) {
118 if (expectedCost && record.getCost() != *expectedCost)
Davide Pesavento97210d52016-10-14 15:45:48 +0200119 return CheckNextHopResult::WRONG_COST;
120 else
121 return CheckNextHopResult::OK;
Yanbiao Li711c7932015-08-19 16:30:16 -0700122 }
123 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700124 return CheckNextHopResult::NO_NEXTHOP;
125 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700126 return CheckNextHopResult::OK;
127 }
128
129protected:
130 Fib& m_fib;
131 FaceTable& m_faceTable;
132 FibManager m_manager;
133};
134
135std::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";
Yanbiao Li711c7932015-08-19 16:30:16 -0700149 };
150
Davide Pesavento1586aff2017-02-19 23:17:51 -0500151 return os << static_cast<int>(result);
Yanbiao Li711c7932015-08-19 16:30:16 -0700152}
153
Davide Pesavento97210d52016-10-14 15:45:48 +0200154BOOST_AUTO_TEST_SUITE(Mgmt)
155BOOST_FIXTURE_TEST_SUITE(TestFibManager, FibManagerFixture)
Yanbiao Li711c7932015-08-19 16:30:16 -0700156
157BOOST_AUTO_TEST_SUITE(AddNextHop)
158
159BOOST_AUTO_TEST_CASE(UnknownFaceId)
160{
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000161 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop",
162 makeParameters("hello", face::FACEID_NULL, 101));
163 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700164 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
165
166 // check response
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000167 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), ControlResponse(410, "Face not found")),
Yanbiao Li711c7932015-08-19 16:30:16 -0700168 CheckResponseResult::OK);
169
170 // double check that the next hop was not added
Davide Pesavento1586aff2017-02-19 23:17:51 -0500171 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, nullopt, 101), CheckNextHopResult::NO_FIB_ENTRY);
Yanbiao Li711c7932015-08-19 16:30:16 -0700172}
173
174BOOST_AUTO_TEST_CASE(ImplicitFaceId)
175{
176 auto face1 = addFace();
177 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
179 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700180
181 Name expectedName;
182 ControlResponse expectedResponse;
183 auto testAddNextHop = [&] (ControlParameters parameters, const FaceId& faceId) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000184 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
185 req.setTag(make_shared<lp::IncomingFaceIdTag>(faceId));
Yanbiao Li711c7932015-08-19 16:30:16 -0700186 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000187 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700188 expectedResponse = makeResponse(200, "Success", parameters.setFaceId(faceId));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000189 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700190 };
191
192 testAddNextHop(ControlParameters().setName("/hello").setCost(100).setFaceId(0), face1);
193 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
194 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
195 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 100), CheckNextHopResult::OK);
196
197 testAddNextHop(ControlParameters().setName("/hello").setCost(100), face2);
198 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
199 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
200 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face2, 100), CheckNextHopResult::OK);
201}
202
203BOOST_AUTO_TEST_CASE(InitialAdd)
204{
205 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700206 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700207
208 auto parameters = makeParameters("hello", addedFaceId, 101);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000209 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
210 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700211
Yanbiao Li711c7932015-08-19 16:30:16 -0700212 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000213 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700214 CheckResponseResult::OK);
215 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 101), CheckNextHopResult::OK);
216}
217
218BOOST_AUTO_TEST_CASE(ImplicitCost)
219{
220 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700221 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700222
223 auto originalParameters = ControlParameters().setName("/hello").setFaceId(addedFaceId);
224 auto parameters = makeParameters("/hello", addedFaceId, 0);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000225 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", originalParameters);
226 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);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000229 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", parameters)),
Yanbiao Li711c7932015-08-19 16:30:16 -0700230 CheckResponseResult::OK);
231 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, addedFaceId, 0), CheckNextHopResult::OK);
232}
233
234BOOST_AUTO_TEST_CASE(AddToExisting)
235{
236 FaceId face = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700237 BOOST_REQUIRE_NE(face, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700238
239 Name expectedName;
240 ControlResponse expectedResponse;
241 auto testAddNextHop = [&] (const ControlParameters& parameters) {
242 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000243 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
244 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700245 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000246 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700247 };
248
249 // add initial, succeeds
250 testAddNextHop(makeParameters("/hello", face, 101));
251 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
252 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
253
254 // add to existing --> update cost, succeeds
255 testAddNextHop(makeParameters("/hello", face, 102));
256 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
257 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
258
259 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face, 102), CheckNextHopResult::WRONG_N_NEXTHOPS);
260 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 101), CheckNextHopResult::WRONG_COST);
261 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face, 102), CheckNextHopResult::OK);
262}
263
264BOOST_AUTO_TEST_SUITE_END() // AddNextHop
265
266BOOST_AUTO_TEST_SUITE(RemoveNextHop)
267
268BOOST_AUTO_TEST_CASE(Basic)
269{
270 Name expectedName;
271 ControlResponse expectedResponse;
272 auto testRemoveNextHop = [&] (const ControlParameters& parameters) {
273 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000274 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
275 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700276 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000277 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700278 };
279
280 FaceId face1 = addFace();
281 FaceId face2 = addFace();
282 FaceId face3 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700283 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
284 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
285 BOOST_REQUIRE_NE(face3, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700286
Junxiao Shia6de4292016-07-12 02:08:10 +0000287 fib::Entry* entry = m_fib.insert("/hello").first;
288 entry->addNextHop(*m_faceTable.get(face1), 101);
289 entry->addNextHop(*m_faceTable.get(face2), 202);
290 entry->addNextHop(*m_faceTable.get(face3), 303);
Yanbiao Li711c7932015-08-19 16:30:16 -0700291
292 testRemoveNextHop(makeParameters("/hello", face1));
293 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
294 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
295 BOOST_CHECK_EQUAL(checkNextHop("/hello", 2, face1, 101), CheckNextHopResult::NO_NEXTHOP);
296
297 testRemoveNextHop(makeParameters("/hello", face2));
298 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
299 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
300 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face2, 202), CheckNextHopResult::NO_NEXTHOP);
301
302 testRemoveNextHop(makeParameters("/hello", face3));
303 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
304 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
305 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face3, 303), CheckNextHopResult::NO_FIB_ENTRY);
306}
307
308BOOST_AUTO_TEST_CASE(PrefixNotFound)
309{
310 FaceId addedFaceId = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700311 BOOST_REQUIRE_NE(addedFaceId, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700312
313 auto parameters = makeParameters("hello", addedFaceId);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000314 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
315 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700316 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000317
318 auto expectedResponse = makeResponse(200, "Success", parameters);
319 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), expectedResponse), CheckResponseResult::OK);
Yanbiao Li711c7932015-08-19 16:30:16 -0700320}
321
322BOOST_AUTO_TEST_CASE(ImplicitFaceId)
323{
324 auto face1 = addFace();
325 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700326 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
327 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700328
329 Name expectedName;
330 ControlResponse expectedResponse;
331 auto testWithImplicitFaceId = [&] (ControlParameters parameters, FaceId face) {
332 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000333 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
334 req.setTag(make_shared<lp::IncomingFaceIdTag>(face));
335 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700336 expectedResponse = makeResponse(200, "Success", parameters.setFaceId(face));
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000337 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700338 };
339
Junxiao Shia6de4292016-07-12 02:08:10 +0000340 fib::Entry* entry = m_fib.insert("/hello").first;
341 entry->addNextHop(*m_faceTable.get(face1), 101);
342 entry->addNextHop(*m_faceTable.get(face2), 202);
Yanbiao Li711c7932015-08-19 16:30:16 -0700343
344 testWithImplicitFaceId(ControlParameters().setName("/hello").setFaceId(0), face1);
345 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
346 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
347 BOOST_CHECK_EQUAL(checkNextHop("/hello", 1, face1, 101), CheckNextHopResult::NO_NEXTHOP);
348
349 testWithImplicitFaceId(ControlParameters().setName("/hello"), face2);
350 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
351 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
352 BOOST_CHECK_EQUAL(checkNextHop("/hello", 0, face2, 202), CheckNextHopResult::NO_FIB_ENTRY);
353}
354
355BOOST_AUTO_TEST_CASE(RecordNotExist)
356{
357 auto face1 = addFace();
358 auto face2 = addFace();
Junxiao Shicde37ad2015-12-24 01:02:05 -0700359 BOOST_REQUIRE_NE(face1, face::INVALID_FACEID);
360 BOOST_REQUIRE_NE(face2, face::INVALID_FACEID);
Yanbiao Li711c7932015-08-19 16:30:16 -0700361
362 Name expectedName;
363 ControlResponse expectedResponse;
364 auto testRemoveNextHop = [&] (ControlParameters parameters) {
365 m_responses.clear();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000366 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
367 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700368 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000369 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700370 };
371
Junxiao Shia6de4292016-07-12 02:08:10 +0000372 m_fib.insert("/hello").first->addNextHop(*m_faceTable.get(face1), 101);
Yanbiao Li711c7932015-08-19 16:30:16 -0700373
374 testRemoveNextHop(makeParameters("/hello", face2 + 100));
375 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist
376 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesavento1586aff2017-02-19 23:17:51 -0500377 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700378
379 testRemoveNextHop(makeParameters("/hello", face2));
380 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist
381 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesavento1586aff2017-02-19 23:17:51 -0500382 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700383}
384
385BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop
386
Davide Pesavento97210d52016-10-14 15:45:48 +0200387BOOST_AUTO_TEST_SUITE(List)
388
Yanbiao Li711c7932015-08-19 16:30:16 -0700389BOOST_AUTO_TEST_CASE(FibDataset)
390{
391 const size_t nEntries = 108;
392 std::set<Name> actualPrefixes;
393 for (size_t i = 0 ; i < nEntries ; i ++) {
394 Name prefix = Name("test").appendSegment(i);
395 actualPrefixes.insert(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000396 fib::Entry* fibEntry = m_fib.insert(prefix).first;
397 fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 1);
398 fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 2);
Yanbiao Li711c7932015-08-19 16:30:16 -0700399 }
400
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000401 receiveInterest(Interest("/localhost/nfd/fib/list"));
Yanbiao Li711c7932015-08-19 16:30:16 -0700402
Davide Pesavento1586aff2017-02-19 23:17:51 -0500403 Block content = concatenateResponses();
404 content.parse();
Yanbiao Li711c7932015-08-19 16:30:16 -0700405 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
406
Davide Pesavento1586aff2017-02-19 23:17:51 -0500407 std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords;
Yanbiao Li711c7932015-08-19 16:30:16 -0700408 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500409 ndn::nfd::FibEntry decodedEntry(content.elements()[idx]);
Yanbiao Li711c7932015-08-19 16:30:16 -0700410 receivedRecords.push_back(decodedEntry);
Yanbiao Li711c7932015-08-19 16:30:16 -0700411 actualPrefixes.erase(decodedEntry.getPrefix());
412
413 auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix());
414 BOOST_REQUIRE(matchedEntry != nullptr);
415
Davide Pesavento1586aff2017-02-19 23:17:51 -0500416 expectedRecords.emplace_back();
417 expectedRecords.back().setPrefix(matchedEntry->getPrefix());
418 for (const auto& nh : matchedEntry->getNextHops()) {
419 expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord()
420 .setFaceId(nh.getFace().getId())
421 .setCost(nh.getCost()));
Yanbiao Li711c7932015-08-19 16:30:16 -0700422 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700423 }
424
425 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Li711c7932015-08-19 16:30:16 -0700426 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
427 expectedRecords.begin(), expectedRecords.end());
428}
429
Davide Pesavento97210d52016-10-14 15:45:48 +0200430BOOST_AUTO_TEST_SUITE_END() // List
431
Yanbiao Li711c7932015-08-19 16:30:16 -0700432BOOST_AUTO_TEST_SUITE_END() // TestFibManager
433BOOST_AUTO_TEST_SUITE_END() // Mgmt
434
435} // namespace tests
436} // namespace nfd