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" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 29 | #include "tests/dummy-client-face.hpp" |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 30 | #include "tests/limited-io.hpp" |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 31 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 32 | #include "rib/rib-status-publisher-common.hpp" |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 33 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/management/nfd-face-status.hpp> |
| 35 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 36 | namespace nfd { |
| 37 | namespace rib { |
| 38 | namespace tests { |
| 39 | |
| 40 | class RibManagerFixture : public nfd::tests::BaseFixture |
| 41 | { |
| 42 | public: |
| 43 | RibManagerFixture() |
| 44 | : COMMAND_PREFIX("/localhost/nfd/rib") |
| 45 | , ADD_NEXTHOP_VERB("add-nexthop") |
| 46 | , REMOVE_NEXTHOP_VERB("remove-nexthop") |
| 47 | { |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 48 | face = nfd::tests::makeDummyClientFace(); |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 49 | |
| 50 | manager = make_shared<RibManager>(ndn::ref(*face)); |
| 51 | manager->registerWithNfd(); |
| 52 | |
| 53 | face->processEvents(time::milliseconds(1)); |
| 54 | face->m_sentInterests.clear(); |
| 55 | } |
| 56 | |
| 57 | ~RibManagerFixture() |
| 58 | { |
| 59 | manager.reset(); |
| 60 | face.reset(); |
| 61 | } |
| 62 | |
| 63 | void extractParameters(Interest& interest, Name::Component& verb, |
| 64 | ControlParameters& extractedParameters) |
| 65 | { |
| 66 | const Name& name = interest.getName(); |
| 67 | verb = name[COMMAND_PREFIX.size()]; |
| 68 | const Name::Component& parameterComponent = name[COMMAND_PREFIX.size() + 1]; |
| 69 | |
| 70 | Block rawParameters = parameterComponent.blockFromValue(); |
| 71 | extractedParameters.wireDecode(rawParameters); |
| 72 | } |
| 73 | |
| 74 | void receiveCommandInterest(Name& name, ControlParameters& parameters) |
| 75 | { |
| 76 | name.append(parameters.wireEncode()); |
| 77 | |
| 78 | Interest command(name); |
| 79 | |
| 80 | face->receive(command); |
| 81 | face->processEvents(time::milliseconds(1)); |
| 82 | } |
| 83 | |
| 84 | public: |
| 85 | shared_ptr<RibManager> manager; |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 86 | shared_ptr<nfd::tests::DummyClientFace> face; |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 87 | |
| 88 | const Name COMMAND_PREFIX; |
| 89 | const Name::Component ADD_NEXTHOP_VERB; |
| 90 | const Name::Component REMOVE_NEXTHOP_VERB; |
| 91 | }; |
| 92 | |
| 93 | class AuthorizedRibManager : public RibManagerFixture |
| 94 | { |
| 95 | public: |
| 96 | AuthorizedRibManager() |
| 97 | { |
| 98 | ConfigFile config; |
| 99 | manager->setConfigFile(config); |
| 100 | |
| 101 | const std::string CONFIG_STRING = |
| 102 | "rib\n" |
| 103 | "{\n" |
| 104 | " localhost_security\n" |
| 105 | " {\n" |
| 106 | " trust-anchor\n" |
| 107 | " {\n" |
| 108 | " type any\n" |
| 109 | " }\n" |
| 110 | " }" |
| 111 | "}"; |
| 112 | |
| 113 | config.parse(CONFIG_STRING, true, "test-rib"); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | typedef RibManagerFixture UnauthorizedRibManager; |
| 118 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 119 | BOOST_FIXTURE_TEST_SUITE(RibManager, RibManagerFixture) |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 120 | |
| 121 | BOOST_FIXTURE_TEST_CASE(Basic, AuthorizedRibManager) |
| 122 | { |
| 123 | ControlParameters parameters; |
| 124 | parameters |
| 125 | .setName("/hello") |
| 126 | .setFaceId(1) |
| 127 | .setCost(10) |
| 128 | .setFlags(0) |
| 129 | .setOrigin(128) |
| 130 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 131 | |
| 132 | Name commandName("/localhost/nfd/rib/register"); |
| 133 | |
| 134 | receiveCommandInterest(commandName, parameters); |
| 135 | |
| 136 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 137 | } |
| 138 | |
| 139 | BOOST_FIXTURE_TEST_CASE(Register, AuthorizedRibManager) |
| 140 | { |
| 141 | ControlParameters parameters; |
| 142 | parameters |
| 143 | .setName("/hello") |
| 144 | .setFaceId(1) |
| 145 | .setCost(10) |
| 146 | .setFlags(0) |
| 147 | .setOrigin(128) |
| 148 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 149 | |
| 150 | Name commandName("/localhost/nfd/rib/register"); |
| 151 | |
| 152 | receiveCommandInterest(commandName, parameters); |
| 153 | |
| 154 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 155 | |
| 156 | Interest& request = face->m_sentInterests[0]; |
| 157 | |
| 158 | ControlParameters extractedParameters; |
| 159 | Name::Component verb; |
| 160 | extractParameters(request, verb, extractedParameters); |
| 161 | |
| 162 | BOOST_CHECK_EQUAL(verb, ADD_NEXTHOP_VERB); |
| 163 | BOOST_CHECK_EQUAL(extractedParameters.getName(), parameters.getName()); |
| 164 | BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), parameters.getFaceId()); |
| 165 | BOOST_CHECK_EQUAL(extractedParameters.getCost(), parameters.getCost()); |
| 166 | } |
| 167 | |
| 168 | BOOST_FIXTURE_TEST_CASE(Unregister, AuthorizedRibManager) |
| 169 | { |
| 170 | ControlParameters addParameters; |
| 171 | addParameters |
| 172 | .setName("/hello") |
| 173 | .setFaceId(1) |
| 174 | .setCost(10) |
| 175 | .setFlags(0) |
| 176 | .setOrigin(128) |
| 177 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 178 | |
| 179 | Name registerName("/localhost/nfd/rib/register"); |
| 180 | |
| 181 | receiveCommandInterest(registerName, addParameters); |
| 182 | face->m_sentInterests.clear(); |
| 183 | |
| 184 | ControlParameters removeParameters; |
| 185 | removeParameters |
| 186 | .setName("/hello") |
| 187 | .setFaceId(1) |
| 188 | .setOrigin(128); |
| 189 | |
| 190 | Name unregisterName("/localhost/nfd/rib/unregister"); |
| 191 | |
| 192 | receiveCommandInterest(unregisterName, removeParameters); |
| 193 | |
| 194 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 195 | |
| 196 | Interest& request = face->m_sentInterests[0]; |
| 197 | |
| 198 | ControlParameters extractedParameters; |
| 199 | Name::Component verb; |
| 200 | extractParameters(request, verb, extractedParameters); |
| 201 | |
| 202 | BOOST_CHECK_EQUAL(verb, REMOVE_NEXTHOP_VERB); |
| 203 | BOOST_CHECK_EQUAL(extractedParameters.getName(), removeParameters.getName()); |
| 204 | BOOST_CHECK_EQUAL(extractedParameters.getFaceId(), removeParameters.getFaceId()); |
| 205 | } |
| 206 | |
| 207 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedRibManager) |
| 208 | { |
| 209 | ControlParameters parameters; |
| 210 | parameters |
| 211 | .setName("/hello") |
| 212 | .setFaceId(1) |
| 213 | .setCost(10) |
| 214 | .setFlags(0) |
| 215 | .setOrigin(128) |
| 216 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 217 | |
| 218 | Name commandName("/localhost/nfd/rib/register"); |
| 219 | |
| 220 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0); |
| 221 | |
| 222 | receiveCommandInterest(commandName, parameters); |
| 223 | |
| 224 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0); |
| 225 | } |
| 226 | |
Vince Lehman | cd16c83 | 2014-07-23 15:14:55 -0700 | [diff] [blame] | 227 | BOOST_FIXTURE_TEST_CASE(RibStatusRequest, AuthorizedRibManager) |
| 228 | { |
| 229 | FaceEntry entry; |
| 230 | Name name("/"); |
| 231 | entry.faceId = 1; |
| 232 | entry.origin = 128; |
| 233 | entry.cost = 32; |
| 234 | entry.flags = ndn::nfd::ROUTE_FLAG_CAPTURE; |
| 235 | |
| 236 | ControlParameters parameters; |
| 237 | parameters |
| 238 | .setName(name) |
| 239 | .setFaceId(entry.faceId) |
| 240 | .setOrigin(entry.origin) |
| 241 | .setCost(entry.cost) |
| 242 | .setFlags(entry.flags) |
| 243 | .setExpirationPeriod(ndn::time::milliseconds::max()); |
| 244 | |
| 245 | Name commandName("/localhost/nfd/rib/register"); |
| 246 | |
| 247 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 0); |
| 248 | |
| 249 | receiveCommandInterest(commandName, parameters); |
| 250 | face->m_sentInterests.clear(); |
| 251 | face->m_sentDatas.clear(); |
| 252 | |
| 253 | face->receive(Interest("/localhost/nfd/rib/list")); |
| 254 | face->processEvents(time::milliseconds(1)); |
| 255 | |
| 256 | BOOST_REQUIRE_EQUAL(face->m_sentDatas.size(), 1); |
| 257 | RibStatusPublisherFixture::decodeRibEntryBlock(face->m_sentDatas[0], name, entry); |
| 258 | } |
| 259 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 260 | BOOST_FIXTURE_TEST_CASE(CancelExpirationEvent, AuthorizedRibManager) |
| 261 | { |
| 262 | // Register face |
| 263 | ControlParameters addParameters; |
| 264 | addParameters |
| 265 | .setName("/expire") |
| 266 | .setFaceId(1) |
| 267 | .setCost(10) |
| 268 | .setFlags(0) |
| 269 | .setOrigin(128) |
| 270 | .setExpirationPeriod(ndn::time::milliseconds(500)); |
| 271 | |
| 272 | Name registerName("/localhost/nfd/rib/register"); |
| 273 | |
| 274 | receiveCommandInterest(registerName, addParameters); |
| 275 | face->m_sentInterests.clear(); |
| 276 | |
| 277 | // Unregister face |
| 278 | ControlParameters removeParameters; |
| 279 | removeParameters |
| 280 | .setName("/expire") |
| 281 | .setFaceId(1) |
| 282 | .setOrigin(128); |
| 283 | |
| 284 | Name unregisterName("/localhost/nfd/rib/unregister"); |
| 285 | |
| 286 | receiveCommandInterest(unregisterName, removeParameters); |
| 287 | |
| 288 | // Reregister face |
| 289 | Name reRegisterName("/localhost/nfd/rib/register"); |
| 290 | addParameters.setExpirationPeriod(ndn::time::milliseconds::max()); |
| 291 | receiveCommandInterest(reRegisterName, addParameters); |
| 292 | |
| 293 | nfd::tests::LimitedIo limitedIo; |
| 294 | limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 295 | |
| 296 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1); |
| 297 | } |
| 298 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 299 | BOOST_FIXTURE_TEST_CASE(RemoveInvalidFaces, AuthorizedRibManager) |
| 300 | { |
| 301 | // Register valid face |
| 302 | ControlParameters validParameters; |
| 303 | validParameters |
| 304 | .setName("/test") |
| 305 | .setFaceId(1); |
| 306 | |
| 307 | Name validName("/localhost/nfd/rib/register"); |
| 308 | receiveCommandInterest(validName, validParameters); |
| 309 | |
| 310 | // Register invalid face |
| 311 | ControlParameters invalidParameters; |
| 312 | invalidParameters |
| 313 | .setName("/test") |
| 314 | .setFaceId(2); |
| 315 | |
| 316 | Name invalidName("/localhost/nfd/rib/register"); |
| 317 | receiveCommandInterest(invalidName, invalidParameters); |
| 318 | |
| 319 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 2); |
| 320 | |
| 321 | // Receive status with only faceId: 1 |
| 322 | ndn::nfd::FaceStatus status; |
| 323 | status.setFaceId(1); |
| 324 | |
| 325 | shared_ptr<Data> data = nfd::tests::makeData("/localhost/nfd/faces/list"); |
| 326 | data->setContent(status.wireEncode()); |
| 327 | |
| 328 | shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>(); |
| 329 | buffer->write(reinterpret_cast<const char*>(data->getContent().value()), |
| 330 | data->getContent().value_size()); |
| 331 | |
| 332 | manager->removeInvalidFaces(buffer); |
| 333 | |
| 334 | // Run scheduler |
| 335 | nfd::tests::LimitedIo limitedIo; |
| 336 | limitedIo.run(nfd::tests::LimitedIo::UNLIMITED_OPS, time::seconds(1)); |
| 337 | |
| 338 | BOOST_REQUIRE_EQUAL(manager->m_managedRib.size(), 1); |
| 339 | |
| 340 | Rib::const_iterator it = manager->m_managedRib.find("/test"); |
| 341 | BOOST_REQUIRE(it != manager->m_managedRib.end()); |
| 342 | |
| 343 | shared_ptr<RibEntry> entry = it->second; |
| 344 | BOOST_CHECK_EQUAL(entry->hasFaceId(1), true); |
| 345 | BOOST_CHECK_EQUAL(entry->hasFaceId(2), false); |
| 346 | } |
| 347 | |
Vince Lehman | 72446ec | 2014-07-09 10:50:02 -0500 | [diff] [blame] | 348 | BOOST_AUTO_TEST_SUITE_END() |
| 349 | |
| 350 | } // namespace tests |
| 351 | } // namespace rib |
| 352 | } // namespace nfd |