blob: e2986bca9898382d6ce658050d8947879d362c92 [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/*
3 * Copyright (c) 2014-2018, 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
Yanbiao Lidf846e52016-01-30 21:53:47 -080038class FibManagerFixture : public NfdManagerCommonFixture
Yanbiao Li711c7932015-08-19 16:30:16 -070039{
40public:
41 FibManagerFixture()
42 : m_fib(m_forwarder.getFib())
43 , m_faceTable(m_forwarder.getFaceTable())
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000044 , m_manager(m_fib, m_faceTable, m_dispatcher, *m_authenticator)
Yanbiao Li711c7932015-08-19 16:30:16 -070045 {
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000046 setTopPrefix();
Yanbiao Lidf846e52016-01-30 21:53:47 -080047 setPrivilege("fib");
Yanbiao Li711c7932015-08-19 16:30:16 -070048 }
49
50public: // for test
Davide Pesavento1586aff2017-02-19 23:17:51 -050051 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070052 makeParameters(const Name& name, const FaceId& id)
53 {
54 return ControlParameters().setName(name).setFaceId(id);
55 }
56
Davide Pesavento1586aff2017-02-19 23:17:51 -050057 static ControlParameters
Yanbiao Li711c7932015-08-19 16:30:16 -070058 makeParameters(const Name& name, const FaceId& id, const uint32_t& cost)
59 {
60 return ControlParameters().setName(name).setFaceId(id).setCost(cost);
61 }
62
63 FaceId
64 addFace()
65 {
66 auto face = make_shared<DummyFace>();
67 m_faceTable.add(face);
68 advanceClocks(time::milliseconds(1), 10);
69 m_responses.clear(); // clear all event notifications, if any
70 return face->getId();
71 }
72
73public: // for check
74 enum class CheckNextHopResult
75 {
76 OK,
77 NO_FIB_ENTRY,
78 WRONG_N_NEXTHOPS,
79 NO_NEXTHOP,
80 WRONG_COST
Davide Pesavento1586aff2017-02-19 23:17:51 -050081 };
Yanbiao Li711c7932015-08-19 16:30:16 -070082
83 /**
84 * @brief check whether the nexthop record is added / removed properly
85 *
Davide Pesavento1586aff2017-02-19 23:17:51 -050086 * @param expectedNNextHops use nullopt to skip this check
87 * @param faceId use nullopt to skip NextHopRecord checks
88 * @param expectedCost use nullopt to skip this check
Yanbiao Li711c7932015-08-19 16:30:16 -070089 *
90 * @retval OK FIB entry is found by exact match and has the expected number of nexthops;
91 * NextHopRe record for faceId is found and has the expected cost
92 * @retval NO_FIB_ENTRY FIB entry is not found
93 * @retval WRONG_N_NEXTHOPS FIB entry is found but has wrong number of nexthops
94 * @retval NO_NEXTHOP NextHopRecord for faceId is not found
95 * @retval WRONG_COST NextHopRecord for faceId has wrong cost
96 */
97 CheckNextHopResult
Davide Pesavento1586aff2017-02-19 23:17:51 -050098 checkNextHop(const Name& prefix,
Davide Pesavento87fc0f82018-04-11 23:43:51 -040099 optional<size_t> expectedNNextHops = nullopt,
100 optional<FaceId> faceId = nullopt,
101 optional<uint64_t> expectedCost = nullopt) const
Yanbiao Li711c7932015-08-19 16:30:16 -0700102 {
Junxiao Shia6de4292016-07-12 02:08:10 +0000103 const fib::Entry* entry = m_fib.findExactMatch(prefix);
104 if (entry == nullptr) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700105 return CheckNextHopResult::NO_FIB_ENTRY;
106 }
107
Davide Pesavento1586aff2017-02-19 23:17:51 -0500108 const auto& nextHops = entry->getNextHops();
109 if (expectedNNextHops && nextHops.size() != *expectedNNextHops) {
Yanbiao Li711c7932015-08-19 16:30:16 -0700110 return CheckNextHopResult::WRONG_N_NEXTHOPS;
111 }
112
Davide Pesavento1586aff2017-02-19 23:17:51 -0500113 if (faceId) {
Davide Pesavento97210d52016-10-14 15:45:48 +0200114 for (const auto& record : nextHops) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500115 if (record.getFace().getId() == *faceId) {
116 if (expectedCost && record.getCost() != *expectedCost)
Davide Pesavento97210d52016-10-14 15:45:48 +0200117 return CheckNextHopResult::WRONG_COST;
118 else
119 return CheckNextHopResult::OK;
Yanbiao Li711c7932015-08-19 16:30:16 -0700120 }
121 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700122 return CheckNextHopResult::NO_NEXTHOP;
123 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700124 return CheckNextHopResult::OK;
125 }
126
127protected:
128 Fib& m_fib;
129 FaceTable& m_faceTable;
130 FibManager m_manager;
131};
132
133std::ostream&
Davide Pesavento1586aff2017-02-19 23:17:51 -0500134operator<<(std::ostream& os, FibManagerFixture::CheckNextHopResult result)
Yanbiao Li711c7932015-08-19 16:30:16 -0700135{
136 switch (result) {
137 case FibManagerFixture::CheckNextHopResult::OK:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500138 return os << "OK";
Yanbiao Li711c7932015-08-19 16:30:16 -0700139 case FibManagerFixture::CheckNextHopResult::NO_FIB_ENTRY:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500140 return os << "NO_FIB_ENTRY";
Yanbiao Li711c7932015-08-19 16:30:16 -0700141 case FibManagerFixture::CheckNextHopResult::WRONG_N_NEXTHOPS:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500142 return os << "WRONG_N_NEXTHOPS";
Yanbiao Li711c7932015-08-19 16:30:16 -0700143 case FibManagerFixture::CheckNextHopResult::NO_NEXTHOP:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500144 return os << "NO_NEXTHOP";
Yanbiao Li711c7932015-08-19 16:30:16 -0700145 case FibManagerFixture::CheckNextHopResult::WRONG_COST:
Davide Pesavento1586aff2017-02-19 23:17:51 -0500146 return os << "WRONG_COST";
Yanbiao Li711c7932015-08-19 16:30:16 -0700147 };
148
Davide Pesavento1586aff2017-02-19 23:17:51 -0500149 return os << static_cast<int>(result);
Yanbiao Li711c7932015-08-19 16:30:16 -0700150}
151
Davide Pesavento97210d52016-10-14 15:45:48 +0200152BOOST_AUTO_TEST_SUITE(Mgmt)
153BOOST_FIXTURE_TEST_SUITE(TestFibManager, FibManagerFixture)
Yanbiao Li711c7932015-08-19 16:30:16 -0700154
155BOOST_AUTO_TEST_SUITE(AddNextHop)
156
157BOOST_AUTO_TEST_CASE(UnknownFaceId)
158{
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000159 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop",
160 makeParameters("hello", face::FACEID_NULL, 101));
161 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 Pesavento1586aff2017-02-19 23:17:51 -0500169 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, nullopt, 101), CheckNextHopResult::NO_FIB_ENTRY);
Yanbiao Li711c7932015-08-19 16:30:16 -0700170}
171
Junxiao Shi02a0e0f2018-02-06 02:03:45 +0000172BOOST_AUTO_TEST_CASE(NameTooLong)
173{
174 Name prefix;
175 while (prefix.size() <= FIB_MAX_DEPTH) {
176 prefix.append("A");
177 }
178
179 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop",
180 makeParameters(prefix, addFace()));
181 receiveInterest(req);
182
183 ControlResponse expected(414, "FIB entry prefix cannot exceed " +
184 ndn::to_string(Fib::getMaxDepth()) + " components");
185 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) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000200 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
201 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();
Yanbiao Li711c7932015-08-19 16:30:16 -0700204 expectedResponse = makeResponse(200, "Success", 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);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000225 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
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, 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);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000241 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", originalParameters);
242 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);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000245 BOOST_CHECK_EQUAL(checkResponse(0, req.getName(), makeResponse(200, "Success", 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();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000259 auto req = makeControlCommandRequest("/localhost/nfd/fib/add-nexthop", parameters);
260 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700261 expectedResponse = makeResponse(200, "Success", 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();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000290 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
291 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700292 expectedResponse = makeResponse(200, "Success", 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;
304 entry->addNextHop(*m_faceTable.get(face1), 101);
305 entry->addNextHop(*m_faceTable.get(face2), 202);
306 entry->addNextHop(*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);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000330 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
331 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
334 auto expectedResponse = makeResponse(200, "Success", parameters);
335 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();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000349 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
350 req.setTag(make_shared<lp::IncomingFaceIdTag>(face));
351 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700352 expectedResponse = makeResponse(200, "Success", 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;
357 entry->addNextHop(*m_faceTable.get(face1), 101);
358 entry->addNextHop(*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();
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000382 auto req = makeControlCommandRequest("/localhost/nfd/fib/remove-nexthop", parameters);
383 expectedName = req.getName();
Yanbiao Li711c7932015-08-19 16:30:16 -0700384 expectedResponse = makeResponse(200, "Success", parameters);
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000385 receiveInterest(req);
Yanbiao Li711c7932015-08-19 16:30:16 -0700386 };
387
Junxiao Shia6de4292016-07-12 02:08:10 +0000388 m_fib.insert("/hello").first->addNextHop(*m_faceTable.get(face1), 101);
Yanbiao Li711c7932015-08-19 16:30:16 -0700389
390 testRemoveNextHop(makeParameters("/hello", face2 + 100));
391 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist
392 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesavento1586aff2017-02-19 23:17:51 -0500393 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700394
395 testRemoveNextHop(makeParameters("/hello", face2));
396 BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist
397 BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
Davide Pesavento1586aff2017-02-19 23:17:51 -0500398 BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2), CheckNextHopResult::NO_NEXTHOP);
Yanbiao Li711c7932015-08-19 16:30:16 -0700399}
400
401BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop
402
Davide Pesavento97210d52016-10-14 15:45:48 +0200403BOOST_AUTO_TEST_SUITE(List)
404
Yanbiao Li711c7932015-08-19 16:30:16 -0700405BOOST_AUTO_TEST_CASE(FibDataset)
406{
407 const size_t nEntries = 108;
408 std::set<Name> actualPrefixes;
409 for (size_t i = 0 ; i < nEntries ; i ++) {
410 Name prefix = Name("test").appendSegment(i);
411 actualPrefixes.insert(prefix);
Junxiao Shia6de4292016-07-12 02:08:10 +0000412 fib::Entry* fibEntry = m_fib.insert(prefix).first;
413 fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 1);
414 fibEntry->addNextHop(*m_faceTable.get(addFace()), std::numeric_limits<uint8_t>::max() - 2);
Yanbiao Li711c7932015-08-19 16:30:16 -0700415 }
416
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000417 receiveInterest(Interest("/localhost/nfd/fib/list"));
Yanbiao Li711c7932015-08-19 16:30:16 -0700418
Davide Pesavento1586aff2017-02-19 23:17:51 -0500419 Block content = concatenateResponses();
420 content.parse();
Yanbiao Li711c7932015-08-19 16:30:16 -0700421 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
422
Davide Pesavento1586aff2017-02-19 23:17:51 -0500423 std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords;
Yanbiao Li711c7932015-08-19 16:30:16 -0700424 for (size_t idx = 0; idx < nEntries; ++idx) {
Davide Pesavento1586aff2017-02-19 23:17:51 -0500425 ndn::nfd::FibEntry decodedEntry(content.elements()[idx]);
Yanbiao Li711c7932015-08-19 16:30:16 -0700426 receivedRecords.push_back(decodedEntry);
Yanbiao Li711c7932015-08-19 16:30:16 -0700427 actualPrefixes.erase(decodedEntry.getPrefix());
428
429 auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix());
430 BOOST_REQUIRE(matchedEntry != nullptr);
431
Davide Pesavento1586aff2017-02-19 23:17:51 -0500432 expectedRecords.emplace_back();
433 expectedRecords.back().setPrefix(matchedEntry->getPrefix());
434 for (const auto& nh : matchedEntry->getNextHops()) {
435 expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord()
436 .setFaceId(nh.getFace().getId())
437 .setCost(nh.getCost()));
Yanbiao Li711c7932015-08-19 16:30:16 -0700438 }
Yanbiao Li711c7932015-08-19 16:30:16 -0700439 }
440
441 BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);
Yanbiao Li711c7932015-08-19 16:30:16 -0700442 BOOST_CHECK_EQUAL_COLLECTIONS(receivedRecords.begin(), receivedRecords.end(),
443 expectedRecords.begin(), expectedRecords.end());
444}
445
Davide Pesavento97210d52016-10-14 15:45:48 +0200446BOOST_AUTO_TEST_SUITE_END() // List
447
Yanbiao Li711c7932015-08-19 16:30:16 -0700448BOOST_AUTO_TEST_SUITE_END() // TestFibManager
449BOOST_AUTO_TEST_SUITE_END() // Mgmt
450
451} // namespace tests
452} // namespace nfd