blob: 82e70b9481b53450660fd053dc41cbe54583eba8 [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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 Shic099ddb2014-12-25 20:53:20 -0700244 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700245 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700246 });
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 Shic099ddb2014-12-25 20:53:20 -0700262 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700263 this->validateControlResponse(response, command.getName(), 400, "Malformed command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700264 });
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 Shic099ddb2014-12-25 20:53:20 -0700289 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700290 this->validateControlResponse(response, command->getName(), 501, "Unsupported command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700291 });
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 Shic099ddb2014-12-25 20:53:20 -0700317 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700318 this->validateControlResponse(response, command.getName(), 401, "Signature required");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700319 });
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 Shic099ddb2014-12-25 20:53:20 -0700347 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700348 this->validateControlResponse(response, command->getName(), 403, "Unauthorized command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700349 });
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 Shic099ddb2014-12-25 20:53:20 -0700370 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700371 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700372 });
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 Shic099ddb2014-12-25 20:53:20 -0700399 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700400 this->validateControlResponse(response, command->getName(), 410, "Face not found");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700401 });
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 Shic099ddb2014-12-25 20:53:20 -0700439 signal::Connection conn = face->onReceiveData.connect(
440 [this, command, encodedExpectedParameters] (const Data& response) {
441 this->validateControlResponse(response, command->getName(),
442 200, "Success", encodedExpectedParameters);
443 });
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700444
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700445 getFibManager().onFibRequest(*command);
446
447 BOOST_REQUIRE(didCallbackFire());
448 BOOST_REQUIRE(addedNextHopWithFace(getFib(), "/hello", 0, 101, getFace(1)));
449
Junxiao Shic099ddb2014-12-25 20:53:20 -0700450 conn.disconnect();
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700451 getFib().erase("/hello");
452 BOOST_REQUIRE_EQUAL(getFib().size(), 0);
453 }
Steve DiBenedetto2693db92014-02-10 15:58:36 -0700454}
455
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700456BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd)
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700457{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700458 addFace(make_shared<DummyFace>());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700459
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700460 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700461
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600462 ControlParameters parameters;
463 parameters.setName("/hello");
464 parameters.setFaceId(1);
465 parameters.setCost(101);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700466
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600467 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700468
469 Name commandName("/localhost/nfd/fib");
470 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600471 commandName.append(encodedParameters);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700472
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700473 shared_ptr<Interest> command(make_shared<Interest>(commandName));
474 generateCommand(*command);
475
Junxiao Shic099ddb2014-12-25 20:53:20 -0700476 face->onReceiveData.connect([this, command, encodedParameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700477 this->validateControlResponse(response, command->getName(),
478 200, "Success", encodedParameters);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700479 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700480
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700481 getFibManager().onFibRequest(*command);
Steve DiBenedetto80ddc212014-02-01 22:23:56 -0700482
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700483 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700484 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 101));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700485}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700486
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600487BOOST_AUTO_TEST_CASE(AddNextHopVerbImplicitCost)
488{
489 addFace(make_shared<DummyFace>());
490
491 shared_ptr<InternalFace> face = getInternalFace();
492
493 ControlParameters parameters;
494 parameters.setName("/hello");
495 parameters.setFaceId(1);
496
497 Block encodedParameters(parameters.wireEncode());
498
499 Name commandName("/localhost/nfd/fib");
500 commandName.append("add-nexthop");
501 commandName.append(encodedParameters);
502
503 shared_ptr<Interest> command(make_shared<Interest>(commandName));
504 generateCommand(*command);
505
506 ControlParameters resultParameters;
507 resultParameters.setName("/hello");
508 resultParameters.setFaceId(1);
509 resultParameters.setCost(0);
510
Junxiao Shic099ddb2014-12-25 20:53:20 -0700511 face->onReceiveData.connect([this, command, resultParameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700512 this->validateControlResponse(response, command->getName(),
513 200, "Success", resultParameters.wireEncode());
Junxiao Shic099ddb2014-12-25 20:53:20 -0700514 });
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600515
516 getFibManager().onFibRequest(*command);
517
518 BOOST_REQUIRE(didCallbackFire());
519 BOOST_REQUIRE(addedNextHopWithCost(getFib(), "/hello", 0, 0));
520}
521
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700522BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700523{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700524 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700525 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700526
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700527 for (int i = 1; i <= 2; i++)
528 {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700529
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600530 ControlParameters parameters;
531 parameters.setName("/hello");
532 parameters.setFaceId(1);
533 parameters.setCost(100 + i);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700534
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600535 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700536
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700537 Name commandName("/localhost/nfd/fib");
538 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600539 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700540
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700541 shared_ptr<Interest> command(make_shared<Interest>(commandName));
542 generateCommand(*command);
543
Junxiao Shic099ddb2014-12-25 20:53:20 -0700544 signal::Connection conn = face->onReceiveData.connect(
545 [this, command, encodedParameters] (const Data& response) {
546 this->validateControlResponse(response, command->getName(),
547 200, "Success", encodedParameters);
548 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700549
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700550 getFibManager().onFibRequest(*command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700551 BOOST_REQUIRE(didCallbackFire());
552 resetCallbackFired();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700553
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700554 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700555
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700556 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700557 {
558 const fib::NextHopList& hops = entry->getNextHops();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700559 BOOST_REQUIRE(hops.size() == 1);
560 BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(),
561 bind(&foundNextHop, -1, 100 + i, _1)) != hops.end());
562
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700563 }
564 else
565 {
566 BOOST_FAIL("Failed to find expected fib entry");
567 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700568
Junxiao Shic099ddb2014-12-25 20:53:20 -0700569 conn.disconnect();
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700570 }
571}
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700572
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700573BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700574{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700575 addFace(make_shared<DummyFace>());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700576 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700577
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600578 ControlParameters parameters;
579 parameters.setName("/hello");
580 parameters.setFaceId(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700581
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700582 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600583 parameters.setCost(1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700584
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600585 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700586
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700587 Name commandName("/localhost/nfd/fib");
588 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600589 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700590
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700591 shared_ptr<Interest> command(make_shared<Interest>(commandName));
592 generateCommand(*command);
593
Junxiao Shic099ddb2014-12-25 20:53:20 -0700594 signal::Connection conn = face->onReceiveData.connect(
595 [this, command, encodedParameters] (const Data& response) {
596 this->validateControlResponse(response, command->getName(),
597 200, "Success", encodedParameters);
598 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700599
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700600 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700601
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700602 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700603
Junxiao Shic099ddb2014-12-25 20:53:20 -0700604 resetCallbackFired();
605 conn.disconnect();
606 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700607
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700608 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600609 parameters.setCost(102);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700610
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600611 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700612
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700613 Name commandName("/localhost/nfd/fib");
614 commandName.append("add-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600615 commandName.append(encodedParameters);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700616
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700617 shared_ptr<Interest> command(make_shared<Interest>(commandName));
618 generateCommand(*command);
619
Junxiao Shic099ddb2014-12-25 20:53:20 -0700620 face->onReceiveData.connect([this, command, encodedParameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700621 this->validateControlResponse(response, command->getName(),
622 200, "Success", encodedParameters);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700623 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700624
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700625 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700626
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700627 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700628 }
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700629
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700630 shared_ptr<fib::Entry> entry = getFib().findExactMatch("/hello");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700631
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700632 // Add faces with cost == FaceID for the name /hello
633 // This test assumes:
634 // FaceIDs are -1 because we don't add them to a forwarder
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700635 if (static_cast<bool>(entry))
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700636 {
637 const fib::NextHopList& hops = entry->getNextHops();
638 BOOST_REQUIRE(hops.size() == 1);
639 BOOST_REQUIRE(std::find_if(hops.begin(),
640 hops.end(),
641 bind(&foundNextHop, -1, 102, _1)) != hops.end());
642 }
643 else
644 {
645 BOOST_FAIL("Failed to find expected fib entry");
646 }
647}
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700648
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600649BOOST_AUTO_TEST_CASE(AddNextHopVerbMissingPrefix)
650{
651 addFace(make_shared<DummyFace>());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700652
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600653 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700654
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600655 ControlParameters parameters;
656 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700657
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600658 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700659
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600660 Name commandName("/localhost/nfd/fib");
661 commandName.append("add-nexthop");
662 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700663
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600664 shared_ptr<Interest> command(make_shared<Interest>(commandName));
665 generateCommand(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700666
Junxiao Shic099ddb2014-12-25 20:53:20 -0700667 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700668 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700669 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700670
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600671 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700672
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600673 BOOST_REQUIRE(didCallbackFire());
674}
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700675
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700676bool
677removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost)
678{
679 shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix);
680
681 if (static_cast<bool>(entry))
682 {
683 const fib::NextHopList& hops = entry->getNextHops();
684 return hops.size() == oldSize - 1 &&
685 std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end();
686 }
687 return false;
688}
689
690void
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700691testRemoveNextHop(CommandFixture<FibManagerFixture>* fixture,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700692 FibManager& manager,
693 Fib& fib,
694 shared_ptr<Face> face,
695 const Name& targetName,
696 FaceId targetFace)
697{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600698 ControlParameters parameters;
699 parameters.setName(targetName);
700 parameters.setFaceId(targetFace);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700701
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600702 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700703
704 Name commandName("/localhost/nfd/fib");
705 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600706 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700707
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700708 shared_ptr<Interest> command(make_shared<Interest>(commandName));
709 fixture->generateCommand(*command);
710
Junxiao Shic099ddb2014-12-25 20:53:20 -0700711 signal::Connection conn = face->onReceiveData.connect(
712 [fixture, command, encodedParameters] (const Data& response) {
713 fixture->validateControlResponse(response, command->getName(),
714 200, "Success", encodedParameters);
715 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700716
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700717 manager.onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700718
719 BOOST_REQUIRE(fixture->didCallbackFire());
720
721 fixture->resetCallbackFired();
Junxiao Shic099ddb2014-12-25 20:53:20 -0700722 conn.disconnect();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700723}
724
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700725BOOST_AUTO_TEST_CASE(RemoveNextHop)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700726{
727 shared_ptr<Face> face1 = make_shared<DummyFace>();
728 shared_ptr<Face> face2 = make_shared<DummyFace>();
729 shared_ptr<Face> face3 = make_shared<DummyFace>();
730
731 addFace(face1);
732 addFace(face2);
733 addFace(face3);
734
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700735 shared_ptr<InternalFace> face = getInternalFace();
736 FibManager& manager = getFibManager();
737 Fib& fib = getFib();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700738
739 shared_ptr<fib::Entry> entry = fib.insert("/hello").first;
740
741 entry->addNextHop(face1, 101);
742 entry->addNextHop(face2, 202);
743 entry->addNextHop(face3, 303);
744
745 testRemoveNextHop(this, manager, fib, face, "/hello", 2);
746 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202));
747
748 testRemoveNextHop(this, manager, fib, face, "/hello", 3);
749 BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303));
750
751 testRemoveNextHop(this, manager, fib, face, "/hello", 1);
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600752 // BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700753
Steve DiBenedettod030cfc2014-03-10 20:04:47 -0600754 BOOST_CHECK(!static_cast<bool>(getFib().findExactMatch("/hello")));
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700755}
756
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600757BOOST_AUTO_TEST_CASE(RemoveFaceNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700758{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700759 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700760
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600761 ControlParameters parameters;
762 parameters.setName("/hello");
763 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700764
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600765 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700766
767 Name commandName("/localhost/nfd/fib");
768 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600769 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700770
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700771 shared_ptr<Interest> command(make_shared<Interest>(commandName));
772 generateCommand(*command);
773
Junxiao Shic099ddb2014-12-25 20:53:20 -0700774 face->onReceiveData.connect([this, command, encodedParameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700775 this->validateControlResponse(response, command->getName(),
776 200, "Success", encodedParameters);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700777 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700778
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700779 getFibManager().onFibRequest(*command);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700780
781 BOOST_REQUIRE(didCallbackFire());
782}
783
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600784BOOST_AUTO_TEST_CASE(RemovePrefixNotFound)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700785{
786 addFace(make_shared<DummyFace>());
787
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700788 shared_ptr<InternalFace> face = getInternalFace();
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700789
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600790 ControlParameters parameters;
791 parameters.setName("/hello");
792 parameters.setFaceId(1);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700793
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600794 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700795
796 Name commandName("/localhost/nfd/fib");
797 commandName.append("remove-nexthop");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600798 commandName.append(encodedParameters);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700799
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700800 shared_ptr<Interest> command(make_shared<Interest>(commandName));
801 generateCommand(*command);
802
Junxiao Shic099ddb2014-12-25 20:53:20 -0700803 face->onReceiveData.connect([this, command, encodedParameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700804 this->validateControlResponse(response, command->getName(),
805 200, "Success", encodedParameters);
Junxiao Shic099ddb2014-12-25 20:53:20 -0700806 });
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600807
808 getFibManager().onFibRequest(*command);
809
810 BOOST_REQUIRE(didCallbackFire());
811}
812
813BOOST_AUTO_TEST_CASE(RemoveMissingPrefix)
814{
815 addFace(make_shared<DummyFace>());
816
817 shared_ptr<InternalFace> face = getInternalFace();
818
819 ControlParameters parameters;
820 parameters.setFaceId(1);
821
822 Block encodedParameters(parameters.wireEncode());
823
824 Name commandName("/localhost/nfd/fib");
825 commandName.append("remove-nexthop");
826 commandName.append(encodedParameters);
827
828 shared_ptr<Interest> command(make_shared<Interest>(commandName));
829 generateCommand(*command);
830
Junxiao Shic099ddb2014-12-25 20:53:20 -0700831 face->onReceiveData.connect([this, command] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700832 this->validateControlResponse(response, command->getName(), 400, "Malformed command");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700833 });
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600834
835 getFibManager().onFibRequest(*command);
836
837 BOOST_REQUIRE(didCallbackFire());
838}
839
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700840BOOST_AUTO_TEST_CASE(RemoveImplicitFaceId)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600841{
842 addFace(make_shared<DummyFace>());
843
844 shared_ptr<InternalFace> face = getInternalFace();
845
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700846 std::vector<ControlParameters> testedParameters;
847 testedParameters.push_back(ControlParameters().setName("/hello").setFaceId(0));
848 testedParameters.push_back(ControlParameters().setName("/hello"));
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600849
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700850 for (std::vector<ControlParameters>::iterator parameters = testedParameters.begin();
851 parameters != testedParameters.end(); ++parameters) {
852 Block encodedParameters(parameters->wireEncode());
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600853
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700854 Name commandName("/localhost/nfd/fib");
855 commandName.append("remove-nexthop");
856 commandName.append(encodedParameters);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600857
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700858 shared_ptr<Interest> command(make_shared<Interest>(commandName));
859 command->setIncomingFaceId(1);
860 generateCommand(*command);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600861
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700862 ControlParameters resultParameters;
863 resultParameters.setFaceId(1);
864 resultParameters.setName("/hello");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700865
Junxiao Shic099ddb2014-12-25 20:53:20 -0700866 signal::Connection conn = face->onReceiveData.connect(
867 [this, command, resultParameters] (const Data& response) {
868 this->validateControlResponse(response, command->getName(),
869 200, "Success", resultParameters.wireEncode());
870 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700871
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700872 getFibManager().onFibRequest(*command);
873
874 BOOST_REQUIRE(didCallbackFire());
875
Junxiao Shic099ddb2014-12-25 20:53:20 -0700876 conn.disconnect();
Tai-Lin Chu6687aab2014-10-07 21:27:24 -0700877 }
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700878}
879
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600880BOOST_FIXTURE_TEST_CASE(TestFibEnumerationRequest, FibManagerFixture)
881{
882 for (int i = 0; i < 87; i++)
883 {
884 Name prefix("/test");
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<uint64_t>::max() - 1);
892 entry->addNextHop(dummy2, std::numeric_limits<uint64_t>::max() - 2);
893
894 m_referenceEntries.insert(entry);
895 }
896 for (int i = 0; i < 2; i++)
897 {
898 Name prefix("/test2");
899 prefix.appendSegment(i);
900
901 shared_ptr<DummyFace> dummy1(make_shared<DummyFace>());
902 shared_ptr<DummyFace> dummy2(make_shared<DummyFace>());
903
904 shared_ptr<fib::Entry> entry = m_fib.insert(prefix).first;
905 entry->addNextHop(dummy1, std::numeric_limits<uint8_t>::max() - 1);
906 entry->addNextHop(dummy2, std::numeric_limits<uint8_t>::max() - 2);
907
908 m_referenceEntries.insert(entry);
909 }
910
911 ndn::EncodingBuffer buffer;
912
Junxiao Shic099ddb2014-12-25 20:53:20 -0700913 m_face->onReceiveData.connect(bind(&FibEnumerationPublisherFixture::decodeFibEntryBlock,
914 this, _1));
Steve DiBenedetto6214e562014-03-15 16:27:04 -0600915
916 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/fib/list"));
917
918 m_manager.onFibRequest(*command);
919 BOOST_REQUIRE(m_finished);
920}
921
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700922BOOST_AUTO_TEST_SUITE_END()
923
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700924} // namespace tests
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700925} // namespace nfd