Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -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/internal-face.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 8 | #include "mgmt/fib-manager.hpp" |
| 9 | #include "table/fib.hpp" |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 10 | #include "../face/dummy-face.hpp" |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 11 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 12 | #include <ndn-cpp-dev/management/fib-management-options.hpp> |
| 13 | #include <ndn-cpp-dev/management/control-response.hpp> |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 14 | #include <ndn-cpp-dev/encoding/block.hpp> |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 15 | |
| 16 | #include <boost/test/unit_test.hpp> |
| 17 | |
| 18 | namespace nfd { |
| 19 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 20 | NFD_LOG_INIT("InternalFaceTest"); |
| 21 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 22 | class InternalFaceFixture |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 23 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 24 | public: |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 25 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 26 | shared_ptr<Face> |
| 27 | getFace(FaceId id) |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 28 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 29 | if (m_faces.size() < id) |
| 30 | { |
| 31 | BOOST_FAIL("Attempted to access invalid FaceId: " << id); |
| 32 | } |
| 33 | return m_faces[id-1]; |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 36 | void |
| 37 | addFace(shared_ptr<Face> face) |
| 38 | { |
| 39 | m_faces.push_back(face); |
| 40 | } |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 41 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 42 | private: |
| 43 | std::vector<shared_ptr<Face> > m_faces; |
| 44 | }; |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 45 | |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 46 | BOOST_AUTO_TEST_SUITE(MgmtInternalFace) |
| 47 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 48 | void |
| 49 | validateControlResponse(const Data& response, |
| 50 | uint32_t expectedCode, |
| 51 | const std::string& expectedText) |
| 52 | { |
| 53 | Block controlRaw = response.getContent().blockFromValue(); |
| 54 | |
| 55 | ndn::ControlResponse control; |
| 56 | control.wireDecode(controlRaw); |
| 57 | |
| 58 | NFD_LOG_DEBUG("received control response" |
| 59 | << " Name: " << response.getName() |
| 60 | << " code: " << control.getCode() |
| 61 | << " text: " << control.getText()); |
| 62 | |
| 63 | BOOST_REQUIRE(control.getCode() == expectedCode); |
| 64 | BOOST_REQUIRE(control.getText() == expectedText); |
| 65 | } |
| 66 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(ValidAddNextHop) |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 68 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 69 | InternalFaceFixture fixture; |
| 70 | fixture.addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 71 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 72 | shared_ptr<InternalFace> face(new InternalFace); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 73 | Fib fib; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 74 | FibManager manager(fib, |
| 75 | bind(&InternalFaceFixture::getFace, |
| 76 | &fixture, _1), |
| 77 | face); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 78 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 79 | face->setInterestFilter("/localhost/nfd/fib", |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 80 | bind(&FibManager::onFibRequest, |
| 81 | &manager, _2)); |
| 82 | |
| 83 | face->onReceiveData += |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 84 | bind(&validateControlResponse, _1, 200, "OK"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 85 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 86 | ndn::FibManagementOptions options; |
| 87 | options.setName("/hello"); |
| 88 | options.setFaceId(1); |
| 89 | options.setCost(1); |
| 90 | |
| 91 | Block encodedOptions(options.wireEncode()); |
| 92 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 93 | Name commandName("/localhost/nfd/fib"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 94 | commandName.append("add-nexthop"); |
| 95 | commandName.append(encodedOptions); |
| 96 | |
| 97 | Interest command(commandName); |
| 98 | face->sendInterest(command); |
| 99 | |
| 100 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 101 | |
| 102 | if (entry) |
| 103 | { |
| 104 | const fib::NextHopList& hops = entry->getNextHops(); |
| 105 | BOOST_REQUIRE(hops.size() == 1); |
| 106 | BOOST_CHECK(hops[0].getCost() == 1); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | BOOST_FAIL("Failed to find expected fib entry"); |
| 111 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration) |
| 115 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 116 | InternalFaceFixture fixture; |
| 117 | fixture.addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 118 | |
| 119 | shared_ptr<InternalFace> face(new InternalFace); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 120 | Fib fib; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 121 | FibManager manager(fib, |
| 122 | bind(&InternalFaceFixture::getFace, |
| 123 | &fixture, _1), |
| 124 | face); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 125 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 126 | face->setInterestFilter("/localhost/nfd/fib", |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 127 | bind(&FibManager::onFibRequest, |
| 128 | &manager, _2)); |
| 129 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 130 | face->onReceiveData += |
| 131 | bind(&validateControlResponse, _1, 404, "MALFORMED"); |
| 132 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 133 | Interest nonRegInterest("/hello"); |
| 134 | face->sendInterest(nonRegInterest); |
| 135 | |
| 136 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 137 | |
| 138 | if (entry) |
| 139 | { |
| 140 | const fib::NextHopList& hops = entry->getNextHops(); |
| 141 | BOOST_REQUIRE(hops.size() == 0); |
| 142 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame^] | 145 | void |
| 146 | validateOnInterestCallback(const Name& name, const Interest& interest) |
| 147 | { |
| 148 | NFD_LOG_DEBUG("Reached correct callback"); |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | validateNoOnInterestCallback(const Name& name, const Interest& interest) |
| 153 | { |
| 154 | BOOST_FAIL("Reached wrong callback"); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(SendInterestHitEnd) |
| 158 | { |
| 159 | InternalFaceFixture fixture; |
| 160 | fixture.addFace(make_shared<DummyFace>()); |
| 161 | |
| 162 | shared_ptr<InternalFace> face(new InternalFace); |
| 163 | Fib fib; |
| 164 | FibManager manager(fib, |
| 165 | bind(&InternalFaceFixture::getFace, |
| 166 | &fixture, _1), |
| 167 | face); |
| 168 | |
| 169 | face->setInterestFilter("/localhost/nfd/fib", |
| 170 | &validateOnInterestCallback); |
| 171 | |
| 172 | // generate command whose name is canonically |
| 173 | // ordered after /localhost/nfd/fib so that |
| 174 | // we hit the end of the std::map |
| 175 | |
| 176 | Name commandName("/localhost/nfd/fib/end"); |
| 177 | Interest command(commandName); |
| 178 | face->sendInterest(command); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | BOOST_AUTO_TEST_CASE(SendInterestHitBegin) |
| 184 | { |
| 185 | InternalFaceFixture fixture; |
| 186 | fixture.addFace(make_shared<DummyFace>()); |
| 187 | |
| 188 | shared_ptr<InternalFace> face(new InternalFace); |
| 189 | Fib fib; |
| 190 | FibManager manager(fib, |
| 191 | bind(&InternalFaceFixture::getFace, |
| 192 | &fixture, _1), |
| 193 | face); |
| 194 | |
| 195 | face->setInterestFilter("/localhost/nfd/fib", |
| 196 | &validateNoOnInterestCallback); |
| 197 | |
| 198 | // generate command whose name is canonically |
| 199 | // ordered before /localhost/nfd/fib so that |
| 200 | // we hit the beginning of the std::map |
| 201 | |
| 202 | Name commandName("/localhost/nfd"); |
| 203 | Interest command(commandName); |
| 204 | face->sendInterest(command); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(SendInterestHitExact) |
| 210 | { |
| 211 | InternalFaceFixture fixture; |
| 212 | fixture.addFace(make_shared<DummyFace>()); |
| 213 | |
| 214 | shared_ptr<InternalFace> face(new InternalFace); |
| 215 | Fib fib; |
| 216 | FibManager manager(fib, |
| 217 | bind(&InternalFaceFixture::getFace, |
| 218 | &fixture, _1), |
| 219 | face); |
| 220 | |
| 221 | face->setInterestFilter("/localhost/nfd/eib", |
| 222 | &validateNoOnInterestCallback); |
| 223 | |
| 224 | face->setInterestFilter("/localhost/nfd/fib", |
| 225 | &validateOnInterestCallback); |
| 226 | |
| 227 | face->setInterestFilter("/localhost/nfd/gib", |
| 228 | &validateNoOnInterestCallback); |
| 229 | |
| 230 | // generate command whose name exactly matches |
| 231 | // /localhost/nfd/fib |
| 232 | |
| 233 | Name commandName("/localhost/nfd/fib"); |
| 234 | Interest command(commandName); |
| 235 | face->sendInterest(command); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(SendInterestHitPrevious) |
| 241 | { |
| 242 | InternalFaceFixture fixture; |
| 243 | fixture.addFace(make_shared<DummyFace>()); |
| 244 | |
| 245 | shared_ptr<InternalFace> face(new InternalFace); |
| 246 | Fib fib; |
| 247 | FibManager manager(fib, |
| 248 | bind(&InternalFaceFixture::getFace, |
| 249 | &fixture, _1), |
| 250 | face); |
| 251 | |
| 252 | face->setInterestFilter("/localhost/nfd/fib", |
| 253 | &validateOnInterestCallback); |
| 254 | |
| 255 | face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/", |
| 256 | &validateNoOnInterestCallback); |
| 257 | |
| 258 | // generate command whose name exactly matches |
| 259 | // an Interest filter |
| 260 | |
| 261 | Name commandName("/localhost/nfd/fib/previous"); |
| 262 | Interest command(commandName); |
| 263 | face->sendInterest(command); |
| 264 | } |
| 265 | |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 266 | BOOST_AUTO_TEST_SUITE_END() |
| 267 | |
| 268 | } // namespace nfd |