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