Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "rib/rib-manager.hpp" |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 28 | #include "rib/rib-status-publisher-common.hpp" |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 29 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
| 31 | #include "tests/limited-io.hpp" |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 33 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace rib { |
| 36 | namespace tests { |
| 37 | |
| 38 | class RibManagerFixture : public nfd::tests::BaseFixture |
| 39 | { |
| 40 | public: |
| 41 | RibManagerFixture() |
| 42 | : COMMAND_PREFIX("/localhost/nfd/rib") |
| 43 | , ADD_NEXTHOP_VERB("add-nexthop") |
| 44 | , REMOVE_NEXTHOP_VERB("remove-nexthop") |
| 45 | { |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 46 | face = ndn::util::makeDummyClientFace(); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 47 | |
| 48 | manager = make_shared<RibManager>(ndn::ref(*face)); |
| 49 | manager->registerWithNfd(); |
| 50 | |
| 51 | face->processEvents(time::milliseconds(1)); |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 52 | face->sentInterests.clear(); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ~RibManagerFixture() |
| 56 | { |
| 57 | manager.reset(); |
| 58 | face.reset(); |
| 59 | } |
| 60 | |
| 61 | void extractParameters(Interest& interest, Name::Component& verb, |
| 62 | ControlParameters& extractedParameters) |
| 63 | { |
| 64 | const Name& name = interest.getName(); |
| 65 | verb = name[COMMAND_PREFIX.size()]; |
| 66 | const Name::Component& parameterComponent = name[COMMAND_PREFIX.size() + 1]; |
| 67 | |
| 68 | Block rawParameters = parameterComponent.blockFromValue(); |
| 69 | extractedParameters.wireDecode(rawParameters); |
| 70 | } |
| 71 | |
| 72 | void receiveCommandInterest(Name& name, ControlParameters& parameters) |
| 73 | { |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 74 | receiveCommandInterest(name.append(parameters.wireEncode())); |
| 75 | } |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 76 | |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 77 | void receiveCommandInterest(const Name& name) |
| 78 | { |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 79 | Interest command(name); |
| 80 | |
| 81 | face->receive(command); |
| 82 | face->processEvents(time::milliseconds(1)); |
| 83 | } |
| 84 | |
| 85 | public: |
| 86 | shared_ptr<RibManager> manager; |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 87 | shared_ptr<ndn::util::DummyClientFace> face; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 88 | |
| 89 | const Name COMMAND_PREFIX; |
| 90 | const Name::Component ADD_NEXTHOP_VERB; |
| 91 | const Name::Component REMOVE_NEXTHOP_VERB; |
| 92 | }; |
| 93 | |
| 94 | class AuthorizedRibManager : public RibManagerFixture |
| 95 | { |
| 96 | public: |
| 97 | AuthorizedRibManager() |
| 98 | { |
| 99 | ConfigFile config; |
| 100 | manager->setConfigFile(config); |
| 101 | |
| 102 | const std::string CONFIG_STRING = |
| 103 | "rib\n" |
| 104 | "{\n" |
| 105 | " localhost_security\n" |
| 106 | " {\n" |
| 107 | " trust-anchor\n" |
| 108 | " {\n" |
| 109 | " type any\n" |
| 110 | " }\n" |
| 111 | " }" |
| 112 | "}"; |
| 113 | |
| 114 | config.parse(CONFIG_STRING, true, "test-rib"); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | typedef RibManagerFixture UnauthorizedRibManager; |
| 119 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 120 | BOOST_FIXTURE_TEST_SUITE(RibManager, RibManagerFixture) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 121 | |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 122 | BOOST_FIXTURE_TEST_CASE(ShortName, AuthorizedRibManager) |
| 123 | { |
| 124 | Name commandName("/localhost/nfd/rib"); |
| 125 | receiveCommandInterest(commandName); |
| 126 | // TODO verify error response |
| 127 | } |
| 128 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 129 | BOOST_FIXTURE_TEST_CASE(Basic, AuthorizedRibManager) |
| 130 | { |
| 131 | ControlParameters parameters; |
| 132 | parameters |
| 133 | .setName("/hello") |
| 134 | .setFaceId(1) |
| 135 | .setCost(10) |
| 136 | .setFlags(0) |
| 137 | .setOrigin(128) |
| 138 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 139 | |
| 140 | Name commandName("/localhost/nfd/rib/register"); |
| 141 | |
| 142 | receiveCommandInterest(commandName, parameters); |
| 143 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 144 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | BOOST_FIXTURE_TEST_CASE(Register, AuthorizedRibManager) |
| 148 | { |
| 149 | ControlParameters parameters; |
| 150 | parameters |
| 151 | .setName("/hello") |
| 152 | .setFaceId(1) |
| 153 | .setCost(10) |
| 154 | .setFlags(0) |
| 155 | .setOrigin(128) |
| 156 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 157 | |
| 158 | Name commandName("/localhost/nfd/rib/register"); |
| 159 | |
| 160 | receiveCommandInterest(commandName, parameters); |
| 161 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 162 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 163 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 164 | Interest& request = face->sentInterests[0]; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 165 | |
| 166 | ControlParameters extractedParameters; |
| 167 | Name::Component verb; |
| 168 | extractParameters(request, verb, extractedParameters); |
| 169 | |
| 170 | BOOST_CHECK_EQUAL(verb, ADD_NEXTHOP_VERB); |
| 171 | BOOST_CHECK_EQUAL(extractedParameters.getName(), parameters.getName()); |
| 172 | BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), parameters.getFaceId()); |
| 173 | BOOST_CHECK_EQUAL(extractedParameters.getCost(), parameters.getCost()); |
| 174 | } |
| 175 | |
| 176 | BOOST_FIXTURE_TEST_CASE(Unregister, AuthorizedRibManager) |
| 177 | { |
| 178 | ControlParameters addParameters; |
| 179 | addParameters |
| 180 | .setName("/hello") |
| 181 | .setFaceId(1) |
| 182 | .setCost(10) |
| 183 | .setFlags(0) |
| 184 | .setOrigin(128) |
| 185 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 186 | |
| 187 | Name registerName("/localhost/nfd/rib/register"); |
| 188 | |
| 189 | receiveCommandInterest(registerName, addParameters); |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 190 | face->sentInterests.clear(); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 191 | |
| 192 | ControlParameters removeParameters; |
| 193 | removeParameters |
| 194 | .setName("/hello") |
| 195 | .setFaceId(1) |
| 196 | .setOrigin(128); |
| 197 | |
| 198 | Name unregisterName("/localhost/nfd/rib/unregister"); |
| 199 | |
| 200 | receiveCommandInterest(unregisterName, removeParameters); |
| 201 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 202 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 203 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 204 | Interest& request = face->sentInterests[0]; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 205 | |
| 206 | ControlParameters extractedParameters; |
| 207 | Name::Component verb; |
| 208 | extractParameters(request, verb, extractedParameters); |
| 209 | |
| 210 | BOOST_CHECK_EQUAL(verb, REMOVE_NEXTHOP_VERB); |
| 211 | BOOST_CHECK_EQUAL(extractedParameters.getName(), removeParameters.getName()); |
| 212 | BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), removeParameters.getFaceId()); |
| 213 | } |
| 214 | |
| 215 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedRibManager) |
| 216 | { |
| 217 | ControlParameters parameters; |
| 218 | parameters |
| 219 | .setName("/hello") |
| 220 | .setFaceId(1) |
| 221 | .setCost(10) |
| 222 | .setFlags(0) |
| 223 | .setOrigin(128) |
| 224 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 225 | |
| 226 | Name commandName("/localhost/nfd/rib/register"); |
| 227 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 228 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 229 | |
| 230 | receiveCommandInterest(commandName, parameters); |
| 231 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 232 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 235 | BOOST_FIXTURE_TEST_CASE(RibStatusRequest, AuthorizedRibManager) |
| 236 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 237 | Route route; |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 238 | Name name("/"); |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 239 | route.faceId = 1; |
| 240 | route.origin = 128; |
| 241 | route.cost = 32; |
| 242 | route.flags = ndn::nfd::ROUTE_FLAG_CAPTURE; |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 243 | |
| 244 | ControlParameters parameters; |
| 245 | parameters |
| 246 | .setName(name) |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 247 | .setFaceId(route.faceId) |
| 248 | .setOrigin(route.origin) |
| 249 | .setCost(route.cost) |
| 250 | .setFlags(route.flags) |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 251 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 252 | |
| 253 | Name commandName("/localhost/nfd/rib/register"); |
| 254 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 255 | BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 256 | |
| 257 | receiveCommandInterest(commandName, parameters); |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 258 | face->sentInterests.clear(); |
| 259 | face->sentDatas.clear(); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 260 | |
| 261 | face->receive(Interest("/localhost/nfd/rib/list")); |
| 262 | face->processEvents(time::milliseconds(1)); |
| 263 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 264 | BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1); |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 265 | RibStatusPublisherFixture::decodeRibEntryBlock(face->sentDatas[0], name, route); |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 268 | BOOST_FIXTURE_TEST_CASE(CancelExpirationEvent, AuthorizedRibManager) |
| 269 | { |
| 270 | // Register face |
| 271 | ControlParameters addParameters; |
| 272 | addParameters |
| 273 | .setName("/expire") |
| 274 | .setFaceId(1) |
| 275 | .setCost(10) |
| 276 | .setFlags(0) |
| 277 | .setOrigin(128) |
| 278 | .setExpirationPeriod(ndn::time::milliseconds(500)); |
| 279 | |
| 280 | Name registerName("/localhost/nfd/rib/register"); |
| 281 | |
| 282 | receiveCommandInterest(registerName, addParameters); |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 283 | face->sentInterests.clear(); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 284 | |
| 285 | // Unregister face |
| 286 | ControlParameters removeParameters; |
| 287 | removeParameters |
| 288 | .setName("/expire") |
| 289 | .setFaceId(1) |
| 290 | .setOrigin(128); |
| 291 | |
| 292 | Name unregisterName("/localhost/nfd/rib/unregister"); |
| 293 | |
| 294 | receiveCommandInterest(unregisterName, removeParameters); |
| 295 | |
| 296 | // Reregister face |
| 297 | Name reRegisterName("/localhost/nfd/rib/register"); |
| 298 | addParameters.setExpirationPeriod(ndn::time::milliseconds::max()); |
| 299 | receiveCommandInterest(reRegisterName, addParameters); |
| 300 | |
| 301 | nfd::tests::LimitedIo limitedIo; |
| 302 | limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 303 | |
| 304 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1); |
| 305 | } |
| 306 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 307 | BOOST_FIXTURE_TEST_CASE(RemoveInvalidFaces, AuthorizedRibManager) |
| 308 | { |
| 309 | // Register valid face |
| 310 | ControlParameters validParameters; |
| 311 | validParameters |
| 312 | .setName("/test") |
| 313 | .setFaceId(1); |
| 314 | |
| 315 | Name validName("/localhost/nfd/rib/register"); |
| 316 | receiveCommandInterest(validName, validParameters); |
| 317 | |
| 318 | // Register invalid face |
| 319 | ControlParameters invalidParameters; |
| 320 | invalidParameters |
| 321 | .setName("/test") |
| 322 | .setFaceId(2); |
| 323 | |
| 324 | Name invalidName("/localhost/nfd/rib/register"); |
| 325 | receiveCommandInterest(invalidName, invalidParameters); |
| 326 | |
| 327 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 2); |
| 328 | |
| 329 | // Receive status with only faceId: 1 |
| 330 | ndn::nfd::FaceStatus status; |
| 331 | status.setFaceId(1); |
| 332 | |
| 333 | shared_ptr<Data> data = nfd::tests::makeData("/localhost/nfd/faces/list"); |
| 334 | data->setContent(status.wireEncode()); |
| 335 | |
| 336 | shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>(); |
| 337 | buffer->write(reinterpret_cast<const char*>(data->getContent().value()), |
| 338 | data->getContent().value_size()); |
| 339 | |
| 340 | manager->removeInvalidFaces(buffer); |
| 341 | |
| 342 | // Run scheduler |
| 343 | nfd::tests::LimitedIo limitedIo; |
| 344 | limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 345 | |
| 346 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1); |
| 347 | |
| 348 | Rib::const_iterator it = manager->m_managedRib.find("/test"); |
| 349 | BOOST_REQUIRE(it != manager->m_managedRib.end()); |
| 350 | |
| 351 | shared_ptr<RibEntry> entry = it->second; |
| 352 | BOOST_CHECK_EQUAL(entry->hasFaceId(1), true); |
| 353 | BOOST_CHECK_EQUAL(entry->hasFaceId(2), false); |
| 354 | } |
| 355 | |
Vince Lehman | 7c7d33a | 2015-01-20 17:40:26 -0600 | [diff] [blame] | 356 | BOOST_FIXTURE_TEST_CASE(LocalHopInherit, AuthorizedRibManager) |
| 357 | { |
| 358 | using nfd::rib::RibManager; |
| 359 | |
| 360 | // Simulate NFD response |
| 361 | ControlParameters result; |
| 362 | result.setFaceId(261); |
| 363 | |
| 364 | manager->onNrdCommandPrefixAddNextHopSuccess(RibManager::REMOTE_COMMAND_PREFIX, result); |
| 365 | |
| 366 | // Register route that localhop prefix should inherit |
| 367 | ControlParameters parameters; |
| 368 | parameters |
| 369 | .setName("/localhop/nfd") |
| 370 | .setFaceId(262) |
| 371 | .setCost(25) |
| 372 | .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
| 373 | |
| 374 | Name commandName("/localhost/nfd/rib/register"); |
| 375 | |
| 376 | receiveCommandInterest(commandName, parameters); |
| 377 | |
| 378 | // REMOTE_COMMAND_PREFIX should have its original route and the inherited route |
| 379 | auto it = manager->m_managedRib.find(RibManager::REMOTE_COMMAND_PREFIX); |
| 380 | |
| 381 | BOOST_REQUIRE(it != manager->m_managedRib.end()); |
| 382 | const RibEntry::RouteList& inheritedRoutes = (*(it->second)).getInheritedRoutes(); |
| 383 | |
| 384 | BOOST_CHECK_EQUAL(inheritedRoutes.size(), 1); |
| 385 | auto routeIt = inheritedRoutes.begin(); |
| 386 | |
| 387 | BOOST_CHECK_EQUAL(routeIt->faceId, 262); |
| 388 | BOOST_CHECK_EQUAL(routeIt->cost, 25); |
| 389 | } |
| 390 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 391 | BOOST_AUTO_TEST_SUITE_END() |
| 392 | |
| 393 | } // namespace tests |
| 394 | } // namespace rib |
| 395 | } // namespace nfd |