blob: 6f5a0065f43a6d31ce063902efc28a102a9a0990 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070025
26#include "mgmt/fib-manager.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070027#include "table/fib.hpp"
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070028#include "table/fib-nexthop.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070029#include "face/face.hpp"
Steve DiBenedetto3970c892014-01-31 23:31:13 -070030#include "mgmt/internal-face.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070031#include "tests/daemon/face/dummy-face.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070032
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070033#include "validation-common.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034#include "tests/test-common.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070035
Steve DiBenedetto6214e562014-03-15 16:27:04 -060036#include "fib-enumeration-publisher-common.hpp"
37
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070038namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070039namespace tests {
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070040
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070041NFD_LOG_INIT("FibManagerTest");
42
Alexander Afanasyevefea8fe2014-03-23 00:00:35 -070043class FibManagerFixture : public FibEnumerationPublisherFixture
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070044{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070045public:
46
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070047 virtual
48 ~FibManagerFixture()
Steve DiBenedettobdedce92014-02-02 22:49:39 -070049 {
Steve DiBenedettobdedce92014-02-02 22:49:39 -070050 }
51
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070052 shared_ptr<Face>
53 getFace(FaceId id)
54 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070055 if (id > 0 && static_cast<size_t>(id) <= m_faces.size())
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070056 {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070057 return m_faces[id - 1];
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070058 }
Steve DiBenedetto80ddc212014-02-01 22:23:56 -070059 NFD_LOG_DEBUG("No face found returning NULL");
60 return shared_ptr<DummyFace>();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070061 }
62
63 void
64 addFace(shared_ptr<Face> face)
65 {
66 m_faces.push_back(face);
67 }
68
Steve DiBenedettobdedce92014-02-02 22:49:39 -070069 void
Steve DiBenedetto2693db92014-02-10 15:58:36 -070070 validateControlResponseCommon(const Data& response,
71 const Name& expectedName,
72 uint32_t expectedCode,
73 const std::string& expectedText,
74 ControlResponse& control)
Steve DiBenedettobdedce92014-02-02 22:49:39 -070075 {
76 m_callbackFired = true;
77 Block controlRaw = response.getContent().blockFromValue();
78
Steve DiBenedettobdedce92014-02-02 22:49:39 -070079 control.wireDecode(controlRaw);
80
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070081 // NFD_LOG_DEBUG("received control response"
82 // << " Name: " << response.getName()
83 // << " code: " << control.getCode()
84 // << " text: " << control.getText());
Steve DiBenedettobdedce92014-02-02 22:49:39 -070085
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070086 BOOST_CHECK_EQUAL(response.getName(), expectedName);
87 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
88 BOOST_CHECK_EQUAL(control.getText(), expectedText);
Steve DiBenedettobdedce92014-02-02 22:49:39 -070089 }
90
Steve DiBenedetto2693db92014-02-10 15:58:36 -070091 void
92 validateControlResponse(const Data& response,
93 const Name& expectedName,
94 uint32_t expectedCode,
95 const std::string& expectedText)
96 {
97 ControlResponse control;
98 validateControlResponseCommon(response, expectedName,
99 expectedCode, expectedText, control);
100
101 if (!control.getBody().empty())
102 {
103 BOOST_FAIL("found unexpected control response body");
104 }
105 }
106
107 void
108 validateControlResponse(const Data& response,
109 const Name& expectedName,
110 uint32_t expectedCode,
111 const std::string& expectedText,
112 const Block& expectedBody)
113 {
114 ControlResponse control;
115 validateControlResponseCommon(response, expectedName,
116 expectedCode, expectedText, control);
117
118 BOOST_REQUIRE(!control.getBody().empty());
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600119 BOOST_REQUIRE_EQUAL(control.getBody().value_size(), expectedBody.value_size());
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700120
121 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
122 expectedBody.value_size()) == 0);
123
124 }
125
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700126 bool
127 didCallbackFire()
128 {
129 return m_callbackFired;
130 }
131
132 void
133 resetCallbackFired()
134 {
135 m_callbackFired = false;
136 }
137
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700138 shared_ptr<InternalFace>
139 getInternalFace()
140 {
141 return m_face;
142 }
143
144 FibManager&
145 getFibManager()
146 {
147 return m_manager;
148 }
149
150 Fib&
151 getFib()
152 {
153 return m_fib;
154 }
155
156 void
157 addInterestRule(const std::string& regex,
158 ndn::IdentityCertificate& certificate)
159 {
160 m_manager.addInterestRule(regex, certificate);
161 }
162
163protected:
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700164 FibManagerFixture()
Vince Lehman5144f822014-07-23 15:12:56 -0700165 : m_manager(ref(m_fib), bind(&FibManagerFixture::getFace, this, _1), m_face, m_keyChain)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700166 , m_callbackFired(false)
167 {
168 }
169
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600170protected:
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700171 FibManager m_manager;
172
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700173 std::vector<shared_ptr<Face> > m_faces;
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700174 bool m_callbackFired;
Vince Lehman5144f822014-07-23 15:12:56 -0700175 ndn::KeyChain m_keyChain;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700176};
177
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700178template <typename T>
179class AuthorizedCommandFixture : public CommandFixture<T>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700180{
181public:
182 AuthorizedCommandFixture()
183 {
184 const std::string regex = "^<localhost><nfd><fib>";
185 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
186 }
187
188 virtual
189 ~AuthorizedCommandFixture()
190 {
191 }
192};
193
194BOOST_FIXTURE_TEST_SUITE(MgmtFibManager, AuthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700195
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700196bool
197foundNextHop(FaceId id, uint32_t cost, const fib::NextHop& next)
198{
199 return id == next.getFace()->getId() && next.getCost() == cost;
200}
201
202bool
203addedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
204{
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700205 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700206
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700207 if (static_cast<bool>(entry))
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700208 {
209 const fib::NextHopList& hops = entry->getNextHops();
210 return hops.size() == oldSize + 1 &&
211 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
212 }
213 return false;
214}
215
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700216bool
217foundNextHopWithFace(FaceId id, uint32_t cost,
218 shared_ptr<Face> face, const fib::NextHop& next)
219{
220 return id == next.getFace()->getId() && next.getCost() == cost && face == next.getFace();
221}
222
223bool
224addedNextHopWithFace(const Fib& fib, const Name& prefix, size_t oldSize,
225 uint32_t cost, shared_ptr<Face> face)
226{
227 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
228
229 if (static_cast<bool>(entry))
230 {
231 const fib::NextHopList& hops = entry->getNextHops();
232 return hops.size() == oldSize + 1 &&
233 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end();
234 }
235 return false;
236}
237
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700238BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700239{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700240 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700241
Junxiao Shi72c3e042014-04-08 15:02:37 -0700242 shared_ptr<Interest> command = makeInterest("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700243
244 face->onReceiveData +=
245 bind(&FibManagerFixture::validateControlResponse, this, _1,
Junxiao Shi72c3e042014-04-08 15:02:37 -0700246 command->getName(), 400, "Malformed command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700247
Junxiao Shi72c3e042014-04-08 15:02:37 -0700248 face->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700249 g_io.run_one();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700250
251 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700252}
253
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700254BOOST_AUTO_TEST_CASE(MalformedCommmand)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700255{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700256 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700257
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700258 BOOST_REQUIRE(didCallbackFire() == false);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700259
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700260 Interest command("/localhost/nfd/fib");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700261
262 face->onReceiveData +=
263 bind(&FibManagerFixture::validateControlResponse, this, _1,
264 command.getName(), 400, "Malformed command");
265
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700266 getFibManager().onFibRequest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700267
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700268 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700269}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700270
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700271BOOST_AUTO_TEST_CASE(UnsupportedVerb)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700272{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700273 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700274
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600275 ControlParameters parameters;
276 parameters.setName("/hello");
277 parameters.setFaceId(1);
278 parameters.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700279
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600280 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700281
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700282 Name commandName("/localhost/nfd/fib");
283 commandName.append("unsupported");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600284 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700285
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700286 shared_ptr<Interest> command(make_shared<Interest>(commandName));
287 generateCommand(*command);
288
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700289 face->onReceiveData +=
290 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700291 command->getName(), 501, "Unsupported command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700292
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700293 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700294
295 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700296}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700297
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700298BOOST_AUTO_TEST_CASE(UnsignedCommand)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700299{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700300 addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700301
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700302 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700303
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600304 ControlParameters parameters;
305 parameters.setName("/hello");
306 parameters.setFaceId(1);
307 parameters.setCost(101);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700308
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600309 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700310
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700311 Name commandName("/localhost/nfd/fib");
312 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600313 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700314
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700315 Interest command(commandName);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700316
317 face->onReceiveData +=
318 bind(&FibManagerFixture::validateControlResponse,
319 this, _1, command.getName(), 401, "Signature required");
320
321
322 getFibManager().onFibRequest(command);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700323
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700324 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700325 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700326}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700327
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700328BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700329{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700330 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700331
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700332 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700333
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600334 ControlParameters parameters;
335 parameters.setName("/hello");
336 parameters.setFaceId(1);
337 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700338
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600339 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700340
341 Name commandName("/localhost/nfd/fib");
342 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600343 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700344
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700345 shared_ptr<Interest> command(make_shared<Interest>(commandName));
346 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700347
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700348 face->onReceiveData +=
349 bind(&FibManagerFixture::validateControlResponse,
350 this, _1, command->getName(), 403, "Unauthorized command");
351
352 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700353
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700354 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700355 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700356}
357
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700358BOOST_AUTO_TEST_CASE(BadOptionParse)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700359{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700360 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700361
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700362 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700363
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700364 Name commandName("/localhost/nfd/fib");
365 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600366 commandName.append("NotReallyParameters");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700367
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700368 shared_ptr<Interest> command(make_shared<Interest>(commandName));
369 generateCommand(*command);
370
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700371 face->onReceiveData +=
372 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700373 command->getName(), 400, "Malformed command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700374
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700375 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700376
377 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700378}
379
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700380BOOST_AUTO_TEST_CASE(UnknownFaceId)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700381{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700382 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700383
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700384 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700385
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600386 ControlParameters parameters;
387 parameters.setName("/hello");
388 parameters.setFaceId(1000);
389 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700390
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600391 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700392
393 Name commandName("/localhost/nfd/fib");
394 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600395 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700396
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700397 shared_ptr<Interest> command(make_shared<Interest>(commandName));
398 generateCommand(*command);
399
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700400 face->onReceiveData +=
401 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600402 command->getName(), 410, "Face not found");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700403
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700404 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700405
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700406 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700407 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101) == false);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700408}
409
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700410BOOST_AUTO_TEST_CASE(AddNextHopVerbImplicitFaceId)
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700411{
412 addFace(make_shared<DummyFace>());
413
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700414 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700415
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700416 std::vector<ControlParameters> testedParameters;
417 testedParameters.push_back(ControlParameters().setName("/hello").setCost(101).setFaceId(0));
418 testedParameters.push_back(ControlParameters().setName("/hello").setCost(101));
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700419
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700420 for (std::vector<ControlParameters>::iterator parameters = testedParameters.begin();
421 parameters != testedParameters.end(); ++parameters) {
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700422
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700423 Block encodedParameters(parameters->wireEncode());
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700424
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700425 Name commandName("/localhost/nfd/fib");
426 commandName.append("add-nexthop");
427 commandName.append(encodedParameters);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700428
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700429 ControlParameters expectedParameters;
430 expectedParameters.setName("/hello");
431 expectedParameters.setFaceId(1);
432 expectedParameters.setCost(101);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700433
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700434 Block encodedExpectedParameters(expectedParameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700435
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700436 shared_ptr<Interest> command(make_shared<Interest>(commandName));
437 command->setIncomingFaceId(1);
438 generateCommand(*command);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700439
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700440 face->onReceiveData +=
441 bind(&FibManagerFixture::validateControlResponse, this, _1,
442 command->getName(), 200, "Success", encodedExpectedParameters);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700443
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700444 getFibManager().onFibRequest(*command);
445
446 BOOST_REQUIRE(didCallbackFire());
447 BOOST_REQUIRE(addedNextHopWithFace(getFib(), "/hello", 0, 101, getFace(1)));
448
449 face->onReceiveData.clear();
450 getFib().erase("/hello");
451 BOOST_REQUIRE_EQUAL(getFib().size(), 0);
452 }
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700453}
454
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700455BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700456{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700457 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700458
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700459 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700460
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600461 ControlParameters parameters;
462 parameters.setName("/hello");
463 parameters.setFaceId(1);
464 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700465
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600466 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700467
468 Name commandName("/localhost/nfd/fib");
469 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600470 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700471
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700472 shared_ptr<Interest> command(make_shared<Interest>(commandName));
473 generateCommand(*command);
474
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700475 face->onReceiveData +=
476 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600477 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700478
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700479 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700480
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700481 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700482 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700483}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700484
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600485BOOST_AUTO_TEST_CASE(AddNextHopVerbImplicitCost)
486{
487 addFace(make_shared<DummyFace>());
488
489 shared_ptr<InternalFace> face = getInternalFace();
490
491 ControlParameters parameters;
492 parameters.setName("/hello");
493 parameters.setFaceId(1);
494
495 Block encodedParameters(parameters.wireEncode());
496
497 Name commandName("/localhost/nfd/fib");
498 commandName.append("add-nexthop");
499 commandName.append(encodedParameters);
500
501 shared_ptr<Interest> command(make_shared<Interest>(commandName));
502 generateCommand(*command);
503
504 ControlParameters resultParameters;
505 resultParameters.setName("/hello");
506 resultParameters.setFaceId(1);
507 resultParameters.setCost(0);
508
509 face->onReceiveData +=
510 bind(&FibManagerFixture::validateControlResponse, this, _1,
511 command->getName(), 200, "Success", resultParameters.wireEncode());
512
513 getFibManager().onFibRequest(*command);
514
515 BOOST_REQUIRE(didCallbackFire());
516 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 0));
517}
518
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700519BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700520{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700521 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700522 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700523
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700524 for (int i = 1; i <= 2; i++)
525 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700526
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600527 ControlParameters parameters;
528 parameters.setName("/hello");
529 parameters.setFaceId(1);
530 parameters.setCost(100 + i);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700531
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600532 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700533
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700534 Name commandName("/localhost/nfd/fib");
535 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600536 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700537
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700538 shared_ptr<Interest> command(make_shared<Interest>(commandName));
539 generateCommand(*command);
540
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700541 face->onReceiveData +=
542 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600543 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700544
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700545 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700546 BOOST_REQUIRE(didCallbackFire());
547 resetCallbackFired();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700548
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700549 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700550
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700551 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700552 {
553 const fib::NextHopList& hops = entry->getNextHops();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700554 BOOST_REQUIRE(hops.size() == 1);
555 BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(),
556 bind(&foundNextHop, -1, 100 + i, _1)) != hops.end());
557
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700558 }
559 else
560 {
561 BOOST_FAIL("Failed to find expected fib entry");
562 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700563
564 face->onReceiveData.clear();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700565 }
566}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700567
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700568BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700569{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700570 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700571 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700572
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600573 ControlParameters parameters;
574 parameters.setName("/hello");
575 parameters.setFaceId(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700576
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700577 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600578 parameters.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700579
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600580 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700581
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700582 Name commandName("/localhost/nfd/fib");
583 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600584 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700585
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700586 shared_ptr<Interest> command(make_shared<Interest>(commandName));
587 generateCommand(*command);
588
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700589 face->onReceiveData +=
590 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600591 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700592
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700593 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700594
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700595 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700596 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700597
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700598 resetCallbackFired();
599 face->onReceiveData.clear();
600
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700601 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600602 parameters.setCost(102);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700603
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600604 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700605
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700606 Name commandName("/localhost/nfd/fib");
607 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600608 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700609
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700610 shared_ptr<Interest> command(make_shared<Interest>(commandName));
611 generateCommand(*command);
612
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700613 face->onReceiveData +=
614 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600615 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700616
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700617 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700618
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700619 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700620 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700621
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700622 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700623
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700624 // Add faces with cost == FaceID for the name /hello
625 // This test assumes:
626 // FaceIDs are -1 because we don't add them to a forwarder
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700627 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700628 {
629 const fib::NextHopList& hops = entry->getNextHops();
630 BOOST_REQUIRE(hops.size() == 1);
631 BOOST_REQUIRE(std::find_if(hops.begin(),
632 hops.end(),
633 bind(&foundNextHop, -1, 102, _1)) != hops.end());
634 }
635 else
636 {
637 BOOST_FAIL("Failed to find expected fib entry");
638 }
639}
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700640
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600641BOOST_AUTO_TEST_CASE(AddNextHopVerbMissingPrefix)
642{
643 addFace(make_shared<DummyFace>());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700644
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600645 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700646
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600647 ControlParameters parameters;
648 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700649
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600650 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700651
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600652 Name commandName("/localhost/nfd/fib");
653 commandName.append("add-nexthop");
654 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700655
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600656 shared_ptr<Interest> command(make_shared<Interest>(commandName));
657 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700658
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600659 face->onReceiveData +=
660 bind(&FibManagerFixture::validateControlResponse, this, _1,
661 command->getName(), 400, "Malformed command");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700662
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600663 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700664
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600665 BOOST_REQUIRE(didCallbackFire());
666}
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700667
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700668bool
669removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
670{
671 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
672
673 if (static_cast<bool>(entry))
674 {
675 const fib::NextHopList& hops = entry->getNextHops();
676 return hops.size() == oldSize - 1 &&
677 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end();
678 }
679 return false;
680}
681
682void
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700683testRemoveNextHop(CommandFixture<FibManagerFixture>* fixture,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700684 FibManager& manager,
685 Fib& fib,
686 shared_ptr<Face> face,
687 const Name& targetName,
688 FaceId targetFace)
689{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600690 ControlParameters parameters;
691 parameters.setName(targetName);
692 parameters.setFaceId(targetFace);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700693
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600694 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700695
696 Name commandName("/localhost/nfd/fib");
697 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600698 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700699
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700700 shared_ptr<Interest> command(make_shared<Interest>(commandName));
701 fixture->generateCommand(*command);
702
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700703 face->onReceiveData +=
704 bind(&FibManagerFixture::validateControlResponse, fixture, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600705 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700706
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700707 manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700708
709 BOOST_REQUIRE(fixture->didCallbackFire());
710
711 fixture->resetCallbackFired();
712 face->onReceiveData.clear();
713}
714
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700715BOOST_AUTO_TEST_CASE(RemoveNextHop)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700716{
717 shared_ptr<Face> face1 = make_shared<DummyFace>();
718 shared_ptr<Face> face2 = make_shared<DummyFace>();
719 shared_ptr<Face> face3 = make_shared<DummyFace>();
720
721 addFace(face1);
722 addFace(face2);
723 addFace(face3);
724
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700725 shared_ptr<InternalFace> face = getInternalFace();
726 FibManager& manager = getFibManager();
727 Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700728
729 shared_ptr<fib::Entry> entry = fib.insert("/hello").first;
730
731 entry->addNextHop(face1, 101);
732 entry->addNextHop(face2, 202);
733 entry->addNextHop(face3, 303);
734
735 testRemoveNextHop(this, manager, fib, face, "/hello", 2);
736 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202));
737
738 testRemoveNextHop(this, manager, fib, face, "/hello", 3);
739 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303));
740
741 testRemoveNextHop(this, manager, fib, face, "/hello", 1);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600742 // BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700743
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600744 BOOST_CHECK(!static_cast<bool>(getFib().findExactMatch("/hello")));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700745}
746
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600747BOOST_AUTO_TEST_CASE(RemoveFaceNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700748{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700749 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700750
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600751 ControlParameters parameters;
752 parameters.setName("/hello");
753 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700754
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600755 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700756
757 Name commandName("/localhost/nfd/fib");
758 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600759 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700760
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700761 shared_ptr<Interest> command(make_shared<Interest>(commandName));
762 generateCommand(*command);
763
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700764 face->onReceiveData +=
765 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600766 command->getName(), 200, "Success", encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700767
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700768 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700769
770 BOOST_REQUIRE(didCallbackFire());
771}
772
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600773BOOST_AUTO_TEST_CASE(RemovePrefixNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700774{
775 addFace(make_shared<DummyFace>());
776
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700777 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700778
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600779 ControlParameters parameters;
780 parameters.setName("/hello");
781 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700782
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600783 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700784
785 Name commandName("/localhost/nfd/fib");
786 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600787 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700788
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700789 shared_ptr<Interest> command(make_shared<Interest>(commandName));
790 generateCommand(*command);
791
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700792 face->onReceiveData +=
793 bind(&FibManagerFixture::validateControlResponse, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600794 command->getName(), 200, "Success", encodedParameters);
795
796 getFibManager().onFibRequest(*command);
797
798 BOOST_REQUIRE(didCallbackFire());
799}
800
801BOOST_AUTO_TEST_CASE(RemoveMissingPrefix)
802{
803 addFace(make_shared<DummyFace>());
804
805 shared_ptr<InternalFace> face = getInternalFace();
806
807 ControlParameters parameters;
808 parameters.setFaceId(1);
809
810 Block encodedParameters(parameters.wireEncode());
811
812 Name commandName("/localhost/nfd/fib");
813 commandName.append("remove-nexthop");
814 commandName.append(encodedParameters);
815
816 shared_ptr<Interest> command(make_shared<Interest>(commandName));
817 generateCommand(*command);
818
819 face->onReceiveData +=
820 bind(&FibManagerFixture::validateControlResponse, this, _1,
821 command->getName(), 400, "Malformed command");
822
823 getFibManager().onFibRequest(*command);
824
825 BOOST_REQUIRE(didCallbackFire());
826}
827
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700828BOOST_AUTO_TEST_CASE(RemoveImplicitFaceId)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600829{
830 addFace(make_shared<DummyFace>());
831
832 shared_ptr<InternalFace> face = getInternalFace();
833
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700834 std::vector<ControlParameters> testedParameters;
835 testedParameters.push_back(ControlParameters().setName("/hello").setFaceId(0));
836 testedParameters.push_back(ControlParameters().setName("/hello"));
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600837
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700838 for (std::vector<ControlParameters>::iterator parameters = testedParameters.begin();
839 parameters != testedParameters.end(); ++parameters) {
840 Block encodedParameters(parameters->wireEncode());
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600841
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700842 Name commandName("/localhost/nfd/fib");
843 commandName.append("remove-nexthop");
844 commandName.append(encodedParameters);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600845
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700846 shared_ptr<Interest> command(make_shared<Interest>(commandName));
847 command->setIncomingFaceId(1);
848 generateCommand(*command);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600849
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700850 ControlParameters resultParameters;
851 resultParameters.setFaceId(1);
852 resultParameters.setName("/hello");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700853
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700854 face->onReceiveData +=
855 bind(&FibManagerFixture::validateControlResponse, this, _1,
856 command->getName(), 200, "Success", resultParameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700857
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700858 getFibManager().onFibRequest(*command);
859
860 BOOST_REQUIRE(didCallbackFire());
861
862 face->onReceiveData.clear();
863 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700864}
865
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600866BOOST_FIXTURE_TEST_CASE(TestFibEnumerationRequest, FibManagerFixture)
867{
868 for (int i = 0; i < 87; i++)
869 {
870 Name prefix("/test");
871 prefix.appendSegment(i);
872
873 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
874 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
875
876 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
877 entry->addNextHop(dummy1, std::numeric_limits<uint64_t>::max() - 1);
878 entry->addNextHop(dummy2, std::numeric_limits<uint64_t>::max() - 2);
879
880 m_referenceEntries.insert(entry);
881 }
882 for (int i = 0; i < 2; i++)
883 {
884 Name prefix("/test2");
885 prefix.appendSegment(i);
886
887 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
888 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
889
890 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
891 entry->addNextHop(dummy1, std::numeric_limits<uint8_t>::max() - 1);
892 entry->addNextHop(dummy2, std::numeric_limits<uint8_t>::max() - 2);
893
894 m_referenceEntries.insert(entry);
895 }
896
897 ndn::EncodingBuffer buffer;
898
899 m_face->onReceiveData +=
900 bind(&FibEnumerationPublisherFixture::decodeFibEntryBlock, this, _1);
901
902 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/fib/list"));
903
904 m_manager.onFibRequest(*command);
905 BOOST_REQUIRE(m_finished);
906}
907
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700908BOOST_AUTO_TEST_SUITE_END()
909
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700910} // namespace tests
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700911} // namespace nfd