blob: 525dfc8e668d8352264bec301104ef4254ba701e [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
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700238BOOST_AUTO_TEST_CASE(ShortName)
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
Junxiao Shicd55cde2014-11-13 16:03:24 -0700244 face->onReceiveData += [this, command] (const Data& response) {
245 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
246 };
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
Junxiao Shicd55cde2014-11-13 16:03:24 -0700262 face->onReceiveData += [this, command] (const Data& response) {
263 this->validateControlResponse(response, command.getName(), 400, "Malformed command");
264 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700265
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
Junxiao Shicd55cde2014-11-13 16:03:24 -0700289 face->onReceiveData += [this, command] (const Data& response) {
290 this->validateControlResponse(response, command->getName(), 501, "Unsupported command");
291 };
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
Junxiao Shicd55cde2014-11-13 16:03:24 -0700317 face->onReceiveData += [this, command] (const Data& response) {
318 this->validateControlResponse(response, command.getName(), 401, "Signature required");
319 };
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700320
321 getFibManager().onFibRequest(command);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700322
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700323 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700324 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700325}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700326
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700327BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FibManagerFixture>)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700328{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700329 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700330
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700331 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700332
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600333 ControlParameters parameters;
334 parameters.setName("/hello");
335 parameters.setFaceId(1);
336 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700337
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600338 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700339
340 Name commandName("/localhost/nfd/fib");
341 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600342 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700343
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700344 shared_ptr<Interest> command(make_shared<Interest>(commandName));
345 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700346
Junxiao Shicd55cde2014-11-13 16:03:24 -0700347 face->onReceiveData += [this, command] (const Data& response) {
348 this->validateControlResponse(response, command->getName(), 403, "Unauthorized command");
349 };
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700350
351 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700352
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700353 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700354 BOOST_REQUIRE(!addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700355}
356
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700357BOOST_AUTO_TEST_CASE(BadOptionParse)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700358{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700359 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700360
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700361 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700362
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700363 Name commandName("/localhost/nfd/fib");
364 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600365 commandName.append("NotReallyParameters");
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700366
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700367 shared_ptr<Interest> command(make_shared<Interest>(commandName));
368 generateCommand(*command);
369
Junxiao Shicd55cde2014-11-13 16:03:24 -0700370 face->onReceiveData += [this, command] (const Data& response) {
371 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
372 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700373
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700374 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700375
376 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700377}
378
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700379BOOST_AUTO_TEST_CASE(UnknownFaceId)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700380{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700381 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700382
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700383 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700384
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600385 ControlParameters parameters;
386 parameters.setName("/hello");
387 parameters.setFaceId(1000);
388 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700389
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600390 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700391
392 Name commandName("/localhost/nfd/fib");
393 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600394 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700395
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700396 shared_ptr<Interest> command(make_shared<Interest>(commandName));
397 generateCommand(*command);
398
Junxiao Shicd55cde2014-11-13 16:03:24 -0700399 face->onReceiveData += [this, command] (const Data& response) {
400 this->validateControlResponse(response, command->getName(), 410, "Face not found");
401 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700402
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700403 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700404
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700405 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700406 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101) == false);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700407}
408
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700409BOOST_AUTO_TEST_CASE(AddNextHopVerbImplicitFaceId)
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700410{
411 addFace(make_shared<DummyFace>());
412
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700413 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700414
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700415 std::vector<ControlParameters> testedParameters;
416 testedParameters.push_back(ControlParameters().setName("/hello").setCost(101).setFaceId(0));
417 testedParameters.push_back(ControlParameters().setName("/hello").setCost(101));
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700418
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700419 for (std::vector<ControlParameters>::iterator parameters = testedParameters.begin();
420 parameters != testedParameters.end(); ++parameters) {
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700421
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700422 Block encodedParameters(parameters->wireEncode());
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700423
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700424 Name commandName("/localhost/nfd/fib");
425 commandName.append("add-nexthop");
426 commandName.append(encodedParameters);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700427
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700428 ControlParameters expectedParameters;
429 expectedParameters.setName("/hello");
430 expectedParameters.setFaceId(1);
431 expectedParameters.setCost(101);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700432
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700433 Block encodedExpectedParameters(expectedParameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700434
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700435 shared_ptr<Interest> command(make_shared<Interest>(commandName));
436 command->setIncomingFaceId(1);
437 generateCommand(*command);
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700438
Junxiao Shicd55cde2014-11-13 16:03:24 -0700439 face->onReceiveData += [this, command, encodedExpectedParameters] (const Data& response) {
440 this->validateControlResponse(response, command->getName(),
441 200, "Success", encodedExpectedParameters);
442 };
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
Junxiao Shicd55cde2014-11-13 16:03:24 -0700475 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
476 this->validateControlResponse(response, command->getName(),
477 200, "Success", encodedParameters);
478 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700479
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700480 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700481
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700482 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700483 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700484}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700485
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600486BOOST_AUTO_TEST_CASE(AddNextHopVerbImplicitCost)
487{
488 addFace(make_shared<DummyFace>());
489
490 shared_ptr<InternalFace> face = getInternalFace();
491
492 ControlParameters parameters;
493 parameters.setName("/hello");
494 parameters.setFaceId(1);
495
496 Block encodedParameters(parameters.wireEncode());
497
498 Name commandName("/localhost/nfd/fib");
499 commandName.append("add-nexthop");
500 commandName.append(encodedParameters);
501
502 shared_ptr<Interest> command(make_shared<Interest>(commandName));
503 generateCommand(*command);
504
505 ControlParameters resultParameters;
506 resultParameters.setName("/hello");
507 resultParameters.setFaceId(1);
508 resultParameters.setCost(0);
509
Junxiao Shicd55cde2014-11-13 16:03:24 -0700510 face->onReceiveData += [this, command, resultParameters] (const Data& response) {
511 this->validateControlResponse(response, command->getName(),
512 200, "Success", resultParameters.wireEncode());
513 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600514
515 getFibManager().onFibRequest(*command);
516
517 BOOST_REQUIRE(didCallbackFire());
518 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 0));
519}
520
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700521BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700522{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700523 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700524 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700525
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700526 for (int i = 1; i <= 2; i++)
527 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700528
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600529 ControlParameters parameters;
530 parameters.setName("/hello");
531 parameters.setFaceId(1);
532 parameters.setCost(100 + i);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700533
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600534 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700535
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700536 Name commandName("/localhost/nfd/fib");
537 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600538 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700539
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700540 shared_ptr<Interest> command(make_shared<Interest>(commandName));
541 generateCommand(*command);
542
Junxiao Shicd55cde2014-11-13 16:03:24 -0700543 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
544 this->validateControlResponse(response, command->getName(),
545 200, "Success", encodedParameters);
546 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700547
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700548 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700549 BOOST_REQUIRE(didCallbackFire());
550 resetCallbackFired();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700551
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700552 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700553
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700554 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700555 {
556 const fib::NextHopList& hops = entry->getNextHops();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700557 BOOST_REQUIRE(hops.size() == 1);
558 BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(),
559 bind(&foundNextHop, -1, 100 + i, _1)) != hops.end());
560
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700561 }
562 else
563 {
564 BOOST_FAIL("Failed to find expected fib entry");
565 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700566
567 face->onReceiveData.clear();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700568 }
569}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700570
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700571BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700572{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700573 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700574 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700575
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600576 ControlParameters parameters;
577 parameters.setName("/hello");
578 parameters.setFaceId(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700579
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700580 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600581 parameters.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700582
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600583 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700584
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700585 Name commandName("/localhost/nfd/fib");
586 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600587 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700588
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700589 shared_ptr<Interest> command(make_shared<Interest>(commandName));
590 generateCommand(*command);
591
Junxiao Shicd55cde2014-11-13 16:03:24 -0700592 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
593 this->validateControlResponse(response, command->getName(),
594 200, "Success", encodedParameters);
595 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700596
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700597 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700598
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700599 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700600 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700601
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700602 resetCallbackFired();
603 face->onReceiveData.clear();
604
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700605 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600606 parameters.setCost(102);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700607
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600608 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700609
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700610 Name commandName("/localhost/nfd/fib");
611 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600612 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700613
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700614 shared_ptr<Interest> command(make_shared<Interest>(commandName));
615 generateCommand(*command);
616
Junxiao Shicd55cde2014-11-13 16:03:24 -0700617 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
618 this->validateControlResponse(response, command->getName(),
619 200, "Success", encodedParameters);
620 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700621
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700622 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700623
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700624 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700625 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700626
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700627 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700628
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700629 // Add faces with cost == FaceID for the name /hello
630 // This test assumes:
631 // FaceIDs are -1 because we don't add them to a forwarder
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700632 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700633 {
634 const fib::NextHopList& hops = entry->getNextHops();
635 BOOST_REQUIRE(hops.size() == 1);
636 BOOST_REQUIRE(std::find_if(hops.begin(),
637 hops.end(),
638 bind(&foundNextHop, -1, 102, _1)) != hops.end());
639 }
640 else
641 {
642 BOOST_FAIL("Failed to find expected fib entry");
643 }
644}
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700645
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600646BOOST_AUTO_TEST_CASE(AddNextHopVerbMissingPrefix)
647{
648 addFace(make_shared<DummyFace>());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700649
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600650 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700651
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600652 ControlParameters parameters;
653 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700654
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600655 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700656
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600657 Name commandName("/localhost/nfd/fib");
658 commandName.append("add-nexthop");
659 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700660
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600661 shared_ptr<Interest> command(make_shared<Interest>(commandName));
662 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700663
Junxiao Shicd55cde2014-11-13 16:03:24 -0700664 face->onReceiveData += [this, command] (const Data& response) {
665 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
666 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700667
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600668 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700669
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600670 BOOST_REQUIRE(didCallbackFire());
671}
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700672
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700673bool
674removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
675{
676 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
677
678 if (static_cast<bool>(entry))
679 {
680 const fib::NextHopList& hops = entry->getNextHops();
681 return hops.size() == oldSize - 1 &&
682 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end();
683 }
684 return false;
685}
686
687void
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700688testRemoveNextHop(CommandFixture<FibManagerFixture>* fixture,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700689 FibManager& manager,
690 Fib& fib,
691 shared_ptr<Face> face,
692 const Name& targetName,
693 FaceId targetFace)
694{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600695 ControlParameters parameters;
696 parameters.setName(targetName);
697 parameters.setFaceId(targetFace);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700698
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600699 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700700
701 Name commandName("/localhost/nfd/fib");
702 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600703 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700704
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700705 shared_ptr<Interest> command(make_shared<Interest>(commandName));
706 fixture->generateCommand(*command);
707
Junxiao Shicd55cde2014-11-13 16:03:24 -0700708 face->onReceiveData += [fixture, command, encodedParameters] (const Data& response) {
709 fixture->validateControlResponse(response, command->getName(),
710 200, "Success", encodedParameters);
711 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700712
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700713 manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700714
715 BOOST_REQUIRE(fixture->didCallbackFire());
716
717 fixture->resetCallbackFired();
718 face->onReceiveData.clear();
719}
720
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700721BOOST_AUTO_TEST_CASE(RemoveNextHop)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700722{
723 shared_ptr<Face> face1 = make_shared<DummyFace>();
724 shared_ptr<Face> face2 = make_shared<DummyFace>();
725 shared_ptr<Face> face3 = make_shared<DummyFace>();
726
727 addFace(face1);
728 addFace(face2);
729 addFace(face3);
730
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700731 shared_ptr<InternalFace> face = getInternalFace();
732 FibManager& manager = getFibManager();
733 Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700734
735 shared_ptr<fib::Entry> entry = fib.insert("/hello").first;
736
737 entry->addNextHop(face1, 101);
738 entry->addNextHop(face2, 202);
739 entry->addNextHop(face3, 303);
740
741 testRemoveNextHop(this, manager, fib, face, "/hello", 2);
742 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202));
743
744 testRemoveNextHop(this, manager, fib, face, "/hello", 3);
745 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303));
746
747 testRemoveNextHop(this, manager, fib, face, "/hello", 1);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600748 // BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700749
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600750 BOOST_CHECK(!static_cast<bool>(getFib().findExactMatch("/hello")));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700751}
752
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600753BOOST_AUTO_TEST_CASE(RemoveFaceNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700754{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700755 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700756
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600757 ControlParameters parameters;
758 parameters.setName("/hello");
759 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700760
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600761 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700762
763 Name commandName("/localhost/nfd/fib");
764 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600765 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700766
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700767 shared_ptr<Interest> command(make_shared<Interest>(commandName));
768 generateCommand(*command);
769
Junxiao Shicd55cde2014-11-13 16:03:24 -0700770 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
771 this->validateControlResponse(response, command->getName(),
772 200, "Success", encodedParameters);
773 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700774
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700775 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700776
777 BOOST_REQUIRE(didCallbackFire());
778}
779
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600780BOOST_AUTO_TEST_CASE(RemovePrefixNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700781{
782 addFace(make_shared<DummyFace>());
783
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700784 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700785
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600786 ControlParameters parameters;
787 parameters.setName("/hello");
788 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700789
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600790 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700791
792 Name commandName("/localhost/nfd/fib");
793 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600794 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700795
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700796 shared_ptr<Interest> command(make_shared<Interest>(commandName));
797 generateCommand(*command);
798
Junxiao Shicd55cde2014-11-13 16:03:24 -0700799 face->onReceiveData += [this, command, encodedParameters] (const Data& response) {
800 this->validateControlResponse(response, command->getName(),
801 200, "Success", encodedParameters);
802 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600803
804 getFibManager().onFibRequest(*command);
805
806 BOOST_REQUIRE(didCallbackFire());
807}
808
809BOOST_AUTO_TEST_CASE(RemoveMissingPrefix)
810{
811 addFace(make_shared<DummyFace>());
812
813 shared_ptr<InternalFace> face = getInternalFace();
814
815 ControlParameters parameters;
816 parameters.setFaceId(1);
817
818 Block encodedParameters(parameters.wireEncode());
819
820 Name commandName("/localhost/nfd/fib");
821 commandName.append("remove-nexthop");
822 commandName.append(encodedParameters);
823
824 shared_ptr<Interest> command(make_shared<Interest>(commandName));
825 generateCommand(*command);
826
Junxiao Shicd55cde2014-11-13 16:03:24 -0700827 face->onReceiveData += [this, command] (const Data& response) {
828 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
829 };
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600830
831 getFibManager().onFibRequest(*command);
832
833 BOOST_REQUIRE(didCallbackFire());
834}
835
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700836BOOST_AUTO_TEST_CASE(RemoveImplicitFaceId)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600837{
838 addFace(make_shared<DummyFace>());
839
840 shared_ptr<InternalFace> face = getInternalFace();
841
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700842 std::vector<ControlParameters> testedParameters;
843 testedParameters.push_back(ControlParameters().setName("/hello").setFaceId(0));
844 testedParameters.push_back(ControlParameters().setName("/hello"));
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600845
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700846 for (std::vector<ControlParameters>::iterator parameters = testedParameters.begin();
847 parameters != testedParameters.end(); ++parameters) {
848 Block encodedParameters(parameters->wireEncode());
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600849
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700850 Name commandName("/localhost/nfd/fib");
851 commandName.append("remove-nexthop");
852 commandName.append(encodedParameters);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600853
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700854 shared_ptr<Interest> command(make_shared<Interest>(commandName));
855 command->setIncomingFaceId(1);
856 generateCommand(*command);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600857
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700858 ControlParameters resultParameters;
859 resultParameters.setFaceId(1);
860 resultParameters.setName("/hello");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700861
Junxiao Shicd55cde2014-11-13 16:03:24 -0700862 face->onReceiveData += [this, command, resultParameters] (const Data& response) {
863 this->validateControlResponse(response, command->getName(),
864 200, "Success", resultParameters.wireEncode());
865 };
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700866
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700867 getFibManager().onFibRequest(*command);
868
869 BOOST_REQUIRE(didCallbackFire());
870
871 face->onReceiveData.clear();
872 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700873}
874
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600875BOOST_FIXTURE_TEST_CASE(TestFibEnumerationRequest, FibManagerFixture)
876{
877 for (int i = 0; i < 87; i++)
878 {
879 Name prefix("/test");
880 prefix.appendSegment(i);
881
882 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
883 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
884
885 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
886 entry->addNextHop(dummy1, std::numeric_limits<uint64_t>::max() - 1);
887 entry->addNextHop(dummy2, std::numeric_limits<uint64_t>::max() - 2);
888
889 m_referenceEntries.insert(entry);
890 }
891 for (int i = 0; i < 2; i++)
892 {
893 Name prefix("/test2");
894 prefix.appendSegment(i);
895
896 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
897 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
898
899 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
900 entry->addNextHop(dummy1, std::numeric_limits<uint8_t>::max() - 1);
901 entry->addNextHop(dummy2, std::numeric_limits<uint8_t>::max() - 2);
902
903 m_referenceEntries.insert(entry);
904 }
905
906 ndn::EncodingBuffer buffer;
907
908 m_face->onReceiveData +=
909 bind(&FibEnumerationPublisherFixture::decodeFibEntryBlock, this, _1);
910
911 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/fib/list"));
912
913 m_manager.onFibRequest(*command);
914 BOOST_REQUIRE(m_finished);
915}
916
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700917BOOST_AUTO_TEST_SUITE_END()
918
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700919} // namespace tests
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700920} // namespace nfd