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 | |
| 44 | NFD_LOG_INIT("StrategyChoiceManagerTest"); |
| 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 | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | virtual |
| 191 | ~AllStrategiesFixture() |
| 192 | { |
| 193 | |
| 194 | } |
| 195 | }; |
| 196 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 197 | template <typename T> class AuthorizedCommandFixture : public CommandFixture<T> |
| 198 | { |
| 199 | public: |
| 200 | AuthorizedCommandFixture() |
| 201 | { |
| 202 | const std::string regex = "^<localhost><nfd><strategy-choice>"; |
| 203 | T::addInterestRule(regex, *CommandFixture<T>::m_certificate); |
| 204 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 205 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 206 | virtual |
| 207 | ~AuthorizedCommandFixture() |
| 208 | { |
| 209 | |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoiceManager, |
| 214 | AuthorizedCommandFixture<AllStrategiesFixture>) |
| 215 | |
| 216 | BOOST_FIXTURE_TEST_CASE(TestFireInterestFilter, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 217 | { |
| 218 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 219 | |
| 220 | getFace()->onReceiveData += |
| 221 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 222 | command->getName(), 400, "Malformed command"); |
| 223 | |
| 224 | getFace()->sendInterest(*command); |
Junxiao Shi | 16d1b7d | 2014-03-27 21:29:09 -0700 | [diff] [blame] | 225 | g_io.run_one(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 226 | |
| 227 | BOOST_REQUIRE(didCallbackFire()); |
| 228 | } |
| 229 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 230 | BOOST_FIXTURE_TEST_CASE(MalformedCommmand, AllStrategiesFixture) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 231 | { |
| 232 | shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/strategy-choice")); |
| 233 | |
| 234 | getFace()->onReceiveData += |
| 235 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 236 | command->getName(), 400, "Malformed command"); |
| 237 | |
| 238 | getManager().onStrategyChoiceRequest(*command); |
| 239 | |
| 240 | BOOST_REQUIRE(didCallbackFire()); |
| 241 | } |
| 242 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 243 | BOOST_FIXTURE_TEST_CASE(UnsignedCommand, AllStrategiesFixture) |
| 244 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 245 | ControlParameters parameters; |
| 246 | parameters.setName("/test"); |
| 247 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 248 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 249 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 250 | |
| 251 | Name commandName("/localhost/nfd/strategy-choice"); |
| 252 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 253 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 254 | |
| 255 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 256 | |
| 257 | getFace()->onReceiveData += |
| 258 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 259 | command->getName(), 401, "Signature required"); |
| 260 | |
| 261 | getManager().onStrategyChoiceRequest(*command); |
| 262 | |
| 263 | BOOST_REQUIRE(didCallbackFire()); |
| 264 | } |
| 265 | |
| 266 | BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, |
| 267 | UnauthorizedCommandFixture<StrategyChoiceManagerFixture>) |
| 268 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 269 | ControlParameters parameters; |
| 270 | parameters.setName("/test"); |
| 271 | parameters.setStrategy("/localhost/nfd/strategy/best-route"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 272 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 273 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 274 | |
| 275 | Name commandName("/localhost/nfd/strategy-choice"); |
| 276 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 277 | commandName.append(encodedParameters); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 278 | |
| 279 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 280 | generateCommand(*command); |
| 281 | |
| 282 | getFace()->onReceiveData += |
| 283 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 284 | command->getName(), 403, "Unauthorized command"); |
| 285 | |
| 286 | getManager().onStrategyChoiceRequest(*command); |
| 287 | |
| 288 | BOOST_REQUIRE(didCallbackFire()); |
| 289 | } |
| 290 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 291 | BOOST_AUTO_TEST_CASE(UnsupportedVerb) |
| 292 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 293 | ControlParameters parameters; |
| 294 | parameters.setName("/test"); |
| 295 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 296 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 297 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 298 | |
| 299 | Name commandName("/localhost/nfd/strategy-choice"); |
| 300 | commandName.append("unsupported"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 301 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 302 | |
| 303 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 304 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 305 | |
| 306 | getFace()->onReceiveData += |
| 307 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 308 | command->getName(), 501, "Unsupported command"); |
| 309 | |
| 310 | getManager().onValidatedStrategyChoiceRequest(command); |
| 311 | |
| 312 | BOOST_REQUIRE(didCallbackFire()); |
| 313 | } |
| 314 | |
| 315 | BOOST_AUTO_TEST_CASE(BadOptionParse) |
| 316 | { |
| 317 | Name commandName("/localhost/nfd/strategy-choice"); |
| 318 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 319 | commandName.append("NotReallyParameters"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 320 | |
| 321 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 322 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 323 | |
| 324 | getFace()->onReceiveData += |
| 325 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 326 | command->getName(), 400, "Malformed command"); |
| 327 | |
| 328 | getManager().onValidatedStrategyChoiceRequest(command); |
| 329 | |
| 330 | BOOST_REQUIRE(didCallbackFire()); |
| 331 | } |
| 332 | |
| 333 | BOOST_AUTO_TEST_CASE(SetStrategies) |
| 334 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 335 | ControlParameters parameters; |
| 336 | parameters.setName("/test"); |
| 337 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 338 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 339 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 340 | |
| 341 | Name commandName("/localhost/nfd/strategy-choice"); |
| 342 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 343 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 344 | |
| 345 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 346 | |
| 347 | getFace()->onReceiveData += |
| 348 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 349 | command->getName(), 200, "Success", encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 350 | |
| 351 | getManager().onValidatedStrategyChoiceRequest(command); |
| 352 | |
| 353 | BOOST_REQUIRE(didCallbackFire()); |
| 354 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 355 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 356 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 357 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 358 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingName) |
| 359 | { |
| 360 | ControlParameters parameters; |
| 361 | parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
| 362 | |
| 363 | Block encodedParameters(parameters.wireEncode()); |
| 364 | |
| 365 | Name commandName("/localhost/nfd/strategy-choice"); |
| 366 | commandName.append("set"); |
| 367 | commandName.append(encodedParameters); |
| 368 | |
| 369 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 370 | |
| 371 | getFace()->onReceiveData += |
| 372 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 373 | command->getName(), 400, "Malformed command"); |
| 374 | |
| 375 | getManager().onValidatedStrategyChoiceRequest(command); |
| 376 | |
| 377 | BOOST_REQUIRE(didCallbackFire()); |
| 378 | } |
| 379 | |
| 380 | BOOST_AUTO_TEST_CASE(SetStrategiesMissingStrategy) |
| 381 | { |
| 382 | ControlParameters parameters; |
| 383 | parameters.setName("/test"); |
| 384 | |
| 385 | Block encodedParameters(parameters.wireEncode()); |
| 386 | |
| 387 | Name commandName("/localhost/nfd/strategy-choice"); |
| 388 | commandName.append("set"); |
| 389 | commandName.append(encodedParameters); |
| 390 | |
| 391 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 392 | |
| 393 | getFace()->onReceiveData += |
| 394 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 395 | command->getName(), 400, "Malformed command"); |
| 396 | |
| 397 | getManager().onValidatedStrategyChoiceRequest(command); |
| 398 | |
| 399 | BOOST_REQUIRE(didCallbackFire()); |
| 400 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 401 | BOOST_REQUIRE_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | BOOST_AUTO_TEST_CASE(SetUnsupportedStrategy) |
| 405 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 406 | ControlParameters parameters; |
| 407 | parameters.setName("/test"); |
| 408 | parameters.setStrategy("/localhost/nfd/strategy/unit-test-doesnotexist"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 409 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 410 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 411 | |
| 412 | Name commandName("/localhost/nfd/strategy-choice"); |
| 413 | commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 414 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 415 | |
| 416 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 417 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 418 | |
| 419 | getFace()->onReceiveData += |
| 420 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 421 | command->getName(), 504, "Unsupported strategy"); |
| 422 | |
| 423 | getManager().onValidatedStrategyChoiceRequest(command); |
| 424 | |
| 425 | BOOST_REQUIRE(didCallbackFire()); |
| 426 | fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 427 | BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 428 | } |
| 429 | |
| 430 | class DefaultStrategyOnlyFixture : public StrategyChoiceManagerFixture |
| 431 | { |
| 432 | public: |
| 433 | DefaultStrategyOnlyFixture() |
| 434 | : StrategyChoiceManagerFixture() |
| 435 | { |
| 436 | |
| 437 | } |
| 438 | |
| 439 | virtual |
| 440 | ~DefaultStrategyOnlyFixture() |
| 441 | { |
| 442 | |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | |
| 447 | /// \todo I'm not sure this code branch (code 405) can happen. The manager tests for the strategy first and will return 504. |
| 448 | // BOOST_FIXTURE_TEST_CASE(SetNotInstalled, DefaultStrategyOnlyFixture) |
| 449 | // { |
| 450 | // BOOST_REQUIRE(!getStrategyChoice().hasStrategy("/localhost/nfd/strategy/test-strategy-b")); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 451 | // ControlParameters parameters; |
| 452 | // parameters.setName("/test"); |
| 453 | // parameters.setStrategy("/localhost/nfd/strategy/test-strategy-b"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 454 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 455 | // Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 456 | |
| 457 | // Name commandName("/localhost/nfd/strategy-choice"); |
| 458 | // commandName.append("set"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 459 | // commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 460 | |
| 461 | // shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 462 | // generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 463 | |
| 464 | // getFace()->onReceiveData += |
| 465 | // bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 466 | // command->getName(), 405, "Strategy not installed"); |
| 467 | |
| 468 | // getManager().onValidatedStrategyChoiceRequest(command); |
| 469 | |
| 470 | // BOOST_REQUIRE(didCallbackFire()); |
| 471 | // fw::Strategy& strategy = getStrategyChoice().findEffectiveStrategy("/test"); |
| 472 | // BOOST_CHECK_EQUAL(strategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 473 | // } |
| 474 | |
| 475 | BOOST_AUTO_TEST_CASE(Unset) |
| 476 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 477 | ControlParameters parameters; |
| 478 | parameters.setName("/test"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 479 | |
| 480 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 481 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 482 | "/localhost/nfd/strategy/test-strategy-b"); |
| 483 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 484 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 485 | |
| 486 | Name commandName("/localhost/nfd/strategy-choice"); |
| 487 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 488 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 489 | |
| 490 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 491 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 492 | |
| 493 | getFace()->onReceiveData += |
| 494 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 495 | command->getName(), 200, "Success", encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 496 | |
| 497 | getManager().onValidatedStrategyChoiceRequest(command); |
| 498 | |
| 499 | BOOST_REQUIRE(didCallbackFire()); |
| 500 | |
| 501 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 502 | "/localhost/nfd/strategy/test-strategy-a"); |
| 503 | } |
| 504 | |
| 505 | BOOST_AUTO_TEST_CASE(UnsetRoot) |
| 506 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 507 | ControlParameters parameters; |
| 508 | parameters.setName("/"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 509 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 510 | Block encodedParameters(parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 511 | |
| 512 | Name commandName("/localhost/nfd/strategy-choice"); |
| 513 | commandName.append("unset"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 514 | commandName.append(encodedParameters); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 515 | |
| 516 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 517 | generateCommand(*command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 518 | |
| 519 | getFace()->onReceiveData += |
| 520 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 521 | command->getName(), 403, "Cannot unset root prefix strategy"); |
| 522 | |
| 523 | getManager().onValidatedStrategyChoiceRequest(command); |
| 524 | |
| 525 | BOOST_REQUIRE(didCallbackFire()); |
| 526 | |
| 527 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 528 | "/localhost/nfd/strategy/test-strategy-a"); |
| 529 | } |
| 530 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 531 | BOOST_AUTO_TEST_CASE(UnsetMissingName) |
| 532 | { |
| 533 | ControlParameters parameters; |
| 534 | |
| 535 | BOOST_REQUIRE(m_strategyChoice.insert("/test", "/localhost/nfd/strategy/test-strategy-b")); |
| 536 | BOOST_REQUIRE_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 537 | "/localhost/nfd/strategy/test-strategy-b"); |
| 538 | |
| 539 | Block encodedParameters(parameters.wireEncode()); |
| 540 | |
| 541 | Name commandName("/localhost/nfd/strategy-choice"); |
| 542 | commandName.append("unset"); |
| 543 | commandName.append(encodedParameters); |
| 544 | |
| 545 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 546 | generateCommand(*command); |
| 547 | |
| 548 | getFace()->onReceiveData += |
| 549 | bind(&StrategyChoiceManagerFixture::validateControlResponse, this, _1, |
| 550 | command->getName(), 400, "Malformed command"); |
| 551 | |
| 552 | getManager().onValidatedStrategyChoiceRequest(command); |
| 553 | |
| 554 | BOOST_REQUIRE(didCallbackFire()); |
| 555 | |
| 556 | BOOST_CHECK_EQUAL(m_strategyChoice.findEffectiveStrategy("/test").getName(), |
| 557 | "/localhost/nfd/strategy/test-strategy-b"); |
| 558 | } |
| 559 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 560 | BOOST_AUTO_TEST_CASE(Publish) |
| 561 | { |
| 562 | Name commandName("/localhost/nfd/strategy-choice/list"); |
| 563 | shared_ptr<Interest> command(make_shared<Interest>(commandName)); |
| 564 | |
| 565 | ndn::nfd::StrategyChoice expectedChoice; |
| 566 | expectedChoice.setStrategy("/localhost/nfd/strategy/test-strategy-a"); |
| 567 | expectedChoice.setName("/"); |
| 568 | |
| 569 | getFace()->onReceiveData += |
| 570 | bind(&StrategyChoiceManagerFixture::validateList, this, _1, expectedChoice); |
| 571 | |
| 572 | m_manager.onStrategyChoiceRequest(*command); |
| 573 | BOOST_REQUIRE(didCallbackFire()); |
| 574 | } |
| 575 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 576 | BOOST_AUTO_TEST_SUITE_END() |
| 577 | |
| 578 | } // namespace tests |
| 579 | } // namespace nfd |