Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 25 | |
| 26 | #include "mgmt/strategy-choice-manager.hpp" |
| 27 | #include "face/face.hpp" |
| 28 | #include "mgmt/internal-face.hpp" |
| 29 | #include "table/name-tree.hpp" |
| 30 | #include "table/strategy-choice.hpp" |
| 31 | #include "fw/forwarder.hpp" |
| 32 | #include "fw/strategy.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 33 | #include "tests/daemon/face/dummy-face.hpp" |
| 34 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 35 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 36 | #include <ndn-cxx/management/nfd-strategy-choice.hpp> |
| 37 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 38 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 39 | #include "validation-common.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 40 | |
| 41 | namespace nfd { |
| 42 | namespace tests { |
| 43 | |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 44 | NFD_LOG_INIT("MgmtStrategyChoiceManager"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 45 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 46 | class StrategyChoiceManagerFixture : protected BaseFixture |
| 47 | { |
| 48 | public: |
| 49 | |
| 50 | StrategyChoiceManagerFixture() |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 51 | : m_strategyChoice(m_forwarder.getStrategyChoice()) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 52 | , m_face(make_shared<InternalFace>()) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 53 | , m_manager(m_strategyChoice, m_face, m_keyChain) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 54 | , m_callbackFired(false) |
| 55 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 56 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 57 | "/localhost/nfd/strategy/test-strategy-a")); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 58 | m_strategyChoice.insert("ndn:/", "/localhost/nfd/strategy/test-strategy-a"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 61 | virtual |
| 62 | ~StrategyChoiceManagerFixture() |
| 63 | { |
| 64 | |
| 65 | } |
| 66 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 67 | void |
| 68 | validateControlResponseCommon(const Data& response, |
| 69 | const Name& expectedName, |
| 70 | uint32_t expectedCode, |
| 71 | const std::string& expectedText, |
| 72 | ControlResponse& control) |
| 73 | { |
| 74 | m_callbackFired = true; |
| 75 | Block controlRaw = response.getContent().blockFromValue(); |
| 76 | |
| 77 | control.wireDecode(controlRaw); |
| 78 | |
| 79 | NFD_LOG_DEBUG("received control response" |
| 80 | << " Name: " << response.getName() |
| 81 | << " code: " << control.getCode() |
| 82 | << " text: " << control.getText()); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(response.getName(), expectedName); |
| 85 | BOOST_CHECK_EQUAL(control.getCode(), expectedCode); |
| 86 | BOOST_CHECK_EQUAL(control.getText(), expectedText); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | validateControlResponse(const Data& response, |
| 91 | const Name& expectedName, |
| 92 | uint32_t expectedCode, |
| 93 | const std::string& expectedText) |
| 94 | { |
| 95 | ControlResponse control; |
| 96 | validateControlResponseCommon(response, expectedName, |
| 97 | expectedCode, expectedText, control); |
| 98 | |
| 99 | if (!control.getBody().empty()) |
| 100 | { |
| 101 | BOOST_FAIL("found unexpected control response body"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | validateControlResponse(const Data& response, |
| 107 | const Name& expectedName, |
| 108 | uint32_t expectedCode, |
| 109 | const std::string& expectedText, |
| 110 | const Block& expectedBody) |
| 111 | { |
| 112 | ControlResponse control; |
| 113 | validateControlResponseCommon(response, expectedName, |
| 114 | expectedCode, expectedText, control); |
| 115 | |
| 116 | BOOST_REQUIRE(!control.getBody().empty()); |
| 117 | BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size()); |
| 118 | |
| 119 | BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(), |
| 120 | expectedBody.value_size()) == 0); |
| 121 | |
| 122 | } |
| 123 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 124 | void |
| 125 | validateList(const Data& data, const ndn::nfd::StrategyChoice& expectedChoice) |
| 126 | { |
| 127 | m_callbackFired = true; |
| 128 | ndn::nfd::StrategyChoice choice(data.getContent().blockFromValue()); |
| 129 | BOOST_CHECK_EQUAL(choice.getStrategy(), expectedChoice.getStrategy()); |
| 130 | BOOST_CHECK_EQUAL(choice.getName(), expectedChoice.getName()); |
| 131 | } |
| 132 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 133 | bool |
| 134 | didCallbackFire() |
| 135 | { |
| 136 | return m_callbackFired; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | resetCallbackFired() |
| 141 | { |
| 142 | m_callbackFired = false; |
| 143 | } |
| 144 | |
| 145 | shared_ptr<InternalFace>& |
| 146 | getFace() |
| 147 | { |
| 148 | return m_face; |
| 149 | } |
| 150 | |
| 151 | StrategyChoiceManager& |
| 152 | getManager() |
| 153 | { |
| 154 | return m_manager; |
| 155 | } |
| 156 | |
| 157 | StrategyChoice& |
| 158 | getStrategyChoice() |
| 159 | { |
| 160 | return m_strategyChoice; |
| 161 | } |
| 162 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 163 | void |
| 164 | addInterestRule(const std::string& regex, |
| 165 | ndn::IdentityCertificate& certificate) |
| 166 | { |
| 167 | m_manager.addInterestRule(regex, certificate); |
| 168 | } |
| 169 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 170 | protected: |
| 171 | Forwarder m_forwarder; |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 172 | StrategyChoice& m_strategyChoice; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 173 | shared_ptr<InternalFace> m_face; |
| 174 | StrategyChoiceManager m_manager; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 175 | ndn::KeyChain m_keyChain; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | bool m_callbackFired; |
| 179 | }; |
| 180 | |
| 181 | class AllStrategiesFixture : public StrategyChoiceManagerFixture |
| 182 | { |
| 183 | public: |
| 184 | AllStrategiesFixture() |
| 185 | { |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 186 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 187 | "/localhost/nfd/strategy/test-strategy-b")); |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 188 | |
| 189 | const Name strategyCVersion1("/localhost/nfd/strategy/test-strategy-c/%FD%01"); |
| 190 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 191 | strategyCVersion1)); |
| 192 | |
| 193 | const Name strategyCVersion2("/localhost/nfd/strategy/test-strategy-c/%FD%02"); |
| 194 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 195 | strategyCVersion2)); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | virtual |
| 199 | ~AllStrategiesFixture() |
| 200 | { |
| 201 | |
| 202 | } |
| 203 | }; |
| 204 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 205 | template <typename T> class AuthorizedCommandFixture : public CommandFixture<T> |
| 206 | { |
| 207 | public: |
| 208 | AuthorizedCommandFixture() |
| 209 | { |
| 210 | const std::string regex = "^<localhost><nfd><strategy-choice>"; |
| 211 | T::addInterestRule(regex, *CommandFixture<T>::m_certificate); |
| 212 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 213 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 214 | virtual |
| 215 | ~AuthorizedCommandFixture() |
| 216 | { |
| 217 | |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager, |
| 222 | AuthorizedCommandFixture<AllStrategiesFixture>) |
| 223 | |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame^] | 224 | BOOST_FIXTURE_TEST_CASE(ShortName, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 225 | { |
| 226 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 227 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 228 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 229 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 230 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 231 | |
| 232 | getFace()->sendInterest(*command); |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 233 | g_io.run_one(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 234 | |
| 235 | BOOST_REQUIRE(didCallbackFire()); |
| 236 | } |
| 237 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 238 | BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 239 | { |
| 240 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 241 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 242 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 243 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 244 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 245 | |
| 246 | getManager().onStrategyChoiceRequest(*command); |
| 247 | |
| 248 | BOOST_REQUIRE(didCallbackFire()); |
| 249 | } |
| 250 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 251 | BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture) |
| 252 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 253 | ControlParameters parameters; |
| 254 | parameters.setName("/test"); |
| 255 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 256 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 257 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 258 | |
| 259 | Name commandName("/localhost/nfd/strategy-choice"); |
| 260 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 261 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 262 | |
| 263 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 264 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 265 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 266 | this->validateControlResponse(response, command->getName(), 401, "Signature required"); |
| 267 | }; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 268 | |
| 269 | getManager().onStrategyChoiceRequest(*command); |
| 270 | |
| 271 | BOOST_REQUIRE(didCallbackFire()); |
| 272 | } |
| 273 | |
| 274 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, |
| 275 | UnauthorizedCommandFixture<StrategyChoiceManagerFixture>) |
| 276 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 277 | ControlParameters parameters; |
| 278 | parameters.setName("/test"); |
| 279 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 280 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 281 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 282 | |
| 283 | Name commandName("/localhost/nfd/strategy-choice"); |
| 284 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 285 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 286 | |
| 287 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 288 | generateCommand(*command); |
| 289 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 290 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 291 | this->validateControlResponse(response, command->getName(), 403, "Unauthorized command"); |
| 292 | }; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 293 | |
| 294 | getManager().onStrategyChoiceRequest(*command); |
| 295 | |
| 296 | BOOST_REQUIRE(didCallbackFire()); |
| 297 | } |
| 298 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 299 | BOOST_AUTO_TEST_CASE(UnsupportedVerb) |
| 300 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 301 | ControlParameters parameters; |
| 302 | parameters.setName("/test"); |
| 303 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 304 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 305 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 306 | |
| 307 | Name commandName("/localhost/nfd/strategy-choice"); |
| 308 | commandName.append("unsupported"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 309 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 310 | |
| 311 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 312 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 313 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 314 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 315 | this->validateControlResponse(response, command->getName(), 501, "Unsupported command"); |
| 316 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 317 | |
| 318 | getManager().onValidatedStrategyChoiceRequest(command); |
| 319 | |
| 320 | BOOST_REQUIRE(didCallbackFire()); |
| 321 | } |
| 322 | |
| 323 | BOOST_AUTO_TEST_CASE(BadOptionParse) |
| 324 | { |
| 325 | Name commandName("/localhost/nfd/strategy-choice"); |
| 326 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 327 | commandName.append("NotReallyParameters"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 328 | |
| 329 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 330 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 331 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 332 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 333 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 334 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 335 | |
| 336 | getManager().onValidatedStrategyChoiceRequest(command); |
| 337 | |
| 338 | BOOST_REQUIRE(didCallbackFire()); |
| 339 | } |
| 340 | |
| 341 | BOOST_AUTO_TEST_CASE(SetStrategies) |
| 342 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 343 | ControlParameters parameters; |
| 344 | parameters.setName("/test"); |
| 345 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 346 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 347 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 348 | |
| 349 | Name commandName("/localhost/nfd/strategy-choice"); |
| 350 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 351 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 352 | |
| 353 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 354 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 355 | getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) { |
| 356 | this->validateControlResponse(response, command->getName(), |
| 357 | 200, "Success", encodedParameters); |
| 358 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 359 | |
| 360 | getManager().onValidatedStrategyChoiceRequest(command); |
| 361 | |
| 362 | BOOST_REQUIRE(didCallbackFire()); |
| 363 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 364 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 365 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 366 | |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 367 | BOOST_AUTO_TEST_CASE(SetStrategySpecifiedVersion) |
| 368 | { |
| 369 | ControlParameters parameters; |
| 370 | parameters.setName("/test"); |
| 371 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%01"); |
| 372 | |
| 373 | Block encodedParameters(parameters.wireEncode()); |
| 374 | |
| 375 | Name commandName("/localhost/nfd/strategy-choice"); |
| 376 | commandName.append("set"); |
| 377 | commandName.append(encodedParameters); |
| 378 | |
| 379 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 380 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 381 | getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) { |
| 382 | this->validateControlResponse(response, command->getName(), |
| 383 | 200, "Success", encodedParameters); |
| 384 | }; |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 385 | |
| 386 | getManager().onValidatedStrategyChoiceRequest(command); |
| 387 | |
| 388 | BOOST_REQUIRE(didCallbackFire()); |
| 389 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 390 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-c/%FD%01"); |
| 391 | } |
| 392 | |
| 393 | BOOST_AUTO_TEST_CASE(SetStrategyLatestVersion) |
| 394 | { |
| 395 | ControlParameters parameters; |
| 396 | parameters.setName("/test"); |
| 397 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-c"); |
| 398 | |
| 399 | Block encodedParameters(parameters.wireEncode()); |
| 400 | |
| 401 | Name commandName("/localhost/nfd/strategy-choice"); |
| 402 | commandName.append("set"); |
| 403 | commandName.append(encodedParameters); |
| 404 | |
| 405 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 406 | |
| 407 | ControlParameters responseParameters; |
| 408 | responseParameters.setName("/test"); |
| 409 | responseParameters.setStrategy("/localhost/nfd/strategy/test-strategy-c/%FD%02"); |
| 410 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 411 | getFace()->onReceiveData += [this, command, responseParameters] (const Data& response) { |
| 412 | this->validateControlResponse(response, command->getName(), |
| 413 | 200, "Success", responseParameters.wireEncode()); |
| 414 | }; |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 415 | |
| 416 | getManager().onValidatedStrategyChoiceRequest(command); |
| 417 | |
| 418 | BOOST_REQUIRE(didCallbackFire()); |
| 419 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 420 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-c/%FD%02"); |
| 421 | } |
| 422 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 423 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingName) |
| 424 | { |
| 425 | ControlParameters parameters; |
| 426 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
| 427 | |
| 428 | Block encodedParameters(parameters.wireEncode()); |
| 429 | |
| 430 | Name commandName("/localhost/nfd/strategy-choice"); |
| 431 | commandName.append("set"); |
| 432 | commandName.append(encodedParameters); |
| 433 | |
| 434 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 435 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 436 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 437 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 438 | }; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 439 | |
| 440 | getManager().onValidatedStrategyChoiceRequest(command); |
| 441 | |
| 442 | BOOST_REQUIRE(didCallbackFire()); |
| 443 | } |
| 444 | |
| 445 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingStrategy) |
| 446 | { |
| 447 | ControlParameters parameters; |
| 448 | parameters.setName("/test"); |
| 449 | |
| 450 | Block encodedParameters(parameters.wireEncode()); |
| 451 | |
| 452 | Name commandName("/localhost/nfd/strategy-choice"); |
| 453 | commandName.append("set"); |
| 454 | commandName.append(encodedParameters); |
| 455 | |
| 456 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 457 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 458 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 459 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 460 | }; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 461 | |
| 462 | getManager().onValidatedStrategyChoiceRequest(command); |
| 463 | |
| 464 | BOOST_REQUIRE(didCallbackFire()); |
| 465 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 466 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy) |
| 470 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 471 | ControlParameters parameters; |
| 472 | parameters.setName("/test"); |
| 473 | parameters.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 474 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 475 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 476 | |
| 477 | Name commandName("/localhost/nfd/strategy-choice"); |
| 478 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 479 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 480 | |
| 481 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 482 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 483 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 484 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 485 | this->validateControlResponse(response, command->getName(), 504, "Unsupported strategy"); |
| 486 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 487 | |
| 488 | getManager().onValidatedStrategyChoiceRequest(command); |
| 489 | |
| 490 | BOOST_REQUIRE(didCallbackFire()); |
| 491 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 492 | BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 493 | } |
| 494 | |
| 495 | class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture |
| 496 | { |
| 497 | public: |
| 498 | DefaultStrategyOnlyFixture() |
| 499 | : StrategyChoiceManagerFixture() |
| 500 | { |
| 501 | |
| 502 | } |
| 503 | |
| 504 | virtual |
| 505 | ~DefaultStrategyOnlyFixture() |
| 506 | { |
| 507 | |
| 508 | } |
| 509 | }; |
| 510 | |
| 511 | |
| 512 | /// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504. |
| 513 | // BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture) |
| 514 | // { |
| 515 | // BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b")); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 516 | // ControlParameters parameters; |
| 517 | // parameters.setName("/test"); |
| 518 | // parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 519 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 520 | // Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 521 | |
| 522 | // Name commandName("/localhost/nfd/strategy-choice"); |
| 523 | // commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 524 | // commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 525 | |
| 526 | // shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 527 | // generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 528 | |
| 529 | // getFace()->onReceiveData += |
| 530 | // bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 531 | // command->getName(), 405, "Strategy not installed"); |
| 532 | |
| 533 | // getManager().onValidatedStrategyChoiceRequest(command); |
| 534 | |
| 535 | // BOOST_REQUIRE(didCallbackFire()); |
| 536 | // fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 537 | // BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 538 | // } |
| 539 | |
| 540 | BOOST_AUTO_TEST_CASE(Unset) |
| 541 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 542 | ControlParameters parameters; |
| 543 | parameters.setName("/test"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 544 | |
| 545 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 546 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 547 | "/localhost/nfd/strategy/test-strategy-b"); |
| 548 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 549 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 550 | |
| 551 | Name commandName("/localhost/nfd/strategy-choice"); |
| 552 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 553 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 554 | |
| 555 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 556 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 557 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 558 | getFace()->onReceiveData += [this, command, encodedParameters] (const Data& response) { |
| 559 | this->validateControlResponse(response, command->getName(), |
| 560 | 200, "Success", encodedParameters); |
| 561 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 562 | |
| 563 | getManager().onValidatedStrategyChoiceRequest(command); |
| 564 | |
| 565 | BOOST_REQUIRE(didCallbackFire()); |
| 566 | |
| 567 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 568 | "/localhost/nfd/strategy/test-strategy-a"); |
| 569 | } |
| 570 | |
| 571 | BOOST_AUTO_TEST_CASE(UnsetRoot) |
| 572 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 573 | ControlParameters parameters; |
| 574 | parameters.setName("/"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 575 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 576 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 577 | |
| 578 | Name commandName("/localhost/nfd/strategy-choice"); |
| 579 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 580 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 581 | |
| 582 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 583 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 584 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 585 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 586 | this->validateControlResponse(response, command->getName(), |
| 587 | 403, "Cannot unset root prefix strategy"); |
| 588 | }; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 589 | |
| 590 | getManager().onValidatedStrategyChoiceRequest(command); |
| 591 | |
| 592 | BOOST_REQUIRE(didCallbackFire()); |
| 593 | |
| 594 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 595 | "/localhost/nfd/strategy/test-strategy-a"); |
| 596 | } |
| 597 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 598 | BOOST_AUTO_TEST_CASE(UnsetMissingName) |
| 599 | { |
| 600 | ControlParameters parameters; |
| 601 | |
| 602 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 603 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 604 | "/localhost/nfd/strategy/test-strategy-b"); |
| 605 | |
| 606 | Block encodedParameters(parameters.wireEncode()); |
| 607 | |
| 608 | Name commandName("/localhost/nfd/strategy-choice"); |
| 609 | commandName.append("unset"); |
| 610 | commandName.append(encodedParameters); |
| 611 | |
| 612 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 613 | generateCommand(*command); |
| 614 | |
Junxiao Shi | cd55cde | 2014-11-13 16:03:24 -0700 | [diff] [blame] | 615 | getFace()->onReceiveData += [this, command] (const Data& response) { |
| 616 | this->validateControlResponse(response, command->getName(), 400, "Malformed command"); |
| 617 | }; |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 618 | |
| 619 | getManager().onValidatedStrategyChoiceRequest(command); |
| 620 | |
| 621 | BOOST_REQUIRE(didCallbackFire()); |
| 622 | |
| 623 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 624 | "/localhost/nfd/strategy/test-strategy-b"); |
| 625 | } |
| 626 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 627 | BOOST_AUTO_TEST_CASE(Publish) |
| 628 | { |
| 629 | Name commandName("/localhost/nfd/strategy-choice/list"); |
| 630 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 631 | |
| 632 | ndn::nfd::StrategyChoice expectedChoice; |
| 633 | expectedChoice.setStrategy("/localhost/nfd/strategy/test-strategy-a"); |
| 634 | expectedChoice.setName("/"); |
| 635 | |
| 636 | getFace()->onReceiveData += |
| 637 | bind(&StrategyChoiceManagerFixture::validateList, this, _1, expectedChoice); |
| 638 | |
| 639 | m_manager.onStrategyChoiceRequest(*command); |
| 640 | BOOST_REQUIRE(didCallbackFire()); |
| 641 | } |
| 642 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 643 | BOOST_AUTO_TEST_SUITE_END() |
| 644 | |
| 645 | } // namespace tests |
| 646 | } // namespace nfd |