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