blob: c2304977a027d84547865d88832cb6b33604f4c4 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/fib-manager.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07008#include "table/fib.hpp"
Steve DiBenedetto43cd0372014-02-01 17:05:07 -07009#include "table/fib-nexthop.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070010#include "face/face.hpp"
Steve DiBenedetto3970c892014-01-31 23:31:13 -070011#include "mgmt/internal-face.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070012#include "tests/face/dummy-face.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070013
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070014#include "validation-common.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015#include "tests/test-common.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070016
Steve DiBenedetto6214e562014-03-15 16:27:04 -060017#include "fib-enumeration-publisher-common.hpp"
18
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070019namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070020namespace tests {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070021
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070022NFD_LOG_INIT("FibManagerTest");
23
Steve DiBenedetto6214e562014-03-15 16:27:04 -060024class FibManagerFixture : protected BaseFixture, public FibEnumerationPublisherFixture
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070026public:
27
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070028 virtual
29 ~FibManagerFixture()
Steve DiBenedettobdedce92014-02-02 22:49:39 -070030 {
Steve DiBenedettobdedce92014-02-02 22:49:39 -070031 }
32
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070033 shared_ptr<Face>
34 getFace(FaceId id)
35 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036 if (id > 0 && static_cast<size_t>(id) <= m_faces.size())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070037 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070038 return m_faces[id - 1];
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070039 }
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070040 NFD_LOG_DEBUG("No face found returning NULL");
41 return shared_ptr<DummyFace>();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070042 }
43
44 void
45 addFace(shared_ptr<Face> face)
46 {
47 m_faces.push_back(face);
48 }
49
Steve DiBenedettobdedce92014-02-02 22:49:39 -070050 void
Steve DiBenedetto2693db92014-02-10 15:58:36 -070051 validateControlResponseCommon(const Data& response,
52 const Name& expectedName,
53 uint32_t expectedCode,
54 const std::string& expectedText,
55 ControlResponse& control)
Steve DiBenedettobdedce92014-02-02 22:49:39 -070056 {
57 m_callbackFired = true;
58 Block controlRaw = response.getContent().blockFromValue();
59
Steve DiBenedettobdedce92014-02-02 22:49:39 -070060 control.wireDecode(controlRaw);
61
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070062 // NFD_LOG_DEBUG("received control response"
63 // << " Name: " << response.getName()
64 // << " code: " << control.getCode()
65 // << " text: " << control.getText());
Steve DiBenedettobdedce92014-02-02 22:49:39 -070066
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070067 BOOST_CHECK_EQUAL(response.getName(), expectedName);
68 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
69 BOOST_CHECK_EQUAL(control.getText(), expectedText);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070070 }
71
Steve DiBenedetto2693db92014-02-10 15:58:36 -070072 void
73 validateControlResponse(const Data& response,
74 const Name& expectedName,
75 uint32_t expectedCode,
76 const std::string& expectedText)
77 {
78 ControlResponse control;
79 validateControlResponseCommon(response, expectedName,
80 expectedCode, expectedText, control);
81
82 if (!control.getBody().empty())
83 {
84 BOOST_FAIL("found unexpected control response body");
85 }
86 }
87
88 void
89 validateControlResponse(const Data& response,
90 const Name& expectedName,
91 uint32_t expectedCode,
92 const std::string& expectedText,
93 const Block& expectedBody)
94 {
95 ControlResponse control;
96 validateControlResponseCommon(response, expectedName,
97 expectedCode, expectedText, control);
98
99 BOOST_REQUIRE(!control.getBody().empty());
100 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
101
102 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
103 expectedBody.value_size()) == 0);
104
105 }
106
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700107 bool
108 didCallbackFire()
109 {
110 return m_callbackFired;
111 }
112
113 void
114 resetCallbackFired()
115 {
116 m_callbackFired = false;
117 }
118
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700119 shared_ptr<InternalFace>
120 getInternalFace()
121 {
122 return m_face;
123 }
124
125 FibManager&
126 getFibManager()
127 {
128 return m_manager;
129 }
130
131 Fib&
132 getFib()
133 {
134 return m_fib;
135 }
136
137 void
138 addInterestRule(const std::string& regex,
139 ndn::IdentityCertificate& certificate)
140 {
141 m_manager.addInterestRule(regex, certificate);
142 }
143
144protected:
145 FibManagerFixture()
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600146 : m_manager(boost::ref(m_fib),
147 bind(&FibManagerFixture::getFace, this, _1),
148 m_face)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700149 , m_callbackFired(false)
150 {
151 }
152
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600153protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700154 FibManager m_manager;
155
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700156 std::vector<shared_ptr<Face> > m_faces;
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700157 bool m_callbackFired;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700158};
159
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700160template <typename T> class AuthorizedCommandFixture:
161 public CommandFixture<T>
162{
163public:
164 AuthorizedCommandFixture()
165 {
166 const std::string regex = "^<localhost><nfd><fib>";
167 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
168 }
169
170 virtual
171 ~AuthorizedCommandFixture()
172 {
173 }
174};
175
176BOOST_FIXTURE_TEST_SUITE(MgmtFibManager, AuthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700177
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700178bool
179foundNextHop(FaceId id, uint32_t cost, const fib::NextHop& next)
180{
181 return id == next.getFace()->getId() && next.getCost() == cost;
182}
183
184bool
185addedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
186{
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700187 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700188
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700189 if (static_cast<bool>(entry))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700190 {
191 const fib::NextHopList& hops = entry->getNextHops();
192 return hops.size() == oldSize + 1 &&
193 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
194 }
195 return false;
196}
197
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700198bool
199foundNextHopWithFace(FaceId id, uint32_t cost,
200 shared_ptr<Face> face, const fib::NextHop& next)
201{
202 return id == next.getFace()->getId() && next.getCost() == cost && face == next.getFace();
203}
204
205bool
206addedNextHopWithFace(const Fib& fib, const Name& prefix, size_t oldSize,
207 uint32_t cost, shared_ptr<Face> face)
208{
209 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
210
211 if (static_cast<bool>(entry))
212 {
213 const fib::NextHopList& hops = entry->getNextHops();
214 return hops.size() == oldSize + 1 &&
215 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
216 }
217 return false;
218}
219
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700220BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700221{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700222 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700223
224 Interest command("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700225
226 face->onReceiveData +=
227 bind(&FibManagerFixture::validateControlResponse, this, _1,
228 command.getName(), 400, "Malformed command");
229
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700230 face->sendInterest(command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700231
232 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700233}
234
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700235BOOST_AUTO_TEST_CASE(MalformedCommmand)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700236{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700237 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700238
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700239 BOOST_REQUIRE(didCallbackFire() == false);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700240
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700241 Interest command("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700242
243 face->onReceiveData +=
244 bind(&FibManagerFixture::validateControlResponse, this, _1,
245 command.getName(), 400, "Malformed command");
246
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700247 getFibManager().onFibRequest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700248
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700249 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700250}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700251
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700252BOOST_AUTO_TEST_CASE(UnsupportedVerb)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700253{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700254 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700255
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800256 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700257 options.setName("/hello");
258 options.setFaceId(1);
259 options.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700260
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700261 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700262
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700263 Name commandName("/localhost/nfd/fib");
264 commandName.append("unsupported");
265 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700266
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700267 shared_ptr<Interest> command(make_shared<Interest>(commandName));
268 generateCommand(*command);
269
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700270 face->onReceiveData +=
271 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700272 command->getName(), 501, "Unsupported command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700273
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700274 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700275
276 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700277}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700278
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700279BOOST_AUTO_TEST_CASE(UnsignedCommand)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700280{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700281 addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700282
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700283 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700284
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800285 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700286 options.setName("/hello");
287 options.setFaceId(1);
288 options.setCost(101);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700289
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700290 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700291
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700292 Name commandName("/localhost/nfd/fib");
293 commandName.append("add-nexthop");
294 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700295
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700296 Interest command(commandName);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700297
298 face->onReceiveData +=
299 bind(&FibManagerFixture::validateControlResponse,
300 this, _1, command.getName(), 401, "Signature required");
301
302
303 getFibManager().onFibRequest(command);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700304
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700305 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700306 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700307}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700308
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700309BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700310{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700311 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700312
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700313 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700314
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800315 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700316 options.setName("/hello");
317 options.setFaceId(1);
318 options.setCost(101);
319
320 Block encodedOptions(options.wireEncode());
321
322 Name commandName("/localhost/nfd/fib");
323 commandName.append("add-nexthop");
324 commandName.append(encodedOptions);
325
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700326 shared_ptr<Interest> command(make_shared<Interest>(commandName));
327 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700328
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700329 face->onReceiveData +=
330 bind(&FibManagerFixture::validateControlResponse,
331 this, _1, command->getName(), 403, "Unauthorized command");
332
333 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700334
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700335 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700336 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700337}
338
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700339BOOST_AUTO_TEST_CASE(BadOptionParse)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700340{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700341 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700342
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700343 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700344
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700345 Name commandName("/localhost/nfd/fib");
346 commandName.append("add-nexthop");
347 commandName.append("NotReallyOptions");
348
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700349 shared_ptr<Interest> command(make_shared<Interest>(commandName));
350 generateCommand(*command);
351
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700352 face->onReceiveData +=
353 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700354 command->getName(), 400, "Malformed command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700355
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700356 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700357
358 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700359}
360
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700361BOOST_AUTO_TEST_CASE(UnknownFaceId)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700362{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700363 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700364
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700365 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700366
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800367 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700368 options.setName("/hello");
369 options.setFaceId(1000);
370 options.setCost(101);
371
372 Block encodedOptions(options.wireEncode());
373
374 Name commandName("/localhost/nfd/fib");
375 commandName.append("add-nexthop");
376 commandName.append(encodedOptions);
377
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700378 shared_ptr<Interest> command(make_shared<Interest>(commandName));
379 generateCommand(*command);
380
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700381 face->onReceiveData +=
382 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700383 command->getName(), 404, "Face not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700384
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700385 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700386
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700387 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700388 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101) == false);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700389}
390
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700391BOOST_AUTO_TEST_CASE(TestImplicitFaceId)
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700392{
393 addFace(make_shared<DummyFace>());
394
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700395 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700396
397 FibManagementOptions options;
398 options.setName("/hello");
399 options.setFaceId(0);
400 options.setCost(101);
401
402 Block encodedOptions(options.wireEncode());
403
404 Name commandName("/localhost/nfd/fib");
405 commandName.append("add-nexthop");
406 commandName.append(encodedOptions);
407
408 FibManagementOptions expectedOptions;
409 expectedOptions.setName("/hello");
410 expectedOptions.setFaceId(1);
411 expectedOptions.setCost(101);
412
413 Block encodedExpectedOptions(expectedOptions.wireEncode());
414
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700415 shared_ptr<Interest> command(make_shared<Interest>(commandName));
416 command->setIncomingFaceId(1);
417 generateCommand(*command);
418
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700419 face->onReceiveData +=
420 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700421 command->getName(), 200, "Success", encodedExpectedOptions);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700422
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700423 getFibManager().onFibRequest(*command);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700424
425 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700426 BOOST_REQUIRE(addedNextHopWithFace(getFib(), "/hello", 0, 101, getFace(1)));
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700427}
428
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700429BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700430{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700431 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700432
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700433 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700434
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800435 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700436 options.setName("/hello");
437 options.setFaceId(1);
438 options.setCost(101);
439
440 Block encodedOptions(options.wireEncode());
441
442 Name commandName("/localhost/nfd/fib");
443 commandName.append("add-nexthop");
444 commandName.append(encodedOptions);
445
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700446 shared_ptr<Interest> command(make_shared<Interest>(commandName));
447 generateCommand(*command);
448
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700449 face->onReceiveData +=
450 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700451 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700452
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700453 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700454
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700455 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700456 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700457}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700458
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700459BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700460{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700461 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700462 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700463
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700464 for (int i = 1; i <= 2; i++)
465 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700466
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800467 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700468 options.setName("/hello");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700469 options.setFaceId(1);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700470 options.setCost(100 + i);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700471
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700472 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700473
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700474 Name commandName("/localhost/nfd/fib");
475 commandName.append("add-nexthop");
476 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700477
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700478 shared_ptr<Interest> command(make_shared<Interest>(commandName));
479 generateCommand(*command);
480
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700481 face->onReceiveData +=
482 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700483 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700484
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700485 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700486 BOOST_REQUIRE(didCallbackFire());
487 resetCallbackFired();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700488
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700489 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700490
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700491 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700492 {
493 const fib::NextHopList& hops = entry->getNextHops();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700494 BOOST_REQUIRE(hops.size() == 1);
495 BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(),
496 bind(&foundNextHop, -1, 100 + i, _1)) != hops.end());
497
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700498 }
499 else
500 {
501 BOOST_FAIL("Failed to find expected fib entry");
502 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700503
504 face->onReceiveData.clear();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700505 }
506}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700507
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700508BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700509{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700510 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700511 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700512
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800513 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700514 options.setName("/hello");
515 options.setFaceId(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700516
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700517 {
518 options.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700519
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700520 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700521
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700522 Name commandName("/localhost/nfd/fib");
523 commandName.append("add-nexthop");
524 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700525
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700526 shared_ptr<Interest> command(make_shared<Interest>(commandName));
527 generateCommand(*command);
528
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700529 face->onReceiveData +=
530 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700531 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700532
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700533 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700534
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700535 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700536 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700537
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700538 resetCallbackFired();
539 face->onReceiveData.clear();
540
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700541 {
542 options.setCost(102);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700543
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700544 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700545
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700546 Name commandName("/localhost/nfd/fib");
547 commandName.append("add-nexthop");
548 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700549
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700550 shared_ptr<Interest> command(make_shared<Interest>(commandName));
551 generateCommand(*command);
552
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700553 face->onReceiveData +=
554 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700555 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700556
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700557 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700558
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700559 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700560 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700561
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700562 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700563
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700564 // Add faces with cost == FaceID for the name /hello
565 // This test assumes:
566 // FaceIDs are -1 because we don't add them to a forwarder
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700567 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700568 {
569 const fib::NextHopList& hops = entry->getNextHops();
570 BOOST_REQUIRE(hops.size() == 1);
571 BOOST_REQUIRE(std::find_if(hops.begin(),
572 hops.end(),
573 bind(&foundNextHop, -1, 102, _1)) != hops.end());
574 }
575 else
576 {
577 BOOST_FAIL("Failed to find expected fib entry");
578 }
579}
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700580
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600581// BOOST_AUTO_TEST_CASE(Insert)
582// {
583// addFace(make_shared<DummyFace>());
584// addFace(make_shared<DummyFace>());
585// shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700586
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600587// {
588// FibManagementOptions options;
589// options.setName("/hello");
590// options.setFaceId(1);
591// options.setCost(101);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700592
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600593// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700594
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600595// Name commandName("/localhost/nfd/fib");
596// commandName.append("add-nexthop");
597// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700598
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600599// shared_ptr<Interest> command(make_shared<Interest>(commandName));
600// generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700601
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600602// face->onReceiveData +=
603// bind(&FibManagerFixture::validateControlResponse, this, _1,
604// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700605
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600606// getFibManager().onFibRequest(*command);
607// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700608
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600609// BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700610
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600611// shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
612// if (static_cast<bool>(entry))
613// {
614// const fib::NextHopList& hops = entry->getNextHops();
615// BOOST_CHECK_EQUAL(hops.size(), 1);
616// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700617
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600618// resetCallbackFired();
619// face->onReceiveData.clear();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700620
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600621// {
622// FibManagementOptions options;
623// options.setName("/hello");
624// options.setFaceId(2);
625// options.setCost(102);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700626
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600627// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700628
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600629// Name commandName("/localhost/nfd/fib");
630// commandName.append("add-nexthop");
631// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700632
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600633// shared_ptr<Interest> command(make_shared<Interest>(commandName));
634// generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700635
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600636// face->onReceiveData +=
637// bind(&FibManagerFixture::validateControlResponse, this, _1,
638// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700639
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600640// getFibManager().onFibRequest(*command);
641// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700642
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600643// BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700644
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600645// entry = getFib().findExactMatch("/hello");
646// if (static_cast<bool>(entry))
647// {
648// const fib::NextHopList& hops = entry->getNextHops();
649// BOOST_CHECK_EQUAL(hops.size(), 2);
650// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700651
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600652// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700653
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600654// void
655// testRemove(CommandFixture<FibManagerFixture>* fixture,
656// FibManager& manager,
657// Fib& fib,
658// shared_ptr<Face> face,
659// const Name& target)
660// {
661// FibManagementOptions options;
662// options.setName(target);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700663
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600664// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700665
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600666// Name commandName("/localhost/nfd/fib");
667// commandName.append("delete");
668// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700669
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600670// shared_ptr<Interest> command(make_shared<Interest>(commandName));
671// fixture->generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700672
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600673// face->onReceiveData +=
674// bind(&FibManagerFixture::validateControlResponse, fixture, _1,
675// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700676
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600677// manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700678
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600679// BOOST_REQUIRE(fixture->didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700680
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600681// if (static_cast<bool>(fib.findExactMatch(target)))
682// {
683// BOOST_FAIL("Found \"removed\" prefix");
684// }
685// face->onReceiveData.clear();
686// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700687
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600688// BOOST_AUTO_TEST_CASE(Delete)
689// {
690// shared_ptr<InternalFace> face = getInternalFace();
691// FibManager& manager = getFibManager();
692// Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700693
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600694// fib.insert("/a");
695// fib.insert("/a/b");
696// fib.insert("/a/b/c");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700697
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600698// testRemove(this, manager, fib, face, "/");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700699
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600700// if (!static_cast<bool>(fib.findExactMatch("/a")) ||
701// !static_cast<bool>(fib.findExactMatch("/a/b")) ||
702// !static_cast<bool>(fib.findExactMatch("/a/b/c")))
703// {
704// BOOST_FAIL("Removed incorrect entry");
705// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700706
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600707// testRemove(this, manager, fib, face, "/a/b");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700708
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600709// if (!static_cast<bool>(fib.findExactMatch("/a")) ||
710// !static_cast<bool>(fib.findExactMatch("/a/b/c")))
711// {
712// BOOST_FAIL("Removed incorrect entry");
713// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700714
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600715// testRemove(this, manager, fib, face, "/a/b/c");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700716
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600717// if (!static_cast<bool>(fib.findExactMatch("/a")))
718// {
719// BOOST_FAIL("Removed incorrect entry");
720// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700721
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600722// testRemove(this, manager, fib, face, "/a");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700723
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600724// testRemove(this, manager, fib, face, "/does/not/exist");
725// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700726
727bool
728removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
729{
730 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
731
732 if (static_cast<bool>(entry))
733 {
734 const fib::NextHopList& hops = entry->getNextHops();
735 return hops.size() == oldSize - 1 &&
736 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end();
737 }
738 return false;
739}
740
741void
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700742testRemoveNextHop(CommandFixture<FibManagerFixture>* fixture,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700743 FibManager& manager,
744 Fib& fib,
745 shared_ptr<Face> face,
746 const Name& targetName,
747 FaceId targetFace)
748{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800749 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700750 options.setName(targetName);
751 options.setFaceId(targetFace);
752
753 Block encodedOptions(options.wireEncode());
754
755 Name commandName("/localhost/nfd/fib");
756 commandName.append("remove-nexthop");
757 commandName.append(encodedOptions);
758
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700759 shared_ptr<Interest> command(make_shared<Interest>(commandName));
760 fixture->generateCommand(*command);
761
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700762 face->onReceiveData +=
763 bind(&FibManagerFixture::validateControlResponse, fixture, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700764 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700765
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700766 manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700767
768 BOOST_REQUIRE(fixture->didCallbackFire());
769
770 fixture->resetCallbackFired();
771 face->onReceiveData.clear();
772}
773
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700774BOOST_AUTO_TEST_CASE(RemoveNextHop)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700775{
776 shared_ptr<Face> face1 = make_shared<DummyFace>();
777 shared_ptr<Face> face2 = make_shared<DummyFace>();
778 shared_ptr<Face> face3 = make_shared<DummyFace>();
779
780 addFace(face1);
781 addFace(face2);
782 addFace(face3);
783
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700784 shared_ptr<InternalFace> face = getInternalFace();
785 FibManager& manager = getFibManager();
786 Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700787
788 shared_ptr<fib::Entry> entry = fib.insert("/hello").first;
789
790 entry->addNextHop(face1, 101);
791 entry->addNextHop(face2, 202);
792 entry->addNextHop(face3, 303);
793
794 testRemoveNextHop(this, manager, fib, face, "/hello", 2);
795 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202));
796
797 testRemoveNextHop(this, manager, fib, face, "/hello", 3);
798 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303));
799
800 testRemoveNextHop(this, manager, fib, face, "/hello", 1);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600801 // BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700802
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600803 BOOST_CHECK(!static_cast<bool>(getFib().findExactMatch("/hello")));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700804}
805
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700806BOOST_AUTO_TEST_CASE(RemoveNoFace)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700807{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700808 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700809
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800810 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700811 options.setName("/hello");
812 options.setFaceId(1);
813
814 Block encodedOptions(options.wireEncode());
815
816 Name commandName("/localhost/nfd/fib");
817 commandName.append("remove-nexthop");
818 commandName.append(encodedOptions);
819
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700820 shared_ptr<Interest> command(make_shared<Interest>(commandName));
821 generateCommand(*command);
822
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700823 face->onReceiveData +=
824 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700825 command->getName(), 404, "Face not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700826
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700827 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700828
829 BOOST_REQUIRE(didCallbackFire());
830}
831
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700832BOOST_AUTO_TEST_CASE(RemoveNoPrefix)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700833{
834 addFace(make_shared<DummyFace>());
835
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700836 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700837
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800838 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700839 options.setName("/hello");
840 options.setFaceId(1);
841
842 Block encodedOptions(options.wireEncode());
843
844 Name commandName("/localhost/nfd/fib");
845 commandName.append("remove-nexthop");
846 commandName.append(encodedOptions);
847
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700848 shared_ptr<Interest> command(make_shared<Interest>(commandName));
849 generateCommand(*command);
850
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700851 face->onReceiveData +=
852 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700853 command->getName(), 404, "Prefix not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700854
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700855 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700856
857 BOOST_REQUIRE(didCallbackFire());
858}
859
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600860BOOST_FIXTURE_TEST_CASE(TestFibEnumerationRequest, FibManagerFixture)
861{
862 for (int i = 0; i < 87; i++)
863 {
864 Name prefix("/test");
865 prefix.appendSegment(i);
866
867 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
868 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
869
870 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
871 entry->addNextHop(dummy1, std::numeric_limits<uint64_t>::max() - 1);
872 entry->addNextHop(dummy2, std::numeric_limits<uint64_t>::max() - 2);
873
874 m_referenceEntries.insert(entry);
875 }
876 for (int i = 0; i < 2; i++)
877 {
878 Name prefix("/test2");
879 prefix.appendSegment(i);
880
881 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
882 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
883
884 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
885 entry->addNextHop(dummy1, std::numeric_limits<uint8_t>::max() - 1);
886 entry->addNextHop(dummy2, std::numeric_limits<uint8_t>::max() - 2);
887
888 m_referenceEntries.insert(entry);
889 }
890
891 ndn::EncodingBuffer buffer;
892
893 m_face->onReceiveData +=
894 bind(&FibEnumerationPublisherFixture::decodeFibEntryBlock, this, _1);
895
896 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/fib/list"));
897
898 m_manager.onFibRequest(*command);
899 BOOST_REQUIRE(m_finished);
900}
901
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700902BOOST_AUTO_TEST_SUITE_END()
903
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700904} // namespace tests
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700905} // namespace nfd