blob: 66ed32b010b9c27d6b53dfc37af2a2af9660bf40 [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 DiBenedetto43cd0372014-02-01 17:05:07 -070017namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070018namespace tests {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070019
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070020NFD_LOG_INIT("FibManagerTest");
21
Junxiao Shid9ee45c2014-02-27 15:38:11 -070022class FibManagerFixture : protected BaseFixture
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070023{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070024public:
25
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070026 virtual
27 ~FibManagerFixture()
Steve DiBenedettobdedce92014-02-02 22:49:39 -070028 {
Steve DiBenedettobdedce92014-02-02 22:49:39 -070029 }
30
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070031 shared_ptr<Face>
32 getFace(FaceId id)
33 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034 if (id > 0 && static_cast<size_t>(id) <= m_faces.size())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070035 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036 return m_faces[id - 1];
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070037 }
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070038 NFD_LOG_DEBUG("No face found returning NULL");
39 return shared_ptr<DummyFace>();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070040 }
41
42 void
43 addFace(shared_ptr<Face> face)
44 {
45 m_faces.push_back(face);
46 }
47
Steve DiBenedettobdedce92014-02-02 22:49:39 -070048 void
Steve DiBenedetto2693db92014-02-10 15:58:36 -070049 validateControlResponseCommon(const Data& response,
50 const Name& expectedName,
51 uint32_t expectedCode,
52 const std::string& expectedText,
53 ControlResponse& control)
Steve DiBenedettobdedce92014-02-02 22:49:39 -070054 {
55 m_callbackFired = true;
56 Block controlRaw = response.getContent().blockFromValue();
57
Steve DiBenedettobdedce92014-02-02 22:49:39 -070058 control.wireDecode(controlRaw);
59
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070060 // NFD_LOG_DEBUG("received control response"
61 // << " Name: " << response.getName()
62 // << " code: " << control.getCode()
63 // << " text: " << control.getText());
Steve DiBenedettobdedce92014-02-02 22:49:39 -070064
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070065 BOOST_CHECK_EQUAL(response.getName(), expectedName);
66 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
67 BOOST_CHECK_EQUAL(control.getText(), expectedText);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070068 }
69
Steve DiBenedetto2693db92014-02-10 15:58:36 -070070 void
71 validateControlResponse(const Data& response,
72 const Name& expectedName,
73 uint32_t expectedCode,
74 const std::string& expectedText)
75 {
76 ControlResponse control;
77 validateControlResponseCommon(response, expectedName,
78 expectedCode, expectedText, control);
79
80 if (!control.getBody().empty())
81 {
82 BOOST_FAIL("found unexpected control response body");
83 }
84 }
85
86 void
87 validateControlResponse(const Data& response,
88 const Name& expectedName,
89 uint32_t expectedCode,
90 const std::string& expectedText,
91 const Block& expectedBody)
92 {
93 ControlResponse control;
94 validateControlResponseCommon(response, expectedName,
95 expectedCode, expectedText, control);
96
97 BOOST_REQUIRE(!control.getBody().empty());
98 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
99
100 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
101 expectedBody.value_size()) == 0);
102
103 }
104
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700105 bool
106 didCallbackFire()
107 {
108 return m_callbackFired;
109 }
110
111 void
112 resetCallbackFired()
113 {
114 m_callbackFired = false;
115 }
116
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700117 shared_ptr<InternalFace>
118 getInternalFace()
119 {
120 return m_face;
121 }
122
123 FibManager&
124 getFibManager()
125 {
126 return m_manager;
127 }
128
129 Fib&
130 getFib()
131 {
132 return m_fib;
133 }
134
135 void
136 addInterestRule(const std::string& regex,
137 ndn::IdentityCertificate& certificate)
138 {
139 m_manager.addInterestRule(regex, certificate);
140 }
141
142protected:
143 FibManagerFixture()
144 : m_face(make_shared<InternalFace>())
145 , m_nameTree(1024)
146 , m_fib(m_nameTree)
147 , m_manager(boost::ref(m_fib),
148 bind(&FibManagerFixture::getFace, this, _1),
149 m_face)
150 , m_callbackFired(false)
151 {
152 }
153
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700154private:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700155 shared_ptr<InternalFace> m_face;
156 NameTree m_nameTree;
157 Fib m_fib;
158 FibManager m_manager;
159
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700160 std::vector<shared_ptr<Face> > m_faces;
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700161 bool m_callbackFired;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700162};
163
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700164template <typename T> class AuthorizedCommandFixture:
165 public CommandFixture<T>
166{
167public:
168 AuthorizedCommandFixture()
169 {
170 const std::string regex = "^<localhost><nfd><fib>";
171 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
172 }
173
174 virtual
175 ~AuthorizedCommandFixture()
176 {
177 }
178};
179
180BOOST_FIXTURE_TEST_SUITE(MgmtFibManager, AuthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700181
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700182bool
183foundNextHop(FaceId id, uint32_t cost, const fib::NextHop& next)
184{
185 return id == next.getFace()->getId() && next.getCost() == cost;
186}
187
188bool
189addedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
190{
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700191 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700192
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700193 if (static_cast<bool>(entry))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700194 {
195 const fib::NextHopList& hops = entry->getNextHops();
196 return hops.size() == oldSize + 1 &&
197 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
198 }
199 return false;
200}
201
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700202bool
203foundNextHopWithFace(FaceId id, uint32_t cost,
204 shared_ptr<Face> face, const fib::NextHop& next)
205{
206 return id == next.getFace()->getId() && next.getCost() == cost && face == next.getFace();
207}
208
209bool
210addedNextHopWithFace(const Fib& fib, const Name& prefix, size_t oldSize,
211 uint32_t cost, shared_ptr<Face> face)
212{
213 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
214
215 if (static_cast<bool>(entry))
216 {
217 const fib::NextHopList& hops = entry->getNextHops();
218 return hops.size() == oldSize + 1 &&
219 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
220 }
221 return false;
222}
223
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700224BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700225{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700226 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700227
228 Interest command("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700229
230 face->onReceiveData +=
231 bind(&FibManagerFixture::validateControlResponse, this, _1,
232 command.getName(), 400, "Malformed command");
233
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700234 face->sendInterest(command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700235
236 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700237}
238
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700239BOOST_AUTO_TEST_CASE(MalformedCommmand)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700240{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700241 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700242
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700243 BOOST_REQUIRE(didCallbackFire() == false);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700244
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700245 Interest command("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700246
247 face->onReceiveData +=
248 bind(&FibManagerFixture::validateControlResponse, this, _1,
249 command.getName(), 400, "Malformed command");
250
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700251 getFibManager().onFibRequest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700252
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700253 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700254}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700255
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700256BOOST_AUTO_TEST_CASE(UnsupportedVerb)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700257{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700258 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700259
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800260 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700261 options.setName("/hello");
262 options.setFaceId(1);
263 options.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700264
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700265 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700266
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700267 Name commandName("/localhost/nfd/fib");
268 commandName.append("unsupported");
269 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700270
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700271 shared_ptr<Interest> command(make_shared<Interest>(commandName));
272 generateCommand(*command);
273
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700274 face->onReceiveData +=
275 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700276 command->getName(), 501, "Unsupported command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700277
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700278 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700279
280 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700281}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700282
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700283BOOST_AUTO_TEST_CASE(UnsignedCommand)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700284{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700285 addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700286
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700287 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700288
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800289 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700290 options.setName("/hello");
291 options.setFaceId(1);
292 options.setCost(101);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700293
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700294 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700295
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700296 Name commandName("/localhost/nfd/fib");
297 commandName.append("add-nexthop");
298 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700299
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700300 Interest command(commandName);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700301
302 face->onReceiveData +=
303 bind(&FibManagerFixture::validateControlResponse,
304 this, _1, command.getName(), 401, "Signature required");
305
306
307 getFibManager().onFibRequest(command);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700308
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700309 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700310 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700311}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700312
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700313BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700314{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700315 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700316
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700317 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700318
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800319 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700320 options.setName("/hello");
321 options.setFaceId(1);
322 options.setCost(101);
323
324 Block encodedOptions(options.wireEncode());
325
326 Name commandName("/localhost/nfd/fib");
327 commandName.append("add-nexthop");
328 commandName.append(encodedOptions);
329
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700330 shared_ptr<Interest> command(make_shared<Interest>(commandName));
331 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700332
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700333 face->onReceiveData +=
334 bind(&FibManagerFixture::validateControlResponse,
335 this, _1, command->getName(), 403, "Unauthorized command");
336
337 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700338
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700339 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700340 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700341}
342
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700343BOOST_AUTO_TEST_CASE(BadOptionParse)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700344{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700345 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700346
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700347 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700348
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700349 Name commandName("/localhost/nfd/fib");
350 commandName.append("add-nexthop");
351 commandName.append("NotReallyOptions");
352
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700353 shared_ptr<Interest> command(make_shared<Interest>(commandName));
354 generateCommand(*command);
355
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700356 face->onReceiveData +=
357 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700358 command->getName(), 400, "Malformed command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700359
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700360 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700361
362 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700363}
364
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700365BOOST_AUTO_TEST_CASE(UnknownFaceId)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700366{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700367 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700368
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700369 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700370
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800371 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700372 options.setName("/hello");
373 options.setFaceId(1000);
374 options.setCost(101);
375
376 Block encodedOptions(options.wireEncode());
377
378 Name commandName("/localhost/nfd/fib");
379 commandName.append("add-nexthop");
380 commandName.append(encodedOptions);
381
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700382 shared_ptr<Interest> command(make_shared<Interest>(commandName));
383 generateCommand(*command);
384
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700385 face->onReceiveData +=
386 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700387 command->getName(), 404, "Face not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700388
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700389 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700390
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700391 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700392 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101) == false);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700393}
394
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700395BOOST_AUTO_TEST_CASE(TestImplicitFaceId)
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700396{
397 addFace(make_shared<DummyFace>());
398
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700399 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700400
401 FibManagementOptions options;
402 options.setName("/hello");
403 options.setFaceId(0);
404 options.setCost(101);
405
406 Block encodedOptions(options.wireEncode());
407
408 Name commandName("/localhost/nfd/fib");
409 commandName.append("add-nexthop");
410 commandName.append(encodedOptions);
411
412 FibManagementOptions expectedOptions;
413 expectedOptions.setName("/hello");
414 expectedOptions.setFaceId(1);
415 expectedOptions.setCost(101);
416
417 Block encodedExpectedOptions(expectedOptions.wireEncode());
418
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700419 shared_ptr<Interest> command(make_shared<Interest>(commandName));
420 command->setIncomingFaceId(1);
421 generateCommand(*command);
422
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700423 face->onReceiveData +=
424 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700425 command->getName(), 200, "Success", encodedExpectedOptions);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700426
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700427 getFibManager().onFibRequest(*command);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700428
429 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700430 BOOST_REQUIRE(addedNextHopWithFace(getFib(), "/hello", 0, 101, getFace(1)));
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700431}
432
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700433BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700434{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700435 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700436
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700437 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700438
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800439 FibManagementOptions options;
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700440 options.setName("/hello");
441 options.setFaceId(1);
442 options.setCost(101);
443
444 Block encodedOptions(options.wireEncode());
445
446 Name commandName("/localhost/nfd/fib");
447 commandName.append("add-nexthop");
448 commandName.append(encodedOptions);
449
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700450 shared_ptr<Interest> command(make_shared<Interest>(commandName));
451 generateCommand(*command);
452
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700453 face->onReceiveData +=
454 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700455 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700456
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700457 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700458
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700459 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700460 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700461}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700462
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700463BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700464{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700465 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700466 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700467
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700468 for (int i = 1; i <= 2; i++)
469 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700470
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800471 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700472 options.setName("/hello");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700473 options.setFaceId(1);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700474 options.setCost(100 + i);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700475
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700476 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700477
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700478 Name commandName("/localhost/nfd/fib");
479 commandName.append("add-nexthop");
480 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700481
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700482 shared_ptr<Interest> command(make_shared<Interest>(commandName));
483 generateCommand(*command);
484
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700485 face->onReceiveData +=
486 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700487 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700488
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700489 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700490 BOOST_REQUIRE(didCallbackFire());
491 resetCallbackFired();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700492
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700493 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700494
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700495 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700496 {
497 const fib::NextHopList& hops = entry->getNextHops();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700498 BOOST_REQUIRE(hops.size() == 1);
499 BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(),
500 bind(&foundNextHop, -1, 100 + i, _1)) != hops.end());
501
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700502 }
503 else
504 {
505 BOOST_FAIL("Failed to find expected fib entry");
506 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700507
508 face->onReceiveData.clear();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700509 }
510}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700511
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700512BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700513{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700514 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700515 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700516
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800517 FibManagementOptions options;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700518 options.setName("/hello");
519 options.setFaceId(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700520
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700521 {
522 options.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700523
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700524 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700525
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700526 Name commandName("/localhost/nfd/fib");
527 commandName.append("add-nexthop");
528 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700529
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700530 shared_ptr<Interest> command(make_shared<Interest>(commandName));
531 generateCommand(*command);
532
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700533 face->onReceiveData +=
534 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700535 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700536
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700537 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700538
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700539 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700540 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700541
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700542 resetCallbackFired();
543 face->onReceiveData.clear();
544
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700545 {
546 options.setCost(102);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700547
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700548 Block encodedOptions(options.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700549
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700550 Name commandName("/localhost/nfd/fib");
551 commandName.append("add-nexthop");
552 commandName.append(encodedOptions);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700553
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700554 shared_ptr<Interest> command(make_shared<Interest>(commandName));
555 generateCommand(*command);
556
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700557 face->onReceiveData +=
558 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700559 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700560
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700561 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700562
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700563 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700564 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700565
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700566 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700567
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700568 // Add faces with cost == FaceID for the name /hello
569 // This test assumes:
570 // FaceIDs are -1 because we don't add them to a forwarder
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700571 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700572 {
573 const fib::NextHopList& hops = entry->getNextHops();
574 BOOST_REQUIRE(hops.size() == 1);
575 BOOST_REQUIRE(std::find_if(hops.begin(),
576 hops.end(),
577 bind(&foundNextHop, -1, 102, _1)) != hops.end());
578 }
579 else
580 {
581 BOOST_FAIL("Failed to find expected fib entry");
582 }
583}
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700584
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600585// BOOST_AUTO_TEST_CASE(Insert)
586// {
587// addFace(make_shared<DummyFace>());
588// addFace(make_shared<DummyFace>());
589// shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700590
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600591// {
592// FibManagementOptions options;
593// options.setName("/hello");
594// options.setFaceId(1);
595// options.setCost(101);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700596
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600597// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700598
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600599// Name commandName("/localhost/nfd/fib");
600// commandName.append("add-nexthop");
601// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700602
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600603// shared_ptr<Interest> command(make_shared<Interest>(commandName));
604// generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700605
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600606// face->onReceiveData +=
607// bind(&FibManagerFixture::validateControlResponse, this, _1,
608// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700609
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600610// getFibManager().onFibRequest(*command);
611// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700612
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600613// BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700614
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600615// shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
616// if (static_cast<bool>(entry))
617// {
618// const fib::NextHopList& hops = entry->getNextHops();
619// BOOST_CHECK_EQUAL(hops.size(), 1);
620// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700621
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600622// resetCallbackFired();
623// face->onReceiveData.clear();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700624
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600625// {
626// FibManagementOptions options;
627// options.setName("/hello");
628// options.setFaceId(2);
629// options.setCost(102);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700630
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600631// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700632
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600633// Name commandName("/localhost/nfd/fib");
634// commandName.append("add-nexthop");
635// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700636
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600637// shared_ptr<Interest> command(make_shared<Interest>(commandName));
638// generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700639
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600640// face->onReceiveData +=
641// bind(&FibManagerFixture::validateControlResponse, this, _1,
642// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700643
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600644// getFibManager().onFibRequest(*command);
645// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700646
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600647// BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700648
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600649// entry = getFib().findExactMatch("/hello");
650// if (static_cast<bool>(entry))
651// {
652// const fib::NextHopList& hops = entry->getNextHops();
653// BOOST_CHECK_EQUAL(hops.size(), 2);
654// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700655
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600656// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700657
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600658// void
659// testRemove(CommandFixture<FibManagerFixture>* fixture,
660// FibManager& manager,
661// Fib& fib,
662// shared_ptr<Face> face,
663// const Name& target)
664// {
665// FibManagementOptions options;
666// options.setName(target);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700667
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600668// Block encodedOptions(options.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700669
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600670// Name commandName("/localhost/nfd/fib");
671// commandName.append("delete");
672// commandName.append(encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700673
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600674// shared_ptr<Interest> command(make_shared<Interest>(commandName));
675// fixture->generateCommand(*command);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700676
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600677// face->onReceiveData +=
678// bind(&FibManagerFixture::validateControlResponse, fixture, _1,
679// command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700680
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600681// manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700682
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600683// BOOST_REQUIRE(fixture->didCallbackFire());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700684
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600685// if (static_cast<bool>(fib.findExactMatch(target)))
686// {
687// BOOST_FAIL("Found \"removed\" prefix");
688// }
689// face->onReceiveData.clear();
690// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700691
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600692// BOOST_AUTO_TEST_CASE(Delete)
693// {
694// shared_ptr<InternalFace> face = getInternalFace();
695// FibManager& manager = getFibManager();
696// Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700697
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600698// fib.insert("/a");
699// fib.insert("/a/b");
700// fib.insert("/a/b/c");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700701
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600702// testRemove(this, manager, fib, face, "/");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700703
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600704// if (!static_cast<bool>(fib.findExactMatch("/a")) ||
705// !static_cast<bool>(fib.findExactMatch("/a/b")) ||
706// !static_cast<bool>(fib.findExactMatch("/a/b/c")))
707// {
708// BOOST_FAIL("Removed incorrect entry");
709// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700710
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600711// testRemove(this, manager, fib, face, "/a/b");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700712
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600713// if (!static_cast<bool>(fib.findExactMatch("/a")) ||
714// !static_cast<bool>(fib.findExactMatch("/a/b/c")))
715// {
716// BOOST_FAIL("Removed incorrect entry");
717// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700718
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600719// testRemove(this, manager, fib, face, "/a/b/c");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700720
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600721// if (!static_cast<bool>(fib.findExactMatch("/a")))
722// {
723// BOOST_FAIL("Removed incorrect entry");
724// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700725
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600726// testRemove(this, manager, fib, face, "/a");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700727
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600728// testRemove(this, manager, fib, face, "/does/not/exist");
729// }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700730
731bool
732removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
733{
734 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
735
736 if (static_cast<bool>(entry))
737 {
738 const fib::NextHopList& hops = entry->getNextHops();
739 return hops.size() == oldSize - 1 &&
740 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end();
741 }
742 return false;
743}
744
745void
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700746testRemoveNextHop(CommandFixture<FibManagerFixture>* fixture,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700747 FibManager& manager,
748 Fib& fib,
749 shared_ptr<Face> face,
750 const Name& targetName,
751 FaceId targetFace)
752{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800753 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700754 options.setName(targetName);
755 options.setFaceId(targetFace);
756
757 Block encodedOptions(options.wireEncode());
758
759 Name commandName("/localhost/nfd/fib");
760 commandName.append("remove-nexthop");
761 commandName.append(encodedOptions);
762
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700763 shared_ptr<Interest> command(make_shared<Interest>(commandName));
764 fixture->generateCommand(*command);
765
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700766 face->onReceiveData +=
767 bind(&FibManagerFixture::validateControlResponse, fixture, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700768 command->getName(), 200, "Success", encodedOptions);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700769
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700770 manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700771
772 BOOST_REQUIRE(fixture->didCallbackFire());
773
774 fixture->resetCallbackFired();
775 face->onReceiveData.clear();
776}
777
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700778BOOST_AUTO_TEST_CASE(RemoveNextHop)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700779{
780 shared_ptr<Face> face1 = make_shared<DummyFace>();
781 shared_ptr<Face> face2 = make_shared<DummyFace>();
782 shared_ptr<Face> face3 = make_shared<DummyFace>();
783
784 addFace(face1);
785 addFace(face2);
786 addFace(face3);
787
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700788 shared_ptr<InternalFace> face = getInternalFace();
789 FibManager& manager = getFibManager();
790 Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700791
792 shared_ptr<fib::Entry> entry = fib.insert("/hello").first;
793
794 entry->addNextHop(face1, 101);
795 entry->addNextHop(face2, 202);
796 entry->addNextHop(face3, 303);
797
798 testRemoveNextHop(this, manager, fib, face, "/hello", 2);
799 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202));
800
801 testRemoveNextHop(this, manager, fib, face, "/hello", 3);
802 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303));
803
804 testRemoveNextHop(this, manager, fib, face, "/hello", 1);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600805 // BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700806
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600807 BOOST_CHECK(!static_cast<bool>(getFib().findExactMatch("/hello")));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700808}
809
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700810BOOST_AUTO_TEST_CASE(RemoveNoFace)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700811{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700812 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700813
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800814 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700815 options.setName("/hello");
816 options.setFaceId(1);
817
818 Block encodedOptions(options.wireEncode());
819
820 Name commandName("/localhost/nfd/fib");
821 commandName.append("remove-nexthop");
822 commandName.append(encodedOptions);
823
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700824 shared_ptr<Interest> command(make_shared<Interest>(commandName));
825 generateCommand(*command);
826
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700827 face->onReceiveData +=
828 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700829 command->getName(), 404, "Face not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700830
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700831 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700832
833 BOOST_REQUIRE(didCallbackFire());
834}
835
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700836BOOST_AUTO_TEST_CASE(RemoveNoPrefix)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700837{
838 addFace(make_shared<DummyFace>());
839
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700840 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700841
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800842 FibManagementOptions options;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700843 options.setName("/hello");
844 options.setFaceId(1);
845
846 Block encodedOptions(options.wireEncode());
847
848 Name commandName("/localhost/nfd/fib");
849 commandName.append("remove-nexthop");
850 commandName.append(encodedOptions);
851
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700852 shared_ptr<Interest> command(make_shared<Interest>(commandName));
853 generateCommand(*command);
854
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700855 face->onReceiveData +=
856 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700857 command->getName(), 404, "Prefix not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700858
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700859 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700860
861 BOOST_REQUIRE(didCallbackFire());
862}
863
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700864BOOST_AUTO_TEST_SUITE_END()
865
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700866} // namespace tests
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700867} // namespace nfd