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