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