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 | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 79 | face->onReceiveData += |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 80 | bind(&validateControlResponse, _1, 200, "OK"); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 81 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 82 | ndn::FibManagementOptions options; |
| 83 | options.setName("/hello"); |
| 84 | options.setFaceId(1); |
| 85 | options.setCost(1); |
| 86 | |
| 87 | Block encodedOptions(options.wireEncode()); |
| 88 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 89 | Name commandName("/localhost/nfd/fib"); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 90 | commandName.append("add-nexthop"); |
| 91 | commandName.append(encodedOptions); |
| 92 | |
| 93 | Interest command(commandName); |
| 94 | face->sendInterest(command); |
| 95 | |
| 96 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 97 | |
| 98 | if (entry) |
| 99 | { |
| 100 | const fib::NextHopList& hops = entry->getNextHops(); |
| 101 | BOOST_REQUIRE(hops.size() == 1); |
| 102 | BOOST_CHECK(hops[0].getCost() == 1); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | BOOST_FAIL("Failed to find expected fib entry"); |
| 107 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration) |
| 111 | { |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 112 | InternalFaceFixture fixture; |
| 113 | fixture.addFace(make_shared<DummyFace>()); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 114 | |
| 115 | shared_ptr<InternalFace> face(new InternalFace); |
Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame] | 116 | Fib fib; |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 117 | FibManager manager(fib, |
| 118 | bind(&InternalFaceFixture::getFace, |
| 119 | &fixture, _1), |
| 120 | face); |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 121 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 122 | face->onReceiveData += |
| 123 | bind(&validateControlResponse, _1, 404, "MALFORMED"); |
| 124 | |
Steve DiBenedetto | 3970c89 | 2014-01-31 23:31:13 -0700 | [diff] [blame] | 125 | Interest nonRegInterest("/hello"); |
| 126 | face->sendInterest(nonRegInterest); |
| 127 | |
| 128 | shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello"); |
| 129 | |
| 130 | if (entry) |
| 131 | { |
| 132 | const fib::NextHopList& hops = entry->getNextHops(); |
| 133 | BOOST_REQUIRE(hops.size() == 0); |
| 134 | } |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Steve DiBenedetto | 43cd037 | 2014-02-01 17:05:07 -0700 | [diff] [blame] | 137 | void |
| 138 | validateOnInterestCallback(const Name& name, const Interest& interest) |
| 139 | { |
| 140 | NFD_LOG_DEBUG("Reached correct callback"); |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | validateNoOnInterestCallback(const Name& name, const Interest& interest) |
| 145 | { |
| 146 | BOOST_FAIL("Reached wrong callback"); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(SendInterestHitEnd) |
| 150 | { |
| 151 | InternalFaceFixture fixture; |
| 152 | fixture.addFace(make_shared<DummyFace>()); |
| 153 | |
| 154 | shared_ptr<InternalFace> face(new InternalFace); |
| 155 | Fib fib; |
| 156 | FibManager manager(fib, |
| 157 | bind(&InternalFaceFixture::getFace, |
| 158 | &fixture, _1), |
| 159 | face); |
| 160 | |
| 161 | face->setInterestFilter("/localhost/nfd/fib", |
| 162 | &validateOnInterestCallback); |
| 163 | |
| 164 | // generate command whose name is canonically |
| 165 | // ordered after /localhost/nfd/fib so that |
| 166 | // we hit the end of the std::map |
| 167 | |
| 168 | Name commandName("/localhost/nfd/fib/end"); |
| 169 | Interest command(commandName); |
| 170 | face->sendInterest(command); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(SendInterestHitBegin) |
| 176 | { |
| 177 | InternalFaceFixture fixture; |
| 178 | fixture.addFace(make_shared<DummyFace>()); |
| 179 | |
| 180 | shared_ptr<InternalFace> face(new InternalFace); |
| 181 | Fib fib; |
| 182 | FibManager manager(fib, |
| 183 | bind(&InternalFaceFixture::getFace, |
| 184 | &fixture, _1), |
| 185 | face); |
| 186 | |
| 187 | face->setInterestFilter("/localhost/nfd/fib", |
| 188 | &validateNoOnInterestCallback); |
| 189 | |
| 190 | // generate command whose name is canonically |
| 191 | // ordered before /localhost/nfd/fib so that |
| 192 | // we hit the beginning of the std::map |
| 193 | |
| 194 | Name commandName("/localhost/nfd"); |
| 195 | Interest command(commandName); |
| 196 | face->sendInterest(command); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(SendInterestHitExact) |
| 202 | { |
| 203 | InternalFaceFixture fixture; |
| 204 | fixture.addFace(make_shared<DummyFace>()); |
| 205 | |
| 206 | shared_ptr<InternalFace> face(new InternalFace); |
| 207 | Fib fib; |
| 208 | FibManager manager(fib, |
| 209 | bind(&InternalFaceFixture::getFace, |
| 210 | &fixture, _1), |
| 211 | face); |
| 212 | |
| 213 | face->setInterestFilter("/localhost/nfd/eib", |
| 214 | &validateNoOnInterestCallback); |
| 215 | |
| 216 | face->setInterestFilter("/localhost/nfd/fib", |
| 217 | &validateOnInterestCallback); |
| 218 | |
| 219 | face->setInterestFilter("/localhost/nfd/gib", |
| 220 | &validateNoOnInterestCallback); |
| 221 | |
| 222 | // generate command whose name exactly matches |
| 223 | // /localhost/nfd/fib |
| 224 | |
| 225 | Name commandName("/localhost/nfd/fib"); |
| 226 | Interest command(commandName); |
| 227 | face->sendInterest(command); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | BOOST_AUTO_TEST_CASE(SendInterestHitPrevious) |
| 233 | { |
| 234 | InternalFaceFixture fixture; |
| 235 | fixture.addFace(make_shared<DummyFace>()); |
| 236 | |
| 237 | shared_ptr<InternalFace> face(new InternalFace); |
| 238 | Fib fib; |
| 239 | FibManager manager(fib, |
| 240 | bind(&InternalFaceFixture::getFace, |
| 241 | &fixture, _1), |
| 242 | face); |
| 243 | |
| 244 | face->setInterestFilter("/localhost/nfd/fib", |
| 245 | &validateOnInterestCallback); |
| 246 | |
| 247 | face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/", |
| 248 | &validateNoOnInterestCallback); |
| 249 | |
| 250 | // generate command whose name exactly matches |
| 251 | // an Interest filter |
| 252 | |
| 253 | Name commandName("/localhost/nfd/fib/previous"); |
| 254 | Interest command(commandName); |
| 255 | face->sendInterest(command); |
| 256 | } |
| 257 | |
Steve DiBenedetto | 5b43398 | 2014-01-29 17:14:27 -0700 | [diff] [blame] | 258 | BOOST_AUTO_TEST_SUITE_END() |
| 259 | |
| 260 | } // namespace nfd |