Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 8 | #include "table/fib.hpp" |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 9 | #include "table/fib-nexthop.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 10 | #include "face/face.hpp" |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 11 | #include "mgmt/internal-face.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 12 | #include "tests/face/dummy-face.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 13 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 14 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 15 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 16 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | namespace tests { |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 18 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 19 | NFD_LOG_INIT("FibManagerTest"); |
| 20 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 21 | class FibManagerFixture : protected BaseFixture |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 22 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 23 | public: |
| 24 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 25 | FibManagerFixture() |
| 26 | : m_callbackFired(false) |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 31 | shared_ptr<Face> |
| 32 | getFace(FaceId id) |
| 33 | { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | if (id > 0 && static_cast<size_t>(id) <= m_faces.size()) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 35 | { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | return m_faces[id - 1]; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 37 | } |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 38 | NFD_LOG_DEBUG("No face found returning NULL"); |
| 39 | return shared_ptr<DummyFace>(); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
| 43 | addFace(shared_ptr<Face> face) |
| 44 | { |
| 45 | m_faces.push_back(face); |
| 46 | } |
| 47 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 48 | void |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 49 | validateControlResponseCommon(const Data& response, |
| 50 | const Name& expectedName, |
| 51 | uint32_t expectedCode, |
| 52 | const std::string& expectedText, |
| 53 | ControlResponse& control) |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 54 | { |
| 55 | m_callbackFired = true; |
| 56 | Block controlRaw = response.getContent().blockFromValue(); |
| 57 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 58 | control.wireDecode(controlRaw); |
| 59 | |
| 60 | NFD_LOG_DEBUG("received control response" |
| 61 | << " Name: " << response.getName() |
| 62 | << " code: " << control.getCode() |
| 63 | << " text: " << control.getText()); |
| 64 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(response.getName(), expectedName); |
| 66 | BOOST_CHECK_EQUAL(control.getCode(), expectedCode); |
| 67 | BOOST_CHECK_EQUAL(control.getText(), expectedText); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 70 | void |
| 71 | validateControlResponse(const Data& response, |
| 72 | const Name& expectedName, |
| 73 | uint32_t expectedCode, |
| 74 | const std::string& expectedText) |
| 75 | { |
| 76 | ControlResponse control; |
| 77 | validateControlResponseCommon(response, expectedName, |
| 78 | expectedCode, expectedText, control); |
| 79 | |
| 80 | if (!control.getBody().empty()) |
| 81 | { |
| 82 | BOOST_FAIL("found unexpected control response body"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | validateControlResponse(const Data& response, |
| 88 | const Name& expectedName, |
| 89 | uint32_t expectedCode, |
| 90 | const std::string& expectedText, |
| 91 | const Block& expectedBody) |
| 92 | { |
| 93 | ControlResponse control; |
| 94 | validateControlResponseCommon(response, expectedName, |
| 95 | expectedCode, expectedText, control); |
| 96 | |
| 97 | BOOST_REQUIRE(!control.getBody().empty()); |
| 98 | BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size()); |
| 99 | |
| 100 | BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(), |
| 101 | expectedBody.value_size()) == 0); |
| 102 | |
| 103 | } |
| 104 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 105 | bool |
| 106 | didCallbackFire() |
| 107 | { |
| 108 | return m_callbackFired; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | resetCallbackFired() |
| 113 | { |
| 114 | m_callbackFired = false; |
| 115 | } |
| 116 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 117 | private: |
| 118 | std::vector<shared_ptr<Face> > m_faces; |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 119 | bool m_callbackFired; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 122 | BOOST_FIXTURE_TEST_SUITE(MgmtFibManager, FibManagerFixture) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 123 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 124 | bool |
| 125 | foundNextHop(FaceId id, uint32_t cost, const fib::NextHop& next) |
| 126 | { |
| 127 | return id == next.getFace()->getId() && next.getCost() == cost; |
| 128 | } |
| 129 | |
| 130 | bool |
| 131 | addedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost) |
| 132 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 133 | shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 134 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 135 | if (static_cast<bool>(entry)) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 136 | { |
| 137 | const fib::NextHopList& hops = entry->getNextHops(); |
| 138 | return hops.size() == oldSize + 1 && |
| 139 | std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end(); |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 144 | bool |
| 145 | foundNextHopWithFace(FaceId id, uint32_t cost, |
| 146 | shared_ptr<Face> face, const fib::NextHop& next) |
| 147 | { |
| 148 | return id == next.getFace()->getId() && next.getCost() == cost && face == next.getFace(); |
| 149 | } |
| 150 | |
| 151 | bool |
| 152 | addedNextHopWithFace(const Fib& fib, const Name& prefix, size_t oldSize, |
| 153 | uint32_t cost, shared_ptr<Face> face) |
| 154 | { |
| 155 | shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix); |
| 156 | |
| 157 | if (static_cast<bool>(entry)) |
| 158 | { |
| 159 | const fib::NextHopList& hops = entry->getNextHops(); |
| 160 | return hops.size() == oldSize + 1 && |
| 161 | std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) != hops.end(); |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(TestFireInterestFilter) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 167 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 168 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 169 | NameTree nameTree(1024); |
| 170 | Fib fib(nameTree); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 171 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 172 | bind(&FibManagerFixture::getFace, this, _1), |
| 173 | face); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 174 | |
| 175 | Interest command("/localhost/nfd/fib"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 176 | |
| 177 | face->onReceiveData += |
| 178 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 179 | command.getName(), 400, "Malformed command"); |
| 180 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 181 | face->sendInterest(command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 182 | |
| 183 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 186 | BOOST_AUTO_TEST_CASE(MalformedCommmand) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 187 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 188 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 189 | NameTree nameTree(1024); |
| 190 | Fib fib(nameTree); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 191 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 192 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 193 | face); |
| 194 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 195 | BOOST_REQUIRE(didCallbackFire() == false); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 196 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 197 | Interest command("/localhost/nfd/fib"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 198 | |
| 199 | face->onReceiveData += |
| 200 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 201 | command.getName(), 400, "Malformed command"); |
| 202 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 203 | manager.onFibRequest(command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 204 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 205 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 206 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 207 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 208 | BOOST_AUTO_TEST_CASE(UnsupportedVerb) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 209 | { |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 210 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 211 | NameTree nameTree(1024); |
| 212 | Fib fib(nameTree); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 213 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 214 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 215 | face); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 217 | FibManagementOptions options; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 218 | options.setName("/hello"); |
| 219 | options.setFaceId(1); |
| 220 | options.setCost(1); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 221 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 222 | Block encodedOptions(options.wireEncode()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 223 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 224 | Name commandName("/localhost/nfd/fib"); |
| 225 | commandName.append("unsupported"); |
| 226 | commandName.append(encodedOptions); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 227 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 228 | face->onReceiveData += |
| 229 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 230 | commandName, 501, "Unsupported command"); |
| 231 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 232 | Interest command(commandName); |
| 233 | manager.onFibRequest(command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 234 | |
| 235 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 236 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 237 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 238 | BOOST_AUTO_TEST_CASE(UnsignedCommand) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 239 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 240 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 241 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 242 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 243 | NameTree nameTree(1024); |
| 244 | Fib fib(nameTree); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 245 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 246 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 247 | face); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 248 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 249 | FibManagementOptions options; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 250 | options.setName("/hello"); |
| 251 | options.setFaceId(1); |
| 252 | options.setCost(101); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 253 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 254 | Block encodedOptions(options.wireEncode()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 255 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 256 | Name commandName("/localhost/nfd/fib"); |
| 257 | commandName.append("add-nexthop"); |
| 258 | commandName.append(encodedOptions); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 259 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 260 | face->onReceiveData += |
| 261 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 262 | commandName, 404, "Prefix not found"); |
| 263 | /// \todo enable once sig checking implemented |
| 264 | // bind(&FibManagerFixture::validateControlResponse, this, _1, 401, "Signature required"); |
| 265 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 266 | Interest command(commandName); |
| 267 | manager.onFibRequest(command); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 268 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 269 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 270 | BOOST_REQUIRE(!addedNextHopWithCost(fib, "/hello", 0, 101)); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 271 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 272 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 273 | BOOST_AUTO_TEST_CASE(UnauthorizedCommand) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 274 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 275 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 276 | |
| 277 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 278 | NameTree nameTree(1024); |
| 279 | Fib fib(nameTree); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 280 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 281 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 282 | face); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 283 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 284 | FibManagementOptions options; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 285 | options.setName("/hello"); |
| 286 | options.setFaceId(1); |
| 287 | options.setCost(101); |
| 288 | |
| 289 | Block encodedOptions(options.wireEncode()); |
| 290 | |
| 291 | Name commandName("/localhost/nfd/fib"); |
| 292 | commandName.append("add-nexthop"); |
| 293 | commandName.append(encodedOptions); |
| 294 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 295 | face->onReceiveData += |
| 296 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 297 | commandName, 404, "Prefix not found"); |
| 298 | /// \todo enable once sig checking implemented |
| 299 | // bind(&FibManagerFixture::validateControlResponse, this, _1, 403, "Unauthorized command"); |
| 300 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 301 | Interest command(commandName); |
| 302 | manager.onFibRequest(command); |
| 303 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 304 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 305 | BOOST_REQUIRE(!addedNextHopWithCost(fib, "/hello", 0, 101)); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 308 | BOOST_AUTO_TEST_CASE(BadOptionParse) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 309 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 310 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 311 | |
| 312 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 313 | NameTree nameTree(1024); |
| 314 | Fib fib(nameTree); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 315 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 316 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 317 | face); |
| 318 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 319 | Name commandName("/localhost/nfd/fib"); |
| 320 | commandName.append("add-nexthop"); |
| 321 | commandName.append("NotReallyOptions"); |
| 322 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 323 | face->onReceiveData += |
| 324 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 325 | commandName, 400, "Malformed command"); |
| 326 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 327 | Interest command(commandName); |
| 328 | manager.onFibRequest(command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 329 | |
| 330 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 333 | BOOST_AUTO_TEST_CASE(UnknownFaceId) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 334 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 335 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 336 | |
| 337 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 338 | NameTree nameTree(1024); |
| 339 | Fib fib(nameTree); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 340 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 341 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 342 | face); |
| 343 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 344 | FibManagementOptions options; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 345 | options.setName("/hello"); |
| 346 | options.setFaceId(1000); |
| 347 | options.setCost(101); |
| 348 | |
| 349 | Block encodedOptions(options.wireEncode()); |
| 350 | |
| 351 | Name commandName("/localhost/nfd/fib"); |
| 352 | commandName.append("add-nexthop"); |
| 353 | commandName.append(encodedOptions); |
| 354 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 355 | face->onReceiveData += |
| 356 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 357 | commandName, 404, "Face not found"); |
| 358 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 359 | Interest command(commandName); |
| 360 | manager.onFibRequest(command); |
| 361 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 362 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 363 | BOOST_REQUIRE(addedNextHopWithCost(fib, "/hello", 0, 101) == false); |
| 364 | } |
| 365 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 366 | BOOST_AUTO_TEST_CASE(TestImplicitFaceId) |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 367 | { |
| 368 | addFace(make_shared<DummyFace>()); |
| 369 | |
| 370 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 371 | NameTree nameTree(1024); |
| 372 | Fib fib(nameTree); |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 373 | FibManager manager(fib, |
| 374 | bind(&FibManagerFixture::getFace, this, _1), |
| 375 | face); |
| 376 | |
| 377 | FibManagementOptions options; |
| 378 | options.setName("/hello"); |
| 379 | options.setFaceId(0); |
| 380 | options.setCost(101); |
| 381 | |
| 382 | Block encodedOptions(options.wireEncode()); |
| 383 | |
| 384 | Name commandName("/localhost/nfd/fib"); |
| 385 | commandName.append("add-nexthop"); |
| 386 | commandName.append(encodedOptions); |
| 387 | |
| 388 | FibManagementOptions expectedOptions; |
| 389 | expectedOptions.setName("/hello"); |
| 390 | expectedOptions.setFaceId(1); |
| 391 | expectedOptions.setCost(101); |
| 392 | |
| 393 | Block encodedExpectedOptions(expectedOptions.wireEncode()); |
| 394 | |
| 395 | face->onReceiveData += |
| 396 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 397 | commandName, 200, "Success", encodedExpectedOptions); |
| 398 | |
| 399 | fib.insert("/hello"); |
| 400 | |
| 401 | Interest command(commandName); |
| 402 | command.setIncomingFaceId(1); |
| 403 | manager.onFibRequest(command); |
| 404 | |
| 405 | BOOST_REQUIRE(didCallbackFire()); |
| 406 | BOOST_REQUIRE(addedNextHopWithFace(fib, "/hello", 0, 101, getFace(1))); |
| 407 | } |
| 408 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 409 | BOOST_AUTO_TEST_CASE(AddNextHopVerbInitialAdd) |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 410 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 411 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 412 | |
| 413 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 414 | NameTree nameTree(1024); |
| 415 | Fib fib(nameTree); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 416 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 417 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 418 | face); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 419 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 420 | FibManagementOptions options; |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 421 | options.setName("/hello"); |
| 422 | options.setFaceId(1); |
| 423 | options.setCost(101); |
| 424 | |
| 425 | Block encodedOptions(options.wireEncode()); |
| 426 | |
| 427 | Name commandName("/localhost/nfd/fib"); |
| 428 | commandName.append("add-nexthop"); |
| 429 | commandName.append(encodedOptions); |
| 430 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 431 | face->onReceiveData += |
| 432 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 433 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 434 | |
| 435 | fib.insert("/hello"); |
| 436 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 437 | Interest command(commandName); |
| 438 | manager.onFibRequest(command); |
| 439 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 440 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 441 | BOOST_REQUIRE(addedNextHopWithCost(fib, "/hello", 0, 101)); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 442 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 443 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 444 | BOOST_AUTO_TEST_CASE(AddNextHopVerbAddToExisting) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 445 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 446 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 447 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 448 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 449 | NameTree nameTree(1024); |
| 450 | Fib fib(nameTree); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 451 | FibManager manager(fib, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 452 | bind(&FibManagerFixture::getFace, this, _1), |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 453 | face); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 454 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 455 | fib.insert("/hello"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 456 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 457 | for (int i = 1; i <= 2; i++) |
| 458 | { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 459 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 460 | FibManagementOptions options; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 461 | options.setName("/hello"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 462 | options.setFaceId(1); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 463 | options.setCost(100 + i); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 464 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 465 | Block encodedOptions(options.wireEncode()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 466 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 467 | Name commandName("/localhost/nfd/fib"); |
| 468 | commandName.append("add-nexthop"); |
| 469 | commandName.append(encodedOptions); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 470 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 471 | face->onReceiveData += |
| 472 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 473 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 474 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 475 | Interest command(commandName); |
| 476 | manager.onFibRequest(command); |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 477 | BOOST_REQUIRE(didCallbackFire()); |
| 478 | resetCallbackFired(); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 479 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 480 | shared_ptr<fib::Entry> entry = fib.findExactMatch("/hello"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 481 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 482 | if (static_cast<bool>(entry)) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 483 | { |
| 484 | const fib::NextHopList& hops = entry->getNextHops(); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 485 | BOOST_REQUIRE(hops.size() == 1); |
| 486 | BOOST_REQUIRE(std::find_if(hops.begin(), hops.end(), |
| 487 | bind(&foundNextHop, -1, 100 + i, _1)) != hops.end()); |
| 488 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
| 492 | BOOST_FAIL("Failed to find expected fib entry"); |
| 493 | } |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 494 | |
| 495 | face->onReceiveData.clear(); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 498 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 499 | BOOST_AUTO_TEST_CASE(AddNextHopVerbUpdateFaceCost) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 500 | { |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 501 | addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 502 | |
Steve DiBenedetto | 80ddc21 | 2014-02-01 22:23:56 -0700 | [diff] [blame] | 503 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 504 | NameTree nameTree(1024); |
| 505 | Fib fib(nameTree); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 506 | FibManager manager(fib, |
| 507 | bind(&FibManagerFixture::getFace, |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 508 | this, _1), |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 509 | face); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 510 | |
| 511 | fib.insert("/hello"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 512 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 513 | FibManagementOptions options; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 514 | options.setName("/hello"); |
| 515 | options.setFaceId(1); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 516 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 517 | { |
| 518 | options.setCost(1); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 519 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 520 | Block encodedOptions(options.wireEncode()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 521 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 522 | Name commandName("/localhost/nfd/fib"); |
| 523 | commandName.append("add-nexthop"); |
| 524 | commandName.append(encodedOptions); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 525 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 526 | face->onReceiveData += |
| 527 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 528 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 529 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 530 | Interest command(commandName); |
| 531 | manager.onFibRequest(command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 532 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 533 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 534 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 535 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 536 | resetCallbackFired(); |
| 537 | face->onReceiveData.clear(); |
| 538 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 539 | { |
| 540 | options.setCost(102); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 541 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 542 | Block encodedOptions(options.wireEncode()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 543 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 544 | Name commandName("/localhost/nfd/fib"); |
| 545 | commandName.append("add-nexthop"); |
| 546 | commandName.append(encodedOptions); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 547 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 548 | face->onReceiveData += |
| 549 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 550 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 551 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 552 | Interest command(commandName); |
| 553 | manager.onFibRequest(command); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 554 | |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 555 | BOOST_REQUIRE(didCallbackFire()); |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 556 | } |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 557 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 558 | shared_ptr<fib::Entry> entry = fib.findExactMatch("/hello"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 559 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 560 | // Add faces with cost == FaceID for the name /hello |
| 561 | // This test assumes: |
| 562 | // FaceIDs are -1 because we don't add them to a forwarder |
Steve DiBenedetto | bdedce9 | 2014-02-02 22:49:39 -0700 | [diff] [blame] | 563 | if (static_cast<bool>(entry)) |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 564 | { |
| 565 | const fib::NextHopList& hops = entry->getNextHops(); |
| 566 | BOOST_REQUIRE(hops.size() == 1); |
| 567 | BOOST_REQUIRE(std::find_if(hops.begin(), |
| 568 | hops.end(), |
| 569 | bind(&foundNextHop, -1, 102, _1)) != hops.end()); |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | BOOST_FAIL("Failed to find expected fib entry"); |
| 574 | } |
| 575 | } |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 576 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 577 | BOOST_AUTO_TEST_CASE(Insert) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 578 | { |
| 579 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 580 | NameTree nameTree(1024); |
| 581 | Fib fib(nameTree); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 582 | FibManager manager(fib, |
| 583 | bind(&FibManagerFixture::getFace, this, _1), |
| 584 | face); |
| 585 | |
| 586 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 587 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 588 | options.setName("/hello"); |
| 589 | |
| 590 | Block encodedOptions(options.wireEncode()); |
| 591 | |
| 592 | Name commandName("/localhost/nfd/fib"); |
| 593 | commandName.append("insert"); |
| 594 | commandName.append(encodedOptions); |
| 595 | |
| 596 | face->onReceiveData += |
| 597 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 598 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 599 | |
| 600 | Interest command(commandName); |
| 601 | manager.onFibRequest(command); |
| 602 | } |
| 603 | |
| 604 | BOOST_REQUIRE(didCallbackFire()); |
| 605 | |
| 606 | shared_ptr<fib::Entry> entry = fib.findExactMatch("/hello"); |
| 607 | if (static_cast<bool>(entry)) |
| 608 | { |
| 609 | const fib::NextHopList& hops = entry->getNextHops(); |
| 610 | BOOST_CHECK_EQUAL(hops.size(), 0); |
| 611 | } |
| 612 | |
| 613 | resetCallbackFired(); |
| 614 | |
| 615 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 616 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 617 | options.setName("/hello"); |
| 618 | |
| 619 | Block encodedOptions(options.wireEncode()); |
| 620 | |
| 621 | Name commandName("/localhost/nfd/fib"); |
| 622 | commandName.append("insert"); |
| 623 | commandName.append(encodedOptions); |
| 624 | |
| 625 | face->onReceiveData += |
| 626 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 627 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 628 | |
| 629 | Interest command(commandName); |
| 630 | manager.onFibRequest(command); |
| 631 | } |
| 632 | |
| 633 | BOOST_REQUIRE(didCallbackFire()); |
| 634 | |
| 635 | entry = fib.findExactMatch("/hello"); |
| 636 | if (static_cast<bool>(entry)) |
| 637 | { |
| 638 | const fib::NextHopList& hops = entry->getNextHops(); |
| 639 | BOOST_CHECK_EQUAL(hops.size(), 0); |
| 640 | } |
| 641 | |
| 642 | } |
| 643 | |
| 644 | void |
| 645 | testRemove(FibManagerFixture* fixture, |
| 646 | FibManager& manager, |
| 647 | Fib& fib, |
| 648 | shared_ptr<Face> face, |
| 649 | const Name& target) |
| 650 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 651 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 652 | options.setName(target); |
| 653 | |
| 654 | Block encodedOptions(options.wireEncode()); |
| 655 | |
| 656 | Name commandName("/localhost/nfd/fib"); |
| 657 | commandName.append("delete"); |
| 658 | commandName.append(encodedOptions); |
| 659 | |
| 660 | face->onReceiveData += |
| 661 | bind(&FibManagerFixture::validateControlResponse, fixture, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 662 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 663 | |
| 664 | Interest command(commandName); |
| 665 | manager.onFibRequest(command); |
| 666 | |
| 667 | BOOST_REQUIRE(fixture->didCallbackFire()); |
| 668 | |
| 669 | if (static_cast<bool>(fib.findExactMatch(target))) |
| 670 | { |
| 671 | BOOST_FAIL("Found \"removed\" prefix"); |
| 672 | } |
| 673 | face->onReceiveData.clear(); |
| 674 | } |
| 675 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 676 | BOOST_AUTO_TEST_CASE(Delete) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 677 | { |
| 678 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 679 | NameTree nameTree(1024); |
| 680 | Fib fib(nameTree); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 681 | FibManager manager(fib, |
| 682 | bind(&FibManagerFixture::getFace, this, _1), |
| 683 | face); |
| 684 | |
| 685 | fib.insert("/a"); |
| 686 | fib.insert("/a/b"); |
| 687 | fib.insert("/a/b/c"); |
| 688 | |
| 689 | testRemove(this, manager, fib, face, "/"); |
| 690 | |
| 691 | if (!static_cast<bool>(fib.findExactMatch("/a")) || |
| 692 | !static_cast<bool>(fib.findExactMatch("/a/b")) || |
| 693 | !static_cast<bool>(fib.findExactMatch("/a/b/c"))) |
| 694 | { |
| 695 | BOOST_FAIL("Removed incorrect entry"); |
| 696 | } |
| 697 | |
| 698 | testRemove(this, manager, fib, face, "/a/b"); |
| 699 | |
| 700 | if (!static_cast<bool>(fib.findExactMatch("/a")) || |
| 701 | !static_cast<bool>(fib.findExactMatch("/a/b/c"))) |
| 702 | { |
| 703 | BOOST_FAIL("Removed incorrect entry"); |
| 704 | } |
| 705 | |
| 706 | testRemove(this, manager, fib, face, "/a/b/c"); |
| 707 | |
| 708 | if (!static_cast<bool>(fib.findExactMatch("/a"))) |
| 709 | { |
| 710 | BOOST_FAIL("Removed incorrect entry"); |
| 711 | } |
| 712 | |
| 713 | testRemove(this, manager, fib, face, "/a"); |
| 714 | |
| 715 | testRemove(this, manager, fib, face, "/does/not/exist"); |
| 716 | } |
| 717 | |
| 718 | bool |
| 719 | removedNextHopWithCost(const Fib& fib, const Name& prefix, size_t oldSize, uint32_t cost) |
| 720 | { |
| 721 | shared_ptr<fib::Entry> entry = fib.findExactMatch(prefix); |
| 722 | |
| 723 | if (static_cast<bool>(entry)) |
| 724 | { |
| 725 | const fib::NextHopList& hops = entry->getNextHops(); |
| 726 | return hops.size() == oldSize - 1 && |
| 727 | std::find_if(hops.begin(), hops.end(), bind(&foundNextHop, -1, cost, _1)) == hops.end(); |
| 728 | } |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | void |
| 733 | testRemoveNextHop(FibManagerFixture* fixture, |
| 734 | FibManager& manager, |
| 735 | Fib& fib, |
| 736 | shared_ptr<Face> face, |
| 737 | const Name& targetName, |
| 738 | FaceId targetFace) |
| 739 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 740 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 741 | options.setName(targetName); |
| 742 | options.setFaceId(targetFace); |
| 743 | |
| 744 | Block encodedOptions(options.wireEncode()); |
| 745 | |
| 746 | Name commandName("/localhost/nfd/fib"); |
| 747 | commandName.append("remove-nexthop"); |
| 748 | commandName.append(encodedOptions); |
| 749 | |
| 750 | face->onReceiveData += |
| 751 | bind(&FibManagerFixture::validateControlResponse, fixture, _1, |
Steve DiBenedetto | 2693db9 | 2014-02-10 15:58:36 -0700 | [diff] [blame] | 752 | commandName, 200, "Success", encodedOptions); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 753 | |
| 754 | Interest command(commandName); |
| 755 | manager.onFibRequest(command); |
| 756 | |
| 757 | BOOST_REQUIRE(fixture->didCallbackFire()); |
| 758 | |
| 759 | fixture->resetCallbackFired(); |
| 760 | face->onReceiveData.clear(); |
| 761 | } |
| 762 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 763 | BOOST_AUTO_TEST_CASE(RemoveNextHop) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 764 | { |
| 765 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 766 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 767 | shared_ptr<Face> face3 = make_shared<DummyFace>(); |
| 768 | |
| 769 | addFace(face1); |
| 770 | addFace(face2); |
| 771 | addFace(face3); |
| 772 | |
| 773 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 774 | NameTree nameTree(1024); |
| 775 | Fib fib(nameTree); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 776 | FibManager manager(fib, |
| 777 | bind(&FibManagerFixture::getFace, this, _1), |
| 778 | face); |
| 779 | |
| 780 | shared_ptr<fib::Entry> entry = fib.insert("/hello").first; |
| 781 | |
| 782 | entry->addNextHop(face1, 101); |
| 783 | entry->addNextHop(face2, 202); |
| 784 | entry->addNextHop(face3, 303); |
| 785 | |
| 786 | testRemoveNextHop(this, manager, fib, face, "/hello", 2); |
| 787 | BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 3, 202)); |
| 788 | |
| 789 | testRemoveNextHop(this, manager, fib, face, "/hello", 3); |
| 790 | BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 2, 303)); |
| 791 | |
| 792 | testRemoveNextHop(this, manager, fib, face, "/hello", 1); |
| 793 | BOOST_REQUIRE(removedNextHopWithCost(fib, "/hello", 1, 101)); |
| 794 | |
| 795 | if (!static_cast<bool>(fib.findExactMatch("/hello"))) |
| 796 | { |
| 797 | BOOST_FAIL("removed entry after removing all next hops"); |
| 798 | } |
| 799 | |
| 800 | } |
| 801 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 802 | BOOST_AUTO_TEST_CASE(RemoveNoFace) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 803 | { |
| 804 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 805 | NameTree nameTree(1024); |
| 806 | Fib fib(nameTree); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 807 | FibManager manager(fib, |
| 808 | bind(&FibManagerFixture::getFace, this, _1), |
| 809 | face); |
| 810 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 811 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 812 | options.setName("/hello"); |
| 813 | options.setFaceId(1); |
| 814 | |
| 815 | Block encodedOptions(options.wireEncode()); |
| 816 | |
| 817 | Name commandName("/localhost/nfd/fib"); |
| 818 | commandName.append("remove-nexthop"); |
| 819 | commandName.append(encodedOptions); |
| 820 | |
| 821 | face->onReceiveData += |
| 822 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 823 | commandName, 404, "Face not found"); |
| 824 | |
| 825 | Interest command(commandName); |
| 826 | manager.onFibRequest(command); |
| 827 | |
| 828 | BOOST_REQUIRE(didCallbackFire()); |
| 829 | } |
| 830 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 831 | BOOST_AUTO_TEST_CASE(RemoveNoPrefix) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 832 | { |
| 833 | addFace(make_shared<DummyFace>()); |
| 834 | |
| 835 | shared_ptr<InternalFace> face(make_shared<InternalFace>()); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 836 | NameTree nameTree(1024); |
| 837 | Fib fib(nameTree); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 838 | FibManager manager(fib, |
| 839 | bind(&FibManagerFixture::getFace, this, _1), |
| 840 | face); |
| 841 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 842 | FibManagementOptions options; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 843 | options.setName("/hello"); |
| 844 | options.setFaceId(1); |
| 845 | |
| 846 | Block encodedOptions(options.wireEncode()); |
| 847 | |
| 848 | Name commandName("/localhost/nfd/fib"); |
| 849 | commandName.append("remove-nexthop"); |
| 850 | commandName.append(encodedOptions); |
| 851 | |
| 852 | face->onReceiveData += |
| 853 | bind(&FibManagerFixture::validateControlResponse, this, _1, |
| 854 | commandName, 404, "Prefix not found"); |
| 855 | |
| 856 | Interest command(commandName); |
| 857 | manager.onFibRequest(command); |
| 858 | |
| 859 | BOOST_REQUIRE(didCallbackFire()); |
| 860 | } |
| 861 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 862 | BOOST_AUTO_TEST_SUITE_END() |
| 863 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 864 | } // namespace tests |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 865 | } // namespace nfd |